gwenhywfar  5.10.1
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 {
31  GWEN_SIGTAIL *st;
32 
35 
36  return st;
37 }
38 
39 
40 
42 {
43  if (st) {
45  if (st->pSignature && st->lSignature)
46  free(st->pSignature);
47 
48  GWEN_FREE_OBJECT(st);
49  }
50 }
51 
52 
53 
54 GWEN_SIGTAIL *GWEN_SigTail_fromBuffer(const uint8_t *p, uint32_t l)
55 {
56  if (p==NULL || l<1) {
57  DBG_INFO(GWEN_LOGDOMAIN, "Bad tag");
58  return NULL;
59  }
60  else {
61  GWEN_SIGTAIL *st;
62  const uint8_t *sp;
63  uint32_t sl;
64 
65  st=GWEN_SigTail_new();
66  sp=p;
67  sl=l;
68  while (sl) {
69  GWEN_TAG16 *subtag;
70  uint32_t subtagLen;
71  const char *subtagPtr;
72  int i;
73 
74  subtag=GWEN_Tag16_fromBuffer2(sp, sl, 0);
75  if (subtag==NULL) {
76  DBG_INFO(GWEN_LOGDOMAIN, "Bad sub-tag");
78  return NULL;
79  }
80  subtagLen=GWEN_Tag16_GetTagLength(subtag);
81  subtagPtr=(const char *)GWEN_Tag16_GetTagData(subtag);
82 
83  if (subtagLen && subtagPtr) {
84  switch (GWEN_Tag16_GetTagType(subtag)) {
85  case GWEN_SIGTAIL_TLV_SIGNATURE:
86  st->pSignature=(uint8_t *)malloc(subtagLen);
87  memmove(st->pSignature, subtagPtr, subtagLen);
88  st->lSignature=subtagLen;
89  break;
90 
91  case GWEN_SIGTAIL_TLV_SIGNUM:
92  if (sscanf(subtagPtr, "%d", &i)==1)
93  st->signatureNumber=i;
94  break;
95 
96  default:
97  DBG_WARN(GWEN_LOGDOMAIN, "Unknown tag %02x", GWEN_Tag16_GetTagType(subtag));
98  }
99  }
100 
101  sp+=GWEN_Tag16_GetTagSize(subtag);
102  sl-=GWEN_Tag16_GetTagSize(subtag);
103  GWEN_Tag16_free(subtag);
104  } /* while */
105 
106  return st;
107  }
108 }
109 
110 
111 
112 int GWEN_SigTail_toBuffer(const GWEN_SIGTAIL *st, GWEN_BUFFER *buf, uint8_t tagType)
113 {
114  char numbuf[32];
115  uint32_t pos;
116  uint8_t *p;
117  uint32_t l;
118 
119  GWEN_Buffer_AppendByte(buf, tagType);
120  pos=GWEN_Buffer_GetPos(buf);
121  GWEN_Buffer_AppendByte(buf, 0);
122  GWEN_Buffer_AppendByte(buf, 0);
123 
124  if (st->pSignature && st->lSignature)
125  GWEN_Tag16_DirectlyToBuffer(GWEN_SIGTAIL_TLV_SIGNATURE,
126  (const char *)st->pSignature,
127  st->lSignature,
128  buf);
129 
130  snprintf(numbuf, sizeof(numbuf), "%d", st->signatureNumber);
131  GWEN_Tag16_DirectlyToBuffer(GWEN_SIGTAIL_TLV_SIGNUM, numbuf, -1, buf);
132 
133  /* write size */
134  l=GWEN_Buffer_GetPos(buf)-pos-2;
135  p=(uint8_t *)GWEN_Buffer_GetStart(buf)+pos;
136  *(p++)=l & 0xff;
137  *p=(l>>8) & 0xff;
138 
139  return 0;
140 }
141 
142 
143 
145 {
146  assert(st);
147  return st->pSignature;
148 }
149 
150 
151 
153 {
154  assert(st);
155  return st->lSignature;
156 }
157 
158 
159 
160 void GWEN_SigTail_SetSignature(GWEN_SIGTAIL *st, const uint8_t *p, uint32_t l)
161 {
162  assert(st);
163  if (st->pSignature && st->lSignature)
164  free(st->pSignature);
165  if (p && l) {
166  st->pSignature=(uint8_t *)malloc(l);
167  memmove(st->pSignature, p, l);
168  st->lSignature=l;
169  }
170  else {
171  st->pSignature=NULL;
172  st->lSignature=0;
173  }
174 }
175 
176 
177 
179 {
180  assert(st);
181  return st->signatureNumber;
182 }
183 
184 
185 
187 {
188  assert(st);
189  st->signatureNumber=i;
190 }
191 
192 
193 
194 
195 
196 
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:235
int GWEN_SigTail_GetSignatureNumber(const GWEN_SIGTAIL *st)
Definition: sigtail.c:178
void GWEN_SigTail_SetSignatureNumber(GWEN_SIGTAIL *st, int i)
Definition: sigtail.c:186
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define NULL
Definition: binreloc.c:300
#define DBG_WARN(dbg_logger, format, args...)
Definition: debug.h:125
void GWEN_SigTail_SetSignature(GWEN_SIGTAIL *st, const uint8_t *p, uint32_t l)
Definition: sigtail.c:160
#define GWEN_LOGDOMAIN
Definition: logger.h:35
unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tlv)
Definition: tag16.c:72
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition: buffer.c:253
struct GWEN_TAG16 GWEN_TAG16
Definition: tag16.h:18
void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType, const char *p, int size, GWEN_BUFFER *buf)
Definition: tag16.c:213
GWEN_SIGTAIL * GWEN_SigTail_new(void)
Definition: sigtail.c:29
const void * GWEN_Tag16_GetTagData(const GWEN_TAG16 *tlv)
Definition: tag16.c:80
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
struct GWEN_SIGTAIL GWEN_SIGTAIL
Definition: sigtail.h:24
const uint8_t * GWEN_SigTail_GetSignaturePtr(const GWEN_SIGTAIL *st)
Definition: sigtail.c:144
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition: buffer.c:394
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:38
unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tlv)
Definition: tag16.c:64
unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tlv)
Definition: tag16.c:56
int GWEN_SigTail_toBuffer(const GWEN_SIGTAIL *st, GWEN_BUFFER *buf, uint8_t tagType)
Definition: sigtail.c:112
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
uint32_t GWEN_SigTail_GetSignatureLen(const GWEN_SIGTAIL *st)
Definition: sigtail.c:152
#define GWEN_LIST_INIT(t, element)
Definition: list1.h:465
GWEN_SIGTAIL * GWEN_SigTail_fromBuffer(const uint8_t *p, uint32_t l)
Definition: sigtail.c:54
#define GWEN_LIST_FUNCTIONS(t, pr)
Definition: list1.h:366
void GWEN_SigTail_free(GWEN_SIGTAIL *st)
Definition: sigtail.c:41
void GWEN_Tag16_free(GWEN_TAG16 *tlv)
Definition: tag16.c:44
#define GWEN_LIST_FINI(t, element)
Definition: list1.h:474
GWEN_TAG16 * GWEN_Tag16_fromBuffer2(const uint8_t *p, uint32_t l, int doCopy)
Definition: tag16.c:151