1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2004 IBM Corporation
4 * Copyright (C) 2014 Intel Corporation
7 #include <linux/asn1_encoder.h>
8 #include <linux/oid_registry.h>
9 #include <linux/string.h>
10 #include <linux/err.h>
11 #include <linux/tpm.h>
12 #include <linux/tpm_command.h>
14 #include <keys/trusted-type.h>
15 #include <keys/trusted_tpm.h>
17 #include <asm/unaligned.h>
19 #include "tpm2key.asn1.h"
21 static struct tpm2_hash tpm2_hash_map[] = {
22 {HASH_ALGO_SHA1, TPM_ALG_SHA1},
23 {HASH_ALGO_SHA256, TPM_ALG_SHA256},
24 {HASH_ALGO_SHA384, TPM_ALG_SHA384},
25 {HASH_ALGO_SHA512, TPM_ALG_SHA512},
26 {HASH_ALGO_SM3_256, TPM_ALG_SM3_256},
29 static u32 tpm2key_oid[] = { 2, 23, 133, 10, 1, 5 };
31 static int tpm2_key_encode(struct trusted_key_payload *payload,
32 struct trusted_key_options *options,
35 const int SCRATCH_SIZE = PAGE_SIZE;
36 u8 *scratch = kmalloc(SCRATCH_SIZE, GFP_KERNEL);
37 u8 *work = scratch, *work1;
38 u8 *end_work = scratch + SCRATCH_SIZE;
40 u16 priv_len, pub_len;
42 priv_len = get_unaligned_be16(src) + 2;
47 pub_len = get_unaligned_be16(src) + 2;
53 work = asn1_encode_oid(work, end_work, tpm2key_oid,
54 asn1_oid_len(tpm2key_oid));
56 if (options->blobauth_len == 0) {
57 unsigned char bool[3], *w = bool;
58 /* tag 0 is emptyAuth */
59 w = asn1_encode_boolean(w, w + sizeof(bool), true);
60 if (WARN(IS_ERR(w), "BUG: Boolean failed to encode"))
62 work = asn1_encode_tag(work, end_work, 0, bool, w - bool);
66 * Assume both octet strings will encode to a 2 byte definite length
68 * Note: For a well behaved TPM, this warning should never
69 * trigger, so if it does there's something nefarious going on
71 if (WARN(work - scratch + pub_len + priv_len + 14 > SCRATCH_SIZE,
72 "BUG: scratch buffer is too small"))
75 work = asn1_encode_integer(work, end_work, options->keyhandle);
76 work = asn1_encode_octet_string(work, end_work, pub, pub_len);
77 work = asn1_encode_octet_string(work, end_work, priv, priv_len);
79 work1 = payload->blob;
80 work1 = asn1_encode_sequence(work1, work1 + sizeof(payload->blob),
81 scratch, work - scratch);
82 if (WARN(IS_ERR(work1), "BUG: ASN.1 encoder failed"))
83 return PTR_ERR(work1);
85 return work1 - payload->blob;
88 struct tpm2_key_context {
96 static int tpm2_key_decode(struct trusted_key_payload *payload,
97 struct trusted_key_options *options,
101 struct tpm2_key_context ctx;
104 memset(&ctx, 0, sizeof(ctx));
106 ret = asn1_ber_decoder(&tpm2key_decoder, &ctx, payload->blob,
111 if (ctx.priv_len + ctx.pub_len > MAX_BLOB_SIZE)
114 blob = kmalloc(ctx.priv_len + ctx.pub_len + 4, GFP_KERNEL);
119 options->keyhandle = ctx.parent;
121 memcpy(blob, ctx.priv, ctx.priv_len);
122 blob += ctx.priv_len;
124 memcpy(blob, ctx.pub, ctx.pub_len);
129 int tpm2_key_parent(void *context, size_t hdrlen,
131 const void *value, size_t vlen)
133 struct tpm2_key_context *ctx = context;
138 for (i = 0; i < vlen; i++) {
146 int tpm2_key_type(void *context, size_t hdrlen,
148 const void *value, size_t vlen)
150 enum OID oid = look_up_OID(value, vlen);
152 if (oid != OID_TPMSealedData) {
155 sprint_oid(value, vlen, buffer, sizeof(buffer));
156 pr_debug("OID is \"%s\" which is not TPMSealedData\n",
164 int tpm2_key_pub(void *context, size_t hdrlen,
166 const void *value, size_t vlen)
168 struct tpm2_key_context *ctx = context;
176 int tpm2_key_priv(void *context, size_t hdrlen,
178 const void *value, size_t vlen)
180 struct tpm2_key_context *ctx = context;
183 ctx->priv_len = vlen;
189 * tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
191 * @buf: an allocated tpm_buf instance
192 * @session_handle: session handle
193 * @nonce: the session nonce, may be NULL if not used
194 * @nonce_len: the session nonce length, may be 0 if not used
195 * @attributes: the session attributes
196 * @hmac: the session HMAC or password, may be NULL if not used
197 * @hmac_len: the session HMAC or password length, maybe 0 if not used
199 static void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,
200 const u8 *nonce, u16 nonce_len,
202 const u8 *hmac, u16 hmac_len)
204 tpm_buf_append_u32(buf, 9 + nonce_len + hmac_len);
205 tpm_buf_append_u32(buf, session_handle);
206 tpm_buf_append_u16(buf, nonce_len);
208 if (nonce && nonce_len)
209 tpm_buf_append(buf, nonce, nonce_len);
211 tpm_buf_append_u8(buf, attributes);
212 tpm_buf_append_u16(buf, hmac_len);
214 if (hmac && hmac_len)
215 tpm_buf_append(buf, hmac, hmac_len);
219 * tpm2_seal_trusted() - seal the payload of a trusted key
221 * @chip: TPM chip to use
222 * @payload: the key data in clear and encrypted form
223 * @options: authentication values and other options
225 * Return: < 0 on error and 0 on success.
227 int tpm2_seal_trusted(struct tpm_chip *chip,
228 struct trusted_key_payload *payload,
229 struct trusted_key_options *options)
231 off_t offset = TPM_HEADER_SIZE;
232 struct tpm_buf buf, sized;
239 for (i = 0; i < ARRAY_SIZE(tpm2_hash_map); i++) {
240 if (options->hash == tpm2_hash_map[i].crypto_id) {
241 hash = tpm2_hash_map[i].tpm_id;
246 if (i == ARRAY_SIZE(tpm2_hash_map))
249 if (!options->keyhandle)
252 rc = tpm_try_get_ops(chip);
256 rc = tpm2_start_auth_session(chip);
260 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
262 tpm2_end_auth_session(chip);
266 rc = tpm_buf_init_sized(&sized);
268 tpm_buf_destroy(&buf);
269 tpm2_end_auth_session(chip);
273 tpm_buf_append_name(chip, &buf, options->keyhandle, NULL);
274 tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_DECRYPT,
275 options->keyauth, TPM_DIGEST_SIZE);
278 tpm_buf_append_u16(&sized, options->blobauth_len);
280 if (options->blobauth_len)
281 tpm_buf_append(&sized, options->blobauth, options->blobauth_len);
283 tpm_buf_append_u16(&sized, payload->key_len);
284 tpm_buf_append(&sized, payload->key, payload->key_len);
285 tpm_buf_append(&buf, sized.data, sized.length);
288 tpm_buf_reset_sized(&sized);
289 tpm_buf_append_u16(&sized, TPM_ALG_KEYEDHASH);
290 tpm_buf_append_u16(&sized, hash);
294 flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;
295 flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT);
296 tpm_buf_append_u32(&sized, flags);
299 tpm_buf_append_u16(&sized, options->policydigest_len);
300 if (options->policydigest_len)
301 tpm_buf_append(&sized, options->policydigest, options->policydigest_len);
303 /* public parameters */
304 tpm_buf_append_u16(&sized, TPM_ALG_NULL);
305 tpm_buf_append_u16(&sized, 0);
307 tpm_buf_append(&buf, sized.data, sized.length);
310 tpm_buf_append_u16(&buf, 0);
313 tpm_buf_append_u32(&buf, 0);
315 if (buf.flags & TPM_BUF_OVERFLOW) {
317 tpm2_end_auth_session(chip);
321 tpm_buf_fill_hmac_session(chip, &buf);
322 rc = tpm_transmit_cmd(chip, &buf, 4, "sealing data");
323 rc = tpm_buf_check_hmac_response(chip, &buf, rc);
327 blob_len = tpm_buf_read_u32(&buf, &offset);
328 if (blob_len > MAX_BLOB_SIZE || buf.flags & TPM_BUF_BOUNDARY_ERROR) {
332 if (buf.length - offset < blob_len) {
337 blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len);
340 tpm_buf_destroy(&sized);
341 tpm_buf_destroy(&buf);
344 if (tpm2_rc_value(rc) == TPM2_RC_HASH)
352 payload->blob_len = blob_len;
360 * tpm2_load_cmd() - execute a TPM2_Load command
362 * @chip: TPM chip to use
363 * @payload: the key data in clear and encrypted form
364 * @options: authentication values and other options
365 * @blob_handle: returned blob handle
367 * Return: 0 on success.
368 * -E2BIG on wrong payload size.
369 * -EPERM on tpm error status.
370 * < 0 error from tpm_send.
372 static int tpm2_load_cmd(struct tpm_chip *chip,
373 struct trusted_key_payload *payload,
374 struct trusted_key_options *options,
378 unsigned int private_len;
379 unsigned int public_len;
380 unsigned int blob_len;
385 rc = tpm2_key_decode(payload, options, &blob);
388 blob = payload->blob;
389 payload->old_format = 1;
392 /* new format carries keyhandle but old format doesn't */
393 if (!options->keyhandle)
396 /* must be big enough for at least the two be16 size counts */
397 if (payload->blob_len < 4)
400 private_len = get_unaligned_be16(blob);
402 /* must be big enough for following public_len */
403 if (private_len + 2 + 2 > (payload->blob_len))
406 public_len = get_unaligned_be16(blob + 2 + private_len);
407 if (private_len + 2 + public_len + 2 > payload->blob_len)
410 pub = blob + 2 + private_len + 2;
411 /* key attributes are always at offset 4 */
412 attrs = get_unaligned_be32(pub + 4);
414 if ((attrs & (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT)) ==
415 (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT))
416 payload->migratable = 0;
418 payload->migratable = 1;
420 blob_len = private_len + public_len + 4;
421 if (blob_len > payload->blob_len)
424 rc = tpm2_start_auth_session(chip);
428 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
430 tpm2_end_auth_session(chip);
434 tpm_buf_append_name(chip, &buf, options->keyhandle, NULL);
435 tpm_buf_append_hmac_session(chip, &buf, 0, options->keyauth,
438 tpm_buf_append(&buf, blob, blob_len);
440 if (buf.flags & TPM_BUF_OVERFLOW) {
442 tpm2_end_auth_session(chip);
446 tpm_buf_fill_hmac_session(chip, &buf);
447 rc = tpm_transmit_cmd(chip, &buf, 4, "loading blob");
448 rc = tpm_buf_check_hmac_response(chip, &buf, rc);
450 *blob_handle = be32_to_cpup(
451 (__be32 *) &buf.data[TPM_HEADER_SIZE]);
454 if (blob != payload->blob)
456 tpm_buf_destroy(&buf);
465 * tpm2_unseal_cmd() - execute a TPM2_Unload command
467 * @chip: TPM chip to use
468 * @payload: the key data in clear and encrypted form
469 * @options: authentication values and other options
470 * @blob_handle: blob handle
472 * Return: 0 on success
473 * -EPERM on tpm error status
474 * < 0 error from tpm_send
476 static int tpm2_unseal_cmd(struct tpm_chip *chip,
477 struct trusted_key_payload *payload,
478 struct trusted_key_options *options,
486 rc = tpm2_start_auth_session(chip);
490 rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
492 tpm2_end_auth_session(chip);
496 tpm_buf_append_name(chip, &buf, blob_handle, NULL);
498 if (!options->policyhandle) {
499 tpm_buf_append_hmac_session(chip, &buf, TPM2_SA_ENCRYPT,
501 options->blobauth_len);
504 * FIXME: The policy session was generated outside the
505 * kernel so we don't known the nonce and thus can't
506 * calculate a HMAC on it. Therefore, the user can
507 * only really use TPM2_PolicyPassword and we must
508 * send down the plain text password, which could be
509 * intercepted. We can still encrypt the returned
510 * key, but that's small comfort since the interposer
511 * could repeat our actions with the exfiltrated
514 tpm2_buf_append_auth(&buf, options->policyhandle,
515 NULL /* nonce */, 0, 0,
516 options->blobauth, options->blobauth_len);
517 tpm_buf_append_hmac_session_opt(chip, &buf, TPM2_SA_ENCRYPT,
521 tpm_buf_fill_hmac_session(chip, &buf);
522 rc = tpm_transmit_cmd(chip, &buf, 6, "unsealing");
523 rc = tpm_buf_check_hmac_response(chip, &buf, rc);
528 data_len = be16_to_cpup(
529 (__be16 *) &buf.data[TPM_HEADER_SIZE + 4]);
530 if (data_len < MIN_KEY_SIZE || data_len > MAX_KEY_SIZE) {
535 if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 6 + data_len) {
539 data = &buf.data[TPM_HEADER_SIZE + 6];
541 if (payload->old_format) {
542 /* migratable flag is at the end of the key */
543 memcpy(payload->key, data, data_len - 1);
544 payload->key_len = data_len - 1;
545 payload->migratable = data[data_len - 1];
548 * migratable flag already collected from key
551 memcpy(payload->key, data, data_len);
552 payload->key_len = data_len;
557 tpm_buf_destroy(&buf);
562 * tpm2_unseal_trusted() - unseal the payload of a trusted key
564 * @chip: TPM chip to use
565 * @payload: the key data in clear and encrypted form
566 * @options: authentication values and other options
568 * Return: Same as with tpm_send.
570 int tpm2_unseal_trusted(struct tpm_chip *chip,
571 struct trusted_key_payload *payload,
572 struct trusted_key_options *options)
577 rc = tpm_try_get_ops(chip);
581 rc = tpm2_load_cmd(chip, payload, options, &blob_handle);
585 rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
586 tpm2_flush_context(chip, blob_handle);