1 /* Verify the signature on a PKCS#7 message.
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
12 #define pr_fmt(fmt) "PKCS7: "fmt
13 #include <linux/kernel.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
17 #include <linux/asn1.h>
18 #include <crypto/hash.h>
19 #include "public_key.h"
20 #include "pkcs7_parser.h"
23 * Digest the relevant parts of the PKCS#7 data
25 static int pkcs7_digest(struct pkcs7_message
*pkcs7
,
26 struct pkcs7_signed_info
*sinfo
)
28 struct crypto_shash
*tfm
;
29 struct shash_desc
*desc
;
30 size_t digest_size
, desc_size
;
34 kenter(",%u,%u", sinfo
->index
, sinfo
->sig
.pkey_hash_algo
);
36 if (sinfo
->sig
.pkey_hash_algo
>= PKEY_HASH__LAST
||
37 !hash_algo_name
[sinfo
->sig
.pkey_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(hash_algo_name
[sinfo
->sig
.pkey_hash_algo
],
46 return (PTR_ERR(tfm
) == -ENOENT
) ? -ENOPKG
: PTR_ERR(tfm
);
48 desc_size
= crypto_shash_descsize(tfm
) + sizeof(*desc
);
49 sinfo
->sig
.digest_size
= digest_size
= crypto_shash_digestsize(tfm
);
52 digest
= kzalloc(digest_size
+ desc_size
, GFP_KERNEL
);
56 desc
= digest
+ digest_size
;
58 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
60 /* Digest the message [RFC2315 9.3] */
61 ret
= crypto_shash_init(desc
);
64 ret
= crypto_shash_finup(desc
, pkcs7
->data
, pkcs7
->data_len
, digest
);
67 pr_devel("MsgDigest = [%*ph]\n", 8, digest
);
69 /* However, if there are authenticated attributes, there must be a
70 * message digest attribute amongst them which corresponds to the
71 * digest we just calculated.
73 if (sinfo
->authattrs
) {
76 if (!sinfo
->msgdigest
) {
77 pr_warn("Sig %u: No messageDigest\n", sinfo
->index
);
82 if (sinfo
->msgdigest_len
!= sinfo
->sig
.digest_size
) {
83 pr_debug("Sig %u: Invalid digest size (%u)\n",
84 sinfo
->index
, sinfo
->msgdigest_len
);
89 if (memcmp(digest
, sinfo
->msgdigest
, 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(digest
, 0, sinfo
->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
, digest
);
114 pr_devel("AADigest = [%*ph]\n", 8, digest
);
117 sinfo
->sig
.digest
= digest
;
123 crypto_free_shash(tfm
);
124 kleave(" = %d", ret
);
129 * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7
130 * uses the issuer's name and the issuing certificate serial number for
131 * matching purposes. These must match the certificate issuer's name (not
132 * subject's name) and the certificate serial number [RFC 2315 6.7].
134 static int pkcs7_find_key(struct pkcs7_message
*pkcs7
,
135 struct pkcs7_signed_info
*sinfo
)
137 struct x509_certificate
*x509
;
140 kenter("%u", sinfo
->index
);
142 for (x509
= pkcs7
->certs
; x509
; x509
= x509
->next
, certix
++) {
143 /* I'm _assuming_ that the generator of the PKCS#7 message will
144 * encode the fields from the X.509 cert in the same way in the
145 * PKCS#7 message - but I can't be 100% sure of that. It's
146 * possible this will need element-by-element comparison.
148 if (!asymmetric_key_id_same(x509
->id
, sinfo
->signing_cert_id
))
150 pr_devel("Sig %u: Found cert serial match X.509[%u]\n",
151 sinfo
->index
, certix
);
153 if (x509
->pub
->pkey_algo
!= sinfo
->sig
.pkey_algo
) {
154 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n",
159 sinfo
->signer
= x509
;
163 /* The relevant X.509 cert isn't found here, but it might be found in
166 pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",
168 sinfo
->signing_cert_id
->len
, sinfo
->signing_cert_id
->data
);
173 * Verify the internal certificate chain as best we can.
175 static int pkcs7_verify_sig_chain(struct pkcs7_message
*pkcs7
,
176 struct pkcs7_signed_info
*sinfo
)
178 struct x509_certificate
*x509
= sinfo
->signer
, *p
;
179 struct asymmetric_key_id
*auth
;
184 for (p
= pkcs7
->certs
; p
; p
= p
->next
)
188 pr_debug("verify %s: %*phN\n",
190 x509
->raw_serial_size
, x509
->raw_serial
);
192 ret
= x509_get_sig_params(x509
);
194 goto maybe_missing_crypto_in_x509
;
196 pr_debug("- issuer %s\n", x509
->issuer
);
198 pr_debug("- authkeyid.id %*phN\n",
199 x509
->akid_id
->len
, x509
->akid_id
->data
);
201 pr_debug("- authkeyid.skid %*phN\n",
202 x509
->akid_skid
->len
, x509
->akid_skid
->data
);
204 if ((!x509
->akid_id
&& !x509
->akid_skid
) ||
205 strcmp(x509
->subject
, x509
->issuer
) == 0) {
206 /* If there's no authority certificate specified, then
207 * the certificate must be self-signed and is the root
208 * of the chain. Likewise if the cert is its own
211 pr_debug("- no auth?\n");
212 if (x509
->raw_subject_size
!= x509
->raw_issuer_size
||
213 memcmp(x509
->raw_subject
, x509
->raw_issuer
,
214 x509
->raw_issuer_size
) != 0)
217 ret
= x509_check_signature(x509
->pub
, x509
);
219 goto maybe_missing_crypto_in_x509
;
221 pr_debug("- self-signed\n");
225 /* Look through the X.509 certificates in the PKCS#7 message's
226 * list to see if the next one is there.
228 auth
= x509
->akid_id
;
230 pr_debug("- want %*phN\n", auth
->len
, auth
->data
);
231 for (p
= pkcs7
->certs
; p
; p
= p
->next
) {
232 pr_debug("- cmp [%u] %*phN\n",
233 p
->index
, p
->id
->len
, p
->id
->data
);
234 if (asymmetric_key_id_same(p
->id
, auth
))
235 goto found_issuer_check_skid
;
238 auth
= x509
->akid_skid
;
239 pr_debug("- want %*phN\n", auth
->len
, auth
->data
);
240 for (p
= pkcs7
->certs
; p
; p
= p
->next
) {
243 pr_debug("- cmp [%u] %*phN\n",
244 p
->index
, p
->skid
->len
, p
->skid
->data
);
245 if (asymmetric_key_id_same(p
->skid
, auth
))
250 /* We didn't find the root of this chain */
254 found_issuer_check_skid
:
255 /* We matched issuer + serialNumber, but if there's an
256 * authKeyId.keyId, that must match the CA subjKeyId also.
258 if (x509
->akid_skid
&&
259 !asymmetric_key_id_same(p
->skid
, x509
->akid_skid
)) {
260 pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",
261 sinfo
->index
, x509
->index
, p
->index
);
262 return -EKEYREJECTED
;
265 pr_debug("- subject %s\n", p
->subject
);
267 pr_warn("Sig %u: X.509 chain contains loop\n",
271 ret
= x509_check_signature(p
->pub
, x509
);
276 pr_debug("- self-signed\n");
283 maybe_missing_crypto_in_x509
:
284 /* Just prune the certificate chain at this point if we lack some
285 * crypto module to go further. Note, however, we don't want to set
286 * sinfo->missing_crypto as the signed info block may still be
287 * validatable against an X.509 cert lower in the chain that we have a
296 * Verify one signed information block from a PKCS#7 message.
298 static int pkcs7_verify_one(struct pkcs7_message
*pkcs7
,
299 struct pkcs7_signed_info
*sinfo
)
303 kenter(",%u", sinfo
->index
);
305 /* First of all, digest the data in the PKCS#7 message and the
306 * signed information block
308 ret
= pkcs7_digest(pkcs7
, sinfo
);
312 /* Find the key for the signature if there is one */
313 ret
= pkcs7_find_key(pkcs7
, sinfo
);
320 pr_devel("Using X.509[%u] for sig %u\n",
321 sinfo
->signer
->index
, sinfo
->index
);
323 /* Check that the PKCS#7 signing time is valid according to the X.509
324 * certificate. We can't, however, check against the system clock
325 * since that may not have been set yet and may be wrong.
327 if (test_bit(sinfo_has_signing_time
, &sinfo
->aa_set
)) {
328 if (sinfo
->signing_time
< sinfo
->signer
->valid_from
||
329 sinfo
->signing_time
> sinfo
->signer
->valid_to
) {
330 pr_warn("Message signed outside of X.509 validity window\n");
331 return -EKEYREJECTED
;
335 /* Verify the PKCS#7 binary against the key */
336 ret
= public_key_verify_signature(sinfo
->signer
->pub
, &sinfo
->sig
);
340 pr_devel("Verified signature %u\n", sinfo
->index
);
342 /* Verify the internal certificate chain */
343 return pkcs7_verify_sig_chain(pkcs7
, sinfo
);
347 * pkcs7_verify - Verify a PKCS#7 message
348 * @pkcs7: The PKCS#7 message to be verified
349 * @usage: The use to which the key is being put
351 * Verify a PKCS#7 message is internally consistent - that is, the data digest
352 * matches the digest in the AuthAttrs and any signature in the message or one
353 * of the X.509 certificates it carries that matches another X.509 cert in the
354 * message can be verified.
356 * This does not look to match the contents of the PKCS#7 message against any
357 * external public keys.
359 * Returns, in order of descending priority:
361 * (*) -EKEYREJECTED if a key was selected that had a usage restriction at
362 * odds with the specified usage, or:
364 * (*) -EKEYREJECTED if a signature failed to match for which we found an
365 * appropriate X.509 certificate, or:
367 * (*) -EBADMSG if some part of the message was invalid, or:
369 * (*) -ENOPKG if none of the signature chains are verifiable because suitable
370 * crypto modules couldn't be found, or:
372 * (*) 0 if all the signature chains that don't incur -ENOPKG can be verified
373 * (note that a signature chain may be of zero length), or:
375 int pkcs7_verify(struct pkcs7_message
*pkcs7
,
376 enum key_being_used_for usage
)
378 struct pkcs7_signed_info
*sinfo
;
379 struct x509_certificate
*x509
;
380 int enopkg
= -ENOPKG
;
386 case VERIFYING_MODULE_SIGNATURE
:
387 if (pkcs7
->data_type
!= OID_data
) {
388 pr_warn("Invalid module sig (not pkcs7-data)\n");
389 return -EKEYREJECTED
;
391 if (pkcs7
->have_authattrs
) {
392 pr_warn("Invalid module sig (has authattrs)\n");
393 return -EKEYREJECTED
;
396 case VERIFYING_FIRMWARE_SIGNATURE
:
397 if (pkcs7
->data_type
!= OID_data
) {
398 pr_warn("Invalid firmware sig (not pkcs7-data)\n");
399 return -EKEYREJECTED
;
401 if (!pkcs7
->have_authattrs
) {
402 pr_warn("Invalid firmware sig (missing authattrs)\n");
403 return -EKEYREJECTED
;
406 case VERIFYING_KEXEC_PE_SIGNATURE
:
407 if (pkcs7
->data_type
!= OID_msIndirectData
) {
408 pr_warn("Invalid kexec sig (not Authenticode)\n");
409 return -EKEYREJECTED
;
411 /* Authattr presence checked in parser */
413 case VERIFYING_UNSPECIFIED_SIGNATURE
:
414 if (pkcs7
->data_type
!= OID_data
) {
415 pr_warn("Invalid unspecified sig (not pkcs7-data)\n");
416 return -EKEYREJECTED
;
423 for (n
= 0, x509
= pkcs7
->certs
; x509
; x509
= x509
->next
, n
++) {
424 ret
= x509_get_sig_params(x509
);
429 for (sinfo
= pkcs7
->signed_infos
; sinfo
; sinfo
= sinfo
->next
) {
430 ret
= pkcs7_verify_one(pkcs7
, sinfo
);
432 if (ret
== -ENOPKG
) {
433 sinfo
->unsupported_crypto
= true;
436 kleave(" = %d", ret
);
442 kleave(" = %d", enopkg
);
445 EXPORT_SYMBOL_GPL(pkcs7_verify
);
448 * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message
449 * @pkcs7: The PKCS#7 message
450 * @data: The data to be verified
451 * @datalen: The amount of data
453 * Supply the detached data needed to verify a PKCS#7 message. Note that no
454 * attempt to retain/pin the data is made. That is left to the caller. The
455 * data will not be modified by pkcs7_verify() and will not be freed when the
456 * PKCS#7 message is freed.
458 * Returns -EINVAL if data is already supplied in the message, 0 otherwise.
460 int pkcs7_supply_detached_data(struct pkcs7_message
*pkcs7
,
461 const void *data
, size_t datalen
)
464 pr_debug("Data already supplied\n");
468 pkcs7
->data_len
= datalen
;