-
Notifications
You must be signed in to change notification settings - Fork 1
/
x509.c
executable file
·525 lines (418 loc) · 14.2 KB
/
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "asn1-parser/dsl.h"
#include "asn1-parser/x509.h"
#include "asn1-parser/internal/macros.h"
#include "asn1-parser/internal/x509.h"
// Common OID prefixes
#define _OID_KEY_USAGE 1, 3, 6, 1, 5, 5, 7, 3
// These are found in DNs
#define OID_DN_COMMON_NAME ASN1_CONST_OID(2, 5, 4, 3)
#define OID_DN_COUNTRY_NAME ASN1_CONST_OID(2, 5, 4, 6)
#define OID_DN_LOCALITY ASN1_CONST_OID(2, 5, 4, 7)
#define OID_DN_STATE_OR_PROVINCE ASN1_CONST_OID(2, 5, 4, 8)
// #define OID_DN_STREET_ADDRESS ASN1_CONST_OID(2,5,4,9)
#define OID_DN_ORGANIZATION ASN1_CONST_OID(2, 5, 4, 10)
#define OID_DN_ORGANIZATION_UNIT ASN1_CONST_OID(2, 5, 4, 11)
// These are possible extended key usage OIDs
#define OID_EXT_KEY_USAGE_ANY ASN1_CONST_OID(2, 5, 29, 37, 0)
#define OID_EXT_KEY_USAGE_SERVER_AUTH ASN1_CONST_OID(_OID_KEY_USAGE, 1)
#define OID_EXT_KEY_USAGE_CLIENT_AUTH ASN1_CONST_OID(_OID_KEY_USAGE, 2)
#define OID_EXT_KEY_USAGE_CODE_SIGNING ASN1_CONST_OID(_OID_KEY_USAGE, 3)
#define OID_EXT_KEY_USAGE_EMAIL_PROT ASN1_CONST_OID(_OID_KEY_USAGE, 4)
#define OID_EXT_KEY_USAGE_TIME_STAMP ASN1_CONST_OID(_OID_KEY_USAGE, 8)
#define OID_EXT_KEY_USAGE_OCSP_SIGN ASN1_CONST_OID(_OID_KEY_USAGE, 9)
typedef asinine_err_t (*signature_parser_t)(
asn1_parser_t *, x509_signature_t *);
typedef struct {
asn1_oid_t oid;
x509_sig_algo_t algorithm;
signature_parser_t parser;
} signature_lookup_t;
typedef asinine_err_t (*extension_parser_t)(asn1_parser_t *, x509_cert_t *);
typedef struct {
asn1_oid_t oid;
extension_parser_t parser;
} extension_lookup_t;
static asinine_err_t parse_optional(asn1_parser_t *, x509_cert_t *);
static asinine_err_t parse_extensions(asn1_parser_t *, x509_cert_t *);
static asinine_err_t parse_null_or_empty_args(
asn1_parser_t *, x509_signature_t *);
static asinine_err_t parse_empty_args(asn1_parser_t *, x509_signature_t *);
static asinine_err_t parse_signature_algo(asn1_parser_t *, x509_signature_t *);
static asinine_err_t parse_validity(asn1_parser_t *, x509_cert_t *);
static asinine_err_t parse_extn_key_usage(asn1_parser_t *, x509_cert_t *);
static asinine_err_t parse_extn_ext_key_usage(asn1_parser_t *, x509_cert_t *);
static asinine_err_t parse_extn_basic_constraints(
asn1_parser_t *, x509_cert_t *);
static asinine_err_t parse_extn_subject_alt_name(
asn1_parser_t *, x509_cert_t *);
static const signature_lookup_t signature_algorithms[] = {
{
ASN1_OID(1, 2, 840, 113549, 1, 1, 2), X509_SIGNATURE_MD2_RSA,
&parse_null_or_empty_args,
},
{
ASN1_OID(1, 2, 840, 113549, 1, 1, 4), X509_SIGNATURE_MD5_RSA,
&parse_null_or_empty_args,
},
{
ASN1_OID(1, 2, 840, 113549, 1, 1, 5), X509_SIGNATURE_SHA1_RSA,
&parse_null_or_empty_args,
},
{
ASN1_OID(1, 2, 840, 113549, 1, 1, 11), X509_SIGNATURE_SHA256_RSA,
&parse_null_or_empty_args,
},
{
ASN1_OID(1, 2, 840, 113549, 1, 1, 12), X509_SIGNATURE_SHA384_RSA,
&parse_null_or_empty_args,
},
{
ASN1_OID(1, 2, 840, 113549, 1, 1, 13), X509_SIGNATURE_SHA512_RSA,
&parse_null_or_empty_args,
},
{
ASN1_OID(1, 2, 840, 10045, 4, 3, 2), X509_SIGNATURE_SHA256_ECDSA,
&parse_empty_args,
},
{
ASN1_OID(1, 2, 840, 10045, 4, 3, 3), X509_SIGNATURE_SHA384_ECDSA,
&parse_empty_args,
},
{
ASN1_OID(1, 2, 840, 10045, 4, 3, 3), X509_SIGNATURE_SHA512_ECDSA,
&parse_empty_args,
},
{
ASN1_OID(2, 16, 840, 1, 101, 3, 4, 3, 2), X509_SIGNATURE_SHA256_DSA,
&parse_empty_args,
},
};
static const extension_lookup_t extensions[] = {
{ASN1_OID(2, 5, 29, 15), &parse_extn_key_usage},
{ASN1_OID(2, 5, 29, 17), &parse_extn_subject_alt_name},
{ASN1_OID(2, 5, 29, 19), &parse_extn_basic_constraints},
{ASN1_OID(2, 5, 29, 37), &parse_extn_ext_key_usage},
};
asinine_err_t
x509_parse_cert(asn1_parser_t *parser, x509_cert_t *cert) {
const asn1_token_t *token;
x509_signature_t sig_check;
*cert = (x509_cert_t){0};
token = &parser->token;
// Certificate
RETURN_ON_ERROR(asn1_push_seq(parser));
// tbsCertificate
RETURN_ON_ERROR(asn1_push_seq(parser));
cert->raw = token->start;
cert->raw_num =
token->length + (size_t)(token->data - (const uint8_t *)token->start);
// version
NEXT_TOKEN(parser);
if (asn1_is(token, ASN1_CLASS_CONTEXT, 0, ASN1_ENCODING_CONSTRUCTED)) {
asn1_word_t version;
RETURN_ON_ERROR(asn1_push(parser));
NEXT_TOKEN(parser);
if (!asn1_is_int(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_int(token, &version));
if (version != X509_V2 && version != X509_V3) {
return ERROR(ASININE_ERR_INVALID, "cert: unknown version");
}
cert->version = (x509_version_t)version;
RETURN_ON_ERROR(asn1_pop(parser));
NEXT_TOKEN(parser);
} else {
cert->version = X509_V1;
}
// serialNumber
// TODO: As per X.509 guide, this should be treated as a binary blob
if (!asn1_is_int(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
// signature
RETURN_ON_ERROR(parse_signature_algo(parser, &cert->signature));
// issuer
RETURN_ON_ERROR(x509_parse_name(parser, &cert->issuer));
// validity
RETURN_ON_ERROR(parse_validity(parser, cert));
// subject
RETURN_ON_ERROR(x509_parse_optional_name(parser, &cert->subject));
// subjectPublicKeyInfo
RETURN_ON_ERROR(x509_parse_pubkey(
parser, &cert->pubkey, &cert->pubkey_params, &cert->has_pubkey_params));
// Optional items (X.509 v2 and up)
RETURN_ON_ERROR(parse_optional(parser, cert));
// End of tbsCertificate
RETURN_ON_ERROR(asn1_pop(parser));
// signatureAlgorithm
RETURN_ON_ERROR(parse_signature_algo(parser, &sig_check));
if (cert->signature.algorithm != sig_check.algorithm) {
// This must compare all fields parsed from signatureAlgorithm,
// but currently we only support algorithms without parameters.
return ERROR(
ASININE_ERR_INVALID, "cert: signature algorithm doesn't match");
}
// signature
NEXT_TOKEN(parser);
if (!asn1_is_bitstring(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
// The signature value claims it's a bitstring, but really is
// a bag of bytes. Contrary to the spec it can end in a zero byte,
// which breaks when validated as a real bitstring.
if (token->length < 1 || token->data[0] != 0) {
return ERROR(
ASININE_ERR_MALFORMED, "cert: signature has invalid prefix");
}
cert->signature.data = token->data + 1;
cert->signature.num = token->length - 1;
// RFC5280 4.1.2.6.
if (cert->is_ca && cert->subject.num == 0) {
return ERROR(ASININE_ERR_INVALID, "cert: missing subject name (as CA)");
}
if ((cert->key_usage & X509_KEYUSE_CRL_SIGN) != 0 &&
cert->subject.num == 0) {
return ERROR(
ASININE_ERR_INVALID, "cert: missing subject name (to sign)");
}
return asn1_pop(parser);
}
static asinine_err_t
parse_optional(asn1_parser_t *parser, x509_cert_t *cert) {
const asn1_token_t *const token = &parser->token;
if (cert->version >= X509_V2) {
OPTIONAL_TOKEN(parser);
// issuerUniqueID
if (asn1_is(token, ASN1_CLASS_CONTEXT, 1, ASN1_ENCODING_PRIMITIVE)) {
// TODO: Do something
OPTIONAL_TOKEN(parser);
}
// subjectUniqueID
if (asn1_is(token, ASN1_CLASS_CONTEXT, 2, ASN1_ENCODING_PRIMITIVE)) {
// TODO: Do something
OPTIONAL_TOKEN(parser);
}
// extensions
if (cert->version != X509_V3) {
// We should not be here if this is not a V3 cert
return ERROR(
ASININE_ERR_INVALID, "cert: extensions should not be present");
}
if (!asn1_is(token, ASN1_CLASS_CONTEXT, 3, ASN1_ENCODING_CONSTRUCTED)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_push(parser));
RETURN_ON_ERROR(parse_extensions(parser, cert));
RETURN_ON_ERROR(asn1_pop(parser));
}
return ERROR(ASININE_OK, NULL);
}
static extension_parser_t
find_extension_parser(const asn1_oid_t *oid) {
size_t i;
for (i = 0; i < NUM(extensions); i++) {
if (asn1_oid_cmp(&extensions[i].oid, oid) == 0) {
return extensions[i].parser;
}
}
return NULL;
}
static asinine_err_t
parse_extensions(asn1_parser_t *parser, x509_cert_t *cert) {
const asn1_token_t *const token = &parser->token;
asn1_oid_t id;
bool critical = false;
extension_parser_t extn_parser;
RETURN_ON_ERROR(asn1_push_seq(parser));
while (!asn1_eof(parser)) {
RETURN_ON_ERROR(asn1_push_seq(parser));
// extnid
NEXT_TOKEN(parser);
if (!asn1_is_oid(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_oid(token, &id));
// critical
NEXT_TOKEN(parser);
critical = false;
if (asn1_is_bool(token)) {
RETURN_ON_ERROR(asn1_bool(token, &critical));
NEXT_TOKEN(parser);
}
// extnValue
if (!asn1_is_octetstring(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
extn_parser = find_extension_parser(&id);
if (extn_parser != NULL) {
RETURN_ON_ERROR(asn1_force_push(parser));
RETURN_ON_ERROR(extn_parser(parser, cert));
RETURN_ON_ERROR(asn1_pop(parser));
} else if (critical) {
return ERROR(ASININE_ERR_UNSUPPORTED, "unknown critical extension");
}
RETURN_ON_ERROR(asn1_pop(parser));
}
return asn1_pop(parser);
}
static const signature_lookup_t *
find_signature_algorithm(const asn1_oid_t *oid) {
size_t i;
for (i = 0; i < NUM(signature_algorithms); i++) {
if (asn1_oid_cmp(oid, &(signature_algorithms[i].oid)) == 0) {
return &signature_algorithms[i];
}
}
return NULL;
}
static asinine_err_t
parse_signature_algo(asn1_parser_t *parser, x509_signature_t *signature) {
const asn1_token_t *const token = &parser->token;
asn1_oid_t oid;
const signature_lookup_t *result;
RETURN_ON_ERROR(asn1_push_seq(parser));
NEXT_TOKEN(parser);
if (!asn1_is_oid(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_oid(token, &oid));
result = find_signature_algorithm(&oid);
if (result == NULL) {
return ERROR(ASININE_ERR_UNSUPPORTED, "signature: unknown algorithm");
}
signature->algorithm = result->algorithm;
RETURN_ON_ERROR(result->parser(parser, signature));
return asn1_pop(parser);
}
asinine_err_t
parse_validity(asn1_parser_t *parser, x509_cert_t *cert) {
const asn1_token_t *const token = &parser->token;
RETURN_ON_ERROR(asn1_push_seq(parser));
// Valid from
NEXT_TOKEN(parser);
if (!asn1_is_time(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_time(token, &cert->valid_from));
// Valid to
NEXT_TOKEN(parser);
if (!asn1_is_time(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_time(token, &cert->valid_to));
return asn1_pop(parser);
}
static asinine_err_t
parse_null_or_empty_args(asn1_parser_t *parser, x509_signature_t *sig) {
(void)sig;
return _x509_parse_null_or_empty_args(parser);
}
asinine_err_t
_x509_parse_null_or_empty_args(asn1_parser_t *parser) {
const asn1_token_t *const token = &parser->token;
// There is at least one implementation which skips the null.
// This deviates from the spec.
if (asn1_eof(parser)) {
return ERROR(ASININE_OK, NULL);
}
NEXT_TOKEN(parser);
if (!asn1_is_null(token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
return asn1_null(token);
}
static asinine_err_t
parse_empty_args(asn1_parser_t *parser, x509_signature_t *sig) {
(void)sig;
if (!asn1_eof(parser)) {
return ERROR(ASININE_ERR_MALFORMED, "algorithm: non-empty args");
}
return ERROR(ASININE_OK, NULL);
}
static asinine_err_t
parse_extn_key_usage(asn1_parser_t *parser, x509_cert_t *cert) {
uint8_t buf[2];
NEXT_TOKEN(parser);
if (!asn1_is_bitstring(&parser->token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_bitstring(&parser->token, buf, sizeof buf));
cert->key_usage = (uint16_t)(buf[1] << 8) | buf[0];
// RFC 3279 2.3.1. to 2.3.5.
if ((cert->key_usage & X509_KEYUSE_DECIPHER_ONLY) != 0 &&
(cert->key_usage & X509_KEYUSE_ENCIPHER_ONLY) != 0) {
return ERROR(ASININE_ERR_INVALID,
"key usage: both decipher only and encipher only asserted");
}
/* RFC 5280, p.30: "When the keyUsage extension appears in a certificate, at
* least one of the bits MUST be set to 1."
*/
if (cert->key_usage == 0) {
return ERROR(ASININE_ERR_INVALID, "key usage: no usage asserted");
}
return ERROR(ASININE_OK, NULL);
}
static asinine_err_t
parse_extn_ext_key_usage(asn1_parser_t *parser, x509_cert_t *cert) {
RETURN_ON_ERROR(asn1_push_seq(parser));
cert->ext_key_usage = 0;
while (!asn1_eof(parser)) {
asn1_oid_t oid;
NEXT_TOKEN(parser);
if (!asn1_is_oid(&parser->token)) {
return ERROR(ASININE_ERR_INVALID, NULL);
}
RETURN_ON_ERROR(asn1_oid(&parser->token, &oid));
/* RFC 5280, p. 43
* If multiple purposes are indicated the application need not recognize
* all purposes indicated, as long as the intended purpose is present.
*/
if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_SERVER_AUTH)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_SERVER_AUTH;
} else if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_CLIENT_AUTH)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_CLIENT_AUTH;
} else if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_CODE_SIGNING)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_CODE_SIGNING;
} else if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_EMAIL_PROT)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_EMAIL_PROT;
} else if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_TIME_STAMP)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_TIME_STAMP;
} else if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_OCSP_SIGN)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_OCSP_SIGN;
} else if (asn1_oid_eq(&oid, OID_EXT_KEY_USAGE_ANY)) {
cert->ext_key_usage |= X509_EXT_KEYUSE_ANY;
}
}
return asn1_pop(parser);
}
static asinine_err_t
parse_extn_basic_constraints(asn1_parser_t *parser, x509_cert_t *cert) {
asn1_word_t value;
RETURN_ON_ERROR(asn1_push_seq(parser));
cert->is_ca = false;
cert->path_len_constraint = -1;
OPTIONAL_TOKEN(parser);
if (asn1_is_bool(&parser->token)) {
RETURN_ON_ERROR(asn1_bool(&parser->token, &cert->is_ca));
OPTIONAL_TOKEN(parser);
}
RETURN_ON_ERROR(asn1_int(&parser->token, &value));
if (value < 0) {
return ERROR(
ASININE_ERR_INVALID, "basic constraints: negative path length");
}
if (value > 127) {
return ERROR(
ASININE_ERR_UNSUPPORTED, "basic constraints: path length too high");
}
cert->path_len_constraint = (int8_t)value;
return asn1_pop(parser);
}
static asinine_err_t
parse_extn_subject_alt_name(asn1_parser_t *parser, x509_cert_t *cert) {
return x509_parse_alt_names(parser, &cert->subject_alt_names);
}