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_ReadClock
23 *
24 * This function invokes the TPM2_ReadClock 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[out] currentTime .
34 * (callee-allocated)
35 * @retval TSS2_RC_SUCCESS if the function call was a success.
36 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
37 * pointers or required output handle references are NULL.
38 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
39 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
40 * internal operations or return parameters.
41 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
42 * operation already pending.
43 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
44 * at least contain the tag, response length, and response code.
45 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
46 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
47 did not verify.
48 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
49 * the 'decrypt' attribute bit set.
50 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
51 * the 'encrypt' attribute bit set.
52 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
53 * 'decrypt' attribute set and the command does not support encryption
54 * of the first command parameter.
55 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
56 * 'encrypt' attribute set and the command does not support encryption
57 * of the first response parameter.
58 * @retval TSS2_RCs produced by lower layers of the software stack may be
59 * returned to the caller unaltered unless handled internally.
60 */
61 TSS2_RC
Esys_ReadClock(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPMS_TIME_INFO ** currentTime)62 Esys_ReadClock(
63 ESYS_CONTEXT *esysContext,
64 ESYS_TR shandle1,
65 ESYS_TR shandle2,
66 ESYS_TR shandle3,
67 TPMS_TIME_INFO **currentTime)
68 {
69 TSS2_RC r;
70
71 r = Esys_ReadClock_Async(esysContext, shandle1, shandle2, shandle3);
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_ReadClock_Finish(esysContext, currentTime);
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_ReadClock
101 *
102 * This function invokes the TPM2_ReadClock 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_ReadClock_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 * @retval ESYS_RC_SUCCESS if the function call was a success.
112 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
113 * pointers or required output handle references are NULL.
114 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
115 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
116 * internal operations or return parameters.
117 * @retval TSS2_RCs produced by lower layers of the software stack may be
118 returned to the caller unaltered unless handled internally.
119 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
120 * the 'decrypt' attribute bit set.
121 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
122 * the 'encrypt' attribute bit set.
123 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
124 * 'decrypt' attribute set and the command does not support encryption
125 * of the first command parameter.
126 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
127 * 'encrypt' attribute set and the command does not support encryption
128 * of the first response parameter.
129 */
130 TSS2_RC
Esys_ReadClock_Async(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)131 Esys_ReadClock_Async(
132 ESYS_CONTEXT *esysContext,
133 ESYS_TR shandle1,
134 ESYS_TR shandle2,
135 ESYS_TR shandle3)
136 {
137 TSS2_RC r;
138 LOG_TRACE("context=%p",
139 esysContext);
140 TSS2L_SYS_AUTH_COMMAND auths;
141
142 /* Check context, sequence correctness and set state to error for now */
143 if (esysContext == NULL) {
144 LOG_ERROR("esyscontext is NULL.");
145 return TSS2_ESYS_RC_BAD_REFERENCE;
146 }
147 r = iesys_check_sequence_async(esysContext);
148 if (r != TSS2_RC_SUCCESS)
149 return r;
150 esysContext->state = _ESYS_STATE_INTERNALERROR;
151
152 /* Check input parameters */
153 r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
154 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
155
156 /* Initial invocation of SAPI to prepare the command buffer with parameters */
157 r = Tss2_Sys_ReadClock_Prepare(esysContext->sys);
158 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
159
160 /* Calculate the cpHash Values */
161 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
162 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
163 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
164 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
165 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
166
167 /* Generate the auth values and set them in the SAPI command buffer */
168 r = iesys_gen_auths(esysContext, NULL, NULL, NULL, &auths);
169 return_state_if_error(r, _ESYS_STATE_INIT,
170 "Error in computation of auth values");
171
172 esysContext->authsCount = auths.count;
173 if (auths.count > 0) {
174 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
175 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
176 }
177
178 /* Trigger execution and finish the async invocation */
179 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
180 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
181 "Finish (Execute Async)");
182
183 esysContext->state = _ESYS_STATE_SENT;
184
185 return r;
186 }
187
188 /** Asynchronous finish function for TPM2_ReadClock
189 *
190 * This function returns the results of a TPM2_ReadClock command
191 * invoked via Esys_ReadClock_Finish. All non-simple output parameters
192 * are allocated by the function's implementation. NULL can be passed for every
193 * output parameter if the value is not required.
194 *
195 * @param[in,out] esysContext The ESYS_CONTEXT.
196 * @param[out] currentTime .
197 * (callee-allocated)
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_ReadClock_Finish(ESYS_CONTEXT * esysContext,TPMS_TIME_INFO ** currentTime)218 Esys_ReadClock_Finish(
219 ESYS_CONTEXT *esysContext,
220 TPMS_TIME_INFO **currentTime)
221 {
222 TSS2_RC r;
223 LOG_TRACE("context=%p, currentTime=%p",
224 esysContext, currentTime);
225
226 if (esysContext == NULL) {
227 LOG_ERROR("esyscontext is NULL.");
228 return TSS2_ESYS_RC_BAD_REFERENCE;
229 }
230
231 /* Check for correct sequence and set sequence to irregular for now */
232 if (esysContext->state != _ESYS_STATE_SENT &&
233 esysContext->state != _ESYS_STATE_RESUBMISSION) {
234 LOG_ERROR("Esys called in bad sequence.");
235 return TSS2_ESYS_RC_BAD_SEQUENCE;
236 }
237 esysContext->state = _ESYS_STATE_INTERNALERROR;
238
239 /* Allocate memory for response parameters */
240 if (currentTime != NULL) {
241 *currentTime = calloc(sizeof(TPMS_TIME_INFO), 1);
242 if (*currentTime == NULL) {
243 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
244 }
245 }
246
247 /*Receive the TPM response and handle resubmissions if necessary. */
248 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
249 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
250 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
251 esysContext->state = _ESYS_STATE_SENT;
252 goto error_cleanup;
253 }
254 /* This block handle the resubmission of TPM commands given a certain set of
255 * TPM response codes. */
256 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
257 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
258 "resubmission: %" PRIx32, r);
259 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
260 LOG_WARNING("Maximum number of (re)submissions has been reached.");
261 esysContext->state = _ESYS_STATE_INIT;
262 goto error_cleanup;
263 }
264 esysContext->state = _ESYS_STATE_RESUBMISSION;
265 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
266 if (r != TSS2_RC_SUCCESS) {
267 LOG_WARNING("Error attempting to resubmit");
268 /* We do not set esysContext->state here but inherit the most recent
269 * state of the _async function. */
270 goto error_cleanup;
271 }
272 r = TSS2_ESYS_RC_TRY_AGAIN;
273 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
274 goto error_cleanup;
275 }
276 /* The following is the "regular error" handling. */
277 if (iesys_tpm_error(r)) {
278 LOG_WARNING("Received TPM Error");
279 esysContext->state = _ESYS_STATE_INIT;
280 goto error_cleanup;
281 } else if (r != TSS2_RC_SUCCESS) {
282 LOG_ERROR("Received a non-TPM Error");
283 esysContext->state = _ESYS_STATE_INTERNALERROR;
284 goto error_cleanup;
285 }
286
287 /*
288 * Now the verification of the response (hmac check) and if necessary the
289 * parameter decryption have to be done.
290 */
291 r = iesys_check_response(esysContext);
292 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
293 error_cleanup);
294
295 /*
296 * After the verification of the response we call the complete function
297 * to deliver the result.
298 */
299 r = Tss2_Sys_ReadClock_Complete(esysContext->sys,
300 (currentTime != NULL) ? *currentTime
301 : NULL);
302 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
303 "Received error from SAPI unmarshaling" ,
304 error_cleanup);
305
306 esysContext->state = _ESYS_STATE_INIT;
307
308 return TSS2_RC_SUCCESS;
309
310 error_cleanup:
311 if (currentTime != NULL)
312 SAFE_FREE(*currentTime);
313
314 return r;
315 }
316