blob: 84cefe3b35856829ad1bcc117a14406828976a83 [file] [log] [blame]
Thomas Gleixnerb4d0d232019-05-20 19:08:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellscfb664f2016-04-06 16:14:26 +01002/* Instantiate a public key crypto key from an X.509 Certificate
3 *
David Howellsa511e1a2016-04-06 16:14:26 +01004 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
David Howellscfb664f2016-04-06 16:14:26 +01005 * Written by David Howells (dhowells@redhat.com)
David Howellscfb664f2016-04-06 16:14:26 +01006 */
7
David Howellsa511e1a2016-04-06 16:14:26 +01008#define pr_fmt(fmt) "ASYM: "fmt
David Howellscfb664f2016-04-06 16:14:26 +01009#include <linux/module.h>
10#include <linux/kernel.h>
David Howellscfb664f2016-04-06 16:14:26 +010011#include <linux/err.h>
David Howellscfb664f2016-04-06 16:14:26 +010012#include <crypto/public_key.h>
13#include "asymmetric_keys.h"
David Howellscfb664f2016-04-06 16:14:26 +010014
15static bool use_builtin_keys;
16static struct asymmetric_key_id *ca_keyid;
17
18#ifndef MODULE
19static struct {
20 struct asymmetric_key_id id;
21 unsigned char data[10];
22} cakey;
23
24static int __init ca_keys_setup(char *str)
25{
26 if (!str) /* default system keyring */
27 return 1;
28
29 if (strncmp(str, "id:", 3) == 0) {
30 struct asymmetric_key_id *p = &cakey.id;
31 size_t hexlen = (strlen(str) - 3) / 2;
32 int ret;
33
34 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
35 pr_err("Missing or invalid ca_keys id\n");
36 return 1;
37 }
38
39 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
40 if (ret < 0)
41 pr_err("Unparsable ca_keys id hex string\n");
42 else
43 ca_keyid = p; /* owner key 'id:xxxxxx' */
44 } else if (strcmp(str, "builtin") == 0) {
45 use_builtin_keys = true;
46 }
47
48 return 1;
49}
50__setup("ca_keys=", ca_keys_setup);
51#endif
52
David Howellsa511e1a2016-04-06 16:14:26 +010053/**
54 * restrict_link_by_signature - Restrict additions to a ring of public keys
Mat Martineauaaf66c82016-08-30 11:33:13 -070055 * @dest_keyring: Keyring being linked to.
David Howellsa511e1a2016-04-06 16:14:26 +010056 * @type: The type of key being added.
57 * @payload: The payload of the new key.
Mat Martineauaaf66c82016-08-30 11:33:13 -070058 * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
David Howellsa511e1a2016-04-06 16:14:26 +010059 *
David Howellscfb664f2016-04-06 16:14:26 +010060 * Check the new certificate against the ones in the trust keyring. If one of
61 * those is the signing key and validates the new certificate, then mark the
62 * new certificate as being trusted.
63 *
David Howellsa511e1a2016-04-06 16:14:26 +010064 * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
65 * matching parent certificate in the trusted list, -EKEYREJECTED if the
Eric Biggers4b349682018-02-22 14:38:34 +000066 * signature check fails or the key is blacklisted, -ENOPKG if the signature
67 * uses unsupported crypto, or some other error if there is a matching
68 * certificate but the signature check cannot be performed.
David Howellscfb664f2016-04-06 16:14:26 +010069 */
Mat Martineauaaf66c82016-08-30 11:33:13 -070070int restrict_link_by_signature(struct key *dest_keyring,
David Howellsa511e1a2016-04-06 16:14:26 +010071 const struct key_type *type,
Mat Martineauaaf66c82016-08-30 11:33:13 -070072 const union key_payload *payload,
73 struct key *trust_keyring)
David Howellscfb664f2016-04-06 16:14:26 +010074{
David Howellsa511e1a2016-04-06 16:14:26 +010075 const struct public_key_signature *sig;
David Howellscfb664f2016-04-06 16:14:26 +010076 struct key *key;
David Howellsa511e1a2016-04-06 16:14:26 +010077 int ret;
David Howellscfb664f2016-04-06 16:14:26 +010078
David Howellsa511e1a2016-04-06 16:14:26 +010079 pr_devel("==>%s()\n", __func__);
David Howellscfb664f2016-04-06 16:14:26 +010080
81 if (!trust_keyring)
David Howellsa511e1a2016-04-06 16:14:26 +010082 return -ENOKEY;
83
84 if (type != &key_type_asymmetric)
David Howellscfb664f2016-04-06 16:14:26 +010085 return -EOPNOTSUPP;
David Howellsa511e1a2016-04-06 16:14:26 +010086
87 sig = payload->data[asym_auth];
Eric Biggers4b349682018-02-22 14:38:34 +000088 if (!sig)
89 return -ENOPKG;
David Howellsa511e1a2016-04-06 16:14:26 +010090 if (!sig->auth_ids[0] && !sig->auth_ids[1])
Mat Martineauacddc722016-07-18 00:10:55 +010091 return -ENOKEY;
David Howellsa511e1a2016-04-06 16:14:26 +010092
David Howellscfb664f2016-04-06 16:14:26 +010093 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
94 return -EPERM;
David Howellscfb664f2016-04-06 16:14:26 +010095
David Howellsa511e1a2016-04-06 16:14:26 +010096 /* See if we have a key that signed this one. */
David Howellscfb664f2016-04-06 16:14:26 +010097 key = find_asymmetric_key(trust_keyring,
98 sig->auth_ids[0], sig->auth_ids[1],
99 false);
100 if (IS_ERR(key))
David Howellsa511e1a2016-04-06 16:14:26 +0100101 return -ENOKEY;
David Howellscfb664f2016-04-06 16:14:26 +0100102
David Howellsa511e1a2016-04-06 16:14:26 +0100103 if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
104 ret = -ENOKEY;
105 else
106 ret = verify_signature(key, sig);
David Howellscfb664f2016-04-06 16:14:26 +0100107 key_put(key);
108 return ret;
109}
Mat Martineau7e3c4d22016-06-27 16:45:16 -0700110
Mat Martineau8e323a02016-10-04 16:42:45 -0700111static bool match_either_id(const struct asymmetric_key_ids *pair,
112 const struct asymmetric_key_id *single)
113{
114 return (asymmetric_key_id_same(pair->id[0], single) ||
115 asymmetric_key_id_same(pair->id[1], single));
116}
117
118static int key_or_keyring_common(struct key *dest_keyring,
119 const struct key_type *type,
120 const union key_payload *payload,
121 struct key *trusted, bool check_dest)
122{
123 const struct public_key_signature *sig;
124 struct key *key = NULL;
125 int ret;
126
127 pr_devel("==>%s()\n", __func__);
128
129 if (!dest_keyring)
130 return -ENOKEY;
131 else if (dest_keyring->type != &key_type_keyring)
132 return -EOPNOTSUPP;
133
134 if (!trusted && !check_dest)
135 return -ENOKEY;
136
137 if (type != &key_type_asymmetric)
138 return -EOPNOTSUPP;
139
140 sig = payload->data[asym_auth];
Eric Biggers4b349682018-02-22 14:38:34 +0000141 if (!sig)
142 return -ENOPKG;
Mat Martineau8e323a02016-10-04 16:42:45 -0700143 if (!sig->auth_ids[0] && !sig->auth_ids[1])
144 return -ENOKEY;
145
146 if (trusted) {
147 if (trusted->type == &key_type_keyring) {
148 /* See if we have a key that signed this one. */
149 key = find_asymmetric_key(trusted, sig->auth_ids[0],
150 sig->auth_ids[1], false);
151 if (IS_ERR(key))
152 key = NULL;
153 } else if (trusted->type == &key_type_asymmetric) {
154 const struct asymmetric_key_ids *signer_ids;
155
156 signer_ids = asymmetric_key_ids(trusted);
157
158 /*
159 * The auth_ids come from the candidate key (the
160 * one that is being considered for addition to
161 * dest_keyring) and identify the key that was
162 * used to sign.
163 *
164 * The signer_ids are identifiers for the
165 * signing key specified for dest_keyring.
166 *
167 * The first auth_id is the preferred id, and
168 * the second is the fallback. If only one
169 * auth_id is present, it may match against
170 * either signer_id. If two auth_ids are
171 * present, the first auth_id must match one
172 * signer_id and the second auth_id must match
173 * the second signer_id.
174 */
175 if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
176 const struct asymmetric_key_id *auth_id;
177
178 auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
179 if (match_either_id(signer_ids, auth_id))
180 key = __key_get(trusted);
181
182 } else if (asymmetric_key_id_same(signer_ids->id[1],
183 sig->auth_ids[1]) &&
184 match_either_id(signer_ids,
185 sig->auth_ids[0])) {
186 key = __key_get(trusted);
187 }
188 } else {
189 return -EOPNOTSUPP;
190 }
191 }
192
193 if (check_dest && !key) {
194 /* See if the destination has a key that signed this one. */
195 key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
196 sig->auth_ids[1], false);
197 if (IS_ERR(key))
198 key = NULL;
199 }
200
201 if (!key)
202 return -ENOKEY;
203
204 ret = key_validate(key);
205 if (ret == 0)
206 ret = verify_signature(key, sig);
207
208 key_put(key);
209 return ret;
210}
211
Mat Martineau7e3c4d22016-06-27 16:45:16 -0700212/**
213 * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
214 * keys using the restrict_key information stored in the ring.
215 * @dest_keyring: Keyring being linked to.
216 * @type: The type of key being added.
217 * @payload: The payload of the new key.
218 * @trusted: A key or ring of keys that can be used to vouch for the new cert.
219 *
220 * Check the new certificate only against the key or keys passed in the data
221 * parameter. If one of those is the signing key and validates the new
222 * certificate, then mark the new certificate as being ok to link.
223 *
224 * Returns 0 if the new certificate was accepted, -ENOKEY if we
225 * couldn't find a matching parent certificate in the trusted list,
Eric Biggers4b349682018-02-22 14:38:34 +0000226 * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
227 * unsupported crypto, or some other error if there is a matching certificate
228 * but the signature check cannot be performed.
Mat Martineau7e3c4d22016-06-27 16:45:16 -0700229 */
230int restrict_link_by_key_or_keyring(struct key *dest_keyring,
231 const struct key_type *type,
232 const union key_payload *payload,
233 struct key *trusted)
234{
Mat Martineau8e323a02016-10-04 16:42:45 -0700235 return key_or_keyring_common(dest_keyring, type, payload, trusted,
236 false);
237}
Mat Martineau7e3c4d22016-06-27 16:45:16 -0700238
Mat Martineau8e323a02016-10-04 16:42:45 -0700239/**
240 * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
241 * public keys using the restrict_key information stored in the ring.
242 * @dest_keyring: Keyring being linked to.
243 * @type: The type of key being added.
244 * @payload: The payload of the new key.
245 * @trusted: A key or ring of keys that can be used to vouch for the new cert.
246 *
Andrew Zaborowski40d32b52021-01-04 17:40:48 +0100247 * Check the new certificate against the key or keys passed in the data
248 * parameter and against the keys already linked to the destination keyring. If
249 * one of those is the signing key and validates the new certificate, then mark
250 * the new certificate as being ok to link.
Mat Martineau8e323a02016-10-04 16:42:45 -0700251 *
252 * Returns 0 if the new certificate was accepted, -ENOKEY if we
253 * couldn't find a matching parent certificate in the trusted list,
Eric Biggers4b349682018-02-22 14:38:34 +0000254 * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
255 * unsupported crypto, or some other error if there is a matching certificate
256 * but the signature check cannot be performed.
Mat Martineau8e323a02016-10-04 16:42:45 -0700257 */
258int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
259 const struct key_type *type,
260 const union key_payload *payload,
261 struct key *trusted)
262{
263 return key_or_keyring_common(dest_keyring, type, payload, trusted,
264 true);
Mat Martineau7e3c4d22016-06-27 16:45:16 -0700265}