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