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_PolicyAuthorizeNV
23 *
24 * This function invokes the TPM2_PolicyAuthorizeNV 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 Handle indicating the source of the authorization
31 * value.
32 * @param[in] nvIndex The NV Index of the area to read.
33 * @param[in] policySession Handle for the policy session being extended.
34 * @param[in] shandle1 Session handle for authorization of authHandle
35 * @param[in] shandle2 Second session handle.
36 * @param[in] shandle3 Third session handle.
37 * @retval TSS2_RC_SUCCESS if the function call was a success.
38 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
39 * pointers or required output handle references are NULL.
40 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
41 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
42 * internal operations or return parameters.
43 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
44 * operation already pending.
45 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
46 * at least contain the tag, response length, and response code.
47 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
48 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
49 did not verify.
50 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
51 * the 'decrypt' attribute bit set.
52 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
53 * the 'encrypt' attribute bit set.
54 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
55 * to the ESYS_CONTEXT or are of the wrong type or if required
56 * ESYS_TR objects are ESYS_TR_NONE.
57 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
58 * 'decrypt' attribute set and the command does not support encryption
59 * of the first command parameter.
60 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
61 * 'encrypt' attribute set and the command does not support encryption
62 * of the first response parameter.
63 * @retval TSS2_RCs produced by lower layers of the software stack may be
64 * returned to the caller unaltered unless handled internally.
65 */
66 TSS2_RC
Esys_PolicyAuthorizeNV(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR nvIndex,ESYS_TR policySession,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)67 Esys_PolicyAuthorizeNV(
68 ESYS_CONTEXT *esysContext,
69 ESYS_TR authHandle,
70 ESYS_TR nvIndex,
71 ESYS_TR policySession,
72 ESYS_TR shandle1,
73 ESYS_TR shandle2,
74 ESYS_TR shandle3)
75 {
76 TSS2_RC r;
77
78 r = Esys_PolicyAuthorizeNV_Async(esysContext, authHandle, nvIndex,
79 policySession, shandle1, shandle2,
80 shandle3);
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_PolicyAuthorizeNV_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_PolicyAuthorizeNV
110 *
111 * This function invokes the TPM2_PolicyAuthorizeNV 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_PolicyAuthorizeNV_Finish.
115 *
116 * @param[in,out] esysContext The ESYS_CONTEXT.
117 * @param[in] authHandle Handle indicating the source of the authorization
118 * value.
119 * @param[in] nvIndex The NV Index of the area to read.
120 * @param[in] policySession Handle for the policy session being extended.
121 * @param[in] shandle1 Session handle for authorization of authHandle
122 * @param[in] shandle2 Second session handle.
123 * @param[in] shandle3 Third session handle.
124 * @retval ESYS_RC_SUCCESS if the function call was a success.
125 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
126 * pointers or required output handle references are NULL.
127 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
128 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
129 * internal operations or return parameters.
130 * @retval TSS2_RCs produced by lower layers of the software stack may be
131 returned to the caller unaltered unless handled internally.
132 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
133 * the 'decrypt' attribute bit set.
134 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
135 * the 'encrypt' attribute bit set.
136 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
137 * to the ESYS_CONTEXT or are of the wrong type or if required
138 * ESYS_TR objects are ESYS_TR_NONE.
139 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
140 * 'decrypt' attribute set and the command does not support encryption
141 * of the first command parameter.
142 * @retval TSS2_ESYS_RC_NO_ENCRYPT_PARAM: if one of the sessions has the
143 * 'encrypt' attribute set and the command does not support encryption
144 * of the first response parameter.
145 */
146 TSS2_RC
Esys_PolicyAuthorizeNV_Async(ESYS_CONTEXT * esysContext,ESYS_TR authHandle,ESYS_TR nvIndex,ESYS_TR policySession,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3)147 Esys_PolicyAuthorizeNV_Async(
148 ESYS_CONTEXT *esysContext,
149 ESYS_TR authHandle,
150 ESYS_TR nvIndex,
151 ESYS_TR policySession,
152 ESYS_TR shandle1,
153 ESYS_TR shandle2,
154 ESYS_TR shandle3)
155 {
156 TSS2_RC r;
157 LOG_TRACE("context=%p, authHandle=%"PRIx32 ", nvIndex=%"PRIx32 ","
158 "policySession=%"PRIx32 "",
159 esysContext, authHandle, nvIndex, policySession);
160 TSS2L_SYS_AUTH_COMMAND auths;
161 RSRC_NODE_T *authHandleNode;
162 RSRC_NODE_T *nvIndexNode;
163 RSRC_NODE_T *policySessionNode;
164
165 /* Check context, sequence correctness and set state to error for now */
166 if (esysContext == NULL) {
167 LOG_ERROR("esyscontext is NULL.");
168 return TSS2_ESYS_RC_BAD_REFERENCE;
169 }
170 r = iesys_check_sequence_async(esysContext);
171 if (r != TSS2_RC_SUCCESS)
172 return r;
173 esysContext->state = _ESYS_STATE_INTERNALERROR;
174
175 /* Check input parameters */
176 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
177 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
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 r = esys_GetResourceObject(esysContext, policySession, &policySessionNode);
185 return_state_if_error(r, _ESYS_STATE_INIT, "policySession unknown.");
186
187 /* Initial invocation of SAPI to prepare the command buffer with parameters */
188 r = Tss2_Sys_PolicyAuthorizeNV_Prepare(esysContext->sys,
189 (authHandleNode == NULL)
190 ? TPM2_RH_NULL
191 : authHandleNode->rsrc.handle,
192 (nvIndexNode == NULL) ? TPM2_RH_NULL
193 : nvIndexNode->rsrc.handle,
194 (policySessionNode == NULL)
195 ? TPM2_RH_NULL
196 : policySessionNode->rsrc.handle);
197 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
198
199 /* Calculate the cpHash Values */
200 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
201 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
202 if (authHandleNode != NULL)
203 iesys_compute_session_value(esysContext->session_tab[0],
204 &authHandleNode->rsrc.name, &authHandleNode->auth);
205 else
206 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
207
208 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
209 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
210
211 /* Generate the auth values and set them in the SAPI command buffer */
212 r = iesys_gen_auths(esysContext, authHandleNode, nvIndexNode, policySessionNode, &auths);
213 return_state_if_error(r, _ESYS_STATE_INIT,
214 "Error in computation of auth values");
215
216 esysContext->authsCount = auths.count;
217 if (auths.count > 0) {
218 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
219 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
220 }
221
222 /* Trigger execution and finish the async invocation */
223 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
224 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
225 "Finish (Execute Async)");
226
227 esysContext->state = _ESYS_STATE_SENT;
228
229 return r;
230 }
231
232 /** Asynchronous finish function for TPM2_PolicyAuthorizeNV
233 *
234 * This function returns the results of a TPM2_PolicyAuthorizeNV command
235 * invoked via Esys_PolicyAuthorizeNV_Finish. All non-simple output parameters
236 * are allocated by the function's implementation. NULL can be passed for every
237 * output parameter if the value is not required.
238 *
239 * @param[in,out] esysContext The ESYS_CONTEXT.
240 * @retval TSS2_RC_SUCCESS on success
241 * @retval ESYS_RC_SUCCESS if the function call was a success.
242 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
243 * pointers or required output handle references are NULL.
244 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
245 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
246 * internal operations or return parameters.
247 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
248 * operation already pending.
249 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
250 * TPM response is received.
251 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
252 * at least contain the tag, response length, and response code.
253 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
254 * not verify.
255 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
256 * @retval TSS2_RCs produced by lower layers of the software stack may be
257 * returned to the caller unaltered unless handled internally.
258 */
259 TSS2_RC
Esys_PolicyAuthorizeNV_Finish(ESYS_CONTEXT * esysContext)260 Esys_PolicyAuthorizeNV_Finish(
261 ESYS_CONTEXT *esysContext)
262 {
263 TSS2_RC r;
264 LOG_TRACE("context=%p",
265 esysContext);
266
267 if (esysContext == NULL) {
268 LOG_ERROR("esyscontext is NULL.");
269 return TSS2_ESYS_RC_BAD_REFERENCE;
270 }
271
272 /* Check for correct sequence and set sequence to irregular for now */
273 if (esysContext->state != _ESYS_STATE_SENT &&
274 esysContext->state != _ESYS_STATE_RESUBMISSION) {
275 LOG_ERROR("Esys called in bad sequence.");
276 return TSS2_ESYS_RC_BAD_SEQUENCE;
277 }
278 esysContext->state = _ESYS_STATE_INTERNALERROR;
279
280 /*Receive the TPM response and handle resubmissions if necessary. */
281 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
282 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
283 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
284 esysContext->state = _ESYS_STATE_SENT;
285 return r;
286 }
287 /* This block handle the resubmission of TPM commands given a certain set of
288 * TPM response codes. */
289 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
290 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
291 "resubmission: %" PRIx32, r);
292 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
293 LOG_WARNING("Maximum number of (re)submissions has been reached.");
294 esysContext->state = _ESYS_STATE_INIT;
295 return r;
296 }
297 esysContext->state = _ESYS_STATE_RESUBMISSION;
298 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
299 if (r != TSS2_RC_SUCCESS) {
300 LOG_WARNING("Error attempting to resubmit");
301 /* We do not set esysContext->state here but inherit the most recent
302 * state of the _async function. */
303 return r;
304 }
305 r = TSS2_ESYS_RC_TRY_AGAIN;
306 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
307 return r;
308 }
309 /* The following is the "regular error" handling. */
310 if (iesys_tpm_error(r)) {
311 LOG_WARNING("Received TPM Error");
312 esysContext->state = _ESYS_STATE_INIT;
313 return r;
314 } else if (r != TSS2_RC_SUCCESS) {
315 LOG_ERROR("Received a non-TPM Error");
316 esysContext->state = _ESYS_STATE_INTERNALERROR;
317 return r;
318 }
319
320 /*
321 * Now the verification of the response (hmac check) and if necessary the
322 * parameter decryption have to be done.
323 */
324 r = iesys_check_response(esysContext);
325 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
326 "Error: check response");
327
328 /*
329 * After the verification of the response we call the complete function
330 * to deliver the result.
331 */
332 r = Tss2_Sys_PolicyAuthorizeNV_Complete(esysContext->sys);
333 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
334 "Received error from SAPI unmarshaling" );
335
336 esysContext->state = _ESYS_STATE_INIT;
337
338 return TSS2_RC_SUCCESS;
339 }
340