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