1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Verify the signature on a PKCS#7 message.
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
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/slab.h>
12 #include <linux/err.h>
13 #include <linux/asn1.h>
14 #include <crypto/hash.h>
15 #include <crypto/hash_info.h>
16 #include <crypto/public_key.h>
17 #include "pkcs7_parser.h"
20 * Digest the relevant parts of the PKCS#7 data
22 static int pkcs7_digest(struct pkcs7_message
*pkcs7
,
23 struct pkcs7_signed_info
*sinfo
)
25 struct public_key_signature
*sig
= sinfo
->sig
;
26 struct crypto_shash
*tfm
;
27 struct shash_desc
*desc
;
31 kenter(",%u,%s", sinfo
->index
, sinfo
->sig
->hash_algo
);
33 /* The digest was calculated already. */
37 if (!sinfo
->sig
->hash_algo
)
40 /* Allocate the hashing algorithm we're going to need and find out how
41 * big the hash operational data will be.
43 tfm
= crypto_alloc_shash(sinfo
->sig
->hash_algo
, 0, 0);
45 return (PTR_ERR(tfm
) == -ENOENT
) ? -ENOPKG
: PTR_ERR(tfm
);
47 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
48 sig
->digest_size
= crypto_shash_digestsize(tfm
);
51 sig
->digest
= kmalloc(sig
->digest_size
, GFP_KERNEL
);
55 desc
= kzalloc(desc_size
, GFP_KERNEL
);
61 /* Digest the message [RFC2315 9.3] */
62 ret
= crypto_shash_digest(desc
, pkcs7
->data
, pkcs7
->data_len
,
66 pr_devel("MsgDigest = [%*ph]\n", 8, sig
->digest
);
68 /* However, if there are authenticated attributes, there must be a
69 * message digest attribute amongst them which corresponds to the
70 * digest we just calculated.
72 if (sinfo
->authattrs
) {
75 if (!sinfo
->msgdigest
) {
76 pr_warn("Sig %u: No messageDigest\n", sinfo
->index
);
81 if (sinfo
->msgdigest_len
!= sig
->digest_size
) {
82 pr_debug("Sig %u: Invalid digest size (%u)\n",
83 sinfo
->index
, sinfo
->msgdigest_len
);
88 if (memcmp(sig
->digest
, sinfo
->msgdigest
,
89 sinfo
->msgdigest_len
) != 0) {
90 pr_debug("Sig %u: Message digest doesn't match\n",
96 /* We then calculate anew, using the authenticated attributes
97 * as the contents of the digest instead. Note that we need to
98 * convert the attributes from a CONT.0 into a SET before we
101 memset(sig
->digest
, 0, sig
->digest_size
);
103 ret
= crypto_shash_init(desc
);
106 tag
= ASN1_CONS_BIT
| ASN1_SET
;
107 ret
= crypto_shash_update(desc
, &tag
, 1);
110 ret
= crypto_shash_finup(desc
, sinfo
->authattrs
,
111 sinfo
->authattrs_len
, sig
->digest
);
114 pr_devel("AADigest = [%*ph]\n", 8, sig
->digest
);
120 crypto_free_shash(tfm
);
121 kleave(" = %d", ret
);
125 int pkcs7_get_digest(struct pkcs7_message
*pkcs7
, const u8
**buf
, u32
*len
,
126 enum hash_algo
*hash_algo
)
128 struct pkcs7_signed_info
*sinfo
= pkcs7
->signed_infos
;
132 * This function doesn't support messages with more than one signature.
134 if (sinfo
== NULL
|| sinfo
->next
!= NULL
)
137 ret
= pkcs7_digest(pkcs7
, sinfo
);
141 *buf
= sinfo
->sig
->digest
;
142 *len
= sinfo
->sig
->digest_size
;
144 for (i
= 0; i
< HASH_ALGO__LAST
; i
++)
145 if (!strcmp(hash_algo_name
[i
], sinfo
->sig
->hash_algo
)) {
154 * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
155 * uses the issuer's name and the issuing certificate serial number for
156 * matching purposes. These must match the certificate issuer's name (not
157 * subject's name) and the certificate serial number [RFC 2315 6.7].
159 static int pkcs7_find_key(struct pkcs7_message
*pkcs7
,
160 struct pkcs7_signed_info
*sinfo
)
162 struct x509_certificate
*x509
;
165 kenter("%u", sinfo
->index
);
167 for (x509
= pkcs7
->certs
; x509
; x509
= x509
->next
, certix
++) {
168 /* I'm _assuming_ that the generator of the PKCS#7 message will
169 * encode the fields from the X.509 cert in the same way in the
170 * PKCS#7 message - but I can't be 100% sure of that. It's
171 * possible this will need element-by-element comparison.
173 if (!asymmetric_key_id_same(x509
->id
, sinfo
->sig
->auth_ids
[0]))
175 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
176 sinfo
->index
, certix
);
178 if (strcmp(x509
->pub
->pkey_algo
, sinfo
->sig
->pkey_algo
) != 0) {
179 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
184 sinfo
->signer
= x509
;
188 /* The relevant X.509 cert isn't found here, but it might be found in
191 pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
193 sinfo
->sig
->auth_ids
[0]->len
, sinfo
->sig
->auth_ids
[0]->data
);
198 * Verify the internal certificate chain as best we can.
200 static int pkcs7_verify_sig_chain(struct pkcs7_message
*pkcs7
,
201 struct pkcs7_signed_info
*sinfo
)
203 struct public_key_signature
*sig
;
204 struct x509_certificate
*x509
= sinfo
->signer
, *p
;
205 struct asymmetric_key_id
*auth
;
210 for (p
= pkcs7
->certs
; p
; p
= p
->next
)
214 pr_debug("verify %s: %*phN\n",
216 x509
->raw_serial_size
, x509
->raw_serial
);
219 if (x509
->blacklisted
) {
220 /* If this cert is blacklisted, then mark everything
221 * that depends on this as blacklisted too.
223 sinfo
->blacklisted
= true;
224 for (p
= sinfo
->signer
; p
!= x509
; p
= p
->signer
)
225 p
->blacklisted
= true;
226 pr_debug("- blacklisted\n");
230 if (x509
->unsupported_key
)
231 goto unsupported_crypto_in_x509
;
233 pr_debug("- issuer %s\n", x509
->issuer
);
235 if (sig
->auth_ids
[0])
236 pr_debug("- authkeyid.id %*phN\n",
237 sig
->auth_ids
[0]->len
, sig
->auth_ids
[0]->data
);
238 if (sig
->auth_ids
[1])
239 pr_debug("- authkeyid.skid %*phN\n",
240 sig
->auth_ids
[1]->len
, sig
->auth_ids
[1]->data
);
242 if (x509
->self_signed
) {
243 /* If there's no authority certificate specified, then
244 * the certificate must be self-signed and is the root
245 * of the chain. Likewise if the cert is its own
248 if (x509
->unsupported_sig
)
249 goto unsupported_crypto_in_x509
;
251 pr_debug("- self-signed\n");
255 /* Look through the X.509 certificates in the PKCS#7 message's
256 * list to see if the next one is there.
258 auth
= sig
->auth_ids
[0];
260 pr_debug("- want %*phN\n", auth
->len
, auth
->data
);
261 for (p
= pkcs7
->certs
; p
; p
= p
->next
) {
262 pr_debug("- cmp [%u] %*phN\n",
263 p
->index
, p
->id
->len
, p
->id
->data
);
264 if (asymmetric_key_id_same(p
->id
, auth
))
265 goto found_issuer_check_skid
;
267 } else if (sig
->auth_ids
[1]) {
268 auth
= sig
->auth_ids
[1];
269 pr_debug("- want %*phN\n", auth
->len
, auth
->data
);
270 for (p
= pkcs7
->certs
; p
; p
= p
->next
) {
273 pr_debug("- cmp [%u] %*phN\n",
274 p
->index
, p
->skid
->len
, p
->skid
->data
);
275 if (asymmetric_key_id_same(p
->skid
, auth
))
280 /* We didn't find the root of this chain */
284 found_issuer_check_skid
:
285 /* We matched issuer + serialNumber, but if there's an
286 * authKeyId.keyId, that must match the CA subjKeyId also.
288 if (sig
->auth_ids
[1] &&
289 !asymmetric_key_id_same(p
->skid
, sig
->auth_ids
[1])) {
290 pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
291 sinfo
->index
, x509
->index
, p
->index
);
292 return -EKEYREJECTED
;
295 pr_debug("- subject %s\n", p
->subject
);
297 pr_warn("Sig %u: X.509 chain contains loop\n",
301 ret
= public_key_verify_signature(p
->pub
, x509
->sig
);
306 pr_debug("- self-signed\n");
313 unsupported_crypto_in_x509
:
314 /* Just prune the certificate chain at this point if we lack some
315 * crypto module to go further. Note, however, we don't want to set
316 * sinfo->unsupported_crypto as the signed info block may still be
317 * validatable against an X.509 cert lower in the chain that we have a
324 * Verify one signed information block from a PKCS#7 message.
326 static int pkcs7_verify_one(struct pkcs7_message
*pkcs7
,
327 struct pkcs7_signed_info
*sinfo
)
331 kenter(",%u", sinfo
->index
);
333 /* First of all, digest the data in the PKCS#7 message and the
334 * signed information block
336 ret
= pkcs7_digest(pkcs7
, sinfo
);
340 /* Find the key for the signature if there is one */
341 ret
= pkcs7_find_key(pkcs7
, sinfo
);
348 pr_devel("Using X.509[%u] for sig %u\n",
349 sinfo
->signer
->index
, sinfo
->index
);
351 /* Check that the PKCS#7 signing time is valid according to the X.509
352 * certificate. We can't, however, check against the system clock
353 * since that may not have been set yet and may be wrong.
355 if (test_bit(sinfo_has_signing_time
, &sinfo
->aa_set
)) {
356 if (sinfo
->signing_time
< sinfo
->signer
->valid_from
||
357 sinfo
->signing_time
> sinfo
->signer
->valid_to
) {
358 pr_warn("Message signed outside of X.509 validity window\n");
359 return -EKEYREJECTED
;
363 /* Verify the PKCS#7 binary against the key */
364 ret
= public_key_verify_signature(sinfo
->signer
->pub
, sinfo
->sig
);
368 pr_devel("Verified signature %u\n", sinfo
->index
);
370 /* Verify the internal certificate chain */
371 return pkcs7_verify_sig_chain(pkcs7
, sinfo
);
375 * pkcs7_verify - Verify a PKCS#7 message
376 * @pkcs7: The PKCS#7 message to be verified
377 * @usage: The use to which the key is being put
379 * Verify a PKCS#7 message is internally consistent - that is, the data digest
380 * matches the digest in the AuthAttrs and any signature in the message or one
381 * of the X.509 certificates it carries that matches another X.509 cert in the
382 * message can be verified.
384 * This does not look to match the contents of the PKCS#7 message against any
385 * external public keys.
387 * Returns, in order of descending priority:
389 * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
390 * odds with the specified usage, or:
392 * (*) -EKEYREJECTED if a signature failed to match for which we found an
393 * appropriate X.509 certificate, or:
395 * (*) -EBADMSG if some part of the message was invalid, or:
397 * (*) 0 if a signature chain passed verification, or:
399 * (*) -EKEYREJECTED if a blacklisted key was encountered, or:
401 * (*) -ENOPKG if none of the signature chains are verifiable because suitable
402 * crypto modules couldn't be found.
404 int pkcs7_verify(struct pkcs7_message
*pkcs7
,
405 enum key_being_used_for usage
)
407 struct pkcs7_signed_info
*sinfo
;
408 int actual_ret
= -ENOPKG
;
414 case VERIFYING_MODULE_SIGNATURE
:
415 if (pkcs7
->data_type
!= OID_data
) {
416 pr_warn("Invalid module sig (not pkcs7-data)\n");
417 return -EKEYREJECTED
;
419 if (pkcs7
->have_authattrs
) {
420 pr_warn("Invalid module sig (has authattrs)\n");
421 return -EKEYREJECTED
;
424 case VERIFYING_FIRMWARE_SIGNATURE
:
425 if (pkcs7
->data_type
!= OID_data
) {
426 pr_warn("Invalid firmware sig (not pkcs7-data)\n");
427 return -EKEYREJECTED
;
429 if (!pkcs7
->have_authattrs
) {
430 pr_warn("Invalid firmware sig (missing authattrs)\n");
431 return -EKEYREJECTED
;
434 case VERIFYING_KEXEC_PE_SIGNATURE
:
435 if (pkcs7
->data_type
!= OID_msIndirectData
) {
436 pr_warn("Invalid kexec sig (not Authenticode)\n");
437 return -EKEYREJECTED
;
439 /* Authattr presence checked in parser */
441 case VERIFYING_UNSPECIFIED_SIGNATURE
:
442 if (pkcs7
->data_type
!= OID_data
) {
443 pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
444 return -EKEYREJECTED
;
451 for (sinfo
= pkcs7
->signed_infos
; sinfo
; sinfo
= sinfo
->next
) {
452 ret
= pkcs7_verify_one(pkcs7
, sinfo
);
453 if (sinfo
->blacklisted
) {
454 if (actual_ret
== -ENOPKG
)
455 actual_ret
= -EKEYREJECTED
;
459 if (ret
== -ENOPKG
) {
460 sinfo
->unsupported_crypto
= true;
463 kleave(" = %d", ret
);
469 kleave(" = %d", actual_ret
);
472 EXPORT_SYMBOL_GPL(pkcs7_verify
);
475 * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
476 * @pkcs7: The PKCS#7 message
477 * @data: The data to be verified
478 * @datalen: The amount of data
480 * Supply the detached data needed to verify a PKCS#7 message. Note that no
481 * attempt to retain/pin the data is made. That is left to the caller. The
482 * data will not be modified by pkcs7_verify() and will not be freed when the
483 * PKCS#7 message is freed.
485 * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
487 int pkcs7_supply_detached_data(struct pkcs7_message
*pkcs7
,
488 const void *data
, size_t datalen
)
491 pr_debug("Data already supplied\n");
495 pkcs7
->data_len
= datalen
;