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