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_OpenContext(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TSS_RESULT result;
35	UINT32 tpm_version = tpm_metrics.version.minor;
36
37	LogDebugFn("thread %ld", THREAD_ID);
38
39	result = TCS_OpenContext_Internal(&hContext);
40	if (result == TSS_SUCCESS) {
41		initData(&data->comm, 2);
42		if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
43			return TCSERR(TSS_E_INTERNAL_ERROR);
44
45		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &tpm_version, 0, &data->comm))
46			return TCSERR(TSS_E_INTERNAL_ERROR);
47
48		/* Set the context in the thread's object. Later, if something goes wrong
49		 * and the connection can't be closed cleanly, we'll still have a reference
50		 * to what resources need to be freed. */
51		data->context = hContext;
52
53		LogDebug("New context is 0x%x", hContext);
54	} else
55		initData(&data->comm, 0);
56
57	data->comm.hdr.u.result = result;
58
59	return TSS_SUCCESS;
60}
61
62TSS_RESULT
63tcs_wrap_CloseContext(struct tcsd_thread_data *data)
64{
65	TCS_CONTEXT_HANDLE hContext;
66	TSS_RESULT result;
67
68	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
69		return TCSERR(TSS_E_INTERNAL_ERROR);
70
71	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
72
73	result = TCS_CloseContext_Internal(hContext);
74
75	/* This will signal the thread that the connection has been closed cleanly */
76	if (result == TSS_SUCCESS)
77		data->context = NULL_TCS_HANDLE;
78
79	initData(&data->comm, 0);
80	data->comm.hdr.u.result = result;
81
82	return TSS_SUCCESS;
83}
84