1 /* Instantiate a public key crypto key from an X.509 Certificate
3 * Copyright (C) 2012, 2016 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) "ASYM: "fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/err.h>
16 #include <crypto/public_key.h>
17 #include "asymmetric_keys.h"
19 static bool use_builtin_keys;
20 static struct asymmetric_key_id *ca_keyid;
24 struct asymmetric_key_id id;
25 unsigned char data[10];
28 static int __init ca_keys_setup(char *str)
30 if (!str) /* default system keyring */
33 if (strncmp(str, "id:", 3) == 0) {
34 struct asymmetric_key_id *p = &cakey.id;
35 size_t hexlen = (strlen(str) - 3) / 2;
38 if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
39 pr_err("Missing or invalid ca_keys id\n");
43 ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
45 pr_err("Unparsable ca_keys id hex string\n");
47 ca_keyid = p; /* owner key 'id:xxxxxx' */
48 } else if (strcmp(str, "builtin") == 0) {
49 use_builtin_keys = true;
54 __setup("ca_keys=", ca_keys_setup);
58 * restrict_link_by_signature - Restrict additions to a ring of public keys
59 * @dest_keyring: Keyring being linked to.
60 * @type: The type of key being added.
61 * @payload: The payload of the new key.
62 * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
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.
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, -ENOPKG if the signature
71 * uses unsupported crypto, or some other error if there is a matching
72 * certificate but the signature check cannot be performed.
74 int restrict_link_by_signature(struct key *dest_keyring,
75 const struct key_type *type,
76 const union key_payload *payload,
77 struct key *trust_keyring)
79 const struct public_key_signature *sig;
83 pr_devel("==>%s()\n", __func__);
88 if (type != &key_type_asymmetric)
91 sig = payload->data[asym_auth];
94 if (!sig->auth_ids[0] && !sig->auth_ids[1])
97 if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
100 /* See if we have a key that signed this one. */
101 key = find_asymmetric_key(trust_keyring,
102 sig->auth_ids[0], sig->auth_ids[1],
107 if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
110 ret = verify_signature(key, sig);
115 static bool match_either_id(const struct asymmetric_key_ids *pair,
116 const struct asymmetric_key_id *single)
118 return (asymmetric_key_id_same(pair->id[0], single) ||
119 asymmetric_key_id_same(pair->id[1], single));
122 static int key_or_keyring_common(struct key *dest_keyring,
123 const struct key_type *type,
124 const union key_payload *payload,
125 struct key *trusted, bool check_dest)
127 const struct public_key_signature *sig;
128 struct key *key = NULL;
131 pr_devel("==>%s()\n", __func__);
135 else if (dest_keyring->type != &key_type_keyring)
138 if (!trusted && !check_dest)
141 if (type != &key_type_asymmetric)
144 sig = payload->data[asym_auth];
147 if (!sig->auth_ids[0] && !sig->auth_ids[1])
151 if (trusted->type == &key_type_keyring) {
152 /* See if we have a key that signed this one. */
153 key = find_asymmetric_key(trusted, sig->auth_ids[0],
154 sig->auth_ids[1], false);
157 } else if (trusted->type == &key_type_asymmetric) {
158 const struct asymmetric_key_ids *signer_ids;
160 signer_ids = asymmetric_key_ids(trusted);
163 * The auth_ids come from the candidate key (the
164 * one that is being considered for addition to
165 * dest_keyring) and identify the key that was
168 * The signer_ids are identifiers for the
169 * signing key specified for dest_keyring.
171 * The first auth_id is the preferred id, and
172 * the second is the fallback. If only one
173 * auth_id is present, it may match against
174 * either signer_id. If two auth_ids are
175 * present, the first auth_id must match one
176 * signer_id and the second auth_id must match
177 * the second signer_id.
179 if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
180 const struct asymmetric_key_id *auth_id;
182 auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
183 if (match_either_id(signer_ids, auth_id))
184 key = __key_get(trusted);
186 } else if (asymmetric_key_id_same(signer_ids->id[1],
188 match_either_id(signer_ids,
190 key = __key_get(trusted);
197 if (check_dest && !key) {
198 /* See if the destination has a key that signed this one. */
199 key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
200 sig->auth_ids[1], false);
208 ret = key_validate(key);
210 ret = verify_signature(key, sig);
217 * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
218 * keys using the restrict_key information stored in the ring.
219 * @dest_keyring: Keyring being linked to.
220 * @type: The type of key being added.
221 * @payload: The payload of the new key.
222 * @trusted: A key or ring of keys that can be used to vouch for the new cert.
224 * Check the new certificate only against the key or keys passed in the data
225 * parameter. If one of those is the signing key and validates the new
226 * certificate, then mark the new certificate as being ok to link.
228 * Returns 0 if the new certificate was accepted, -ENOKEY if we
229 * couldn't find a matching parent certificate in the trusted list,
230 * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
231 * unsupported crypto, or some other error if there is a matching certificate
232 * but the signature check cannot be performed.
234 int restrict_link_by_key_or_keyring(struct key *dest_keyring,
235 const struct key_type *type,
236 const union key_payload *payload,
239 return key_or_keyring_common(dest_keyring, type, payload, trusted,
244 * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
245 * public keys using the restrict_key information stored in the ring.
246 * @dest_keyring: Keyring being linked to.
247 * @type: The type of key being added.
248 * @payload: The payload of the new key.
249 * @trusted: A key or ring of keys that can be used to vouch for the new cert.
251 * Check the new certificate only against the key or keys passed in the data
252 * parameter. If one of those is the signing key and validates the new
253 * certificate, then mark the new certificate as being ok to link.
255 * Returns 0 if the new certificate was accepted, -ENOKEY if we
256 * couldn't find a matching parent certificate in the trusted list,
257 * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
258 * unsupported crypto, or some other error if there is a matching certificate
259 * but the signature check cannot be performed.
261 int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
262 const struct key_type *type,
263 const union key_payload *payload,
266 return key_or_keyring_common(dest_keyring, type, payload, trusted,