]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - crypto/asymmetric_keys/restrict.c
Merge tag 'mmc-v4.15-rc2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[mirror_ubuntu-bionic-kernel.git] / crypto / asymmetric_keys / restrict.c
CommitLineData
cfb664ff
DH
1/* Instantiate a public key crypto key from an X.509 Certificate
2 *
a511e1af 3 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
cfb664ff
DH
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
a511e1af 12#define pr_fmt(fmt) "ASYM: "fmt
cfb664ff
DH
13#include <linux/module.h>
14#include <linux/kernel.h>
cfb664ff 15#include <linux/err.h>
cfb664ff
DH
16#include <crypto/public_key.h>
17#include "asymmetric_keys.h"
cfb664ff
DH
18
19static bool use_builtin_keys;
20static struct asymmetric_key_id *ca_keyid;
21
22#ifndef MODULE
23static struct {
24 struct asymmetric_key_id id;
25 unsigned char data[10];
26} cakey;
27
28static int __init ca_keys_setup(char *str)
29{
30 if (!str) /* default system keyring */
31 return 1;
32
33 if (strncmp(str, "id:", 3) == 0) {
34 struct asymmetric_key_id *p = &cakey.id;
35 size_t hexlen = (strlen(str) - 3) / 2;
36 int ret;
37
38 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
39 pr_err("Missing or invalid ca_keys id\n");
40 return 1;
41 }
42
43 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
44 if (ret < 0)
45 pr_err("Unparsable ca_keys id hex string\n");
46 else
47 ca_keyid = p; /* owner key 'id:xxxxxx' */
48 } else if (strcmp(str, "builtin") == 0) {
49 use_builtin_keys = true;
50 }
51
52 return 1;
53}
54__setup("ca_keys=", ca_keys_setup);
55#endif
56
a511e1af
DH
57/**
58 * restrict_link_by_signature - Restrict additions to a ring of public keys
aaf66c88 59 * @dest_keyring: Keyring being linked to.
a511e1af
DH
60 * @type: The type of key being added.
61 * @payload: The payload of the new key.
aaf66c88 62 * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
a511e1af 63 *
cfb664ff
DH
64 * Check the new certificate against the ones in the trust keyring. If one of
65 * those is the signing key and validates the new certificate, then mark the
66 * new certificate as being trusted.
67 *
a511e1af
DH
68 * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
69 * matching parent certificate in the trusted list, -EKEYREJECTED if the
70 * signature check fails or the key is blacklisted and some other error if
71 * there is a matching certificate but the signature check cannot be performed.
cfb664ff 72 */
aaf66c88 73int restrict_link_by_signature(struct key *dest_keyring,
a511e1af 74 const struct key_type *type,
aaf66c88
MM
75 const union key_payload *payload,
76 struct key *trust_keyring)
cfb664ff 77{
a511e1af 78 const struct public_key_signature *sig;
cfb664ff 79 struct key *key;
a511e1af 80 int ret;
cfb664ff 81
a511e1af 82 pr_devel("==>%s()\n", __func__);
cfb664ff
DH
83
84 if (!trust_keyring)
a511e1af
DH
85 return -ENOKEY;
86
87 if (type != &key_type_asymmetric)
cfb664ff 88 return -EOPNOTSUPP;
a511e1af
DH
89
90 sig = payload->data[asym_auth];
91 if (!sig->auth_ids[0] && !sig->auth_ids[1])
acddc720 92 return -ENOKEY;
a511e1af 93
cfb664ff
DH
94 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
95 return -EPERM;
cfb664ff 96
a511e1af 97 /* See if we have a key that signed this one. */
cfb664ff
DH
98 key = find_asymmetric_key(trust_keyring,
99 sig->auth_ids[0], sig->auth_ids[1],
100 false);
101 if (IS_ERR(key))
a511e1af 102 return -ENOKEY;
cfb664ff 103
a511e1af
DH
104 if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
105 ret = -ENOKEY;
106 else
107 ret = verify_signature(key, sig);
cfb664ff
DH
108 key_put(key);
109 return ret;
110}
7e3c4d22 111
8e323a02
MM
112static bool match_either_id(const struct asymmetric_key_ids *pair,
113 const struct asymmetric_key_id *single)
114{
115 return (asymmetric_key_id_same(pair->id[0], single) ||
116 asymmetric_key_id_same(pair->id[1], single));
117}
118
119static int key_or_keyring_common(struct key *dest_keyring,
120 const struct key_type *type,
121 const union key_payload *payload,
122 struct key *trusted, bool check_dest)
7e3c4d22
MM
123{
124 const struct public_key_signature *sig;
8e323a02 125 struct key *key = NULL;
7e3c4d22
MM
126 int ret;
127
128 pr_devel("==>%s()\n", __func__);
129
130 if (!dest_keyring)
131 return -ENOKEY;
132 else if (dest_keyring->type != &key_type_keyring)
133 return -EOPNOTSUPP;
134
8e323a02 135 if (!trusted && !check_dest)
7e3c4d22
MM
136 return -ENOKEY;
137
138 if (type != &key_type_asymmetric)
139 return -EOPNOTSUPP;
140
141 sig = payload->data[asym_auth];
142 if (!sig->auth_ids[0] && !sig->auth_ids[1])
143 return -ENOKEY;
144
8e323a02
MM
145 if (trusted) {
146 if (trusted->type == &key_type_keyring) {
147 /* See if we have a key that signed this one. */
148 key = find_asymmetric_key(trusted, sig->auth_ids[0],
149 sig->auth_ids[1], false);
150 if (IS_ERR(key))
151 key = NULL;
152 } else if (trusted->type == &key_type_asymmetric) {
153 const struct asymmetric_key_ids *signer_ids;
7e3c4d22 154
8e323a02 155 signer_ids = asymmetric_key_ids(trusted);
7e3c4d22 156
8e323a02
MM
157 /*
158 * The auth_ids come from the candidate key (the
159 * one that is being considered for addition to
160 * dest_keyring) and identify the key that was
161 * used to sign.
162 *
163 * The signer_ids are identifiers for the
164 * signing key specified for dest_keyring.
165 *
166 * The first auth_id is the preferred id, and
167 * the second is the fallback. If only one
168 * auth_id is present, it may match against
169 * either signer_id. If two auth_ids are
170 * present, the first auth_id must match one
171 * signer_id and the second auth_id must match
172 * the second signer_id.
173 */
174 if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
175 const struct asymmetric_key_id *auth_id;
7e3c4d22 176
8e323a02
MM
177 auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
178 if (match_either_id(signer_ids, auth_id))
179 key = __key_get(trusted);
180
181 } else if (asymmetric_key_id_same(signer_ids->id[1],
182 sig->auth_ids[1]) &&
183 match_either_id(signer_ids,
184 sig->auth_ids[0])) {
185 key = __key_get(trusted);
186 }
187 } else {
188 return -EOPNOTSUPP;
189 }
7e3c4d22
MM
190 }
191
8e323a02
MM
192 if (check_dest && !key) {
193 /* See if the destination has a key that signed this one. */
194 key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
195 sig->auth_ids[1], false);
196 if (IS_ERR(key))
197 key = NULL;
198 }
199
200 if (!key)
201 return -ENOKEY;
202
7e3c4d22
MM
203 ret = key_validate(key);
204 if (ret == 0)
205 ret = verify_signature(key, sig);
206
207 key_put(key);
208 return ret;
209}
8e323a02
MM
210
211/**
212 * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
213 * keys using the restrict_key information stored in the ring.
214 * @dest_keyring: Keyring being linked to.
215 * @type: The type of key being added.
216 * @payload: The payload of the new key.
217 * @trusted: A key or ring of keys that can be used to vouch for the new cert.
218 *
219 * Check the new certificate only against the key or keys passed in the data
220 * parameter. If one of those is the signing key and validates the new
221 * certificate, then mark the new certificate as being ok to link.
222 *
223 * Returns 0 if the new certificate was accepted, -ENOKEY if we
224 * couldn't find a matching parent certificate in the trusted list,
225 * -EKEYREJECTED if the signature check fails, and some other error if
226 * there is a matching certificate but the signature check cannot be
227 * performed.
228 */
229int restrict_link_by_key_or_keyring(struct key *dest_keyring,
230 const struct key_type *type,
231 const union key_payload *payload,
232 struct key *trusted)
233{
234 return key_or_keyring_common(dest_keyring, type, payload, trusted,
235 false);
236}
237
238/**
239 * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
240 * public keys using the restrict_key information stored in the ring.
241 * @dest_keyring: Keyring being linked to.
242 * @type: The type of key being added.
243 * @payload: The payload of the new key.
244 * @trusted: A key or ring of keys that can be used to vouch for the new cert.
245 *
246 * Check the new certificate only against the key or keys passed in the data
247 * parameter. If one of those is the signing key and validates the new
248 * certificate, then mark the new certificate as being ok to link.
249 *
250 * Returns 0 if the new certificate was accepted, -ENOKEY if we
251 * couldn't find a matching parent certificate in the trusted list,
252 * -EKEYREJECTED if the signature check fails, and some other error if
253 * there is a matching certificate but the signature check cannot be
254 * performed.
255 */
256int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
257 const struct key_type *type,
258 const union key_payload *payload,
259 struct key *trusted)
260{
261 return key_or_keyring_common(dest_keyring, type, payload, trusted,
262 true);
263}