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 /** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
store_input_parameters(ESYS_CONTEXT * esysContext,ESYS_TR sequenceHandle)23 static void store_input_parameters (
24 ESYS_CONTEXT *esysContext,
25 ESYS_TR sequenceHandle)
26 {
27 esysContext->in.SequenceComplete.sequenceHandle = sequenceHandle;
28 }
29
30 /** One-Call function for TPM2_SequenceComplete
31 *
32 * This function invokes the TPM2_SequenceComplete command in a one-call
33 * variant. This means the function will block until the TPM response is
34 * available. All input parameters are const. The memory for non-simple output
35 * parameters is allocated by the function implementation.
36 *
37 * @param[in,out] esysContext The ESYS_CONTEXT.
38 * @param[in] sequenceHandle Authorization for the sequence.
39 * @param[in] shandle1 Session handle for authorization of sequenceHandle
40 * @param[in] shandle2 Second session handle.
41 * @param[in] shandle3 Third session handle.
42 * @param[in] buffer Data to be added to the hash/HMAC.
43 * @param[in] hierarchy Hierarchy of the ticket for a hash.
44 * @param[out] result The returned HMAC or digest in a sized buffer.
45 * (callee-allocated)
46 * @param[out] validation TPM2_Ticket indicating that the sequence of octets used to
47 * compute outDigest did not start with TPM2_GENERATED_VALUE.
48 * (callee-allocated)
49 * @retval TSS2_RC_SUCCESS if the function call was a success.
50 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
51 * pointers or required output handle references are NULL.
52 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
53 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
54 * internal operations or return parameters.
55 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
56 * operation already pending.
57 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
58 * at least contain the tag, response length, and response code.
59 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
60 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
61 did not verify.
62 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
63 * the 'decrypt' attribute bit set.
64 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
65 * the 'encrypt' attribute bit set.
66 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
67 * to the ESYS_CONTEXT or are of the wrong type or if required
68 * ESYS_TR objects are ESYS_TR_NONE.
69 * @retval TSS2_RCs produced by lower layers of the software stack may be
70 * returned to the caller unaltered unless handled internally.
71 */
72 TSS2_RC
Esys_SequenceComplete(ESYS_CONTEXT * esysContext,ESYS_TR sequenceHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_MAX_BUFFER * buffer,ESYS_TR hierarchy,TPM2B_DIGEST ** result,TPMT_TK_HASHCHECK ** validation)73 Esys_SequenceComplete(
74 ESYS_CONTEXT *esysContext,
75 ESYS_TR sequenceHandle,
76 ESYS_TR shandle1,
77 ESYS_TR shandle2,
78 ESYS_TR shandle3,
79 const TPM2B_MAX_BUFFER *buffer,
80 ESYS_TR hierarchy,
81 TPM2B_DIGEST **result,
82 TPMT_TK_HASHCHECK **validation)
83 {
84 TSS2_RC r;
85
86 r = Esys_SequenceComplete_Async(esysContext, sequenceHandle, shandle1,
87 shandle2, shandle3, buffer, hierarchy);
88 return_if_error(r, "Error in async function");
89
90 /* Set the timeout to indefinite for now, since we want _Finish to block */
91 int32_t timeouttmp = esysContext->timeout;
92 esysContext->timeout = -1;
93 /*
94 * Now we call the finish function, until return code is not equal to
95 * from TSS2_BASE_RC_TRY_AGAIN.
96 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
97 * have set the timeout to -1. This occurs for example if the TPM requests
98 * a retransmission of the command via TPM2_RC_YIELDED.
99 */
100 do {
101 r = Esys_SequenceComplete_Finish(esysContext, result, validation);
102 /* This is just debug information about the reattempt to finish the
103 command */
104 if (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN)
105 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
106 " => resubmitting command", r);
107 } while (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN);
108
109 /* Restore the timeout value to the original value */
110 esysContext->timeout = timeouttmp;
111 return_if_error(r, "Esys Finish");
112
113 return TSS2_RC_SUCCESS;
114 }
115
116 /** Asynchronous function for TPM2_SequenceComplete
117 *
118 * This function invokes the TPM2_SequenceComplete command in a asynchronous
119 * variant. This means the function will return as soon as the command has been
120 * sent downwards the stack to the TPM. All input parameters are const.
121 * In order to retrieve the TPM's response call Esys_SequenceComplete_Finish.
122 *
123 * @param[in,out] esysContext The ESYS_CONTEXT.
124 * @param[in] sequenceHandle Authorization for the sequence.
125 * @param[in] shandle1 Session handle for authorization of sequenceHandle
126 * @param[in] shandle2 Second session handle.
127 * @param[in] shandle3 Third session handle.
128 * @param[in] buffer Data to be added to the hash/HMAC.
129 * @param[in] hierarchy Hierarchy of the ticket for a hash.
130 * @retval ESYS_RC_SUCCESS if the function call was a success.
131 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
132 * pointers or required output handle references are NULL.
133 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
134 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
135 * internal operations or return parameters.
136 * @retval TSS2_RCs produced by lower layers of the software stack may be
137 returned to the caller unaltered unless handled internally.
138 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
139 * the 'decrypt' attribute bit set.
140 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
141 * the 'encrypt' attribute bit set.
142 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
143 * to the ESYS_CONTEXT or are of the wrong type or if required
144 * ESYS_TR objects are ESYS_TR_NONE.
145 */
146 TSS2_RC
Esys_SequenceComplete_Async(ESYS_CONTEXT * esysContext,ESYS_TR sequenceHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_MAX_BUFFER * buffer,ESYS_TR hierarchy)147 Esys_SequenceComplete_Async(
148 ESYS_CONTEXT *esysContext,
149 ESYS_TR sequenceHandle,
150 ESYS_TR shandle1,
151 ESYS_TR shandle2,
152 ESYS_TR shandle3,
153 const TPM2B_MAX_BUFFER *buffer,
154 ESYS_TR hierarchy)
155 {
156 TSS2_RC r;
157 LOG_TRACE("context=%p, sequenceHandle=%"PRIx32 ", buffer=%p,"
158 "hierarchy=%"PRIx32 "",
159 esysContext, sequenceHandle, buffer, hierarchy);
160 TSS2L_SYS_AUTH_COMMAND auths;
161 RSRC_NODE_T *sequenceHandleNode;
162 TPMI_RH_HIERARCHY tpm_hierarchy;
163
164 /* Check context, sequence correctness and set state to error for now */
165 if (esysContext == NULL) {
166 LOG_ERROR("esyscontext is NULL.");
167 return TSS2_ESYS_RC_BAD_REFERENCE;
168 }
169
170 r = iesys_handle_to_tpm_handle(hierarchy, &tpm_hierarchy);
171 if (r != TSS2_RC_SUCCESS) {
172 if (!iesys_is_platform_handle(hierarchy)) {
173 return r;
174 }
175 tpm_hierarchy = hierarchy;
176 }
177
178 r = iesys_check_sequence_async(esysContext);
179 if (r != TSS2_RC_SUCCESS)
180 return r;
181 esysContext->state = _ESYS_STATE_INTERNALERROR;
182
183 /* Check input parameters */
184 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
185 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
186 store_input_parameters(esysContext, sequenceHandle);
187
188 /* Retrieve the metadata objects for provided handles */
189 r = esys_GetResourceObject(esysContext, sequenceHandle, &sequenceHandleNode);
190 return_state_if_error(r, _ESYS_STATE_INIT, "sequenceHandle unknown.");
191
192 /* Initial invocation of SAPI to prepare the command buffer with parameters */
193 r = Tss2_Sys_SequenceComplete_Prepare(esysContext->sys,
194 (sequenceHandleNode == NULL)
195 ? TPM2_RH_NULL
196 : sequenceHandleNode->rsrc.handle,
197 buffer, tpm_hierarchy);
198 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
199
200 /* Calculate the cpHash Values */
201 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
202 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
203 if (sequenceHandleNode != NULL)
204 iesys_compute_session_value(esysContext->session_tab[0],
205 &sequenceHandleNode->rsrc.name, &sequenceHandleNode->auth);
206 else
207 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
208
209 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
210 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
211
212 /* Generate the auth values and set them in the SAPI command buffer */
213 r = iesys_gen_auths(esysContext, sequenceHandleNode, NULL, NULL, &auths);
214 return_state_if_error(r, _ESYS_STATE_INIT,
215 "Error in computation of auth values");
216
217 esysContext->authsCount = auths.count;
218 if (auths.count > 0) {
219 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
220 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
221 }
222
223 /* Trigger execution and finish the async invocation */
224 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
225 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
226 "Finish (Execute Async)");
227
228 esysContext->state = _ESYS_STATE_SENT;
229
230 return r;
231 }
232
233 /** Asynchronous finish function for TPM2_SequenceComplete
234 *
235 * This function returns the results of a TPM2_SequenceComplete command
236 * invoked via Esys_SequenceComplete_Finish. All non-simple output parameters
237 * are allocated by the function's implementation. NULL can be passed for every
238 * output parameter if the value is not required.
239 *
240 * @param[in,out] esysContext The ESYS_CONTEXT.
241 * @param[out] result The returned HMAC or digest in a sized buffer.
242 * (callee-allocated)
243 * @param[out] validation TPM2_Ticket indicating that the sequence of octets used to
244 * compute outDigest did not start with TPM2_GENERATED_VALUE.
245 * (callee-allocated)
246 * @retval TSS2_RC_SUCCESS on success
247 * @retval ESYS_RC_SUCCESS if the function call was a success.
248 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
249 * pointers or required output handle references are NULL.
250 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
251 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
252 * internal operations or return parameters.
253 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
254 * operation already pending.
255 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
256 * TPM response is received.
257 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
258 * at least contain the tag, response length, and response code.
259 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
260 * not verify.
261 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
262 * @retval TSS2_RCs produced by lower layers of the software stack may be
263 * returned to the caller unaltered unless handled internally.
264 */
265 TSS2_RC
Esys_SequenceComplete_Finish(ESYS_CONTEXT * esysContext,TPM2B_DIGEST ** result,TPMT_TK_HASHCHECK ** validation)266 Esys_SequenceComplete_Finish(
267 ESYS_CONTEXT *esysContext,
268 TPM2B_DIGEST **result,
269 TPMT_TK_HASHCHECK **validation)
270 {
271 TSS2_RC r;
272 LOG_TRACE("context=%p, result=%p, validation=%p",
273 esysContext, result, validation);
274
275 if (esysContext == NULL) {
276 LOG_ERROR("esyscontext is NULL.");
277 return TSS2_ESYS_RC_BAD_REFERENCE;
278 }
279
280 /* Check for correct sequence and set sequence to irregular for now */
281 if (esysContext->state != _ESYS_STATE_SENT &&
282 esysContext->state != _ESYS_STATE_RESUBMISSION) {
283 LOG_ERROR("Esys called in bad sequence.");
284 return TSS2_ESYS_RC_BAD_SEQUENCE;
285 }
286 esysContext->state = _ESYS_STATE_INTERNALERROR;
287
288 /* Allocate memory for response parameters */
289 if (result != NULL) {
290 *result = calloc(sizeof(TPM2B_DIGEST), 1);
291 if (*result == NULL) {
292 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
293 }
294 }
295 if (validation != NULL) {
296 *validation = calloc(sizeof(TPMT_TK_HASHCHECK), 1);
297 if (*validation == NULL) {
298 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
299 }
300 }
301
302 /*Receive the TPM response and handle resubmissions if necessary. */
303 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
304 if (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN) {
305 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
306 esysContext->state = _ESYS_STATE_SENT;
307 goto error_cleanup;
308 }
309 /* This block handle the resubmission of TPM commands given a certain set of
310 * TPM response codes. */
311 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
312 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
313 "resubmission: %" PRIx32, r);
314 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
315 LOG_WARNING("Maximum number of (re)submissions has been reached.");
316 esysContext->state = _ESYS_STATE_INIT;
317 goto error_cleanup;
318 }
319 esysContext->state = _ESYS_STATE_RESUBMISSION;
320 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
321 if (r != TSS2_RC_SUCCESS) {
322 LOG_WARNING("Error attempting to resubmit");
323 /* We do not set esysContext->state here but inherit the most recent
324 * state of the _async function. */
325 goto error_cleanup;
326 }
327 r = TSS2_ESYS_RC_TRY_AGAIN;
328 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
329 goto error_cleanup;
330 }
331 /* The following is the "regular error" handling. */
332 if (iesys_tpm_error(r)) {
333 LOG_WARNING("Received TPM Error");
334 esysContext->state = _ESYS_STATE_INIT;
335 goto error_cleanup;
336 } else if (r != TSS2_RC_SUCCESS) {
337 LOG_ERROR("Received a non-TPM Error");
338 esysContext->state = _ESYS_STATE_INTERNALERROR;
339 goto error_cleanup;
340 }
341
342 /*
343 * Now the verification of the response (hmac check) and if necessary the
344 * parameter decryption have to be done.
345 */
346 r = iesys_check_response(esysContext);
347 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
348 error_cleanup);
349
350 /*
351 * After the verification of the response we call the complete function
352 * to deliver the result.
353 */
354 r = Tss2_Sys_SequenceComplete_Complete(esysContext->sys,
355 (result != NULL) ? *result : NULL,
356 (validation != NULL) ? *validation
357 : NULL);
358 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
359 "Received error from SAPI unmarshaling" ,
360 error_cleanup);
361
362 /* The ESYS_TR sequence object has to be invalidated */
363 r = Esys_TR_Close(esysContext,
364 &esysContext->in.SequenceComplete.sequenceHandle);
365 goto_if_error(r, "invalidate object", error_cleanup);
366
367 esysContext->state = _ESYS_STATE_INIT;
368
369 return TSS2_RC_SUCCESS;
370
371 error_cleanup:
372 if (result != NULL)
373 SAFE_FREE(*result);
374 if (validation != NULL)
375 SAFE_FREE(*validation);
376
377 return r;
378 }
379