2516e97c58f1b5d98efb0d88692bfe167baaae97
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / crypto / asymmetric_keys / pkcs7_parser.c
1 /* PKCS#7 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) "PKCS7: "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 "pkcs7_parser.h"
20 #include "pkcs7-asn1.h"
21
22 struct pkcs7_parse_context {
23 struct pkcs7_message *msg; /* Message being constructed */
24 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
25 struct pkcs7_signed_info **ppsinfo;
26 struct x509_certificate *certs; /* Certificate cache */
27 struct x509_certificate **ppcerts;
28 unsigned long data; /* Start of data */
29 enum OID last_oid; /* Last OID encountered */
30 unsigned x509_index;
31 unsigned sinfo_index;
32 const void *raw_serial;
33 unsigned raw_serial_size;
34 unsigned raw_issuer_size;
35 const void *raw_issuer;
36 const void *raw_skid;
37 unsigned raw_skid_size;
38 bool expect_skid;
39 };
40
41 /*
42 * Free a signed information block.
43 */
44 static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
45 {
46 if (sinfo) {
47 mpi_free(sinfo->sig.mpi[0]);
48 kfree(sinfo->sig.digest);
49 kfree(sinfo->signing_cert_id);
50 kfree(sinfo);
51 }
52 }
53
54 /**
55 * pkcs7_free_message - Free a PKCS#7 message
56 * @pkcs7: The PKCS#7 message to free
57 */
58 void pkcs7_free_message(struct pkcs7_message *pkcs7)
59 {
60 struct x509_certificate *cert;
61 struct pkcs7_signed_info *sinfo;
62
63 if (pkcs7) {
64 while (pkcs7->certs) {
65 cert = pkcs7->certs;
66 pkcs7->certs = cert->next;
67 x509_free_certificate(cert);
68 }
69 while (pkcs7->crl) {
70 cert = pkcs7->crl;
71 pkcs7->crl = cert->next;
72 x509_free_certificate(cert);
73 }
74 while (pkcs7->signed_infos) {
75 sinfo = pkcs7->signed_infos;
76 pkcs7->signed_infos = sinfo->next;
77 pkcs7_free_signed_info(sinfo);
78 }
79 kfree(pkcs7);
80 }
81 }
82 EXPORT_SYMBOL_GPL(pkcs7_free_message);
83
84 /*
85 * Check authenticatedAttributes are provided or not provided consistently.
86 */
87 static int pkcs7_check_authattrs(struct pkcs7_message *msg)
88 {
89 struct pkcs7_signed_info *sinfo;
90 bool want;
91
92 sinfo = msg->signed_infos;
93 if (!sinfo)
94 goto inconsistent;
95
96 if (sinfo->authattrs) {
97 want = true;
98 msg->have_authattrs = true;
99 }
100
101 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
102 if (!!sinfo->authattrs != want)
103 goto inconsistent;
104 return 0;
105
106 inconsistent:
107 pr_warn("Inconsistently supplied authAttrs\n");
108 return -EINVAL;
109 }
110
111 /**
112 * pkcs7_parse_message - Parse a PKCS#7 message
113 * @data: The raw binary ASN.1 encoded message to be parsed
114 * @datalen: The size of the encoded message
115 */
116 struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
117 {
118 struct pkcs7_parse_context *ctx;
119 struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
120 int ret;
121
122 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
123 if (!ctx)
124 goto out_no_ctx;
125 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
126 if (!ctx->msg)
127 goto out_no_msg;
128 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
129 if (!ctx->sinfo)
130 goto out_no_sinfo;
131
132 ctx->data = (unsigned long)data;
133 ctx->ppcerts = &ctx->certs;
134 ctx->ppsinfo = &ctx->msg->signed_infos;
135
136 /* Attempt to decode the signature */
137 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
138 if (ret < 0) {
139 msg = ERR_PTR(ret);
140 goto out;
141 }
142
143 ret = pkcs7_check_authattrs(ctx->msg);
144 if (ret < 0)
145 goto out;
146
147 msg = ctx->msg;
148 ctx->msg = NULL;
149
150 out:
151 while (ctx->certs) {
152 struct x509_certificate *cert = ctx->certs;
153 ctx->certs = cert->next;
154 x509_free_certificate(cert);
155 }
156 pkcs7_free_signed_info(ctx->sinfo);
157 out_no_sinfo:
158 pkcs7_free_message(ctx->msg);
159 out_no_msg:
160 kfree(ctx);
161 out_no_ctx:
162 return msg;
163 }
164 EXPORT_SYMBOL_GPL(pkcs7_parse_message);
165
166 /**
167 * pkcs7_get_content_data - Get access to the PKCS#7 content
168 * @pkcs7: The preparsed PKCS#7 message to access
169 * @_data: Place to return a pointer to the data
170 * @_data_len: Place to return the data length
171 * @want_wrapper: True if the ASN.1 object header should be included in the data
172 *
173 * Get access to the data content of the PKCS#7 message, including, optionally,
174 * the header of the ASN.1 object that contains it. Returns -ENODATA if the
175 * data object was missing from the message.
176 */
177 int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
178 const void **_data, size_t *_data_len,
179 bool want_wrapper)
180 {
181 size_t wrapper;
182
183 if (!pkcs7->data)
184 return -ENODATA;
185
186 wrapper = want_wrapper ? pkcs7->data_hdrlen : 0;
187 *_data = pkcs7->data - wrapper;
188 *_data_len = pkcs7->data_len + wrapper;
189 return 0;
190 }
191 EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
192
193 /*
194 * Note an OID when we find one for later processing when we know how
195 * to interpret it.
196 */
197 int pkcs7_note_OID(void *context, size_t hdrlen,
198 unsigned char tag,
199 const void *value, size_t vlen)
200 {
201 struct pkcs7_parse_context *ctx = context;
202
203 ctx->last_oid = look_up_OID(value, vlen);
204 if (ctx->last_oid == OID__NR) {
205 char buffer[50];
206 sprint_oid(value, vlen, buffer, sizeof(buffer));
207 printk("PKCS7: Unknown OID: [%lu] %s\n",
208 (unsigned long)value - ctx->data, buffer);
209 }
210 return 0;
211 }
212
213 /*
214 * Note the digest algorithm for the signature.
215 */
216 int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
217 unsigned char tag,
218 const void *value, size_t vlen)
219 {
220 struct pkcs7_parse_context *ctx = context;
221
222 switch (ctx->last_oid) {
223 case OID_md4:
224 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD4;
225 break;
226 case OID_md5:
227 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_MD5;
228 break;
229 case OID_sha1:
230 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA1;
231 break;
232 case OID_sha256:
233 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA256;
234 break;
235 case OID_sha384:
236 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA384;
237 break;
238 case OID_sha512:
239 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA512;
240 break;
241 case OID_sha224:
242 ctx->sinfo->sig.pkey_hash_algo = HASH_ALGO_SHA224;
243 default:
244 printk("Unsupported digest algo: %u\n", ctx->last_oid);
245 return -ENOPKG;
246 }
247 return 0;
248 }
249
250 /*
251 * Note the public key algorithm for the signature.
252 */
253 int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
254 unsigned char tag,
255 const void *value, size_t vlen)
256 {
257 struct pkcs7_parse_context *ctx = context;
258
259 switch (ctx->last_oid) {
260 case OID_rsaEncryption:
261 ctx->sinfo->sig.pkey_algo = PKEY_ALGO_RSA;
262 break;
263 default:
264 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
265 return -ENOPKG;
266 }
267 return 0;
268 }
269
270 /*
271 * We only support signed data [RFC2315 sec 9].
272 */
273 int pkcs7_check_content_type(void *context, size_t hdrlen,
274 unsigned char tag,
275 const void *value, size_t vlen)
276 {
277 struct pkcs7_parse_context *ctx = context;
278
279 if (ctx->last_oid != OID_signed_data) {
280 pr_warn("Only support pkcs7_signedData type\n");
281 return -EINVAL;
282 }
283
284 return 0;
285 }
286
287 /*
288 * Note the SignedData version
289 */
290 int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
291 unsigned char tag,
292 const void *value, size_t vlen)
293 {
294 struct pkcs7_parse_context *ctx = context;
295 unsigned version;
296
297 if (vlen != 1)
298 goto unsupported;
299
300 ctx->msg->version = version = *(const u8 *)value;
301 switch (version) {
302 case 1:
303 /* PKCS#7 SignedData [RFC2315 sec 9.1]
304 * CMS ver 1 SignedData [RFC5652 sec 5.1]
305 */
306 break;
307 case 3:
308 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
309 break;
310 default:
311 goto unsupported;
312 }
313
314 return 0;
315
316 unsupported:
317 pr_warn("Unsupported SignedData version\n");
318 return -EINVAL;
319 }
320
321 /*
322 * Note the SignerInfo version
323 */
324 int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
325 unsigned char tag,
326 const void *value, size_t vlen)
327 {
328 struct pkcs7_parse_context *ctx = context;
329 unsigned version;
330
331 if (vlen != 1)
332 goto unsupported;
333
334 version = *(const u8 *)value;
335 switch (version) {
336 case 1:
337 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
338 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
339 */
340 if (ctx->msg->version != 1)
341 goto version_mismatch;
342 ctx->expect_skid = false;
343 break;
344 case 3:
345 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
346 if (ctx->msg->version == 1)
347 goto version_mismatch;
348 ctx->expect_skid = true;
349 break;
350 default:
351 goto unsupported;
352 }
353
354 return 0;
355
356 unsupported:
357 pr_warn("Unsupported SignerInfo version\n");
358 return -EINVAL;
359 version_mismatch:
360 pr_warn("SignedData-SignerInfo version mismatch\n");
361 return -EBADMSG;
362 }
363
364 /*
365 * Extract a certificate and store it in the context.
366 */
367 int pkcs7_extract_cert(void *context, size_t hdrlen,
368 unsigned char tag,
369 const void *value, size_t vlen)
370 {
371 struct pkcs7_parse_context *ctx = context;
372 struct x509_certificate *x509;
373
374 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
375 pr_debug("Cert began with tag %02x at %lu\n",
376 tag, (unsigned long)ctx - ctx->data);
377 return -EBADMSG;
378 }
379
380 /* We have to correct for the header so that the X.509 parser can start
381 * from the beginning. Note that since X.509 stipulates DER, there
382 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
383 * stipulates BER).
384 */
385 value -= hdrlen;
386 vlen += hdrlen;
387
388 if (((u8*)value)[1] == 0x80)
389 vlen += 2; /* Indefinite length - there should be an EOC */
390
391 x509 = x509_cert_parse(value, vlen);
392 if (IS_ERR(x509))
393 return PTR_ERR(x509);
394
395 x509->index = ++ctx->x509_index;
396 pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
397 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
398
399 *ctx->ppcerts = x509;
400 ctx->ppcerts = &x509->next;
401 return 0;
402 }
403
404 /*
405 * Save the certificate list
406 */
407 int pkcs7_note_certificate_list(void *context, size_t hdrlen,
408 unsigned char tag,
409 const void *value, size_t vlen)
410 {
411 struct pkcs7_parse_context *ctx = context;
412
413 pr_devel("Got cert list (%02x)\n", tag);
414
415 *ctx->ppcerts = ctx->msg->certs;
416 ctx->msg->certs = ctx->certs;
417 ctx->certs = NULL;
418 ctx->ppcerts = &ctx->certs;
419 return 0;
420 }
421
422 /*
423 * Note the content type.
424 */
425 int pkcs7_note_content(void *context, size_t hdrlen,
426 unsigned char tag,
427 const void *value, size_t vlen)
428 {
429 struct pkcs7_parse_context *ctx = context;
430
431 if (ctx->last_oid != OID_data &&
432 ctx->last_oid != OID_msIndirectData) {
433 pr_warn("Unsupported data type %d\n", ctx->last_oid);
434 return -EINVAL;
435 }
436
437 ctx->msg->data_type = ctx->last_oid;
438 return 0;
439 }
440
441 /*
442 * Extract the data from the message and store that and its content type OID in
443 * the context.
444 */
445 int pkcs7_note_data(void *context, size_t hdrlen,
446 unsigned char tag,
447 const void *value, size_t vlen)
448 {
449 struct pkcs7_parse_context *ctx = context;
450
451 pr_debug("Got data\n");
452
453 ctx->msg->data = value;
454 ctx->msg->data_len = vlen;
455 ctx->msg->data_hdrlen = hdrlen;
456 return 0;
457 }
458
459 /*
460 * Parse authenticated attributes.
461 */
462 int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
463 unsigned char tag,
464 const void *value, size_t vlen)
465 {
466 struct pkcs7_parse_context *ctx = context;
467 struct pkcs7_signed_info *sinfo = ctx->sinfo;
468 enum OID content_type;
469
470 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
471
472 switch (ctx->last_oid) {
473 case OID_contentType:
474 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
475 goto repeated;
476 content_type = look_up_OID(value, vlen);
477 if (content_type != ctx->msg->data_type) {
478 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
479 ctx->msg->data_type, sinfo->index,
480 content_type);
481 return -EBADMSG;
482 }
483 return 0;
484
485 case OID_signingTime:
486 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
487 goto repeated;
488 /* Should we check that the signing time is consistent
489 * with the signer's X.509 cert?
490 */
491 return x509_decode_time(&sinfo->signing_time,
492 hdrlen, tag, value, vlen);
493
494 case OID_messageDigest:
495 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
496 goto repeated;
497 if (tag != ASN1_OTS)
498 return -EBADMSG;
499 sinfo->msgdigest = value;
500 sinfo->msgdigest_len = vlen;
501 return 0;
502
503 case OID_smimeCapabilites:
504 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
505 goto repeated;
506 if (ctx->msg->data_type != OID_msIndirectData) {
507 pr_warn("S/MIME Caps only allowed with Authenticode\n");
508 return -EKEYREJECTED;
509 }
510 return 0;
511
512 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
513 * char URLs and cont[1] 8-bit char URLs.
514 *
515 * Microsoft StatementType seems to contain a list of OIDs that
516 * are also used as extendedKeyUsage types in X.509 certs.
517 */
518 case OID_msSpOpusInfo:
519 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
520 goto repeated;
521 goto authenticode_check;
522 case OID_msStatementType:
523 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
524 goto repeated;
525 authenticode_check:
526 if (ctx->msg->data_type != OID_msIndirectData) {
527 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
528 return -EKEYREJECTED;
529 }
530 /* I'm not sure how to validate these */
531 return 0;
532 default:
533 return 0;
534 }
535
536 repeated:
537 /* We permit max one item per AuthenticatedAttribute and no repeats */
538 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
539 return -EKEYREJECTED;
540 }
541
542 /*
543 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
544 */
545 int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
546 unsigned char tag,
547 const void *value, size_t vlen)
548 {
549 struct pkcs7_parse_context *ctx = context;
550 struct pkcs7_signed_info *sinfo = ctx->sinfo;
551
552 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
553 !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
554 pr_warn("Missing required AuthAttr\n");
555 return -EBADMSG;
556 }
557
558 if (ctx->msg->data_type != OID_msIndirectData &&
559 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
560 pr_warn("Unexpected Authenticode AuthAttr\n");
561 return -EBADMSG;
562 }
563
564 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
565 sinfo->authattrs = value - (hdrlen - 1);
566 sinfo->authattrs_len = vlen + (hdrlen - 1);
567 return 0;
568 }
569
570 /*
571 * Note the issuing certificate serial number
572 */
573 int pkcs7_sig_note_serial(void *context, size_t hdrlen,
574 unsigned char tag,
575 const void *value, size_t vlen)
576 {
577 struct pkcs7_parse_context *ctx = context;
578 ctx->raw_serial = value;
579 ctx->raw_serial_size = vlen;
580 return 0;
581 }
582
583 /*
584 * Note the issuer's name
585 */
586 int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
587 unsigned char tag,
588 const void *value, size_t vlen)
589 {
590 struct pkcs7_parse_context *ctx = context;
591 ctx->raw_issuer = value;
592 ctx->raw_issuer_size = vlen;
593 return 0;
594 }
595
596 /*
597 * Note the issuing cert's subjectKeyIdentifier
598 */
599 int pkcs7_sig_note_skid(void *context, size_t hdrlen,
600 unsigned char tag,
601 const void *value, size_t vlen)
602 {
603 struct pkcs7_parse_context *ctx = context;
604
605 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
606
607 ctx->raw_skid = value;
608 ctx->raw_skid_size = vlen;
609 return 0;
610 }
611
612 /*
613 * Note the signature data
614 */
615 int pkcs7_sig_note_signature(void *context, size_t hdrlen,
616 unsigned char tag,
617 const void *value, size_t vlen)
618 {
619 struct pkcs7_parse_context *ctx = context;
620 MPI mpi;
621
622 BUG_ON(ctx->sinfo->sig.pkey_algo != PKEY_ALGO_RSA);
623
624 mpi = mpi_read_raw_data(value, vlen);
625 if (!mpi)
626 return -ENOMEM;
627
628 ctx->sinfo->sig.mpi[0] = mpi;
629 ctx->sinfo->sig.nr_mpi = 1;
630 return 0;
631 }
632
633 /*
634 * Note a signature information block
635 */
636 int pkcs7_note_signed_info(void *context, size_t hdrlen,
637 unsigned char tag,
638 const void *value, size_t vlen)
639 {
640 struct pkcs7_parse_context *ctx = context;
641 struct pkcs7_signed_info *sinfo = ctx->sinfo;
642 struct asymmetric_key_id *kid;
643
644 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
645 pr_warn("Authenticode requires AuthAttrs\n");
646 return -EBADMSG;
647 }
648
649 /* Generate cert issuer + serial number key ID */
650 if (!ctx->expect_skid) {
651 kid = asymmetric_key_generate_id(ctx->raw_serial,
652 ctx->raw_serial_size,
653 ctx->raw_issuer,
654 ctx->raw_issuer_size);
655 } else {
656 kid = asymmetric_key_generate_id(ctx->raw_skid,
657 ctx->raw_skid_size,
658 "", 0);
659 }
660 if (IS_ERR(kid))
661 return PTR_ERR(kid);
662
663 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
664
665 sinfo->signing_cert_id = kid;
666 sinfo->index = ++ctx->sinfo_index;
667 *ctx->ppsinfo = sinfo;
668 ctx->ppsinfo = &sinfo->next;
669 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
670 if (!ctx->sinfo)
671 return -ENOMEM;
672 return 0;
673 }