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-2006
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
28TSS_RESULT
29RPC_GetRegisteredKeyByPublicInfo_TP(struct host_table_entry *hte,
30				     TCPA_ALGORITHM_ID algID,	/* in */
31				     UINT32 ulPublicInfoLength,	/* in */
32				     BYTE * rgbPublicInfo,	/* in */
33				     UINT32 * keySize,		/* out */
34				     BYTE ** keyBlob)		/* out */
35{
36	TSS_RESULT result;
37
38	initData(&hte->comm, 4);
39	hte->comm.hdr.u.ordinal = TCSD_ORD_GETREGISTEREDKEYBYPUBLICINFO;
40	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
41
42	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
43		return TSPERR(TSS_E_INTERNAL_ERROR);
44	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &algID, 0, &hte->comm))
45		return TSPERR(TSS_E_INTERNAL_ERROR);
46	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &ulPublicInfoLength, 0, &hte->comm))
47		return TSPERR(TSS_E_INTERNAL_ERROR);
48	if (setData(TCSD_PACKET_TYPE_PBYTE, 3, rgbPublicInfo, ulPublicInfoLength, &hte->comm))
49		return TSPERR(TSS_E_INTERNAL_ERROR);
50
51	result = sendTCSDPacket(hte);
52
53	if (result == TSS_SUCCESS)
54		result = hte->comm.hdr.u.result;
55
56	if (result == TSS_SUCCESS) {
57		if (getData(TCSD_PACKET_TYPE_UINT32, 0, keySize, 0, &hte->comm)) {
58			result = TSPERR(TSS_E_INTERNAL_ERROR);
59			goto done;
60		}
61		*keyBlob = (BYTE *) malloc(*keySize);
62		if (*keyBlob == NULL) {
63			LogError("malloc of %u bytes failed.", *keySize);
64			result = TSPERR(TSS_E_OUTOFMEMORY);
65			goto done;
66		}
67		if (getData(TCSD_PACKET_TYPE_PBYTE, 1, *keyBlob, *keySize, &hte->comm)) {
68			free(*keyBlob);
69			result = TSPERR(TSS_E_INTERNAL_ERROR);
70			goto done;
71		}
72	}
73
74done:
75	return result;
76}
77
78TSS_RESULT
79RPC_RegisterKey_TP(struct host_table_entry *hte,
80			       TSS_UUID WrappingKeyUUID,	/* in */
81			       TSS_UUID KeyUUID,	/* in */
82			       UINT32 cKeySize,	/* in */
83			       BYTE * rgbKey,	/* in */
84			       UINT32 cVendorData,	/* in */
85			       BYTE * gbVendorData	/* in */
86    ) {
87	TSS_RESULT result;
88
89	initData(&hte->comm, 7);
90	hte->comm.hdr.u.ordinal = TCSD_ORD_REGISTERKEY;
91	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
92
93	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
94		return TSPERR(TSS_E_INTERNAL_ERROR);
95	if (setData(TCSD_PACKET_TYPE_UUID, 1, &WrappingKeyUUID, 0, &hte->comm))
96		return TSPERR(TSS_E_INTERNAL_ERROR);
97	if (setData(TCSD_PACKET_TYPE_UUID, 2, &KeyUUID, 0, &hte->comm))
98		return TSPERR(TSS_E_INTERNAL_ERROR);
99	if (setData(TCSD_PACKET_TYPE_UINT32, 3, &cKeySize, 0, &hte->comm))
100		return TSPERR(TSS_E_INTERNAL_ERROR);
101	if (setData(TCSD_PACKET_TYPE_PBYTE, 4, rgbKey, cKeySize, &hte->comm))
102		return TSPERR(TSS_E_INTERNAL_ERROR);
103	if (setData(TCSD_PACKET_TYPE_UINT32, 5, &cVendorData, 0, &hte->comm))
104		return TSPERR(TSS_E_INTERNAL_ERROR);
105	if (setData(TCSD_PACKET_TYPE_PBYTE, 6, gbVendorData, cVendorData, &hte->comm))
106		return TSPERR(TSS_E_INTERNAL_ERROR);
107
108	result = sendTCSDPacket(hte);
109
110	if (result == TSS_SUCCESS)
111		result = hte->comm.hdr.u.result;
112
113	return result;
114}
115
116TSS_RESULT
117RPC_UnregisterKey_TP(struct host_table_entry *hte,
118				  TSS_UUID KeyUUID	/* in */
119    ) {
120	TSS_RESULT result;
121
122	initData(&hte->comm, 2);
123	hte->comm.hdr.u.ordinal = TCSD_ORD_UNREGISTERKEY;
124	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
125
126	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
127		return TSPERR(TSS_E_INTERNAL_ERROR);
128	if (setData(TCSD_PACKET_TYPE_UUID, 1, &KeyUUID, 0, &hte->comm))
129		return TSPERR(TSS_E_INTERNAL_ERROR);
130
131	result = sendTCSDPacket(hte);
132
133	if (result == TSS_SUCCESS)
134		result = hte->comm.hdr.u.result;
135
136	return result;
137}
138
139TSS_RESULT
140RPC_EnumRegisteredKeys_TP(struct host_table_entry *hte,
141				      TSS_UUID * pKeyUUID,	/* in */
142				      UINT32 * pcKeyHierarchySize,	/* out */
143				      TSS_KM_KEYINFO ** ppKeyHierarchy	/* out */
144    ) {
145	TSS_RESULT result;
146	int i, j;
147
148	initData(&hte->comm, 2);
149	hte->comm.hdr.u.ordinal = TCSD_ORD_ENUMREGISTEREDKEYS;
150	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
151
152	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
153		return TSPERR(TSS_E_INTERNAL_ERROR);
154
155	if (pKeyUUID != NULL) {
156		if (setData(TCSD_PACKET_TYPE_UUID, 1, pKeyUUID, 0, &hte->comm))
157			return TSPERR(TSS_E_INTERNAL_ERROR);
158	}
159
160	result = sendTCSDPacket(hte);
161
162	if (result == TSS_SUCCESS)
163		result = hte->comm.hdr.u.result;
164
165	if (result == TSS_SUCCESS) {
166		i = 0;
167		if (getData(TCSD_PACKET_TYPE_UINT32, i++, pcKeyHierarchySize, 0, &hte->comm)) {
168			result = TSPERR(TSS_E_INTERNAL_ERROR);
169			goto done;
170		}
171
172		if (*pcKeyHierarchySize > 0) {
173			*ppKeyHierarchy = malloc((*pcKeyHierarchySize) * sizeof(TSS_KM_KEYINFO));
174			if (*ppKeyHierarchy == NULL) {
175				LogError("malloc of %zu bytes failed.", (*pcKeyHierarchySize) *
176					 sizeof(TSS_KM_KEYINFO));
177				result = TSPERR(TSS_E_OUTOFMEMORY);
178				goto done;
179			}
180			for (j = 0; (UINT32)j < *pcKeyHierarchySize; j++) {
181				if (getData(TCSD_PACKET_TYPE_KM_KEYINFO, i++,
182					    &((*ppKeyHierarchy)[j]), 0, &hte->comm)) {
183					free(*ppKeyHierarchy);
184					result = TSPERR(TSS_E_INTERNAL_ERROR);
185					goto done;
186				}
187			}
188		} else {
189			*ppKeyHierarchy = NULL;
190		}
191	}
192
193done:
194	return result;
195}
196
197TSS_RESULT
198RPC_EnumRegisteredKeys2_TP(struct host_table_entry *hte,
199				      TSS_UUID * pKeyUUID,	/* in */
200				      UINT32 * pcKeyHierarchySize,	/* out */
201				      TSS_KM_KEYINFO2 ** ppKeyHierarchy	/* out */
202					  )
203{
204	TSS_RESULT result;
205	int i, j;
206
207	initData(&hte->comm, 2);
208	hte->comm.hdr.u.ordinal = TCSD_ORD_ENUMREGISTEREDKEYS2;
209	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
210
211	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
212		return TSPERR(TSS_E_INTERNAL_ERROR);
213
214	if (pKeyUUID != NULL) {
215		if (setData(TCSD_PACKET_TYPE_UUID, 1, pKeyUUID, 0, &hte->comm))
216			return TSPERR(TSS_E_INTERNAL_ERROR);
217	}
218
219	result = sendTCSDPacket(hte);
220
221	if (result == TSS_SUCCESS)
222		result = hte->comm.hdr.u.result;
223
224	if (result == TSS_SUCCESS) {
225		i = 0;
226		if (getData(TCSD_PACKET_TYPE_UINT32, i++, pcKeyHierarchySize, 0, &hte->comm)) {
227			result = TSPERR(TSS_E_INTERNAL_ERROR);
228			goto done;
229		}
230
231		if (*pcKeyHierarchySize > 0) {
232			*ppKeyHierarchy = malloc((*pcKeyHierarchySize) * sizeof(TSS_KM_KEYINFO2));
233			if (*ppKeyHierarchy == NULL) {
234				LogError("malloc of %zu bytes failed.", (*pcKeyHierarchySize) *
235					 sizeof(TSS_KM_KEYINFO2));
236				result = TSPERR(TSS_E_OUTOFMEMORY);
237				goto done;
238			}
239			for (j = 0; (UINT32)j < *pcKeyHierarchySize; j++) {
240				if (getData(TCSD_PACKET_TYPE_KM_KEYINFO2, i++,
241					    &((*ppKeyHierarchy)[j]), 0, &hte->comm)) {
242					free(*ppKeyHierarchy);
243					result = TSPERR(TSS_E_INTERNAL_ERROR);
244					goto done;
245				}
246			}
247		} else {
248			*ppKeyHierarchy = NULL;
249		}
250	}
251
252done:
253	return result;
254}
255
256TSS_RESULT
257RPC_GetRegisteredKey_TP(struct host_table_entry *hte,
258				    TSS_UUID KeyUUID,	/* in */
259				    TSS_KM_KEYINFO ** ppKeyInfo	/* out */
260    ) {
261	return TSPERR(TSS_E_NOTIMPL);
262}
263
264TSS_RESULT
265RPC_GetRegisteredKeyBlob_TP(struct host_table_entry *hte,
266					TSS_UUID KeyUUID,	/* in */
267					UINT32 * pcKeySize,	/* out */
268					BYTE ** prgbKey	/* out */
269    ) {
270	TSS_RESULT result;
271
272	initData(&hte->comm, 2);
273	hte->comm.hdr.u.ordinal = TCSD_ORD_GETREGISTEREDKEYBLOB;
274	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
275
276	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
277		return TSPERR(TSS_E_INTERNAL_ERROR);
278	if (setData(TCSD_PACKET_TYPE_UUID, 1, &KeyUUID, 0, &hte->comm))
279		return TSPERR(TSS_E_INTERNAL_ERROR);
280
281	result = sendTCSDPacket(hte);
282
283	if (result == TSS_SUCCESS)
284		result = hte->comm.hdr.u.result;
285
286	if (result == TSS_SUCCESS) {
287		if (getData(TCSD_PACKET_TYPE_UINT32, 0, pcKeySize, 0, &hte->comm)) {
288			result = TSPERR(TSS_E_INTERNAL_ERROR);
289			goto done;
290		}
291		*prgbKey = malloc(*pcKeySize);
292		if (*prgbKey == NULL) {
293			LogError("malloc of %u bytes failed.", *pcKeySize);
294			result = TSPERR(TSS_E_OUTOFMEMORY);
295			goto done;
296		}
297		if (getData(TCSD_PACKET_TYPE_PBYTE, 1, *prgbKey, *pcKeySize, &hte->comm)) {
298			free(*prgbKey);
299			result = TSPERR(TSS_E_INTERNAL_ERROR);
300		}
301	}
302
303done:
304	return result;
305
306}
307
308TSS_RESULT
309RPC_LoadKeyByUUID_TP(struct host_table_entry *hte,
310				  TSS_UUID KeyUUID,	/* in */
311				  TCS_LOADKEY_INFO * pLoadKeyInfo,	/* in, out */
312				  TCS_KEY_HANDLE * phKeyTCSI	/* out */
313    ) {
314	TSS_RESULT result;
315
316	initData(&hte->comm, 3);
317	hte->comm.hdr.u.ordinal = TCSD_ORD_LOADKEYBYUUID;
318	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
319
320	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
321		return TSPERR(TSS_E_INTERNAL_ERROR);
322	if (setData(TCSD_PACKET_TYPE_UUID, 1, &KeyUUID, 0, &hte->comm))
323		return TSPERR(TSS_E_INTERNAL_ERROR);
324
325	if (pLoadKeyInfo != NULL) {
326		if (setData(TCSD_PACKET_TYPE_LOADKEY_INFO, 2, pLoadKeyInfo, 0, &hte->comm))
327			return TSPERR(TSS_E_INTERNAL_ERROR);
328	}
329
330	result = sendTCSDPacket(hte);
331
332	if (result == TSS_SUCCESS)
333		result = hte->comm.hdr.u.result;
334
335	if (result == TSS_SUCCESS) {
336		if (getData(TCSD_PACKET_TYPE_UINT32, 0, phKeyTCSI, 0, &hte->comm))
337			result = TSPERR(TSS_E_INTERNAL_ERROR);
338
339		LogDebugFn("TCS key handle: 0x%x", *phKeyTCSI);
340	} else if (pLoadKeyInfo && (result == (TCS_E_KM_LOADFAILED | TSS_LAYER_TCS))) {
341		if (getData(TCSD_PACKET_TYPE_LOADKEY_INFO, 0, pLoadKeyInfo, 0, &hte->comm))
342			result = TSPERR(TSS_E_INTERNAL_ERROR);
343	}
344
345	return result;
346}
347
348void
349LoadBlob_LOADKEY_INFO(UINT64 *offset, BYTE *blob, TCS_LOADKEY_INFO *info)
350{
351	Trspi_LoadBlob_UUID(offset, blob, info->keyUUID);
352	Trspi_LoadBlob_UUID(offset, blob, info->parentKeyUUID);
353	Trspi_LoadBlob(offset, TCPA_DIGEST_SIZE, blob, info->paramDigest.digest);
354	Trspi_LoadBlob_UINT32(offset, info->authData.AuthHandle, blob);
355	Trspi_LoadBlob(offset, TCPA_NONCE_SIZE, blob, (BYTE *)&info->authData.NonceOdd.nonce);
356	Trspi_LoadBlob(offset, TCPA_NONCE_SIZE, blob, (BYTE *)&info->authData.NonceEven.nonce);
357	Trspi_LoadBlob_BOOL(offset, info->authData.fContinueAuthSession, blob);
358	Trspi_LoadBlob(offset, TCPA_DIGEST_SIZE, blob, (BYTE *)&info->authData.HMAC);
359}
360
361void
362UnloadBlob_LOADKEY_INFO(UINT64 *offset, BYTE *blob, TCS_LOADKEY_INFO *info)
363{
364	Trspi_UnloadBlob_UUID(offset, blob, &info->keyUUID);
365	Trspi_UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
366	Trspi_UnloadBlob(offset, TCPA_DIGEST_SIZE, blob, (BYTE *)&info->paramDigest.digest);
367	Trspi_UnloadBlob_UINT32(offset, &info->authData.AuthHandle, blob);
368	Trspi_UnloadBlob(offset, TCPA_NONCE_SIZE, blob, (BYTE *)&info->authData.NonceOdd.nonce);
369	Trspi_UnloadBlob(offset, TCPA_NONCE_SIZE, blob, (BYTE *)&info->authData.NonceEven.nonce);
370	Trspi_UnloadBlob_BOOL(offset, &info->authData.fContinueAuthSession, blob);
371	Trspi_UnloadBlob(offset, TCPA_DIGEST_SIZE, blob, (BYTE *)&info->authData.HMAC);
372}
373
374