1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* PKCS#7 parser 3 * 4 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #define pr_fmt(fmt) "PKCS7: "fmt 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/export.h> 12 #include <linux/slab.h> 13 #include <linux/err.h> 14 #include <linux/oid_registry.h> 15 #include <crypto/public_key.h> 16 #include "pkcs7_parser.h" 17 #include "pkcs7.asn1.h" 18 19 MODULE_DESCRIPTION("PKCS#7 parser"); 20 MODULE_AUTHOR("Red Hat, Inc."); 21 MODULE_LICENSE("GPL"); 22 23 struct pkcs7_parse_context { 24 struct pkcs7_message *msg; /* Message being constructed */ 25 struct pkcs7_signed_info *sinfo; /* SignedInfo being constructed */ 26 struct pkcs7_signed_info **ppsinfo; 27 struct x509_certificate *certs; /* Certificate cache */ 28 struct x509_certificate **ppcerts; 29 unsigned long data; /* Start of data */ 30 enum OID last_oid; /* Last OID encountered */ 31 unsigned x509_index; 32 unsigned sinfo_index; 33 const void *raw_serial; 34 unsigned raw_serial_size; 35 unsigned raw_issuer_size; 36 const void *raw_issuer; 37 const void *raw_skid; 38 unsigned raw_skid_size; 39 bool expect_skid; 40 }; 41 42 /* 43 * Free a signed information block. 44 */ 45 static void pkcs7_free_signed_info(struct pkcs7_signed_info *sinfo) 46 { 47 if (sinfo) { 48 public_key_signature_free(sinfo->sig); 49 kfree(sinfo); 50 } 51 } 52 53 /** 54 * pkcs7_free_message - Free a PKCS#7 message 55 * @pkcs7: The PKCS#7 message to free 56 */ 57 void pkcs7_free_message(struct pkcs7_message *pkcs7) 58 { 59 struct x509_certificate *cert; 60 struct pkcs7_signed_info *sinfo; 61 62 if (pkcs7) { 63 while (pkcs7->certs) { 64 cert = pkcs7->certs; 65 pkcs7->certs = cert->next; 66 x509_free_certificate(cert); 67 } 68 while (pkcs7->crl) { 69 cert = pkcs7->crl; 70 pkcs7->crl = cert->next; 71 x509_free_certificate(cert); 72 } 73 while (pkcs7->signed_infos) { 74 sinfo = pkcs7->signed_infos; 75 pkcs7->signed_infos = sinfo->next; 76 pkcs7_free_signed_info(sinfo); 77 } 78 kfree(pkcs7); 79 } 80 } 81 EXPORT_SYMBOL_GPL(pkcs7_free_message); 82 83 /* 84 * Check authenticatedAttributes are provided or not provided consistently. 85 */ 86 static int pkcs7_check_authattrs(struct pkcs7_message *msg) 87 { 88 struct pkcs7_signed_info *sinfo; 89 bool want = false; 90 91 sinfo = msg->signed_infos; 92 if (!sinfo) 93 goto inconsistent; 94 95 if (sinfo->authattrs) { 96 want = true; 97 msg->have_authattrs = true; 98 } 99 100 for (sinfo = sinfo->next; sinfo; sinfo = sinfo->next) 101 if (!!sinfo->authattrs != want) 102 goto inconsistent; 103 return 0; 104 105 inconsistent: 106 pr_warn("Inconsistently supplied authAttrs\n"); 107 return -EINVAL; 108 } 109 110 /** 111 * pkcs7_parse_message - Parse a PKCS#7 message 112 * @data: The raw binary ASN.1 encoded message to be parsed 113 * @datalen: The size of the encoded message 114 */ 115 struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen) 116 { 117 struct pkcs7_parse_context *ctx; 118 struct pkcs7_message *msg = ERR_PTR(-ENOMEM); 119 int ret; 120 121 ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL); 122 if (!ctx) 123 goto out_no_ctx; 124 ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL); 125 if (!ctx->msg) 126 goto out_no_msg; 127 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL); 128 if (!ctx->sinfo) 129 goto out_no_sinfo; 130 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature), 131 GFP_KERNEL); 132 if (!ctx->sinfo->sig) 133 goto out_no_sig; 134 135 ctx->data = (unsigned long)data; 136 ctx->ppcerts = &ctx->certs; 137 ctx->ppsinfo = &ctx->msg->signed_infos; 138 139 /* Attempt to decode the signature */ 140 ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen); 141 if (ret < 0) { 142 msg = ERR_PTR(ret); 143 goto out; 144 } 145 146 ret = pkcs7_check_authattrs(ctx->msg); 147 if (ret < 0) { 148 msg = ERR_PTR(ret); 149 goto out; 150 } 151 152 msg = ctx->msg; 153 ctx->msg = NULL; 154 155 out: 156 while (ctx->certs) { 157 struct x509_certificate *cert = ctx->certs; 158 ctx->certs = cert->next; 159 x509_free_certificate(cert); 160 } 161 out_no_sig: 162 pkcs7_free_signed_info(ctx->sinfo); 163 out_no_sinfo: 164 pkcs7_free_message(ctx->msg); 165 out_no_msg: 166 kfree(ctx); 167 out_no_ctx: 168 return msg; 169 } 170 EXPORT_SYMBOL_GPL(pkcs7_parse_message); 171 172 /** 173 * pkcs7_get_content_data - Get access to the PKCS#7 content 174 * @pkcs7: The preparsed PKCS#7 message to access 175 * @_data: Place to return a pointer to the data 176 * @_data_len: Place to return the data length 177 * @_headerlen: Size of ASN.1 header not included in _data 178 * 179 * Get access to the data content of the PKCS#7 message. The size of the 180 * header of the ASN.1 object that contains it is also provided and can be used 181 * to adjust *_data and *_data_len to get the entire object. 182 * 183 * Returns -ENODATA if the data object was missing from the message. 184 */ 185 int pkcs7_get_content_data(const struct pkcs7_message *pkcs7, 186 const void **_data, size_t *_data_len, 187 size_t *_headerlen) 188 { 189 if (!pkcs7->data) 190 return -ENODATA; 191 192 *_data = pkcs7->data; 193 *_data_len = pkcs7->data_len; 194 if (_headerlen) 195 *_headerlen = pkcs7->data_hdrlen; 196 return 0; 197 } 198 EXPORT_SYMBOL_GPL(pkcs7_get_content_data); 199 200 /* 201 * Note an OID when we find one for later processing when we know how 202 * to interpret it. 203 */ 204 int pkcs7_note_OID(void *context, size_t hdrlen, 205 unsigned char tag, 206 const void *value, size_t vlen) 207 { 208 struct pkcs7_parse_context *ctx = context; 209 210 ctx->last_oid = look_up_OID(value, vlen); 211 if (ctx->last_oid == OID__NR) { 212 char buffer[50]; 213 sprint_oid(value, vlen, buffer, sizeof(buffer)); 214 printk("PKCS7: Unknown OID: [%lu] %s\n", 215 (unsigned long)value - ctx->data, buffer); 216 } 217 return 0; 218 } 219 220 /* 221 * Note the digest algorithm for the signature. 222 */ 223 int pkcs7_sig_note_digest_algo(void *context, size_t hdrlen, 224 unsigned char tag, 225 const void *value, size_t vlen) 226 { 227 struct pkcs7_parse_context *ctx = context; 228 229 switch (ctx->last_oid) { 230 case OID_sha256: 231 ctx->sinfo->sig->hash_algo = "sha256"; 232 break; 233 case OID_sha384: 234 ctx->sinfo->sig->hash_algo = "sha384"; 235 break; 236 case OID_sha512: 237 ctx->sinfo->sig->hash_algo = "sha512"; 238 break; 239 case OID_sha224: 240 ctx->sinfo->sig->hash_algo = "sha224"; 241 break; 242 case OID_sm3: 243 ctx->sinfo->sig->hash_algo = "sm3"; 244 break; 245 case OID_gost2012Digest256: 246 ctx->sinfo->sig->hash_algo = "streebog256"; 247 break; 248 case OID_gost2012Digest512: 249 ctx->sinfo->sig->hash_algo = "streebog512"; 250 break; 251 default: 252 printk("Unsupported digest algo: %u\n", ctx->last_oid); 253 return -ENOPKG; 254 } 255 return 0; 256 } 257 258 /* 259 * Note the public key algorithm for the signature. 260 */ 261 int pkcs7_sig_note_pkey_algo(void *context, size_t hdrlen, 262 unsigned char tag, 263 const void *value, size_t vlen) 264 { 265 struct pkcs7_parse_context *ctx = context; 266 267 switch (ctx->last_oid) { 268 case OID_rsaEncryption: 269 ctx->sinfo->sig->pkey_algo = "rsa"; 270 ctx->sinfo->sig->encoding = "pkcs1"; 271 break; 272 case OID_id_ecdsa_with_sha224: 273 case OID_id_ecdsa_with_sha256: 274 case OID_id_ecdsa_with_sha384: 275 case OID_id_ecdsa_with_sha512: 276 ctx->sinfo->sig->pkey_algo = "ecdsa"; 277 ctx->sinfo->sig->encoding = "x962"; 278 break; 279 case OID_SM2_with_SM3: 280 ctx->sinfo->sig->pkey_algo = "sm2"; 281 ctx->sinfo->sig->encoding = "raw"; 282 break; 283 case OID_gost2012PKey256: 284 case OID_gost2012PKey512: 285 ctx->sinfo->sig->pkey_algo = "ecrdsa"; 286 ctx->sinfo->sig->encoding = "raw"; 287 break; 288 default: 289 printk("Unsupported pkey algo: %u\n", ctx->last_oid); 290 return -ENOPKG; 291 } 292 return 0; 293 } 294 295 /* 296 * We only support signed data [RFC2315 sec 9]. 297 */ 298 int pkcs7_check_content_type(void *context, size_t hdrlen, 299 unsigned char tag, 300 const void *value, size_t vlen) 301 { 302 struct pkcs7_parse_context *ctx = context; 303 304 if (ctx->last_oid != OID_signed_data) { 305 pr_warn("Only support pkcs7_signedData type\n"); 306 return -EINVAL; 307 } 308 309 return 0; 310 } 311 312 /* 313 * Note the SignedData version 314 */ 315 int pkcs7_note_signeddata_version(void *context, size_t hdrlen, 316 unsigned char tag, 317 const void *value, size_t vlen) 318 { 319 struct pkcs7_parse_context *ctx = context; 320 unsigned version; 321 322 if (vlen != 1) 323 goto unsupported; 324 325 ctx->msg->version = version = *(const u8 *)value; 326 switch (version) { 327 case 1: 328 /* PKCS#7 SignedData [RFC2315 sec 9.1] 329 * CMS ver 1 SignedData [RFC5652 sec 5.1] 330 */ 331 break; 332 case 3: 333 /* CMS ver 3 SignedData [RFC2315 sec 5.1] */ 334 break; 335 default: 336 goto unsupported; 337 } 338 339 return 0; 340 341 unsupported: 342 pr_warn("Unsupported SignedData version\n"); 343 return -EINVAL; 344 } 345 346 /* 347 * Note the SignerInfo version 348 */ 349 int pkcs7_note_signerinfo_version(void *context, size_t hdrlen, 350 unsigned char tag, 351 const void *value, size_t vlen) 352 { 353 struct pkcs7_parse_context *ctx = context; 354 unsigned version; 355 356 if (vlen != 1) 357 goto unsupported; 358 359 version = *(const u8 *)value; 360 switch (version) { 361 case 1: 362 /* PKCS#7 SignerInfo [RFC2315 sec 9.2] 363 * CMS ver 1 SignerInfo [RFC5652 sec 5.3] 364 */ 365 if (ctx->msg->version != 1) 366 goto version_mismatch; 367 ctx->expect_skid = false; 368 break; 369 case 3: 370 /* CMS ver 3 SignerInfo [RFC2315 sec 5.3] */ 371 if (ctx->msg->version == 1) 372 goto version_mismatch; 373 ctx->expect_skid = true; 374 break; 375 default: 376 goto unsupported; 377 } 378 379 return 0; 380 381 unsupported: 382 pr_warn("Unsupported SignerInfo version\n"); 383 return -EINVAL; 384 version_mismatch: 385 pr_warn("SignedData-SignerInfo version mismatch\n"); 386 return -EBADMSG; 387 } 388 389 /* 390 * Extract a certificate and store it in the context. 391 */ 392 int pkcs7_extract_cert(void *context, size_t hdrlen, 393 unsigned char tag, 394 const void *value, size_t vlen) 395 { 396 struct pkcs7_parse_context *ctx = context; 397 struct x509_certificate *x509; 398 399 if (tag != ((ASN1_UNIV << 6) | ASN1_CONS_BIT | ASN1_SEQ)) { 400 pr_debug("Cert began with tag %02x at %lu\n", 401 tag, (unsigned long)ctx - ctx->data); 402 return -EBADMSG; 403 } 404 405 /* We have to correct for the header so that the X.509 parser can start 406 * from the beginning. Note that since X.509 stipulates DER, there 407 * probably shouldn't be an EOC trailer - but it is in PKCS#7 (which 408 * stipulates BER). 409 */ 410 value -= hdrlen; 411 vlen += hdrlen; 412 413 if (((u8*)value)[1] == 0x80) 414 vlen += 2; /* Indefinite length - there should be an EOC */ 415 416 x509 = x509_cert_parse(value, vlen); 417 if (IS_ERR(x509)) 418 return PTR_ERR(x509); 419 420 x509->index = ++ctx->x509_index; 421 pr_debug("Got cert %u for %s\n", x509->index, x509->subject); 422 pr_debug("- fingerprint %*phN\n", x509->id->len, x509->id->data); 423 424 *ctx->ppcerts = x509; 425 ctx->ppcerts = &x509->next; 426 return 0; 427 } 428 429 /* 430 * Save the certificate list 431 */ 432 int pkcs7_note_certificate_list(void *context, size_t hdrlen, 433 unsigned char tag, 434 const void *value, size_t vlen) 435 { 436 struct pkcs7_parse_context *ctx = context; 437 438 pr_devel("Got cert list (%02x)\n", tag); 439 440 *ctx->ppcerts = ctx->msg->certs; 441 ctx->msg->certs = ctx->certs; 442 ctx->certs = NULL; 443 ctx->ppcerts = &ctx->certs; 444 return 0; 445 } 446 447 /* 448 * Note the content type. 449 */ 450 int pkcs7_note_content(void *context, size_t hdrlen, 451 unsigned char tag, 452 const void *value, size_t vlen) 453 { 454 struct pkcs7_parse_context *ctx = context; 455 456 if (ctx->last_oid != OID_data && 457 ctx->last_oid != OID_msIndirectData) { 458 pr_warn("Unsupported data type %d\n", ctx->last_oid); 459 return -EINVAL; 460 } 461 462 ctx->msg->data_type = ctx->last_oid; 463 return 0; 464 } 465 466 /* 467 * Extract the data from the message and store that and its content type OID in 468 * the context. 469 */ 470 int pkcs7_note_data(void *context, size_t hdrlen, 471 unsigned char tag, 472 const void *value, size_t vlen) 473 { 474 struct pkcs7_parse_context *ctx = context; 475 476 pr_debug("Got data\n"); 477 478 ctx->msg->data = value; 479 ctx->msg->data_len = vlen; 480 ctx->msg->data_hdrlen = hdrlen; 481 return 0; 482 } 483 484 /* 485 * Parse authenticated attributes. 486 */ 487 int pkcs7_sig_note_authenticated_attr(void *context, size_t hdrlen, 488 unsigned char tag, 489 const void *value, size_t vlen) 490 { 491 struct pkcs7_parse_context *ctx = context; 492 struct pkcs7_signed_info *sinfo = ctx->sinfo; 493 enum OID content_type; 494 495 pr_devel("AuthAttr: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value); 496 497 switch (ctx->last_oid) { 498 case OID_contentType: 499 if (__test_and_set_bit(sinfo_has_content_type, &sinfo->aa_set)) 500 goto repeated; 501 content_type = look_up_OID(value, vlen); 502 if (content_type != ctx->msg->data_type) { 503 pr_warn("Mismatch between global data type (%d) and sinfo %u (%d)\n", 504 ctx->msg->data_type, sinfo->index, 505 content_type); 506 return -EBADMSG; 507 } 508 return 0; 509 510 case OID_signingTime: 511 if (__test_and_set_bit(sinfo_has_signing_time, &sinfo->aa_set)) 512 goto repeated; 513 /* Should we check that the signing time is consistent 514 * with the signer's X.509 cert? 515 */ 516 return x509_decode_time(&sinfo->signing_time, 517 hdrlen, tag, value, vlen); 518 519 case OID_messageDigest: 520 if (__test_and_set_bit(sinfo_has_message_digest, &sinfo->aa_set)) 521 goto repeated; 522 if (tag != ASN1_OTS) 523 return -EBADMSG; 524 sinfo->msgdigest = value; 525 sinfo->msgdigest_len = vlen; 526 return 0; 527 528 case OID_smimeCapabilites: 529 if (__test_and_set_bit(sinfo_has_smime_caps, &sinfo->aa_set)) 530 goto repeated; 531 if (ctx->msg->data_type != OID_msIndirectData) { 532 pr_warn("S/MIME Caps only allowed with Authenticode\n"); 533 return -EKEYREJECTED; 534 } 535 return 0; 536 537 /* Microsoft SpOpusInfo seems to be contain cont[0] 16-bit BE 538 * char URLs and cont[1] 8-bit char URLs. 539 * 540 * Microsoft StatementType seems to contain a list of OIDs that 541 * are also used as extendedKeyUsage types in X.509 certs. 542 */ 543 case OID_msSpOpusInfo: 544 if (__test_and_set_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) 545 goto repeated; 546 goto authenticode_check; 547 case OID_msStatementType: 548 if (__test_and_set_bit(sinfo_has_ms_statement_type, &sinfo->aa_set)) 549 goto repeated; 550 authenticode_check: 551 if (ctx->msg->data_type != OID_msIndirectData) { 552 pr_warn("Authenticode AuthAttrs only allowed with Authenticode\n"); 553 return -EKEYREJECTED; 554 } 555 /* I'm not sure how to validate these */ 556 return 0; 557 default: 558 return 0; 559 } 560 561 repeated: 562 /* We permit max one item per AuthenticatedAttribute and no repeats */ 563 pr_warn("Repeated/multivalue AuthAttrs not permitted\n"); 564 return -EKEYREJECTED; 565 } 566 567 /* 568 * Note the set of auth attributes for digestion purposes [RFC2315 sec 9.3] 569 */ 570 int pkcs7_sig_note_set_of_authattrs(void *context, size_t hdrlen, 571 unsigned char tag, 572 const void *value, size_t vlen) 573 { 574 struct pkcs7_parse_context *ctx = context; 575 struct pkcs7_signed_info *sinfo = ctx->sinfo; 576 577 if (!test_bit(sinfo_has_content_type, &sinfo->aa_set) || 578 !test_bit(sinfo_has_message_digest, &sinfo->aa_set)) { 579 pr_warn("Missing required AuthAttr\n"); 580 return -EBADMSG; 581 } 582 583 if (ctx->msg->data_type != OID_msIndirectData && 584 test_bit(sinfo_has_ms_opus_info, &sinfo->aa_set)) { 585 pr_warn("Unexpected Authenticode AuthAttr\n"); 586 return -EBADMSG; 587 } 588 589 /* We need to switch the 'CONT 0' to a 'SET OF' when we digest */ 590 sinfo->authattrs = value - (hdrlen - 1); 591 sinfo->authattrs_len = vlen + (hdrlen - 1); 592 return 0; 593 } 594 595 /* 596 * Note the issuing certificate serial number 597 */ 598 int pkcs7_sig_note_serial(void *context, size_t hdrlen, 599 unsigned char tag, 600 const void *value, size_t vlen) 601 { 602 struct pkcs7_parse_context *ctx = context; 603 ctx->raw_serial = value; 604 ctx->raw_serial_size = vlen; 605 return 0; 606 } 607 608 /* 609 * Note the issuer's name 610 */ 611 int pkcs7_sig_note_issuer(void *context, size_t hdrlen, 612 unsigned char tag, 613 const void *value, size_t vlen) 614 { 615 struct pkcs7_parse_context *ctx = context; 616 ctx->raw_issuer = value; 617 ctx->raw_issuer_size = vlen; 618 return 0; 619 } 620 621 /* 622 * Note the issuing cert's subjectKeyIdentifier 623 */ 624 int pkcs7_sig_note_skid(void *context, size_t hdrlen, 625 unsigned char tag, 626 const void *value, size_t vlen) 627 { 628 struct pkcs7_parse_context *ctx = context; 629 630 pr_devel("SKID: %02x %zu [%*ph]\n", tag, vlen, (unsigned)vlen, value); 631 632 ctx->raw_skid = value; 633 ctx->raw_skid_size = vlen; 634 return 0; 635 } 636 637 /* 638 * Note the signature data 639 */ 640 int pkcs7_sig_note_signature(void *context, size_t hdrlen, 641 unsigned char tag, 642 const void *value, size_t vlen) 643 { 644 struct pkcs7_parse_context *ctx = context; 645 646 ctx->sinfo->sig->s = kmemdup(value, vlen, GFP_KERNEL); 647 if (!ctx->sinfo->sig->s) 648 return -ENOMEM; 649 650 ctx->sinfo->sig->s_size = vlen; 651 return 0; 652 } 653 654 /* 655 * Note a signature information block 656 */ 657 int pkcs7_note_signed_info(void *context, size_t hdrlen, 658 unsigned char tag, 659 const void *value, size_t vlen) 660 { 661 struct pkcs7_parse_context *ctx = context; 662 struct pkcs7_signed_info *sinfo = ctx->sinfo; 663 struct asymmetric_key_id *kid; 664 665 if (ctx->msg->data_type == OID_msIndirectData && !sinfo->authattrs) { 666 pr_warn("Authenticode requires AuthAttrs\n"); 667 return -EBADMSG; 668 } 669 670 /* Generate cert issuer + serial number key ID */ 671 if (!ctx->expect_skid) { 672 kid = asymmetric_key_generate_id(ctx->raw_serial, 673 ctx->raw_serial_size, 674 ctx->raw_issuer, 675 ctx->raw_issuer_size); 676 } else { 677 kid = asymmetric_key_generate_id(ctx->raw_skid, 678 ctx->raw_skid_size, 679 "", 0); 680 } 681 if (IS_ERR(kid)) 682 return PTR_ERR(kid); 683 684 pr_devel("SINFO KID: %u [%*phN]\n", kid->len, kid->len, kid->data); 685 686 sinfo->sig->auth_ids[0] = kid; 687 sinfo->index = ++ctx->sinfo_index; 688 *ctx->ppsinfo = sinfo; 689 ctx->ppsinfo = &sinfo->next; 690 ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL); 691 if (!ctx->sinfo) 692 return -ENOMEM; 693 ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature), 694 GFP_KERNEL); 695 if (!ctx->sinfo->sig) 696 return -ENOMEM; 697 return 0; 698 } 699