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,const TPM2B_PUBLIC * inPublic)23 static void store_input_parameters (
24 ESYS_CONTEXT *esysContext,
25 const TPM2B_PUBLIC *inPublic)
26 {
27 if (inPublic == NULL) {
28 esysContext->in.LoadExternal.inPublic = NULL;
29 } else {
30 esysContext->in.LoadExternal.inPublicData = *inPublic;
31 esysContext->in.LoadExternal.inPublic =
32 &esysContext->in.LoadExternal.inPublicData;
33 }
34 }
35
36 /** One-Call function for TPM2_LoadExternal
37 *
38 * This function invokes the TPM2_LoadExternal command in a one-call
39 * variant. This means the function will block until the TPM response is
40 * available. All input parameters are const. The memory for non-simple output
41 * parameters is allocated by the function implementation.
42 *
43 * @param[in,out] esysContext The ESYS_CONTEXT.
44 * @param[in] shandle1 First session handle.
45 * @param[in] shandle2 Second session handle.
46 * @param[in] shandle3 Third session handle.
47 * @param[in] inPrivate The sensitive portion of the object (optional).
48 * @param[in] inPublic The public portion of the object.
49 * @param[in] hierarchy Hierarchy with which the object area is associated.
50 * @param[out] objectHandle ESYS_TR handle of ESYS resource for TPM2_HANDLE.
51 * @retval TSS2_RC_SUCCESS if the function call was a success.
52 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
53 * pointers or required output handle references are NULL.
54 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
55 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
56 * internal operations or return parameters.
57 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
58 * operation already pending.
59 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
60 * at least contain the tag, response length, and response code.
61 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
62 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
63 did not verify.
64 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
65 * the 'decrypt' attribute bit set.
66 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
67 * the 'encrypt' attribute bit set.
68 * @retval TSS2_RCs produced by lower layers of the software stack may be
69 * returned to the caller unaltered unless handled internally.
70 */
71 TSS2_RC
Esys_LoadExternal(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_SENSITIVE * inPrivate,const TPM2B_PUBLIC * inPublic,TPMI_RH_HIERARCHY hierarchy,ESYS_TR * objectHandle)72 Esys_LoadExternal(
73 ESYS_CONTEXT *esysContext,
74 ESYS_TR shandle1,
75 ESYS_TR shandle2,
76 ESYS_TR shandle3,
77 const TPM2B_SENSITIVE *inPrivate,
78 const TPM2B_PUBLIC *inPublic,
79 TPMI_RH_HIERARCHY hierarchy, ESYS_TR *objectHandle)
80 {
81 TSS2_RC r;
82
83 r = Esys_LoadExternal_Async(esysContext, shandle1, shandle2, shandle3,
84 inPrivate, inPublic, hierarchy);
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_LoadExternal_Finish(esysContext, objectHandle);
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_LoadExternal
114 *
115 * This function invokes the TPM2_LoadExternal 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_LoadExternal_Finish.
119 *
120 * @param[in,out] esysContext The ESYS_CONTEXT.
121 * @param[in] shandle1 First session handle.
122 * @param[in] shandle2 Second session handle.
123 * @param[in] shandle3 Third session handle.
124 * @param[in] inPrivate The sensitive portion of the object (optional).
125 * @param[in] inPublic The public portion of the object.
126 * @param[in] hierarchy Hierarchy with which the object area is associated.
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 */
140 TSS2_RC
Esys_LoadExternal_Async(ESYS_CONTEXT * esysContext,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_SENSITIVE * inPrivate,const TPM2B_PUBLIC * inPublic,TPMI_RH_HIERARCHY hierarchy)141 Esys_LoadExternal_Async(
142 ESYS_CONTEXT *esysContext,
143 ESYS_TR shandle1,
144 ESYS_TR shandle2,
145 ESYS_TR shandle3,
146 const TPM2B_SENSITIVE *inPrivate,
147 const TPM2B_PUBLIC *inPublic,
148 TPMI_RH_HIERARCHY hierarchy)
149 {
150 TSS2_RC r;
151 LOG_TRACE("context=%p, inPrivate=%p, inPublic=%p,"
152 "hierarchy=%"PRIx32 "",
153 esysContext, inPrivate, inPublic, hierarchy);
154 TSS2L_SYS_AUTH_COMMAND auths;
155
156 /* Check context, sequence correctness and set state to error for now */
157 if (esysContext == NULL) {
158 LOG_ERROR("esyscontext is NULL.");
159 return TSS2_ESYS_RC_BAD_REFERENCE;
160 }
161 r = iesys_check_sequence_async(esysContext);
162 if (r != TSS2_RC_SUCCESS)
163 return r;
164 esysContext->state = _ESYS_STATE_INTERNALERROR;
165
166 /* Check input parameters */
167 r = check_session_feasibility(shandle1, shandle2, shandle3, 0);
168 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
169 store_input_parameters(esysContext, inPublic);
170
171 /* Initial invocation of SAPI to prepare the command buffer with parameters */
172 r = Tss2_Sys_LoadExternal_Prepare(esysContext->sys, inPrivate, inPublic,
173 hierarchy);
174 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
175
176 /* Calculate the cpHash Values */
177 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
178 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
179 iesys_compute_session_value(esysContext->session_tab[0], NULL, NULL);
180 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
181 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
182
183 /* Generate the auth values and set them in the SAPI command buffer */
184 r = iesys_gen_auths(esysContext, NULL, NULL, NULL, &auths);
185 return_state_if_error(r, _ESYS_STATE_INIT,
186 "Error in computation of auth values");
187
188 esysContext->authsCount = auths.count;
189 if (auths.count > 0) {
190 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
191 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
192 }
193
194 /* Trigger execution and finish the async invocation */
195 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
196 return_state_if_error(r, _ESYS_STATE_INTERNALERROR,
197 "Finish (Execute Async)");
198
199 esysContext->state = _ESYS_STATE_SENT;
200
201 return r;
202 }
203
204 /** Asynchronous finish function for TPM2_LoadExternal
205 *
206 * This function returns the results of a TPM2_LoadExternal command
207 * invoked via Esys_LoadExternal_Finish. All non-simple output parameters
208 * are allocated by the function's implementation. NULL can be passed for every
209 * output parameter if the value is not required.
210 *
211 * @param[in,out] esysContext The ESYS_CONTEXT.
212 * @param[out] objectHandle ESYS_TR handle of ESYS resource for TPM2_HANDLE.
213 * @retval TSS2_RC_SUCCESS on success
214 * @retval ESYS_RC_SUCCESS if the function call was a success.
215 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
216 * pointers or required output handle references are NULL.
217 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
218 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
219 * internal operations or return parameters.
220 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
221 * operation already pending.
222 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
223 * TPM response is received.
224 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
225 * at least contain the tag, response length, and response code.
226 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
227 * not verify.
228 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
229 * @retval TSS2_RCs produced by lower layers of the software stack may be
230 * returned to the caller unaltered unless handled internally.
231 */
232 TSS2_RC
Esys_LoadExternal_Finish(ESYS_CONTEXT * esysContext,ESYS_TR * objectHandle)233 Esys_LoadExternal_Finish(
234 ESYS_CONTEXT *esysContext, ESYS_TR *objectHandle)
235 {
236 TSS2_RC r;
237 LOG_TRACE("context=%p, objectHandle=%p",
238 esysContext, objectHandle);
239
240 if (esysContext == NULL) {
241 LOG_ERROR("esyscontext is NULL.");
242 return TSS2_ESYS_RC_BAD_REFERENCE;
243 }
244
245 /* Check for correct sequence and set sequence to irregular for now */
246 if (esysContext->state != _ESYS_STATE_SENT &&
247 esysContext->state != _ESYS_STATE_RESUBMISSION) {
248 LOG_ERROR("Esys called in bad sequence.");
249 return TSS2_ESYS_RC_BAD_SEQUENCE;
250 }
251 esysContext->state = _ESYS_STATE_INTERNALERROR;
252 TPM2B_NAME name;
253 RSRC_NODE_T *objectHandleNode = NULL;
254
255 /* Allocate memory for response parameters */
256 if (objectHandle == NULL) {
257 LOG_ERROR("Handle objectHandle may not be NULL");
258 return TSS2_ESYS_RC_BAD_REFERENCE;
259 }
260 *objectHandle = esysContext->esys_handle_cnt++;
261 r = esys_CreateResourceObject(esysContext, *objectHandle, &objectHandleNode);
262 if (r != TSS2_RC_SUCCESS)
263 return r;
264
265 if (esysContext->in.LoadExternal.inPublic) {
266 objectHandleNode->rsrc.rsrcType = IESYSC_KEY_RSRC;
267 objectHandleNode->rsrc.misc.rsrc_key_pub =
268 *esysContext->in.LoadExternal.inPublic;
269 } else {
270 objectHandleNode->rsrc.misc.rsrc_key_pub.size = 0;
271 }
272
273 /*Receive the TPM response and handle resubmissions if necessary. */
274 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
275 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
276 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
277 esysContext->state = _ESYS_STATE_SENT;
278 goto error_cleanup;
279 }
280 /* This block handle the resubmission of TPM commands given a certain set of
281 * TPM response codes. */
282 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
283 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
284 "resubmission: %" PRIx32, r);
285 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
286 LOG_WARNING("Maximum number of (re)submissions has been reached.");
287 esysContext->state = _ESYS_STATE_INIT;
288 goto error_cleanup;
289 }
290 esysContext->state = _ESYS_STATE_RESUBMISSION;
291 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
292 if (r != TSS2_RC_SUCCESS) {
293 LOG_WARNING("Error attempting to resubmit");
294 /* We do not set esysContext->state here but inherit the most recent
295 * state of the _async function. */
296 goto error_cleanup;
297 }
298 r = TSS2_ESYS_RC_TRY_AGAIN;
299 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
300 goto error_cleanup;
301 }
302 /* The following is the "regular error" handling. */
303 if (iesys_tpm_error(r)) {
304 LOG_WARNING("Received TPM Error");
305 esysContext->state = _ESYS_STATE_INIT;
306 goto error_cleanup;
307 } else if (r != TSS2_RC_SUCCESS) {
308 LOG_ERROR("Received a non-TPM Error");
309 esysContext->state = _ESYS_STATE_INTERNALERROR;
310 goto error_cleanup;
311 }
312
313 /*
314 * Now the verification of the response (hmac check) and if necessary the
315 * parameter decryption have to be done.
316 */
317 r = iesys_check_response(esysContext);
318 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
319 error_cleanup);
320
321 /*
322 * After the verification of the response we call the complete function
323 * to deliver the result.
324 */
325 r = Tss2_Sys_LoadExternal_Complete(esysContext->sys,
326 &objectHandleNode->rsrc.handle, &name);
327 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
328 "Received error from SAPI unmarshaling" ,
329 error_cleanup);
330
331
332 /* check name against inPublic */
333 if (!iesys_compare_name(esysContext->in.LoadExternal.inPublic, &name)) {
334 goto_error(r, TSS2_ESYS_RC_MALFORMED_RESPONSE,
335 "in Public name not equal name in response", error_cleanup);
336 }
337 objectHandleNode->rsrc.name = name;
338 esysContext->state = _ESYS_STATE_INIT;
339
340 return TSS2_RC_SUCCESS;
341
342 error_cleanup:
343 Esys_TR_Close(esysContext, objectHandle);
344
345 return r;
346 }
347