• 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 saveHandle)23 static void store_input_parameters (
24     ESYS_CONTEXT *esysContext,
25     ESYS_TR saveHandle)
26 {
27     esysContext->in.ContextSave.saveHandle = saveHandle;
28 }
29 
30 /** One-Call function for TPM2_ContextSave
31  *
32  * This function invokes the TPM2_ContextSave command in a one-call
33  * variant. This means the function will block until the TPM response is
34  * available. All input parameters are const. The memory for non-simple output
35  * parameters is allocated by the function implementation.
36  *
37  * @param[in,out] esysContext The ESYS_CONTEXT.
38  * @param[in]  saveHandle Handle of the resource to save.
39  * @param[out] context .
40  *             (callee-allocated)
41  * @retval TSS2_RC_SUCCESS if the function call was a success.
42  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
43  *         pointers or required output handle references are NULL.
44  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
45  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
46  *         internal operations or return parameters.
47  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
48  *         operation already pending.
49  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
50  *          at least contain the tag, response length, and response code.
51  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
52  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
53            did not verify.
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_RCs produced by lower layers of the software stack may be
58  *         returned to the caller unaltered unless handled internally.
59  */
60 TSS2_RC
Esys_ContextSave(ESYS_CONTEXT * esysContext,ESYS_TR saveHandle,TPMS_CONTEXT ** context)61 Esys_ContextSave(
62     ESYS_CONTEXT *esysContext,
63     ESYS_TR saveHandle,
64     TPMS_CONTEXT **context)
65 {
66     TSS2_RC r;
67 
68     r = Esys_ContextSave_Async(esysContext, saveHandle);
69     return_if_error(r, "Error in async function");
70 
71     /* Set the timeout to indefinite for now, since we want _Finish to block */
72     int32_t timeouttmp = esysContext->timeout;
73     esysContext->timeout = -1;
74     /*
75      * Now we call the finish function, until return code is not equal to
76      * from TSS2_BASE_RC_TRY_AGAIN.
77      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
78      * have set the timeout to -1. This occurs for example if the TPM requests
79      * a retransmission of the command via TPM2_RC_YIELDED.
80      */
81     do {
82         r = Esys_ContextSave_Finish(esysContext, context);
83         /* This is just debug information about the reattempt to finish the
84            command */
85         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
86             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
87                       " => resubmitting command", r);
88     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
89 
90     /* Restore the timeout value to the original value */
91     esysContext->timeout = timeouttmp;
92     return_if_error(r, "Esys Finish");
93 
94     return TSS2_RC_SUCCESS;
95 }
96 
97 /** Asynchronous function for TPM2_ContextSave
98  *
99  * This function invokes the TPM2_ContextSave command in a asynchronous
100  * variant. This means the function will return as soon as the command has been
101  * sent downwards the stack to the TPM. All input parameters are const.
102  * In order to retrieve the TPM's response call Esys_ContextSave_Finish.
103  *
104  * @param[in,out] esysContext The ESYS_CONTEXT.
105  * @param[in]  saveHandle Handle of the resource to save.
106  * @retval ESYS_RC_SUCCESS if the function call was a success.
107  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
108  *         pointers or required output handle references are NULL.
109  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
110  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
111  *         internal operations or return parameters.
112  * @retval TSS2_RCs produced by lower layers of the software stack may be
113            returned to the caller unaltered unless handled internally.
114  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
115  *         to the ESYS_CONTEXT or are of the wrong type or if required
116  *         ESYS_TR objects are ESYS_TR_NONE.
117  */
118 TSS2_RC
Esys_ContextSave_Async(ESYS_CONTEXT * esysContext,ESYS_TR saveHandle)119 Esys_ContextSave_Async(
120     ESYS_CONTEXT *esysContext,
121     ESYS_TR saveHandle)
122 {
123     TSS2_RC r;
124     LOG_TRACE("context=%p, saveHandle=%"PRIx32 "",
125               esysContext, saveHandle);
126     RSRC_NODE_T *saveHandleNode;
127 
128     /* Check context, sequence correctness and set state to error for now */
129     if (esysContext == NULL) {
130         LOG_ERROR("esyscontext is NULL.");
131         return TSS2_ESYS_RC_BAD_REFERENCE;
132     }
133     r = iesys_check_sequence_async(esysContext);
134     if (r != TSS2_RC_SUCCESS)
135         return r;
136     esysContext->state = _ESYS_STATE_INTERNALERROR;
137     store_input_parameters(esysContext, saveHandle);
138 
139     /* Retrieve the metadata objects for provided handles */
140     r = esys_GetResourceObject(esysContext, saveHandle, &saveHandleNode);
141     return_state_if_error(r, _ESYS_STATE_INIT, "saveHandle unknown.");
142 
143     /* Initial invocation of SAPI to prepare the command buffer with parameters */
144     r = Tss2_Sys_ContextSave_Prepare(esysContext->sys,
145                                      (saveHandleNode == NULL) ? TPM2_RH_NULL
146                                       : saveHandleNode->rsrc.handle);
147     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
148     /* Trigger execution and finish the async invocation */
149     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
150     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
151                           "Finish (Execute Async)");
152 
153     esysContext->state = _ESYS_STATE_SENT;
154 
155     return r;
156 }
157 
158 /** Asynchronous finish function for TPM2_ContextSave
159  *
160  * This function returns the results of a TPM2_ContextSave command
161  * invoked via Esys_ContextSave_Finish. All non-simple output parameters
162  * are allocated by the function's implementation. NULL can be passed for every
163  * output parameter if the value is not required.
164  *
165  * @param[in,out] esysContext The ESYS_CONTEXT.
166  * @param[out] context .
167  *             (callee-allocated)
168  * @retval TSS2_RC_SUCCESS on success
169  * @retval ESYS_RC_SUCCESS if the function call was a success.
170  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
171  *         pointers or required output handle references are NULL.
172  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
173  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
174  *         internal operations or return parameters.
175  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
176  *         operation already pending.
177  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
178  *         TPM response is received.
179  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
180  *         at least contain the tag, response length, and response code.
181  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
182  *         not verify.
183  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
184  * @retval TSS2_RCs produced by lower layers of the software stack may be
185  *         returned to the caller unaltered unless handled internally.
186  */
187 TSS2_RC
Esys_ContextSave_Finish(ESYS_CONTEXT * esysContext,TPMS_CONTEXT ** context)188 Esys_ContextSave_Finish(
189     ESYS_CONTEXT *esysContext,
190     TPMS_CONTEXT **context)
191 {
192     TPMS_CONTEXT *lcontext = NULL;
193     TSS2_RC r;
194     LOG_TRACE("context=%p, context=%p",
195               esysContext, context);
196 
197     if (esysContext == NULL) {
198         LOG_ERROR("esyscontext is NULL.");
199         return TSS2_ESYS_RC_BAD_REFERENCE;
200     }
201 
202     /* Check for correct sequence and set sequence to irregular for now */
203     if (esysContext->state != _ESYS_STATE_SENT &&
204         esysContext->state != _ESYS_STATE_RESUBMISSION) {
205         LOG_ERROR("Esys called in bad sequence.");
206         return TSS2_ESYS_RC_BAD_SEQUENCE;
207     }
208     esysContext->state = _ESYS_STATE_INTERNALERROR;
209 
210     /* Allocate memory for response parameters */
211     lcontext = calloc(sizeof(TPMS_CONTEXT), 1);
212     if (lcontext == NULL) {
213         return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
214     }
215 
216     /*Receive the TPM response and handle resubmissions if necessary. */
217     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
218     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
219         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
220         esysContext->state = _ESYS_STATE_SENT;
221         goto error_cleanup;
222     }
223     /* This block handle the resubmission of TPM commands given a certain set of
224      * TPM response codes. */
225     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
226         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
227             "resubmission: %" PRIx32, r);
228         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
229             LOG_WARNING("Maximum number of (re)submissions has been reached.");
230             esysContext->state = _ESYS_STATE_INIT;
231             goto error_cleanup;
232         }
233         esysContext->state = _ESYS_STATE_RESUBMISSION;
234         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
235         if (r != TSS2_RC_SUCCESS) {
236             LOG_WARNING("Error attempting to resubmit");
237             /* We do not set esysContext->state here but inherit the most recent
238              * state of the _async function. */
239             goto error_cleanup;
240         }
241         r = TSS2_ESYS_RC_TRY_AGAIN;
242         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
243         goto error_cleanup;
244     }
245     /* The following is the "regular error" handling. */
246     if (iesys_tpm_error(r)) {
247         LOG_WARNING("Received TPM Error");
248         esysContext->state = _ESYS_STATE_INIT;
249         goto error_cleanup;
250     } else if (r != TSS2_RC_SUCCESS) {
251         LOG_ERROR("Received a non-TPM Error");
252         esysContext->state = _ESYS_STATE_INTERNALERROR;
253         goto error_cleanup;
254     }
255     r = Tss2_Sys_ContextSave_Complete(esysContext->sys, lcontext);
256     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
257                         "Received error from SAPI unmarshaling" ,
258                         error_cleanup);
259 
260 
261     /* ESYS Special Handling Code: Extend the  context with metadata of the object */
262     IESYS_CONTEXT_DATA esyscontextData;
263     RSRC_NODE_T *esys_object;
264     size_t offset = 0;
265     esyscontextData.reserved = 0;
266     memcpy(&esyscontextData.tpmContext.buffer[0], &(lcontext)->contextBlob.buffer[0],
267            (lcontext)->contextBlob.size);
268     esyscontextData.tpmContext.size = (lcontext)->contextBlob.size;
269     r =  esys_GetResourceObject(esysContext, esysContext->in.ContextSave.saveHandle,
270                                 &esys_object);
271     goto_if_error(r, "Error GetResourceObjectn", error_cleanup);
272 
273     esyscontextData.esysMetadata.size = 0;
274     esyscontextData.esysMetadata.data = esys_object->rsrc;
275     offset = 0;
276     r = iesys_MU_IESYS_CONTEXT_DATA_Marshal(&esyscontextData,
277                                             &(lcontext)->contextBlob.buffer[0],
278                                             sizeof(TPMS_CONTEXT_DATA), &offset);
279     goto_if_error(r, "while marshaling context ", error_cleanup);
280 
281     (lcontext)->contextBlob.size = offset;
282     /*
283      * If the ESYS_TR object being saved refers to a session,
284      * the ESYS_TR object is invalidated.
285      */
286     if (esys_object->rsrc.rsrcType == IESYSC_SESSION_RSRC) {
287         r = Esys_TR_Close(esysContext, &esysContext->in.ContextSave.saveHandle);
288         goto_if_error(r, "invalidate object", error_cleanup);
289     }
290     if (context != NULL)
291         *context = lcontext;
292     else
293         SAFE_FREE(lcontext);
294 
295     esysContext->state = _ESYS_STATE_INIT;
296 
297     return TSS2_RC_SUCCESS;
298 
299 error_cleanup:
300     SAFE_FREE(lcontext);
301 
302     return r;
303 }
304