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