trusted.c 29 KB

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