trusted-keys: check for NULL before using it
[deliverable/linux.git] / security / keys / trusted_defined.c
CommitLineData
d00a1c72
MZ
1/*
2 * Copyright (C) 2010 IBM Corporation
3 *
4 * Author:
5 * David Safford <safford@us.ibm.com>
6 *
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.
10 *
11 * See Documentation/keys-trusted-encrypted.txt
12 */
13
14#include <linux/uaccess.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/slab.h>
18#include <linux/parser.h>
19#include <linux/string.h>
93ae86e7 20#include <linux/err.h>
d00a1c72
MZ
21#include <keys/user-type.h>
22#include <keys/trusted-type.h>
23#include <linux/key-type.h>
24#include <linux/rcupdate.h>
25#include <linux/crypto.h>
26#include <crypto/hash.h>
27#include <crypto/sha.h>
28#include <linux/capability.h>
29#include <linux/tpm.h>
30#include <linux/tpm_command.h>
31
32#include "trusted_defined.h"
33
34static const char hmac_alg[] = "hmac(sha1)";
35static const char hash_alg[] = "sha1";
36
37struct sdesc {
38 struct shash_desc shash;
39 char ctx[];
40};
41
42static struct crypto_shash *hashalg;
43static struct crypto_shash *hmacalg;
44
45static struct sdesc *init_sdesc(struct crypto_shash *alg)
46{
47 struct sdesc *sdesc;
48 int size;
49
50 size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
51 sdesc = kmalloc(size, GFP_KERNEL);
52 if (!sdesc)
53 return ERR_PTR(-ENOMEM);
54 sdesc->shash.tfm = alg;
55 sdesc->shash.flags = 0x0;
56 return sdesc;
57}
58
1bdbb402 59static int TSS_sha1(const unsigned char *data, unsigned int datalen,
d00a1c72
MZ
60 unsigned char *digest)
61{
62 struct sdesc *sdesc;
63 int ret;
64
65 sdesc = init_sdesc(hashalg);
66 if (IS_ERR(sdesc)) {
67 pr_info("trusted_key: can't alloc %s\n", hash_alg);
68 return PTR_ERR(sdesc);
69 }
70
71 ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
72 kfree(sdesc);
73 return ret;
74}
75
76static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
1bdbb402 77 unsigned int keylen, ...)
d00a1c72
MZ
78{
79 struct sdesc *sdesc;
80 va_list argp;
81 unsigned int dlen;
82 unsigned char *data;
83 int ret;
84
85 sdesc = init_sdesc(hmacalg);
86 if (IS_ERR(sdesc)) {
87 pr_info("trusted_key: can't alloc %s\n", hmac_alg);
88 return PTR_ERR(sdesc);
89 }
90
91 ret = crypto_shash_setkey(hmacalg, key, keylen);
92 if (ret < 0)
93 goto out;
94 ret = crypto_shash_init(&sdesc->shash);
95 if (ret < 0)
96 goto out;
97
98 va_start(argp, keylen);
99 for (;;) {
100 dlen = va_arg(argp, unsigned int);
101 if (dlen == 0)
102 break;
103 data = va_arg(argp, unsigned char *);
35576eab
TH
104 if (data == NULL) {
105 ret = -EINVAL;
106 break;
107 }
d00a1c72
MZ
108 ret = crypto_shash_update(&sdesc->shash, data, dlen);
109 if (ret < 0)
35576eab 110 break;
d00a1c72
MZ
111 }
112 va_end(argp);
bc5e0af0
MZ
113 if (!ret)
114 ret = crypto_shash_final(&sdesc->shash, digest);
d00a1c72
MZ
115out:
116 kfree(sdesc);
117 return ret;
118}
119
120/*
121 * calculate authorization info fields to send to TPM
122 */
bc5e0af0 123static int TSS_authhmac(unsigned char *digest, const unsigned char *key,
1bdbb402 124 unsigned int keylen, unsigned char *h1,
bc5e0af0 125 unsigned char *h2, unsigned char h3, ...)
d00a1c72
MZ
126{
127 unsigned char paramdigest[SHA1_DIGEST_SIZE];
128 struct sdesc *sdesc;
129 unsigned int dlen;
130 unsigned char *data;
131 unsigned char c;
132 int ret;
133 va_list argp;
134
135 sdesc = init_sdesc(hashalg);
136 if (IS_ERR(sdesc)) {
137 pr_info("trusted_key: can't alloc %s\n", hash_alg);
138 return PTR_ERR(sdesc);
139 }
140
141 c = h3;
142 ret = crypto_shash_init(&sdesc->shash);
143 if (ret < 0)
144 goto out;
145 va_start(argp, h3);
146 for (;;) {
147 dlen = va_arg(argp, unsigned int);
148 if (dlen == 0)
149 break;
150 data = va_arg(argp, unsigned char *);
0e7491f6
TH
151 if (!data) {
152 ret = -EINVAL;
153 va_end(argp);
154 goto out;
155 }
d00a1c72 156 ret = crypto_shash_update(&sdesc->shash, data, dlen);
bc5e0af0
MZ
157 if (ret < 0) {
158 va_end(argp);
d00a1c72 159 goto out;
bc5e0af0 160 }
d00a1c72
MZ
161 }
162 va_end(argp);
163 ret = crypto_shash_final(&sdesc->shash, paramdigest);
164 if (!ret)
bc5e0af0
MZ
165 ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
166 paramdigest, TPM_NONCE_SIZE, h1,
167 TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
d00a1c72
MZ
168out:
169 kfree(sdesc);
170 return ret;
171}
172
173/*
174 * verify the AUTH1_COMMAND (Seal) result from TPM
175 */
bc5e0af0
MZ
176static int TSS_checkhmac1(unsigned char *buffer,
177 const uint32_t command,
178 const unsigned char *ononce,
179 const unsigned char *key,
1bdbb402 180 unsigned int keylen, ...)
d00a1c72
MZ
181{
182 uint32_t bufsize;
183 uint16_t tag;
184 uint32_t ordinal;
185 uint32_t result;
186 unsigned char *enonce;
187 unsigned char *continueflag;
188 unsigned char *authdata;
189 unsigned char testhmac[SHA1_DIGEST_SIZE];
190 unsigned char paramdigest[SHA1_DIGEST_SIZE];
191 struct sdesc *sdesc;
192 unsigned int dlen;
193 unsigned int dpos;
194 va_list argp;
195 int ret;
196
197 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
198 tag = LOAD16(buffer, 0);
199 ordinal = command;
200 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
201 if (tag == TPM_TAG_RSP_COMMAND)
202 return 0;
203 if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
204 return -EINVAL;
205 authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
206 continueflag = authdata - 1;
207 enonce = continueflag - TPM_NONCE_SIZE;
208
209 sdesc = init_sdesc(hashalg);
210 if (IS_ERR(sdesc)) {
211 pr_info("trusted_key: can't alloc %s\n", hash_alg);
212 return PTR_ERR(sdesc);
213 }
214 ret = crypto_shash_init(&sdesc->shash);
215 if (ret < 0)
216 goto out;
217 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
218 sizeof result);
219 if (ret < 0)
220 goto out;
221 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
222 sizeof ordinal);
223 if (ret < 0)
224 goto out;
225 va_start(argp, keylen);
226 for (;;) {
227 dlen = va_arg(argp, unsigned int);
228 if (dlen == 0)
229 break;
230 dpos = va_arg(argp, unsigned int);
231 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
bc5e0af0
MZ
232 if (ret < 0) {
233 va_end(argp);
d00a1c72 234 goto out;
bc5e0af0 235 }
d00a1c72
MZ
236 }
237 va_end(argp);
238 ret = crypto_shash_final(&sdesc->shash, paramdigest);
239 if (ret < 0)
240 goto out;
bc5e0af0 241
d00a1c72
MZ
242 ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
243 TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
244 1, continueflag, 0, 0);
245 if (ret < 0)
246 goto out;
bc5e0af0 247
d00a1c72
MZ
248 if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
249 ret = -EINVAL;
250out:
251 kfree(sdesc);
252 return ret;
253}
254
255/*
256 * verify the AUTH2_COMMAND (unseal) result from TPM
257 */
bc5e0af0
MZ
258static int TSS_checkhmac2(unsigned char *buffer,
259 const uint32_t command,
260 const unsigned char *ononce,
261 const unsigned char *key1,
1bdbb402 262 unsigned int keylen1,
bc5e0af0 263 const unsigned char *key2,
1bdbb402 264 unsigned int keylen2, ...)
d00a1c72
MZ
265{
266 uint32_t bufsize;
267 uint16_t tag;
268 uint32_t ordinal;
269 uint32_t result;
270 unsigned char *enonce1;
271 unsigned char *continueflag1;
272 unsigned char *authdata1;
273 unsigned char *enonce2;
274 unsigned char *continueflag2;
275 unsigned char *authdata2;
276 unsigned char testhmac1[SHA1_DIGEST_SIZE];
277 unsigned char testhmac2[SHA1_DIGEST_SIZE];
278 unsigned char paramdigest[SHA1_DIGEST_SIZE];
279 struct sdesc *sdesc;
280 unsigned int dlen;
281 unsigned int dpos;
282 va_list argp;
283 int ret;
284
285 bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
286 tag = LOAD16(buffer, 0);
287 ordinal = command;
288 result = LOAD32N(buffer, TPM_RETURN_OFFSET);
289
290 if (tag == TPM_TAG_RSP_COMMAND)
291 return 0;
292 if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
293 return -EINVAL;
294 authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
295 + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
296 authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
297 continueflag1 = authdata1 - 1;
298 continueflag2 = authdata2 - 1;
299 enonce1 = continueflag1 - TPM_NONCE_SIZE;
300 enonce2 = continueflag2 - TPM_NONCE_SIZE;
301
302 sdesc = init_sdesc(hashalg);
303 if (IS_ERR(sdesc)) {
304 pr_info("trusted_key: can't alloc %s\n", hash_alg);
305 return PTR_ERR(sdesc);
306 }
307 ret = crypto_shash_init(&sdesc->shash);
308 if (ret < 0)
309 goto out;
310 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
311 sizeof result);
312 if (ret < 0)
313 goto out;
314 ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
315 sizeof ordinal);
316 if (ret < 0)
317 goto out;
318
319 va_start(argp, keylen2);
320 for (;;) {
321 dlen = va_arg(argp, unsigned int);
322 if (dlen == 0)
323 break;
324 dpos = va_arg(argp, unsigned int);
325 ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
bc5e0af0
MZ
326 if (ret < 0) {
327 va_end(argp);
d00a1c72 328 goto out;
bc5e0af0 329 }
d00a1c72 330 }
bc5e0af0 331 va_end(argp);
d00a1c72
MZ
332 ret = crypto_shash_final(&sdesc->shash, paramdigest);
333 if (ret < 0)
334 goto out;
335
336 ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
337 paramdigest, TPM_NONCE_SIZE, enonce1,
338 TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
bc5e0af0
MZ
339 if (ret < 0)
340 goto out;
d00a1c72
MZ
341 if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
342 ret = -EINVAL;
343 goto out;
344 }
345 ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
346 paramdigest, TPM_NONCE_SIZE, enonce2,
347 TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
bc5e0af0
MZ
348 if (ret < 0)
349 goto out;
d00a1c72
MZ
350 if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
351 ret = -EINVAL;
352out:
353 kfree(sdesc);
354 return ret;
355}
356
357/*
358 * For key specific tpm requests, we will generate and send our
359 * own TPM command packets using the drivers send function.
360 */
361static int trusted_tpm_send(const u32 chip_num, unsigned char *cmd,
362 size_t buflen)
363{
364 int rc;
365
366 dump_tpm_buf(cmd);
367 rc = tpm_send(chip_num, cmd, buflen);
368 dump_tpm_buf(cmd);
369 if (rc > 0)
370 /* Can't return positive return codes values to keyctl */
371 rc = -EPERM;
372 return rc;
373}
374
375/*
376 * get a random value from TPM
377 */
378static int tpm_get_random(struct tpm_buf *tb, unsigned char *buf, uint32_t len)
379{
380 int ret;
381
382 INIT_BUF(tb);
383 store16(tb, TPM_TAG_RQU_COMMAND);
384 store32(tb, TPM_GETRANDOM_SIZE);
385 store32(tb, TPM_ORD_GETRANDOM);
386 store32(tb, len);
387 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, sizeof tb->data);
bc5e0af0
MZ
388 if (!ret)
389 memcpy(buf, tb->data + TPM_GETRANDOM_SIZE, len);
d00a1c72
MZ
390 return ret;
391}
392
393static int my_get_random(unsigned char *buf, int len)
394{
395 struct tpm_buf *tb;
396 int ret;
397
1bdbb402 398 tb = kmalloc(sizeof *tb, GFP_KERNEL);
d00a1c72
MZ
399 if (!tb)
400 return -ENOMEM;
401 ret = tpm_get_random(tb, buf, len);
402
403 kfree(tb);
404 return ret;
405}
406
407/*
408 * Lock a trusted key, by extending a selected PCR.
409 *
410 * Prevents a trusted key that is sealed to PCRs from being accessed.
411 * This uses the tpm driver's extend function.
412 */
413static int pcrlock(const int pcrnum)
414{
415 unsigned char hash[SHA1_DIGEST_SIZE];
bc5e0af0 416 int ret;
d00a1c72
MZ
417
418 if (!capable(CAP_SYS_ADMIN))
419 return -EPERM;
bc5e0af0
MZ
420 ret = my_get_random(hash, SHA1_DIGEST_SIZE);
421 if (ret < 0)
422 return ret;
d00a1c72
MZ
423 return tpm_pcr_extend(TPM_ANY_NUM, pcrnum, hash) ? -EINVAL : 0;
424}
425
426/*
427 * Create an object specific authorisation protocol (OSAP) session
428 */
429static int osap(struct tpm_buf *tb, struct osapsess *s,
1bdbb402 430 const unsigned char *key, uint16_t type, uint32_t handle)
d00a1c72
MZ
431{
432 unsigned char enonce[TPM_NONCE_SIZE];
433 unsigned char ononce[TPM_NONCE_SIZE];
434 int ret;
435
436 ret = tpm_get_random(tb, ononce, TPM_NONCE_SIZE);
437 if (ret < 0)
438 return ret;
439
440 INIT_BUF(tb);
441 store16(tb, TPM_TAG_RQU_COMMAND);
442 store32(tb, TPM_OSAP_SIZE);
443 store32(tb, TPM_ORD_OSAP);
444 store16(tb, type);
445 store32(tb, handle);
446 storebytes(tb, ononce, TPM_NONCE_SIZE);
447
448 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
449 if (ret < 0)
450 return ret;
451
452 s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
453 memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
454 TPM_NONCE_SIZE);
455 memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
456 TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
bc5e0af0
MZ
457 return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
458 enonce, TPM_NONCE_SIZE, ononce, 0, 0);
d00a1c72
MZ
459}
460
461/*
462 * Create an object independent authorisation protocol (oiap) session
463 */
464static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
465{
466 int ret;
467
468 INIT_BUF(tb);
469 store16(tb, TPM_TAG_RQU_COMMAND);
470 store32(tb, TPM_OIAP_SIZE);
471 store32(tb, TPM_ORD_OIAP);
472 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
473 if (ret < 0)
474 return ret;
475
476 *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
477 memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
478 TPM_NONCE_SIZE);
bc5e0af0 479 return 0;
d00a1c72
MZ
480}
481
482struct tpm_digests {
483 unsigned char encauth[SHA1_DIGEST_SIZE];
484 unsigned char pubauth[SHA1_DIGEST_SIZE];
485 unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
486 unsigned char xorhash[SHA1_DIGEST_SIZE];
487 unsigned char nonceodd[TPM_NONCE_SIZE];
488};
489
490/*
491 * Have the TPM seal(encrypt) the trusted key, possibly based on
492 * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
493 */
1bdbb402
MZ
494static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
495 uint32_t keyhandle, const unsigned char *keyauth,
496 const unsigned char *data, uint32_t datalen,
d00a1c72
MZ
497 unsigned char *blob, uint32_t *bloblen,
498 const unsigned char *blobauth,
1bdbb402 499 const unsigned char *pcrinfo, uint32_t pcrinfosize)
d00a1c72
MZ
500{
501 struct osapsess sess;
502 struct tpm_digests *td;
503 unsigned char cont;
504 uint32_t ordinal;
505 uint32_t pcrsize;
506 uint32_t datsize;
507 int sealinfosize;
508 int encdatasize;
509 int storedsize;
510 int ret;
511 int i;
512
513 /* alloc some work space for all the hashes */
514 td = kmalloc(sizeof *td, GFP_KERNEL);
515 if (!td)
516 return -ENOMEM;
517
518 /* get session for sealing key */
519 ret = osap(tb, &sess, keyauth, keytype, keyhandle);
520 if (ret < 0)
40c10017 521 goto out;
d00a1c72
MZ
522 dump_sess(&sess);
523
524 /* calculate encrypted authorization value */
525 memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
526 memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
527 ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
528 if (ret < 0)
40c10017 529 goto out;
d00a1c72
MZ
530
531 ret = tpm_get_random(tb, td->nonceodd, TPM_NONCE_SIZE);
532 if (ret < 0)
40c10017 533 goto out;
d00a1c72
MZ
534 ordinal = htonl(TPM_ORD_SEAL);
535 datsize = htonl(datalen);
536 pcrsize = htonl(pcrinfosize);
537 cont = 0;
538
539 /* encrypt data authorization key */
540 for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
541 td->encauth[i] = td->xorhash[i] ^ blobauth[i];
542
543 /* calculate authorization HMAC value */
544 if (pcrinfosize == 0) {
545 /* no pcr info specified */
bc5e0af0
MZ
546 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
547 sess.enonce, td->nonceodd, cont,
548 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
549 td->encauth, sizeof(uint32_t), &pcrsize,
550 sizeof(uint32_t), &datsize, datalen, data, 0,
551 0);
d00a1c72
MZ
552 } else {
553 /* pcr info specified */
bc5e0af0
MZ
554 ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
555 sess.enonce, td->nonceodd, cont,
556 sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
557 td->encauth, sizeof(uint32_t), &pcrsize,
558 pcrinfosize, pcrinfo, sizeof(uint32_t),
559 &datsize, datalen, data, 0, 0);
d00a1c72 560 }
bc5e0af0 561 if (ret < 0)
40c10017 562 goto out;
d00a1c72
MZ
563
564 /* build and send the TPM request packet */
565 INIT_BUF(tb);
566 store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
567 store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
568 store32(tb, TPM_ORD_SEAL);
569 store32(tb, keyhandle);
570 storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
571 store32(tb, pcrinfosize);
572 storebytes(tb, pcrinfo, pcrinfosize);
573 store32(tb, datalen);
574 storebytes(tb, data, datalen);
575 store32(tb, sess.handle);
576 storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
577 store8(tb, cont);
578 storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
579
580 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
581 if (ret < 0)
40c10017 582 goto out;
d00a1c72
MZ
583
584 /* calculate the size of the returned Blob */
585 sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
586 encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
587 sizeof(uint32_t) + sealinfosize);
588 storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
589 sizeof(uint32_t) + encdatasize;
590
591 /* check the HMAC in the response */
592 ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
593 SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
594 0);
595
596 /* copy the returned blob to caller */
bc5e0af0
MZ
597 if (!ret) {
598 memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
599 *bloblen = storedsize;
600 }
40c10017
MZ
601out:
602 kfree(td);
d00a1c72
MZ
603 return ret;
604}
605
606/*
607 * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
608 */
609static int tpm_unseal(struct tpm_buf *tb,
1bdbb402
MZ
610 uint32_t keyhandle, const unsigned char *keyauth,
611 const unsigned char *blob, int bloblen,
d00a1c72
MZ
612 const unsigned char *blobauth,
613 unsigned char *data, unsigned int *datalen)
614{
615 unsigned char nonceodd[TPM_NONCE_SIZE];
616 unsigned char enonce1[TPM_NONCE_SIZE];
617 unsigned char enonce2[TPM_NONCE_SIZE];
618 unsigned char authdata1[SHA1_DIGEST_SIZE];
619 unsigned char authdata2[SHA1_DIGEST_SIZE];
620 uint32_t authhandle1 = 0;
621 uint32_t authhandle2 = 0;
622 unsigned char cont = 0;
623 uint32_t ordinal;
624 uint32_t keyhndl;
625 int ret;
626
627 /* sessions for unsealing key and data */
628 ret = oiap(tb, &authhandle1, enonce1);
629 if (ret < 0) {
630 pr_info("trusted_key: oiap failed (%d)\n", ret);
631 return ret;
632 }
633 ret = oiap(tb, &authhandle2, enonce2);
634 if (ret < 0) {
635 pr_info("trusted_key: oiap failed (%d)\n", ret);
636 return ret;
637 }
638
639 ordinal = htonl(TPM_ORD_UNSEAL);
640 keyhndl = htonl(SRKHANDLE);
641 ret = tpm_get_random(tb, nonceodd, TPM_NONCE_SIZE);
642 if (ret < 0) {
643 pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
644 return ret;
645 }
bc5e0af0
MZ
646 ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
647 enonce1, nonceodd, cont, sizeof(uint32_t),
648 &ordinal, bloblen, blob, 0, 0);
649 if (ret < 0)
650 return ret;
651 ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
652 enonce2, nonceodd, cont, sizeof(uint32_t),
653 &ordinal, bloblen, blob, 0, 0);
654 if (ret < 0)
655 return ret;
d00a1c72
MZ
656
657 /* build and send TPM request packet */
658 INIT_BUF(tb);
659 store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
660 store32(tb, TPM_UNSEAL_SIZE + bloblen);
661 store32(tb, TPM_ORD_UNSEAL);
662 store32(tb, keyhandle);
663 storebytes(tb, blob, bloblen);
664 store32(tb, authhandle1);
665 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
666 store8(tb, cont);
667 storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
668 store32(tb, authhandle2);
669 storebytes(tb, nonceodd, TPM_NONCE_SIZE);
670 store8(tb, cont);
671 storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
672
673 ret = trusted_tpm_send(TPM_ANY_NUM, tb->data, MAX_BUF_SIZE);
674 if (ret < 0) {
675 pr_info("trusted_key: authhmac failed (%d)\n", ret);
676 return ret;
677 }
678
679 *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
680 ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
681 keyauth, SHA1_DIGEST_SIZE,
682 blobauth, SHA1_DIGEST_SIZE,
683 sizeof(uint32_t), TPM_DATA_OFFSET,
684 *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
685 0);
bc5e0af0 686 if (ret < 0) {
d00a1c72 687 pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
bc5e0af0
MZ
688 return ret;
689 }
d00a1c72 690 memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
bc5e0af0 691 return 0;
d00a1c72
MZ
692}
693
694/*
695 * Have the TPM seal(encrypt) the symmetric key
696 */
697static int key_seal(struct trusted_key_payload *p,
698 struct trusted_key_options *o)
699{
700 struct tpm_buf *tb;
701 int ret;
702
703 tb = kzalloc(sizeof *tb, GFP_KERNEL);
704 if (!tb)
705 return -ENOMEM;
706
707 /* include migratable flag at end of sealed key */
708 p->key[p->key_len] = p->migratable;
709
710 ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
711 p->key, p->key_len + 1, p->blob, &p->blob_len,
712 o->blobauth, o->pcrinfo, o->pcrinfo_len);
713 if (ret < 0)
714 pr_info("trusted_key: srkseal failed (%d)\n", ret);
715
716 kfree(tb);
717 return ret;
718}
719
720/*
721 * Have the TPM unseal(decrypt) the symmetric key
722 */
723static int key_unseal(struct trusted_key_payload *p,
724 struct trusted_key_options *o)
725{
726 struct tpm_buf *tb;
727 int ret;
728
729 tb = kzalloc(sizeof *tb, GFP_KERNEL);
730 if (!tb)
731 return -ENOMEM;
732
733 ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
734 o->blobauth, p->key, &p->key_len);
d00a1c72
MZ
735 if (ret < 0)
736 pr_info("trusted_key: srkunseal failed (%d)\n", ret);
bc5e0af0
MZ
737 else
738 /* pull migratable flag out of sealed key */
739 p->migratable = p->key[--p->key_len];
d00a1c72
MZ
740
741 kfree(tb);
742 return ret;
743}
744
745enum {
746 Opt_err = -1,
747 Opt_new, Opt_load, Opt_update,
748 Opt_keyhandle, Opt_keyauth, Opt_blobauth,
749 Opt_pcrinfo, Opt_pcrlock, Opt_migratable
750};
751
752static const match_table_t key_tokens = {
753 {Opt_new, "new"},
754 {Opt_load, "load"},
755 {Opt_update, "update"},
756 {Opt_keyhandle, "keyhandle=%s"},
757 {Opt_keyauth, "keyauth=%s"},
758 {Opt_blobauth, "blobauth=%s"},
759 {Opt_pcrinfo, "pcrinfo=%s"},
760 {Opt_pcrlock, "pcrlock=%s"},
761 {Opt_migratable, "migratable=%s"},
762 {Opt_err, NULL}
763};
764
765/* can have zero or more token= options */
766static int getoptions(char *c, struct trusted_key_payload *pay,
767 struct trusted_key_options *opt)
768{
769 substring_t args[MAX_OPT_ARGS];
770 char *p = c;
771 int token;
772 int res;
773 unsigned long handle;
774 unsigned long lock;
775
776 while ((p = strsep(&c, " \t"))) {
777 if (*p == '\0' || *p == ' ' || *p == '\t')
778 continue;
779 token = match_token(p, key_tokens, args);
780
781 switch (token) {
782 case Opt_pcrinfo:
783 opt->pcrinfo_len = strlen(args[0].from) / 2;
784 if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
785 return -EINVAL;
786 hex2bin(opt->pcrinfo, args[0].from, opt->pcrinfo_len);
787 break;
788 case Opt_keyhandle:
789 res = strict_strtoul(args[0].from, 16, &handle);
790 if (res < 0)
791 return -EINVAL;
792 opt->keytype = SEAL_keytype;
793 opt->keyhandle = handle;
794 break;
795 case Opt_keyauth:
796 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
797 return -EINVAL;
798 hex2bin(opt->keyauth, args[0].from, SHA1_DIGEST_SIZE);
799 break;
800 case Opt_blobauth:
801 if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
802 return -EINVAL;
803 hex2bin(opt->blobauth, args[0].from, SHA1_DIGEST_SIZE);
804 break;
805 case Opt_migratable:
806 if (*args[0].from == '0')
807 pay->migratable = 0;
808 else
809 return -EINVAL;
810 break;
811 case Opt_pcrlock:
812 res = strict_strtoul(args[0].from, 10, &lock);
813 if (res < 0)
814 return -EINVAL;
815 opt->pcrlock = lock;
816 break;
817 default:
818 return -EINVAL;
819 }
820 }
821 return 0;
822}
823
824/*
825 * datablob_parse - parse the keyctl data and fill in the
826 * payload and options structures
827 *
828 * On success returns 0, otherwise -EINVAL.
829 */
830static int datablob_parse(char *datablob, struct trusted_key_payload *p,
831 struct trusted_key_options *o)
832{
833 substring_t args[MAX_OPT_ARGS];
834 long keylen;
835 int ret = -EINVAL;
836 int key_cmd;
837 char *c;
838
839 /* main command */
840 c = strsep(&datablob, " \t");
841 if (!c)
842 return -EINVAL;
843 key_cmd = match_token(c, key_tokens, args);
844 switch (key_cmd) {
845 case Opt_new:
846 /* first argument is key size */
847 c = strsep(&datablob, " \t");
848 if (!c)
849 return -EINVAL;
850 ret = strict_strtol(c, 10, &keylen);
851 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
852 return -EINVAL;
853 p->key_len = keylen;
854 ret = getoptions(datablob, p, o);
855 if (ret < 0)
856 return ret;
857 ret = Opt_new;
858 break;
859 case Opt_load:
860 /* first argument is sealed blob */
861 c = strsep(&datablob, " \t");
862 if (!c)
863 return -EINVAL;
864 p->blob_len = strlen(c) / 2;
865 if (p->blob_len > MAX_BLOB_SIZE)
866 return -EINVAL;
867 hex2bin(p->blob, c, p->blob_len);
868 ret = getoptions(datablob, p, o);
869 if (ret < 0)
870 return ret;
871 ret = Opt_load;
872 break;
873 case Opt_update:
874 /* all arguments are options */
875 ret = getoptions(datablob, p, o);
876 if (ret < 0)
877 return ret;
878 ret = Opt_update;
879 break;
880 case Opt_err:
881 return -EINVAL;
882 break;
883 }
884 return ret;
885}
886
887static struct trusted_key_options *trusted_options_alloc(void)
888{
889 struct trusted_key_options *options;
890
891 options = kzalloc(sizeof *options, GFP_KERNEL);
bc5e0af0
MZ
892 if (options) {
893 /* set any non-zero defaults */
894 options->keytype = SRK_keytype;
895 options->keyhandle = SRKHANDLE;
896 }
d00a1c72
MZ
897 return options;
898}
899
900static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
901{
902 struct trusted_key_payload *p = NULL;
903 int ret;
904
905 ret = key_payload_reserve(key, sizeof *p);
906 if (ret < 0)
907 return p;
908 p = kzalloc(sizeof *p, GFP_KERNEL);
bc5e0af0
MZ
909 if (p)
910 p->migratable = 1; /* migratable by default */
d00a1c72
MZ
911 return p;
912}
913
914/*
915 * trusted_instantiate - create a new trusted key
916 *
917 * Unseal an existing trusted blob or, for a new key, get a
918 * random key, then seal and create a trusted key-type key,
919 * adding it to the specified keyring.
920 *
921 * On success, return 0. Otherwise return errno.
922 */
923static int trusted_instantiate(struct key *key, const void *data,
1bdbb402 924 size_t datalen)
d00a1c72
MZ
925{
926 struct trusted_key_payload *payload = NULL;
927 struct trusted_key_options *options = NULL;
928 char *datablob;
929 int ret = 0;
930 int key_cmd;
931
932 if (datalen <= 0 || datalen > 32767 || !data)
933 return -EINVAL;
934
935 datablob = kmalloc(datalen + 1, GFP_KERNEL);
936 if (!datablob)
937 return -ENOMEM;
938 memcpy(datablob, data, datalen);
939 datablob[datalen] = '\0';
940
941 options = trusted_options_alloc();
942 if (!options) {
943 ret = -ENOMEM;
944 goto out;
945 }
946 payload = trusted_payload_alloc(key);
947 if (!payload) {
948 ret = -ENOMEM;
949 goto out;
950 }
951
952 key_cmd = datablob_parse(datablob, payload, options);
953 if (key_cmd < 0) {
954 ret = key_cmd;
955 goto out;
956 }
957
958 dump_payload(payload);
959 dump_options(options);
960
961 switch (key_cmd) {
962 case Opt_load:
963 ret = key_unseal(payload, options);
964 dump_payload(payload);
965 dump_options(options);
966 if (ret < 0)
967 pr_info("trusted_key: key_unseal failed (%d)\n", ret);
968 break;
969 case Opt_new:
970 ret = my_get_random(payload->key, payload->key_len);
971 if (ret < 0) {
972 pr_info("trusted_key: key_create failed (%d)\n", ret);
973 goto out;
974 }
975 ret = key_seal(payload, options);
976 if (ret < 0)
977 pr_info("trusted_key: key_seal failed (%d)\n", ret);
978 break;
979 default:
980 ret = -EINVAL;
981 goto out;
982 }
983 if (!ret && options->pcrlock)
984 ret = pcrlock(options->pcrlock);
985out:
986 kfree(datablob);
987 kfree(options);
988 if (!ret)
989 rcu_assign_pointer(key->payload.data, payload);
990 else
991 kfree(payload);
992 return ret;
993}
994
995static void trusted_rcu_free(struct rcu_head *rcu)
996{
997 struct trusted_key_payload *p;
998
999 p = container_of(rcu, struct trusted_key_payload, rcu);
1000 memset(p->key, 0, p->key_len);
1001 kfree(p);
1002}
1003
1004/*
1005 * trusted_update - reseal an existing key with new PCR values
1006 */
1bdbb402 1007static int trusted_update(struct key *key, const void *data, size_t datalen)
d00a1c72
MZ
1008{
1009 struct trusted_key_payload *p = key->payload.data;
1010 struct trusted_key_payload *new_p;
1011 struct trusted_key_options *new_o;
1012 char *datablob;
1013 int ret = 0;
1014
1015 if (!p->migratable)
1016 return -EPERM;
1017 if (datalen <= 0 || datalen > 32767 || !data)
1018 return -EINVAL;
1019
1020 datablob = kmalloc(datalen + 1, GFP_KERNEL);
1021 if (!datablob)
1022 return -ENOMEM;
1023 new_o = trusted_options_alloc();
1024 if (!new_o) {
1025 ret = -ENOMEM;
1026 goto out;
1027 }
1028 new_p = trusted_payload_alloc(key);
1029 if (!new_p) {
1030 ret = -ENOMEM;
1031 goto out;
1032 }
1033
1034 memcpy(datablob, data, datalen);
1035 datablob[datalen] = '\0';
1036 ret = datablob_parse(datablob, new_p, new_o);
1037 if (ret != Opt_update) {
1038 ret = -EINVAL;
1039 goto out;
1040 }
1041 /* copy old key values, and reseal with new pcrs */
1042 new_p->migratable = p->migratable;
1043 new_p->key_len = p->key_len;
1044 memcpy(new_p->key, p->key, p->key_len);
1045 dump_payload(p);
1046 dump_payload(new_p);
1047
1048 ret = key_seal(new_p, new_o);
1049 if (ret < 0) {
1050 pr_info("trusted_key: key_seal failed (%d)\n", ret);
1051 kfree(new_p);
1052 goto out;
1053 }
1054 if (new_o->pcrlock) {
1055 ret = pcrlock(new_o->pcrlock);
1056 if (ret < 0) {
1057 pr_info("trusted_key: pcrlock failed (%d)\n", ret);
1058 kfree(new_p);
1059 goto out;
1060 }
1061 }
1062 rcu_assign_pointer(key->payload.data, new_p);
1063 call_rcu(&p->rcu, trusted_rcu_free);
1064out:
1065 kfree(datablob);
1066 kfree(new_o);
1067 return ret;
1068}
1069
1070/*
1071 * trusted_read - copy the sealed blob data to userspace in hex.
1072 * On success, return to userspace the trusted key datablob size.
1073 */
1074static long trusted_read(const struct key *key, char __user *buffer,
1075 size_t buflen)
1076{
1077 struct trusted_key_payload *p;
1078 char *ascii_buf;
1079 char *bufp;
1080 int i;
1081
1082 p = rcu_dereference_protected(key->payload.data,
1083 rwsem_is_locked(&((struct key *)key)->sem));
1084 if (!p)
1085 return -EINVAL;
1086 if (!buffer || buflen <= 0)
1087 return 2 * p->blob_len;
1088 ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
1089 if (!ascii_buf)
1090 return -ENOMEM;
1091
1092 bufp = ascii_buf;
1093 for (i = 0; i < p->blob_len; i++)
1094 bufp = pack_hex_byte(bufp, p->blob[i]);
1095 if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
1096 kfree(ascii_buf);
1097 return -EFAULT;
1098 }
1099 kfree(ascii_buf);
1100 return 2 * p->blob_len;
1101}
1102
1103/*
1104 * trusted_destroy - before freeing the key, clear the decrypted data
1105 */
1106static void trusted_destroy(struct key *key)
1107{
1108 struct trusted_key_payload *p = key->payload.data;
1109
1110 if (!p)
1111 return;
1112 memset(p->key, 0, p->key_len);
1113 kfree(key->payload.data);
1114}
1115
1116struct key_type key_type_trusted = {
1117 .name = "trusted",
1118 .instantiate = trusted_instantiate,
1119 .update = trusted_update,
1120 .match = user_match,
1121 .destroy = trusted_destroy,
1122 .describe = user_describe,
1123 .read = trusted_read,
1124};
1125
1126EXPORT_SYMBOL_GPL(key_type_trusted);
1127
1128static void trusted_shash_release(void)
1129{
1130 if (hashalg)
1131 crypto_free_shash(hashalg);
1132 if (hmacalg)
1133 crypto_free_shash(hmacalg);
1134}
1135
1136static int __init trusted_shash_alloc(void)
1137{
1138 int ret;
1139
1140 hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
1141 if (IS_ERR(hmacalg)) {
1142 pr_info("trusted_key: could not allocate crypto %s\n",
1143 hmac_alg);
1144 return PTR_ERR(hmacalg);
1145 }
1146
1147 hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
1148 if (IS_ERR(hashalg)) {
1149 pr_info("trusted_key: could not allocate crypto %s\n",
1150 hash_alg);
1151 ret = PTR_ERR(hashalg);
1152 goto hashalg_fail;
1153 }
1154
1155 return 0;
1156
1157hashalg_fail:
1158 crypto_free_shash(hmacalg);
1159 return ret;
1160}
1161
1162static int __init init_trusted(void)
1163{
1164 int ret;
1165
1166 ret = trusted_shash_alloc();
1167 if (ret < 0)
1168 return ret;
1169 ret = register_key_type(&key_type_trusted);
1170 if (ret < 0)
1171 trusted_shash_release();
1172 return ret;
1173}
1174
1175static void __exit cleanup_trusted(void)
1176{
1177 trusted_shash_release();
1178 unregister_key_type(&key_type_trusted);
1179}
1180
1181late_initcall(init_trusted);
1182module_exit(cleanup_trusted);
1183
1184MODULE_LICENSE("GPL");
This page took 0.077311 seconds and 5 git commands to generate.