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 /** One-Call function for TPM2_GetCapability
23*758e9fbaSOystein Eftevaag *
24*758e9fbaSOystein Eftevaag * This function invokes the TPM2_GetCapability command in a one-call
25*758e9fbaSOystein Eftevaag * variant. This means the function will block until the TPM response is
26*758e9fbaSOystein Eftevaag * available. All input parameters are const. The memory for non-simple output
27*758e9fbaSOystein Eftevaag * parameters is allocated by the function implementation.
28*758e9fbaSOystein Eftevaag *
29*758e9fbaSOystein Eftevaag * @param[in,out] esysContext The ESYS_CONTEXT.
30*758e9fbaSOystein Eftevaag * @param[in] shandle1 First session handle.
31*758e9fbaSOystein Eftevaag * @param[in] shandle2 Second session handle.
32*758e9fbaSOystein Eftevaag * @param[in] shandle3 Third session handle.
33*758e9fbaSOystein Eftevaag * @param[in] capability Group selection; determines the format of the
34*758e9fbaSOystein Eftevaag * response.
35*758e9fbaSOystein Eftevaag * @param[in] property Further definition of information.
36*758e9fbaSOystein Eftevaag * @param[in] propertyCount Number of properties of the indicated type to
37*758e9fbaSOystein Eftevaag * return.
38*758e9fbaSOystein Eftevaag * @param[out] moreData Flag to indicate if there are more values of this type.
39*758e9fbaSOystein Eftevaag * (callee-allocated)
40*758e9fbaSOystein Eftevaag * @param[out] capabilityData The capability data.
41*758e9fbaSOystein Eftevaag * (callee-allocated)
42*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS if the function call was a success.
43*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
44*758e9fbaSOystein Eftevaag * pointers or required output handle references are NULL.
45*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
46*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
47*758e9fbaSOystein Eftevaag * internal operations or return parameters.
48*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
49*758e9fbaSOystein Eftevaag * operation already pending.
50*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
51*758e9fbaSOystein Eftevaag * at least contain the tag, response length, and response code.
52*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
53*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
54*758e9fbaSOystein Eftevaag did not verify.
55*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
56*758e9fbaSOystein Eftevaag * the 'decrypt' attribute bit set.
57*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
58*758e9fbaSOystein Eftevaag * the 'encrypt' attribute bit set.
59*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
60*758e9fbaSOystein Eftevaag * 'decrypt' attribute set and the command does not support encryption
61*758e9fbaSOystein Eftevaag * of the first command parameter.
62*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
63*758e9fbaSOystein Eftevaag * 'encrypt' attribute set and the command does not support encryption
64*758e9fbaSOystein Eftevaag * of the first response parameter.
65*758e9fbaSOystein Eftevaag * @retval TSS2_RCs produced by lower layers of the software stack may be
66*758e9fbaSOystein Eftevaag * returned to the caller unaltered unless handled internally.
67*758e9fbaSOystein Eftevaag */
68*758e9fbaSOystein Eftevaag TSS2_RC
Esys_GetCapability(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPM2_CAP capability,UINT32 property,UINT32 propertyCount,TPMI_YES_NO * moreData,TPMS_CAPABILITY_DATA ** capabilityData)69*758e9fbaSOystein Eftevaag Esys_GetCapability(
70*758e9fbaSOystein Eftevaag ESYS_CONTEXT *esysContext,
71*758e9fbaSOystein Eftevaag ESYS_TR shandle1,
72*758e9fbaSOystein Eftevaag ESYS_TR shandle2,
73*758e9fbaSOystein Eftevaag ESYS_TR shandle3,
74*758e9fbaSOystein Eftevaag TPM2_CAP capability,
75*758e9fbaSOystein Eftevaag UINT32 property,
76*758e9fbaSOystein Eftevaag UINT32 propertyCount,
77*758e9fbaSOystein Eftevaag TPMI_YES_NO *moreData,
78*758e9fbaSOystein Eftevaag TPMS_CAPABILITY_DATA **capabilityData)
79*758e9fbaSOystein Eftevaag {
80*758e9fbaSOystein Eftevaag TSS2_RC r;
81*758e9fbaSOystein Eftevaag
82*758e9fbaSOystein Eftevaag r = Esys_GetCapability_Async(esysContext, shandle1, shandle2, shandle3,
83*758e9fbaSOystein Eftevaag capability, property, propertyCount);
84*758e9fbaSOystein Eftevaag return_if_error(r, "Error in async function");
85*758e9fbaSOystein Eftevaag
86*758e9fbaSOystein Eftevaag /* Set the timeout to indefinite for now, since we want _Finish to block */
87*758e9fbaSOystein Eftevaag int32_t timeouttmp = esysContext->timeout;
88*758e9fbaSOystein Eftevaag esysContext->timeout = -1;
89*758e9fbaSOystein Eftevaag /*
90*758e9fbaSOystein Eftevaag * Now we call the finish function, until return code is not equal to
91*758e9fbaSOystein Eftevaag * from TSS2_BASE_RC_TRY_AGAIN.
92*758e9fbaSOystein Eftevaag * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
93*758e9fbaSOystein Eftevaag * have set the timeout to -1. This occurs for example if the TPM requests
94*758e9fbaSOystein Eftevaag * a retransmission of the command via TPM2_RC_YIELDED.
95*758e9fbaSOystein Eftevaag */
96*758e9fbaSOystein Eftevaag do {
97*758e9fbaSOystein Eftevaag r = Esys_GetCapability_Finish(esysContext, moreData, capabilityData);
98*758e9fbaSOystein Eftevaag /* This is just debug information about the reattempt to finish the
99*758e9fbaSOystein Eftevaag command */
100*758e9fbaSOystein Eftevaag if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
101*758e9fbaSOystein Eftevaag LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
102*758e9fbaSOystein Eftevaag " => resubmitting command", r);
103*758e9fbaSOystein Eftevaag } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
104*758e9fbaSOystein Eftevaag
105*758e9fbaSOystein Eftevaag /* Restore the timeout value to the original value */
106*758e9fbaSOystein Eftevaag esysContext->timeout = timeouttmp;
107*758e9fbaSOystein Eftevaag return_if_error(r, "Esys Finish");
108*758e9fbaSOystein Eftevaag
109*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
110*758e9fbaSOystein Eftevaag }
111*758e9fbaSOystein Eftevaag
112*758e9fbaSOystein Eftevaag /** Asynchronous function for TPM2_GetCapability
113*758e9fbaSOystein Eftevaag *
114*758e9fbaSOystein Eftevaag * This function invokes the TPM2_GetCapability command in a asynchronous
115*758e9fbaSOystein Eftevaag * variant. This means the function will return as soon as the command has been
116*758e9fbaSOystein Eftevaag * sent downwards the stack to the TPM. All input parameters are const.
117*758e9fbaSOystein Eftevaag * In order to retrieve the TPM's response call Esys_GetCapability_Finish.
118*758e9fbaSOystein Eftevaag *
119*758e9fbaSOystein Eftevaag * @param[in,out] esysContext The ESYS_CONTEXT.
120*758e9fbaSOystein Eftevaag * @param[in] shandle1 First session handle.
121*758e9fbaSOystein Eftevaag * @param[in] shandle2 Second session handle.
122*758e9fbaSOystein Eftevaag * @param[in] shandle3 Third session handle.
123*758e9fbaSOystein Eftevaag * @param[in] capability Group selection; determines the format of the
124*758e9fbaSOystein Eftevaag * response.
125*758e9fbaSOystein Eftevaag * @param[in] property Further definition of information.
126*758e9fbaSOystein Eftevaag * @param[in] propertyCount Number of properties of the indicated type to
127*758e9fbaSOystein Eftevaag * return.
128*758e9fbaSOystein Eftevaag * @retval ESYS_RC_SUCCESS if the function call was a success.
129*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
130*758e9fbaSOystein Eftevaag * pointers or required output handle references are NULL.
131*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
132*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
133*758e9fbaSOystein Eftevaag * internal operations or return parameters.
134*758e9fbaSOystein Eftevaag * @retval TSS2_RCs produced by lower layers of the software stack may be
135*758e9fbaSOystein Eftevaag returned to the caller unaltered unless handled internally.
136*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
137*758e9fbaSOystein Eftevaag * the 'decrypt' attribute bit set.
138*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
139*758e9fbaSOystein Eftevaag * the 'encrypt' attribute bit set.
140*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
141*758e9fbaSOystein Eftevaag * 'decrypt' attribute set and the command does not support encryption
142*758e9fbaSOystein Eftevaag * of the first command parameter.
143*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
144*758e9fbaSOystein Eftevaag * 'encrypt' attribute set and the command does not support encryption
145*758e9fbaSOystein Eftevaag * of the first response parameter.
146*758e9fbaSOystein Eftevaag */
147*758e9fbaSOystein Eftevaag TSS2_RC
Esys_GetCapability_Async(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPM2_CAP capability,UINT32 property,UINT32 propertyCount)148*758e9fbaSOystein Eftevaag Esys_GetCapability_Async(
149*758e9fbaSOystein Eftevaag ESYS_CONTEXT *esysContext,
150*758e9fbaSOystein Eftevaag ESYS_TR shandle1,
151*758e9fbaSOystein Eftevaag ESYS_TR shandle2,
152*758e9fbaSOystein Eftevaag ESYS_TR shandle3,
153*758e9fbaSOystein Eftevaag TPM2_CAP capability,
154*758e9fbaSOystein Eftevaag UINT32 property,
155*758e9fbaSOystein Eftevaag UINT32 propertyCount)
156*758e9fbaSOystein Eftevaag {
157*758e9fbaSOystein Eftevaag TSS2_RC r;
158*758e9fbaSOystein Eftevaag LOG_TRACE("context=%p, capability=%"PRIx32 ", property=%"PRIx32 ","
159*758e9fbaSOystein Eftevaag "propertyCount=%"PRIx32 "",
160*758e9fbaSOystein Eftevaag esysContext, capability, property, propertyCount);
161*758e9fbaSOystein Eftevaag TSS2L_SYS_AUTH_COMMAND auths;
162*758e9fbaSOystein Eftevaag
163*758e9fbaSOystein Eftevaag /* Check context, sequence correctness and set state to error for now */
164*758e9fbaSOystein Eftevaag if (esysContext == NULL) {
165*758e9fbaSOystein Eftevaag LOG_ERROR("esyscontext is NULL.");
166*758e9fbaSOystein Eftevaag return TSS2_ESYS_RC_BAD_REFERENCE;
167*758e9fbaSOystein Eftevaag }
168*758e9fbaSOystein Eftevaag r = iesys_check_sequence_async(esysContext);
169*758e9fbaSOystein Eftevaag if (r != TSS2_RC_SUCCESS)
170*758e9fbaSOystein Eftevaag return r;
171*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_INTERNALERROR;
172*758e9fbaSOystein Eftevaag
173*758e9fbaSOystein Eftevaag /* Check input parameters */
174*758e9fbaSOystein Eftevaag r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
175*758e9fbaSOystein Eftevaag return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
176*758e9fbaSOystein Eftevaag
177*758e9fbaSOystein Eftevaag /* Initial invocation of SAPI to prepare the command buffer with parameters */
178*758e9fbaSOystein Eftevaag r = Tss2_Sys_GetCapability_Prepare(esysContext->sys, capability, property,
179*758e9fbaSOystein Eftevaag propertyCount);
180*758e9fbaSOystein Eftevaag return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
181*758e9fbaSOystein Eftevaag
182*758e9fbaSOystein Eftevaag /* Calculate the cpHash Values */
183*758e9fbaSOystein Eftevaag r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
184*758e9fbaSOystein Eftevaag return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
185*758e9fbaSOystein Eftevaag iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
186*758e9fbaSOystein Eftevaag iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
187*758e9fbaSOystein Eftevaag iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
188*758e9fbaSOystein Eftevaag
189*758e9fbaSOystein Eftevaag /* Generate the auth values and set them in the SAPI command buffer */
190*758e9fbaSOystein Eftevaag r = iesys_gen_auths(esysContext, NULL, NULL, NULL, &auths);
191*758e9fbaSOystein Eftevaag return_state_if_error(r, _ESYS_STATE_INIT,
192*758e9fbaSOystein Eftevaag "Error in computation of auth values");
193*758e9fbaSOystein Eftevaag
194*758e9fbaSOystein Eftevaag esysContext->authsCount = auths.count;
195*758e9fbaSOystein Eftevaag if (auths.count > 0) {
196*758e9fbaSOystein Eftevaag r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
197*758e9fbaSOystein Eftevaag return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
198*758e9fbaSOystein Eftevaag }
199*758e9fbaSOystein Eftevaag
200*758e9fbaSOystein Eftevaag /* Trigger execution and finish the async invocation */
201*758e9fbaSOystein Eftevaag r = Tss2_Sys_ExecuteAsync(esysContext->sys);
202*758e9fbaSOystein Eftevaag return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
203*758e9fbaSOystein Eftevaag "Finish (Execute Async)");
204*758e9fbaSOystein Eftevaag
205*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_SENT;
206*758e9fbaSOystein Eftevaag
207*758e9fbaSOystein Eftevaag return r;
208*758e9fbaSOystein Eftevaag }
209*758e9fbaSOystein Eftevaag
210*758e9fbaSOystein Eftevaag /** Asynchronous finish function for TPM2_GetCapability
211*758e9fbaSOystein Eftevaag *
212*758e9fbaSOystein Eftevaag * This function returns the results of a TPM2_GetCapability command
213*758e9fbaSOystein Eftevaag * invoked via Esys_GetCapability_Finish. All non-simple output parameters
214*758e9fbaSOystein Eftevaag * are allocated by the function's implementation. NULL can be passed for every
215*758e9fbaSOystein Eftevaag * output parameter if the value is not required.
216*758e9fbaSOystein Eftevaag *
217*758e9fbaSOystein Eftevaag * @param[in,out] esysContext The ESYS_CONTEXT.
218*758e9fbaSOystein Eftevaag * @param[out] moreData Flag to indicate if there are more values of this type.
219*758e9fbaSOystein Eftevaag * (callee-allocated)
220*758e9fbaSOystein Eftevaag * @param[out] capabilityData The capability data.
221*758e9fbaSOystein Eftevaag * (callee-allocated)
222*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS on success
223*758e9fbaSOystein Eftevaag * @retval ESYS_RC_SUCCESS if the function call was a success.
224*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
225*758e9fbaSOystein Eftevaag * pointers or required output handle references are NULL.
226*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
227*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
228*758e9fbaSOystein Eftevaag * internal operations or return parameters.
229*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
230*758e9fbaSOystein Eftevaag * operation already pending.
231*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
232*758e9fbaSOystein Eftevaag * TPM response is received.
233*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
234*758e9fbaSOystein Eftevaag * at least contain the tag, response length, and response code.
235*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
236*758e9fbaSOystein Eftevaag * not verify.
237*758e9fbaSOystein Eftevaag * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
238*758e9fbaSOystein Eftevaag * @retval TSS2_RCs produced by lower layers of the software stack may be
239*758e9fbaSOystein Eftevaag * returned to the caller unaltered unless handled internally.
240*758e9fbaSOystein Eftevaag */
241*758e9fbaSOystein Eftevaag TSS2_RC
Esys_GetCapability_Finish(ESYS_CONTEXT * esysContext,TPMI_YES_NO * moreData,TPMS_CAPABILITY_DATA ** capabilityData)242*758e9fbaSOystein Eftevaag Esys_GetCapability_Finish(
243*758e9fbaSOystein Eftevaag ESYS_CONTEXT *esysContext,
244*758e9fbaSOystein Eftevaag TPMI_YES_NO *moreData,
245*758e9fbaSOystein Eftevaag TPMS_CAPABILITY_DATA **capabilityData)
246*758e9fbaSOystein Eftevaag {
247*758e9fbaSOystein Eftevaag TSS2_RC r;
248*758e9fbaSOystein Eftevaag LOG_TRACE("context=%p, moreData=%p, capabilityData=%p",
249*758e9fbaSOystein Eftevaag esysContext, moreData, capabilityData);
250*758e9fbaSOystein Eftevaag
251*758e9fbaSOystein Eftevaag if (esysContext == NULL) {
252*758e9fbaSOystein Eftevaag LOG_ERROR("esyscontext is NULL.");
253*758e9fbaSOystein Eftevaag return TSS2_ESYS_RC_BAD_REFERENCE;
254*758e9fbaSOystein Eftevaag }
255*758e9fbaSOystein Eftevaag
256*758e9fbaSOystein Eftevaag /* Check for correct sequence and set sequence to irregular for now */
257*758e9fbaSOystein Eftevaag if (esysContext->state != _ESYS_STATE_SENT &&
258*758e9fbaSOystein Eftevaag esysContext->state != _ESYS_STATE_RESUBMISSION) {
259*758e9fbaSOystein Eftevaag LOG_ERROR("Esys called in bad sequence.");
260*758e9fbaSOystein Eftevaag return TSS2_ESYS_RC_BAD_SEQUENCE;
261*758e9fbaSOystein Eftevaag }
262*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_INTERNALERROR;
263*758e9fbaSOystein Eftevaag
264*758e9fbaSOystein Eftevaag /* Allocate memory for response parameters */
265*758e9fbaSOystein Eftevaag if (capabilityData != NULL) {
266*758e9fbaSOystein Eftevaag *capabilityData = calloc(sizeof(TPMS_CAPABILITY_DATA), 1);
267*758e9fbaSOystein Eftevaag if (*capabilityData == NULL) {
268*758e9fbaSOystein Eftevaag return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
269*758e9fbaSOystein Eftevaag }
270*758e9fbaSOystein Eftevaag }
271*758e9fbaSOystein Eftevaag
272*758e9fbaSOystein Eftevaag /*Receive the TPM response and handle resubmissions if necessary. */
273*758e9fbaSOystein Eftevaag r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
274*758e9fbaSOystein Eftevaag if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
275*758e9fbaSOystein Eftevaag LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
276*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_SENT;
277*758e9fbaSOystein Eftevaag goto error_cleanup;
278*758e9fbaSOystein Eftevaag }
279*758e9fbaSOystein Eftevaag /* This block handle the resubmission of TPM commands given a certain set of
280*758e9fbaSOystein Eftevaag * TPM response codes. */
281*758e9fbaSOystein Eftevaag if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
282*758e9fbaSOystein Eftevaag LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
283*758e9fbaSOystein Eftevaag "resubmission: %" PRIx32, r);
284*758e9fbaSOystein Eftevaag if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
285*758e9fbaSOystein Eftevaag LOG_WARNING("Maximum number of (re)submissions has been reached.");
286*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_INIT;
287*758e9fbaSOystein Eftevaag goto error_cleanup;
288*758e9fbaSOystein Eftevaag }
289*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_RESUBMISSION;
290*758e9fbaSOystein Eftevaag r = Tss2_Sys_ExecuteAsync(esysContext->sys);
291*758e9fbaSOystein Eftevaag if (r != TSS2_RC_SUCCESS) {
292*758e9fbaSOystein Eftevaag LOG_WARNING("Error attempting to resubmit");
293*758e9fbaSOystein Eftevaag /* We do not set esysContext->state here but inherit the most recent
294*758e9fbaSOystein Eftevaag * state of the _async function. */
295*758e9fbaSOystein Eftevaag goto error_cleanup;
296*758e9fbaSOystein Eftevaag }
297*758e9fbaSOystein Eftevaag r = TSS2_ESYS_RC_TRY_AGAIN;
298*758e9fbaSOystein Eftevaag LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
299*758e9fbaSOystein Eftevaag goto error_cleanup;
300*758e9fbaSOystein Eftevaag }
301*758e9fbaSOystein Eftevaag /* The following is the "regular error" handling. */
302*758e9fbaSOystein Eftevaag if (iesys_tpm_error(r)) {
303*758e9fbaSOystein Eftevaag LOG_WARNING("Received TPM Error");
304*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_INIT;
305*758e9fbaSOystein Eftevaag goto error_cleanup;
306*758e9fbaSOystein Eftevaag } else if (r != TSS2_RC_SUCCESS) {
307*758e9fbaSOystein Eftevaag LOG_ERROR("Received a non-TPM Error");
308*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_INTERNALERROR;
309*758e9fbaSOystein Eftevaag goto error_cleanup;
310*758e9fbaSOystein Eftevaag }
311*758e9fbaSOystein Eftevaag
312*758e9fbaSOystein Eftevaag /*
313*758e9fbaSOystein Eftevaag * Now the verification of the response (hmac check) and if necessary the
314*758e9fbaSOystein Eftevaag * parameter decryption have to be done.
315*758e9fbaSOystein Eftevaag */
316*758e9fbaSOystein Eftevaag r = iesys_check_response(esysContext);
317*758e9fbaSOystein Eftevaag goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
318*758e9fbaSOystein Eftevaag error_cleanup);
319*758e9fbaSOystein Eftevaag
320*758e9fbaSOystein Eftevaag /*
321*758e9fbaSOystein Eftevaag * After the verification of the response we call the complete function
322*758e9fbaSOystein Eftevaag * to deliver the result.
323*758e9fbaSOystein Eftevaag */
324*758e9fbaSOystein Eftevaag r = Tss2_Sys_GetCapability_Complete(esysContext->sys, moreData,
325*758e9fbaSOystein Eftevaag (capabilityData != NULL)
326*758e9fbaSOystein Eftevaag ? *capabilityData : NULL);
327*758e9fbaSOystein Eftevaag goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
328*758e9fbaSOystein Eftevaag "Received error from SAPI unmarshaling" ,
329*758e9fbaSOystein Eftevaag error_cleanup);
330*758e9fbaSOystein Eftevaag
331*758e9fbaSOystein Eftevaag esysContext->state = _ESYS_STATE_INIT;
332*758e9fbaSOystein Eftevaag
333*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
334*758e9fbaSOystein Eftevaag
335*758e9fbaSOystein Eftevaag error_cleanup:
336*758e9fbaSOystein Eftevaag if (capabilityData != NULL)
337*758e9fbaSOystein Eftevaag SAFE_FREE(*capabilityData);
338*758e9fbaSOystein Eftevaag
339*758e9fbaSOystein Eftevaag return r;
340*758e9fbaSOystein Eftevaag }
341