Path: blob/master/crypto/asymmetric_keys/pkcs7_verify.c
26278 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);3132/* The digest was calculated already. */33if (sig->digest)34return 0;3536if (!sinfo->sig->hash_algo)37return -ENOPKG;3839/* Allocate the hashing algorithm we're going to need and find out how40* big the hash operational data will be.41*/42tfm = crypto_alloc_shash(sinfo->sig->hash_algo, 0, 0);43if (IS_ERR(tfm))44return (PTR_ERR(tfm) == -ENOENT) ? -ENOPKG : PTR_ERR(tfm);4546desc_size = crypto_shash_descsize(tfm) + sizeof(*desc);47sig->digest_size = crypto_shash_digestsize(tfm);4849ret = -ENOMEM;50sig->digest = kmalloc(sig->digest_size, GFP_KERNEL);51if (!sig->digest)52goto error_no_desc;5354desc = kzalloc(desc_size, GFP_KERNEL);55if (!desc)56goto error_no_desc;5758desc->tfm = tfm;5960/* Digest the message [RFC2315 9.3] */61ret = crypto_shash_digest(desc, pkcs7->data, pkcs7->data_len,62sig->digest);63if (ret < 0)64goto error;65pr_devel("MsgDigest = [%*ph]\n", 8, sig->digest);6667/* However, if there are authenticated attributes, there must be a68* message digest attribute amongst them which corresponds to the69* digest we just calculated.70*/71if (sinfo->authattrs) {72u8 tag;7374if (!sinfo->msgdigest) {75pr_warn("Sig %u: No messageDigest\n", sinfo->index);76ret = -EKEYREJECTED;77goto error;78}7980if (sinfo->msgdigest_len != sig->digest_size) {81pr_warn("Sig %u: Invalid digest size (%u)\n",82sinfo->index, sinfo->msgdigest_len);83ret = -EBADMSG;84goto error;85}8687if (memcmp(sig->digest, sinfo->msgdigest,88sinfo->msgdigest_len) != 0) {89pr_warn("Sig %u: Message digest doesn't match\n",90sinfo->index);91ret = -EKEYREJECTED;92goto error;93}9495/* We then calculate anew, using the authenticated attributes96* as the contents of the digest instead. Note that we need to97* convert the attributes from a CONT.0 into a SET before we98* hash it.99*/100memset(sig->digest, 0, sig->digest_size);101102ret = crypto_shash_init(desc);103if (ret < 0)104goto error;105tag = ASN1_CONS_BIT | ASN1_SET;106ret = crypto_shash_update(desc, &tag, 1);107if (ret < 0)108goto error;109ret = crypto_shash_finup(desc, sinfo->authattrs,110sinfo->authattrs_len, sig->digest);111if (ret < 0)112goto error;113pr_devel("AADigest = [%*ph]\n", 8, sig->digest);114}115116error:117kfree(desc);118error_no_desc:119crypto_free_shash(tfm);120kleave(" = %d", ret);121return ret;122}123124int pkcs7_get_digest(struct pkcs7_message *pkcs7, const u8 **buf, u32 *len,125enum hash_algo *hash_algo)126{127struct pkcs7_signed_info *sinfo = pkcs7->signed_infos;128int i, ret;129130/*131* This function doesn't support messages with more than one signature.132*/133if (sinfo == NULL || sinfo->next != NULL)134return -EBADMSG;135136ret = pkcs7_digest(pkcs7, sinfo);137if (ret)138return ret;139140*buf = sinfo->sig->digest;141*len = sinfo->sig->digest_size;142143i = match_string(hash_algo_name, HASH_ALGO__LAST,144sinfo->sig->hash_algo);145if (i >= 0)146*hash_algo = i;147148return 0;149}150151/*152* Find the key (X.509 certificate) to use to verify a PKCS#7 message. PKCS#7153* uses the issuer's name and the issuing certificate serial number for154* matching purposes. These must match the certificate issuer's name (not155* subject's name) and the certificate serial number [RFC 2315 6.7].156*/157static int pkcs7_find_key(struct pkcs7_message *pkcs7,158struct pkcs7_signed_info *sinfo)159{160struct x509_certificate *x509;161unsigned certix = 1;162163kenter("%u", sinfo->index);164165for (x509 = pkcs7->certs; x509; x509 = x509->next, certix++) {166/* I'm _assuming_ that the generator of the PKCS#7 message will167* encode the fields from the X.509 cert in the same way in the168* PKCS#7 message - but I can't be 100% sure of that. It's169* possible this will need element-by-element comparison.170*/171if (!asymmetric_key_id_same(x509->id, sinfo->sig->auth_ids[0]))172continue;173pr_devel("Sig %u: Found cert serial match X.509[%u]\n",174sinfo->index, certix);175176sinfo->signer = x509;177return 0;178}179180/* The relevant X.509 cert isn't found here, but it might be found in181* the trust keyring.182*/183pr_debug("Sig %u: Issuing X.509 cert not found (#%*phN)\n",184sinfo->index,185sinfo->sig->auth_ids[0]->len, sinfo->sig->auth_ids[0]->data);186return 0;187}188189/*190* Verify the internal certificate chain as best we can.191*/192static int pkcs7_verify_sig_chain(struct pkcs7_message *pkcs7,193struct pkcs7_signed_info *sinfo)194{195struct public_key_signature *sig;196struct x509_certificate *x509 = sinfo->signer, *p;197struct asymmetric_key_id *auth;198int ret;199200kenter("");201202for (p = pkcs7->certs; p; p = p->next)203p->seen = false;204205for (;;) {206pr_debug("verify %s: %*phN\n",207x509->subject,208x509->raw_serial_size, x509->raw_serial);209x509->seen = true;210211if (x509->blacklisted) {212/* If this cert is blacklisted, then mark everything213* that depends on this as blacklisted too.214*/215sinfo->blacklisted = true;216for (p = sinfo->signer; p != x509; p = p->signer)217p->blacklisted = true;218pr_debug("- blacklisted\n");219return 0;220}221222pr_debug("- issuer %s\n", x509->issuer);223sig = x509->sig;224if (sig->auth_ids[0])225pr_debug("- authkeyid.id %*phN\n",226sig->auth_ids[0]->len, sig->auth_ids[0]->data);227if (sig->auth_ids[1])228pr_debug("- authkeyid.skid %*phN\n",229sig->auth_ids[1]->len, sig->auth_ids[1]->data);230231if (x509->self_signed) {232/* If there's no authority certificate specified, then233* the certificate must be self-signed and is the root234* of the chain. Likewise if the cert is its own235* authority.236*/237if (x509->unsupported_sig)238goto unsupported_sig_in_x509;239x509->signer = x509;240pr_debug("- self-signed\n");241return 0;242}243244/* Look through the X.509 certificates in the PKCS#7 message's245* list to see if the next one is there.246*/247auth = sig->auth_ids[0];248if (auth) {249pr_debug("- want %*phN\n", auth->len, auth->data);250for (p = pkcs7->certs; p; p = p->next) {251pr_debug("- cmp [%u] %*phN\n",252p->index, p->id->len, p->id->data);253if (asymmetric_key_id_same(p->id, auth))254goto found_issuer_check_skid;255}256} else if (sig->auth_ids[1]) {257auth = sig->auth_ids[1];258pr_debug("- want %*phN\n", auth->len, auth->data);259for (p = pkcs7->certs; p; p = p->next) {260if (!p->skid)261continue;262pr_debug("- cmp [%u] %*phN\n",263p->index, p->skid->len, p->skid->data);264if (asymmetric_key_id_same(p->skid, auth))265goto found_issuer;266}267}268269/* We didn't find the root of this chain */270pr_debug("- top\n");271return 0;272273found_issuer_check_skid:274/* We matched issuer + serialNumber, but if there's an275* authKeyId.keyId, that must match the CA subjKeyId also.276*/277if (sig->auth_ids[1] &&278!asymmetric_key_id_same(p->skid, sig->auth_ids[1])) {279pr_warn("Sig %u: X.509 chain contains auth-skid nonmatch (%u->%u)\n",280sinfo->index, x509->index, p->index);281return -EKEYREJECTED;282}283found_issuer:284pr_debug("- subject %s\n", p->subject);285if (p->seen) {286pr_warn("Sig %u: X.509 chain contains loop\n",287sinfo->index);288return 0;289}290ret = public_key_verify_signature(p->pub, x509->sig);291if (ret < 0)292return ret;293x509->signer = p;294if (x509 == p) {295pr_debug("- self-signed\n");296return 0;297}298x509 = p;299might_sleep();300}301302unsupported_sig_in_x509:303/* Just prune the certificate chain at this point if we lack some304* crypto module to go further. Note, however, we don't want to set305* sinfo->unsupported_crypto as the signed info block may still be306* validatable against an X.509 cert lower in the chain that we have a307* trusted copy of.308*/309return 0;310}311312/*313* Verify one signed information block from a PKCS#7 message.314*/315static int pkcs7_verify_one(struct pkcs7_message *pkcs7,316struct pkcs7_signed_info *sinfo)317{318int ret;319320kenter(",%u", sinfo->index);321322/* First of all, digest the data in the PKCS#7 message and the323* signed information block324*/325ret = pkcs7_digest(pkcs7, sinfo);326if (ret < 0)327return ret;328329/* Find the key for the signature if there is one */330ret = pkcs7_find_key(pkcs7, sinfo);331if (ret < 0)332return ret;333334if (!sinfo->signer)335return 0;336337pr_devel("Using X.509[%u] for sig %u\n",338sinfo->signer->index, sinfo->index);339340/* Check that the PKCS#7 signing time is valid according to the X.509341* certificate. We can't, however, check against the system clock342* since that may not have been set yet and may be wrong.343*/344if (test_bit(sinfo_has_signing_time, &sinfo->aa_set)) {345if (sinfo->signing_time < sinfo->signer->valid_from ||346sinfo->signing_time > sinfo->signer->valid_to) {347pr_warn("Message signed outside of X.509 validity window\n");348return -EKEYREJECTED;349}350}351352/* Verify the PKCS#7 binary against the key */353ret = public_key_verify_signature(sinfo->signer->pub, sinfo->sig);354if (ret < 0)355return ret;356357pr_devel("Verified signature %u\n", sinfo->index);358359/* Verify the internal certificate chain */360return pkcs7_verify_sig_chain(pkcs7, sinfo);361}362363/**364* pkcs7_verify - Verify a PKCS#7 message365* @pkcs7: The PKCS#7 message to be verified366* @usage: The use to which the key is being put367*368* Verify a PKCS#7 message is internally consistent - that is, the data digest369* matches the digest in the AuthAttrs and any signature in the message or one370* of the X.509 certificates it carries that matches another X.509 cert in the371* message can be verified.372*373* This does not look to match the contents of the PKCS#7 message against any374* external public keys.375*376* Returns, in order of descending priority:377*378* (*) -EKEYREJECTED if a key was selected that had a usage restriction at379* odds with the specified usage, or:380*381* (*) -EKEYREJECTED if a signature failed to match for which we found an382* appropriate X.509 certificate, or:383*384* (*) -EBADMSG if some part of the message was invalid, or:385*386* (*) 0 if a signature chain passed verification, or:387*388* (*) -EKEYREJECTED if a blacklisted key was encountered, or:389*390* (*) -ENOPKG if none of the signature chains are verifiable because suitable391* crypto modules couldn't be found.392*/393int pkcs7_verify(struct pkcs7_message *pkcs7,394enum key_being_used_for usage)395{396struct pkcs7_signed_info *sinfo;397int actual_ret = -ENOPKG;398int ret;399400kenter("");401402switch (usage) {403case VERIFYING_MODULE_SIGNATURE:404if (pkcs7->data_type != OID_data) {405pr_warn("Invalid module sig (not pkcs7-data)\n");406return -EKEYREJECTED;407}408if (pkcs7->have_authattrs) {409pr_warn("Invalid module sig (has authattrs)\n");410return -EKEYREJECTED;411}412break;413case VERIFYING_FIRMWARE_SIGNATURE:414if (pkcs7->data_type != OID_data) {415pr_warn("Invalid firmware sig (not pkcs7-data)\n");416return -EKEYREJECTED;417}418if (!pkcs7->have_authattrs) {419pr_warn("Invalid firmware sig (missing authattrs)\n");420return -EKEYREJECTED;421}422break;423case VERIFYING_KEXEC_PE_SIGNATURE:424if (pkcs7->data_type != OID_msIndirectData) {425pr_warn("Invalid kexec sig (not Authenticode)\n");426return -EKEYREJECTED;427}428/* Authattr presence checked in parser */429break;430case VERIFYING_UNSPECIFIED_SIGNATURE:431if (pkcs7->data_type != OID_data) {432pr_warn("Invalid unspecified sig (not pkcs7-data)\n");433return -EKEYREJECTED;434}435break;436default:437return -EINVAL;438}439440for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {441ret = pkcs7_verify_one(pkcs7, sinfo);442if (sinfo->blacklisted) {443if (actual_ret == -ENOPKG)444actual_ret = -EKEYREJECTED;445continue;446}447if (ret < 0) {448if (ret == -ENOPKG) {449sinfo->unsupported_crypto = true;450continue;451}452kleave(" = %d", ret);453return ret;454}455actual_ret = 0;456}457458kleave(" = %d", actual_ret);459return actual_ret;460}461EXPORT_SYMBOL_GPL(pkcs7_verify);462463/**464* pkcs7_supply_detached_data - Supply the data needed to verify a PKCS#7 message465* @pkcs7: The PKCS#7 message466* @data: The data to be verified467* @datalen: The amount of data468*469* Supply the detached data needed to verify a PKCS#7 message. Note that no470* attempt to retain/pin the data is made. That is left to the caller. The471* data will not be modified by pkcs7_verify() and will not be freed when the472* PKCS#7 message is freed.473*474* Returns -EINVAL if data is already supplied in the message, 0 otherwise.475*/476int pkcs7_supply_detached_data(struct pkcs7_message *pkcs7,477const void *data, size_t datalen)478{479if (pkcs7->data) {480pr_warn("Data already supplied\n");481return -EINVAL;482}483pkcs7->data = data;484pkcs7->data_len = datalen;485return 0;486}487EXPORT_SYMBOL_GPL(pkcs7_supply_detached_data);488489490