trusted_defined.c 28 KB

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