ia64/xen-unstable

view tools/vtpm_manager/tcs/tcs.c @ 15923:da8b8fbe26c2

[VTPM] VTPM manager -- Fix parameter check

If vtpm_managed is started with the same vtpm instance, the following
error occurs.

++++++++++++
Loading NVM.
Sending LoadNVM command
INFO[VTSP]: Unbinding 256 bytes of data.
ERROR in VTSP_Unbind at vtsp.c:719 code: TPM_BAD_PARAMETER.
ERROR in envelope_decrypt at securestorage.c:156
code:TPM_BAD_PARAMETER.
ERROR[VTPM]: Failed to envelope decrypt data
.ERROR in VTPM_Handle_Load_NVM at securestorage.c:284 code:
TPM_BAD_PARAMETER.
ERROR[VTPM]: Failed to load NVM
.INFO[VTPM]: [VTPM Listener]: VTPM Listener waiting for messages.
Reading LoadNVM header
++++++++++++

Signed-off-by: Kouichi YASAKI <yasaki.kouichi@jp.fujitsu.com>
author kfraser@localhost.localdomain
date Tue Sep 18 15:06:20 2007 +0100 (2007-09-18)
parents 6b08035e10c5
children
line source
1 // ===================================================================
2 //
3 // Copyright (c) 2005, Intel Corp.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following
14 // disclaimer in the documentation and/or other materials provided
15 // with the distribution.
16 // * Neither the name of Intel Corporation nor the names of its
17 // contributors may be used to endorse or promote products derived
18 // from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
32 // ===================================================================
33 //
34 // tcs.c
35 //
36 // This file contains the functions that implement a TCS.
37 //
38 // ==================================================================
40 #include <stdio.h>
41 #include <string.h>
42 #include <malloc.h>
44 #include "tcg.h"
45 #include "bsg.h"
46 #include "tcs.h"
47 #include "contextmgr.h"
48 #include "tpmddl.h"
49 #include "log.h"
50 #include "hashtable.h"
51 #include "hashtable_itr.h"
53 // Static Global Vars for the TCS
54 static int TCS_m_nCount = 0;
56 #define TCPA_MAX_BUFFER_LENGTH 0x2000
58 static BYTE InBuf [TCPA_MAX_BUFFER_LENGTH];
59 static BYTE OutBuf[TCPA_MAX_BUFFER_LENGTH];
61 struct hashtable *context_ht;
63 // -------------------------- Hash table functions --------------------
65 static unsigned int hashfunc32(void *ky) {
66 return (* (UINT32 *) ky);
67 }
69 static int equals32(void *k1, void *k2) {
70 return (*(UINT32 *) k1 == *(UINT32 *) k2);
71 }
73 CONTEXT_HANDLE *LookupContext( TCS_CONTEXT_HANDLE hContext) {
74 return( (CONTEXT_HANDLE *) hashtable_search(context_ht, &hContext) );
75 }
77 // ---------------------------------------------------------------------------------
78 // Initialization/Uninitialization SubComponent API
79 // ---------------------------------------------------------------------------------
80 TPM_RESULT TCS_create() {
81 TDDL_RESULT hRes = TDDL_E_FAIL;
82 TPM_RESULT result = TPM_FAIL;
84 if (TCS_m_nCount == 0) {
85 vtpmloginfo(VTPM_LOG_TCS, "Constructing new TCS:\n");
86 hRes = TDDL_Open();
88 context_ht = create_hashtable(10, hashfunc32, equals32);
90 if ((hRes == TDDL_SUCCESS) && (context_ht != NULL)) {
91 result = TPM_SUCCESS;
92 TCS_m_nCount++;
93 } else {
94 result = TPM_IOERROR;
95 hashtable_destroy(context_ht, 1);
96 }
97 } else
98 TCS_m_nCount++;
100 return(result);
101 }
104 void TCS_destroy()
105 {
106 TCS_m_nCount--;
108 if (TCS_m_nCount == 0) {
109 vtpmloginfo(VTPM_LOG_TCS, "Destructing TCS:\n");
110 TDDL_Close();
112 struct hashtable_itr *context_itr;
113 TCS_CONTEXT_HANDLE *hContext;
115 // Close all the TCS contexts. TCS should evict keys based on this
116 if (hashtable_count(context_ht) > 0) {
117 context_itr = hashtable_iterator(context_ht);
118 do {
119 hContext = (TCS_CONTEXT_HANDLE *) hashtable_iterator_key(context_itr);
120 if (TCS_CloseContext(*hContext) != TPM_SUCCESS)
121 vtpmlogerror(VTPM_LOG_TCS, "Failed to close context %d properly.\n", *hContext);
123 } while (hashtable_iterator_advance(context_itr));
124 free(context_itr);
125 }
126 hashtable_destroy(context_ht, 1);
127 }
129 }
131 TPM_RESULT TCS_Malloc( TCS_CONTEXT_HANDLE hContext, // in
132 UINT32 MemSize, // in
133 BYTE** ppMemPtr) {// out
135 TPM_RESULT returnCode = TPM_FAIL;
136 CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
138 if (pContextHandle != NULL && ppMemPtr != NULL) {
139 *ppMemPtr = (BYTE *)AddMemBlock(pContextHandle, MemSize);
140 returnCode = TPM_SUCCESS;
141 }
143 return returnCode;
144 }
146 TPM_RESULT TCS_FreeMemory( TCS_CONTEXT_HANDLE hContext, // in
147 BYTE* pMemory) { // in
148 TPM_RESULT returnCode = TPM_FAIL;
149 CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
151 if ( (pContextHandle != NULL && pMemory != NULL) &&
152 (DeleteMemBlock(pContextHandle, pMemory) == TRUE) )
153 returnCode = TPM_SUCCESS;
156 return returnCode;
157 }
159 TPM_RESULT TCS_OpenContext(TCS_CONTEXT_HANDLE* hContext) { // out
160 TPM_RESULT returnCode = TPM_FAIL;
161 TCS_CONTEXT_HANDLE *newContext;
163 vtpmloginfo(VTPM_LOG_TCS, "Calling TCS_OpenContext:\n");
165 if (hContext) {
166 CONTEXT_HANDLE* pContextHandle = (CONTEXT_HANDLE *) malloc(sizeof(CONTEXT_HANDLE));
167 if (pContextHandle == NULL)
168 return TPM_SIZE;
170 // initialize to 0
171 pContextHandle->nBlockCount = 0;
172 pContextHandle->pTopBlock = NULL;
173 pContextHandle->pHandleList = NULL;
175 // Create New Block
176 AddMemBlock(pContextHandle, BLOCK_SIZE);
178 newContext = (TCS_CONTEXT_HANDLE *) malloc(sizeof(TCS_CONTEXT_HANDLE));
179 *newContext = (TCS_CONTEXT_HANDLE) (((uintptr_t) pContextHandle >> 2) & 0xffffffff);
181 if (hashtable_search(context_ht, &newContext) !=NULL)
182 *newContext += 1;
184 pContextHandle->handle = *newContext;
185 if (!hashtable_insert(context_ht, newContext, pContextHandle)) {
186 free(newContext);
187 free(pContextHandle);
188 returnCode = TPM_FAIL;
189 } else {
190 *hContext = *newContext;
191 returnCode = TPM_SUCCESS;
192 }
193 }
195 return(returnCode);
196 }
198 TPM_RESULT TCS_CloseContext(TCS_CONTEXT_HANDLE hContext) {// in
199 //FIXME: TCS SHOULD Track failed auths and make sure
200 //we don't try and re-free them here.
201 TPM_RESULT returnCode = TPM_FAIL;
203 CONTEXT_HANDLE* pContextHandle = LookupContext(hContext);
205 if(pContextHandle != NULL) {
206 // Print test info
207 vtpmloginfo(VTPM_LOG_TCS, "Calling TCS_CloseContext.\n");
209 // free memory for all the blocks
210 DeleteMemBlock(pContextHandle, NULL );
211 pContextHandle->pTopBlock = NULL;
213 FreeHandleList(pContextHandle);
214 if (pContextHandle->pHandleList != NULL)
215 vtpmlogerror(VTPM_LOG_TCS, "Not all handles evicted from TPM.\n");
217 // Release the TPM's resources
218 if (hashtable_remove(context_ht, &hContext) == NULL)
219 vtpmlogerror(VTPM_LOG_TCS, "Not all handles evicted from TPM.\n");
221 free(pContextHandle);
222 returnCode = TPM_SUCCESS;
223 }
225 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Finished closing context\n");
226 return(returnCode);
227 }
229 // ------------------------------------------------------------------
230 // Internal Functions
231 // ------------------------------------------------------------------
232 int packAuth(BYTE* dst, TCS_AUTH* auth) {
233 // CHECK: according to the command specs, the outgoing auth params are:
234 // nonceEven
235 // nonceOdd
236 // continueAuthSession
237 // auth digest for return params
238 //
239 // this is a bit different than this code...
241 return BSG_PackList(dst, 4,
242 BSG_TYPE_UINT32, &(auth->AuthHandle),
243 BSG_TPM_NONCE, &(auth->NonceOdd),
244 BSG_TYPE_BOOL, &(auth->fContinueAuthSession),
245 BSG_TPM_AUTHDATA, &(auth->HMAC));
246 }
248 int unpackAuth(TCS_AUTH* auth, BYTE* src) {
249 return BSG_UnpackList(src, 3,
250 BSG_TPM_NONCE, &(auth->NonceEven),
251 BSG_TYPE_BOOL, &(auth->fContinueAuthSession),
252 BSG_TPM_AUTHDATA, &(auth->HMAC));
253 }
255 // ------------------------------------------------------------------
256 // Authorization Commands
257 // ------------------------------------------------------------------
259 TPM_RESULT TCSP_OIAP(TCS_CONTEXT_HANDLE hContext, // in
260 TCS_AUTHHANDLE* authHandle, // out
261 TPM_NONCE* nonce0) // out
262 {
263 // setup input/output parameters block
264 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
265 TPM_COMMAND_CODE ordinal = TPM_ORD_OIAP;
266 UINT32 paramSize = 0;
267 TPM_RESULT returnCode = TPM_SUCCESS;
269 // setup the TPM driver input and output buffers
270 TDDL_RESULT hRes = TDDL_E_FAIL;
271 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
272 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
274 // check input params
275 if (authHandle == NULL || nonce0 == NULL)
276 return TPM_BAD_PARAMETER;
278 // Convert Byte Input parameter in the input byte stream InBuf
279 InLength = BSG_PackList(InBuf, 3,
280 BSG_TPM_TAG, &tag,
281 BSG_TYPE_UINT32, &paramSize,
282 BSG_TPM_COMMAND_CODE, &ordinal);
284 // fill paramSize again as we now have the correct size
285 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
287 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
289 // call the TPM driver
290 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
291 == TDDL_SUCCESS) {
293 // unpack to get the tag, paramSize, & returnCode
294 int i = BSG_UnpackList( OutBuf, 3,
295 BSG_TPM_TAG, &tag,
296 BSG_TYPE_UINT32, &paramSize,
297 BSG_TPM_COMMAND_CODE, &returnCode);
299 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
300 // Extract the remaining output parameters
301 BSG_UnpackList(OutBuf+i, 2,
302 BSG_TYPE_UINT32, authHandle,
303 BSG_TPM_NONCE, nonce0);
305 if (!AddHandleToList(hContext, TPM_RT_AUTH, *authHandle))
306 vtpmlogerror(VTPM_LOG_TCS, "New AuthHandle not recorded\n");
308 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
309 } else
310 vtpmlogerror(VTPM_LOG_TCS, "Failed with return code %s\n", tpm_get_error_name(returnCode));
312 }
314 return(returnCode);
315 }
317 TPM_RESULT TCSP_OSAP(TCS_CONTEXT_HANDLE hContext, // in
318 TPM_ENTITY_TYPE entityType, // in
319 UINT32 entityValue, // in
320 TPM_NONCE nonceOddOSAP, // in
321 TCS_AUTHHANDLE* authHandle, // out
322 TPM_NONCE* nonceEven, // out
323 TPM_NONCE* nonceEvenOSAP) // out
324 {
325 // setup input/output parameters block
326 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
327 UINT32 paramSize = 0;
328 TPM_COMMAND_CODE ordinal = TPM_ORD_OSAP;
329 TPM_RESULT returnCode = TPM_SUCCESS;
331 // setup the TPM driver input and output buffers
332 TDDL_RESULT hRes = TDDL_E_FAIL;
333 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
334 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
336 // check input params
337 if (authHandle == NULL || nonceEven == NULL || nonceEvenOSAP == NULL)
338 return TPM_BAD_PARAMETER;
340 // Convert Byte Input parameter in the input byte stream InBuf
341 InLength = BSG_PackList(InBuf, 6,
342 BSG_TPM_TAG, &tag,
343 BSG_TYPE_UINT32, &paramSize,
344 BSG_TPM_COMMAND_CODE, &ordinal,
345 BSG_TYPE_UINT16, &entityType,
346 BSG_TYPE_UINT32, &entityValue,
347 BSG_TPM_NONCE, &nonceOddOSAP);
349 // fill paramSize again as we now have the correct size
350 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
352 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
354 // call the TPM driver
355 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
356 == TDDL_SUCCESS) {
358 // unpack to get the tag, paramSize, & returnCode
359 int i = BSG_UnpackList(OutBuf, 3,
360 BSG_TPM_TAG, &tag,
361 BSG_TYPE_UINT32, &paramSize,
362 BSG_TPM_COMMAND_CODE, &returnCode);
364 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
365 // Extract the remaining output parameters
366 BSG_UnpackList(OutBuf+i, 3,
367 BSG_TYPE_UINT32, authHandle,
368 BSG_TPM_NONCE, nonceEven,
369 BSG_TPM_NONCE, nonceEvenOSAP);
371 if (!AddHandleToList(hContext, TPM_RT_AUTH, *authHandle)) {
372 vtpmlogerror(VTPM_LOG_TCS, "New AuthHandle not recorded\n");
373 }
375 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
376 } else
377 vtpmlogerror(VTPM_LOG_TCS, "Failed with return code %s\n", tpm_get_error_name(returnCode));
379 }
381 return(returnCode);
382 }
384 TPM_RESULT TCSP_TakeOwnership(TCS_CONTEXT_HANDLE hContext, // in
385 UINT16 protocolID, // in
386 UINT32 encOwnerAuthSize, // in
387 BYTE* encOwnerAuth, // in
388 UINT32 encSrkAuthSize, // in
389 BYTE* encSrkAuth, // in
390 UINT32* SrkSize, // in, out
391 BYTE** Srk, // in, out
392 TCS_AUTH* ownerAuth) // in, out
393 {
394 // setup input/output parameters block
395 TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;
396 UINT32 paramSize = 0;
397 TPM_COMMAND_CODE ordinal = TPM_ORD_TakeOwnership;
398 TPM_RESULT returnCode = TPM_SUCCESS;
400 // setup the TPM driver input and output buffers
401 TDDL_RESULT hRes = TDDL_E_FAIL;
402 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
403 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
405 // check input params
406 if (encOwnerAuth == NULL || encSrkAuth == NULL || SrkSize == NULL || *Srk == NULL)
407 return TPM_BAD_PARAMETER;
409 // Convert Byte Input parameter in the input byte stream InBuf
410 InLength = BSG_PackList(InBuf, 5,
411 BSG_TPM_TAG, &tag,
412 BSG_TYPE_UINT32, &paramSize,
413 BSG_TPM_COMMAND_CODE, &ordinal,
414 BSG_TYPE_UINT16, &protocolID,
415 BSG_TYPE_UINT32, &encOwnerAuthSize);
417 memcpy(InBuf+InLength, encOwnerAuth, encOwnerAuthSize);
418 InLength += encOwnerAuthSize;
419 InLength += BSG_Pack( BSG_TYPE_UINT32,
420 &encSrkAuthSize,
421 InBuf+InLength);
422 memcpy(InBuf+InLength, encSrkAuth, encSrkAuthSize);
423 InLength += encSrkAuthSize;
424 memcpy(InBuf+InLength, *Srk, *SrkSize);
425 InLength += *SrkSize;
426 InLength += packAuth(InBuf+InLength, ownerAuth);
427 // fill paramSize again as we now have the correct size
428 BSG_Pack(BSG_TYPE_UINT32,
429 &InLength,
430 InBuf+2);
432 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
434 // call the TPM driver
435 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
436 == TDDL_SUCCESS){
438 // unpack to get the tag, paramSize, & returnCode
439 int i = BSG_UnpackList( OutBuf, 3,
440 BSG_TPM_TAG, &tag,
441 BSG_TYPE_UINT32, &paramSize,
442 BSG_TPM_COMMAND_CODE, &returnCode);
444 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH1_COMMAND) {
445 // Extract the remaining output parameters
446 TPM_KEY srkPub;
447 i += BSG_Unpack(BSG_TPM_KEY, OutBuf+i, &srkPub);
448 unpackAuth(ownerAuth, OutBuf+i);
450 // fill output params
451 BYTE tempBuf[1024];
452 *SrkSize = BSG_Pack(BSG_TPM_KEY, &srkPub, tempBuf);
453 if (TCS_Malloc(hContext, *SrkSize, Srk) == TPM_FAIL) {
454 return(TPM_SIZE);
455 }
456 memcpy(*Srk, tempBuf, *SrkSize);
458 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d", paramSize);
459 } else
460 vtpmlogerror(VTPM_LOG_TCS, "TCSP_TakeOwnership Failed with return code %s\n", tpm_get_error_name(returnCode));
461 }
463 return(returnCode);
464 }
467 TPM_RESULT TCSP_DisablePubekRead ( TCS_CONTEXT_HANDLE hContext, // in
468 TCS_AUTH* ownerAuth) { // in, out
470 // setup input/output parameters block
471 TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;
472 UINT32 paramSize = 0;
473 TPM_COMMAND_CODE ordinal = TPM_ORD_DisablePubekRead;
474 TPM_RESULT returnCode = TPM_SUCCESS;
476 // setup the TPM driver input and output buffers
477 TDDL_RESULT hRes = TDDL_E_FAIL;
478 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
479 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
481 // Convert Byte Input parameter in the input byte stream InBuf
482 InLength = BSG_PackList(InBuf, 3,
483 BSG_TPM_TAG, &tag,
484 BSG_TYPE_UINT32, &paramSize,
485 BSG_TPM_COMMAND_CODE, &ordinal);
487 InLength += packAuth(InBuf+InLength, ownerAuth);
489 // fill paramSize again as we now have the correct size
490 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
492 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
494 // call the TPM driver
495 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
496 == TDDL_SUCCESS){
498 // unpack to get the tag, paramSize, & returnCode
499 int i = BSG_UnpackList( OutBuf, 3,
500 BSG_TPM_TAG, &tag,
501 BSG_TYPE_UINT32, &paramSize,
502 BSG_TPM_COMMAND_CODE, &returnCode);
504 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH1_COMMAND) {
505 // Extract the remaining output parameters
506 unpackAuth(ownerAuth, OutBuf+i);
507 } else
508 vtpmlogerror(VTPM_LOG_TCS, "TCSP_DisablePubekRead Failed with return code %s\n", tpm_get_error_name(returnCode));
509 }
511 return(returnCode);
512 }
515 TPM_RESULT TCSP_TerminateHandle(TCS_CONTEXT_HANDLE hContext, // in
516 TCS_AUTHHANDLE handle) // in
517 {
518 // setup input/output parameters block
519 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
520 UINT32 paramSize = 0;
521 TPM_COMMAND_CODE ordinal = TPM_ORD_Terminate_Handle;
522 TPM_RESULT returnCode = TPM_SUCCESS;
524 // setup the TPM driver input and output buffers
525 TDDL_RESULT hRes = TDDL_E_FAIL;
526 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
527 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
529 // Convert Byte Input parameter in the input byte stream InBuf
530 InLength = BSG_PackList(InBuf, 4,
531 BSG_TPM_TAG, &tag,
532 BSG_TYPE_UINT32, &paramSize,
533 BSG_TPM_COMMAND_CODE, &ordinal,
534 BSG_TYPE_UINT32, &handle);
535 // fill paramSize again as we now have the correct size
536 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
538 // call the TPM driver
539 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
540 == TDDL_SUCCESS) {
542 // unpack to get the tag, paramSize, & returnCode
543 BSG_UnpackList(OutBuf, 3,
544 BSG_TPM_TAG, &tag,
545 BSG_TYPE_UINT32, &paramSize,
546 BSG_TPM_COMMAND_CODE, &returnCode);
548 if (!DeleteHandleFromList(hContext, handle))
549 vtpmlogerror(VTPM_LOG_TCS, "KeyHandle not removed from list\n");
552 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
553 // Print debug info
555 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d", paramSize);
556 } else
557 vtpmlogerror(VTPM_LOG_TCS, "TCSP_TerminateHandle Failed with return code %s\n", tpm_get_error_name(returnCode));
559 }
561 return(returnCode);
562 }
564 // TPM Mandatory
565 TPM_RESULT TCSP_Extend( TCS_CONTEXT_HANDLE hContext, // in
566 TPM_PCRINDEX pcrNum, // in
567 TPM_DIGEST inDigest, // in
568 TPM_PCRVALUE* outDigest) // out
569 {
570 // setup input/output parameters block
571 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
572 UINT32 paramSize = 0;
573 TPM_COMMAND_CODE ordinal = TPM_ORD_Extend;
574 TPM_RESULT returnCode = TPM_SUCCESS;
576 // setup the TPM driver input and output buffers
577 TDDL_RESULT hRes = TDDL_E_FAIL;
578 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
579 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
581 // Convert Byte Input parameter in the input byte stream InBuf
582 InLength = BSG_PackList(InBuf, 5,
583 BSG_TPM_TAG, &tag,
584 BSG_TYPE_UINT32, &paramSize,
585 BSG_TPM_COMMAND_CODE, &ordinal,
586 BSG_TYPE_UINT32, &pcrNum,
587 BSG_TPM_DIGEST, &inDigest);
588 // fill paramSize again as we now have the correct size
589 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
591 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
593 // call the TPM driver
594 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
595 == TDDL_SUCCESS) {
597 // unpack to get the tag, paramSize, & returnCode
598 int i = BSG_UnpackList(OutBuf, 3,
599 BSG_TPM_TAG, &tag,
600 BSG_TYPE_UINT32, &paramSize,
601 BSG_TPM_COMMAND_CODE, &returnCode);
603 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND){
604 // Extract the remaining output parameters
605 BSG_Unpack(BSG_TPM_PCRVALUE, OutBuf+i, outDigest);
607 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
608 } else
609 vtpmlogerror(VTPM_LOG_TCS, "TCSP_Extend Failed with return code %s\n", tpm_get_error_name(returnCode));
610 }
612 return(returnCode);
613 }
615 TPM_RESULT TCSP_Seal( TCS_CONTEXT_HANDLE hContext, // in
616 TCS_KEY_HANDLE keyHandle, // in
617 TPM_ENCAUTH encAuth, // in
618 UINT32 pcrInfoSize, // in
619 BYTE* PcrInfo, // in
620 UINT32 inDataSize, // in
621 BYTE* inData, // in
622 TCS_AUTH* pubAuth, // in, out
623 UINT32* SealedDataSize, // out
624 BYTE** SealedData) // out
625 {
626 // setup input/output parameters block
627 TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;
628 UINT32 paramSize = 0;
629 TPM_COMMAND_CODE ordinal = TPM_ORD_Seal;
630 TPM_RESULT returnCode = TPM_SUCCESS;
632 // setup the TPM driver input and output buffers
633 TDDL_RESULT hRes = TDDL_E_FAIL;
634 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
635 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
637 // check input params
638 if (inData == NULL || pubAuth == NULL || SealedDataSize == NULL || SealedData == NULL)
639 return TPM_BAD_PARAMETER;
641 // Convert Byte Input parameter in the input byte stream InBuf
642 InLength = BSG_PackList(InBuf, 6,
643 BSG_TPM_TAG, &tag,
644 BSG_TYPE_UINT32, &paramSize,
645 BSG_TPM_COMMAND_CODE, &ordinal,
646 BSG_TYPE_UINT32, &keyHandle,
647 BSG_TPM_ENCAUTH, encAuth,
648 BSG_TYPE_UINT32, &pcrInfoSize);
649 memcpy(InBuf+InLength, PcrInfo, pcrInfoSize);
650 InLength += pcrInfoSize;
651 InLength += BSG_Pack(BSG_TYPE_UINT32, &inDataSize, InBuf+InLength);
652 memcpy(InBuf+InLength, inData, inDataSize);
653 InLength += inDataSize;
654 InLength += packAuth(InBuf+InLength, pubAuth);
655 // fill paramSize again as we now have the correct size
656 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
658 // call the TPM driver
659 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength))
660 == TDDL_SUCCESS) {
661 // unpack OutBuf to get the tag, paramSize, & returnCode
662 int i = BSG_UnpackList(OutBuf, 3,
663 BSG_TPM_TAG, &tag,
664 BSG_TYPE_UINT32, &paramSize,
665 BSG_TPM_COMMAND_CODE, &returnCode);
667 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH1_COMMAND) {
668 // Extract the remaining output parameters
669 TPM_STORED_DATA sealedData;
671 i += BSG_Unpack(BSG_TPM_STORED_DATA, OutBuf+i, &sealedData);
672 unpackAuth(pubAuth, OutBuf+i);
674 // fill SealedData
675 BYTE tempBuf[1024];
676 *SealedDataSize = BSG_Pack(BSG_TPM_STORED_DATA, &sealedData, tempBuf);
677 if (TCS_Malloc(hContext, *SealedDataSize, SealedData) == TPM_FAIL) {
678 return TPM_SIZE;
679 }
680 memcpy(*SealedData, tempBuf, *SealedDataSize);
682 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
683 } else
684 vtpmlogerror(VTPM_LOG_TCS, "TCSP_Seal Failed with return code %s\n", tpm_get_error_name(returnCode));
685 }
687 return(returnCode);
688 }
690 TPM_RESULT TCSP_Unseal(TCS_CONTEXT_HANDLE hContext, // in
691 TCS_KEY_HANDLE parentHandle, // in
692 UINT32 SealedDataSize, // in
693 BYTE* SealedData, // in
694 TCS_AUTH* parentAuth, // in, out
695 TCS_AUTH* dataAuth, // in, out
696 UINT32* DataSize, // out
697 BYTE** Data) // out
698 {
699 // setup input/output parameters block
700 TPM_TAG tag = TPM_TAG_RQU_AUTH2_COMMAND;
701 UINT32 paramSize = 0;
702 TPM_COMMAND_CODE ordinal = TPM_ORD_Unseal;
703 TPM_RESULT returnCode = TPM_SUCCESS;
705 // setup the TPM driver input and output buffers
706 TDDL_RESULT hRes = TDDL_E_FAIL;
707 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
708 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
710 // check input params
711 if (SealedData == NULL || parentAuth == NULL || dataAuth == NULL ||
712 DataSize == NULL || Data == NULL)
713 return TPM_BAD_PARAMETER;
716 // Convert Byte Input parameter in the input byte stream InBuf
717 InLength = BSG_PackList(InBuf, 4,
718 BSG_TPM_TAG, &tag,
719 BSG_TYPE_UINT32, &paramSize,
720 BSG_TPM_COMMAND_CODE, &ordinal,
721 BSG_TYPE_UINT32, &parentHandle);
722 memcpy(InBuf+InLength, SealedData, SealedDataSize);
723 InLength += SealedDataSize;
724 InLength += packAuth(InBuf+InLength, parentAuth);
725 InLength += packAuth(InBuf+InLength, dataAuth);
726 // fill paramSize again as we now have the correct size
727 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
729 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
731 // call the TPM driver
732 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
733 // unpack OutBuf to get the tag, paramSize, & returnCode
734 int i = BSG_UnpackList( OutBuf, 3,
735 BSG_TPM_TAG, &tag,
736 BSG_TYPE_UINT32, &paramSize,
737 BSG_TPM_COMMAND_CODE, &returnCode);
739 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH2_COMMAND) {
740 // Extract the remaining output parameters
741 i += BSG_Unpack(BSG_TYPE_UINT32, OutBuf+i, DataSize);
742 if (TCS_Malloc(hContext, *DataSize, Data) == TPM_FAIL) {
743 return TPM_SIZE;
744 }
745 memcpy(*Data, OutBuf+i, *DataSize);
746 i += *DataSize;
747 i += unpackAuth(parentAuth, OutBuf+i);
748 unpackAuth(dataAuth, OutBuf+i);
750 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
751 } else
752 vtpmlogerror(VTPM_LOG_TCS, "TCSP_Unseal Failed with return code %s\n", tpm_get_error_name(returnCode));
753 }
755 return(returnCode);
756 }
758 TPM_RESULT TCSP_UnBind(TCS_CONTEXT_HANDLE hContext, // in
759 TCS_KEY_HANDLE keyHandle, // in
760 UINT32 inDataSize, // in
761 BYTE* inData, // in
762 TCS_AUTH* privAuth, // in, out
763 UINT32* outDataSize, // out
764 BYTE** outData) // out
765 {
766 // setup input/output parameters block
767 TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;
768 UINT32 paramSize = 0;
769 TPM_COMMAND_CODE ordinal = TPM_ORD_UnBind;
770 TPM_RESULT returnCode = TPM_SUCCESS;
772 // setup the TPM driver input and output buffers
773 TDDL_RESULT hRes = TDDL_E_FAIL;
774 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
775 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
777 // check input params
778 if (inData == NULL || privAuth == NULL || outDataSize == NULL || outData == NULL)
779 return TPM_BAD_PARAMETER;
781 // Convert Byte Input parameter in the input byte stream InBuf
782 InLength = BSG_PackList(InBuf, 5,
783 BSG_TPM_TAG, &tag,
784 BSG_TYPE_UINT32, &paramSize,
785 BSG_TPM_COMMAND_CODE, &ordinal,
786 BSG_TYPE_UINT32, &keyHandle,
787 BSG_TYPE_UINT32, &inDataSize);
788 memcpy(InBuf+InLength, inData, inDataSize);
789 InLength += inDataSize;
790 InLength += packAuth(InBuf+InLength, privAuth);
791 // fill paramSize again as we now have the correct size
792 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
794 vtpmloginfo(VTPM_LOG_TCS_DEEP, "\n\tSending paramSize = %d", InLength);
796 // call the TPM driver
797 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
798 // unpack OutBuf to get the tag, paramSize, & returnCode
799 int i = BSG_UnpackList(OutBuf, 3,
800 BSG_TPM_TAG, &tag,
801 BSG_TYPE_UINT32, &paramSize,
802 BSG_TPM_COMMAND_CODE, &returnCode);
804 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH1_COMMAND) {
805 // Extract the remaining output parameters
806 i += BSG_Unpack(BSG_TYPE_UINT32, OutBuf+i, outDataSize);
807 if (TCS_Malloc(hContext, *outDataSize, outData) == TPM_FAIL)
808 return TPM_SIZE;
810 memcpy(*outData, OutBuf+i, *outDataSize);
811 i += *outDataSize;
812 unpackAuth(privAuth, OutBuf+i);
814 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
815 } else
816 vtpmlogerror(VTPM_LOG_TCS, "TCSP_UnBind Failed with return code %s\n", tpm_get_error_name(returnCode));
817 }
819 return(returnCode);
820 }
822 TPM_RESULT TCSP_CreateWrapKey(TCS_CONTEXT_HANDLE hContext, // in
823 TCS_KEY_HANDLE hWrappingKey, // in
824 TPM_ENCAUTH KeyUsageAuth, // in
825 TPM_ENCAUTH KeyMigrationAuth, // in
826 UINT32* pcKeySize, // in, out
827 BYTE** prgbKey, // in, out
828 TCS_AUTH* pAuth) // in, out
829 {
830 // setup input/output parameters block
831 TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;
832 UINT32 paramSize = 0;
833 TPM_COMMAND_CODE ordinal = TPM_ORD_CreateWrapKey;
834 TPM_RESULT returnCode = TPM_SUCCESS;
836 // setup the TPM driver input and output buffers
837 TDDL_RESULT hRes = TDDL_E_FAIL;
838 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
839 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
841 // check input params
842 if (pcKeySize == NULL || *prgbKey == NULL || pAuth == NULL)
843 return TPM_BAD_PARAMETER;
846 // Convert Byte Input parameter in the input byte stream InBuf
847 InLength = BSG_PackList(InBuf, 6,
848 BSG_TPM_TAG, &tag,
849 BSG_TYPE_UINT32, &paramSize,
850 BSG_TPM_COMMAND_CODE, &ordinal,
851 BSG_TYPE_UINT32, &hWrappingKey,
852 BSG_TPM_ENCAUTH, KeyUsageAuth,
853 BSG_TPM_ENCAUTH, KeyMigrationAuth);
854 memcpy(InBuf+InLength, *prgbKey, *pcKeySize);
855 InLength += *pcKeySize;
856 InLength += packAuth(InBuf+InLength, pAuth);
857 // fill paramSize again as we now have the correct size
858 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
860 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
862 // call the TPM driver
863 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
864 // unpack OutBuf to get the tag, paramSize, & returnCode
865 int i = BSG_UnpackList(OutBuf, 3,
866 BSG_TPM_TAG, &tag,
867 BSG_TYPE_UINT32, &paramSize,
868 BSG_TPM_RESULT, &returnCode);
870 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH1_COMMAND) {
871 // Extract the remaining output parameters
872 TPM_KEY wrappedKey;
874 i += BSG_Unpack(BSG_TPM_KEY, OutBuf+i, &wrappedKey);
875 unpackAuth(pAuth, OutBuf+i);
877 // Fill prgbKey
878 BYTE tempBuf[1024];
879 *pcKeySize = BSG_Pack(BSG_TPM_KEY, &wrappedKey, tempBuf);
880 if (TCS_Malloc(hContext, *pcKeySize, prgbKey) == TPM_FAIL)
881 return TPM_SIZE;
883 memcpy(*prgbKey, tempBuf, *pcKeySize);
885 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
886 } else
887 vtpmlogerror(VTPM_LOG_TCS, "TCSP_CreateWrapKey Failed with return code %s\n", tpm_get_error_name(returnCode));
888 }
890 return(returnCode);
891 }
893 TPM_RESULT TCSP_LoadKeyByBlob(TCS_CONTEXT_HANDLE hContext, // in
894 TCS_KEY_HANDLE hUnwrappingKey, // in
895 UINT32 cWrappedKeyBlobSize, // in
896 BYTE* rgbWrappedKeyBlob, // in
897 TCS_AUTH* pAuth, // in, out
898 TCS_KEY_HANDLE* phKeyTCSI, // out
899 TCS_KEY_HANDLE* phKeyHMAC) // out
900 {
901 // setup input/output parameters block
902 TPM_TAG tag = TPM_TAG_RQU_AUTH1_COMMAND;
903 UINT32 paramSize = 0;
904 TPM_COMMAND_CODE ordinal = TPM_ORD_LoadKey;
905 TPM_RESULT returnCode = TPM_SUCCESS;
907 // setup the TPM driver input and output buffers
908 TDDL_RESULT hRes = TDDL_E_FAIL;
909 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
910 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
912 // check input params
913 if (rgbWrappedKeyBlob == NULL || pAuth == NULL || phKeyTCSI == NULL || phKeyHMAC == NULL)
914 return TPM_BAD_PARAMETER;
916 *phKeyHMAC = hUnwrappingKey; // the parent key is the one that the TPM use to make the HMAC calc
918 // Convert Byte Input parameter in the input byte stream InBuf
919 InLength = BSG_PackList(InBuf, 4,
920 BSG_TPM_TAG, &tag,
921 BSG_TYPE_UINT32, &paramSize,
922 BSG_TPM_COMMAND_CODE, &ordinal,
923 BSG_TYPE_UINT32, &hUnwrappingKey);
924 memcpy(InBuf+InLength, rgbWrappedKeyBlob, cWrappedKeyBlobSize);
925 InLength += cWrappedKeyBlobSize;
926 InLength += packAuth(InBuf+InLength, pAuth);
927 // fill paramSize again as we now have the correct size
928 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
930 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
932 // call the TPM driver
933 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
934 // unpack OutBuf to get the tag, paramSize, & returnCode
935 int i = BSG_UnpackList(OutBuf, 3,
936 BSG_TPM_TAG, &tag,
937 BSG_TYPE_UINT32, &paramSize,
938 BSG_TPM_COMMAND_CODE, &returnCode);
940 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_AUTH1_COMMAND) {
941 // Extract the remaining output parameters
942 i += BSG_Unpack(BSG_TYPE_UINT32,
943 OutBuf+i,
944 phKeyTCSI);
945 unpackAuth(pAuth, OutBuf+i);
947 if (!AddHandleToList(hContext, TPM_RT_KEY, *phKeyTCSI)) {
948 vtpmlogerror(VTPM_LOG_TCS, "New KeyHandle not recorded\n");
949 }
951 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
952 } else
953 vtpmlogerror(VTPM_LOG_TCS, "TCSP_LoadKeyByBlob Failed with return code %s\n", tpm_get_error_name(returnCode));
954 }
956 return(returnCode);
957 }
959 TPM_RESULT TCSP_EvictKey(TCS_CONTEXT_HANDLE hContext, // in
960 TCS_KEY_HANDLE hKey) // in
961 {
962 // setup input/output parameters block
963 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
964 UINT32 paramSize = 0;
965 TPM_COMMAND_CODE ordinal = TPM_ORD_EvictKey;
966 TPM_RESULT returnCode = TPM_SUCCESS;
968 // setup the TPM driver input and output buffers
969 TDDL_RESULT hRes = TDDL_E_FAIL;
970 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
971 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
973 // Convert Byte Input parameter in the input byte stream InBuf
974 InLength = BSG_PackList(InBuf, 4,
975 BSG_TPM_TAG, &tag,
976 BSG_TYPE_UINT32, &paramSize,
977 BSG_TPM_COMMAND_CODE, &ordinal,
978 BSG_TYPE_UINT32, &hKey);
979 // fill paramSize again as we now have the correct size
980 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
982 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
984 // call the TPM driver
985 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
986 // unpack OutBuf to get the tag, paramSize, & returnCode
987 BSG_UnpackList(OutBuf, 3,
988 BSG_TPM_TAG, &tag,
989 BSG_TYPE_UINT32, &paramSize,
990 BSG_TPM_COMMAND_CODE, &returnCode);
992 if (!DeleteHandleFromList(hContext, hKey)) {
993 vtpmlogerror(VTPM_LOG_TCS, "KeyHandle not removed from list\n");
994 }
996 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
997 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
998 } else {
999 vtpmlogerror(VTPM_LOG_TCS, "TCSP_EvictKey Failed with return code %s\n", tpm_get_error_name(returnCode));
1003 return(returnCode);
1006 TPM_RESULT TCSP_GetRandom(TCS_CONTEXT_HANDLE hContext, // in
1007 UINT32* bytesRequested, // in, out
1008 BYTE** randomBytes) // out
1010 // setup input/output parameters block
1011 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
1012 UINT32 paramSize = 0;
1013 TPM_COMMAND_CODE ordinal = TPM_ORD_GetRandom;
1014 TPM_RESULT returnCode = TPM_SUCCESS;
1016 // setup the TPM driver input and output buffers
1017 TDDL_RESULT hRes = TDDL_E_FAIL;
1018 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
1019 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
1021 // check input params
1022 if (bytesRequested == NULL || *randomBytes == NULL){
1023 return TPM_BAD_PARAMETER;
1026 // Convert Byte Input parameter in the input byte stream InBuf
1027 InLength = BSG_PackList(InBuf, 4,
1028 BSG_TPM_TAG, &tag,
1029 BSG_TYPE_UINT32, &paramSize,
1030 BSG_TPM_COMMAND_CODE, &ordinal,
1031 BSG_TYPE_UINT32, bytesRequested);
1032 // fill paramSize again as we now have the correct size
1033 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
1035 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
1037 // call the TPM driver
1038 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
1039 // unpack OutBuf to get the tag, paramSize, & returnCode
1040 int i = BSG_UnpackList(OutBuf, 3,
1041 BSG_TPM_TAG, &tag,
1042 BSG_TYPE_UINT32, &paramSize,
1043 BSG_TPM_COMMAND_CODE, &returnCode);
1045 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
1046 // Extract the remaining output parameters
1047 BSG_Unpack(BSG_TYPE_UINT32, OutBuf+i, bytesRequested);
1048 if (TCS_Malloc(hContext, *bytesRequested, randomBytes) == TPM_FAIL) {
1049 return TPM_SIZE;
1051 memcpy(*randomBytes, OutBuf+i+sizeof(UINT32), *bytesRequested);
1053 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
1054 } else {
1055 vtpmlogerror(VTPM_LOG_TCS, "TCSP_GetRandom Failed with return code %s\n", tpm_get_error_name(returnCode));
1059 return(returnCode);
1063 TPM_RESULT TCSP_ReadPubek(TCS_CONTEXT_HANDLE hContext, // in
1064 TPM_NONCE antiReplay, // in
1065 UINT32* pubEndorsementKeySize, // out
1066 BYTE** pubEndorsementKey, // out
1067 TPM_DIGEST* checksum) // out
1069 // setup input/output parameters block
1070 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
1071 UINT32 paramSize = 0;
1072 TPM_COMMAND_CODE ordinal = TPM_ORD_ReadPubek;
1073 TPM_RESULT returnCode = TPM_SUCCESS;
1075 // setup the TPM driver input and output buffers
1076 TDDL_RESULT hRes = TDDL_E_FAIL;
1077 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
1078 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
1080 // check input params
1081 if (pubEndorsementKeySize == NULL || pubEndorsementKey == NULL || checksum == NULL) {
1082 return TPM_BAD_PARAMETER;
1085 // Convert Byte Input parameter in the input byte stream InBuf
1086 InLength = BSG_PackList(InBuf, 4,
1087 BSG_TPM_TAG, &tag,
1088 BSG_TYPE_UINT32, &paramSize,
1089 BSG_TPM_COMMAND_CODE, &ordinal,
1090 BSG_TPM_NONCE, &antiReplay);
1091 // fill paramSize again as we now have the correct size
1092 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
1094 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
1096 // call the TPM driver
1097 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
1098 // unpack OutBuf to get the tag, paramSize, & returnCode
1099 int i = BSG_UnpackList(OutBuf, 3,
1100 BSG_TPM_TAG, &tag,
1101 BSG_TYPE_UINT32, &paramSize,
1102 BSG_TPM_COMMAND_CODE, &returnCode);
1104 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
1105 // Extract the remaining output parameters
1106 TPM_PUBKEY pubEK;
1107 i += BSG_UnpackList(OutBuf+i, 2,
1108 BSG_TPM_PUBKEY, &pubEK,
1109 BSG_TPM_DIGEST, checksum);
1111 // fill EndorsementKey
1112 BYTE tempBuf[1024];
1113 *pubEndorsementKeySize = BSG_Pack(BSG_TPM_PUBKEY, &pubEK, tempBuf);
1114 if (TCS_Malloc(hContext, *pubEndorsementKeySize, pubEndorsementKey) == TPM_FAIL) {
1115 return TPM_SIZE;
1117 memcpy(*pubEndorsementKey, tempBuf, *pubEndorsementKeySize);
1119 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
1120 } else {
1121 vtpmlogerror(VTPM_LOG_TCS, "TCSP_ReadPubek Failed with return code %s\n", tpm_get_error_name(returnCode));
1125 return(returnCode);
1129 TPM_RESULT TCSP_SaveState(TCS_CONTEXT_HANDLE hContext) // in
1131 // setup input/output parameters block
1132 TPM_TAG tag = TPM_TAG_RQU_COMMAND;
1133 UINT32 paramSize = 0;
1134 TPM_COMMAND_CODE ordinal = TPM_ORD_SaveState;
1135 TPM_RESULT returnCode = TPM_SUCCESS;
1137 // setup the TPM driver input and output buffers
1138 TDDL_RESULT hRes = TDDL_E_FAIL;
1139 TDDL_UINT32 InLength = TCPA_MAX_BUFFER_LENGTH;
1140 TDDL_UINT32 OutLength = TCPA_MAX_BUFFER_LENGTH;
1142 // Convert Byte Input parameter in the input byte stream InBuf
1143 InLength = BSG_PackList(InBuf, 3,
1144 BSG_TPM_TAG, &tag,
1145 BSG_TYPE_UINT32, &paramSize,
1146 BSG_TPM_COMMAND_CODE, &ordinal);
1147 // fill paramSize again as we now have the correct size
1148 BSG_Pack(BSG_TYPE_UINT32, &InLength, InBuf+2);
1150 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Sending paramSize = %d\n", InLength);
1152 // call the TPM driver
1153 if ((hRes = TDDL_TransmitData(InBuf, InLength, OutBuf, &OutLength)) == TDDL_SUCCESS) {
1154 // unpack OutBuf to get the tag, paramSize, & returnCode
1155 BSG_UnpackList(OutBuf, 3,
1156 BSG_TPM_TAG, &tag,
1157 BSG_TYPE_UINT32, &paramSize,
1158 BSG_TPM_COMMAND_CODE, &returnCode);
1160 if (returnCode == TPM_SUCCESS && tag == TPM_TAG_RSP_COMMAND) {
1161 vtpmloginfo(VTPM_LOG_TCS_DEEP, "Received paramSize : %d\n", paramSize);
1162 } else {
1163 vtpmlogerror(VTPM_LOG_TCS, "TCSP_SaveState Failed with return code %s\n", tpm_get_error_name(returnCode));
1167 return(returnCode);
1171 TPM_RESULT TCSP_RawTransmitData( UINT32 inDataSize, // in
1172 BYTE *inData, // in
1173 UINT32 *outDataSize,// in/out
1174 BYTE *outData) { // out
1176 TDDL_RESULT hRes;
1178 vtpmloginfo(VTPM_LOG_TCS, "Calling TransmitData directly.\n");
1179 //FIXME: Add Context Management
1180 hRes = TDDL_TransmitData( inData,
1181 inDataSize,
1182 outData,
1183 outDataSize);
1185 if (hRes == TDDL_SUCCESS) {
1186 return TPM_SUCCESS;
1187 } else {
1188 vtpmlogerror(VTPM_LOG_TCS, "TCSP_RawTransmitData Failed with return code %s\n", tpm_get_error_name(TPM_IOERROR));
1189 return TPM_IOERROR;