1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2cfb664ffSDavid Howells /* Instantiate a public key crypto key from an X.509 Certificate
3cfb664ffSDavid Howells *
4a511e1afSDavid Howells * Copyright (C) 2012, 2016 Red Hat, Inc. All Rights Reserved.
5cfb664ffSDavid Howells * Written by David Howells (dhowells@redhat.com)
6cfb664ffSDavid Howells */
7cfb664ffSDavid Howells
8a511e1afSDavid Howells #define pr_fmt(fmt) "ASYM: "fmt
9cfb664ffSDavid Howells #include <linux/module.h>
10cfb664ffSDavid Howells #include <linux/kernel.h>
11cfb664ffSDavid Howells #include <linux/err.h>
12cfb664ffSDavid Howells #include <crypto/public_key.h>
13cfb664ffSDavid Howells #include "asymmetric_keys.h"
14cfb664ffSDavid Howells
15cfb664ffSDavid Howells static bool use_builtin_keys;
16cfb664ffSDavid Howells static struct asymmetric_key_id *ca_keyid;
17cfb664ffSDavid Howells
18cfb664ffSDavid Howells #ifndef MODULE
19cfb664ffSDavid Howells static struct {
20cfb664ffSDavid Howells struct asymmetric_key_id id;
21cfb664ffSDavid Howells unsigned char data[10];
22cfb664ffSDavid Howells } cakey;
23cfb664ffSDavid Howells
ca_keys_setup(char * str)24cfb664ffSDavid Howells static int __init ca_keys_setup(char *str)
25cfb664ffSDavid Howells {
26cfb664ffSDavid Howells if (!str) /* default system keyring */
27cfb664ffSDavid Howells return 1;
28cfb664ffSDavid Howells
29cfb664ffSDavid Howells if (strncmp(str, "id:", 3) == 0) {
30cfb664ffSDavid Howells struct asymmetric_key_id *p = &cakey.id;
31cfb664ffSDavid Howells size_t hexlen = (strlen(str) - 3) / 2;
32cfb664ffSDavid Howells int ret;
33cfb664ffSDavid Howells
34cfb664ffSDavid Howells if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
35cfb664ffSDavid Howells pr_err("Missing or invalid ca_keys id\n");
36cfb664ffSDavid Howells return 1;
37cfb664ffSDavid Howells }
38cfb664ffSDavid Howells
39cfb664ffSDavid Howells ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
40cfb664ffSDavid Howells if (ret < 0)
41cfb664ffSDavid Howells pr_err("Unparsable ca_keys id hex string\n");
42cfb664ffSDavid Howells else
43cfb664ffSDavid Howells ca_keyid = p; /* owner key 'id:xxxxxx' */
44cfb664ffSDavid Howells } else if (strcmp(str, "builtin") == 0) {
45cfb664ffSDavid Howells use_builtin_keys = true;
46cfb664ffSDavid Howells }
47cfb664ffSDavid Howells
48cfb664ffSDavid Howells return 1;
49cfb664ffSDavid Howells }
50cfb664ffSDavid Howells __setup("ca_keys=", ca_keys_setup);
51cfb664ffSDavid Howells #endif
52cfb664ffSDavid Howells
53a511e1afSDavid Howells /**
54a511e1afSDavid Howells * restrict_link_by_signature - Restrict additions to a ring of public keys
55aaf66c88SMat Martineau * @dest_keyring: Keyring being linked to.
56a511e1afSDavid Howells * @type: The type of key being added.
57a511e1afSDavid Howells * @payload: The payload of the new key.
58aaf66c88SMat Martineau * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
59a511e1afSDavid Howells *
60cfb664ffSDavid Howells * Check the new certificate against the ones in the trust keyring. If one of
61cfb664ffSDavid Howells * those is the signing key and validates the new certificate, then mark the
62cfb664ffSDavid Howells * new certificate as being trusted.
63cfb664ffSDavid Howells *
64a511e1afSDavid Howells * Returns 0 if the new certificate was accepted, -ENOKEY if we couldn't find a
65a511e1afSDavid Howells * matching parent certificate in the trusted list, -EKEYREJECTED if the
664b34968eSEric Biggers * signature check fails or the key is blacklisted, -ENOPKG if the signature
674b34968eSEric Biggers * uses unsupported crypto, or some other error if there is a matching
684b34968eSEric Biggers * certificate but the signature check cannot be performed.
69cfb664ffSDavid Howells */
restrict_link_by_signature(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trust_keyring)70aaf66c88SMat Martineau int restrict_link_by_signature(struct key *dest_keyring,
71a511e1afSDavid Howells const struct key_type *type,
72aaf66c88SMat Martineau const union key_payload *payload,
73aaf66c88SMat Martineau struct key *trust_keyring)
74cfb664ffSDavid Howells {
75a511e1afSDavid Howells const struct public_key_signature *sig;
76cfb664ffSDavid Howells struct key *key;
77a511e1afSDavid Howells int ret;
78cfb664ffSDavid Howells
79a511e1afSDavid Howells pr_devel("==>%s()\n", __func__);
80cfb664ffSDavid Howells
81cfb664ffSDavid Howells if (!trust_keyring)
82a511e1afSDavid Howells return -ENOKEY;
83a511e1afSDavid Howells
84a511e1afSDavid Howells if (type != &key_type_asymmetric)
85cfb664ffSDavid Howells return -EOPNOTSUPP;
86a511e1afSDavid Howells
87a511e1afSDavid Howells sig = payload->data[asym_auth];
884b34968eSEric Biggers if (!sig)
894b34968eSEric Biggers return -ENOPKG;
907d30198eSAndrew Zaborowski if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
91acddc720SMat Martineau return -ENOKEY;
92a511e1afSDavid Howells
93cfb664ffSDavid Howells if (ca_keyid && !asymmetric_key_id_partial(sig->auth_ids[1], ca_keyid))
94cfb664ffSDavid Howells return -EPERM;
95cfb664ffSDavid Howells
96a511e1afSDavid Howells /* See if we have a key that signed this one. */
97cfb664ffSDavid Howells key = find_asymmetric_key(trust_keyring,
98cfb664ffSDavid Howells sig->auth_ids[0], sig->auth_ids[1],
997d30198eSAndrew Zaborowski sig->auth_ids[2], false);
100cfb664ffSDavid Howells if (IS_ERR(key))
101a511e1afSDavid Howells return -ENOKEY;
102cfb664ffSDavid Howells
103a511e1afSDavid Howells if (use_builtin_keys && !test_bit(KEY_FLAG_BUILTIN, &key->flags))
104a511e1afSDavid Howells ret = -ENOKEY;
105a511e1afSDavid Howells else
106a511e1afSDavid Howells ret = verify_signature(key, sig);
107cfb664ffSDavid Howells key_put(key);
108cfb664ffSDavid Howells return ret;
109cfb664ffSDavid Howells }
1107e3c4d22SMat Martineau
11176adb2fbSEric Snowberg /**
11276adb2fbSEric Snowberg * restrict_link_by_ca - Restrict additions to a ring of CA keys
11376adb2fbSEric Snowberg * @dest_keyring: Keyring being linked to.
11476adb2fbSEric Snowberg * @type: The type of key being added.
11576adb2fbSEric Snowberg * @payload: The payload of the new key.
11676adb2fbSEric Snowberg * @trust_keyring: Unused.
11776adb2fbSEric Snowberg *
11876adb2fbSEric Snowberg * Check if the new certificate is a CA. If it is a CA, then mark the new
11976adb2fbSEric Snowberg * certificate as being ok to link.
12076adb2fbSEric Snowberg *
12176adb2fbSEric Snowberg * Returns 0 if the new certificate was accepted, -ENOKEY if the
12276adb2fbSEric Snowberg * certificate is not a CA. -ENOPKG if the signature uses unsupported
12376adb2fbSEric Snowberg * crypto, or some other error if there is a matching certificate but
12476adb2fbSEric Snowberg * the signature check cannot be performed.
12576adb2fbSEric Snowberg */
restrict_link_by_ca(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trust_keyring)12676adb2fbSEric Snowberg int restrict_link_by_ca(struct key *dest_keyring,
12776adb2fbSEric Snowberg const struct key_type *type,
12876adb2fbSEric Snowberg const union key_payload *payload,
12976adb2fbSEric Snowberg struct key *trust_keyring)
13076adb2fbSEric Snowberg {
13176adb2fbSEric Snowberg const struct public_key *pkey;
13276adb2fbSEric Snowberg
13376adb2fbSEric Snowberg if (type != &key_type_asymmetric)
13476adb2fbSEric Snowberg return -EOPNOTSUPP;
13576adb2fbSEric Snowberg
13676adb2fbSEric Snowberg pkey = payload->data[asym_crypto];
13776adb2fbSEric Snowberg if (!pkey)
13876adb2fbSEric Snowberg return -ENOPKG;
13976adb2fbSEric Snowberg if (!test_bit(KEY_EFLAG_CA, &pkey->key_eflags))
14076adb2fbSEric Snowberg return -ENOKEY;
14176adb2fbSEric Snowberg if (!test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags))
14276adb2fbSEric Snowberg return -ENOKEY;
143099f26f2SEric Snowberg if (!IS_ENABLED(CONFIG_INTEGRITY_CA_MACHINE_KEYRING_MAX))
144099f26f2SEric Snowberg return 0;
14576adb2fbSEric Snowberg if (test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags))
14676adb2fbSEric Snowberg return -ENOKEY;
14776adb2fbSEric Snowberg
14876adb2fbSEric Snowberg return 0;
14976adb2fbSEric Snowberg }
15076adb2fbSEric Snowberg
151*4cfb9080SEric Snowberg /**
152*4cfb9080SEric Snowberg * restrict_link_by_digsig - Restrict additions to a ring of digsig keys
153*4cfb9080SEric Snowberg * @dest_keyring: Keyring being linked to.
154*4cfb9080SEric Snowberg * @type: The type of key being added.
155*4cfb9080SEric Snowberg * @payload: The payload of the new key.
156*4cfb9080SEric Snowberg * @trust_keyring: A ring of keys that can be used to vouch for the new cert.
157*4cfb9080SEric Snowberg *
158*4cfb9080SEric Snowberg * Check if the new certificate has digitalSignature usage set. If it is,
159*4cfb9080SEric Snowberg * then mark the new certificate as being ok to link. Afterwards verify
160*4cfb9080SEric Snowberg * the new certificate against the ones in the trust_keyring.
161*4cfb9080SEric Snowberg *
162*4cfb9080SEric Snowberg * Returns 0 if the new certificate was accepted, -ENOKEY if the
163*4cfb9080SEric Snowberg * certificate is not a digsig. -ENOPKG if the signature uses unsupported
164*4cfb9080SEric Snowberg * crypto, or some other error if there is a matching certificate but
165*4cfb9080SEric Snowberg * the signature check cannot be performed.
166*4cfb9080SEric Snowberg */
restrict_link_by_digsig(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trust_keyring)167*4cfb9080SEric Snowberg int restrict_link_by_digsig(struct key *dest_keyring,
168*4cfb9080SEric Snowberg const struct key_type *type,
169*4cfb9080SEric Snowberg const union key_payload *payload,
170*4cfb9080SEric Snowberg struct key *trust_keyring)
171*4cfb9080SEric Snowberg {
172*4cfb9080SEric Snowberg const struct public_key *pkey;
173*4cfb9080SEric Snowberg
174*4cfb9080SEric Snowberg if (type != &key_type_asymmetric)
175*4cfb9080SEric Snowberg return -EOPNOTSUPP;
176*4cfb9080SEric Snowberg
177*4cfb9080SEric Snowberg pkey = payload->data[asym_crypto];
178*4cfb9080SEric Snowberg
179*4cfb9080SEric Snowberg if (!pkey)
180*4cfb9080SEric Snowberg return -ENOPKG;
181*4cfb9080SEric Snowberg
182*4cfb9080SEric Snowberg if (!test_bit(KEY_EFLAG_DIGITALSIG, &pkey->key_eflags))
183*4cfb9080SEric Snowberg return -ENOKEY;
184*4cfb9080SEric Snowberg
185*4cfb9080SEric Snowberg if (test_bit(KEY_EFLAG_CA, &pkey->key_eflags))
186*4cfb9080SEric Snowberg return -ENOKEY;
187*4cfb9080SEric Snowberg
188*4cfb9080SEric Snowberg if (test_bit(KEY_EFLAG_KEYCERTSIGN, &pkey->key_eflags))
189*4cfb9080SEric Snowberg return -ENOKEY;
190*4cfb9080SEric Snowberg
191*4cfb9080SEric Snowberg return restrict_link_by_signature(dest_keyring, type, payload,
192*4cfb9080SEric Snowberg trust_keyring);
193*4cfb9080SEric Snowberg }
194*4cfb9080SEric Snowberg
match_either_id(const struct asymmetric_key_id ** pair,const struct asymmetric_key_id * single)1957d30198eSAndrew Zaborowski static bool match_either_id(const struct asymmetric_key_id **pair,
1968e323a02SMat Martineau const struct asymmetric_key_id *single)
1978e323a02SMat Martineau {
1987d30198eSAndrew Zaborowski return (asymmetric_key_id_same(pair[0], single) ||
1997d30198eSAndrew Zaborowski asymmetric_key_id_same(pair[1], single));
2008e323a02SMat Martineau }
2018e323a02SMat Martineau
key_or_keyring_common(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trusted,bool check_dest)2028e323a02SMat Martineau static int key_or_keyring_common(struct key *dest_keyring,
2038e323a02SMat Martineau const struct key_type *type,
2048e323a02SMat Martineau const union key_payload *payload,
2058e323a02SMat Martineau struct key *trusted, bool check_dest)
2068e323a02SMat Martineau {
2078e323a02SMat Martineau const struct public_key_signature *sig;
2088e323a02SMat Martineau struct key *key = NULL;
2098e323a02SMat Martineau int ret;
2108e323a02SMat Martineau
2118e323a02SMat Martineau pr_devel("==>%s()\n", __func__);
2128e323a02SMat Martineau
2138e323a02SMat Martineau if (!dest_keyring)
2148e323a02SMat Martineau return -ENOKEY;
2158e323a02SMat Martineau else if (dest_keyring->type != &key_type_keyring)
2168e323a02SMat Martineau return -EOPNOTSUPP;
2178e323a02SMat Martineau
2188e323a02SMat Martineau if (!trusted && !check_dest)
2198e323a02SMat Martineau return -ENOKEY;
2208e323a02SMat Martineau
2218e323a02SMat Martineau if (type != &key_type_asymmetric)
2228e323a02SMat Martineau return -EOPNOTSUPP;
2238e323a02SMat Martineau
2248e323a02SMat Martineau sig = payload->data[asym_auth];
2254b34968eSEric Biggers if (!sig)
2264b34968eSEric Biggers return -ENOPKG;
2277d30198eSAndrew Zaborowski if (!sig->auth_ids[0] && !sig->auth_ids[1] && !sig->auth_ids[2])
2288e323a02SMat Martineau return -ENOKEY;
2298e323a02SMat Martineau
2308e323a02SMat Martineau if (trusted) {
2318e323a02SMat Martineau if (trusted->type == &key_type_keyring) {
2328e323a02SMat Martineau /* See if we have a key that signed this one. */
2338e323a02SMat Martineau key = find_asymmetric_key(trusted, sig->auth_ids[0],
2347d30198eSAndrew Zaborowski sig->auth_ids[1],
2357d30198eSAndrew Zaborowski sig->auth_ids[2], false);
2368e323a02SMat Martineau if (IS_ERR(key))
2378e323a02SMat Martineau key = NULL;
2388e323a02SMat Martineau } else if (trusted->type == &key_type_asymmetric) {
2397d30198eSAndrew Zaborowski const struct asymmetric_key_id **signer_ids;
2408e323a02SMat Martineau
2417d30198eSAndrew Zaborowski signer_ids = (const struct asymmetric_key_id **)
2427d30198eSAndrew Zaborowski asymmetric_key_ids(trusted)->id;
2438e323a02SMat Martineau
2448e323a02SMat Martineau /*
2458e323a02SMat Martineau * The auth_ids come from the candidate key (the
2468e323a02SMat Martineau * one that is being considered for addition to
2478e323a02SMat Martineau * dest_keyring) and identify the key that was
2488e323a02SMat Martineau * used to sign.
2498e323a02SMat Martineau *
2508e323a02SMat Martineau * The signer_ids are identifiers for the
2518e323a02SMat Martineau * signing key specified for dest_keyring.
2528e323a02SMat Martineau *
2537d30198eSAndrew Zaborowski * The first auth_id is the preferred id, 2nd and
2547d30198eSAndrew Zaborowski * 3rd are the fallbacks. If exactly one of
2557d30198eSAndrew Zaborowski * auth_ids[0] and auth_ids[1] is present, it may
2567d30198eSAndrew Zaborowski * match either signer_ids[0] or signed_ids[1].
2577d30198eSAndrew Zaborowski * If both are present the first one may match
2587d30198eSAndrew Zaborowski * either signed_id but the second one must match
2597d30198eSAndrew Zaborowski * the second signer_id. If neither of them is
2607d30198eSAndrew Zaborowski * available, auth_ids[2] is matched against
2617d30198eSAndrew Zaborowski * signer_ids[2] as a fallback.
2628e323a02SMat Martineau */
2637d30198eSAndrew Zaborowski if (!sig->auth_ids[0] && !sig->auth_ids[1]) {
2647d30198eSAndrew Zaborowski if (asymmetric_key_id_same(signer_ids[2],
2657d30198eSAndrew Zaborowski sig->auth_ids[2]))
2667d30198eSAndrew Zaborowski key = __key_get(trusted);
2677d30198eSAndrew Zaborowski
2687d30198eSAndrew Zaborowski } else if (!sig->auth_ids[0] || !sig->auth_ids[1]) {
2698e323a02SMat Martineau const struct asymmetric_key_id *auth_id;
2708e323a02SMat Martineau
2718e323a02SMat Martineau auth_id = sig->auth_ids[0] ?: sig->auth_ids[1];
2728e323a02SMat Martineau if (match_either_id(signer_ids, auth_id))
2738e323a02SMat Martineau key = __key_get(trusted);
2748e323a02SMat Martineau
2757d30198eSAndrew Zaborowski } else if (asymmetric_key_id_same(signer_ids[1],
2768e323a02SMat Martineau sig->auth_ids[1]) &&
2778e323a02SMat Martineau match_either_id(signer_ids,
2788e323a02SMat Martineau sig->auth_ids[0])) {
2798e323a02SMat Martineau key = __key_get(trusted);
2808e323a02SMat Martineau }
2818e323a02SMat Martineau } else {
2828e323a02SMat Martineau return -EOPNOTSUPP;
2838e323a02SMat Martineau }
2848e323a02SMat Martineau }
2858e323a02SMat Martineau
2868e323a02SMat Martineau if (check_dest && !key) {
2878e323a02SMat Martineau /* See if the destination has a key that signed this one. */
2888e323a02SMat Martineau key = find_asymmetric_key(dest_keyring, sig->auth_ids[0],
2897d30198eSAndrew Zaborowski sig->auth_ids[1], sig->auth_ids[2],
2907d30198eSAndrew Zaborowski false);
2918e323a02SMat Martineau if (IS_ERR(key))
2928e323a02SMat Martineau key = NULL;
2938e323a02SMat Martineau }
2948e323a02SMat Martineau
2958e323a02SMat Martineau if (!key)
2968e323a02SMat Martineau return -ENOKEY;
2978e323a02SMat Martineau
2988e323a02SMat Martineau ret = key_validate(key);
2998e323a02SMat Martineau if (ret == 0)
3008e323a02SMat Martineau ret = verify_signature(key, sig);
3018e323a02SMat Martineau
3028e323a02SMat Martineau key_put(key);
3038e323a02SMat Martineau return ret;
3048e323a02SMat Martineau }
3058e323a02SMat Martineau
3067e3c4d22SMat Martineau /**
3077e3c4d22SMat Martineau * restrict_link_by_key_or_keyring - Restrict additions to a ring of public
3087e3c4d22SMat Martineau * keys using the restrict_key information stored in the ring.
3097e3c4d22SMat Martineau * @dest_keyring: Keyring being linked to.
3107e3c4d22SMat Martineau * @type: The type of key being added.
3117e3c4d22SMat Martineau * @payload: The payload of the new key.
3127e3c4d22SMat Martineau * @trusted: A key or ring of keys that can be used to vouch for the new cert.
3137e3c4d22SMat Martineau *
3147e3c4d22SMat Martineau * Check the new certificate only against the key or keys passed in the data
3157e3c4d22SMat Martineau * parameter. If one of those is the signing key and validates the new
3167e3c4d22SMat Martineau * certificate, then mark the new certificate as being ok to link.
3177e3c4d22SMat Martineau *
3187e3c4d22SMat Martineau * Returns 0 if the new certificate was accepted, -ENOKEY if we
3197e3c4d22SMat Martineau * couldn't find a matching parent certificate in the trusted list,
3204b34968eSEric Biggers * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
3214b34968eSEric Biggers * unsupported crypto, or some other error if there is a matching certificate
3224b34968eSEric Biggers * but the signature check cannot be performed.
3237e3c4d22SMat Martineau */
restrict_link_by_key_or_keyring(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trusted)3247e3c4d22SMat Martineau int restrict_link_by_key_or_keyring(struct key *dest_keyring,
3257e3c4d22SMat Martineau const struct key_type *type,
3267e3c4d22SMat Martineau const union key_payload *payload,
3277e3c4d22SMat Martineau struct key *trusted)
3287e3c4d22SMat Martineau {
3298e323a02SMat Martineau return key_or_keyring_common(dest_keyring, type, payload, trusted,
3308e323a02SMat Martineau false);
3317e3c4d22SMat Martineau }
3327e3c4d22SMat Martineau
3338e323a02SMat Martineau /**
3348e323a02SMat Martineau * restrict_link_by_key_or_keyring_chain - Restrict additions to a ring of
3358e323a02SMat Martineau * public keys using the restrict_key information stored in the ring.
3368e323a02SMat Martineau * @dest_keyring: Keyring being linked to.
3378e323a02SMat Martineau * @type: The type of key being added.
3388e323a02SMat Martineau * @payload: The payload of the new key.
3398e323a02SMat Martineau * @trusted: A key or ring of keys that can be used to vouch for the new cert.
3408e323a02SMat Martineau *
34140d32b59SAndrew Zaborowski * Check the new certificate against the key or keys passed in the data
34240d32b59SAndrew Zaborowski * parameter and against the keys already linked to the destination keyring. If
34340d32b59SAndrew Zaborowski * one of those is the signing key and validates the new certificate, then mark
34440d32b59SAndrew Zaborowski * the new certificate as being ok to link.
3458e323a02SMat Martineau *
3468e323a02SMat Martineau * Returns 0 if the new certificate was accepted, -ENOKEY if we
3478e323a02SMat Martineau * couldn't find a matching parent certificate in the trusted list,
3484b34968eSEric Biggers * -EKEYREJECTED if the signature check fails, -ENOPKG if the signature uses
3494b34968eSEric Biggers * unsupported crypto, or some other error if there is a matching certificate
3504b34968eSEric Biggers * but the signature check cannot be performed.
3518e323a02SMat Martineau */
restrict_link_by_key_or_keyring_chain(struct key * dest_keyring,const struct key_type * type,const union key_payload * payload,struct key * trusted)3528e323a02SMat Martineau int restrict_link_by_key_or_keyring_chain(struct key *dest_keyring,
3538e323a02SMat Martineau const struct key_type *type,
3548e323a02SMat Martineau const union key_payload *payload,
3558e323a02SMat Martineau struct key *trusted)
3568e323a02SMat Martineau {
3578e323a02SMat Martineau return key_or_keyring_common(dest_keyring, type, payload, trusted,
3588e323a02SMat Martineau true);
3597e3c4d22SMat Martineau }
360