blob: c41f9a64336fab838859788fe60ac499284b72df [file] [log] [blame]
Peter Huewed5a36f62018-06-12 00:59:26 +02001/* SPDX-License-Identifier: BSD-2 */
Juergen Reppff821bd2017-12-11 15:21:42 +01002/*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
Juergen Reppff821bd2017-12-11 15:21:42 +01005 ******************************************************************************/
6
Philip Tricca910f17c2018-03-15 12:38:37 -07007#include "tss2_mu.h"
8#include "tss2_sys.h"
Philip Tricca8ffd3c42018-03-09 16:27:24 -08009#include "tss2_esys.h"
Juergen Repped6e6e22018-03-19 17:34:32 +010010
11#include "esys_types.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010012#include "esys_iutil.h"
13#include "esys_mu.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010014#define LOGMODULE esys
Philip Triccaa7c51ce2018-03-10 18:28:25 -080015#include "util/log.h"
Juergen Repp35c121f2018-10-15 17:02:25 +020016#include "util/aux_util.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010017
Juergen Reppadd438d2018-04-09 10:00:19 +020018/** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
Juergen Reppff821bd2017-12-11 15:21:42 +010019static void store_input_parameters (
20 ESYS_CONTEXT *esysContext,
21 ESYS_TR auth,
22 TPMI_YES_NO disable)
23{
24 esysContext->in.ClearControl.auth = auth;
25 esysContext->in.ClearControl.disable = disable;
26}
27
28/** One-Call function for TPM2_ClearControl
29 *
30 * This function invokes the TPM2_ClearControl command in a one-call
31 * variant. This means the function will block until the TPM response is
32 * available. All input parameters are const. The memory for non-simple output
33 * parameters is allocated by the function implementation.
34 *
35 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020036 * @param[in] auth TPM2_RH_LOCKOUT or TPM2_RH_PLATFORM+{PP}.
37 * @param[in] shandle1 Session handle for authorization of auth
38 * @param[in] shandle2 Second session handle.
39 * @param[in] shandle3 Third session handle.
Juergen Repp66989272018-09-04 11:13:06 +020040 * @param[in] disable YES if the disableOwnerClear flag is to be SET, NO if
41 * the flag is to be CLEAR..
Juergen Reppc3cf4b62018-08-22 15:27:59 +020042 * @retval TSS2_RC_SUCCESS if the function call was a success.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020043 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
44 * pointers or required output handle references are NULL.
45 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
46 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
47 * internal operations or return parameters.
48 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
49 * operation already pending.
50 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
51 * at least contain the tag, response length, and response code.
52 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
Juergen Reppc3cf4b62018-08-22 15:27:59 +020053 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
54 did not verify.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020055 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
56 * the 'decrypt' attribute bit set.
57 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
58 * the 'encrypt' attribute bit set.
Juergen Repp66989272018-09-04 11:13:06 +020059 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
60 * to the ESYS_CONTEXT or are of the wrong type or if required
61 * ESYS_TR objects are ESYS_TR_NONE.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020062 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
63 * 'decrypt' attribute set and the command does not support encryption
64 * of the first command parameter.
65 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
66 * 'encrypt' attribute set and the command does not support encryption
67 * of the first response parameter.
68 * @retval TSS2_RCs produced by lower layers of the software stack may be
69 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +010070 */
71TSS2_RC
72Esys_ClearControl(
73 ESYS_CONTEXT *esysContext,
74 ESYS_TR auth,
75 ESYS_TR shandle1,
76 ESYS_TR shandle2,
77 ESYS_TR shandle3,
78 TPMI_YES_NO disable)
79{
Juergen Repp3b7b41b2018-03-19 15:58:04 +010080 TSS2_RC r;
Juergen Reppff821bd2017-12-11 15:21:42 +010081
Juergen Repp66989272018-09-04 11:13:06 +020082 r = Esys_ClearControl_Async(esysContext, auth, shandle1, shandle2, shandle3,
83 disable);
Juergen Reppff821bd2017-12-11 15:21:42 +010084 return_if_error(r, "Error in async function");
85
Juergen Reppadd438d2018-04-09 10:00:19 +020086 /* Set the timeout to indefinite for now, since we want _Finish to block */
Juergen Reppff821bd2017-12-11 15:21:42 +010087 int32_t timeouttmp = esysContext->timeout;
88 esysContext->timeout = -1;
89 /*
90 * Now we call the finish function, until return code is not equal to
91 * from TSS2_BASE_RC_TRY_AGAIN.
92 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
93 * have set the timeout to -1. This occurs for example if the TPM requests
94 * a retransmission of the command via TPM2_RC_YIELDED.
95 */
96 do {
Juergen Reppadd438d2018-04-09 10:00:19 +020097 r = Esys_ClearControl_Finish(esysContext);
Juergen Reppff821bd2017-12-11 15:21:42 +010098 /* This is just debug information about the reattempt to finish the
99 command */
100 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
101 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
102 " => resubmitting command", r);
103 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
104
105 /* Restore the timeout value to the original value */
106 esysContext->timeout = timeouttmp;
107 return_if_error(r, "Esys Finish");
108
109 return TSS2_RC_SUCCESS;
110}
111
112/** Asynchronous function for TPM2_ClearControl
113 *
114 * This function invokes the TPM2_ClearControl command in a asynchronous
115 * variant. This means the function will return as soon as the command has been
116 * sent downwards the stack to the TPM. All input parameters are const.
Juergen Reppadd438d2018-04-09 10:00:19 +0200117 * In order to retrieve the TPM's response call Esys_ClearControl_Finish.
Juergen Reppff821bd2017-12-11 15:21:42 +0100118 *
119 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200120 * @param[in] auth TPM2_RH_LOCKOUT or TPM2_RH_PLATFORM+{PP}.
121 * @param[in] shandle1 Session handle for authorization of auth
122 * @param[in] shandle2 Second session handle.
123 * @param[in] shandle3 Third session handle.
Juergen Repp66989272018-09-04 11:13:06 +0200124 * @param[in] disable YES if the disableOwnerClear flag is to be SET, NO if
125 * the flag is to be CLEAR..
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200126 * @retval ESYS_RC_SUCCESS if the function call was a success.
127 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
128 * pointers or required output handle references are NULL.
129 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
130 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
131 * internal operations or return parameters.
132 * @retval TSS2_RCs produced by lower layers of the software stack may be
133 returned to the caller unaltered unless handled internally.
134 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
135 * the 'decrypt' attribute bit set.
136 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
137 * the 'encrypt' attribute bit set.
Juergen Repp66989272018-09-04 11:13:06 +0200138 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
139 * to the ESYS_CONTEXT or are of the wrong type or if required
140 * ESYS_TR objects are ESYS_TR_NONE.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200141 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
142 * 'decrypt' attribute set and the command does not support encryption
143 * of the first command parameter.
144 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
145 * 'encrypt' attribute set and the command does not support encryption
146 * of the first response parameter.
Juergen Reppff821bd2017-12-11 15:21:42 +0100147 */
148TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200149Esys_ClearControl_Async(
Juergen Reppff821bd2017-12-11 15:21:42 +0100150 ESYS_CONTEXT *esysContext,
151 ESYS_TR auth,
152 ESYS_TR shandle1,
153 ESYS_TR shandle2,
154 ESYS_TR shandle3,
155 TPMI_YES_NO disable)
156{
Juergen Repp3b7b41b2018-03-19 15:58:04 +0100157 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200158 LOG_TRACE("context=%p, auth=%"PRIx32 ", disable=%02"PRIx8"",
159 esysContext, auth, disable);
Andreas Fuchsc5a73eb2018-03-19 16:01:00 +0100160 TSS2L_SYS_AUTH_COMMAND auths;
Juergen Reppff821bd2017-12-11 15:21:42 +0100161 RSRC_NODE_T *authNode;
162
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100163 /* Check context, sequence correctness and set state to error for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100164 if (esysContext == NULL) {
165 LOG_ERROR("esyscontext is NULL.");
166 return TSS2_ESYS_RC_BAD_REFERENCE;
167 }
168 r = iesys_check_sequence_async(esysContext);
169 if (r != TSS2_RC_SUCCESS)
170 return r;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100171 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100172
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100173 /* Check and store input parameters */
dantpmf6ef2472018-04-06 15:21:59 -0700174 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100175 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
Juergen Repp66989272018-09-04 11:13:06 +0200176 store_input_parameters(esysContext, auth, disable);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100177
178 /* Retrieve the metadata objects for provided handles */
Juergen Reppff821bd2017-12-11 15:21:42 +0100179 r = esys_GetResourceObject(esysContext, auth, &authNode);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100180 return_state_if_error(r, _ESYS_STATE_INIT, "auth unknown.");
181
182 /* Initial invocation of SAPI to prepare the command buffer with parameters */
Juergen Reppff821bd2017-12-11 15:21:42 +0100183 r = Tss2_Sys_ClearControl_Prepare(esysContext->sys,
Juergen Repp66989272018-09-04 11:13:06 +0200184 (authNode == NULL) ? TPM2_RH_NULL
185 : authNode->rsrc.handle, disable);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100186 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100187
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100188 /* Calculate the cpHash Values */
189 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
190 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
Juergen Reppff821bd2017-12-11 15:21:42 +0100191 iesys_compute_session_value(esysContext->session_tab[0],
192 &authNode->rsrc.name, &authNode->auth);
193 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100194 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
Juergen Reppff821bd2017-12-11 15:21:42 +0100195
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100196 /* Generate the auth values and set them in the SAPI command buffer */
197 r = iesys_gen_auths(esysContext, authNode, NULL, NULL, &auths);
Juergen Repp66989272018-09-04 11:13:06 +0200198 return_state_if_error(r, _ESYS_STATE_INIT,
199 "Error in computation of auth values");
200
Juergen Reppff821bd2017-12-11 15:21:42 +0100201 esysContext->authsCount = auths.count;
202 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100203 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
Juergen Reppff821bd2017-12-11 15:21:42 +0100204
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100205 /* Trigger execution and finish the async invocation */
Juergen Reppff821bd2017-12-11 15:21:42 +0100206 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
Juergen Repp66989272018-09-04 11:13:06 +0200207 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
208 "Finish (Execute Async)");
Juergen Reppff821bd2017-12-11 15:21:42 +0100209
210 esysContext->state = _ESYS_STATE_SENT;
211
212 return r;
213}
214
215/** Asynchronous finish function for TPM2_ClearControl
216 *
217 * This function returns the results of a TPM2_ClearControl command
Juergen Reppadd438d2018-04-09 10:00:19 +0200218 * invoked via Esys_ClearControl_Finish. All non-simple output parameters
Juergen Reppff821bd2017-12-11 15:21:42 +0100219 * 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
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200224 * @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
Juergen Reppc3cf4b62018-08-22 15:27:59 +0200235 * 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.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200238 * @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.
Juergen Reppff821bd2017-12-11 15:21:42 +0100241 */
242TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200243Esys_ClearControl_Finish(
Juergen Reppff821bd2017-12-11 15:21:42 +0100244 ESYS_CONTEXT *esysContext)
245{
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100246 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200247 LOG_TRACE("context=%p",
248 esysContext);
249
Juergen Reppff821bd2017-12-11 15:21:42 +0100250 if (esysContext == NULL) {
251 LOG_ERROR("esyscontext is NULL.");
252 return TSS2_ESYS_RC_BAD_REFERENCE;
253 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100254
255 /* Check for correct sequence and set sequence to irregular for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100256 if (esysContext->state != _ESYS_STATE_SENT) {
257 LOG_ERROR("Esys called in bad sequence.");
258 return TSS2_ESYS_RC_BAD_SEQUENCE;
259 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100260 esysContext->state = _ESYS_STATE_INTERNALERROR;
261
262 /*Receive the TPM response and handle resubmissions if necessary. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100263 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);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100266 esysContext->state = _ESYS_STATE_SENT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100267 return r;
268 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100269 /* This block handle the resubmission of TPM commands given a certain set of
270 * TPM response codes. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100271 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);
Andreas Fuchs631d7e62018-03-19 10:54:56 +0100274 if (esysContext->submissionCount >= _ESYS_MAX_SUBMISSIONS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100275 LOG_WARNING("Maximum number of (re)submissions has been reached.");
276 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100277 return r;
278 }
279 esysContext->state = _ESYS_STATE_RESUBMISSION;
Juergen Reppadd438d2018-04-09 10:00:19 +0200280 r = Esys_ClearControl_Async(esysContext,
Juergen Repp66989272018-09-04 11:13:06 +0200281 esysContext->in.ClearControl.auth,
282 esysContext->session_type[0],
283 esysContext->session_type[1],
284 esysContext->session_type[2],
285 esysContext->in.ClearControl.disable);
Juergen Reppff821bd2017-12-11 15:21:42 +0100286 if (r != TSS2_RC_SUCCESS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100287 LOG_WARNING("Error attempting to resubmit");
288 /* We do not set esysContext->state here but inherit the most recent
289 * state of the _async function. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100290 return r;
291 }
292 r = TSS2_ESYS_RC_TRY_AGAIN;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100293 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100294 return r;
295 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100296 /* The following is the "regular error" handling. */
Juergen Repp033da112018-07-10 13:06:18 +0200297 if (iesys_tpm_error(r)) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100298 LOG_WARNING("Received TPM Error");
299 esysContext->state = _ESYS_STATE_INIT;
300 return r;
301 } else if (r != TSS2_RC_SUCCESS) {
302 LOG_ERROR("Received a non-TPM Error");
303 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100304 return r;
305 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100306
Juergen Reppff821bd2017-12-11 15:21:42 +0100307 /*
308 * Now the verification of the response (hmac check) and if necessary the
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100309 * parameter decryption have to be done.
Juergen Reppff821bd2017-12-11 15:21:42 +0100310 */
Juergen Repp109de7d2018-02-23 13:42:08 +0100311 r = iesys_check_response(esysContext);
Juergen Repp66989272018-09-04 11:13:06 +0200312 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
313 "Error: check response");
314
Juergen Reppff821bd2017-12-11 15:21:42 +0100315 /*
316 * After the verification of the response we call the complete function
317 * to deliver the result.
318 */
319 r = Tss2_Sys_ClearControl_Complete(esysContext->sys);
Juergen Repp66989272018-09-04 11:13:06 +0200320 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
321 "Received error from SAPI unmarshaling" );
322
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100323 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100324
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100325 return TSS2_RC_SUCCESS;
Philip Tricca910f17c2018-03-15 12:38:37 -0700326}