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 #ifdef TSS_BUILD_TRANSPORT
26 TSS_RESULT
Transport_ReadCurrentTicks(TSS_HCONTEXT tspContext,UINT32 * pulCurrentTime,BYTE ** prgbCurrentTime)27 Transport_ReadCurrentTicks(TSS_HCONTEXT tspContext,           /* in */
28                                  UINT32*      pulCurrentTime,       /* out */
29                                  BYTE**       prgbCurrentTime)      /* out */
30 {
31           TSS_RESULT result;
32           UINT32 decLen = 0;
33           BYTE *dec = NULL;
34           TCS_HANDLE handlesLen = 0;
35 
36           if ((result = obj_context_transport_init(tspContext)))
37                     return result;
38 
39           LogDebugFn("Executing in a transport session");
40 
41           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_GetTicks, 0, NULL,
42                                                                 NULL, &handlesLen, NULL, NULL, NULL, &decLen,
43                                                                 &dec)))
44                     return result;
45 
46           *pulCurrentTime = decLen;
47           *prgbCurrentTime = dec;
48 
49           return TSS_SUCCESS;
50 }
51 
52 TSS_RESULT
Transport_TickStampBlob(TSS_HCONTEXT tspContext,TCS_KEY_HANDLE hKey,TPM_NONCE * antiReplay,TPM_DIGEST * digestToStamp,TPM_AUTH * privAuth,UINT32 * pulSignatureLength,BYTE ** prgbSignature,UINT32 * pulTickCountLength,BYTE ** prgbTickCount)53 Transport_TickStampBlob(TSS_HCONTEXT   tspContext,            /* in */
54                               TCS_KEY_HANDLE hKey,                  /* in */
55                               TPM_NONCE*     antiReplay,            /* in */
56                               TPM_DIGEST*    digestToStamp,       /* in */
57                               TPM_AUTH*      privAuth,              /* in, out */
58                               UINT32*        pulSignatureLength,    /* out */
59                               BYTE**         prgbSignature,       /* out */
60                               UINT32*        pulTickCountLength,    /* out */
61                               BYTE**         prgbTickCount)       /* out */
62 {
63           TSS_RESULT result;
64           UINT32 handlesLen, decLen = 0;
65           TCS_HANDLE *handles, handle;
66           BYTE *dec = NULL;
67           UINT64 offset;
68           TPM_DIGEST pubKeyHash;
69           Trspi_HashCtx hashCtx;
70           BYTE data[sizeof(TPM_NONCE) + sizeof(TPM_DIGEST)];
71 
72           if ((result = obj_context_transport_init(tspContext)))
73                     return result;
74 
75           LogDebugFn("Executing in a transport session");
76 
77           if ((result = obj_tcskey_get_pubkeyhash(hKey, pubKeyHash.digest)))
78                     return result;
79 
80           result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
81           result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
82           if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
83                     return result;
84 
85           handlesLen = 1;
86           handle = hKey;
87           handles = &handle;
88 
89           offset = 0;
90           Trspi_LoadBlob_NONCE(&offset, data, antiReplay);
91           Trspi_LoadBlob_DIGEST(&offset, data, digestToStamp);
92 
93           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_TickStampBlob, sizeof(data),
94                                                                 data, &pubKeyHash, &handlesLen, &handles,
95                                                                 privAuth, NULL, &decLen, &dec)))
96                     return result;
97 
98           offset = 0;
99           Trspi_UnloadBlob_CURRENT_TICKS(&offset, dec, NULL);
100           *pulTickCountLength = (UINT32)offset;
101           if ((*prgbTickCount = malloc(*pulTickCountLength)) == NULL) {
102                     free(dec);
103                     LogError("malloc of %u bytes failed", *pulTickCountLength);
104                     return TSPERR(TSS_E_OUTOFMEMORY);
105           }
106           offset = 0;
107           Trspi_UnloadBlob(&offset, *pulTickCountLength, dec, *prgbTickCount);
108 
109           Trspi_UnloadBlob_UINT32(&offset, pulSignatureLength, dec);
110           if ((*prgbSignature = malloc(*pulSignatureLength)) == NULL) {
111                     free(dec);
112                     free(*prgbTickCount);
113                     *pulTickCountLength = 0;
114                     LogError("malloc of %u bytes failed", *pulSignatureLength);
115                     *pulSignatureLength = 0;
116                     return TSPERR(TSS_E_OUTOFMEMORY);
117           }
118           Trspi_UnloadBlob(&offset, *pulSignatureLength, dec, *prgbSignature);
119 
120           free(dec);
121 
122           return result;
123 }
124 #endif
125