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