1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Verify the signature on a PKCS#7 message.
4 * Imported from crypto/asymmetric_keys/pkcs7_verify.c of linux 5.7
5 * with modification marked as __UBOOT__.
7 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
8 * Written by David Howells (dhowells@redhat.com)
11 #define pr_fmt(fmt) "PKCS7: "fmt
15 #include <linux/bitops.h>
16 #include <linux/compat.h>
17 #include <linux/asn1.h>
18 #include <u-boot/rsa-checksum.h>
19 #include <crypto/public_key.h>
20 #include <crypto/pkcs7_parser.h>
22 #include <linux/kernel.h>
23 #include <linux/export.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/asn1.h>
27 #include <crypto/hash.h>
28 #include <crypto/hash_info.h>
29 #include <crypto/public_key.h>
30 #include "pkcs7_parser.h"
34 * pkcs7_digest - Digest the relevant parts of the PKCS#7 data
35 * @pkcs7: PKCS7 Signed Data
36 * @sinfo: PKCS7 Signed Info
38 * Digest the relevant parts of the PKCS#7 data, @pkcs7, using signature
39 * information in @sinfo. But if there are authentication attributes,
40 * i.e. signed image case, the digest must be calculated against
41 * the authentication attributes.
43 * Return: 0 - on success, non-zero error code - otherwise
46 static int pkcs7_digest(struct pkcs7_message *pkcs7,
47 struct pkcs7_signed_info *sinfo)
49 struct public_key_signature *sig = sinfo->sig;
50 struct image_region regions[2];
55 * If the authenticated attributes are present,
56 * the message-digest is calculated on the
57 * attributes present in the
58 * authenticatedAttributes field and not just
61 if (!sinfo->authattrs && sig->digest)
64 if (!sinfo->sig->hash_algo)
66 if (!strcmp(sinfo->sig->hash_algo, "sha256"))
67 sig->digest_size = SHA256_SUM_LEN;
68 else if (!strcmp(sinfo->sig->hash_algo, "sha1"))
69 sig->digest_size = SHA1_SUM_LEN;
74 * Calculate the hash only if the data is present.
75 * In case of authenticated variable and capsule,
76 * the hash has already been calculated on the
77 * efi_image_regions and populated
80 sig->digest = calloc(1, sig->digest_size);
82 pr_warn("Sig %u: Out of memory\n", sinfo->index);
86 regions[0].data = pkcs7->data;
87 regions[0].size = pkcs7->data_len;
89 /* Digest the message [RFC2315 9.3] */
90 hash_calculate(sinfo->sig->hash_algo, regions, 1, sig->digest);
93 /* However, if there are authenticated attributes, there must be a
94 * message digest attribute amongst them which corresponds to the
95 * digest we just calculated.
97 if (sinfo->authattrs) {
100 if (!sinfo->msgdigest) {
101 pr_warn("Sig %u: No messageDigest\n", sinfo->index);
106 if (sinfo->msgdigest_len != sig->digest_size) {
107 pr_debug("Sig %u: Invalid digest size (%u)\n",
108 sinfo->index, sinfo->msgdigest_len);
113 if (memcmp(sig->digest, sinfo->msgdigest,
114 sinfo->msgdigest_len) != 0) {
115 pr_debug("Sig %u: Message digest doesn't match\n",
121 /* We then calculate anew, using the authenticated attributes
122 * as the contents of the digest instead. Note that we need to
123 * convert the attributes from a CONT.0 into a SET before we
126 memset(sig->digest, 0, sig->digest_size);
129 regions[0].data = &tag;
131 regions[1].data = sinfo->authattrs;
132 regions[1].size = sinfo->authattrs_len;
134 hash_calculate(sinfo->sig->hash_algo, regions, 2, sig->digest);
141 #else /* !__UBOOT__ */
142 static int pkcs7_digest(struct pkcs7_message *pkcs7,
143 struct pkcs7_signed_info *sinfo)
145 struct public_key_signature *sig = sinfo->sig;
146 struct crypto_shash *tfm;
147 struct shash_desc *desc;
151 kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo);
153 /* The digest was calculated already. */
157 if (!sinfo->sig->hash_algo)
160 /* Allocate the hashing algorithm we're going to need and find out how
161 * big the hash operational data will be.
163 tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0);
165 return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);
167 desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);
168 sig->digest_size = crypto_shash_digestsize(tfm);
171 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);
175 desc = kzalloc(desc_size, GFP_KERNEL);
181 /* Digest the message [RFC2315 9.3] */
182 ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len,
186 pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest);
188 /* However, if there are authenticated attributes, there must be a
189 * message digest attribute amongst them which corresponds to the
190 * digest we just calculated.
192 if (sinfo->authattrs) {
195 if (!sinfo->msgdigest) {
196 pr_warn("Sig %u: No messageDigest\n", sinfo->index);
201 if (sinfo->msgdigest_len != sig->digest_size) {
202 pr_debug("Sig %u: Invalid digest size (%u)\n",
203 sinfo->index, sinfo->msgdigest_len);
208 if (memcmp(sig->digest, sinfo->msgdigest,
209 sinfo->msgdigest_len) != 0) {
210 pr_debug("Sig %u: Message digest doesn't match\n",
216 /* We then calculate anew, using the authenticated attributes
217 * as the contents of the digest instead. Note that we need to
218 * convert the attributes from a CONT.0 into a SET before we
221 memset(sig->digest, 0, sig->digest_size);
223 ret = crypto_shash_init(desc);
226 tag = ASN1_CONS_BIT | ASN1_SET;
227 ret = crypto_shash_update(desc, &tag, 1);
230 ret = crypto_shash_finup(desc, sinfo->authattrs,
231 sinfo->authattrs_len, sig->digest);
234 pr_devel("AADigest = [%*ph]\n", 8, sig->digest);
240 crypto_free_shash(tfm);
241 kleave(" = %d", ret);
245 int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,
246 enum hash_algo *hash_algo)
248 struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;
252 * This function doesn't support messages with more than one signature.
254 if (sinfo == NULL || sinfo->next != NULL)
257 ret = pkcs7_digest(pkcs7, sinfo);
261 *buf = sinfo->sig->digest;
262 *len = sinfo->sig->digest_size;
264 for (i = 0; i < HASH_ALGO__LAST; i++)
265 if (!strcmp(hash_algo_name[i], sinfo->sig->hash_algo)) {
272 #endif /* !__UBOOT__ */
275 * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
276 * uses the issuer's name and the issuing certificate serial number for
277 * matching purposes. These must match the certificate issuer's name (not
278 * subject's name) and the certificate serial number [RFC 2315 6.7].
280 static int pkcs7_find_key(struct pkcs7_message *pkcs7,
281 struct pkcs7_signed_info *sinfo)
283 struct x509_certificate *x509;
286 kenter("%u", sinfo->index);
288 for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {
289 /* I'm _assuming_ that the generator of the PKCS#7 message will
290 * encode the fields from the X.509 cert in the same way in the
291 * PKCS#7 message - but I can't be 100% sure of that. It's
292 * possible this will need element-by-element comparison.
294 if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))
296 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
297 sinfo->index, certix);
299 if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) {
300 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
305 sinfo->signer = x509;
309 /* The relevant X.509 cert isn't found here, but it might be found in
312 pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
314 sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);
319 * pkcs7_verify_sig_chain - Verify the internal certificate chain as best
321 * @pkcs7: PKCS7 Signed Data
322 * @sinfo: PKCS7 Signed Info
323 * @signer: Singer's certificate
325 * Build up and verify the internal certificate chain against a signature
326 * in @sinfo, using certificates contained in @pkcs7 as best as we can.
327 * If the chain reaches the end, the last certificate will be returned
330 * Return: 0 - on success, non-zero error code - otherwise
333 static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
334 struct pkcs7_signed_info *sinfo,
335 struct x509_certificate **signer)
337 static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,
338 struct pkcs7_signed_info *sinfo)
341 struct public_key_signature *sig;
342 struct x509_certificate *x509 = sinfo->signer, *p;
343 struct asymmetric_key_id *auth;
350 for (p = pkcs7->certs; p; p = p->next)
354 pr_debug("verify %s: %*phN\n",
356 x509->raw_serial_size, x509->raw_serial);
359 if (x509->blacklisted) {
360 /* If this cert is blacklisted, then mark everything
361 * that depends on this as blacklisted too.
363 sinfo->blacklisted = true;
364 for (p = sinfo->signer; p != x509; p = p->signer)
365 p->blacklisted = true;
366 pr_debug("- blacklisted\n");
373 if (x509->unsupported_key)
374 goto unsupported_crypto_in_x509;
376 pr_debug("- issuer %s\n", x509->issuer);
378 if (sig->auth_ids[0])
379 pr_debug("- authkeyid.id %*phN\n",
380 sig->auth_ids[0]->len, sig->auth_ids[0]->data);
381 if (sig->auth_ids[1])
382 pr_debug("- authkeyid.skid %*phN\n",
383 sig->auth_ids[1]->len, sig->auth_ids[1]->data);
385 if (x509->self_signed) {
386 /* If there's no authority certificate specified, then
387 * the certificate must be self-signed and is the root
388 * of the chain. Likewise if the cert is its own
391 if (x509->unsupported_sig)
392 goto unsupported_crypto_in_x509;
394 pr_debug("- self-signed\n");
401 /* Look through the X.509 certificates in the PKCS#7 message's
402 * list to see if the next one is there.
404 auth = sig->auth_ids[0];
406 pr_debug("- want %*phN\n", auth->len, auth->data);
407 for (p = pkcs7->certs; p; p = p->next) {
408 pr_debug("- cmp [%u] %*phN\n",
409 p->index, p->id->len, p->id->data);
410 if (asymmetric_key_id_same(p->id, auth))
411 goto found_issuer_check_skid;
413 } else if (sig->auth_ids[1]) {
414 auth = sig->auth_ids[1];
415 pr_debug("- want %*phN\n", auth->len, auth->data);
416 for (p = pkcs7->certs; p; p = p->next) {
419 pr_debug("- cmp [%u] %*phN\n",
420 p->index, p->skid->len, p->skid->data);
421 if (asymmetric_key_id_same(p->skid, auth))
426 /* We didn't find the root of this chain */
433 found_issuer_check_skid:
434 /* We matched issuer + serialNumber, but if there's an
435 * authKeyId.keyId, that must match the CA subjKeyId also.
437 if (sig->auth_ids[1] &&
438 !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {
439 pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
440 sinfo->index, x509->index, p->index);
441 return -EKEYREJECTED;
444 pr_debug("- subject %s\n", p->subject);
446 pr_warn("Sig %u: X.509 chain contains loop\n",
453 ret = public_key_verify_signature(p->pub, x509->sig);
458 pr_debug("- self-signed\n");
470 unsupported_crypto_in_x509:
471 /* Just prune the certificate chain at this point if we lack some
472 * crypto module to go further. Note, however, we don't want to set
473 * sinfo->unsupported_crypto as the signed info block may still be
474 * validatable against an X.509 cert lower in the chain that we have a
481 * pkcs7_verify_one - Verify one signed information block from a PKCS#7
483 * @pkcs7: PKCS7 Signed Data
484 * @sinfo: PKCS7 Signed Info
485 * @signer: Signer's certificate
487 * Verify one signature in @sinfo and follow the certificate chain.
488 * If the chain reaches the end, the last certificate will be returned
491 * Return: 0 - on success, non-zero error code - otherwise
494 int pkcs7_verify_one(struct pkcs7_message *pkcs7,
495 struct pkcs7_signed_info *sinfo,
496 struct x509_certificate **signer)
498 static int pkcs7_verify_one(struct pkcs7_message *pkcs7,
499 struct pkcs7_signed_info *sinfo)
504 kenter(",%u", sinfo->index);
506 /* First of all, digest the data in the PKCS#7 message and the
507 * signed information block
509 ret = pkcs7_digest(pkcs7, sinfo);
513 /* Find the key for the signature if there is one */
514 ret = pkcs7_find_key(pkcs7, sinfo);
521 pr_devel("Using X.509[%u] for sig %u\n",
522 sinfo->signer->index, sinfo->index);
524 /* Check that the PKCS#7 signing time is valid according to the X.509
525 * certificate. We can't, however, check against the system clock
526 * since that may not have been set yet and may be wrong.
528 if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {
529 if (sinfo->signing_time < sinfo->signer->valid_from ||
530 sinfo->signing_time > sinfo->signer->valid_to) {
531 pr_warn("Message signed outside of X.509 validity window\n");
532 return -EKEYREJECTED;
536 /* Verify the PKCS#7 binary against the key */
537 ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);
541 pr_devel("Verified signature %u\n", sinfo->index);
543 /* Verify the internal certificate chain */
544 return pkcs7_verify_sig_chain(pkcs7, sinfo, signer);
549 * pkcs7_verify - Verify a PKCS#7 message
550 * @pkcs7: The PKCS#7 message to be verified
551 * @usage: The use to which the key is being put
553 * Verify a PKCS#7 message is internally consistent - that is, the data digest
554 * matches the digest in the AuthAttrs and any signature in the message or one
555 * of the X.509 certificates it carries that matches another X.509 cert in the
556 * message can be verified.
558 * This does not look to match the contents of the PKCS#7 message against any
559 * external public keys.
561 * Returns, in order of descending priority:
563 * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
564 * odds with the specified usage, or:
566 * (*) -EKEYREJECTED if a signature failed to match for which we found an
567 * appropriate X.509 certificate, or:
569 * (*) -EBADMSG if some part of the message was invalid, or:
571 * (*) 0 if a signature chain passed verification, or:
573 * (*) -EKEYREJECTED if a blacklisted key was encountered, or:
575 * (*) -ENOPKG if none of the signature chains are verifiable because suitable
576 * crypto modules couldn't be found.
578 int pkcs7_verify(struct pkcs7_message *pkcs7,
579 enum key_being_used_for usage)
581 struct pkcs7_signed_info *sinfo;
582 int actual_ret = -ENOPKG;
588 case VERIFYING_MODULE_SIGNATURE:
589 if (pkcs7->data_type != OID_data) {
590 pr_warn("Invalid module sig (not pkcs7-data)\n");
591 return -EKEYREJECTED;
593 if (pkcs7->have_authattrs) {
594 pr_warn("Invalid module sig (has authattrs)\n");
595 return -EKEYREJECTED;
598 case VERIFYING_FIRMWARE_SIGNATURE:
599 if (pkcs7->data_type != OID_data) {
600 pr_warn("Invalid firmware sig (not pkcs7-data)\n");
601 return -EKEYREJECTED;
603 if (!pkcs7->have_authattrs) {
604 pr_warn("Invalid firmware sig (missing authattrs)\n");
605 return -EKEYREJECTED;
608 case VERIFYING_KEXEC_PE_SIGNATURE:
609 if (pkcs7->data_type != OID_msIndirectData) {
610 pr_warn("Invalid kexec sig (not Authenticode)\n");
611 return -EKEYREJECTED;
613 /* Authattr presence checked in parser */
615 case VERIFYING_UNSPECIFIED_SIGNATURE:
616 if (pkcs7->data_type != OID_data) {
617 pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
618 return -EKEYREJECTED;
625 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
626 ret = pkcs7_verify_one(pkcs7, sinfo);
627 if (sinfo->blacklisted) {
628 if (actual_ret == -ENOPKG)
629 actual_ret = -EKEYREJECTED;
633 if (ret == -ENOPKG) {
634 sinfo->unsupported_crypto = true;
637 kleave(" = %d", ret);
643 kleave(" = %d", actual_ret);
646 EXPORT_SYMBOL_GPL(pkcs7_verify);
649 * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
650 * @pkcs7: The PKCS#7 message
651 * @data: The data to be verified
652 * @datalen: The amount of data
654 * Supply the detached data needed to verify a PKCS#7 message. Note that no
655 * attempt to retain/pin the data is made. That is left to the caller. The
656 * data will not be modified by pkcs7_verify() and will not be freed when the
657 * PKCS#7 message is freed.
659 * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
661 int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,
662 const void *data, size_t datalen)
665 pr_debug("Data already supplied\n");
669 pkcs7->data_len = datalen;
672 #endif /* __UBOOT__ */