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
10 #include <linux/bitops.h>
11 #include <linux/compat.h>
13 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
19 #include <linux/err.h>
20 #include <linux/oid_registry.h>
21 #include <crypto/public_key.h>
22 #include "pkcs7_parser.h"
23 #include "pkcs7.asn1.h"
25 MODULE_DESCRIPTION("PKCS#7 parser");
26 MODULE_AUTHOR("Red Hat, Inc.");
27 MODULE_LICENSE("GPL");
29 struct pkcs7_parse_context {
30 struct pkcs7_message *msg; /* Message being constructed */
31 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */
32 struct pkcs7_signed_info **ppsinfo;
33 struct x509_certificate *certs; /* Certificate cache */
34 struct x509_certificate **ppcerts;
35 unsigned long data; /* Start of data */
36 enum OID last_oid; /* Last OID encountered */
39 const void *raw_serial;
40 unsigned raw_serial_size;
41 unsigned raw_issuer_size;
42 const void *raw_issuer;
44 unsigned raw_skid_size;
49 * Free a signed information block.
51 static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo)
54 public_key_signature_free(sinfo->sig);
60 * pkcs7_free_message - Free a PKCS#7 message
61 * @pkcs7: The PKCS#7 message to free
63 void pkcs7_free_message(struct pkcs7_message *pkcs7)
65 struct x509_certificate *cert;
66 struct pkcs7_signed_info *sinfo;
69 while (pkcs7->certs) {
71 pkcs7->certs = cert->next;
72 x509_free_certificate(cert);
76 pkcs7->crl = cert->next;
77 x509_free_certificate(cert);
79 while (pkcs7->signed_infos) {
80 sinfo = pkcs7->signed_infos;
81 pkcs7->signed_infos = sinfo->next;
82 pkcs7_free_signed_info(sinfo);
87 EXPORT_SYMBOL_GPL(pkcs7_free_message);
90 * Check authenticatedAttributes are provided or not provided consistently.
92 static int pkcs7_check_authattrs(struct pkcs7_message *msg)
94 struct pkcs7_signed_info *sinfo;
97 sinfo = msg->signed_infos;
101 if (sinfo->authattrs) {
103 msg->have_authattrs = true;
106 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next)
107 if (!!sinfo->authattrs != want)
112 pr_warn("Inconsistently supplied authAttrs\n");
117 * pkcs7_parse_message - Parse a PKCS#7 message
118 * @data: The raw binary ASN.1 encoded message to be parsed
119 * @datalen: The size of the encoded message
121 struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen)
123 struct pkcs7_parse_context *ctx;
124 struct pkcs7_message *msg = ERR_PTR(-ENOMEM);
127 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL);
130 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL);
133 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
136 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
138 if (!ctx->sinfo->sig)
141 ctx->data = (unsigned long)data;
142 ctx->ppcerts = &ctx->certs;
143 ctx->ppsinfo = &ctx->msg->signed_infos;
145 /* Attempt to decode the signature */
146 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen);
152 ret = pkcs7_check_authattrs(ctx->msg);
163 struct x509_certificate *cert = ctx->certs;
164 ctx->certs = cert->next;
165 x509_free_certificate(cert);
168 pkcs7_free_signed_info(ctx->sinfo);
170 pkcs7_free_message(ctx->msg);
176 EXPORT_SYMBOL_GPL(pkcs7_parse_message);
179 * pkcs7_get_content_data - Get access to the PKCS#7 content
180 * @pkcs7: The preparsed PKCS#7 message to access
181 * @_data: Place to return a pointer to the data
182 * @_data_len: Place to return the data length
183 * @_headerlen: Size of ASN.1 header not included in _data
185 * Get access to the data content of the PKCS#7 message. The size of the
186 * header of the ASN.1 object that contains it is also provided and can be used
187 * to adjust *_data and *_data_len to get the entire object.
189 * Returns -ENODATA if the data object was missing from the message.
191 int pkcs7_get_content_data(const struct pkcs7_message *pkcs7,
192 const void **_data, size_t *_data_len,
198 *_data = pkcs7->data;
199 *_data_len = pkcs7->data_len;
201 *_headerlen = pkcs7->data_hdrlen;
204 EXPORT_SYMBOL_GPL(pkcs7_get_content_data);
207 * Note an OID when we find one for later processing when we know how
210 int pkcs7_note_OID(void *context, size_t hdrlen,
212 const void *value, size_t vlen)
214 struct pkcs7_parse_context *ctx = context;
216 ctx->last_oid = look_up_OID(value, vlen);
217 if (ctx->last_oid == OID__NR) {
219 sprint_oid(value, vlen, buffer, sizeof(buffer));
220 printk("PKCS7: Unknown OID: [%lu] %s\n",
221 (unsigned long)value - ctx->data, buffer);
227 * Note the digest algorithm for the signature.
229 int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen,
231 const void *value, size_t vlen)
233 struct pkcs7_parse_context *ctx = context;
235 switch (ctx->last_oid) {
237 ctx->sinfo->sig->hash_algo = "md4";
240 ctx->sinfo->sig->hash_algo = "md5";
243 ctx->sinfo->sig->hash_algo = "sha1";
246 ctx->sinfo->sig->hash_algo = "sha256";
249 ctx->sinfo->sig->hash_algo = "sha384";
252 ctx->sinfo->sig->hash_algo = "sha512";
255 ctx->sinfo->sig->hash_algo = "sha224";
258 printk("Unsupported digest algo: %u\n", ctx->last_oid);
265 * Note the public key algorithm for the signature.
267 int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen,
269 const void *value, size_t vlen)
271 struct pkcs7_parse_context *ctx = context;
273 switch (ctx->last_oid) {
274 case OID_rsaEncryption:
275 ctx->sinfo->sig->pkey_algo = "rsa";
276 ctx->sinfo->sig->encoding = "pkcs1";
279 printk("Unsupported pkey algo: %u\n", ctx->last_oid);
286 * We only support signed data [RFC2315 sec 9].
288 int pkcs7_check_content_type(void *context, size_t hdrlen,
290 const void *value, size_t vlen)
292 struct pkcs7_parse_context *ctx = context;
294 if (ctx->last_oid != OID_signed_data) {
295 pr_warn("Only support pkcs7_signedData type\n");
303 * Note the SignedData version
305 int pkcs7_note_signeddata_version(void *context, size_t hdrlen,
307 const void *value, size_t vlen)
309 struct pkcs7_parse_context *ctx = context;
315 ctx->msg->version = version = *(const u8 *)value;
318 /* PKCS#7 SignedData [RFC2315 sec 9.1]
319 * CMS ver 1 SignedData [RFC5652 sec 5.1]
323 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
332 pr_warn("Unsupported SignedData version\n");
337 * Note the SignerInfo version
339 int pkcs7_note_signerinfo_version(void *context, size_t hdrlen,
341 const void *value, size_t vlen)
343 struct pkcs7_parse_context *ctx = context;
349 version = *(const u8 *)value;
352 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
353 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
355 if (ctx->msg->version != 1)
356 goto version_mismatch;
357 ctx->expect_skid = false;
360 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
361 if (ctx->msg->version == 1)
362 goto version_mismatch;
363 ctx->expect_skid = true;
372 pr_warn("Unsupported SignerInfo version\n");
375 pr_warn("SignedData-SignerInfo version mismatch\n");
380 * Extract a certificate and store it in the context.
382 int pkcs7_extract_cert(void *context, size_t hdrlen,
384 const void *value, size_t vlen)
386 struct pkcs7_parse_context *ctx = context;
387 struct x509_certificate *x509;
389 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) {
390 pr_debug("Cert began with tag %02x at %lu\n",
391 tag, (unsigned long)ctx - ctx->data);
395 /* We have to correct for the header so that the X.509 parser can start
396 * from the beginning. Note that since X.509 stipulates DER, there
397 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
403 if (((u8*)value)[1] == 0x80)
404 vlen += 2; /* Indefinite length - there should be an EOC */
406 x509 = x509_cert_parse(value, vlen);
408 return PTR_ERR(x509);
410 x509->index = ++ctx->x509_index;
411 pr_debug("Got cert %u for %s\n", x509->index, x509->subject);
412 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data);
414 *ctx->ppcerts = x509;
415 ctx->ppcerts = &x509->next;
420 * Save the certificate list
422 int pkcs7_note_certificate_list(void *context, size_t hdrlen,
424 const void *value, size_t vlen)
426 struct pkcs7_parse_context *ctx = context;
428 pr_devel("Got cert list (%02x)\n", tag);
430 *ctx->ppcerts = ctx->msg->certs;
431 ctx->msg->certs = ctx->certs;
433 ctx->ppcerts = &ctx->certs;
438 * Note the content type.
440 int pkcs7_note_content(void *context, size_t hdrlen,
442 const void *value, size_t vlen)
444 struct pkcs7_parse_context *ctx = context;
446 if (ctx->last_oid != OID_data &&
447 ctx->last_oid != OID_msIndirectData) {
448 pr_warn("Unsupported data type %d\n", ctx->last_oid);
452 ctx->msg->data_type = ctx->last_oid;
457 * Extract the data from the message and store that and its content type OID in
460 int pkcs7_note_data(void *context, size_t hdrlen,
462 const void *value, size_t vlen)
464 struct pkcs7_parse_context *ctx = context;
466 pr_debug("Got data\n");
468 ctx->msg->data = value;
469 ctx->msg->data_len = vlen;
470 ctx->msg->data_hdrlen = hdrlen;
475 * Parse authenticated attributes.
477 int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen,
479 const void *value, size_t vlen)
481 struct pkcs7_parse_context *ctx = context;
482 struct pkcs7_signed_info *sinfo = ctx->sinfo;
483 enum OID content_type;
485 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
487 switch (ctx->last_oid) {
488 case OID_contentType:
489 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set))
491 content_type = look_up_OID(value, vlen);
492 if (content_type != ctx->msg->data_type) {
493 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
494 ctx->msg->data_type, sinfo->index,
500 case OID_signingTime:
501 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set))
503 /* Should we check that the signing time is consistent
504 * with the signer's X.509 cert?
506 return x509_decode_time(&sinfo->signing_time,
507 hdrlen, tag, value, vlen);
509 case OID_messageDigest:
510 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set))
514 sinfo->msgdigest = value;
515 sinfo->msgdigest_len = vlen;
518 case OID_smimeCapabilites:
519 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set))
521 #ifdef __UBOOT__ /* OID_data is needed for authenticated UEFI variables */
522 if (ctx->msg->data_type != OID_msIndirectData &&
523 ctx->msg->data_type != OID_data) {
525 if (ctx->msg->data_type != OID_msIndirectData) {
527 pr_warn("S/MIME Caps only allowed with Authenticode\n");
528 return -EKEYREJECTED;
532 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
533 * char URLs and cont[1] 8-bit char URLs.
535 * Microsoft StatementType seems to contain a list of OIDs that
536 * are also used as extendedKeyUsage types in X.509 certs.
538 case OID_msSpOpusInfo:
539 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set))
541 goto authenticode_check;
542 case OID_msStatementType:
543 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set))
546 if (ctx->msg->data_type != OID_msIndirectData) {
547 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
548 return -EKEYREJECTED;
550 /* I'm not sure how to validate these */
557 /* We permit max one item per AuthenticatedAttribute and no repeats */
558 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
559 return -EKEYREJECTED;
563 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
565 int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen,
567 const void *value, size_t vlen)
569 struct pkcs7_parse_context *ctx = context;
570 struct pkcs7_signed_info *sinfo = ctx->sinfo;
572 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) ||
573 !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) {
574 pr_warn("Missing required AuthAttr\n");
578 if (ctx->msg->data_type != OID_msIndirectData &&
579 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) {
580 pr_warn("Unexpected Authenticode AuthAttr\n");
584 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
585 sinfo->authattrs = value - (hdrlen - 1);
586 sinfo->authattrs_len = vlen + (hdrlen - 1);
591 * Note the issuing certificate serial number
593 int pkcs7_sig_note_serial(void *context, size_t hdrlen,
595 const void *value, size_t vlen)
597 struct pkcs7_parse_context *ctx = context;
598 ctx->raw_serial = value;
599 ctx->raw_serial_size = vlen;
604 * Note the issuer's name
606 int pkcs7_sig_note_issuer(void *context, size_t hdrlen,
608 const void *value, size_t vlen)
610 struct pkcs7_parse_context *ctx = context;
611 ctx->raw_issuer = value;
612 ctx->raw_issuer_size = vlen;
617 * Note the issuing cert's subjectKeyIdentifier
619 int pkcs7_sig_note_skid(void *context, size_t hdrlen,
621 const void *value, size_t vlen)
623 struct pkcs7_parse_context *ctx = context;
625 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value);
627 ctx->raw_skid = value;
628 ctx->raw_skid_size = vlen;
633 * Note the signature data
635 int pkcs7_sig_note_signature(void *context, size_t hdrlen,
637 const void *value, size_t vlen)
639 struct pkcs7_parse_context *ctx = context;
641 ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL);
642 if (!ctx->sinfo->sig->s)
645 ctx->sinfo->sig->s_size = vlen;
650 * Note a signature information block
652 int pkcs7_note_signed_info(void *context, size_t hdrlen,
654 const void *value, size_t vlen)
656 struct pkcs7_parse_context *ctx = context;
657 struct pkcs7_signed_info *sinfo = ctx->sinfo;
658 struct asymmetric_key_id *kid;
660 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) {
661 pr_warn("Authenticode requires AuthAttrs\n");
665 /* Generate cert issuer + serial number key ID */
666 if (!ctx->expect_skid) {
667 kid = asymmetric_key_generate_id(ctx->raw_serial,
668 ctx->raw_serial_size,
670 ctx->raw_issuer_size);
672 kid = asymmetric_key_generate_id(ctx->raw_skid,
679 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data);
681 sinfo->sig->auth_ids[0] = kid;
682 sinfo->index = ++ctx->sinfo_index;
683 *ctx->ppsinfo = sinfo;
684 ctx->ppsinfo = &sinfo->next;
685 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL);
688 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature),
690 if (!ctx->sinfo->sig)