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