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 
30 TSS_RESULT
tcs_wrap_PhysicalSetDeactivated(struct tcsd_thread_data * data)31 tcs_wrap_PhysicalSetDeactivated(struct tcsd_thread_data *data)
32 {
33 	TCS_CONTEXT_HANDLE hContext;
34 	TSS_BOOL state;
35 	TSS_RESULT result;
36 
37 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
38 		return TCSERR(TSS_E_INTERNAL_ERROR);
39 
40 	if ((result = ctx_verify_context(hContext)))
41 		goto done;
42 
43 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
44 
45 	if (getData(TCSD_PACKET_TYPE_BOOL, 1, &state, 0, &data->comm))
46 		return TCSERR(TSS_E_INTERNAL_ERROR);
47 
48 	MUTEX_LOCK(tcsp_lock);
49 
50 	result = TCSP_PhysicalSetDeactivated_Internal(hContext, state);
51 
52 	MUTEX_UNLOCK(tcsp_lock);
53 done:
54 	initData(&data->comm, 0);
55 	data->comm.hdr.u.result = result;
56 
57 	return TSS_SUCCESS;
58 }
59 
60 TSS_RESULT
tcs_wrap_DisableOwnerClear(struct tcsd_thread_data * data)61 tcs_wrap_DisableOwnerClear(struct tcsd_thread_data *data)
62 {
63 	TCS_CONTEXT_HANDLE hContext;
64 	TSS_RESULT result;
65 	TPM_AUTH auth;
66 
67 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
68 		return TCSERR(TSS_E_INTERNAL_ERROR);
69 
70 	if ((result = ctx_verify_context(hContext)))
71 		goto done;
72 
73 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
74 
75 	if (getData(TCSD_PACKET_TYPE_AUTH, 1, &auth, 0, &data->comm))
76 		return TCSERR(TSS_E_INTERNAL_ERROR);
77 
78 	MUTEX_LOCK(tcsp_lock);
79 
80 	result = TCSP_DisableOwnerClear_Internal(hContext, &auth);
81 
82 	MUTEX_UNLOCK(tcsp_lock);
83 
84 	if (result == TSS_SUCCESS) {
85 		initData(&data->comm, 1);
86 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) {
87 			return TCSERR(TSS_E_INTERNAL_ERROR);
88 		}
89 	} else
90 done:		initData(&data->comm, 0);
91 
92 	data->comm.hdr.u.result = result;
93 	return TSS_SUCCESS;
94 }
95 
96 TSS_RESULT
tcs_wrap_ForceClear(struct tcsd_thread_data * data)97 tcs_wrap_ForceClear(struct tcsd_thread_data *data)
98 {
99 	TCS_CONTEXT_HANDLE hContext;
100 	TSS_RESULT result;
101 
102 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
103 		return TCSERR(TSS_E_INTERNAL_ERROR);
104 
105 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
106 
107 	MUTEX_LOCK(tcsp_lock);
108 
109 	result = TCSP_ForceClear_Internal(hContext);
110 
111 	MUTEX_UNLOCK(tcsp_lock);
112 
113 	initData(&data->comm, 0);
114 	data->comm.hdr.u.result = result;
115 
116 	return TSS_SUCCESS;
117 }
118 
119 TSS_RESULT
tcs_wrap_DisableForceClear(struct tcsd_thread_data * data)120 tcs_wrap_DisableForceClear(struct tcsd_thread_data *data)
121 {
122 	TCS_CONTEXT_HANDLE hContext;
123 	TSS_RESULT result;
124 
125 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
126 		return TCSERR(TSS_E_INTERNAL_ERROR);
127 
128 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
129 
130 	MUTEX_LOCK(tcsp_lock);
131 
132 	result = TCSP_DisableForceClear_Internal(hContext);
133 
134 	MUTEX_UNLOCK(tcsp_lock);
135 
136 	initData(&data->comm, 0);
137 	data->comm.hdr.u.result = result;
138 
139 	return TSS_SUCCESS;
140 }
141 
142 TSS_RESULT
tcs_wrap_PhysicalEnable(struct tcsd_thread_data * data)143 tcs_wrap_PhysicalEnable(struct tcsd_thread_data *data)
144 {
145 	TCS_CONTEXT_HANDLE hContext;
146 	TSS_RESULT result;
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 	MUTEX_LOCK(tcsp_lock);
154 
155 	result = TCSP_PhysicalEnable_Internal(hContext);
156 
157 	MUTEX_UNLOCK(tcsp_lock);
158 
159 	initData(&data->comm, 0);
160 	data->comm.hdr.u.result = result;
161 
162 	return TSS_SUCCESS;
163 }
164 
165 TSS_RESULT
tcs_wrap_SetOwnerInstall(struct tcsd_thread_data * data)166 tcs_wrap_SetOwnerInstall(struct tcsd_thread_data *data)
167 {
168 	TCS_CONTEXT_HANDLE hContext;
169 	TSS_BOOL state;
170 	TSS_RESULT result;
171 
172 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
173 		return TCSERR(TSS_E_INTERNAL_ERROR);
174 
175 	if ((result = ctx_verify_context(hContext)))
176 		goto done;
177 
178 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
179 
180 	if (getData(TCSD_PACKET_TYPE_BOOL, 1, &state, 0, &data->comm))
181 		return TCSERR(TSS_E_INTERNAL_ERROR);
182 
183 	MUTEX_LOCK(tcsp_lock);
184 
185 	result = TCSP_SetOwnerInstall_Internal(hContext, state);
186 
187 	MUTEX_UNLOCK(tcsp_lock);
188 done:
189 	initData(&data->comm, 0);
190 	data->comm.hdr.u.result = result;
191 
192 	return TSS_SUCCESS;
193 }
194 
195 TSS_RESULT
tcs_wrap_OwnerSetDisable(struct tcsd_thread_data * data)196 tcs_wrap_OwnerSetDisable(struct tcsd_thread_data *data)
197 {
198 	TCS_CONTEXT_HANDLE hContext;
199 	TSS_BOOL disableState;
200 	TPM_AUTH ownerAuth;
201 	TSS_RESULT result;
202 
203 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
204 		return TCSERR(TSS_E_INTERNAL_ERROR);
205 
206 	if ((result = ctx_verify_context(hContext)))
207 		goto done;
208 
209 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
210 
211 	if (getData(TCSD_PACKET_TYPE_BOOL, 1, &disableState, 0, &data->comm))
212 		return TCSERR(TSS_E_INTERNAL_ERROR);
213 
214 	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &ownerAuth, 0, &data->comm))
215 		return TCSERR(TSS_E_INTERNAL_ERROR);
216 
217 	MUTEX_LOCK(tcsp_lock);
218 
219 	result = TCSP_OwnerSetDisable_Internal(hContext, disableState, &ownerAuth);
220 
221 	MUTEX_UNLOCK(tcsp_lock);
222 
223 	if (result == TSS_SUCCESS) {
224 		initData(&data->comm, 1);
225 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
226 			return TCSERR(TSS_E_INTERNAL_ERROR);
227 		}
228 	} else
229 done:		initData(&data->comm, 0);
230 	data->comm.hdr.u.result = result;
231 
232 	return TSS_SUCCESS;
233 }
234 
235 TSS_RESULT
tcs_wrap_PhysicalDisable(struct tcsd_thread_data * data)236 tcs_wrap_PhysicalDisable(struct tcsd_thread_data *data)
237 {
238 	TCS_CONTEXT_HANDLE hContext;
239 	TSS_RESULT result;
240 
241 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
242 		return TCSERR(TSS_E_INTERNAL_ERROR);
243 
244 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
245 
246 	MUTEX_LOCK(tcsp_lock);
247 
248 	result = TCSP_PhysicalDisable_Internal(hContext);
249 
250 	MUTEX_UNLOCK(tcsp_lock);
251 
252 	initData(&data->comm, 0);
253 	data->comm.hdr.u.result = result;
254 
255 	return TSS_SUCCESS;
256 }
257 
258 TSS_RESULT
tcs_wrap_PhysicalPresence(struct tcsd_thread_data * data)259 tcs_wrap_PhysicalPresence(struct tcsd_thread_data *data)
260 {
261 	TCS_CONTEXT_HANDLE hContext;
262 	TSS_RESULT result;
263 	TCPA_PHYSICAL_PRESENCE phyPresFlags;
264 
265 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
266 		return TCSERR(TSS_E_INTERNAL_ERROR);
267 
268 	if ((result = ctx_verify_context(hContext)))
269 		goto done;
270 
271 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
272 
273 	if (getData(TCSD_PACKET_TYPE_UINT16, 1, &phyPresFlags, 0, &data->comm))
274 		return TCSERR(TSS_E_INTERNAL_ERROR);
275 
276 	MUTEX_LOCK(tcsp_lock);
277 
278 	result = TCSP_PhysicalPresence_Internal(hContext, phyPresFlags);
279 
280 	MUTEX_UNLOCK(tcsp_lock);
281 done:
282 	initData(&data->comm, 0);
283 	data->comm.hdr.u.result = result;
284 
285 	return TSS_SUCCESS;
286 }
287 
288 TSS_RESULT
tcs_wrap_SetTempDeactivated(struct tcsd_thread_data * data)289 tcs_wrap_SetTempDeactivated(struct tcsd_thread_data *data)
290 {
291 	TCS_CONTEXT_HANDLE hContext;
292 	TSS_RESULT result;
293 
294 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
295 		return TCSERR(TSS_E_INTERNAL_ERROR);
296 
297 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
298 
299 	MUTEX_LOCK(tcsp_lock);
300 
301 	result = TCSP_SetTempDeactivated_Internal(hContext);
302 
303 	MUTEX_UNLOCK(tcsp_lock);
304 
305 	initData(&data->comm, 0);
306 	data->comm.hdr.u.result = result;
307 
308 	return TSS_SUCCESS;
309 }
310 
311 #ifdef TSS_BUILD_TSS12
312 TSS_RESULT
tcs_wrap_SetTempDeactivated2(struct tcsd_thread_data * data)313 tcs_wrap_SetTempDeactivated2(struct tcsd_thread_data *data)
314 {
315 	TCS_CONTEXT_HANDLE hContext;
316 	TPM_AUTH operatorAuth, nullAuth, *pAuth;
317 	TSS_RESULT result;
318 
319 	memset(&operatorAuth, 0, sizeof(TPM_AUTH));
320 	memset(&nullAuth, 0, sizeof(TPM_AUTH));
321 
322 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
323 		return TCSERR(TSS_E_INTERNAL_ERROR);
324 
325 	if ((result = ctx_verify_context(hContext)))
326 		goto done;
327 
328 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
329 
330 	if (getData(TCSD_PACKET_TYPE_AUTH, 1, &operatorAuth, 0, &data->comm))
331 		return TCSERR(TSS_E_INTERNAL_ERROR);
332 
333 	if (memcmp(&nullAuth, &operatorAuth, sizeof(TPM_AUTH)))
334 		pAuth = &operatorAuth;
335 	else
336 		pAuth = NULL;
337 
338 	MUTEX_LOCK(tcsp_lock);
339 
340 	result = TCSP_SetTempDeactivated2_Internal(hContext, pAuth);
341 
342 	MUTEX_UNLOCK(tcsp_lock);
343 
344 	if (result == TSS_SUCCESS) {
345 		initData(&data->comm, 1);
346 		if (pAuth) {
347 			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &data->comm)) {
348 				return TCSERR(TSS_E_INTERNAL_ERROR);
349 			}
350 		}
351 	} else
352 done:		initData(&data->comm, 0);
353 
354 	data->comm.hdr.u.result = result;
355 
356 	return TSS_SUCCESS;
357 }
358 
359 TSS_RESULT
tcs_wrap_ResetLockValue(struct tcsd_thread_data * data)360 tcs_wrap_ResetLockValue(struct tcsd_thread_data *data)
361 {
362 	TCS_CONTEXT_HANDLE hContext;
363 	TPM_AUTH ownerAuth;
364 	TSS_RESULT result;
365 
366 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
367 		return TCSERR(TSS_E_INTERNAL_ERROR);
368 
369 	if ((result = ctx_verify_context(hContext)))
370 		goto done;
371 
372 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
373 
374 	if (getData(TCSD_PACKET_TYPE_AUTH, 1, &ownerAuth, 0, &data->comm))
375 		return TCSERR(TSS_E_INTERNAL_ERROR);
376 
377 	MUTEX_LOCK(tcsp_lock);
378 
379 	result = TCSP_ResetLockValue_Internal(hContext, &ownerAuth);
380 
381 	MUTEX_UNLOCK(tcsp_lock);
382 
383 	if (result == TSS_SUCCESS) {
384 		initData(&data->comm, 1);
385 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
386 			return TCSERR(TSS_E_INTERNAL_ERROR);
387 		}
388 	} else
389 done:		initData(&data->comm, 0);
390 
391 	data->comm.hdr.u.result = result;
392 
393 	return TSS_SUCCESS;
394 }
395 
396 TSS_RESULT
tcs_wrap_FlushSpecific(struct tcsd_thread_data * data)397 tcs_wrap_FlushSpecific(struct tcsd_thread_data *data)
398 {
399 	TCS_CONTEXT_HANDLE hContext;
400 	TCS_HANDLE hResHandle;
401 	TPM_RESOURCE_TYPE resourceType;
402 	TSS_RESULT result;
403 
404 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
405 		return TCSERR(TSS_E_INTERNAL_ERROR);
406 
407 	if ((result = ctx_verify_context(hContext)))
408 		goto done;
409 
410 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
411 
412 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hResHandle, 0, &data->comm))
413 		return TCSERR(TSS_E_INTERNAL_ERROR);
414 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &resourceType, 0, &data->comm))
415 		return TCSERR(TSS_E_INTERNAL_ERROR);
416 
417 	MUTEX_LOCK(tcsp_lock);
418 
419 	result = TCSP_FlushSpecific_Internal(hContext, hResHandle, resourceType);
420 
421 	MUTEX_UNLOCK(tcsp_lock);
422 done:
423 	initData(&data->comm, 0);
424 	data->comm.hdr.u.result = result;
425 
426 	return TSS_SUCCESS;
427 }
428 #endif
429