Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KEYS: trusted: Add generic trusted keys framework

Current trusted keys framework is tightly coupled to use TPM device as
an underlying implementation which makes it difficult for implementations
like Trusted Execution Environment (TEE) etc. to provide trusted keys
support in case platform doesn't posses a TPM device.

Add a generic trusted keys framework where underlying implementations
can be easily plugged in. Create struct trusted_key_ops to achieve this,
which contains necessary functions of a backend.

Also, define a module parameter in order to select a particular trust
source in case a platform support multiple trust sources. In case its
not specified then implementation itetrates through trust sources list
starting with TPM and assign the first trust source as a backend which
has initiazed successfully during iteration.

Note that current implementation only supports a single trust source at
runtime which is either selectable at compile time or during boot via
aforementioned module parameter.

Suggested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>

authored by

Sumit Garg and committed by
Jarkko Sakkinen
5d0682be e5fb5d2c

+512 -333
+12
Documentation/admin-guide/kernel-parameters.txt
··· 5462 5462 See Documentation/admin-guide/mm/transhuge.rst 5463 5463 for more details. 5464 5464 5465 + trusted.source= [KEYS] 5466 + Format: <string> 5467 + This parameter identifies the trust source as a backend 5468 + for trusted keys implementation. Supported trust 5469 + sources: 5470 + - "tpm" 5471 + - "tee" 5472 + If not specified then it defaults to iterating through 5473 + the trust source list starting with TPM and assigns the 5474 + first trust source as a backend which is initialized 5475 + successfully during iteration. 5476 + 5465 5477 tsc= Disable clocksource stability checks for TSC. 5466 5478 Format: <string> 5467 5479 [x86] reliable: mark tsc clocksource as reliable, this
+53
include/keys/trusted-type.h
··· 11 11 #include <linux/rcupdate.h> 12 12 #include <linux/tpm.h> 13 13 14 + #ifdef pr_fmt 15 + #undef pr_fmt 16 + #endif 17 + 18 + #define pr_fmt(fmt) "trusted_key: " fmt 19 + 14 20 #define MIN_KEY_SIZE 32 15 21 #define MAX_KEY_SIZE 128 16 22 #define MAX_BLOB_SIZE 512 ··· 48 42 uint32_t policyhandle; 49 43 }; 50 44 45 + struct trusted_key_ops { 46 + /* 47 + * flag to indicate if trusted key implementation supports migration 48 + * or not. 49 + */ 50 + unsigned char migratable; 51 + 52 + /* Initialize key interface. */ 53 + int (*init)(void); 54 + 55 + /* Seal a key. */ 56 + int (*seal)(struct trusted_key_payload *p, char *datablob); 57 + 58 + /* Unseal a key. */ 59 + int (*unseal)(struct trusted_key_payload *p, char *datablob); 60 + 61 + /* Get a randomized key. */ 62 + int (*get_random)(unsigned char *key, size_t key_len); 63 + 64 + /* Exit key interface. */ 65 + void (*exit)(void); 66 + }; 67 + 68 + struct trusted_key_source { 69 + char *name; 70 + struct trusted_key_ops *ops; 71 + }; 72 + 51 73 extern struct key_type key_type_trusted; 74 + 75 + #define TRUSTED_DEBUG 0 76 + 77 + #if TRUSTED_DEBUG 78 + static inline void dump_payload(struct trusted_key_payload *p) 79 + { 80 + pr_info("key_len %d\n", p->key_len); 81 + print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE, 82 + 16, 1, p->key, p->key_len, 0); 83 + pr_info("bloblen %d\n", p->blob_len); 84 + print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE, 85 + 16, 1, p->blob, p->blob_len, 0); 86 + pr_info("migratable %d\n", p->migratable); 87 + } 88 + #else 89 + static inline void dump_payload(struct trusted_key_payload *p) 90 + { 91 + } 92 + #endif 52 93 53 94 #endif /* _KEYS_TRUSTED_TYPE_H */
+8 -21
include/keys/trusted_tpm.h
··· 16 16 #define LOAD32N(buffer, offset) (*(uint32_t *)&buffer[offset]) 17 17 #define LOAD16(buffer, offset) (ntohs(*(uint16_t *)&buffer[offset])) 18 18 19 + extern struct trusted_key_ops trusted_key_tpm_ops; 20 + 19 21 struct osapsess { 20 22 uint32_t handle; 21 23 unsigned char secret[SHA1_DIGEST_SIZE]; ··· 54 52 #if TPM_DEBUG 55 53 static inline void dump_options(struct trusted_key_options *o) 56 54 { 57 - pr_info("trusted_key: sealing key type %d\n", o->keytype); 58 - pr_info("trusted_key: sealing key handle %0X\n", o->keyhandle); 59 - pr_info("trusted_key: pcrlock %d\n", o->pcrlock); 60 - pr_info("trusted_key: pcrinfo %d\n", o->pcrinfo_len); 55 + pr_info("sealing key type %d\n", o->keytype); 56 + pr_info("sealing key handle %0X\n", o->keyhandle); 57 + pr_info("pcrlock %d\n", o->pcrlock); 58 + pr_info("pcrinfo %d\n", o->pcrinfo_len); 61 59 print_hex_dump(KERN_INFO, "pcrinfo ", DUMP_PREFIX_NONE, 62 60 16, 1, o->pcrinfo, o->pcrinfo_len, 0); 63 - } 64 - 65 - static inline void dump_payload(struct trusted_key_payload *p) 66 - { 67 - pr_info("trusted_key: key_len %d\n", p->key_len); 68 - print_hex_dump(KERN_INFO, "key ", DUMP_PREFIX_NONE, 69 - 16, 1, p->key, p->key_len, 0); 70 - pr_info("trusted_key: bloblen %d\n", p->blob_len); 71 - print_hex_dump(KERN_INFO, "blob ", DUMP_PREFIX_NONE, 72 - 16, 1, p->blob, p->blob_len, 0); 73 - pr_info("trusted_key: migratable %d\n", p->migratable); 74 61 } 75 62 76 63 static inline void dump_sess(struct osapsess *s) 77 64 { 78 65 print_hex_dump(KERN_INFO, "trusted-key: handle ", DUMP_PREFIX_NONE, 79 66 16, 1, &s->handle, 4, 0); 80 - pr_info("trusted-key: secret:\n"); 67 + pr_info("secret:\n"); 81 68 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 82 69 16, 1, &s->secret, SHA1_DIGEST_SIZE, 0); 83 70 pr_info("trusted-key: enonce:\n"); ··· 78 87 { 79 88 int len; 80 89 81 - pr_info("\ntrusted-key: tpm buffer\n"); 90 + pr_info("\ntpm buffer\n"); 82 91 len = LOAD32(buf, TPM_SIZE_OFFSET); 83 92 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, buf, len, 0); 84 93 } 85 94 #else 86 95 static inline void dump_options(struct trusted_key_options *o) 87 - { 88 - } 89 - 90 - static inline void dump_payload(struct trusted_key_payload *p) 91 96 { 92 97 } 93 98
+1
security/keys/trusted-keys/Makefile
··· 4 4 # 5 5 6 6 obj-$(CONFIG_TRUSTED_KEYS) += trusted.o 7 + trusted-y += trusted_core.o 7 8 trusted-y += trusted_tpm1.o 8 9 9 10 $(obj)/trusted_tpm2.o: $(obj)/tpm2key.asn1.h
+354
security/keys/trusted-keys/trusted_core.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright (C) 2010 IBM Corporation 4 + * Copyright (c) 2019-2021, Linaro Limited 5 + * 6 + * See Documentation/security/keys/trusted-encrypted.rst 7 + */ 8 + 9 + #include <keys/user-type.h> 10 + #include <keys/trusted-type.h> 11 + #include <keys/trusted_tpm.h> 12 + #include <linux/capability.h> 13 + #include <linux/err.h> 14 + #include <linux/init.h> 15 + #include <linux/key-type.h> 16 + #include <linux/module.h> 17 + #include <linux/parser.h> 18 + #include <linux/rcupdate.h> 19 + #include <linux/slab.h> 20 + #include <linux/static_call.h> 21 + #include <linux/string.h> 22 + #include <linux/uaccess.h> 23 + 24 + static char *trusted_key_source; 25 + module_param_named(source, trusted_key_source, charp, 0); 26 + MODULE_PARM_DESC(source, "Select trusted keys source (tpm or tee)"); 27 + 28 + static const struct trusted_key_source trusted_key_sources[] = { 29 + #if defined(CONFIG_TCG_TPM) 30 + { "tpm", &trusted_key_tpm_ops }, 31 + #endif 32 + }; 33 + 34 + DEFINE_STATIC_CALL_NULL(trusted_key_init, *trusted_key_sources[0].ops->init); 35 + DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal); 36 + DEFINE_STATIC_CALL_NULL(trusted_key_unseal, 37 + *trusted_key_sources[0].ops->unseal); 38 + DEFINE_STATIC_CALL_NULL(trusted_key_get_random, 39 + *trusted_key_sources[0].ops->get_random); 40 + DEFINE_STATIC_CALL_NULL(trusted_key_exit, *trusted_key_sources[0].ops->exit); 41 + static unsigned char migratable; 42 + 43 + enum { 44 + Opt_err, 45 + Opt_new, Opt_load, Opt_update, 46 + }; 47 + 48 + static const match_table_t key_tokens = { 49 + {Opt_new, "new"}, 50 + {Opt_load, "load"}, 51 + {Opt_update, "update"}, 52 + {Opt_err, NULL} 53 + }; 54 + 55 + /* 56 + * datablob_parse - parse the keyctl data and fill in the 57 + * payload structure 58 + * 59 + * On success returns 0, otherwise -EINVAL. 60 + */ 61 + static int datablob_parse(char *datablob, struct trusted_key_payload *p) 62 + { 63 + substring_t args[MAX_OPT_ARGS]; 64 + long keylen; 65 + int ret = -EINVAL; 66 + int key_cmd; 67 + char *c; 68 + 69 + /* main command */ 70 + c = strsep(&datablob, " \t"); 71 + if (!c) 72 + return -EINVAL; 73 + key_cmd = match_token(c, key_tokens, args); 74 + switch (key_cmd) { 75 + case Opt_new: 76 + /* first argument is key size */ 77 + c = strsep(&datablob, " \t"); 78 + if (!c) 79 + return -EINVAL; 80 + ret = kstrtol(c, 10, &keylen); 81 + if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE) 82 + return -EINVAL; 83 + p->key_len = keylen; 84 + ret = Opt_new; 85 + break; 86 + case Opt_load: 87 + /* first argument is sealed blob */ 88 + c = strsep(&datablob, " \t"); 89 + if (!c) 90 + return -EINVAL; 91 + p->blob_len = strlen(c) / 2; 92 + if (p->blob_len > MAX_BLOB_SIZE) 93 + return -EINVAL; 94 + ret = hex2bin(p->blob, c, p->blob_len); 95 + if (ret < 0) 96 + return -EINVAL; 97 + ret = Opt_load; 98 + break; 99 + case Opt_update: 100 + ret = Opt_update; 101 + break; 102 + case Opt_err: 103 + return -EINVAL; 104 + } 105 + return ret; 106 + } 107 + 108 + static struct trusted_key_payload *trusted_payload_alloc(struct key *key) 109 + { 110 + struct trusted_key_payload *p = NULL; 111 + int ret; 112 + 113 + ret = key_payload_reserve(key, sizeof(*p)); 114 + if (ret < 0) 115 + return p; 116 + p = kzalloc(sizeof(*p), GFP_KERNEL); 117 + 118 + p->migratable = migratable; 119 + 120 + return p; 121 + } 122 + 123 + /* 124 + * trusted_instantiate - create a new trusted key 125 + * 126 + * Unseal an existing trusted blob or, for a new key, get a 127 + * random key, then seal and create a trusted key-type key, 128 + * adding it to the specified keyring. 129 + * 130 + * On success, return 0. Otherwise return errno. 131 + */ 132 + static int trusted_instantiate(struct key *key, 133 + struct key_preparsed_payload *prep) 134 + { 135 + struct trusted_key_payload *payload = NULL; 136 + size_t datalen = prep->datalen; 137 + char *datablob; 138 + int ret = 0; 139 + int key_cmd; 140 + size_t key_len; 141 + 142 + if (datalen <= 0 || datalen > 32767 || !prep->data) 143 + return -EINVAL; 144 + 145 + datablob = kmalloc(datalen + 1, GFP_KERNEL); 146 + if (!datablob) 147 + return -ENOMEM; 148 + memcpy(datablob, prep->data, datalen); 149 + datablob[datalen] = '\0'; 150 + 151 + payload = trusted_payload_alloc(key); 152 + if (!payload) { 153 + ret = -ENOMEM; 154 + goto out; 155 + } 156 + 157 + key_cmd = datablob_parse(datablob, payload); 158 + if (key_cmd < 0) { 159 + ret = key_cmd; 160 + goto out; 161 + } 162 + 163 + dump_payload(payload); 164 + 165 + switch (key_cmd) { 166 + case Opt_load: 167 + ret = static_call(trusted_key_unseal)(payload, datablob); 168 + dump_payload(payload); 169 + if (ret < 0) 170 + pr_info("key_unseal failed (%d)\n", ret); 171 + break; 172 + case Opt_new: 173 + key_len = payload->key_len; 174 + ret = static_call(trusted_key_get_random)(payload->key, 175 + key_len); 176 + if (ret < 0) 177 + goto out; 178 + 179 + if (ret != key_len) { 180 + pr_info("key_create failed (%d)\n", ret); 181 + ret = -EIO; 182 + goto out; 183 + } 184 + 185 + ret = static_call(trusted_key_seal)(payload, datablob); 186 + if (ret < 0) 187 + pr_info("key_seal failed (%d)\n", ret); 188 + break; 189 + default: 190 + ret = -EINVAL; 191 + } 192 + out: 193 + kfree_sensitive(datablob); 194 + if (!ret) 195 + rcu_assign_keypointer(key, payload); 196 + else 197 + kfree_sensitive(payload); 198 + return ret; 199 + } 200 + 201 + static void trusted_rcu_free(struct rcu_head *rcu) 202 + { 203 + struct trusted_key_payload *p; 204 + 205 + p = container_of(rcu, struct trusted_key_payload, rcu); 206 + kfree_sensitive(p); 207 + } 208 + 209 + /* 210 + * trusted_update - reseal an existing key with new PCR values 211 + */ 212 + static int trusted_update(struct key *key, struct key_preparsed_payload *prep) 213 + { 214 + struct trusted_key_payload *p; 215 + struct trusted_key_payload *new_p; 216 + size_t datalen = prep->datalen; 217 + char *datablob; 218 + int ret = 0; 219 + 220 + if (key_is_negative(key)) 221 + return -ENOKEY; 222 + p = key->payload.data[0]; 223 + if (!p->migratable) 224 + return -EPERM; 225 + if (datalen <= 0 || datalen > 32767 || !prep->data) 226 + return -EINVAL; 227 + 228 + datablob = kmalloc(datalen + 1, GFP_KERNEL); 229 + if (!datablob) 230 + return -ENOMEM; 231 + 232 + new_p = trusted_payload_alloc(key); 233 + if (!new_p) { 234 + ret = -ENOMEM; 235 + goto out; 236 + } 237 + 238 + memcpy(datablob, prep->data, datalen); 239 + datablob[datalen] = '\0'; 240 + ret = datablob_parse(datablob, new_p); 241 + if (ret != Opt_update) { 242 + ret = -EINVAL; 243 + kfree_sensitive(new_p); 244 + goto out; 245 + } 246 + 247 + /* copy old key values, and reseal with new pcrs */ 248 + new_p->migratable = p->migratable; 249 + new_p->key_len = p->key_len; 250 + memcpy(new_p->key, p->key, p->key_len); 251 + dump_payload(p); 252 + dump_payload(new_p); 253 + 254 + ret = static_call(trusted_key_seal)(new_p, datablob); 255 + if (ret < 0) { 256 + pr_info("key_seal failed (%d)\n", ret); 257 + kfree_sensitive(new_p); 258 + goto out; 259 + } 260 + 261 + rcu_assign_keypointer(key, new_p); 262 + call_rcu(&p->rcu, trusted_rcu_free); 263 + out: 264 + kfree_sensitive(datablob); 265 + return ret; 266 + } 267 + 268 + /* 269 + * trusted_read - copy the sealed blob data to userspace in hex. 270 + * On success, return to userspace the trusted key datablob size. 271 + */ 272 + static long trusted_read(const struct key *key, char *buffer, 273 + size_t buflen) 274 + { 275 + const struct trusted_key_payload *p; 276 + char *bufp; 277 + int i; 278 + 279 + p = dereference_key_locked(key); 280 + if (!p) 281 + return -EINVAL; 282 + 283 + if (buffer && buflen >= 2 * p->blob_len) { 284 + bufp = buffer; 285 + for (i = 0; i < p->blob_len; i++) 286 + bufp = hex_byte_pack(bufp, p->blob[i]); 287 + } 288 + return 2 * p->blob_len; 289 + } 290 + 291 + /* 292 + * trusted_destroy - clear and free the key's payload 293 + */ 294 + static void trusted_destroy(struct key *key) 295 + { 296 + kfree_sensitive(key->payload.data[0]); 297 + } 298 + 299 + struct key_type key_type_trusted = { 300 + .name = "trusted", 301 + .instantiate = trusted_instantiate, 302 + .update = trusted_update, 303 + .destroy = trusted_destroy, 304 + .describe = user_describe, 305 + .read = trusted_read, 306 + }; 307 + EXPORT_SYMBOL_GPL(key_type_trusted); 308 + 309 + static int __init init_trusted(void) 310 + { 311 + int i, ret = 0; 312 + 313 + for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) { 314 + if (trusted_key_source && 315 + strncmp(trusted_key_source, trusted_key_sources[i].name, 316 + strlen(trusted_key_sources[i].name))) 317 + continue; 318 + 319 + static_call_update(trusted_key_init, 320 + trusted_key_sources[i].ops->init); 321 + static_call_update(trusted_key_seal, 322 + trusted_key_sources[i].ops->seal); 323 + static_call_update(trusted_key_unseal, 324 + trusted_key_sources[i].ops->unseal); 325 + static_call_update(trusted_key_get_random, 326 + trusted_key_sources[i].ops->get_random); 327 + static_call_update(trusted_key_exit, 328 + trusted_key_sources[i].ops->exit); 329 + migratable = trusted_key_sources[i].ops->migratable; 330 + 331 + ret = static_call(trusted_key_init)(); 332 + if (!ret) 333 + break; 334 + } 335 + 336 + /* 337 + * encrypted_keys.ko depends on successful load of this module even if 338 + * trusted key implementation is not found. 339 + */ 340 + if (ret == -ENODEV) 341 + return 0; 342 + 343 + return ret; 344 + } 345 + 346 + static void __exit cleanup_trusted(void) 347 + { 348 + static_call(trusted_key_exit)(); 349 + } 350 + 351 + late_initcall(init_trusted); 352 + module_exit(cleanup_trusted); 353 + 354 + MODULE_LICENSE("GPL");
+84 -312
security/keys/trusted-keys/trusted_tpm1.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 2 /* 3 3 * Copyright (C) 2010 IBM Corporation 4 - * 5 - * Author: 6 - * David Safford <safford@us.ibm.com> 4 + * Copyright (c) 2019-2021, Linaro Limited 7 5 * 8 6 * See Documentation/security/keys/trusted-encrypted.rst 9 7 */ 10 8 11 9 #include <crypto/hash_info.h> 12 - #include <linux/uaccess.h> 13 - #include <linux/module.h> 14 10 #include <linux/init.h> 15 11 #include <linux/slab.h> 16 12 #include <linux/parser.h> 17 13 #include <linux/string.h> 18 14 #include <linux/err.h> 19 - #include <keys/user-type.h> 20 15 #include <keys/trusted-type.h> 21 16 #include <linux/key-type.h> 22 - #include <linux/rcupdate.h> 23 17 #include <linux/crypto.h> 24 18 #include <crypto/hash.h> 25 19 #include <crypto/sha1.h> 26 - #include <linux/capability.h> 27 20 #include <linux/tpm.h> 28 21 #include <linux/tpm_command.h> 29 22 ··· 56 63 57 64 sdesc = init_sdesc(hashalg); 58 65 if (IS_ERR(sdesc)) { 59 - pr_info("trusted_key: can't alloc %s\n", hash_alg); 66 + pr_info("can't alloc %s\n", hash_alg); 60 67 return PTR_ERR(sdesc); 61 68 } 62 69 ··· 76 83 77 84 sdesc = init_sdesc(hmacalg); 78 85 if (IS_ERR(sdesc)) { 79 - pr_info("trusted_key: can't alloc %s\n", hmac_alg); 86 + pr_info("can't alloc %s\n", hmac_alg); 80 87 return PTR_ERR(sdesc); 81 88 } 82 89 ··· 129 136 130 137 sdesc = init_sdesc(hashalg); 131 138 if (IS_ERR(sdesc)) { 132 - pr_info("trusted_key: can't alloc %s\n", hash_alg); 139 + pr_info("can't alloc %s\n", hash_alg); 133 140 return PTR_ERR(sdesc); 134 141 } 135 142 ··· 205 212 206 213 sdesc = init_sdesc(hashalg); 207 214 if (IS_ERR(sdesc)) { 208 - pr_info("trusted_key: can't alloc %s\n", hash_alg); 215 + pr_info("can't alloc %s\n", hash_alg); 209 216 return PTR_ERR(sdesc); 210 217 } 211 218 ret = crypto_shash_init(&sdesc->shash); ··· 298 305 299 306 sdesc = init_sdesc(hashalg); 300 307 if (IS_ERR(sdesc)) { 301 - pr_info("trusted_key: can't alloc %s\n", hash_alg); 308 + pr_info("can't alloc %s\n", hash_alg); 302 309 return PTR_ERR(sdesc); 303 310 } 304 311 ret = crypto_shash_init(&sdesc->shash); ··· 590 597 /* sessions for unsealing key and data */ 591 598 ret = oiap(tb, &authhandle1, enonce1); 592 599 if (ret < 0) { 593 - pr_info("trusted_key: oiap failed (%d)\n", ret); 600 + pr_info("oiap failed (%d)\n", ret); 594 601 return ret; 595 602 } 596 603 ret = oiap(tb, &authhandle2, enonce2); 597 604 if (ret < 0) { 598 - pr_info("trusted_key: oiap failed (%d)\n", ret); 605 + pr_info("oiap failed (%d)\n", ret); 599 606 return ret; 600 607 } 601 608 ··· 605 612 return ret; 606 613 607 614 if (ret != TPM_NONCE_SIZE) { 608 - pr_info("trusted_key: tpm_get_random failed (%d)\n", ret); 615 + pr_info("tpm_get_random failed (%d)\n", ret); 609 616 return -EIO; 610 617 } 611 618 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE, ··· 634 641 635 642 ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE); 636 643 if (ret < 0) { 637 - pr_info("trusted_key: authhmac failed (%d)\n", ret); 644 + pr_info("authhmac failed (%d)\n", ret); 638 645 return ret; 639 646 } 640 647 ··· 646 653 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0, 647 654 0); 648 655 if (ret < 0) { 649 - pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret); 656 + pr_info("TSS_checkhmac2 failed (%d)\n", ret); 650 657 return ret; 651 658 } 652 659 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen); ··· 673 680 p->key, p->key_len + 1, p->blob, &p->blob_len, 674 681 o->blobauth, o->pcrinfo, o->pcrinfo_len); 675 682 if (ret < 0) 676 - pr_info("trusted_key: srkseal failed (%d)\n", ret); 683 + pr_info("srkseal failed (%d)\n", ret); 677 684 678 685 tpm_buf_destroy(&tb); 679 686 return ret; ··· 695 702 ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, 696 703 o->blobauth, p->key, &p->key_len); 697 704 if (ret < 0) 698 - pr_info("trusted_key: srkunseal failed (%d)\n", ret); 705 + pr_info("srkunseal failed (%d)\n", ret); 699 706 else 700 707 /* pull migratable flag out of sealed key */ 701 708 p->migratable = p->key[--p->key_len]; ··· 706 713 707 714 enum { 708 715 Opt_err, 709 - Opt_new, Opt_load, Opt_update, 710 716 Opt_keyhandle, Opt_keyauth, Opt_blobauth, 711 717 Opt_pcrinfo, Opt_pcrlock, Opt_migratable, 712 718 Opt_hash, ··· 714 722 }; 715 723 716 724 static const match_table_t key_tokens = { 717 - {Opt_new, "new"}, 718 - {Opt_load, "load"}, 719 - {Opt_update, "update"}, 720 725 {Opt_keyhandle, "keyhandle=%s"}, 721 726 {Opt_keyauth, "keyauth=%s"}, 722 727 {Opt_blobauth, "blobauth=%s"}, ··· 831 842 if (i == HASH_ALGO__LAST) 832 843 return -EINVAL; 833 844 if (!tpm2 && i != HASH_ALGO_SHA1) { 834 - pr_info("trusted_key: TPM 1.x only supports SHA-1.\n"); 845 + pr_info("TPM 1.x only supports SHA-1.\n"); 835 846 return -EINVAL; 836 847 } 837 848 break; ··· 860 871 return 0; 861 872 } 862 873 863 - /* 864 - * datablob_parse - parse the keyctl data and fill in the 865 - * payload and options structures 866 - * 867 - * On success returns 0, otherwise -EINVAL. 868 - */ 869 - static int datablob_parse(char *datablob, struct trusted_key_payload *p, 870 - struct trusted_key_options *o) 871 - { 872 - substring_t args[MAX_OPT_ARGS]; 873 - long keylen; 874 - int ret = -EINVAL; 875 - int key_cmd; 876 - char *c; 877 - 878 - /* main command */ 879 - c = strsep(&datablob, " \t"); 880 - if (!c) 881 - return -EINVAL; 882 - key_cmd = match_token(c, key_tokens, args); 883 - switch (key_cmd) { 884 - case Opt_new: 885 - /* first argument is key size */ 886 - c = strsep(&datablob, " \t"); 887 - if (!c) 888 - return -EINVAL; 889 - ret = kstrtol(c, 10, &keylen); 890 - if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE) 891 - return -EINVAL; 892 - p->key_len = keylen; 893 - ret = getoptions(datablob, p, o); 894 - if (ret < 0) 895 - return ret; 896 - ret = Opt_new; 897 - break; 898 - case Opt_load: 899 - /* first argument is sealed blob */ 900 - c = strsep(&datablob, " \t"); 901 - if (!c) 902 - return -EINVAL; 903 - p->blob_len = strlen(c) / 2; 904 - if (p->blob_len > MAX_BLOB_SIZE) 905 - return -EINVAL; 906 - ret = hex2bin(p->blob, c, p->blob_len); 907 - if (ret < 0) 908 - return -EINVAL; 909 - ret = getoptions(datablob, p, o); 910 - if (ret < 0) 911 - return ret; 912 - ret = Opt_load; 913 - break; 914 - case Opt_update: 915 - /* all arguments are options */ 916 - ret = getoptions(datablob, p, o); 917 - if (ret < 0) 918 - return ret; 919 - ret = Opt_update; 920 - break; 921 - case Opt_err: 922 - return -EINVAL; 923 - break; 924 - } 925 - return ret; 926 - } 927 - 928 874 static struct trusted_key_options *trusted_options_alloc(void) 929 875 { 930 876 struct trusted_key_options *options; ··· 880 956 return options; 881 957 } 882 958 883 - static struct trusted_key_payload *trusted_payload_alloc(struct key *key) 959 + static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob) 884 960 { 885 - struct trusted_key_payload *p = NULL; 886 - int ret; 887 - 888 - ret = key_payload_reserve(key, sizeof *p); 889 - if (ret < 0) 890 - return p; 891 - p = kzalloc(sizeof *p, GFP_KERNEL); 892 - if (p) 893 - p->migratable = 1; /* migratable by default */ 894 - return p; 895 - } 896 - 897 - /* 898 - * trusted_instantiate - create a new trusted key 899 - * 900 - * Unseal an existing trusted blob or, for a new key, get a 901 - * random key, then seal and create a trusted key-type key, 902 - * adding it to the specified keyring. 903 - * 904 - * On success, return 0. Otherwise return errno. 905 - */ 906 - static int trusted_instantiate(struct key *key, 907 - struct key_preparsed_payload *prep) 908 - { 909 - struct trusted_key_payload *payload = NULL; 910 961 struct trusted_key_options *options = NULL; 911 - size_t datalen = prep->datalen; 912 - char *datablob; 913 962 int ret = 0; 914 - int key_cmd; 915 - size_t key_len; 916 963 int tpm2; 917 964 918 965 tpm2 = tpm_is_tpm2(chip); 919 966 if (tpm2 < 0) 920 967 return tpm2; 921 968 922 - if (datalen <= 0 || datalen > 32767 || !prep->data) 923 - return -EINVAL; 924 - 925 - datablob = kmalloc(datalen + 1, GFP_KERNEL); 926 - if (!datablob) 927 - return -ENOMEM; 928 - memcpy(datablob, prep->data, datalen); 929 - datablob[datalen] = '\0'; 930 - 931 969 options = trusted_options_alloc(); 932 - if (!options) { 933 - ret = -ENOMEM; 934 - goto out; 935 - } 936 - payload = trusted_payload_alloc(key); 937 - if (!payload) { 938 - ret = -ENOMEM; 939 - goto out; 940 - } 970 + if (!options) 971 + return -ENOMEM; 941 972 942 - key_cmd = datablob_parse(datablob, payload, options); 943 - if (key_cmd < 0) { 944 - ret = key_cmd; 973 + ret = getoptions(datablob, p, options); 974 + if (ret < 0) 945 975 goto out; 946 - } 976 + dump_options(options); 947 977 948 978 if (!options->keyhandle && !tpm2) { 949 979 ret = -EINVAL; 950 980 goto out; 951 981 } 952 982 953 - dump_payload(payload); 983 + if (tpm2) 984 + ret = tpm2_seal_trusted(chip, p, options); 985 + else 986 + ret = key_seal(p, options); 987 + if (ret < 0) { 988 + pr_info("key_seal failed (%d)\n", ret); 989 + goto out; 990 + } 991 + 992 + if (options->pcrlock) { 993 + ret = pcrlock(options->pcrlock); 994 + if (ret < 0) { 995 + pr_info("pcrlock failed (%d)\n", ret); 996 + goto out; 997 + } 998 + } 999 + out: 1000 + kfree_sensitive(options); 1001 + return ret; 1002 + } 1003 + 1004 + static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob) 1005 + { 1006 + struct trusted_key_options *options = NULL; 1007 + int ret = 0; 1008 + int tpm2; 1009 + 1010 + tpm2 = tpm_is_tpm2(chip); 1011 + if (tpm2 < 0) 1012 + return tpm2; 1013 + 1014 + options = trusted_options_alloc(); 1015 + if (!options) 1016 + return -ENOMEM; 1017 + 1018 + ret = getoptions(datablob, p, options); 1019 + if (ret < 0) 1020 + goto out; 954 1021 dump_options(options); 955 1022 956 - switch (key_cmd) { 957 - case Opt_load: 958 - if (tpm2) 959 - ret = tpm2_unseal_trusted(chip, payload, options); 960 - else 961 - ret = key_unseal(payload, options); 962 - dump_payload(payload); 963 - dump_options(options); 964 - if (ret < 0) 965 - pr_info("trusted_key: key_unseal failed (%d)\n", ret); 966 - break; 967 - case Opt_new: 968 - key_len = payload->key_len; 969 - ret = tpm_get_random(chip, payload->key, key_len); 970 - if (ret < 0) 971 - goto out; 972 - 973 - if (ret != key_len) { 974 - pr_info("trusted_key: key_create failed (%d)\n", ret); 975 - ret = -EIO; 976 - goto out; 977 - } 978 - if (tpm2) 979 - ret = tpm2_seal_trusted(chip, payload, options); 980 - else 981 - ret = key_seal(payload, options); 982 - if (ret < 0) 983 - pr_info("trusted_key: key_seal failed (%d)\n", ret); 984 - break; 985 - default: 1023 + if (!options->keyhandle) { 986 1024 ret = -EINVAL; 987 1025 goto out; 988 1026 } 989 - if (!ret && options->pcrlock) 990 - ret = pcrlock(options->pcrlock); 991 - out: 992 - kfree_sensitive(datablob); 993 - kfree_sensitive(options); 994 - if (!ret) 995 - rcu_assign_keypointer(key, payload); 1027 + 1028 + if (tpm2) 1029 + ret = tpm2_unseal_trusted(chip, p, options); 996 1030 else 997 - kfree_sensitive(payload); 998 - return ret; 999 - } 1031 + ret = key_unseal(p, options); 1032 + if (ret < 0) 1033 + pr_info("key_unseal failed (%d)\n", ret); 1000 1034 1001 - static void trusted_rcu_free(struct rcu_head *rcu) 1002 - { 1003 - struct trusted_key_payload *p; 1004 - 1005 - p = container_of(rcu, struct trusted_key_payload, rcu); 1006 - kfree_sensitive(p); 1007 - } 1008 - 1009 - /* 1010 - * trusted_update - reseal an existing key with new PCR values 1011 - */ 1012 - static int trusted_update(struct key *key, struct key_preparsed_payload *prep) 1013 - { 1014 - struct trusted_key_payload *p; 1015 - struct trusted_key_payload *new_p; 1016 - struct trusted_key_options *new_o; 1017 - size_t datalen = prep->datalen; 1018 - char *datablob; 1019 - int ret = 0; 1020 - 1021 - if (key_is_negative(key)) 1022 - return -ENOKEY; 1023 - p = key->payload.data[0]; 1024 - if (!p->migratable) 1025 - return -EPERM; 1026 - if (datalen <= 0 || datalen > 32767 || !prep->data) 1027 - return -EINVAL; 1028 - 1029 - datablob = kmalloc(datalen + 1, GFP_KERNEL); 1030 - if (!datablob) 1031 - return -ENOMEM; 1032 - new_o = trusted_options_alloc(); 1033 - if (!new_o) { 1034 - ret = -ENOMEM; 1035 - goto out; 1036 - } 1037 - new_p = trusted_payload_alloc(key); 1038 - if (!new_p) { 1039 - ret = -ENOMEM; 1040 - goto out; 1041 - } 1042 - 1043 - memcpy(datablob, prep->data, datalen); 1044 - datablob[datalen] = '\0'; 1045 - ret = datablob_parse(datablob, new_p, new_o); 1046 - if (ret != Opt_update) { 1047 - ret = -EINVAL; 1048 - kfree_sensitive(new_p); 1049 - goto out; 1050 - } 1051 - 1052 - if (!new_o->keyhandle) { 1053 - ret = -EINVAL; 1054 - kfree_sensitive(new_p); 1055 - goto out; 1056 - } 1057 - 1058 - /* copy old key values, and reseal with new pcrs */ 1059 - new_p->migratable = p->migratable; 1060 - new_p->key_len = p->key_len; 1061 - memcpy(new_p->key, p->key, p->key_len); 1062 - dump_payload(p); 1063 - dump_payload(new_p); 1064 - 1065 - ret = key_seal(new_p, new_o); 1066 - if (ret < 0) { 1067 - pr_info("trusted_key: key_seal failed (%d)\n", ret); 1068 - kfree_sensitive(new_p); 1069 - goto out; 1070 - } 1071 - if (new_o->pcrlock) { 1072 - ret = pcrlock(new_o->pcrlock); 1035 + if (options->pcrlock) { 1036 + ret = pcrlock(options->pcrlock); 1073 1037 if (ret < 0) { 1074 - pr_info("trusted_key: pcrlock failed (%d)\n", ret); 1075 - kfree_sensitive(new_p); 1038 + pr_info("pcrlock failed (%d)\n", ret); 1076 1039 goto out; 1077 1040 } 1078 1041 } 1079 - rcu_assign_keypointer(key, new_p); 1080 - call_rcu(&p->rcu, trusted_rcu_free); 1081 1042 out: 1082 - kfree_sensitive(datablob); 1083 - kfree_sensitive(new_o); 1043 + kfree_sensitive(options); 1084 1044 return ret; 1085 1045 } 1086 1046 1087 - /* 1088 - * trusted_read - copy the sealed blob data to userspace in hex. 1089 - * On success, return to userspace the trusted key datablob size. 1090 - */ 1091 - static long trusted_read(const struct key *key, char *buffer, 1092 - size_t buflen) 1047 + static int trusted_tpm_get_random(unsigned char *key, size_t key_len) 1093 1048 { 1094 - const struct trusted_key_payload *p; 1095 - char *bufp; 1096 - int i; 1097 - 1098 - p = dereference_key_locked(key); 1099 - if (!p) 1100 - return -EINVAL; 1101 - 1102 - if (buffer && buflen >= 2 * p->blob_len) { 1103 - bufp = buffer; 1104 - for (i = 0; i < p->blob_len; i++) 1105 - bufp = hex_byte_pack(bufp, p->blob[i]); 1106 - } 1107 - return 2 * p->blob_len; 1049 + return tpm_get_random(chip, key, key_len); 1108 1050 } 1109 - 1110 - /* 1111 - * trusted_destroy - clear and free the key's payload 1112 - */ 1113 - static void trusted_destroy(struct key *key) 1114 - { 1115 - kfree_sensitive(key->payload.data[0]); 1116 - } 1117 - 1118 - struct key_type key_type_trusted = { 1119 - .name = "trusted", 1120 - .instantiate = trusted_instantiate, 1121 - .update = trusted_update, 1122 - .destroy = trusted_destroy, 1123 - .describe = user_describe, 1124 - .read = trusted_read, 1125 - }; 1126 - 1127 - EXPORT_SYMBOL_GPL(key_type_trusted); 1128 1051 1129 1052 static void trusted_shash_release(void) 1130 1053 { ··· 987 1216 988 1217 hmacalg = crypto_alloc_shash(hmac_alg, 0, 0); 989 1218 if (IS_ERR(hmacalg)) { 990 - pr_info("trusted_key: could not allocate crypto %s\n", 1219 + pr_info("could not allocate crypto %s\n", 991 1220 hmac_alg); 992 1221 return PTR_ERR(hmacalg); 993 1222 } 994 1223 995 1224 hashalg = crypto_alloc_shash(hash_alg, 0, 0); 996 1225 if (IS_ERR(hashalg)) { 997 - pr_info("trusted_key: could not allocate crypto %s\n", 1226 + pr_info("could not allocate crypto %s\n", 998 1227 hash_alg); 999 1228 ret = PTR_ERR(hashalg); 1000 1229 goto hashalg_fail; ··· 1022 1251 return 0; 1023 1252 } 1024 1253 1025 - static int __init init_trusted(void) 1254 + static int __init trusted_tpm_init(void) 1026 1255 { 1027 1256 int ret; 1028 1257 1029 - /* encrypted_keys.ko depends on successful load of this module even if 1030 - * TPM is not used. 1031 - */ 1032 1258 chip = tpm_default_chip(); 1033 1259 if (!chip) 1034 - return 0; 1260 + return -ENODEV; 1035 1261 1036 1262 ret = init_digests(); 1037 1263 if (ret < 0) ··· 1049 1281 return ret; 1050 1282 } 1051 1283 1052 - static void __exit cleanup_trusted(void) 1284 + static void trusted_tpm_exit(void) 1053 1285 { 1054 1286 if (chip) { 1055 1287 put_device(&chip->dev); ··· 1059 1291 } 1060 1292 } 1061 1293 1062 - late_initcall(init_trusted); 1063 - module_exit(cleanup_trusted); 1064 - 1065 - MODULE_LICENSE("GPL"); 1294 + struct trusted_key_ops trusted_key_tpm_ops = { 1295 + .migratable = 1, /* migratable by default */ 1296 + .init = trusted_tpm_init, 1297 + .seal = trusted_tpm_seal, 1298 + .unseal = trusted_tpm_unseal, 1299 + .get_random = trusted_tpm_get_random, 1300 + .exit = trusted_tpm_exit, 1301 + };