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