trusted_defined.c 28 KB

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