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