summaryrefslogtreecommitdiff
path: root/src/tspi/tsp_migration.c
blob: 9c83753fac92e14c676a567b71c94c12d6c30367 (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

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

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

#include "trousers/tss.h"
#include "trousers/trousers.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "obj.h"

#ifdef TSS_BUILD_TRANSPORT
TSS_RESULT
Transport_CreateMigrationBlob(TSS_HCONTEXT tspContext,	/* in */
			      TCS_KEY_HANDLE parentHandle,	/* in */
			      TCPA_MIGRATE_SCHEME migrationType,	/* in */
			      UINT32 MigrationKeyAuthSize,	/* in */
			      BYTE * MigrationKeyAuth,	/* in */
			      UINT32 encDataSize,	/* in */
			      BYTE * encData,	/* in */
			      TPM_AUTH * parentAuth,	/* in, out */
			      TPM_AUTH * entityAuth,	/* in, out */
			      UINT32 * randomSize,	/* out */
			      BYTE ** random,	/* out */
			      UINT32 * outDataSize,	/* out */
			      BYTE ** outData)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 handlesLen, dataLen, decLen;
	TCS_HANDLE *handles, handle;
	TPM_DIGEST pubKeyHash;
	Trspi_HashCtx hashCtx;
	BYTE *data, *dec;


	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	if ((result = obj_tcskey_get_pubkeyhash(parentHandle, pubKeyHash.digest)))
		return result;

	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
	result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
	if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
		return result;

	handlesLen = 1;
	handle = parentHandle;
	handles = &handle;

	dataLen = sizeof(TCPA_MIGRATE_SCHEME)
		  + MigrationKeyAuthSize
		  + sizeof(UINT32)
		  + encDataSize;
	if ((data = malloc(dataLen)) == NULL) {
		LogError("malloc of %u bytes failed", dataLen);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

	offset = 0;
	Trspi_LoadBlob_UINT16(&offset, migrationType, data);
	Trspi_LoadBlob(&offset, MigrationKeyAuthSize, data, MigrationKeyAuth);
	Trspi_LoadBlob_UINT32(&offset, encDataSize, data);
	Trspi_LoadBlob(&offset, encDataSize, data, encData);

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_CreateMigrationBlob,
						    dataLen, data, &pubKeyHash, &handlesLen,
						    &handles, parentAuth, entityAuth, &decLen,
						    &dec))) {
		free(data);
		return result;
	}
	free(data);

	offset = 0;
	Trspi_UnloadBlob_UINT32(&offset, randomSize, dec);

	if ((*random = malloc(*randomSize)) == NULL) {
		free(dec);
		LogError("malloc of %u bytes failed", *randomSize);
		*randomSize = 0;
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	Trspi_UnloadBlob(&offset, *randomSize, dec, *random);

	Trspi_UnloadBlob_UINT32(&offset, outDataSize, dec);

	if ((*outData = malloc(*outDataSize)) == NULL) {
		free(*random);
		*random = NULL;
		*randomSize = 0;
		free(dec);
		LogError("malloc of %u bytes failed", *outDataSize);
		*outDataSize = 0;
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	Trspi_UnloadBlob(&offset, *outDataSize, dec, *outData);
	free(dec);

	return result;
}

TSS_RESULT
Transport_ConvertMigrationBlob(TSS_HCONTEXT tspContext,	/* in */
			       TCS_KEY_HANDLE parentHandle,	/* in */
			       UINT32 inDataSize,	/* in */
			       BYTE * inData,	/* in */
			       UINT32 randomSize,	/* in */
			       BYTE * random,	/* in */
			       TPM_AUTH * parentAuth,	/* in, out */
			       UINT32 * outDataSize,	/* out */
			       BYTE ** outData)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 handlesLen, dataLen, decLen;
	TCS_HANDLE *handles, handle;
	TPM_DIGEST pubKeyHash;
	Trspi_HashCtx hashCtx;
	BYTE *data, *dec;


	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	if ((result = obj_tcskey_get_pubkeyhash(parentHandle, pubKeyHash.digest)))
		return result;

	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
	result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
	if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
		return result;

	handlesLen = 1;
	handle = parentHandle;
	handles = &handle;

	dataLen = (2 * sizeof(UINT32)) + randomSize + inDataSize;
	if ((data = malloc(dataLen)) == NULL) {
		LogError("malloc of %u bytes failed", dataLen);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

	offset = 0;
	Trspi_LoadBlob_UINT32(&offset, inDataSize, data);
	Trspi_LoadBlob(&offset, inDataSize, data, inData);
	Trspi_LoadBlob_UINT32(&offset, randomSize, data);
	Trspi_LoadBlob(&offset, randomSize, data, random);

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_ConvertMigrationBlob,
						    dataLen, data, &pubKeyHash, &handlesLen,
						    &handles, parentAuth, NULL, &decLen, &dec))) {
		free(data);
		return result;
	}
	free(data);

	offset = 0;
	Trspi_UnloadBlob_UINT32(&offset, outDataSize, dec);

	if ((*outData = malloc(*outDataSize)) == NULL) {
		free(dec);
		LogError("malloc of %u bytes failed", *outDataSize);
		*outDataSize = 0;
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	Trspi_UnloadBlob(&offset, *outDataSize, dec, *outData);
	free(dec);

	return result;
}

TSS_RESULT
Transport_AuthorizeMigrationKey(TSS_HCONTEXT tspContext,	/* in */
				TCPA_MIGRATE_SCHEME migrateScheme,	/* in */
				UINT32 MigrationKeySize,	/* in */
				BYTE * MigrationKey,	/* in */
				TPM_AUTH * ownerAuth,	/* in, out */
				UINT32 * MigrationKeyAuthSize,	/* out */
				BYTE ** MigrationKeyAuth)	/* out */
{
	UINT64 offset;
	UINT16 tpmMigrateScheme;
	TSS_RESULT result;
	UINT32 handlesLen = 0, dataLen, decLen;
	BYTE *data, *dec;


	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	/* The TSS_MIGRATE_SCHEME must be changed to a TPM_MIGRATE_SCHEME here, since the TCS
	 * expects a TSS migrate scheme, but this could be wrapped by the TSP before it gets to the
	 * TCS. */
	switch (migrateScheme) {
		case TSS_MS_MIGRATE:
			tpmMigrateScheme = TCPA_MS_MIGRATE;
			break;
		case TSS_MS_REWRAP:
			tpmMigrateScheme = TCPA_MS_REWRAP;
			break;
		case TSS_MS_MAINT:
			tpmMigrateScheme = TCPA_MS_MAINT;
			break;
#ifdef TSS_BUILD_CMK
		case TSS_MS_RESTRICT_MIGRATE:
			tpmMigrateScheme = TPM_MS_RESTRICT_MIGRATE;
			break;

		case TSS_MS_RESTRICT_APPROVE_DOUBLE:
			tpmMigrateScheme = TPM_MS_RESTRICT_APPROVE_DOUBLE;
			break;
#endif
		default:
			return TSPERR(TSS_E_BAD_PARAMETER);
			break;
	}

	dataLen = sizeof(TCPA_MIGRATE_SCHEME) + MigrationKeySize;
	if ((data = malloc(dataLen)) == NULL) {
		LogError("malloc of %u bytes failed", dataLen);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

	offset = 0;
	Trspi_LoadBlob_UINT16(&offset, tpmMigrateScheme, data);
	Trspi_LoadBlob(&offset, MigrationKeySize, data, MigrationKey);

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_AuthorizeMigrationKey,
						    dataLen, data, NULL, &handlesLen, NULL,
						    ownerAuth, NULL, &decLen, &dec))) {
		free(data);
		return result;
	}
	free(data);

	*MigrationKeyAuthSize = decLen;
	*MigrationKeyAuth = dec;

	return result;
}

#endif