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. 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_SetOrdinalAuditStatus(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TPM_AUTH ownerAuth;
35	UINT32 ulOrdinal;
36	TSS_BOOL bAuditState;
37	TSS_RESULT result;
38
39	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
40		return TCSERR(TSS_E_INTERNAL_ERROR);
41
42	if ((result = ctx_verify_context(hContext)))
43		goto done;
44
45	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
46
47	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &ulOrdinal, 0, &data->comm))
48		return TCSERR(TSS_E_INTERNAL_ERROR);
49
50	if (getData(TCSD_PACKET_TYPE_BOOL, 2, &bAuditState, 0, &data->comm))
51		return TCSERR(TSS_E_INTERNAL_ERROR);
52
53	if (getData(TCSD_PACKET_TYPE_AUTH, 3, &ownerAuth, 0, &data->comm))
54		return TCSERR(TSS_E_INTERNAL_ERROR);
55
56	MUTEX_LOCK(tcsp_lock);
57
58	result = TCSP_SetOrdinalAuditStatus_Internal(hContext, &ownerAuth, ulOrdinal, bAuditState);
59
60	MUTEX_UNLOCK(tcsp_lock);
61
62	if (result == TSS_SUCCESS) {
63		initData(&data->comm, 1);
64		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
65			return TCSERR(TSS_E_INTERNAL_ERROR);
66	} else
67done:		initData(&data->comm, 0);
68
69	data->comm.hdr.u.result = result;
70
71	return TSS_SUCCESS;
72}
73
74TSS_RESULT
75tcs_wrap_GetAuditDigest(struct tcsd_thread_data *data)
76{
77	TCS_CONTEXT_HANDLE hContext;
78	UINT32 startOrdinal;
79	TPM_DIGEST auditDigest;
80	UINT32 counterValueSize;
81	BYTE *counterValue;
82	TSS_BOOL more;
83	UINT32 ordSize;
84	UINT32 *ordList;
85	TSS_RESULT result;
86
87	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
88		return TCSERR(TSS_E_INTERNAL_ERROR);
89
90	if ((result = ctx_verify_context(hContext)))
91		goto done;
92
93	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
94
95	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &startOrdinal, 0, &data->comm))
96		return TCSERR(TSS_E_INTERNAL_ERROR);
97
98	MUTEX_LOCK(tcsp_lock);
99
100	result = TCSP_GetAuditDigest_Internal(hContext, startOrdinal, &auditDigest, &counterValueSize, &counterValue,
101						&more, &ordSize, &ordList);
102
103	MUTEX_UNLOCK(tcsp_lock);
104
105	if (result == TSS_SUCCESS) {
106		initData(&data->comm, 6);
107		if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &auditDigest, 0, &data->comm)) {
108			free(counterValue);
109			free(ordList);
110			return TCSERR(TSS_E_INTERNAL_ERROR);
111		}
112		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &counterValueSize, 0, &data->comm)) {
113			free(counterValue);
114			free(ordList);
115			return TCSERR(TSS_E_INTERNAL_ERROR);
116		}
117		if (setData(TCSD_PACKET_TYPE_PBYTE, 2, counterValue, counterValueSize, &data->comm)) {
118			free(counterValue);
119			free(ordList);
120			return TCSERR(TSS_E_INTERNAL_ERROR);
121		}
122		free(counterValue);
123		if (setData(TCSD_PACKET_TYPE_BOOL, 3, &more, 0, &data->comm)) {
124			free(ordList);
125			return TCSERR(TSS_E_INTERNAL_ERROR);
126		}
127		if (setData(TCSD_PACKET_TYPE_UINT32, 4, &ordSize, 0, &data->comm)) {
128			free(ordList);
129			return TCSERR(TSS_E_INTERNAL_ERROR);
130		}
131		if (setData(TCSD_PACKET_TYPE_PBYTE, 5, ordList, ordSize * sizeof(UINT32), &data->comm)) {
132			free(ordList);
133			return TCSERR(TSS_E_INTERNAL_ERROR);
134		}
135		free(ordList);
136	} else
137done:		initData(&data->comm, 0);
138
139	data->comm.hdr.u.result = result;
140
141	return TSS_SUCCESS;
142}
143
144TSS_RESULT
145tcs_wrap_GetAuditDigestSigned(struct tcsd_thread_data *data)
146{
147	TCS_CONTEXT_HANDLE hContext;
148	TCS_KEY_HANDLE keyHandle;
149	TSS_BOOL closeAudit;
150	TPM_NONCE antiReplay;
151	TPM_AUTH privAuth, nullAuth, *pAuth;
152	UINT32 counterValueSize;
153	BYTE *counterValue;
154	TPM_DIGEST auditDigest;
155	TPM_DIGEST ordinalDigest;
156	UINT32 sigSize;
157	BYTE *sig;
158	TSS_RESULT result;
159	int i;
160
161	memset(&privAuth, 0, sizeof(TPM_AUTH));
162	memset(&nullAuth, 0, sizeof(TPM_AUTH));
163
164	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
165		return TCSERR(TSS_E_INTERNAL_ERROR);
166
167	if ((result = ctx_verify_context(hContext)))
168		goto done;
169
170	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
171
172	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &keyHandle, 0, &data->comm))
173		return TCSERR(TSS_E_INTERNAL_ERROR);
174	if (getData(TCSD_PACKET_TYPE_BOOL, 2, &closeAudit, 0, &data->comm))
175		return TCSERR(TSS_E_INTERNAL_ERROR);
176	if (getData(TCSD_PACKET_TYPE_NONCE, 3, &antiReplay, 0, &data->comm))
177		return TCSERR(TSS_E_INTERNAL_ERROR);
178	if (getData(TCSD_PACKET_TYPE_AUTH, 4, &privAuth, 0, &data->comm))
179		return TCSERR(TSS_E_INTERNAL_ERROR);
180
181	if (memcmp(&nullAuth, &privAuth, sizeof(TPM_AUTH)))
182		pAuth = &privAuth;
183	else
184		pAuth = NULL;
185
186	MUTEX_LOCK(tcsp_lock);
187
188	result = TCSP_GetAuditDigestSigned_Internal(hContext, keyHandle, closeAudit, antiReplay,
189							pAuth, &counterValueSize, &counterValue,
190							&auditDigest, &ordinalDigest,
191							&sigSize, &sig);
192
193	MUTEX_UNLOCK(tcsp_lock);
194
195	if (result == TSS_SUCCESS) {
196		i = 0;
197		initData(&data->comm, 7);
198		if (pAuth) {
199			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
200				free(counterValue);
201				free(sig);
202				return TCSERR(TSS_E_INTERNAL_ERROR);
203			}
204		}
205		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &counterValueSize, 0, &data->comm)) {
206			free(counterValue);
207			free(sig);
208			return TCSERR(TSS_E_INTERNAL_ERROR);
209		}
210		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, counterValue, counterValueSize, &data->comm)) {
211			free(counterValue);
212			free(sig);
213			return TCSERR(TSS_E_INTERNAL_ERROR);
214		}
215		free(counterValue);
216		if (setData(TCSD_PACKET_TYPE_DIGEST, i++, &auditDigest, 0, &data->comm)) {
217			free(sig);
218			return TCSERR(TSS_E_INTERNAL_ERROR);
219		}
220		if (setData(TCSD_PACKET_TYPE_DIGEST, i++, &ordinalDigest, 0, &data->comm)) {
221			free(sig);
222			return TCSERR(TSS_E_INTERNAL_ERROR);
223		}
224		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &sigSize, 0, &data->comm)) {
225			free(sig);
226			return TCSERR(TSS_E_INTERNAL_ERROR);
227		}
228		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, sig, sigSize, &data->comm)) {
229			free(sig);
230			return TCSERR(TSS_E_INTERNAL_ERROR);
231		}
232		free(sig);
233	} else
234done:		initData(&data->comm, 0);
235
236	data->comm.hdr.u.result = result;
237
238	return TSS_SUCCESS;
239}
240