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