trusted_tpm2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 IBM Corporation
  4. * Copyright (C) 2014 Intel Corporation
  5. */
  6. #include <linux/asn1_encoder.h>
  7. #include <linux/oid_registry.h>
  8. #include <linux/string.h>
  9. #include <linux/err.h>
  10. #include <linux/tpm.h>
  11. #include <linux/tpm_command.h>
  12. #include <keys/trusted-type.h>
  13. #include <keys/trusted_tpm.h>
  14. #include <linux/unaligned.h>
  15. #include "tpm2key.asn1.h"
  16. static struct tpm2_hash tpm2_hash_map[] = {
  17. {HASH_ALGO_SHA1, TPM_ALG_SHA1},
  18. {HASH_ALGO_SHA256, TPM_ALG_SHA256},
  19. {HASH_ALGO_SHA384, TPM_ALG_SHA384},
  20. {HASH_ALGO_SHA512, TPM_ALG_SHA512},
  21. {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
  22. };
  23. static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
  24. static int tpm2_key_encode(struct trusted_key_payload *payload,
  25. struct trusted_key_options *options,
  26. u8 *src, u32 len)
  27. {
  28. const int SCRATCH_SIZE = PAGE_SIZE;
  29. u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
  30. u8 *work = scratch, *work1;
  31. u8 *end_work = scratch + SCRATCH_SIZE;
  32. u8 *priv, *pub;
  33. u16 priv_len, pub_len;
  34. int ret;
  35. priv_len = get_unaligned_be16(src) + 2;
  36. priv = src;
  37. src += priv_len;
  38. pub_len = get_unaligned_be16(src) + 2;
  39. pub = src;
  40. if (!scratch)
  41. return -ENOMEM;
  42. work = asn1_encode_oid(work, end_work, tpm2key_oid,
  43. asn1_oid_len(tpm2key_oid));
  44. if (options->blobauth_len == 0) {
  45. unsigned char bool[3], *w = bool;
  46. /* tag 0 is emptyAuth */
  47. w = asn1_encode_boolean(w, w + sizeof(bool), true);
  48. if (WARN(IS_ERR(w), "BUG: Boolean failed to encode")) {
  49. ret = PTR_ERR(w);
  50. goto err;
  51. }
  52. work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
  53. }
  54. /*
  55. * Assume both octet strings will encode to a 2 byte definite length
  56. *
  57. * Note: For a well behaved TPM, this warning should never
  58. * trigger, so if it does there's something nefarious going on
  59. */
  60. if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
  61. "BUG: scratch buffer is too small")) {
  62. ret = -EINVAL;
  63. goto err;
  64. }
  65. work = asn1_encode_integer(work, end_work, options->keyhandle);
  66. work = asn1_encode_octet_string(work, end_work, pub, pub_len);
  67. work = asn1_encode_octet_string(work, end_work, priv, priv_len);
  68. work1 = payload->blob;
  69. work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
  70. scratch, work - scratch);
  71. if (IS_ERR(work1)) {
  72. ret = PTR_ERR(work1);
  73. pr_err("BUG: ASN.1 encoder failed with %d\n", ret);
  74. goto err;
  75. }
  76. kfree(scratch);
  77. return work1 - payload->blob;
  78. err:
  79. kfree(scratch);
  80. return ret;
  81. }
  82. struct tpm2_key_context {
  83. u32 parent;
  84. const u8 *pub;
  85. u32 pub_len;
  86. const u8 *priv;
  87. u32 priv_len;
  88. };
  89. static int tpm2_key_decode(struct trusted_key_payload *payload,
  90. struct trusted_key_options *options,
  91. u8 **buf)
  92. {
  93. int ret;
  94. struct tpm2_key_context ctx;
  95. u8 *blob;
  96. memset(&ctx, 0, sizeof(ctx));
  97. ret = asn1_ber_decoder(&tpm2key_decoder, &ctx, payload->blob,
  98. payload->blob_len);
  99. if (ret < 0)
  100. return ret;
  101. if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
  102. return -EINVAL;
  103. blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);
  104. if (!blob)
  105. return -ENOMEM;
  106. *buf = blob;
  107. options->keyhandle = ctx.parent;
  108. memcpy(blob, ctx.priv, ctx.priv_len);
  109. blob += ctx.priv_len;
  110. memcpy(blob, ctx.pub, ctx.pub_len);
  111. return 0;
  112. }
  113. int tpm2_key_parent(void *context, size_t hdrlen,
  114. unsigned char tag,
  115. const void *value, size_t vlen)
  116. {
  117. struct tpm2_key_context *ctx = context;
  118. const u8 *v = value;
  119. int i;
  120. ctx->parent = 0;
  121. for (i = 0; i < vlen; i++) {
  122. ctx->parent <<= 8;
  123. ctx->parent |= v[i];
  124. }
  125. return 0;
  126. }
  127. int tpm2_key_type(void *context, size_t hdrlen,
  128. unsigned char tag,
  129. const void *value, size_t vlen)
  130. {
  131. enum OID oid = look_up_OID(value, vlen);
  132. if (oid != OID_TPMSealedData) {
  133. char buffer[50];
  134. sprint_oid(value, vlen, buffer, sizeof(buffer));
  135. pr_debug("OID is \"%s\" which is not TPMSealedData\n",
  136. buffer);
  137. return -EINVAL;
  138. }
  139. return 0;
  140. }
  141. int tpm2_key_pub(void *context, size_t hdrlen,
  142. unsigned char tag,
  143. const void *value, size_t vlen)
  144. {
  145. struct tpm2_key_context *ctx = context;
  146. ctx->pub = value;
  147. ctx->pub_len = vlen;
  148. return 0;
  149. }
  150. int tpm2_key_priv(void *context, size_t hdrlen,
  151. unsigned char tag,
  152. const void *value, size_t vlen)
  153. {
  154. struct tpm2_key_context *ctx = context;
  155. ctx->priv = value;
  156. ctx->priv_len = vlen;
  157. return 0;
  158. }
  159. /**
  160. * tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
  161. *
  162. * @buf: an allocated tpm_buf instance
  163. * @session_handle: session handle
  164. * @nonce: the session nonce, may be NULL if not used
  165. * @nonce_len: the session nonce length, may be 0 if not used
  166. * @attributes: the session attributes
  167. * @hmac: the session HMAC or password, may be NULL if not used
  168. * @hmac_len: the session HMAC or password length, maybe 0 if not used
  169. */
  170. static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
  171. const u8 *nonce, u16 nonce_len,
  172. u8 attributes,
  173. const u8 *hmac, u16 hmac_len)
  174. {
  175. tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
  176. tpm_buf_append_u32(buf, session_handle);
  177. tpm_buf_append_u16(buf, nonce_len);
  178. if (nonce && nonce_len)
  179. tpm_buf_append(buf, nonce, nonce_len);
  180. tpm_buf_append_u8(buf, attributes);
  181. tpm_buf_append_u16(buf, hmac_len);
  182. if (hmac && hmac_len)
  183. tpm_buf_append(buf, hmac, hmac_len);
  184. }
  185. /**
  186. * tpm2_seal_trusted() - seal the payload of a trusted key
  187. *
  188. * @chip: TPM chip to use
  189. * @payload: the key data in clear and encrypted form
  190. * @options: authentication values and other options
  191. *
  192. * Return: < 0 on error and 0 on success.
  193. */
  194. int tpm2_seal_trusted(struct tpm_chip *chip,
  195. struct trusted_key_payload *payload,
  196. struct trusted_key_options *options)
  197. {
  198. off_t offset = TPM_HEADER_SIZE;
  199. struct tpm_buf buf, sized;
  200. int blob_len = 0;
  201. u32 hash;
  202. u32 flags;
  203. int i;
  204. int rc;
  205. for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
  206. if (options->hash == tpm2_hash_map[i].crypto_id) {
  207. hash = tpm2_hash_map[i].tpm_id;
  208. break;
  209. }
  210. }
  211. if (i == ARRAY_SIZE(tpm2_hash_map))
  212. return -EINVAL;
  213. if (!options->keyhandle)
  214. return -EINVAL;
  215. rc = tpm_try_get_ops(chip);
  216. if (rc)
  217. return rc;
  218. rc = tpm2_start_auth_session(chip);
  219. if (rc)
  220. goto out_put;
  221. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
  222. if (rc) {
  223. tpm2_end_auth_session(chip);
  224. goto out_put;
  225. }
  226. rc = tpm_buf_init_sized(&sized);
  227. if (rc) {
  228. tpm_buf_destroy(&buf);
  229. tpm2_end_auth_session(chip);
  230. goto out_put;
  231. }
  232. tpm_buf_append_name(chip, &buf, options->keyhandle, NULL);
  233. tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_DECRYPT,
  234. options->keyauth, TPM_DIGEST_SIZE);
  235. /* sensitive */
  236. tpm_buf_append_u16(&sized, options->blobauth_len);
  237. if (options->blobauth_len)
  238. tpm_buf_append(&sized, options->blobauth, options->blobauth_len);
  239. tpm_buf_append_u16(&sized, payload->key_len);
  240. tpm_buf_append(&sized, payload->key, payload->key_len);
  241. tpm_buf_append(&buf, sized.data, sized.length);
  242. /* public */
  243. tpm_buf_reset_sized(&sized);
  244. tpm_buf_append_u16(&sized, TPM_ALG_KEYEDHASH);
  245. tpm_buf_append_u16(&sized, hash);
  246. /* key properties */
  247. flags = 0;
  248. flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;
  249. flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT);
  250. tpm_buf_append_u32(&sized, flags);
  251. /* policy */
  252. tpm_buf_append_u16(&sized, options->policydigest_len);
  253. if (options->policydigest_len)
  254. tpm_buf_append(&sized, options->policydigest, options->policydigest_len);
  255. /* public parameters */
  256. tpm_buf_append_u16(&sized, TPM_ALG_NULL);
  257. tpm_buf_append_u16(&sized, 0);
  258. tpm_buf_append(&buf, sized.data, sized.length);
  259. /* outside info */
  260. tpm_buf_append_u16(&buf, 0);
  261. /* creation PCR */
  262. tpm_buf_append_u32(&buf, 0);
  263. if (buf.flags & TPM_BUF_OVERFLOW) {
  264. rc = -E2BIG;
  265. tpm2_end_auth_session(chip);
  266. goto out;
  267. }
  268. tpm_buf_fill_hmac_session(chip, &buf);
  269. rc = tpm_transmit_cmd(chip, &buf, 4, "sealing data");
  270. rc = tpm_buf_check_hmac_response(chip, &buf, rc);
  271. if (rc)
  272. goto out;
  273. blob_len = tpm_buf_read_u32(&buf, &offset);
  274. if (blob_len > MAX_BLOB_SIZE || buf.flags & TPM_BUF_BOUNDARY_ERROR) {
  275. rc = -E2BIG;
  276. goto out;
  277. }
  278. if (buf.length - offset < blob_len) {
  279. rc = -EFAULT;
  280. goto out;
  281. }
  282. blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len);
  283. out:
  284. tpm_buf_destroy(&sized);
  285. tpm_buf_destroy(&buf);
  286. if (rc > 0) {
  287. if (tpm2_rc_value(rc) == TPM2_RC_HASH)
  288. rc = -EINVAL;
  289. else
  290. rc = -EPERM;
  291. }
  292. if (blob_len < 0)
  293. rc = blob_len;
  294. else
  295. payload->blob_len = blob_len;
  296. out_put:
  297. tpm_put_ops(chip);
  298. return rc;
  299. }
  300. /**
  301. * tpm2_load_cmd() - execute a TPM2_Load command
  302. *
  303. * @chip: TPM chip to use
  304. * @payload: the key data in clear and encrypted form
  305. * @options: authentication values and other options
  306. * @blob_handle: returned blob handle
  307. *
  308. * Return: 0 on success.
  309. * -E2BIG on wrong payload size.
  310. * -EPERM on tpm error status.
  311. * < 0 error from tpm_send.
  312. */
  313. static int tpm2_load_cmd(struct tpm_chip *chip,
  314. struct trusted_key_payload *payload,
  315. struct trusted_key_options *options,
  316. u32 *blob_handle)
  317. {
  318. struct tpm_buf buf;
  319. unsigned int private_len;
  320. unsigned int public_len;
  321. unsigned int blob_len;
  322. u8 *blob, *pub;
  323. int rc;
  324. u32 attrs;
  325. rc = tpm2_key_decode(payload, options, &blob);
  326. if (rc) {
  327. /* old form */
  328. blob = payload->blob;
  329. payload->old_format = 1;
  330. }
  331. /* new format carries keyhandle but old format doesn't */
  332. if (!options->keyhandle)
  333. return -EINVAL;
  334. /* must be big enough for at least the two be16 size counts */
  335. if (payload->blob_len < 4)
  336. return -EINVAL;
  337. private_len = get_unaligned_be16(blob);
  338. /* must be big enough for following public_len */
  339. if (private_len + 2 + 2 > (payload->blob_len))
  340. return -E2BIG;
  341. public_len = get_unaligned_be16(blob + 2 + private_len);
  342. if (private_len + 2 + public_len + 2 > payload->blob_len)
  343. return -E2BIG;
  344. pub = blob + 2 + private_len + 2;
  345. /* key attributes are always at offset 4 */
  346. attrs = get_unaligned_be32(pub + 4);
  347. if ((attrs & (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT)) ==
  348. (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT))
  349. payload->migratable = 0;
  350. else
  351. payload->migratable = 1;
  352. blob_len = private_len + public_len + 4;
  353. if (blob_len > payload->blob_len)
  354. return -E2BIG;
  355. rc = tpm2_start_auth_session(chip);
  356. if (rc)
  357. return rc;
  358. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
  359. if (rc) {
  360. tpm2_end_auth_session(chip);
  361. return rc;
  362. }
  363. tpm_buf_append_name(chip, &buf, options->keyhandle, NULL);
  364. tpm_buf_append_hmac_session(chip, &buf, 0, options->keyauth,
  365. TPM_DIGEST_SIZE);
  366. tpm_buf_append(&buf, blob, blob_len);
  367. if (buf.flags & TPM_BUF_OVERFLOW) {
  368. rc = -E2BIG;
  369. tpm2_end_auth_session(chip);
  370. goto out;
  371. }
  372. tpm_buf_fill_hmac_session(chip, &buf);
  373. rc = tpm_transmit_cmd(chip, &buf, 4, "loading blob");
  374. rc = tpm_buf_check_hmac_response(chip, &buf, rc);
  375. if (!rc)
  376. *blob_handle = be32_to_cpup(
  377. (__be32 *) &buf.data[TPM_HEADER_SIZE]);
  378. out:
  379. if (blob != payload->blob)
  380. kfree(blob);
  381. tpm_buf_destroy(&buf);
  382. if (rc > 0)
  383. rc = -EPERM;
  384. return rc;
  385. }
  386. /**
  387. * tpm2_unseal_cmd() - execute a TPM2_Unload command
  388. *
  389. * @chip: TPM chip to use
  390. * @payload: the key data in clear and encrypted form
  391. * @options: authentication values and other options
  392. * @blob_handle: blob handle
  393. *
  394. * Return: 0 on success
  395. * -EPERM on tpm error status
  396. * < 0 error from tpm_send
  397. */
  398. static int tpm2_unseal_cmd(struct tpm_chip *chip,
  399. struct trusted_key_payload *payload,
  400. struct trusted_key_options *options,
  401. u32 blob_handle)
  402. {
  403. struct tpm_buf buf;
  404. u16 data_len;
  405. u8 *data;
  406. int rc;
  407. rc = tpm2_start_auth_session(chip);
  408. if (rc)
  409. return rc;
  410. rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
  411. if (rc) {
  412. tpm2_end_auth_session(chip);
  413. return rc;
  414. }
  415. tpm_buf_append_name(chip, &buf, blob_handle, NULL);
  416. if (!options->policyhandle) {
  417. tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT,
  418. options->blobauth,
  419. options->blobauth_len);
  420. } else {
  421. /*
  422. * FIXME: The policy session was generated outside the
  423. * kernel so we don't known the nonce and thus can't
  424. * calculate a HMAC on it. Therefore, the user can
  425. * only really use TPM2_PolicyPassword and we must
  426. * send down the plain text password, which could be
  427. * intercepted. We can still encrypt the returned
  428. * key, but that's small comfort since the interposer
  429. * could repeat our actions with the exfiltrated
  430. * password.
  431. */
  432. tpm2_buf_append_auth(&buf, options->policyhandle,
  433. NULL /* nonce */, 0, 0,
  434. options->blobauth, options->blobauth_len);
  435. tpm_buf_append_hmac_session_opt(chip, &buf, TPM2_SA_ENCRYPT,
  436. NULL, 0);
  437. }
  438. tpm_buf_fill_hmac_session(chip, &buf);
  439. rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
  440. rc = tpm_buf_check_hmac_response(chip, &buf, rc);
  441. if (rc > 0)
  442. rc = -EPERM;
  443. if (!rc) {
  444. data_len = be16_to_cpup(
  445. (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
  446. if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE) {
  447. rc = -EFAULT;
  448. goto out;
  449. }
  450. if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
  451. rc = -EFAULT;
  452. goto out;
  453. }
  454. data = &buf.data[TPM_HEADER_SIZE + 6];
  455. if (payload->old_format) {
  456. /* migratable flag is at the end of the key */
  457. memcpy(payload->key, data, data_len - 1);
  458. payload->key_len = data_len - 1;
  459. payload->migratable = data[data_len - 1];
  460. } else {
  461. /*
  462. * migratable flag already collected from key
  463. * attributes
  464. */
  465. memcpy(payload->key, data, data_len);
  466. payload->key_len = data_len;
  467. }
  468. }
  469. out:
  470. tpm_buf_destroy(&buf);
  471. return rc;
  472. }
  473. /**
  474. * tpm2_unseal_trusted() - unseal the payload of a trusted key
  475. *
  476. * @chip: TPM chip to use
  477. * @payload: the key data in clear and encrypted form
  478. * @options: authentication values and other options
  479. *
  480. * Return: Same as with tpm_send.
  481. */
  482. int tpm2_unseal_trusted(struct tpm_chip *chip,
  483. struct trusted_key_payload *payload,
  484. struct trusted_key_options *options)
  485. {
  486. u32 blob_handle;
  487. int rc;
  488. rc = tpm_try_get_ops(chip);
  489. if (rc)
  490. return rc;
  491. rc = tpm2_load_cmd(chip, payload, options, &blob_handle);
  492. if (rc)
  493. goto out;
  494. rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
  495. tpm2_flush_context(chip, blob_handle);
  496. out:
  497. tpm_put_ops(chip);
  498. return rc;
  499. }