• 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 /** One-Call function for TPM2_Startup
23  *
24  * This function invokes the TPM2_Startup command in a one-call
25  * variant. This means the function will block until the TPM response is
26  * available. All input parameters are const. The memory for non-simple output
27  * parameters is allocated by the function implementation.
28  *
29  * @param[in,out] esysContext The ESYS_CONTEXT.
30  * @param[in]  startupType TPM2_SU_CLEAR or TPM2_SU_STATE.
31  * @retval TSS2_RC_SUCCESS if the function call was a success.
32  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
33  *         pointers or required output handle references are NULL.
34  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
35  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
36  *         internal operations or return parameters.
37  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
38  *         operation already pending.
39  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
40  *          at least contain the tag, response length, and response code.
41  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
42  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
43            did not verify.
44  * @retval TSS2_RCs produced by lower layers of the software stack may be
45  *         returned to the caller unaltered unless handled internally.
46  */
47 TSS2_RC
Esys_Startup(ESYS_CONTEXT * esysContext,TPM2_SU startupType)48 Esys_Startup(
49     ESYS_CONTEXT *esysContext,
50     TPM2_SU startupType)
51 {
52     TSS2_RC r;
53 
54     r = Esys_Startup_Async(esysContext, startupType);
55     return_if_error(r, "Error in async function");
56 
57     /* Set the timeout to indefinite for now, since we want _Finish to block */
58     int32_t timeouttmp = esysContext->timeout;
59     esysContext->timeout = -1;
60     /*
61      * Now we call the finish function, until return code is not equal to
62      * from TSS2_BASE_RC_TRY_AGAIN.
63      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
64      * have set the timeout to -1. This occurs for example if the TPM requests
65      * a retransmission of the command via TPM2_RC_YIELDED.
66      */
67     do {
68         r = Esys_Startup_Finish(esysContext);
69         /* This is just debug information about the reattempt to finish the
70            command */
71         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
72             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
73                       " => resubmitting command", r);
74     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
75 
76     /* Restore the timeout value to the original value */
77     esysContext->timeout = timeouttmp;
78     return_if_error(r, "Esys Finish");
79 
80     return TSS2_RC_SUCCESS;
81 }
82 
83 /** Asynchronous function for TPM2_Startup
84  *
85  * This function invokes the TPM2_Startup command in a asynchronous
86  * variant. This means the function will return as soon as the command has been
87  * sent downwards the stack to the TPM. All input parameters are const.
88  * In order to retrieve the TPM's response call Esys_Startup_Finish.
89  *
90  * @param[in,out] esysContext The ESYS_CONTEXT.
91  * @param[in]  startupType TPM2_SU_CLEAR or TPM2_SU_STATE.
92  * @retval ESYS_RC_SUCCESS if the function call was a success.
93  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
94  *         pointers or required output handle references are NULL.
95  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
96  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
97  *         internal operations or return parameters.
98  * @retval TSS2_RCs produced by lower layers of the software stack may be
99            returned to the caller unaltered unless handled internally.
100  */
101 TSS2_RC
Esys_Startup_Async(ESYS_CONTEXT * esysContext,TPM2_SU startupType)102 Esys_Startup_Async(
103     ESYS_CONTEXT *esysContext,
104     TPM2_SU startupType)
105 {
106     TSS2_RC r;
107     LOG_TRACE("context=%p, startupType=%04"PRIx16"",
108               esysContext, startupType);
109 
110     /* Check context, sequence correctness and set state to error for now */
111     if (esysContext == NULL) {
112         LOG_ERROR("esyscontext is NULL.");
113         return TSS2_ESYS_RC_BAD_REFERENCE;
114     }
115     r = iesys_check_sequence_async(esysContext);
116     if (r != TSS2_RC_SUCCESS)
117         return r;
118     esysContext->state = _ESYS_STATE_INTERNALERROR;
119 
120     /* Initial invocation of SAPI to prepare the command buffer with parameters */
121     r = Tss2_Sys_Startup_Prepare(esysContext->sys, startupType);
122     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
123     /* Trigger execution and finish the async invocation */
124     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
125     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
126                           "Finish (Execute Async)");
127 
128     esysContext->state = _ESYS_STATE_SENT;
129 
130     return r;
131 }
132 
133 /** Asynchronous finish function for TPM2_Startup
134  *
135  * This function returns the results of a TPM2_Startup command
136  * invoked via Esys_Startup_Finish. All non-simple output parameters
137  * are allocated by the function's implementation. NULL can be passed for every
138  * output parameter if the value is not required.
139  *
140  * @param[in,out] esysContext The ESYS_CONTEXT.
141  * @retval TSS2_RC_SUCCESS on success
142  * @retval ESYS_RC_SUCCESS if the function call was a success.
143  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
144  *         pointers or required output handle references are NULL.
145  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
146  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
147  *         internal operations or return parameters.
148  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
149  *         operation already pending.
150  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
151  *         TPM response is received.
152  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
153  *         at least contain the tag, response length, and response code.
154  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
155  *         not verify.
156  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
157  * @retval TSS2_RCs produced by lower layers of the software stack may be
158  *         returned to the caller unaltered unless handled internally.
159  */
160 TSS2_RC
Esys_Startup_Finish(ESYS_CONTEXT * esysContext)161 Esys_Startup_Finish(
162     ESYS_CONTEXT *esysContext)
163 {
164     TSS2_RC r;
165     LOG_TRACE("context=%p",
166               esysContext);
167 
168     if (esysContext == NULL) {
169         LOG_ERROR("esyscontext is NULL.");
170         return TSS2_ESYS_RC_BAD_REFERENCE;
171     }
172 
173     /* Check for correct sequence and set sequence to irregular for now */
174     if (esysContext->state != _ESYS_STATE_SENT &&
175         esysContext->state != _ESYS_STATE_RESUBMISSION) {
176         LOG_ERROR("Esys called in bad sequence.");
177         return TSS2_ESYS_RC_BAD_SEQUENCE;
178     }
179     esysContext->state = _ESYS_STATE_INTERNALERROR;
180 
181     /*Receive the TPM response and handle resubmissions if necessary. */
182     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
183     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
184         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
185         esysContext->state = _ESYS_STATE_SENT;
186         return r;
187     }
188     /* This block handle the resubmission of TPM commands given a certain set of
189      * TPM response codes. */
190     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
191         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
192             "resubmission: %" PRIx32, r);
193         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
194             LOG_WARNING("Maximum number of (re)submissions has been reached.");
195             esysContext->state = _ESYS_STATE_INIT;
196             return r;
197         }
198         esysContext->state = _ESYS_STATE_SENT;
199 	r = Tss2_Sys_ExecuteAsync(esysContext->sys);
200         if (r != TSS2_RC_SUCCESS) {
201             LOG_WARNING("Error attempting to resubmit");
202             /* We do not set esysContext->state here but inherit the most recent
203              * state of the _async function. */
204             return r;
205         }
206         r = TSS2_ESYS_RC_TRY_AGAIN;
207         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
208         return r;
209     }
210     /* The following is the "regular error" handling. */
211     if (iesys_tpm_error(r) && r != TPM2_RC_INITIALIZE) {
212         LOG_WARNING("Received TPM Error");
213         esysContext->state = _ESYS_STATE_INIT;
214         return r;
215     } else if (r != TSS2_RC_SUCCESS && r != TPM2_RC_INITIALIZE) {
216         LOG_ERROR("Received a non-TPM Error");
217         esysContext->state = _ESYS_STATE_INTERNALERROR;
218         return r;
219     }
220     r = Tss2_Sys_Startup_Complete(esysContext->sys);
221     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
222                           "Received error from SAPI unmarshaling" );
223 
224     esysContext->state = _ESYS_STATE_INIT;
225 
226     return TSS2_RC_SUCCESS;
227 }
228