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 <linux/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
;
43 priv_len
= get_unaligned_be16(src
) + 2;
48 pub_len
= get_unaligned_be16(src
) + 2;
54 work
= asn1_encode_oid(work
, end_work
, tpm2key_oid
,
55 asn1_oid_len(tpm2key_oid
));
57 if (options
->blobauth_len
== 0) {
58 unsigned char bool[3], *w
= bool;
59 /* tag 0 is emptyAuth */
60 w
= asn1_encode_boolean(w
, w
+ sizeof(bool), true);
61 if (WARN(IS_ERR(w
), "BUG: Boolean failed to encode")) {
65 work
= asn1_encode_tag(work
, end_work
, 0, bool, w
- bool);
69 * Assume both octet strings will encode to a 2 byte definite length
71 * Note: For a well behaved TPM, this warning should never
72 * trigger, so if it does there's something nefarious going on
74 if (WARN(work
- scratch
+ pub_len
+ priv_len
+ 14 > SCRATCH_SIZE
,
75 "BUG: scratch buffer is too small")) {
80 work
= asn1_encode_integer(work
, end_work
, options
->keyhandle
);
81 work
= asn1_encode_octet_string(work
, end_work
, pub
, pub_len
);
82 work
= asn1_encode_octet_string(work
, end_work
, priv
, priv_len
);
84 work1
= payload
->blob
;
85 work1
= asn1_encode_sequence(work1
, work1
+ sizeof(payload
->blob
),
86 scratch
, work
- scratch
);
89 pr_err("BUG: ASN.1 encoder failed with %d\n", ret
);
94 return work1
- payload
->blob
;
101 struct tpm2_key_context
{
109 static int tpm2_key_decode(struct trusted_key_payload
*payload
,
110 struct trusted_key_options
*options
,
114 struct tpm2_key_context ctx
;
117 memset(&ctx
, 0, sizeof(ctx
));
119 ret
= asn1_ber_decoder(&tpm2key_decoder
, &ctx
, payload
->blob
,
124 if (ctx
.priv_len
+ ctx
.pub_len
> MAX_BLOB_SIZE
)
127 blob
= kmalloc(ctx
.priv_len
+ ctx
.pub_len
+ 4, GFP_KERNEL
);
132 options
->keyhandle
= ctx
.parent
;
134 memcpy(blob
, ctx
.priv
, ctx
.priv_len
);
135 blob
+= ctx
.priv_len
;
137 memcpy(blob
, ctx
.pub
, ctx
.pub_len
);
142 int tpm2_key_parent(void *context
, size_t hdrlen
,
144 const void *value
, size_t vlen
)
146 struct tpm2_key_context
*ctx
= context
;
151 for (i
= 0; i
< vlen
; i
++) {
159 int tpm2_key_type(void *context
, size_t hdrlen
,
161 const void *value
, size_t vlen
)
163 enum OID oid
= look_up_OID(value
, vlen
);
165 if (oid
!= OID_TPMSealedData
) {
168 sprint_oid(value
, vlen
, buffer
, sizeof(buffer
));
169 pr_debug("OID is \"%s\" which is not TPMSealedData\n",
177 int tpm2_key_pub(void *context
, size_t hdrlen
,
179 const void *value
, size_t vlen
)
181 struct tpm2_key_context
*ctx
= context
;
189 int tpm2_key_priv(void *context
, size_t hdrlen
,
191 const void *value
, size_t vlen
)
193 struct tpm2_key_context
*ctx
= context
;
196 ctx
->priv_len
= vlen
;
202 * tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
204 * @buf: an allocated tpm_buf instance
205 * @session_handle: session handle
206 * @nonce: the session nonce, may be NULL if not used
207 * @nonce_len: the session nonce length, may be 0 if not used
208 * @attributes: the session attributes
209 * @hmac: the session HMAC or password, may be NULL if not used
210 * @hmac_len: the session HMAC or password length, maybe 0 if not used
212 static void tpm2_buf_append_auth(struct tpm_buf
*buf
, u32 session_handle
,
213 const u8
*nonce
, u16 nonce_len
,
215 const u8
*hmac
, u16 hmac_len
)
217 tpm_buf_append_u32(buf
, 9 + nonce_len
+ hmac_len
);
218 tpm_buf_append_u32(buf
, session_handle
);
219 tpm_buf_append_u16(buf
, nonce_len
);
221 if (nonce
&& nonce_len
)
222 tpm_buf_append(buf
, nonce
, nonce_len
);
224 tpm_buf_append_u8(buf
, attributes
);
225 tpm_buf_append_u16(buf
, hmac_len
);
227 if (hmac
&& hmac_len
)
228 tpm_buf_append(buf
, hmac
, hmac_len
);
232 * tpm2_seal_trusted() - seal the payload of a trusted key
234 * @chip: TPM chip to use
235 * @payload: the key data in clear and encrypted form
236 * @options: authentication values and other options
238 * Return: < 0 on error and 0 on success.
240 int tpm2_seal_trusted(struct tpm_chip
*chip
,
241 struct trusted_key_payload
*payload
,
242 struct trusted_key_options
*options
)
244 off_t offset
= TPM_HEADER_SIZE
;
245 struct tpm_buf buf
, sized
;
252 for (i
= 0; i
< ARRAY_SIZE(tpm2_hash_map
); i
++) {
253 if (options
->hash
== tpm2_hash_map
[i
].crypto_id
) {
254 hash
= tpm2_hash_map
[i
].tpm_id
;
259 if (i
== ARRAY_SIZE(tpm2_hash_map
))
262 if (!options
->keyhandle
)
265 rc
= tpm_try_get_ops(chip
);
269 rc
= tpm2_start_auth_session(chip
);
273 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
, TPM2_CC_CREATE
);
275 tpm2_end_auth_session(chip
);
279 rc
= tpm_buf_init_sized(&sized
);
281 tpm_buf_destroy(&buf
);
282 tpm2_end_auth_session(chip
);
286 tpm_buf_append_name(chip
, &buf
, options
->keyhandle
, NULL
);
287 tpm_buf_append_hmac_session(chip
, &buf
, TPM2_SA_DECRYPT
,
288 options
->keyauth
, TPM_DIGEST_SIZE
);
291 tpm_buf_append_u16(&sized
, options
->blobauth_len
);
293 if (options
->blobauth_len
)
294 tpm_buf_append(&sized
, options
->blobauth
, options
->blobauth_len
);
296 tpm_buf_append_u16(&sized
, payload
->key_len
);
297 tpm_buf_append(&sized
, payload
->key
, payload
->key_len
);
298 tpm_buf_append(&buf
, sized
.data
, sized
.length
);
301 tpm_buf_reset_sized(&sized
);
302 tpm_buf_append_u16(&sized
, TPM_ALG_KEYEDHASH
);
303 tpm_buf_append_u16(&sized
, hash
);
307 flags
|= options
->policydigest_len
? 0 : TPM2_OA_USER_WITH_AUTH
;
308 flags
|= payload
->migratable
? 0 : (TPM2_OA_FIXED_TPM
| TPM2_OA_FIXED_PARENT
);
309 tpm_buf_append_u32(&sized
, flags
);
312 tpm_buf_append_u16(&sized
, options
->policydigest_len
);
313 if (options
->policydigest_len
)
314 tpm_buf_append(&sized
, options
->policydigest
, options
->policydigest_len
);
316 /* public parameters */
317 tpm_buf_append_u16(&sized
, TPM_ALG_NULL
);
318 tpm_buf_append_u16(&sized
, 0);
320 tpm_buf_append(&buf
, sized
.data
, sized
.length
);
323 tpm_buf_append_u16(&buf
, 0);
326 tpm_buf_append_u32(&buf
, 0);
328 if (buf
.flags
& TPM_BUF_OVERFLOW
) {
330 tpm2_end_auth_session(chip
);
334 tpm_buf_fill_hmac_session(chip
, &buf
);
335 rc
= tpm_transmit_cmd(chip
, &buf
, 4, "sealing data");
336 rc
= tpm_buf_check_hmac_response(chip
, &buf
, rc
);
340 blob_len
= tpm_buf_read_u32(&buf
, &offset
);
341 if (blob_len
> MAX_BLOB_SIZE
|| buf
.flags
& TPM_BUF_BOUNDARY_ERROR
) {
345 if (buf
.length
- offset
< blob_len
) {
350 blob_len
= tpm2_key_encode(payload
, options
, &buf
.data
[offset
], blob_len
);
353 tpm_buf_destroy(&sized
);
354 tpm_buf_destroy(&buf
);
357 if (tpm2_rc_value(rc
) == TPM2_RC_HASH
)
365 payload
->blob_len
= blob_len
;
373 * tpm2_load_cmd() - execute a TPM2_Load command
375 * @chip: TPM chip to use
376 * @payload: the key data in clear and encrypted form
377 * @options: authentication values and other options
378 * @blob_handle: returned blob handle
380 * Return: 0 on success.
381 * -E2BIG on wrong payload size.
382 * -EPERM on tpm error status.
383 * < 0 error from tpm_send.
385 static int tpm2_load_cmd(struct tpm_chip
*chip
,
386 struct trusted_key_payload
*payload
,
387 struct trusted_key_options
*options
,
391 unsigned int private_len
;
392 unsigned int public_len
;
393 unsigned int blob_len
;
398 rc
= tpm2_key_decode(payload
, options
, &blob
);
401 blob
= payload
->blob
;
402 payload
->old_format
= 1;
405 /* new format carries keyhandle but old format doesn't */
406 if (!options
->keyhandle
)
409 /* must be big enough for at least the two be16 size counts */
410 if (payload
->blob_len
< 4)
413 private_len
= get_unaligned_be16(blob
);
415 /* must be big enough for following public_len */
416 if (private_len
+ 2 + 2 > (payload
->blob_len
))
419 public_len
= get_unaligned_be16(blob
+ 2 + private_len
);
420 if (private_len
+ 2 + public_len
+ 2 > payload
->blob_len
)
423 pub
= blob
+ 2 + private_len
+ 2;
424 /* key attributes are always at offset 4 */
425 attrs
= get_unaligned_be32(pub
+ 4);
427 if ((attrs
& (TPM2_OA_FIXED_TPM
| TPM2_OA_FIXED_PARENT
)) ==
428 (TPM2_OA_FIXED_TPM
| TPM2_OA_FIXED_PARENT
))
429 payload
->migratable
= 0;
431 payload
->migratable
= 1;
433 blob_len
= private_len
+ public_len
+ 4;
434 if (blob_len
> payload
->blob_len
)
437 rc
= tpm2_start_auth_session(chip
);
441 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
, TPM2_CC_LOAD
);
443 tpm2_end_auth_session(chip
);
447 tpm_buf_append_name(chip
, &buf
, options
->keyhandle
, NULL
);
448 tpm_buf_append_hmac_session(chip
, &buf
, 0, options
->keyauth
,
451 tpm_buf_append(&buf
, blob
, blob_len
);
453 if (buf
.flags
& TPM_BUF_OVERFLOW
) {
455 tpm2_end_auth_session(chip
);
459 tpm_buf_fill_hmac_session(chip
, &buf
);
460 rc
= tpm_transmit_cmd(chip
, &buf
, 4, "loading blob");
461 rc
= tpm_buf_check_hmac_response(chip
, &buf
, rc
);
463 *blob_handle
= be32_to_cpup(
464 (__be32
*) &buf
.data
[TPM_HEADER_SIZE
]);
467 if (blob
!= payload
->blob
)
469 tpm_buf_destroy(&buf
);
478 * tpm2_unseal_cmd() - execute a TPM2_Unload command
480 * @chip: TPM chip to use
481 * @payload: the key data in clear and encrypted form
482 * @options: authentication values and other options
483 * @blob_handle: blob handle
485 * Return: 0 on success
486 * -EPERM on tpm error status
487 * < 0 error from tpm_send
489 static int tpm2_unseal_cmd(struct tpm_chip
*chip
,
490 struct trusted_key_payload
*payload
,
491 struct trusted_key_options
*options
,
499 rc
= tpm2_start_auth_session(chip
);
503 rc
= tpm_buf_init(&buf
, TPM2_ST_SESSIONS
, TPM2_CC_UNSEAL
);
505 tpm2_end_auth_session(chip
);
509 tpm_buf_append_name(chip
, &buf
, blob_handle
, NULL
);
511 if (!options
->policyhandle
) {
512 tpm_buf_append_hmac_session(chip
, &buf
, TPM2_SA_ENCRYPT
,
514 options
->blobauth_len
);
517 * FIXME: The policy session was generated outside the
518 * kernel so we don't known the nonce and thus can't
519 * calculate a HMAC on it. Therefore, the user can
520 * only really use TPM2_PolicyPassword and we must
521 * send down the plain text password, which could be
522 * intercepted. We can still encrypt the returned
523 * key, but that's small comfort since the interposer
524 * could repeat our actions with the exfiltrated
527 tpm2_buf_append_auth(&buf
, options
->policyhandle
,
528 NULL
/* nonce */, 0, 0,
529 options
->blobauth
, options
->blobauth_len
);
530 tpm_buf_append_hmac_session_opt(chip
, &buf
, TPM2_SA_ENCRYPT
,
534 tpm_buf_fill_hmac_session(chip
, &buf
);
535 rc
= tpm_transmit_cmd(chip
, &buf
, 6, "unsealing");
536 rc
= tpm_buf_check_hmac_response(chip
, &buf
, rc
);
541 data_len
= be16_to_cpup(
542 (__be16
*) &buf
.data
[TPM_HEADER_SIZE
+ 4]);
543 if (data_len
< MIN_KEY_SIZE
|| data_len
> MAX_KEY_SIZE
) {
548 if (tpm_buf_length(&buf
) < TPM_HEADER_SIZE
+ 6 + data_len
) {
552 data
= &buf
.data
[TPM_HEADER_SIZE
+ 6];
554 if (payload
->old_format
) {
555 /* migratable flag is at the end of the key */
556 memcpy(payload
->key
, data
, data_len
- 1);
557 payload
->key_len
= data_len
- 1;
558 payload
->migratable
= data
[data_len
- 1];
561 * migratable flag already collected from key
564 memcpy(payload
->key
, data
, data_len
);
565 payload
->key_len
= data_len
;
570 tpm_buf_destroy(&buf
);
575 * tpm2_unseal_trusted() - unseal the payload of a trusted key
577 * @chip: TPM chip to use
578 * @payload: the key data in clear and encrypted form
579 * @options: authentication values and other options
581 * Return: Same as with tpm_send.
583 int tpm2_unseal_trusted(struct tpm_chip
*chip
,
584 struct trusted_key_payload
*payload
,
585 struct trusted_key_options
*options
)
590 rc
= tpm_try_get_ops(chip
);
594 rc
= tpm2_load_cmd(chip
, payload
, options
, &blob_handle
);
598 rc
= tpm2_unseal_cmd(chip
, payload
, options
, blob_handle
);
599 tpm2_flush_context(chip
, blob_handle
);