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
|
/*
* Licensed Materials - Property of IBM
*
* trousers - An open source TCG Software Stack
*
* (C) Copyright International Business Machines Corp. 2004-2006
*
*/
#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"
TSS_RESULT
Tspi_TPM_AuthorizeMigrationTicket(TSS_HTPM hTPM, /* in */
TSS_HKEY hMigrationKey, /* in */
TSS_MIGRATION_SCHEME migrationScheme, /* in */
UINT32 * pulMigTicketLength, /* out */
BYTE ** prgbMigTicket) /* out */
{
UINT64 offset;
TCPA_DIGEST digest;
TCPA_RESULT result;
TSS_HPOLICY hOwnerPolicy;
UINT32 migrationKeySize;
BYTE *migrationKeyBlob;
TSS_KEY tssKey;
BYTE pubKeyBlob[0x1000];
TPM_AUTH ownerAuth;
UINT32 pubKeySize;
TSS_HCONTEXT tspContext;
UINT32 tpmMigrationScheme;
Trspi_HashCtx hashCtx;
if (pulMigTicketLength == NULL || prgbMigTicket == NULL)
return TSPERR(TSS_E_BAD_PARAMETER);
if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
return result;
/* get the tpm Policy */
if ((result = obj_tpm_get_policy(hTPM, TSS_POLICY_USAGE, &hOwnerPolicy)))
return result;
switch (migrationScheme) {
case TSS_MS_MIGRATE:
tpmMigrationScheme = TCPA_MS_MIGRATE;
break;
case TSS_MS_REWRAP:
tpmMigrationScheme = TCPA_MS_REWRAP;
break;
case TSS_MS_MAINT:
tpmMigrationScheme = TCPA_MS_MAINT;
break;
#ifdef TSS_BUILD_CMK
case TSS_MS_RESTRICT_MIGRATE:
tpmMigrationScheme = TPM_MS_RESTRICT_MIGRATE;
break;
case TSS_MS_RESTRICT_APPROVE_DOUBLE:
tpmMigrationScheme = TPM_MS_RESTRICT_APPROVE_DOUBLE;
break;
#endif
default:
return TSPERR(TSS_E_BAD_PARAMETER);
break;
}
/* Get the migration key blob */
if ((result = obj_rsakey_get_blob(hMigrationKey, &migrationKeySize, &migrationKeyBlob)))
return result;
/* First, turn the keyBlob into a TSS_KEY structure */
offset = 0;
memset(&tssKey, 0, sizeof(TSS_KEY));
if ((result = UnloadBlob_TSS_KEY(&offset, migrationKeyBlob, &tssKey))) {
free_tspi(tspContext, migrationKeyBlob);
return result;
}
free_tspi(tspContext, migrationKeyBlob);
/* Then pull the _PUBKEY portion out of that struct into a blob */
offset = 0;
Trspi_LoadBlob_KEY_PARMS(&offset, pubKeyBlob, &tssKey.algorithmParms);
Trspi_LoadBlob_STORE_PUBKEY(&offset, pubKeyBlob, &tssKey.pubKey);
pubKeySize = offset;
free_key_refs(&tssKey);
/* Auth */
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_AuthorizeMigrationKey);
result |= Trspi_Hash_UINT16(&hashCtx, tpmMigrationScheme);
result |= Trspi_HashUpdate(&hashCtx, pubKeySize, pubKeyBlob);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
return result;
if ((result = secret_PerformAuth_OIAP(hTPM, TPM_ORD_AuthorizeMigrationKey, hOwnerPolicy,
FALSE, &digest, &ownerAuth)))
return result;
/* Send command */
if ((result = TCS_API(tspContext)->AuthorizeMigrationKey(tspContext, migrationScheme,
pubKeySize, pubKeyBlob, &ownerAuth,
pulMigTicketLength,
prgbMigTicket)))
return result;
/* Validate Auth */
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, result);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_AuthorizeMigrationKey);
result |= Trspi_HashUpdate(&hashCtx, *pulMigTicketLength, *prgbMigTicket);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest))) {
*pulMigTicketLength = 0;
free(*prgbMigTicket);
return result;
}
if ((result = obj_policy_validate_auth_oiap(hOwnerPolicy, &digest, &ownerAuth))) {
*pulMigTicketLength = 0;
free(*prgbMigTicket);
return result;
}
if ((result = __tspi_add_mem_entry(tspContext, *prgbMigTicket))) {
*pulMigTicketLength = 0;
free(*prgbMigTicket);
return result;
}
return TSS_SUCCESS;
}
TSS_RESULT
Tspi_Key_CreateMigrationBlob(TSS_HKEY hKeyToMigrate, /* in */
TSS_HKEY hParentKey, /* in */
UINT32 ulMigTicketLength, /* in */
BYTE * rgbMigTicket, /* in */
UINT32 * pulRandomLength, /* out */
BYTE ** prgbRandom, /* out */
UINT32 * pulMigrationBlobLength, /* out */
BYTE ** prgbMigrationBlob) /* out */
{
TPM_AUTH parentAuth, entityAuth;
TPM_AUTH *pParentAuth;
TCPA_RESULT result;
UINT64 offset;
TCPA_DIGEST digest;
UINT32 keyToMigrateSize;
BYTE *keyToMigrateBlob = NULL;
TSS_HPOLICY hParentPolicy;
TSS_HPOLICY hMigratePolicy;
TCPA_MIGRATIONKEYAUTH migAuth;
TSS_KEY tssKey;
TCS_KEY_HANDLE parentHandle;
TSS_BOOL parentUsesAuth;
UINT32 randomSize;
BYTE *random = NULL;
UINT32 blobSize;
BYTE *blob = NULL;
TSS_HCONTEXT tspContext;
Trspi_HashCtx hashCtx;
memset(&tssKey, 0, sizeof(TSS_KEY));
if (pulRandomLength == NULL || prgbRandom == NULL || rgbMigTicket == NULL ||
pulMigrationBlobLength == NULL || prgbMigrationBlob == NULL)
return TSPERR(TSS_E_BAD_PARAMETER);
if (!obj_is_rsakey(hKeyToMigrate))
return TSPERR(TSS_E_INVALID_HANDLE);
if ((result = obj_rsakey_get_tsp_context(hKeyToMigrate, &tspContext)))
return result;
if ((result = obj_rsakey_get_blob(hKeyToMigrate, &keyToMigrateSize, &keyToMigrateBlob)))
goto done;
if ((result = obj_rsakey_get_policy(hParentKey, TSS_POLICY_USAGE, &hParentPolicy,
&parentUsesAuth)))
goto done;
if ((result = obj_rsakey_get_policy(hKeyToMigrate, TSS_POLICY_MIGRATION, &hMigratePolicy,
NULL)))
goto done;
/* Parsing the migration scheme from the blob and key object */
memset(&migAuth, 0, sizeof(TCPA_MIGRATIONKEYAUTH));
offset = 0;
if ((result = Trspi_UnloadBlob_MIGRATIONKEYAUTH(&offset, rgbMigTicket, &migAuth)))
goto done;
/* free these now, since none are used below */
free(migAuth.migrationKey.algorithmParms.parms);
migAuth.migrationKey.algorithmParms.parmSize = 0;
free(migAuth.migrationKey.pubKey.key);
migAuth.migrationKey.pubKey.keyLength = 0;
offset = 0;
if ((result = UnloadBlob_TSS_KEY(&offset, keyToMigrateBlob, &tssKey)))
goto done;
/* Generate the Authorization data */
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_CreateMigrationBlob);
result |= Trspi_Hash_UINT16(&hashCtx, migAuth.migrationScheme);
result |= Trspi_HashUpdate(&hashCtx, ulMigTicketLength, rgbMigTicket);
result |= Trspi_Hash_UINT32(&hashCtx, tssKey.encSize);
result |= Trspi_HashUpdate(&hashCtx, tssKey.encSize, tssKey.encData);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
goto done;
if (parentUsesAuth) {
if ((result = secret_PerformAuth_OIAP(hParentPolicy, TPM_ORD_CreateMigrationBlob,
hParentPolicy, FALSE, &digest,
&parentAuth)))
goto done;
pParentAuth = &parentAuth;
} else {
pParentAuth = NULL;
}
if ((result = secret_PerformAuth_OIAP(hKeyToMigrate, TPM_ORD_CreateMigrationBlob,
hMigratePolicy, FALSE, &digest, &entityAuth)))
goto done;
if ((result = obj_rsakey_get_tcs_handle(hParentKey, &parentHandle)))
goto done;
if ((result = TCS_API(tspContext)->CreateMigrationBlob(tspContext, parentHandle,
migAuth.migrationScheme,
ulMigTicketLength, rgbMigTicket,
tssKey.encSize, tssKey.encData,
pParentAuth, &entityAuth,
&randomSize, &random,
&blobSize, &blob)))
goto done;
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, result);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_CreateMigrationBlob);
result |= Trspi_Hash_UINT32(&hashCtx, randomSize);
result |= Trspi_HashUpdate(&hashCtx, randomSize, random);
result |= Trspi_Hash_UINT32(&hashCtx, blobSize);
result |= Trspi_HashUpdate(&hashCtx, blobSize, blob);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
goto done;
if (parentUsesAuth) {
if ((result = obj_policy_validate_auth_oiap(hParentPolicy, &digest, &parentAuth)))
goto done;
}
if ((result = obj_policy_validate_auth_oiap(hMigratePolicy, &digest, &entityAuth)))
goto done;
free(tssKey.encData);
tssKey.encSize = blobSize;
tssKey.encData = blob;
/* Set blob to null since it will now be freed during key ref freeing */
blob = NULL;
offset = 0;
LoadBlob_TSS_KEY(&offset, NULL, &tssKey);
*pulMigrationBlobLength = offset;
*prgbMigrationBlob = calloc_tspi(tspContext, *pulMigrationBlobLength);
if (*prgbMigrationBlob == NULL) {
LogError("malloc of %u bytes failed.", *pulMigrationBlobLength);
result = TSPERR(TSS_E_OUTOFMEMORY);
goto done;
}
offset = 0;
LoadBlob_TSS_KEY(&offset, *prgbMigrationBlob, &tssKey);
if (randomSize) {
if ((result = __tspi_add_mem_entry(tspContext, random)))
goto done;
}
*pulRandomLength = randomSize;
*prgbRandom = random;
done:
if (result)
free(random);
free_tspi(tspContext, keyToMigrateBlob);
free_key_refs(&tssKey);
free(blob);
return result;
}
TSS_RESULT
Tspi_Key_ConvertMigrationBlob(TSS_HKEY hKeyToMigrate, /* in */
TSS_HKEY hParentKey, /* in */
UINT32 ulRandomLength, /* in */
BYTE * rgbRandom, /* in */
UINT32 ulMigrationBlobLength, /* in */
BYTE * rgbMigrationBlob) /* in */
{
TCPA_RESULT result;
TSS_KEY tssKey;
UINT32 outDataSize;
BYTE *outData = NULL;
TCS_KEY_HANDLE parentHandle;
TPM_AUTH parentAuth;
TSS_HPOLICY hParentPolicy;
TCPA_DIGEST digest;
TSS_BOOL useAuth;
TPM_AUTH *pParentAuth;
TSS_HCONTEXT tspContext;
Trspi_HashCtx hashCtx;
UINT64 offset;
memset(&tssKey, 0, sizeof(TSS_KEY));
if ((result = obj_rsakey_get_tsp_context(hKeyToMigrate, &tspContext)))
return result;
if (!obj_is_rsakey(hParentKey))
return TSPERR(TSS_E_INVALID_HANDLE);
/* Get the parent key handle */
if ((result = obj_rsakey_get_tcs_handle(hParentKey, &parentHandle)))
return result;
/* Get the policy */
if ((result = obj_rsakey_get_policy(hParentKey, TSS_POLICY_USAGE,
&hParentPolicy, &useAuth)))
return result;
offset = 0;
if ((result = UnloadBlob_TSS_KEY(&offset, rgbMigrationBlob, &tssKey)))
return result;
/* Generate the authorization */
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_ConvertMigrationBlob);
result |= Trspi_Hash_UINT32(&hashCtx, tssKey.encSize);
result |= Trspi_HashUpdate(&hashCtx, tssKey.encSize, tssKey.encData);
result |= Trspi_Hash_UINT32(&hashCtx, ulRandomLength);
result |= Trspi_HashUpdate(&hashCtx, ulRandomLength, rgbRandom);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
goto done;
if (useAuth) {
if ((result = secret_PerformAuth_OIAP(hParentPolicy, TPM_ORD_ConvertMigrationBlob,
hParentPolicy, FALSE, &digest, &parentAuth)))
goto done;
pParentAuth = &parentAuth;
} else {
pParentAuth = NULL;
}
if ((result = TCS_API(tspContext)->ConvertMigrationBlob(tspContext, parentHandle,
tssKey.encSize, tssKey.encData,
ulRandomLength, rgbRandom,
pParentAuth,
&outDataSize, &outData)))
goto done;
/* add validation */
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, result);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_ConvertMigrationBlob);
result |= Trspi_Hash_UINT32(&hashCtx, outDataSize);
result |= Trspi_HashUpdate(&hashCtx, outDataSize, outData);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
goto done;
if (useAuth) {
if ((result = obj_policy_validate_auth_oiap(hParentPolicy, &digest, &parentAuth)))
goto done;
}
/* Set the key object to the now migrated key */
if ((result = obj_rsakey_set_tcpakey(hKeyToMigrate, ulMigrationBlobLength, rgbMigrationBlob)))
goto done;
if ((result = obj_rsakey_set_privkey(hKeyToMigrate, TRUE, outDataSize, outData)))
goto done;
result = obj_rsakey_set_tcs_handle(hKeyToMigrate, 0);
done:
free_key_refs(&tssKey);
free(outData);
return result;
}
|