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_UnBind(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	TCS_KEY_HANDLE keyHandle;
35	UINT32 inDataSize;
36	BYTE *inData;
37
38	TPM_AUTH privAuth;
39	TPM_AUTH *pPrivAuth;
40
41	UINT32 outDataSize;
42	BYTE *outData;
43	TSS_RESULT result;
44
45	int i;
46
47	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
48		return TCSERR(TSS_E_INTERNAL_ERROR);
49
50	if ((result = ctx_verify_context(hContext)))
51		goto done;
52
53	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
54
55	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &keyHandle, 0, &data->comm))
56		return TCSERR(TSS_E_INTERNAL_ERROR);
57	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &inDataSize, 0, &data->comm))
58		return TCSERR(TSS_E_INTERNAL_ERROR);
59
60	inData = calloc(1, inDataSize);
61	if (inData == NULL) {
62		LogError("malloc of %u bytes failed.", inDataSize);
63		return TCSERR(TSS_E_OUTOFMEMORY);
64	}
65	if (getData(TCSD_PACKET_TYPE_PBYTE, 3, inData, inDataSize, &data->comm)) {
66		free(inData);
67		return TCSERR(TSS_E_INTERNAL_ERROR);
68	}
69
70	result = getData(TCSD_PACKET_TYPE_AUTH, 4, &privAuth, 0, &data->comm);
71	if (result == TSS_TCP_RPC_BAD_PACKET_TYPE)
72		pPrivAuth = NULL;
73	else if (result) {
74		free(inData);
75		return result;
76	} else
77		pPrivAuth = &privAuth;
78
79	MUTEX_LOCK(tcsp_lock);
80
81	result = TCSP_UnBind_Internal(hContext, keyHandle, inDataSize, inData,
82				 pPrivAuth, &outDataSize, &outData);
83
84	MUTEX_UNLOCK(tcsp_lock);
85	free(inData);
86
87	if (result == TSS_SUCCESS) {
88		i = 0;
89		initData(&data->comm, 3);
90		if (pPrivAuth != NULL) {
91			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pPrivAuth, 0, &data->comm)) {
92				return TCSERR(TSS_E_INTERNAL_ERROR);
93			}
94		}
95		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) {
96			return TCSERR(TSS_E_INTERNAL_ERROR);
97		}
98		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) {
99			return TCSERR(TSS_E_INTERNAL_ERROR);
100		}
101		free(outData);
102	} else
103done:		initData(&data->comm, 0);
104
105	data->comm.hdr.u.result = result;
106
107	return TSS_SUCCESS;
108}
109