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-2007
8  *
9  */
10 
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.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 "obj.h"
23 
24 
25 TSS_RESULT
Transport_LoadKeyByBlob(TSS_HCONTEXT hContext,TPM_COMMAND_CODE ordinal,TSS_HKEY hParentKey,UINT32 ulBlobLength,BYTE * rgbBlobData,TPM_AUTH * pAuth,TCS_KEY_HANDLE * phKey,TPM_KEY_HANDLE * phSlot)26 Transport_LoadKeyByBlob(TSS_HCONTEXT     hContext,
27                               TPM_COMMAND_CODE ordinal,
28                               TSS_HKEY         hParentKey,
29                               UINT32           ulBlobLength,
30                               BYTE*            rgbBlobData,
31                               TPM_AUTH*        pAuth,
32                               TCS_KEY_HANDLE*  phKey,
33                               TPM_KEY_HANDLE*  phSlot)
34 {
35           TSS_RESULT result;
36           UINT32 handleListSize, decLen;
37           TCS_KEY_HANDLE hTCSParentKey;
38           TCS_HANDLE *handleList;
39           BYTE *dec = NULL;
40           TPM_DIGEST pubKeyHash;
41           Trspi_HashCtx hashCtx;
42 
43 
44           if ((result = obj_context_transport_init(hContext)) == TSS_TSPATTRIB_DISABLE_TRANSPORT) {
45                     if ((result = obj_rsakey_get_tcs_handle(hParentKey, &hTCSParentKey)))
46                               return result;
47 
48                     return TCSP_LoadKeyByBlob(hContext, hTCSParentKey, ulBlobLength, rgbBlobData, pAuth,
49                                                     phKey, phSlot);
50           } else if (result != TSS_TSPATTRIB_ENABLE_TRANSPORT)
51                     return result;
52 
53           LogDebugFn("Executing in a transport session");
54 
55           if ((result = obj_rsakey_get_transport_attribs(hParentKey, &hTCSParentKey, &pubKeyHash)))
56                     return result;
57 
58           result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
59           result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
60           if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
61                     return result;
62 
63           /* Call ExecuteTransport */
64           handleListSize = 1;
65           if ((handleList = malloc(sizeof(TCS_HANDLE))) == NULL) {
66                     LogError("malloc of %zd bytes failed", sizeof(TCS_HANDLE));
67                     return TSPERR(TSS_E_OUTOFMEMORY);
68           }
69 
70           *handleList = hTCSParentKey;
71 
72           if ((result = obj_context_transport_execute(hContext, ordinal, ulBlobLength,
73                                                                 rgbBlobData, &pubKeyHash, &handleListSize,
74                                                                 &handleList, pAuth, NULL, &decLen, &dec))) {
75                     free(handleList);
76                     return result;
77           }
78 
79           if (handleListSize == 1)
80                     *phKey = *(TCS_KEY_HANDLE *)handleList;
81           else
82                     result = TSPERR(TSS_E_INTERNAL_ERROR);
83 
84           free(handleList);
85           free(dec);
86 
87           return result;
88 }
89