Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 2 | /* Verify the signature on a PKCS#7 message. |
| 3 | * |
| 4 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 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> |
Tadeusz Struk | db6c43b | 2016-02-02 10:08:53 -0800 | [diff] [blame] | 15 | #include <crypto/public_key.h> |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 16 | #include "pkcs7_parser.h" |
| 17 | |
| 18 | /* |
| 19 | * Digest the relevant parts of the PKCS#7 data |
| 20 | */ |
| 21 | static int pkcs7_digest(struct pkcs7_message *pkcs7, |
| 22 | struct pkcs7_signed_info *sinfo) |
| 23 | { |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 24 | struct public_key_signature *sig = sinfo->sig; |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 25 | struct crypto_shash *tfm; |
| 26 | struct shash_desc *desc; |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 27 | size_t desc_size; |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 28 | int ret; |
| 29 | |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 30 | kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 31 | |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 32 | if (!sinfo->sig->hash_algo) |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 33 | return -ENOPKG; |
| 34 | |
| 35 | /* Allocate the hashing algorithm we're going to need and find out how |
| 36 | * big the hash operational data will be. |
| 37 | */ |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 38 | tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 39 | if (IS_ERR(tfm)) |
| 40 | return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm); |
| 41 | |
| 42 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 43 | sig->digest_size = crypto_shash_digestsize(tfm); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 44 | |
| 45 | ret = -ENOMEM; |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 46 | sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); |
| 47 | if (!sig->digest) |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 48 | goto error_no_desc; |
| 49 | |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 50 | desc = kzalloc(desc_size, GFP_KERNEL); |
| 51 | if (!desc) |
| 52 | goto error_no_desc; |
| 53 | |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 54 | desc->tfm = tfm; |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 55 | |
| 56 | /* Digest the message [RFC2315 9.3] */ |
Eric Biggers | a80745a | 2017-12-08 15:13:28 +0000 | [diff] [blame] | 57 | ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len, |
| 58 | sig->digest); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 59 | if (ret < 0) |
| 60 | goto error; |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 61 | pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 62 | |
| 63 | /* However, if there are authenticated attributes, there must be a |
| 64 | * message digest attribute amongst them which corresponds to the |
| 65 | * digest we just calculated. |
| 66 | */ |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 67 | if (sinfo->authattrs) { |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 68 | u8 tag; |
| 69 | |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 70 | if (!sinfo->msgdigest) { |
| 71 | pr_warn("Sig %u: No messageDigest\n", sinfo->index); |
| 72 | ret = -EKEYREJECTED; |
| 73 | goto error; |
| 74 | } |
| 75 | |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 76 | if (sinfo->msgdigest_len != sig->digest_size) { |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 77 | pr_debug("Sig %u: Invalid digest size (%u)\n", |
| 78 | sinfo->index, sinfo->msgdigest_len); |
| 79 | ret = -EBADMSG; |
| 80 | goto error; |
| 81 | } |
| 82 | |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 83 | if (memcmp(sig->digest, sinfo->msgdigest, |
| 84 | sinfo->msgdigest_len) != 0) { |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 85 | pr_debug("Sig %u: Message digest doesn't match\n", |
| 86 | sinfo->index); |
| 87 | ret = -EKEYREJECTED; |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | /* We then calculate anew, using the authenticated attributes |
| 92 | * as the contents of the digest instead. Note that we need to |
| 93 | * convert the attributes from a CONT.0 into a SET before we |
| 94 | * hash it. |
| 95 | */ |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 96 | memset(sig->digest, 0, sig->digest_size); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 97 | |
| 98 | ret = crypto_shash_init(desc); |
| 99 | if (ret < 0) |
| 100 | goto error; |
| 101 | tag = ASN1_CONS_BIT | ASN1_SET; |
| 102 | ret = crypto_shash_update(desc, &tag, 1); |
| 103 | if (ret < 0) |
| 104 | goto error; |
| 105 | ret = crypto_shash_finup(desc, sinfo->authattrs, |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 106 | sinfo->authattrs_len, sig->digest); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 107 | if (ret < 0) |
| 108 | goto error; |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 109 | pr_devel("AADigest = [%*ph]\n", 8, sig->digest); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 110 | } |
| 111 | |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 112 | error: |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 113 | kfree(desc); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 114 | error_no_desc: |
| 115 | crypto_free_shash(tfm); |
| 116 | kleave(" = %d", ret); |
| 117 | return ret; |
| 118 | } |
| 119 | |
| 120 | /* |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 121 | * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7 |
| 122 | * uses the issuer's name and the issuing certificate serial number for |
| 123 | * matching purposes. These must match the certificate issuer's name (not |
| 124 | * subject's name) and the certificate serial number [RFC 2315 6.7]. |
| 125 | */ |
| 126 | static int pkcs7_find_key(struct pkcs7_message *pkcs7, |
| 127 | struct pkcs7_signed_info *sinfo) |
| 128 | { |
| 129 | struct x509_certificate *x509; |
| 130 | unsigned certix = 1; |
| 131 | |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 132 | kenter("%u", sinfo->index); |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 133 | |
| 134 | for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) { |
| 135 | /* I'm _assuming_ that the generator of the PKCS#7 message will |
| 136 | * encode the fields from the X.509 cert in the same way in the |
| 137 | * PKCS#7 message - but I can't be 100% sure of that. It's |
| 138 | * possible this will need element-by-element comparison. |
| 139 | */ |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 140 | if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0])) |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 141 | continue; |
| 142 | pr_devel("Sig %u: Found cert serial match X.509[%u]\n", |
| 143 | sinfo->index, certix); |
| 144 | |
Eric Biggers | 54c1fb3 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 145 | if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) { |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 146 | pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n", |
| 147 | sinfo->index); |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | sinfo->signer = x509; |
| 152 | return 0; |
| 153 | } |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 154 | |
David Howells | 757932e | 2014-09-16 17:36:17 +0100 | [diff] [blame] | 155 | /* The relevant X.509 cert isn't found here, but it might be found in |
| 156 | * the trust keyring. |
| 157 | */ |
| 158 | pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n", |
| 159 | sinfo->index, |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 160 | sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data); |
David Howells | 757932e | 2014-09-16 17:36:17 +0100 | [diff] [blame] | 161 | return 0; |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 162 | } |
| 163 | |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 164 | /* |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 165 | * Verify the internal certificate chain as best we can. |
| 166 | */ |
| 167 | static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, |
| 168 | struct pkcs7_signed_info *sinfo) |
| 169 | { |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 170 | struct public_key_signature *sig; |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 171 | struct x509_certificate *x509 = sinfo->signer, *p; |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 172 | struct asymmetric_key_id *auth; |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 173 | int ret; |
| 174 | |
| 175 | kenter(""); |
| 176 | |
| 177 | for (p = pkcs7->certs; p; p = p->next) |
| 178 | p->seen = false; |
| 179 | |
| 180 | for (;;) { |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 181 | pr_debug("verify %s: %*phN\n", |
| 182 | x509->subject, |
| 183 | x509->raw_serial_size, x509->raw_serial); |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 184 | x509->seen = true; |
David Howells | 03bb793 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 185 | |
| 186 | if (x509->blacklisted) { |
| 187 | /* If this cert is blacklisted, then mark everything |
| 188 | * that depends on this as blacklisted too. |
| 189 | */ |
| 190 | sinfo->blacklisted = true; |
| 191 | for (p = sinfo->signer; p != x509; p = p->signer) |
| 192 | p->blacklisted = true; |
| 193 | pr_debug("- blacklisted\n"); |
| 194 | return 0; |
| 195 | } |
| 196 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 197 | if (x509->unsupported_key) |
| 198 | goto unsupported_crypto_in_x509; |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 199 | |
David Howells | 412eccb | 2014-07-31 14:46:44 +0100 | [diff] [blame] | 200 | pr_debug("- issuer %s\n", x509->issuer); |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 201 | sig = x509->sig; |
| 202 | if (sig->auth_ids[0]) |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 203 | pr_debug("- authkeyid.id %*phN\n", |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 204 | sig->auth_ids[0]->len, sig->auth_ids[0]->data); |
| 205 | if (sig->auth_ids[1]) |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 206 | pr_debug("- authkeyid.skid %*phN\n", |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 207 | sig->auth_ids[1]->len, sig->auth_ids[1]->data); |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 208 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 209 | if (x509->self_signed) { |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 210 | /* If there's no authority certificate specified, then |
| 211 | * the certificate must be self-signed and is the root |
| 212 | * of the chain. Likewise if the cert is its own |
| 213 | * authority. |
| 214 | */ |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 215 | if (x509->unsupported_sig) |
| 216 | goto unsupported_crypto_in_x509; |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 217 | x509->signer = x509; |
| 218 | pr_debug("- self-signed\n"); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* Look through the X.509 certificates in the PKCS#7 message's |
| 223 | * list to see if the next one is there. |
| 224 | */ |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 225 | auth = sig->auth_ids[0]; |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 226 | if (auth) { |
| 227 | pr_debug("- want %*phN\n", auth->len, auth->data); |
| 228 | for (p = pkcs7->certs; p; p = p->next) { |
| 229 | pr_debug("- cmp [%u] %*phN\n", |
| 230 | p->index, p->id->len, p->id->data); |
| 231 | if (asymmetric_key_id_same(p->id, auth)) |
| 232 | goto found_issuer_check_skid; |
| 233 | } |
Lans Zhang | a46e667 | 2016-07-18 00:10:39 +0100 | [diff] [blame] | 234 | } else if (sig->auth_ids[1]) { |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 235 | auth = sig->auth_ids[1]; |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 236 | pr_debug("- want %*phN\n", auth->len, auth->data); |
| 237 | for (p = pkcs7->certs; p; p = p->next) { |
| 238 | if (!p->skid) |
| 239 | continue; |
| 240 | pr_debug("- cmp [%u] %*phN\n", |
| 241 | p->index, p->skid->len, p->skid->data); |
| 242 | if (asymmetric_key_id_same(p->skid, auth)) |
| 243 | goto found_issuer; |
| 244 | } |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | /* We didn't find the root of this chain */ |
| 248 | pr_debug("- top\n"); |
| 249 | return 0; |
| 250 | |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 251 | found_issuer_check_skid: |
| 252 | /* We matched issuer + serialNumber, but if there's an |
| 253 | * authKeyId.keyId, that must match the CA subjKeyId also. |
| 254 | */ |
David Howells | 77d0910 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 255 | if (sig->auth_ids[1] && |
| 256 | !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) { |
David Howells | 4573b64 | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 257 | pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n", |
| 258 | sinfo->index, x509->index, p->index); |
| 259 | return -EKEYREJECTED; |
| 260 | } |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 261 | found_issuer: |
David Howells | 46963b7 | 2014-09-16 17:36:13 +0100 | [diff] [blame] | 262 | pr_debug("- subject %s\n", p->subject); |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 263 | if (p->seen) { |
| 264 | pr_warn("Sig %u: X.509 chain contains loop\n", |
| 265 | sinfo->index); |
| 266 | return 0; |
| 267 | } |
Eric Biggers | 971b42c | 2018-02-22 14:38:33 +0000 | [diff] [blame] | 268 | ret = public_key_verify_signature(p->pub, x509->sig); |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 269 | if (ret < 0) |
| 270 | return ret; |
| 271 | x509->signer = p; |
| 272 | if (x509 == p) { |
| 273 | pr_debug("- self-signed\n"); |
| 274 | return 0; |
| 275 | } |
| 276 | x509 = p; |
| 277 | might_sleep(); |
| 278 | } |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 279 | |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 280 | unsupported_crypto_in_x509: |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 281 | /* Just prune the certificate chain at this point if we lack some |
| 282 | * crypto module to go further. Note, however, we don't want to set |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 283 | * sinfo->unsupported_crypto as the signed info block may still be |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 284 | * validatable against an X.509 cert lower in the chain that we have a |
| 285 | * trusted copy of. |
| 286 | */ |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 287 | return 0; |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /* |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 291 | * Verify one signed information block from a PKCS#7 message. |
| 292 | */ |
| 293 | static int pkcs7_verify_one(struct pkcs7_message *pkcs7, |
| 294 | struct pkcs7_signed_info *sinfo) |
| 295 | { |
| 296 | int ret; |
| 297 | |
| 298 | kenter(",%u", sinfo->index); |
| 299 | |
| 300 | /* First of all, digest the data in the PKCS#7 message and the |
| 301 | * signed information block |
| 302 | */ |
| 303 | ret = pkcs7_digest(pkcs7, sinfo); |
| 304 | if (ret < 0) |
| 305 | return ret; |
| 306 | |
David Howells | 757932e | 2014-09-16 17:36:17 +0100 | [diff] [blame] | 307 | /* Find the key for the signature if there is one */ |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 308 | ret = pkcs7_find_key(pkcs7, sinfo); |
| 309 | if (ret < 0) |
| 310 | return ret; |
| 311 | |
David Howells | 757932e | 2014-09-16 17:36:17 +0100 | [diff] [blame] | 312 | if (!sinfo->signer) |
| 313 | return 0; |
| 314 | |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 315 | pr_devel("Using X.509[%u] for sig %u\n", |
| 316 | sinfo->signer->index, sinfo->index); |
| 317 | |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 318 | /* Check that the PKCS#7 signing time is valid according to the X.509 |
| 319 | * certificate. We can't, however, check against the system clock |
| 320 | * since that may not have been set yet and may be wrong. |
| 321 | */ |
| 322 | if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) { |
| 323 | if (sinfo->signing_time < sinfo->signer->valid_from || |
| 324 | sinfo->signing_time > sinfo->signer->valid_to) { |
| 325 | pr_warn("Message signed outside of X.509 validity window\n"); |
| 326 | return -EKEYREJECTED; |
| 327 | } |
| 328 | } |
| 329 | |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 330 | /* Verify the PKCS#7 binary against the key */ |
David Howells | 566a117 | 2016-04-06 16:13:33 +0100 | [diff] [blame] | 331 | ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig); |
David Howells | a473035 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 332 | if (ret < 0) |
| 333 | return ret; |
| 334 | |
| 335 | pr_devel("Verified signature %u\n", sinfo->index); |
| 336 | |
David Howells | 8c76d79 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 337 | /* Verify the internal certificate chain */ |
| 338 | return pkcs7_verify_sig_chain(pkcs7, sinfo); |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * pkcs7_verify - Verify a PKCS#7 message |
| 343 | * @pkcs7: The PKCS#7 message to be verified |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 344 | * @usage: The use to which the key is being put |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 345 | * |
| 346 | * Verify a PKCS#7 message is internally consistent - that is, the data digest |
| 347 | * matches the digest in the AuthAttrs and any signature in the message or one |
| 348 | * of the X.509 certificates it carries that matches another X.509 cert in the |
| 349 | * message can be verified. |
| 350 | * |
| 351 | * This does not look to match the contents of the PKCS#7 message against any |
| 352 | * external public keys. |
| 353 | * |
| 354 | * Returns, in order of descending priority: |
| 355 | * |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 356 | * (*) -EKEYREJECTED if a key was selected that had a usage restriction at |
| 357 | * odds with the specified usage, or: |
| 358 | * |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 359 | * (*) -EKEYREJECTED if a signature failed to match for which we found an |
| 360 | * appropriate X.509 certificate, or: |
| 361 | * |
| 362 | * (*) -EBADMSG if some part of the message was invalid, or: |
| 363 | * |
Eric Biggers | 29f4a67 | 2018-02-22 14:38:33 +0000 | [diff] [blame] | 364 | * (*) 0 if a signature chain passed verification, or: |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 365 | * |
David Howells | 03bb793 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 366 | * (*) -EKEYREJECTED if a blacklisted key was encountered, or: |
| 367 | * |
| 368 | * (*) -ENOPKG if none of the signature chains are verifiable because suitable |
| 369 | * crypto modules couldn't be found. |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 370 | */ |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 371 | int pkcs7_verify(struct pkcs7_message *pkcs7, |
| 372 | enum key_being_used_for usage) |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 373 | { |
| 374 | struct pkcs7_signed_info *sinfo; |
David Howells | 03bb793 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 375 | int actual_ret = -ENOPKG; |
David Howells | 6c2dc5a | 2016-04-06 16:13:34 +0100 | [diff] [blame] | 376 | int ret; |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 377 | |
| 378 | kenter(""); |
| 379 | |
David Howells | 99db443 | 2015-08-05 15:22:27 +0100 | [diff] [blame] | 380 | switch (usage) { |
| 381 | case VERIFYING_MODULE_SIGNATURE: |
| 382 | if (pkcs7->data_type != OID_data) { |
| 383 | pr_warn("Invalid module sig (not pkcs7-data)\n"); |
| 384 | return -EKEYREJECTED; |
| 385 | } |
| 386 | if (pkcs7->have_authattrs) { |
| 387 | pr_warn("Invalid module sig (has authattrs)\n"); |
| 388 | return -EKEYREJECTED; |
| 389 | } |
| 390 | break; |
| 391 | case VERIFYING_FIRMWARE_SIGNATURE: |
| 392 | if (pkcs7->data_type != OID_data) { |
| 393 | pr_warn("Invalid firmware sig (not pkcs7-data)\n"); |
| 394 | return -EKEYREJECTED; |
| 395 | } |
| 396 | if (!pkcs7->have_authattrs) { |
| 397 | pr_warn("Invalid firmware sig (missing authattrs)\n"); |
| 398 | return -EKEYREJECTED; |
| 399 | } |
| 400 | break; |
| 401 | case VERIFYING_KEXEC_PE_SIGNATURE: |
| 402 | if (pkcs7->data_type != OID_msIndirectData) { |
| 403 | pr_warn("Invalid kexec sig (not Authenticode)\n"); |
| 404 | return -EKEYREJECTED; |
| 405 | } |
| 406 | /* Authattr presence checked in parser */ |
| 407 | break; |
| 408 | case VERIFYING_UNSPECIFIED_SIGNATURE: |
| 409 | if (pkcs7->data_type != OID_data) { |
| 410 | pr_warn("Invalid unspecified sig (not pkcs7-data)\n"); |
| 411 | return -EKEYREJECTED; |
| 412 | } |
| 413 | break; |
| 414 | default: |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 418 | for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) { |
| 419 | ret = pkcs7_verify_one(pkcs7, sinfo); |
Eric Biggers | 29f4a67 | 2018-02-22 14:38:33 +0000 | [diff] [blame] | 420 | if (sinfo->blacklisted) { |
| 421 | if (actual_ret == -ENOPKG) |
| 422 | actual_ret = -EKEYREJECTED; |
| 423 | continue; |
| 424 | } |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 425 | if (ret < 0) { |
David Howells | 4155942 | 2014-09-16 17:36:15 +0100 | [diff] [blame] | 426 | if (ret == -ENOPKG) { |
| 427 | sinfo->unsupported_crypto = true; |
| 428 | continue; |
| 429 | } |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 430 | kleave(" = %d", ret); |
| 431 | return ret; |
| 432 | } |
David Howells | 03bb793 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 433 | actual_ret = 0; |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 434 | } |
| 435 | |
David Howells | 03bb793 | 2017-04-03 16:07:25 +0100 | [diff] [blame] | 436 | kleave(" = %d", actual_ret); |
| 437 | return actual_ret; |
David Howells | 9f0d331 | 2014-07-01 16:40:19 +0100 | [diff] [blame] | 438 | } |
| 439 | EXPORT_SYMBOL_GPL(pkcs7_verify); |
David Howells | 4ebdb76f | 2015-07-20 21:16:26 +0100 | [diff] [blame] | 440 | |
| 441 | /** |
| 442 | * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message |
| 443 | * @pkcs7: The PKCS#7 message |
| 444 | * @data: The data to be verified |
| 445 | * @datalen: The amount of data |
| 446 | * |
| 447 | * Supply the detached data needed to verify a PKCS#7 message. Note that no |
| 448 | * attempt to retain/pin the data is made. That is left to the caller. The |
| 449 | * data will not be modified by pkcs7_verify() and will not be freed when the |
| 450 | * PKCS#7 message is freed. |
| 451 | * |
| 452 | * Returns -EINVAL if data is already supplied in the message, 0 otherwise. |
| 453 | */ |
| 454 | int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7, |
| 455 | const void *data, size_t datalen) |
| 456 | { |
| 457 | if (pkcs7->data) { |
| 458 | pr_debug("Data already supplied\n"); |
| 459 | return -EINVAL; |
| 460 | } |
| 461 | pkcs7->data = data; |
| 462 | pkcs7->data_len = datalen; |
| 463 | return 0; |
| 464 | } |