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 
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include "trousers/tss.h"
16 #include "trousers/trousers.h"
17 #include "trousers_types.h"
18 #include "spi_utils.h"
19 #include "capabilities.h"
20 #include "tsplog.h"
21 #include "obj.h"
22 
23 
24 #ifdef TSS_BUILD_TRANSPORT
25 TSS_RESULT
Transport_UnBind(TSS_HCONTEXT tspContext,TCS_KEY_HANDLE keyHandle,UINT32 inDataSize,BYTE * inData,TPM_AUTH * privAuth,UINT32 * outDataSize,BYTE ** outData)26 Transport_UnBind(TSS_HCONTEXT tspContext,         /* in */
27                      TCS_KEY_HANDLE keyHandle,    /* in */
28                      UINT32 inDataSize, /* in */
29                      BYTE * inData,     /* in */
30                      TPM_AUTH * privAuth,         /* in, out */
31                      UINT32 * outDataSize,        /* out */
32                      BYTE ** outData)   /* out */
33 {
34           TSS_RESULT result;
35           UINT32 handlesLen, dataLen, decLen;
36           TCS_HANDLE *handles, handle;
37           TPM_DIGEST pubKeyHash;
38           Trspi_HashCtx hashCtx;
39           BYTE *dec, *data;
40           UINT64 offset;
41 
42           if ((result = obj_context_transport_init(tspContext)))
43                     return result;
44 
45           LogDebugFn("Executing in a transport session");
46 
47           if ((result = obj_tcskey_get_pubkeyhash(keyHandle, pubKeyHash.digest)))
48                     return result;
49 
50           result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
51           result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
52           if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
53                     return result;
54 
55           handlesLen = 1;
56           handle = keyHandle;
57           handles = &handle;
58 
59           dataLen = sizeof(UINT32) + inDataSize;
60           if ((data = malloc(dataLen)) == NULL) {
61                     LogError("malloc of %u bytes failed", dataLen);
62                     return TSPERR(TSS_E_OUTOFMEMORY);
63           }
64 
65           offset = 0;
66           Trspi_LoadBlob_UINT32(&offset, inDataSize, data);
67           Trspi_LoadBlob(&offset, inDataSize, data, inData);
68 
69           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_UnBind, dataLen, data,
70                                                                 &pubKeyHash, &handlesLen, &handles,
71                                                                 privAuth, NULL, &decLen, &dec))) {
72                     free(data);
73                     return result;
74           }
75           free(data);
76 
77           offset = 0;
78           Trspi_UnloadBlob_UINT32(&offset, outDataSize, dec);
79 
80           if ((*outData = malloc(*outDataSize)) == NULL) {
81                     free(dec);
82                     LogError("malloc of %u bytes failed", *outDataSize);
83                     return TSPERR(TSS_E_OUTOFMEMORY);
84           }
85           Trspi_UnloadBlob(&offset, *outDataSize, dec, *outData);
86 
87           free(dec);
88 
89           return TSS_SUCCESS;
90 }
91 #endif
92 
93