trusted.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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(unsigned char *cmd, size_t buflen)
  330. {
  331. int rc;
  332. dump_tpm_buf(cmd);
  333. rc = tpm_send(NULL, 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. * Lock a trusted key, by extending a selected PCR.
  342. *
  343. * Prevents a trusted key that is sealed to PCRs from being accessed.
  344. * This uses the tpm driver's extend function.
  345. */
  346. static int pcrlock(const int pcrnum)
  347. {
  348. unsigned char hash[SHA1_DIGEST_SIZE];
  349. int ret;
  350. if (!capable(CAP_SYS_ADMIN))
  351. return -EPERM;
  352. ret = tpm_get_random(NULL, hash, SHA1_DIGEST_SIZE);
  353. if (ret != SHA1_DIGEST_SIZE)
  354. return ret;
  355. return tpm_pcr_extend(NULL, pcrnum, hash) ? -EINVAL : 0;
  356. }
  357. /*
  358. * Create an object specific authorisation protocol (OSAP) session
  359. */
  360. static int osap(struct tpm_buf *tb, struct osapsess *s,
  361. const unsigned char *key, uint16_t type, uint32_t handle)
  362. {
  363. unsigned char enonce[TPM_NONCE_SIZE];
  364. unsigned char ononce[TPM_NONCE_SIZE];
  365. int ret;
  366. ret = tpm_get_random(NULL, ononce, TPM_NONCE_SIZE);
  367. if (ret != TPM_NONCE_SIZE)
  368. return ret;
  369. INIT_BUF(tb);
  370. store16(tb, TPM_TAG_RQU_COMMAND);
  371. store32(tb, TPM_OSAP_SIZE);
  372. store32(tb, TPM_ORD_OSAP);
  373. store16(tb, type);
  374. store32(tb, handle);
  375. storebytes(tb, ononce, TPM_NONCE_SIZE);
  376. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  377. if (ret < 0)
  378. return ret;
  379. s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
  380. memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
  381. TPM_NONCE_SIZE);
  382. memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
  383. TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
  384. return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
  385. enonce, TPM_NONCE_SIZE, ononce, 0, 0);
  386. }
  387. /*
  388. * Create an object independent authorisation protocol (oiap) session
  389. */
  390. static int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
  391. {
  392. int ret;
  393. INIT_BUF(tb);
  394. store16(tb, TPM_TAG_RQU_COMMAND);
  395. store32(tb, TPM_OIAP_SIZE);
  396. store32(tb, TPM_ORD_OIAP);
  397. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  398. if (ret < 0)
  399. return ret;
  400. *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
  401. memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
  402. TPM_NONCE_SIZE);
  403. return 0;
  404. }
  405. struct tpm_digests {
  406. unsigned char encauth[SHA1_DIGEST_SIZE];
  407. unsigned char pubauth[SHA1_DIGEST_SIZE];
  408. unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
  409. unsigned char xorhash[SHA1_DIGEST_SIZE];
  410. unsigned char nonceodd[TPM_NONCE_SIZE];
  411. };
  412. /*
  413. * Have the TPM seal(encrypt) the trusted key, possibly based on
  414. * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
  415. */
  416. static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
  417. uint32_t keyhandle, const unsigned char *keyauth,
  418. const unsigned char *data, uint32_t datalen,
  419. unsigned char *blob, uint32_t *bloblen,
  420. const unsigned char *blobauth,
  421. const unsigned char *pcrinfo, uint32_t pcrinfosize)
  422. {
  423. struct osapsess sess;
  424. struct tpm_digests *td;
  425. unsigned char cont;
  426. uint32_t ordinal;
  427. uint32_t pcrsize;
  428. uint32_t datsize;
  429. int sealinfosize;
  430. int encdatasize;
  431. int storedsize;
  432. int ret;
  433. int i;
  434. /* alloc some work space for all the hashes */
  435. td = kmalloc(sizeof *td, GFP_KERNEL);
  436. if (!td)
  437. return -ENOMEM;
  438. /* get session for sealing key */
  439. ret = osap(tb, &sess, keyauth, keytype, keyhandle);
  440. if (ret < 0)
  441. goto out;
  442. dump_sess(&sess);
  443. /* calculate encrypted authorization value */
  444. memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
  445. memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
  446. ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
  447. if (ret < 0)
  448. goto out;
  449. ret = tpm_get_random(NULL, td->nonceodd, TPM_NONCE_SIZE);
  450. if (ret != TPM_NONCE_SIZE)
  451. goto out;
  452. ordinal = htonl(TPM_ORD_SEAL);
  453. datsize = htonl(datalen);
  454. pcrsize = htonl(pcrinfosize);
  455. cont = 0;
  456. /* encrypt data authorization key */
  457. for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
  458. td->encauth[i] = td->xorhash[i] ^ blobauth[i];
  459. /* calculate authorization HMAC value */
  460. if (pcrinfosize == 0) {
  461. /* no pcr info specified */
  462. ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
  463. sess.enonce, td->nonceodd, cont,
  464. sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
  465. td->encauth, sizeof(uint32_t), &pcrsize,
  466. sizeof(uint32_t), &datsize, datalen, data, 0,
  467. 0);
  468. } else {
  469. /* pcr info specified */
  470. ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
  471. sess.enonce, td->nonceodd, cont,
  472. sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
  473. td->encauth, sizeof(uint32_t), &pcrsize,
  474. pcrinfosize, pcrinfo, sizeof(uint32_t),
  475. &datsize, datalen, data, 0, 0);
  476. }
  477. if (ret < 0)
  478. goto out;
  479. /* build and send the TPM request packet */
  480. INIT_BUF(tb);
  481. store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
  482. store32(tb, TPM_SEAL_SIZE + pcrinfosize + datalen);
  483. store32(tb, TPM_ORD_SEAL);
  484. store32(tb, keyhandle);
  485. storebytes(tb, td->encauth, SHA1_DIGEST_SIZE);
  486. store32(tb, pcrinfosize);
  487. storebytes(tb, pcrinfo, pcrinfosize);
  488. store32(tb, datalen);
  489. storebytes(tb, data, datalen);
  490. store32(tb, sess.handle);
  491. storebytes(tb, td->nonceodd, TPM_NONCE_SIZE);
  492. store8(tb, cont);
  493. storebytes(tb, td->pubauth, SHA1_DIGEST_SIZE);
  494. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  495. if (ret < 0)
  496. goto out;
  497. /* calculate the size of the returned Blob */
  498. sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
  499. encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
  500. sizeof(uint32_t) + sealinfosize);
  501. storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
  502. sizeof(uint32_t) + encdatasize;
  503. /* check the HMAC in the response */
  504. ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
  505. SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
  506. 0);
  507. /* copy the returned blob to caller */
  508. if (!ret) {
  509. memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
  510. *bloblen = storedsize;
  511. }
  512. out:
  513. kzfree(td);
  514. return ret;
  515. }
  516. /*
  517. * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
  518. */
  519. static int tpm_unseal(struct tpm_buf *tb,
  520. uint32_t keyhandle, const unsigned char *keyauth,
  521. const unsigned char *blob, int bloblen,
  522. const unsigned char *blobauth,
  523. unsigned char *data, unsigned int *datalen)
  524. {
  525. unsigned char nonceodd[TPM_NONCE_SIZE];
  526. unsigned char enonce1[TPM_NONCE_SIZE];
  527. unsigned char enonce2[TPM_NONCE_SIZE];
  528. unsigned char authdata1[SHA1_DIGEST_SIZE];
  529. unsigned char authdata2[SHA1_DIGEST_SIZE];
  530. uint32_t authhandle1 = 0;
  531. uint32_t authhandle2 = 0;
  532. unsigned char cont = 0;
  533. uint32_t ordinal;
  534. uint32_t keyhndl;
  535. int ret;
  536. /* sessions for unsealing key and data */
  537. ret = oiap(tb, &authhandle1, enonce1);
  538. if (ret < 0) {
  539. pr_info("trusted_key: oiap failed (%d)\n", ret);
  540. return ret;
  541. }
  542. ret = oiap(tb, &authhandle2, enonce2);
  543. if (ret < 0) {
  544. pr_info("trusted_key: oiap failed (%d)\n", ret);
  545. return ret;
  546. }
  547. ordinal = htonl(TPM_ORD_UNSEAL);
  548. keyhndl = htonl(SRKHANDLE);
  549. ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
  550. if (ret != TPM_NONCE_SIZE) {
  551. pr_info("trusted_key: tpm_get_random failed (%d)\n", ret);
  552. return ret;
  553. }
  554. ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
  555. enonce1, nonceodd, cont, sizeof(uint32_t),
  556. &ordinal, bloblen, blob, 0, 0);
  557. if (ret < 0)
  558. return ret;
  559. ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
  560. enonce2, nonceodd, cont, sizeof(uint32_t),
  561. &ordinal, bloblen, blob, 0, 0);
  562. if (ret < 0)
  563. return ret;
  564. /* build and send TPM request packet */
  565. INIT_BUF(tb);
  566. store16(tb, TPM_TAG_RQU_AUTH2_COMMAND);
  567. store32(tb, TPM_UNSEAL_SIZE + bloblen);
  568. store32(tb, TPM_ORD_UNSEAL);
  569. store32(tb, keyhandle);
  570. storebytes(tb, blob, bloblen);
  571. store32(tb, authhandle1);
  572. storebytes(tb, nonceodd, TPM_NONCE_SIZE);
  573. store8(tb, cont);
  574. storebytes(tb, authdata1, SHA1_DIGEST_SIZE);
  575. store32(tb, authhandle2);
  576. storebytes(tb, nonceodd, TPM_NONCE_SIZE);
  577. store8(tb, cont);
  578. storebytes(tb, authdata2, SHA1_DIGEST_SIZE);
  579. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  580. if (ret < 0) {
  581. pr_info("trusted_key: authhmac failed (%d)\n", ret);
  582. return ret;
  583. }
  584. *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
  585. ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
  586. keyauth, SHA1_DIGEST_SIZE,
  587. blobauth, SHA1_DIGEST_SIZE,
  588. sizeof(uint32_t), TPM_DATA_OFFSET,
  589. *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
  590. 0);
  591. if (ret < 0) {
  592. pr_info("trusted_key: TSS_checkhmac2 failed (%d)\n", ret);
  593. return ret;
  594. }
  595. memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
  596. return 0;
  597. }
  598. /*
  599. * Have the TPM seal(encrypt) the symmetric key
  600. */
  601. static int key_seal(struct trusted_key_payload *p,
  602. struct trusted_key_options *o)
  603. {
  604. struct tpm_buf *tb;
  605. int ret;
  606. tb = kzalloc(sizeof *tb, GFP_KERNEL);
  607. if (!tb)
  608. return -ENOMEM;
  609. /* include migratable flag at end of sealed key */
  610. p->key[p->key_len] = p->migratable;
  611. ret = tpm_seal(tb, o->keytype, o->keyhandle, o->keyauth,
  612. p->key, p->key_len + 1, p->blob, &p->blob_len,
  613. o->blobauth, o->pcrinfo, o->pcrinfo_len);
  614. if (ret < 0)
  615. pr_info("trusted_key: srkseal failed (%d)\n", ret);
  616. kzfree(tb);
  617. return ret;
  618. }
  619. /*
  620. * Have the TPM unseal(decrypt) the symmetric key
  621. */
  622. static int key_unseal(struct trusted_key_payload *p,
  623. struct trusted_key_options *o)
  624. {
  625. struct tpm_buf *tb;
  626. int ret;
  627. tb = kzalloc(sizeof *tb, GFP_KERNEL);
  628. if (!tb)
  629. return -ENOMEM;
  630. ret = tpm_unseal(tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
  631. o->blobauth, p->key, &p->key_len);
  632. if (ret < 0)
  633. pr_info("trusted_key: srkunseal failed (%d)\n", ret);
  634. else
  635. /* pull migratable flag out of sealed key */
  636. p->migratable = p->key[--p->key_len];
  637. kzfree(tb);
  638. return ret;
  639. }
  640. enum {
  641. Opt_err = -1,
  642. Opt_new, Opt_load, Opt_update,
  643. Opt_keyhandle, Opt_keyauth, Opt_blobauth,
  644. Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
  645. Opt_hash,
  646. Opt_policydigest,
  647. Opt_policyhandle,
  648. };
  649. static const match_table_t key_tokens = {
  650. {Opt_new, "new"},
  651. {Opt_load, "load"},
  652. {Opt_update, "update"},
  653. {Opt_keyhandle, "keyhandle=%s"},
  654. {Opt_keyauth, "keyauth=%s"},
  655. {Opt_blobauth, "blobauth=%s"},
  656. {Opt_pcrinfo, "pcrinfo=%s"},
  657. {Opt_pcrlock, "pcrlock=%s"},
  658. {Opt_migratable, "migratable=%s"},
  659. {Opt_hash, "hash=%s"},
  660. {Opt_policydigest, "policydigest=%s"},
  661. {Opt_policyhandle, "policyhandle=%s"},
  662. {Opt_err, NULL}
  663. };
  664. /* can have zero or more token= options */
  665. static int getoptions(char *c, struct trusted_key_payload *pay,
  666. struct trusted_key_options *opt)
  667. {
  668. substring_t args[MAX_OPT_ARGS];
  669. char *p = c;
  670. int token;
  671. int res;
  672. unsigned long handle;
  673. unsigned long lock;
  674. unsigned long token_mask = 0;
  675. unsigned int digest_len;
  676. int i;
  677. int tpm2;
  678. tpm2 = tpm_is_tpm2(NULL);
  679. if (tpm2 < 0)
  680. return tpm2;
  681. opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
  682. while ((p = strsep(&c, " \t"))) {
  683. if (*p == '\0' || *p == ' ' || *p == '\t')
  684. continue;
  685. token = match_token(p, key_tokens, args);
  686. if (test_and_set_bit(token, &token_mask))
  687. return -EINVAL;
  688. switch (token) {
  689. case Opt_pcrinfo:
  690. opt->pcrinfo_len = strlen(args[0].from) / 2;
  691. if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
  692. return -EINVAL;
  693. res = hex2bin(opt->pcrinfo, args[0].from,
  694. opt->pcrinfo_len);
  695. if (res < 0)
  696. return -EINVAL;
  697. break;
  698. case Opt_keyhandle:
  699. res = kstrtoul(args[0].from, 16, &handle);
  700. if (res < 0)
  701. return -EINVAL;
  702. opt->keytype = SEAL_keytype;
  703. opt->keyhandle = handle;
  704. break;
  705. case Opt_keyauth:
  706. if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
  707. return -EINVAL;
  708. res = hex2bin(opt->keyauth, args[0].from,
  709. SHA1_DIGEST_SIZE);
  710. if (res < 0)
  711. return -EINVAL;
  712. break;
  713. case Opt_blobauth:
  714. if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
  715. return -EINVAL;
  716. res = hex2bin(opt->blobauth, args[0].from,
  717. SHA1_DIGEST_SIZE);
  718. if (res < 0)
  719. return -EINVAL;
  720. break;
  721. case Opt_migratable:
  722. if (*args[0].from == '0')
  723. pay->migratable = 0;
  724. else
  725. return -EINVAL;
  726. break;
  727. case Opt_pcrlock:
  728. res = kstrtoul(args[0].from, 10, &lock);
  729. if (res < 0)
  730. return -EINVAL;
  731. opt->pcrlock = lock;
  732. break;
  733. case Opt_hash:
  734. if (test_bit(Opt_policydigest, &token_mask))
  735. return -EINVAL;
  736. for (i = 0; i < HASH_ALGO__LAST; i++) {
  737. if (!strcmp(args[0].from, hash_algo_name[i])) {
  738. opt->hash = i;
  739. break;
  740. }
  741. }
  742. if (i == HASH_ALGO__LAST)
  743. return -EINVAL;
  744. if (!tpm2 && i != HASH_ALGO_SHA1) {
  745. pr_info("trusted_key: TPM 1.x only supports SHA-1.\n");
  746. return -EINVAL;
  747. }
  748. break;
  749. case Opt_policydigest:
  750. digest_len = hash_digest_size[opt->hash];
  751. if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
  752. return -EINVAL;
  753. res = hex2bin(opt->policydigest, args[0].from,
  754. digest_len);
  755. if (res < 0)
  756. return -EINVAL;
  757. opt->policydigest_len = digest_len;
  758. break;
  759. case Opt_policyhandle:
  760. if (!tpm2)
  761. return -EINVAL;
  762. res = kstrtoul(args[0].from, 16, &handle);
  763. if (res < 0)
  764. return -EINVAL;
  765. opt->policyhandle = handle;
  766. break;
  767. default:
  768. return -EINVAL;
  769. }
  770. }
  771. return 0;
  772. }
  773. /*
  774. * datablob_parse - parse the keyctl data and fill in the
  775. * payload and options structures
  776. *
  777. * On success returns 0, otherwise -EINVAL.
  778. */
  779. static int datablob_parse(char *datablob, struct trusted_key_payload *p,
  780. struct trusted_key_options *o)
  781. {
  782. substring_t args[MAX_OPT_ARGS];
  783. long keylen;
  784. int ret = -EINVAL;
  785. int key_cmd;
  786. char *c;
  787. /* main command */
  788. c = strsep(&datablob, " \t");
  789. if (!c)
  790. return -EINVAL;
  791. key_cmd = match_token(c, key_tokens, args);
  792. switch (key_cmd) {
  793. case Opt_new:
  794. /* first argument is key size */
  795. c = strsep(&datablob, " \t");
  796. if (!c)
  797. return -EINVAL;
  798. ret = kstrtol(c, 10, &keylen);
  799. if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
  800. return -EINVAL;
  801. p->key_len = keylen;
  802. ret = getoptions(datablob, p, o);
  803. if (ret < 0)
  804. return ret;
  805. ret = Opt_new;
  806. break;
  807. case Opt_load:
  808. /* first argument is sealed blob */
  809. c = strsep(&datablob, " \t");
  810. if (!c)
  811. return -EINVAL;
  812. p->blob_len = strlen(c) / 2;
  813. if (p->blob_len > MAX_BLOB_SIZE)
  814. return -EINVAL;
  815. ret = hex2bin(p->blob, c, p->blob_len);
  816. if (ret < 0)
  817. return -EINVAL;
  818. ret = getoptions(datablob, p, o);
  819. if (ret < 0)
  820. return ret;
  821. ret = Opt_load;
  822. break;
  823. case Opt_update:
  824. /* all arguments are options */
  825. ret = getoptions(datablob, p, o);
  826. if (ret < 0)
  827. return ret;
  828. ret = Opt_update;
  829. break;
  830. case Opt_err:
  831. return -EINVAL;
  832. break;
  833. }
  834. return ret;
  835. }
  836. static struct trusted_key_options *trusted_options_alloc(void)
  837. {
  838. struct trusted_key_options *options;
  839. int tpm2;
  840. tpm2 = tpm_is_tpm2(NULL);
  841. if (tpm2 < 0)
  842. return NULL;
  843. options = kzalloc(sizeof *options, GFP_KERNEL);
  844. if (options) {
  845. /* set any non-zero defaults */
  846. options->keytype = SRK_keytype;
  847. if (!tpm2)
  848. options->keyhandle = SRKHANDLE;
  849. }
  850. return options;
  851. }
  852. static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
  853. {
  854. struct trusted_key_payload *p = NULL;
  855. int ret;
  856. ret = key_payload_reserve(key, sizeof *p);
  857. if (ret < 0)
  858. return p;
  859. p = kzalloc(sizeof *p, GFP_KERNEL);
  860. if (p)
  861. p->migratable = 1; /* migratable by default */
  862. return p;
  863. }
  864. /*
  865. * trusted_instantiate - create a new trusted key
  866. *
  867. * Unseal an existing trusted blob or, for a new key, get a
  868. * random key, then seal and create a trusted key-type key,
  869. * adding it to the specified keyring.
  870. *
  871. * On success, return 0. Otherwise return errno.
  872. */
  873. static int trusted_instantiate(struct key *key,
  874. struct key_preparsed_payload *prep)
  875. {
  876. struct trusted_key_payload *payload = NULL;
  877. struct trusted_key_options *options = NULL;
  878. size_t datalen = prep->datalen;
  879. char *datablob;
  880. int ret = 0;
  881. int key_cmd;
  882. size_t key_len;
  883. int tpm2;
  884. tpm2 = tpm_is_tpm2(NULL);
  885. if (tpm2 < 0)
  886. return tpm2;
  887. if (datalen <= 0 || datalen > 32767 || !prep->data)
  888. return -EINVAL;
  889. datablob = kmalloc(datalen + 1, GFP_KERNEL);
  890. if (!datablob)
  891. return -ENOMEM;
  892. memcpy(datablob, prep->data, datalen);
  893. datablob[datalen] = '\0';
  894. options = trusted_options_alloc();
  895. if (!options) {
  896. ret = -ENOMEM;
  897. goto out;
  898. }
  899. payload = trusted_payload_alloc(key);
  900. if (!payload) {
  901. ret = -ENOMEM;
  902. goto out;
  903. }
  904. key_cmd = datablob_parse(datablob, payload, options);
  905. if (key_cmd < 0) {
  906. ret = key_cmd;
  907. goto out;
  908. }
  909. if (!options->keyhandle) {
  910. ret = -EINVAL;
  911. goto out;
  912. }
  913. dump_payload(payload);
  914. dump_options(options);
  915. switch (key_cmd) {
  916. case Opt_load:
  917. if (tpm2)
  918. ret = tpm_unseal_trusted(NULL, payload, options);
  919. else
  920. ret = key_unseal(payload, options);
  921. dump_payload(payload);
  922. dump_options(options);
  923. if (ret < 0)
  924. pr_info("trusted_key: key_unseal failed (%d)\n", ret);
  925. break;
  926. case Opt_new:
  927. key_len = payload->key_len;
  928. ret = tpm_get_random(NULL, payload->key, key_len);
  929. if (ret != key_len) {
  930. pr_info("trusted_key: key_create failed (%d)\n", ret);
  931. goto out;
  932. }
  933. if (tpm2)
  934. ret = tpm_seal_trusted(NULL, payload, options);
  935. else
  936. ret = key_seal(payload, options);
  937. if (ret < 0)
  938. pr_info("trusted_key: key_seal failed (%d)\n", ret);
  939. break;
  940. default:
  941. ret = -EINVAL;
  942. goto out;
  943. }
  944. if (!ret && options->pcrlock)
  945. ret = pcrlock(options->pcrlock);
  946. out:
  947. kzfree(datablob);
  948. kzfree(options);
  949. if (!ret)
  950. rcu_assign_keypointer(key, payload);
  951. else
  952. kzfree(payload);
  953. return ret;
  954. }
  955. static void trusted_rcu_free(struct rcu_head *rcu)
  956. {
  957. struct trusted_key_payload *p;
  958. p = container_of(rcu, struct trusted_key_payload, rcu);
  959. kzfree(p);
  960. }
  961. /*
  962. * trusted_update - reseal an existing key with new PCR values
  963. */
  964. static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
  965. {
  966. struct trusted_key_payload *p;
  967. struct trusted_key_payload *new_p;
  968. struct trusted_key_options *new_o;
  969. size_t datalen = prep->datalen;
  970. char *datablob;
  971. int ret = 0;
  972. if (key_is_negative(key))
  973. return -ENOKEY;
  974. p = key->payload.data[0];
  975. if (!p->migratable)
  976. return -EPERM;
  977. if (datalen <= 0 || datalen > 32767 || !prep->data)
  978. return -EINVAL;
  979. datablob = kmalloc(datalen + 1, GFP_KERNEL);
  980. if (!datablob)
  981. return -ENOMEM;
  982. new_o = trusted_options_alloc();
  983. if (!new_o) {
  984. ret = -ENOMEM;
  985. goto out;
  986. }
  987. new_p = trusted_payload_alloc(key);
  988. if (!new_p) {
  989. ret = -ENOMEM;
  990. goto out;
  991. }
  992. memcpy(datablob, prep->data, datalen);
  993. datablob[datalen] = '\0';
  994. ret = datablob_parse(datablob, new_p, new_o);
  995. if (ret != Opt_update) {
  996. ret = -EINVAL;
  997. kzfree(new_p);
  998. goto out;
  999. }
  1000. if (!new_o->keyhandle) {
  1001. ret = -EINVAL;
  1002. kzfree(new_p);
  1003. goto out;
  1004. }
  1005. /* copy old key values, and reseal with new pcrs */
  1006. new_p->migratable = p->migratable;
  1007. new_p->key_len = p->key_len;
  1008. memcpy(new_p->key, p->key, p->key_len);
  1009. dump_payload(p);
  1010. dump_payload(new_p);
  1011. ret = key_seal(new_p, new_o);
  1012. if (ret < 0) {
  1013. pr_info("trusted_key: key_seal failed (%d)\n", ret);
  1014. kzfree(new_p);
  1015. goto out;
  1016. }
  1017. if (new_o->pcrlock) {
  1018. ret = pcrlock(new_o->pcrlock);
  1019. if (ret < 0) {
  1020. pr_info("trusted_key: pcrlock failed (%d)\n", ret);
  1021. kzfree(new_p);
  1022. goto out;
  1023. }
  1024. }
  1025. rcu_assign_keypointer(key, new_p);
  1026. call_rcu(&p->rcu, trusted_rcu_free);
  1027. out:
  1028. kzfree(datablob);
  1029. kzfree(new_o);
  1030. return ret;
  1031. }
  1032. /*
  1033. * trusted_read - copy the sealed blob data to userspace in hex.
  1034. * On success, return to userspace the trusted key datablob size.
  1035. */
  1036. static long trusted_read(const struct key *key, char __user *buffer,
  1037. size_t buflen)
  1038. {
  1039. const struct trusted_key_payload *p;
  1040. char *ascii_buf;
  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. ascii_buf = kmalloc_array(2, p->blob_len, GFP_KERNEL);
  1048. if (!ascii_buf)
  1049. return -ENOMEM;
  1050. bufp = ascii_buf;
  1051. for (i = 0; i < p->blob_len; i++)
  1052. bufp = hex_byte_pack(bufp, p->blob[i]);
  1053. if (copy_to_user(buffer, ascii_buf, 2 * p->blob_len) != 0) {
  1054. kzfree(ascii_buf);
  1055. return -EFAULT;
  1056. }
  1057. kzfree(ascii_buf);
  1058. }
  1059. return 2 * p->blob_len;
  1060. }
  1061. /*
  1062. * trusted_destroy - clear and free the key's payload
  1063. */
  1064. static void trusted_destroy(struct key *key)
  1065. {
  1066. kzfree(key->payload.data[0]);
  1067. }
  1068. struct key_type key_type_trusted = {
  1069. .name = "trusted",
  1070. .instantiate = trusted_instantiate,
  1071. .update = trusted_update,
  1072. .destroy = trusted_destroy,
  1073. .describe = user_describe,
  1074. .read = trusted_read,
  1075. };
  1076. EXPORT_SYMBOL_GPL(key_type_trusted);
  1077. static void trusted_shash_release(void)
  1078. {
  1079. if (hashalg)
  1080. crypto_free_shash(hashalg);
  1081. if (hmacalg)
  1082. crypto_free_shash(hmacalg);
  1083. }
  1084. static int __init trusted_shash_alloc(void)
  1085. {
  1086. int ret;
  1087. hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
  1088. if (IS_ERR(hmacalg)) {
  1089. pr_info("trusted_key: could not allocate crypto %s\n",
  1090. hmac_alg);
  1091. return PTR_ERR(hmacalg);
  1092. }
  1093. hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
  1094. if (IS_ERR(hashalg)) {
  1095. pr_info("trusted_key: could not allocate crypto %s\n",
  1096. hash_alg);
  1097. ret = PTR_ERR(hashalg);
  1098. goto hashalg_fail;
  1099. }
  1100. return 0;
  1101. hashalg_fail:
  1102. crypto_free_shash(hmacalg);
  1103. return ret;
  1104. }
  1105. static int __init init_trusted(void)
  1106. {
  1107. int ret;
  1108. ret = trusted_shash_alloc();
  1109. if (ret < 0)
  1110. return ret;
  1111. ret = register_key_type(&key_type_trusted);
  1112. if (ret < 0)
  1113. trusted_shash_release();
  1114. return ret;
  1115. }
  1116. static void __exit cleanup_trusted(void)
  1117. {
  1118. trusted_shash_release();
  1119. unregister_key_type(&key_type_trusted);
  1120. }
  1121. late_initcall(init_trusted);
  1122. module_exit(cleanup_trusted);
  1123. MODULE_LICENSE("GPL");