xref: /aosp_15_r20/external/tpm2-tss/src/tss2-fapi/api/Fapi_NvWrite.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
1*758e9fbaSOystein Eftevaag /* SPDX-License-Identifier: BSD-2-Clause */
2*758e9fbaSOystein Eftevaag /*******************************************************************************
3*758e9fbaSOystein Eftevaag  * Copyright 2018-2019, Fraunhofer SIT sponsored by Infineon Technologies AG
4*758e9fbaSOystein Eftevaag  * All rights reserved.
5*758e9fbaSOystein Eftevaag  ******************************************************************************/
6*758e9fbaSOystein Eftevaag 
7*758e9fbaSOystein Eftevaag #ifdef HAVE_CONFIG_H
8*758e9fbaSOystein Eftevaag #include <config.h>
9*758e9fbaSOystein Eftevaag #endif
10*758e9fbaSOystein Eftevaag 
11*758e9fbaSOystein Eftevaag #include <string.h>
12*758e9fbaSOystein Eftevaag #include <stdlib.h>
13*758e9fbaSOystein Eftevaag #include <unistd.h>
14*758e9fbaSOystein Eftevaag #include <errno.h>
15*758e9fbaSOystein Eftevaag 
16*758e9fbaSOystein Eftevaag #include "tss2_fapi.h"
17*758e9fbaSOystein Eftevaag #include "fapi_int.h"
18*758e9fbaSOystein Eftevaag #include "fapi_util.h"
19*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
20*758e9fbaSOystein Eftevaag #include "fapi_policy.h"
21*758e9fbaSOystein Eftevaag #define LOGMODULE fapi
22*758e9fbaSOystein Eftevaag #include "util/log.h"
23*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
24*758e9fbaSOystein Eftevaag 
25*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_NvWrite
26*758e9fbaSOystein Eftevaag  *
27*758e9fbaSOystein Eftevaag  * Writes data to a "regular" (not pin, extend or counter) NV index.
28*758e9fbaSOystein Eftevaag  *
29*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
30*758e9fbaSOystein Eftevaag  * @param[in] nvPath The path of the NV index to write
31*758e9fbaSOystein Eftevaag  * @param[in] data The data to write to the NV index
32*758e9fbaSOystein Eftevaag  * @param[in] size The size of data in bytes
33*758e9fbaSOystein Eftevaag  *
34*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
35*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, nvPath, or data is NULL.
36*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
37*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH: if nvPath is not found.
38*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NV_EXCEEDED: if the NV is not large enough for the data
39*758e9fbaSOystein Eftevaag  *         to be written.
40*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NV_WRONG_TYPE: if the NV index is not a "regular" one.
41*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NV_NOT_WRITEABLE: if the NV is not a writeable index.
42*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_POLICY_UNKNOWN: if the policy is unknown.
43*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
44*758e9fbaSOystein Eftevaag  *         operation already pending.
45*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
46*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
47*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
48*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
49*758e9fbaSOystein Eftevaag  *         config file.
50*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
51*758e9fbaSOystein Eftevaag  *         this function needs to be called again.
52*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
53*758e9fbaSOystein Eftevaag  *         the function.
54*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
55*758e9fbaSOystein Eftevaag  *         during authorization.
56*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
57*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
58*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
59*758e9fbaSOystein Eftevaag  *         is not set.
60*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
61*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
62*758e9fbaSOystein Eftevaag  */
63*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_NvWrite(FAPI_CONTEXT * context,char const * nvPath,uint8_t const * data,size_t size)64*758e9fbaSOystein Eftevaag Fapi_NvWrite(
65*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context,
66*758e9fbaSOystein Eftevaag     char    const *nvPath,
67*758e9fbaSOystein Eftevaag     uint8_t const *data,
68*758e9fbaSOystein Eftevaag     size_t         size)
69*758e9fbaSOystein Eftevaag {
70*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
71*758e9fbaSOystein Eftevaag 
72*758e9fbaSOystein Eftevaag     TSS2_RC r, r2;
73*758e9fbaSOystein Eftevaag 
74*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
75*758e9fbaSOystein Eftevaag     check_not_null(context);
76*758e9fbaSOystein Eftevaag     check_not_null(nvPath);
77*758e9fbaSOystein Eftevaag     check_not_null(data);
78*758e9fbaSOystein Eftevaag 
79*758e9fbaSOystein Eftevaag     /* Check whether TCTI and ESYS are initialized */
80*758e9fbaSOystein Eftevaag     return_if_null(context->esys, "Command can't be executed in none TPM mode.",
81*758e9fbaSOystein Eftevaag                    TSS2_FAPI_RC_NO_TPM);
82*758e9fbaSOystein Eftevaag 
83*758e9fbaSOystein Eftevaag     /* If the async state automata of FAPI shall be tested, then we must not set
84*758e9fbaSOystein Eftevaag        the timeouts of ESYS to blocking mode.
85*758e9fbaSOystein Eftevaag        During testing, the mssim tcti will ensure multiple re-invocations.
86*758e9fbaSOystein Eftevaag        Usually however the synchronous invocations of FAPI shall instruct ESYS
87*758e9fbaSOystein Eftevaag        to block until a result is available. */
88*758e9fbaSOystein Eftevaag #ifndef TEST_FAPI_ASYNC
89*758e9fbaSOystein Eftevaag     r = Esys_SetTimeout(context->esys, TSS2_TCTI_TIMEOUT_BLOCK);
90*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Set Timeout to blocking");
91*758e9fbaSOystein Eftevaag #endif /* TEST_FAPI_ASYNC */
92*758e9fbaSOystein Eftevaag 
93*758e9fbaSOystein Eftevaag     r = Fapi_NvWrite_Async(context, nvPath, data, size);
94*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "NV_Write");
95*758e9fbaSOystein Eftevaag 
96*758e9fbaSOystein Eftevaag     do {
97*758e9fbaSOystein Eftevaag         /* We wait for file I/O to be ready if the FAPI state automata
98*758e9fbaSOystein Eftevaag            are in a file I/O state. */
99*758e9fbaSOystein Eftevaag         r = ifapi_io_poll(&context->io);
100*758e9fbaSOystein Eftevaag         return_if_error(r, "Something went wrong with IO polling");
101*758e9fbaSOystein Eftevaag 
102*758e9fbaSOystein Eftevaag         /* Repeatedly call the finish function, until FAPI has transitioned
103*758e9fbaSOystein Eftevaag            through all execution stages / states of this invocation. */
104*758e9fbaSOystein Eftevaag         r = Fapi_NvWrite_Finish(context);
105*758e9fbaSOystein Eftevaag     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
106*758e9fbaSOystein Eftevaag 
107*758e9fbaSOystein Eftevaag     /* Reset the ESYS timeout to non-blocking, immediate response. */
108*758e9fbaSOystein Eftevaag     r2 = Esys_SetTimeout(context->esys, 0);
109*758e9fbaSOystein Eftevaag     return_if_error(r2, "Set Timeout to non-blocking");
110*758e9fbaSOystein Eftevaag 
111*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "NV_Write");
112*758e9fbaSOystein Eftevaag 
113*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
114*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
115*758e9fbaSOystein Eftevaag }
116*758e9fbaSOystein Eftevaag 
117*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_NvWrite
118*758e9fbaSOystein Eftevaag  *
119*758e9fbaSOystein Eftevaag  * Writes data to a "regular" (not pin, extend or counter) NV index.
120*758e9fbaSOystein Eftevaag  *
121*758e9fbaSOystein Eftevaag  * Call Fapi_NvWrite_Finish to finish the execution of this command.
122*758e9fbaSOystein Eftevaag  *
123*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
124*758e9fbaSOystein Eftevaag  * @param[in] nvPath The path of the NV index to write
125*758e9fbaSOystein Eftevaag  * @param[in] data The data to write to the NV index
126*758e9fbaSOystein Eftevaag  * @param[in] size The size of data in bytes
127*758e9fbaSOystein Eftevaag  *
128*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
129*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, nvPath, or data is NULL.
130*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
131*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH: if nvPath is not found.
132*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NV_EXCEEDED: if the NV is not large enough for the data
133*758e9fbaSOystein Eftevaag  *         to be written.
134*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NV_WRONG_TYPE: if the NV index is not a "regular" one.
135*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NV_NOT_WRITEABLE: if the NV is not a writeable index.
136*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_POLICY_UNKNOWN: if the policy is unknown.
137*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
138*758e9fbaSOystein Eftevaag  *         operation already pending.
139*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
140*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
141*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
142*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
143*758e9fbaSOystein Eftevaag  *         config file.
144*758e9fbaSOystein Eftevaag  */
145*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_NvWrite_Async(FAPI_CONTEXT * context,char const * nvPath,uint8_t const * data,size_t size)146*758e9fbaSOystein Eftevaag Fapi_NvWrite_Async(
147*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context,
148*758e9fbaSOystein Eftevaag     char    const *nvPath,
149*758e9fbaSOystein Eftevaag     uint8_t const *data,
150*758e9fbaSOystein Eftevaag     size_t         size)
151*758e9fbaSOystein Eftevaag {
152*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
153*758e9fbaSOystein Eftevaag     LOG_TRACE("nvPath: %s", nvPath);
154*758e9fbaSOystein Eftevaag     if (data) {
155*758e9fbaSOystein Eftevaag         LOGBLOB_TRACE(data, size, "data");
156*758e9fbaSOystein Eftevaag     } else {
157*758e9fbaSOystein Eftevaag         LOG_TRACE("data: (null) size: %zi", size);
158*758e9fbaSOystein Eftevaag     }
159*758e9fbaSOystein Eftevaag 
160*758e9fbaSOystein Eftevaag     TSS2_RC r;
161*758e9fbaSOystein Eftevaag 
162*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
163*758e9fbaSOystein Eftevaag     check_not_null(context);
164*758e9fbaSOystein Eftevaag     check_not_null(nvPath);
165*758e9fbaSOystein Eftevaag     check_not_null(data);
166*758e9fbaSOystein Eftevaag 
167*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
168*758e9fbaSOystein Eftevaag     IFAPI_NV_Cmds * command = &context->nv_cmd;
169*758e9fbaSOystein Eftevaag 
170*758e9fbaSOystein Eftevaag     /* Reset all context-internal session state information. */
171*758e9fbaSOystein Eftevaag     r = ifapi_session_init(context);
172*758e9fbaSOystein Eftevaag     return_if_error(r, "Initialize NV_Write");
173*758e9fbaSOystein Eftevaag 
174*758e9fbaSOystein Eftevaag     /* Initialize the command */
175*758e9fbaSOystein Eftevaag     uint8_t * commandData = NULL;
176*758e9fbaSOystein Eftevaag     memset(&context->nv_cmd, 0, sizeof(IFAPI_NV_Cmds));
177*758e9fbaSOystein Eftevaag     command->offset = 0;
178*758e9fbaSOystein Eftevaag     command->data = NULL;
179*758e9fbaSOystein Eftevaag 
180*758e9fbaSOystein Eftevaag 
181*758e9fbaSOystein Eftevaag     /* Copy parameters to context for use during _Finish. */
182*758e9fbaSOystein Eftevaag     strdup_check(command->nvPath, nvPath, r, error_cleanup);
183*758e9fbaSOystein Eftevaag 
184*758e9fbaSOystein Eftevaag     commandData = malloc(size);
185*758e9fbaSOystein Eftevaag     goto_if_null2(commandData, "Out of memory", r, TSS2_FAPI_RC_MEMORY,
186*758e9fbaSOystein Eftevaag             error_cleanup);
187*758e9fbaSOystein Eftevaag     memcpy(commandData, data, size);
188*758e9fbaSOystein Eftevaag     command->data = commandData;
189*758e9fbaSOystein Eftevaag 
190*758e9fbaSOystein Eftevaag     context->primary_state = PRIMARY_INIT;
191*758e9fbaSOystein Eftevaag     command->numBytes = size;
192*758e9fbaSOystein Eftevaag 
193*758e9fbaSOystein Eftevaag     /* Initialize the context state for this operation. */
194*758e9fbaSOystein Eftevaag     context->state = NV_WRITE_READ;
195*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
196*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
197*758e9fbaSOystein Eftevaag 
198*758e9fbaSOystein Eftevaag error_cleanup:
199*758e9fbaSOystein Eftevaag     /* Cleanup duplicated input parameters that were copied before. */
200*758e9fbaSOystein Eftevaag     SAFE_FREE(command->nvPath);
201*758e9fbaSOystein Eftevaag     SAFE_FREE(command->data);
202*758e9fbaSOystein Eftevaag     return r;
203*758e9fbaSOystein Eftevaag }
204*758e9fbaSOystein Eftevaag 
205*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_NvWrite
206*758e9fbaSOystein Eftevaag  *
207*758e9fbaSOystein Eftevaag  * This function should be called after a previous Fapi_NvWrite.
208*758e9fbaSOystein Eftevaag  *
209*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
210*758e9fbaSOystein Eftevaag  *
211*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
212*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context is NULL.
213*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
214*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
215*758e9fbaSOystein Eftevaag  *         operation already pending.
216*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
217*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
218*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
219*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
220*758e9fbaSOystein Eftevaag  *         complete. Call this function again later.
221*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
222*758e9fbaSOystein Eftevaag  *         the function.
223*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH if the used path in inappropriate-
224*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
225*758e9fbaSOystein Eftevaag  *         during authorization.
226*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
227*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
228*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
229*758e9fbaSOystein Eftevaag  *         is not set.
230*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
231*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
232*758e9fbaSOystein Eftevaag  *         was not successful.
233*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
234*758e9fbaSOystein Eftevaag  */
235*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_NvWrite_Finish(FAPI_CONTEXT * context)236*758e9fbaSOystein Eftevaag Fapi_NvWrite_Finish(
237*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context)
238*758e9fbaSOystein Eftevaag {
239*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
240*758e9fbaSOystein Eftevaag 
241*758e9fbaSOystein Eftevaag     TSS2_RC r;
242*758e9fbaSOystein Eftevaag     json_object *jso = NULL;
243*758e9fbaSOystein Eftevaag 
244*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
245*758e9fbaSOystein Eftevaag     check_not_null(context);
246*758e9fbaSOystein Eftevaag 
247*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
248*758e9fbaSOystein Eftevaag     IFAPI_NV_Cmds * command = &context->nv_cmd;
249*758e9fbaSOystein Eftevaag 
250*758e9fbaSOystein Eftevaag     switch (context->state) {
251*758e9fbaSOystein Eftevaag     statecase(context->state, NV_WRITE_READ);
252*758e9fbaSOystein Eftevaag         /* First check whether the file in object store can be updated. */
253*758e9fbaSOystein Eftevaag         r = ifapi_keystore_check_writeable(&context->keystore, &context->io, command->nvPath);
254*758e9fbaSOystein Eftevaag         goto_if_error_reset_state(r, "Check whether update object store is possible.", error_cleanup);
255*758e9fbaSOystein Eftevaag 
256*758e9fbaSOystein Eftevaag         /* Write to the NV index. */
257*758e9fbaSOystein Eftevaag         r = ifapi_nv_write(context, command->nvPath, command->offset,
258*758e9fbaSOystein Eftevaag                            command->data, command->numBytes);
259*758e9fbaSOystein Eftevaag 
260*758e9fbaSOystein Eftevaag         return_try_again(r);
261*758e9fbaSOystein Eftevaag         goto_if_error_reset_state(r, " FAPI NV Write", error_cleanup);
262*758e9fbaSOystein Eftevaag 
263*758e9fbaSOystein Eftevaag 
264*758e9fbaSOystein Eftevaag         /* Perform esys serialization if necessary */
265*758e9fbaSOystein Eftevaag         r = ifapi_esys_serialize_object(context->esys, &command->nv_object);
266*758e9fbaSOystein Eftevaag         goto_if_error(r, "Prepare serialization", error_cleanup);
267*758e9fbaSOystein Eftevaag 
268*758e9fbaSOystein Eftevaag         /* Start writing the NV object to the key store */
269*758e9fbaSOystein Eftevaag         r = ifapi_keystore_store_async(&context->keystore, &context->io,
270*758e9fbaSOystein Eftevaag                                        command->nvPath,
271*758e9fbaSOystein Eftevaag                                        &command->nv_object);
272*758e9fbaSOystein Eftevaag         goto_if_error_reset_state(r, "Could not open: %sh", error_cleanup,
273*758e9fbaSOystein Eftevaag                                   command->nvPath);
274*758e9fbaSOystein Eftevaag 
275*758e9fbaSOystein Eftevaag         fallthrough;
276*758e9fbaSOystein Eftevaag 
277*758e9fbaSOystein Eftevaag     statecase(context->state, NV_WRITE_WRITE);
278*758e9fbaSOystein Eftevaag         /* Finish writing the NV object to the key store */
279*758e9fbaSOystein Eftevaag         r = ifapi_keystore_store_finish(&context->keystore, &context->io);
280*758e9fbaSOystein Eftevaag         return_try_again(r);
281*758e9fbaSOystein Eftevaag         return_if_error_reset_state(r, "write_finish failed");
282*758e9fbaSOystein Eftevaag 
283*758e9fbaSOystein Eftevaag         fallthrough;
284*758e9fbaSOystein Eftevaag 
285*758e9fbaSOystein Eftevaag     statecase(context->state, NV_WRITE_CLEANUP)
286*758e9fbaSOystein Eftevaag         /* Cleanup the authorization session. */
287*758e9fbaSOystein Eftevaag         r = ifapi_cleanup_session(context);
288*758e9fbaSOystein Eftevaag         try_again_or_error_goto(r, "Cleanup", error_cleanup);
289*758e9fbaSOystein Eftevaag 
290*758e9fbaSOystein Eftevaag         context->state = _FAPI_STATE_INIT;
291*758e9fbaSOystein Eftevaag         break;
292*758e9fbaSOystein Eftevaag 
293*758e9fbaSOystein Eftevaag     statecasedefault(context->state);
294*758e9fbaSOystein Eftevaag     }
295*758e9fbaSOystein Eftevaag 
296*758e9fbaSOystein Eftevaag error_cleanup:
297*758e9fbaSOystein Eftevaag     /* Cleanup any intermediate results and state stored in the context. */
298*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&command->nv_object);
299*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
300*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(context->loadKey.key_object);
301*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
302*758e9fbaSOystein Eftevaag     SAFE_FREE(context->nv_cmd.write_data);
303*758e9fbaSOystein Eftevaag     SAFE_FREE(command->nvPath);
304*758e9fbaSOystein Eftevaag     SAFE_FREE(command->data);
305*758e9fbaSOystein Eftevaag     SAFE_FREE(jso);
306*758e9fbaSOystein Eftevaag     ifapi_session_clean(context);
307*758e9fbaSOystein Eftevaag 
308*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
309*758e9fbaSOystein Eftevaag     return r;
310*758e9fbaSOystein Eftevaag }
311