ipv4: fix memory leaks in ip_cmsg_send() callers
[linux/fpc-iii.git] / security / keys / trusted.c
blob0dcab20cdacdf9dbd4061fd589c872501fee5d01
1 /*
2 * Copyright (C) 2010 IBM Corporation
4 * Author:
5 * David Safford <safford@us.ibm.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, version 2 of the License.
11 * See Documentation/security/keys-trusted-encrypted.txt
14 #include <crypto/hash_info.h>
15 #include <linux/uaccess.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/slab.h>
19 #include <linux/parser.h>
20 #include <linux/string.h>
21 #include <linux/err.h>
22 #include <keys/user-type.h>
23 #include <keys/trusted-type.h>
24 #include <linux/key-type.h>
25 #include <linux/rcupdate.h>
26 #include <linux/crypto.h>
27 #include <crypto/hash.h>
28 #include <crypto/sha.h>
29 #include <linux/capability.h>
30 #include <linux/tpm.h>
31 #include <linux/tpm_command.h>
33 #include "trusted.h"
35 static const char hmac_alg[] = "hmac(sha1)";
36 static const char hash_alg[] = "sha1";
38 struct sdesc {
39 struct shash_desc shash;
40 char ctx[];
43 static struct crypto_shash *hashalg;
44 static struct crypto_shash *hmacalg;
46 static struct sdesc *init_sdesc(struct crypto_shash *alg)
48 struct sdesc *sdesc;
49 int size;
51 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
52 sdesc = kmalloc(size, GFP_KERNEL);
53 if (!sdesc)
54 return ERR_PTR(-ENOMEM);
55 sdesc->shash.tfm = alg;
56 sdesc->shash.flags = 0x0;
57 return sdesc;
60 static int TSS_sha1(const unsigned char *data, unsigned int datalen,
61 unsigned char *digest)
63 struct sdesc *sdesc;
64 int ret;
66 sdesc = init_sdesc(hashalg);
67 if (IS_ERR(sdesc)) {
68 pr_info("trusted_key: can't alloc %s\n", hash_alg);
69 return PTR_ERR(sdesc);
72 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
73 kfree(sdesc);
74 return ret;
77 static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
78 unsigned int keylen, ...)
80 struct sdesc *sdesc;
81 va_list argp;
82 unsigned int dlen;
83 unsigned char *data;
84 int ret;
86 sdesc = init_sdesc(hmacalg);
87 if (IS_ERR(sdesc)) {
88 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
89 return PTR_ERR(sdesc);
92 ret = crypto_shash_setkey(hmacalg, key, keylen);
93 if (ret < 0)
94 goto out;
95 ret = crypto_shash_init(&sdesc->shash);
96 if (ret < 0)
97 goto out;
99 va_start(argp, keylen);
100 for (;;) {
101 dlen = va_arg(argp, unsigned int);
102 if (dlen == 0)
103 break;
104 data = va_arg(argp, unsigned char *);
105 if (data == NULL) {
106 ret = -EINVAL;
107 break;
109 ret = crypto_shash_update(&sdesc->shash, data, dlen);
110 if (ret < 0)
111 break;
113 va_end(argp);
114 if (!ret)
115 ret = crypto_shash_final(&sdesc->shash, digest);
116 out:
117 kfree(sdesc);
118 return ret;
122 * calculate authorization info fields to send to TPM
124 static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
125 unsigned int keylen, unsigned char *h1,
126 unsigned char *h2, unsigned char h3, ...)
128 unsigned char paramdigest[SHA1_DIGEST_SIZE];
129 struct sdesc *sdesc;
130 unsigned int dlen;
131 unsigned char *data;
132 unsigned char c;
133 int ret;
134 va_list argp;
136 sdesc = init_sdesc(hashalg);
137 if (IS_ERR(sdesc)) {
138 pr_info("trusted_key: can't alloc %s\n", hash_alg);
139 return PTR_ERR(sdesc);
142 c = h3;
143 ret = crypto_shash_init(&sdesc->shash);
144 if (ret < 0)
145 goto out;
146 va_start(argp, h3);
147 for (;;) {
148 dlen = va_arg(argp, unsigned int);
149 if (dlen == 0)
150 break;
151 data = va_arg(argp, unsigned char *);
152 if (!data) {
153 ret = -EINVAL;
154 break;
156 ret = crypto_shash_update(&sdesc->shash, data, dlen);
157 if (ret < 0)
158 break;
160 va_end(argp);
161 if (!ret)
162 ret = crypto_shash_final(&sdesc->shash, paramdigest);
163 if (!ret)
164 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
165 paramdigest, TPM_NONCE_SIZE, h1,
166 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
167 out:
168 kfree(sdesc);
169 return ret;
173 * verify the AUTH1_COMMAND (Seal) result from TPM
175 static int TSS_checkhmac1(unsigned char *buffer,
176 const uint32_t command,
177 const unsigned char *ononce,
178 const unsigned char *key,
179 unsigned int keylen, ...)
181 uint32_t bufsize;
182 uint16_t tag;
183 uint32_t ordinal;
184 uint32_t result;
185 unsigned char *enonce;
186 unsigned char *continueflag;
187 unsigned char *authdata;
188 unsigned char testhmac[SHA1_DIGEST_SIZE];
189 unsigned char paramdigest[SHA1_DIGEST_SIZE];
190 struct sdesc *sdesc;
191 unsigned int dlen;
192 unsigned int dpos;
193 va_list argp;
194 int ret;
196 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
197 tag = LOAD16(buffer, 0);
198 ordinal = command;
199 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
200 if (tag == TPM_TAG_RSP_COMMAND)
201 return 0;
202 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
203 return -EINVAL;
204 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
205 continueflag = authdata - 1;
206 enonce = continueflag - TPM_NONCE_SIZE;
208 sdesc = init_sdesc(hashalg);
209 if (IS_ERR(sdesc)) {
210 pr_info("trusted_key: can't alloc %s\n", hash_alg);
211 return PTR_ERR(sdesc);
213 ret = crypto_shash_init(&sdesc->shash);
214 if (ret < 0)
215 goto out;
216 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
217 sizeof result);
218 if (ret < 0)
219 goto out;
220 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
221 sizeof ordinal);
222 if (ret < 0)
223 goto out;
224 va_start(argp, keylen);
225 for (;;) {
226 dlen = va_arg(argp, unsigned int);
227 if (dlen == 0)
228 break;
229 dpos = va_arg(argp, unsigned int);
230 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
231 if (ret < 0)
232 break;
234 va_end(argp);
235 if (!ret)
236 ret = crypto_shash_final(&sdesc->shash, paramdigest);
237 if (ret < 0)
238 goto out;
240 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
241 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
242 1, continueflag, 0, 0);
243 if (ret < 0)
244 goto out;
246 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
247 ret = -EINVAL;
248 out:
249 kfree(sdesc);
250 return ret;
254 * verify the AUTH2_COMMAND (unseal) result from TPM
256 static int TSS_checkhmac2(unsigned char *buffer,
257 const uint32_t command,
258 const unsigned char *ononce,
259 const unsigned char *key1,
260 unsigned int keylen1,
261 const unsigned char *key2,
262 unsigned int keylen2, ...)
264 uint32_t bufsize;
265 uint16_t tag;
266 uint32_t ordinal;
267 uint32_t result;
268 unsigned char *enonce1;
269 unsigned char *continueflag1;
270 unsigned char *authdata1;
271 unsigned char *enonce2;
272 unsigned char *continueflag2;
273 unsigned char *authdata2;
274 unsigned char testhmac1[SHA1_DIGEST_SIZE];
275 unsigned char testhmac2[SHA1_DIGEST_SIZE];
276 unsigned char paramdigest[SHA1_DIGEST_SIZE];
277 struct sdesc *sdesc;
278 unsigned int dlen;
279 unsigned int dpos;
280 va_list argp;
281 int ret;
283 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
284 tag = LOAD16(buffer, 0);
285 ordinal = command;
286 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
288 if (tag == TPM_TAG_RSP_COMMAND)
289 return 0;
290 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
291 return -EINVAL;
292 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
293 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
294 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
295 continueflag1 = authdata1 - 1;
296 continueflag2 = authdata2 - 1;
297 enonce1 = continueflag1 - TPM_NONCE_SIZE;
298 enonce2 = continueflag2 - TPM_NONCE_SIZE;
300 sdesc = init_sdesc(hashalg);
301 if (IS_ERR(sdesc)) {
302 pr_info("trusted_key: can't alloc %s\n", hash_alg);
303 return PTR_ERR(sdesc);
305 ret = crypto_shash_init(&sdesc->shash);
306 if (ret < 0)
307 goto out;
308 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
309 sizeof result);
310 if (ret < 0)
311 goto out;
312 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
313 sizeof ordinal);
314 if (ret < 0)
315 goto out;
317 va_start(argp, keylen2);
318 for (;;) {
319 dlen = va_arg(argp, unsigned int);
320 if (dlen == 0)
321 break;
322 dpos = va_arg(argp, unsigned int);
323 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
324 if (ret < 0)
325 break;
327 va_end(argp);
328 if (!ret)
329 ret = crypto_shash_final(&sdesc->shash, paramdigest);
330 if (ret < 0)
331 goto out;
333 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
334 paramdigest, TPM_NONCE_SIZE, enonce1,
335 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
336 if (ret < 0)
337 goto out;
338 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
339 ret = -EINVAL;
340 goto out;
342 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
343 paramdigest, TPM_NONCE_SIZE, enonce2,
344 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
345 if (ret < 0)
346 goto out;
347 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
348 ret = -EINVAL;
349 out:
350 kfree(sdesc);
351 return ret;
355 * For key specific tpm requests, we will generate and send our
356 * own TPM command packets using the drivers send function.
358 static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd,
359 size_t buflen)
361 int rc;
363 dump_tpm_buf(cmd);
364 rc = tpm_send(chip_num, cmd, buflen);
365 dump_tpm_buf(cmd);
366 if (rc > 0)
367 /* Can't return positive return codes values to keyctl */
368 rc = -EPERM;
369 return rc;
373 * Lock a trusted key, by extending a selected PCR.
375 * Prevents a trusted key that is sealed to PCRs from being accessed.
376 * This uses the tpm driver's extend function.
378 static int pcrlock(const int pcrnum)
380 unsigned char hash[SHA1_DIGEST_SIZE];
381 int ret;
383 if (!capable(CAP_SYS_ADMIN))
384 return -EPERM;
385 ret = tpm_get_random(TPM_ANY_NUM, hash, SHA1_DIGEST_SIZE);
386 if (ret != SHA1_DIGEST_SIZE)
387 return ret;
388 return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0;
392 * Create an object specific authorisation protocol (OSAP) session
394 static int osap(struct tpm_buf *tb, struct osapsess *s,
395 const unsigned char *key, uint16_t type, uint32_t handle)
397 unsigned char enonce[TPM_NONCE_SIZE];
398 unsigned char ononce[TPM_NONCE_SIZE];
399 int ret;
401 ret = tpm_get_random(TPM_ANY_NUM, ononce, TPM_NONCE_SIZE);
402 if (ret != TPM_NONCE_SIZE)
403 return ret;
405 INIT_BUF(tb);
406 store16(tb, TPM_TAG_RQU_COMMAND);
407 store32(tb, TPM_OSAP_SIZE);
408 store32(tb, TPM_ORD_OSAP);
409 store16(tb, type);
410 store32(tb, handle);
411 storebytes(tb, ononce, TPM_NONCE_SIZE);
413 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
414 if (ret < 0)
415 return ret;
417 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
418 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
419 TPM_NONCE_SIZE);
420 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
421 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
422 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
423 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
427 * Create an object independent authorisation protocol (oiap) session
429 static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
431 int ret;
433 INIT_BUF(tb);
434 store16(tb, TPM_TAG_RQU_COMMAND);
435 store32(tb, TPM_OIAP_SIZE);
436 store32(tb, TPM_ORD_OIAP);
437 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
438 if (ret < 0)
439 return ret;
441 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
442 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
443 TPM_NONCE_SIZE);
444 return 0;
447 struct tpm_digests {
448 unsigned char encauth[SHA1_DIGEST_SIZE];
449 unsigned char pubauth[SHA1_DIGEST_SIZE];
450 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
451 unsigned char xorhash[SHA1_DIGEST_SIZE];
452 unsigned char nonceodd[TPM_NONCE_SIZE];
456 * Have the TPM seal(encrypt) the trusted key, possibly based on
457 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
459 static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
460 uint32_t keyhandle, const unsigned char *keyauth,
461 const unsigned char *data, uint32_t datalen,
462 unsigned char *blob, uint32_t *bloblen,
463 const unsigned char *blobauth,
464 const unsigned char *pcrinfo, uint32_t pcrinfosize)
466 struct osapsess sess;
467 struct tpm_digests *td;
468 unsigned char cont;
469 uint32_t ordinal;
470 uint32_t pcrsize;
471 uint32_t datsize;
472 int sealinfosize;
473 int encdatasize;
474 int storedsize;
475 int ret;
476 int i;
478 /* alloc some work space for all the hashes */
479 td = kmalloc(sizeof *td, GFP_KERNEL);
480 if (!td)
481 return -ENOMEM;
483 /* get session for sealing key */
484 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
485 if (ret < 0)
486 goto out;
487 dump_sess(&sess);
489 /* calculate encrypted authorization value */
490 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
491 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
492 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
493 if (ret < 0)
494 goto out;
496 ret = tpm_get_random(TPM_ANY_NUM, td->nonceodd, TPM_NONCE_SIZE);
497 if (ret != TPM_NONCE_SIZE)
498 goto out;
499 ordinal = htonl(TPM_ORD_SEAL);
500 datsize = htonl(datalen);
501 pcrsize = htonl(pcrinfosize);
502 cont = 0;
504 /* encrypt data authorization key */
505 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
506 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
508 /* calculate authorization HMAC value */
509 if (pcrinfosize == 0) {
510 /* no pcr info specified */
511 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
512 sess.enonce, td->nonceodd, cont,
513 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
514 td->encauth, sizeof(uint32_t), &pcrsize,
515 sizeof(uint32_t), &datsize, datalen, data, 0,
517 } else {
518 /* pcr info specified */
519 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
520 sess.enonce, td->nonceodd, cont,
521 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
522 td->encauth, sizeof(uint32_t), &pcrsize,
523 pcrinfosize, pcrinfo, sizeof(uint32_t),
524 &datsize, datalen, data, 0, 0);
526 if (ret < 0)
527 goto out;
529 /* build and send the TPM request packet */
530 INIT_BUF(tb);
531 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
532 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
533 store32(tb, TPM_ORD_SEAL);
534 store32(tb, keyhandle);
535 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
536 store32(tb, pcrinfosize);
537 storebytes(tb, pcrinfo, pcrinfosize);
538 store32(tb, datalen);
539 storebytes(tb, data, datalen);
540 store32(tb, sess.handle);
541 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
542 store8(tb, cont);
543 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
545 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
546 if (ret < 0)
547 goto out;
549 /* calculate the size of the returned Blob */
550 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
551 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
552 sizeof(uint32_t) + sealinfosize);
553 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
554 sizeof(uint32_t) + encdatasize;
556 /* check the HMAC in the response */
557 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
558 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
561 /* copy the returned blob to caller */
562 if (!ret) {
563 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
564 *bloblen = storedsize;
566 out:
567 kfree(td);
568 return ret;
572 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
574 static int tpm_unseal(struct tpm_buf *tb,
575 uint32_t keyhandle, const unsigned char *keyauth,
576 const unsigned char *blob, int bloblen,
577 const unsigned char *blobauth,
578 unsigned char *data, unsigned int *datalen)
580 unsigned char nonceodd[TPM_NONCE_SIZE];
581 unsigned char enonce1[TPM_NONCE_SIZE];
582 unsigned char enonce2[TPM_NONCE_SIZE];
583 unsigned char authdata1[SHA1_DIGEST_SIZE];
584 unsigned char authdata2[SHA1_DIGEST_SIZE];
585 uint32_t authhandle1 = 0;
586 uint32_t authhandle2 = 0;
587 unsigned char cont = 0;
588 uint32_t ordinal;
589 uint32_t keyhndl;
590 int ret;
592 /* sessions for unsealing key and data */
593 ret = oiap(tb, &authhandle1, enonce1);
594 if (ret < 0) {
595 pr_info("trusted_key: oiap failed (%d)\n", ret);
596 return ret;
598 ret = oiap(tb, &authhandle2, enonce2);
599 if (ret < 0) {
600 pr_info("trusted_key: oiap failed (%d)\n", ret);
601 return ret;
604 ordinal = htonl(TPM_ORD_UNSEAL);
605 keyhndl = htonl(SRKHANDLE);
606 ret = tpm_get_random(TPM_ANY_NUM, nonceodd, TPM_NONCE_SIZE);
607 if (ret != TPM_NONCE_SIZE) {
608 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
609 return ret;
611 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
612 enonce1, nonceodd, cont, sizeof(uint32_t),
613 &ordinal, bloblen, blob, 0, 0);
614 if (ret < 0)
615 return ret;
616 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
617 enonce2, nonceodd, cont, sizeof(uint32_t),
618 &ordinal, bloblen, blob, 0, 0);
619 if (ret < 0)
620 return ret;
622 /* build and send TPM request packet */
623 INIT_BUF(tb);
624 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
625 store32(tb, TPM_UNSEAL_SIZE + bloblen);
626 store32(tb, TPM_ORD_UNSEAL);
627 store32(tb, keyhandle);
628 storebytes(tb, blob, bloblen);
629 store32(tb, authhandle1);
630 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
631 store8(tb, cont);
632 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
633 store32(tb, authhandle2);
634 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
635 store8(tb, cont);
636 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
638 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
639 if (ret < 0) {
640 pr_info("trusted_key: authhmac failed (%d)\n", ret);
641 return ret;
644 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
645 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
646 keyauth, SHA1_DIGEST_SIZE,
647 blobauth, SHA1_DIGEST_SIZE,
648 sizeof(uint32_t), TPM_DATA_OFFSET,
649 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
651 if (ret < 0) {
652 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
653 return ret;
655 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
656 return 0;
660 * Have the TPM seal(encrypt) the symmetric key
662 static int key_seal(struct trusted_key_payload *p,
663 struct trusted_key_options *o)
665 struct tpm_buf *tb;
666 int ret;
668 tb = kzalloc(sizeof *tb, GFP_KERNEL);
669 if (!tb)
670 return -ENOMEM;
672 /* include migratable flag at end of sealed key */
673 p->key[p->key_len] = p->migratable;
675 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
676 p->key, p->key_len + 1, p->blob, &p->blob_len,
677 o->blobauth, o->pcrinfo, o->pcrinfo_len);
678 if (ret < 0)
679 pr_info("trusted_key: srkseal failed (%d)\n", ret);
681 kfree(tb);
682 return ret;
686 * Have the TPM unseal(decrypt) the symmetric key
688 static int key_unseal(struct trusted_key_payload *p,
689 struct trusted_key_options *o)
691 struct tpm_buf *tb;
692 int ret;
694 tb = kzalloc(sizeof *tb, GFP_KERNEL);
695 if (!tb)
696 return -ENOMEM;
698 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
699 o->blobauth, p->key, &p->key_len);
700 if (ret < 0)
701 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
702 else
703 /* pull migratable flag out of sealed key */
704 p->migratable = p->key[--p->key_len];
706 kfree(tb);
707 return ret;
710 enum {
711 Opt_err = -1,
712 Opt_new, Opt_load, Opt_update,
713 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
714 Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
715 Opt_hash,
716 Opt_policydigest,
717 Opt_policyhandle,
720 static const match_table_t key_tokens = {
721 {Opt_new, "new"},
722 {Opt_load, "load"},
723 {Opt_update, "update"},
724 {Opt_keyhandle, "keyhandle=%s"},
725 {Opt_keyauth, "keyauth=%s"},
726 {Opt_blobauth, "blobauth=%s"},
727 {Opt_pcrinfo, "pcrinfo=%s"},
728 {Opt_pcrlock, "pcrlock=%s"},
729 {Opt_migratable, "migratable=%s"},
730 {Opt_hash, "hash=%s"},
731 {Opt_policydigest, "policydigest=%s"},
732 {Opt_policyhandle, "policyhandle=%s"},
733 {Opt_err, NULL}
736 /* can have zero or more token= options */
737 static int getoptions(char *c, struct trusted_key_payload *pay,
738 struct trusted_key_options *opt)
740 substring_t args[MAX_OPT_ARGS];
741 char *p = c;
742 int token;
743 int res;
744 unsigned long handle;
745 unsigned long lock;
746 unsigned long token_mask = 0;
747 int i;
748 int tpm2;
750 tpm2 = tpm_is_tpm2(TPM_ANY_NUM);
751 if (tpm2 < 0)
752 return tpm2;
754 opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
755 opt->digest_len = hash_digest_size[opt->hash];
757 while ((p = strsep(&c, " \t"))) {
758 if (*p == '\0' || *p == ' ' || *p == '\t')
759 continue;
760 token = match_token(p, key_tokens, args);
761 if (test_and_set_bit(token, &token_mask))
762 return -EINVAL;
764 switch (token) {
765 case Opt_pcrinfo:
766 opt->pcrinfo_len = strlen(args[0].from) / 2;
767 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
768 return -EINVAL;
769 res = hex2bin(opt->pcrinfo, args[0].from,
770 opt->pcrinfo_len);
771 if (res < 0)
772 return -EINVAL;
773 break;
774 case Opt_keyhandle:
775 res = kstrtoul(args[0].from, 16, &handle);
776 if (res < 0)
777 return -EINVAL;
778 opt->keytype = SEAL_keytype;
779 opt->keyhandle = handle;
780 break;
781 case Opt_keyauth:
782 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
783 return -EINVAL;
784 res = hex2bin(opt->keyauth, args[0].from,
785 SHA1_DIGEST_SIZE);
786 if (res < 0)
787 return -EINVAL;
788 break;
789 case Opt_blobauth:
790 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
791 return -EINVAL;
792 res = hex2bin(opt->blobauth, args[0].from,
793 SHA1_DIGEST_SIZE);
794 if (res < 0)
795 return -EINVAL;
796 break;
797 case Opt_migratable:
798 if (*args[0].from == '0')
799 pay->migratable = 0;
800 else
801 return -EINVAL;
802 break;
803 case Opt_pcrlock:
804 res = kstrtoul(args[0].from, 10, &lock);
805 if (res < 0)
806 return -EINVAL;
807 opt->pcrlock = lock;
808 break;
809 case Opt_hash:
810 if (test_bit(Opt_policydigest, &token_mask))
811 return -EINVAL;
812 for (i = 0; i < HASH_ALGO__LAST; i++) {
813 if (!strcmp(args[0].from, hash_algo_name[i])) {
814 opt->hash = i;
815 opt->digest_len =
816 hash_digest_size[opt->hash];
817 break;
820 if (i == HASH_ALGO__LAST)
821 return -EINVAL;
822 if (!tpm2 && i != HASH_ALGO_SHA1) {
823 pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
824 return -EINVAL;
826 break;
827 case Opt_policydigest:
828 if (!tpm2 ||
829 strlen(args[0].from) != (2 * opt->digest_len))
830 return -EINVAL;
831 res = hex2bin(opt->policydigest, args[0].from,
832 opt->digest_len);
833 if (res < 0)
834 return -EINVAL;
835 break;
836 case Opt_policyhandle:
837 if (!tpm2)
838 return -EINVAL;
839 res = kstrtoul(args[0].from, 16, &handle);
840 if (res < 0)
841 return -EINVAL;
842 opt->policyhandle = handle;
843 break;
844 default:
845 return -EINVAL;
848 return 0;
852 * datablob_parse - parse the keyctl data and fill in the
853 * payload and options structures
855 * On success returns 0, otherwise -EINVAL.
857 static int datablob_parse(char *datablob, struct trusted_key_payload *p,
858 struct trusted_key_options *o)
860 substring_t args[MAX_OPT_ARGS];
861 long keylen;
862 int ret = -EINVAL;
863 int key_cmd;
864 char *c;
866 /* main command */
867 c = strsep(&datablob, " \t");
868 if (!c)
869 return -EINVAL;
870 key_cmd = match_token(c, key_tokens, args);
871 switch (key_cmd) {
872 case Opt_new:
873 /* first argument is key size */
874 c = strsep(&datablob, " \t");
875 if (!c)
876 return -EINVAL;
877 ret = kstrtol(c, 10, &keylen);
878 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
879 return -EINVAL;
880 p->key_len = keylen;
881 ret = getoptions(datablob, p, o);
882 if (ret < 0)
883 return ret;
884 ret = Opt_new;
885 break;
886 case Opt_load:
887 /* first argument is sealed blob */
888 c = strsep(&datablob, " \t");
889 if (!c)
890 return -EINVAL;
891 p->blob_len = strlen(c) / 2;
892 if (p->blob_len > MAX_BLOB_SIZE)
893 return -EINVAL;
894 ret = hex2bin(p->blob, c, p->blob_len);
895 if (ret < 0)
896 return -EINVAL;
897 ret = getoptions(datablob, p, o);
898 if (ret < 0)
899 return ret;
900 ret = Opt_load;
901 break;
902 case Opt_update:
903 /* all arguments are options */
904 ret = getoptions(datablob, p, o);
905 if (ret < 0)
906 return ret;
907 ret = Opt_update;
908 break;
909 case Opt_err:
910 return -EINVAL;
911 break;
913 return ret;
916 static struct trusted_key_options *trusted_options_alloc(void)
918 struct trusted_key_options *options;
919 int tpm2;
921 tpm2 = tpm_is_tpm2(TPM_ANY_NUM);
922 if (tpm2 < 0)
923 return NULL;
925 options = kzalloc(sizeof *options, GFP_KERNEL);
926 if (options) {
927 /* set any non-zero defaults */
928 options->keytype = SRK_keytype;
930 if (!tpm2)
931 options->keyhandle = SRKHANDLE;
933 return options;
936 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
938 struct trusted_key_payload *p = NULL;
939 int ret;
941 ret = key_payload_reserve(key, sizeof *p);
942 if (ret < 0)
943 return p;
944 p = kzalloc(sizeof *p, GFP_KERNEL);
945 if (p)
946 p->migratable = 1; /* migratable by default */
947 return p;
951 * trusted_instantiate - create a new trusted key
953 * Unseal an existing trusted blob or, for a new key, get a
954 * random key, then seal and create a trusted key-type key,
955 * adding it to the specified keyring.
957 * On success, return 0. Otherwise return errno.
959 static int trusted_instantiate(struct key *key,
960 struct key_preparsed_payload *prep)
962 struct trusted_key_payload *payload = NULL;
963 struct trusted_key_options *options = NULL;
964 size_t datalen = prep->datalen;
965 char *datablob;
966 int ret = 0;
967 int key_cmd;
968 size_t key_len;
969 int tpm2;
971 tpm2 = tpm_is_tpm2(TPM_ANY_NUM);
972 if (tpm2 < 0)
973 return tpm2;
975 if (datalen <= 0 || datalen > 32767 || !prep->data)
976 return -EINVAL;
978 datablob = kmalloc(datalen + 1, GFP_KERNEL);
979 if (!datablob)
980 return -ENOMEM;
981 memcpy(datablob, prep->data, datalen);
982 datablob[datalen] = '\0';
984 options = trusted_options_alloc();
985 if (!options) {
986 ret = -ENOMEM;
987 goto out;
989 payload = trusted_payload_alloc(key);
990 if (!payload) {
991 ret = -ENOMEM;
992 goto out;
995 key_cmd = datablob_parse(datablob, payload, options);
996 if (key_cmd < 0) {
997 ret = key_cmd;
998 goto out;
1001 if (!options->keyhandle) {
1002 ret = -EINVAL;
1003 goto out;
1006 dump_payload(payload);
1007 dump_options(options);
1009 switch (key_cmd) {
1010 case Opt_load:
1011 if (tpm2)
1012 ret = tpm_unseal_trusted(TPM_ANY_NUM, payload, options);
1013 else
1014 ret = key_unseal(payload, options);
1015 dump_payload(payload);
1016 dump_options(options);
1017 if (ret < 0)
1018 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
1019 break;
1020 case Opt_new:
1021 key_len = payload->key_len;
1022 ret = tpm_get_random(TPM_ANY_NUM, payload->key, key_len);
1023 if (ret != key_len) {
1024 pr_info("trusted_key: key_create failed (%d)\n", ret);
1025 goto out;
1027 if (tpm2)
1028 ret = tpm_seal_trusted(TPM_ANY_NUM, payload, options);
1029 else
1030 ret = key_seal(payload, options);
1031 if (ret < 0)
1032 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1033 break;
1034 default:
1035 ret = -EINVAL;
1036 goto out;
1038 if (!ret && options->pcrlock)
1039 ret = pcrlock(options->pcrlock);
1040 out:
1041 kfree(datablob);
1042 kfree(options);
1043 if (!ret)
1044 rcu_assign_keypointer(key, payload);
1045 else
1046 kfree(payload);
1047 return ret;
1050 static void trusted_rcu_free(struct rcu_head *rcu)
1052 struct trusted_key_payload *p;
1054 p = container_of(rcu, struct trusted_key_payload, rcu);
1055 memset(p->key, 0, p->key_len);
1056 kfree(p);
1060 * trusted_update - reseal an existing key with new PCR values
1062 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
1064 struct trusted_key_payload *p;
1065 struct trusted_key_payload *new_p;
1066 struct trusted_key_options *new_o;
1067 size_t datalen = prep->datalen;
1068 char *datablob;
1069 int ret = 0;
1071 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
1072 return -ENOKEY;
1073 p = key->payload.data[0];
1074 if (!p->migratable)
1075 return -EPERM;
1076 if (datalen <= 0 || datalen > 32767 || !prep->data)
1077 return -EINVAL;
1079 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1080 if (!datablob)
1081 return -ENOMEM;
1082 new_o = trusted_options_alloc();
1083 if (!new_o) {
1084 ret = -ENOMEM;
1085 goto out;
1087 new_p = trusted_payload_alloc(key);
1088 if (!new_p) {
1089 ret = -ENOMEM;
1090 goto out;
1093 memcpy(datablob, prep->data, datalen);
1094 datablob[datalen] = '\0';
1095 ret = datablob_parse(datablob, new_p, new_o);
1096 if (ret != Opt_update) {
1097 ret = -EINVAL;
1098 kfree(new_p);
1099 goto out;
1102 if (!new_o->keyhandle) {
1103 ret = -EINVAL;
1104 kfree(new_p);
1105 goto out;
1108 /* copy old key values, and reseal with new pcrs */
1109 new_p->migratable = p->migratable;
1110 new_p->key_len = p->key_len;
1111 memcpy(new_p->key, p->key, p->key_len);
1112 dump_payload(p);
1113 dump_payload(new_p);
1115 ret = key_seal(new_p, new_o);
1116 if (ret < 0) {
1117 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1118 kfree(new_p);
1119 goto out;
1121 if (new_o->pcrlock) {
1122 ret = pcrlock(new_o->pcrlock);
1123 if (ret < 0) {
1124 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1125 kfree(new_p);
1126 goto out;
1129 rcu_assign_keypointer(key, new_p);
1130 call_rcu(&p->rcu, trusted_rcu_free);
1131 out:
1132 kfree(datablob);
1133 kfree(new_o);
1134 return ret;
1138 * trusted_read - copy the sealed blob data to userspace in hex.
1139 * On success, return to userspace the trusted key datablob size.
1141 static long trusted_read(const struct key *key, char __user *buffer,
1142 size_t buflen)
1144 struct trusted_key_payload *p;
1145 char *ascii_buf;
1146 char *bufp;
1147 int i;
1149 p = rcu_dereference_key(key);
1150 if (!p)
1151 return -EINVAL;
1152 if (!buffer || buflen <= 0)
1153 return 2 * p->blob_len;
1154 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1155 if (!ascii_buf)
1156 return -ENOMEM;
1158 bufp = ascii_buf;
1159 for (i = 0; i < p->blob_len; i++)
1160 bufp = hex_byte_pack(bufp, p->blob[i]);
1161 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1162 kfree(ascii_buf);
1163 return -EFAULT;
1165 kfree(ascii_buf);
1166 return 2 * p->blob_len;
1170 * trusted_destroy - before freeing the key, clear the decrypted data
1172 static void trusted_destroy(struct key *key)
1174 struct trusted_key_payload *p = key->payload.data[0];
1176 if (!p)
1177 return;
1178 memset(p->key, 0, p->key_len);
1179 kfree(key->payload.data[0]);
1182 struct key_type key_type_trusted = {
1183 .name = "trusted",
1184 .instantiate = trusted_instantiate,
1185 .update = trusted_update,
1186 .destroy = trusted_destroy,
1187 .describe = user_describe,
1188 .read = trusted_read,
1191 EXPORT_SYMBOL_GPL(key_type_trusted);
1193 static void trusted_shash_release(void)
1195 if (hashalg)
1196 crypto_free_shash(hashalg);
1197 if (hmacalg)
1198 crypto_free_shash(hmacalg);
1201 static int __init trusted_shash_alloc(void)
1203 int ret;
1205 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1206 if (IS_ERR(hmacalg)) {
1207 pr_info("trusted_key: could not allocate crypto %s\n",
1208 hmac_alg);
1209 return PTR_ERR(hmacalg);
1212 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1213 if (IS_ERR(hashalg)) {
1214 pr_info("trusted_key: could not allocate crypto %s\n",
1215 hash_alg);
1216 ret = PTR_ERR(hashalg);
1217 goto hashalg_fail;
1220 return 0;
1222 hashalg_fail:
1223 crypto_free_shash(hmacalg);
1224 return ret;
1227 static int __init init_trusted(void)
1229 int ret;
1231 ret = trusted_shash_alloc();
1232 if (ret < 0)
1233 return ret;
1234 ret = register_key_type(&key_type_trusted);
1235 if (ret < 0)
1236 trusted_shash_release();
1237 return ret;
1240 static void __exit cleanup_trusted(void)
1242 trusted_shash_release();
1243 unregister_key_type(&key_type_trusted);
1246 late_initcall(init_trusted);
1247 module_exit(cleanup_trusted);
1249 MODULE_LICENSE("GPL");