Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/* Instantiate a public key crypto key from an X.509 Certificate
  3 *
  4 * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
  5 * Written by David Howells (dhowells@redhat.com)
 
 
 
 
 
  6 */
  7
  8#define pr_fmt(fmt) "ASYM: "fmt
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/err.h>
 12#include <crypto/public_key.h>
 13#include "asymmetric_keys.h"
 14
 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
 53/**
 54 * restrict_link_by_signature - Restrict additions to a ring of public keys
 55 * @dest_keyring: Keyring being linked to.
 56 * @type: The type of key being added.
 57 * @payload: The payload of the new key.
 58 * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
 59 *
 60 * 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 *
 64 * 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
 66 * 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.
 69 */
 70int restrict_link_by_signature(struct key *dest_keyring,
 71			       const struct key_type *type,
 72			       const union key_payload *payload,
 73			       struct key *trust_keyring)
 74{
 75	const struct public_key_signature *sig;
 76	struct key *key;
 77	int ret;
 78
 79	pr_devel("==>%s()\n", __func__);
 80
 81	if (!trust_keyring)
 82		return -ENOKEY;
 83
 84	if (type != &key_type_asymmetric)
 85		return -EOPNOTSUPP;
 86
 87	sig = payload->data[asym_auth];
 88	if (!sig)
 89		return -ENOPKG;
 90	if (!sig->auth_ids[0] && !sig->auth_ids[1])
 91		return -ENOKEY;
 92
 93	if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
 94		return -EPERM;
 95
 96	/* See if we have a key that signed this one. */
 97	key = find_asymmetric_key(trust_keyring,
 98				  sig->auth_ids[0], sig->auth_ids[1],
 99				  false);
100	if (IS_ERR(key))
101		return -ENOKEY;
102
103	if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
104		ret = -ENOKEY;
105	else
106		ret = verify_signature(key, sig);
107	key_put(key);
108	return ret;
109}
110
111static 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];
141	if (!sig)
142		return -ENOPKG;
143	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
212/**
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,
226 * -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.
229 */
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{
235	return key_or_keyring_common(dest_keyring, type, payload, trusted,
236				     false);
237}
238
239/**
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 *
247 * Check the new certificate only against the key or keys passed in the data
248 * parameter. If one of those is the signing key and validates the new
249 * certificate, then mark the new certificate as being ok to link.
250 *
251 * Returns 0 if the new certificate was accepted, -ENOKEY if we
252 * couldn't find a matching parent certificate in the trusted list,
253 * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
254 * unsupported crypto, or some other error if there is a matching certificate
255 * but the signature check cannot be performed.
256 */
257int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
258					  const struct key_type *type,
259					  const union key_payload *payload,
260					  struct key *trusted)
261{
262	return key_or_keyring_common(dest_keyring, type, payload, trusted,
263				     true);
264}
v4.17
 
  1/* Instantiate a public key crypto key from an X.509 Certificate
  2 *
  3 * Copyright (C) 2012, 2016 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) "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"
 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
 57/**
 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.
 63 *
 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 *
 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.
 73 */
 74int 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)
 78{
 79	const struct public_key_signature *sig;
 80	struct key *key;
 81	int ret;
 82
 83	pr_devel("==>%s()\n", __func__);
 84
 85	if (!trust_keyring)
 86		return -ENOKEY;
 87
 88	if (type != &key_type_asymmetric)
 89		return -EOPNOTSUPP;
 90
 91	sig = payload->data[asym_auth];
 92	if (!sig)
 93		return -ENOPKG;
 94	if (!sig->auth_ids[0] && !sig->auth_ids[1])
 95		return -ENOKEY;
 96
 97	if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
 98		return -EPERM;
 99
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],
103				  false);
104	if (IS_ERR(key))
105		return -ENOKEY;
106
107	if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
108		ret = -ENOKEY;
109	else
110		ret = verify_signature(key, sig);
111	key_put(key);
112	return ret;
113}
114
115static bool match_either_id(const struct asymmetric_key_ids *pair,
116			    const struct asymmetric_key_id *single)
117{
118	return (asymmetric_key_id_same(pair->id[0], single) ||
119		asymmetric_key_id_same(pair->id[1], single));
120}
121
122static 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)
126{
127	const struct public_key_signature *sig;
128	struct key *key = NULL;
129	int ret;
130
131	pr_devel("==>%s()\n", __func__);
132
133	if (!dest_keyring)
134		return -ENOKEY;
135	else if (dest_keyring->type != &key_type_keyring)
136		return -EOPNOTSUPP;
137
138	if (!trusted && !check_dest)
139		return -ENOKEY;
140
141	if (type != &key_type_asymmetric)
142		return -EOPNOTSUPP;
143
144	sig = payload->data[asym_auth];
145	if (!sig)
146		return -ENOPKG;
147	if (!sig->auth_ids[0] && !sig->auth_ids[1])
148		return -ENOKEY;
149
150	if (trusted) {
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);
155			if (IS_ERR(key))
156				key = NULL;
157		} else if (trusted->type == &key_type_asymmetric) {
158			const struct asymmetric_key_ids *signer_ids;
159
160			signer_ids = asymmetric_key_ids(trusted);
161
162			/*
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
166			 * used to sign.
167			 *
168			 * The signer_ids are identifiers for the
169			 * signing key specified for dest_keyring.
170			 *
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.
178			 */
179			if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
180				const struct asymmetric_key_id *auth_id;
181
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);
185
186			} else if (asymmetric_key_id_same(signer_ids->id[1],
187							  sig->auth_ids[1]) &&
188				   match_either_id(signer_ids,
189						   sig->auth_ids[0])) {
190				key = __key_get(trusted);
191			}
192		} else {
193			return -EOPNOTSUPP;
194		}
195	}
196
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);
201		if (IS_ERR(key))
202			key = NULL;
203	}
204
205	if (!key)
206		return -ENOKEY;
207
208	ret = key_validate(key);
209	if (ret == 0)
210		ret = verify_signature(key, sig);
211
212	key_put(key);
213	return ret;
214}
215
216/**
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.
223 *
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.
227 *
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.
233 */
234int restrict_link_by_key_or_keyring(struct key *dest_keyring,
235				    const struct key_type *type,
236				    const union key_payload *payload,
237				    struct key *trusted)
238{
239	return key_or_keyring_common(dest_keyring, type, payload, trusted,
240				     false);
241}
242
243/**
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.
250 *
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.
254 *
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.
260 */
261int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
262					  const struct key_type *type,
263					  const union key_payload *payload,
264					  struct key *trusted)
265{
266	return key_or_keyring_common(dest_keyring, type, payload, trusted,
267				     true);
268}