• 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_SetDescription
26  *
27  * Associates a human readable description with an object in the metadata store.
28  *
29  * @param[in,out] context The FAPI_CONTEXT
30  * @param[in] path The path of the object in the metadata store
31  * @param[in] description The description that is associated with the object.
32  *            May be NULL
33  *
34  * @retval TSS2_RC_SUCCESS: if the function call was a success.
35  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
36  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
37  * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
38  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
39  *         operation already pending.
40  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
41  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
42  *         internal operations or return parameters.
43  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
44  *         the function.
45  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
46  *         during authorization.
47  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
48  * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
49  *         this function needs to be called again.
50  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
51  */
52 TSS2_RC
Fapi_SetDescription(FAPI_CONTEXT * context,char const * path,char const * description)53 Fapi_SetDescription(
54     FAPI_CONTEXT *context,
55     char   const *path,
56     char   const *description)
57 {
58     LOG_TRACE("called for context:%p", context);
59 
60     TSS2_RC r;
61 
62     /* Check for NULL parameters */
63     check_not_null(context);
64     check_not_null(path);
65 
66     r = Fapi_SetDescription_Async(context, path, description);
67     return_if_error_reset_state(r, "Path_SetDescription");
68 
69     do {
70         /* We wait for file I/O to be ready if the FAPI state automata
71            are in a file I/O state. */
72         r = ifapi_io_poll(&context->io);
73         return_if_error(r, "Something went wrong with IO polling");
74 
75         /* Repeatedly call the finish function, until FAPI has transitioned
76            through all execution stages / states of this invocation. */
77         r = Fapi_SetDescription_Finish(context);
78     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
79 
80     return_if_error_reset_state(r, "Path_SetDescription");
81 
82     LOG_TRACE("finished");
83     return TSS2_RC_SUCCESS;
84 }
85 
86 /** Asynchronous function for Fapi_SetDescription
87  *
88  * Associates a human readable description with an object in the metadata store.
89  *
90  * Call Fapi_SetDescription_Finish to finish the execution of this command.
91  *
92  * @param[in,out] context The FAPI_CONTEXT
93  * @param[in] path The path of the object in the metadata store
94  * @param[in] description The description that is associated with the object.
95  *            May be NULL
96  *
97  * @retval TSS2_RC_SUCCESS: if the function call was a success.
98  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
99  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
100  * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
101  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
102  *         operation already pending.
103  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
104  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
105  *         internal operations or return parameters.
106  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
107  *         the function.
108  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
109  *         during authorization.
110  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
111  */
112 TSS2_RC
Fapi_SetDescription_Async(FAPI_CONTEXT * context,char const * path,char const * description)113 Fapi_SetDescription_Async(
114     FAPI_CONTEXT *context,
115     char   const *path,
116     char   const *description)
117 {
118     LOG_TRACE("called for context:%p", context);
119     LOG_TRACE("path: %s", path);
120     LOG_TRACE("description: %s", description);
121 
122     TSS2_RC r;
123 
124     /* Check for NULL parameters */
125     check_not_null(context);
126     check_not_null(path);
127 
128     /* Check for invalid parameters */
129     if (description && strlen(description) + 1 > 1024) {
130         return_error(TSS2_FAPI_RC_BAD_VALUE,
131                      "Length of description > 1024");
132     }
133 
134     /* Helpful alias pointers */
135     IFAPI_Path_SetDescription * command = &context->cmd.path_set_info;
136 
137     /* Copy parameters to context for use during _Finish. */
138     strdup_check(command->object_path, path, r, error_cleanup);
139 
140     /* Load the object's current metadata from the keystore. */
141     r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
142     goto_if_error2(r, "Could not open: %s", error_cleanup, path);
143 
144     if (description == NULL) {
145         command->description = NULL;
146     } else {
147         strdup_check(command->description, description, r, error_cleanup);
148     }
149 
150 
151     /* Initialize the context state for this operation. */
152     context->state = PATH_SET_DESCRIPTION_READ;
153     LOG_TRACE("finished");
154     return TSS2_RC_SUCCESS;
155 
156 error_cleanup:
157     /* Cleanup duplicated input parameters that were copied before. */
158     SAFE_FREE(command->object_path);
159     SAFE_FREE(command->description);
160     return r;
161 }
162 
163 /** Asynchronous finish function for Fapi_SetDescription
164  *
165  * This function should be called after a previous Fapi_SetDescription_Async.
166  *
167  * @param[in,out] context The FAPI_CONTEXT
168  *
169  * @retval TSS2_RC_SUCCESS: if the function call was a success.
170  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context is NULL.
171  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
172  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
173  *         operation already pending.
174  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
175  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
176  *         internal operations or return parameters.
177  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND If no file is found after pathname expansion.
178  * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
179  *         complete. Call this function again later.
180  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
181  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
182  *         the function.
183  */
184 TSS2_RC
Fapi_SetDescription_Finish(FAPI_CONTEXT * context)185 Fapi_SetDescription_Finish(
186     FAPI_CONTEXT *context)
187 {
188     LOG_TRACE("called for context:%p", context);
189 
190     TSS2_RC r;
191 
192     /* Check for NULL parameters */
193     check_not_null(context);
194 
195     /* Helpful alias pointers */
196     IFAPI_Path_SetDescription * command = &context->cmd.path_set_info;
197     IFAPI_OBJECT *object = &command->object;
198 
199     switch (context->state) {
200         statecase(context->state, PATH_SET_DESCRIPTION_READ);
201             r = ifapi_keystore_load_finish(&context->keystore, &context->io, object);
202             return_try_again(r);
203             goto_if_error_reset_state(r, "read_finish failed", error_cleanup);
204 
205             /* Add new description to object and save object */
206             ifapi_set_description(object, command->description);
207 
208             /* Store the updated metadata back to the keystore. */
209             r = ifapi_keystore_store_async(&context->keystore, &context->io,
210                                            command->object_path, object);
211             goto_if_error_reset_state(r, "Could not open: %sh", error_cleanup,
212                                       command->object_path);
213 
214             fallthrough;
215 
216         statecase(context->state, PATH_SET_DESCRIPTION_WRITE);
217             r = ifapi_keystore_store_finish(&context->keystore, &context->io);
218             return_try_again(r);
219             return_if_error_reset_state(r, "write_finish failed");
220 
221             context->state = _FAPI_STATE_INIT;
222             r = TSS2_RC_SUCCESS;
223             break;
224 
225         statecasedefault(context->state);
226     }
227 
228 error_cleanup:
229     /* Cleanup any intermediate results and state stored in the context. */
230     ifapi_cleanup_ifapi_object(object);
231     ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
232     ifapi_cleanup_ifapi_object(context->loadKey.key_object);
233     ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
234     SAFE_FREE(command->object_path);
235     LOG_TRACE("finished");
236     return r;
237 }
238