trusted.c 29 KB

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