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