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