blob: 4a8df29ab71379f490ec737334298c33747592a2 [file] [log] [blame]
David Howellsc26fd692012-09-24 17:11:48 +01001/* X.509 certificate parser
2 *
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
11
12#define pr_fmt(fmt) "X.509: "fmt
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/err.h>
16#include <linux/oid_registry.h>
17#include "public_key.h"
18#include "x509_parser.h"
19#include "x509-asn1.h"
20#include "x509_rsakey-asn1.h"
21
22struct x509_parse_context {
23 struct x509_certificate *cert; /* Certificate being constructed */
24 unsigned long data; /* Start of data */
25 const void *cert_start; /* Start of cert content */
26 const void *key; /* Key data */
27 size_t key_size; /* Size of key data */
28 enum OID last_oid; /* Last OID encountered */
29 enum OID algo_oid; /* Algorithm OID */
30 unsigned char nr_mpi; /* Number of MPIs stored */
31 u8 o_size; /* Size of organizationName (O) */
32 u8 cn_size; /* Size of commonName (CN) */
33 u8 email_size; /* Size of emailAddress */
34 u16 o_offset; /* Offset of organizationName (O) */
35 u16 cn_offset; /* Offset of commonName (CN) */
36 u16 email_offset; /* Offset of emailAddress */
37};
38
39/*
40 * Free an X.509 certificate
41 */
42void x509_free_certificate(struct x509_certificate *cert)
43{
44 if (cert) {
45 public_key_destroy(cert->pub);
46 kfree(cert->issuer);
47 kfree(cert->subject);
48 kfree(cert->fingerprint);
49 kfree(cert->authority);
David Howellsb426beb2013-08-30 16:18:02 +010050 kfree(cert->sig.digest);
51 mpi_free(cert->sig.rsa.s);
David Howellsc26fd692012-09-24 17:11:48 +010052 kfree(cert);
53 }
54}
55
56/*
57 * Parse an X.509 certificate
58 */
59struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
60{
61 struct x509_certificate *cert;
62 struct x509_parse_context *ctx;
63 long ret;
64
65 ret = -ENOMEM;
66 cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);
67 if (!cert)
68 goto error_no_cert;
69 cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
70 if (!cert->pub)
71 goto error_no_ctx;
72 ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL);
73 if (!ctx)
74 goto error_no_ctx;
75
76 ctx->cert = cert;
77 ctx->data = (unsigned long)data;
78
79 /* Attempt to decode the certificate */
80 ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen);
81 if (ret < 0)
82 goto error_decode;
83
84 /* Decode the public key */
85 ret = asn1_ber_decoder(&x509_rsakey_decoder, ctx,
86 ctx->key, ctx->key_size);
87 if (ret < 0)
88 goto error_decode;
89
90 kfree(ctx);
91 return cert;
92
93error_decode:
94 kfree(ctx);
95error_no_ctx:
96 x509_free_certificate(cert);
97error_no_cert:
98 return ERR_PTR(ret);
99}
100
101/*
102 * Note an OID when we find one for later processing when we know how
103 * to interpret it.
104 */
105int x509_note_OID(void *context, size_t hdrlen,
106 unsigned char tag,
107 const void *value, size_t vlen)
108{
109 struct x509_parse_context *ctx = context;
110
111 ctx->last_oid = look_up_OID(value, vlen);
112 if (ctx->last_oid == OID__NR) {
113 char buffer[50];
114 sprint_oid(value, vlen, buffer, sizeof(buffer));
Randy Dunlapcf754462012-10-03 16:04:46 -0700115 pr_debug("Unknown OID: [%lu] %s\n",
David Howellsc26fd692012-09-24 17:11:48 +0100116 (unsigned long)value - ctx->data, buffer);
117 }
118 return 0;
119}
120
121/*
122 * Save the position of the TBS data so that we can check the signature over it
123 * later.
124 */
125int x509_note_tbs_certificate(void *context, size_t hdrlen,
126 unsigned char tag,
127 const void *value, size_t vlen)
128{
129 struct x509_parse_context *ctx = context;
130
131 pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n",
132 hdrlen, tag, (unsigned long)value - ctx->data, vlen);
133
134 ctx->cert->tbs = value - hdrlen;
135 ctx->cert->tbs_size = vlen + hdrlen;
136 return 0;
137}
138
139/*
140 * Record the public key algorithm
141 */
142int x509_note_pkey_algo(void *context, size_t hdrlen,
143 unsigned char tag,
144 const void *value, size_t vlen)
145{
146 struct x509_parse_context *ctx = context;
147
148 pr_debug("PubKey Algo: %u\n", ctx->last_oid);
149
150 switch (ctx->last_oid) {
151 case OID_md2WithRSAEncryption:
152 case OID_md3WithRSAEncryption:
153 default:
154 return -ENOPKG; /* Unsupported combination */
155
156 case OID_md4WithRSAEncryption:
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300157 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_MD5;
David Howellsb426beb2013-08-30 16:18:02 +0100158 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
David Howellsc26fd692012-09-24 17:11:48 +0100159 break;
160
161 case OID_sha1WithRSAEncryption:
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300162 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA1;
David Howellsb426beb2013-08-30 16:18:02 +0100163 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
David Howellsc26fd692012-09-24 17:11:48 +0100164 break;
165
166 case OID_sha256WithRSAEncryption:
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300167 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA256;
David Howellsb426beb2013-08-30 16:18:02 +0100168 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
David Howellsc26fd692012-09-24 17:11:48 +0100169 break;
170
171 case OID_sha384WithRSAEncryption:
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300172 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA384;
David Howellsb426beb2013-08-30 16:18:02 +0100173 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
David Howellsc26fd692012-09-24 17:11:48 +0100174 break;
175
176 case OID_sha512WithRSAEncryption:
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300177 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA512;
David Howellsb426beb2013-08-30 16:18:02 +0100178 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
David Howellsc26fd692012-09-24 17:11:48 +0100179 break;
180
181 case OID_sha224WithRSAEncryption:
Dmitry Kasatkin3fe78ca2013-05-06 15:58:15 +0300182 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA224;
David Howellsb426beb2013-08-30 16:18:02 +0100183 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
David Howellsc26fd692012-09-24 17:11:48 +0100184 break;
185 }
186
187 ctx->algo_oid = ctx->last_oid;
188 return 0;
189}
190
191/*
192 * Note the whereabouts and type of the signature.
193 */
194int x509_note_signature(void *context, size_t hdrlen,
195 unsigned char tag,
196 const void *value, size_t vlen)
197{
198 struct x509_parse_context *ctx = context;
199
200 pr_debug("Signature type: %u size %zu\n", ctx->last_oid, vlen);
201
202 if (ctx->last_oid != ctx->algo_oid) {
203 pr_warn("Got cert with pkey (%u) and sig (%u) algorithm OIDs\n",
204 ctx->algo_oid, ctx->last_oid);
205 return -EINVAL;
206 }
207
David Howellsb426beb2013-08-30 16:18:02 +0100208 ctx->cert->raw_sig = value;
209 ctx->cert->raw_sig_size = vlen;
David Howellsc26fd692012-09-24 17:11:48 +0100210 return 0;
211}
212
213/*
David Howells84aabd42014-07-01 16:40:19 +0100214 * Note the certificate serial number
215 */
216int x509_note_serial(void *context, size_t hdrlen,
217 unsigned char tag,
218 const void *value, size_t vlen)
219{
220 struct x509_parse_context *ctx = context;
221 ctx->cert->raw_serial = value;
222 ctx->cert->raw_serial_size = vlen;
223 return 0;
224}
225
226/*
David Howellsc26fd692012-09-24 17:11:48 +0100227 * Note some of the name segments from which we'll fabricate a name.
228 */
229int x509_extract_name_segment(void *context, size_t hdrlen,
230 unsigned char tag,
231 const void *value, size_t vlen)
232{
233 struct x509_parse_context *ctx = context;
234
235 switch (ctx->last_oid) {
236 case OID_commonName:
237 ctx->cn_size = vlen;
238 ctx->cn_offset = (unsigned long)value - ctx->data;
239 break;
240 case OID_organizationName:
241 ctx->o_size = vlen;
242 ctx->o_offset = (unsigned long)value - ctx->data;
243 break;
244 case OID_email_address:
245 ctx->email_size = vlen;
246 ctx->email_offset = (unsigned long)value - ctx->data;
247 break;
248 default:
249 break;
250 }
251
252 return 0;
253}
254
255/*
256 * Fabricate and save the issuer and subject names
257 */
258static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
259 unsigned char tag,
260 char **_name, size_t vlen)
261{
262 const void *name, *data = (const void *)ctx->data;
263 size_t namesize;
264 char *buffer;
265
266 if (*_name)
267 return -EINVAL;
268
269 /* Empty name string if no material */
270 if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) {
271 buffer = kmalloc(1, GFP_KERNEL);
272 if (!buffer)
273 return -ENOMEM;
274 buffer[0] = 0;
275 goto done;
276 }
277
278 if (ctx->cn_size && ctx->o_size) {
279 /* Consider combining O and CN, but use only the CN if it is
280 * prefixed by the O, or a significant portion thereof.
281 */
282 namesize = ctx->cn_size;
283 name = data + ctx->cn_offset;
284 if (ctx->cn_size >= ctx->o_size &&
285 memcmp(data + ctx->cn_offset, data + ctx->o_offset,
286 ctx->o_size) == 0)
287 goto single_component;
288 if (ctx->cn_size >= 7 &&
289 ctx->o_size >= 7 &&
290 memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0)
291 goto single_component;
292
293 buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1,
294 GFP_KERNEL);
295 if (!buffer)
296 return -ENOMEM;
297
298 memcpy(buffer,
299 data + ctx->o_offset, ctx->o_size);
300 buffer[ctx->o_size + 0] = ':';
301 buffer[ctx->o_size + 1] = ' ';
302 memcpy(buffer + ctx->o_size + 2,
303 data + ctx->cn_offset, ctx->cn_size);
304 buffer[ctx->o_size + 2 + ctx->cn_size] = 0;
305 goto done;
306
307 } else if (ctx->cn_size) {
308 namesize = ctx->cn_size;
309 name = data + ctx->cn_offset;
310 } else if (ctx->o_size) {
311 namesize = ctx->o_size;
312 name = data + ctx->o_offset;
313 } else {
314 namesize = ctx->email_size;
315 name = data + ctx->email_offset;
316 }
317
318single_component:
319 buffer = kmalloc(namesize + 1, GFP_KERNEL);
320 if (!buffer)
321 return -ENOMEM;
322 memcpy(buffer, name, namesize);
323 buffer[namesize] = 0;
324
325done:
326 *_name = buffer;
327 ctx->cn_size = 0;
328 ctx->o_size = 0;
329 ctx->email_size = 0;
330 return 0;
331}
332
333int x509_note_issuer(void *context, size_t hdrlen,
334 unsigned char tag,
335 const void *value, size_t vlen)
336{
337 struct x509_parse_context *ctx = context;
David Howells84aabd42014-07-01 16:40:19 +0100338 ctx->cert->raw_issuer = value;
339 ctx->cert->raw_issuer_size = vlen;
David Howellsc26fd692012-09-24 17:11:48 +0100340 return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen);
341}
342
343int x509_note_subject(void *context, size_t hdrlen,
344 unsigned char tag,
345 const void *value, size_t vlen)
346{
347 struct x509_parse_context *ctx = context;
David Howells84aabd42014-07-01 16:40:19 +0100348 ctx->cert->raw_subject = value;
349 ctx->cert->raw_subject_size = vlen;
David Howellsc26fd692012-09-24 17:11:48 +0100350 return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen);
351}
352
353/*
354 * Extract the data for the public key algorithm
355 */
356int x509_extract_key_data(void *context, size_t hdrlen,
357 unsigned char tag,
358 const void *value, size_t vlen)
359{
360 struct x509_parse_context *ctx = context;
361
362 if (ctx->last_oid != OID_rsaEncryption)
363 return -ENOPKG;
364
David Howells67f7d60b2013-08-30 16:15:24 +0100365 ctx->cert->pub->pkey_algo = PKEY_ALGO_RSA;
366
367 /* Discard the BIT STRING metadata */
David Howellsc26fd692012-09-24 17:11:48 +0100368 ctx->key = value + 1;
369 ctx->key_size = vlen - 1;
370 return 0;
371}
372
373/*
374 * Extract a RSA public key value
375 */
376int rsa_extract_mpi(void *context, size_t hdrlen,
377 unsigned char tag,
378 const void *value, size_t vlen)
379{
380 struct x509_parse_context *ctx = context;
381 MPI mpi;
382
383 if (ctx->nr_mpi >= ARRAY_SIZE(ctx->cert->pub->mpi)) {
384 pr_err("Too many public key MPIs in certificate\n");
385 return -EBADMSG;
386 }
387
388 mpi = mpi_read_raw_data(value, vlen);
389 if (!mpi)
390 return -ENOMEM;
391
392 ctx->cert->pub->mpi[ctx->nr_mpi++] = mpi;
393 return 0;
394}
395
Chun-Yi Lee04b00bd2013-04-22 10:56:55 +0930396/* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
397#define SEQ_TAG_KEYID (ASN1_CONT << 6)
398
David Howellsc26fd692012-09-24 17:11:48 +0100399/*
400 * Process certificate extensions that are used to qualify the certificate.
401 */
402int x509_process_extension(void *context, size_t hdrlen,
403 unsigned char tag,
404 const void *value, size_t vlen)
405{
406 struct x509_parse_context *ctx = context;
407 const unsigned char *v = value;
408 char *f;
409 int i;
410
411 pr_debug("Extension: %u\n", ctx->last_oid);
412
413 if (ctx->last_oid == OID_subjectKeyIdentifier) {
414 /* Get hold of the key fingerprint */
415 if (vlen < 3)
416 return -EBADMSG;
417 if (v[0] != ASN1_OTS || v[1] != vlen - 2)
418 return -EBADMSG;
419 v += 2;
420 vlen -= 2;
421
422 f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
423 if (!f)
424 return -ENOMEM;
425 for (i = 0; i < vlen; i++)
426 sprintf(f + i * 2, "%02x", v[i]);
427 pr_debug("fingerprint %s\n", f);
428 ctx->cert->fingerprint = f;
429 return 0;
430 }
431
432 if (ctx->last_oid == OID_authorityKeyIdentifier) {
Chun-Yi Lee04b00bd2013-04-22 10:56:55 +0930433 size_t key_len;
434
David Howellsc26fd692012-09-24 17:11:48 +0100435 /* Get hold of the CA key fingerprint */
436 if (vlen < 5)
437 return -EBADMSG;
David Howellsc26fd692012-09-24 17:11:48 +0100438
Chun-Yi Lee04b00bd2013-04-22 10:56:55 +0930439 /* Authority Key Identifier must be a Constructed SEQUENCE */
440 if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
441 return -EBADMSG;
442
443 /* Authority Key Identifier is not indefinite length */
444 if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
445 return -EBADMSG;
446
447 if (vlen < ASN1_INDEFINITE_LENGTH) {
448 /* Short Form length */
449 if (v[1] != vlen - 2 ||
450 v[2] != SEQ_TAG_KEYID ||
451 v[3] > vlen - 4)
452 return -EBADMSG;
453
454 key_len = v[3];
455 v += 4;
456 } else {
457 /* Long Form length */
458 size_t seq_len = 0;
459 size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
460
461 if (sub > 2)
462 return -EBADMSG;
463
464 /* calculate the length from subsequent octets */
465 v += 2;
466 for (i = 0; i < sub; i++) {
467 seq_len <<= 8;
468 seq_len |= v[i];
469 }
470
471 if (seq_len != vlen - 2 - sub ||
472 v[sub] != SEQ_TAG_KEYID ||
473 v[sub + 1] > vlen - 4 - sub)
474 return -EBADMSG;
475
476 key_len = v[sub + 1];
477 v += (sub + 2);
478 }
479
480 f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
David Howellsc26fd692012-09-24 17:11:48 +0100481 if (!f)
482 return -ENOMEM;
Chun-Yi Lee04b00bd2013-04-22 10:56:55 +0930483 for (i = 0; i < key_len; i++)
David Howellsc26fd692012-09-24 17:11:48 +0100484 sprintf(f + i * 2, "%02x", v[i]);
485 pr_debug("authority %s\n", f);
486 ctx->cert->authority = f;
487 return 0;
488 }
489
490 return 0;
491}
492
493/*
494 * Record a certificate time.
495 */
David Howellsa5752d12012-10-02 14:36:16 +0100496static int x509_note_time(struct tm *tm, size_t hdrlen,
David Howellsc26fd692012-09-24 17:11:48 +0100497 unsigned char tag,
498 const unsigned char *value, size_t vlen)
499{
David Howellsc26fd692012-09-24 17:11:48 +0100500 const unsigned char *p = value;
501
502#define dec2bin(X) ((X) - '0')
503#define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })
504
505 if (tag == ASN1_UNITIM) {
506 /* UTCTime: YYMMDDHHMMSSZ */
507 if (vlen != 13)
508 goto unsupported_time;
David Howellsa5752d12012-10-02 14:36:16 +0100509 tm->tm_year = DD2bin(p);
510 if (tm->tm_year >= 50)
511 tm->tm_year += 1900;
David Howellsc26fd692012-09-24 17:11:48 +0100512 else
David Howellsa5752d12012-10-02 14:36:16 +0100513 tm->tm_year += 2000;
David Howellsc26fd692012-09-24 17:11:48 +0100514 } else if (tag == ASN1_GENTIM) {
515 /* GenTime: YYYYMMDDHHMMSSZ */
516 if (vlen != 15)
517 goto unsupported_time;
David Howellsa5752d12012-10-02 14:36:16 +0100518 tm->tm_year = DD2bin(p) * 100 + DD2bin(p);
David Howellsc26fd692012-09-24 17:11:48 +0100519 } else {
520 goto unsupported_time;
521 }
522
David Howellsa5752d12012-10-02 14:36:16 +0100523 tm->tm_year -= 1900;
524 tm->tm_mon = DD2bin(p) - 1;
525 tm->tm_mday = DD2bin(p);
526 tm->tm_hour = DD2bin(p);
527 tm->tm_min = DD2bin(p);
528 tm->tm_sec = DD2bin(p);
David Howellsc26fd692012-09-24 17:11:48 +0100529
530 if (*p != 'Z')
531 goto unsupported_time;
532
David Howellsc26fd692012-09-24 17:11:48 +0100533 return 0;
534
535unsupported_time:
536 pr_debug("Got unsupported time [tag %02x]: '%*.*s'\n",
537 tag, (int)vlen, (int)vlen, value);
538 return -EBADMSG;
539}
540
541int x509_note_not_before(void *context, size_t hdrlen,
542 unsigned char tag,
543 const void *value, size_t vlen)
544{
545 struct x509_parse_context *ctx = context;
546 return x509_note_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen);
547}
548
549int x509_note_not_after(void *context, size_t hdrlen,
550 unsigned char tag,
551 const void *value, size_t vlen)
552{
553 struct x509_parse_context *ctx = context;
554 return x509_note_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen);
555}