trusted.c 29 KB

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