trusted_defined.c 28 KB

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