AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* Verify the signature on a PKCS#7 message. |
| 3 | * |
| 4 | * Imported from crypto/asymmetric_keys/pkcs7_verify.c of linux 5.7 |
| 5 | * with modification marked as __UBOOT__. |
| 6 | * |
| 7 | * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. |
| 8 | * Written by David Howells (dhowells@redhat.com) |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) "PKCS7: "fmt |
| 12 | #ifdef __UBOOT__ |
AKASHI Takahiro | 0cad191 | 2020-07-21 19:35:20 +0900 | [diff] [blame] | 13 | #include <image.h> |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 14 | #include <string.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/compat.h> |
| 17 | #include <linux/asn1.h> |
AKASHI Takahiro | 0cad191 | 2020-07-21 19:35:20 +0900 | [diff] [blame] | 18 | #include <u-boot/rsa-checksum.h> |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 19 | #include <crypto/public_key.h> |
| 20 | #include <crypto/pkcs7_parser.h> |
| 21 | #else |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/export.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/asn1.h> |
| 27 | #include <crypto/hash.h> |
| 28 | #include <crypto/hash_info.h> |
| 29 | #include <crypto/public_key.h> |
| 30 | #include "pkcs7_parser.h" |
| 31 | #endif |
| 32 | |
| 33 | /* |
AKASHI Takahiro | 0cad191 | 2020-07-21 19:35:20 +0900 | [diff] [blame] | 34 | * pkcs7_digest - Digest the relevant parts of the PKCS#7 data |
| 35 | * @pkcs7: PKCS7 Signed Data |
| 36 | * @sinfo: PKCS7 Signed Info |
| 37 | * |
| 38 | * Digest the relevant parts of the PKCS#7 data, @pkcs7, using signature |
| 39 | * information in @sinfo. But if there are authentication attributes, |
| 40 | * i.e. signed image case, the digest must be calculated against |
| 41 | * the authentication attributes. |
| 42 | * |
| 43 | * Return: 0 - on success, non-zero error code - otherwise |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 44 | */ |
| 45 | #ifdef __UBOOT__ |
| 46 | static int pkcs7_digest(struct pkcs7_message *pkcs7, |
| 47 | struct pkcs7_signed_info *sinfo) |
| 48 | { |
AKASHI Takahiro | 0cad191 | 2020-07-21 19:35:20 +0900 | [diff] [blame] | 49 | struct public_key_signature *sig = sinfo->sig; |
| 50 | struct image_region regions[2]; |
| 51 | int ret = 0; |
| 52 | |
| 53 | /* The digest was calculated already. */ |
| 54 | if (sig->digest) |
| 55 | return 0; |
| 56 | |
| 57 | if (!sinfo->sig->hash_algo) |
| 58 | return -ENOPKG; |
| 59 | if (!strcmp(sinfo->sig->hash_algo, "sha256")) |
| 60 | sig->digest_size = SHA256_SUM_LEN; |
| 61 | else if (!strcmp(sinfo->sig->hash_algo, "sha1")) |
| 62 | sig->digest_size = SHA1_SUM_LEN; |
| 63 | else |
| 64 | return -ENOPKG; |
| 65 | |
| 66 | sig->digest = calloc(1, sig->digest_size); |
| 67 | if (!sig->digest) { |
| 68 | pr_warn("Sig %u: Out of memory\n", sinfo->index); |
| 69 | return -ENOMEM; |
| 70 | } |
| 71 | |
| 72 | regions[0].data = pkcs7->data; |
| 73 | regions[0].size = pkcs7->data_len; |
| 74 | |
| 75 | /* Digest the message [RFC2315 9.3] */ |
| 76 | hash_calculate(sinfo->sig->hash_algo, regions, 1, sig->digest); |
| 77 | |
| 78 | /* However, if there are authenticated attributes, there must be a |
| 79 | * message digest attribute amongst them which corresponds to the |
| 80 | * digest we just calculated. |
| 81 | */ |
| 82 | if (sinfo->authattrs) { |
| 83 | u8 tag; |
| 84 | |
| 85 | if (!sinfo->msgdigest) { |
| 86 | pr_warn("Sig %u: No messageDigest\n", sinfo->index); |
| 87 | ret = -EKEYREJECTED; |
| 88 | goto error; |
| 89 | } |
| 90 | |
| 91 | if (sinfo->msgdigest_len != sig->digest_size) { |
| 92 | pr_debug("Sig %u: Invalid digest size (%u)\n", |
| 93 | sinfo->index, sinfo->msgdigest_len); |
| 94 | ret = -EBADMSG; |
| 95 | goto error; |
| 96 | } |
| 97 | |
| 98 | if (memcmp(sig->digest, sinfo->msgdigest, |
| 99 | sinfo->msgdigest_len) != 0) { |
| 100 | pr_debug("Sig %u: Message digest doesn't match\n", |
| 101 | sinfo->index); |
| 102 | ret = -EKEYREJECTED; |
| 103 | goto error; |
| 104 | } |
| 105 | |
| 106 | /* We then calculate anew, using the authenticated attributes |
| 107 | * as the contents of the digest instead. Note that we need to |
| 108 | * convert the attributes from a CONT.0 into a SET before we |
| 109 | * hash it. |
| 110 | */ |
| 111 | memset(sig->digest, 0, sig->digest_size); |
| 112 | |
| 113 | tag = 0x31; |
| 114 | regions[0].data = &tag; |
| 115 | regions[0].size = 1; |
| 116 | regions[1].data = sinfo->authattrs; |
| 117 | regions[1].size = sinfo->authattrs_len; |
| 118 | |
| 119 | hash_calculate(sinfo->sig->hash_algo, regions, 2, sig->digest); |
| 120 | |
| 121 | ret = 0; |
| 122 | } |
| 123 | error: |
| 124 | return ret; |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 125 | } |
AKASHI Takahiro | 0cad191 | 2020-07-21 19:35:20 +0900 | [diff] [blame] | 126 | #else /* !__UBOOT__ */ |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 127 | static int pkcs7_digest(struct pkcs7_message *pkcs7, |
| 128 | struct pkcs7_signed_info *sinfo) |
| 129 | { |
| 130 | struct public_key_signature *sig = sinfo->sig; |
| 131 | struct crypto_shash *tfm; |
| 132 | struct shash_desc *desc; |
| 133 | size_t desc_size; |
| 134 | int ret; |
| 135 | |
| 136 | kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo); |
| 137 | |
| 138 | /* The digest was calculated already. */ |
| 139 | if (sig->digest) |
| 140 | return 0; |
| 141 | |
| 142 | if (!sinfo->sig->hash_algo) |
| 143 | return -ENOPKG; |
| 144 | |
| 145 | /* Allocate the hashing algorithm we're going to need and find out how |
| 146 | * big the hash operational data will be. |
| 147 | */ |
| 148 | tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0); |
| 149 | if (IS_ERR(tfm)) |
| 150 | return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm); |
| 151 | |
| 152 | desc_size = crypto_shash_descsize(tfm) + sizeof(*desc); |
| 153 | sig->digest_size = crypto_shash_digestsize(tfm); |
| 154 | |
| 155 | ret = -ENOMEM; |
| 156 | sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); |
| 157 | if (!sig->digest) |
| 158 | goto error_no_desc; |
| 159 | |
| 160 | desc = kzalloc(desc_size, GFP_KERNEL); |
| 161 | if (!desc) |
| 162 | goto error_no_desc; |
| 163 | |
| 164 | desc->tfm = tfm; |
| 165 | |
| 166 | /* Digest the message [RFC2315 9.3] */ |
| 167 | ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len, |
| 168 | sig->digest); |
| 169 | if (ret < 0) |
| 170 | goto error; |
| 171 | pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest); |
| 172 | |
| 173 | /* However, if there are authenticated attributes, there must be a |
| 174 | * message digest attribute amongst them which corresponds to the |
| 175 | * digest we just calculated. |
| 176 | */ |
| 177 | if (sinfo->authattrs) { |
| 178 | u8 tag; |
| 179 | |
| 180 | if (!sinfo->msgdigest) { |
| 181 | pr_warn("Sig %u: No messageDigest\n", sinfo->index); |
| 182 | ret = -EKEYREJECTED; |
| 183 | goto error; |
| 184 | } |
| 185 | |
| 186 | if (sinfo->msgdigest_len != sig->digest_size) { |
| 187 | pr_debug("Sig %u: Invalid digest size (%u)\n", |
| 188 | sinfo->index, sinfo->msgdigest_len); |
| 189 | ret = -EBADMSG; |
| 190 | goto error; |
| 191 | } |
| 192 | |
| 193 | if (memcmp(sig->digest, sinfo->msgdigest, |
| 194 | sinfo->msgdigest_len) != 0) { |
| 195 | pr_debug("Sig %u: Message digest doesn't match\n", |
| 196 | sinfo->index); |
| 197 | ret = -EKEYREJECTED; |
| 198 | goto error; |
| 199 | } |
| 200 | |
| 201 | /* We then calculate anew, using the authenticated attributes |
| 202 | * as the contents of the digest instead. Note that we need to |
| 203 | * convert the attributes from a CONT.0 into a SET before we |
| 204 | * hash it. |
| 205 | */ |
| 206 | memset(sig->digest, 0, sig->digest_size); |
| 207 | |
| 208 | ret = crypto_shash_init(desc); |
| 209 | if (ret < 0) |
| 210 | goto error; |
| 211 | tag = ASN1_CONS_BIT | ASN1_SET; |
| 212 | ret = crypto_shash_update(desc, &tag, 1); |
| 213 | if (ret < 0) |
| 214 | goto error; |
| 215 | ret = crypto_shash_finup(desc, sinfo->authattrs, |
| 216 | sinfo->authattrs_len, sig->digest); |
| 217 | if (ret < 0) |
| 218 | goto error; |
| 219 | pr_devel("AADigest = [%*ph]\n", 8, sig->digest); |
| 220 | } |
| 221 | |
| 222 | error: |
| 223 | kfree(desc); |
| 224 | error_no_desc: |
| 225 | crypto_free_shash(tfm); |
| 226 | kleave(" = %d", ret); |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len, |
| 231 | enum hash_algo *hash_algo) |
| 232 | { |
| 233 | struct pkcs7_signed_info *sinfo = pkcs7->signed_infos; |
| 234 | int i, ret; |
| 235 | |
| 236 | /* |
| 237 | * This function doesn't support messages with more than one signature. |
| 238 | */ |
| 239 | if (sinfo == NULL || sinfo->next != NULL) |
| 240 | return -EBADMSG; |
| 241 | |
| 242 | ret = pkcs7_digest(pkcs7, sinfo); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | |
| 246 | *buf = sinfo->sig->digest; |
| 247 | *len = sinfo->sig->digest_size; |
| 248 | |
| 249 | for (i = 0; i < HASH_ALGO__LAST; i++) |
| 250 | if (!strcmp(hash_algo_name[i], sinfo->sig->hash_algo)) { |
| 251 | *hash_algo = i; |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | #endif /* !__UBOOT__ */ |
| 258 | |
| 259 | /* |
| 260 | * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7 |
| 261 | * uses the issuer's name and the issuing certificate serial number for |
| 262 | * matching purposes. These must match the certificate issuer's name (not |
| 263 | * subject's name) and the certificate serial number [RFC 2315 6.7]. |
| 264 | */ |
| 265 | static int pkcs7_find_key(struct pkcs7_message *pkcs7, |
| 266 | struct pkcs7_signed_info *sinfo) |
| 267 | { |
| 268 | struct x509_certificate *x509; |
| 269 | unsigned certix = 1; |
| 270 | |
| 271 | kenter("%u", sinfo->index); |
| 272 | |
| 273 | for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) { |
| 274 | /* I'm _assuming_ that the generator of the PKCS#7 message will |
| 275 | * encode the fields from the X.509 cert in the same way in the |
| 276 | * PKCS#7 message - but I can't be 100% sure of that. It's |
| 277 | * possible this will need element-by-element comparison. |
| 278 | */ |
| 279 | if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0])) |
| 280 | continue; |
| 281 | pr_devel("Sig %u: Found cert serial match X.509[%u]\n", |
| 282 | sinfo->index, certix); |
| 283 | |
| 284 | if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) { |
| 285 | pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n", |
| 286 | sinfo->index); |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | sinfo->signer = x509; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* The relevant X.509 cert isn't found here, but it might be found in |
| 295 | * the trust keyring. |
| 296 | */ |
| 297 | pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n", |
| 298 | sinfo->index, |
| 299 | sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | /* |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 304 | * pkcs7_verify_sig_chain - Verify the internal certificate chain as best |
| 305 | * as we can. |
| 306 | * @pkcs7: PKCS7 Signed Data |
| 307 | * @sinfo: PKCS7 Signed Info |
| 308 | * @signer: Singer's certificate |
| 309 | * |
| 310 | * Build up and verify the internal certificate chain against a signature |
| 311 | * in @sinfo, using certificates contained in @pkcs7 as best as we can. |
| 312 | * If the chain reaches the end, the last certificate will be returned |
| 313 | * in @signer. |
| 314 | * |
| 315 | * Return: 0 - on success, non-zero error code - otherwise |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 316 | */ |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 317 | #ifdef __UBOOT__ |
| 318 | static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, |
| 319 | struct pkcs7_signed_info *sinfo, |
| 320 | struct x509_certificate **signer) |
| 321 | #else |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 322 | static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, |
| 323 | struct pkcs7_signed_info *sinfo) |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 324 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 325 | { |
| 326 | struct public_key_signature *sig; |
| 327 | struct x509_certificate *x509 = sinfo->signer, *p; |
| 328 | struct asymmetric_key_id *auth; |
| 329 | int ret; |
| 330 | |
| 331 | kenter(""); |
| 332 | |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 333 | *signer = NULL; |
| 334 | |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 335 | for (p = pkcs7->certs; p; p = p->next) |
| 336 | p->seen = false; |
| 337 | |
| 338 | for (;;) { |
| 339 | pr_debug("verify %s: %*phN\n", |
| 340 | x509->subject, |
| 341 | x509->raw_serial_size, x509->raw_serial); |
| 342 | x509->seen = true; |
| 343 | |
| 344 | if (x509->blacklisted) { |
| 345 | /* If this cert is blacklisted, then mark everything |
| 346 | * that depends on this as blacklisted too. |
| 347 | */ |
| 348 | sinfo->blacklisted = true; |
| 349 | for (p = sinfo->signer; p != x509; p = p->signer) |
| 350 | p->blacklisted = true; |
| 351 | pr_debug("- blacklisted\n"); |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 352 | #ifdef __UBOOT__ |
| 353 | *signer = x509; |
| 354 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | if (x509->unsupported_key) |
| 359 | goto unsupported_crypto_in_x509; |
| 360 | |
| 361 | pr_debug("- issuer %s\n", x509->issuer); |
| 362 | sig = x509->sig; |
| 363 | if (sig->auth_ids[0]) |
| 364 | pr_debug("- authkeyid.id %*phN\n", |
| 365 | sig->auth_ids[0]->len, sig->auth_ids[0]->data); |
| 366 | if (sig->auth_ids[1]) |
| 367 | pr_debug("- authkeyid.skid %*phN\n", |
| 368 | sig->auth_ids[1]->len, sig->auth_ids[1]->data); |
| 369 | |
| 370 | if (x509->self_signed) { |
| 371 | /* If there's no authority certificate specified, then |
| 372 | * the certificate must be self-signed and is the root |
| 373 | * of the chain. Likewise if the cert is its own |
| 374 | * authority. |
| 375 | */ |
| 376 | if (x509->unsupported_sig) |
| 377 | goto unsupported_crypto_in_x509; |
| 378 | x509->signer = x509; |
| 379 | pr_debug("- self-signed\n"); |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 380 | #ifdef __UBOOT__ |
| 381 | *signer = x509; |
| 382 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /* Look through the X.509 certificates in the PKCS#7 message's |
| 387 | * list to see if the next one is there. |
| 388 | */ |
| 389 | auth = sig->auth_ids[0]; |
| 390 | if (auth) { |
| 391 | pr_debug("- want %*phN\n", auth->len, auth->data); |
| 392 | for (p = pkcs7->certs; p; p = p->next) { |
| 393 | pr_debug("- cmp [%u] %*phN\n", |
| 394 | p->index, p->id->len, p->id->data); |
| 395 | if (asymmetric_key_id_same(p->id, auth)) |
| 396 | goto found_issuer_check_skid; |
| 397 | } |
| 398 | } else if (sig->auth_ids[1]) { |
| 399 | auth = sig->auth_ids[1]; |
| 400 | pr_debug("- want %*phN\n", auth->len, auth->data); |
| 401 | for (p = pkcs7->certs; p; p = p->next) { |
| 402 | if (!p->skid) |
| 403 | continue; |
| 404 | pr_debug("- cmp [%u] %*phN\n", |
| 405 | p->index, p->skid->len, p->skid->data); |
| 406 | if (asymmetric_key_id_same(p->skid, auth)) |
| 407 | goto found_issuer; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* We didn't find the root of this chain */ |
| 412 | pr_debug("- top\n"); |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 413 | #ifdef __UBOOT__ |
| 414 | *signer = x509; |
| 415 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 416 | return 0; |
| 417 | |
| 418 | found_issuer_check_skid: |
| 419 | /* We matched issuer + serialNumber, but if there's an |
| 420 | * authKeyId.keyId, that must match the CA subjKeyId also. |
| 421 | */ |
| 422 | if (sig->auth_ids[1] && |
| 423 | !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) { |
| 424 | pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n", |
| 425 | sinfo->index, x509->index, p->index); |
| 426 | return -EKEYREJECTED; |
| 427 | } |
| 428 | found_issuer: |
| 429 | pr_debug("- subject %s\n", p->subject); |
| 430 | if (p->seen) { |
| 431 | pr_warn("Sig %u: X.509 chain contains loop\n", |
| 432 | sinfo->index); |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 433 | #ifdef __UBOOT__ |
| 434 | *signer = p; |
| 435 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | ret = public_key_verify_signature(p->pub, x509->sig); |
| 439 | if (ret < 0) |
| 440 | return ret; |
| 441 | x509->signer = p; |
| 442 | if (x509 == p) { |
| 443 | pr_debug("- self-signed\n"); |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 444 | #ifdef __UBOOT__ |
| 445 | *signer = p; |
| 446 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 447 | return 0; |
| 448 | } |
| 449 | x509 = p; |
| 450 | #ifndef __UBOOT__ |
| 451 | might_sleep(); |
| 452 | #endif |
| 453 | } |
| 454 | |
| 455 | unsupported_crypto_in_x509: |
| 456 | /* Just prune the certificate chain at this point if we lack some |
| 457 | * crypto module to go further. Note, however, we don't want to set |
| 458 | * sinfo->unsupported_crypto as the signed info block may still be |
| 459 | * validatable against an X.509 cert lower in the chain that we have a |
| 460 | * trusted copy of. |
| 461 | */ |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | /* |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 466 | * pkcs7_verify_one - Verify one signed information block from a PKCS#7 |
| 467 | * message. |
| 468 | * @pkcs7: PKCS7 Signed Data |
| 469 | * @sinfo: PKCS7 Signed Info |
| 470 | * @signer: Signer's certificate |
| 471 | * |
| 472 | * Verify one signature in @sinfo and follow the certificate chain. |
| 473 | * If the chain reaches the end, the last certificate will be returned |
| 474 | * in @signer. |
| 475 | * |
| 476 | * Return: 0 - on success, non-zero error code - otherwise |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 477 | */ |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 478 | #ifdef __UBOOT__ |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 479 | int pkcs7_verify_one(struct pkcs7_message *pkcs7, |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 480 | struct pkcs7_signed_info *sinfo, |
| 481 | struct x509_certificate **signer) |
| 482 | #else |
| 483 | static int pkcs7_verify_one(struct pkcs7_message *pkcs7, |
| 484 | struct pkcs7_signed_info *sinfo) |
| 485 | #endif |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 486 | { |
| 487 | int ret; |
| 488 | |
| 489 | kenter(",%u", sinfo->index); |
| 490 | |
| 491 | /* First of all, digest the data in the PKCS#7 message and the |
| 492 | * signed information block |
| 493 | */ |
| 494 | ret = pkcs7_digest(pkcs7, sinfo); |
| 495 | if (ret < 0) |
| 496 | return ret; |
| 497 | |
| 498 | /* Find the key for the signature if there is one */ |
| 499 | ret = pkcs7_find_key(pkcs7, sinfo); |
| 500 | if (ret < 0) |
| 501 | return ret; |
| 502 | |
| 503 | if (!sinfo->signer) |
| 504 | return 0; |
| 505 | |
| 506 | pr_devel("Using X.509[%u] for sig %u\n", |
| 507 | sinfo->signer->index, sinfo->index); |
| 508 | |
| 509 | /* Check that the PKCS#7 signing time is valid according to the X.509 |
| 510 | * certificate. We can't, however, check against the system clock |
| 511 | * since that may not have been set yet and may be wrong. |
| 512 | */ |
| 513 | if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) { |
| 514 | if (sinfo->signing_time < sinfo->signer->valid_from || |
| 515 | sinfo->signing_time > sinfo->signer->valid_to) { |
| 516 | pr_warn("Message signed outside of X.509 validity window\n"); |
| 517 | return -EKEYREJECTED; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* Verify the PKCS#7 binary against the key */ |
| 522 | ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig); |
| 523 | if (ret < 0) |
| 524 | return ret; |
| 525 | |
| 526 | pr_devel("Verified signature %u\n", sinfo->index); |
| 527 | |
| 528 | /* Verify the internal certificate chain */ |
AKASHI Takahiro | f2d87ff | 2020-07-21 19:35:21 +0900 | [diff] [blame] | 529 | return pkcs7_verify_sig_chain(pkcs7, sinfo, signer); |
AKASHI Takahiro | ab837f2 | 2020-07-21 19:35:19 +0900 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | #ifndef __UBOOT__ |
| 533 | /** |
| 534 | * pkcs7_verify - Verify a PKCS#7 message |
| 535 | * @pkcs7: The PKCS#7 message to be verified |
| 536 | * @usage: The use to which the key is being put |
| 537 | * |
| 538 | * Verify a PKCS#7 message is internally consistent - that is, the data digest |
| 539 | * matches the digest in the AuthAttrs and any signature in the message or one |
| 540 | * of the X.509 certificates it carries that matches another X.509 cert in the |
| 541 | * message can be verified. |
| 542 | * |
| 543 | * This does not look to match the contents of the PKCS#7 message against any |
| 544 | * external public keys. |
| 545 | * |
| 546 | * Returns, in order of descending priority: |
| 547 | * |
| 548 | * (*) -EKEYREJECTED if a key was selected that had a usage restriction at |
| 549 | * odds with the specified usage, or: |
| 550 | * |
| 551 | * (*) -EKEYREJECTED if a signature failed to match for which we found an |
| 552 | * appropriate X.509 certificate, or: |
| 553 | * |
| 554 | * (*) -EBADMSG if some part of the message was invalid, or: |
| 555 | * |
| 556 | * (*) 0 if a signature chain passed verification, or: |
| 557 | * |
| 558 | * (*) -EKEYREJECTED if a blacklisted key was encountered, or: |
| 559 | * |
| 560 | * (*) -ENOPKG if none of the signature chains are verifiable because suitable |
| 561 | * crypto modules couldn't be found. |
| 562 | */ |
| 563 | int pkcs7_verify(struct pkcs7_message *pkcs7, |
| 564 | enum key_being_used_for usage) |
| 565 | { |
| 566 | struct pkcs7_signed_info *sinfo; |
| 567 | int actual_ret = -ENOPKG; |
| 568 | int ret; |
| 569 | |
| 570 | kenter(""); |
| 571 | |
| 572 | switch (usage) { |
| 573 | case VERIFYING_MODULE_SIGNATURE: |
| 574 | if (pkcs7->data_type != OID_data) { |
| 575 | pr_warn("Invalid module sig (not pkcs7-data)\n"); |
| 576 | return -EKEYREJECTED; |
| 577 | } |
| 578 | if (pkcs7->have_authattrs) { |
| 579 | pr_warn("Invalid module sig (has authattrs)\n"); |
| 580 | return -EKEYREJECTED; |
| 581 | } |
| 582 | break; |
| 583 | case VERIFYING_FIRMWARE_SIGNATURE: |
| 584 | if (pkcs7->data_type != OID_data) { |
| 585 | pr_warn("Invalid firmware sig (not pkcs7-data)\n"); |
| 586 | return -EKEYREJECTED; |
| 587 | } |
| 588 | if (!pkcs7->have_authattrs) { |
| 589 | pr_warn("Invalid firmware sig (missing authattrs)\n"); |
| 590 | return -EKEYREJECTED; |
| 591 | } |
| 592 | break; |
| 593 | case VERIFYING_KEXEC_PE_SIGNATURE: |
| 594 | if (pkcs7->data_type != OID_msIndirectData) { |
| 595 | pr_warn("Invalid kexec sig (not Authenticode)\n"); |
| 596 | return -EKEYREJECTED; |
| 597 | } |
| 598 | /* Authattr presence checked in parser */ |
| 599 | break; |
| 600 | case VERIFYING_UNSPECIFIED_SIGNATURE: |
| 601 | if (pkcs7->data_type != OID_data) { |
| 602 | pr_warn("Invalid unspecified sig (not pkcs7-data)\n"); |
| 603 | return -EKEYREJECTED; |
| 604 | } |
| 605 | break; |
| 606 | default: |
| 607 | return -EINVAL; |
| 608 | } |
| 609 | |
| 610 | for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) { |
| 611 | ret = pkcs7_verify_one(pkcs7, sinfo); |
| 612 | if (sinfo->blacklisted) { |
| 613 | if (actual_ret == -ENOPKG) |
| 614 | actual_ret = -EKEYREJECTED; |
| 615 | continue; |
| 616 | } |
| 617 | if (ret < 0) { |
| 618 | if (ret == -ENOPKG) { |
| 619 | sinfo->unsupported_crypto = true; |
| 620 | continue; |
| 621 | } |
| 622 | kleave(" = %d", ret); |
| 623 | return ret; |
| 624 | } |
| 625 | actual_ret = 0; |
| 626 | } |
| 627 | |
| 628 | kleave(" = %d", actual_ret); |
| 629 | return actual_ret; |
| 630 | } |
| 631 | EXPORT_SYMBOL_GPL(pkcs7_verify); |
| 632 | |
| 633 | /** |
| 634 | * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message |
| 635 | * @pkcs7: The PKCS#7 message |
| 636 | * @data: The data to be verified |
| 637 | * @datalen: The amount of data |
| 638 | * |
| 639 | * Supply the detached data needed to verify a PKCS#7 message. Note that no |
| 640 | * attempt to retain/pin the data is made. That is left to the caller. The |
| 641 | * data will not be modified by pkcs7_verify() and will not be freed when the |
| 642 | * PKCS#7 message is freed. |
| 643 | * |
| 644 | * Returns -EINVAL if data is already supplied in the message, 0 otherwise. |
| 645 | */ |
| 646 | int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7, |
| 647 | const void *data, size_t datalen) |
| 648 | { |
| 649 | if (pkcs7->data) { |
| 650 | pr_debug("Data already supplied\n"); |
| 651 | return -EINVAL; |
| 652 | } |
| 653 | pkcs7->data = data; |
| 654 | pkcs7->data_len = datalen; |
| 655 | return 0; |
| 656 | } |
| 657 | #endif /* __UBOOT__ */ |