Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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

/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2007
 *
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "trousers/tss.h"
#include "trousers_types.h"
#include "tcs_tsp.h"
#include "tcs_utils.h"
#include "tcs_int_literals.h"
#include "capabilities.h"
#include "tcslog.h"
#include "tcsps.h"
#include "req_mgr.h"


TSS_RESULT
TCSP_EstablishTransport_Internal(TCS_CONTEXT_HANDLE      hContext,
				 UINT32                  ulTransControlFlags,
				 TCS_KEY_HANDLE          hEncKey,
				 UINT32                  ulTransSessionInfoSize,
				 BYTE*                   rgbTransSessionInfo,
				 UINT32                  ulSecretSize,
				 BYTE*                   rgbSecret,
				 TPM_AUTH*               pEncKeyAuth,
				 TPM_MODIFIER_INDICATOR* pbLocality,
				 TCS_HANDLE*             hTransSession,
				 UINT32*                 ulCurrentTicks,
				 BYTE**                  prgbCurrentTicks,
				 TPM_NONCE*              pTransNonce)
{
	TSS_RESULT result;
	UINT32 paramSize;
	UINT64 offset;
	TPM_KEY_HANDLE keySlot = TPM_KH_TRANSPORT;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (ulTransControlFlags == TSS_TCSATTRIB_TRANSPORT_EXCLUSIVE) {
		if ((result = ctx_req_exclusive_transport(hContext)))
			return result;
	}

	if (pEncKeyAuth) {
		if ((result = auth_mgr_check(hContext, &pEncKeyAuth->AuthHandle)))
			return result;
	}

	/* if hEncKey is set to TPM_KH_TRANSPORT, that's the signal to the TPM that this will be
	 * an unencrypted transport session, so we don't need to check that its loaded */
	if (hEncKey != TPM_KH_TRANSPORT) {
		if ((result = ensureKeyIsLoaded(hContext, hEncKey, &keySlot)))
			return result;
	}

	offset = TSS_TPM_TXBLOB_HDR_LEN;
	LoadBlob_UINT32(&offset, keySlot, txBlob);
	LoadBlob(&offset, ulTransSessionInfoSize, txBlob, rgbTransSessionInfo);
	LoadBlob_UINT32(&offset, ulSecretSize, txBlob);
	LoadBlob(&offset, ulSecretSize, txBlob, rgbSecret);
	if (pEncKeyAuth) {
		LoadBlob_Auth(&offset, txBlob, pEncKeyAuth);
		LoadBlob_Header(TPM_TAG_RQU_AUTH1_COMMAND, offset, TPM_ORD_EstablishTransport,
				txBlob);
	} else
		LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_EstablishTransport, txBlob);

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
		goto done;
	}

	offset = TSS_TPM_TXBLOB_HDR_LEN;
	UnloadBlob_UINT32(&offset, hTransSession, txBlob);
	UnloadBlob_UINT32(&offset, pbLocality, txBlob);

	*ulCurrentTicks = sizeof(TPM_STRUCTURE_TAG)
			  + sizeof(UINT64)
			  + sizeof(UINT16)
			  + sizeof(TPM_NONCE);

	*prgbCurrentTicks = malloc(*ulCurrentTicks);
	if (*prgbCurrentTicks == NULL) {
		result = TCSERR(TSS_E_OUTOFMEMORY);
		goto done;
	}

	UnloadBlob(&offset, *ulCurrentTicks, txBlob, *prgbCurrentTicks);
	UnloadBlob(&offset, sizeof(TPM_NONCE), txBlob, (BYTE *)pTransNonce);
	if (pEncKeyAuth)
		UnloadBlob_Auth(&offset, txBlob, pEncKeyAuth);

	ctx_set_transport_enabled(hContext, *hTransSession);
done:
	auth_mgr_release_auth(pEncKeyAuth, NULL, hContext);
	return result;
}

TSS_RESULT
TCSP_ExecuteTransport_Internal(TCS_CONTEXT_HANDLE      hContext,
			       TPM_COMMAND_CODE        unWrappedCommandOrdinal,
			       UINT32                  ulWrappedCmdParamInSize,
			       BYTE*                   rgbWrappedCmdParamIn,
			       UINT32*                 pulHandleListSize,	/* in, out */
			       TCS_HANDLE**            rghHandles,		/* in, out */
			       TPM_AUTH*               pWrappedCmdAuth1,	/* in, out */
			       TPM_AUTH*               pWrappedCmdAuth2,	/* in, out */
			       TPM_AUTH*               pTransAuth,		/* in, out */
			       UINT64*                 punCurrentTicks,
			       TPM_MODIFIER_INDICATOR* pbLocality,
			       TPM_RESULT*             pulWrappedCmdReturnCode,
			       UINT32*                 ulWrappedCmdParamOutSize,
			       BYTE**                  rgbWrappedCmdParamOut)
{
	TSS_RESULT result;
	UINT32 paramSize, wrappedSize, val1 = 0, val2 = 0, *pVal1 = NULL, *pVal2 = NULL;
	TCS_HANDLE handle1 = 0, handle2 = 0;
	UINT64 offset, wrappedOffset = 0;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];


	if (*pulHandleListSize > 2) {
		LogDebugFn("************ EXPAND KEYSLOT SIZE *********");
		return TCSERR(TSS_E_INTERNAL_ERROR);
	}

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (pWrappedCmdAuth1)
		if ((result = auth_mgr_check(hContext, &pWrappedCmdAuth1->AuthHandle)))
			goto done;

	if (pWrappedCmdAuth2)
		if ((result = auth_mgr_check(hContext, &pWrappedCmdAuth2->AuthHandle)))
			goto done;

	switch (unWrappedCommandOrdinal) {
	/* If the command is FlushSpecific, we get handle that needs to be freed, but we don't know
	 * what type it is. */
	case TPM_ORD_FlushSpecific:
		if (*pulHandleListSize == 0) { /* invalid */
			result = TCSERR(TSS_E_BAD_PARAMETER);
			goto done;
		}

		/* If this is a transport handle, remove the context's reference to the session */
		ctx_set_transport_disabled(hContext, rghHandles[0]);

		/* Is it a key? If so, jump to the get_slot_lite and key management calls below */
		if (ctx_has_key_loaded(hContext, *rghHandles[0]))
			goto map_key_handles;

		/* fall through */
	case TPM_ORD_Terminate_Handle:
		if (!auth_mgr_check(hContext, rghHandles[0]))
			auth_mgr_release_auth_handle(*rghHandles[0], hContext, FALSE);

		/* If the handle is an auth handle or any other kind of handle, there's no
		 * mapping done in the TCS, so pass its value straight to the TPM and jump over
		 * the switch statement below where we assume FLushSpecific is being done on a
		 * key */
		handle1 = val1 = *rghHandles[0];
		pVal1 = &val1;

		goto build_command;
	default:
		break;
	}

map_key_handles:
	if (*pulHandleListSize == 2) {
		handle2 = (*rghHandles)[1];

		if ((result = get_slot_lite(hContext, handle2, &val2))) {
			*pulHandleListSize = 0;
			goto done;
		}

		pVal2 = &val2;
	}

	if (*pulHandleListSize >= 1) {
		handle1 = *rghHandles[0];

		*pulHandleListSize = 0;

		if ((result = get_slot_lite(hContext, handle1, &val1)))
			goto done;

		pVal1 = &val1;
	}

	switch (unWrappedCommandOrdinal) {
	case TPM_ORD_EvictKey:
	case TPM_ORD_FlushSpecific:
	{
		if ((result = ctx_remove_key_loaded(hContext, handle1)))
			goto done;

		if ((result = key_mgr_dec_ref_count(handle1)))
			goto done;

		/* we can't call key_mgr_ref_cnt() here since it calls TPM_EvictKey directly */
		mc_set_slot_by_handle(handle1, NULL_TPM_HANDLE);
		break;
	}
	case TPM_ORD_OIAP:
	{
		/* are the maximum number of auth sessions open? */
		if (auth_mgr_req_new(hContext) == FALSE) {
			if ((result = auth_mgr_swap_out(hContext)))
				goto done;
		}

		break;
	}
	case TPM_ORD_OSAP:
	{
		UINT16 entityType;
		UINT32 entityValue, newEntValue;

		/* are the maximum number of auth sessions open? */
		if (auth_mgr_req_new(hContext) == FALSE) {
			if ((result = auth_mgr_swap_out(hContext)))
				goto done;
		}

		offset = 0;
		UnloadBlob_UINT16(&offset, &entityType, rgbWrappedCmdParamIn);
		UnloadBlob_UINT32(&offset, &entityValue, rgbWrappedCmdParamIn);

		if (entityType == TCPA_ET_KEYHANDLE || entityType == TCPA_ET_KEY) {
			if (ensureKeyIsLoaded(hContext, entityValue, &newEntValue))
				return TCSERR(TSS_E_KEY_NOT_LOADED);

			/* OSAP is never encrypted in a transport session, so changing
			 * rgbWrappedCmdParamIn is ok here */
			offset = sizeof(UINT16);
			LoadBlob_UINT32(&offset, newEntValue, rgbWrappedCmdParamIn);
		}

		break;
	}
	case TPM_ORD_DSAP:
	{
		UINT16 entityType;
		UINT32 keyHandle, tpmKeyHandle;

		/* are the maximum number of auth sessions open? */
		if (auth_mgr_req_new(hContext) == FALSE) {
			if ((result = auth_mgr_swap_out(hContext)))
				goto done;
		}

		offset = 0;
		UnloadBlob_UINT16(&offset, &entityType, rgbWrappedCmdParamIn);
		UnloadBlob_UINT32(&offset, &keyHandle, rgbWrappedCmdParamIn);

		if (ensureKeyIsLoaded(hContext, keyHandle, &tpmKeyHandle)) {
			result = TCSERR(TSS_E_KEY_NOT_LOADED);
			goto done;
		}

		/* DSAP's only encrypted paramter is entityValue, so replacing keyHandle inside
		 * rgbWrappedCmdParamIn is ok */
		offset = sizeof(UINT16);
		LoadBlob_UINT32(&offset, tpmKeyHandle, rgbWrappedCmdParamIn);
	}
	default:
		break;
	}

build_command:
	if ((result = tpm_rqu_build(TPM_ORD_ExecuteTransport, &wrappedOffset,
				    &txBlob[TSS_TXBLOB_WRAPPEDCMD_OFFSET], unWrappedCommandOrdinal,
				    pVal1, pVal2, ulWrappedCmdParamInSize, rgbWrappedCmdParamIn,
				    pWrappedCmdAuth1, pWrappedCmdAuth2)))
		goto done;

	/* The blob we'll load here looks like this:
	 *
	 * |TAGet|LENet|ORDet|wrappedCmdSize|wrappedCmd|AUTHet|
	 *
	 * wrappedCmd looks like this:
	 *
	 * |TAGw|LENw|ORDw|HANDLESw|DATAw|AUTH1w|AUTH2w|
	 *
	 * w = wrapped command info
	 * et = execute transport command info
	 *
	 * Note that the wrapped command was loaded into the blob by the tpm_rqu_build call
	 * above.
	 *
	 */
	offset = TSS_TPM_TXBLOB_HDR_LEN;
	/* Load wrapped command size: |wrappedCmdSize| */
	LoadBlob_UINT32(&offset, wrappedOffset, txBlob);

	/* offset + wrappedOffset is the position of the execute transport auth struct */
	offset += wrappedOffset;

	if (pTransAuth) {
		/* Load the auth for the execute transport command: |AUTHet| */
		LoadBlob_Auth(&offset, txBlob, pTransAuth);
		/* Load the outer header: |TAGet|LENet|ORDet| */
		LoadBlob_Header(TPM_TAG_RQU_AUTH1_COMMAND, offset, TPM_ORD_ExecuteTransport,
				txBlob);
	} else {
		/* Load the outer header: |TAGet|LENet|ORDet| */
		LoadBlob_Header(TPM_TAG_RQU_COMMAND, offset, TPM_ORD_ExecuteTransport, txBlob);
	}

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	/* Unload the Execute Transport (outer) header */
	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
		goto done;
	}

	/* The response from the TPM looks like this:
	 *
	 * |TAGet|LENet|RCet|currentTicks|locality|wrappedRspSize|wrappedRsp|AUTHet|
	 *
	 * and wrappedRsp looks like:
	 *
	 * |TAGw|LENw|RCw|HANDLESw|DATAw|AUTH1w|AUTH2w|
	 */

	offset = TSS_TPM_TXBLOB_HDR_LEN;
	UnloadBlob_UINT64(&offset, punCurrentTicks, txBlob);
	UnloadBlob_UINT32(&offset, pbLocality, txBlob);

	/* Unload the wrapped response size: |wrappedRspSize| */
	UnloadBlob_UINT32(&offset, &wrappedSize, txBlob);

	/* We've parsed right up to wrappedRsp, so save off this offset for later */
	wrappedOffset = offset;

	/* The current offset + the response size will be the offset of |AUTHet| */
	offset += wrappedSize;
	if (pTransAuth)
		UnloadBlob_Auth(&offset, txBlob, pTransAuth);

	/* Now parse through the returned response @ wrappedOffset */
	if ((result = UnloadBlob_Header(&txBlob[wrappedOffset], &paramSize))) {
		LogDebugFn("Wrapped command (Ordinal 0x%x) failed: rc=0x%x",
			   unWrappedCommandOrdinal, result);

		/* This is the result of the wrapped command. If its not success, return its value
		 * in the pulWrappedCmdReturnCode variable and return indicating that the execute
		 * transport command was successful */
		*pulWrappedCmdReturnCode = result;
		*ulWrappedCmdParamOutSize = 0;
		*rgbWrappedCmdParamOut = NULL;
		auth_mgr_release_auth(pWrappedCmdAuth1, pWrappedCmdAuth2, hContext);

		return TSS_SUCCESS;
	}

	*pulWrappedCmdReturnCode = TSS_SUCCESS;

	pVal1 = pVal2 = NULL;
	switch (unWrappedCommandOrdinal) {
		/* The commands below have 1 outgoing handle */
		case TPM_ORD_LoadKey2:
			pVal1 = &val1;
			break;
		default:
			break;
	}

	result = tpm_rsp_parse(TPM_ORD_ExecuteTransport, &txBlob[wrappedOffset], paramSize,
			       pVal1, pVal2, ulWrappedCmdParamOutSize, rgbWrappedCmdParamOut,
			       pWrappedCmdAuth1, pWrappedCmdAuth2);

	offset = 0;
	switch (unWrappedCommandOrdinal) {
	case TPM_ORD_LoadKey2:
	{
		TCS_KEY_HANDLE tcs_handle = NULL_TCS_HANDLE;

		if ((result = load_key_final(hContext, handle1, &tcs_handle, NULL, val1)))
			goto done;

		*rghHandles[0] = tcs_handle;
		*pulHandleListSize = 1;
		break;
	}
	case TPM_ORD_DSAP:
	case TPM_ORD_OSAP:
	case TPM_ORD_OIAP:
	{
		UINT32 handle;

		UnloadBlob_UINT32(&offset, &handle, *rgbWrappedCmdParamOut);
		result = auth_mgr_add(hContext, handle);
		break;
	}
	default:
		break;
	}

done:
	auth_mgr_release_auth(pWrappedCmdAuth1, pWrappedCmdAuth2, hContext);
	return result;
}

TSS_RESULT
TCSP_ReleaseTransportSigned_Internal(TCS_CONTEXT_HANDLE      hContext,
				     TCS_KEY_HANDLE          hSignatureKey,
				     TPM_NONCE*              AntiReplayNonce,
				     TPM_AUTH*               pKeyAuth,		/* in, out */
				     TPM_AUTH*               pTransAuth,	/* in, out */
				     TPM_MODIFIER_INDICATOR* pbLocality,
				     UINT32*                 pulCurrentTicksSize,
				     BYTE**                  prgbCurrentTicks,
				     UINT32*                 pulSignatureSize,
				     BYTE**                  prgbSignature)
{
	TSS_RESULT result;
	UINT32 paramSize;
	UINT64 offset;
	TPM_KEY_HANDLE keySlot;
	BYTE txBlob[TSS_TPM_TXBLOB_SIZE];

	if ((result = ctx_verify_context(hContext)))
		return result;

	if (pKeyAuth) {
		if ((result = auth_mgr_check(hContext, &pKeyAuth->AuthHandle)))
			return result;
	}

	if ((result = ensureKeyIsLoaded(hContext, hSignatureKey, &keySlot)))
		return result;

	offset = TSS_TPM_TXBLOB_HDR_LEN;
	LoadBlob_UINT32(&offset, keySlot, txBlob);
	LoadBlob(&offset, sizeof(TPM_NONCE), txBlob, (BYTE *)AntiReplayNonce);
	if (pKeyAuth) {
		LoadBlob_Auth(&offset, txBlob, pKeyAuth);
		LoadBlob_Auth(&offset, txBlob, pTransAuth);
		LoadBlob_Header(TPM_TAG_RQU_AUTH2_COMMAND, offset, TPM_ORD_ReleaseTransportSigned,
				txBlob);
	} else {
		LoadBlob_Auth(&offset, txBlob, pTransAuth);
		LoadBlob_Header(TPM_TAG_RQU_AUTH1_COMMAND, offset, TPM_ORD_ReleaseTransportSigned,
				txBlob);
	}

	if ((result = req_mgr_submit_req(txBlob)))
		goto done;

	if ((result = UnloadBlob_Header(txBlob, &paramSize))) {
		LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
		goto done;
	}

	/* unconditionally disable our accounting of the session */
	ctx_set_transport_disabled(hContext, NULL);

	offset = TSS_TPM_TXBLOB_HDR_LEN;
	UnloadBlob_UINT32(&offset, pbLocality, txBlob);

	*pulCurrentTicksSize = sizeof(TPM_STRUCTURE_TAG)
			       + sizeof(UINT64)
			       + sizeof(UINT16)
			       + sizeof(TPM_NONCE);

	*prgbCurrentTicks = malloc(*pulCurrentTicksSize);
	if (*prgbCurrentTicks == NULL) {
		result = TCSERR(TSS_E_OUTOFMEMORY);
		goto done;
	}

	UnloadBlob(&offset, *pulCurrentTicksSize, txBlob, *prgbCurrentTicks);
	UnloadBlob_UINT32(&offset, pulSignatureSize, txBlob);

	*prgbSignature = malloc(*pulSignatureSize);
	if (*prgbSignature == NULL) {
		free(*prgbCurrentTicks);
		result = TCSERR(TSS_E_OUTOFMEMORY);
		goto done;
	}

	UnloadBlob(&offset, *pulSignatureSize, txBlob, *prgbSignature);

	if (pKeyAuth)
		UnloadBlob_Auth(&offset, txBlob, pKeyAuth);
	UnloadBlob_Auth(&offset, txBlob, pTransAuth);

done:
	auth_mgr_release_auth(pKeyAuth, NULL, hContext);
	return result;
}