-
Notifications
You must be signed in to change notification settings - Fork 9
/
asn1-x509.c
483 lines (388 loc) · 10.8 KB
/
asn1-x509.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
* Basic implementation of ITU-T X.690 (07/2002) for parsing BER encoded
* X.509 certificates
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_STDARG_H
# include <stdarg.h>
#endif
#ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include "asn1-x509.h"
struct asn1_object {
unsigned long tag;
unsigned long size;
void *contents;
unsigned long asn1rep_len;
void *asn1rep;
};
struct x509_object {
struct asn1_object wholething;
struct asn1_object certificate;
struct asn1_object version;
struct asn1_object serial_number;
struct asn1_object signature_algo;
struct asn1_object issuer;
struct asn1_object validity;
struct asn1_object subject;
struct asn1_object pubkeyinfo;
struct asn1_object pubkey_algoid;
struct asn1_object pubkey_algo;
struct asn1_object pubkey_algoparm;
struct asn1_object pubkey;
};
static int _asn1_x509_read_asn1_object(unsigned char *buf, size_t buflen, va_list *args) {
unsigned char small_object_size;
unsigned char *buf_p;
struct asn1_object *outbuf;
outbuf = va_arg(*args, struct asn1_object *);
if (outbuf == NULL) {
return(0);
}
if (buflen == 0) {
return(-1);
}
buf_p = buf;
outbuf->tag = *buf_p;
buf_p++;
buflen--;
<<<<<<< HEAD
if (buflen == 0) {
return(-1);
}
/* NULL Tag -- no size is required */
if (outbuf->tag == 0x00) {
return(_asn1_x509_read_asn1_object(buf_p, buflen, args));
}
=======
/* NULL Tag -- no size is required */
if (outbuf->tag == 0x00) {
outbuf->size = 0;
outbuf->asn1rep_len = 1;
outbuf->asn1rep = buf;
return(_asn1_x509_read_asn1_object(buf_p, buflen, args));
}
if (buflen == 0) {
return(-1);
}
>>>>>>> trunk
small_object_size = *buf_p;
buf_p++;
buflen--;
if (buflen == 0) {
return(-1);
}
if ((small_object_size & 0x80) == 0x80) {
outbuf->size = 0;
for (small_object_size ^= 0x80; small_object_size; small_object_size--) {
outbuf->size <<= 8;
outbuf->size += *buf_p;
buf_p++;
buflen--;
<<<<<<< HEAD
=======
>>>>>>> trunk
if (buflen == 0) {
break;
}
}
} else {
outbuf->size = small_object_size;
}
if (outbuf->size > buflen) {
return(-1);
}
<<<<<<< HEAD
outbuf->contents = buf_p;
=======
if (buflen != 0) {
outbuf->contents = buf_p;
}
>>>>>>> trunk
outbuf->asn1rep_len = outbuf->size + (buf_p - buf);
outbuf->asn1rep = buf;
buf_p += outbuf->size;
buflen -= outbuf->size;
return(_asn1_x509_read_asn1_object(buf_p, buflen, args));
}
static int asn1_x509_read_asn1_object(unsigned char *buf, size_t buflen, ...) {
va_list args;
int retval;
va_start(args, buflen);
retval = _asn1_x509_read_asn1_object(buf, buflen, &args);
va_end(args);
return(retval);
}
static int asn1_x509_read_object(unsigned char *buf, size_t buflen, struct x509_object *outbuf) {
int read_ret;
read_ret = asn1_x509_read_asn1_object(buf, buflen, &outbuf->wholething, NULL);
if (read_ret != 0) {
<<<<<<< HEAD
=======
CACKEY_DEBUG_PRINTF("Failed at reading the contents from the wrapper")
>>>>>>> trunk
return(-1);
}
read_ret = asn1_x509_read_asn1_object(outbuf->wholething.contents, outbuf->wholething.size, &outbuf->certificate, NULL);
if (read_ret != 0) {
<<<<<<< HEAD
=======
CACKEY_DEBUG_PRINTF("Failed at reading the certificate from the contents");
>>>>>>> trunk
return(-1);
}
read_ret = asn1_x509_read_asn1_object(outbuf->certificate.contents, outbuf->certificate.size, &outbuf->version, &outbuf->serial_number, &outbuf->signature_algo, &outbuf->issuer, &outbuf->validity, &outbuf->subject, &outbuf->pubkeyinfo, NULL);
if (read_ret != 0) {
<<<<<<< HEAD
=======
CACKEY_DEBUG_PRINTF("Failed at reading the certificate components from the certificate");
>>>>>>> trunk
return(-1);
}
read_ret = asn1_x509_read_asn1_object(outbuf->pubkeyinfo.contents, outbuf->pubkeyinfo.size, &outbuf->pubkey_algoid, &outbuf->pubkey, NULL);
if (read_ret != 0) {
<<<<<<< HEAD
=======
CACKEY_DEBUG_PRINTF("Failed at reading the public key from the certificate components");
>>>>>>> trunk
return(-1);
}
return(0);
}
ssize_t x509_to_issuer(void *x509_der_buf, size_t x509_der_buf_len, void **outbuf) {
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
return(-1);
}
if (outbuf) {
*outbuf = x509.issuer.asn1rep;
}
return(x509.issuer.asn1rep_len);
}
ssize_t x509_to_subject(void *x509_der_buf, size_t x509_der_buf_len, void **outbuf) {
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
return(-1);
}
if (outbuf) {
*outbuf = x509.subject.asn1rep;
}
return(x509.subject.asn1rep_len);
}
static ssize_t x509_to_serial(void *x509_der_buf, size_t x509_der_buf_len, void **outbuf) {
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
<<<<<<< HEAD
=======
CACKEY_DEBUG_PRINTF("Unable to read serial number from a %lu byte buffer", x509_der_buf_len);
CACKEY_DEBUG_PRINTBUF("X.509 DER:", x509_der_buf, x509_der_buf_len);
>>>>>>> trunk
return(-1);
}
if (outbuf) {
*outbuf = x509.serial_number.asn1rep;
}
return(x509.serial_number.asn1rep_len);
}
static ssize_t x509_to_pubkey(void *x509_der_buf, size_t x509_der_buf_len, void **outbuf) {
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
return(-1);
}
*outbuf = x509.pubkey.contents;
return(x509.pubkey.size);
}
static ssize_t x509_to_modulus(void *x509_der_buf, size_t x509_der_buf_len, void **outbuf) {
struct asn1_object null, pubkey, modulus, exponent;
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
return(-1);
}
/* The structure of "pubkey" is specified in PKCS #1 */
read_ret = asn1_x509_read_asn1_object(x509.pubkey.contents, x509.pubkey.size, &null, &pubkey, NULL);
if (read_ret != 0) {
return(-1);
}
read_ret = asn1_x509_read_asn1_object(pubkey.contents, pubkey.size, &modulus, &exponent, NULL);
if (read_ret != 0) {
return(-1);
}
if (outbuf) {
*outbuf = modulus.contents;
}
return(modulus.size);
}
static ssize_t x509_to_exponent(void *x509_der_buf, size_t x509_der_buf_len, void **outbuf) {
struct asn1_object null, pubkey, modulus, exponent;
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
return(-1);
}
/* The structure of "pubkey" is specified in PKCS #1 */
read_ret = asn1_x509_read_asn1_object(x509.pubkey.contents, x509.pubkey.size, &null, &pubkey, NULL);
if (read_ret != 0) {
return(-1);
}
read_ret = asn1_x509_read_asn1_object(pubkey.contents, pubkey.size, &modulus, &exponent, NULL);
if (read_ret != 0) {
return(-1);
}
if (outbuf) {
*outbuf = exponent.contents;
}
return(exponent.size);
}
static ssize_t x509_to_keysize(void *x509_der_buf, size_t x509_der_buf_len) {
struct asn1_object null, pubkey, modulus, exponent;
struct x509_object x509;
int read_ret;
read_ret = asn1_x509_read_object(x509_der_buf, x509_der_buf_len, &x509);
if (read_ret != 0) {
return(-1);
}
/* The structure of "pubkey" is specified in PKCS #1 */
read_ret = asn1_x509_read_asn1_object(x509.pubkey.contents, x509.pubkey.size, &null, &pubkey, NULL);
if (read_ret != 0) {
return(-1);
}
read_ret = asn1_x509_read_asn1_object(pubkey.contents, pubkey.size, &modulus, &exponent, NULL);
if (read_ret != 0) {
return(-1);
}
return(modulus.size - 1);
}
/*
* http://www.blackberry.com/developers/docs/4.6.0api/javax/microedition/pki/Certificate.html
*/
static const char *_x509_objectid_to_label_string(void *buf, size_t buflen) {
switch (buflen) {
case 3:
if (memcmp(buf, "\x55\x04\x03", 3) == 0) {
return("CN");
}
if (memcmp(buf, "\x55\x04\x04", 3) == 0) {
return("SN");
}
if (memcmp(buf, "\x55\x04\x06", 3) == 0) {
return("C");
}
if (memcmp(buf, "\x55\x04\x07", 3) == 0) {
return("L");
}
if (memcmp(buf, "\x55\x04\x08", 3) == 0) {
return("ST");
}
if (memcmp(buf, "\x55\x04\x09", 3) == 0) {
return("STREET");
}
if (memcmp(buf, "\x55\x04\x0A", 3) == 0) {
return("O");
}
if (memcmp(buf, "\x55\x04\x0B", 3) == 0) {
return("OU");
}
break;
case 9:
if (memcmp(buf, "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01", 9) == 0) {
return("EmailAddress");
}
break;
}
return("???");
}
static ssize_t x509_dn_to_string(void *asn1_der_buf, size_t asn1_der_buf_len, char *outbuf, size_t outbuf_len, char *matchlabel) {
struct asn1_object whole_thing, current_set, current_seq;
struct asn1_object label, value;
const char *label_str;
ssize_t snprintf_ret, retval;
char *outbuf_s;
int read_ret;
int offset;
if (outbuf == NULL) {
return(-1);
}
if (outbuf_len == 0 || asn1_der_buf_len == 0 || asn1_der_buf == NULL) {
return(0);
}
read_ret = asn1_x509_read_asn1_object(asn1_der_buf, asn1_der_buf_len, &whole_thing, NULL);
if (read_ret != 0) {
return(-1);
}
/* Terminate string, in case no valid elements are found we still return a valid string */
*outbuf = '\0';
outbuf_s = outbuf;
offset = 0;
while (1) {
<<<<<<< HEAD
read_ret = asn1_x509_read_asn1_object(whole_thing.contents + offset, whole_thing.size - offset, ¤t_set, NULL);
=======
read_ret = asn1_x509_read_asn1_object(((unsigned char *) whole_thing.contents) + offset, whole_thing.size - offset, ¤t_set, NULL);
>>>>>>> trunk
if (read_ret != 0) {
break;
}
offset += current_set.size + 2;
read_ret = asn1_x509_read_asn1_object(current_set.contents, current_set.size, ¤t_seq, NULL);
if (read_ret != 0) {
break;
}
read_ret = asn1_x509_read_asn1_object(current_seq.contents, current_seq.size, &label, &value, NULL);
label_str = _x509_objectid_to_label_string(label.contents, label.size);
/* If the user requested only certain labels, exclude others */
if (matchlabel) {
if (strcmp(matchlabel, label_str) != 0) {
continue;
}
}
/* If the user requested only certain labels, don't include them in the reply */
if (matchlabel) {
snprintf_ret = snprintf(outbuf, outbuf_len, "%.*s, ", (unsigned int) value.size, (char *) value.contents);
} else {
snprintf_ret = snprintf(outbuf, outbuf_len, "%s=%.*s, ", label_str, (unsigned int) value.size, (char *) value.contents);
}
if (snprintf_ret < 0) {
break;
}
if (snprintf_ret > outbuf_len) {
snprintf_ret = outbuf_len;
}
outbuf += snprintf_ret;
outbuf_len -= snprintf_ret;
if (outbuf_len < 2) {
break;
}
}
retval = outbuf - outbuf_s;
/* Remove trailing ", " added by cumulative process, if found. */
if (retval > 2) {
if (outbuf_s[retval - 2] == ',') {
outbuf_s[retval - 2] = '\0';
retval -= 2;
}
}
return(retval);
}