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