1/*
2 * The Initial Developer of the Original Code is Intel Corporation.
3 * Portions created by Intel Corporation are Copyright (C) 2007 Intel Corporation.
4 * All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the Common Public License as published by
8 * IBM Corporation; either version 1 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * Common Public License for more details.
15 *
16 * You should have received a copy of the Common Public License
17 * along with this program; if not, a copy can be viewed at
18 * http://www.opensource.org/licenses/cpl1.0.php.
19 *
20 * trousers - An open source TCG Software Stack
21 *
22 * Author: james.xu@intel.com Rossey.liu@intel.com
23 *
24 */
25
26#include <stdlib.h>
27#include <stdio.h>
28#include <syslog.h>
29#include <string.h>
30#include <netdb.h>
31
32#include "trousers/tss.h"
33#include "trousers_types.h"
34#include "tcs_tsp.h"
35#include "tcs_utils.h"
36#include "tcs_int_literals.h"
37#include "capabilities.h"
38#include "tcslog.h"
39#include "tcsd_wrap.h"
40#include "tcsd.h"
41#include "tcs_utils.h"
42#include "rpc_tcstp_tcs.h"
43
44
45TSS_RESULT
46tcs_wrap_NV_DefineOrReleaseSpace(struct tcsd_thread_data *data)
47{
48	TCS_CONTEXT_HANDLE hContext;
49	UINT32 cPubInfoSize;
50	BYTE *pubInfo = NULL;
51	TSS_RESULT result;
52	TPM_ENCAUTH encAuth;
53	TPM_AUTH Auth, *pAuth;
54
55	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
56		return TCSERR(TSS_E_INTERNAL_ERROR);
57
58	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &cPubInfoSize, 0, &data->comm))
59		return TCSERR(TSS_E_INTERNAL_ERROR);
60
61	pubInfo = calloc(1, cPubInfoSize);
62	if (pubInfo == NULL) {
63		LogError("malloc of %u bytes failed.", cPubInfoSize);
64		return TCSERR(TSS_E_OUTOFMEMORY);
65	}
66
67	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, pubInfo, cPubInfoSize, &data->comm)) {
68		free(pubInfo);
69		return TCSERR(TSS_E_INTERNAL_ERROR);
70	}
71
72	if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &encAuth, 0, &data->comm)) {
73		free(pubInfo);
74		return TCSERR(TSS_E_INTERNAL_ERROR);
75	}
76
77	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &Auth, 0, &data->comm))
78		pAuth = NULL;
79	else
80		pAuth = &Auth;
81
82	MUTEX_LOCK(tcsp_lock);
83
84	result = TCSP_NV_DefineOrReleaseSpace_Internal(hContext,
85						       cPubInfoSize, pubInfo, encAuth, pAuth);
86
87	MUTEX_UNLOCK(tcsp_lock);
88
89	free(pubInfo);
90
91	if (result == TSS_SUCCESS) {
92		initData(&data->comm, 1);
93		if ( pAuth) {
94			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
95				return TCSERR(TSS_E_INTERNAL_ERROR);
96			}
97		}
98	} else
99		initData(&data->comm, 0);
100
101	data->comm.hdr.u.result = result;
102	return TSS_SUCCESS;
103}
104
105TSS_RESULT
106tcs_wrap_NV_WriteValue(struct tcsd_thread_data *data)
107{
108	TCS_CONTEXT_HANDLE hContext;
109	TSS_NV_INDEX hNVStore;
110	UINT32 offset,ulDataLength;
111	BYTE *rgbDataToWrite = NULL;
112	TSS_RESULT result;
113	TPM_AUTH Auth, *pAuth;
114
115	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
116		return TCSERR(TSS_E_INTERNAL_ERROR);
117
118	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
119		return TCSERR(TSS_E_INTERNAL_ERROR);
120
121	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
122		return TCSERR(TSS_E_INTERNAL_ERROR);
123
124	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
125		return TCSERR(TSS_E_INTERNAL_ERROR);
126
127	rgbDataToWrite = calloc(1, ulDataLength);
128	if (rgbDataToWrite == NULL) {
129		LogError("malloc of %u bytes failed.", ulDataLength);
130		return TCSERR(TSS_E_OUTOFMEMORY);
131	}
132
133	if (getData(TCSD_PACKET_TYPE_PBYTE, 4, rgbDataToWrite, ulDataLength, &data->comm)) {
134		free(rgbDataToWrite);
135		return TCSERR(TSS_E_INTERNAL_ERROR);
136	}
137
138	if (getData(TCSD_PACKET_TYPE_AUTH, 5, &Auth, 0, &data->comm))
139		pAuth = NULL;
140	else
141		pAuth = &Auth;
142
143	MUTEX_LOCK(tcsp_lock);
144
145	result = TCSP_NV_WriteValue_Internal(hContext, hNVStore,
146					     offset, ulDataLength, rgbDataToWrite, pAuth);
147
148	MUTEX_UNLOCK(tcsp_lock);
149
150	free(rgbDataToWrite);
151
152	if (result == TSS_SUCCESS) {
153		initData(&data->comm, 1);
154		if (pAuth) {
155			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
156				return TCSERR(TSS_E_INTERNAL_ERROR);
157			}
158		}
159	} else
160		initData(&data->comm, 0);
161
162	data->comm.hdr.u.result = result;
163	return TSS_SUCCESS;
164}
165
166TSS_RESULT
167tcs_wrap_NV_WriteValueAuth(struct tcsd_thread_data *data)
168{
169	TCS_CONTEXT_HANDLE hContext;
170	TSS_NV_INDEX hNVStore;
171	UINT32 offset,ulDataLength;
172	BYTE *rgbDataToWrite = NULL;
173	TSS_RESULT result;
174	TPM_AUTH Auth, *pAuth;
175
176	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
177		return TCSERR(TSS_E_INTERNAL_ERROR);
178
179	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
180		return TCSERR(TSS_E_INTERNAL_ERROR);
181
182	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
183		return TCSERR(TSS_E_INTERNAL_ERROR);
184
185	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
186		return TCSERR(TSS_E_INTERNAL_ERROR);
187
188	rgbDataToWrite = calloc(1, ulDataLength);
189	if (rgbDataToWrite == NULL) {
190		LogError("malloc of %u bytes failed.", ulDataLength);
191		return TCSERR(TSS_E_OUTOFMEMORY);
192	}
193
194	if (getData(TCSD_PACKET_TYPE_PBYTE, 4, rgbDataToWrite, ulDataLength, &data->comm)) {
195		free(rgbDataToWrite);
196		return TCSERR(TSS_E_INTERNAL_ERROR);
197	}
198	if (getData(TCSD_PACKET_TYPE_AUTH, 5, &Auth, 0, &data->comm))
199		pAuth = NULL;
200	else
201		pAuth = &Auth;
202
203	MUTEX_LOCK(tcsp_lock);
204
205	result = TCSP_NV_WriteValueAuth_Internal(hContext, hNVStore,
206						 offset, ulDataLength, rgbDataToWrite, pAuth);
207
208	MUTEX_UNLOCK(tcsp_lock);
209
210	free(rgbDataToWrite);
211
212	if (result == TSS_SUCCESS) {
213		initData(&data->comm, 1);
214		if ( pAuth) {
215			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
216				return TCSERR(TSS_E_INTERNAL_ERROR);
217			}
218		}
219	} else
220		initData(&data->comm, 0);
221
222	data->comm.hdr.u.result = result;
223	return TSS_SUCCESS;
224}
225
226TSS_RESULT
227tcs_wrap_NV_ReadValue(struct tcsd_thread_data *data)
228{
229	TCS_CONTEXT_HANDLE hContext;
230	TSS_NV_INDEX hNVStore;
231	UINT32 offset,ulDataLength, i;
232	BYTE *rgbDataRead = NULL;
233	TSS_RESULT result;
234	TPM_AUTH Auth, *pAuth;
235
236	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
237		return TCSERR(TSS_E_INTERNAL_ERROR);
238
239	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
240		return TCSERR(TSS_E_INTERNAL_ERROR);
241
242	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
243		return TCSERR(TSS_E_INTERNAL_ERROR);
244
245	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
246		return TCSERR(TSS_E_INTERNAL_ERROR);
247
248	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &Auth, 0, &data->comm))
249		pAuth = NULL;
250	else
251		pAuth = &Auth;
252
253	MUTEX_LOCK(tcsp_lock);
254
255	result = TCSP_NV_ReadValue_Internal(hContext, hNVStore,
256					    offset, &ulDataLength, pAuth, &rgbDataRead);
257
258	MUTEX_UNLOCK(tcsp_lock);
259
260	if (result == TSS_SUCCESS) {
261		i = 0;
262		initData(&data->comm, 3);
263		if ( pAuth) {
264			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
265				free(rgbDataRead);
266				return TCSERR(TSS_E_INTERNAL_ERROR);
267			}
268		}
269		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &ulDataLength, 0, &data->comm)) {
270			free(rgbDataRead);
271			return TCSERR(TSS_E_INTERNAL_ERROR);
272		}
273		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, rgbDataRead, ulDataLength, &data->comm)) {
274			free(rgbDataRead);
275			return TCSERR(TSS_E_INTERNAL_ERROR);
276		}
277		free(rgbDataRead);
278	} else
279		initData(&data->comm, 0);
280
281	data->comm.hdr.u.result = result;
282	return TSS_SUCCESS;
283}
284
285TSS_RESULT
286tcs_wrap_NV_ReadValueAuth(struct tcsd_thread_data *data)
287{
288	TCS_CONTEXT_HANDLE hContext;
289	TSS_NV_INDEX hNVStore;
290	UINT32 offset,ulDataLength, i;
291	BYTE *rgbDataRead = NULL;
292	TSS_RESULT result;
293	TPM_AUTH NVAuth, *pNVAuth;
294
295	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
296		return TCSERR(TSS_E_INTERNAL_ERROR);
297
298	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &data->comm))
299		return TCSERR(TSS_E_INTERNAL_ERROR);
300
301	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &data->comm))
302		return TCSERR(TSS_E_INTERNAL_ERROR);
303
304	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &data->comm))
305		return TCSERR(TSS_E_INTERNAL_ERROR);
306
307	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &NVAuth, 0, &data->comm)) {
308		pNVAuth = NULL;
309	} else {
310		pNVAuth = &NVAuth;
311	}
312
313	MUTEX_LOCK(tcsp_lock);
314
315	result = TCSP_NV_ReadValueAuth_Internal(hContext, hNVStore,
316						offset, &ulDataLength, pNVAuth, &rgbDataRead);
317
318	MUTEX_UNLOCK(tcsp_lock);
319
320	if (result == TSS_SUCCESS) {
321		i = 0;
322		initData(&data->comm, 3);
323		if ( pNVAuth) {
324			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pNVAuth, 0, &data->comm)) {
325				free(rgbDataRead);
326				return TCSERR(TSS_E_INTERNAL_ERROR);
327			}
328		}
329		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &ulDataLength, 0, &data->comm)) {
330			free(rgbDataRead);
331			return TCSERR(TSS_E_INTERNAL_ERROR);
332		}
333		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, rgbDataRead, ulDataLength, &data->comm)) {
334			free(rgbDataRead);
335			return TCSERR(TSS_E_INTERNAL_ERROR);
336		}
337		free(rgbDataRead);
338	} else
339		initData(&data->comm, 0);
340
341	data->comm.hdr.u.result = result;
342	return TSS_SUCCESS;
343}
344
345