mirror of
https://github.com/torvalds/linux.git
synced 2024-12-15 07:33:56 +00:00
7d30198ee2
There are non-root X.509 v3 certificates in use out there that contain no Authority Key Identifier extension (RFC5280 section 4.2.1.1). For trust verification purposes the kernel asymmetric key type keeps two struct asymmetric_key_id instances that the key can be looked up by, and another two to look up the key's issuer. The x509 public key type and the PKCS7 type generate them from the SKID and AKID extensions in the certificate. In effect current code has no way to look up the issuer certificate for verification without the AKID. To remedy this, add a third asymmetric_key_id blob to the arrays in both asymmetric_key_id's (for certficate subject) and in the public_keys_signature's auth_ids (for issuer lookup), using just raw subject and issuer DNs from the certificate. Adapt asymmetric_key_ids() and its callers to use the third ID for lookups when none of the other two are available. Attempt to keep the logic intact when they are, to minimise behaviour changes. Adapt the restrict functions' NULL-checks to include that ID too. Do not modify the lookup logic in pkcs7_verify.c, the AKID extensions are still required there. Internally use a new "dn:" prefix to the search specifier string generated for the key lookup in find_asymmetric_key(). This tells asymmetric_key_match_preparse to only match the data against the raw DN in the third ID and shouldn't conflict with search specifiers already in use. In effect implement what (2) in the struct asymmetric_key_id comment (include/keys/asymmetric-type.h) is probably talking about already, so do not modify that comment. It is also how "openssl verify" looks up issuer certificates without the AKID available. Lookups by the raw DN are unambiguous only provided that the CAs respect the condition in RFC5280 4.2.1.1 that the AKID may only be omitted if the CA uses a single signing key. The following is an example of two things that this change enables. A self-signed ceritficate is generated following the example from https://letsencrypt.org/docs/certificates-for-localhost/, and can be looked up by an identifier and verified against itself by linking to a restricted keyring -- both things not possible before due to the missing AKID extension: $ openssl req -x509 -out localhost.crt -outform DER -keyout localhost.key \ -newkey rsa:2048 -nodes -sha256 \ -subj '/CN=localhost' -extensions EXT -config <( \ echo -e "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\n" \ "subjectAltName=DNS:localhost\nkeyUsage=digitalSignature\n" \ "extendedKeyUsage=serverAuth") $ keyring=`keyctl newring test @u` $ trusted=`keyctl padd asymmetric trusted $keyring < localhost.crt`; \ echo $trusted 39726322 $ keyctl search $keyring asymmetric dn:3112301006035504030c096c6f63616c686f7374 39726322 $ keyctl restrict_keyring $keyring asymmetric key_or_keyring:$trusted $ keyctl padd asymmetric verified $keyring < localhost.crt Signed-off-by: Andrew Zaborowski <andrew.zaborowski@intel.com> Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org> Acked-by: Jarkko Sakkinen <jarkko@kernel.org> Acked-by: David Howells <dhowells@redhat.com> Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
189 lines
4.6 KiB
C
189 lines
4.6 KiB
C
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
/* Validate the trust chain of a PKCS#7 message.
|
|
*
|
|
* Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
|
|
* Written by David Howells (dhowells@redhat.com)
|
|
*/
|
|
|
|
#define pr_fmt(fmt) "PKCS7: "fmt
|
|
#include <linux/kernel.h>
|
|
#include <linux/export.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/err.h>
|
|
#include <linux/asn1.h>
|
|
#include <linux/key.h>
|
|
#include <keys/asymmetric-type.h>
|
|
#include <crypto/public_key.h>
|
|
#include "pkcs7_parser.h"
|
|
|
|
/*
|
|
* Check the trust on one PKCS#7 SignedInfo block.
|
|
*/
|
|
static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
|
|
struct pkcs7_signed_info *sinfo,
|
|
struct key *trust_keyring)
|
|
{
|
|
struct public_key_signature *sig = sinfo->sig;
|
|
struct x509_certificate *x509, *last = NULL, *p;
|
|
struct key *key;
|
|
int ret;
|
|
|
|
kenter(",%u,", sinfo->index);
|
|
|
|
if (sinfo->unsupported_crypto) {
|
|
kleave(" = -ENOPKG [cached]");
|
|
return -ENOPKG;
|
|
}
|
|
|
|
for (x509 = sinfo->signer; x509; x509 = x509->signer) {
|
|
if (x509->seen) {
|
|
if (x509->verified)
|
|
goto verified;
|
|
kleave(" = -ENOKEY [cached]");
|
|
return -ENOKEY;
|
|
}
|
|
x509->seen = true;
|
|
|
|
/* Look to see if this certificate is present in the trusted
|
|
* keys.
|
|
*/
|
|
key = find_asymmetric_key(trust_keyring,
|
|
x509->id, x509->skid, NULL, false);
|
|
if (!IS_ERR(key)) {
|
|
/* One of the X.509 certificates in the PKCS#7 message
|
|
* is apparently the same as one we already trust.
|
|
* Verify that the trusted variant can also validate
|
|
* the signature on the descendant.
|
|
*/
|
|
pr_devel("sinfo %u: Cert %u as key %x\n",
|
|
sinfo->index, x509->index, key_serial(key));
|
|
goto matched;
|
|
}
|
|
if (key == ERR_PTR(-ENOMEM))
|
|
return -ENOMEM;
|
|
|
|
/* Self-signed certificates form roots of their own, and if we
|
|
* don't know them, then we can't accept them.
|
|
*/
|
|
if (x509->signer == x509) {
|
|
kleave(" = -ENOKEY [unknown self-signed]");
|
|
return -ENOKEY;
|
|
}
|
|
|
|
might_sleep();
|
|
last = x509;
|
|
sig = last->sig;
|
|
}
|
|
|
|
/* No match - see if the root certificate has a signer amongst the
|
|
* trusted keys.
|
|
*/
|
|
if (last && (last->sig->auth_ids[0] || last->sig->auth_ids[1])) {
|
|
key = find_asymmetric_key(trust_keyring,
|
|
last->sig->auth_ids[0],
|
|
last->sig->auth_ids[1],
|
|
NULL, false);
|
|
if (!IS_ERR(key)) {
|
|
x509 = last;
|
|
pr_devel("sinfo %u: Root cert %u signer is key %x\n",
|
|
sinfo->index, x509->index, key_serial(key));
|
|
goto matched;
|
|
}
|
|
if (PTR_ERR(key) != -ENOKEY)
|
|
return PTR_ERR(key);
|
|
}
|
|
|
|
/* As a last resort, see if we have a trusted public key that matches
|
|
* the signed info directly.
|
|
*/
|
|
key = find_asymmetric_key(trust_keyring,
|
|
sinfo->sig->auth_ids[0], NULL, NULL, false);
|
|
if (!IS_ERR(key)) {
|
|
pr_devel("sinfo %u: Direct signer is key %x\n",
|
|
sinfo->index, key_serial(key));
|
|
x509 = NULL;
|
|
sig = sinfo->sig;
|
|
goto matched;
|
|
}
|
|
if (PTR_ERR(key) != -ENOKEY)
|
|
return PTR_ERR(key);
|
|
|
|
kleave(" = -ENOKEY [no backref]");
|
|
return -ENOKEY;
|
|
|
|
matched:
|
|
ret = verify_signature(key, sig);
|
|
key_put(key);
|
|
if (ret < 0) {
|
|
if (ret == -ENOMEM)
|
|
return ret;
|
|
kleave(" = -EKEYREJECTED [verify %d]", ret);
|
|
return -EKEYREJECTED;
|
|
}
|
|
|
|
verified:
|
|
if (x509) {
|
|
x509->verified = true;
|
|
for (p = sinfo->signer; p != x509; p = p->signer)
|
|
p->verified = true;
|
|
}
|
|
kleave(" = 0");
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* pkcs7_validate_trust - Validate PKCS#7 trust chain
|
|
* @pkcs7: The PKCS#7 certificate to validate
|
|
* @trust_keyring: Signing certificates to use as starting points
|
|
*
|
|
* Validate that the certificate chain inside the PKCS#7 message intersects
|
|
* keys we already know and trust.
|
|
*
|
|
* Returns, in order of descending priority:
|
|
*
|
|
* (*) -EKEYREJECTED if a signature failed to match for which we have a valid
|
|
* key, or:
|
|
*
|
|
* (*) 0 if at least one signature chain intersects with the keys in the trust
|
|
* keyring, or:
|
|
*
|
|
* (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
|
|
* chain.
|
|
*
|
|
* (*) -ENOKEY if we couldn't find a match for any of the signature chains in
|
|
* the message.
|
|
*
|
|
* May also return -ENOMEM.
|
|
*/
|
|
int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
|
|
struct key *trust_keyring)
|
|
{
|
|
struct pkcs7_signed_info *sinfo;
|
|
struct x509_certificate *p;
|
|
int cached_ret = -ENOKEY;
|
|
int ret;
|
|
|
|
for (p = pkcs7->certs; p; p = p->next)
|
|
p->seen = false;
|
|
|
|
for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
|
|
ret = pkcs7_validate_trust_one(pkcs7, sinfo, trust_keyring);
|
|
switch (ret) {
|
|
case -ENOKEY:
|
|
continue;
|
|
case -ENOPKG:
|
|
if (cached_ret == -ENOKEY)
|
|
cached_ret = -ENOPKG;
|
|
continue;
|
|
case 0:
|
|
cached_ret = 0;
|
|
continue;
|
|
default:
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
return cached_ret;
|
|
}
|
|
EXPORT_SYMBOL_GPL(pkcs7_validate_trust);
|