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