xref: /linux/security/keys/trusted-keys/trusted_tpm2.c (revision 5dd1818b15d98d4a20806cd00b1b40320b06004f)
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 
tpm2_key_encode(struct trusted_key_payload * payload,struct trusted_key_options * options,u8 * src,u32 len)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 
tpm2_key_decode(struct trusted_key_payload * payload,struct trusted_key_options * options,u8 ** buf,unsigned int * blob_len)100 static int tpm2_key_decode(struct trusted_key_payload *payload,
101 			   struct trusted_key_options *options,
102 			   u8 **buf, unsigned int *blob_len)
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 	*blob_len = ctx.priv_len + ctx.pub_len;
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 
tpm2_key_parent(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)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 
tpm2_key_type(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)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 
tpm2_key_pub(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)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 
tpm2_key_priv(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)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  */
tpm2_buf_append_auth(struct tpm_buf * buf,u32 session_handle,const u8 * nonce,u16 nonce_len,u8 attributes,const u8 * hmac,u16 hmac_len)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  */
tpm2_seal_trusted(struct tpm_chip * chip,struct trusted_key_payload * payload,struct trusted_key_options * options)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 __free(kfree) = NULL;
238 	struct tpm_buf *sized __free(kfree) = NULL;
239 	int blob_len = 0;
240 	int hash;
241 	u32 flags;
242 	int rc;
243 
244 	hash = tpm2_find_hash_alg(options->hash);
245 	if (hash < 0)
246 		return hash;
247 
248 	if (!options->keyhandle)
249 		return -EINVAL;
250 
251 	rc = tpm_try_get_ops(chip);
252 	if (rc)
253 		return rc;
254 
255 	rc = tpm2_start_auth_session(chip);
256 	if (rc)
257 		goto out_put;
258 
259 	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
260 	if (!buf) {
261 		rc = -ENOMEM;
262 		tpm2_end_auth_session(chip);
263 		goto out_put;
264 	}
265 
266 	tpm_buf_init(buf, TPM_BUFSIZE);
267 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
268 
269 	sized = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
270 	if (!sized) {
271 		rc = -ENOMEM;
272 		tpm2_end_auth_session(chip);
273 		goto out_put;
274 	}
275 
276 	tpm_buf_init_sized(sized, TPM_BUFSIZE);
277 
278 	rc = tpm_buf_append_name(chip, buf, options->keyhandle, NULL);
279 	if (rc)
280 		goto out;
281 
282 	tpm_buf_append_hmac_session(chip, buf, TPM2_SA_DECRYPT,
283 				    options->keyauth, TPM_DIGEST_SIZE);
284 
285 	/* sensitive */
286 	tpm_buf_append_u16(sized, options->blobauth_len);
287 
288 	if (options->blobauth_len)
289 		tpm_buf_append(sized, options->blobauth, options->blobauth_len);
290 
291 	tpm_buf_append_u16(sized, payload->key_len);
292 	tpm_buf_append(sized, payload->key, payload->key_len);
293 	tpm_buf_append(buf, sized->data, sized->length);
294 
295 	/* public */
296 	tpm_buf_reset_sized(sized);
297 	tpm_buf_append_u16(sized, TPM_ALG_KEYEDHASH);
298 	tpm_buf_append_u16(sized, hash);
299 
300 	/* key properties */
301 	flags = 0;
302 	flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;
303 	flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT);
304 	tpm_buf_append_u32(sized, flags);
305 
306 	/* policy */
307 	tpm_buf_append_u16(sized, options->policydigest_len);
308 	if (options->policydigest_len)
309 		tpm_buf_append(sized, options->policydigest, options->policydigest_len);
310 
311 	/* public parameters */
312 	tpm_buf_append_u16(sized, TPM_ALG_NULL);
313 	tpm_buf_append_u16(sized, 0);
314 
315 	tpm_buf_append(buf, sized->data, sized->length);
316 
317 	/* outside info */
318 	tpm_buf_append_u16(buf, 0);
319 
320 	/* creation PCR */
321 	tpm_buf_append_u32(buf, 0);
322 
323 	if (buf->flags & TPM_BUF_INVALID) {
324 		rc = -E2BIG;
325 		tpm2_end_auth_session(chip);
326 		goto out;
327 	}
328 
329 	rc = tpm_buf_fill_hmac_session(chip, buf);
330 	if (rc)
331 		goto out;
332 
333 	rc = tpm_transmit_cmd(chip, buf, 4, "sealing data");
334 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
335 	if (rc)
336 		goto out;
337 
338 	blob_len = tpm_buf_read_u32(buf, &offset);
339 	if (blob_len > MAX_BLOB_SIZE || buf->flags & TPM_BUF_INVALID) {
340 		rc = -E2BIG;
341 		goto out;
342 	}
343 	if (buf->length - offset < blob_len) {
344 		rc = -EFAULT;
345 		goto out;
346 	}
347 
348 	blob_len = tpm2_key_encode(payload, options, &buf->data[offset], blob_len);
349 	if (blob_len < 0)
350 		rc = blob_len;
351 
352 out:
353 	if (!rc)
354 		payload->blob_len = blob_len;
355 
356 out_put:
357 	tpm_put_ops(chip);
358 	return tpm_ret_to_err(rc);
359 }
360 
361 /**
362  * tpm2_load_cmd() - execute a TPM2_Load command
363  *
364  * @chip: TPM chip to use
365  * @payload: the key data in clear and encrypted form
366  * @options: authentication values and other options
367  * @blob_handle: returned blob handle
368  *
369  * Return: 0 on success.
370  *        -E2BIG on wrong payload size.
371  *        -EPERM on tpm error status.
372  *        < 0 error from tpm_send.
373  */
tpm2_load_cmd(struct tpm_chip * chip,struct trusted_key_payload * payload,struct trusted_key_options * options,u32 * blob_handle)374 static int tpm2_load_cmd(struct tpm_chip *chip,
375 			 struct trusted_key_payload *payload,
376 			 struct trusted_key_options *options,
377 			 u32 *blob_handle)
378 {
379 	u8 *blob_ref __free(kfree) = NULL;
380 	struct tpm_buf *buf __free(kfree) = NULL;
381 	unsigned int private_len;
382 	unsigned int public_len;
383 	unsigned int blob_len;
384 	u8 *blob, *pub;
385 	int rc;
386 	u32 attrs;
387 
388 	rc = tpm2_key_decode(payload, options, &blob, &blob_len);
389 	if (rc) {
390 		/* old form */
391 		blob = payload->blob;
392 		blob_len = payload->blob_len;
393 		payload->old_format = 1;
394 	} else {
395 		/* Bind for cleanup: */
396 		blob_ref = blob;
397 	}
398 
399 	/* new format carries keyhandle but old format doesn't */
400 	if (!options->keyhandle)
401 		return -EINVAL;
402 
403 	/* must be big enough for at least the two be16 size counts */
404 	if (blob_len < 4)
405 		return -EINVAL;
406 
407 	private_len = get_unaligned_be16(blob);
408 
409 	/* must be big enough for following public_len */
410 	if (private_len + 2 + 2 > blob_len)
411 		return -E2BIG;
412 
413 	public_len = get_unaligned_be16(blob + 2 + private_len);
414 	if (private_len + 2 + public_len + 2 > blob_len)
415 		return -E2BIG;
416 
417 	pub = blob + 2 + private_len + 2;
418 	/* key attributes are always at offset 4 */
419 	attrs = get_unaligned_be32(pub + 4);
420 
421 	if ((attrs & (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT)) ==
422 	    (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT))
423 		payload->migratable = 0;
424 	else
425 		payload->migratable = 1;
426 
427 	blob_len = private_len + public_len + 4;
428 	if (blob_len > payload->blob_len)
429 		return -E2BIG;
430 
431 	rc = tpm2_start_auth_session(chip);
432 	if (rc)
433 		return rc;
434 
435 	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
436 	if (!buf) {
437 		tpm2_end_auth_session(chip);
438 		return -ENOMEM;
439 	}
440 
441 	tpm_buf_init(buf, TPM_BUFSIZE);
442 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD);
443 
444 	rc = tpm_buf_append_name(chip, buf, options->keyhandle, NULL);
445 	if (rc)
446 		return rc;
447 
448 	tpm_buf_append_hmac_session(chip, buf, 0, options->keyauth,
449 				    TPM_DIGEST_SIZE);
450 
451 	tpm_buf_append(buf, blob, blob_len);
452 
453 	if (buf->flags & TPM_BUF_INVALID) {
454 		tpm2_end_auth_session(chip);
455 		return -E2BIG;
456 	}
457 
458 	rc = tpm_buf_fill_hmac_session(chip, buf);
459 	if (rc)
460 		return rc;
461 
462 	rc = tpm_transmit_cmd(chip, buf, 4, "loading blob");
463 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
464 	if (!rc)
465 		*blob_handle = be32_to_cpup(
466 			(__be32 *)&buf->data[TPM_HEADER_SIZE]);
467 
468 	return tpm_ret_to_err(rc);
469 }
470 
471 /**
472  * tpm2_unseal_cmd() - execute a TPM2_Unseal command
473  *
474  * @chip: TPM chip to use
475  * @payload: the key data in clear and encrypted form
476  * @options: authentication values and other options
477  * @blob_handle: blob handle
478  *
479  * Return: 0 on success
480  *         -EPERM on tpm error status
481  *         < 0 error from tpm_send
482  */
tpm2_unseal_cmd(struct tpm_chip * chip,struct trusted_key_payload * payload,struct trusted_key_options * options,u32 blob_handle)483 static int tpm2_unseal_cmd(struct tpm_chip *chip,
484 			   struct trusted_key_payload *payload,
485 			   struct trusted_key_options *options,
486 			   u32 blob_handle)
487 {
488 	struct tpm_header *head;
489 	struct tpm_buf *buf __free(kfree) = NULL;
490 	u16 data_len;
491 	int offset;
492 	u8 *data;
493 	int rc;
494 
495 	rc = tpm2_start_auth_session(chip);
496 	if (rc)
497 		return rc;
498 
499 	buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL);
500 	if (!buf) {
501 		tpm2_end_auth_session(chip);
502 		return -ENOMEM;
503 	}
504 
505 	tpm_buf_init(buf, TPM_BUFSIZE);
506 	tpm_buf_reset(buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL);
507 
508 	rc = tpm_buf_append_name(chip, buf, blob_handle, NULL);
509 	if (rc)
510 		return rc;
511 
512 	if (!options->policyhandle) {
513 		tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
514 					    options->blobauth,
515 					    options->blobauth_len);
516 	} else {
517 		/*
518 		 * FIXME: The policy session was generated outside the
519 		 * kernel so we don't known the nonce and thus can't
520 		 * calculate a HMAC on it.  Therefore, the user can
521 		 * only really use TPM2_PolicyPassword and we must
522 		 * send down the plain text password, which could be
523 		 * intercepted.  We can still encrypt the returned
524 		 * key, but that's small comfort since the interposer
525 		 * could repeat our actions with the exfiltrated
526 		 * password.
527 		 */
528 		tpm2_buf_append_auth(buf, options->policyhandle,
529 				     NULL /* nonce */, 0, 0,
530 				     options->blobauth, options->blobauth_len);
531 		if (tpm2_chip_auth(chip)) {
532 			tpm_buf_append_hmac_session(chip, buf, TPM2_SA_ENCRYPT,
533 						    NULL, 0);
534 		} else  {
535 			offset = buf->handles * 4 + TPM_HEADER_SIZE;
536 			head = (struct tpm_header *)buf->data;
537 			if (tpm_buf_length(buf) == offset)
538 				head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS);
539 		}
540 	}
541 
542 	rc = tpm_buf_fill_hmac_session(chip, buf);
543 	if (rc)
544 		return rc;
545 
546 	rc = tpm_transmit_cmd(chip, buf, 6, "unsealing");
547 	rc = tpm_buf_check_hmac_response(chip, buf, rc);
548 
549 	if (!rc) {
550 		data_len = be16_to_cpup(
551 			(__be16 *)&buf->data[TPM_HEADER_SIZE + 4]);
552 		if (data_len < MIN_KEY_SIZE ||  data_len > MAX_KEY_SIZE)
553 			return -EFAULT;
554 
555 		if (tpm_buf_length(buf) < TPM_HEADER_SIZE + 6 + data_len)
556 			return -EFAULT;
557 		data = &buf->data[TPM_HEADER_SIZE + 6];
558 
559 		if (payload->old_format) {
560 			/* migratable flag is at the end of the key */
561 			memcpy(payload->key, data, data_len - 1);
562 			payload->key_len = data_len - 1;
563 			payload->migratable = data[data_len - 1];
564 		} else {
565 			/*
566 			 * migratable flag already collected from key
567 			 * attributes
568 			 */
569 			memcpy(payload->key, data, data_len);
570 			payload->key_len = data_len;
571 		}
572 	}
573 
574 	return tpm_ret_to_err(rc);
575 }
576 
577 /**
578  * tpm2_unseal_trusted() - unseal the payload of a trusted key
579  *
580  * @chip: TPM chip to use
581  * @payload: the key data in clear and encrypted form
582  * @options: authentication values and other options
583  *
584  * Return: Same as with tpm_send.
585  */
tpm2_unseal_trusted(struct tpm_chip * chip,struct trusted_key_payload * payload,struct trusted_key_options * options)586 int tpm2_unseal_trusted(struct tpm_chip *chip,
587 			struct trusted_key_payload *payload,
588 			struct trusted_key_options *options)
589 {
590 	u32 blob_handle;
591 	int rc;
592 
593 	rc = tpm_try_get_ops(chip);
594 	if (rc)
595 		return rc;
596 
597 	rc = tpm2_load_cmd(chip, payload, options, &blob_handle);
598 	if (rc)
599 		goto out;
600 
601 	rc = tpm2_unseal_cmd(chip, payload, options, blob_handle);
602 	tpm2_flush_context(chip, blob_handle);
603 
604 out:
605 	tpm_put_ops(chip);
606 	return tpm_ret_to_err(rc);
607 }
608