trusted_defined.c 28 KB

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