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