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