trusted_defined.c 28 KB

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