xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcs/rpc/tcstp/rpc_caps_tpm.c (revision 1023804e3833a0bd94414f2545512128f6502c74)
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_GetCapability(struct tcsd_thread_data * data)31 tcs_wrap_GetCapability(struct tcsd_thread_data *data)
32 {
33 	TCS_CONTEXT_HANDLE hContext;
34 	TCPA_CAPABILITY_AREA capArea;
35 	UINT32 subCapSize;
36 	BYTE *subCap;
37 	UINT32 respSize;
38 	BYTE *resp;
39 	TSS_RESULT result;
40 
41 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
42 		return TCSERR(TSS_E_INTERNAL_ERROR);
43 
44 	if ((result = ctx_verify_context(hContext)))
45 		goto done;
46 
47 	LogDebugFn("thread %ldd context %x", THREAD_ID, hContext);
48 
49 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &capArea, 0, &data->comm))
50 		return TCSERR(TSS_E_INTERNAL_ERROR);
51 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &subCapSize, 0, &data->comm))
52 		return TCSERR(TSS_E_INTERNAL_ERROR);
53 
54 	if (subCapSize == 0)
55 		subCap = NULL;
56 	else {
57 		subCap = calloc(1, subCapSize);
58 		if (subCap == NULL) {
59 			LogError("malloc of %u bytes failed.", subCapSize);
60 			return TCSERR(TSS_E_OUTOFMEMORY);
61 		}
62 		if (getData(TCSD_PACKET_TYPE_PBYTE, 3, subCap, subCapSize, &data->comm)) {
63 			free(subCap);
64 			return TCSERR(TSS_E_INTERNAL_ERROR);
65 		}
66 	}
67 
68 	MUTEX_LOCK(tcsp_lock);
69 
70 	result = TCSP_GetCapability_Internal(hContext, capArea, subCapSize, subCap, &respSize,
71 					     &resp);
72 
73 	MUTEX_UNLOCK(tcsp_lock);
74 	free(subCap);
75 
76 	if (result == TSS_SUCCESS) {
77 		initData(&data->comm, 2);
78 		if (setData(TCSD_PACKET_TYPE_UINT32, 0, &respSize, 0, &data->comm)) {
79 			free(resp);
80 			return TCSERR(TSS_E_INTERNAL_ERROR);
81 		}
82 		if (setData(TCSD_PACKET_TYPE_PBYTE, 1, resp, respSize, &data->comm)) {
83 			free(resp);
84 			return TCSERR(TSS_E_INTERNAL_ERROR);
85 		}
86 		free(resp);
87 	} else
88 done:		initData(&data->comm, 0);
89 
90 	data->comm.hdr.u.result = result;
91 	return TSS_SUCCESS;
92 }
93 
94 TSS_RESULT
tcs_wrap_GetCapabilityOwner(struct tcsd_thread_data * data)95 tcs_wrap_GetCapabilityOwner(struct tcsd_thread_data *data)
96 {
97 	TCS_CONTEXT_HANDLE hContext;
98 	TPM_AUTH ownerAuth;
99 	TCPA_VERSION version;
100 	UINT32 nonVol;
101 	UINT32 vol;
102 	TSS_RESULT result;
103 
104 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
105 		return TCSERR(TSS_E_INTERNAL_ERROR);
106 
107 	if ((result = ctx_verify_context(hContext)))
108 		goto done;
109 
110 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
111 
112 	if (getData(TCSD_PACKET_TYPE_AUTH, 1, &ownerAuth, 0, &data->comm))
113 		return TCSERR(TSS_E_INTERNAL_ERROR);
114 
115 	MUTEX_LOCK(tcsp_lock);
116 
117 	result = TCSP_GetCapabilityOwner_Internal(hContext, &ownerAuth, &version, &nonVol, &vol);
118 
119 	MUTEX_UNLOCK(tcsp_lock);
120 
121 	if (result == TSS_SUCCESS) {
122 		initData(&data->comm, 4);
123 		if (setData(TCSD_PACKET_TYPE_VERSION, 0, &version, 0, &data->comm)) {
124 			return TCSERR(TSS_E_INTERNAL_ERROR);
125 		}
126 		if (setData(TCSD_PACKET_TYPE_UINT32, 1, &nonVol, 0, &data->comm)) {
127 			return TCSERR(TSS_E_INTERNAL_ERROR);
128 		}
129 		if (setData(TCSD_PACKET_TYPE_UINT32, 2, &vol, 0, &data->comm)) {
130 			return TCSERR(TSS_E_INTERNAL_ERROR);
131 		}
132 		if (setData(TCSD_PACKET_TYPE_AUTH, 3, &ownerAuth, 0, &data->comm)) {
133 			return TCSERR(TSS_E_INTERNAL_ERROR);
134 		}
135 	} else
136 done:		initData(&data->comm, 0);
137 
138 	data->comm.hdr.u.result = result;
139 	return TSS_SUCCESS;
140 }
141 
142 TSS_RESULT
tcs_wrap_SetCapability(struct tcsd_thread_data * data)143 tcs_wrap_SetCapability(struct tcsd_thread_data *data)
144 {
145 	TCS_CONTEXT_HANDLE hContext;
146 	TCPA_CAPABILITY_AREA capArea;
147 	UINT32 subCapSize;
148 	BYTE *subCap;
149 	UINT32 valueSize;
150 	BYTE *value;
151 	TSS_RESULT result;
152 	TPM_AUTH ownerAuth, *pOwnerAuth;
153 
154 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
155 		return TCSERR(TSS_E_INTERNAL_ERROR);
156 
157 	if ((result = ctx_verify_context(hContext)))
158 		goto done;
159 
160 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
161 
162 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &capArea, 0, &data->comm))
163 		return TCSERR(TSS_E_INTERNAL_ERROR);
164 	if (getData(TCSD_PACKET_TYPE_UINT32, 2, &subCapSize, 0, &data->comm))
165 		return TCSERR(TSS_E_INTERNAL_ERROR);
166 
167 	if (subCapSize == 0)
168 		subCap = NULL;
169 	else {
170 		subCap = calloc(1, subCapSize);
171 		if (subCap == NULL) {
172 			LogError("malloc of %u bytes failed.", subCapSize);
173 			return TCSERR(TSS_E_OUTOFMEMORY);
174 		}
175 		if (getData(TCSD_PACKET_TYPE_PBYTE, 3, subCap, subCapSize, &data->comm)) {
176 			free(subCap);
177 			return TCSERR(TSS_E_INTERNAL_ERROR);
178 		}
179 	}
180 
181 	if (getData(TCSD_PACKET_TYPE_UINT32, 4, &valueSize, 0, &data->comm)) {
182 		free(subCap);
183 		return TCSERR(TSS_E_INTERNAL_ERROR);
184 	}
185 
186 	if (valueSize == 0)
187 		value = NULL;
188 	else {
189 		value = calloc(1, valueSize);
190 		if (value == NULL) {
191 			free(subCap);
192 			LogError("malloc of %u bytes failed.", valueSize);
193 			return TCSERR(TSS_E_OUTOFMEMORY);
194 		}
195 		if (getData(TCSD_PACKET_TYPE_PBYTE, 5, value, valueSize, &data->comm)) {
196 			free(subCap);
197 			free(value);
198 			return TCSERR(TSS_E_INTERNAL_ERROR);
199 		}
200 	}
201 
202 	if (getData(TCSD_PACKET_TYPE_AUTH, 6, &ownerAuth, 0, &data->comm))
203 		pOwnerAuth = NULL;
204 	else
205 		pOwnerAuth = &ownerAuth;
206 
207 
208 	MUTEX_LOCK(tcsp_lock);
209 
210 	result = TCSP_SetCapability_Internal(hContext, capArea, subCapSize, subCap, valueSize,
211 					     value, pOwnerAuth);
212 
213 	MUTEX_UNLOCK(tcsp_lock);
214 	free(subCap);
215 	free(value);
216 
217 	if (result == TSS_SUCCESS) {
218 		initData(&data->comm, 1);
219 		if (pOwnerAuth) {
220 			if (setData(TCSD_PACKET_TYPE_AUTH, 0, pOwnerAuth, 0, &data->comm)) {
221 				return TCSERR(TSS_E_INTERNAL_ERROR);
222 			}
223 		}
224 	} else
225 done:		initData(&data->comm, 0);
226 
227 	data->comm.hdr.u.result = result;
228 	return TSS_SUCCESS;
229 }
230