summaryrefslogtreecommitdiff
path: root/firmware/lib/tpm2_lite/tlcl.c
blob: 02d1a91b737a3dfd0f489cdb9964d5e4df8ec906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/* Copyright 2016 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Some TPM constants and type definitions for standalone compilation for use
 * in the firmware
 */

#include "2common.h"
#include "rollback_index.h"
#include "2sysincludes.h"
#include "tlcl.h"
#include "tpm2_marshaling.h"
#include "utility.h"

/* Global buffer for deserialized responses. */
struct tpm2_response tpm2_resp;

/*
 * Serializes and sends the command, gets back the response and
 * parses it into the provided buffer.
 *
 * @command: command code.
 * @command_body: command-specific payload.
 * @response: pointer to the buffer to place the parsed response to.
 *
 * Returns the result of processing the command:
 *   - if an error happened at marshaling, sending, receiving or unmarshaling
 *     stages, returns the error code;
 *   - if the received response was successfully unmarshaled, returns success
 *     regardless of the received response code.
 */
static uint32_t tpm_get_response(TPM_CC command,
				 void *command_body,
				 struct tpm2_response *response)
{
	/* Command/response buffer. */
	static uint8_t cr_buffer[TPM_BUFFER_SIZE];
	int out_size;
	uint32_t res;
	uint32_t in_size;

	out_size = tpm_marshal_command(command, command_body,
				       cr_buffer, sizeof(cr_buffer));
	if (out_size < 0) {
		VB2_DEBUG("command %#x, failed to serialize\n", command);
		return TPM_E_WRITE_FAILURE;
	}

	in_size = sizeof(cr_buffer);
	res = VbExTpmSendReceive(cr_buffer, out_size, cr_buffer, &in_size);
	if (res != TPM_SUCCESS) {
		VB2_DEBUG("tpm transaction failed for %#x with error %#x\n",
			  command, res);
		return res;
	}

	if (tpm_unmarshal_response(command, cr_buffer, in_size, response) < 0) {
		VB2_DEBUG("command %#x, failed to parse response\n", command);
		return TPM_E_READ_FAILURE;
	}

	VB2_DEBUG("command %#x, return code %#x\n", command,
		  response->hdr.tpm_code);

	return TPM_SUCCESS;
}

/*
 * Same as tpm_get_response() but, if the response was successfully received,
 * returns the received response code. The set of errors returned by the
 * communication stack doesn't overlap with the set of errors returned by the
 * TPM, so it's always possible to distinguish the two. In case of communication
 * errors, the caller should not check other fields of response, as the response
 * is likely not filled. In any case, it is recommended that callers, who need
 * to work with response fields even if a non-zero response code was received
 * from the TPM, use tpm_get_response() and explicitly check the response code
 * themselves.
 */
static uint32_t tpm_send_receive(TPM_CC command,
				 void *command_body,
				 struct tpm2_response *response)
{
	uint32_t rv = tpm_get_response(command, command_body, response);

	return rv ? rv : response->hdr.tpm_code;
}

/*
 * Same as tpm_send_receive() for callers that care only about the return code.
 */
static uint32_t tpm_get_response_code(TPM_CC command, void *command_body)
{
	return tpm_send_receive(command, command_body, &tpm2_resp);
}

static uint32_t tlcl_read_ph_disabled(void)
{
	uint32_t rv;
	TPM_STCLEAR_FLAGS flags;

	rv = TlclGetSTClearFlags(&flags);
	if (rv == TPM_SUCCESS)
		tpm_set_ph_disabled(!flags.phEnable);

	return rv;
}

uint32_t TlclLibInit(void)
{
	uint32_t rv;

	rv = VbExTpmInit();
	if (rv != TPM_SUCCESS)
		return rv;

	rv = tlcl_read_ph_disabled();
	if (rv != TPM_SUCCESS)
		TlclLibClose();

	return rv;
}

uint32_t TlclLibClose(void)
{
	return VbExTpmClose();
}

uint32_t TlclSendReceive(const uint8_t *request, uint8_t *response,
			 int max_length)
{
	uint32_t rv, resp_size;

	resp_size = max_length;
	rv = VbExTpmSendReceive(request, tpm_get_packet_size(request),
				response, &resp_size);

	return rv ? rv : tpm_get_packet_response_code(response);
}

int TlclPacketSize(const uint8_t *packet)
{
	return tpm_get_packet_size(packet);
}

uint32_t TlclStartup(void)
{
	struct tpm2_startup_cmd startup;

	startup.startup_type = TPM_SU_CLEAR;

	return tpm_get_response_code(TPM2_Startup, &startup);
}

uint32_t TlclSaveState(void)
{
	struct tpm2_shutdown_cmd shutdown;

	shutdown.shutdown_type = TPM_SU_STATE;

	return tpm_get_response_code(TPM2_Shutdown, &shutdown);
}

uint32_t TlclResume(void)
{
	struct tpm2_startup_cmd startup;

	startup.startup_type = TPM_SU_STATE;

	return tpm_get_response_code(TPM2_Startup, &startup);
}

uint32_t TlclSelfTestFull(void)
{
	struct tpm2_self_test_cmd self_test;

	self_test.full_test = 1;

	return tpm_get_response_code(TPM2_SelfTest, &self_test);
}

uint32_t TlclContinueSelfTest(void)
{
	struct tpm2_self_test_cmd self_test;

	self_test.full_test = 0;

	return tpm_get_response_code(TPM2_SelfTest, &self_test);
}

uint32_t TlclDefineSpace(uint32_t index, uint32_t perm, uint32_t size)
{
	return TlclDefineSpaceEx(NULL, 0, index, perm, size, NULL, 0);
}

#ifdef CHROMEOS_ENVIRONMENT

uint32_t TlclUndefineSpace(uint32_t index)
{
	return TlclUndefineSpaceEx(NULL, 0, index);
}

uint32_t TlclUndefineSpaceEx(const uint8_t* owner_auth,
			     uint32_t owner_auth_size,
			     uint32_t index)
{
	struct tpm2_nv_undefine_space_cmd undefine_space;
	uint32_t permissions;
	uint32_t rv;

	/* Authentication support is not implemented. */
	VbAssert(owner_auth == NULL && owner_auth_size == 0);

	/* get the publicInfo of index */
	rv = TlclGetPermissions(index, &permissions);
	if (rv != TPM_SUCCESS) {
		return rv;
	}
	undefine_space.nvIndex = HR_NV_INDEX + index;
	undefine_space.use_platform_auth =
		(permissions & TPMA_NV_PLATFORMCREATE) > 0;
	return tpm_get_response_code(TPM2_NV_UndefineSpace, &undefine_space);
}

#endif  /* CHROMEOS_ENVIRONMENT */

uint32_t TlclDefineSpaceEx(const uint8_t* owner_auth, uint32_t owner_auth_size,
			   uint32_t index, uint32_t perm, uint32_t size,
			   const void* auth_policy, uint32_t auth_policy_size)
{
	struct tpm2_nv_define_space_cmd define_space;

	/* Authentication support is not implemented. */
	VbAssert(owner_auth == NULL && owner_auth_size == 0);

	/* For backwards-compatibility, if no READ or WRITE permissions are set,
	 * assume readable/writeable with empty auth value.
	 */
	if (!(perm & TPMA_NV_MASK_WRITE))
		perm |= TPMA_NV_AUTHWRITE;
	if (!(perm & TPMA_NV_MASK_READ))
		perm |= TPMA_NV_AUTHREAD;

	memset(&define_space, 0, sizeof(define_space));
	define_space.publicInfo.nvIndex = HR_NV_INDEX + index;
	define_space.publicInfo.dataSize = size;
	define_space.publicInfo.attributes = perm;
	define_space.publicInfo.nameAlg = TPM_ALG_SHA256;
	if (auth_policy && auth_policy_size > 0) {
		define_space.publicInfo.authPolicy.size = auth_policy_size;
		define_space.publicInfo.authPolicy.buffer =
				(uint8_t*) auth_policy;
	}

	return tpm_get_response_code(TPM2_NV_DefineSpace, &define_space);
}

uint32_t TlclInitNvAuthPolicy(uint32_t pcr_selection_bitmap,
			      const uint8_t pcr_values[][TPM_PCR_DIGEST],
			      void* auth_policy, uint32_t* auth_policy_size)
{
	/* Actual PCR selection isn't implemented. */
	VbAssert(pcr_selection_bitmap == 0);
	*auth_policy_size = 0;
	return TPM_SUCCESS;
}

/**
 * Issue a ForceClear.  The TPM error code is returned.
 */
uint32_t TlclForceClear(void)
{
	return tpm_get_response_code(TPM2_Clear, NULL);
}

uint32_t TlclSetDeactivated(uint8_t flag)
{
	VB2_DEBUG("NOT YET IMPLEMENTED\n");
	return TPM_SUCCESS;
}

uint32_t TlclSetEnable(void)
{
	VB2_DEBUG("NOT YET IMPLEMENTED\n");
	return TPM_SUCCESS;
}

uint32_t TlclGetFlags(uint8_t* disable,
		      uint8_t* deactivated,
		      uint8_t *nvlocked)
{
	/* For TPM2 the flags are always the same */
	if (disable)
		*disable = 0;
	if (deactivated)
		*deactivated = 0;
	if (nvlocked)
		*nvlocked = 1;
	return TPM_SUCCESS;
}

int TlclIsOwned(void)
{
	VB2_DEBUG("NOT YET IMPLEMENTED\n");
	return 0;
}

uint32_t TlclExtend(int pcr_num, const uint8_t *in_digest, uint8_t *out_digest)
{
	VB2_DEBUG("NOT YET IMPLEMENTED\n");
	return TPM_SUCCESS;
}


static uint32_t tlcl_nv_read_public(uint32_t index,
				    struct nv_read_public_response **presp)
{
	struct tpm2_response *response = &tpm2_resp;
	struct tpm2_nv_read_public_cmd read_pub;
	uint32_t rv;

	memset(&read_pub, 0, sizeof(read_pub));
	read_pub.nvIndex = HR_NV_INDEX + index;

	rv = tpm_send_receive(TPM2_NV_ReadPublic, &read_pub, response);
	if (rv == TPM_SUCCESS)
		*presp = &response->nv_read_public;

	return rv;
}

/**
 * Get the permission bits for the NVRAM space with |index|.
 */
uint32_t TlclGetPermissions(uint32_t index, uint32_t *permissions)
{
	uint32_t rv;
	struct nv_read_public_response *resp;

	rv = tlcl_nv_read_public(index, &resp);
	if (rv == TPM_SUCCESS)
		*permissions = resp->nvPublic.attributes;

	return rv;
}

uint32_t TlclGetSpaceInfo(uint32_t index, uint32_t *attributes, uint32_t *size,
			  void* auth_policy, uint32_t* auth_policy_size)
{
	uint32_t rv;
	struct nv_read_public_response *resp;

	rv = tlcl_nv_read_public(index, &resp);
	if (rv != TPM_SUCCESS)
		return rv;

	*attributes = resp->nvPublic.attributes;
	*size = resp->nvPublic.dataSize;
	if (resp->nvPublic.authPolicy.size > *auth_policy_size) {
		return TPM_E_BUFFER_SIZE;
	}

	*auth_policy_size = resp->nvPublic.authPolicy.size;
	memcpy(auth_policy, resp->nvPublic.authPolicy.buffer,
	       *auth_policy_size);

	return TPM_SUCCESS;
}

static uint32_t tlcl_get_capability(TPM_CAP cap, TPM_PT property,
				    struct get_capability_response **presp)
{
	struct tpm2_response *response = &tpm2_resp;
	struct tpm2_get_capability_cmd getcap;
	uint32_t rv;

	getcap.capability = cap;
	getcap.property = property;
	getcap.property_count = 1;

	rv = tpm_send_receive(TPM2_GetCapability, &getcap, response);
	if (rv == TPM_SUCCESS)
		*presp = &response->cap;

	return rv;
}

static uint32_t tlcl_get_tpm_property(TPM_PT property, uint32_t *pvalue)
{
	uint32_t rv;
	struct get_capability_response *resp;
	TPML_TAGGED_TPM_PROPERTY *tpm_prop;

	rv = tlcl_get_capability(TPM_CAP_TPM_PROPERTIES, property, &resp);
	if (rv != TPM_SUCCESS)
		return rv;

	if (resp->capability_data.capability != TPM_CAP_TPM_PROPERTIES)
		return TPM_E_IOERROR;

	tpm_prop = &resp->capability_data.data.tpm_properties;

	if ((tpm_prop->count != 1) ||
	    (tpm_prop->tpm_property[0].property != property))
		return TPM_E_IOERROR;

	*pvalue = tpm_prop->tpm_property[0].value;
	return TPM_SUCCESS;
}

uint32_t TlclGetPermanentFlags(TPM_PERMANENT_FLAGS *pflags)
{
	return tlcl_get_tpm_property(TPM_PT_PERMANENT,
				     (uint32_t *)pflags);
}

uint32_t TlclGetSTClearFlags(TPM_STCLEAR_FLAGS *pflags)
{
	return tlcl_get_tpm_property(TPM_PT_STARTUP_CLEAR,
				     (uint32_t *)pflags);
}

uint32_t TlclGetOwnership(uint8_t *owned)
{
	uint32_t rv;
	TPM_PERMANENT_FLAGS flags;
	*owned = 0;

	rv = TlclGetPermanentFlags(&flags);
	if (rv == TPM_SUCCESS)
		*owned = flags.ownerAuthSet;

	return rv;
}

static uint32_t tlcl_lock_nv_write(uint32_t index)
{
	struct tpm2_nv_write_lock_cmd nv_wl;

	nv_wl.nvIndex = HR_NV_INDEX + index;
	return tpm_get_response_code(TPM2_NV_WriteLock, &nv_wl);
}

static uint32_t tlcl_disable_platform_hierarchy(void)
{
	struct tpm2_hierarchy_control_cmd hc;
	uint32_t rv;

	hc.enable = TPM_RH_PLATFORM;
	hc.state = 0;

	rv = tpm_get_response_code(TPM2_Hierarchy_Control, &hc);
	if (rv == TPM_SUCCESS)
		tpm_set_ph_disabled(1);

	return rv;
}

/**
 * The name of the function was kept to maintain the existing TPM API, but
 * TPM2.0 does not use the global lock to protect the FW rollback counter.
 * Instead it calls WriteLock for the FW NVRAM index to prevent future
 * writes to it.
 *
 * It first checks if the platform hierarchy is already disabled, and does
 * nothing, if so. Otherwise, WriteLock for the index obviously fails.
 */
uint32_t TlclSetGlobalLock(void)
{
	if (tpm_is_ph_disabled())
		return TPM_SUCCESS;
	else
		return tlcl_lock_nv_write(FIRMWARE_NV_INDEX);
}

/**
 * Turn off physical presence and locks it off until next reboot.  The TPM
 * error code is returned.
 *
 * The name of the function was kept to maintain the existing TPM API, but
 * TPM2.0 does not have to use the Physical Presence concept. Instead it just
 * removes platform authorization - this makes sure that firmware and kernel
 * rollback counter spaces can not be modified.
 *
 * It also explicitly locks the kernel rollback counter space (the FW rollback
 * counter space was locked before RW firmware started.)
 */
uint32_t TlclLockPhysicalPresence(void)
{
	if (tpm_is_ph_disabled())
		return TPM_SUCCESS;

	return tlcl_disable_platform_hierarchy();
}

uint32_t TlclRead(uint32_t index, void* data, uint32_t length)
{
	struct tpm2_nv_read_cmd nv_readc;
	struct tpm2_response *response = &tpm2_resp;
	uint32_t rv;

	memset(&nv_readc, 0, sizeof(nv_readc));

	nv_readc.nvIndex = HR_NV_INDEX + index;
	nv_readc.size = length;

	rv = tpm_send_receive(TPM2_NV_Read, &nv_readc, response);

	/* Need to map tpm error codes into internal values. */
	switch (rv) {
	case TPM_SUCCESS:
		break;

	case 0x28b:
		return TPM_E_BADINDEX;

	default:
		return rv;
	}

	if (length > response->nvr.buffer.t.size)
		return TPM_E_RESPONSE_TOO_LARGE;

	if (length < response->nvr.buffer.t.size)
		return TPM_E_READ_EMPTY;

	memcpy(data, response->nvr.buffer.t.buffer, length);

	return TPM_SUCCESS;
}

uint32_t TlclWrite(uint32_t index, const void *data, uint32_t length)
{
	struct tpm2_nv_write_cmd nv_writec;

	memset(&nv_writec, 0, sizeof(nv_writec));

	nv_writec.nvIndex = HR_NV_INDEX + index;
	nv_writec.data.t.size = length;
	nv_writec.data.t.buffer = data;

	return tpm_get_response_code(TPM2_NV_Write, &nv_writec);
}

uint32_t TlclPCRRead(uint32_t index, void *data, uint32_t length)
{
	VB2_DEBUG("NOT YET IMPLEMENTED\n");
	return TPM_SUCCESS;
}

uint32_t TlclWriteLock(uint32_t index)
{
	struct tpm2_nv_write_lock_cmd nv_writelockc;

	memset(&nv_writelockc, 0, sizeof(nv_writelockc));

	nv_writelockc.nvIndex = HR_NV_INDEX | index;

	return tpm_get_response_code(TPM2_NV_WriteLock, &nv_writelockc);
}

uint32_t TlclReadLock(uint32_t index)
{
	struct tpm2_nv_read_lock_cmd nv_readlockc;

	memset(&nv_readlockc, 0, sizeof(nv_readlockc));

	nv_readlockc.nvIndex = HR_NV_INDEX | index;

	return tpm_get_response_code(TPM2_NV_ReadLock, &nv_readlockc);
}

uint32_t TlclGetRandom(uint8_t *data, uint32_t length, uint32_t *size)
{
	uint32_t rv;
	struct tpm2_get_random_cmd random;
	struct get_random_response *response = &tpm2_resp.random;
	size_t max_len, offset;

	offset = offsetof(struct tpm2_response, random.random_bytes.buffer);
	max_len = TPM_BUFFER_SIZE - offset;

	if (length > max_len)
		return TPM_E_BUFFER_SIZE;

	random.bytes_requested = length;

	rv = tpm_send_receive(TPM2_GetRandom, &random, &tpm2_resp);
	if (rv != TPM_SUCCESS)
		return rv;

	*size = response->random_bytes.size;
	if (*size > length)
		return TPM_E_RESPONSE_TOO_LARGE;

	memcpy(data, response->random_bytes.buffer, *size);

	return rv;
}

// Converts TPM_PT_VENDOR_STRING_x |value| to an array of bytes in |buf|.
// Returns the number of bytes in the array.
// |buf| should be at least 4 bytes long.
static size_t tlcl_vendor_string_parse(uint32_t value, uint8_t* buf)
{
	size_t len = 0;
	int shift = 24;
	for (; len < 4; shift -= 8) {
		uint8_t byte = (value >> shift) & 0xffu;
		if (!byte)
			break;
		buf[len++] = byte;
	}
	return len;
}

uint32_t TlclGetVersion(uint32_t* vendor, uint64_t* firmware_version,
			uint8_t* vendor_specific_buf,
			size_t* vendor_specific_buf_size)
{
	uint32_t result =  tlcl_get_tpm_property(TPM_PT_MANUFACTURER, vendor);
	if (result != TPM_SUCCESS)
		return result;

	uint32_t version_1;
	uint32_t version_2;
	result = tlcl_get_tpm_property(TPM_PT_FIRMWARE_VERSION_1, &version_1);
	if (result != TPM_SUCCESS)
		return result;
	result = tlcl_get_tpm_property(TPM_PT_FIRMWARE_VERSION_2, &version_2);
	if (result != TPM_SUCCESS)
		return result;

	*firmware_version = ((uint64_t) version_1 << 32) | version_2;

	if (!vendor_specific_buf_size)
		return TPM_SUCCESS;

	size_t total_size = 0;
	uint32_t prop_id;
	uint8_t prop_string[16];
	for (prop_id = TPM_PT_VENDOR_STRING_1;
	     prop_id <= TPM_PT_VENDOR_STRING_4;
	     ++prop_id) {
		uint32_t prop_value;
		result = tlcl_get_tpm_property(prop_id, &prop_value);
		if (result != TPM_SUCCESS)
			break;

		size_t prop_len = tlcl_vendor_string_parse(
				prop_value, prop_string + total_size);
		VbAssert(prop_len <= 4 &&
			 total_size + prop_len <= sizeof(prop_string));
		total_size += prop_len;
		if (prop_len < 4)
			break;
	}
	if (vendor_specific_buf) {
		if (total_size > *vendor_specific_buf_size)
			total_size = *vendor_specific_buf_size;
		memcpy(vendor_specific_buf, prop_string, total_size);
	}
	*vendor_specific_buf_size = total_size;
	return TPM_SUCCESS;
}

uint32_t TlclIFXFieldUpgradeInfo(TPM_IFX_FIELDUPGRADEINFO* info)
{
	VB2_DEBUG("NOT YET IMPLEMENTED\n");
	return TPM_E_IOERROR;
}