trusted.c 28 KB

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