xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/rpc/tcstp/rpc_nv.c (revision 1023804e3833a0bd94414f2545512128f6502c74)
1 /*
2  * The Initial Developer of the Original Code is Intel Corporation.
3  * Portions created by Intel Corporation are Copyright (C) 2007 Intel Corporation.
4  * All Rights Reserved.
5  *
6  * trousers - An open source TCG Software Stack
7  *
8  * Author: james.xu@intel.com Rossey.liu@intel.com
9  *
10  */
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <assert.h>
16 
17 #include "trousers/tss.h"
18 #include "trousers/trousers.h"
19 #include "trousers_types.h"
20 #include "spi_utils.h"
21 #include "capabilities.h"
22 #include "tsplog.h"
23 #include "hosttable.h"
24 #include "tcsd_wrap.h"
25 #include "obj.h"
26 #include "rpc_tcstp_tsp.h"
27 
28 TSS_RESULT
RPC_NV_DefineOrReleaseSpace_TP(struct host_table_entry * hte,UINT32 cPubInfoSize,BYTE * pPubInfo,TCPA_ENCAUTH encAuth,TPM_AUTH * pAuth)29 RPC_NV_DefineOrReleaseSpace_TP(struct host_table_entry *hte,	/* in */
30 				UINT32 cPubInfoSize,	/* in */
31 				BYTE* pPubInfo,			/* in */
32 				TCPA_ENCAUTH encAuth,		/* in */
33 				TPM_AUTH* pAuth)		/* in,out */
34 {
35 	TSS_RESULT result;
36 
37 	initData(&hte->comm, 5);
38 	hte->comm.hdr.u.ordinal = TCSD_ORD_NVDEFINEORRELEASESPACE;
39 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
40 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
41 		return TSPERR(TSS_E_INTERNAL_ERROR);
42 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &cPubInfoSize, 0, &hte->comm))
43 		return TSPERR(TSS_E_INTERNAL_ERROR);
44 	if (setData(TCSD_PACKET_TYPE_PBYTE, 2, pPubInfo, cPubInfoSize, &hte->comm))
45 		return TSPERR(TSS_E_INTERNAL_ERROR);
46 	if (setData(TCSD_PACKET_TYPE_ENCAUTH, 3, &encAuth, 0, &hte->comm))
47 		return TSPERR(TSS_E_INTERNAL_ERROR);
48 	if( pAuth) {
49 		if (setData(TCSD_PACKET_TYPE_AUTH, 4, pAuth, 0, &hte->comm))
50 			return TSPERR(TSS_E_INTERNAL_ERROR);
51 	}
52 
53 	result = sendTCSDPacket(hte);
54 	if (result == TSS_SUCCESS)
55 		result = hte->comm.hdr.u.result;
56 
57 	if (result == TSS_SUCCESS) {
58 		LogDebugFn("getData outputSize");
59 
60 		if( pAuth) {
61 			if (getData(TCSD_PACKET_TYPE_AUTH, 0, pAuth, 0, &hte->comm)) {
62 				result = TSPERR(TSS_E_INTERNAL_ERROR);
63 				goto done;
64 			}
65 		}
66 	}
67 
68 done:
69 	LogDebugFn("result=%u", result);
70 	return result;
71 }
72 
73 TSS_RESULT
RPC_NV_WriteValue_TP(struct host_table_entry * hte,TSS_NV_INDEX hNVStore,UINT32 offset,UINT32 ulDataLength,BYTE * rgbDataToWrite,TPM_AUTH * privAuth)74 RPC_NV_WriteValue_TP(struct host_table_entry *hte,	/* in */
75 		      TSS_NV_INDEX hNVStore,		/* in */
76 		      UINT32 offset,			/* in */
77 		      UINT32 ulDataLength,		/* in */
78 		      BYTE* rgbDataToWrite,		/* in */
79 		      TPM_AUTH* privAuth)		/* in,out */
80 {
81 	TSS_RESULT result;
82 
83 	initData(&hte->comm, 6);
84 	hte->comm.hdr.u.ordinal = TCSD_ORD_NVWRITEVALUE;
85 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
86 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
87 		return TSPERR(TSS_E_INTERNAL_ERROR);
88 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &hte->comm))
89 		return TSPERR(TSS_E_INTERNAL_ERROR);
90 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &hte->comm))
91 		return TSPERR(TSS_E_INTERNAL_ERROR);
92 	if (setData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &hte->comm))
93 		return TSPERR(TSS_E_INTERNAL_ERROR);
94 	if (setData(TCSD_PACKET_TYPE_PBYTE, 4, rgbDataToWrite, ulDataLength, &hte->comm))
95 		return TSPERR(TSS_E_INTERNAL_ERROR);
96 	if( privAuth) {
97 		if (setData(TCSD_PACKET_TYPE_AUTH, 5, privAuth, 0, &hte->comm))
98 			return TSPERR(TSS_E_INTERNAL_ERROR);
99 	}
100 
101 	result = sendTCSDPacket(hte);
102 	if (result == TSS_SUCCESS)
103 		result = hte->comm.hdr.u.result;
104 
105 	if (result == TSS_SUCCESS) {
106 		LogDebugFn("getData outputSize");
107 
108 		if( privAuth) {
109 			if (getData(TCSD_PACKET_TYPE_AUTH, 0, privAuth, 0, &hte->comm)) {
110 				result = TSPERR(TSS_E_INTERNAL_ERROR);
111 				goto done;
112 			}
113 		}
114 	}
115 
116 done:
117 	LogDebugFn("result=%u", result);
118 	return result;
119 }
120 
121 TSS_RESULT
RPC_NV_WriteValueAuth_TP(struct host_table_entry * hte,TSS_NV_INDEX hNVStore,UINT32 offset,UINT32 ulDataLength,BYTE * rgbDataToWrite,TPM_AUTH * NVAuth)122 RPC_NV_WriteValueAuth_TP(struct host_table_entry *hte,	/* in */
123 			  TSS_NV_INDEX hNVStore,	/* in */
124 			  UINT32 offset,		/* in */
125 			  UINT32 ulDataLength,		/* in */
126 			  BYTE* rgbDataToWrite,		/* in */
127 			  TPM_AUTH* NVAuth)		/* in,out */
128 {
129 	TSS_RESULT result;
130 
131 	initData(&hte->comm, 6);
132 	hte->comm.hdr.u.ordinal = TCSD_ORD_NVWRITEVALUEAUTH;
133 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
134 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
135 		return TSPERR(TSS_E_INTERNAL_ERROR);
136 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &hte->comm))
137 		return TSPERR(TSS_E_INTERNAL_ERROR);
138 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &hte->comm))
139 		return TSPERR(TSS_E_INTERNAL_ERROR);
140 	if (setData(TCSD_PACKET_TYPE_UINT32, 3, &ulDataLength, 0, &hte->comm))
141 		return TSPERR(TSS_E_INTERNAL_ERROR);
142 	if (setData(TCSD_PACKET_TYPE_PBYTE, 4, rgbDataToWrite, ulDataLength, &hte->comm))
143 		return TSPERR(TSS_E_INTERNAL_ERROR);
144 	if( NVAuth) {
145 		if (setData(TCSD_PACKET_TYPE_AUTH, 5, NVAuth, 0, &hte->comm))
146 			return TSPERR(TSS_E_INTERNAL_ERROR);
147 	}
148 
149 	result = sendTCSDPacket(hte);
150 	if (result == TSS_SUCCESS)
151 		result = hte->comm.hdr.u.result;
152 
153 	if (result == TSS_SUCCESS) {
154 		LogDebugFn("getData outputSize");
155 
156 		if( NVAuth) {
157 			if (getData(TCSD_PACKET_TYPE_AUTH, 0, NVAuth, 0, &hte->comm)) {
158 				result = TSPERR(TSS_E_INTERNAL_ERROR);
159 				goto done;
160 			}
161 		}
162 	}
163 
164 done:
165 	LogDebugFn("result=%u", result);
166 	return result;
167 }
168 
169 TSS_RESULT
RPC_NV_ReadValue_TP(struct host_table_entry * hte,TSS_NV_INDEX hNVStore,UINT32 offset,UINT32 * pulDataLength,TPM_AUTH * privAuth,BYTE ** rgbDataRead)170 RPC_NV_ReadValue_TP(struct host_table_entry *hte,	/* in */
171 		     TSS_NV_INDEX hNVStore,		/* in */
172 		     UINT32 offset,			/* in */
173 		     UINT32* pulDataLength,		/* in,out */
174 		     TPM_AUTH* privAuth,		/* in,out */
175 		     BYTE** rgbDataRead)		/* out */
176 {
177 	TSS_RESULT result;
178 	UINT32 i;
179 
180 	initData(&hte->comm, 5);
181 	hte->comm.hdr.u.ordinal = TCSD_ORD_NVREADVALUE;
182 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
183 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
184 		return TSPERR(TSS_E_INTERNAL_ERROR);
185 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &hte->comm))
186 		return TSPERR(TSS_E_INTERNAL_ERROR);
187 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &hte->comm))
188 		return TSPERR(TSS_E_INTERNAL_ERROR);
189 	if (setData(TCSD_PACKET_TYPE_UINT32, 3, pulDataLength, 0, &hte->comm))
190 		return TSPERR(TSS_E_INTERNAL_ERROR);
191 	LogDebugFn("SetData privAuth\n");
192 	if( privAuth) {
193 		if (setData(TCSD_PACKET_TYPE_AUTH, 4, privAuth, 0, &hte->comm))
194 			return TSPERR(TSS_E_INTERNAL_ERROR);
195 	}
196 
197 	LogDebugFn("Send data.\n");
198 	result = sendTCSDPacket(hte);
199 	if (result == TSS_SUCCESS)
200 		result = hte->comm.hdr.u.result;
201 
202 	LogDebugFn("result=%u", result);
203 	if (result == TSS_SUCCESS) {
204 		i = 0;
205 		LogDebugFn("getData outputSize");
206 
207 		if( privAuth) {
208 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, privAuth, 0, &hte->comm)) {
209 				result = TSPERR(TSS_E_INTERNAL_ERROR);
210 				goto done;
211 			}
212 		}
213 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, pulDataLength, 0, &hte->comm)) {
214 			result = TSPERR(TSS_E_INTERNAL_ERROR);
215 			goto done;
216 		}
217 		*rgbDataRead = (BYTE *) malloc(*pulDataLength);
218 		if (*rgbDataRead == NULL) {
219 			LogError("malloc of %u bytes failed.", *pulDataLength);
220 			result = TSPERR(TSS_E_OUTOFMEMORY);
221 			goto done;
222 		}
223 		LogDebugFn("getData rgbDataRead (pulDataLength=%u)", *pulDataLength);
224 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *rgbDataRead, *pulDataLength, &hte->comm)) {
225 			free(*rgbDataRead);
226 			*rgbDataRead = NULL;
227 			result = TSPERR(TSS_E_INTERNAL_ERROR);
228 			goto done;
229 		}
230 	}
231 
232 done:
233 	LogDebugFn("result=%u", result);
234 	return result;
235 }
236 
237 TSS_RESULT
RPC_NV_ReadValueAuth_TP(struct host_table_entry * hte,TSS_NV_INDEX hNVStore,UINT32 offset,UINT32 * pulDataLength,TPM_AUTH * NVAuth,BYTE ** rgbDataRead)238 RPC_NV_ReadValueAuth_TP(struct host_table_entry *hte,	/* in */
239 			 TSS_NV_INDEX hNVStore,		/* in */
240 			 UINT32 offset,			/* in */
241 			 UINT32* pulDataLength,		/* in,out */
242 			 TPM_AUTH* NVAuth,		/* in,out */
243 			 BYTE** rgbDataRead)		/* out */
244 {
245 	TSS_RESULT result;
246 	UINT32 i;
247 
248 	initData(&hte->comm, 5);
249 	hte->comm.hdr.u.ordinal = TCSD_ORD_NVREADVALUEAUTH;
250 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
251 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
252 		return TSPERR(TSS_E_INTERNAL_ERROR);
253 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &hNVStore, 0, &hte->comm))
254 		return TSPERR(TSS_E_INTERNAL_ERROR);
255 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &offset, 0, &hte->comm))
256 		return TSPERR(TSS_E_INTERNAL_ERROR);
257 	if (setData(TCSD_PACKET_TYPE_UINT32, 3, pulDataLength, 0, &hte->comm))
258 		return TSPERR(TSS_E_INTERNAL_ERROR);
259 	if( NVAuth) {
260 		if (setData(TCSD_PACKET_TYPE_AUTH, 4, NVAuth, 0, &hte->comm))
261 			return TSPERR(TSS_E_INTERNAL_ERROR);
262 	}
263 
264 	result = sendTCSDPacket(hte);
265 	if (result == TSS_SUCCESS)
266 		result = hte->comm.hdr.u.result;
267 
268 	if (result == TSS_SUCCESS) {
269 		i = 0;
270 		LogDebugFn("getData outputSize");
271 
272 		if( NVAuth) {
273 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, NVAuth, 0, &hte->comm)) {
274 				result = TSPERR(TSS_E_INTERNAL_ERROR);
275 				goto done;
276 			}
277 		}
278 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, pulDataLength, 0, &hte->comm)) {
279 			result = TSPERR(TSS_E_INTERNAL_ERROR);
280 			goto done;
281 		}
282 		*rgbDataRead = (BYTE *) malloc(*pulDataLength);
283 		if (*rgbDataRead == NULL) {
284 			LogError("malloc of %u bytes failed.", *pulDataLength);
285 			result = TSPERR(TSS_E_OUTOFMEMORY);
286 			goto done;
287 		}
288 		LogDebugFn("getData rgbDataRead (pulDataLength=%u)", *pulDataLength);
289 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *rgbDataRead, *pulDataLength, &hte->comm)) {
290 			free(*rgbDataRead);
291 			*rgbDataRead = NULL;
292 			result = TSPERR(TSS_E_INTERNAL_ERROR);
293 			goto done;
294 		}
295 	}
296 
297 done:
298 	LogDebugFn("result=%u", result);
299 	return result;
300 }
301 
302