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