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/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"
22 struct 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 */
40 * Free an X.509 certificate
42 void x509_free_certificate(struct x509_certificate
*cert
)
45 public_key_destroy(cert
->pub
);
48 kfree(cert
->fingerprint
);
49 kfree(cert
->authority
);
55 * Parse an X.509 certificate
57 struct x509_certificate
*x509_cert_parse(const void *data
, size_t datalen
)
59 struct x509_certificate
*cert
;
60 struct x509_parse_context
*ctx
;
64 cert
= kzalloc(sizeof(struct x509_certificate
), GFP_KERNEL
);
67 cert
->pub
= kzalloc(sizeof(struct public_key
), GFP_KERNEL
);
70 ctx
= kzalloc(sizeof(struct x509_parse_context
), GFP_KERNEL
);
75 ctx
->data
= (unsigned long)data
;
77 /* Attempt to decode the certificate */
78 ret
= asn1_ber_decoder(&x509_decoder
, ctx
, data
, datalen
);
82 /* Decode the public key */
83 ret
= asn1_ber_decoder(&x509_rsakey_decoder
, ctx
,
84 ctx
->key
, ctx
->key_size
);
94 x509_free_certificate(cert
);
100 * Note an OID when we find one for later processing when we know how
103 int x509_note_OID(void *context
, size_t hdrlen
,
105 const void *value
, size_t vlen
)
107 struct x509_parse_context
*ctx
= context
;
109 ctx
->last_oid
= look_up_OID(value
, vlen
);
110 if (ctx
->last_oid
== OID__NR
) {
112 sprint_oid(value
, vlen
, buffer
, sizeof(buffer
));
113 pr_debug("Unknown OID: [%lu] %s\n",
114 (unsigned long)value
- ctx
->data
, buffer
);
120 * Save the position of the TBS data so that we can check the signature over it
123 int x509_note_tbs_certificate(void *context
, size_t hdrlen
,
125 const void *value
, size_t vlen
)
127 struct x509_parse_context
*ctx
= context
;
129 pr_debug("x509_note_tbs_certificate(,%zu,%02x,%ld,%zu)!\n",
130 hdrlen
, tag
, (unsigned long)value
- ctx
->data
, vlen
);
132 ctx
->cert
->tbs
= value
- hdrlen
;
133 ctx
->cert
->tbs_size
= vlen
+ hdrlen
;
138 * Record the public key algorithm
140 int x509_note_pkey_algo(void *context
, size_t hdrlen
,
142 const void *value
, size_t vlen
)
144 struct x509_parse_context
*ctx
= context
;
146 pr_debug("PubKey Algo: %u\n", ctx
->last_oid
);
148 switch (ctx
->last_oid
) {
149 case OID_md2WithRSAEncryption
:
150 case OID_md3WithRSAEncryption
:
152 return -ENOPKG
; /* Unsupported combination */
154 case OID_md4WithRSAEncryption
:
155 ctx
->cert
->sig_hash_algo
= PKEY_HASH_MD5
;
156 ctx
->cert
->sig_pkey_algo
= PKEY_ALGO_RSA
;
159 case OID_sha1WithRSAEncryption
:
160 ctx
->cert
->sig_hash_algo
= PKEY_HASH_SHA1
;
161 ctx
->cert
->sig_pkey_algo
= PKEY_ALGO_RSA
;
164 case OID_sha256WithRSAEncryption
:
165 ctx
->cert
->sig_hash_algo
= PKEY_HASH_SHA256
;
166 ctx
->cert
->sig_pkey_algo
= PKEY_ALGO_RSA
;
169 case OID_sha384WithRSAEncryption
:
170 ctx
->cert
->sig_hash_algo
= PKEY_HASH_SHA384
;
171 ctx
->cert
->sig_pkey_algo
= PKEY_ALGO_RSA
;
174 case OID_sha512WithRSAEncryption
:
175 ctx
->cert
->sig_hash_algo
= PKEY_HASH_SHA512
;
176 ctx
->cert
->sig_pkey_algo
= PKEY_ALGO_RSA
;
179 case OID_sha224WithRSAEncryption
:
180 ctx
->cert
->sig_hash_algo
= PKEY_HASH_SHA224
;
181 ctx
->cert
->sig_pkey_algo
= PKEY_ALGO_RSA
;
185 ctx
->algo_oid
= ctx
->last_oid
;
190 * Note the whereabouts and type of the signature.
192 int x509_note_signature(void *context
, size_t hdrlen
,
194 const void *value
, size_t vlen
)
196 struct x509_parse_context
*ctx
= context
;
198 pr_debug("Signature type: %u size %zu\n", ctx
->last_oid
, vlen
);
200 if (ctx
->last_oid
!= ctx
->algo_oid
) {
201 pr_warn("Got cert with pkey (%u) and sig (%u) algorithm OIDs\n",
202 ctx
->algo_oid
, ctx
->last_oid
);
206 ctx
->cert
->sig
= value
;
207 ctx
->cert
->sig_size
= vlen
;
212 * Note some of the name segments from which we'll fabricate a name.
214 int x509_extract_name_segment(void *context
, size_t hdrlen
,
216 const void *value
, size_t vlen
)
218 struct x509_parse_context
*ctx
= context
;
220 switch (ctx
->last_oid
) {
223 ctx
->cn_offset
= (unsigned long)value
- ctx
->data
;
225 case OID_organizationName
:
227 ctx
->o_offset
= (unsigned long)value
- ctx
->data
;
229 case OID_email_address
:
230 ctx
->email_size
= vlen
;
231 ctx
->email_offset
= (unsigned long)value
- ctx
->data
;
241 * Fabricate and save the issuer and subject names
243 static int x509_fabricate_name(struct x509_parse_context
*ctx
, size_t hdrlen
,
245 char **_name
, size_t vlen
)
247 const void *name
, *data
= (const void *)ctx
->data
;
254 /* Empty name string if no material */
255 if (!ctx
->cn_size
&& !ctx
->o_size
&& !ctx
->email_size
) {
256 buffer
= kmalloc(1, GFP_KERNEL
);
263 if (ctx
->cn_size
&& ctx
->o_size
) {
264 /* Consider combining O and CN, but use only the CN if it is
265 * prefixed by the O, or a significant portion thereof.
267 namesize
= ctx
->cn_size
;
268 name
= data
+ ctx
->cn_offset
;
269 if (ctx
->cn_size
>= ctx
->o_size
&&
270 memcmp(data
+ ctx
->cn_offset
, data
+ ctx
->o_offset
,
272 goto single_component
;
273 if (ctx
->cn_size
>= 7 &&
275 memcmp(data
+ ctx
->cn_offset
, data
+ ctx
->o_offset
, 7) == 0)
276 goto single_component
;
278 buffer
= kmalloc(ctx
->o_size
+ 2 + ctx
->cn_size
+ 1,
284 data
+ ctx
->o_offset
, ctx
->o_size
);
285 buffer
[ctx
->o_size
+ 0] = ':';
286 buffer
[ctx
->o_size
+ 1] = ' ';
287 memcpy(buffer
+ ctx
->o_size
+ 2,
288 data
+ ctx
->cn_offset
, ctx
->cn_size
);
289 buffer
[ctx
->o_size
+ 2 + ctx
->cn_size
] = 0;
292 } else if (ctx
->cn_size
) {
293 namesize
= ctx
->cn_size
;
294 name
= data
+ ctx
->cn_offset
;
295 } else if (ctx
->o_size
) {
296 namesize
= ctx
->o_size
;
297 name
= data
+ ctx
->o_offset
;
299 namesize
= ctx
->email_size
;
300 name
= data
+ ctx
->email_offset
;
304 buffer
= kmalloc(namesize
+ 1, GFP_KERNEL
);
307 memcpy(buffer
, name
, namesize
);
308 buffer
[namesize
] = 0;
318 int x509_note_issuer(void *context
, size_t hdrlen
,
320 const void *value
, size_t vlen
)
322 struct x509_parse_context
*ctx
= context
;
323 return x509_fabricate_name(ctx
, hdrlen
, tag
, &ctx
->cert
->issuer
, vlen
);
326 int x509_note_subject(void *context
, size_t hdrlen
,
328 const void *value
, size_t vlen
)
330 struct x509_parse_context
*ctx
= context
;
331 return x509_fabricate_name(ctx
, hdrlen
, tag
, &ctx
->cert
->subject
, vlen
);
335 * Extract the data for the public key algorithm
337 int x509_extract_key_data(void *context
, size_t hdrlen
,
339 const void *value
, size_t vlen
)
341 struct x509_parse_context
*ctx
= context
;
343 if (ctx
->last_oid
!= OID_rsaEncryption
)
346 /* There seems to be an extraneous 0 byte on the front of the data */
347 ctx
->cert
->pkey_algo
= PKEY_ALGO_RSA
;
348 ctx
->key
= value
+ 1;
349 ctx
->key_size
= vlen
- 1;
354 * Extract a RSA public key value
356 int rsa_extract_mpi(void *context
, size_t hdrlen
,
358 const void *value
, size_t vlen
)
360 struct x509_parse_context
*ctx
= context
;
363 if (ctx
->nr_mpi
>= ARRAY_SIZE(ctx
->cert
->pub
->mpi
)) {
364 pr_err("Too many public key MPIs in certificate\n");
368 mpi
= mpi_read_raw_data(value
, vlen
);
372 ctx
->cert
->pub
->mpi
[ctx
->nr_mpi
++] = mpi
;
376 /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
377 #define SEQ_TAG_KEYID (ASN1_CONT << 6)
380 * Process certificate extensions that are used to qualify the certificate.
382 int x509_process_extension(void *context
, size_t hdrlen
,
384 const void *value
, size_t vlen
)
386 struct x509_parse_context
*ctx
= context
;
387 const unsigned char *v
= value
;
391 pr_debug("Extension: %u\n", ctx
->last_oid
);
393 if (ctx
->last_oid
== OID_subjectKeyIdentifier
) {
394 /* Get hold of the key fingerprint */
397 if (v
[0] != ASN1_OTS
|| v
[1] != vlen
- 2)
402 f
= kmalloc(vlen
* 2 + 1, GFP_KERNEL
);
405 for (i
= 0; i
< vlen
; i
++)
406 sprintf(f
+ i
* 2, "%02x", v
[i
]);
407 pr_debug("fingerprint %s\n", f
);
408 ctx
->cert
->fingerprint
= f
;
412 if (ctx
->last_oid
== OID_authorityKeyIdentifier
) {
415 /* Get hold of the CA key fingerprint */
419 /* Authority Key Identifier must be a Constructed SEQUENCE */
420 if (v
[0] != (ASN1_SEQ
| (ASN1_CONS
<< 5)))
423 /* Authority Key Identifier is not indefinite length */
424 if (unlikely(vlen
== ASN1_INDEFINITE_LENGTH
))
427 if (vlen
< ASN1_INDEFINITE_LENGTH
) {
428 /* Short Form length */
429 if (v
[1] != vlen
- 2 ||
430 v
[2] != SEQ_TAG_KEYID
||
437 /* Long Form length */
439 size_t sub
= v
[1] - ASN1_INDEFINITE_LENGTH
;
444 /* calculate the length from subsequent octets */
446 for (i
= 0; i
< sub
; i
++) {
451 if (seq_len
!= vlen
- 2 - sub
||
452 v
[sub
] != SEQ_TAG_KEYID
||
453 v
[sub
+ 1] > vlen
- 4 - sub
)
456 key_len
= v
[sub
+ 1];
460 f
= kmalloc(key_len
* 2 + 1, GFP_KERNEL
);
463 for (i
= 0; i
< key_len
; i
++)
464 sprintf(f
+ i
* 2, "%02x", v
[i
]);
465 pr_debug("authority %s\n", f
);
466 ctx
->cert
->authority
= f
;
474 * Record a certificate time.
476 static int x509_note_time(struct tm
*tm
, size_t hdrlen
,
478 const unsigned char *value
, size_t vlen
)
480 const unsigned char *p
= value
;
482 #define dec2bin(X) ((X) - '0')
483 #define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })
485 if (tag
== ASN1_UNITIM
) {
486 /* UTCTime: YYMMDDHHMMSSZ */
488 goto unsupported_time
;
489 tm
->tm_year
= DD2bin(p
);
490 if (tm
->tm_year
>= 50)
494 } else if (tag
== ASN1_GENTIM
) {
495 /* GenTime: YYYYMMDDHHMMSSZ */
497 goto unsupported_time
;
498 tm
->tm_year
= DD2bin(p
) * 100 + DD2bin(p
);
500 goto unsupported_time
;
504 tm
->tm_mon
= DD2bin(p
) - 1;
505 tm
->tm_mday
= DD2bin(p
);
506 tm
->tm_hour
= DD2bin(p
);
507 tm
->tm_min
= DD2bin(p
);
508 tm
->tm_sec
= DD2bin(p
);
511 goto unsupported_time
;
516 pr_debug("Got unsupported time [tag %02x]: '%*.*s'\n",
517 tag
, (int)vlen
, (int)vlen
, value
);
521 int x509_note_not_before(void *context
, size_t hdrlen
,
523 const void *value
, size_t vlen
)
525 struct x509_parse_context
*ctx
= context
;
526 return x509_note_time(&ctx
->cert
->valid_from
, hdrlen
, tag
, value
, vlen
);
529 int x509_note_not_after(void *context
, size_t hdrlen
,
531 const void *value
, size_t vlen
)
533 struct x509_parse_context
*ctx
= context
;
534 return x509_note_time(&ctx
->cert
->valid_to
, hdrlen
, tag
, value
, vlen
);