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