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 <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_ReadCounter(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TSS_COUNTER_ID idCounter;
35	TPM_COUNTER_VALUE counterValue;
36	TSS_RESULT result;
37
38	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
39		return TCSERR(TSS_E_INTERNAL_ERROR);
40
41	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
42
43	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm))
44		return TCSERR(TSS_E_INTERNAL_ERROR);
45
46	MUTEX_LOCK(tcsp_lock);
47
48	result = TCSP_ReadCounter_Internal(hContext, idCounter, &counterValue);
49
50	MUTEX_UNLOCK(tcsp_lock);
51
52	if (result == TSS_SUCCESS) {
53		initData(&data->comm, 1);
54		if (setData(TCSD_PACKET_TYPE_COUNTER_VALUE, 0, &counterValue, 0, &data->comm))
55			return TCSERR(TSS_E_INTERNAL_ERROR);
56	} else
57		initData(&data->comm, 0);
58
59	data->comm.hdr.u.result = result;
60	return TSS_SUCCESS;
61}
62
63TSS_RESULT
64tcs_wrap_CreateCounter(struct tcsd_thread_data *data)
65{
66	TCS_CONTEXT_HANDLE hContext;
67	TSS_COUNTER_ID idCounter;
68	TPM_COUNTER_VALUE counterValue;
69	TPM_AUTH auth;
70	TPM_ENCAUTH encauth;
71	UINT32 LabelSize;
72	BYTE *pLabel = NULL;
73	TSS_RESULT result;
74
75	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
76		return TCSERR(TSS_E_INTERNAL_ERROR);
77
78	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
79
80	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &LabelSize, 0, &data->comm))
81		return TCSERR(TSS_E_INTERNAL_ERROR);
82
83	if ((pLabel = calloc(1, LabelSize)) == NULL) {
84		LogError("malloc of %u bytes failed.", LabelSize);
85		return TCSERR(TSS_E_OUTOFMEMORY);
86	}
87
88	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, &pLabel, LabelSize, &data->comm)) {
89		free(pLabel);
90		return TCSERR(TSS_E_INTERNAL_ERROR);
91	}
92	if (getData(TCSD_PACKET_TYPE_ENCAUTH, 3, &encauth, 0, &data->comm)) {
93		free(pLabel);
94		return TCSERR(TSS_E_INTERNAL_ERROR);
95	}
96	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &auth, 0, &data->comm)) {
97		free(pLabel);
98		return TCSERR(TSS_E_INTERNAL_ERROR);
99	}
100
101	MUTEX_LOCK(tcsp_lock);
102
103	result = TCSP_CreateCounter_Internal(hContext, LabelSize, pLabel, encauth, &auth,
104					     &idCounter, &counterValue);
105
106	MUTEX_UNLOCK(tcsp_lock);
107
108	free(pLabel);
109
110	if (result == TSS_SUCCESS) {
111		initData(&data->comm, 3);
112		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
113			return TCSERR(TSS_E_INTERNAL_ERROR);
114		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm))
115			return TCSERR(TSS_E_INTERNAL_ERROR);
116		if (setData(TCSD_PACKET_TYPE_COUNTER_VALUE, 2, &counterValue, 0, &data->comm))
117			return TCSERR(TSS_E_INTERNAL_ERROR);
118	} else
119		initData(&data->comm, 0);
120
121	data->comm.hdr.u.result = result;
122	return TSS_SUCCESS;
123}
124
125TSS_RESULT
126tcs_wrap_IncrementCounter(struct tcsd_thread_data *data)
127{
128	TCS_CONTEXT_HANDLE hContext;
129	TSS_COUNTER_ID idCounter;
130	TPM_COUNTER_VALUE counterValue;
131	TPM_AUTH auth;
132	TSS_RESULT result;
133
134	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
135		return TCSERR(TSS_E_INTERNAL_ERROR);
136
137	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
138
139	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm))
140		return TCSERR(TSS_E_INTERNAL_ERROR);
141	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm))
142		return TCSERR(TSS_E_INTERNAL_ERROR);
143
144	MUTEX_LOCK(tcsp_lock);
145
146	result = TCSP_IncrementCounter_Internal(hContext, idCounter, &auth, &counterValue);
147
148	MUTEX_UNLOCK(tcsp_lock);
149
150	if (result == TSS_SUCCESS) {
151		initData(&data->comm, 2);
152		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
153			return TCSERR(TSS_E_INTERNAL_ERROR);
154		if (setData(TCSD_PACKET_TYPE_COUNTER_VALUE, 1, &counterValue, 0, &data->comm))
155			return TCSERR(TSS_E_INTERNAL_ERROR);
156	} else
157		initData(&data->comm, 0);
158
159	data->comm.hdr.u.result = result;
160	return TSS_SUCCESS;
161}
162
163TSS_RESULT
164tcs_wrap_ReleaseCounter(struct tcsd_thread_data *data)
165{
166	TCS_CONTEXT_HANDLE hContext;
167	TSS_COUNTER_ID idCounter;
168	TPM_AUTH auth;
169	TSS_RESULT result;
170
171	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
172		return TCSERR(TSS_E_INTERNAL_ERROR);
173
174	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
175
176	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm))
177		return TCSERR(TSS_E_INTERNAL_ERROR);
178	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm))
179		return TCSERR(TSS_E_INTERNAL_ERROR);
180
181	MUTEX_LOCK(tcsp_lock);
182
183	result = TCSP_ReleaseCounter_Internal(hContext, idCounter, &auth);
184
185	MUTEX_UNLOCK(tcsp_lock);
186
187	if (result == TSS_SUCCESS) {
188		initData(&data->comm, 1);
189		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
190			return TCSERR(TSS_E_INTERNAL_ERROR);
191	} else
192		initData(&data->comm, 0);
193
194	data->comm.hdr.u.result = result;
195	return TSS_SUCCESS;
196}
197
198TSS_RESULT
199tcs_wrap_ReleaseCounterOwner(struct tcsd_thread_data *data)
200{
201	TCS_CONTEXT_HANDLE hContext;
202	TSS_COUNTER_ID idCounter;
203	TPM_AUTH auth;
204	TSS_RESULT result;
205
206	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
207		return TCSERR(TSS_E_INTERNAL_ERROR);
208
209	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
210
211	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &idCounter, 0, &data->comm))
212		return TCSERR(TSS_E_INTERNAL_ERROR);
213	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &auth, 0, &data->comm))
214		return TCSERR(TSS_E_INTERNAL_ERROR);
215
216	MUTEX_LOCK(tcsp_lock);
217
218	result = TCSP_ReleaseCounterOwner_Internal(hContext, idCounter, &auth);
219
220	MUTEX_UNLOCK(tcsp_lock);
221
222	if (result == TSS_SUCCESS) {
223		initData(&data->comm, 1);
224		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm))
225			return TCSERR(TSS_E_INTERNAL_ERROR);
226	} else
227		initData(&data->comm, 0);
228
229	data->comm.hdr.u.result = result;
230	return TSS_SUCCESS;
231}
232