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_PCR_SetAuthValue
23  *
24  * This function invokes the TPM2_PCR_SetAuthValue 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]  pcrHandle Handle for a PCR that may have an authorization value
31  *             set.
32  * @param[in]  shandle1 Session handle for authorization of pcrHandle
33  * @param[in]  shandle2 Second session handle.
34  * @param[in]  shandle3 Third session handle.
35  * @param[in]  auth The desired authorization value.
36  * @retval TSS2_RC_SUCCESS if the function call was a success.
37  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
38  *         pointers or required output handle references are NULL.
39  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
40  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
41  *         internal operations or return parameters.
42  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
43  *         operation already pending.
44  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
45  *          at least contain the tag, response length, and response code.
46  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
47  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
48            did not verify.
49  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
50  *         the 'decrypt' attribute bit set.
51  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
52  *         the 'encrypt' attribute bit set.
53  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
54  *         to the ESYS_CONTEXT or are of the wrong type or if required
55  *         ESYS_TR objects are ESYS_TR_NONE.
56  * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
57  *         'encrypt' attribute set and the command does not support encryption
58  *          of the first response parameter.
59  * @retval TSS2_RCs produced by lower layers of the software stack may be
60  *         returned to the caller unaltered unless handled internally.
61  */
62 TSS2_RC
Esys_PCR_SetAuthValue(ESYS_CONTEXT * esysContext,ESYS_TR pcrHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_DIGEST * auth)63 Esys_PCR_SetAuthValue(
64     ESYS_CONTEXT *esysContext,
65     ESYS_TR pcrHandle,
66     ESYS_TR shandle1,
67     ESYS_TR shandle2,
68     ESYS_TR shandle3,
69     const TPM2B_DIGEST *auth)
70 {
71     TSS2_RC r;
72 
73     r = Esys_PCR_SetAuthValue_Async(esysContext, pcrHandle, shandle1, shandle2,
74                                     shandle3, auth);
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_PCR_SetAuthValue_Finish(esysContext);
89         /* This is just debug information about the reattempt to finish the
90            command */
91         if (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN)
92             LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
93                       " => resubmitting command", r);
94     } while (base_rc(r) == 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_PCR_SetAuthValue
104  *
105  * This function invokes the TPM2_PCR_SetAuthValue 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_PCR_SetAuthValue_Finish.
109  *
110  * @param[in,out] esysContext The ESYS_CONTEXT.
111  * @param[in]  pcrHandle Handle for a PCR that may have an authorization value
112  *             set.
113  * @param[in]  shandle1 Session handle for authorization of pcrHandle
114  * @param[in]  shandle2 Second session handle.
115  * @param[in]  shandle3 Third session handle.
116  * @param[in]  auth The desired authorization value.
117  * @retval ESYS_RC_SUCCESS if the function call was a success.
118  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
119  *         pointers or required output handle references are NULL.
120  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
121  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
122  *         internal operations or return parameters.
123  * @retval TSS2_RCs produced by lower layers of the software stack may be
124            returned to the caller unaltered unless handled internally.
125  * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
126  *         the 'decrypt' attribute bit set.
127  * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
128  *         the 'encrypt' attribute bit set.
129  * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
130  *         to the ESYS_CONTEXT or are of the wrong type or if required
131  *         ESYS_TR objects are ESYS_TR_NONE.
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_PCR_SetAuthValue_Async(ESYS_CONTEXT * esysContext,ESYS_TR pcrHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_DIGEST * auth)137 Esys_PCR_SetAuthValue_Async(
138     ESYS_CONTEXT *esysContext,
139     ESYS_TR pcrHandle,
140     ESYS_TR shandle1,
141     ESYS_TR shandle2,
142     ESYS_TR shandle3,
143     const TPM2B_DIGEST *auth)
144 {
145     TSS2_RC r;
146     LOG_TRACE("context=%p, pcrHandle=%"PRIx32 ", auth=%p",
147               esysContext, pcrHandle, auth);
148     TSS2L_SYS_AUTH_COMMAND auths;
149     RSRC_NODE_T *pcrHandleNode;
150 
151     /* Check context, sequence correctness and set state to error for now */
152     if (esysContext == NULL) {
153         LOG_ERROR("esyscontext is NULL.");
154         return TSS2_ESYS_RC_BAD_REFERENCE;
155     }
156     r = iesys_check_sequence_async(esysContext);
157     if (r != TSS2_RC_SUCCESS)
158         return r;
159     esysContext->state = _ESYS_STATE_INTERNALERROR;
160 
161     /* Check input parameters */
162     r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
163     return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
164 
165     /* Retrieve the metadata objects for provided handles */
166     r = esys_GetResourceObject(esysContext, pcrHandle, &pcrHandleNode);
167     return_state_if_error(r, _ESYS_STATE_INIT, "pcrHandle unknown.");
168 
169     /* Initial invocation of SAPI to prepare the command buffer with parameters */
170     r = Tss2_Sys_PCR_SetAuthValue_Prepare(esysContext->sys,
171                                           (pcrHandleNode == NULL) ? TPM2_RH_NULL
172                                            : pcrHandleNode->rsrc.handle, auth);
173     return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
174 
175     /* Calculate the cpHash Values */
176     r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
177     return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
178     if (pcrHandleNode != NULL)
179         iesys_compute_session_value(esysContext->session_tab[0],
180                 &pcrHandleNode->rsrc.name, &pcrHandleNode->auth);
181     else
182         iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
183 
184     iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
185     iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
186 
187     /* Generate the auth values and set them in the SAPI command buffer */
188     r = iesys_gen_auths(esysContext, pcrHandleNode, NULL, NULL, &auths);
189     return_state_if_error(r, _ESYS_STATE_INIT,
190                           "Error in computation of auth values");
191 
192     esysContext->authsCount = auths.count;
193     if (auths.count > 0) {
194         r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
195         return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
196     }
197 
198     /* Trigger execution and finish the async invocation */
199     r = Tss2_Sys_ExecuteAsync(esysContext->sys);
200     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
201                           "Finish (Execute Async)");
202 
203     esysContext->state = _ESYS_STATE_SENT;
204 
205     return r;
206 }
207 
208 /** Asynchronous finish function for TPM2_PCR_SetAuthValue
209  *
210  * This function returns the results of a TPM2_PCR_SetAuthValue command
211  * invoked via Esys_PCR_SetAuthValue_Finish. All non-simple output parameters
212  * are allocated by the function's implementation. NULL can be passed for every
213  * output parameter if the value is not required.
214  *
215  * @param[in,out] esysContext The ESYS_CONTEXT.
216  * @retval TSS2_RC_SUCCESS on success
217  * @retval ESYS_RC_SUCCESS if the function call was a success.
218  * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
219  *         pointers or required output handle references are NULL.
220  * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
221  * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
222  *         internal operations or return parameters.
223  * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
224  *         operation already pending.
225  * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
226  *         TPM response is received.
227  * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
228  *         at least contain the tag, response length, and response code.
229  * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
230  *         not verify.
231  * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
232  * @retval TSS2_RCs produced by lower layers of the software stack may be
233  *         returned to the caller unaltered unless handled internally.
234  */
235 TSS2_RC
Esys_PCR_SetAuthValue_Finish(ESYS_CONTEXT * esysContext)236 Esys_PCR_SetAuthValue_Finish(
237     ESYS_CONTEXT *esysContext)
238 {
239     TSS2_RC r;
240     LOG_TRACE("context=%p",
241               esysContext);
242 
243     if (esysContext == NULL) {
244         LOG_ERROR("esyscontext is NULL.");
245         return TSS2_ESYS_RC_BAD_REFERENCE;
246     }
247 
248     /* Check for correct sequence and set sequence to irregular for now */
249     if (esysContext->state != _ESYS_STATE_SENT &&
250         esysContext->state != _ESYS_STATE_RESUBMISSION) {
251         LOG_ERROR("Esys called in bad sequence.");
252         return TSS2_ESYS_RC_BAD_SEQUENCE;
253     }
254     esysContext->state = _ESYS_STATE_INTERNALERROR;
255 
256     /*Receive the TPM response and handle resubmissions if necessary. */
257     r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
258     if (base_rc(r) == TSS2_BASE_RC_TRY_AGAIN) {
259         LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
260         esysContext->state = _ESYS_STATE_SENT;
261         return r;
262     }
263     /* This block handle the resubmission of TPM commands given a certain set of
264      * TPM response codes. */
265     if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
266         LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
267             "resubmission: %" PRIx32, r);
268         if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
269             LOG_WARNING("Maximum number of (re)submissions has been reached.");
270             esysContext->state = _ESYS_STATE_INIT;
271             return r;
272         }
273         esysContext->state = _ESYS_STATE_RESUBMISSION;
274         r = Tss2_Sys_ExecuteAsync(esysContext->sys);
275         if (r != TSS2_RC_SUCCESS) {
276             LOG_WARNING("Error attempting to resubmit");
277             /* We do not set esysContext->state here but inherit the most recent
278              * state of the _async function. */
279             return r;
280         }
281         r = TSS2_ESYS_RC_TRY_AGAIN;
282         LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
283         return r;
284     }
285     /* The following is the "regular error" handling. */
286     if (iesys_tpm_error(r)) {
287         LOG_WARNING("Received TPM Error");
288         esysContext->state = _ESYS_STATE_INIT;
289         return r;
290     } else if (r != TSS2_RC_SUCCESS) {
291         LOG_ERROR("Received a non-TPM Error");
292         esysContext->state = _ESYS_STATE_INTERNALERROR;
293         return r;
294     }
295 
296     /*
297      * Now the verification of the response (hmac check) and if necessary the
298      * parameter decryption have to be done.
299      */
300     r = iesys_check_response(esysContext);
301     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
302                           "Error: check response");
303 
304     /*
305      * After the verification of the response we call the complete function
306      * to deliver the result.
307      */
308     r = Tss2_Sys_PCR_SetAuthValue_Complete(esysContext->sys);
309     return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
310                           "Received error from SAPI unmarshaling" );
311 
312     esysContext->state = _ESYS_STATE_INIT;
313 
314     return TSS2_RC_SUCCESS;
315 }
316