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_PolicyDuplicationSelect
23 *
24 * This function invokes the TPM2_PolicyDuplicationSelect 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] policySession Handle for the policy session being extended.
31 * @param[in] shandle1 First session handle.
32 * @param[in] shandle2 Second session handle.
33 * @param[in] shandle3 Third session handle.
34 * @param[in] objectName The Name of the object to be duplicated.
35 * @param[in] newParentName The Name of the new parent.
36 * @param[in] includeObject If YES, the objectName will be included in the
37 * value in policySession->policyDigest.
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_PolicyDuplicationSelect(ESYS_CONTEXT * esysContext,ESYS_TR policySession,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_NAME * objectName,const TPM2B_NAME * newParentName,TPMI_YES_NO includeObject)68 Esys_PolicyDuplicationSelect(
69 ESYS_CONTEXT *esysContext,
70 ESYS_TR policySession,
71 ESYS_TR shandle1,
72 ESYS_TR shandle2,
73 ESYS_TR shandle3,
74 const TPM2B_NAME *objectName,
75 const TPM2B_NAME *newParentName,
76 TPMI_YES_NO includeObject)
77 {
78 TSS2_RC r;
79
80 r = Esys_PolicyDuplicationSelect_Async(esysContext, policySession, shandle1,
81 shandle2, shandle3, objectName,
82 newParentName, includeObject);
83 return_if_error(r, "Error in async function");
84
85 /* Set the timeout to indefinite for now, since we want _Finish to block */
86 int32_t timeouttmp = esysContext->timeout;
87 esysContext->timeout = -1;
88 /*
89 * Now we call the finish function, until return code is not equal to
90 * from TSS2_BASE_RC_TRY_AGAIN.
91 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
92 * have set the timeout to -1. This occurs for example if the TPM requests
93 * a retransmission of the command via TPM2_RC_YIELDED.
94 */
95 do {
96 r = Esys_PolicyDuplicationSelect_Finish(esysContext);
97 /* This is just debug information about the reattempt to finish the
98 command */
99 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
100 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
101 " => resubmitting command", r);
102 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
103
104 /* Restore the timeout value to the original value */
105 esysContext->timeout = timeouttmp;
106 return_if_error(r, "Esys Finish");
107
108 return TSS2_RC_SUCCESS;
109 }
110
111 /** Asynchronous function for TPM2_PolicyDuplicationSelect
112 *
113 * This function invokes the TPM2_PolicyDuplicationSelect command in a asynchronous
114 * variant. This means the function will return as soon as the command has been
115 * sent downwards the stack to the TPM. All input parameters are const.
116 * In order to retrieve the TPM's response call Esys_PolicyDuplicationSelect_Finish.
117 *
118 * @param[in,out] esysContext The ESYS_CONTEXT.
119 * @param[in] policySession Handle for the policy session being extended.
120 * @param[in] shandle1 First session handle.
121 * @param[in] shandle2 Second session handle.
122 * @param[in] shandle3 Third session handle.
123 * @param[in] objectName The Name of the object to be duplicated.
124 * @param[in] newParentName The Name of the new parent.
125 * @param[in] includeObject If YES, the objectName will be included in the
126 * value in policySession->policyDigest.
127 * @retval ESYS_RC_SUCCESS if the function call was a success.
128 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
129 * pointers or required output handle references are NULL.
130 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
131 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
132 * internal operations or return parameters.
133 * @retval TSS2_RCs produced by lower layers of the software stack may be
134 returned to the caller unaltered unless handled internally.
135 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
136 * the 'decrypt' attribute bit set.
137 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
138 * the 'encrypt' attribute bit set.
139 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
140 * to the ESYS_CONTEXT or are of the wrong type or if required
141 * ESYS_TR objects are ESYS_TR_NONE.
142 * @retval TSS2_ESYS_RC_NO_DECRYPT_PARAM: if one of the sessions has the
143 * 'decrypt' attribute set and the command does not support encryption
144 * of the first command parameter.
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_PolicyDuplicationSelect_Async(ESYS_CONTEXT * esysContext,ESYS_TR policySession,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_NAME * objectName,const TPM2B_NAME * newParentName,TPMI_YES_NO includeObject)150 Esys_PolicyDuplicationSelect_Async(
151 ESYS_CONTEXT *esysContext,
152 ESYS_TR policySession,
153 ESYS_TR shandle1,
154 ESYS_TR shandle2,
155 ESYS_TR shandle3,
156 const TPM2B_NAME *objectName,
157 const TPM2B_NAME *newParentName,
158 TPMI_YES_NO includeObject)
159 {
160 TSS2_RC r;
161 LOG_TRACE("context=%p, policySession=%"PRIx32 ", objectName=%p,"
162 "newParentName=%p, includeObject=%02"PRIx8"",
163 esysContext, policySession, objectName, newParentName, includeObject);
164 TSS2L_SYS_AUTH_COMMAND auths;
165 RSRC_NODE_T *policySessionNode;
166
167 /* Check context, sequence correctness and set state to error for now */
168 if (esysContext == NULL) {
169 LOG_ERROR("esyscontext is NULL.");
170 return TSS2_ESYS_RC_BAD_REFERENCE;
171 }
172 r = iesys_check_sequence_async(esysContext);
173 if (r != TSS2_RC_SUCCESS)
174 return r;
175 esysContext->state = _ESYS_STATE_INTERNALERROR;
176
177 /* Check input parameters */
178 r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
179 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
180
181 /* Retrieve the metadata objects for provided handles */
182 r = esys_GetResourceObject(esysContext, policySession, &policySessionNode);
183 return_state_if_error(r, _ESYS_STATE_INIT, "policySession unknown.");
184
185 /* Initial invocation of SAPI to prepare the command buffer with parameters */
186 r = Tss2_Sys_PolicyDuplicationSelect_Prepare(esysContext->sys,
187 (policySessionNode == NULL)
188 ? TPM2_RH_NULL
189 : policySessionNode->rsrc.handle,
190 objectName, newParentName,
191 includeObject);
192 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
193
194 /* Calculate the cpHash Values */
195 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
196 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
197 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
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, policySessionNode, 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_PolicyDuplicationSelect
223 *
224 * This function returns the results of a TPM2_PolicyDuplicationSelect command
225 * invoked via Esys_PolicyDuplicationSelect_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_PolicyDuplicationSelect_Finish(ESYS_CONTEXT * esysContext)250 Esys_PolicyDuplicationSelect_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_PolicyDuplicationSelect_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