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