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> 19 #include <crypto/public_key.h> 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 public_key_signature *sig = sinfo->sig; 29 struct crypto_shash *tfm; 30 struct shash_desc *desc; 31 size_t desc_size; 32 int ret; 33 34 kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo); 35 36 if (!sinfo->sig->hash_algo) 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 */ 42 tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0); 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 sig->digest_size = crypto_shash_digestsize(tfm); 48 49 ret = -ENOMEM; 50 sig->digest = kmalloc(sig->digest_size, GFP_KERNEL); 51 if (!sig->digest) 52 goto error_no_desc; 53 54 desc = kzalloc(desc_size, GFP_KERNEL); 55 if (!desc) 56 goto error_no_desc; 57 58 desc->tfm = tfm; 59 60 /* Digest the message [RFC2315 9.3] */ 61 ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len, 62 sig->digest); 63 if (ret < 0) 64 goto error; 65 pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest); 66 67 /* However, if there are authenticated attributes, there must be a 68 * message digest attribute amongst them which corresponds to the 69 * digest we just calculated. 70 */ 71 if (sinfo->authattrs) { 72 u8 tag; 73 74 if (!sinfo->msgdigest) { 75 pr_warn("Sig %u: No messageDigest\n", sinfo->index); 76 ret = -EKEYREJECTED; 77 goto error; 78 } 79 80 if (sinfo->msgdigest_len != sig->digest_size) { 81 pr_debug("Sig %u: Invalid digest size (%u)\n", 82 sinfo->index, sinfo->msgdigest_len); 83 ret = -EBADMSG; 84 goto error; 85 } 86 87 if (memcmp(sig->digest, sinfo->msgdigest, 88 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(sig->digest, 0, 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, sig->digest); 111 if (ret < 0) 112 goto error; 113 pr_devel("AADigest = [%*ph]\n", 8, sig->digest); 114 } 115 116 error: 117 kfree(desc); 118 error_no_desc: 119 crypto_free_shash(tfm); 120 kleave(" = %d", ret); 121 return ret; 122 } 123 124 /* 125 * Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7 126 * uses the issuer's name and the issuing certificate serial number for 127 * matching purposes. These must match the certificate issuer's name (not 128 * subject's name) and the certificate serial number [RFC 2315 6.7]. 129 */ 130 static int pkcs7_find_key(struct pkcs7_message *pkcs7, 131 struct pkcs7_signed_info *sinfo) 132 { 133 struct x509_certificate *x509; 134 unsigned certix = 1; 135 136 kenter("%u", sinfo->index); 137 138 for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) { 139 /* I'm _assuming_ that the generator of the PKCS#7 message will 140 * encode the fields from the X.509 cert in the same way in the 141 * PKCS#7 message - but I can't be 100% sure of that. It's 142 * possible this will need element-by-element comparison. 143 */ 144 if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0])) 145 continue; 146 pr_devel("Sig %u: Found cert serial match X.509[%u]\n", 147 sinfo->index, certix); 148 149 if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) { 150 pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n", 151 sinfo->index); 152 continue; 153 } 154 155 sinfo->signer = x509; 156 return 0; 157 } 158 159 /* The relevant X.509 cert isn't found here, but it might be found in 160 * the trust keyring. 161 */ 162 pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n", 163 sinfo->index, 164 sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data); 165 return 0; 166 } 167 168 /* 169 * Verify the internal certificate chain as best we can. 170 */ 171 static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7, 172 struct pkcs7_signed_info *sinfo) 173 { 174 struct public_key_signature *sig; 175 struct x509_certificate *x509 = sinfo->signer, *p; 176 struct asymmetric_key_id *auth; 177 int ret; 178 179 kenter(""); 180 181 for (p = pkcs7->certs; p; p = p->next) 182 p->seen = false; 183 184 for (;;) { 185 pr_debug("verify %s: %*phN\n", 186 x509->subject, 187 x509->raw_serial_size, x509->raw_serial); 188 x509->seen = true; 189 190 if (x509->blacklisted) { 191 /* If this cert is blacklisted, then mark everything 192 * that depends on this as blacklisted too. 193 */ 194 sinfo->blacklisted = true; 195 for (p = sinfo->signer; p != x509; p = p->signer) 196 p->blacklisted = true; 197 pr_debug("- blacklisted\n"); 198 return 0; 199 } 200 201 if (x509->unsupported_key) 202 goto unsupported_crypto_in_x509; 203 204 pr_debug("- issuer %s\n", x509->issuer); 205 sig = x509->sig; 206 if (sig->auth_ids[0]) 207 pr_debug("- authkeyid.id %*phN\n", 208 sig->auth_ids[0]->len, sig->auth_ids[0]->data); 209 if (sig->auth_ids[1]) 210 pr_debug("- authkeyid.skid %*phN\n", 211 sig->auth_ids[1]->len, sig->auth_ids[1]->data); 212 213 if (x509->self_signed) { 214 /* If there's no authority certificate specified, then 215 * the certificate must be self-signed and is the root 216 * of the chain. Likewise if the cert is its own 217 * authority. 218 */ 219 if (x509->unsupported_sig) 220 goto unsupported_crypto_in_x509; 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 */ 229 auth = sig->auth_ids[0]; 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 if (sig->auth_ids[1]) { 239 auth = sig->auth_ids[1]; 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 } 249 } 250 251 /* We didn't find the root of this chain */ 252 pr_debug("- top\n"); 253 return 0; 254 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 */ 259 if (sig->auth_ids[1] && 260 !asymmetric_key_id_same(p->skid, sig->auth_ids[1])) { 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 } 265 found_issuer: 266 pr_debug("- subject %s\n", p->subject); 267 if (p->seen) { 268 pr_warn("Sig %u: X.509 chain contains loop\n", 269 sinfo->index); 270 return 0; 271 } 272 ret = public_key_verify_signature(p->pub, x509->sig); 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 } 283 284 unsupported_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->unsupported_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 return 0; 292 } 293 294 /* 295 * Verify one signed information block from a PKCS#7 message. 296 */ 297 static int pkcs7_verify_one(struct pkcs7_message *pkcs7, 298 struct pkcs7_signed_info *sinfo) 299 { 300 int ret; 301 302 kenter(",%u", sinfo->index); 303 304 /* First of all, digest the data in the PKCS#7 message and the 305 * signed information block 306 */ 307 ret = pkcs7_digest(pkcs7, sinfo); 308 if (ret < 0) 309 return ret; 310 311 /* Find the key for the signature if there is one */ 312 ret = pkcs7_find_key(pkcs7, sinfo); 313 if (ret < 0) 314 return ret; 315 316 if (!sinfo->signer) 317 return 0; 318 319 pr_devel("Using X.509[%u] for sig %u\n", 320 sinfo->signer->index, sinfo->index); 321 322 /* Check that the PKCS#7 signing time is valid according to the X.509 323 * certificate. We can't, however, check against the system clock 324 * since that may not have been set yet and may be wrong. 325 */ 326 if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) { 327 if (sinfo->signing_time < sinfo->signer->valid_from || 328 sinfo->signing_time > sinfo->signer->valid_to) { 329 pr_warn("Message signed outside of X.509 validity window\n"); 330 return -EKEYREJECTED; 331 } 332 } 333 334 /* Verify the PKCS#7 binary against the key */ 335 ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig); 336 if (ret < 0) 337 return ret; 338 339 pr_devel("Verified signature %u\n", sinfo->index); 340 341 /* Verify the internal certificate chain */ 342 return pkcs7_verify_sig_chain(pkcs7, sinfo); 343 } 344 345 /** 346 * pkcs7_verify - Verify a PKCS#7 message 347 * @pkcs7: The PKCS#7 message to be verified 348 * @usage: The use to which the key is being put 349 * 350 * Verify a PKCS#7 message is internally consistent - that is, the data digest 351 * matches the digest in the AuthAttrs and any signature in the message or one 352 * of the X.509 certificates it carries that matches another X.509 cert in the 353 * message can be verified. 354 * 355 * This does not look to match the contents of the PKCS#7 message against any 356 * external public keys. 357 * 358 * Returns, in order of descending priority: 359 * 360 * (*) -EKEYREJECTED if a key was selected that had a usage restriction at 361 * odds with the specified usage, or: 362 * 363 * (*) -EKEYREJECTED if a signature failed to match for which we found an 364 * appropriate X.509 certificate, or: 365 * 366 * (*) -EBADMSG if some part of the message was invalid, or: 367 * 368 * (*) 0 if a signature chain passed verification, or: 369 * 370 * (*) -EKEYREJECTED if a blacklisted key was encountered, or: 371 * 372 * (*) -ENOPKG if none of the signature chains are verifiable because suitable 373 * crypto modules couldn't be found. 374 */ 375 int pkcs7_verify(struct pkcs7_message *pkcs7, 376 enum key_being_used_for usage) 377 { 378 struct pkcs7_signed_info *sinfo; 379 int actual_ret = -ENOPKG; 380 int ret; 381 382 kenter(""); 383 384 switch (usage) { 385 case VERIFYING_MODULE_SIGNATURE: 386 if (pkcs7->data_type != OID_data) { 387 pr_warn("Invalid module sig (not pkcs7-data)\n"); 388 return -EKEYREJECTED; 389 } 390 if (pkcs7->have_authattrs) { 391 pr_warn("Invalid module sig (has authattrs)\n"); 392 return -EKEYREJECTED; 393 } 394 break; 395 case VERIFYING_FIRMWARE_SIGNATURE: 396 if (pkcs7->data_type != OID_data) { 397 pr_warn("Invalid firmware sig (not pkcs7-data)\n"); 398 return -EKEYREJECTED; 399 } 400 if (!pkcs7->have_authattrs) { 401 pr_warn("Invalid firmware sig (missing authattrs)\n"); 402 return -EKEYREJECTED; 403 } 404 break; 405 case VERIFYING_KEXEC_PE_SIGNATURE: 406 if (pkcs7->data_type != OID_msIndirectData) { 407 pr_warn("Invalid kexec sig (not Authenticode)\n"); 408 return -EKEYREJECTED; 409 } 410 /* Authattr presence checked in parser */ 411 break; 412 case VERIFYING_UNSPECIFIED_SIGNATURE: 413 if (pkcs7->data_type != OID_data) { 414 pr_warn("Invalid unspecified sig (not pkcs7-data)\n"); 415 return -EKEYREJECTED; 416 } 417 break; 418 default: 419 return -EINVAL; 420 } 421 422 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) { 423 ret = pkcs7_verify_one(pkcs7, sinfo); 424 if (sinfo->blacklisted) { 425 if (actual_ret == -ENOPKG) 426 actual_ret = -EKEYREJECTED; 427 continue; 428 } 429 if (ret < 0) { 430 if (ret == -ENOPKG) { 431 sinfo->unsupported_crypto = true; 432 continue; 433 } 434 kleave(" = %d", ret); 435 return ret; 436 } 437 actual_ret = 0; 438 } 439 440 kleave(" = %d", actual_ret); 441 return actual_ret; 442 } 443 EXPORT_SYMBOL_GPL(pkcs7_verify); 444 445 /** 446 * pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message 447 * @pkcs7: The PKCS#7 message 448 * @data: The data to be verified 449 * @datalen: The amount of data 450 * 451 * Supply the detached data needed to verify a PKCS#7 message. Note that no 452 * attempt to retain/pin the data is made. That is left to the caller. The 453 * data will not be modified by pkcs7_verify() and will not be freed when the 454 * PKCS#7 message is freed. 455 * 456 * Returns -EINVAL if data is already supplied in the message, 0 otherwise. 457 */ 458 int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7, 459 const void *data, size_t datalen) 460 { 461 if (pkcs7->data) { 462 pr_debug("Data already supplied\n"); 463 return -EINVAL; 464 } 465 pkcs7->data = data; 466 pkcs7->data_len = datalen; 467 return 0; 468 } 469