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