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