1 /* X.509 certificate parser
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #define pr_fmt(fmt) "X.509: "fmt
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/oid_registry.h>
18 #include "public_key.h"
19 #include "x509_parser.h"
20 #include "x509-asn1.h"
21 #include "x509_rsakey-asn1.h"
23 struct x509_parse_context {
24 struct x509_certificate *cert; /* Certificate being constructed */
25 unsigned long data; /* Start of data */
26 const void *cert_start; /* Start of cert content */
27 const void *key; /* Key data */
28 size_t key_size; /* Size of key data */
29 enum OID last_oid; /* Last OID encountered */
30 enum OID algo_oid; /* Algorithm OID */
31 unsigned char nr_mpi; /* Number of MPIs stored */
32 u8 o_size; /* Size of organizationName (O) */
33 u8 cn_size; /* Size of commonName (CN) */
34 u8 email_size; /* Size of emailAddress */
35 u16 o_offset; /* Offset of organizationName (O) */
36 u16 cn_offset; /* Offset of commonName (CN) */
37 u16 email_offset; /* Offset of emailAddress */
41 * Free an X.509 certificate
43 void x509_free_certificate(struct x509_certificate *cert)
46 public_key_destroy(cert->pub);
51 kfree(cert->authority);
52 kfree(cert->sig.digest);
53 mpi_free(cert->sig.rsa.s);
57 EXPORT_SYMBOL_GPL(x509_free_certificate);
60 * Parse an X.509 certificate
62 struct x509_certificate *x509_cert_parse(const void *data, size_t datalen)
64 struct x509_certificate *cert;
65 struct x509_parse_context *ctx;
66 struct asymmetric_key_id *kid;
70 cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL);
73 cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL);
76 ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL);
81 ctx->data = (unsigned long)data;
83 /* Attempt to decode the certificate */
84 ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen);
88 /* Decode the public key */
89 ret = asn1_ber_decoder(&x509_rsakey_decoder, ctx,
90 ctx->key, ctx->key_size);
94 /* Generate cert issuer + serial number key ID */
95 kid = asymmetric_key_generate_id(cert->raw_serial,
96 cert->raw_serial_size,
98 cert->raw_issuer_size);
111 x509_free_certificate(cert);
115 EXPORT_SYMBOL_GPL(x509_cert_parse);
118 * Note an OID when we find one for later processing when we know how
121 int x509_note_OID(void *context, size_t hdrlen,
123 const void *value, size_t vlen)
125 struct x509_parse_context *ctx = context;
127 ctx->last_oid = look_up_OID(value, vlen);
128 if (ctx->last_oid == OID__NR) {
130 sprint_oid(value, vlen, buffer, sizeof(buffer));
131 pr_debug("Unknown OID: [%lu] %s\n",
132 (unsigned long)value - ctx->data, buffer);
138 * Save the position of the TBS data so that we can check the signature over it
141 int x509_note_tbs_certificate(void *context, size_t hdrlen,
143 const void *value, size_t vlen)
145 struct x509_parse_context *ctx = context;
147 pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n",
148 hdrlen, tag, (unsigned long)value - ctx->data, vlen);
150 ctx->cert->tbs = value - hdrlen;
151 ctx->cert->tbs_size = vlen + hdrlen;
156 * Record the public key algorithm
158 int x509_note_pkey_algo(void *context, size_t hdrlen,
160 const void *value, size_t vlen)
162 struct x509_parse_context *ctx = context;
164 pr_debug("PubKey Algo: %u\n", ctx->last_oid);
166 switch (ctx->last_oid) {
167 case OID_md2WithRSAEncryption:
168 case OID_md3WithRSAEncryption:
170 return -ENOPKG; /* Unsupported combination */
172 case OID_md4WithRSAEncryption:
173 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_MD5;
174 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
177 case OID_sha1WithRSAEncryption:
178 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA1;
179 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
182 case OID_sha256WithRSAEncryption:
183 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA256;
184 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
187 case OID_sha384WithRSAEncryption:
188 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA384;
189 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
192 case OID_sha512WithRSAEncryption:
193 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA512;
194 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
197 case OID_sha224WithRSAEncryption:
198 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA224;
199 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
203 ctx->algo_oid = ctx->last_oid;
208 * Note the whereabouts and type of the signature.
210 int x509_note_signature(void *context, size_t hdrlen,
212 const void *value, size_t vlen)
214 struct x509_parse_context *ctx = context;
216 pr_debug("Signature type: %u size %zu\n", ctx->last_oid, vlen);
218 if (ctx->last_oid != ctx->algo_oid) {
219 pr_warn("Got cert with pkey (%u) and sig (%u) algorithm OIDs\n",
220 ctx->algo_oid, ctx->last_oid);
224 ctx->cert->raw_sig = value;
225 ctx->cert->raw_sig_size = vlen;
230 * Note the certificate serial number
232 int x509_note_serial(void *context, size_t hdrlen,
234 const void *value, size_t vlen)
236 struct x509_parse_context *ctx = context;
237 ctx->cert->raw_serial = value;
238 ctx->cert->raw_serial_size = vlen;
243 * Note some of the name segments from which we'll fabricate a name.
245 int x509_extract_name_segment(void *context, size_t hdrlen,
247 const void *value, size_t vlen)
249 struct x509_parse_context *ctx = context;
251 switch (ctx->last_oid) {
254 ctx->cn_offset = (unsigned long)value - ctx->data;
256 case OID_organizationName:
258 ctx->o_offset = (unsigned long)value - ctx->data;
260 case OID_email_address:
261 ctx->email_size = vlen;
262 ctx->email_offset = (unsigned long)value - ctx->data;
272 * Fabricate and save the issuer and subject names
274 static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
276 char **_name, size_t vlen)
278 const void *name, *data = (const void *)ctx->data;
285 /* Empty name string if no material */
286 if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) {
287 buffer = kmalloc(1, GFP_KERNEL);
294 if (ctx->cn_size && ctx->o_size) {
295 /* Consider combining O and CN, but use only the CN if it is
296 * prefixed by the O, or a significant portion thereof.
298 namesize = ctx->cn_size;
299 name = data + ctx->cn_offset;
300 if (ctx->cn_size >= ctx->o_size &&
301 memcmp(data + ctx->cn_offset, data + ctx->o_offset,
303 goto single_component;
304 if (ctx->cn_size >= 7 &&
306 memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0)
307 goto single_component;
309 buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1,
315 data + ctx->o_offset, ctx->o_size);
316 buffer[ctx->o_size + 0] = ':';
317 buffer[ctx->o_size + 1] = ' ';
318 memcpy(buffer + ctx->o_size + 2,
319 data + ctx->cn_offset, ctx->cn_size);
320 buffer[ctx->o_size + 2 + ctx->cn_size] = 0;
323 } else if (ctx->cn_size) {
324 namesize = ctx->cn_size;
325 name = data + ctx->cn_offset;
326 } else if (ctx->o_size) {
327 namesize = ctx->o_size;
328 name = data + ctx->o_offset;
330 namesize = ctx->email_size;
331 name = data + ctx->email_offset;
335 buffer = kmalloc(namesize + 1, GFP_KERNEL);
338 memcpy(buffer, name, namesize);
339 buffer[namesize] = 0;
349 int x509_note_issuer(void *context, size_t hdrlen,
351 const void *value, size_t vlen)
353 struct x509_parse_context *ctx = context;
354 ctx->cert->raw_issuer = value;
355 ctx->cert->raw_issuer_size = vlen;
356 return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen);
359 int x509_note_subject(void *context, size_t hdrlen,
361 const void *value, size_t vlen)
363 struct x509_parse_context *ctx = context;
364 ctx->cert->raw_subject = value;
365 ctx->cert->raw_subject_size = vlen;
366 return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen);
370 * Extract the data for the public key algorithm
372 int x509_extract_key_data(void *context, size_t hdrlen,
374 const void *value, size_t vlen)
376 struct x509_parse_context *ctx = context;
378 if (ctx->last_oid != OID_rsaEncryption)
381 ctx->cert->pub->pkey_algo = PKEY_ALGO_RSA;
383 /* Discard the BIT STRING metadata */
384 ctx->key = value + 1;
385 ctx->key_size = vlen - 1;
390 * Extract a RSA public key value
392 int rsa_extract_mpi(void *context, size_t hdrlen,
394 const void *value, size_t vlen)
396 struct x509_parse_context *ctx = context;
399 if (ctx->nr_mpi >= ARRAY_SIZE(ctx->cert->pub->mpi)) {
400 pr_err("Too many public key MPIs in certificate\n");
404 mpi = mpi_read_raw_data(value, vlen);
408 ctx->cert->pub->mpi[ctx->nr_mpi++] = mpi;
412 /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
413 #define SEQ_TAG_KEYID (ASN1_CONT << 6)
416 * Process certificate extensions that are used to qualify the certificate.
418 int x509_process_extension(void *context, size_t hdrlen,
420 const void *value, size_t vlen)
422 struct x509_parse_context *ctx = context;
423 struct asymmetric_key_id *kid;
424 const unsigned char *v = value;
427 pr_debug("Extension: %u\n", ctx->last_oid);
429 if (ctx->last_oid == OID_subjectKeyIdentifier) {
430 /* Get hold of the key fingerprint */
431 if (ctx->cert->skid || vlen < 3)
433 if (v[0] != ASN1_OTS || v[1] != vlen - 2)
438 ctx->cert->raw_skid_size = vlen;
439 ctx->cert->raw_skid = v;
440 kid = asymmetric_key_generate_id(ctx->cert->raw_subject,
441 ctx->cert->raw_subject_size,
445 ctx->cert->skid = kid;
446 pr_debug("subjkeyid %*phN\n", kid->len, kid->data);
450 if (ctx->last_oid == OID_authorityKeyIdentifier) {
451 /* Get hold of the CA key fingerprint */
452 if (ctx->cert->authority || vlen < 5)
455 /* Authority Key Identifier must be a Constructed SEQUENCE */
456 if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
459 /* Authority Key Identifier is not indefinite length */
460 if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
463 if (vlen < ASN1_INDEFINITE_LENGTH) {
464 /* Short Form length */
465 if (v[1] != vlen - 2 ||
466 v[2] != SEQ_TAG_KEYID ||
473 /* Long Form length */
475 size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
480 /* calculate the length from subsequent octets */
482 for (i = 0; i < sub; i++) {
487 if (seq_len != vlen - 2 - sub ||
488 v[sub] != SEQ_TAG_KEYID ||
489 v[sub + 1] > vlen - 4 - sub)
496 kid = asymmetric_key_generate_id(ctx->cert->raw_issuer,
497 ctx->cert->raw_issuer_size,
501 pr_debug("authkeyid %*phN\n", kid->len, kid->data);
502 ctx->cert->authority = kid;
510 * Record a certificate time.
512 static int x509_note_time(struct tm *tm, size_t hdrlen,
514 const unsigned char *value, size_t vlen)
516 const unsigned char *p = value;
518 #define dec2bin(X) ((X) - '0')
519 #define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })
521 if (tag == ASN1_UNITIM) {
522 /* UTCTime: YYMMDDHHMMSSZ */
524 goto unsupported_time;
525 tm->tm_year = DD2bin(p);
526 if (tm->tm_year >= 50)
530 } else if (tag == ASN1_GENTIM) {
531 /* GenTime: YYYYMMDDHHMMSSZ */
533 goto unsupported_time;
534 tm->tm_year = DD2bin(p) * 100 + DD2bin(p);
536 goto unsupported_time;
540 tm->tm_mon = DD2bin(p) - 1;
541 tm->tm_mday = DD2bin(p);
542 tm->tm_hour = DD2bin(p);
543 tm->tm_min = DD2bin(p);
544 tm->tm_sec = DD2bin(p);
547 goto unsupported_time;
552 pr_debug("Got unsupported time [tag %02x]: '%*.*s'\n",
553 tag, (int)vlen, (int)vlen, value);
557 int x509_note_not_before(void *context, size_t hdrlen,
559 const void *value, size_t vlen)
561 struct x509_parse_context *ctx = context;
562 return x509_note_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen);
565 int x509_note_not_after(void *context, size_t hdrlen,
567 const void *value, size_t vlen)
569 struct x509_parse_context *ctx = context;
570 return x509_note_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen);