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
8  *
9  */
10 
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <inttypes.h>
16 
17 #include "trousers/tss.h"
18 #include "trousers_types.h"
19 #include "tcs_tsp.h"
20 #include "tcsps.h"
21 #include "tcs_utils.h"
22 #include "tcs_int_literals.h"
23 #include "capabilities.h"
24 #include "tcslog.h"
25 #include "req_mgr.h"
26 #include "tcsd_wrap.h"
27 #include "tcsd.h"
28 
29 TSS_RESULT
TCSP_CreateMaintenanceArchive_Internal(TCS_CONTEXT_HANDLE hContext,TSS_BOOL generateRandom,TPM_AUTH * ownerAuth,UINT32 * randomSize,BYTE ** random,UINT32 * archiveSize,BYTE ** archive)30 TCSP_CreateMaintenanceArchive_Internal(TCS_CONTEXT_HANDLE hContext,   /* in */
31                                                TSS_BOOL generateRandom,         /* in */
32                                                TPM_AUTH * ownerAuth,  /* in, out */
33                                                UINT32 * randomSize,   /* out */
34                                                BYTE ** random,        /* out */
35                                                UINT32 * archiveSize,  /* out */
36                                                BYTE ** archive)       /* out */
37 {
38           TSS_RESULT result;
39           UINT32 paramSize;
40           UINT64 offset = 0;
41           BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
42 
43           LogDebug("Create Main Archive");
44 
45           if ((result = ctx_verify_context(hContext)))
46                     goto done;
47 
48           if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
49                     goto done;
50 
51           if ((result = tpm_rqu_build(TPM_ORD_CreateMaintenanceArchive, &offset, txBlob,
52                                             generateRandom, ownerAuth)))
53                     goto done;
54 
55           if ((result = req_mgr_submit_req(txBlob)))
56                     goto done;
57 
58           result = UnloadBlob_Header(txBlob, &paramSize);
59           if (!result) {
60                     result = tpm_rsp_parse(TPM_ORD_CreateMaintenanceArchive, txBlob, paramSize,
61                                                randomSize, random, archiveSize, archive, ownerAuth);
62           }
63           LogResult("Create Main Archive", result);
64 done:
65           auth_mgr_release_auth(ownerAuth, NULL, hContext);
66           return result;
67 }
68 
69 TSS_RESULT
TCSP_LoadMaintenanceArchive_Internal(TCS_CONTEXT_HANDLE hContext,UINT32 dataInSize,BYTE * dataIn,TPM_AUTH * ownerAuth,UINT32 * dataOutSize,BYTE ** dataOut)70 TCSP_LoadMaintenanceArchive_Internal(TCS_CONTEXT_HANDLE hContext,     /* in */
71                                              UINT32 dataInSize,       /* in */
72                                              BYTE * dataIn, /* in */
73                                              TPM_AUTH * ownerAuth,    /* in, out */
74                                              UINT32 * dataOutSize,    /* out */
75                                              BYTE ** dataOut)         /* out */
76 {
77           TSS_RESULT result;
78           UINT32 paramSize;
79           UINT64 offset = 0;
80           BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
81 
82           LogDebug("Load Maint Archive");
83 
84           if ((result = ctx_verify_context(hContext)))
85                     goto done;
86 
87           if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
88                     goto done;
89 
90           if ((result = tpm_rqu_build(TPM_ORD_LoadMaintenanceArchive, &offset, txBlob, dataInSize,
91                                             dataInSize, dataIn, ownerAuth, NULL)))
92                     return result;
93 
94           if ((result = req_mgr_submit_req(txBlob)))
95                     goto done;
96 
97           result = UnloadBlob_Header(txBlob, &paramSize);
98           if (!result) {
99                     result = tpm_rsp_parse(TPM_ORD_LoadMaintenanceArchive, txBlob, paramSize,
100                                                dataOutSize, dataOut, ownerAuth, NULL);
101           }
102           LogResult("Load Maint Archive", result);
103 done:
104           auth_mgr_release_auth(ownerAuth, NULL, hContext);
105           return result;
106 }
107 
108 TSS_RESULT
TCSP_KillMaintenanceFeature_Internal(TCS_CONTEXT_HANDLE hContext,TPM_AUTH * ownerAuth)109 TCSP_KillMaintenanceFeature_Internal(TCS_CONTEXT_HANDLE hContext,     /* in */
110                                              TPM_AUTH * ownerAuth)    /* in, out */
111 {
112           TSS_RESULT result;
113           UINT32 paramSize;
114           UINT64 offset = 0;
115           BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
116 
117           if ((result = ctx_verify_context(hContext)))
118                     goto done;
119 
120           if ((result = auth_mgr_check(hContext, &ownerAuth->AuthHandle)))
121                     goto done;
122 
123           if ((result = tpm_rqu_build(TPM_ORD_KillMaintenanceFeature, &offset, txBlob, ownerAuth)))
124                     return result;
125 
126           if ((result = req_mgr_submit_req(txBlob)))
127                     goto done;
128 
129           result = UnloadBlob_Header(txBlob, &paramSize);
130 
131           if (!result) {
132                     result = tpm_rsp_parse(TPM_ORD_KillMaintenanceFeature, txBlob, paramSize,
133                                                ownerAuth);
134           }
135 done:
136           auth_mgr_release_auth(ownerAuth, NULL, hContext);
137           return result;
138 }
139 
140 TSS_RESULT
TCSP_LoadManuMaintPub_Internal(TCS_CONTEXT_HANDLE hContext,TCPA_NONCE antiReplay,UINT32 PubKeySize,BYTE * PubKey,TCPA_DIGEST * checksum)141 TCSP_LoadManuMaintPub_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
142                                      TCPA_NONCE antiReplay, /* in */
143                                      UINT32 PubKeySize,     /* in */
144                                      BYTE * PubKey,         /* in */
145                                      TCPA_DIGEST * checksum)          /* out */
146 {
147           TSS_RESULT result;
148           UINT32 paramSize;
149           UINT64 offset = 0;
150           BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
151 
152           LogDebug("Entering Load Manu Maint Pub");
153 
154           if ((result = tpm_rqu_build(TPM_ORD_LoadManuMaintPub, &offset, txBlob, TPM_NONCE_SIZE,
155                                             antiReplay.nonce, PubKeySize, PubKey, NULL)))
156                     return result;
157 
158           if ((result = req_mgr_submit_req(txBlob)))
159                     return result;
160 
161           result = UnloadBlob_Header(txBlob, &paramSize);
162           if (!result) {
163                     result = tpm_rsp_parse(TPM_ORD_LoadManuMaintPub, txBlob, paramSize, NULL,
164                                                checksum->digest);
165           }
166           LogResult("Load Manu Maint Pub", result);
167           return result;
168 }
169 
170 TSS_RESULT
TCSP_ReadManuMaintPub_Internal(TCS_CONTEXT_HANDLE hContext,TCPA_NONCE antiReplay,TCPA_DIGEST * checksum)171 TCSP_ReadManuMaintPub_Internal(TCS_CONTEXT_HANDLE hContext, /* in */
172                                      TCPA_NONCE antiReplay, /* in */
173                                      TCPA_DIGEST * checksum)          /* out */
174 {
175           TSS_RESULT result;
176           UINT32 paramSize;
177           UINT64 offset = 0;
178           BYTE txBlob[TSS_TPM_TXBLOB_SIZE];
179 
180           LogDebug("Entering Read Manu Maint Pub");
181 
182           if ((result = tpm_rqu_build(TPM_ORD_ReadManuMaintPub, &offset, txBlob, TPM_NONCE_SIZE,
183                                             antiReplay.nonce)))
184                     return result;
185 
186           if ((result = req_mgr_submit_req(txBlob)))
187                     return result;
188 
189           result = UnloadBlob_Header(txBlob, &paramSize);
190           if (!result) {
191                     result = tpm_rsp_parse(TPM_ORD_ReadManuMaintPub, txBlob, paramSize, NULL,
192                                                checksum->digest);
193           }
194           LogResult("Read Manu Maint Pub", result);
195           return result;
196 }
197 
198