trusted_tpm2.c 12 KB

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