xref: /aosp_15_r20/external/tpm2-tss/src/tss2-fapi/api/Fapi_GetDescription.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
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_GetDescription
26  *
27  * Returns the description of a previously stored object.
28  *
29  * @param[in,out] context The FAPI_CONTEXT
30  * @param[in] path The path of the object for which the description is loaded
31  * @param[out] description The description of the object
32  *
33  * @retval TSS2_RC_SUCCESS: if the function call was a success.
34  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, path or description is NULL.
35  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
36  * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
37  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
38  *         operation already pending.
39  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
40  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
41  *         internal operations or return parameters.
42  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
43  *         during authorization.
44  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
45  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
46  *         the function.
47  * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
48  *         this function needs to be called again.
49  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
50  */
51 TSS2_RC
Fapi_GetDescription(FAPI_CONTEXT * context,char const * path,char ** description)52 Fapi_GetDescription(
53     FAPI_CONTEXT *context,
54     char   const *path,
55     char        **description)
56 {
57     LOG_TRACE("called for context:%p", context);
58 
59     TSS2_RC r;
60 
61     /* Check for NULL parameters */
62     check_not_null(context);
63     check_not_null(path);
64     check_not_null(description);
65 
66     r = Fapi_GetDescription_Async(context, path);
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_GetDescription_Finish(context, description);
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_GetDescription
87  *
88  * Returns the description of a previously stored object.
89  *
90  * Call Fapi_GetDescription_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 for which the description is loaded
94  *
95  * @retval TSS2_RC_SUCCESS: if the function call was a success.
96  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
97  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
98  * @retval TSS2_FAPI_RC_BAD_PATH: if path does not map to a FAPI entity.
99  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
100  *         operation already pending.
101  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
102  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
103  *         internal operations or return parameters.
104  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
105  *         during authorization.
106  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
107  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
108  *         the function.
109  */
110 TSS2_RC
Fapi_GetDescription_Async(FAPI_CONTEXT * context,char const * path)111 Fapi_GetDescription_Async(
112     FAPI_CONTEXT *context,
113     char   const *path)
114 {
115     LOG_TRACE("called for context:%p", context);
116     LOG_TRACE("path: %s", path);
117 
118     TSS2_RC r;
119 
120     /* Check for NULL parameters */
121     check_not_null(context);
122     check_not_null(path);
123 
124     /* Load the object metadata from keystore. */
125     r = ifapi_keystore_load_async(&context->keystore, &context->io, path);
126     return_if_error2(r, "Could not open: %s", path);
127 
128     /* Initialize the context state for this operation. */
129     context->state = PATH_GET_DESCRIPTION_READ;
130 
131     LOG_TRACE("finished");
132     return TSS2_RC_SUCCESS;
133 }
134 
135 /** Asynchronous finish function for Fapi_GetDescription
136  *
137  * This function should be called after a previous Fapi_GetDescription_Async.
138  *
139  * @param[in,out] context The FAPI_CONTEXT
140  * @param[out] description The description of the object
141  *
142  * @retval TSS2_RC_SUCCESS: if the function call was a success.
143  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or description is NULL.
144  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
145  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
146  *         operation already pending.
147  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
148  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
149  *         internal operations or return parameters.
150  * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
151  *         complete. Call this function again later.
152  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
153  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
154  *         the function.
155  */
156 TSS2_RC
Fapi_GetDescription_Finish(FAPI_CONTEXT * context,char ** description)157 Fapi_GetDescription_Finish(
158     FAPI_CONTEXT *context,
159     char        **description)
160 {
161     LOG_TRACE("called for context:%p", context);
162 
163     TSS2_RC r;
164     IFAPI_OBJECT object;
165 
166     /* Check for NULL parameters */
167     check_not_null(context);
168     check_not_null(description);
169 
170     switch (context->state) {
171         statecase(context->state, PATH_GET_DESCRIPTION_READ);
172             r = ifapi_keystore_load_finish(&context->keystore, &context->io, &object);
173             return_try_again(r);
174             return_if_error_reset_state(r, "read_finish failed");
175 
176             /* Retrieve the description from the metadata object. */
177             r = ifapi_get_description(&object, description);
178             ifapi_cleanup_ifapi_object(&object);
179             return_if_error_reset_state(r, "Get description");
180 
181             context->state = _FAPI_STATE_INIT;
182             r = TSS2_RC_SUCCESS;
183             break;
184 
185         statecasedefault(context->state);
186     }
187     LOG_TRACE("finished");
188     /* Cleanup any intermediate results and state stored in the context. */
189     ifapi_cleanup_ifapi_object(&object);
190     ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
191     ifapi_cleanup_ifapi_object(context->loadKey.key_object);
192     ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
193     return r;
194 }
195