xref: /aosp_15_r20/external/tpm2-tss/src/tss2-fapi/api/Fapi_SetAppData.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 <stdlib.h>
12*758e9fbaSOystein Eftevaag #include <errno.h>
13*758e9fbaSOystein Eftevaag #include <unistd.h>
14*758e9fbaSOystein Eftevaag #include <errno.h>
15*758e9fbaSOystein Eftevaag #include <string.h>
16*758e9fbaSOystein Eftevaag 
17*758e9fbaSOystein Eftevaag #include "tss2_fapi.h"
18*758e9fbaSOystein Eftevaag #include "fapi_int.h"
19*758e9fbaSOystein Eftevaag #include "fapi_util.h"
20*758e9fbaSOystein Eftevaag #include "tss2_esys.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_SetAppData
26*758e9fbaSOystein Eftevaag  *
27*758e9fbaSOystein Eftevaag  * Associates an arbitrary data blob with a given object.
28*758e9fbaSOystein Eftevaag  *
29*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
30*758e9fbaSOystein Eftevaag  * @param[in] path The path to the object the blob is associated with
31*758e9fbaSOystein Eftevaag  * @param[in] appData The blob to associate with the object. May be NULL
32*758e9fbaSOystein Eftevaag  * @param[in] appDataSize The size of appData in bytes. Must be 0 if appData is
33*758e9fbaSOystein Eftevaag  *            NULL
34*758e9fbaSOystein Eftevaag  *
35*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
36*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL or if appData
37*758e9fbaSOystein Eftevaag  *         is NULL and appDataSize is not 0.
38*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
39*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
40*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
41*758e9fbaSOystein Eftevaag  *         operation already pending.
42*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
43*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
44*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
45*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
46*758e9fbaSOystein Eftevaag  *         the function.
47*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
48*758e9fbaSOystein Eftevaag  *         during authorization.
49*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
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_GENERAL_FAILURE if an internal error occurred.
53*758e9fbaSOystein Eftevaag  */
54*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetAppData(FAPI_CONTEXT * context,char const * path,uint8_t const * appData,size_t appDataSize)55*758e9fbaSOystein Eftevaag Fapi_SetAppData(
56*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context,
57*758e9fbaSOystein Eftevaag     char    const *path,
58*758e9fbaSOystein Eftevaag     uint8_t const *appData,
59*758e9fbaSOystein Eftevaag     size_t         appDataSize)
60*758e9fbaSOystein Eftevaag {
61*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
62*758e9fbaSOystein Eftevaag 
63*758e9fbaSOystein Eftevaag     TSS2_RC r;
64*758e9fbaSOystein Eftevaag 
65*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
66*758e9fbaSOystein Eftevaag     check_not_null(context);
67*758e9fbaSOystein Eftevaag     check_not_null(path);
68*758e9fbaSOystein Eftevaag 
69*758e9fbaSOystein Eftevaag     r = Fapi_SetAppData_Async(context, path, appData, appDataSize);
70*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "SetAppData");
71*758e9fbaSOystein Eftevaag 
72*758e9fbaSOystein Eftevaag     do {
73*758e9fbaSOystein Eftevaag         /* We wait for file I/O to be ready if the FAPI state automata
74*758e9fbaSOystein Eftevaag            are in a file I/O state. */
75*758e9fbaSOystein Eftevaag         r = ifapi_io_poll(&context->io);
76*758e9fbaSOystein Eftevaag         return_if_error(r, "Something went wrong with IO polling");
77*758e9fbaSOystein Eftevaag 
78*758e9fbaSOystein Eftevaag         /* Repeatedly call the finish function, until FAPI has transitioned
79*758e9fbaSOystein Eftevaag            through all execution stages / states of this invocation. */
80*758e9fbaSOystein Eftevaag         r = Fapi_SetAppData_Finish(context);
81*758e9fbaSOystein Eftevaag     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
82*758e9fbaSOystein Eftevaag 
83*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "SetAppData");
84*758e9fbaSOystein Eftevaag 
85*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
86*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
87*758e9fbaSOystein Eftevaag }
88*758e9fbaSOystein Eftevaag 
89*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_SetAppData
90*758e9fbaSOystein Eftevaag  *
91*758e9fbaSOystein Eftevaag  * Associates an arbitrary data blob with a given object.
92*758e9fbaSOystein Eftevaag  *
93*758e9fbaSOystein Eftevaag  * Call Fapi_SetAppData_Finish to finish the execution of this command.
94*758e9fbaSOystein Eftevaag  *
95*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
96*758e9fbaSOystein Eftevaag  * @param[in] path The path to the object the blob is associated with
97*758e9fbaSOystein Eftevaag  * @param[in] appData The blob to associate with the object. May be NULL
98*758e9fbaSOystein Eftevaag  * @param[in] appDataSize The size of appData in bytes. Must be 0 if appData is
99*758e9fbaSOystein Eftevaag  *            NULL
100*758e9fbaSOystein Eftevaag  *
101*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
102*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL or if appData
103*758e9fbaSOystein Eftevaag  *         is NULL and appDataSize is not 0.
104*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
105*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
106*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
107*758e9fbaSOystein Eftevaag  *         operation already pending.
108*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
109*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
110*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
111*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
112*758e9fbaSOystein Eftevaag  *         the function.
113*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
114*758e9fbaSOystein Eftevaag  *         during authorization.
115*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
116*758e9fbaSOystein Eftevaag  */
117*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetAppData_Async(FAPI_CONTEXT * context,char const * path,uint8_t const * appData,size_t appDataSize)118*758e9fbaSOystein Eftevaag Fapi_SetAppData_Async(
119*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context,
120*758e9fbaSOystein Eftevaag     char    const *path,
121*758e9fbaSOystein Eftevaag     uint8_t const *appData,
122*758e9fbaSOystein Eftevaag     size_t         appDataSize)
123*758e9fbaSOystein Eftevaag {
124*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
125*758e9fbaSOystein Eftevaag     LOG_TRACE("path: %s", path);
126*758e9fbaSOystein Eftevaag     if (appData) {
127*758e9fbaSOystein Eftevaag         LOGBLOB_TRACE(appData, appDataSize, "appData");
128*758e9fbaSOystein Eftevaag     } else {
129*758e9fbaSOystein Eftevaag         LOG_TRACE("appData: (null) appDataSize: %zi", appDataSize);
130*758e9fbaSOystein Eftevaag     }
131*758e9fbaSOystein Eftevaag 
132*758e9fbaSOystein Eftevaag     TSS2_RC r;
133*758e9fbaSOystein Eftevaag 
134*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
135*758e9fbaSOystein Eftevaag     check_not_null(context);
136*758e9fbaSOystein Eftevaag     check_not_null(path);
137*758e9fbaSOystein Eftevaag 
138*758e9fbaSOystein Eftevaag     /* Check for invalid parameters */
139*758e9fbaSOystein Eftevaag     if (!appData && appDataSize != 0) {
140*758e9fbaSOystein Eftevaag         return_error(TSS2_FAPI_RC_BAD_VALUE,
141*758e9fbaSOystein Eftevaag                      "NULL-pointer passed for appData, though appDataSize != 0.");
142*758e9fbaSOystein Eftevaag     }
143*758e9fbaSOystein Eftevaag 
144*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
145*758e9fbaSOystein Eftevaag     IFAPI_Path_SetDescription * command = &context->cmd.path_set_info;
146*758e9fbaSOystein Eftevaag 
147*758e9fbaSOystein Eftevaag     /* Copy parameters to context for use during _Finish. */
148*758e9fbaSOystein Eftevaag     strdup_check(command->object_path, path, r, error_cleanup);
149*758e9fbaSOystein Eftevaag 
150*758e9fbaSOystein Eftevaag     if (appDataSize > 0) {
151*758e9fbaSOystein Eftevaag         command->appData.buffer = malloc(appDataSize);
152*758e9fbaSOystein Eftevaag         goto_if_null2(command->appData.buffer, "Out of memory.",
153*758e9fbaSOystein Eftevaag                       r, TSS2_FAPI_RC_MEMORY, error_cleanup);
154*758e9fbaSOystein Eftevaag 
155*758e9fbaSOystein Eftevaag         memcpy(&command->appData.buffer[0], appData, appDataSize);
156*758e9fbaSOystein Eftevaag     } else {
157*758e9fbaSOystein Eftevaag         command->appData.buffer = NULL;
158*758e9fbaSOystein Eftevaag     }
159*758e9fbaSOystein Eftevaag     command->appData.size = appDataSize;
160*758e9fbaSOystein Eftevaag 
161*758e9fbaSOystein Eftevaag     /* Load the current metadata for the object from keystore. */
162*758e9fbaSOystein Eftevaag     r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
163*758e9fbaSOystein Eftevaag     return_if_error2(r, "Could not open: %s", path);
164*758e9fbaSOystein Eftevaag 
165*758e9fbaSOystein Eftevaag     /* Initialize the context state for this operation. */
166*758e9fbaSOystein Eftevaag     context->state = APP_DATA_SET_READ;
167*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
168*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
169*758e9fbaSOystein Eftevaag 
170*758e9fbaSOystein Eftevaag error_cleanup:
171*758e9fbaSOystein Eftevaag     /* Cleanup duplicated input parameters that were copied before. */
172*758e9fbaSOystein Eftevaag     SAFE_FREE(command->object_path);
173*758e9fbaSOystein Eftevaag     SAFE_FREE(command->appData.buffer);
174*758e9fbaSOystein Eftevaag     return r;
175*758e9fbaSOystein Eftevaag }
176*758e9fbaSOystein Eftevaag 
177*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_SetAppData
178*758e9fbaSOystein Eftevaag  *
179*758e9fbaSOystein Eftevaag  * This function should be called after a previous Fapi_SetAppData_Async.
180*758e9fbaSOystein Eftevaag  *
181*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
182*758e9fbaSOystein Eftevaag  *
183*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
184*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context is NULL.
185*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
186*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
187*758e9fbaSOystein Eftevaag  *         operation already pending.
188*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
189*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
190*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
191*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
192*758e9fbaSOystein Eftevaag  *         complete. Call this function again later.
193*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH if the used path in inappropriate-
194*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
195*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
196*758e9fbaSOystein Eftevaag  *         the function.
197*758e9fbaSOystein Eftevaag  */
198*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_SetAppData_Finish(FAPI_CONTEXT * context)199*758e9fbaSOystein Eftevaag Fapi_SetAppData_Finish(
200*758e9fbaSOystein Eftevaag     FAPI_CONTEXT *context)
201*758e9fbaSOystein Eftevaag {
202*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
203*758e9fbaSOystein Eftevaag 
204*758e9fbaSOystein Eftevaag     TSS2_RC r;
205*758e9fbaSOystein Eftevaag 
206*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
207*758e9fbaSOystein Eftevaag     check_not_null(context);
208*758e9fbaSOystein Eftevaag 
209*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
210*758e9fbaSOystein Eftevaag     IFAPI_Path_SetDescription * command = &context->cmd.path_set_info;
211*758e9fbaSOystein Eftevaag     IFAPI_OBJECT *object = &command->object;
212*758e9fbaSOystein Eftevaag     UINT8_ARY *objAppData;
213*758e9fbaSOystein Eftevaag 
214*758e9fbaSOystein Eftevaag     switch (context->state) {
215*758e9fbaSOystein Eftevaag         statecase(context->state, APP_DATA_SET_READ);
216*758e9fbaSOystein Eftevaag             r = ifapi_keystore_load_finish(&context->keystore, &context->io, object);
217*758e9fbaSOystein Eftevaag             return_try_again(r);
218*758e9fbaSOystein Eftevaag             return_if_error_reset_state(r, "read_finish failed");
219*758e9fbaSOystein Eftevaag 
220*758e9fbaSOystein Eftevaag             /* Depending on the object type get the correct appData pointer. */
221*758e9fbaSOystein Eftevaag             switch (object->objectType) {
222*758e9fbaSOystein Eftevaag             case IFAPI_KEY_OBJ:
223*758e9fbaSOystein Eftevaag                 objAppData = &object->misc.key.appData;
224*758e9fbaSOystein Eftevaag                 break;
225*758e9fbaSOystein Eftevaag             case IFAPI_NV_OBJ:
226*758e9fbaSOystein Eftevaag                 objAppData = &object->misc.nv.appData;
227*758e9fbaSOystein Eftevaag                 break;
228*758e9fbaSOystein Eftevaag             default:
229*758e9fbaSOystein Eftevaag                 goto_error(r, TSS2_FAPI_RC_BAD_PATH, "Object has no app data.", error_cleanup);
230*758e9fbaSOystein Eftevaag             }
231*758e9fbaSOystein Eftevaag 
232*758e9fbaSOystein Eftevaag             /* If exists delete old appData */
233*758e9fbaSOystein Eftevaag             SAFE_FREE(objAppData->buffer);
234*758e9fbaSOystein Eftevaag 
235*758e9fbaSOystein Eftevaag             /* Set new appData for object */
236*758e9fbaSOystein Eftevaag             objAppData->size = command->appData.size;
237*758e9fbaSOystein Eftevaag             objAppData->buffer = command->appData.buffer;
238*758e9fbaSOystein Eftevaag 
239*758e9fbaSOystein Eftevaag             /* Prepare (over-)writing of object */
240*758e9fbaSOystein Eftevaag             r = ifapi_keystore_store_async(&context->keystore, &context->io,
241*758e9fbaSOystein Eftevaag                                            command->object_path, object);
242*758e9fbaSOystein Eftevaag             goto_if_error_reset_state(r, "Could not open: %sh", error_cleanup,
243*758e9fbaSOystein Eftevaag                                       command->object_path);
244*758e9fbaSOystein Eftevaag 
245*758e9fbaSOystein Eftevaag             fallthrough;
246*758e9fbaSOystein Eftevaag 
247*758e9fbaSOystein Eftevaag         statecase(context->state, APP_DATA_SET_WRITE);
248*758e9fbaSOystein Eftevaag             /* Finish writing of object */
249*758e9fbaSOystein Eftevaag             r = ifapi_keystore_store_finish(&context->keystore, &context->io);
250*758e9fbaSOystein Eftevaag             return_try_again(r);
251*758e9fbaSOystein Eftevaag             return_if_error_reset_state(r, "write_finish failed");
252*758e9fbaSOystein Eftevaag             ifapi_cleanup_ifapi_object(object);
253*758e9fbaSOystein Eftevaag 
254*758e9fbaSOystein Eftevaag             context->state = _FAPI_STATE_INIT;
255*758e9fbaSOystein Eftevaag             r = TSS2_RC_SUCCESS;
256*758e9fbaSOystein Eftevaag             break;
257*758e9fbaSOystein Eftevaag 
258*758e9fbaSOystein Eftevaag         statecasedefault(context->state);
259*758e9fbaSOystein Eftevaag     }
260*758e9fbaSOystein Eftevaag 
261*758e9fbaSOystein Eftevaag error_cleanup:
262*758e9fbaSOystein Eftevaag     /* Cleanup any intermediate results and state stored in the context. */
263*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(object);
264*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
265*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(context->loadKey.key_object);
266*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
267*758e9fbaSOystein Eftevaag     SAFE_FREE(command->object_path);
268*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
269*758e9fbaSOystein Eftevaag     return r;
270*758e9fbaSOystein Eftevaag }
271