~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/crypto/asymmetric_keys/x509_cert_parser.c

Version: ~ [ linux-5.14-rc1 ] ~ [ linux-5.13.1 ] ~ [ linux-5.12.16 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.49 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.131 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.197 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.239 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.275 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.275 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* 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 
 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 */
 37 };
 38 
 39 /*
 40  * Free an X.509 certificate
 41  */
 42 void 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);
 50                 kfree(cert->sig.digest);
 51                 mpi_free(cert->sig.rsa.s);
 52                 kfree(cert);
 53         }
 54 }
 55 
 56 /*
 57  * Parse an X.509 certificate
 58  */
 59 struct 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 
 93 error_decode:
 94         kfree(ctx);
 95 error_no_ctx:
 96         x509_free_certificate(cert);
 97 error_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  */
105 int 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));
115                 pr_debug("Unknown OID: [%lu] %s\n",
116                          (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  */
125 int 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  */
142 int 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:
157                 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_MD5;
158                 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
159                 break;
160 
161         case OID_sha1WithRSAEncryption:
162                 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA1;
163                 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
164                 break;
165 
166         case OID_sha256WithRSAEncryption:
167                 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA256;
168                 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
169                 break;
170 
171         case OID_sha384WithRSAEncryption:
172                 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA384;
173                 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
174                 break;
175 
176         case OID_sha512WithRSAEncryption:
177                 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA512;
178                 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
179                 break;
180 
181         case OID_sha224WithRSAEncryption:
182                 ctx->cert->sig.pkey_hash_algo = HASH_ALGO_SHA224;
183                 ctx->cert->sig.pkey_algo = PKEY_ALGO_RSA;
184                 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  */
194 int 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 
208         if (ctx->cert->sig.pkey_algo == PKEY_ALGO_RSA) {
209                 /* Discard the BIT STRING metadata */
210                 if (vlen < 1 || *(const u8 *)value != 0)
211                         return -EBADMSG;
212 
213                 value++;
214                 vlen--;
215         }
216 
217         ctx->cert->raw_sig = value;
218         ctx->cert->raw_sig_size = vlen;
219         return 0;
220 }
221 
222 /*
223  * Note some of the name segments from which we'll fabricate a name.
224  */
225 int x509_extract_name_segment(void *context, size_t hdrlen,
226                               unsigned char tag,
227                               const void *value, size_t vlen)
228 {
229         struct x509_parse_context *ctx = context;
230 
231         switch (ctx->last_oid) {
232         case OID_commonName:
233                 ctx->cn_size = vlen;
234                 ctx->cn_offset = (unsigned long)value - ctx->data;
235                 break;
236         case OID_organizationName:
237                 ctx->o_size = vlen;
238                 ctx->o_offset = (unsigned long)value - ctx->data;
239                 break;
240         case OID_email_address:
241                 ctx->email_size = vlen;
242                 ctx->email_offset = (unsigned long)value - ctx->data;
243                 break;
244         default:
245                 break;
246         }
247 
248         return 0;
249 }
250 
251 /*
252  * Fabricate and save the issuer and subject names
253  */
254 static int x509_fabricate_name(struct x509_parse_context *ctx, size_t hdrlen,
255                                unsigned char tag,
256                                char **_name, size_t vlen)
257 {
258         const void *name, *data = (const void *)ctx->data;
259         size_t namesize;
260         char *buffer;
261 
262         if (*_name)
263                 return -EINVAL;
264 
265         /* Empty name string if no material */
266         if (!ctx->cn_size && !ctx->o_size && !ctx->email_size) {
267                 buffer = kmalloc(1, GFP_KERNEL);
268                 if (!buffer)
269                         return -ENOMEM;
270                 buffer[0] = 0;
271                 goto done;
272         }
273 
274         if (ctx->cn_size && ctx->o_size) {
275                 /* Consider combining O and CN, but use only the CN if it is
276                  * prefixed by the O, or a significant portion thereof.
277                  */
278                 namesize = ctx->cn_size;
279                 name = data + ctx->cn_offset;
280                 if (ctx->cn_size >= ctx->o_size &&
281                     memcmp(data + ctx->cn_offset, data + ctx->o_offset,
282                            ctx->o_size) == 0)
283                         goto single_component;
284                 if (ctx->cn_size >= 7 &&
285                     ctx->o_size >= 7 &&
286                     memcmp(data + ctx->cn_offset, data + ctx->o_offset, 7) == 0)
287                         goto single_component;
288 
289                 buffer = kmalloc(ctx->o_size + 2 + ctx->cn_size + 1,
290                                  GFP_KERNEL);
291                 if (!buffer)
292                         return -ENOMEM;
293 
294                 memcpy(buffer,
295                        data + ctx->o_offset, ctx->o_size);
296                 buffer[ctx->o_size + 0] = ':';
297                 buffer[ctx->o_size + 1] = ' ';
298                 memcpy(buffer + ctx->o_size + 2,
299                        data + ctx->cn_offset, ctx->cn_size);
300                 buffer[ctx->o_size + 2 + ctx->cn_size] = 0;
301                 goto done;
302 
303         } else if (ctx->cn_size) {
304                 namesize = ctx->cn_size;
305                 name = data + ctx->cn_offset;
306         } else if (ctx->o_size) {
307                 namesize = ctx->o_size;
308                 name = data + ctx->o_offset;
309         } else {
310                 namesize = ctx->email_size;
311                 name = data + ctx->email_offset;
312         }
313 
314 single_component:
315         buffer = kmalloc(namesize + 1, GFP_KERNEL);
316         if (!buffer)
317                 return -ENOMEM;
318         memcpy(buffer, name, namesize);
319         buffer[namesize] = 0;
320 
321 done:
322         *_name = buffer;
323         ctx->cn_size = 0;
324         ctx->o_size = 0;
325         ctx->email_size = 0;
326         return 0;
327 }
328 
329 int x509_note_issuer(void *context, size_t hdrlen,
330                      unsigned char tag,
331                      const void *value, size_t vlen)
332 {
333         struct x509_parse_context *ctx = context;
334         return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->issuer, vlen);
335 }
336 
337 int x509_note_subject(void *context, size_t hdrlen,
338                       unsigned char tag,
339                       const void *value, size_t vlen)
340 {
341         struct x509_parse_context *ctx = context;
342         return x509_fabricate_name(ctx, hdrlen, tag, &ctx->cert->subject, vlen);
343 }
344 
345 /*
346  * Extract the data for the public key algorithm
347  */
348 int x509_extract_key_data(void *context, size_t hdrlen,
349                           unsigned char tag,
350                           const void *value, size_t vlen)
351 {
352         struct x509_parse_context *ctx = context;
353 
354         if (ctx->last_oid != OID_rsaEncryption)
355                 return -ENOPKG;
356 
357         ctx->cert->pub->pkey_algo = PKEY_ALGO_RSA;
358 
359         /* Discard the BIT STRING metadata */
360         if (vlen < 1 || *(const u8 *)value != 0)
361                 return -EBADMSG;
362         ctx->key = value + 1;
363         ctx->key_size = vlen - 1;
364         return 0;
365 }
366 
367 /*
368  * Extract a RSA public key value
369  */
370 int rsa_extract_mpi(void *context, size_t hdrlen,
371                     unsigned char tag,
372                     const void *value, size_t vlen)
373 {
374         struct x509_parse_context *ctx = context;
375         MPI mpi;
376 
377         if (ctx->nr_mpi >= ARRAY_SIZE(ctx->cert->pub->mpi)) {
378                 pr_err("Too many public key MPIs in certificate\n");
379                 return -EBADMSG;
380         }
381 
382         mpi = mpi_read_raw_data(value, vlen);
383         if (!mpi)
384                 return -ENOMEM;
385 
386         ctx->cert->pub->mpi[ctx->nr_mpi++] = mpi;
387         return 0;
388 }
389 
390 /* The keyIdentifier in AuthorityKeyIdentifier SEQUENCE is tag(CONT,PRIM,0) */
391 #define SEQ_TAG_KEYID (ASN1_CONT << 6)
392 
393 /*
394  * Process certificate extensions that are used to qualify the certificate.
395  */
396 int x509_process_extension(void *context, size_t hdrlen,
397                            unsigned char tag,
398                            const void *value, size_t vlen)
399 {
400         struct x509_parse_context *ctx = context;
401         const unsigned char *v = value;
402         char *f;
403         int i;
404 
405         pr_debug("Extension: %u\n", ctx->last_oid);
406 
407         if (ctx->last_oid == OID_subjectKeyIdentifier) {
408                 /* Get hold of the key fingerprint */
409                 if (vlen < 3)
410                         return -EBADMSG;
411                 if (v[0] != ASN1_OTS || v[1] != vlen - 2)
412                         return -EBADMSG;
413                 v += 2;
414                 vlen -= 2;
415 
416                 f = kmalloc(vlen * 2 + 1, GFP_KERNEL);
417                 if (!f)
418                         return -ENOMEM;
419                 for (i = 0; i < vlen; i++)
420                         sprintf(f + i * 2, "%02x", v[i]);
421                 pr_debug("fingerprint %s\n", f);
422                 ctx->cert->fingerprint = f;
423                 return 0;
424         }
425 
426         if (ctx->last_oid == OID_authorityKeyIdentifier) {
427                 size_t key_len;
428 
429                 /* Get hold of the CA key fingerprint */
430                 if (vlen < 5)
431                         return -EBADMSG;
432 
433                 /* Authority Key Identifier must be a Constructed SEQUENCE */
434                 if (v[0] != (ASN1_SEQ | (ASN1_CONS << 5)))
435                         return -EBADMSG;
436 
437                 /* Authority Key Identifier is not indefinite length */
438                 if (unlikely(vlen == ASN1_INDEFINITE_LENGTH))
439                         return -EBADMSG;
440 
441                 if (vlen < ASN1_INDEFINITE_LENGTH) {
442                         /* Short Form length */
443                         if (v[1] != vlen - 2 ||
444                             v[2] != SEQ_TAG_KEYID ||
445                             v[3] > vlen - 4)
446                                 return -EBADMSG;
447 
448                         key_len = v[3];
449                         v += 4;
450                 } else {
451                         /* Long Form length */
452                         size_t seq_len = 0;
453                         size_t sub = v[1] - ASN1_INDEFINITE_LENGTH;
454 
455                         if (sub > 2)
456                                 return -EBADMSG;
457 
458                         /* calculate the length from subsequent octets */
459                         v += 2;
460                         for (i = 0; i < sub; i++) {
461                                 seq_len <<= 8;
462                                 seq_len |= v[i];
463                         }
464 
465                         if (seq_len != vlen - 2 - sub ||
466                             v[sub] != SEQ_TAG_KEYID ||
467                             v[sub + 1] > vlen - 4 - sub)
468                                 return -EBADMSG;
469 
470                         key_len = v[sub + 1];
471                         v += (sub + 2);
472                 }
473 
474                 f = kmalloc(key_len * 2 + 1, GFP_KERNEL);
475                 if (!f)
476                         return -ENOMEM;
477                 for (i = 0; i < key_len; i++)
478                         sprintf(f + i * 2, "%02x", v[i]);
479                 pr_debug("authority   %s\n", f);
480                 ctx->cert->authority = f;
481                 return 0;
482         }
483 
484         return 0;
485 }
486 
487 /*
488  * Record a certificate time.
489  */
490 static int x509_note_time(struct tm *tm,  size_t hdrlen,
491                           unsigned char tag,
492                           const unsigned char *value, size_t vlen)
493 {
494         const unsigned char *p = value;
495 
496 #define dec2bin(X) ((X) - '')
497 #define DD2bin(P) ({ unsigned x = dec2bin(P[0]) * 10 + dec2bin(P[1]); P += 2; x; })
498 
499         if (tag == ASN1_UNITIM) {
500                 /* UTCTime: YYMMDDHHMMSSZ */
501                 if (vlen != 13)
502                         goto unsupported_time;
503                 tm->tm_year = DD2bin(p);
504                 if (tm->tm_year >= 50)
505                         tm->tm_year += 1900;
506                 else
507                         tm->tm_year += 2000;
508         } else if (tag == ASN1_GENTIM) {
509                 /* GenTime: YYYYMMDDHHMMSSZ */
510                 if (vlen != 15)
511                         goto unsupported_time;
512                 tm->tm_year = DD2bin(p) * 100 + DD2bin(p);
513         } else {
514                 goto unsupported_time;
515         }
516 
517         tm->tm_year -= 1900;
518         tm->tm_mon  = DD2bin(p) - 1;
519         tm->tm_mday = DD2bin(p);
520         tm->tm_hour = DD2bin(p);
521         tm->tm_min  = DD2bin(p);
522         tm->tm_sec  = DD2bin(p);
523 
524         if (*p != 'Z')
525                 goto unsupported_time;
526 
527         return 0;
528 
529 unsupported_time:
530         pr_debug("Got unsupported time [tag %02x]: '%*.*s'\n",
531                  tag, (int)vlen, (int)vlen, value);
532         return -EBADMSG;
533 }
534 
535 int x509_note_not_before(void *context, size_t hdrlen,
536                          unsigned char tag,
537                          const void *value, size_t vlen)
538 {
539         struct x509_parse_context *ctx = context;
540         return x509_note_time(&ctx->cert->valid_from, hdrlen, tag, value, vlen);
541 }
542 
543 int x509_note_not_after(void *context, size_t hdrlen,
544                         unsigned char tag,
545                         const void *value, size_t vlen)
546 {
547         struct x509_parse_context *ctx = context;
548         return x509_note_time(&ctx->cert->valid_to, hdrlen, tag, value, vlen);
549 }
550 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp