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 /** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
store_input_parameters(ESYS_CONTEXT * esysContext,const TPMS_CONTEXT * context)23 static void store_input_parameters (
24 ESYS_CONTEXT *esysContext,
25 const TPMS_CONTEXT *context)
26 {
27 if (context == NULL) {
28 esysContext->in.ContextLoad.context = NULL;
29 } else {
30 esysContext->in.ContextLoad.contextData = *context;
31 esysContext->in.ContextLoad.context =
32 &esysContext->in.ContextLoad.contextData;
33 }
34 }
35
36 /** One-Call function for TPM2_ContextLoad
37 *
38 * This function invokes the TPM2_ContextLoad command in a one-call
39 * variant. This means the function will block until the TPM response is
40 * available. All input parameters are const. The memory for non-simple output
41 * parameters is allocated by the function implementation.
42 *
43 * @param[in,out] esysContext The ESYS_CONTEXT.
44 * @param[in] context The context blob.
45 * @param[out] loadedHandle ESYS_TR handle of ESYS resource for TPMI_DH_CONTEXT.
46 * @retval TSS2_RC_SUCCESS if the function call was a success.
47 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
48 * pointers or required output handle references are NULL.
49 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
50 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
51 * internal operations or return parameters.
52 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
53 * operation already pending.
54 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
55 * at least contain the tag, response length, and response code.
56 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
57 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
58 did not verify.
59 * @retval TSS2_RCs produced by lower layers of the software stack may be
60 * returned to the caller unaltered unless handled internally.
61 */
62 TSS2_RC
Esys_ContextLoad(ESYS_CONTEXT * esysContext,const TPMS_CONTEXT * context,ESYS_TR * loadedHandle)63 Esys_ContextLoad(
64 ESYS_CONTEXT *esysContext,
65 const TPMS_CONTEXT *context, ESYS_TR *loadedHandle)
66 {
67 TSS2_RC r;
68
69 r = Esys_ContextLoad_Async(esysContext, context);
70 return_if_error(r, "Error in async function");
71
72 /* Set the timeout to indefinite for now, since we want _Finish to block */
73 int32_t timeouttmp = esysContext->timeout;
74 esysContext->timeout = -1;
75 /*
76 * Now we call the finish function, until return code is not equal to
77 * from TSS2_BASE_RC_TRY_AGAIN.
78 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
79 * have set the timeout to -1. This occurs for example if the TPM requests
80 * a retransmission of the command via TPM2_RC_YIELDED.
81 */
82 do {
83 r = Esys_ContextLoad_Finish(esysContext, loadedHandle);
84 /* This is just debug information about the reattempt to finish the
85 command */
86 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
87 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
88 " => resubmitting command", r);
89 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
90
91 /* Restore the timeout value to the original value */
92 esysContext->timeout = timeouttmp;
93 return_if_error(r, "Esys Finish");
94
95 return TSS2_RC_SUCCESS;
96 }
97
98 /** Asynchronous function for TPM2_ContextLoad
99 *
100 * This function invokes the TPM2_ContextLoad command in a asynchronous
101 * variant. This means the function will return as soon as the command has been
102 * sent downwards the stack to the TPM. All input parameters are const.
103 * In order to retrieve the TPM's response call Esys_ContextLoad_Finish.
104 *
105 * @param[in,out] esysContext The ESYS_CONTEXT.
106 * @param[in] context The context blob.
107 * @retval ESYS_RC_SUCCESS if the function call was a success.
108 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
109 * pointers or required output handle references are NULL.
110 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
111 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
112 * internal operations or return parameters.
113 * @retval TSS2_RCs produced by lower layers of the software stack may be
114 returned to the caller unaltered unless handled internally.
115 */
116 TSS2_RC
Esys_ContextLoad_Async(ESYS_CONTEXT * esysContext,const TPMS_CONTEXT * context)117 Esys_ContextLoad_Async(
118 ESYS_CONTEXT *esysContext,
119 const TPMS_CONTEXT *context)
120 {
121 TSS2_RC r;
122 LOG_TRACE("context=%p, context=%p",
123 esysContext, context);
124 IESYS_CONTEXT_DATA esyscontextData;
125 TPMS_CONTEXT tpmContext;
126
127 /* Check context, sequence correctness and set state to error for now */
128 if (esysContext == NULL) {
129 LOG_ERROR("esyscontext is NULL.");
130 return TSS2_ESYS_RC_BAD_REFERENCE;
131 }
132 r = iesys_check_sequence_async(esysContext);
133 if (r != TSS2_RC_SUCCESS)
134 return r;
135 esysContext->state = _ESYS_STATE_INTERNALERROR;
136 store_input_parameters(esysContext, context);
137 size_t offset = 0;
138
139 /*
140 * ESYS Special Handling Code: The context was extended with metadata during
141 * Esys_ContextSave. Here we extract the TPM-parts to pass then to the TPM.
142 */
143 if (context == NULL) {
144 LOG_ERROR("context is NULL.");
145 return TSS2_ESYS_RC_BAD_REFERENCE;
146 }
147 r = iesys_MU_IESYS_CONTEXT_DATA_Unmarshal (&context->contextBlob.buffer[0],
148 context->contextBlob.size,
149 &offset, &esyscontextData);
150 return_if_error(r, "while unmarshaling context ");
151
152 /* The actual contextBlob for the TPM is embedded inside the
153 ESYS_CONTEXT_DATA. Some of the values at the start of TPMS_CONTEXT
154 need to be kept though. */
155
156 tpmContext.sequence = context->sequence;
157 tpmContext.savedHandle = context->savedHandle;
158 tpmContext.hierarchy = context->hierarchy;
159 tpmContext.contextBlob = esyscontextData.tpmContext;
160
161 /* Now we override the context parameter with the corrected version, since
162 it is nowhere used beyond this point. */
163 context = &tpmContext;
164
165
166 /* Initial invocation of SAPI to prepare the command buffer with parameters */
167 r = Tss2_Sys_ContextLoad_Prepare(esysContext->sys, context);
168 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
169 /* Trigger execution and finish the async invocation */
170 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
171 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
172 "Finish (Execute Async)");
173
174 esysContext->state = _ESYS_STATE_SENT;
175
176 return r;
177 }
178
179 /** Asynchronous finish function for TPM2_ContextLoad
180 *
181 * This function returns the results of a TPM2_ContextLoad command
182 * invoked via Esys_ContextLoad_Finish. All non-simple output parameters
183 * are allocated by the function's implementation. NULL can be passed for every
184 * output parameter if the value is not required.
185 *
186 * @param[in,out] esysContext The ESYS_CONTEXT.
187 * @param[out] loadedHandle ESYS_TR handle of ESYS resource for TPMI_DH_CONTEXT.
188 * @retval TSS2_RC_SUCCESS on success
189 * @retval ESYS_RC_SUCCESS if the function call was a success.
190 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
191 * pointers or required output handle references are NULL.
192 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
193 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
194 * internal operations or return parameters.
195 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
196 * operation already pending.
197 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
198 * TPM response is received.
199 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
200 * at least contain the tag, response length, and response code.
201 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
202 * not verify.
203 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
204 * @retval TSS2_RCs produced by lower layers of the software stack may be
205 * returned to the caller unaltered unless handled internally.
206 */
207 TSS2_RC
Esys_ContextLoad_Finish(ESYS_CONTEXT * esysContext,ESYS_TR * loadedHandle)208 Esys_ContextLoad_Finish(
209 ESYS_CONTEXT *esysContext, ESYS_TR *loadedHandle)
210 {
211 TSS2_RC r;
212 LOG_TRACE("context=%p, loadedHandle=%p",
213 esysContext, loadedHandle);
214
215 if (esysContext == NULL) {
216 LOG_ERROR("esyscontext is NULL.");
217 return TSS2_ESYS_RC_BAD_REFERENCE;
218 }
219
220 /* Check for correct sequence and set sequence to irregular for now */
221 if (esysContext->state != _ESYS_STATE_SENT &&
222 esysContext->state != _ESYS_STATE_RESUBMISSION) {
223 LOG_ERROR("Esys called in bad sequence.");
224 return TSS2_ESYS_RC_BAD_SEQUENCE;
225 }
226 esysContext->state = _ESYS_STATE_INTERNALERROR;
227 RSRC_NODE_T *loadedHandleNode = NULL;
228
229 /* Allocate memory for response parameters */
230 if (loadedHandle == NULL) {
231 LOG_ERROR("Handle loadedHandle may not be NULL");
232 return TSS2_ESYS_RC_BAD_REFERENCE;
233 }
234 *loadedHandle = esysContext->esys_handle_cnt++;
235 r = esys_CreateResourceObject(esysContext, *loadedHandle, &loadedHandleNode);
236 if (r != TSS2_RC_SUCCESS)
237 return r;
238
239 IESYS_CONTEXT_DATA esyscontextData;
240 size_t offset = 0;
241 r = iesys_MU_IESYS_CONTEXT_DATA_Unmarshal(&esysContext->in.ContextLoad.context->contextBlob.buffer[0],
242 sizeof(IESYS_CONTEXT_DATA),
243 &offset, &esyscontextData);
244 goto_if_error(r, "while unmarshaling context ", error_cleanup);
245
246 loadedHandleNode->rsrc = esyscontextData.esysMetadata.data;
247
248 /*Receive the TPM response and handle resubmissions if necessary. */
249 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
250 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
251 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
252 esysContext->state = _ESYS_STATE_SENT;
253 goto error_cleanup;
254 }
255 /* This block handle the resubmission of TPM commands given a certain set of
256 * TPM response codes. */
257 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
258 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
259 "resubmission: %" PRIx32, r);
260 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
261 LOG_WARNING("Maximum number of (re)submissions has been reached.");
262 esysContext->state = _ESYS_STATE_INIT;
263 goto error_cleanup;
264 }
265 esysContext->state = _ESYS_STATE_RESUBMISSION;
266 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
267 if (r != TSS2_RC_SUCCESS) {
268 LOG_WARNING("Error attempting to resubmit");
269 /* We do not set esysContext->state here but inherit the most recent
270 * state of the _async function. */
271 goto error_cleanup;
272 }
273 r = TSS2_ESYS_RC_TRY_AGAIN;
274 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
275 goto error_cleanup;
276 }
277 /* The following is the "regular error" handling. */
278 if (iesys_tpm_error(r)) {
279 LOG_WARNING("Received TPM Error");
280 esysContext->state = _ESYS_STATE_INIT;
281 goto error_cleanup;
282 } else if (r != TSS2_RC_SUCCESS) {
283 LOG_ERROR("Received a non-TPM Error");
284 esysContext->state = _ESYS_STATE_INTERNALERROR;
285 goto error_cleanup;
286 }
287 r = Tss2_Sys_ContextLoad_Complete(esysContext->sys,
288 &loadedHandleNode->rsrc.handle);
289 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
290 "Received error from SAPI unmarshaling" ,
291 error_cleanup);
292
293 esysContext->state = _ESYS_STATE_INIT;
294
295 return TSS2_RC_SUCCESS;
296
297 error_cleanup:
298 Esys_TR_Close(esysContext, loadedHandle);
299
300 return r;
301 }
302