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