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_TakeOwnership(struct tcsd_thread_data *data)
32{
33	TCS_CONTEXT_HANDLE hContext;
34	UINT16 protocolID;
35	UINT32 encOwnerAuthSize;
36	BYTE *encOwnerAuth;
37	UINT32 encSrkAuthSize;
38	BYTE *encSrkAuth;
39	UINT32 srkInfoSize;
40	BYTE *srkInfo;
41	TPM_AUTH ownerAuth;
42
43	UINT32 srkKeySize;
44	BYTE *srkKey;
45	TSS_RESULT result;
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_UINT16, 1, &protocolID, 0, &data->comm))
56		return TCSERR(TSS_E_INTERNAL_ERROR);
57	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &encOwnerAuthSize, 0, &data->comm))
58		return TCSERR(TSS_E_INTERNAL_ERROR);
59	encOwnerAuth = calloc(1, encOwnerAuthSize);
60	if (encOwnerAuth == NULL) {
61		LogError("malloc of %d bytes failed.", encOwnerAuthSize);
62		return TCSERR(TSS_E_OUTOFMEMORY);
63	}
64	if (getData(TCSD_PACKET_TYPE_PBYTE, 3, encOwnerAuth, encOwnerAuthSize, &data->comm)) {
65		free(encOwnerAuth);
66		return TCSERR(TSS_E_INTERNAL_ERROR);
67	}
68	if (getData(TCSD_PACKET_TYPE_UINT32, 4, &encSrkAuthSize, 0, &data->comm)) {
69		free(encOwnerAuth);
70		return TCSERR(TSS_E_INTERNAL_ERROR);
71	}
72	encSrkAuth = calloc(1, encSrkAuthSize);
73	if (encSrkAuth == NULL) {
74		LogError("malloc of %d bytes failed.", encSrkAuthSize);
75		free(encOwnerAuth);
76		return TCSERR(TSS_E_INTERNAL_ERROR);
77	}
78	if (getData(TCSD_PACKET_TYPE_PBYTE, 5, encSrkAuth, encSrkAuthSize, &data->comm)) {
79		free(encOwnerAuth);
80		free(encSrkAuth);
81		return TCSERR(TSS_E_INTERNAL_ERROR);
82	}
83	if (getData(TCSD_PACKET_TYPE_UINT32, 6, &srkInfoSize, 0, &data->comm)) {
84		free(encOwnerAuth);
85		free(encSrkAuth);
86		return TCSERR(TSS_E_INTERNAL_ERROR);
87	}
88
89	srkInfo = calloc(1, srkInfoSize);
90	if (srkInfo == NULL) {
91		LogError("malloc of %d bytes failed.", srkInfoSize);
92		free(encOwnerAuth);
93		free(encSrkAuth);
94		return TCSERR(TSS_E_INTERNAL_ERROR);
95	}
96	if (getData(TCSD_PACKET_TYPE_PBYTE, 7, srkInfo, srkInfoSize, &data->comm)) {
97		free(encOwnerAuth);
98		free(encSrkAuth);
99		free(srkInfo);
100		return TCSERR(TSS_E_INTERNAL_ERROR);
101	}
102	if (getData(TCSD_PACKET_TYPE_AUTH, 8, &ownerAuth, 0, &data->comm)) {
103		free(encOwnerAuth);
104		free(encSrkAuth);
105		free(srkInfo);
106		return TCSERR(TSS_E_INTERNAL_ERROR);
107	}
108
109	MUTEX_LOCK(tcsp_lock);
110
111	result = TCSP_TakeOwnership_Internal(hContext, protocolID, encOwnerAuthSize, encOwnerAuth,
112					     encSrkAuthSize, encSrkAuth, srkInfoSize, srkInfo,
113					     &ownerAuth, &srkKeySize, &srkKey);
114
115	MUTEX_UNLOCK(tcsp_lock);
116	free(encOwnerAuth);
117	free(encSrkAuth);
118	free(srkInfo);
119
120	if (result == TSS_SUCCESS) {
121		initData(&data->comm, 3);
122		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
123			free(srkKey);
124			return TCSERR(TSS_E_INTERNAL_ERROR);
125		}
126		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &srkKeySize, 0, &data->comm)) {
127			free(srkKey);
128			return TCSERR(TSS_E_INTERNAL_ERROR);
129		}
130		if (setData(TCSD_PACKET_TYPE_PBYTE, 2, srkKey, srkKeySize, &data->comm)) {
131			free(srkKey);
132			return TCSERR(TSS_E_INTERNAL_ERROR);
133		}
134		free(srkKey);
135	} else
136done:		initData(&data->comm, 0);
137
138	data->comm.hdr.u.result = result;
139	return TSS_SUCCESS;
140}
141
142TSS_RESULT
143tcs_wrap_OwnerClear(struct tcsd_thread_data *data)
144{
145	TCS_CONTEXT_HANDLE hContext;
146	TSS_RESULT result;
147	TPM_AUTH auth;
148
149	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
150		return TCSERR(TSS_E_INTERNAL_ERROR);
151
152	if ((result = ctx_verify_context(hContext)))
153		goto done;
154
155	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
156
157	if (getData(TCSD_PACKET_TYPE_AUTH, 1, &auth, 0, &data->comm))
158		return TCSERR(TSS_E_INTERNAL_ERROR);
159
160	MUTEX_LOCK(tcsp_lock);
161
162	result = TCSP_OwnerClear_Internal(hContext, &auth);
163
164	MUTEX_UNLOCK(tcsp_lock);
165
166	if (result == TSS_SUCCESS) {
167		initData(&data->comm, 1);
168		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &auth, 0, &data->comm)) {
169			return TCSERR(TSS_E_INTERNAL_ERROR);
170		}
171	} else
172done:		initData(&data->comm, 0);
173
174	data->comm.hdr.u.result = result;
175	return TSS_SUCCESS;
176}
177