xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/rpc/tcstp/rpc_migration.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 <string.h>
14 #include <assert.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "capabilities.h"
21 #include "tsplog.h"
22 #include "hosttable.h"
23 #include "tcsd_wrap.h"
24 #include "obj.h"
25 #include "rpc_tcstp_tsp.h"
26 
27 
28 TSS_RESULT
RPC_CreateMigrationBlob_TP(struct host_table_entry * hte,TCS_KEY_HANDLE parentHandle,TSS_MIGRATE_SCHEME migrationType,UINT32 MigrationKeyAuthSize,BYTE * MigrationKeyAuth,UINT32 encDataSize,BYTE * encData,TPM_AUTH * parentAuth,TPM_AUTH * entityAuth,UINT32 * randomSize,BYTE ** random,UINT32 * outDataSize,BYTE ** outData)29 RPC_CreateMigrationBlob_TP(struct host_table_entry *hte,
30 					TCS_KEY_HANDLE parentHandle,	/* in */
31 					TSS_MIGRATE_SCHEME migrationType,	/* in */
32 					UINT32 MigrationKeyAuthSize,	/* in */
33 					BYTE * MigrationKeyAuth,	/* in */
34 					UINT32 encDataSize,	/* in */
35 					BYTE * encData,	/* in */
36 					TPM_AUTH * parentAuth,	/* in, out */
37 					TPM_AUTH * entityAuth,	/* in, out */
38 					UINT32 * randomSize,	/* out */
39 					BYTE ** random,	/* out */
40 					UINT32 * outDataSize,	/* out */
41 					BYTE ** outData	/* out */
42     ) {
43 	TSS_RESULT result;
44 	TPM_AUTH null_auth;
45 	UINT32 i;
46 
47 	initData(&hte->comm, 9);
48 	__tspi_memset(&null_auth, 0, sizeof(TPM_AUTH));
49 
50 	hte->comm.hdr.u.ordinal = TCSD_ORD_CREATEMIGRATIONBLOB;
51 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
52 
53 	i = 0;
54 	if (setData(TCSD_PACKET_TYPE_UINT32, i++, &hte->tcsContext, 0, &hte->comm))
55 		return TSPERR(TSS_E_INTERNAL_ERROR);
56 	if (setData(TCSD_PACKET_TYPE_UINT32, i++, &parentHandle, 0, &hte->comm))
57 		return TSPERR(TSS_E_INTERNAL_ERROR);
58 	if (setData(TCSD_PACKET_TYPE_UINT16, i++, &migrationType, 0, &hte->comm))
59 		return TSPERR(TSS_E_INTERNAL_ERROR);
60 	if (setData(TCSD_PACKET_TYPE_UINT32, i++, &MigrationKeyAuthSize, 0, &hte->comm))
61 		return TSPERR(TSS_E_INTERNAL_ERROR);
62 	if (setData(TCSD_PACKET_TYPE_PBYTE, i++, MigrationKeyAuth, MigrationKeyAuthSize, &hte->comm))
63 		return TSPERR(TSS_E_INTERNAL_ERROR);
64 	if (setData(TCSD_PACKET_TYPE_UINT32, i++, &encDataSize, 0, &hte->comm))
65 		return TSPERR(TSS_E_INTERNAL_ERROR);
66 	if (setData(TCSD_PACKET_TYPE_PBYTE, i++, encData, encDataSize, &hte->comm))
67 		return TSPERR(TSS_E_INTERNAL_ERROR);
68 
69 	if (parentAuth) {
70 		if (setData(TCSD_PACKET_TYPE_AUTH, i++, parentAuth, 0, &hte->comm))
71 			return TSPERR(TSS_E_INTERNAL_ERROR);
72 	}
73 
74 	if (setData(TCSD_PACKET_TYPE_AUTH, i++, entityAuth, 0, &hte->comm))
75 		return TSPERR(TSS_E_INTERNAL_ERROR);
76 
77 	result = sendTCSDPacket(hte);
78 
79 	if (result == TSS_SUCCESS)
80 		result = hte->comm.hdr.u.result;
81 
82 	if (result == TSS_SUCCESS) {
83 		i = 0;
84 		if (parentAuth) {
85 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, parentAuth, 0, &hte->comm)) {
86 				result = TSPERR(TSS_E_INTERNAL_ERROR);
87 				goto done;
88 			}
89 		}
90 		if (getData(TCSD_PACKET_TYPE_AUTH, i++, entityAuth, 0, &hte->comm)) {
91 			result = TSPERR(TSS_E_INTERNAL_ERROR);
92 			goto done;
93 		}
94 
95 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, randomSize, 0, &hte->comm)) {
96 			result = TSPERR(TSS_E_INTERNAL_ERROR);
97 			goto done;
98 		}
99 
100 		if (*randomSize > 0) {
101 			*random = (BYTE *)malloc(*randomSize);
102 			if (*random == NULL) {
103 				LogError("malloc of %u bytes failed.", *randomSize);
104 				result = TSPERR(TSS_E_OUTOFMEMORY);
105 				goto done;
106 			}
107 			if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *random, *randomSize, &hte->comm)) {
108 				free(*random);
109 				result = TSPERR(TSS_E_INTERNAL_ERROR);
110 				goto done;
111 			}
112 		}
113 
114 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, outDataSize, 0, &hte->comm)) {
115 			if (*randomSize > 0)
116 				free(*random);
117 			result = TSPERR(TSS_E_INTERNAL_ERROR);
118 			goto done;
119 		}
120 
121 		*outData = (BYTE *)malloc(*outDataSize);
122 		if (*outData == NULL) {
123 			if (*randomSize > 0)
124 				free(*random);
125 			LogError("malloc of %u bytes failed.", *outDataSize);
126 			result = TSPERR(TSS_E_OUTOFMEMORY);
127 			goto done;
128 		}
129 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *outData, *outDataSize, &hte->comm)) {
130 			if (*randomSize > 0)
131 				free(*random);
132 			free(*outData);
133 			result = TSPERR(TSS_E_INTERNAL_ERROR);
134 			goto done;
135 		}
136 	}
137 
138 done:
139 	return result;
140 }
141 
142 TSS_RESULT
RPC_ConvertMigrationBlob_TP(struct host_table_entry * hte,TCS_KEY_HANDLE parentHandle,UINT32 inDataSize,BYTE * inData,UINT32 randomSize,BYTE * random,TPM_AUTH * parentAuth,UINT32 * outDataSize,BYTE ** outData)143 RPC_ConvertMigrationBlob_TP(struct host_table_entry *hte,
144 					 TCS_KEY_HANDLE parentHandle,	/* in */
145 					 UINT32 inDataSize,	/* in */
146 					 BYTE * inData,	/* in */
147 					 UINT32 randomSize,	/* in */
148 					 BYTE * random,	/* in */
149 					 TPM_AUTH * parentAuth,	/* in, out */
150 					 UINT32 * outDataSize,	/* out */
151 					 BYTE ** outData	/* out */
152     ) {
153 	TSS_RESULT result;
154 	UINT32 i;
155 
156 	initData(&hte->comm, 7);
157 	hte->comm.hdr.u.ordinal = TCSD_ORD_CONVERTMIGRATIONBLOB;
158 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
159 
160 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
161 		return TSPERR(TSS_E_INTERNAL_ERROR);
162 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &parentHandle, 0, &hte->comm))
163 		return TSPERR(TSS_E_INTERNAL_ERROR);
164 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &inDataSize, 0, &hte->comm))
165 		return TSPERR(TSS_E_INTERNAL_ERROR);
166 	if (setData(TCSD_PACKET_TYPE_PBYTE, 3, inData, inDataSize, &hte->comm))
167 		return TSPERR(TSS_E_INTERNAL_ERROR);
168 	if (setData(TCSD_PACKET_TYPE_UINT32, 4, &randomSize, 0, &hte->comm))
169 		return TSPERR(TSS_E_INTERNAL_ERROR);
170 	if (setData(TCSD_PACKET_TYPE_PBYTE, 5, random, randomSize, &hte->comm))
171 		return TSPERR(TSS_E_INTERNAL_ERROR);
172 
173 	if (parentAuth) {
174 		if (setData(TCSD_PACKET_TYPE_AUTH, 6, parentAuth, 0, &hte->comm))
175 			return TSPERR(TSS_E_INTERNAL_ERROR);
176 	}
177 
178 	result = sendTCSDPacket(hte);
179 
180 	if (result == TSS_SUCCESS)
181 		result = hte->comm.hdr.u.result;
182 
183 	if (result == TSS_SUCCESS) {
184 		i = 0;
185 		if (parentAuth) {
186 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, parentAuth, 0, &hte->comm)) {
187 				result = TSPERR(TSS_E_INTERNAL_ERROR);
188 				goto done;
189 			}
190 		}
191 
192 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, outDataSize, 0, &hte->comm)) {
193 			result = TSPERR(TSS_E_INTERNAL_ERROR);
194 			goto done;
195 		}
196 
197 		*outData = (BYTE *)malloc(*outDataSize);
198 		if (*outData == NULL) {
199 			LogError("malloc of %u bytes failed.", *outDataSize);
200 			result = TSPERR(TSS_E_OUTOFMEMORY);
201 			goto done;
202 		}
203 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *outData, *outDataSize, &hte->comm)) {
204 			result = TSPERR(TSS_E_INTERNAL_ERROR);
205 			goto done;
206 		}
207 	}
208 
209 done:
210 	return result;
211 }
212 
213 TSS_RESULT
RPC_AuthorizeMigrationKey_TP(struct host_table_entry * hte,TSS_MIGRATE_SCHEME migrateScheme,UINT32 MigrationKeySize,BYTE * MigrationKey,TPM_AUTH * ownerAuth,UINT32 * MigrationKeyAuthSize,BYTE ** MigrationKeyAuth)214 RPC_AuthorizeMigrationKey_TP(struct host_table_entry *hte,
215 					  TSS_MIGRATE_SCHEME migrateScheme,	/* in */
216 					  UINT32 MigrationKeySize,	/* in */
217 					  BYTE * MigrationKey,	/* in */
218 					  TPM_AUTH * ownerAuth,	/* in, out */
219 					  UINT32 * MigrationKeyAuthSize,	/* out */
220 					  BYTE ** MigrationKeyAuth	/* out */
221     ) {
222 	TSS_RESULT result;
223 
224 	initData(&hte->comm, 5);
225 	hte->comm.hdr.u.ordinal = TCSD_ORD_AUTHORIZEMIGRATIONKEY;
226 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
227 
228 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
229 		return TSPERR(TSS_E_INTERNAL_ERROR);
230 	if (setData(TCSD_PACKET_TYPE_UINT16, 1, &migrateScheme, 0, &hte->comm))
231 		return TSPERR(TSS_E_INTERNAL_ERROR);
232 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &MigrationKeySize, 0, &hte->comm))
233 		return TSPERR(TSS_E_INTERNAL_ERROR);
234 	if (setData(TCSD_PACKET_TYPE_PBYTE, 3, MigrationKey, MigrationKeySize, &hte->comm))
235 		return TSPERR(TSS_E_INTERNAL_ERROR);
236 	if (setData(TCSD_PACKET_TYPE_AUTH, 4, ownerAuth, 0, &hte->comm))
237 		return TSPERR(TSS_E_INTERNAL_ERROR);
238 
239 	result = sendTCSDPacket(hte);
240 
241 	if (result == TSS_SUCCESS)
242 		result = hte->comm.hdr.u.result;
243 
244 	if (result == TSS_SUCCESS) {
245 		if (getData(TCSD_PACKET_TYPE_AUTH, 0, ownerAuth, 0, &hte->comm)) {
246 			result = TSPERR(TSS_E_INTERNAL_ERROR);
247 			goto done;
248 		}
249 		if (getData(TCSD_PACKET_TYPE_UINT32, 1, MigrationKeyAuthSize, 0, &hte->comm)) {
250 			result = TSPERR(TSS_E_INTERNAL_ERROR);
251 			goto done;
252 		}
253 
254 		*MigrationKeyAuth = (BYTE *)malloc(*MigrationKeyAuthSize);
255 		if (*MigrationKeyAuth == NULL) {
256 			LogError("malloc of %u bytes failed.", *MigrationKeyAuthSize);
257 			result = TSPERR(TSS_E_OUTOFMEMORY);
258 			goto done;
259 		}
260 		if (getData(TCSD_PACKET_TYPE_PBYTE, 2, *MigrationKeyAuth, *MigrationKeyAuthSize,
261 			    &hte->comm)) {
262 			free(*MigrationKeyAuth);
263 			result = TSPERR(TSS_E_INTERNAL_ERROR);
264 			goto done;
265 		}
266 	}
267 
268 done:
269 	return result;
270 }
271