15d0682beSSumit Garg // SPDX-License-Identifier: GPL-2.0-only
25d0682beSSumit Garg /*
35d0682beSSumit Garg * Copyright (C) 2010 IBM Corporation
45d0682beSSumit Garg * Copyright (c) 2019-2021, Linaro Limited
55d0682beSSumit Garg *
65d0682beSSumit Garg * See Documentation/security/keys/trusted-encrypted.rst
75d0682beSSumit Garg */
85d0682beSSumit Garg
95d0682beSSumit Garg #include <keys/user-type.h>
105d0682beSSumit Garg #include <keys/trusted-type.h>
110a95ebc9SSumit Garg #include <keys/trusted_tee.h>
12e9c5048cSAhmad Fatoum #include <keys/trusted_caam.h>
132e8a0f40SDavid Gstir #include <keys/trusted_dcp.h>
145d0682beSSumit Garg #include <keys/trusted_tpm.h>
155d0682beSSumit Garg #include <linux/capability.h>
165d0682beSSumit Garg #include <linux/err.h>
175d0682beSSumit Garg #include <linux/init.h>
185d0682beSSumit Garg #include <linux/key-type.h>
195d0682beSSumit Garg #include <linux/module.h>
205d0682beSSumit Garg #include <linux/parser.h>
21fcd7c269SAhmad Fatoum #include <linux/random.h>
225d0682beSSumit Garg #include <linux/rcupdate.h>
235d0682beSSumit Garg #include <linux/slab.h>
245d0682beSSumit Garg #include <linux/static_call.h>
255d0682beSSumit Garg #include <linux/string.h>
265d0682beSSumit Garg #include <linux/uaccess.h>
275d0682beSSumit Garg
28fcd7c269SAhmad Fatoum static char *trusted_rng = "default";
29fcd7c269SAhmad Fatoum module_param_named(rng, trusted_rng, charp, 0);
30fcd7c269SAhmad Fatoum MODULE_PARM_DESC(rng, "Select trusted key RNG");
31fcd7c269SAhmad Fatoum
325d0682beSSumit Garg static char *trusted_key_source;
335d0682beSSumit Garg module_param_named(source, trusted_key_source, charp, 0);
342e8a0f40SDavid Gstir MODULE_PARM_DESC(source, "Select trusted keys source (tpm, tee, caam or dcp)");
355d0682beSSumit Garg
365d0682beSSumit Garg static const struct trusted_key_source trusted_key_sources[] = {
37be07858fSAhmad Fatoum #if defined(CONFIG_TRUSTED_KEYS_TPM)
385d0682beSSumit Garg { "tpm", &trusted_key_tpm_ops },
395d0682beSSumit Garg #endif
40be07858fSAhmad Fatoum #if defined(CONFIG_TRUSTED_KEYS_TEE)
410a95ebc9SSumit Garg { "tee", &trusted_key_tee_ops },
420a95ebc9SSumit Garg #endif
43e9c5048cSAhmad Fatoum #if defined(CONFIG_TRUSTED_KEYS_CAAM)
44e9c5048cSAhmad Fatoum { "caam", &trusted_key_caam_ops },
45e9c5048cSAhmad Fatoum #endif
462e8a0f40SDavid Gstir #if defined(CONFIG_TRUSTED_KEYS_DCP)
472e8a0f40SDavid Gstir { "dcp", &dcp_trusted_key_ops },
482e8a0f40SDavid Gstir #endif
495d0682beSSumit Garg };
505d0682beSSumit Garg
515d0682beSSumit Garg DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal);
525d0682beSSumit Garg DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
535d0682beSSumit Garg *trusted_key_sources[0].ops->unseal);
545d0682beSSumit Garg DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
555d0682beSSumit Garg *trusted_key_sources[0].ops->get_random);
5601bbafc6SSumit Garg static void (*trusted_key_exit)(void);
575d0682beSSumit Garg static unsigned char migratable;
585d0682beSSumit Garg
595d0682beSSumit Garg enum {
605d0682beSSumit Garg Opt_err,
615d0682beSSumit Garg Opt_new, Opt_load, Opt_update,
625d0682beSSumit Garg };
635d0682beSSumit Garg
645d0682beSSumit Garg static const match_table_t key_tokens = {
655d0682beSSumit Garg {Opt_new, "new"},
665d0682beSSumit Garg {Opt_load, "load"},
675d0682beSSumit Garg {Opt_update, "update"},
685d0682beSSumit Garg {Opt_err, NULL}
695d0682beSSumit Garg };
705d0682beSSumit Garg
715d0682beSSumit Garg /*
725d0682beSSumit Garg * datablob_parse - parse the keyctl data and fill in the
735d0682beSSumit Garg * payload structure
745d0682beSSumit Garg *
755d0682beSSumit Garg * On success returns 0, otherwise -EINVAL.
765d0682beSSumit Garg */
datablob_parse(char ** datablob,struct trusted_key_payload * p)7760dc5f1bSJames Bottomley static int datablob_parse(char **datablob, struct trusted_key_payload *p)
785d0682beSSumit Garg {
795d0682beSSumit Garg substring_t args[MAX_OPT_ARGS];
805d0682beSSumit Garg long keylen;
815d0682beSSumit Garg int ret = -EINVAL;
825d0682beSSumit Garg int key_cmd;
835d0682beSSumit Garg char *c;
845d0682beSSumit Garg
855d0682beSSumit Garg /* main command */
8660dc5f1bSJames Bottomley c = strsep(datablob, " \t");
875d0682beSSumit Garg if (!c)
885d0682beSSumit Garg return -EINVAL;
895d0682beSSumit Garg key_cmd = match_token(c, key_tokens, args);
905d0682beSSumit Garg switch (key_cmd) {
915d0682beSSumit Garg case Opt_new:
925d0682beSSumit Garg /* first argument is key size */
9360dc5f1bSJames Bottomley c = strsep(datablob, " \t");
945d0682beSSumit Garg if (!c)
955d0682beSSumit Garg return -EINVAL;
965d0682beSSumit Garg ret = kstrtol(c, 10, &keylen);
975d0682beSSumit Garg if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
985d0682beSSumit Garg return -EINVAL;
995d0682beSSumit Garg p->key_len = keylen;
1005d0682beSSumit Garg ret = Opt_new;
1015d0682beSSumit Garg break;
1025d0682beSSumit Garg case Opt_load:
1035d0682beSSumit Garg /* first argument is sealed blob */
10460dc5f1bSJames Bottomley c = strsep(datablob, " \t");
1055d0682beSSumit Garg if (!c)
1065d0682beSSumit Garg return -EINVAL;
1075d0682beSSumit Garg p->blob_len = strlen(c) / 2;
1085d0682beSSumit Garg if (p->blob_len > MAX_BLOB_SIZE)
1095d0682beSSumit Garg return -EINVAL;
1105d0682beSSumit Garg ret = hex2bin(p->blob, c, p->blob_len);
1115d0682beSSumit Garg if (ret < 0)
1125d0682beSSumit Garg return -EINVAL;
1135d0682beSSumit Garg ret = Opt_load;
1145d0682beSSumit Garg break;
1155d0682beSSumit Garg case Opt_update:
1165d0682beSSumit Garg ret = Opt_update;
1175d0682beSSumit Garg break;
1185d0682beSSumit Garg case Opt_err:
1195d0682beSSumit Garg return -EINVAL;
1205d0682beSSumit Garg }
1215d0682beSSumit Garg return ret;
1225d0682beSSumit Garg }
1235d0682beSSumit Garg
trusted_payload_alloc(struct key * key)1245d0682beSSumit Garg static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
1255d0682beSSumit Garg {
1265d0682beSSumit Garg struct trusted_key_payload *p = NULL;
1275d0682beSSumit Garg int ret;
1285d0682beSSumit Garg
1295d0682beSSumit Garg ret = key_payload_reserve(key, sizeof(*p));
1305d0682beSSumit Garg if (ret < 0)
131aec00aa0SColin Ian King goto err;
1325d0682beSSumit Garg p = kzalloc(sizeof(*p), GFP_KERNEL);
133aec00aa0SColin Ian King if (!p)
134aec00aa0SColin Ian King goto err;
1355d0682beSSumit Garg
1365d0682beSSumit Garg p->migratable = migratable;
137aec00aa0SColin Ian King err:
1385d0682beSSumit Garg return p;
1395d0682beSSumit Garg }
1405d0682beSSumit Garg
1415d0682beSSumit Garg /*
1425d0682beSSumit Garg * trusted_instantiate - create a new trusted key
1435d0682beSSumit Garg *
1445d0682beSSumit Garg * Unseal an existing trusted blob or, for a new key, get a
1455d0682beSSumit Garg * random key, then seal and create a trusted key-type key,
1465d0682beSSumit Garg * adding it to the specified keyring.
1475d0682beSSumit Garg *
1485d0682beSSumit Garg * On success, return 0. Otherwise return errno.
1495d0682beSSumit Garg */
trusted_instantiate(struct key * key,struct key_preparsed_payload * prep)1505d0682beSSumit Garg static int trusted_instantiate(struct key *key,
1515d0682beSSumit Garg struct key_preparsed_payload *prep)
1525d0682beSSumit Garg {
1535d0682beSSumit Garg struct trusted_key_payload *payload = NULL;
1545d0682beSSumit Garg size_t datalen = prep->datalen;
15560dc5f1bSJames Bottomley char *datablob, *orig_datablob;
1565d0682beSSumit Garg int ret = 0;
1575d0682beSSumit Garg int key_cmd;
1585d0682beSSumit Garg size_t key_len;
1595d0682beSSumit Garg
1605d0682beSSumit Garg if (datalen <= 0 || datalen > 32767 || !prep->data)
1615d0682beSSumit Garg return -EINVAL;
1625d0682beSSumit Garg
16360dc5f1bSJames Bottomley orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
1645d0682beSSumit Garg if (!datablob)
1655d0682beSSumit Garg return -ENOMEM;
1665d0682beSSumit Garg memcpy(datablob, prep->data, datalen);
1675d0682beSSumit Garg datablob[datalen] = '\0';
1685d0682beSSumit Garg
1695d0682beSSumit Garg payload = trusted_payload_alloc(key);
1705d0682beSSumit Garg if (!payload) {
1715d0682beSSumit Garg ret = -ENOMEM;
1725d0682beSSumit Garg goto out;
1735d0682beSSumit Garg }
1745d0682beSSumit Garg
17560dc5f1bSJames Bottomley key_cmd = datablob_parse(&datablob, payload);
1765d0682beSSumit Garg if (key_cmd < 0) {
1775d0682beSSumit Garg ret = key_cmd;
1785d0682beSSumit Garg goto out;
1795d0682beSSumit Garg }
1805d0682beSSumit Garg
1815d0682beSSumit Garg dump_payload(payload);
1825d0682beSSumit Garg
1835d0682beSSumit Garg switch (key_cmd) {
1845d0682beSSumit Garg case Opt_load:
1855d0682beSSumit Garg ret = static_call(trusted_key_unseal)(payload, datablob);
1865d0682beSSumit Garg dump_payload(payload);
1875d0682beSSumit Garg if (ret < 0)
1885d0682beSSumit Garg pr_info("key_unseal failed (%d)\n", ret);
1895d0682beSSumit Garg break;
1905d0682beSSumit Garg case Opt_new:
1915d0682beSSumit Garg key_len = payload->key_len;
1925d0682beSSumit Garg ret = static_call(trusted_key_get_random)(payload->key,
1935d0682beSSumit Garg key_len);
1945d0682beSSumit Garg if (ret < 0)
1955d0682beSSumit Garg goto out;
1965d0682beSSumit Garg
1975d0682beSSumit Garg if (ret != key_len) {
1985d0682beSSumit Garg pr_info("key_create failed (%d)\n", ret);
1995d0682beSSumit Garg ret = -EIO;
2005d0682beSSumit Garg goto out;
2015d0682beSSumit Garg }
2025d0682beSSumit Garg
2035d0682beSSumit Garg ret = static_call(trusted_key_seal)(payload, datablob);
2045d0682beSSumit Garg if (ret < 0)
2055d0682beSSumit Garg pr_info("key_seal failed (%d)\n", ret);
2065d0682beSSumit Garg break;
2075d0682beSSumit Garg default:
2085d0682beSSumit Garg ret = -EINVAL;
2095d0682beSSumit Garg }
2105d0682beSSumit Garg out:
21160dc5f1bSJames Bottomley kfree_sensitive(orig_datablob);
2125d0682beSSumit Garg if (!ret)
2135d0682beSSumit Garg rcu_assign_keypointer(key, payload);
2145d0682beSSumit Garg else
2155d0682beSSumit Garg kfree_sensitive(payload);
2165d0682beSSumit Garg return ret;
2175d0682beSSumit Garg }
2185d0682beSSumit Garg
trusted_rcu_free(struct rcu_head * rcu)2195d0682beSSumit Garg static void trusted_rcu_free(struct rcu_head *rcu)
2205d0682beSSumit Garg {
2215d0682beSSumit Garg struct trusted_key_payload *p;
2225d0682beSSumit Garg
2235d0682beSSumit Garg p = container_of(rcu, struct trusted_key_payload, rcu);
2245d0682beSSumit Garg kfree_sensitive(p);
2255d0682beSSumit Garg }
2265d0682beSSumit Garg
2275d0682beSSumit Garg /*
2285d0682beSSumit Garg * trusted_update - reseal an existing key with new PCR values
2295d0682beSSumit Garg */
trusted_update(struct key * key,struct key_preparsed_payload * prep)2305d0682beSSumit Garg static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
2315d0682beSSumit Garg {
2325d0682beSSumit Garg struct trusted_key_payload *p;
2335d0682beSSumit Garg struct trusted_key_payload *new_p;
2345d0682beSSumit Garg size_t datalen = prep->datalen;
23560dc5f1bSJames Bottomley char *datablob, *orig_datablob;
2365d0682beSSumit Garg int ret = 0;
2375d0682beSSumit Garg
2385d0682beSSumit Garg if (key_is_negative(key))
2395d0682beSSumit Garg return -ENOKEY;
2405d0682beSSumit Garg p = key->payload.data[0];
2415d0682beSSumit Garg if (!p->migratable)
2425d0682beSSumit Garg return -EPERM;
2435d0682beSSumit Garg if (datalen <= 0 || datalen > 32767 || !prep->data)
2445d0682beSSumit Garg return -EINVAL;
2455d0682beSSumit Garg
24660dc5f1bSJames Bottomley orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
2475d0682beSSumit Garg if (!datablob)
2485d0682beSSumit Garg return -ENOMEM;
2495d0682beSSumit Garg
2505d0682beSSumit Garg new_p = trusted_payload_alloc(key);
2515d0682beSSumit Garg if (!new_p) {
2525d0682beSSumit Garg ret = -ENOMEM;
2535d0682beSSumit Garg goto out;
2545d0682beSSumit Garg }
2555d0682beSSumit Garg
2565d0682beSSumit Garg memcpy(datablob, prep->data, datalen);
2575d0682beSSumit Garg datablob[datalen] = '\0';
25860dc5f1bSJames Bottomley ret = datablob_parse(&datablob, new_p);
2595d0682beSSumit Garg if (ret != Opt_update) {
2605d0682beSSumit Garg ret = -EINVAL;
2615d0682beSSumit Garg kfree_sensitive(new_p);
2625d0682beSSumit Garg goto out;
2635d0682beSSumit Garg }
2645d0682beSSumit Garg
2655d0682beSSumit Garg /* copy old key values, and reseal with new pcrs */
2665d0682beSSumit Garg new_p->migratable = p->migratable;
2675d0682beSSumit Garg new_p->key_len = p->key_len;
2685d0682beSSumit Garg memcpy(new_p->key, p->key, p->key_len);
2695d0682beSSumit Garg dump_payload(p);
2705d0682beSSumit Garg dump_payload(new_p);
2715d0682beSSumit Garg
2725d0682beSSumit Garg ret = static_call(trusted_key_seal)(new_p, datablob);
2735d0682beSSumit Garg if (ret < 0) {
2745d0682beSSumit Garg pr_info("key_seal failed (%d)\n", ret);
2755d0682beSSumit Garg kfree_sensitive(new_p);
2765d0682beSSumit Garg goto out;
2775d0682beSSumit Garg }
2785d0682beSSumit Garg
2795d0682beSSumit Garg rcu_assign_keypointer(key, new_p);
2805d0682beSSumit Garg call_rcu(&p->rcu, trusted_rcu_free);
2815d0682beSSumit Garg out:
28260dc5f1bSJames Bottomley kfree_sensitive(orig_datablob);
2835d0682beSSumit Garg return ret;
2845d0682beSSumit Garg }
2855d0682beSSumit Garg
2865d0682beSSumit Garg /*
2875d0682beSSumit Garg * trusted_read - copy the sealed blob data to userspace in hex.
2885d0682beSSumit Garg * On success, return to userspace the trusted key datablob size.
2895d0682beSSumit Garg */
trusted_read(const struct key * key,char * buffer,size_t buflen)2905d0682beSSumit Garg static long trusted_read(const struct key *key, char *buffer,
2915d0682beSSumit Garg size_t buflen)
2925d0682beSSumit Garg {
2935d0682beSSumit Garg const struct trusted_key_payload *p;
2945d0682beSSumit Garg char *bufp;
2955d0682beSSumit Garg int i;
2965d0682beSSumit Garg
2975d0682beSSumit Garg p = dereference_key_locked(key);
2985d0682beSSumit Garg if (!p)
2995d0682beSSumit Garg return -EINVAL;
3005d0682beSSumit Garg
3015d0682beSSumit Garg if (buffer && buflen >= 2 * p->blob_len) {
3025d0682beSSumit Garg bufp = buffer;
3035d0682beSSumit Garg for (i = 0; i < p->blob_len; i++)
3045d0682beSSumit Garg bufp = hex_byte_pack(bufp, p->blob[i]);
3055d0682beSSumit Garg }
3065d0682beSSumit Garg return 2 * p->blob_len;
3075d0682beSSumit Garg }
3085d0682beSSumit Garg
3095d0682beSSumit Garg /*
3105d0682beSSumit Garg * trusted_destroy - clear and free the key's payload
3115d0682beSSumit Garg */
trusted_destroy(struct key * key)3125d0682beSSumit Garg static void trusted_destroy(struct key *key)
3135d0682beSSumit Garg {
3145d0682beSSumit Garg kfree_sensitive(key->payload.data[0]);
3155d0682beSSumit Garg }
3165d0682beSSumit Garg
3175d0682beSSumit Garg struct key_type key_type_trusted = {
3185d0682beSSumit Garg .name = "trusted",
3195d0682beSSumit Garg .instantiate = trusted_instantiate,
3205d0682beSSumit Garg .update = trusted_update,
3215d0682beSSumit Garg .destroy = trusted_destroy,
3225d0682beSSumit Garg .describe = user_describe,
3235d0682beSSumit Garg .read = trusted_read,
3245d0682beSSumit Garg };
3255d0682beSSumit Garg EXPORT_SYMBOL_GPL(key_type_trusted);
3265d0682beSSumit Garg
kernel_get_random(unsigned char * key,size_t key_len)327fcd7c269SAhmad Fatoum static int kernel_get_random(unsigned char *key, size_t key_len)
328fcd7c269SAhmad Fatoum {
329fcd7c269SAhmad Fatoum return get_random_bytes_wait(key, key_len) ?: key_len;
330fcd7c269SAhmad Fatoum }
331fcd7c269SAhmad Fatoum
init_trusted(void)3325d0682beSSumit Garg static int __init init_trusted(void)
3335d0682beSSumit Garg {
334fcd7c269SAhmad Fatoum int (*get_random)(unsigned char *key, size_t key_len);
3355d0682beSSumit Garg int i, ret = 0;
3365d0682beSSumit Garg
3375d0682beSSumit Garg for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
3385d0682beSSumit Garg if (trusted_key_source &&
3395d0682beSSumit Garg strncmp(trusted_key_source, trusted_key_sources[i].name,
3405d0682beSSumit Garg strlen(trusted_key_sources[i].name)))
3415d0682beSSumit Garg continue;
3425d0682beSSumit Garg
343fcd7c269SAhmad Fatoum /*
344fcd7c269SAhmad Fatoum * We always support trusted.rng="kernel" and "default" as
345fcd7c269SAhmad Fatoum * well as trusted.rng=$trusted.source if the trust source
346fcd7c269SAhmad Fatoum * defines its own get_random callback.
347fcd7c269SAhmad Fatoum */
348fcd7c269SAhmad Fatoum get_random = trusted_key_sources[i].ops->get_random;
349fcd7c269SAhmad Fatoum if (trusted_rng && strcmp(trusted_rng, "default")) {
350fcd7c269SAhmad Fatoum if (!strcmp(trusted_rng, "kernel")) {
351fcd7c269SAhmad Fatoum get_random = kernel_get_random;
352fcd7c269SAhmad Fatoum } else if (strcmp(trusted_rng, trusted_key_sources[i].name) ||
353fcd7c269SAhmad Fatoum !get_random) {
354fcd7c269SAhmad Fatoum pr_warn("Unsupported RNG. Supported: kernel");
355fcd7c269SAhmad Fatoum if (get_random)
356fcd7c269SAhmad Fatoum pr_cont(", %s", trusted_key_sources[i].name);
357fcd7c269SAhmad Fatoum pr_cont(", default\n");
358fcd7c269SAhmad Fatoum return -EINVAL;
359fcd7c269SAhmad Fatoum }
360fcd7c269SAhmad Fatoum }
361fcd7c269SAhmad Fatoum
362fcd7c269SAhmad Fatoum if (!get_random)
363fcd7c269SAhmad Fatoum get_random = kernel_get_random;
364fcd7c269SAhmad Fatoum
36531de2873SJarkko Sakkinen ret = trusted_key_sources[i].ops->init();
36631de2873SJarkko Sakkinen if (!ret) {
36731de2873SJarkko Sakkinen static_call_update(trusted_key_seal, trusted_key_sources[i].ops->seal);
36831de2873SJarkko Sakkinen static_call_update(trusted_key_unseal, trusted_key_sources[i].ops->unseal);
36931de2873SJarkko Sakkinen static_call_update(trusted_key_get_random, get_random);
37031de2873SJarkko Sakkinen
37101bbafc6SSumit Garg trusted_key_exit = trusted_key_sources[i].ops->exit;
3725d0682beSSumit Garg migratable = trusted_key_sources[i].ops->migratable;
37331de2873SJarkko Sakkinen }
3745d0682beSSumit Garg
37531de2873SJarkko Sakkinen if (!ret || ret != -ENODEV)
3765d0682beSSumit Garg break;
3775d0682beSSumit Garg }
3785d0682beSSumit Garg
3795d0682beSSumit Garg /*
3805d0682beSSumit Garg * encrypted_keys.ko depends on successful load of this module even if
3815d0682beSSumit Garg * trusted key implementation is not found.
3825d0682beSSumit Garg */
3835d0682beSSumit Garg if (ret == -ENODEV)
3845d0682beSSumit Garg return 0;
3855d0682beSSumit Garg
3865d0682beSSumit Garg return ret;
3875d0682beSSumit Garg }
3885d0682beSSumit Garg
cleanup_trusted(void)3895d0682beSSumit Garg static void __exit cleanup_trusted(void)
3905d0682beSSumit Garg {
39101bbafc6SSumit Garg if (trusted_key_exit)
39201bbafc6SSumit Garg (*trusted_key_exit)();
3935d0682beSSumit Garg }
3945d0682beSSumit Garg
3955d0682beSSumit Garg late_initcall(init_trusted);
3965d0682beSSumit Garg module_exit(cleanup_trusted);
3975d0682beSSumit Garg
398*0a1ba365SJeff Johnson MODULE_DESCRIPTION("Trusted Key type");
3995d0682beSSumit Garg MODULE_LICENSE("GPL");
400