gwenhywfar 5.14.1
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
25
26
28{
30
32
33 return ch;
34}
35
36
37
39{
40 if (ch) {
41 free(ch->keyName);
42 if (ch->pKey &&ch->lKey)
43 free(ch->pKey);
45 }
46}
47
48
49
50GWEN_CRYPTHEAD *GWEN_CryptHead_fromBuffer(const uint8_t *p, uint32_t l)
51{
52 if (p==NULL || l<1) {
53 DBG_INFO(GWEN_LOGDOMAIN, "Bad tag");
54 return NULL;
55 }
56 else {
58 const uint8_t *sp;
59 uint32_t sl;
60
62 sp=p;
63 sl=l;
64 while (sl) {
65 GWEN_TAG16 *subtag;
66 uint32_t subtagLen;
67 const char *subtagPtr;
68 int i;
69
70 subtag=GWEN_Tag16_fromBuffer2(sp, sl, 0);
71 if (subtag==NULL) {
72 DBG_INFO(GWEN_LOGDOMAIN, "Bad sub-tag");
74 return NULL;
75 }
76 subtagLen=GWEN_Tag16_GetTagLength(subtag);
77 subtagPtr=(const char *)GWEN_Tag16_GetTagData(subtag);
78
79 if (subtagLen && subtagPtr) {
80 switch (GWEN_Tag16_GetTagType(subtag)) {
81
82 case GWEN_CRYPTHEAD_TLV_KEYNAME:
83 ch->keyName=(char *)malloc(subtagLen+1);
84 memmove(ch->keyName, subtagPtr, subtagLen);
85 ch->keyName[subtagLen]=0;
86 break;
87
88 case GWEN_CRYPTHEAD_TLV_KEYNUM:
89 if (sscanf(subtagPtr, "%d", &i)==1)
90 ch->keyNumber=i;
91 break;
92
93 case GWEN_CRYPTHEAD_TLV_KEYVER:
94 if (sscanf(subtagPtr, "%d", &i)==1)
95 ch->keyVersion=i;
96 break;
97
98 case GWEN_CRYPTHEAD_TLV_KEY:
99 ch->pKey=(uint8_t *)malloc(subtagLen);
100 assert(ch->pKey);
101 memmove(ch->pKey, subtagPtr, subtagLen);
102 ch->lKey=subtagLen;
103 break;
104
105 case GWEN_CRYPTHEAD_TLV_CRYPTPROFILE:
106 if (sscanf(subtagPtr, "%d", &i)==1)
107 ch->cryptProfile=i;
108 break;
109
110 default:
111 DBG_WARN(GWEN_LOGDOMAIN, "Unknown tag %02x", GWEN_Tag16_GetTagType(subtag));
112 }
113 }
114
115 sp+=GWEN_Tag16_GetTagSize(subtag);
116 sl-=GWEN_Tag16_GetTagSize(subtag);
117 GWEN_Tag16_free(subtag);
118 } /* while */
119
120 return ch;
121 }
122
123}
124
125
126
127int GWEN_CryptHead_toBuffer(const GWEN_CRYPTHEAD *ch, GWEN_BUFFER *buf, uint8_t tagType)
128{
129 char numbuf[32];
130 uint32_t pos;
131 uint8_t *p;
132 uint32_t l;
133
134 GWEN_Buffer_AppendByte(buf, tagType);
135 pos=GWEN_Buffer_GetPos(buf);
138
139 if (ch->keyName)
140 GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYNAME, ch->keyName, -1, buf);
141
142 snprintf(numbuf, sizeof(numbuf), "%d", ch->keyNumber);
143 GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYNUM, numbuf, -1, buf);
144
145 snprintf(numbuf, sizeof(numbuf), "%d", ch->keyVersion);
146 GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYVER, numbuf, -1, buf);
147 if (ch->pKey && ch->lKey)
148 GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEY,
149 (const char *)ch->pKey,
150 ch->lKey,
151 buf);
152
153 snprintf(numbuf, sizeof(numbuf), "%d", ch->cryptProfile);
154 GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_CRYPTPROFILE, numbuf, -1, buf);
155
156 /* write size */
157 l=GWEN_Buffer_GetPos(buf)-pos-2;
158 p=(uint8_t *)GWEN_Buffer_GetStart(buf)+pos;
159 *(p++)=l & 0xff;
160 *p=(l>>8) & 0xff;
161
162 return 0;
163}
164
165
166
168{
169 assert(ch);
170 return ch->keyName;
171}
172
173
174
176{
177 assert(ch);
178 free(ch->keyName);
179 if (s)
180 ch->keyName=strdup(s);
181 else
182 ch->keyName=NULL;
183}
184
185
186
188{
189 assert(ch);
190 return ch->keyNumber;
191}
192
193
194
196{
197 assert(ch);
198 ch->keyNumber=i;
199}
200
201
202
204{
205 assert(ch);
206 return ch->keyVersion;
207}
208
209
210
212{
213 assert(ch);
214 ch->keyVersion=i;
215}
216
217
218
220{
221 assert(ch);
222 return ch->cryptProfile;
223}
224
225
226
228{
229 assert(ch);
230 ch->cryptProfile=i;
231}
232
233
234
236{
237 assert(ch);
238 return ch->pKey;
239}
240
241
242
244{
245 assert(ch);
246 return ch->lKey;
247}
248
249
250
251void GWEN_CryptHead_SetKey(GWEN_CRYPTHEAD *ch, const uint8_t *p, uint32_t l)
252{
253 assert(ch);
254 if (ch->pKey && ch->lKey)
255 free(ch->pKey);
256 if (p && l) {
257 ch->pKey=(uint8_t *)malloc(l);
258 assert(ch->pKey);
259 memmove(ch->pKey, p, l);
260 ch->lKey=l;
261 }
262 else {
263 ch->pKey=NULL;
264 ch->lKey=0;
265 }
266}
267
268
269
270
#define NULL
Definition binreloc.c:300
uint32_t GWEN_Buffer_GetPos(const GWEN_BUFFER *bf)
Definition buffer.c:253
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition buffer.c:235
int GWEN_Buffer_AppendByte(GWEN_BUFFER *bf, char c)
Definition buffer.c:393
GWEN_CRYPTHEAD * GWEN_CryptHead_new(void)
Definition crypthead.c:27
void GWEN_CryptHead_SetKey(GWEN_CRYPTHEAD *ch, const uint8_t *p, uint32_t l)
Definition crypthead.c:251
int GWEN_CryptHead_toBuffer(const GWEN_CRYPTHEAD *ch, GWEN_BUFFER *buf, uint8_t tagType)
Definition crypthead.c:127
void GWEN_CryptHead_free(GWEN_CRYPTHEAD *ch)
Definition crypthead.c:38
GWEN_CRYPTHEAD * GWEN_CryptHead_fromBuffer(const uint8_t *p, uint32_t l)
Definition crypthead.c:50
int GWEN_CryptHead_GetCryptProfile(const GWEN_CRYPTHEAD *ch)
Definition crypthead.c:219
int GWEN_CryptHead_GetKeyVersion(const GWEN_CRYPTHEAD *ch)
Definition crypthead.c:203
const char * GWEN_CryptHead_GetKeyName(const GWEN_CRYPTHEAD *ch)
Definition crypthead.c:167
int GWEN_CryptHead_GetKeyNumber(const GWEN_CRYPTHEAD *ch)
Definition crypthead.c:187
void GWEN_CryptHead_SetKeyVersion(GWEN_CRYPTHEAD *ch, int i)
Definition crypthead.c:211
uint32_t GWEN_CryptHead_GetKeyLen(const GWEN_CRYPTHEAD *ch)
Definition crypthead.c:243
const uint8_t * GWEN_CryptHead_GetKeyPtr(const GWEN_CRYPTHEAD *ch)
Definition crypthead.c:235
void GWEN_CryptHead_SetKeyName(GWEN_CRYPTHEAD *ch, const char *s)
Definition crypthead.c:175
void GWEN_CryptHead_SetKeyNumber(GWEN_CRYPTHEAD *ch, int i)
Definition crypthead.c:195
void GWEN_CryptHead_SetCryptProfile(GWEN_CRYPTHEAD *ch, int i)
Definition crypthead.c:227
struct GWEN_CRYPTHEAD GWEN_CRYPTHEAD
Definition crypthead.h:23
#define DBG_INFO(dbg_logger, format,...)
Definition debug.h:181
#define DBG_WARN(dbg_logger, format,...)
Definition debug.h:125
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition buffer.h:38
#define GWEN_LIST_FUNCTIONS(t, pr)
Definition list1.h:367
#define GWEN_LOGDOMAIN
Definition logger.h:32
#define GWEN_FREE_OBJECT(varname)
Definition memory.h:61
#define GWEN_NEW_OBJECT(typ, varname)
Definition memory.h:55
void GWEN_Tag16_DirectlyToBuffer(unsigned int tagType, const char *p, int size, GWEN_BUFFER *buf)
Definition tag16.c:240
unsigned int GWEN_Tag16_GetTagLength(const GWEN_TAG16 *tag)
Definition tag16.c:122
const void * GWEN_Tag16_GetTagData(const GWEN_TAG16 *tag)
Definition tag16.c:136
void GWEN_Tag16_free(GWEN_TAG16 *tag)
Definition tag16.c:103
unsigned int GWEN_Tag16_GetTagType(const GWEN_TAG16 *tag)
Definition tag16.c:115
GWEN_TAG16 * GWEN_Tag16_fromBuffer2(const uint8_t *bufferPtr, uint32_t bufferLen, int doCopy)
Definition tag16.c:156
unsigned int GWEN_Tag16_GetTagSize(const GWEN_TAG16 *tag)
Definition tag16.c:129
struct GWEN_TAG16 GWEN_TAG16
Definition tag16.h:35