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