blob: 05082b7e4b7ff1cc536e4d2b504e77a9bc45e3bc [file] [log] [blame]
Peter Huewed5a36f62018-06-12 00:59:26 +02001/* SPDX-License-Identifier: BSD-2 */
Juergen Reppff821bd2017-12-11 15:21:42 +01002/*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
Juergen Reppff821bd2017-12-11 15:21:42 +01005 ******************************************************************************/
6
Philip Tricca910f17c2018-03-15 12:38:37 -07007#include "tss2_mu.h"
8#include "tss2_sys.h"
Philip Tricca8ffd3c42018-03-09 16:27:24 -08009#include "tss2_esys.h"
Juergen Repped6e6e22018-03-19 17:34:32 +010010
11#include "esys_types.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010012#include "esys_iutil.h"
13#include "esys_mu.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010014#define LOGMODULE esys
Philip Triccaa7c51ce2018-03-10 18:28:25 -080015#include "util/log.h"
Juergen Reppff821bd2017-12-11 15:21:42 +010016
Juergen Reppadd438d2018-04-09 10:00:19 +020017/** Store command parameters inside the ESYS_CONTEXT for use during _Finish */
Juergen Reppff821bd2017-12-11 15:21:42 +010018static void store_input_parameters (
19 ESYS_CONTEXT *esysContext,
20 ESYS_TR oldParent,
21 ESYS_TR newParent,
22 const TPM2B_PRIVATE *inDuplicate,
23 const TPM2B_NAME *name,
24 const TPM2B_ENCRYPTED_SECRET *inSymSeed)
25{
26 esysContext->in.Rewrap.oldParent = oldParent;
27 esysContext->in.Rewrap.newParent = newParent;
28 if (inDuplicate == NULL) {
29 esysContext->in.Rewrap.inDuplicate = NULL;
30 } else {
31 esysContext->in.Rewrap.inDuplicateData = *inDuplicate;
32 esysContext->in.Rewrap.inDuplicate =
33 &esysContext->in.Rewrap.inDuplicateData;
34 }
35 if (name == NULL) {
36 esysContext->in.Rewrap.name = NULL;
37 } else {
38 esysContext->in.Rewrap.nameData = *name;
39 esysContext->in.Rewrap.name =
40 &esysContext->in.Rewrap.nameData;
41 }
42 if (inSymSeed == NULL) {
43 esysContext->in.Rewrap.inSymSeed = NULL;
44 } else {
45 esysContext->in.Rewrap.inSymSeedData = *inSymSeed;
46 esysContext->in.Rewrap.inSymSeed =
47 &esysContext->in.Rewrap.inSymSeedData;
48 }
49}
50
51/** One-Call function for TPM2_Rewrap
52 *
53 * This function invokes the TPM2_Rewrap command in a one-call
54 * variant. This means the function will block until the TPM response is
55 * available. All input parameters are const. The memory for non-simple output
56 * parameters is allocated by the function implementation.
57 *
58 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +020059 * @param[in] oldParent Parent of object.
60 * @param[in] newParent New parent of the object.
61 * @param[in] shandle1 Session handle for authorization of oldParent
62 * @param[in] shandle2 Second session handle.
63 * @param[in] shandle3 Third session handle.
64 * @param[in] inDuplicate An object encrypted using symmetric key derived from
65 * inSymSeed.
66 * @param[in] name The Name of the object being rewrapped.
67 * @param[in] inSymSeed The seed for the symmetric key and HMAC key.
68 * @param[out] outDuplicate An object encrypted using symmetric key derived from
69 * outSymSeed.
70 * (callee-allocated)
71 * @param[out] outSymSeed Seed for a symmetric key protected by newParent
72 * asymmetric key.
73 * (callee-allocated)
Juergen Reppff821bd2017-12-11 15:21:42 +010074 * @retval TSS2_RC_SUCCESS on success
Juergen Reppf7e5bd32018-04-26 11:11:32 +020075 * @retval ESYS_RC_SUCCESS if the function call was a success.
76 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
77 * pointers or required output handle references are NULL.
78 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
79 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
80 * internal operations or return parameters.
81 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
82 * operation already pending.
83 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
84 * at least contain the tag, response length, and response code.
85 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
86 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
87 * the 'decrypt' attribute bit set.
88 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
89 * the 'encrypt' attribute bit set.
90 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown to the
91 * ESYS_CONTEXT or are of the wrong type or if required ESYS_TR objects
92 * are ESYS_TR_NONE.
93 * @retval TSS2_RCs produced by lower layers of the software stack may be
94 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +010095 */
96TSS2_RC
97Esys_Rewrap(
98 ESYS_CONTEXT *esysContext,
99 ESYS_TR oldParent,
100 ESYS_TR newParent,
101 ESYS_TR shandle1,
102 ESYS_TR shandle2,
103 ESYS_TR shandle3,
104 const TPM2B_PRIVATE *inDuplicate,
105 const TPM2B_NAME *name,
106 const TPM2B_ENCRYPTED_SECRET *inSymSeed,
107 TPM2B_PRIVATE **outDuplicate,
108 TPM2B_ENCRYPTED_SECRET **outSymSeed)
109{
Juergen Repp3b7b41b2018-03-19 15:58:04 +0100110 TSS2_RC r;
Juergen Reppff821bd2017-12-11 15:21:42 +0100111
Juergen Reppadd438d2018-04-09 10:00:19 +0200112 r = Esys_Rewrap_Async(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +0100113 oldParent,
114 newParent,
115 shandle1,
116 shandle2,
117 shandle3,
118 inDuplicate,
119 name,
120 inSymSeed);
121 return_if_error(r, "Error in async function");
122
Juergen Reppadd438d2018-04-09 10:00:19 +0200123 /* Set the timeout to indefinite for now, since we want _Finish to block */
Juergen Reppff821bd2017-12-11 15:21:42 +0100124 int32_t timeouttmp = esysContext->timeout;
125 esysContext->timeout = -1;
126 /*
127 * Now we call the finish function, until return code is not equal to
128 * from TSS2_BASE_RC_TRY_AGAIN.
129 * Note that the finish function may return TSS2_RC_TRY_AGAIN, even if we
130 * have set the timeout to -1. This occurs for example if the TPM requests
131 * a retransmission of the command via TPM2_RC_YIELDED.
132 */
133 do {
Juergen Reppadd438d2018-04-09 10:00:19 +0200134 r = Esys_Rewrap_Finish(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +0100135 outDuplicate,
136 outSymSeed);
137 /* This is just debug information about the reattempt to finish the
138 command */
139 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN)
140 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32
141 " => resubmitting command", r);
142 } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
143
144 /* Restore the timeout value to the original value */
145 esysContext->timeout = timeouttmp;
146 return_if_error(r, "Esys Finish");
147
148 return TSS2_RC_SUCCESS;
149}
150
151/** Asynchronous function for TPM2_Rewrap
152 *
153 * This function invokes the TPM2_Rewrap command in a asynchronous
154 * variant. This means the function will return as soon as the command has been
155 * sent downwards the stack to the TPM. All input parameters are const.
Juergen Reppadd438d2018-04-09 10:00:19 +0200156 * In order to retrieve the TPM's response call Esys_Rewrap_Finish.
Juergen Reppff821bd2017-12-11 15:21:42 +0100157 *
158 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200159 * @param[in] oldParent Parent of object.
160 * @param[in] newParent New parent of the object.
161 * @param[in] shandle1 Session handle for authorization of oldParent
162 * @param[in] shandle2 Second session handle.
163 * @param[in] shandle3 Third session handle.
164 * @param[in] inDuplicate An object encrypted using symmetric key derived from
165 * inSymSeed.
166 * @param[in] name The Name of the object being rewrapped.
167 * @param[in] inSymSeed The seed for the symmetric key and HMAC key.
168 * @retval ESYS_RC_SUCCESS if the function call was a success.
169 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
170 * pointers or required output handle references are NULL.
171 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
172 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
173 * internal operations or return parameters.
174 * @retval TSS2_RCs produced by lower layers of the software stack may be
175 returned to the caller unaltered unless handled internally.
176 * @retval TSS2_ESYS_RC_MULTIPLE_DECRYPT_SESSIONS: if more than one session has
177 * the 'decrypt' attribute bit set.
178 * @retval TSS2_ESYS_RC_MULTIPLE_ENCRYPT_SESSIONS: if more than one session has
179 * the 'encrypt' attribute bit set.
180 * @retval TSS2_ESYS_RC_BAD_TR: if any of the ESYS_TR objects are unknown to the
181 ESYS_CONTEXT or are of the wrong type or if required ESYS_TR objects
182 are ESYS_TR_NONE.
Juergen Reppff821bd2017-12-11 15:21:42 +0100183 */
184TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200185Esys_Rewrap_Async(
Juergen Reppff821bd2017-12-11 15:21:42 +0100186 ESYS_CONTEXT *esysContext,
187 ESYS_TR oldParent,
188 ESYS_TR newParent,
189 ESYS_TR shandle1,
190 ESYS_TR shandle2,
191 ESYS_TR shandle3,
192 const TPM2B_PRIVATE *inDuplicate,
193 const TPM2B_NAME *name,
194 const TPM2B_ENCRYPTED_SECRET *inSymSeed)
195{
Juergen Repp3b7b41b2018-03-19 15:58:04 +0100196 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200197 LOG_TRACE("context=%p, oldParent=%"PRIx32 ", newParent=%"PRIx32 ","
198 "inDuplicate=%p, name=%p, inSymSeed=%p",
199 esysContext, oldParent, newParent, inDuplicate, name,
200 inSymSeed);
Andreas Fuchsc5a73eb2018-03-19 16:01:00 +0100201 TSS2L_SYS_AUTH_COMMAND auths;
Juergen Reppff821bd2017-12-11 15:21:42 +0100202 RSRC_NODE_T *oldParentNode;
203 RSRC_NODE_T *newParentNode;
204
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100205 /* Check context, sequence correctness and set state to error for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100206 if (esysContext == NULL) {
207 LOG_ERROR("esyscontext is NULL.");
208 return TSS2_ESYS_RC_BAD_REFERENCE;
209 }
210 r = iesys_check_sequence_async(esysContext);
211 if (r != TSS2_RC_SUCCESS)
212 return r;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100213 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100214
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100215 /* Check and store input parameters */
dantpmf6ef2472018-04-06 15:21:59 -0700216 r = check_session_feasibility(shandle1, shandle2, shandle3, 1);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100217 return_state_if_error(r, _ESYS_STATE_INIT, "Check session usage");
Juergen Reppff821bd2017-12-11 15:21:42 +0100218 store_input_parameters(esysContext, oldParent, newParent,
219 inDuplicate,
220 name,
221 inSymSeed);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100222
223 /* Retrieve the metadata objects for provided handles */
Juergen Reppff821bd2017-12-11 15:21:42 +0100224 r = esys_GetResourceObject(esysContext, oldParent, &oldParentNode);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100225 return_state_if_error(r, _ESYS_STATE_INIT, "oldParent unknown.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100226 r = esys_GetResourceObject(esysContext, newParent, &newParentNode);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100227 return_state_if_error(r, _ESYS_STATE_INIT, "newParent unknown.");
228
229 /* Initial invocation of SAPI to prepare the command buffer with parameters */
Juergen Reppff821bd2017-12-11 15:21:42 +0100230 r = Tss2_Sys_Rewrap_Prepare(esysContext->sys,
231 (oldParentNode == NULL) ? TPM2_RH_NULL : oldParentNode->rsrc.handle,
232 (newParentNode == NULL) ? TPM2_RH_NULL : newParentNode->rsrc.handle,
233 inDuplicate,
234 name,
235 inSymSeed);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100236 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI Prepare returned error.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100237
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100238 /* Calculate the cpHash Values */
239 r = init_session_tab(esysContext, shandle1, shandle2, shandle3);
240 return_state_if_error(r, _ESYS_STATE_INIT, "Initialize session resources");
Juergen Reppff821bd2017-12-11 15:21:42 +0100241 iesys_compute_session_value(esysContext->session_tab[0],
242 &oldParentNode->rsrc.name, &oldParentNode->auth);
243 iesys_compute_session_value(esysContext->session_tab[1], NULL, NULL);
244 iesys_compute_session_value(esysContext->session_tab[2], NULL, NULL);
Juergen Reppff821bd2017-12-11 15:21:42 +0100245
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100246 /* Generate the auth values and set them in the SAPI command buffer */
247 r = iesys_gen_auths(esysContext, oldParentNode, newParentNode, NULL, &auths);
248 return_state_if_error(r, _ESYS_STATE_INIT, "Error in computation of auth values");
Juergen Reppff821bd2017-12-11 15:21:42 +0100249 esysContext->authsCount = auths.count;
250 r = Tss2_Sys_SetCmdAuths(esysContext->sys, &auths);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100251 return_state_if_error(r, _ESYS_STATE_INIT, "SAPI error on SetCmdAuths");
Juergen Reppff821bd2017-12-11 15:21:42 +0100252
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100253 /* Trigger execution and finish the async invocation */
Juergen Reppff821bd2017-12-11 15:21:42 +0100254 r = Tss2_Sys_ExecuteAsync(esysContext->sys);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100255 return_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Finish (Execute Async)");
Juergen Reppff821bd2017-12-11 15:21:42 +0100256
257 esysContext->state = _ESYS_STATE_SENT;
258
259 return r;
260}
261
262/** Asynchronous finish function for TPM2_Rewrap
263 *
264 * This function returns the results of a TPM2_Rewrap command
Juergen Reppadd438d2018-04-09 10:00:19 +0200265 * invoked via Esys_Rewrap_Finish. All non-simple output parameters
Juergen Reppff821bd2017-12-11 15:21:42 +0100266 * are allocated by the function's implementation. NULL can be passed for every
267 * output parameter if the value is not required.
268 *
269 * @param[in,out] esysContext The ESYS_CONTEXT.
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200270 * @param[out] outDuplicate An object encrypted using symmetric key derived from
271 * outSymSeed.
272 * (callee-allocated)
273 * @param[out] outSymSeed Seed for a symmetric key protected by newParent
274 * asymmetric key.
275 * (callee-allocated)
Juergen Reppff821bd2017-12-11 15:21:42 +0100276 * @retval TSS2_RC_SUCCESS on success
Juergen Reppf7e5bd32018-04-26 11:11:32 +0200277 * @retval ESYS_RC_SUCCESS if the function call was a success.
278 * @retval TSS2_ESYS_RC_BAD_REFERENCE if the esysContext or required input
279 * pointers or required output handle references are NULL.
280 * @retval TSS2_ESYS_RC_BAD_CONTEXT: if esysContext corruption is detected.
281 * @retval TSS2_ESYS_RC_MEMORY: if the ESAPI cannot allocate enough memory for
282 * internal operations or return parameters.
283 * @retval TSS2_ESYS_RC_BAD_SEQUENCE: if the context has an asynchronous
284 * operation already pending.
285 * @retval TSS2_ESYS_RC_TRY_AGAIN: if the timeout counter expires before the
286 * TPM response is received.
287 * @retval TSS2_ESYS_RC_INSUFFICIENT_RESPONSE: if the TPM's response does not
288 * at least contain the tag, response length, and response code.
289 * @retval TSS2_ESYS_RC_MALFORMED_RESPONSE: if the TPM's response is corrupted.
290 * @retval TSS2_RCs produced by lower layers of the software stack may be
291 * returned to the caller unaltered unless handled internally.
Juergen Reppff821bd2017-12-11 15:21:42 +0100292 */
293TSS2_RC
Juergen Reppadd438d2018-04-09 10:00:19 +0200294Esys_Rewrap_Finish(
Juergen Reppff821bd2017-12-11 15:21:42 +0100295 ESYS_CONTEXT *esysContext,
296 TPM2B_PRIVATE **outDuplicate,
297 TPM2B_ENCRYPTED_SECRET **outSymSeed)
298{
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100299 TSS2_RC r;
Andreas Fuchs15bbb672018-03-27 13:21:13 +0200300 LOG_TRACE("context=%p, outDuplicate=%p, outSymSeed=%p",
301 esysContext, outDuplicate, outSymSeed);
302
Juergen Reppff821bd2017-12-11 15:21:42 +0100303 if (esysContext == NULL) {
304 LOG_ERROR("esyscontext is NULL.");
305 return TSS2_ESYS_RC_BAD_REFERENCE;
306 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100307
308 /* Check for correct sequence and set sequence to irregular for now */
Juergen Reppff821bd2017-12-11 15:21:42 +0100309 if (esysContext->state != _ESYS_STATE_SENT) {
310 LOG_ERROR("Esys called in bad sequence.");
311 return TSS2_ESYS_RC_BAD_SEQUENCE;
312 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100313 esysContext->state = _ESYS_STATE_INTERNALERROR;
314
315 /* Allocate memory for response parameters */
Juergen Reppff821bd2017-12-11 15:21:42 +0100316 if (outDuplicate != NULL) {
317 *outDuplicate = calloc(sizeof(TPM2B_PRIVATE), 1);
318 if (*outDuplicate == NULL) {
319 return_error(TSS2_ESYS_RC_MEMORY, "Out of memory");
320 }
321 }
322 if (outSymSeed != NULL) {
323 *outSymSeed = calloc(sizeof(TPM2B_ENCRYPTED_SECRET), 1);
324 if (*outSymSeed == NULL) {
325 goto_error(r, TSS2_ESYS_RC_MEMORY, "Out of memory", error_cleanup);
326 }
327 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100328
329 /*Receive the TPM response and handle resubmissions if necessary. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100330 r = Tss2_Sys_ExecuteFinish(esysContext->sys, esysContext->timeout);
331 if ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN) {
332 LOG_DEBUG("A layer below returned TRY_AGAIN: %" PRIx32, r);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100333 esysContext->state = _ESYS_STATE_SENT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100334 goto error_cleanup;
335 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100336 /* This block handle the resubmission of TPM commands given a certain set of
337 * TPM response codes. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100338 if (r == TPM2_RC_RETRY || r == TPM2_RC_TESTING || r == TPM2_RC_YIELDED) {
339 LOG_DEBUG("TPM returned RETRY, TESTING or YIELDED, which triggers a "
340 "resubmission: %" PRIx32, r);
Andreas Fuchs631d7e62018-03-19 10:54:56 +0100341 if (esysContext->submissionCount >= _ESYS_MAX_SUBMISSIONS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100342 LOG_WARNING("Maximum number of (re)submissions has been reached.");
343 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100344 goto error_cleanup;
345 }
346 esysContext->state = _ESYS_STATE_RESUBMISSION;
Juergen Reppadd438d2018-04-09 10:00:19 +0200347 r = Esys_Rewrap_Async(esysContext,
Juergen Reppff821bd2017-12-11 15:21:42 +0100348 esysContext->in.Rewrap.oldParent,
349 esysContext->in.Rewrap.newParent,
350 esysContext->session_type[0],
351 esysContext->session_type[1],
352 esysContext->session_type[2],
353 esysContext->in.Rewrap.inDuplicate,
354 esysContext->in.Rewrap.name,
355 esysContext->in.Rewrap.inSymSeed);
356 if (r != TSS2_RC_SUCCESS) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100357 LOG_WARNING("Error attempting to resubmit");
358 /* We do not set esysContext->state here but inherit the most recent
359 * state of the _async function. */
Juergen Reppff821bd2017-12-11 15:21:42 +0100360 goto error_cleanup;
361 }
362 r = TSS2_ESYS_RC_TRY_AGAIN;
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100363 LOG_DEBUG("Resubmission initiated and returning RC_TRY_AGAIN.");
Juergen Reppff821bd2017-12-11 15:21:42 +0100364 goto error_cleanup;
365 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100366 /* The following is the "regular error" handling. */
Juergen Repp033da112018-07-10 13:06:18 +0200367 if (iesys_tpm_error(r)) {
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100368 LOG_WARNING("Received TPM Error");
369 esysContext->state = _ESYS_STATE_INIT;
370 goto error_cleanup;
371 } else if (r != TSS2_RC_SUCCESS) {
372 LOG_ERROR("Received a non-TPM Error");
373 esysContext->state = _ESYS_STATE_INTERNALERROR;
Juergen Reppff821bd2017-12-11 15:21:42 +0100374 goto error_cleanup;
375 }
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100376
Juergen Reppff821bd2017-12-11 15:21:42 +0100377 /*
378 * Now the verification of the response (hmac check) and if necessary the
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100379 * parameter decryption have to be done.
Juergen Reppff821bd2017-12-11 15:21:42 +0100380 */
Juergen Repp109de7d2018-02-23 13:42:08 +0100381 r = iesys_check_response(esysContext);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100382 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Error: check response",
Juergen Reppff821bd2017-12-11 15:21:42 +0100383 error_cleanup);
Juergen Reppff821bd2017-12-11 15:21:42 +0100384 /*
385 * After the verification of the response we call the complete function
386 * to deliver the result.
387 */
388 r = Tss2_Sys_Rewrap_Complete(esysContext->sys,
389 (outDuplicate != NULL) ? *outDuplicate : NULL,
390 (outSymSeed != NULL) ? *outSymSeed : NULL);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100391 goto_state_if_error(r, _ESYS_STATE_INTERNALERROR, "Received error from SAPI"
dantpmf6ef2472018-04-06 15:21:59 -0700392 " unmarshaling" ,error_cleanup);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100393 esysContext->state = _ESYS_STATE_INIT;
Juergen Reppff821bd2017-12-11 15:21:42 +0100394
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100395 return TSS2_RC_SUCCESS;
Juergen Reppff821bd2017-12-11 15:21:42 +0100396
397error_cleanup:
398 if (outDuplicate != NULL)
399 SAFE_FREE(*outDuplicate);
400 if (outSymSeed != NULL)
401 SAFE_FREE(*outSymSeed);
Andreas Fuchs4d9961e2018-03-20 15:51:48 +0100402
Juergen Reppff821bd2017-12-11 15:21:42 +0100403 return r;
Philip Tricca910f17c2018-03-15 12:38:37 -0700404}