-
Notifications
You must be signed in to change notification settings - Fork 117
/
asn1.c
380 lines (326 loc) · 9.04 KB
/
asn1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**********************************************************************
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2013 by Alain Pannetrat <[email protected]>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cardpeek. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "misc.h"
#include "asn1.h" /* includes <bytestring> */
#define ASN1_VERIFY(bs) if (bs->width!=8) { log_printf(LOG_ERROR,"ASN1 operation requires 8 bit-width bytestring"); return BYTESTRING_ERROR; }
int FORCE_SINGLE_BYTE_LENGTH = 0;
int asn1_force_single_byte_length_parsing(int enable)
{
int retval = FORCE_SINGLE_BYTE_LENGTH;
FORCE_SINGLE_BYTE_LENGTH = enable;
log_printf(LOG_INFO,
"Setting ASN1 single byte length parsing to %s",
enable?"true":"false");
return retval;
}
int asn1_skip_tlv(unsigned *pos, const bytestring_t *tlvlist)
{
unsigned tag;
if (asn1_decode_tag(pos,tlvlist,&tag)!=BYTESTRING_OK)
return BYTESTRING_ERROR;
return asn1_skip_value(pos,tlvlist);
}
int asn1_decode_tag(unsigned *pos, const bytestring_t *tlv, unsigned *tag)
{
unsigned char element;
unsigned room = bytestring_get_size(tlv);
*tag = 0;
ASN1_VERIFY(tlv);
if (room<*pos+1)
{
log_printf(LOG_WARNING,"Error decoding tag in tlv");
return BYTESTRING_ERROR;
}
room -=*pos;
bytestring_get_element(&element,tlv,(*pos)++);
*tag=element;
room--;
if ((*tag&0x1F)!=0x1F)
return BYTESTRING_OK;
do {
*tag<<=8;
bytestring_get_element(&element,tlv,(*pos)++);
*tag|=element;
room--;
} while (((*tag&0xFF)>0x80) && room>0);
if (room==0 && (*tag&0xFF)>0x80)
{
log_printf(LOG_WARNING,"Tag length error in tlv");
return BYTESTRING_ERROR;
}
return BYTESTRING_OK;
}
int asn1_decode_length(unsigned *pos, const bytestring_t *lv, unsigned* len)
{
unsigned char element;
unsigned room = bytestring_get_size(lv);
unsigned i;
*len=0;
ASN1_VERIFY(lv);
if (room<*pos+1)
{
log_printf(LOG_WARNING,"Missing tlv length");
return BYTESTRING_ERROR;
}
room -=*pos;
bytestring_get_element(&element,lv,(*pos)++);
if ((element&0x80)==0x80 && !FORCE_SINGLE_BYTE_LENGTH)
{
unsigned len_of_len=element&0x7F;
if (room<=len_of_len || len_of_len>4)
{
log_printf(LOG_WARNING,"Length of length error in tlv");
return BYTESTRING_ERROR;
}
for (i=0;i<len_of_len;i++)
{
bytestring_get_element(&element,lv,(*pos)++);
*len=(*len<<8)+element;
}
}
else
{
*len=element;
}
return BYTESTRING_OK;
}
int asn1_skip_value(unsigned *pos, const bytestring_t *lv)
{
unsigned len;
if (asn1_decode_length(pos,lv,&len)!=BYTESTRING_OK)
return BYTESTRING_ERROR;
if (*pos+len>bytestring_get_size(lv))
return BYTESTRING_ERROR;
*pos+=len;
return BYTESTRING_OK;
}
int asn1_decode_value(unsigned *pos, const bytestring_t *lv, bytestring_t *val)
{
unsigned len;
bytestring_clear(val);
if (asn1_decode_length(pos,lv,&len)!=BYTESTRING_OK)
return BYTESTRING_ERROR;
if (*pos+len>bytestring_get_size(lv))
{
log_printf(LOG_ERROR,"Value length error in tlv at position %i: expected %i, have only %i",*pos,*pos+len,bytestring_get_size(lv));
*pos+=bytestring_get_size(lv);
return BYTESTRING_ERROR;
}
/***
{
log_printf(LOG_WARNING,"Value length error in tlv at position %i: expected %i, have only %i",*pos,*pos+len,bytestring_get_size(lv));
len = bytestring_get_size(lv)-*pos;
log_printf(LOG_WARNING,"Attempting to shrink tlv length to %i, with unpredictable results",len);
}
***/
bytestring_substr(val,*pos,len,lv);
*pos+=len;
return BYTESTRING_OK;
}
int asn1_decode_tlv(unsigned *pos, const bytestring_t *tlv, unsigned *tag, bytestring_t *val)
{
if (asn1_decode_tag(pos,tlv,tag)!=BYTESTRING_OK)
return BYTESTRING_ERROR;
if (asn1_decode_value(pos,tlv,val)!=BYTESTRING_OK)
{
log_printf(LOG_WARNING,"... after parsing tag `%X'",*tag);
return BYTESTRING_ERROR;
}
return BYTESTRING_OK;
}
int asn1_encode_tag(unsigned tag, bytestring_t* bertag)
{
ASN1_VERIFY(bertag);
bytestring_clear(bertag);
if (tag>0xFFFFFF)
bytestring_pushback(bertag,tag>>24);
if (tag>0xFFFF)
bytestring_pushback(bertag,(tag>>16)&0xFF);
if (tag>0xFF)
bytestring_pushback(bertag,(tag>>8)&0xFF);
bytestring_pushback(bertag,tag&0xFF);
return BYTESTRING_OK;
}
int asn1_encode_tlv(unsigned tag, const bytestring_t *val, bytestring_t *bertlv)
{
unsigned len = bytestring_get_size(val);
if (asn1_encode_tag(tag,bertlv)!=BYTESTRING_OK)
return BYTESTRING_ERROR;
if (len>0xFFFFFF)
{
bytestring_pushback(bertlv,0x84);
bytestring_pushback(bertlv,(len>>24)&0xFF);
bytestring_pushback(bertlv,(len>>16)&0xFF);
bytestring_pushback(bertlv,(len>>8)&0xFF);
bytestring_pushback(bertlv,(len)&0xFF);
}
else if (len>0xFFFF)
{
bytestring_pushback(bertlv,0x83);
bytestring_pushback(bertlv,(len>>16)&0xFF);
bytestring_pushback(bertlv,(len>>8)&0xFF);
bytestring_pushback(bertlv,(len)&0xFF);
}
else if (len>0xFF)
{
bytestring_pushback(bertlv,0x82);
bytestring_pushback(bertlv,(len>>8)&0xFF);
bytestring_pushback(bertlv,(len)&0xFF);
}
else if (len>0x80)
{
bytestring_pushback(bertlv,0x81);
bytestring_pushback(bertlv,(len)&0xFF);
}
else
bytestring_pushback(bertlv,len&0xFF);
bytestring_append(bertlv,val);
return BYTESTRING_OK;
}
/************************************************************/
const unsigned ANY_TAG = 0xFFFFFFFF;
static int seek_index(unsigned *pos, unsigned max, const bytestring_t *data, unsigned t_index, unsigned tag)
{
unsigned cindex = 0;
unsigned ctag=0;
while (cindex<=t_index && *pos<max)
{
if (asn1_decode_tag(pos,data,&ctag)!=BYTESTRING_OK)
return BYTESTRING_ERROR;
if (tag==ANY_TAG || tag==ctag)
{
if (t_index==cindex)
return BYTESTRING_OK;
else
cindex++;
}
if (!asn1_skip_value(pos,data))
return BYTESTRING_ERROR;
}
return BYTESTRING_ERROR;
}
static const char *parse_tag(unsigned *tag, const char *s)
{
char s_pos[15];
int s_pos_i;
*tag=ANY_TAG;
if (!isxdigit(*s))
return NULL;
s_pos_i=0;
while (isxdigit(*s) && s_pos_i<14)
{
s_pos[s_pos_i++]=*s++;
}
s_pos[s_pos_i]=0;
if (!isxdigit(*s))
{
*tag=strtol(s_pos,NULL,16);
return s;
}
return NULL;
}
static const char *parse_index(unsigned *t_index, const char *s)
{
char s_index[15];
int s_index_i;
*t_index=0;
if (*s!='[')
return NULL;
s++;
s_index_i=0;
while (isdigit(*s) && s_index_i<15)
{
s_index[s_index_i++]=*s++;
}
s_index[s_index_i]=0;
if (*s==']')
{
*t_index=atoi(s_index);
return s+1;
}
return NULL;
}
static int parse_path_internal(const char* path, unsigned *pos, unsigned max, const bytestring_t *src, bytestring_t *val)
{
unsigned tag=ANY_TAG;
unsigned t_index=0;
unsigned length;
while (*path=='/') path++;
if (*path==0)
goto parse_path_fail;
if (*path=='[')
path=parse_index(&t_index,path);
else
{
path=parse_tag(&tag,path);
if (path!=NULL && *path=='[')
path=parse_index(&t_index,path);
}
if (path==NULL) /* parse error */
goto parse_path_fail;
if (seek_index(pos,max,src,t_index,tag)==BYTESTRING_OK)
{
if (*path)
{
asn1_decode_length(pos,src,&length);
return parse_path_internal(path,pos,*pos+length,src,val);
}
return asn1_decode_value(pos,src,val);
}
parse_path_fail:
bytestring_clear(val);
return BYTESTRING_ERROR;
}
int asn1_parse_path(const char* path, const bytestring_t *src, bytestring_t *val)
{
unsigned pos=0;
return parse_path_internal(path,&pos,bytestring_get_size(src),src,val);
}
/***************************************************************/
#ifdef TEST_ASN1
#include <stdio.h>
int main(int argc, char **argv)
{
const char *s = "6F 25 84 07 A0 00 00 00 03 10 10 A5 1A 50 0A 56 49 53 41 43 52 45 44 49 54 87 01 01 5F 2D 08 65 6E 7A 68 6A 61 6B 6F";
bytestring_t* t = bytestring_new_from_hex(s);
unsigned tag,len;
bytestring_t* value = bytestring_new();
unsigned pos=0;
char *tmp;
asn1_force_single_byte_length_parsing(1);
tmp = bytestring_to_hex_string(t);
fprintf(stderr,"%s\n",tmp);
free(tmp);
fprintf(stderr,"retcode=%i\n",asn1_decode_tag(&pos,t,&tag));
fprintf(stderr,"pos=%i\n",pos);
fprintf(stderr,"tag=%x\n",tag);
fprintf(stderr,"retcode=%i\n",asn1_decode_value(&pos,t,value));
fprintf(stderr,"pos=%i\n",pos);
tmp = bytestring_to_hex_string(value);
fprintf(stderr,"val=%s\n",tmp);
free(tmp);
bytestring_free(t);
bytestring_free(value);
}
#endif