3
* Licensed Materials - Property of IBM
5
* trousers - An open source TCG Software Stack
7
* (C) Copyright International Business Machines Corp. 2004, 2005
18
#include "trousers/tss.h"
19
#include "trousers/trousers.h"
20
#include "spi_internal_types.h"
21
#include "spi_utils.h"
22
#include "capabilities.h"
29
* newPIN - non-zero to popup the dialog to enter a new PIN, zero to popup a dialog
30
* to enter an existing PIN
31
* hash_mode - flag indicating whether to include null terminating data in the hash
32
* of the secret (1.2 backport only).
33
* popup_str - string to appear in the title bar of the popup dialog
34
* auth_hash - the 20+ byte buffer that receives the SHA1 hash of the auth data
35
* entered into the dialog box
39
popup_GetSecret(UINT32 new_pin, UINT32 hash_mode, BYTE *popup_str, void *auth_hash)
41
BYTE secret[UI_MAX_SECRET_STRING_LENGTH] = { 0 };
42
BYTE *dflt = (BYTE *)"TSS Authentication Dialog";
45
if (popup_str == NULL)
48
/* pin the area where the secret will be put in memory */
49
if (pin_mem(&secret, UI_MAX_SECRET_STRING_LENGTH)) {
50
LogError("Failed to pin secret in memory.");
51
return TSPERR(TSS_E_INTERNAL_ERROR);
55
DisplayNewPINWindow(secret, &secret_len, popup_str);
57
DisplayPINWindow(secret, &secret_len, popup_str);
60
unpin_mem(&secret, UI_MAX_SECRET_STRING_LENGTH);
61
return TSPERR(TSS_E_POLICY_NO_SECRET);
64
#ifndef TSS_SPEC_COMPLIANCE
65
if (hash_mode == TSS_TSPATTRIB_HASH_MODE_NOT_NULL)
66
secret_len -= sizeof(UNICODE); // Take off the NULL terminator
68
LogDebug("Hashing these %u bytes as the secret:", secret_len);
69
LogDebugData(secret_len, secret);
70
Trspi_Hash(TSS_HASH_SHA1, secret_len, secret, auth_hash);
72
/* zero, then unpin the memory */
73
memset(secret, 0, secret_len);
74
unpin_mem(&secret, UI_MAX_SECRET_STRING_LENGTH);
80
secret_PerformAuth_OIAP(TSS_HOBJECT hAuthorizedObject, UINT32 ulPendingFn,
81
TSS_HPOLICY hPolicy, TCPA_DIGEST *hashDigest,
88
TSS_HCONTEXT tspContext;
90
/* This validates that the secret can be used */
91
if ((result = obj_policy_has_expired(hPolicy, &bExpired)))
95
return TSPERR(TSS_E_INVALID_OBJ_ACCESS);
97
if ((result = obj_policy_get_tsp_context(hPolicy, &tspContext)))
100
if ((result = obj_policy_get_mode(hPolicy, &mode)))
103
if ((result = Init_AuthNonce(tspContext, auth)))
106
/* added retry logic */
107
if ((result = TCSP_OIAP(tspContext, &auth->AuthHandle, &auth->NonceEven))) {
108
if (result == TCPA_E_RESOURCES) {
111
/* POSIX sleep time, { secs, nanosecs } */
112
struct timespec t = { 0, AUTH_RETRY_NANOSECS };
116
result = TCSP_OIAP(tspContext, &auth->AuthHandle, &auth->NonceEven);
117
} while (result == TCPA_E_RESOURCES && ++retry < AUTH_RETRY_COUNT);
125
case TSS_SECRET_MODE_CALLBACK:
126
result = obj_policy_do_hmac(hPolicy, hAuthorizedObject,
128
auth->fContinueAuthSession,
130
auth->NonceEven.nonce,
131
auth->NonceOdd.nonce,
134
(BYTE *)&auth->HMAC);
136
case TSS_SECRET_MODE_SHA1:
137
case TSS_SECRET_MODE_PLAIN:
138
case TSS_SECRET_MODE_POPUP:
139
if ((result = obj_policy_get_secret(hPolicy, TR_SECRET_CTX_NOT_NEW,
143
HMAC_Auth(secret.authdata, hashDigest->digest, auth);
145
case TSS_SECRET_MODE_NONE:
148
result = TSPERR(TSS_E_POLICY_NO_SECRET);
153
TCSP_TerminateHandle(tspContext, auth->AuthHandle);
157
return obj_policy_dec_counter(hPolicy);
161
secret_PerformXOR_OSAP(TSS_HPOLICY hPolicy, TSS_HPOLICY hUsagePolicy,
162
TSS_HPOLICY hMigrationPolicy, TSS_HOBJECT hOSAPObject,
163
UINT16 osapType, UINT32 osapData,
164
TCPA_ENCAUTH * encAuthUsage, TCPA_ENCAUTH * encAuthMig,
165
BYTE *sharedSecret, TPM_AUTH * auth, TCPA_NONCE * nonceEvenOSAP)
168
TCPA_SECRET keySecret;
169
TCPA_SECRET usageSecret;
170
TCPA_SECRET migSecret = { { 0, } };
171
UINT32 keyMode, usageMode, migMode = 0;
173
TSS_HCONTEXT tspContext;
175
if ((result = obj_policy_has_expired(hPolicy, &bExpired)))
178
if (bExpired == TRUE)
179
return TSPERR(TSS_E_INVALID_OBJ_ACCESS);
181
if ((result = obj_policy_has_expired(hUsagePolicy, &bExpired)))
184
if (bExpired == TRUE)
185
return TSPERR(TSS_E_INVALID_OBJ_ACCESS);
187
if (hMigrationPolicy) {
188
if ((result = obj_policy_has_expired(hMigrationPolicy, &bExpired)))
191
if (bExpired == TRUE)
192
return TSPERR(TSS_E_INVALID_OBJ_ACCESS);
194
if ((result = obj_policy_get_mode(hMigrationPolicy, &migMode)))
199
if ((result = obj_policy_get_tsp_context(hPolicy, &tspContext)))
202
if ((result = obj_policy_get_mode(hPolicy, &keyMode)))
205
if ((result = obj_policy_get_mode(hUsagePolicy, &usageMode)))
208
if (keyMode == TSS_SECRET_MODE_CALLBACK ||
209
usageMode == TSS_SECRET_MODE_CALLBACK ||
210
migMode == TSS_SECRET_MODE_CALLBACK) {
211
if (keyMode != TSS_SECRET_MODE_CALLBACK ||
212
usageMode != TSS_SECRET_MODE_CALLBACK ||
213
(hMigrationPolicy && migMode != TSS_SECRET_MODE_CALLBACK))
214
return TSPERR(TSS_E_BAD_PARAMETER);
217
if (keyMode != TSS_SECRET_MODE_CALLBACK) {
218
if ((result = obj_policy_get_secret(hPolicy, TR_SECRET_CTX_NOT_NEW, &keySecret)))
221
if ((result = obj_policy_get_secret(hUsagePolicy, TR_SECRET_CTX_NEW, &usageSecret)))
224
if (hMigrationPolicy) {
225
if ((result = obj_policy_get_secret(hMigrationPolicy, TR_SECRET_CTX_NEW,
230
if ((result = OSAP_Calc(tspContext, osapType, osapData,
231
keySecret.authdata, usageSecret.authdata,
232
migSecret.authdata, encAuthUsage,
233
encAuthMig, sharedSecret, auth)))
236
/* If the secret mode is NONE here, we don't return an error. This is
237
* because there are commands such as CreateKey, which require an auth
238
* session even when creating no-auth keys. A secret of all 0's will be
239
* used in this case. */
240
if ((result = TCSP_OSAP(tspContext, osapType, osapData,
241
auth->NonceOdd, &auth->AuthHandle,
242
&auth->NonceEven, nonceEvenOSAP)))
245
if ((result = obj_policy_do_xor(hPolicy, hOSAPObject,
247
auth->NonceEven.nonce, NULL,
248
nonceEvenOSAP->nonce,
249
auth->NonceOdd.nonce, 20,
250
encAuthUsage->authdata,
251
encAuthMig->authdata))) {
252
TCSP_TerminateHandle(tspContext, auth->AuthHandle);
261
secret_PerformAuth_OSAP(TSS_HOBJECT hAuthorizedObject, UINT32 ulPendingFn,
262
TSS_HPOLICY hPolicy, TSS_HPOLICY hUsagePolicy,
263
TSS_HPOLICY hMigPolicy, BYTE sharedSecret[20],
264
TPM_AUTH *auth, BYTE *hashDigest,
265
TCPA_NONCE *nonceEvenOSAP)
268
UINT32 keyMode, usageMode, migMode = 0;
270
if ((result = obj_policy_get_mode(hPolicy, &keyMode)))
273
if ((result = obj_policy_get_mode(hUsagePolicy, &usageMode)))
277
if ((result = obj_policy_get_mode(hMigPolicy, &migMode)))
281
/* --- If any of them is a callback */
282
if (keyMode == TSS_SECRET_MODE_CALLBACK ||
283
usageMode == TSS_SECRET_MODE_CALLBACK ||
284
(hMigPolicy && migMode == TSS_SECRET_MODE_CALLBACK)) {
285
/* --- And they're not all callback */
286
if (keyMode != TSS_SECRET_MODE_CALLBACK ||
287
usageMode != TSS_SECRET_MODE_CALLBACK ||
288
(hMigPolicy && migMode != TSS_SECRET_MODE_CALLBACK))
289
return TSPERR(TSS_E_BAD_PARAMETER);
292
if (keyMode == TSS_SECRET_MODE_CALLBACK) {
293
if ((result = obj_policy_do_hmac(hPolicy, hAuthorizedObject,
295
auth->fContinueAuthSession,
297
auth->NonceEven.nonce,
299
nonceEvenOSAP->nonce,
300
auth->NonceOdd.nonce, 20,
302
(BYTE *)&auth->HMAC)))
305
HMAC_Auth(sharedSecret, hashDigest, auth);
308
if ((result = obj_policy_dec_counter(hPolicy)))
311
if ((result = obj_policy_dec_counter(hUsagePolicy)))
315
if ((result = obj_policy_dec_counter(hMigPolicy)))
323
secret_ValidateAuth_OSAP(TSS_HOBJECT hAuthorizedObject, UINT32 ulPendingFn,
324
TSS_HPOLICY hPolicy, TSS_HPOLICY hUsagePolicy,
325
TSS_HPOLICY hMigPolicy, BYTE sharedSecret[20],
326
TPM_AUTH *auth, BYTE *hashDigest,
327
TCPA_NONCE *nonceEvenOSAP)
330
UINT32 keyMode, usageMode, migMode = 0;
332
if ((result = obj_policy_get_mode(hPolicy, &keyMode)))
335
if ((result = obj_policy_get_mode(hUsagePolicy, &usageMode)))
339
if ((result = obj_policy_get_mode(hMigPolicy, &migMode)))
343
/* --- If any of them is a callback */
344
if (keyMode == TSS_SECRET_MODE_CALLBACK ||
345
usageMode == TSS_SECRET_MODE_CALLBACK ||
346
migMode == TSS_SECRET_MODE_CALLBACK) {
347
/* --- And they're not all callback */
348
if (keyMode != TSS_SECRET_MODE_CALLBACK ||
349
usageMode != TSS_SECRET_MODE_CALLBACK ||
350
(hMigPolicy && migMode != TSS_SECRET_MODE_CALLBACK))
351
return TSPERR(TSS_E_BAD_PARAMETER);
354
if (keyMode != TSS_SECRET_MODE_CALLBACK) {
355
if (validateReturnAuth(sharedSecret, hashDigest, auth))
356
return TSPERR(TSS_E_TSP_AUTHFAIL);
358
if ((result = obj_policy_do_hmac(hPolicy, hAuthorizedObject,
360
auth->fContinueAuthSession,
362
auth->NonceEven.nonce,
364
nonceEvenOSAP->nonce,
365
auth->NonceOdd.nonce, 20,
367
(BYTE *)&auth->HMAC)))
375
secret_TakeOwnership(TSS_HKEY hEndorsementPubKey,
379
UINT32 * encOwnerAuthLength,
380
BYTE * encOwnerAuth, UINT32 * encSRKAuthLength, BYTE * encSRKAuth)
383
UINT32 endorsementKeySize;
384
BYTE *endorsementKey;
387
TCPA_SECRET ownerSecret;
388
TCPA_SECRET srkSecret;
391
TSS_HPOLICY hSrkPolicy;
392
TSS_HPOLICY hOwnerPolicy;
393
UINT32 srkKeyBlobLength;
395
TSS_HCONTEXT tspContext;
396
UINT32 ownerMode, srkMode;
398
if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
401
/*************************************************
402
* First, get the policy objects and check them for how
403
* to handle the secrets. If they cannot be found
404
* or there is an error, then we must fail
405
**************************************************/
407
/* First get the Owner Policy */
408
if ((result = Tspi_GetPolicyObject(hTPM, TSS_POLICY_USAGE, &hOwnerPolicy)))
411
/* Now get the SRK Policy */
413
if ((result = Tspi_GetPolicyObject(hKeySRK, TSS_POLICY_USAGE, &hSrkPolicy)))
416
if ((result = obj_policy_get_mode(hOwnerPolicy, &ownerMode)))
419
if ((result = obj_policy_get_mode(hSrkPolicy, &srkMode)))
422
/* If the policy callback's aren't the same, that's an error if one is callback */
423
if (srkMode == TSS_SECRET_MODE_CALLBACK ||
424
ownerMode == TSS_SECRET_MODE_CALLBACK) {
425
if (srkMode != TSS_SECRET_MODE_CALLBACK ||
426
ownerMode != TSS_SECRET_MODE_CALLBACK) {
427
LogError("Policy callback modes for SRK policy and "
428
"Owner policy differ");
429
return TSPERR(TSS_E_BAD_PARAMETER);
433
if (ownerMode != TSS_SECRET_MODE_CALLBACK) {
434
/* First, get the Endorsement Public Key for Encrypting */
435
if ((result = Tspi_GetAttribData(hEndorsementPubKey,
436
TSS_TSPATTRIB_KEY_BLOB,
437
TSS_TSPATTRIB_KEYBLOB_BLOB,
438
&endorsementKeySize, &endorsementKey)))
441
/* now stick it in a Key Structure */
443
if ((result = Trspi_UnloadBlob_KEY(&offset, endorsementKey, &dummyKey)))
446
if ((result = obj_policy_get_secret(hOwnerPolicy, TR_SECRET_CTX_NEW,
448
free(dummyKey.pubKey.key);
449
free(dummyKey.algorithmParms.parms);
453
if ((result = obj_policy_get_secret(hSrkPolicy, TR_SECRET_CTX_NEW, &srkSecret))) {
454
free(dummyKey.pubKey.key);
455
free(dummyKey.algorithmParms.parms);
459
/* Encrypt the Owner, SRK Authorizations */
460
if ((result = Trspi_RSA_Encrypt(ownerSecret.authdata, 20, encOwnerAuth,
461
encOwnerAuthLength, dummyKey.pubKey.key,
462
dummyKey.pubKey.keyLength))) {
463
free(dummyKey.pubKey.key);
464
free(dummyKey.algorithmParms.parms);
468
if ((result = Trspi_RSA_Encrypt(srkSecret.authdata, 20, encSRKAuth,
469
encSRKAuthLength, dummyKey.pubKey.key,
470
dummyKey.pubKey.keyLength))) {
471
free(dummyKey.pubKey.key);
472
free(dummyKey.algorithmParms.parms);
476
free(dummyKey.pubKey.key);
477
free(dummyKey.algorithmParms.parms);
479
*encOwnerAuthLength = 256;
480
*encSRKAuthLength = 256;
481
if ((result = obj_policy_do_takeowner(hOwnerPolicy, hTPM,
488
if ((result = Tspi_GetAttribData(hKeySRK,
489
TSS_TSPATTRIB_KEY_BLOB,
490
TSS_TSPATTRIB_KEYBLOB_BLOB,
495
/* Authorizatin Digest Calculation */
496
/* Hash first the following: */
498
Trspi_LoadBlob_UINT32(&offset, TPM_ORD_TakeOwnership, hashblob);
499
Trspi_LoadBlob_UINT16(&offset, TCPA_PID_OWNER, hashblob);
500
Trspi_LoadBlob_UINT32(&offset, *encOwnerAuthLength, hashblob);
501
Trspi_LoadBlob(&offset, *encOwnerAuthLength, hashblob, encOwnerAuth);
502
Trspi_LoadBlob_UINT32(&offset, *encSRKAuthLength, hashblob);
503
Trspi_LoadBlob(&offset, *encSRKAuthLength, hashblob, encSRKAuth);
504
Trspi_LoadBlob(&offset, srkKeyBlobLength, hashblob, srkKeyBlob);
506
Trspi_Hash(TSS_HASH_SHA1, offset, hashblob, digest.digest);
508
/* HMAC for the final digest */
510
if ((result = secret_PerformAuth_OIAP(hTPM, TPM_ORD_TakeOwnership,
511
hOwnerPolicy, &digest, auth)))