rpc_caps_tpm.c revision 1.1.1.1.4.2
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 <syslog.h>
14#include <string.h>
15#include <netdb.h>
16
17#include "trousers/tss.h"
18#include "trousers_types.h"
19#include "tcs_tsp.h"
20#include "tcs_utils.h"
21#include "tcs_int_literals.h"
22#include "capabilities.h"
23#include "tcslog.h"
24#include "tcsd_wrap.h"
25#include "tcsd.h"
26#include "tcs_utils.h"
27#include "rpc_tcstp_tcs.h"
28
29
30TSS_RESULT
31tcs_wrap_GetCapability(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TCPA_CAPABILITY_AREA capArea;
35	UINT32 subCapSize;
36	BYTE *subCap;
37	UINT32 respSize;
38	BYTE *resp;
39	TSS_RESULT result;
40
41	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
42		return TCSERR(TSS_E_INTERNAL_ERROR);
43
44	LogDebugFn("thread %ldd context %x", THREAD_ID, hContext);
45
46	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &capArea, 0, &data->comm))
47		return TCSERR(TSS_E_INTERNAL_ERROR);
48	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &subCapSize, 0, &data->comm))
49		return TCSERR(TSS_E_INTERNAL_ERROR);
50
51	if (subCapSize == 0)
52		subCap = NULL;
53	else {
54		subCap = calloc(1, subCapSize);
55		if (subCap == NULL) {
56			LogError("malloc of %u bytes failed.", subCapSize);
57			return TCSERR(TSS_E_OUTOFMEMORY);
58		}
59		if (getData(TCSD_PACKET_TYPE_PBYTE, 3, subCap, subCapSize, &data->comm)) {
60			free(subCap);
61			return TCSERR(TSS_E_INTERNAL_ERROR);
62		}
63	}
64
65	MUTEX_LOCK(tcsp_lock);
66
67	result = TCSP_GetCapability_Internal(hContext, capArea, subCapSize, subCap, &respSize,
68					     &resp);
69
70	MUTEX_UNLOCK(tcsp_lock);
71	free(subCap);
72
73	if (result == TSS_SUCCESS) {
74		initData(&data->comm, 2);
75		if (setData(TCSD_PACKET_TYPE_UINT32, 0, &respSize, 0, &data->comm)) {
76			free(resp);
77			return TCSERR(TSS_E_INTERNAL_ERROR);
78		}
79		if (setData(TCSD_PACKET_TYPE_PBYTE, 1, resp, respSize, &data->comm)) {
80			free(resp);
81			return TCSERR(TSS_E_INTERNAL_ERROR);
82		}
83		free(resp);
84	} else
85		initData(&data->comm, 0);
86
87	data->comm.hdr.u.result = result;
88	return TSS_SUCCESS;
89}
90
91TSS_RESULT
92tcs_wrap_GetCapabilityOwner(struct tcsd_thread_data *data)
93{
94	TCS_CONTEXT_HANDLE hContext;
95	TPM_AUTH ownerAuth;
96	TCPA_VERSION version;
97	UINT32 nonVol;
98	UINT32 vol;
99	TSS_RESULT result;
100
101	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
102		return TCSERR(TSS_E_INTERNAL_ERROR);
103
104	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
105
106	if (getData(TCSD_PACKET_TYPE_AUTH, 1, &ownerAuth, 0, &data->comm))
107		return TCSERR(TSS_E_INTERNAL_ERROR);
108
109	MUTEX_LOCK(tcsp_lock);
110
111	result = TCSP_GetCapabilityOwner_Internal(hContext, &ownerAuth, &version, &nonVol, &vol);
112
113	MUTEX_UNLOCK(tcsp_lock);
114
115	if (result == TSS_SUCCESS) {
116		initData(&data->comm, 4);
117		if (setData(TCSD_PACKET_TYPE_VERSION, 0, &version, 0, &data->comm)) {
118			return TCSERR(TSS_E_INTERNAL_ERROR);
119		}
120		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &nonVol, 0, &data->comm)) {
121			return TCSERR(TSS_E_INTERNAL_ERROR);
122		}
123		if (setData(TCSD_PACKET_TYPE_UINT32, 2, &vol, 0, &data->comm)) {
124			return TCSERR(TSS_E_INTERNAL_ERROR);
125		}
126		if (setData(TCSD_PACKET_TYPE_AUTH, 3, &ownerAuth, 0, &data->comm)) {
127			return TCSERR(TSS_E_INTERNAL_ERROR);
128		}
129	} else
130		initData(&data->comm, 0);
131
132	data->comm.hdr.u.result = result;
133	return TSS_SUCCESS;
134}
135
136TSS_RESULT
137tcs_wrap_SetCapability(struct tcsd_thread_data *data)
138{
139	TCS_CONTEXT_HANDLE hContext;
140	TCPA_CAPABILITY_AREA capArea;
141	UINT32 subCapSize;
142	BYTE *subCap;
143	UINT32 valueSize;
144	BYTE *value;
145	TSS_RESULT result;
146	TPM_AUTH ownerAuth, *pOwnerAuth;
147
148	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
149		return TCSERR(TSS_E_INTERNAL_ERROR);
150
151	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
152
153	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &capArea, 0, &data->comm))
154		return TCSERR(TSS_E_INTERNAL_ERROR);
155	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &subCapSize, 0, &data->comm))
156		return TCSERR(TSS_E_INTERNAL_ERROR);
157
158	if (subCapSize == 0)
159		subCap = NULL;
160	else {
161		subCap = calloc(1, subCapSize);
162		if (subCap == NULL) {
163			LogError("malloc of %u bytes failed.", subCapSize);
164			return TCSERR(TSS_E_OUTOFMEMORY);
165		}
166		if (getData(TCSD_PACKET_TYPE_PBYTE, 3, subCap, subCapSize, &data->comm)) {
167			free(subCap);
168			return TCSERR(TSS_E_INTERNAL_ERROR);
169		}
170	}
171
172	if (getData(TCSD_PACKET_TYPE_UINT32, 4, &valueSize, 0, &data->comm)) {
173		free(subCap);
174		return TCSERR(TSS_E_INTERNAL_ERROR);
175	}
176
177	if (valueSize == 0)
178		value = NULL;
179	else {
180		value = calloc(1, valueSize);
181		if (value == NULL) {
182			free(subCap);
183			LogError("malloc of %u bytes failed.", valueSize);
184			return TCSERR(TSS_E_OUTOFMEMORY);
185		}
186		if (getData(TCSD_PACKET_TYPE_PBYTE, 5, value, valueSize, &data->comm)) {
187			free(subCap);
188			free(value);
189			return TCSERR(TSS_E_INTERNAL_ERROR);
190		}
191	}
192
193	if (getData(TCSD_PACKET_TYPE_AUTH, 6, &ownerAuth, 0, &data->comm))
194		pOwnerAuth = NULL;
195	else
196		pOwnerAuth = &ownerAuth;
197
198
199	MUTEX_LOCK(tcsp_lock);
200
201	result = TCSP_SetCapability_Internal(hContext, capArea, subCapSize, subCap, valueSize,
202					     value, pOwnerAuth);
203
204	MUTEX_UNLOCK(tcsp_lock);
205	free(subCap);
206	free(value);
207
208	if (result == TSS_SUCCESS) {
209		initData(&data->comm, 1);
210		if (pOwnerAuth) {
211			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pOwnerAuth, 0, &data->comm)) {
212				return TCSERR(TSS_E_INTERNAL_ERROR);
213			}
214		}
215	} else
216		initData(&data->comm, 0);
217
218	data->comm.hdr.u.result = result;
219	return TSS_SUCCESS;
220}
221