1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2004-2007
8  *
9  */
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <assert.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "capabilities.h"
21 #include "tsplog.h"
22 #include "hosttable.h"
23 #include "tcsd_wrap.h"
24 #include "obj.h"
25 #include "rpc_tcstp_tsp.h"
26 
27 
28 TSS_RESULT
RPC_LoadKeyByBlob_TP(struct host_table_entry * hte,TCS_KEY_HANDLE hUnwrappingKey,UINT32 cWrappedKeyBlobSize,BYTE * rgbWrappedKeyBlob,TPM_AUTH * pAuth,TCS_KEY_HANDLE * phKeyTCSI,TCS_KEY_HANDLE * phKeyHMAC)29 RPC_LoadKeyByBlob_TP(struct host_table_entry *hte,
30 		      TCS_KEY_HANDLE hUnwrappingKey,	/* in */
31 		      UINT32 cWrappedKeyBlobSize,	/* in */
32 		      BYTE * rgbWrappedKeyBlob,	/* in */
33 		      TPM_AUTH * pAuth,	/* in, out */
34 		      TCS_KEY_HANDLE * phKeyTCSI,	/* out */
35 		      TCS_KEY_HANDLE * phKeyHMAC)	/* out */
36 {
37 	TSS_RESULT result;
38 	int i;
39 
40 	initData(&hte->comm, 5);
41 	hte->comm.hdr.u.ordinal = TCSD_ORD_LOADKEYBYBLOB;
42 	LogDebugFn("IN: TCS Context: 0x%x", hte->tcsContext);
43 
44 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
45 		return TSPERR(TSS_E_INTERNAL_ERROR);
46 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hUnwrappingKey, 0, &hte->comm))
47 		return TSPERR(TSS_E_INTERNAL_ERROR);
48 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &cWrappedKeyBlobSize, 0, &hte->comm))
49 		return TSPERR(TSS_E_INTERNAL_ERROR);
50 	if (setData(TCSD_PACKET_TYPE_PBYTE, 3, rgbWrappedKeyBlob, cWrappedKeyBlobSize, &hte->comm))
51 			return TSPERR(TSS_E_INTERNAL_ERROR);
52 
53 	if (pAuth != NULL) {
54 		if (setData(TCSD_PACKET_TYPE_AUTH, 4, pAuth, 0, &hte->comm))
55 			return TSPERR(TSS_E_INTERNAL_ERROR);
56 	}
57 
58 	result = sendTCSDPacket(hte);
59 
60 	if (result == TSS_SUCCESS)
61 		result = hte->comm.hdr.u.result;
62 
63 	if (result == TSS_SUCCESS) {
64 		i = 0;
65 		if (pAuth != NULL) {
66 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &hte->comm))
67 				result = TSPERR(TSS_E_INTERNAL_ERROR);
68 		}
69 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, phKeyTCSI, 0, &hte->comm))
70 			result = TSPERR(TSS_E_INTERNAL_ERROR);
71 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, phKeyHMAC, 0, &hte->comm))
72 			result = TSPERR(TSS_E_INTERNAL_ERROR);
73 
74 		LogDebugFn("OUT: TCS key handle: 0x%x, TPM key slot: 0x%x", *phKeyTCSI,
75 			   *phKeyHMAC);
76 	}
77 
78 	return result;
79 }
80 
81 TSS_RESULT
RPC_EvictKey_TP(struct host_table_entry * hte,TCS_KEY_HANDLE hKey)82 RPC_EvictKey_TP(struct host_table_entry *hte,
83 		 TCS_KEY_HANDLE hKey)	/* in */
84 {
85 	TSS_RESULT result;
86 
87 	initData(&hte->comm, 2);
88 	hte->comm.hdr.u.ordinal = TCSD_ORD_EVICTKEY;
89 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
90 
91 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
92 		return TSPERR(TSS_E_INTERNAL_ERROR);
93 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &hte->comm))
94 		return TSPERR(TSS_E_INTERNAL_ERROR);
95 
96 	result = sendTCSDPacket(hte);
97 
98 	if (result == TSS_SUCCESS)
99 		result = hte->comm.hdr.u.result;
100 
101 	return result;
102 }
103 
104 TSS_RESULT
RPC_CreateWrapKey_TP(struct host_table_entry * hte,TCS_KEY_HANDLE hWrappingKey,TCPA_ENCAUTH * KeyUsageAuth,TCPA_ENCAUTH * KeyMigrationAuth,UINT32 keyInfoSize,BYTE * keyInfo,UINT32 * keyDataSize,BYTE ** keyData,TPM_AUTH * pAuth)105 RPC_CreateWrapKey_TP(struct host_table_entry *hte,
106 		      TCS_KEY_HANDLE hWrappingKey,	/* in */
107 		      TCPA_ENCAUTH *KeyUsageAuth,	/* in */
108 		      TCPA_ENCAUTH *KeyMigrationAuth,	/* in */
109 		      UINT32 keyInfoSize,	/* in */
110 		      BYTE * keyInfo,	/* in */
111 		      UINT32 * keyDataSize,	/* out */
112 		      BYTE ** keyData,	/* out */
113 		      TPM_AUTH * pAuth)	/* in, out */
114 {
115 	TSS_RESULT result;
116 
117 	initData(&hte->comm, 7);
118 	hte->comm.hdr.u.ordinal = TCSD_ORD_CREATEWRAPKEY;
119 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
120 
121 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
122 		return TSPERR(TSS_E_INTERNAL_ERROR);
123 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hWrappingKey, 0, &hte->comm))
124 		return TSPERR(TSS_E_INTERNAL_ERROR);
125 	if (setData(TCSD_PACKET_TYPE_ENCAUTH, 2, KeyUsageAuth, 0, &hte->comm))
126 		return TSPERR(TSS_E_INTERNAL_ERROR);
127 	if (setData(TCSD_PACKET_TYPE_ENCAUTH, 3, KeyMigrationAuth, 0, &hte->comm))
128 		return TSPERR(TSS_E_INTERNAL_ERROR);
129 	if (setData(TCSD_PACKET_TYPE_UINT32, 4, &keyInfoSize, 0, &hte->comm))
130 		return TSPERR(TSS_E_INTERNAL_ERROR);
131 	if (setData(TCSD_PACKET_TYPE_PBYTE, 5, keyInfo, keyInfoSize, &hte->comm))
132 		return TSPERR(TSS_E_INTERNAL_ERROR);
133 	if (pAuth) {
134 		if (setData(TCSD_PACKET_TYPE_AUTH, 6, pAuth, 0, &hte->comm))
135 			return TSPERR(TSS_E_INTERNAL_ERROR);
136 	}
137 
138 	result = sendTCSDPacket(hte);
139 
140 	if (result == TSS_SUCCESS)
141 		result = hte->comm.hdr.u.result;
142 
143 	if (result == TSS_SUCCESS) {
144 		if (getData(TCSD_PACKET_TYPE_UINT32, 0, keyDataSize, 0, &hte->comm)) {
145 			result = TSPERR(TSS_E_INTERNAL_ERROR);
146 			goto done;
147 		}
148 		*keyData = (BYTE *) malloc(*keyDataSize);
149 		if (*keyData == NULL) {
150 			LogError("malloc of %u bytes failed.", *keyDataSize);
151 			result = TSPERR(TSS_E_OUTOFMEMORY);
152 			goto done;
153 		}
154 		if (getData(TCSD_PACKET_TYPE_PBYTE, 1, *keyData, *keyDataSize, &hte->comm)) {
155 			free(*keyData);
156 			result = TSPERR(TSS_E_INTERNAL_ERROR);
157 			goto done;
158 		}
159 		if (pAuth) {
160 			if (getData(TCSD_PACKET_TYPE_AUTH, 2, pAuth, 0, &hte->comm)) {
161 				free(*keyData);
162 				result = TSPERR(TSS_E_INTERNAL_ERROR);
163 				goto done;
164 			}
165 		}
166 	}
167 
168 done:
169 	return result;
170 }
171 
172 TSS_RESULT
RPC_GetPubKey_TP(struct host_table_entry * hte,TCS_KEY_HANDLE hKey,TPM_AUTH * pAuth,UINT32 * pcPubKeySize,BYTE ** prgbPubKey)173 RPC_GetPubKey_TP(struct host_table_entry *hte,
174 		  TCS_KEY_HANDLE hKey,	/* in */
175 		  TPM_AUTH * pAuth,	/* in, out */
176 		  UINT32 * pcPubKeySize,	/* out */
177 		  BYTE ** prgbPubKey)	/* out */
178 {
179 	TSS_RESULT result;
180 	int i;
181 
182 	initData(&hte->comm, 3);
183 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETPUBKEY;
184 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
185 
186 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
187 		return TSPERR(TSS_E_INTERNAL_ERROR);
188 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &hte->comm))
189 		return TSPERR(TSS_E_INTERNAL_ERROR);
190 	if (pAuth != NULL) {
191 		if (setData(TCSD_PACKET_TYPE_AUTH, 2, pAuth, 0, &hte->comm))
192 			return TSPERR(TSS_E_INTERNAL_ERROR);
193 	}
194 
195 	result = sendTCSDPacket(hte);
196 
197 	if (result == TSS_SUCCESS)
198 		result = hte->comm.hdr.u.result;
199 
200 	i = 0;
201 	if (result == TSS_SUCCESS) {
202 		if (pAuth != NULL) {
203 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &hte->comm)) {
204 				result = TSPERR(TSS_E_INTERNAL_ERROR);
205 				goto done;
206 			}
207 		}
208 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, pcPubKeySize, 0, &hte->comm)) {
209 			result = TSPERR(TSS_E_INTERNAL_ERROR);
210 			goto done;
211 		}
212 
213 		*prgbPubKey = (BYTE *) malloc(*pcPubKeySize);
214 		if (*prgbPubKey == NULL) {
215 			LogError("malloc of %u bytes failed.", *pcPubKeySize);
216 			result = TSPERR(TSS_E_OUTOFMEMORY);
217 			goto done;
218 		}
219 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *prgbPubKey, *pcPubKeySize, &hte->comm)) {
220 			free(*prgbPubKey);
221 			result = TSPERR(TSS_E_INTERNAL_ERROR);
222 		}
223 	}
224 
225 done:
226 	return result;
227 }
228 
229 TSS_RESULT
RPC_TerminateHandle_TP(struct host_table_entry * hte,TCS_AUTHHANDLE handle)230 RPC_TerminateHandle_TP(struct host_table_entry *hte,
231 			TCS_AUTHHANDLE handle)	/* in */
232 {
233 	TSS_RESULT result;
234 
235 	initData(&hte->comm, 2);
236 	hte->comm.hdr.u.ordinal = TCSD_ORD_TERMINATEHANDLE;
237 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
238 
239 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
240 		return TSPERR(TSS_E_INTERNAL_ERROR);
241 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &handle, 0, &hte->comm))
242 		return TSPERR(TSS_E_INTERNAL_ERROR);
243 
244 	result = sendTCSDPacket(hte);
245 
246 	if (result == TSS_SUCCESS)
247 		result = hte->comm.hdr.u.result;
248 
249 	return result;
250 }
251 
252 TSS_RESULT
RPC_OwnerReadInternalPub_TP(struct host_table_entry * hte,TCS_KEY_HANDLE hKey,TPM_AUTH * pOwnerAuth,UINT32 * punPubKeySize,BYTE ** ppbPubKeyData)253 RPC_OwnerReadInternalPub_TP(struct host_table_entry *hte,
254 			     TCS_KEY_HANDLE hKey,	/* in */
255 			     TPM_AUTH * pOwnerAuth,	/* in, out */
256 			     UINT32 * punPubKeySize,	/* out */
257 			     BYTE ** ppbPubKeyData)	/* out */
258 {
259 	TSS_RESULT result;
260 
261 	initData(&hte->comm, 3);
262 	hte->comm.hdr.u.ordinal = TCSD_ORD_OWNERREADINTERNALPUB;
263 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
264 
265 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
266 		return TSPERR(TSS_E_INTERNAL_ERROR);
267 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &hte->comm))
268 		return TSPERR(TSS_E_INTERNAL_ERROR);
269 	if (pOwnerAuth != NULL) {
270 		if (setData(TCSD_PACKET_TYPE_AUTH, 2, pOwnerAuth, 0, &hte->comm))
271 			return TSPERR(TSS_E_INTERNAL_ERROR);
272 	}
273 
274 	result = sendTCSDPacket(hte);
275 
276 	if (result == TSS_SUCCESS)
277 		result = hte->comm.hdr.u.result;
278 
279 	if (result == TSS_SUCCESS) {
280 		if (getData(TCSD_PACKET_TYPE_AUTH, 0, pOwnerAuth, 0, &hte->comm))
281 			result = TSPERR(TSS_E_INTERNAL_ERROR);
282 		if (getData(TCSD_PACKET_TYPE_UINT32, 1, punPubKeySize, 0, &hte->comm))
283 			result = TSPERR(TSS_E_INTERNAL_ERROR);
284 
285 		*ppbPubKeyData = (BYTE *) malloc(*punPubKeySize);
286 		if (*ppbPubKeyData == NULL) {
287 			LogError("malloc of %u bytes failed.", *punPubKeySize);
288 			return TSPERR(TSS_E_OUTOFMEMORY);
289 		}
290 		if (getData(TCSD_PACKET_TYPE_PBYTE, 2, *ppbPubKeyData, *punPubKeySize,
291 			    &hte->comm)) {
292 			free(*ppbPubKeyData);
293 			result = TSPERR(TSS_E_INTERNAL_ERROR);
294 		}
295 	}
296 
297 	return result;
298 }
299 
300 /* TSS 1.2-only interfaces */
301 #ifdef TSS_BUILD_TSS12
302 TSS_RESULT
RPC_KeyControlOwner_TP(struct host_table_entry * hte,TCS_KEY_HANDLE hKey,UINT32 ulPublicInfoLength,BYTE * rgbPublicInfo,UINT32 attribName,TSS_BOOL attribValue,TPM_AUTH * pOwnerAuth,TSS_UUID * pUuidData)303 RPC_KeyControlOwner_TP(struct host_table_entry *hte,		// in
304 			TCS_KEY_HANDLE     hKey,		// in
305 			UINT32             ulPublicInfoLength,	// in
306 			BYTE*              rgbPublicInfo,	// in
307 			UINT32             attribName,		// in
308 			TSS_BOOL           attribValue,		// in
309 			TPM_AUTH*          pOwnerAuth,		// in, out
310 			TSS_UUID*          pUuidData)		// out
311 
312 {
313 	TSS_RESULT result;
314 
315 	initData(&hte->comm, 7);
316 	hte->comm.hdr.u.ordinal = TCSD_ORD_KEYCONTROLOWNER;
317 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
318 
319 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
320 		return TSPERR(TSS_E_INTERNAL_ERROR);
321 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &hte->comm))
322 		return TSPERR(TSS_E_INTERNAL_ERROR);
323 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &ulPublicInfoLength, 0, &hte->comm))
324 		return TSPERR(TSS_E_INTERNAL_ERROR);
325 	if (setData(TCSD_PACKET_TYPE_PBYTE, 3, rgbPublicInfo, ulPublicInfoLength, &hte->comm))
326 		return TSPERR(TSS_E_INTERNAL_ERROR);
327 	if (setData(TCSD_PACKET_TYPE_UINT32, 4, &attribName, 0, &hte->comm))
328 		return TSPERR(TSS_E_INTERNAL_ERROR);
329 	if (setData(TCSD_PACKET_TYPE_BOOL, 5, &attribValue, 0, &hte->comm))
330 		return TSPERR(TSS_E_INTERNAL_ERROR);
331 	if (pOwnerAuth != NULL) {
332 		if (setData(TCSD_PACKET_TYPE_AUTH, 6, pOwnerAuth, 0, &hte->comm))
333 			return TSPERR(TSS_E_INTERNAL_ERROR);
334 	}
335 
336 	result = sendTCSDPacket(hte);
337 
338 	if (result == TSS_SUCCESS)
339 		result = hte->comm.hdr.u.result;
340 
341 	if (result == TSS_SUCCESS) {
342 		if (getData(TCSD_PACKET_TYPE_AUTH, 0, pOwnerAuth, 0, &hte->comm))
343 			result = TSPERR(TSS_E_INTERNAL_ERROR);
344 		if (getData(TCSD_PACKET_TYPE_UUID, 1, pUuidData, 0, &hte->comm))
345 			result = TSPERR(TSS_E_INTERNAL_ERROR);
346 	}
347 
348 	return result;
349 }
350 #endif
351