Path: blob/master/crypto/asymmetric_keys/pkcs7_verify.c
51667 views
// SPDX-License-Identifier: GPL-2.0-or-later1/* Verify the signature on a PKCS#7 message.2*3* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.4* Written by David Howells ([email protected])5*/67#define pr_fmt(fmt) "PKCS7: "fmt8#include <linux/kernel.h>9#include <linux/export.h>10#include <linux/slab.h>11#include <linux/err.h>12#include <linux/asn1.h>13#include <crypto/hash.h>14#include <crypto/hash_info.h>15#include <crypto/public_key.h>16#include "pkcs7_parser.h"1718/*19* Digest the relevant parts of the PKCS#7 data20*/21static int pkcs7_digest(struct pkcs7_message *pkcs7,22struct pkcs7_signed_info *sinfo)23{24struct public_key_signature *sig = sinfo->sig;25struct crypto_shash *tfm;26struct shash_desc *desc;27size_t desc_size;28int ret;2930kenter(",%u,%s", sinfo->index, sinfo->sig->hash_algo);3132if (!sinfo->authattrs && sig->algo_takes_data) {33/* There's no intermediate digest and the signature algo34* doesn't want the data prehashing.35*/36sig->m = (void *)pkcs7->data;37sig->m_size = pkcs7->data_len;38sig->m_free = false;39return 0;40}4142/* The digest was calculated already. */43if (sig->m)44return 0;4546if (!sinfo->sig->hash_algo)47return -ENOPKG;4849/* Allocate the hashing algorithm we're going to need and find out how50* big the hash operational data will be.51*/52tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0);53if (IS_ERR(tfm))54return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);5556desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);57sig->m_size = crypto_shash_digestsize(tfm);5859ret = -ENOMEM;60sig->m = kmalloc(umax(sinfo->authattrs_len, sig->m_size), GFP_KERNEL);61if (!sig->m)62goto error_no_desc;63sig->m_free = true;6465desc = kzalloc(desc_size, GFP_KERNEL);66if (!desc)67goto error_no_desc;6869desc->tfm = tfm;7071/* Digest the message [RFC2315 9.3] */72ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len, sig->m);73if (ret < 0)74goto error;75pr_devel("MsgDigest = [%*ph]\n", 8, sig->m);7677/* However, if there are authenticated attributes, there must be a78* message digest attribute amongst them which corresponds to the79* digest we just calculated.80*/81if (sinfo->authattrs) {82if (!sinfo->msgdigest) {83pr_warn("Sig %u: No messageDigest\n", sinfo->index);84ret = -EKEYREJECTED;85goto error;86}8788if (sinfo->msgdigest_len != sig->m_size) {89pr_warn("Sig %u: Invalid digest size (%u)\n",90sinfo->index, sinfo->msgdigest_len);91ret = -EBADMSG;92goto error;93}9495if (memcmp(sig->m, sinfo->msgdigest,96sinfo->msgdigest_len) != 0) {97pr_warn("Sig %u: Message digest doesn't match\n",98sinfo->index);99ret = -EKEYREJECTED;100goto error;101}102103/* We then calculate anew, using the authenticated attributes104* as the contents of the digest instead. Note that we need to105* convert the attributes from a CONT.0 into a SET before we106* hash it.107*108* However, for certain algorithms, such as ML-DSA, the digest109* is integrated into the signing algorithm. In such a case,110* we copy the authattrs, modifying the tag type, and set that111* as the digest.112*/113memcpy(sig->m, sinfo->authattrs, sinfo->authattrs_len);114sig->m[0] = ASN1_CONS_BIT | ASN1_SET;115116if (sig->algo_takes_data) {117sig->m_size = sinfo->authattrs_len;118ret = 0;119} else {120ret = crypto_shash_digest(desc, sig->m,121sinfo->authattrs_len,122sig->m);123if (ret < 0)124goto error;125}126pr_devel("AADigest = [%*ph]\n", 8, sig->m);127}128129error:130kfree(desc);131error_no_desc:132crypto_free_shash(tfm);133kleave(" = %d", ret);134return ret;135}136137int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,138enum hash_algo *hash_algo)139{140struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;141int i, ret;142143/*144* This function doesn't support messages with more than one signature.145*/146if (sinfo == NULL || sinfo->next != NULL)147return -EBADMSG;148149ret = pkcs7_digest(pkcs7, sinfo);150if (ret)151return ret;152if (!sinfo->sig->m_free) {153pr_notice_once("%s: No digest available\n", __func__);154return -EINVAL; /* TODO: MLDSA doesn't necessarily calculate an155* intermediate digest. */156}157158*buf = sinfo->sig->m;159*len = sinfo->sig->m_size;160161i = match_string(hash_algo_name, HASH_ALGO__LAST,162sinfo->sig->hash_algo);163if (i >= 0)164*hash_algo = i;165166return 0;167}168169/*170* Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7171* uses the issuer's name and the issuing certificate serial number for172* matching purposes. These must match the certificate issuer's name (not173* subject's name) and the certificate serial number [RFC 2315 6.7].174*/175static int pkcs7_find_key(struct pkcs7_message *pkcs7,176struct pkcs7_signed_info *sinfo)177{178struct x509_certificate *x509;179unsigned certix = 1;180181kenter("%u", sinfo->index);182183for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {184/* I'm _assuming_ that the generator of the PKCS#7 message will185* encode the fields from the X.509 cert in the same way in the186* PKCS#7 message - but I can't be 100% sure of that. It's187* possible this will need element-by-element comparison.188*/189if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))190continue;191pr_devel("Sig %u: Found cert serial match X.509[%u]\n",192sinfo->index, certix);193194sinfo->signer = x509;195return 0;196}197198/* The relevant X.509 cert isn't found here, but it might be found in199* the trust keyring.200*/201pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",202sinfo->index,203sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);204return 0;205}206207/*208* Verify the internal certificate chain as best we can.209*/210static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,211struct pkcs7_signed_info *sinfo)212{213struct public_key_signature *sig;214struct x509_certificate *x509 = sinfo->signer, *p;215struct asymmetric_key_id *auth;216int ret;217218kenter("");219220for (p = pkcs7->certs; p; p = p->next)221p->seen = false;222223for (;;) {224pr_debug("verify %s: %*phN\n",225x509->subject,226x509->raw_serial_size, x509->raw_serial);227x509->seen = true;228229if (x509->blacklisted) {230/* If this cert is blacklisted, then mark everything231* that depends on this as blacklisted too.232*/233sinfo->blacklisted = true;234for (p = sinfo->signer; p != x509; p = p->signer)235p->blacklisted = true;236pr_debug("- blacklisted\n");237return 0;238}239240pr_debug("- issuer %s\n", x509->issuer);241sig = x509->sig;242if (sig->auth_ids[0])243pr_debug("- authkeyid.id %*phN\n",244sig->auth_ids[0]->len, sig->auth_ids[0]->data);245if (sig->auth_ids[1])246pr_debug("- authkeyid.skid %*phN\n",247sig->auth_ids[1]->len, sig->auth_ids[1]->data);248249if (x509->self_signed) {250/* If there's no authority certificate specified, then251* the certificate must be self-signed and is the root252* of the chain. Likewise if the cert is its own253* authority.254*/255if (x509->unsupported_sig)256goto unsupported_sig_in_x509;257x509->signer = x509;258pr_debug("- self-signed\n");259return 0;260}261262/* Look through the X.509 certificates in the PKCS#7 message's263* list to see if the next one is there.264*/265auth = sig->auth_ids[0];266if (auth) {267pr_debug("- want %*phN\n", auth->len, auth->data);268for (p = pkcs7->certs; p; p = p->next) {269pr_debug("- cmp [%u] %*phN\n",270p->index, p->id->len, p->id->data);271if (asymmetric_key_id_same(p->id, auth))272goto found_issuer_check_skid;273}274} else if (sig->auth_ids[1]) {275auth = sig->auth_ids[1];276pr_debug("- want %*phN\n", auth->len, auth->data);277for (p = pkcs7->certs; p; p = p->next) {278if (!p->skid)279continue;280pr_debug("- cmp [%u] %*phN\n",281p->index, p->skid->len, p->skid->data);282if (asymmetric_key_id_same(p->skid, auth))283goto found_issuer;284}285}286287/* We didn't find the root of this chain */288pr_debug("- top\n");289return 0;290291found_issuer_check_skid:292/* We matched issuer + serialNumber, but if there's an293* authKeyId.keyId, that must match the CA subjKeyId also.294*/295if (sig->auth_ids[1] &&296!asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {297pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",298sinfo->index, x509->index, p->index);299return -EKEYREJECTED;300}301found_issuer:302pr_debug("- subject %s\n", p->subject);303if (p->seen) {304pr_warn("Sig %u: X.509 chain contains loop\n",305sinfo->index);306return 0;307}308ret = public_key_verify_signature(p->pub, x509->sig);309if (ret < 0)310return ret;311x509->signer = p;312if (x509 == p) {313pr_debug("- self-signed\n");314return 0;315}316x509 = p;317might_sleep();318}319320unsupported_sig_in_x509:321/* Just prune the certificate chain at this point if we lack some322* crypto module to go further. Note, however, we don't want to set323* sinfo->unsupported_crypto as the signed info block may still be324* validatable against an X.509 cert lower in the chain that we have a325* trusted copy of.326*/327return 0;328}329330/*331* Verify one signed information block from a PKCS#7 message.332*/333static int pkcs7_verify_one(struct pkcs7_message *pkcs7,334struct pkcs7_signed_info *sinfo)335{336int ret;337338kenter(",%u", sinfo->index);339340/* First of all, digest the data in the PKCS#7 message and the341* signed information block342*/343ret = pkcs7_digest(pkcs7, sinfo);344if (ret < 0)345return ret;346347/* Find the key for the signature if there is one */348ret = pkcs7_find_key(pkcs7, sinfo);349if (ret < 0)350return ret;351352if (!sinfo->signer)353return 0;354355pr_devel("Using X.509[%u] for sig %u\n",356sinfo->signer->index, sinfo->index);357358/* Check that the PKCS#7 signing time is valid according to the X.509359* certificate. We can't, however, check against the system clock360* since that may not have been set yet and may be wrong.361*/362if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {363if (sinfo->signing_time < sinfo->signer->valid_from ||364sinfo->signing_time > sinfo->signer->valid_to) {365pr_warn("Message signed outside of X.509 validity window\n");366return -EKEYREJECTED;367}368}369370/* Verify the PKCS#7 binary against the key */371ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);372if (ret < 0)373return ret;374375pr_devel("Verified signature %u\n", sinfo->index);376377/* Verify the internal certificate chain */378return pkcs7_verify_sig_chain(pkcs7, sinfo);379}380381/**382* pkcs7_verify - Verify a PKCS#7 message383* @pkcs7: The PKCS#7 message to be verified384* @usage: The use to which the key is being put385*386* Verify a PKCS#7 message is internally consistent - that is, the data digest387* matches the digest in the AuthAttrs and any signature in the message or one388* of the X.509 certificates it carries that matches another X.509 cert in the389* message can be verified.390*391* This does not look to match the contents of the PKCS#7 message against any392* external public keys.393*394* Returns, in order of descending priority:395*396* (*) -EKEYREJECTED if a key was selected that had a usage restriction at397* odds with the specified usage, or:398*399* (*) -EKEYREJECTED if a signature failed to match for which we found an400* appropriate X.509 certificate, or:401*402* (*) -EBADMSG if some part of the message was invalid, or:403*404* (*) 0 if a signature chain passed verification, or:405*406* (*) -EKEYREJECTED if a blacklisted key was encountered, or:407*408* (*) -ENOPKG if none of the signature chains are verifiable because suitable409* crypto modules couldn't be found.410*/411int pkcs7_verify(struct pkcs7_message *pkcs7,412enum key_being_used_for usage)413{414struct pkcs7_signed_info *sinfo;415int actual_ret = -ENOPKG;416int ret;417418kenter("");419420switch (usage) {421case VERIFYING_MODULE_SIGNATURE:422if (pkcs7->data_type != OID_data) {423pr_warn("Invalid module sig (not pkcs7-data)\n");424return -EKEYREJECTED;425}426if (pkcs7->have_authattrs) {427#ifdef CONFIG_PKCS7_WAIVE_AUTHATTRS_REJECTION_FOR_MLDSA428if (pkcs7->authattrs_rej_waivable) {429pr_warn_once("Waived invalid module sig (has authattrs)\n");430break;431}432#endif433pr_warn("Invalid module sig (has authattrs)\n");434return -EKEYREJECTED;435}436break;437case VERIFYING_FIRMWARE_SIGNATURE:438if (pkcs7->data_type != OID_data) {439pr_warn("Invalid firmware sig (not pkcs7-data)\n");440return -EKEYREJECTED;441}442if (!pkcs7->have_authattrs) {443pr_warn("Invalid firmware sig (missing authattrs)\n");444return -EKEYREJECTED;445}446break;447case VERIFYING_KEXEC_PE_SIGNATURE:448if (pkcs7->data_type != OID_msIndirectData) {449pr_warn("Invalid kexec sig (not Authenticode)\n");450return -EKEYREJECTED;451}452/* Authattr presence checked in parser */453break;454case VERIFYING_UNSPECIFIED_SIGNATURE:455case VERIFYING_BPF_SIGNATURE:456if (pkcs7->data_type != OID_data) {457pr_warn("Invalid unspecified sig (not pkcs7-data)\n");458return -EKEYREJECTED;459}460break;461default:462return -EINVAL;463}464465for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {466ret = pkcs7_verify_one(pkcs7, sinfo);467if (sinfo->blacklisted) {468if (actual_ret == -ENOPKG)469actual_ret = -EKEYREJECTED;470continue;471}472if (ret < 0) {473if (ret == -ENOPKG) {474sinfo->unsupported_crypto = true;475continue;476}477kleave(" = %d", ret);478return ret;479}480actual_ret = 0;481}482483kleave(" = %d", actual_ret);484return actual_ret;485}486EXPORT_SYMBOL_GPL(pkcs7_verify);487488/**489* pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message490* @pkcs7: The PKCS#7 message491* @data: The data to be verified492* @datalen: The amount of data493*494* Supply the detached data needed to verify a PKCS#7 message. Note that no495* attempt to retain/pin the data is made. That is left to the caller. The496* data will not be modified by pkcs7_verify() and will not be freed when the497* PKCS#7 message is freed.498*499* Returns -EINVAL if data is already supplied in the message, 0 otherwise.500*/501int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,502const void *data, size_t datalen)503{504if (pkcs7->data) {505pr_warn("Data already supplied\n");506return -EINVAL;507}508pkcs7->data = data;509pkcs7->data_len = datalen;510return 0;511}512EXPORT_SYMBOL_GPL(pkcs7_supply_detached_data);513514515