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