blob: ae47be6128c411f6fc3c3f44e729a14218c9b55c [file] [log] [blame]
David Howells08815b62014-07-01 16:40:20 +01001/* Validate the trust chain of a PKCS#7 message.
2 *
3 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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 "public_key.h"
21#include "pkcs7_parser.h"
22
David Howells08815b62014-07-01 16:40:20 +010023/**
24 * Check the trust on one PKCS#7 SignedInfo block.
25 */
David Howells15155b92014-09-16 17:07:07 +010026static int pkcs7_validate_trust_one(struct pkcs7_message *pkcs7,
27 struct pkcs7_signed_info *sinfo,
28 struct key *trust_keyring)
David Howells08815b62014-07-01 16:40:20 +010029{
30 struct public_key_signature *sig = &sinfo->sig;
31 struct x509_certificate *x509, *last = NULL, *p;
32 struct key *key;
33 bool trusted;
34 int ret;
35
36 kenter(",%u,", sinfo->index);
37
David Howells41559422014-09-16 17:36:15 +010038 if (sinfo->unsupported_crypto) {
39 kleave(" = -ENOPKG [cached]");
40 return -ENOPKG;
41 }
42
David Howells08815b62014-07-01 16:40:20 +010043 for (x509 = sinfo->signer; x509; x509 = x509->signer) {
44 if (x509->seen) {
45 if (x509->verified) {
46 trusted = x509->trusted;
47 goto verified;
48 }
49 kleave(" = -ENOKEY [cached]");
50 return -ENOKEY;
51 }
52 x509->seen = true;
53
54 /* Look to see if this certificate is present in the trusted
55 * keys.
56 */
David Howells46963b72014-09-16 17:36:13 +010057 key = x509_request_asymmetric_key(trust_keyring, x509->id);
David Howells757932e2014-09-16 17:36:17 +010058 if (!IS_ERR(key)) {
David Howells08815b62014-07-01 16:40:20 +010059 /* One of the X.509 certificates in the PKCS#7 message
60 * is apparently the same as one we already trust.
61 * Verify that the trusted variant can also validate
62 * the signature on the descendant.
63 */
David Howells757932e2014-09-16 17:36:17 +010064 pr_devel("sinfo %u: Cert %u as key %x\n",
65 sinfo->index, x509->index, key_serial(key));
David Howells08815b62014-07-01 16:40:20 +010066 goto matched;
David Howells757932e2014-09-16 17:36:17 +010067 }
David Howells08815b62014-07-01 16:40:20 +010068 if (key == ERR_PTR(-ENOMEM))
69 return -ENOMEM;
70
71 /* Self-signed certificates form roots of their own, and if we
72 * don't know them, then we can't accept them.
73 */
74 if (x509->next == x509) {
75 kleave(" = -ENOKEY [unknown self-signed]");
76 return -ENOKEY;
77 }
78
79 might_sleep();
80 last = x509;
81 sig = &last->sig;
82 }
83
84 /* No match - see if the root certificate has a signer amongst the
85 * trusted keys.
86 */
David Howells757932e2014-09-16 17:36:17 +010087 if (last && last->authority) {
88 key = x509_request_asymmetric_key(trust_keyring, last->authority);
89 if (!IS_ERR(key)) {
90 x509 = last;
91 pr_devel("sinfo %u: Root cert %u signer is key %x\n",
92 sinfo->index, x509->index, key_serial(key));
93 goto matched;
94 }
95 if (PTR_ERR(key) != -ENOKEY)
96 return PTR_ERR(key);
David Howells08815b62014-07-01 16:40:20 +010097 }
98
David Howells757932e2014-09-16 17:36:17 +010099 /* As a last resort, see if we have a trusted public key that matches
100 * the signed info directly.
101 */
102 key = x509_request_asymmetric_key(trust_keyring,
103 sinfo->signing_cert_id);
104 if (!IS_ERR(key)) {
105 pr_devel("sinfo %u: Direct signer is key %x\n",
106 sinfo->index, key_serial(key));
107 x509 = NULL;
108 goto matched;
109 }
110 if (PTR_ERR(key) != -ENOKEY)
111 return PTR_ERR(key);
112
113 kleave(" = -ENOKEY [no backref]");
114 return -ENOKEY;
David Howells08815b62014-07-01 16:40:20 +0100115
116matched:
117 ret = verify_signature(key, sig);
118 trusted = test_bit(KEY_FLAG_TRUSTED, &key->flags);
119 key_put(key);
120 if (ret < 0) {
121 if (ret == -ENOMEM)
122 return ret;
123 kleave(" = -EKEYREJECTED [verify %d]", ret);
124 return -EKEYREJECTED;
125 }
126
127verified:
David Howells757932e2014-09-16 17:36:17 +0100128 if (x509) {
129 x509->verified = true;
130 for (p = sinfo->signer; p != x509; p = p->signer) {
131 p->verified = true;
132 p->trusted = trusted;
133 }
David Howells08815b62014-07-01 16:40:20 +0100134 }
135 sinfo->trusted = trusted;
136 kleave(" = 0");
137 return 0;
138}
139
140/**
141 * pkcs7_validate_trust - Validate PKCS#7 trust chain
142 * @pkcs7: The PKCS#7 certificate to validate
143 * @trust_keyring: Signing certificates to use as starting points
144 * @_trusted: Set to true if trustworth, false otherwise
145 *
146 * Validate that the certificate chain inside the PKCS#7 message intersects
147 * keys we already know and trust.
148 *
149 * Returns, in order of descending priority:
150 *
151 * (*) -EKEYREJECTED if a signature failed to match for which we have a valid
152 * key, or:
153 *
154 * (*) 0 if at least one signature chain intersects with the keys in the trust
155 * keyring, or:
156 *
157 * (*) -ENOPKG if a suitable crypto module couldn't be found for a check on a
158 * chain.
159 *
160 * (*) -ENOKEY if we couldn't find a match for any of the signature chains in
161 * the message.
162 *
163 * May also return -ENOMEM.
164 */
165int pkcs7_validate_trust(struct pkcs7_message *pkcs7,
166 struct key *trust_keyring,
167 bool *_trusted)
168{
169 struct pkcs7_signed_info *sinfo;
170 struct x509_certificate *p;
David Howells41559422014-09-16 17:36:15 +0100171 int cached_ret = -ENOKEY;
172 int ret;
David Howells08815b62014-07-01 16:40:20 +0100173
174 for (p = pkcs7->certs; p; p = p->next)
175 p->seen = false;
176
177 for (sinfo = pkcs7->signed_infos; sinfo; sinfo = sinfo->next) {
178 ret = pkcs7_validate_trust_one(pkcs7, sinfo, trust_keyring);
David Howells41559422014-09-16 17:36:15 +0100179 switch (ret) {
180 case -ENOKEY:
181 continue;
182 case -ENOPKG:
183 if (cached_ret == -ENOKEY)
David Howells08815b62014-07-01 16:40:20 +0100184 cached_ret = -ENOPKG;
David Howells41559422014-09-16 17:36:15 +0100185 continue;
186 case 0:
187 *_trusted |= sinfo->trusted;
188 cached_ret = 0;
189 continue;
190 default:
191 return ret;
David Howells08815b62014-07-01 16:40:20 +0100192 }
David Howells08815b62014-07-01 16:40:20 +0100193 }
194
195 return cached_ret;
196}
197EXPORT_SYMBOL_GPL(pkcs7_validate_trust);