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_Import
23 *
24 * This function invokes the TPM2_Import 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] parentHandle The handle of the new parent for the object.
31 * @param[in] shandle1 Session handle for authorization of parentHandle
32 * @param[in] shandle2 Second session handle.
33 * @param[in] shandle3 Third session handle.
34 * @param[in] encryptionKey The optional symmetric encryption key used as the
35 * inner wrapper for duplicate.
36 * @param[in] objectPublic The public area of the object to be imported.
37 * @param[in] duplicate The symmetrically encrypted duplicate object that may
38 * contain an inner symmetric wrapper.
39 * @param[in] inSymSeed The seed for the symmetric key and HMAC key.
40 * @param[in] symmetricAlg Definition for the symmetric algorithm to use for
41 * the inner wrapper.
42 * @param[out] outPrivate The sensitive area encrypted with the symmetric key
43 * of parentHandle.
44 * (callee-allocated)
45 * @retval TSS2_RC_SUCCESS if the function call was a success.
46 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
47 * pointers or required output handle references are NULL.
48 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
49 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
50 * internal operations or return parameters.
51 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
52 * operation already pending.
53 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
54 * at least contain the tag, response length, and response code.
55 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
56 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM
57 did not verify.
58 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
59 * the 'decrypt' attribute bit set.
60 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
61 * the 'encrypt' attribute bit set.
62 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
63 * to the ESYS_CONTEXT or are of the wrong type or if required
64 * ESYS_TR objects are ESYS_TR_NONE.
65 * @retval TSS2_RCs produced by lower layers of the software stack may be
66 * returned to the caller unaltered unless handled internally.
67 */
68 TSS2_RC
Esys_Import(ESYS_CONTEXT * esysContext,ESYS_TR parentHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_DATA * encryptionKey,const TPM2B_PUBLIC * objectPublic,const TPM2B_PRIVATE * duplicate,const TPM2B_ENCRYPTED_SECRET * inSymSeed,const TPMT_SYM_DEF_OBJECT * symmetricAlg,TPM2B_PRIVATE ** outPrivate)69 Esys_Import(
70 ESYS_CONTEXT *esysContext,
71 ESYS_TR parentHandle,
72 ESYS_TR shandle1,
73 ESYS_TR shandle2,
74 ESYS_TR shandle3,
75 const TPM2B_DATA *encryptionKey,
76 const TPM2B_PUBLIC *objectPublic,
77 const TPM2B_PRIVATE *duplicate,
78 const TPM2B_ENCRYPTED_SECRET *inSymSeed,
79 const TPMT_SYM_DEF_OBJECT *symmetricAlg,
80 TPM2B_PRIVATE **outPrivate)
81 {
82 TSS2_RC r;
83
84 r = Esys_Import_Async(esysContext, parentHandle, shandle1, shandle2,
85 shandle3, encryptionKey, objectPublic, duplicate,
86 inSymSeed, symmetricAlg);
87 return_if_error(r, "Error in async function");
88
89 /* Set the timeout to indefinite for now, since we want _Finish to block */
90 int32_t timeouttmp = esysContext->timeout;
91 esysContext->timeout = -1;
92 /*
93 * Now we call the finish function, until return code is not equal to
94 * from TSS2_BASE_RC_TRY_AGAIN.
95 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
96 * have set the timeout to -1. This occurs for example if the TPM requests
97 * a retransmission of the command via TPM2_RC_YIELDED.
98 */
99 do {
100 r = Esys_Import_Finish(esysContext, outPrivate);
101 /* This is just debug information about the reattempt to finish the
102 command */
103 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
104 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
105 " => resubmitting command", r);
106 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
107
108 /* Restore the timeout value to the original value */
109 esysContext->timeout = timeouttmp;
110 return_if_error(r, "Esys Finish");
111
112 return TSS2_RC_SUCCESS;
113 }
114
115 /** Asynchronous function for TPM2_Import
116 *
117 * This function invokes the TPM2_Import command in a asynchronous
118 * variant. This means the function will return as soon as the command has been
119 * sent downwards the stack to the TPM. All input parameters are const.
120 * In order to retrieve the TPM's response call Esys_Import_Finish.
121 *
122 * @param[in,out] esysContext The ESYS_CONTEXT.
123 * @param[in] parentHandle The handle of the new parent for the object.
124 * @param[in] shandle1 Session handle for authorization of parentHandle
125 * @param[in] shandle2 Second session handle.
126 * @param[in] shandle3 Third session handle.
127 * @param[in] encryptionKey The optional symmetric encryption key used as the
128 * inner wrapper for duplicate.
129 * @param[in] objectPublic The public area of the object to be imported.
130 * @param[in] duplicate The symmetrically encrypted duplicate object that may
131 * contain an inner symmetric wrapper.
132 * @param[in] inSymSeed The seed for the symmetric key and HMAC key.
133 * @param[in] symmetricAlg Definition for the symmetric algorithm to use for
134 * the inner wrapper.
135 * @retval ESYS_RC_SUCCESS if the function call was a success.
136 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
137 * pointers or required output handle references are NULL.
138 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
139 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
140 * internal operations or return parameters.
141 * @retval TSS2_RCs produced by lower layers of the software stack may be
142 returned to the caller unaltered unless handled internally.
143 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
144 * the 'decrypt' attribute bit set.
145 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
146 * the 'encrypt' attribute bit set.
147 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown
148 * to the ESYS_CONTEXT or are of the wrong type or if required
149 * ESYS_TR objects are ESYS_TR_NONE.
150 */
151 TSS2_RC
Esys_Import_Async(ESYS_CONTEXT * esysContext,ESYS_TR parentHandle,ESYS_TR shandle1,ESYS_TR shandle2,ESYS_TR shandle3,const TPM2B_DATA * encryptionKey,const TPM2B_PUBLIC * objectPublic,const TPM2B_PRIVATE * duplicate,const TPM2B_ENCRYPTED_SECRET * inSymSeed,const TPMT_SYM_DEF_OBJECT * symmetricAlg)152 Esys_Import_Async(
153 ESYS_CONTEXT *esysContext,
154 ESYS_TR parentHandle,
155 ESYS_TR shandle1,
156 ESYS_TR shandle2,
157 ESYS_TR shandle3,
158 const TPM2B_DATA *encryptionKey,
159 const TPM2B_PUBLIC *objectPublic,
160 const TPM2B_PRIVATE *duplicate,
161 const TPM2B_ENCRYPTED_SECRET *inSymSeed,
162 const TPMT_SYM_DEF_OBJECT *symmetricAlg)
163 {
164 TSS2_RC r;
165 LOG_TRACE("context=%p, parentHandle=%"PRIx32 ", encryptionKey=%p,"
166 "objectPublic=%p, duplicate=%p, inSymSeed=%p,"
167 "symmetricAlg=%p",
168 esysContext, parentHandle, encryptionKey, objectPublic, duplicate,
169 inSymSeed, symmetricAlg);
170 TSS2L_SYS_AUTH_COMMAND auths;
171 RSRC_NODE_T *parentHandleNode;
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, parentHandle, &parentHandleNode);
189 return_state_if_error(r, _ESYS_STATE_INIT, "parentHandle unknown.");
190
191 /* Initial invocation of SAPI to prepare the command buffer with parameters */
192 r = Tss2_Sys_Import_Prepare(esysContext->sys,
193 (parentHandleNode == NULL) ? TPM2_RH_NULL
194 : parentHandleNode->rsrc.handle, encryptionKey,
195 objectPublic, duplicate, inSymSeed,
196 symmetricAlg);
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 (parentHandleNode != NULL)
203 iesys_compute_session_value(esysContext->session_tab[0],
204 &parentHandleNode->rsrc.name, &parentHandleNode->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, parentHandleNode, NULL, NULL, &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_Import
233 *
234 * This function returns the results of a TPM2_Import command
235 * invoked via Esys_Import_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 * @param[out] outPrivate The sensitive area encrypted with the symmetric key
241 * of parentHandle.
242 * (callee-allocated)
243 * @retval TSS2_RC_SUCCESS on success
244 * @retval ESYS_RC_SUCCESS if the function call was a success.
245 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
246 * pointers or required output handle references are NULL.
247 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
248 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
249 * internal operations or return parameters.
250 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
251 * operation already pending.
252 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
253 * TPM response is received.
254 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
255 * at least contain the tag, response length, and response code.
256 * @retval TSS2_ESYS_RC_RSP_AUTH_FAILED: if the response HMAC from the TPM did
257 * not verify.
258 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
259 * @retval TSS2_RCs produced by lower layers of the software stack may be
260 * returned to the caller unaltered unless handled internally.
261 */
262 TSS2_RC
Esys_Import_Finish(ESYS_CONTEXT * esysContext,TPM2B_PRIVATE ** outPrivate)263 Esys_Import_Finish(
264 ESYS_CONTEXT *esysContext,
265 TPM2B_PRIVATE **outPrivate)
266 {
267 TSS2_RC r;
268 LOG_TRACE("context=%p, outPrivate=%p",
269 esysContext, outPrivate);
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 /* Allocate memory for response parameters */
285 if (outPrivate != NULL) {
286 *outPrivate = calloc(sizeof(TPM2B_PRIVATE), 1);
287 if (*outPrivate == NULL) {
288 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
289 }
290 }
291
292 /*Receive the TPM response and handle resubmissions if necessary. */
293 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
294 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
295 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
296 esysContext->state = _ESYS_STATE_SENT;
297 goto error_cleanup;
298 }
299 /* This block handle the resubmission of TPM commands given a certain set of
300 * TPM response codes. */
301 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
302 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
303 "resubmission: %" PRIx32, r);
304 if (esysContext->submissionCount++ >= _ESYS_MAX_SUBMISSIONS) {
305 LOG_WARNING("Maximum number of (re)submissions has been reached.");
306 esysContext->state = _ESYS_STATE_INIT;
307 goto error_cleanup;
308 }
309 esysContext->state = _ESYS_STATE_RESUBMISSION;
310 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
311 if (r != TSS2_RC_SUCCESS) {
312 LOG_WARNING("Error attempting to resubmit");
313 /* We do not set esysContext->state here but inherit the most recent
314 * state of the _async function. */
315 goto error_cleanup;
316 }
317 r = TSS2_ESYS_RC_TRY_AGAIN;
318 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
319 goto error_cleanup;
320 }
321 /* The following is the "regular error" handling. */
322 if (iesys_tpm_error(r)) {
323 LOG_WARNING("Received TPM Error");
324 esysContext->state = _ESYS_STATE_INIT;
325 goto error_cleanup;
326 } else if (r != TSS2_RC_SUCCESS) {
327 LOG_ERROR("Received a non-TPM Error");
328 esysContext->state = _ESYS_STATE_INTERNALERROR;
329 goto error_cleanup;
330 }
331
332 /*
333 * Now the verification of the response (hmac check) and if necessary the
334 * parameter decryption have to be done.
335 */
336 r = iesys_check_response(esysContext);
337 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
338 error_cleanup);
339
340 /*
341 * After the verification of the response we call the complete function
342 * to deliver the result.
343 */
344 r = Tss2_Sys_Import_Complete(esysContext->sys,
345 (outPrivate != NULL) ? *outPrivate : NULL);
346 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR,
347 "Received error from SAPI unmarshaling" ,
348 error_cleanup);
349
350 esysContext->state = _ESYS_STATE_INIT;
351
352 return TSS2_RC_SUCCESS;
353
354 error_cleanup:
355 if (outPrivate != NULL)
356 SAFE_FREE(*outPrivate);
357
358 return r;
359 }
360