• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3  * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4  * All rights reserved.
5  ******************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include "tss2_mu.h"
12 #include "tss2_sys.h"
13 #include "tss2_esys.h"
14 
15 #include "esys_types.h"
16 #include "esys_iutil.h"
17 #include "esys_mu.h"
18 #define LOGMODULE esys
19 #include "util/log.h"
20 #include "util/aux_util.h"
21 
22 /** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
store_input_parameters(ESYS_CONTEXT * esysContext,ESYS_TR flushHandle)23 static void store_input_parameters (
24     ESYS_CONTEXT *esysContext,
25     ESYS_TR flushHandle)
26 {
27     esysContext->in.FlushContext.flushHandle = flushHandle;
28 }
29 
30 /** One-Call function for TPM2_FlushContext
31  *
32  * This function invokes the TPM2_FlushContext command in a one-call
33  * variant. This means the function will block until the TPM response is
34  * available. All input parameters are const. The memory for non-simple output
35  * parameters is allocated by the function implementation.
36  *
37  * @param[in,out] esysContext The ESYS_CONTEXT.
38  * @param[in]  flushHandle The handle of the item to flush.
39  * @retval TSS2_RC_SUCCESS if the function call was a success.
40  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
41  *         pointers or required output handle references are NULL.
42  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
43  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
44  *         internal operations or return parameters.
45  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
46  *         operation already pending.
47  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
48  *          at least contain the tag, response length, and response code.
49  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
50  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
51            did not verify.
52  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
53  *         to the ESYS_CONTEXT or are of the wrong type or if required
54  *         ESYS_TR objects are ESYS_TR_NONE.
55  * @retval TSS2_RCs produced by lower layers of the software stack may be
56  *         returned to the caller unaltered unless handled internally.
57  */
58 TSS2_RC
Esys_FlushContext(ESYS_CONTEXT * esysContext,ESYS_TR flushHandle)59 Esys_FlushContext(
60     ESYS_CONTEXT *esysContext,
61     ESYS_TR flushHandle)
62 {
63     TSS2_RC r;
64 
65     r = Esys_FlushContext_Async(esysContext, flushHandle);
66     return_if_error(r, "Error in async function");
67 
68     /* Set the timeout to indefinite for now, since we want _Finish to block */
69     int32_t timeouttmp = esysContext->timeout;
70     esysContext->timeout = -1;
71     /*
72      * Now we call the finish function, until return code is not equal to
73      * from TSS2_BASE_RC_TRY_AGAIN.
74      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
75      * have set the timeout to -1. This occurs for example if the TPM requests
76      * a retransmission of the command via TPM2_RC_YIELDED.
77      */
78     do {
79         r = Esys_FlushContext_Finish(esysContext);
80         /* This is just debug information about the reattempt to finish the
81            command */
82         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
83             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
84                       " => resubmitting command", r);
85     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
86 
87     /* Restore the timeout value to the original value */
88     esysContext->timeout = timeouttmp;
89     return_if_error(r, "Esys Finish");
90 
91     return TSS2_RC_SUCCESS;
92 }
93 
94 /** Asynchronous function for TPM2_FlushContext
95  *
96  * This function invokes the TPM2_FlushContext command in a asynchronous
97  * variant. This means the function will return as soon as the command has been
98  * sent downwards the stack to the TPM. All input parameters are const.
99  * In order to retrieve the TPM's response call Esys_FlushContext_Finish.
100  *
101  * @param[in,out] esysContext The ESYS_CONTEXT.
102  * @param[in]  flushHandle The handle of the item to flush.
103  * @retval ESYS_RC_SUCCESS if the function call was a success.
104  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
105  *         pointers or required output handle references are NULL.
106  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
107  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
108  *         internal operations or return parameters.
109  * @retval TSS2_RCs produced by lower layers of the software stack may be
110            returned to the caller unaltered unless handled internally.
111  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
112  *         to the ESYS_CONTEXT or are of the wrong type or if required
113  *         ESYS_TR objects are ESYS_TR_NONE.
114  */
115 TSS2_RC
Esys_FlushContext_Async(ESYS_CONTEXT * esysContext,ESYS_TR flushHandle)116 Esys_FlushContext_Async(
117     ESYS_CONTEXT *esysContext,
118     ESYS_TR flushHandle)
119 {
120     TSS2_RC r;
121     LOG_TRACE("context=%p, flushHandle=%"PRIx32 "",
122               esysContext, flushHandle);
123     RSRC_NODE_T *flushHandleNode;
124 
125     /* Check context, sequence correctness and set state to error for now */
126     if (esysContext == NULL) {
127         LOG_ERROR("esyscontext is NULL.");
128         return TSS2_ESYS_RC_BAD_REFERENCE;
129     }
130     r = iesys_check_sequence_async(esysContext);
131     if (r != TSS2_RC_SUCCESS)
132         return r;
133     esysContext->state = _ESYS_STATE_INTERNALERROR;
134     store_input_parameters(esysContext, flushHandle);
135 
136     /* Retrieve the metadata objects for provided handles */
137     r = esys_GetResourceObject(esysContext, flushHandle, &flushHandleNode);
138     return_state_if_error(r, _ESYS_STATE_INIT, "flushHandle unknown.");
139 
140     /* Initial invocation of SAPI to prepare the command buffer with parameters */
141     r = Tss2_Sys_FlushContext_Prepare(esysContext->sys,
142                                       (flushHandleNode == NULL) ? TPM2_RH_NULL
143                                        : flushHandleNode->rsrc.handle);
144     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
145     /* Trigger execution and finish the async invocation */
146     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
147     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
148                           "Finish (Execute Async)");
149 
150     esysContext->state = _ESYS_STATE_SENT;
151 
152     return r;
153 }
154 
155 /** Asynchronous finish function for TPM2_FlushContext
156  *
157  * This function returns the results of a TPM2_FlushContext command
158  * invoked via Esys_FlushContext_Finish. All non-simple output parameters
159  * are allocated by the function's implementation. NULL can be passed for every
160  * output parameter if the value is not required.
161  *
162  * @param[in,out] esysContext The ESYS_CONTEXT.
163  * @retval TSS2_RC_SUCCESS on success
164  * @retval ESYS_RC_SUCCESS if the function call was a success.
165  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
166  *         pointers or required output handle references are NULL.
167  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
168  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
169  *         internal operations or return parameters.
170  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
171  *         operation already pending.
172  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
173  *         TPM response is received.
174  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
175  *         at least contain the tag, response length, and response code.
176  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
177  *         not verify.
178  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
179  * @retval TSS2_RCs produced by lower layers of the software stack may be
180  *         returned to the caller unaltered unless handled internally.
181  */
182 TSS2_RC
Esys_FlushContext_Finish(ESYS_CONTEXT * esysContext)183 Esys_FlushContext_Finish(
184     ESYS_CONTEXT *esysContext)
185 {
186     TSS2_RC r;
187     LOG_TRACE("context=%p",
188               esysContext);
189 
190     if (esysContext == NULL) {
191         LOG_ERROR("esyscontext is NULL.");
192         return TSS2_ESYS_RC_BAD_REFERENCE;
193     }
194 
195     /* Check for correct sequence and set sequence to irregular for now */
196     if (esysContext->state != _ESYS_STATE_SENT &&
197         esysContext->state != _ESYS_STATE_RESUBMISSION) {
198         LOG_ERROR("Esys called in bad sequence.");
199         return TSS2_ESYS_RC_BAD_SEQUENCE;
200     }
201     esysContext->state = _ESYS_STATE_INTERNALERROR;
202 
203     /*Receive the TPM response and handle resubmissions if necessary. */
204     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
205     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
206         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
207         esysContext->state = _ESYS_STATE_SENT;
208         return r;
209     }
210     /* This block handle the resubmission of TPM commands given a certain set of
211      * TPM response codes. */
212     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
213         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
214             "resubmission: %" PRIx32, r);
215         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
216             LOG_WARNING("Maximum number of (re)submissions has been reached.");
217             esysContext->state = _ESYS_STATE_INIT;
218             return r;
219         }
220         esysContext->state = _ESYS_STATE_RESUBMISSION;
221         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
222         if (r != TSS2_RC_SUCCESS) {
223             LOG_WARNING("Error attempting to resubmit");
224             /* We do not set esysContext->state here but inherit the most recent
225              * state of the _async function. */
226             return r;
227         }
228         r = TSS2_ESYS_RC_TRY_AGAIN;
229         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
230         return r;
231     }
232     /* The following is the "regular error" handling. */
233     if (iesys_tpm_error(r)) {
234         LOG_WARNING("Received TPM Error");
235         esysContext->state = _ESYS_STATE_INIT;
236         return r;
237     } else if (r != TSS2_RC_SUCCESS) {
238         LOG_ERROR("Received a non-TPM Error");
239         esysContext->state = _ESYS_STATE_INTERNALERROR;
240         return r;
241     }
242     r = Tss2_Sys_FlushContext_Complete(esysContext->sys);
243     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
244                           "Received error from SAPI unmarshaling" );
245 
246     /* The ESYS_TR object has to be invalidated */
247     r = Esys_TR_Close(esysContext, &esysContext->in.FlushContext.flushHandle);
248     return_if_error(r, "invalidate object");
249 
250     esysContext->state = _ESYS_STATE_INIT;
251 
252     return TSS2_RC_SUCCESS;
253 }
254