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 
30 TSS_RESULT
tcs_wrap_KillMaintenanceFeature(struct tcsd_thread_data * data)31 tcs_wrap_KillMaintenanceFeature(struct tcsd_thread_data *data)
32 {
33 	TCS_CONTEXT_HANDLE hContext;
34 	TSS_RESULT result;
35 	TPM_AUTH ownerAuth;
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_AUTH, 1, &ownerAuth, 0, &data->comm))
46 		return TCSERR(TSS_E_INTERNAL_ERROR);
47 
48 	MUTEX_LOCK(tcsp_lock);
49 
50 	result = TCSP_KillMaintenanceFeature_Internal(hContext, &ownerAuth);
51 
52 	MUTEX_UNLOCK(tcsp_lock);
53 
54 	if (result == TSS_SUCCESS) {
55 		initData(&data->comm, 1);
56 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
57 			return TCSERR(TSS_E_INTERNAL_ERROR);
58 		}
59 	} else
60 done:		initData(&data->comm, 0);
61 
62 	data->comm.hdr.u.result = result;
63 
64 	return TSS_SUCCESS;
65 }
66 
67 TSS_RESULT
tcs_wrap_CreateMaintenanceArchive(struct tcsd_thread_data * data)68 tcs_wrap_CreateMaintenanceArchive(struct tcsd_thread_data *data)
69 {
70 	TCS_CONTEXT_HANDLE hContext;
71 	TSS_RESULT result;
72 	TPM_AUTH ownerAuth;
73 	TSS_BOOL generateRandom;
74 	UINT32 randomSize, archiveSize;
75 	BYTE *random, *archive;
76 
77 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
78 		return TCSERR(TSS_E_INTERNAL_ERROR);
79 
80 	if ((result = ctx_verify_context(hContext)))
81 		goto done;
82 
83 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
84 
85 	if (getData(TCSD_PACKET_TYPE_BOOL, 1, &generateRandom, 0, &data->comm))
86 		return TCSERR(TSS_E_INTERNAL_ERROR);
87 
88 	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &ownerAuth, 0, &data->comm))
89 		return TCSERR(TSS_E_INTERNAL_ERROR);
90 
91 	MUTEX_LOCK(tcsp_lock);
92 
93 	result = TCSP_CreateMaintenanceArchive_Internal(hContext, generateRandom, &ownerAuth,
94 							&randomSize, &random, &archiveSize,
95 							&archive);
96 
97 	MUTEX_UNLOCK(tcsp_lock);
98 
99 	if (result == TSS_SUCCESS) {
100 		initData(&data->comm, 5);
101 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
102 			free(random);
103 			free(archive);
104 			return TCSERR(TSS_E_INTERNAL_ERROR);
105 		}
106 
107 		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &randomSize, 0, &data->comm)) {
108 			free(random);
109 			free(archive);
110 			return TCSERR(TSS_E_INTERNAL_ERROR);
111 		}
112 		if (setData(TCSD_PACKET_TYPE_PBYTE, 2, random, randomSize, &data->comm)) {
113 			free(random);
114 			free(archive);
115 			return TCSERR(TSS_E_INTERNAL_ERROR);
116 		}
117 
118 		if (setData(TCSD_PACKET_TYPE_UINT32, 3, &archiveSize, 0, &data->comm)) {
119 			free(random);
120 			free(archive);
121 			return TCSERR(TSS_E_INTERNAL_ERROR);
122 		}
123 		if (setData(TCSD_PACKET_TYPE_PBYTE, 4, archive, archiveSize, &data->comm)) {
124 			free(random);
125 			free(archive);
126 			return TCSERR(TSS_E_INTERNAL_ERROR);
127 		}
128 
129 		free(random);
130 		free(archive);
131 	} else
132 done:		initData(&data->comm, 0);
133 
134 	data->comm.hdr.u.result = result;
135 
136 	return TSS_SUCCESS;
137 }
138 
139 TSS_RESULT
tcs_wrap_LoadMaintenanceArchive(struct tcsd_thread_data * data)140 tcs_wrap_LoadMaintenanceArchive(struct tcsd_thread_data *data)
141 {
142 	TCS_CONTEXT_HANDLE hContext;
143 	TSS_RESULT result;
144 	TPM_AUTH ownerAuth;
145 	UINT32 dataInSize, dataOutSize;
146 	BYTE *dataIn, *dataOut;
147 
148 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
149 		return TCSERR(TSS_E_INTERNAL_ERROR);
150 
151 	if ((result = ctx_verify_context(hContext)))
152 		goto done;
153 
154 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
155 
156 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &dataInSize, 0, &data->comm))
157 		return TCSERR(TSS_E_INTERNAL_ERROR);
158 
159 	dataIn = (BYTE *)malloc(dataInSize);
160 	if (dataIn == NULL) {
161 		LogError("malloc of %d bytes failed.", dataInSize);
162 		return TCSERR(TSS_E_INTERNAL_ERROR);
163 	}
164 	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, dataIn, dataInSize, &data->comm)) {
165 		free(dataIn);
166 		return TCSERR(TSS_E_INTERNAL_ERROR);
167 	}
168 
169 	if (getData(TCSD_PACKET_TYPE_AUTH, 3, &ownerAuth, 0, &data->comm)) {
170 		free(dataIn);
171 		return TCSERR(TSS_E_INTERNAL_ERROR);
172 	}
173 
174 	MUTEX_LOCK(tcsp_lock);
175 
176 	result = TCSP_LoadMaintenanceArchive_Internal(hContext, dataInSize, dataIn, &ownerAuth,
177 							&dataOutSize, &dataOut);
178 
179 	MUTEX_UNLOCK(tcsp_lock);
180 	free(dataIn);
181 
182 	if (result == TSS_SUCCESS) {
183 		initData(&data->comm, 3);
184 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm)) {
185 			free(dataOut);
186 			return TCSERR(TSS_E_INTERNAL_ERROR);
187 		}
188 		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &dataOutSize, 0, &data->comm)) {
189 			free(dataOut);
190 			return TCSERR(TSS_E_INTERNAL_ERROR);
191 		}
192 		if (setData(TCSD_PACKET_TYPE_PBYTE, 2, dataOut, dataOutSize, &data->comm)) {
193 			free(dataOut);
194 			return TCSERR(TSS_E_INTERNAL_ERROR);
195 		}
196 
197 		free(dataOut);
198 	} else
199 done:		initData(&data->comm, 0);
200 
201 	data->comm.hdr.u.result = result;
202 
203 	return TSS_SUCCESS;
204 }
205 
206 TSS_RESULT
tcs_wrap_LoadManuMaintPub(struct tcsd_thread_data * data)207 tcs_wrap_LoadManuMaintPub(struct tcsd_thread_data *data)
208 {
209 	TCS_CONTEXT_HANDLE hContext;
210 	TSS_RESULT result;
211 	UINT32 pubKeySize;
212 	BYTE *pubKey;
213 	TCPA_NONCE antiReplay;
214 	TCPA_DIGEST checksum;
215 
216 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
217 		return TCSERR(TSS_E_INTERNAL_ERROR);
218 
219 	if ((result = ctx_verify_context(hContext)))
220 		goto done;
221 
222 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
223 
224 	if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, &data->comm))
225 		return TCSERR(TSS_E_INTERNAL_ERROR);
226 
227 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &pubKeySize, 0, &data->comm))
228 		return TCSERR(TSS_E_INTERNAL_ERROR);
229 
230 	pubKey = (BYTE *)malloc(pubKeySize);
231 	if (pubKey == NULL) {
232 		LogError("malloc of %d bytes failed.", pubKeySize);
233 		return TCSERR(TSS_E_INTERNAL_ERROR);
234 	}
235 
236 	if (getData(TCSD_PACKET_TYPE_PBYTE, 3, pubKey, pubKeySize, &data->comm)) {
237 		free(pubKey);
238 		return TCSERR(TSS_E_INTERNAL_ERROR);
239 	}
240 
241 	MUTEX_LOCK(tcsp_lock);
242 
243 	result = TCSP_LoadManuMaintPub_Internal(hContext, antiReplay, pubKeySize, pubKey,
244 						&checksum);
245 
246 	MUTEX_UNLOCK(tcsp_lock);
247 	free(pubKey);
248 
249 	if (result == TSS_SUCCESS) {
250 		initData(&data->comm, 1);
251 		if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &checksum, 0, &data->comm)) {
252 			return TCSERR(TSS_E_INTERNAL_ERROR);
253 		}
254 	} else
255 done:		initData(&data->comm, 0);
256 
257 	data->comm.hdr.u.result = result;
258 
259 	return TSS_SUCCESS;
260 }
261 
262 TSS_RESULT
tcs_wrap_ReadManuMaintPub(struct tcsd_thread_data * data)263 tcs_wrap_ReadManuMaintPub(struct tcsd_thread_data *data)
264 {
265 	TCS_CONTEXT_HANDLE hContext;
266 	TSS_RESULT result;
267 	TCPA_NONCE antiReplay;
268 	TCPA_DIGEST checksum;
269 
270 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
271 		return TCSERR(TSS_E_INTERNAL_ERROR);
272 
273 	if ((result = ctx_verify_context(hContext)))
274 		goto done;
275 
276 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
277 
278 	if (getData(TCSD_PACKET_TYPE_NONCE, 1, &antiReplay, 0, &data->comm))
279 		return TCSERR(TSS_E_INTERNAL_ERROR);
280 
281 	MUTEX_LOCK(tcsp_lock);
282 
283 	result = TCSP_ReadManuMaintPub_Internal(hContext, antiReplay, &checksum);
284 
285 	MUTEX_UNLOCK(tcsp_lock);
286 
287 	if (result == TSS_SUCCESS) {
288 		initData(&data->comm, 1);
289 		if (setData(TCSD_PACKET_TYPE_DIGEST, 0, &checksum, 0, &data->comm)) {
290 			return TCSERR(TSS_E_INTERNAL_ERROR);
291 		}
292 	} else
293 done:		initData(&data->comm, 0);
294 
295 	data->comm.hdr.u.result = result;
296 
297 	return TSS_SUCCESS;
298 }
299