1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #define pr_fmt(fmt) "PKCS7: "fmt
11 #include <dm/devres.h>
12 #include <linux/bitops.h>
13 #include <linux/compat.h>
15 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/export.h>
19 #include <linux/slab.h>
21 #include <linux/err.h>
22 #include <linux/oid_registry.h>
23 #include <crypto/public_key.h>
25 #include <crypto/pkcs7_parser.h>
27 #include "pkcs7_parser.h"
29 #include "pkcs7.asn1.h"
31 MODULE_DESCRIPTION("PKCS#7 parser");
32 MODULE_AUTHOR("Red Hat, Inc.");
33 MODULE_LICENSE("GPL");
35 struct pkcs7_parse_context {
36 struct pkcs7_message *msg; /* Message being constructed */
37 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
38 struct pkcs7_signed_info **ppsinfo;
39 struct x509_certificate *certs; /* Certificate cache */
40 struct x509_certificate **ppcerts;
41 unsigned long data; /* Start of data */
42 enum OID last_oid; /* Last OID encountered */
45 const void *raw_serial;
46 unsigned raw_serial_size;
47 unsigned raw_issuer_size;
48 const void *raw_issuer;
50 unsigned raw_skid_size;
55 * Free a signed information block.
57 static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
60 public_key_signature_free(sinfo->sig);
66 * pkcs7_free_message - Free a PKCS#7 message
67 * @pkcs7: The PKCS#7 message to free
69 void pkcs7_free_message(struct pkcs7_message *pkcs7)
71 struct x509_certificate *cert;
72 struct pkcs7_signed_info *sinfo;
75 while (pkcs7->certs) {
77 pkcs7->certs = cert->next;
78 x509_free_certificate(cert);
82 pkcs7->crl = cert->next;
83 x509_free_certificate(cert);
85 while (pkcs7->signed_infos) {
86 sinfo = pkcs7->signed_infos;
87 pkcs7->signed_infos = sinfo->next;
88 pkcs7_free_signed_info(sinfo);
93 EXPORT_SYMBOL_GPL(pkcs7_free_message);
96 * Check authenticatedAttributes are provided or not provided consistently.
98 static int pkcs7_check_authattrs(struct pkcs7_message *msg)
100 struct pkcs7_signed_info *sinfo;
103 sinfo = msg->signed_infos;
107 if (sinfo->authattrs) {
109 msg->have_authattrs = true;
112 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
113 if (!!sinfo->authattrs != want)
118 pr_warn("Inconsistently supplied authAttrs\n");
123 * pkcs7_parse_message - Parse a PKCS#7 message
124 * @data: The raw binary ASN.1 encoded message to be parsed
125 * @datalen: The size of the encoded message
127 struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
129 struct pkcs7_parse_context *ctx;
130 struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
133 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
136 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
139 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
142 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
144 if (!ctx->sinfo->sig)
147 ctx->data = (unsigned long)data;
148 ctx->ppcerts = &ctx->certs;
149 ctx->ppsinfo = &ctx->msg->signed_infos;
151 /* Attempt to decode the signature */
152 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
158 ret = pkcs7_check_authattrs(ctx->msg);
169 struct x509_certificate *cert = ctx->certs;
170 ctx->certs = cert->next;
171 x509_free_certificate(cert);
174 pkcs7_free_signed_info(ctx->sinfo);
176 pkcs7_free_message(ctx->msg);
182 EXPORT_SYMBOL_GPL(pkcs7_parse_message);
185 * pkcs7_get_content_data - Get access to the PKCS#7 content
186 * @pkcs7: The preparsed PKCS#7 message to access
187 * @_data: Place to return a pointer to the data
188 * @_data_len: Place to return the data length
189 * @_headerlen: Size of ASN.1 header not included in _data
191 * Get access to the data content of the PKCS#7 message. The size of the
192 * header of the ASN.1 object that contains it is also provided and can be used
193 * to adjust *_data and *_data_len to get the entire object.
195 * Returns -ENODATA if the data object was missing from the message.
197 int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
198 const void **_data, size_t *_data_len,
204 *_data = pkcs7->data;
205 *_data_len = pkcs7->data_len;
207 *_headerlen = pkcs7->data_hdrlen;
210 EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
213 * Note an OID when we find one for later processing when we know how
216 int pkcs7_note_OID(void *context, size_t hdrlen,
218 const void *value, size_t vlen)
220 struct pkcs7_parse_context *ctx = context;
222 ctx->last_oid = look_up_OID(value, vlen);
223 if (ctx->last_oid == OID__NR) {
225 sprint_oid(value, vlen, buffer, sizeof(buffer));
226 printk("PKCS7: Unknown OID: [%lu] %s\n",
227 (unsigned long)value - ctx->data, buffer);
233 * Note the digest algorithm for the signature.
235 int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
237 const void *value, size_t vlen)
239 struct pkcs7_parse_context *ctx = context;
241 switch (ctx->last_oid) {
243 ctx->sinfo->sig->hash_algo = "md4";
246 ctx->sinfo->sig->hash_algo = "md5";
249 ctx->sinfo->sig->hash_algo = "sha1";
252 ctx->sinfo->sig->hash_algo = "sha256";
255 ctx->sinfo->sig->hash_algo = "sha384";
258 ctx->sinfo->sig->hash_algo = "sha512";
261 ctx->sinfo->sig->hash_algo = "sha224";
264 printk("Unsupported digest algo: %u\n", ctx->last_oid);
271 * Note the public key algorithm for the signature.
273 int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
275 const void *value, size_t vlen)
277 struct pkcs7_parse_context *ctx = context;
279 switch (ctx->last_oid) {
280 case OID_rsaEncryption:
281 ctx->sinfo->sig->pkey_algo = "rsa";
282 ctx->sinfo->sig->encoding = "pkcs1";
285 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
292 * We only support signed data [RFC2315 sec 9].
294 int pkcs7_check_content_type(void *context, size_t hdrlen,
296 const void *value, size_t vlen)
298 struct pkcs7_parse_context *ctx = context;
300 if (ctx->last_oid != OID_signed_data) {
301 pr_warn("Only support pkcs7_signedData type\n");
309 * Note the SignedData version
311 int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
313 const void *value, size_t vlen)
315 struct pkcs7_parse_context *ctx = context;
321 ctx->msg->version = version = *(const u8 *)value;
324 /* PKCS#7 SignedData [RFC2315 sec 9.1]
325 * CMS ver 1 SignedData [RFC5652 sec 5.1]
329 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
338 pr_warn("Unsupported SignedData version\n");
343 * Note the SignerInfo version
345 int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
347 const void *value, size_t vlen)
349 struct pkcs7_parse_context *ctx = context;
355 version = *(const u8 *)value;
358 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
359 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
361 if (ctx->msg->version != 1)
362 goto version_mismatch;
363 ctx->expect_skid = false;
366 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
367 if (ctx->msg->version == 1)
368 goto version_mismatch;
369 ctx->expect_skid = true;
378 pr_warn("Unsupported SignerInfo version\n");
381 pr_warn("SignedData-SignerInfo version mismatch\n");
386 * Extract a certificate and store it in the context.
388 int pkcs7_extract_cert(void *context, size_t hdrlen,
390 const void *value, size_t vlen)
392 struct pkcs7_parse_context *ctx = context;
393 struct x509_certificate *x509;
395 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
396 pr_debug("Cert began with tag %02x at %lu\n",
397 tag, (unsigned long)ctx - ctx->data);
401 /* We have to correct for the header so that the X.509 parser can start
402 * from the beginning. Note that since X.509 stipulates DER, there
403 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
409 if (((u8*)value)[1] == 0x80)
410 vlen += 2; /* Indefinite length - there should be an EOC */
412 x509 = x509_cert_parse(value, vlen);
414 return PTR_ERR(x509);
416 x509->index = ++ctx->x509_index;
417 pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
418 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
420 *ctx->ppcerts = x509;
421 ctx->ppcerts = &x509->next;
426 * Save the certificate list
428 int pkcs7_note_certificate_list(void *context, size_t hdrlen,
430 const void *value, size_t vlen)
432 struct pkcs7_parse_context *ctx = context;
434 pr_devel("Got cert list (%02x)\n", tag);
436 *ctx->ppcerts = ctx->msg->certs;
437 ctx->msg->certs = ctx->certs;
439 ctx->ppcerts = &ctx->certs;
444 * Note the content type.
446 int pkcs7_note_content(void *context, size_t hdrlen,
448 const void *value, size_t vlen)
450 struct pkcs7_parse_context *ctx = context;
452 if (ctx->last_oid != OID_data &&
453 ctx->last_oid != OID_msIndirectData) {
454 pr_warn("Unsupported data type %d\n", ctx->last_oid);
458 ctx->msg->data_type = ctx->last_oid;
463 * Extract the data from the message and store that and its content type OID in
466 int pkcs7_note_data(void *context, size_t hdrlen,
468 const void *value, size_t vlen)
470 struct pkcs7_parse_context *ctx = context;
472 pr_debug("Got data\n");
474 ctx->msg->data = value;
475 ctx->msg->data_len = vlen;
476 ctx->msg->data_hdrlen = hdrlen;
481 * Parse authenticated attributes.
483 int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
485 const void *value, size_t vlen)
487 struct pkcs7_parse_context *ctx = context;
488 struct pkcs7_signed_info *sinfo = ctx->sinfo;
489 enum OID content_type;
491 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
493 switch (ctx->last_oid) {
494 case OID_contentType:
495 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
497 content_type = look_up_OID(value, vlen);
498 if (content_type != ctx->msg->data_type) {
499 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
500 ctx->msg->data_type, sinfo->index,
506 case OID_signingTime:
507 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
509 /* Should we check that the signing time is consistent
510 * with the signer's X.509 cert?
512 return x509_decode_time(&sinfo->signing_time,
513 hdrlen, tag, value, vlen);
515 case OID_messageDigest:
516 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
520 sinfo->msgdigest = value;
521 sinfo->msgdigest_len = vlen;
524 case OID_smimeCapabilites:
525 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
527 #ifdef __UBOOT__ /* OID_data is needed for authenticated UEFI variables */
528 if (ctx->msg->data_type != OID_msIndirectData &&
529 ctx->msg->data_type != OID_data) {
531 if (ctx->msg->data_type != OID_msIndirectData) {
533 pr_warn("S/MIME Caps only allowed with Authenticode\n");
534 return -EKEYREJECTED;
538 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
539 * char URLs and cont[1] 8-bit char URLs.
541 * Microsoft StatementType seems to contain a list of OIDs that
542 * are also used as extendedKeyUsage types in X.509 certs.
544 case OID_msSpOpusInfo:
545 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
547 goto authenticode_check;
548 case OID_msStatementType:
549 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
552 if (ctx->msg->data_type != OID_msIndirectData) {
553 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
554 return -EKEYREJECTED;
556 /* I'm not sure how to validate these */
563 /* We permit max one item per AuthenticatedAttribute and no repeats */
564 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
565 return -EKEYREJECTED;
569 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
571 int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
573 const void *value, size_t vlen)
575 struct pkcs7_parse_context *ctx = context;
576 struct pkcs7_signed_info *sinfo = ctx->sinfo;
578 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
579 !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
580 pr_warn("Missing required AuthAttr\n");
584 if (ctx->msg->data_type != OID_msIndirectData &&
585 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
586 pr_warn("Unexpected Authenticode AuthAttr\n");
590 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
591 sinfo->authattrs = value - (hdrlen - 1);
592 sinfo->authattrs_len = vlen + (hdrlen - 1);
597 * Note the issuing certificate serial number
599 int pkcs7_sig_note_serial(void *context, size_t hdrlen,
601 const void *value, size_t vlen)
603 struct pkcs7_parse_context *ctx = context;
604 ctx->raw_serial = value;
605 ctx->raw_serial_size = vlen;
610 * Note the issuer's name
612 int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
614 const void *value, size_t vlen)
616 struct pkcs7_parse_context *ctx = context;
617 ctx->raw_issuer = value;
618 ctx->raw_issuer_size = vlen;
623 * Note the issuing cert's subjectKeyIdentifier
625 int pkcs7_sig_note_skid(void *context, size_t hdrlen,
627 const void *value, size_t vlen)
629 struct pkcs7_parse_context *ctx = context;
631 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
633 ctx->raw_skid = value;
634 ctx->raw_skid_size = vlen;
639 * Note the signature data
641 int pkcs7_sig_note_signature(void *context, size_t hdrlen,
643 const void *value, size_t vlen)
645 struct pkcs7_parse_context *ctx = context;
647 ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
648 if (!ctx->sinfo->sig->s)
651 ctx->sinfo->sig->s_size = vlen;
656 * Note a signature information block
658 int pkcs7_note_signed_info(void *context, size_t hdrlen,
660 const void *value, size_t vlen)
662 struct pkcs7_parse_context *ctx = context;
663 struct pkcs7_signed_info *sinfo = ctx->sinfo;
664 struct asymmetric_key_id *kid;
666 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
667 pr_warn("Authenticode requires AuthAttrs\n");
671 /* Generate cert issuer + serial number key ID */
672 if (!ctx->expect_skid) {
673 kid = asymmetric_key_generate_id(ctx->raw_serial,
674 ctx->raw_serial_size,
676 ctx->raw_issuer_size);
678 kid = asymmetric_key_generate_id(ctx->raw_skid,
685 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
687 sinfo->sig->auth_ids[0] = kid;
688 sinfo->index = ++ctx->sinfo_index;
689 *ctx->ppsinfo = sinfo;
690 ctx->ppsinfo = &sinfo->next;
691 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
694 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
696 if (!ctx->sinfo->sig)