• 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_FieldUpgradeData
23  *
24  * This function invokes the TPM2_FieldUpgradeData 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]  shandle1 First session handle.
31  * @param[in]  shandle2 Second session handle.
32  * @param[in]  shandle3 Third session handle.
33  * @param[in]  fuData Field upgrade image data.
34  * @param[out] nextDigest Tagged digest of the next block.
35  *             (callee-allocated)
36  * @param[out] firstDigest Tagged digest of the first block of the sequence.
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_NO_ENCRYPT_PARAM: if one of the sessions has the
56  *         'encrypt' attribute set and the command does not support encryption
57  *          of the first response parameter.
58  * @retval TSS2_RCs produced by lower layers of the software stack may be
59  *         returned to the caller unaltered unless handled internally.
60  */
61 TSS2_RC
Esys_FieldUpgradeData(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_MAX_BUFFER * fuData,TPMT_HA ** nextDigest,TPMT_HA ** firstDigest)62 Esys_FieldUpgradeData(
63     ESYS_CONTEXT *esysContext,
64     ESYS_TR shandle1,
65     ESYS_TR shandle2,
66     ESYS_TR shandle3,
67     const TPM2B_MAX_BUFFER *fuData,
68     TPMT_HA **nextDigest,
69     TPMT_HA **firstDigest)
70 {
71     TSS2_RC r;
72 
73     r = Esys_FieldUpgradeData_Async(esysContext, shandle1, shandle2, shandle3,
74                                     fuData);
75     return_if_error(r, "Error in async function");
76 
77     /* Set the timeout to indefinite for now, since we want _Finish to block */
78     int32_t timeouttmp = esysContext->timeout;
79     esysContext->timeout = -1;
80     /*
81      * Now we call the finish function, until return code is not equal to
82      * from TSS2_BASE_RC_TRY_AGAIN.
83      * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
84      * have set the timeout to -1. This occurs for example if the TPM requests
85      * a retransmission of the command via TPM2_RC_YIELDED.
86      */
87     do {
88         r = Esys_FieldUpgradeData_Finish(esysContext, nextDigest, firstDigest);
89         /* This is just debug information about the reattempt to finish the
90            command */
91         if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
92             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
93                       " => resubmitting command", r);
94     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
95 
96     /* Restore the timeout value to the original value */
97     esysContext->timeout = timeouttmp;
98     return_if_error(r, "Esys Finish");
99 
100     return TSS2_RC_SUCCESS;
101 }
102 
103 /** Asynchronous function for TPM2_FieldUpgradeData
104  *
105  * This function invokes the TPM2_FieldUpgradeData command in a asynchronous
106  * variant. This means the function will return as soon as the command has been
107  * sent downwards the stack to the TPM. All input parameters are const.
108  * In order to retrieve the TPM's response call Esys_FieldUpgradeData_Finish.
109  *
110  * @param[in,out] esysContext The ESYS_CONTEXT.
111  * @param[in]  shandle1 First session handle.
112  * @param[in]  shandle2 Second session handle.
113  * @param[in]  shandle3 Third session handle.
114  * @param[in]  fuData Field upgrade image data.
115  * @retval ESYS_RC_SUCCESS if the function call was a success.
116  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
117  *         pointers or required output handle references are NULL.
118  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
119  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
120  *         internal operations or return parameters.
121  * @retval TSS2_RCs produced by lower layers of the software stack may be
122            returned to the caller unaltered unless handled internally.
123  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
124  *         the 'decrypt' attribute bit set.
125  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
126  *         the 'encrypt' attribute bit set.
127  * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
128  *         'encrypt' attribute set and the command does not support encryption
129  *          of the first response parameter.
130  */
131 TSS2_RC
Esys_FieldUpgradeData_Async(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_MAX_BUFFER * fuData)132 Esys_FieldUpgradeData_Async(
133     ESYS_CONTEXT *esysContext,
134     ESYS_TR shandle1,
135     ESYS_TR shandle2,
136     ESYS_TR shandle3,
137     const TPM2B_MAX_BUFFER *fuData)
138 {
139     TSS2_RC r;
140     LOG_TRACE("context=%p, fuData=%p",
141               esysContext, fuData);
142     TSS2L_SYS_AUTH_COMMAND auths;
143 
144     /* Check context, sequence correctness and set state to error for now */
145     if (esysContext == NULL) {
146         LOG_ERROR("esyscontext is NULL.");
147         return TSS2_ESYS_RC_BAD_REFERENCE;
148     }
149     r = iesys_check_sequence_async(esysContext);
150     if (r != TSS2_RC_SUCCESS)
151         return r;
152     esysContext->state = _ESYS_STATE_INTERNALERROR;
153 
154     /* Check input parameters */
155     r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
156     return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
157 
158     /* Initial invocation of SAPI to prepare the command buffer with parameters */
159     r = Tss2_Sys_FieldUpgradeData_Prepare(esysContext->sys, fuData);
160     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
161 
162     /* Calculate the cpHash Values */
163     r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
164     return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
165     iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
166     iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
167     iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
168 
169     /* Generate the auth values and set them in the SAPI command buffer */
170     r = iesys_gen_auths(esysContext, NULL, NULL, NULL, &auths);
171     return_state_if_error(r, _ESYS_STATE_INIT,
172                           "Error in computation of auth values");
173 
174     esysContext->authsCount = auths.count;
175     if (auths.count > 0) {
176         r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
177         return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
178     }
179 
180     /* Trigger execution and finish the async invocation */
181     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
182     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
183                           "Finish (Execute Async)");
184 
185     esysContext->state = _ESYS_STATE_SENT;
186 
187     return r;
188 }
189 
190 /** Asynchronous finish function for TPM2_FieldUpgradeData
191  *
192  * This function returns the results of a TPM2_FieldUpgradeData command
193  * invoked via Esys_FieldUpgradeData_Finish. All non-simple output parameters
194  * are allocated by the function's implementation. NULL can be passed for every
195  * output parameter if the value is not required.
196  *
197  * @param[in,out] esysContext The ESYS_CONTEXT.
198  * @param[out] nextDigest Tagged digest of the next block.
199  *             (callee-allocated)
200  * @param[out] firstDigest Tagged digest of the first block of the sequence.
201  *             (callee-allocated)
202  * @retval TSS2_RC_SUCCESS on success
203  * @retval ESYS_RC_SUCCESS if the function call was a success.
204  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
205  *         pointers or required output handle references are NULL.
206  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
207  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
208  *         internal operations or return parameters.
209  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
210  *         operation already pending.
211  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
212  *         TPM response is received.
213  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
214  *         at least contain the tag, response length, and response code.
215  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
216  *         not verify.
217  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
218  * @retval TSS2_RCs produced by lower layers of the software stack may be
219  *         returned to the caller unaltered unless handled internally.
220  */
221 TSS2_RC
Esys_FieldUpgradeData_Finish(ESYS_CONTEXT * esysContext,TPMT_HA ** nextDigest,TPMT_HA ** firstDigest)222 Esys_FieldUpgradeData_Finish(
223     ESYS_CONTEXT *esysContext,
224     TPMT_HA **nextDigest,
225     TPMT_HA **firstDigest)
226 {
227     TSS2_RC r;
228     LOG_TRACE("context=%p, nextDigest=%p, firstDigest=%p",
229               esysContext, nextDigest, firstDigest);
230 
231     if (esysContext == NULL) {
232         LOG_ERROR("esyscontext is NULL.");
233         return TSS2_ESYS_RC_BAD_REFERENCE;
234     }
235 
236     /* Check for correct sequence and set sequence to irregular for now */
237     if (esysContext->state != _ESYS_STATE_SENT &&
238         esysContext->state != _ESYS_STATE_RESUBMISSION) {
239         LOG_ERROR("Esys called in bad sequence.");
240         return TSS2_ESYS_RC_BAD_SEQUENCE;
241     }
242     esysContext->state = _ESYS_STATE_INTERNALERROR;
243 
244     /* Allocate memory for response parameters */
245     if (nextDigest != NULL) {
246         *nextDigest = calloc(sizeof(TPMT_HA), 1);
247         if (*nextDigest == NULL) {
248             return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
249         }
250     }
251     if (firstDigest != NULL) {
252         *firstDigest = calloc(sizeof(TPMT_HA), 1);
253         if (*firstDigest == NULL) {
254             goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
255         }
256     }
257 
258     /*Receive the TPM response and handle resubmissions if necessary. */
259     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
260     if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
261         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
262         esysContext->state = _ESYS_STATE_SENT;
263         goto error_cleanup;
264     }
265     /* This block handle the resubmission of TPM commands given a certain set of
266      * TPM response codes. */
267     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
268         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
269             "resubmission: %" PRIx32, r);
270         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
271             LOG_WARNING("Maximum number of (re)submissions has been reached.");
272             esysContext->state = _ESYS_STATE_INIT;
273             goto error_cleanup;
274         }
275         esysContext->state = _ESYS_STATE_RESUBMISSION;
276         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
277         if (r != TSS2_RC_SUCCESS) {
278             LOG_WARNING("Error attempting to resubmit");
279             /* We do not set esysContext->state here but inherit the most recent
280              * state of the _async function. */
281             goto error_cleanup;
282         }
283         r = TSS2_ESYS_RC_TRY_AGAIN;
284         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
285         goto error_cleanup;
286     }
287     /* The following is the "regular error" handling. */
288     if (iesys_tpm_error(r)) {
289         LOG_WARNING("Received TPM Error");
290         esysContext->state = _ESYS_STATE_INIT;
291         goto error_cleanup;
292     } else if (r != TSS2_RC_SUCCESS) {
293         LOG_ERROR("Received a non-TPM Error");
294         esysContext->state = _ESYS_STATE_INTERNALERROR;
295         goto error_cleanup;
296     }
297 
298     /*
299      * Now the verification of the response (hmac check) and if necessary the
300      * parameter decryption have to be done.
301      */
302     r = iesys_check_response(esysContext);
303     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
304                         error_cleanup);
305 
306     /*
307      * After the verification of the response we call the complete function
308      * to deliver the result.
309      */
310     r = Tss2_Sys_FieldUpgradeData_Complete(esysContext->sys,
311                                            (nextDigest != NULL) ? *nextDigest
312                                             : NULL,
313                                            (firstDigest != NULL) ? *firstDigest
314                                             : NULL);
315     goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
316                         "Received error from SAPI unmarshaling" ,
317                         error_cleanup);
318 
319     esysContext->state = _ESYS_STATE_INIT;
320 
321     return TSS2_RC_SUCCESS;
322 
323 error_cleanup:
324     if (nextDigest != NULL)
325         SAFE_FREE(*nextDigest);
326     if (firstDigest != NULL)
327         SAFE_FREE(*firstDigest);
328 
329     return r;
330 }
331