xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tcs/rpc/tcstp/rpc_cmk.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. 2007
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_CMK_SetRestrictions(struct tcsd_thread_data * data)31 tcs_wrap_CMK_SetRestrictions(struct tcsd_thread_data *data)
32 {
33 	TCS_CONTEXT_HANDLE hContext;
34 	TSS_CMK_DELEGATE restriction;
35 	TPM_AUTH ownerAuth;
36 	TSS_RESULT result;
37 
38 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
39 		return TCSERR(TSS_E_INTERNAL_ERROR);
40 
41 	if ((result = ctx_verify_context(hContext)))
42 		goto done;
43 
44 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
45 
46 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &restriction, 0, &data->comm))
47 		return TCSERR(TSS_E_INTERNAL_ERROR);
48 
49 	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &ownerAuth, 0, &data->comm))
50 		return TCSERR(TSS_E_INTERNAL_ERROR);
51 
52 	MUTEX_LOCK(tcsp_lock);
53 
54 	result = TCSP_CMK_SetRestrictions_Internal(hContext, restriction, &ownerAuth);
55 
56 	MUTEX_UNLOCK(tcsp_lock);
57 
58 	if (result == TSS_SUCCESS) {
59 		initData(&data->comm, 1);
60 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
61 			return TCSERR(TSS_E_INTERNAL_ERROR);
62 	} else
63 done:		initData(&data->comm, 0);
64 
65 	data->comm.hdr.u.result = result;
66 
67 	return TSS_SUCCESS;
68 }
69 
70 TSS_RESULT
tcs_wrap_CMK_ApproveMA(struct tcsd_thread_data * data)71 tcs_wrap_CMK_ApproveMA(struct tcsd_thread_data *data)
72 {
73 	TCS_CONTEXT_HANDLE hContext;
74 	TPM_DIGEST migAuthorityDigest;
75 	TPM_AUTH ownerAuth;
76 	TPM_HMAC migAuthorityApproval;
77 	TSS_RESULT result;
78 
79 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
80 		return TCSERR(TSS_E_INTERNAL_ERROR);
81 
82 	if ((result = ctx_verify_context(hContext)))
83 		goto done;
84 
85 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
86 
87 	if (getData(TCSD_PACKET_TYPE_DIGEST, 1, &migAuthorityDigest, 0, &data->comm))
88 		return TCSERR(TSS_E_INTERNAL_ERROR);
89 
90 	if (getData(TCSD_PACKET_TYPE_AUTH, 2, &ownerAuth, 0, &data->comm))
91 		return TCSERR(TSS_E_INTERNAL_ERROR);
92 
93 	MUTEX_LOCK(tcsp_lock);
94 
95 	result = TCSP_CMK_ApproveMA_Internal(hContext, migAuthorityDigest, &ownerAuth,
96 			&migAuthorityApproval);
97 
98 	MUTEX_UNLOCK(tcsp_lock);
99 
100 	if (result == TSS_SUCCESS) {
101 		initData(&data->comm, 2);
102 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
103 			return TCSERR(TSS_E_INTERNAL_ERROR);
104 
105 		if (setData(TCSD_PACKET_TYPE_DIGEST, 1, &migAuthorityApproval, 0, &data->comm))
106 			return TCSERR(TSS_E_INTERNAL_ERROR);
107 	} else
108 done:		initData(&data->comm, 0);
109 
110 	data->comm.hdr.u.result = result;
111 
112 	return TSS_SUCCESS;
113 }
114 
115 TSS_RESULT
tcs_wrap_CMK_CreateKey(struct tcsd_thread_data * data)116 tcs_wrap_CMK_CreateKey(struct tcsd_thread_data *data)
117 {
118 	TCS_CONTEXT_HANDLE hContext;
119 	TCS_KEY_HANDLE hKey;
120 	TPM_ENCAUTH keyUsageAuth;
121 	TPM_HMAC migAuthorityApproval;
122 	TPM_DIGEST migAuthorityDigest;
123 	UINT32 keyDataSize;
124 	BYTE *keyData;
125 	TPM_AUTH parentAuth, nullAuth, *pAuth;
126 	TSS_RESULT result;
127 
128 	memset(&parentAuth, 0, sizeof(TPM_AUTH));
129 	memset(&nullAuth, 0, sizeof(TPM_AUTH));
130 
131 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
132 		return TCSERR(TSS_E_INTERNAL_ERROR);
133 
134 	if ((result = ctx_verify_context(hContext)))
135 		goto done;
136 
137 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
138 
139 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
140 		return TCSERR(TSS_E_INTERNAL_ERROR);
141 
142 	if (getData(TCSD_PACKET_TYPE_ENCAUTH, 2, &keyUsageAuth, 0, &data->comm))
143 		return TCSERR(TSS_E_INTERNAL_ERROR);
144 
145 	if (getData(TCSD_PACKET_TYPE_DIGEST, 3, &migAuthorityApproval, 0, &data->comm))
146 		return TCSERR(TSS_E_INTERNAL_ERROR);
147 
148 	if (getData(TCSD_PACKET_TYPE_DIGEST, 4, &migAuthorityDigest, 0, &data->comm))
149 		return TCSERR(TSS_E_INTERNAL_ERROR);
150 
151 	if (getData(TCSD_PACKET_TYPE_UINT32, 5, &keyDataSize, 0, &data->comm))
152 		return TCSERR(TSS_E_INTERNAL_ERROR);
153 
154 	keyData = malloc(keyDataSize);
155 	if (keyData == NULL) {
156 		LogError("malloc of %u bytes failed.", keyDataSize);
157 		return TCSERR(TSS_E_OUTOFMEMORY);
158 	}
159 	if (getData(TCSD_PACKET_TYPE_PBYTE, 6, keyData, keyDataSize, &data->comm)) {
160 		free(keyData);
161 		return TCSERR(TSS_E_INTERNAL_ERROR);
162 	}
163 
164 	if (getData(TCSD_PACKET_TYPE_AUTH, 7, &parentAuth, 0, &data->comm)) {
165 		free(keyData);
166 		return TCSERR(TSS_E_INTERNAL_ERROR);
167 	}
168 
169 	if (memcmp(&nullAuth, &parentAuth, sizeof(TPM_AUTH)))
170 		pAuth = &parentAuth;
171 	else
172 		pAuth = NULL;
173 
174 	MUTEX_LOCK(tcsp_lock);
175 
176 	result = TCSP_CMK_CreateKey_Internal(hContext, hKey, keyUsageAuth, migAuthorityApproval,
177 			migAuthorityDigest, &keyDataSize, &keyData, pAuth);
178 
179 	MUTEX_UNLOCK(tcsp_lock);
180 
181 	if (result == TSS_SUCCESS) {
182 		initData(&data->comm, 3);
183 		if (setData(TCSD_PACKET_TYPE_UINT32, 0, &keyDataSize, 0, &data->comm)) {
184 			free(keyData);
185 			return TCSERR(TSS_E_INTERNAL_ERROR);
186 		}
187 
188 		if (setData(TCSD_PACKET_TYPE_PBYTE, 1, keyData, keyDataSize, &data->comm)) {
189 			free(keyData);
190 			return TCSERR(TSS_E_INTERNAL_ERROR);
191 		}
192 		free(keyData);
193 
194 		if (pAuth) {
195 			if (setData(TCSD_PACKET_TYPE_AUTH, 2, pAuth, 0, &data->comm))
196 				return TCSERR(TSS_E_INTERNAL_ERROR);
197 		}
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_CMK_CreateTicket(struct tcsd_thread_data * data)207 tcs_wrap_CMK_CreateTicket(struct tcsd_thread_data *data)
208 {
209 	TCS_CONTEXT_HANDLE hContext;
210 	UINT32 publicVerifyKeySize;
211 	BYTE *publicVerifyKey;
212 	TPM_DIGEST signedData;
213 	UINT32 sigValueSize;
214 	BYTE *sigValue;
215 	TPM_AUTH ownerAuth;
216 	TPM_HMAC sigTicket;
217 	TSS_RESULT result;
218 
219 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
220 		return TCSERR(TSS_E_INTERNAL_ERROR);
221 
222 	if ((result = ctx_verify_context(hContext)))
223 		goto done;
224 
225 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
226 
227 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &publicVerifyKeySize, 0, &data->comm))
228 		return TCSERR(TSS_E_INTERNAL_ERROR);
229 
230 	publicVerifyKey = malloc(publicVerifyKeySize);
231 	if (publicVerifyKey == NULL) {
232 		LogError("malloc of %u bytes failed.", publicVerifyKeySize);
233 		return TCSERR(TSS_E_OUTOFMEMORY);
234 	}
235 	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, publicVerifyKey, publicVerifyKeySize, &data->comm)) {
236 		free(publicVerifyKey);
237 		return TCSERR(TSS_E_INTERNAL_ERROR);
238 	}
239 
240 	if (getData(TCSD_PACKET_TYPE_DIGEST, 3, &signedData, 0, &data->comm)) {
241 		free(publicVerifyKey);
242 		return TCSERR(TSS_E_INTERNAL_ERROR);
243 	}
244 
245 	if (getData(TCSD_PACKET_TYPE_UINT32, 4, &sigValueSize, 0, &data->comm)) {
246 		free(publicVerifyKey);
247 		return TCSERR(TSS_E_INTERNAL_ERROR);
248 	}
249 
250 	sigValue = malloc(sigValueSize);
251 	if (sigValue == NULL) {
252 		LogError("malloc of %u bytes failed.", sigValueSize);
253 		free(publicVerifyKey);
254 		return TCSERR(TSS_E_OUTOFMEMORY);
255 	}
256 	if (getData(TCSD_PACKET_TYPE_PBYTE, 5, sigValue, sigValueSize, &data->comm)) {
257 		free(publicVerifyKey);
258 		free(sigValue);
259 		return TCSERR(TSS_E_INTERNAL_ERROR);
260 	}
261 
262 	if (getData(TCSD_PACKET_TYPE_AUTH, 6, &ownerAuth, 0, &data->comm)) {
263 		free(publicVerifyKey);
264 		free(sigValue);
265 		return TCSERR(TSS_E_INTERNAL_ERROR);
266 	}
267 
268 	MUTEX_LOCK(tcsp_lock);
269 
270 	result = TCSP_CMK_CreateTicket_Internal(hContext, publicVerifyKeySize, publicVerifyKey,
271 			signedData, sigValueSize, sigValue, &ownerAuth, &sigTicket);
272 
273 	MUTEX_UNLOCK(tcsp_lock);
274 	free(publicVerifyKey);
275 	free(sigValue);
276 
277 	if (result == TSS_SUCCESS) {
278 		initData(&data->comm, 2);
279 		if (setData(TCSD_PACKET_TYPE_AUTH, 0, &ownerAuth, 0, &data->comm))
280 			return TCSERR(TSS_E_INTERNAL_ERROR);
281 
282 		if (setData(TCSD_PACKET_TYPE_DIGEST, 1, &sigTicket, 0, &data->comm))
283 			return TCSERR(TSS_E_INTERNAL_ERROR);
284 	} else
285 done:		initData(&data->comm, 0);
286 
287 	data->comm.hdr.u.result = result;
288 
289 	return TSS_SUCCESS;
290 }
291 
292 TSS_RESULT
tcs_wrap_CMK_CreateBlob(struct tcsd_thread_data * data)293 tcs_wrap_CMK_CreateBlob(struct tcsd_thread_data *data)
294 {
295 	TCS_CONTEXT_HANDLE hContext;
296 	TCS_KEY_HANDLE hKey;
297 	UINT16 migrationType;
298 	UINT32 migKeyAuthSize;
299 	BYTE *migKeyAuth;
300 	TPM_DIGEST pubSourceKeyDigest;
301 	UINT32 msaListSize, restrictTicketSize, sigTicketSize, encDataSize;
302 	BYTE *msaList, *restrictTicket, *sigTicket, *encData;
303 	TPM_AUTH parentAuth, nullAuth, *pAuth;
304 	UINT32 randomSize, outDataSize;
305 	BYTE *random, *outData;
306 	TSS_RESULT result;
307 	int i;
308 
309 	memset(&parentAuth, 0, sizeof(TPM_AUTH));
310 	memset(&nullAuth, 0, sizeof(TPM_AUTH));
311 
312 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
313 		return TCSERR(TSS_E_INTERNAL_ERROR);
314 
315 	if ((result = ctx_verify_context(hContext)))
316 		goto done;
317 
318 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
319 
320 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
321 		return TCSERR(TSS_E_INTERNAL_ERROR);
322 
323 	if (getData(TCSD_PACKET_TYPE_UINT16, 2, &migrationType, 0, &data->comm))
324 		return TCSERR(TSS_E_INTERNAL_ERROR);
325 
326 	if (getData(TCSD_PACKET_TYPE_UINT32, 3, &migKeyAuthSize, 0, &data->comm))
327 		return TCSERR(TSS_E_INTERNAL_ERROR);
328 
329 	migKeyAuth = malloc(migKeyAuthSize);
330 	if (migKeyAuth == NULL) {
331 		LogError("malloc of %u bytes failed.", migKeyAuthSize);
332 		return TCSERR(TSS_E_OUTOFMEMORY);
333 	}
334 	if (getData(TCSD_PACKET_TYPE_PBYTE, 4, migKeyAuth, migKeyAuthSize, &data->comm)) {
335 		free(migKeyAuth);
336 		return TCSERR(TSS_E_INTERNAL_ERROR);
337 	}
338 
339 	if (getData(TCSD_PACKET_TYPE_DIGEST, 5, &pubSourceKeyDigest, 0, &data->comm)) {
340 		free(migKeyAuth);
341 		return TCSERR(TSS_E_INTERNAL_ERROR);
342 	}
343 
344 	if (getData(TCSD_PACKET_TYPE_UINT32, 6, &msaListSize, 0, &data->comm)) {
345 		free(migKeyAuth);
346 		return TCSERR(TSS_E_INTERNAL_ERROR);
347 	}
348 
349 	msaList = malloc(msaListSize);
350 	if (msaList == NULL) {
351 		LogError("malloc of %u bytes failed.", msaListSize);
352 		free(migKeyAuth);
353 		return TCSERR(TSS_E_OUTOFMEMORY);
354 	}
355 	if (getData(TCSD_PACKET_TYPE_PBYTE, 7, msaList, msaListSize, &data->comm)) {
356 		free(migKeyAuth);
357 		free(msaList);
358 		return TCSERR(TSS_E_INTERNAL_ERROR);
359 	}
360 
361 	if (getData(TCSD_PACKET_TYPE_UINT32, 8, &restrictTicketSize, 0, &data->comm)) {
362 		free(migKeyAuth);
363 		free(msaList);
364 		return TCSERR(TSS_E_INTERNAL_ERROR);
365 	}
366 
367 	restrictTicket = malloc(restrictTicketSize);
368 	if (restrictTicket == NULL) {
369 		LogError("malloc of %u bytes failed.", restrictTicketSize);
370 		free(migKeyAuth);
371 		free(msaList);
372 		return TCSERR(TSS_E_OUTOFMEMORY);
373 	}
374 	if (getData(TCSD_PACKET_TYPE_PBYTE, 9, restrictTicket, restrictTicketSize, &data->comm)) {
375 		free(migKeyAuth);
376 		free(msaList);
377 		free(restrictTicket);
378 		return TCSERR(TSS_E_INTERNAL_ERROR);
379 	}
380 
381 	if (getData(TCSD_PACKET_TYPE_UINT32, 10, &sigTicketSize, 0, &data->comm)) {
382 		free(migKeyAuth);
383 		free(msaList);
384 		free(restrictTicket);
385 		return TCSERR(TSS_E_INTERNAL_ERROR);
386 	}
387 
388 	sigTicket = malloc(sigTicketSize);
389 	if (sigTicket == NULL) {
390 		LogError("malloc of %u bytes failed.", sigTicketSize);
391 		free(migKeyAuth);
392 		free(msaList);
393 		free(restrictTicket);
394 		return TCSERR(TSS_E_OUTOFMEMORY);
395 	}
396 	if (getData(TCSD_PACKET_TYPE_PBYTE, 11, sigTicket, sigTicketSize, &data->comm)) {
397 		free(migKeyAuth);
398 		free(msaList);
399 		free(restrictTicket);
400 		free(sigTicket);
401 		return TCSERR(TSS_E_INTERNAL_ERROR);
402 	}
403 
404 	if (getData(TCSD_PACKET_TYPE_UINT32, 12, &encDataSize, 0, &data->comm)) {
405 		free(migKeyAuth);
406 		free(msaList);
407 		free(restrictTicket);
408 		free(sigTicket);
409 		return TCSERR(TSS_E_INTERNAL_ERROR);
410 	}
411 
412 	encData = malloc(encDataSize);
413 	if (encData == NULL) {
414 		LogError("malloc of %u bytes failed.", encDataSize);
415 		free(migKeyAuth);
416 		free(msaList);
417 		free(restrictTicket);
418 		free(sigTicket);
419 		return TCSERR(TSS_E_OUTOFMEMORY);
420 	}
421 	if (getData(TCSD_PACKET_TYPE_PBYTE, 13, encData, encDataSize, &data->comm)) {
422 		free(migKeyAuth);
423 		free(msaList);
424 		free(restrictTicket);
425 		free(sigTicket);
426 		free(encData);
427 		return TCSERR(TSS_E_INTERNAL_ERROR);
428 	}
429 
430 	if (getData(TCSD_PACKET_TYPE_AUTH, 14, &parentAuth, 0, &data->comm)) {
431 		free(migKeyAuth);
432 		free(msaList);
433 		free(restrictTicket);
434 		free(sigTicket);
435 		free(encData);
436 		return TCSERR(TSS_E_INTERNAL_ERROR);
437 	}
438 
439 	if (memcmp(&nullAuth, &parentAuth, sizeof(TPM_AUTH)))
440 		pAuth = &parentAuth;
441 	else
442 		pAuth = NULL;
443 
444 	MUTEX_LOCK(tcsp_lock);
445 
446 	result = TCSP_CMK_CreateBlob_Internal(hContext, hKey, migrationType, migKeyAuthSize,
447 			migKeyAuth, pubSourceKeyDigest, msaListSize, msaList, restrictTicketSize,
448 			restrictTicket, sigTicketSize, sigTicket, encDataSize, encData, pAuth,
449 			&randomSize, &random, &outDataSize, &outData);
450 
451 	MUTEX_UNLOCK(tcsp_lock);
452 	free(migKeyAuth);
453 	free(msaList);
454 	free(restrictTicket);
455 	free(sigTicket);
456 	free(encData);
457 
458 	if (result == TSS_SUCCESS) {
459 		i = 0;
460 		initData(&data->comm, 5);
461 		if (pAuth) {
462 			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
463 				free(random);
464 				free(outData);
465 				return TCSERR(TSS_E_INTERNAL_ERROR);
466 			}
467 		}
468 
469 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &randomSize, 0, &data->comm)) {
470 			free(random);
471 			free(outData);
472 			return TCSERR(TSS_E_INTERNAL_ERROR);
473 		}
474 
475 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, random, randomSize, &data->comm)) {
476 			free(random);
477 			free(outData);
478 			return TCSERR(TSS_E_INTERNAL_ERROR);
479 		}
480 		free(random);
481 
482 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) {
483 			free(outData);
484 			return TCSERR(TSS_E_INTERNAL_ERROR);
485 		}
486 
487 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) {
488 			free(outData);
489 			return TCSERR(TSS_E_INTERNAL_ERROR);
490 		}
491 		free(outData);
492 	} else
493 done:		initData(&data->comm, 0);
494 
495 	data->comm.hdr.u.result = result;
496 
497 	return TSS_SUCCESS;
498 }
499 
500 TSS_RESULT
tcs_wrap_CMK_ConvertMigration(struct tcsd_thread_data * data)501 tcs_wrap_CMK_ConvertMigration(struct tcsd_thread_data *data)
502 {
503 	TCS_CONTEXT_HANDLE hContext;
504 	TCS_KEY_HANDLE hKey;
505 	TPM_CMK_AUTH restrictTicket;
506 	TPM_HMAC sigTicket;
507 	UINT32 keyDataSize, msaListSize, randomSize;
508 	BYTE *keyData, *msaList, *random;
509 	TPM_AUTH parentAuth, nullAuth, *pAuth;
510 	UINT32 outDataSize;
511 	BYTE *outData;
512 	TSS_RESULT result;
513 	int i;
514 
515 	memset(&parentAuth, 0, sizeof(TPM_AUTH));
516 	memset(&nullAuth, 0, sizeof(TPM_AUTH));
517 
518 	if (getData(TCSD_PACKET_TYPE_UINT32, 0, &hContext, 0, &data->comm))
519 		return TCSERR(TSS_E_INTERNAL_ERROR);
520 
521 	if ((result = ctx_verify_context(hContext)))
522 		goto done;
523 
524 	LogDebugFn("thread %ld context %x", THREAD_ID, hContext);
525 
526 	if (getData(TCSD_PACKET_TYPE_UINT32, 1, &hKey, 0, &data->comm))
527 		return TCSERR(TSS_E_INTERNAL_ERROR);
528 
529 	if (getData(TCSD_PACKET_TYPE_PBYTE, 2, &restrictTicket, sizeof(restrictTicket), &data->comm))
530 		return TCSERR(TSS_E_INTERNAL_ERROR);
531 
532 	if (getData(TCSD_PACKET_TYPE_DIGEST, 3, &sigTicket, 0, &data->comm))
533 		return TCSERR(TSS_E_INTERNAL_ERROR);
534 
535 	if (getData(TCSD_PACKET_TYPE_UINT32, 4, &keyDataSize, 0, &data->comm))
536 		return TCSERR(TSS_E_INTERNAL_ERROR);
537 
538 	keyData = malloc(keyDataSize);
539 	if (keyData == NULL) {
540 		LogError("malloc of %u bytes failed.", keyDataSize);
541 		return TCSERR(TSS_E_OUTOFMEMORY);
542 	}
543 	if (getData(TCSD_PACKET_TYPE_PBYTE, 5, keyData, keyDataSize, &data->comm)) {
544 		free(keyData);
545 		return TCSERR(TSS_E_INTERNAL_ERROR);
546 	}
547 
548 	if (getData(TCSD_PACKET_TYPE_UINT32, 6, &msaListSize, 0, &data->comm)) {
549 		free(keyData);
550 		return TCSERR(TSS_E_INTERNAL_ERROR);
551 	}
552 
553 	msaList = malloc(msaListSize);
554 	if (msaList == NULL) {
555 		LogError("malloc of %u bytes failed.", msaListSize);
556 		free(keyData);
557 		return TCSERR(TSS_E_OUTOFMEMORY);
558 	}
559 	if (getData(TCSD_PACKET_TYPE_PBYTE, 7, msaList, msaListSize, &data->comm)) {
560 		free(keyData);
561 		free(msaList);
562 		return TCSERR(TSS_E_INTERNAL_ERROR);
563 	}
564 
565 	if (getData(TCSD_PACKET_TYPE_UINT32, 8, &randomSize, 0, &data->comm)) {
566 		free(keyData);
567 		free(msaList);
568 		return TCSERR(TSS_E_INTERNAL_ERROR);
569 	}
570 
571 	random = malloc(randomSize);
572 	if (random == NULL) {
573 		LogError("malloc of %u bytes failed.", randomSize);
574 		free(keyData);
575 		free(msaList);
576 		return TCSERR(TSS_E_OUTOFMEMORY);
577 	}
578 	if (getData(TCSD_PACKET_TYPE_PBYTE, 9, random, randomSize, &data->comm)) {
579 		free(keyData);
580 		free(msaList);
581 		free(random);
582 		return TCSERR(TSS_E_INTERNAL_ERROR);
583 	}
584 
585 	if (getData(TCSD_PACKET_TYPE_AUTH, 10, &parentAuth, 0, &data->comm)) {
586 		free(keyData);
587 		free(msaList);
588 		free(random);
589 		return TCSERR(TSS_E_INTERNAL_ERROR);
590 	}
591 
592 	if (memcmp(&nullAuth, &parentAuth, sizeof(TPM_AUTH)))
593 		pAuth = &parentAuth;
594 	else
595 		pAuth = NULL;
596 
597 	MUTEX_LOCK(tcsp_lock);
598 
599 	result = TCSP_CMK_ConvertMigration_Internal(hContext, hKey, restrictTicket, sigTicket,
600 			keyDataSize, keyData, msaListSize, msaList, randomSize, random,
601 			pAuth, &outDataSize, &outData);
602 
603 	MUTEX_UNLOCK(tcsp_lock);
604 	free(keyData);
605 	free(msaList);
606 	free(random);
607 
608 	if (result == TSS_SUCCESS) {
609 		i = 0;
610 		initData(&data->comm, 3);
611 		if (pAuth) {
612 			if (setData(TCSD_PACKET_TYPE_AUTH, i++, pAuth, 0, &data->comm)) {
613 				free(outData);
614 				return TCSERR(TSS_E_INTERNAL_ERROR);
615 			}
616 		}
617 
618 		if (setData(TCSD_PACKET_TYPE_UINT32, i++, &outDataSize, 0, &data->comm)) {
619 			free(outData);
620 			return TCSERR(TSS_E_INTERNAL_ERROR);
621 		}
622 
623 		if (setData(TCSD_PACKET_TYPE_PBYTE, i++, outData, outDataSize, &data->comm)) {
624 			free(outData);
625 			return TCSERR(TSS_E_INTERNAL_ERROR);
626 		}
627 		free(outData);
628 	} else
629 done:		initData(&data->comm, 0);
630 
631 	data->comm.hdr.u.result = result;
632 
633 	return TSS_SUCCESS;
634 }
635 
636