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