1 /* Validate the trust chain of a PKCS#7 message.
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
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 <linux/key.h>
19 #include <keys/asymmetric-type.h>
20 #include <crypto/public_key.h>
21 #include "pkcs7_parser.h"
24 * Check the trust on one PKCS#7 SignedInfo block.
26 static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
27 struct pkcs7_signed_info *sinfo,
28 struct key *trust_keyring)
30 struct public_key_signature *sig = &sinfo->sig;
31 struct x509_certificate *x509, *last = NULL, *p;
36 kenter(",%u,", sinfo->index);
38 if (sinfo->unsupported_crypto) {
39 kleave(" = -ENOPKG [cached]");
43 for (x509 = sinfo->signer; x509; x509 = x509->signer) {
46 trusted = x509->trusted;
49 kleave(" = -ENOKEY [cached]");
54 /* Look to see if this certificate is present in the trusted
57 key = x509_request_asymmetric_key(trust_keyring,
61 /* One of the X.509 certificates in the PKCS#7 message
62 * is apparently the same as one we already trust.
63 * Verify that the trusted variant can also validate
64 * the signature on the descendant.
66 pr_devel("sinfo %u: Cert %u as key %x\n",
67 sinfo->index, x509->index, key_serial(key));
70 if (key == ERR_PTR(-ENOMEM))
73 /* Self-signed certificates form roots of their own, and if we
74 * don't know them, then we can't accept them.
76 if (x509->next == x509) {
77 kleave(" = -ENOKEY [unknown self-signed]");
86 /* No match - see if the root certificate has a signer amongst the
89 if (last && (last->akid_id || last->akid_skid)) {
90 key = x509_request_asymmetric_key(trust_keyring,
96 pr_devel("sinfo %u: Root cert %u signer is key %x\n",
97 sinfo->index, x509->index, key_serial(key));
100 if (PTR_ERR(key) != -ENOKEY)
104 /* As a last resort, see if we have a trusted public key that matches
105 * the signed info directly.
107 key = x509_request_asymmetric_key(trust_keyring,
108 sinfo->signing_cert_id,
112 pr_devel("sinfo %u: Direct signer is key %x\n",
113 sinfo->index, key_serial(key));
117 if (PTR_ERR(key) != -ENOKEY)
120 kleave(" = -ENOKEY [no backref]");
124 ret = verify_signature(key, sig);
125 trusted = test_bit(KEY_FLAG_TRUSTED, &key->flags);
130 kleave(" = -EKEYREJECTED [verify %d]", ret);
131 return -EKEYREJECTED;
136 x509->verified = true;
137 for (p = sinfo->signer; p != x509; p = p->signer) {
139 p->trusted = trusted;
142 sinfo->trusted = trusted;
148 * pkcs7_validate_trust - Validate PKCS#7 trust chain
149 * @pkcs7: The PKCS#7 certificate to validate
150 * @trust_keyring: Signing certificates to use as starting points
151 * @_trusted: Set to true if trustworth, false otherwise
153 * Validate that the certificate chain inside the PKCS#7 message intersects
154 * keys we already know and trust.
156 * Returns, in order of descending priority:
158 * (*) -EKEYREJECTED if a signature failed to match for which we have a valid
161 * (*) 0 if at least one signature chain intersects with the keys in the trust
164 * (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
167 * (*) -ENOKEY if we couldn't find a match for any of the signature chains in
170 * May also return -ENOMEM.
172 int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
173 struct key *trust_keyring,
176 struct pkcs7_signed_info *sinfo;
177 struct x509_certificate *p;
178 int cached_ret = -ENOKEY;
183 for (p = pkcs7->certs; p; p = p->next)
186 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
187 ret = pkcs7_validate_trust_one(pkcs7, sinfo, trust_keyring);
192 if (cached_ret == -ENOKEY)
193 cached_ret = -ENOPKG;
196 *_trusted |= sinfo->trusted;
206 EXPORT_SYMBOL_GPL(pkcs7_validate_trust);