1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
8 #define pr_fmt(fmt) "PKCS7: "fmt
11 #include <dm/devres.h>
12 #include <linux/bitops.h>
13 #include <linux/compat.h>
14 #include <linux/printk.h>
16 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/export.h>
20 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/oid_registry.h>
24 #include <crypto/public_key.h>
26 #include <crypto/pkcs7_parser.h>
28 #include "pkcs7_parser.h"
30 #include "pkcs7.asn1.h"
32 MODULE_DESCRIPTION("PKCS#7 parser");
33 MODULE_AUTHOR("Red Hat, Inc.");
34 MODULE_LICENSE("GPL");
36 struct pkcs7_parse_context {
37 struct pkcs7_message *msg; /* Message being constructed */
38 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
39 struct pkcs7_signed_info **ppsinfo;
40 struct x509_certificate *certs; /* Certificate cache */
41 struct x509_certificate **ppcerts;
42 unsigned long data; /* Start of data */
43 enum OID last_oid; /* Last OID encountered */
46 const void *raw_serial;
47 unsigned raw_serial_size;
48 unsigned raw_issuer_size;
49 const void *raw_issuer;
51 unsigned raw_skid_size;
56 * Free a signed information block.
58 static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
61 public_key_signature_free(sinfo->sig);
67 * pkcs7_free_message - Free a PKCS#7 message
68 * @pkcs7: The PKCS#7 message to free
70 void pkcs7_free_message(struct pkcs7_message *pkcs7)
72 struct x509_certificate *cert;
73 struct pkcs7_signed_info *sinfo;
76 while (pkcs7->certs) {
78 pkcs7->certs = cert->next;
79 x509_free_certificate(cert);
83 pkcs7->crl = cert->next;
84 x509_free_certificate(cert);
86 while (pkcs7->signed_infos) {
87 sinfo = pkcs7->signed_infos;
88 pkcs7->signed_infos = sinfo->next;
89 pkcs7_free_signed_info(sinfo);
94 EXPORT_SYMBOL_GPL(pkcs7_free_message);
97 * Check authenticatedAttributes are provided or not provided consistently.
99 static int pkcs7_check_authattrs(struct pkcs7_message *msg)
101 struct pkcs7_signed_info *sinfo;
104 sinfo = msg->signed_infos;
108 if (sinfo->authattrs) {
110 msg->have_authattrs = true;
113 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
114 if (!!sinfo->authattrs != want)
119 pr_warn("Inconsistently supplied authAttrs\n");
124 * pkcs7_parse_message - Parse a PKCS#7 message
125 * @data: The raw binary ASN.1 encoded message to be parsed
126 * @datalen: The size of the encoded message
128 struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
130 struct pkcs7_parse_context *ctx;
131 struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
134 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
137 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
140 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
143 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
145 if (!ctx->sinfo->sig)
148 ctx->data = (unsigned long)data;
149 ctx->ppcerts = &ctx->certs;
150 ctx->ppsinfo = &ctx->msg->signed_infos;
152 /* Attempt to decode the signature */
153 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
159 ret = pkcs7_check_authattrs(ctx->msg);
170 struct x509_certificate *cert = ctx->certs;
171 ctx->certs = cert->next;
172 x509_free_certificate(cert);
175 pkcs7_free_signed_info(ctx->sinfo);
177 pkcs7_free_message(ctx->msg);
183 EXPORT_SYMBOL_GPL(pkcs7_parse_message);
186 * Note an OID when we find one for later processing when we know how
189 int pkcs7_note_OID(void *context, size_t hdrlen,
191 const void *value, size_t vlen)
193 struct pkcs7_parse_context *ctx = context;
195 ctx->last_oid = look_up_OID(value, vlen);
196 if (ctx->last_oid == OID__NR) {
198 sprint_oid(value, vlen, buffer, sizeof(buffer));
199 printk("PKCS7: Unknown OID: [%lu] %s\n",
200 (unsigned long)value - ctx->data, buffer);
206 * Note the digest algorithm for the signature.
208 int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
210 const void *value, size_t vlen)
212 struct pkcs7_parse_context *ctx = context;
214 switch (ctx->last_oid) {
216 ctx->sinfo->sig->hash_algo = "md4";
219 ctx->sinfo->sig->hash_algo = "md5";
222 ctx->sinfo->sig->hash_algo = "sha1";
225 ctx->sinfo->sig->hash_algo = "sha256";
228 ctx->sinfo->sig->hash_algo = "sha384";
231 ctx->sinfo->sig->hash_algo = "sha512";
234 ctx->sinfo->sig->hash_algo = "sha224";
237 printk("Unsupported digest algo: %u\n", ctx->last_oid);
244 * Note the public key algorithm for the signature.
246 int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
248 const void *value, size_t vlen)
250 struct pkcs7_parse_context *ctx = context;
252 switch (ctx->last_oid) {
253 case OID_rsaEncryption:
254 ctx->sinfo->sig->pkey_algo = "rsa";
255 ctx->sinfo->sig->encoding = "pkcs1";
258 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
265 * We only support signed data [RFC2315 sec 9].
267 int pkcs7_check_content_type(void *context, size_t hdrlen,
269 const void *value, size_t vlen)
271 struct pkcs7_parse_context *ctx = context;
273 if (ctx->last_oid != OID_signed_data) {
274 pr_warn("Only support pkcs7_signedData type\n");
282 * Note the SignedData version
284 int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
286 const void *value, size_t vlen)
288 struct pkcs7_parse_context *ctx = context;
294 ctx->msg->version = version = *(const u8 *)value;
297 /* PKCS#7 SignedData [RFC2315 sec 9.1]
298 * CMS ver 1 SignedData [RFC5652 sec 5.1]
302 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
311 pr_warn("Unsupported SignedData version\n");
316 * Note the SignerInfo version
318 int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
320 const void *value, size_t vlen)
322 struct pkcs7_parse_context *ctx = context;
328 version = *(const u8 *)value;
331 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
332 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
334 if (ctx->msg->version != 1)
335 goto version_mismatch;
336 ctx->expect_skid = false;
339 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
340 if (ctx->msg->version == 1)
341 goto version_mismatch;
342 ctx->expect_skid = true;
351 pr_warn("Unsupported SignerInfo version\n");
354 pr_warn("SignedData-SignerInfo version mismatch\n");
359 * Extract a certificate and store it in the context.
361 int pkcs7_extract_cert(void *context, size_t hdrlen,
363 const void *value, size_t vlen)
365 struct pkcs7_parse_context *ctx = context;
366 struct x509_certificate *x509;
368 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
369 pr_debug("Cert began with tag %02x at %lu\n",
370 tag, (unsigned long)ctx - ctx->data);
374 /* We have to correct for the header so that the X.509 parser can start
375 * from the beginning. Note that since X.509 stipulates DER, there
376 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
382 if (((u8*)value)[1] == 0x80)
383 vlen += 2; /* Indefinite length - there should be an EOC */
385 x509 = x509_cert_parse(value, vlen);
387 return PTR_ERR(x509);
389 x509->index = ++ctx->x509_index;
390 pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
391 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
393 *ctx->ppcerts = x509;
394 ctx->ppcerts = &x509->next;
399 * Save the certificate list
401 int pkcs7_note_certificate_list(void *context, size_t hdrlen,
403 const void *value, size_t vlen)
405 struct pkcs7_parse_context *ctx = context;
407 pr_devel("Got cert list (%02x)\n", tag);
409 *ctx->ppcerts = ctx->msg->certs;
410 ctx->msg->certs = ctx->certs;
412 ctx->ppcerts = &ctx->certs;
417 * Note the content type.
419 int pkcs7_note_content(void *context, size_t hdrlen,
421 const void *value, size_t vlen)
423 struct pkcs7_parse_context *ctx = context;
425 if (ctx->last_oid != OID_data &&
426 ctx->last_oid != OID_msIndirectData) {
427 pr_warn("Unsupported data type %d\n", ctx->last_oid);
431 ctx->msg->data_type = ctx->last_oid;
436 * Extract the data from the message and store that and its content type OID in
439 int pkcs7_note_data(void *context, size_t hdrlen,
441 const void *value, size_t vlen)
443 struct pkcs7_parse_context *ctx = context;
445 pr_debug("Got data\n");
447 ctx->msg->data = value;
448 ctx->msg->data_len = vlen;
449 ctx->msg->data_hdrlen = hdrlen;
454 * Parse authenticated attributes.
456 int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
458 const void *value, size_t vlen)
460 struct pkcs7_parse_context *ctx = context;
461 struct pkcs7_signed_info *sinfo = ctx->sinfo;
462 enum OID content_type;
464 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
466 switch (ctx->last_oid) {
467 case OID_contentType:
468 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
470 content_type = look_up_OID(value, vlen);
471 if (content_type != ctx->msg->data_type) {
472 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
473 ctx->msg->data_type, sinfo->index,
479 case OID_signingTime:
480 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
482 /* Should we check that the signing time is consistent
483 * with the signer's X.509 cert?
485 return x509_decode_time(&sinfo->signing_time,
486 hdrlen, tag, value, vlen);
488 case OID_messageDigest:
489 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
493 sinfo->msgdigest = value;
494 sinfo->msgdigest_len = vlen;
497 case OID_smimeCapabilites:
498 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
500 #ifdef __UBOOT__ /* OID_data is needed for authenticated UEFI variables */
501 if (ctx->msg->data_type != OID_msIndirectData &&
502 ctx->msg->data_type != OID_data) {
504 if (ctx->msg->data_type != OID_msIndirectData) {
506 pr_warn("S/MIME Caps only allowed with Authenticode\n");
507 return -EKEYREJECTED;
511 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
512 * char URLs and cont[1] 8-bit char URLs.
514 * Microsoft StatementType seems to contain a list of OIDs that
515 * are also used as extendedKeyUsage types in X.509 certs.
517 case OID_msSpOpusInfo:
518 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
520 goto authenticode_check;
521 case OID_msStatementType:
522 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
525 if (ctx->msg->data_type != OID_msIndirectData) {
526 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
527 return -EKEYREJECTED;
529 /* I'm not sure how to validate these */
536 /* We permit max one item per AuthenticatedAttribute and no repeats */
537 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
538 return -EKEYREJECTED;
542 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
544 int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
546 const void *value, size_t vlen)
548 struct pkcs7_parse_context *ctx = context;
549 struct pkcs7_signed_info *sinfo = ctx->sinfo;
551 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
552 !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
553 pr_warn("Missing required AuthAttr\n");
557 if (ctx->msg->data_type != OID_msIndirectData &&
558 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
559 pr_warn("Unexpected Authenticode AuthAttr\n");
563 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
564 sinfo->authattrs = value - (hdrlen - 1);
565 sinfo->authattrs_len = vlen + (hdrlen - 1);
570 * Note the issuing certificate serial number
572 int pkcs7_sig_note_serial(void *context, size_t hdrlen,
574 const void *value, size_t vlen)
576 struct pkcs7_parse_context *ctx = context;
577 ctx->raw_serial = value;
578 ctx->raw_serial_size = vlen;
583 * Note the issuer's name
585 int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
587 const void *value, size_t vlen)
589 struct pkcs7_parse_context *ctx = context;
590 ctx->raw_issuer = value;
591 ctx->raw_issuer_size = vlen;
596 * Note the issuing cert's subjectKeyIdentifier
598 int pkcs7_sig_note_skid(void *context, size_t hdrlen,
600 const void *value, size_t vlen)
602 struct pkcs7_parse_context *ctx = context;
604 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
606 ctx->raw_skid = value;
607 ctx->raw_skid_size = vlen;
612 * Note the signature data
614 int pkcs7_sig_note_signature(void *context, size_t hdrlen,
616 const void *value, size_t vlen)
618 struct pkcs7_parse_context *ctx = context;
620 ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
621 if (!ctx->sinfo->sig->s)
624 ctx->sinfo->sig->s_size = vlen;
629 * Note a signature information block
631 int pkcs7_note_signed_info(void *context, size_t hdrlen,
633 const void *value, size_t vlen)
635 struct pkcs7_parse_context *ctx = context;
636 struct pkcs7_signed_info *sinfo = ctx->sinfo;
637 struct asymmetric_key_id *kid;
639 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
640 pr_warn("Authenticode requires AuthAttrs\n");
644 /* Generate cert issuer + serial number key ID */
645 if (!ctx->expect_skid) {
646 kid = asymmetric_key_generate_id(ctx->raw_serial,
647 ctx->raw_serial_size,
649 ctx->raw_issuer_size);
651 kid = asymmetric_key_generate_id(ctx->raw_skid,
658 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
660 sinfo->sig->auth_ids[0] = kid;
661 sinfo->index = ++ctx->sinfo_index;
662 *ctx->ppsinfo = sinfo;
663 ctx->ppsinfo = &sinfo->next;
664 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
667 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
669 if (!ctx->sinfo->sig)