gwenhywfar  4.3.3
crypthead.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Mon Dec 01 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 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #define DISABLE_DEBUGLOG
15 
16 
17 #include "crypthead_p.h"
18 #include "i18n_l.h"
19 #include <gwenhywfar/misc.h>
20 #include <gwenhywfar/debug.h>
21 #include <gwenhywfar/tag16.h>
22 
23 
24 GWEN_LIST_FUNCTIONS(GWEN_CRYPTHEAD, GWEN_CryptHead)
25 
26 
28  GWEN_CRYPTHEAD *ch;
29 
31 
32  return ch;
33 }
34 
35 
36 
38  if (ch) {
39  free(ch->keyName);
40  if (ch->pKey &&ch->lKey)
41  free(ch->pKey);
42  GWEN_FREE_OBJECT(ch);
43  }
44 }
45 
46 
47 
48 GWEN_CRYPTHEAD *GWEN_CryptHead_fromBuffer(const uint8_t *p, uint32_t l) {
49  if (p==NULL || l<1) {
50  DBG_INFO(GWEN_LOGDOMAIN, "Bad tag");
51  return NULL;
52  }
53  else {
54  GWEN_CRYPTHEAD *ch;
55  const uint8_t *sp;
56  uint32_t sl;
57 
58  ch=GWEN_CryptHead_new();
59  sp=p;
60  sl=l;
61  while(sl) {
62  GWEN_TAG16 *subtag;
63  uint32_t subtagLen;
64  const char *subtagPtr;
65  int i;
66 
67  subtag=GWEN_Tag16_fromBuffer2(sp, sl, 0);
68  if (subtag==NULL) {
69  DBG_INFO(GWEN_LOGDOMAIN, "Bad sub-tag");
71  return NULL;
72  }
73  subtagLen=GWEN_Tag16_GetTagLength(subtag);
74  subtagPtr=(const char*)GWEN_Tag16_GetTagData(subtag);
75 
76  if (subtagLen && subtagPtr) {
77  switch(GWEN_Tag16_GetTagType(subtag)) {
78 
79  case GWEN_CRYPTHEAD_TLV_KEYNAME:
80  ch->keyName=(char*)malloc(subtagLen+1);
81  memmove(ch->keyName, subtagPtr, subtagLen);
82  ch->keyName[subtagLen]=0;
83  break;
84 
85  case GWEN_CRYPTHEAD_TLV_KEYNUM:
86  if (sscanf(subtagPtr, "%d", &i)==1)
87  ch->keyNumber=i;
88  break;
89 
90  case GWEN_CRYPTHEAD_TLV_KEYVER:
91  if (sscanf(subtagPtr, "%d", &i)==1)
92  ch->keyVersion=i;
93  break;
94 
95  case GWEN_CRYPTHEAD_TLV_KEY:
96  ch->pKey=(uint8_t*)malloc(subtagLen);
97  assert(ch->pKey);
98  memmove(ch->pKey, subtagPtr, subtagLen);
99  ch->lKey=subtagLen;
100  break;
101 
102  case GWEN_CRYPTHEAD_TLV_CRYPTPROFILE:
103  if (sscanf(subtagPtr, "%d", &i)==1)
104  ch->cryptProfile=i;
105  break;
106 
107  default:
108  DBG_WARN(GWEN_LOGDOMAIN, "Unknown tag %02x", GWEN_Tag16_GetTagType(subtag));
109  }
110  }
111 
112  sp+=GWEN_Tag16_GetTagSize(subtag);
113  sl-=GWEN_Tag16_GetTagSize(subtag);
114  GWEN_Tag16_free(subtag);
115  } /* while */
116 
117  return ch;
118  }
119 
120 }
121 
122 
123 
124 int GWEN_CryptHead_toBuffer(const GWEN_CRYPTHEAD *ch, GWEN_BUFFER *buf, uint8_t tagType) {
125  char numbuf[32];
126  uint32_t pos;
127  uint8_t *p;
128  uint32_t l;
129 
130  GWEN_Buffer_AppendByte(buf, tagType);
131  pos=GWEN_Buffer_GetPos(buf);
132  GWEN_Buffer_AppendByte(buf, 0);
133  GWEN_Buffer_AppendByte(buf, 0);
134 
135  if (ch->keyName)
136  GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYNAME, ch->keyName, -1, buf);
137 
138  snprintf(numbuf, sizeof(numbuf), "%d", ch->keyNumber);
139  GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYNUM, numbuf, -1, buf);
140 
141  snprintf(numbuf, sizeof(numbuf), "%d", ch->keyVersion);
142  GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYVER, numbuf, -1, buf);
143  if (ch->pKey && ch->lKey)
144  GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEY,
145  (const char*)ch->pKey,
146  ch->lKey,
147  buf);
148 
149  snprintf(numbuf, sizeof(numbuf), "%d", ch->cryptProfile);
150  GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_CRYPTPROFILE, numbuf, -1, buf);
151 
152  /* write size */
153  l=GWEN_Buffer_GetPos(buf)-pos-2;
154  p=(uint8_t*)GWEN_Buffer_GetStart(buf)+pos;
155  *(p++)=l & 0xff;
156  *p=(l>>8) & 0xff;
157 
158  return 0;
159 }
160 
161 
162 
164  assert(ch);
165  return ch->keyName;
166 }
167 
168 
169 
170 void GWEN_CryptHead_SetKeyName(GWEN_CRYPTHEAD *ch, const char *s) {
171  assert(ch);
172  free(ch->keyName);
173  if (s) ch->keyName=strdup(s);
174  else ch->keyName=NULL;
175 }
176 
177 
178 
180  assert(ch);
181  return ch->keyNumber;
182 }
183 
184 
185 
187  assert(ch);
188  ch->keyNumber=i;
189 }
190 
191 
192 
194  assert(ch);
195  return ch->keyVersion;
196 }
197 
198 
199 
201  assert(ch);
202  ch->keyVersion=i;
203 }
204 
205 
206 
208  assert(ch);
209  return ch->cryptProfile;
210 }
211 
212 
213 
215  assert(ch);
216  ch->cryptProfile=i;
217 }
218 
219 
220 
221 const uint8_t *GWEN_CryptHead_GetKeyPtr(const GWEN_CRYPTHEAD *ch) {
222  assert(ch);
223  return ch->pKey;
224 }
225 
226 
227 
229  assert(ch);
230  return ch->lKey;
231 }
232 
233 
234 
235 void GWEN_CryptHead_SetKey(GWEN_CRYPTHEAD *ch, const uint8_t *p, uint32_t l) {
236  assert(ch);
237  if (ch->pKey && ch->lKey)
238  free(ch->pKey);
239  if (p && l) {
240  ch->pKey=(uint8_t*)malloc(l);
241  assert(ch->pKey);
242  memmove(ch->pKey, p, l);
243  ch->lKey=l;
244  }
245  else {
246  ch->pKey=NULL;
247  ch->lKey=0;
248  }
249 }
250 
251 
252 
253