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
|
/*
* 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 "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_LoadKeyByBlob_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hUnwrappingKey, /* in */
UINT32 cWrappedKeyBlobSize, /* in */
BYTE * rgbWrappedKeyBlob, /* in */
TPM_AUTH * pAuth, /* in, out */
TCS_KEY_HANDLE * phKeyTCSI, /* out */
TCS_KEY_HANDLE * phKeyHMAC) /* out */
{
return LoadKeyByBlob_Internal(TPM_ORD_LoadKey, hContext, hUnwrappingKey,
cWrappedKeyBlobSize, rgbWrappedKeyBlob, pAuth, phKeyTCSI,
phKeyHMAC);
}
TSS_RESULT
TCSP_LoadKey2ByBlob_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hUnwrappingKey, /* in */
UINT32 cWrappedKeyBlobSize, /* in */
BYTE * rgbWrappedKeyBlob, /* in */
TPM_AUTH * pAuth, /* in, out */
TCS_KEY_HANDLE * phKeyTCSI) /* out */
{
return LoadKeyByBlob_Internal(TPM_ORD_LoadKey2, hContext, hUnwrappingKey,
cWrappedKeyBlobSize, rgbWrappedKeyBlob, pAuth, phKeyTCSI,
NULL);
}
TSS_RESULT
LoadKeyByBlob_Internal(UINT32 ord, /* The ordinal to use, LoadKey or LoadKey2 */
TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hUnwrappingKey, /* in */
UINT32 cWrappedKeyBlobSize, /* in */
BYTE * rgbWrappedKeyBlob, /* in */
TPM_AUTH * pAuth, /* in, out */
TCS_KEY_HANDLE * phKeyTCSI, /* out */
TCS_KEY_HANDLE * phKeyHMAC) /* out */
{
UINT64 offset;
TSS_RESULT result;
UINT32 paramSize;
TPM_KEY_HANDLE parentSlot, newSlot;
TCS_KEY_HANDLE newHandle = NULL_TCS_HANDLE;
TSS_BOOL canLoad;
TSS_KEY key;
BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
if ((result = ctx_verify_context(hContext)))
return result;
LogDebugFn("Enter");
LogDebugUnrollKey(rgbWrappedKeyBlob);
if ((result = get_slot(hContext, hUnwrappingKey, &parentSlot)))
return result;
offset = 0;
memset(&key, 0, sizeof(TSS_KEY));
if ((result = UnloadBlob_TSS_KEY(&offset, rgbWrappedKeyBlob, &key)))
return result;
if (!pAuth) {
LogDebugFn("Checking if LoadKeyByBlob can be avoided by using existing key");
if ((newHandle = mc_get_handle_by_pub(&key.pubKey, hUnwrappingKey))) {
LogDebugFn("tcs key handle exists");
newSlot = mc_get_slot_by_handle(newHandle);
if (newSlot && (isKeyLoaded(newSlot) == TRUE)) {
LogDebugFn("Don't need to reload this key.");
*phKeyTCSI = newHandle;
if (phKeyHMAC)
*phKeyHMAC = newSlot;
return TSS_SUCCESS;
}
}
}
LogDebugFn("calling canILoadThisKey");
if ((result = canILoadThisKey(&(key.algorithmParms), &canLoad)))
goto error;
if (canLoad == FALSE) {
LogDebugFn("calling evictFirstKey");
/* Evict a key that isn't the parent */
if ((result = evictFirstKey(hUnwrappingKey)))
goto error;
}
offset = 0;
if ((result = tpm_rqu_build(ord, &offset, txBlob, parentSlot, cWrappedKeyBlobSize,
rgbWrappedKeyBlob, pAuth, NULL)))
goto error;
LogDebugFn("Submitting request to the TPM");
if ((result = req_mgr_submit_req(txBlob)))
goto error;
if ((result = UnloadBlob_Header(txBlob, ¶mSize))) {
LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
goto error;
}
if ((result = tpm_rsp_parse(ord, txBlob, paramSize, &newSlot, pAuth)))
goto error;
if ((result = load_key_final(hContext, hUnwrappingKey, &newHandle, rgbWrappedKeyBlob,
newSlot)))
goto error;
/* Setup the outHandles */
*phKeyTCSI = newHandle;
if (phKeyHMAC)
*phKeyHMAC = newSlot;
LogDebugFn("Key handles for loadKeyByBlob slot:%.8X tcshandle:%.8X", newSlot, newHandle);
error:
auth_mgr_release_auth(pAuth, NULL, hContext);
return result;
}
TSS_RESULT
TCSP_EvictKey_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hKey) /* in */
{
TSS_RESULT result;
TCPA_KEY_HANDLE tpm_handle;
if ((result = ctx_verify_context(hContext)))
return result;
tpm_handle = mc_get_slot_by_handle(hKey);
if (tpm_handle == NULL_TPM_HANDLE)
return TSS_SUCCESS; /*let's call this success if the key is already evicted */
if ((result = internal_EvictByKeySlot(tpm_handle)))
return result;
result = mc_set_slot_by_slot(tpm_handle, NULL_TPM_HANDLE);
return result;
}
TSS_RESULT
TCSP_CreateWrapKey_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hWrappingKey, /* in */
TCPA_ENCAUTH KeyUsageAuth, /* in */
TCPA_ENCAUTH KeyMigrationAuth, /* in */
UINT32 keyInfoSize, /* in */
BYTE * keyInfo, /* in */
UINT32 * keyDataSize, /* out */
BYTE ** keyData, /* out */
TPM_AUTH * pAuth) /* in, out */
{
UINT64 offset = 0;
UINT32 paramSize;
TSS_RESULT result;
TCPA_KEY_HANDLE parentSlot;
BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
LogDebug("Entering Create Wrap Key");
if ((result = ctx_verify_context(hContext)))
goto done;
if (pAuth) {
if ((result = auth_mgr_check(hContext, &pAuth->AuthHandle)))
goto done;
}
/* Since hWrappingKey must already be loaded, we can fail immediately if
* mc_get_slot_by_handle_lock() fails.*/
parentSlot = mc_get_slot_by_handle_lock(hWrappingKey);
if (parentSlot == NULL_TPM_HANDLE) {
result = TCSERR(TSS_E_FAIL);
goto done;
}
if ((result = tpm_rqu_build(TPM_ORD_CreateWrapKey, &offset, txBlob, parentSlot,
KeyUsageAuth.authdata, KeyMigrationAuth.authdata, keyInfoSize,
keyInfo, pAuth)))
goto done;
if ((result = req_mgr_submit_req(txBlob)))
goto done;
result = UnloadBlob_Header(txBlob, ¶mSize);
if (!result) {
result = tpm_rsp_parse(TPM_ORD_CreateWrapKey, txBlob, paramSize, keyDataSize,
keyData, pAuth);
}
LogResult("Create Wrap Key", result);
done:
auth_mgr_release_auth(pAuth, NULL, hContext);
return result;
}
TSS_RESULT
TCSP_GetPubKey_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hKey, /* in */
TPM_AUTH * pAuth, /* in, out */
UINT32 * pcPubKeySize, /* out */
BYTE ** prgbPubKey) /* out */
{
UINT64 offset = 0;
UINT32 paramSize;
TSS_RESULT result;
TCPA_KEY_HANDLE keySlot;
BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
LogDebug("Entering Get pub key");
if ((result = ctx_verify_context(hContext)))
goto done;
if (pAuth != NULL) {
LogDebug("Auth Used");
if ((result = auth_mgr_check(hContext, &pAuth->AuthHandle)))
goto done;
} else {
LogDebug("No Auth");
}
if (ensureKeyIsLoaded(hContext, hKey, &keySlot)) {
result = TCSERR(TCS_E_KM_LOADFAILED);
goto done;
}
LogDebug("GetPubKey: handle: 0x%x, slot: 0x%x", hKey, keySlot);
if ((result = tpm_rqu_build(TPM_ORD_GetPubKey, &offset, txBlob, keySlot, pAuth)))
goto done;
if ((result = req_mgr_submit_req(txBlob)))
goto done;
offset = 10;
result = UnloadBlob_Header(txBlob, ¶mSize);
if (!result) {
result = tpm_rsp_parse(TPM_ORD_GetPubKey, txBlob, paramSize, pcPubKeySize,
prgbPubKey, pAuth);
}
LogResult("Get Public Key", result);
done:
auth_mgr_release_auth(pAuth, NULL, hContext);
return result;
}
TSS_RESULT
TCSP_OwnerReadInternalPub_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hKey, /* in */
TPM_AUTH * pOwnerAuth, /* in, out */
UINT32 * punPubKeySize, /* out */
BYTE ** ppbPubKeyData) /* out */
{
UINT64 offset = 0;
UINT32 paramSize;
TSS_RESULT result;
BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
LogDebug("Entering OwnerReadInternalPub");
if ((result = ctx_verify_context(hContext)))
goto done;
LogDebug("OwnerReadInternalPub: handle: 0x%x", hKey);
if (hKey != TPM_KH_SRK && hKey != TPM_KH_EK) {
result = TCSERR(TSS_E_FAIL);
LogDebug("OwnerReadInternalPub - Unsupported Key Handle");
goto done;
}
if ((result = auth_mgr_check(hContext, &pOwnerAuth->AuthHandle)))
goto done;
if ((result = tpm_rqu_build(TPM_ORD_OwnerReadInternalPub, &offset, txBlob, hKey,
pOwnerAuth)))
goto done;
if ((result = req_mgr_submit_req(txBlob)))
goto done;
result = UnloadBlob_Header(txBlob, ¶mSize);
if (!result) {
result = tpm_rsp_parse(TPM_ORD_OwnerReadInternalPub, txBlob, paramSize,
punPubKeySize, ppbPubKeyData, pOwnerAuth);
}
LogResult("OwnerReadInternalPub", result);
done:
auth_mgr_release_auth(pOwnerAuth, NULL, hContext);
return result;
}
TSS_RESULT
TCSP_KeyControlOwner_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
TCS_KEY_HANDLE hTcsKey, /* in */
UINT32 ulPubKeyLength, /* in */
BYTE* rgbPubKey, /* in */
UINT32 attribName, /* in */
TSS_BOOL attribValue, /* in */
TPM_AUTH* pOwnerAuth, /* in,out */
TSS_UUID* pUuidData) /* out */
{
UINT64 offset = 0;
UINT32 paramSize;
TSS_RESULT result;
TPM_KEY_HANDLE hTpmKey;
BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
LogDebugFn("Enter");
if ((result = ctx_verify_context(hContext))) {
LogDebug("Invalid TSS Context");
goto done;
}
if ((result = get_slot_lite(hContext, hTcsKey, &hTpmKey))) {
LogDebug("Can't get TPM Keyhandle for TCS key 0x%x", hTcsKey);
goto done;
}
LogDebugFn("TCS hKey=0x%x, TPM hKey=0x%x", hTcsKey, hTpmKey);
if ((result = auth_mgr_check(hContext, &pOwnerAuth->AuthHandle))) {
LogDebug("Owner Authentication failed");
goto done;
}
if ((result = mc_find_next_ownerevict_uuid(pUuidData))) {
LogDebugFn("mc_find_next_ownerevict_uuid failed: rc=0x%x", result);
goto done;
}
if ((result = tpm_rqu_build(TPM_ORD_KeyControlOwner, &offset, txBlob, hTpmKey,
ulPubKeyLength, rgbPubKey, attribName, attribValue,
pOwnerAuth))) {
LogDebugFn("rqu build failed");
goto done;
}
if ((result = req_mgr_submit_req(txBlob))) {
LogDebugFn("Request submission failed");
goto done;
}
if ((result = UnloadBlob_Header(txBlob, ¶mSize))) {
LogDebugFn("UnloadBlob_Header failed: rc=0x%x", result);
goto done;
}
if ((result = tpm_rsp_parse(TPM_ORD_KeyControlOwner, txBlob, paramSize, pOwnerAuth))) {
LogDebugFn("tpm_rsp_parse failed: rc=0x%x", result);
goto done;
}
if ((result = mc_set_uuid(hTcsKey, pUuidData))){
LogDebugFn("mc_set_uuid failed: rc=0x%x", result);
goto done;
}
LogResult("KeyControlOwner", result);
done:
auth_mgr_release_auth(pOwnerAuth, NULL, hContext);
return result;
}
|