xref: /aosp_15_r20/external/tpm2-tss/src/tss2-fapi/api/Fapi_CreateKey.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 "fapi_policy.h"
21*758e9fbaSOystein Eftevaag #include "tss2_esys.h"
22*758e9fbaSOystein Eftevaag #define LOGMODULE fapi
23*758e9fbaSOystein Eftevaag #include "util/log.h"
24*758e9fbaSOystein Eftevaag #include "util/aux_util.h"
25*758e9fbaSOystein Eftevaag 
26*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_CreateKey
27*758e9fbaSOystein Eftevaag  *
28*758e9fbaSOystein Eftevaag  * Creates a key inside the TPM based on the Key type, using the supplied
29*758e9fbaSOystein Eftevaag  * policy and authValue. The key is then stored either in the FAPI metadata
30*758e9fbaSOystein Eftevaag  * store or the TPM.
31*758e9fbaSOystein Eftevaag  *
32*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
33*758e9fbaSOystein Eftevaag  * @param[in] path The path where the new key is stored
34*758e9fbaSOystein Eftevaag  * @param[in] type The type of the new key. May be NULL
35*758e9fbaSOystein Eftevaag  * @param[in] policyPath The path to the policy that is associated with the new
36*758e9fbaSOystein Eftevaag  *       key. May be NULL
37*758e9fbaSOystein Eftevaag  * @param[in] authValue The authorization value for the new key. May be NULL
38*758e9fbaSOystein Eftevaag  *
39*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
40*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
41*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
42*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH: if policyPath is non-NULL and does not map to
43*758e9fbaSOystein Eftevaag  *         a FAPI policy.
44*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if the parent key does not map to a FAPI
45*758e9fbaSOystein Eftevaag  *         key.
46*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_ALREADY_EXISTS: if a file already exists at path.
47*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE: if the keyType is non-NULL and invalid.
48*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
49*758e9fbaSOystein Eftevaag  *         operation already pending.
50*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
51*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
52*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
53*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
54*758e9fbaSOystein Eftevaag  *         config file.
55*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
56*758e9fbaSOystein Eftevaag  *         this function needs to be called again.
57*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
58*758e9fbaSOystein Eftevaag  *         is not set.
59*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
60*758e9fbaSOystein Eftevaag  */
61*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_CreateKey(FAPI_CONTEXT * context,char const * path,char const * type,char const * policyPath,char const * authValue)62*758e9fbaSOystein Eftevaag Fapi_CreateKey(
63*758e9fbaSOystein Eftevaag     FAPI_CONTEXT *context,
64*758e9fbaSOystein Eftevaag     char   const *path,
65*758e9fbaSOystein Eftevaag     char   const *type,
66*758e9fbaSOystein Eftevaag     char   const *policyPath,
67*758e9fbaSOystein Eftevaag     char   const *authValue)
68*758e9fbaSOystein Eftevaag {
69*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
70*758e9fbaSOystein Eftevaag 
71*758e9fbaSOystein Eftevaag     TSS2_RC r, r2;
72*758e9fbaSOystein Eftevaag 
73*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
74*758e9fbaSOystein Eftevaag     check_not_null(context);
75*758e9fbaSOystein Eftevaag     check_not_null(path);
76*758e9fbaSOystein Eftevaag 
77*758e9fbaSOystein Eftevaag     /* Check whether TCTI and ESYS are initialized */
78*758e9fbaSOystein Eftevaag     return_if_null(context->esys, "Command can't be executed in none TPM mode.",
79*758e9fbaSOystein Eftevaag                    TSS2_FAPI_RC_NO_TPM);
80*758e9fbaSOystein Eftevaag 
81*758e9fbaSOystein Eftevaag     /* If the async state automata of FAPI shall be tested, then we must not set
82*758e9fbaSOystein Eftevaag        the timeouts of ESYS to blocking mode.
83*758e9fbaSOystein Eftevaag        During testing, the mssim tcti will ensure multiple re-invocations.
84*758e9fbaSOystein Eftevaag        Usually however the synchronous invocations of FAPI shall instruct ESYS
85*758e9fbaSOystein Eftevaag        to block until a result is available. */
86*758e9fbaSOystein Eftevaag #ifndef TEST_FAPI_ASYNC
87*758e9fbaSOystein Eftevaag     r = Esys_SetTimeout(context->esys, TSS2_TCTI_TIMEOUT_BLOCK);
88*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Set Timeout to blocking");
89*758e9fbaSOystein Eftevaag #endif /* TEST_FAPI_ASYNC */
90*758e9fbaSOystein Eftevaag 
91*758e9fbaSOystein Eftevaag     r = Fapi_CreateKey_Async(context, path, type, policyPath, authValue);
92*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Key_Create");
93*758e9fbaSOystein Eftevaag 
94*758e9fbaSOystein Eftevaag     do {
95*758e9fbaSOystein Eftevaag         /* We wait for file I/O to be ready if the FAPI state automata
96*758e9fbaSOystein Eftevaag            are in a file I/O state. */
97*758e9fbaSOystein Eftevaag         r = ifapi_io_poll(&context->io);
98*758e9fbaSOystein Eftevaag         return_if_error(r, "Something went wrong with IO polling");
99*758e9fbaSOystein Eftevaag 
100*758e9fbaSOystein Eftevaag         /* Repeatedly call the finish function, until FAPI has transitioned
101*758e9fbaSOystein Eftevaag            through all execution stages / states of this invocation. */
102*758e9fbaSOystein Eftevaag         r = Fapi_CreateKey_Finish(context);
103*758e9fbaSOystein Eftevaag     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
104*758e9fbaSOystein Eftevaag 
105*758e9fbaSOystein Eftevaag     /* Reset the ESYS timeout to non-blocking, immediate response. */
106*758e9fbaSOystein Eftevaag     r2 = Esys_SetTimeout(context->esys, 0);
107*758e9fbaSOystein Eftevaag     return_if_error(r2, "Set Timeout to non-blocking");
108*758e9fbaSOystein Eftevaag 
109*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Key_Create");
110*758e9fbaSOystein Eftevaag 
111*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
112*758e9fbaSOystein Eftevaag }
113*758e9fbaSOystein Eftevaag 
114*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_CreateKey
115*758e9fbaSOystein Eftevaag  *
116*758e9fbaSOystein Eftevaag  * Creates a key inside the TPM based on the Key type, using the supplied
117*758e9fbaSOystein Eftevaag  * policy and authValue. The key is then stored either in the FAPI metadata
118*758e9fbaSOystein Eftevaag  * store or the TPM.
119*758e9fbaSOystein Eftevaag  *
120*758e9fbaSOystein Eftevaag  * Call Fapi_CreateKey_Finish to finish the execution of this command.
121*758e9fbaSOystein Eftevaag  *
122*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
123*758e9fbaSOystein Eftevaag  * @param[in] path The path where the new key is stored
124*758e9fbaSOystein Eftevaag  * @param[in] type The type of the new key. May be NULL
125*758e9fbaSOystein Eftevaag  * @param[in] policyPath The path to the policy that is associated with the new
126*758e9fbaSOystein Eftevaag  *            key. May be NULL
127*758e9fbaSOystein Eftevaag  * @param[in] authValue The authorization value for the new key. May be NULL
128*758e9fbaSOystein Eftevaag  *
129*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
130*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or path is NULL.
131*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
132*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_PATH: if policyPath is non-NULL and does not map to
133*758e9fbaSOystein Eftevaag  *         a FAPI policy.
134*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if the parent key does not map to a FAPI
135*758e9fbaSOystein Eftevaag  *         key.
136*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_ALREADY_EXISTS: if a file already exists at path.
137*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE: if the keyType is non-NULL and invalid.
138*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
139*758e9fbaSOystein Eftevaag  *         operation already pending.
140*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
141*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
142*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
143*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
144*758e9fbaSOystein Eftevaag  *         config file.
145*758e9fbaSOystein Eftevaag  */
146*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_CreateKey_Async(FAPI_CONTEXT * context,char const * path,char const * type,char const * policyPath,char const * authValue)147*758e9fbaSOystein Eftevaag Fapi_CreateKey_Async(
148*758e9fbaSOystein Eftevaag     FAPI_CONTEXT *context,
149*758e9fbaSOystein Eftevaag     char   const *path,
150*758e9fbaSOystein Eftevaag     char   const *type,
151*758e9fbaSOystein Eftevaag     char   const *policyPath,
152*758e9fbaSOystein Eftevaag     char   const *authValue)
153*758e9fbaSOystein Eftevaag {
154*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
155*758e9fbaSOystein Eftevaag     LOG_TRACE("path: %s", path);
156*758e9fbaSOystein Eftevaag     LOG_TRACE("type: %s", type);
157*758e9fbaSOystein Eftevaag     LOG_TRACE("policyPath: %s", policyPath);
158*758e9fbaSOystein Eftevaag     LOG_TRACE("authValue: %s", authValue);
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(path);
165*758e9fbaSOystein Eftevaag 
166*758e9fbaSOystein Eftevaag     /* Reset all context-internal session state information. */
167*758e9fbaSOystein Eftevaag     r = ifapi_session_init(context);
168*758e9fbaSOystein Eftevaag     return_if_error(r, "Initialize CreateKey");
169*758e9fbaSOystein Eftevaag 
170*758e9fbaSOystein Eftevaag     /* Prepare the key creation with the authValue.
171*758e9fbaSOystein Eftevaag        This will also copy the input information for use during the finish call. */
172*758e9fbaSOystein Eftevaag     r = ifapi_key_create_prepare_auth(context, path, policyPath, authValue);
173*758e9fbaSOystein Eftevaag     return_if_error(r, "Key create.");
174*758e9fbaSOystein Eftevaag 
175*758e9fbaSOystein Eftevaag     /* Set the flags of the key to be created. If no type is given the empty-string
176*758e9fbaSOystein Eftevaag        default type flags are set. If no policy is given, userWithAuth flag is set. */
177*758e9fbaSOystein Eftevaag     r = ifapi_set_key_flags(type ? type : "",
178*758e9fbaSOystein Eftevaag                             (policyPath && strcmp(policyPath, "") != 0) ? true : false,
179*758e9fbaSOystein Eftevaag                             &context->cmd.Key_Create.public_templ);
180*758e9fbaSOystein Eftevaag     return_if_error(r, "Set key flags for key");
181*758e9fbaSOystein Eftevaag 
182*758e9fbaSOystein Eftevaag     /* Initialize the context state for this operation. */
183*758e9fbaSOystein Eftevaag     context->state = KEY_CREATE;
184*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
185*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
186*758e9fbaSOystein Eftevaag }
187*758e9fbaSOystein Eftevaag 
188*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_CreateKey
189*758e9fbaSOystein Eftevaag  *
190*758e9fbaSOystein Eftevaag  * This function should be called after a previous Fapi_CreateKey_Async.
191*758e9fbaSOystein Eftevaag  *
192*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
193*758e9fbaSOystein Eftevaag  *
194*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
195*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context is NULL.
196*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
197*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
198*758e9fbaSOystein Eftevaag  *         operation already pending.
199*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
200*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
201*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
202*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
203*758e9fbaSOystein Eftevaag  *         complete. Call this function again later.
204*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
205*758e9fbaSOystein Eftevaag  *         the function.
206*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
207*758e9fbaSOystein Eftevaag  *         is not set.
208*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
209*758e9fbaSOystein Eftevaag  */
210*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_CreateKey_Finish(FAPI_CONTEXT * context)211*758e9fbaSOystein Eftevaag Fapi_CreateKey_Finish(
212*758e9fbaSOystein Eftevaag     FAPI_CONTEXT *context)
213*758e9fbaSOystein Eftevaag {
214*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
215*758e9fbaSOystein Eftevaag 
216*758e9fbaSOystein Eftevaag     TSS2_RC r;
217*758e9fbaSOystein Eftevaag 
218*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
219*758e9fbaSOystein Eftevaag     check_not_null(context);
220*758e9fbaSOystein Eftevaag 
221*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
222*758e9fbaSOystein Eftevaag     IFAPI_Key_Create * command = &context->cmd.Key_Create;
223*758e9fbaSOystein Eftevaag 
224*758e9fbaSOystein Eftevaag     switch (context->state) {
225*758e9fbaSOystein Eftevaag         statecase(context->state, KEY_CREATE);
226*758e9fbaSOystein Eftevaag             /* Finish the key creation inside the helper function. */
227*758e9fbaSOystein Eftevaag             r = ifapi_key_create(context, &command->public_templ);
228*758e9fbaSOystein Eftevaag             return_try_again(r);
229*758e9fbaSOystein Eftevaag             goto_if_error(r, "Key create", error_cleanup);
230*758e9fbaSOystein Eftevaag 
231*758e9fbaSOystein Eftevaag             /* Cleanup any intermediate results and state stored in the context. */
232*758e9fbaSOystein Eftevaag             ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
233*758e9fbaSOystein Eftevaag             ifapi_cleanup_ifapi_object(context->loadKey.key_object);
234*758e9fbaSOystein Eftevaag             ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
235*758e9fbaSOystein Eftevaag             context->state = _FAPI_STATE_INIT;
236*758e9fbaSOystein Eftevaag             LOG_TRACE("finished");
237*758e9fbaSOystein Eftevaag             return TSS2_RC_SUCCESS;
238*758e9fbaSOystein Eftevaag 
239*758e9fbaSOystein Eftevaag         statecasedefault(context->state);
240*758e9fbaSOystein Eftevaag     }
241*758e9fbaSOystein Eftevaag 
242*758e9fbaSOystein Eftevaag error_cleanup:
243*758e9fbaSOystein Eftevaag     /* Cleanup any intermediate results and state stored in the context. */
244*758e9fbaSOystein Eftevaag     context->cmd.Key_Create.state = KEY_CREATE_INIT;
245*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
246*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(context->loadKey.key_object);
247*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
248*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
249*758e9fbaSOystein Eftevaag     return r;
250*758e9fbaSOystein Eftevaag }
251