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