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