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 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <inttypes.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 #ifdef TSS_BUILD_TRANSPORT
25 TSS_RESULT
Transport_CreateMaintenanceArchive(TSS_HCONTEXT tspContext,TSS_BOOL generateRandom,TPM_AUTH * ownerAuth,UINT32 * randomSize,BYTE ** random,UINT32 * archiveSize,BYTE ** archive)26 Transport_CreateMaintenanceArchive(TSS_HCONTEXT tspContext, /* in */
27                                            TSS_BOOL generateRandom,   /* in */
28                                            TPM_AUTH * ownerAuth,      /* in, out */
29                                            UINT32 * randomSize,       /* out */
30                                            BYTE ** random,  /* out */
31                                            UINT32 * archiveSize,      /* out */
32                                            BYTE ** archive) /* out */
33 {
34           UINT64 offset;
35           TSS_RESULT result;
36           UINT32 handlesLen = 0, decLen;
37           BYTE *dec;
38 
39           if ((result = obj_context_transport_init(tspContext)))
40                     return result;
41 
42           LogDebugFn("Executing in a transport session");
43 
44           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_CreateMaintenanceArchive,
45                                                                 sizeof(TSS_BOOL), (BYTE *)&generateRandom, NULL,
46                                                                 &handlesLen, NULL, ownerAuth, NULL, &decLen,
47                                                                 &dec)))
48                     return result;
49 
50           offset = 0;
51           Trspi_UnloadBlob_UINT32(&offset, randomSize, dec);
52           if (*randomSize > 0) {
53                     if ((*random = malloc(*randomSize)) == NULL) {
54                               *randomSize = 0;
55                               free(dec);
56                               LogError("malloc of %u bytes failed", *randomSize);
57                               return TSPERR(TSS_E_OUTOFMEMORY);
58                     }
59                     Trspi_UnloadBlob(&offset, *randomSize, dec, *random);
60           }
61 
62           Trspi_UnloadBlob_UINT32(&offset, archiveSize, dec);
63           if ((*archive = malloc(*archiveSize)) == NULL) {
64                     free(*random);
65                     *random = NULL;
66                     *randomSize = 0;
67                     free(dec);
68                     LogError("malloc of %u bytes failed", *archiveSize);
69                     *archiveSize = 0;
70                     return TSPERR(TSS_E_OUTOFMEMORY);
71           }
72           Trspi_UnloadBlob(&offset, *archiveSize, dec, *archive);
73           free(dec);
74 
75           return result;
76 }
77 
78 TSS_RESULT
Transport_LoadMaintenanceArchive(TSS_HCONTEXT tspContext,UINT32 dataInSize,BYTE * dataIn,TPM_AUTH * ownerAuth,UINT32 * dataOutSize,BYTE ** dataOut)79 Transport_LoadMaintenanceArchive(TSS_HCONTEXT tspContext,   /* in */
80                                          UINT32 dataInSize, /* in */
81                                          BYTE * dataIn, /* in */
82                                          TPM_AUTH * ownerAuth,        /* in, out */
83                                          UINT32 * dataOutSize,        /* out */
84                                          BYTE ** dataOut)   /* out */
85 {
86           UINT64 offset;
87           TSS_RESULT result;
88           UINT32 handlesLen = 0, decLen;
89           BYTE *dec;
90 
91 
92           if ((result = obj_context_transport_init(tspContext)))
93                     return result;
94 
95           LogDebugFn("Executing in a transport session");
96 
97           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_LoadMaintenanceArchive,
98                                                                 dataInSize, dataIn, NULL, &handlesLen, NULL,
99                                                                 ownerAuth, NULL, &decLen, &dec)))
100                     return result;
101 
102           offset = 0;
103           Trspi_UnloadBlob_UINT32(&offset, dataOutSize, dec);
104 
105           /* sacrifice 4 bytes */
106           *dataOut = &dec[offset];
107 
108           return result;
109 }
110 
111 TSS_RESULT
Transport_KillMaintenanceFeature(TSS_HCONTEXT tspContext,TPM_AUTH * ownerAuth)112 Transport_KillMaintenanceFeature(TSS_HCONTEXT tspContext,   /* in */
113                                          TPM_AUTH * ownerAuth)        /* in, out */
114 {
115           TSS_RESULT result;
116           UINT32 handlesLen = 0;
117 
118           if ((result = obj_context_transport_init(tspContext)))
119                     return result;
120 
121           LogDebugFn("Executing in a transport session");
122 
123           return obj_context_transport_execute(tspContext, TPM_ORD_KillMaintenanceFeature, 0, NULL,
124                                                        NULL, &handlesLen, NULL, ownerAuth, NULL, NULL, NULL);
125 }
126 
127 TSS_RESULT
Transport_LoadManuMaintPub(TSS_HCONTEXT tspContext,TCPA_NONCE antiReplay,UINT32 PubKeySize,BYTE * PubKey,TCPA_DIGEST * checksum)128 Transport_LoadManuMaintPub(TSS_HCONTEXT tspContext,         /* in */
129                                  TCPA_NONCE antiReplay,     /* in */
130                                  UINT32 PubKeySize,         /* in */
131                                  BYTE * PubKey,   /* in */
132                                  TCPA_DIGEST * checksum)    /* out */
133 {
134           UINT64 offset;
135           TSS_RESULT result;
136           UINT32 handlesLen = 0, dataLen, decLen;
137           BYTE *data, *dec;
138 
139           if ((result = obj_context_transport_init(tspContext)))
140                     return result;
141 
142           LogDebugFn("Executing in a transport session");
143 
144           dataLen = sizeof(TCPA_NONCE) + PubKeySize;
145           if ((data = malloc(dataLen)) == NULL) {
146                     LogError("malloc of %u bytes failed", dataLen);
147                     return TSPERR(TSS_E_OUTOFMEMORY);
148           }
149 
150           offset = 0;
151           Trspi_LoadBlob(&offset, TPM_SHA1_160_HASH_LEN, data, antiReplay.nonce);
152           Trspi_LoadBlob(&offset, PubKeySize, data, PubKey);
153 
154           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_LoadManuMaintPub,
155                                                                 dataLen, data, NULL, &handlesLen, NULL, NULL,
156                                                                 NULL, &decLen, &dec))) {
157                     free(data);
158                     return result;
159           }
160           free(data);
161 
162           offset = 0;
163           Trspi_UnloadBlob_DIGEST(&offset, dec, checksum);
164           free(dec);
165 
166           return result;
167 }
168 
169 TSS_RESULT
Transport_ReadManuMaintPub(TSS_HCONTEXT tspContext,TCPA_NONCE antiReplay,TCPA_DIGEST * checksum)170 Transport_ReadManuMaintPub(TSS_HCONTEXT tspContext,         /* in */
171                                  TCPA_NONCE antiReplay,     /* in */
172                                  TCPA_DIGEST * checksum)    /* out */
173 {
174           UINT64 offset;
175           TSS_RESULT result;
176           UINT32 handlesLen = 0, decLen;
177           BYTE *dec;
178 
179           if ((result = obj_context_transport_init(tspContext)))
180                     return result;
181 
182           LogDebugFn("Executing in a transport session");
183 
184           if ((result = obj_context_transport_execute(tspContext, TPM_ORD_ReadManuMaintPub,
185                                                                 sizeof(TCPA_NONCE), antiReplay.nonce, NULL,
186                                                                 &handlesLen, NULL, NULL, NULL, &decLen,
187                                                                 &dec)))
188                     return result;
189 
190           offset = 0;
191           Trspi_UnloadBlob_DIGEST(&offset, dec, checksum);
192           free(dec);
193 
194           return result;
195 }
196 #endif
197 
198