trusted.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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;
  923. struct trusted_key_payload *new_p;
  924. struct trusted_key_options *new_o;
  925. char *datablob;
  926. int ret = 0;
  927. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
  928. return -ENOKEY;
  929. p = key->payload.data;
  930. if (!p->migratable)
  931. return -EPERM;
  932. if (datalen <= 0 || datalen > 32767 || !data)
  933. return -EINVAL;
  934. datablob = kmalloc(datalen + 1, GFP_KERNEL);
  935. if (!datablob)
  936. return -ENOMEM;
  937. new_o = trusted_options_alloc();
  938. if (!new_o) {
  939. ret = -ENOMEM;
  940. goto out;
  941. }
  942. new_p = trusted_payload_alloc(key);
  943. if (!new_p) {
  944. ret = -ENOMEM;
  945. goto out;
  946. }
  947. memcpy(datablob, data, datalen);
  948. datablob[datalen] = '\0';
  949. ret = datablob_parse(datablob, new_p, new_o);
  950. if (ret != Opt_update) {
  951. ret = -EINVAL;
  952. kfree(new_p);
  953. goto out;
  954. }
  955. /* copy old key values, and reseal with new pcrs */
  956. new_p->migratable = p->migratable;
  957. new_p->key_len = p->key_len;
  958. memcpy(new_p->key, p->key, p->key_len);
  959. dump_payload(p);
  960. dump_payload(new_p);
  961. ret = key_seal(new_p, new_o);
  962. if (ret < 0) {
  963. pr_info("trusted_key: key_seal failed (%d)\n", ret);
  964. kfree(new_p);
  965. goto out;
  966. }
  967. if (new_o->pcrlock) {
  968. ret = pcrlock(new_o->pcrlock);
  969. if (ret < 0) {
  970. pr_info("trusted_key: pcrlock failed (%d)\n", ret);
  971. kfree(new_p);
  972. goto out;
  973. }
  974. }
  975. rcu_assign_keypointer(key, new_p);
  976. call_rcu(&p->rcu, trusted_rcu_free);
  977. out:
  978. kfree(datablob);
  979. kfree(new_o);
  980. return ret;
  981. }
  982. /*
  983. * trusted_read - copy the sealed blob data to userspace in hex.
  984. * On success, return to userspace the trusted key datablob size.
  985. */
  986. static long trusted_read(const struct key *key, char __user *buffer,
  987. size_t buflen)
  988. {
  989. struct trusted_key_payload *p;
  990. char *ascii_buf;
  991. char *bufp;
  992. int i;
  993. p = rcu_dereference_key(key);
  994. if (!p)
  995. return -EINVAL;
  996. if (!buffer || buflen <= 0)
  997. return 2 * p->blob_len;
  998. ascii_buf = kmalloc(2 * p->blob_len, GFP_KERNEL);
  999. if (!ascii_buf)
  1000. return -ENOMEM;
  1001. bufp = ascii_buf;
  1002. for (i = 0; i < p->blob_len; i++)
  1003. bufp = hex_byte_pack(bufp, p->blob[i]);
  1004. if ((copy_to_user(buffer, ascii_buf, 2 * p->blob_len)) != 0) {
  1005. kfree(ascii_buf);
  1006. return -EFAULT;
  1007. }
  1008. kfree(ascii_buf);
  1009. return 2 * p->blob_len;
  1010. }
  1011. /*
  1012. * trusted_destroy - before freeing the key, clear the decrypted data
  1013. */
  1014. static void trusted_destroy(struct key *key)
  1015. {
  1016. struct trusted_key_payload *p = key->payload.data;
  1017. if (!p)
  1018. return;
  1019. memset(p->key, 0, p->key_len);
  1020. kfree(key->payload.data);
  1021. }
  1022. struct key_type key_type_trusted = {
  1023. .name = "trusted",
  1024. .instantiate = trusted_instantiate,
  1025. .update = trusted_update,
  1026. .match = user_match,
  1027. .destroy = trusted_destroy,
  1028. .describe = user_describe,
  1029. .read = trusted_read,
  1030. };
  1031. EXPORT_SYMBOL_GPL(key_type_trusted);
  1032. static void trusted_shash_release(void)
  1033. {
  1034. if (hashalg)
  1035. crypto_free_shash(hashalg);
  1036. if (hmacalg)
  1037. crypto_free_shash(hmacalg);
  1038. }
  1039. static int __init trusted_shash_alloc(void)
  1040. {
  1041. int ret;
  1042. hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
  1043. if (IS_ERR(hmacalg)) {
  1044. pr_info("trusted_key: could not allocate crypto %s\n",
  1045. hmac_alg);
  1046. return PTR_ERR(hmacalg);
  1047. }
  1048. hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
  1049. if (IS_ERR(hashalg)) {
  1050. pr_info("trusted_key: could not allocate crypto %s\n",
  1051. hash_alg);
  1052. ret = PTR_ERR(hashalg);
  1053. goto hashalg_fail;
  1054. }
  1055. return 0;
  1056. hashalg_fail:
  1057. crypto_free_shash(hmacalg);
  1058. return ret;
  1059. }
  1060. static int __init init_trusted(void)
  1061. {
  1062. int ret;
  1063. ret = trusted_shash_alloc();
  1064. if (ret < 0)
  1065. return ret;
  1066. ret = register_key_type(&key_type_trusted);
  1067. if (ret < 0)
  1068. trusted_shash_release();
  1069. return ret;
  1070. }
  1071. static void __exit cleanup_trusted(void)
  1072. {
  1073. trusted_shash_release();
  1074. unregister_key_type(&key_type_trusted);
  1075. }
  1076. late_initcall(init_trusted);
  1077. module_exit(cleanup_trusted);
  1078. MODULE_LICENSE("GPL");