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