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>
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 * pkcs7_get_content_data - Get access to the PKCS#7 content
187 * @pkcs7: The preparsed PKCS#7 message to access
188 * @_data: Place to return a pointer to the data
189 * @_data_len: Place to return the data length
190 * @_headerlen: Size of ASN.1 header not included in _data
192 * Get access to the data content of the PKCS#7 message. The size of the
193 * header of the ASN.1 object that contains it is also provided and can be used
194 * to adjust *_data and *_data_len to get the entire object.
196 * Returns -ENODATA if the data object was missing from the message.
198 int pkcs7_get_content_data(const struct pkcs7_message
*pkcs7
,
199 const void **_data
, size_t *_data_len
,
205 *_data
= pkcs7
->data
;
206 *_data_len
= pkcs7
->data_len
;
208 *_headerlen
= pkcs7
->data_hdrlen
;
211 EXPORT_SYMBOL_GPL(pkcs7_get_content_data
);
214 * Note an OID when we find one for later processing when we know how
217 int pkcs7_note_OID(void *context
, size_t hdrlen
,
219 const void *value
, size_t vlen
)
221 struct pkcs7_parse_context
*ctx
= context
;
223 ctx
->last_oid
= look_up_OID(value
, vlen
);
224 if (ctx
->last_oid
== OID__NR
) {
226 sprint_oid(value
, vlen
, buffer
, sizeof(buffer
));
227 printk("PKCS7: Unknown OID: [%lu] %s\n",
228 (unsigned long)value
- ctx
->data
, buffer
);
234 * Note the digest algorithm for the signature.
236 int pkcs7_sig_note_digest_algo(void *context
, size_t hdrlen
,
238 const void *value
, size_t vlen
)
240 struct pkcs7_parse_context
*ctx
= context
;
242 switch (ctx
->last_oid
) {
244 ctx
->sinfo
->sig
->hash_algo
= "md4";
247 ctx
->sinfo
->sig
->hash_algo
= "md5";
250 ctx
->sinfo
->sig
->hash_algo
= "sha1";
253 ctx
->sinfo
->sig
->hash_algo
= "sha256";
256 ctx
->sinfo
->sig
->hash_algo
= "sha384";
259 ctx
->sinfo
->sig
->hash_algo
= "sha512";
262 ctx
->sinfo
->sig
->hash_algo
= "sha224";
265 printk("Unsupported digest algo: %u\n", ctx
->last_oid
);
272 * Note the public key algorithm for the signature.
274 int pkcs7_sig_note_pkey_algo(void *context
, size_t hdrlen
,
276 const void *value
, size_t vlen
)
278 struct pkcs7_parse_context
*ctx
= context
;
280 switch (ctx
->last_oid
) {
281 case OID_rsaEncryption
:
282 ctx
->sinfo
->sig
->pkey_algo
= "rsa";
283 ctx
->sinfo
->sig
->encoding
= "pkcs1";
286 printk("Unsupported pkey algo: %u\n", ctx
->last_oid
);
293 * We only support signed data [RFC2315 sec 9].
295 int pkcs7_check_content_type(void *context
, size_t hdrlen
,
297 const void *value
, size_t vlen
)
299 struct pkcs7_parse_context
*ctx
= context
;
301 if (ctx
->last_oid
!= OID_signed_data
) {
302 pr_warn("Only support pkcs7_signedData type\n");
310 * Note the SignedData version
312 int pkcs7_note_signeddata_version(void *context
, size_t hdrlen
,
314 const void *value
, size_t vlen
)
316 struct pkcs7_parse_context
*ctx
= context
;
322 ctx
->msg
->version
= version
= *(const u8
*)value
;
325 /* PKCS#7 SignedData [RFC2315 sec 9.1]
326 * CMS ver 1 SignedData [RFC5652 sec 5.1]
330 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */
339 pr_warn("Unsupported SignedData version\n");
344 * Note the SignerInfo version
346 int pkcs7_note_signerinfo_version(void *context
, size_t hdrlen
,
348 const void *value
, size_t vlen
)
350 struct pkcs7_parse_context
*ctx
= context
;
356 version
= *(const u8
*)value
;
359 /* PKCS#7 SignerInfo [RFC2315 sec 9.2]
360 * CMS ver 1 SignerInfo [RFC5652 sec 5.3]
362 if (ctx
->msg
->version
!= 1)
363 goto version_mismatch
;
364 ctx
->expect_skid
= false;
367 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */
368 if (ctx
->msg
->version
== 1)
369 goto version_mismatch
;
370 ctx
->expect_skid
= true;
379 pr_warn("Unsupported SignerInfo version\n");
382 pr_warn("SignedData-SignerInfo version mismatch\n");
387 * Extract a certificate and store it in the context.
389 int pkcs7_extract_cert(void *context
, size_t hdrlen
,
391 const void *value
, size_t vlen
)
393 struct pkcs7_parse_context
*ctx
= context
;
394 struct x509_certificate
*x509
;
396 if (tag
!= ((ASN1_UNIV
<< 6) | ASN1_CONS_BIT
| ASN1_SEQ
)) {
397 pr_debug("Cert began with tag %02x at %lu\n",
398 tag
, (unsigned long)ctx
- ctx
->data
);
402 /* We have to correct for the header so that the X.509 parser can start
403 * from the beginning. Note that since X.509 stipulates DER, there
404 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which
410 if (((u8
*)value
)[1] == 0x80)
411 vlen
+= 2; /* Indefinite length - there should be an EOC */
413 x509
= x509_cert_parse(value
, vlen
);
415 return PTR_ERR(x509
);
417 x509
->index
= ++ctx
->x509_index
;
418 pr_debug("Got cert %u for %s\n", x509
->index
, x509
->subject
);
419 pr_debug("- fingerprint %*phN\n", x509
->id
->len
, x509
->id
->data
);
421 *ctx
->ppcerts
= x509
;
422 ctx
->ppcerts
= &x509
->next
;
427 * Save the certificate list
429 int pkcs7_note_certificate_list(void *context
, size_t hdrlen
,
431 const void *value
, size_t vlen
)
433 struct pkcs7_parse_context
*ctx
= context
;
435 pr_devel("Got cert list (%02x)\n", tag
);
437 *ctx
->ppcerts
= ctx
->msg
->certs
;
438 ctx
->msg
->certs
= ctx
->certs
;
440 ctx
->ppcerts
= &ctx
->certs
;
445 * Note the content type.
447 int pkcs7_note_content(void *context
, size_t hdrlen
,
449 const void *value
, size_t vlen
)
451 struct pkcs7_parse_context
*ctx
= context
;
453 if (ctx
->last_oid
!= OID_data
&&
454 ctx
->last_oid
!= OID_msIndirectData
) {
455 pr_warn("Unsupported data type %d\n", ctx
->last_oid
);
459 ctx
->msg
->data_type
= ctx
->last_oid
;
464 * Extract the data from the message and store that and its content type OID in
467 int pkcs7_note_data(void *context
, size_t hdrlen
,
469 const void *value
, size_t vlen
)
471 struct pkcs7_parse_context
*ctx
= context
;
473 pr_debug("Got data\n");
475 ctx
->msg
->data
= value
;
476 ctx
->msg
->data_len
= vlen
;
477 ctx
->msg
->data_hdrlen
= hdrlen
;
482 * Parse authenticated attributes.
484 int pkcs7_sig_note_authenticated_attr(void *context
, size_t hdrlen
,
486 const void *value
, size_t vlen
)
488 struct pkcs7_parse_context
*ctx
= context
;
489 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
490 enum OID content_type
;
492 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
494 switch (ctx
->last_oid
) {
495 case OID_contentType
:
496 if (__test_and_set_bit(sinfo_has_content_type
, &sinfo
->aa_set
))
498 content_type
= look_up_OID(value
, vlen
);
499 if (content_type
!= ctx
->msg
->data_type
) {
500 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n",
501 ctx
->msg
->data_type
, sinfo
->index
,
507 case OID_signingTime
:
508 if (__test_and_set_bit(sinfo_has_signing_time
, &sinfo
->aa_set
))
510 /* Should we check that the signing time is consistent
511 * with the signer's X.509 cert?
513 return x509_decode_time(&sinfo
->signing_time
,
514 hdrlen
, tag
, value
, vlen
);
516 case OID_messageDigest
:
517 if (__test_and_set_bit(sinfo_has_message_digest
, &sinfo
->aa_set
))
521 sinfo
->msgdigest
= value
;
522 sinfo
->msgdigest_len
= vlen
;
525 case OID_smimeCapabilites
:
526 if (__test_and_set_bit(sinfo_has_smime_caps
, &sinfo
->aa_set
))
528 #ifdef __UBOOT__ /* OID_data is needed for authenticated UEFI variables */
529 if (ctx
->msg
->data_type
!= OID_msIndirectData
&&
530 ctx
->msg
->data_type
!= OID_data
) {
532 if (ctx
->msg
->data_type
!= OID_msIndirectData
) {
534 pr_warn("S/MIME Caps only allowed with Authenticode\n");
535 return -EKEYREJECTED
;
539 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE
540 * char URLs and cont[1] 8-bit char URLs.
542 * Microsoft StatementType seems to contain a list of OIDs that
543 * are also used as extendedKeyUsage types in X.509 certs.
545 case OID_msSpOpusInfo
:
546 if (__test_and_set_bit(sinfo_has_ms_opus_info
, &sinfo
->aa_set
))
548 goto authenticode_check
;
549 case OID_msStatementType
:
550 if (__test_and_set_bit(sinfo_has_ms_statement_type
, &sinfo
->aa_set
))
553 if (ctx
->msg
->data_type
!= OID_msIndirectData
) {
554 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n");
555 return -EKEYREJECTED
;
557 /* I'm not sure how to validate these */
564 /* We permit max one item per AuthenticatedAttribute and no repeats */
565 pr_warn("Repeated/multivalue AuthAttrs not permitted\n");
566 return -EKEYREJECTED
;
570 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3]
572 int pkcs7_sig_note_set_of_authattrs(void *context
, size_t hdrlen
,
574 const void *value
, size_t vlen
)
576 struct pkcs7_parse_context
*ctx
= context
;
577 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
579 if (!test_bit(sinfo_has_content_type
, &sinfo
->aa_set
) ||
580 !test_bit(sinfo_has_message_digest
, &sinfo
->aa_set
)) {
581 pr_warn("Missing required AuthAttr\n");
585 if (ctx
->msg
->data_type
!= OID_msIndirectData
&&
586 test_bit(sinfo_has_ms_opus_info
, &sinfo
->aa_set
)) {
587 pr_warn("Unexpected Authenticode AuthAttr\n");
591 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */
592 sinfo
->authattrs
= value
- (hdrlen
- 1);
593 sinfo
->authattrs_len
= vlen
+ (hdrlen
- 1);
598 * Note the issuing certificate serial number
600 int pkcs7_sig_note_serial(void *context
, size_t hdrlen
,
602 const void *value
, size_t vlen
)
604 struct pkcs7_parse_context
*ctx
= context
;
605 ctx
->raw_serial
= value
;
606 ctx
->raw_serial_size
= vlen
;
611 * Note the issuer's name
613 int pkcs7_sig_note_issuer(void *context
, size_t hdrlen
,
615 const void *value
, size_t vlen
)
617 struct pkcs7_parse_context
*ctx
= context
;
618 ctx
->raw_issuer
= value
;
619 ctx
->raw_issuer_size
= vlen
;
624 * Note the issuing cert's subjectKeyIdentifier
626 int pkcs7_sig_note_skid(void *context
, size_t hdrlen
,
628 const void *value
, size_t vlen
)
630 struct pkcs7_parse_context
*ctx
= context
;
632 pr_devel("SKID: %02x %zu [%*ph]\n", tag
, vlen
, (unsigned)vlen
, value
);
634 ctx
->raw_skid
= value
;
635 ctx
->raw_skid_size
= vlen
;
640 * Note the signature data
642 int pkcs7_sig_note_signature(void *context
, size_t hdrlen
,
644 const void *value
, size_t vlen
)
646 struct pkcs7_parse_context
*ctx
= context
;
648 ctx
->sinfo
->sig
->s
= kmemdup(value
, vlen
, GFP_KERNEL
);
649 if (!ctx
->sinfo
->sig
->s
)
652 ctx
->sinfo
->sig
->s_size
= vlen
;
657 * Note a signature information block
659 int pkcs7_note_signed_info(void *context
, size_t hdrlen
,
661 const void *value
, size_t vlen
)
663 struct pkcs7_parse_context
*ctx
= context
;
664 struct pkcs7_signed_info
*sinfo
= ctx
->sinfo
;
665 struct asymmetric_key_id
*kid
;
667 if (ctx
->msg
->data_type
== OID_msIndirectData
&& !sinfo
->authattrs
) {
668 pr_warn("Authenticode requires AuthAttrs\n");
672 /* Generate cert issuer + serial number key ID */
673 if (!ctx
->expect_skid
) {
674 kid
= asymmetric_key_generate_id(ctx
->raw_serial
,
675 ctx
->raw_serial_size
,
677 ctx
->raw_issuer_size
);
679 kid
= asymmetric_key_generate_id(ctx
->raw_skid
,
686 pr_devel("SINFO KID: %u [%*phN]\n", kid
->len
, kid
->len
, kid
->data
);
688 sinfo
->sig
->auth_ids
[0] = kid
;
689 sinfo
->index
= ++ctx
->sinfo_index
;
690 *ctx
->ppsinfo
= sinfo
;
691 ctx
->ppsinfo
= &sinfo
->next
;
692 ctx
->sinfo
= kzalloc(sizeof(struct pkcs7_signed_info
), GFP_KERNEL
);
695 ctx
->sinfo
->sig
= kzalloc(sizeof(struct public_key_signature
),
697 if (!ctx
->sinfo
->sig
)