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_UndefineSpaceSpecial
31 *
32 * This function invokes the TPM2_NV_UndefineSpaceSpecial 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] nvIndex Index to be deleted.
39 * @param[in] platform TPM2_RH_PLATFORM + {PP}.
40 * @param[in] shandle1 Session handle for authorization of nvIndex
41 * @param[in] shandle2 Session handle for authorization of platform
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_UndefineSpaceSpecial(ESYS_CONTEXT * esysContext,ESYS_TR nvIndex,ESYS_TR platform,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)73 Esys_NV_UndefineSpaceSpecial(
74 ESYS_CONTEXT *esysContext,
75 ESYS_TR nvIndex,
76 ESYS_TR platform,
77 ESYS_TR shandle1,
78 ESYS_TR shandle2,
79 ESYS_TR shandle3)
80 {
81 TSS2_RC r;
82
83 r = Esys_NV_UndefineSpaceSpecial_Async(esysContext, nvIndex, platform,
84 shandle1, 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_UndefineSpaceSpecial_Finish(esysContext);
99 /* This is just debug information about the reattempt to finish the
100 command */
101 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
102 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
103 " => resubmitting command", r);
104 } while ((r & ~TSS2_RC_LAYER_MASK) == 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_UndefineSpaceSpecial
114 *
115 * This function invokes the TPM2_NV_UndefineSpaceSpecial 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_UndefineSpaceSpecial_Finish.
119 *
120 * @param[in,out] esysContext The ESYS_CONTEXT.
121 * @param[in] nvIndex Index to be deleted.
122 * @param[in] platform TPM2_RH_PLATFORM + {PP}.
123 * @param[in] shandle1 Session handle for authorization of nvIndex
124 * @param[in] shandle2 Session handle for authorization of platform
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_UndefineSpaceSpecial_Async(ESYS_CONTEXT * esysContext,ESYS_TR nvIndex,ESYS_TR platform,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)149 Esys_NV_UndefineSpaceSpecial_Async(
150 ESYS_CONTEXT *esysContext,
151 ESYS_TR nvIndex,
152 ESYS_TR platform,
153 ESYS_TR shandle1,
154 ESYS_TR shandle2,
155 ESYS_TR shandle3)
156 {
157 TSS2_RC r;
158 LOG_TRACE("context=%p, nvIndex=%"PRIx32 ", platform=%"PRIx32 "",
159 esysContext, nvIndex, platform);
160 TSS2L_SYS_AUTH_COMMAND auths;
161 RSRC_NODE_T *nvIndexNode;
162 RSRC_NODE_T *platformNode;
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, nvIndex, &nvIndexNode);
181 return_state_if_error(r, _ESYS_STATE_INIT, "nvIndex unknown.");
182 r = esys_GetResourceObject(esysContext, platform, &platformNode);
183 return_state_if_error(r, _ESYS_STATE_INIT, "platform unknown.");
184
185 /* Initial invocation of SAPI to prepare the command buffer with parameters */
186 r = Tss2_Sys_NV_UndefineSpaceSpecial_Prepare(esysContext->sys,
187 (nvIndexNode == NULL)
188 ? TPM2_RH_NULL
189 : nvIndexNode->rsrc.handle,
190 (platformNode == NULL)
191 ? TPM2_RH_NULL
192 : platformNode->rsrc.handle);
193 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
194
195 /* Calculate the cpHash Values */
196 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
197 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
198 if (nvIndexNode != NULL)
199 iesys_compute_session_value(esysContext->session_tab[0],
200 &nvIndexNode->rsrc.name, &nvIndexNode->auth);
201 else
202 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
203
204 if (platformNode != NULL)
205 iesys_compute_session_value(esysContext->session_tab[1],
206 &platformNode->rsrc.name, &platformNode->auth);
207 else
208 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
209
210 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
211
212 /* Generate the auth values and set them in the SAPI command buffer */
213 r = iesys_gen_auths(esysContext, nvIndexNode, platformNode, NULL, &auths);
214 return_state_if_error(r, _ESYS_STATE_INIT,
215 "Error in computation of auth values");
216
217 esysContext->authsCount = auths.count;
218 if (auths.count > 0) {
219 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
220 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
221 }
222
223 /* Trigger execution and finish the async invocation */
224 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
225 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
226 "Finish (Execute Async)");
227
228 esysContext->state = _ESYS_STATE_SENT;
229
230 return r;
231 }
232
233 /** Asynchronous finish function for TPM2_NV_UndefineSpaceSpecial
234 *
235 * This function returns the results of a TPM2_NV_UndefineSpaceSpecial command
236 * invoked via Esys_NV_UndefineSpaceSpecial_Finish. All non-simple output parameters
237 * are allocated by the function's implementation. NULL can be passed for every
238 * output parameter if the value is not required.
239 *
240 * @param[in,out] esysContext The ESYS_CONTEXT.
241 * @retval TSS2_RC_SUCCESS on success
242 * @retval ESYS_RC_SUCCESS if the function call was a success.
243 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
244 * pointers or required output handle references are NULL.
245 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
246 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
247 * internal operations or return parameters.
248 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
249 * operation already pending.
250 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
251 * TPM response is received.
252 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
253 * at least contain the tag, response length, and response code.
254 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
255 * not verify.
256 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
257 * @retval TSS2_RCs produced by lower layers of the software stack may be
258 * returned to the caller unaltered unless handled internally.
259 */
260 TSS2_RC
Esys_NV_UndefineSpaceSpecial_Finish(ESYS_CONTEXT * esysContext)261 Esys_NV_UndefineSpaceSpecial_Finish(
262 ESYS_CONTEXT *esysContext)
263 {
264 ESYS_TR nvIndex;
265 RSRC_NODE_T *nvIndexNode;
266 RSRC_NODE_T *session;
267 TSS2_RC r;
268 LOG_TRACE("context=%p",
269 esysContext);
270
271 if (esysContext == NULL) {
272 LOG_ERROR("esyscontext is NULL.");
273 return TSS2_ESYS_RC_BAD_REFERENCE;
274 }
275
276 /* Check for correct sequence and set sequence to irregular for now */
277 if (esysContext->state != _ESYS_STATE_SENT &&
278 esysContext->state != _ESYS_STATE_RESUBMISSION) {
279 LOG_ERROR("Esys called in bad sequence.");
280 return TSS2_ESYS_RC_BAD_SEQUENCE;
281 }
282 esysContext->state = _ESYS_STATE_INTERNALERROR;
283
284 /*Receive the TPM response and handle resubmissions if necessary. */
285 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
286 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
287 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
288 esysContext->state = _ESYS_STATE_SENT;
289 return r;
290 }
291 /* This block handle the resubmission of TPM commands given a certain set of
292 * TPM response codes. */
293 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
294 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
295 "resubmission: %" PRIx32, r);
296 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
297 LOG_WARNING("Maximum number of (re)submissions has been reached.");
298 esysContext->state = _ESYS_STATE_INIT;
299 return r;
300 }
301 esysContext->state = _ESYS_STATE_RESUBMISSION;
302 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
303 if (r != TSS2_RC_SUCCESS) {
304 LOG_WARNING("Error attempting to resubmit");
305 /* We do not set esysContext->state here but inherit the most recent
306 * state of the _async function. */
307 return r;
308 }
309 r = TSS2_ESYS_RC_TRY_AGAIN;
310 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
311 return r;
312 }
313 /* The following is the "regular error" handling. */
314 if (iesys_tpm_error(r)) {
315 LOG_WARNING("Received TPM Error");
316 esysContext->state = _ESYS_STATE_INIT;
317 return r;
318 } else if (r != TSS2_RC_SUCCESS) {
319 LOG_ERROR("Received a non-TPM Error");
320 esysContext->state = _ESYS_STATE_INTERNALERROR;
321 return r;
322 }
323 /*
324 * Session value has to be updated before checking the response to ensure
325 * correct computation of HMAC. The size of the session value is
326 * decreased because the auth value is not used for the response HMAC.
327 */
328 nvIndex = esysContext->in.NV.nvIndex;
329 r = esys_GetResourceObject(esysContext, nvIndex, &nvIndexNode);
330 return_if_error(r, "get resource");
331
332 r = esys_GetResourceObject(esysContext, esysContext->session_type[0], &session);
333 return_if_error(r, "get resource");
334
335 session->rsrc.misc.rsrc_session.sizeHmacValue -= nvIndexNode->auth.size;
336
337 /* The ESYS_TR object (nvIndex) has to be invalidated */
338 r = Esys_TR_Close(esysContext, &esysContext->in.NV.nvIndex);
339 return_if_error(r, "TR_Close");
340
341 /*
342 * Now the verification of the response (hmac check) and if necessary the
343 * parameter decryption have to be done.
344 */
345 r = iesys_check_response(esysContext);
346 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
347 "Error: check response");
348
349 /*
350 * After the verification of the response we call the complete function
351 * to deliver the result.
352 */
353 r = Tss2_Sys_NV_UndefineSpaceSpecial_Complete(esysContext->sys);
354 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
355 "Received error from SAPI unmarshaling" );
356
357 esysContext->state = _ESYS_STATE_INIT;
358
359 return TSS2_RC_SUCCESS;
360 }
361