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
|
/*
* 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 "trousers/tss.h"
#include "trousers/trousers.h"
#include "trousers_types.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "obj.h"
TSS_RESULT
Tspi_Key_CertifyKey(TSS_HKEY hKey, /* in */
TSS_HKEY hCertifyingKey, /* in */
TSS_VALIDATION * pValidationData) /* in, out */
{
TCPA_RESULT result;
TPM_AUTH certAuth;
TPM_AUTH keyAuth;
TCPA_DIGEST digest;
TCPA_NONCE antiReplay;
UINT32 CertifyInfoSize;
BYTE *CertifyInfo;
UINT32 outDataSize;
BYTE *outData;
TSS_HPOLICY hPolicy;
TSS_HPOLICY hCertPolicy;
TCS_KEY_HANDLE certifyTCSKeyHandle, keyTCSKeyHandle;
TSS_BOOL useAuthCert;
TSS_BOOL useAuthKey;
TPM_AUTH *pCertAuth = &certAuth;
TPM_AUTH *pKeyAuth = &keyAuth;
TSS_HCONTEXT tspContext;
Trspi_HashCtx hashCtx;
if ((result = obj_rsakey_get_tsp_context(hKey, &tspContext)))
return result;
if ((result = obj_rsakey_get_policy(hKey, TSS_POLICY_USAGE,
&hPolicy, &useAuthKey)))
return result;
if ((result = obj_rsakey_get_policy(hCertifyingKey, TSS_POLICY_USAGE,
&hCertPolicy, &useAuthCert)))
return result;
if ((result = obj_rsakey_get_tcs_handle(hCertifyingKey, &certifyTCSKeyHandle)))
return result;
if ((result = obj_rsakey_get_tcs_handle(hKey, &keyTCSKeyHandle)))
return result;
if (pValidationData == NULL) {
LogDebug("Internal Verify");
if ((result = get_local_random(tspContext, FALSE, sizeof(TCPA_NONCE),
(BYTE **)antiReplay.nonce)))
return result;
} else {
LogDebug("External Verify");
if (pValidationData->ulExternalDataLength < sizeof(antiReplay.nonce))
return TSPERR(TSS_E_BAD_PARAMETER);
memcpy(antiReplay.nonce, pValidationData->rgbExternalData,
sizeof(antiReplay.nonce));
}
if (useAuthCert && !useAuthKey)
return TSPERR(TSS_E_BAD_PARAMETER);
/* Setup the auths */
if (useAuthCert || useAuthKey) {
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_CertifyKey);
result |= Trspi_HashUpdate(&hashCtx, sizeof(antiReplay.nonce), antiReplay.nonce);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
return result;
}
if (useAuthKey) {
if ((result = secret_PerformAuth_OIAP(hKey, TPM_ORD_CertifyKey, hPolicy, FALSE,
&digest, &keyAuth)))
return result;
} else
pKeyAuth = NULL;
if (useAuthCert) {
if ((result = secret_PerformAuth_OIAP(hCertifyingKey, TPM_ORD_CertifyKey,
hCertPolicy, FALSE, &digest,
&certAuth)))
return result;
} else
pCertAuth = NULL;
/* XXX free CertifyInfo */
if ((result = TCS_API(tspContext)->CertifyKey(tspContext, certifyTCSKeyHandle,
keyTCSKeyHandle, &antiReplay, pCertAuth,
pKeyAuth, &CertifyInfoSize, &CertifyInfo,
&outDataSize, &outData)))
return result;
/* Validate auth */
if (useAuthCert || useAuthKey) {
result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
result |= Trspi_Hash_UINT32(&hashCtx, result);
result |= Trspi_Hash_UINT32(&hashCtx, TPM_ORD_CertifyKey);
result |= Trspi_HashUpdate(&hashCtx, CertifyInfoSize, CertifyInfo);
result |= Trspi_Hash_UINT32(&hashCtx, outDataSize);
result |= Trspi_HashUpdate(&hashCtx, outDataSize, outData);
if ((result |= Trspi_HashFinal(&hashCtx, digest.digest)))
return result;
if (useAuthKey)
if ((result = obj_policy_validate_auth_oiap(hPolicy, &digest, &keyAuth)))
return result;
if (useAuthCert)
if ((result = obj_policy_validate_auth_oiap(hCertPolicy, &digest,
&certAuth)))
return result;
}
if (pValidationData == NULL) {
if ((result = Trspi_Hash(TSS_HASH_SHA1, CertifyInfoSize, CertifyInfo,
digest.digest)))
return result;
if ((result = __tspi_rsa_verify(hCertifyingKey, TSS_HASH_SHA1, TPM_SHA1_160_HASH_LEN,
digest.digest, outDataSize, outData)))
return TSPERR(TSS_E_VERIFICATION_FAILED);
} else {
pValidationData->ulDataLength = CertifyInfoSize;
pValidationData->rgbData = calloc_tspi(tspContext, CertifyInfoSize);
if (pValidationData->rgbData == NULL) {
LogError("malloc of %u bytes failed.", CertifyInfoSize);
return TSPERR(TSS_E_OUTOFMEMORY);
}
memcpy(pValidationData->rgbData, CertifyInfo, CertifyInfoSize);
pValidationData->ulValidationDataLength = outDataSize;
pValidationData->rgbValidationData = calloc_tspi(tspContext, outDataSize);
if (pValidationData->rgbValidationData == NULL) {
LogError("malloc of %u bytes failed.", outDataSize);
return TSPERR(TSS_E_OUTOFMEMORY);
}
memcpy(pValidationData->rgbValidationData, outData, outDataSize);
}
return TSS_SUCCESS;
}
|