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_ClearControl
23 *
24 * This function invokes the TPM2_ClearControl 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] auth TPM2_RH_LOCKOUT or TPM2_RH_PLATFORM+{PP}.
31 * @param[in] shandle1 Session handle for authorization of auth
32 * @param[in] shandle2 Second session handle.
33 * @param[in] shandle3 Third session handle.
34 * @param[in] disable YES if the disableOwnerClear flag is to be SET, NO if
35 * the flag is to be CLEAR..
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_DECRYPT_PARAM: if one of the sessions has the
57 * 'decrypt' attribute set and the command does not support encryption
58 * of the first command parameter.
59 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
60 * 'encrypt' attribute set and the command does not support encryption
61 * of the first response parameter.
62 * @retval TSS2_RCs produced by lower layers of the software stack may be
63 * returned to the caller unaltered unless handled internally.
64 */
65 TSS2_RC
Esys_ClearControl(ESYS_CONTEXT * esysContext,ESYS_TR auth,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPMI_YES_NO disable)66 Esys_ClearControl(
67 ESYS_CONTEXT *esysContext,
68 ESYS_TR auth,
69 ESYS_TR shandle1,
70 ESYS_TR shandle2,
71 ESYS_TR shandle3,
72 TPMI_YES_NO disable)
73 {
74 TSS2_RC r;
75
76 r = Esys_ClearControl_Async(esysContext, auth, shandle1, shandle2, shandle3,
77 disable);
78 return_if_error(r, "Error in async function");
79
80 /* Set the timeout to indefinite for now, since we want _Finish to block */
81 int32_t timeouttmp = esysContext->timeout;
82 esysContext->timeout = -1;
83 /*
84 * Now we call the finish function, until return code is not equal to
85 * from TSS2_BASE_RC_TRY_AGAIN.
86 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
87 * have set the timeout to -1. This occurs for example if the TPM requests
88 * a retransmission of the command via TPM2_RC_YIELDED.
89 */
90 do {
91 r = Esys_ClearControl_Finish(esysContext);
92 /* This is just debug information about the reattempt to finish the
93 command */
94 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
95 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
96 " => resubmitting command", r);
97 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
98
99 /* Restore the timeout value to the original value */
100 esysContext->timeout = timeouttmp;
101 return_if_error(r, "Esys Finish");
102
103 return TSS2_RC_SUCCESS;
104 }
105
106 /** Asynchronous function for TPM2_ClearControl
107 *
108 * This function invokes the TPM2_ClearControl command in a asynchronous
109 * variant. This means the function will return as soon as the command has been
110 * sent downwards the stack to the TPM. All input parameters are const.
111 * In order to retrieve the TPM's response call Esys_ClearControl_Finish.
112 *
113 * @param[in,out] esysContext The ESYS_CONTEXT.
114 * @param[in] auth TPM2_RH_LOCKOUT or TPM2_RH_PLATFORM+{PP}.
115 * @param[in] shandle1 Session handle for authorization of auth
116 * @param[in] shandle2 Second session handle.
117 * @param[in] shandle3 Third session handle.
118 * @param[in] disable YES if the disableOwnerClear flag is to be SET, NO if
119 * the flag is to be CLEAR..
120 * @retval ESYS_RC_SUCCESS if the function call was a success.
121 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
122 * pointers or required output handle references are NULL.
123 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
124 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
125 * internal operations or return parameters.
126 * @retval TSS2_RCs produced by lower layers of the software stack may be
127 returned to the caller unaltered unless handled internally.
128 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
129 * the 'decrypt' attribute bit set.
130 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
131 * the 'encrypt' attribute bit set.
132 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
133 * to the ESYS_CONTEXT or are of the wrong type or if required
134 * ESYS_TR objects are ESYS_TR_NONE.
135 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
136 * 'decrypt' attribute set and the command does not support encryption
137 * of the first command parameter.
138 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
139 * 'encrypt' attribute set and the command does not support encryption
140 * of the first response parameter.
141 */
142 TSS2_RC
Esys_ClearControl_Async(ESYS_CONTEXT * esysContext,ESYS_TR auth,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,TPMI_YES_NO disable)143 Esys_ClearControl_Async(
144 ESYS_CONTEXT *esysContext,
145 ESYS_TR auth,
146 ESYS_TR shandle1,
147 ESYS_TR shandle2,
148 ESYS_TR shandle3,
149 TPMI_YES_NO disable)
150 {
151 TSS2_RC r;
152 LOG_TRACE("context=%p, auth=%"PRIx32 ", disable=%02"PRIx8"",
153 esysContext, auth, disable);
154 TSS2L_SYS_AUTH_COMMAND auths;
155 RSRC_NODE_T *authNode;
156
157 /* Check context, sequence correctness and set state to error for now */
158 if (esysContext == NULL) {
159 LOG_ERROR("esyscontext is NULL.");
160 return TSS2_ESYS_RC_BAD_REFERENCE;
161 }
162 r = iesys_check_sequence_async(esysContext);
163 if (r != TSS2_RC_SUCCESS)
164 return r;
165 esysContext->state = _ESYS_STATE_INTERNALERROR;
166
167 /* Check input parameters */
168 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
169 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
170
171 /* Retrieve the metadata objects for provided handles */
172 r = esys_GetResourceObject(esysContext, auth, &authNode);
173 return_state_if_error(r, _ESYS_STATE_INIT, "auth unknown.");
174
175 /* Initial invocation of SAPI to prepare the command buffer with parameters */
176 r = Tss2_Sys_ClearControl_Prepare(esysContext->sys,
177 (authNode == NULL) ? TPM2_RH_NULL
178 : authNode->rsrc.handle, disable);
179 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
180
181 /* Calculate the cpHash Values */
182 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
183 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
184 if (authNode != NULL)
185 iesys_compute_session_value(esysContext->session_tab[0],
186 &authNode->rsrc.name, &authNode->auth);
187 else
188 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
189
190 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
191 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
192
193 /* Generate the auth values and set them in the SAPI command buffer */
194 r = iesys_gen_auths(esysContext, authNode, NULL, NULL, &auths);
195 return_state_if_error(r, _ESYS_STATE_INIT,
196 "Error in computation of auth values");
197
198 esysContext->authsCount = auths.count;
199 if (auths.count > 0) {
200 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
201 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
202 }
203
204 /* Trigger execution and finish the async invocation */
205 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
206 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
207 "Finish (Execute Async)");
208
209 esysContext->state = _ESYS_STATE_SENT;
210
211 return r;
212 }
213
214 /** Asynchronous finish function for TPM2_ClearControl
215 *
216 * This function returns the results of a TPM2_ClearControl command
217 * invoked via Esys_ClearControl_Finish. All non-simple output parameters
218 * are allocated by the function's implementation. NULL can be passed for every
219 * output parameter if the value is not required.
220 *
221 * @param[in,out] esysContext The ESYS_CONTEXT.
222 * @retval TSS2_RC_SUCCESS on success
223 * @retval ESYS_RC_SUCCESS if the function call was a success.
224 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
225 * pointers or required output handle references are NULL.
226 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
227 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
228 * internal operations or return parameters.
229 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
230 * operation already pending.
231 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
232 * TPM response is received.
233 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
234 * at least contain the tag, response length, and response code.
235 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
236 * not verify.
237 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
238 * @retval TSS2_RCs produced by lower layers of the software stack may be
239 * returned to the caller unaltered unless handled internally.
240 */
241 TSS2_RC
Esys_ClearControl_Finish(ESYS_CONTEXT * esysContext)242 Esys_ClearControl_Finish(
243 ESYS_CONTEXT *esysContext)
244 {
245 TSS2_RC r;
246 LOG_TRACE("context=%p",
247 esysContext);
248
249 if (esysContext == NULL) {
250 LOG_ERROR("esyscontext is NULL.");
251 return TSS2_ESYS_RC_BAD_REFERENCE;
252 }
253
254 /* Check for correct sequence and set sequence to irregular for now */
255 if (esysContext->state != _ESYS_STATE_SENT &&
256 esysContext->state != _ESYS_STATE_RESUBMISSION) {
257 LOG_ERROR("Esys called in bad sequence.");
258 return TSS2_ESYS_RC_BAD_SEQUENCE;
259 }
260 esysContext->state = _ESYS_STATE_INTERNALERROR;
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 return r;
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 return r;
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 return r;
286 }
287 r = TSS2_ESYS_RC_TRY_AGAIN;
288 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
289 return r;
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 return r;
296 } else if (r != TSS2_RC_SUCCESS) {
297 LOG_ERROR("Received a non-TPM Error");
298 esysContext->state = _ESYS_STATE_INTERNALERROR;
299 return r;
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 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
308 "Error: check response");
309
310 /*
311 * After the verification of the response we call the complete function
312 * to deliver the result.
313 */
314 r = Tss2_Sys_ClearControl_Complete(esysContext->sys);
315 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
316 "Received error from SAPI unmarshaling" );
317
318 esysContext->state = _ESYS_STATE_INIT;
319
320 return TSS2_RC_SUCCESS;
321 }
322