gwenhywfar  4.7.0beta
sigtail.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Sun Nov 30 2008
3  copyright : (C) 2008 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 
15 #define DISABLE_DEBUGLOG
16 
17 
18 #include "sigtail_p.h"
19 #include "i18n_l.h"
20 #include <gwenhywfar/misc.h>
21 #include <gwenhywfar/debug.h>
22 #include <gwenhywfar/tag16.h>
23 
24 
25 GWEN_LIST_FUNCTIONS(GWEN_SIGTAIL, GWEN_SigTail)
26 
27 
28 
30  GWEN_SIGTAIL *st;
31 
34 
35  return st;
36 }
37 
38 
39 
41  if (st) {
43  if (st->pSignature && st->lSignature)
44  free(st->pSignature);
45 
46  GWEN_FREE_OBJECT(st);
47  }
48 }
49 
50 
51 
52 GWEN_SIGTAIL *GWEN_SigTail_fromBuffer(const uint8_t *p, uint32_t l) {
53  if (p==NULL || l<1) {
54  DBG_INFO(GWEN_LOGDOMAIN, "Bad tag");
55  return NULL;
56  }
57  else {
58  GWEN_SIGTAIL *st;
59  const uint8_t *sp;
60  uint32_t sl;
61 
62  st=GWEN_SigTail_new();
63  sp=p;
64  sl=l;
65  while(sl) {
66  GWEN_TAG16 *subtag;
67  uint32_t subtagLen;
68  const char *subtagPtr;
69  int i;
70 
71  subtag=GWEN_Tag16_fromBuffer2(sp, sl, 0);
72  if (subtag==NULL) {
73  DBG_INFO(GWEN_LOGDOMAIN, "Bad sub-tag");
75  return NULL;
76  }
77  subtagLen=GWEN_Tag16_GetTagLength(subtag);
78  subtagPtr=(const char*)GWEN_Tag16_GetTagData(subtag);
79 
80  if (subtagLen && subtagPtr) {
81  switch(GWEN_Tag16_GetTagType(subtag)) {
82  case GWEN_SIGTAIL_TLV_SIGNATURE:
83  st->pSignature=(uint8_t*)malloc(subtagLen);
84  memmove(st->pSignature, subtagPtr, subtagLen);
85  st->lSignature=subtagLen;
86  break;
87 
88  case GWEN_SIGTAIL_TLV_SIGNUM:
89  if (sscanf(subtagPtr, "%d", &i)==1)
90  st->signatureNumber=i;
91  break;
92 
93  default:
94  DBG_WARN(GWEN_LOGDOMAIN, "Unknown tag %02x", GWEN_Tag16_GetTagType(subtag));
95  }
96  }
97 
98  sp+=GWEN_Tag16_GetTagSize(subtag);
99  sl-=GWEN_Tag16_GetTagSize(subtag);
100  GWEN_Tag16_free(subtag);
101  } /* while */
102 
103  return st;
104  }
105 }
106 
107 
108 
109 int GWEN_SigTail_toBuffer(const GWEN_SIGTAIL *st, GWEN_BUFFER *buf, uint8_t tagType) {
110  char numbuf[32];
111  uint32_t pos;
112  uint8_t *p;
113  uint32_t l;
114 
115  GWEN_Buffer_AppendByte(buf, tagType);
116  pos=GWEN_Buffer_GetPos(buf);
117  GWEN_Buffer_AppendByte(buf, 0);
118  GWEN_Buffer_AppendByte(buf, 0);
119 
120  if (st->pSignature && st->lSignature)
121  GWEN_Tag16_DirectlyToBuffer(GWEN_SIGTAIL_TLV_SIGNATURE,
122  (const char*)st->pSignature,
123  st->lSignature,
124  buf);
125 
126  snprintf(numbuf, sizeof(numbuf), "%d", st->signatureNumber);
127  GWEN_Tag16_DirectlyToBuffer(GWEN_SIGTAIL_TLV_SIGNUM, numbuf, -1, buf);
128 
129  /* write size */
130  l=GWEN_Buffer_GetPos(buf)-pos-2;
131  p=(uint8_t*)GWEN_Buffer_GetStart(buf)+pos;
132  *(p++)=l & 0xff;
133  *p=(l>>8) & 0xff;
134 
135  return 0;
136 }
137 
138 
139 
140 const uint8_t *GWEN_SigTail_GetSignaturePtr(const GWEN_SIGTAIL *st) {
141  assert(st);
142  return st->pSignature;
143 }
144 
145 
146 
148  assert(st);
149  return st->lSignature;
150 }
151 
152 
153 
154 void GWEN_SigTail_SetSignature(GWEN_SIGTAIL *st, const uint8_t *p, uint32_t l) {
155  assert(st);
156  if (st->pSignature && st->lSignature)
157  free(st->pSignature);
158  if (p && l) {
159  st->pSignature=(uint8_t*)malloc(l);
160  memmove(st->pSignature, p, l);
161  st->lSignature=l;
162  }
163  else {
164  st->pSignature=NULL;
165  st->lSignature=0;
166  }
167 }
168 
169 
170 
172  assert(st);
173  return st->signatureNumber;
174 }
175 
176 
177 
179  assert(st);
180  st->signatureNumber=i;
181 }
182 
183 
184 
185 
186 
187