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