xref: /aosp_15_r20/external/tpm2-tss/src/tss2-fapi/api/Fapi_Sign.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 #include "fapi_crypto.h"
26*758e9fbaSOystein Eftevaag 
27*758e9fbaSOystein Eftevaag /** One-Call function for Fapi_Sign
28*758e9fbaSOystein Eftevaag  *
29*758e9fbaSOystein Eftevaag  * Uses a key, identified by its path, to sign a digest and puts the result in a
30*758e9fbaSOystein Eftevaag  * TPM2B bytestream.
31*758e9fbaSOystein Eftevaag  *
32*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
33*758e9fbaSOystein Eftevaag  * @param[in] keyPath The path of the signature key
34*758e9fbaSOystein Eftevaag  * @param[in] padding A padding algorithm. Must be either "RSA_SSA" or
35*758e9fbaSOystein Eftevaag  *            "RSA_PSS" or NULL
36*758e9fbaSOystein Eftevaag  * @param[in] digest The digest to sign. Must be already hashed
37*758e9fbaSOystein Eftevaag  * @param[in] digestSize The size of the digest in bytes
38*758e9fbaSOystein Eftevaag  * @param[out] signature The signature
39*758e9fbaSOystein Eftevaag  * @param[out] signatureSize The size of signature in bytes. May be NULL
40*758e9fbaSOystein Eftevaag  * @param[out] publicKey The public key that can be used to verify signature
41*758e9fbaSOystein Eftevaag  *            in PEM format. May be NULL
42*758e9fbaSOystein Eftevaag  * @param[out] certificate The certificate associated with the signing key in PEM
43*758e9fbaSOystein Eftevaag  *            format. May be NULL
44*758e9fbaSOystein Eftevaag  *
45*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
46*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, keyPath, digest or signature
47*758e9fbaSOystein Eftevaag  *         is NULL.
48*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
49*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if keyPath does not map to a FAPI key.
50*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_KEY: if the object at keyPath is not a key, or is a
51*758e9fbaSOystein Eftevaag  *         key that is unsuitable for the requested operation.
52*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE: if the digestSize is zero.
53*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
54*758e9fbaSOystein Eftevaag  *         operation already pending.
55*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
56*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
57*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
58*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
59*758e9fbaSOystein Eftevaag  *         config file.
60*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN if an I/O operation is not finished yet and
61*758e9fbaSOystein Eftevaag  *         this function needs to be called again.
62*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
63*758e9fbaSOystein Eftevaag  *         during authorization.
64*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
65*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
66*758e9fbaSOystein Eftevaag  *         is not set.
67*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
68*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
69*758e9fbaSOystein Eftevaag  *         was not successful.
70*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
71*758e9fbaSOystein Eftevaag  */
72*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Sign(FAPI_CONTEXT * context,char const * keyPath,char const * padding,uint8_t const * digest,size_t digestSize,uint8_t ** signature,size_t * signatureSize,char ** publicKey,char ** certificate)73*758e9fbaSOystein Eftevaag Fapi_Sign(
74*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context,
75*758e9fbaSOystein Eftevaag     char    const *keyPath,
76*758e9fbaSOystein Eftevaag     char    const *padding,
77*758e9fbaSOystein Eftevaag     uint8_t const *digest,
78*758e9fbaSOystein Eftevaag     size_t         digestSize,
79*758e9fbaSOystein Eftevaag     uint8_t      **signature,
80*758e9fbaSOystein Eftevaag     size_t        *signatureSize,
81*758e9fbaSOystein Eftevaag     char         **publicKey,
82*758e9fbaSOystein Eftevaag     char         **certificate)
83*758e9fbaSOystein Eftevaag {
84*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
85*758e9fbaSOystein Eftevaag 
86*758e9fbaSOystein Eftevaag     TSS2_RC r, r2;
87*758e9fbaSOystein Eftevaag 
88*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
89*758e9fbaSOystein Eftevaag     check_not_null(context);
90*758e9fbaSOystein Eftevaag     check_not_null(keyPath);
91*758e9fbaSOystein Eftevaag     check_not_null(digest);
92*758e9fbaSOystein Eftevaag     check_not_null(signature);
93*758e9fbaSOystein Eftevaag 
94*758e9fbaSOystein Eftevaag     /* Check whether TCTI and ESYS are initialized */
95*758e9fbaSOystein Eftevaag     return_if_null(context->esys, "Command can't be executed in none TPM mode.",
96*758e9fbaSOystein Eftevaag                    TSS2_FAPI_RC_NO_TPM);
97*758e9fbaSOystein Eftevaag 
98*758e9fbaSOystein Eftevaag     /* If the async state automata of FAPI shall be tested, then we must not set
99*758e9fbaSOystein Eftevaag        the timeouts of ESYS to blocking mode.
100*758e9fbaSOystein Eftevaag        During testing, the mssim tcti will ensure multiple re-invocations.
101*758e9fbaSOystein Eftevaag        Usually however the synchronous invocations of FAPI shall instruct ESYS
102*758e9fbaSOystein Eftevaag        to block until a result is available. */
103*758e9fbaSOystein Eftevaag #ifndef TEST_FAPI_ASYNC
104*758e9fbaSOystein Eftevaag     r = Esys_SetTimeout(context->esys, TSS2_TCTI_TIMEOUT_BLOCK);
105*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Set Timeout to blocking");
106*758e9fbaSOystein Eftevaag #endif /* TEST_FAPI_ASYNC */
107*758e9fbaSOystein Eftevaag 
108*758e9fbaSOystein Eftevaag     r = Fapi_Sign_Async(context, keyPath, padding, digest, digestSize);
109*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Key_Sign");
110*758e9fbaSOystein Eftevaag 
111*758e9fbaSOystein Eftevaag     do {
112*758e9fbaSOystein Eftevaag         /* We wait for file I/O to be ready if the FAPI state automata
113*758e9fbaSOystein Eftevaag            are in a file I/O state. */
114*758e9fbaSOystein Eftevaag         r = ifapi_io_poll(&context->io);
115*758e9fbaSOystein Eftevaag         return_if_error(r, "Something went wrong with IO polling");
116*758e9fbaSOystein Eftevaag 
117*758e9fbaSOystein Eftevaag         /* Repeatedly call the finish function, until FAPI has transitioned
118*758e9fbaSOystein Eftevaag            through all execution stages / states of this invocation. */
119*758e9fbaSOystein Eftevaag         r = Fapi_Sign_Finish(context, signature, signatureSize, publicKey,
120*758e9fbaSOystein Eftevaag                               certificate);
121*758e9fbaSOystein Eftevaag     } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
122*758e9fbaSOystein Eftevaag 
123*758e9fbaSOystein Eftevaag     /* Reset the ESYS timeout to non-blocking, immediate response. */
124*758e9fbaSOystein Eftevaag     r2 = Esys_SetTimeout(context->esys, 0);
125*758e9fbaSOystein Eftevaag     return_if_error(r2, "Set Timeout to non-blocking");
126*758e9fbaSOystein Eftevaag 
127*758e9fbaSOystein Eftevaag     return_if_error_reset_state(r, "Key_Sign");
128*758e9fbaSOystein Eftevaag 
129*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
130*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
131*758e9fbaSOystein Eftevaag }
132*758e9fbaSOystein Eftevaag 
133*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_Sign
134*758e9fbaSOystein Eftevaag  *
135*758e9fbaSOystein Eftevaag  * Uses a key, identified by its path, to sign a digest and puts the result in a
136*758e9fbaSOystein Eftevaag  * TPM2B bytestream.
137*758e9fbaSOystein Eftevaag  *
138*758e9fbaSOystein Eftevaag  * Call Fapi_Sign_Finish to finish the execution of this command.
139*758e9fbaSOystein Eftevaag  *
140*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
141*758e9fbaSOystein Eftevaag  * @param[in] keyPath The path of the signature key
142*758e9fbaSOystein Eftevaag  * @param[in] padding A padding algorithm. Must be either "RSA_SSA" or
143*758e9fbaSOystein Eftevaag  *            "RSA_PSS" or NULL
144*758e9fbaSOystein Eftevaag  * @param[in] digest The digest to sign. Must be already hashed
145*758e9fbaSOystein Eftevaag  * @param[in] digestSize The size of the digest in bytes
146*758e9fbaSOystein Eftevaag  *
147*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
148*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, keyPath or digest
149*758e9fbaSOystein Eftevaag  *         is NULL.
150*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
151*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if keyPath does not map to a FAPI key.
152*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_KEY: if the object at keyPath is not a key, or is a
153*758e9fbaSOystein Eftevaag  *         key that is unsuitable for the requested operation.
154*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE: if the digestSize is zero.
155*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
156*758e9fbaSOystein Eftevaag  *         operation already pending.
157*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
158*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
159*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
160*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_NO_TPM if FAPI was initialized in no-TPM-mode via its
161*758e9fbaSOystein Eftevaag  *         config file.
162*758e9fbaSOystein Eftevaag  */
163*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Sign_Async(FAPI_CONTEXT * context,char const * keyPath,char const * padding,uint8_t const * digest,size_t digestSize)164*758e9fbaSOystein Eftevaag Fapi_Sign_Async(
165*758e9fbaSOystein Eftevaag     FAPI_CONTEXT  *context,
166*758e9fbaSOystein Eftevaag     char    const *keyPath,
167*758e9fbaSOystein Eftevaag     char    const *padding,
168*758e9fbaSOystein Eftevaag     uint8_t const *digest,
169*758e9fbaSOystein Eftevaag     size_t         digestSize)
170*758e9fbaSOystein Eftevaag {
171*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
172*758e9fbaSOystein Eftevaag     LOG_TRACE("keyPath: %s", keyPath);
173*758e9fbaSOystein Eftevaag     LOG_TRACE("padding: %s", padding);
174*758e9fbaSOystein Eftevaag     if (digest) {
175*758e9fbaSOystein Eftevaag         LOGBLOB_TRACE(digest, digestSize, "digest");
176*758e9fbaSOystein Eftevaag     } else {
177*758e9fbaSOystein Eftevaag         LOG_TRACE("digest: (null) digestSize: %zi", digestSize);
178*758e9fbaSOystein Eftevaag     }
179*758e9fbaSOystein Eftevaag 
180*758e9fbaSOystein Eftevaag     TSS2_RC r;
181*758e9fbaSOystein Eftevaag 
182*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
183*758e9fbaSOystein Eftevaag     check_not_null(context);
184*758e9fbaSOystein Eftevaag     check_not_null(keyPath);
185*758e9fbaSOystein Eftevaag     check_not_null(digest);
186*758e9fbaSOystein Eftevaag 
187*758e9fbaSOystein Eftevaag     /* Check for invalid parameters */
188*758e9fbaSOystein Eftevaag     if (padding) {
189*758e9fbaSOystein Eftevaag         if (strcasecmp("RSA_SSA", padding) != 0 &&
190*758e9fbaSOystein Eftevaag                 strcasecmp("RSA_PSS", padding) != 0) {
191*758e9fbaSOystein Eftevaag             return_error(TSS2_FAPI_RC_BAD_VALUE,
192*758e9fbaSOystein Eftevaag                     "Only padding RSA_SSA or RSA_PSS allowed.");
193*758e9fbaSOystein Eftevaag         }
194*758e9fbaSOystein Eftevaag     }
195*758e9fbaSOystein Eftevaag 
196*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
197*758e9fbaSOystein Eftevaag     IFAPI_Key_Sign * command = &context->Key_Sign;
198*758e9fbaSOystein Eftevaag 
199*758e9fbaSOystein Eftevaag     /* Reset all context-internal session state information. */
200*758e9fbaSOystein Eftevaag     r = ifapi_session_init(context);
201*758e9fbaSOystein Eftevaag     return_if_error(r, "Initialize Sign");
202*758e9fbaSOystein Eftevaag 
203*758e9fbaSOystein Eftevaag     /* Copy parameters to context for use during _Finish. */
204*758e9fbaSOystein Eftevaag     FAPI_COPY_DIGEST(&command->digest.buffer[0],
205*758e9fbaSOystein Eftevaag                      command->digest.size, digest, digestSize);
206*758e9fbaSOystein Eftevaag     strdup_check(command->keyPath, keyPath, r, error_cleanup);
207*758e9fbaSOystein Eftevaag     strdup_check(command->padding, padding, r, error_cleanup);
208*758e9fbaSOystein Eftevaag 
209*758e9fbaSOystein Eftevaag     /* Initialize the context state for this operation. */
210*758e9fbaSOystein Eftevaag     context->state = KEY_SIGN_WAIT_FOR_KEY;
211*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
212*758e9fbaSOystein Eftevaag     return TSS2_RC_SUCCESS;
213*758e9fbaSOystein Eftevaag 
214*758e9fbaSOystein Eftevaag error_cleanup:
215*758e9fbaSOystein Eftevaag     /* Cleanup duplicated input parameters that were copied before. */
216*758e9fbaSOystein Eftevaag     SAFE_FREE(command->keyPath);
217*758e9fbaSOystein Eftevaag     SAFE_FREE(command->padding);
218*758e9fbaSOystein Eftevaag     return r;
219*758e9fbaSOystein Eftevaag }
220*758e9fbaSOystein Eftevaag 
221*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_Sign
222*758e9fbaSOystein Eftevaag  *
223*758e9fbaSOystein Eftevaag  * This function should be called after a previous Fapi_Sign_Async.
224*758e9fbaSOystein Eftevaag  *
225*758e9fbaSOystein Eftevaag  * @param[in,out] context The FAPI_CONTEXT
226*758e9fbaSOystein Eftevaag  * @param[out] signature The signature
227*758e9fbaSOystein Eftevaag  * @param[out] signatureSize The size of signature in bytes. May be NULL
228*758e9fbaSOystein Eftevaag  * @param[out] publicKey The public key that can be used to verify signature
229*758e9fbaSOystein Eftevaag  *            in PEM format. May be NULL
230*758e9fbaSOystein Eftevaag  * @param[out] certificate The certificate associated with the signing key in PEM
231*758e9fbaSOystein Eftevaag  *            format. May be NULL
232*758e9fbaSOystein Eftevaag  *
233*758e9fbaSOystein Eftevaag  * @retval TSS2_RC_SUCCESS: if the function call was a success.
234*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context or signature is NULL.
235*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
236*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
237*758e9fbaSOystein Eftevaag  *         operation already pending.
238*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
239*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
240*758e9fbaSOystein Eftevaag  *         internal operations or return parameters.
241*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
242*758e9fbaSOystein Eftevaag  *         complete. Call this function again later.
243*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
244*758e9fbaSOystein Eftevaag  *         during authorization.
245*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_KEY_NOT_FOUND if a key was not found.
246*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
247*758e9fbaSOystein Eftevaag  *         the function.
248*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
249*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_UNKNOWN if a required authorization callback
250*758e9fbaSOystein Eftevaag  *         is not set.
251*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_AUTHORIZATION_FAILED if the authorization attempt fails.
252*758e9fbaSOystein Eftevaag  * @retval TSS2_FAPI_RC_POLICY_UNKNOWN if policy search for a certain policy digest
253*758e9fbaSOystein Eftevaag  *         was not successful.
254*758e9fbaSOystein Eftevaag  * @retval TSS2_ESYS_RC_* possible error codes of ESAPI.
255*758e9fbaSOystein Eftevaag  */
256*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_Sign_Finish(FAPI_CONTEXT * context,uint8_t ** signature,size_t * signatureSize,char ** publicKey,char ** certificate)257*758e9fbaSOystein Eftevaag Fapi_Sign_Finish(
258*758e9fbaSOystein Eftevaag     FAPI_CONTEXT *context,
259*758e9fbaSOystein Eftevaag     uint8_t     **signature,
260*758e9fbaSOystein Eftevaag     size_t       *signatureSize,
261*758e9fbaSOystein Eftevaag     char        **publicKey,
262*758e9fbaSOystein Eftevaag     char        **certificate)
263*758e9fbaSOystein Eftevaag {
264*758e9fbaSOystein Eftevaag     LOG_TRACE("called for context:%p", context);
265*758e9fbaSOystein Eftevaag 
266*758e9fbaSOystein Eftevaag     TSS2_RC r;
267*758e9fbaSOystein Eftevaag     size_t resultSignatureSize;
268*758e9fbaSOystein Eftevaag 
269*758e9fbaSOystein Eftevaag     /* Check for NULL parameters */
270*758e9fbaSOystein Eftevaag     check_not_null(context);
271*758e9fbaSOystein Eftevaag     check_not_null(signature);
272*758e9fbaSOystein Eftevaag 
273*758e9fbaSOystein Eftevaag     /* Helpful alias pointers */
274*758e9fbaSOystein Eftevaag     IFAPI_Key_Sign * command = &context->Key_Sign;
275*758e9fbaSOystein Eftevaag 
276*758e9fbaSOystein Eftevaag     switch (context->state) {
277*758e9fbaSOystein Eftevaag         statecase(context->state, KEY_SIGN_WAIT_FOR_KEY);
278*758e9fbaSOystein Eftevaag             /* Load the key used for signing with a helper. */
279*758e9fbaSOystein Eftevaag             r = ifapi_load_key(context, command->keyPath,
280*758e9fbaSOystein Eftevaag                                &command->key_object);
281*758e9fbaSOystein Eftevaag             return_try_again(r);
282*758e9fbaSOystein Eftevaag             goto_if_error(r, "Fapi load key.", error_cleanup);
283*758e9fbaSOystein Eftevaag 
284*758e9fbaSOystein Eftevaag             fallthrough;
285*758e9fbaSOystein Eftevaag 
286*758e9fbaSOystein Eftevaag         statecase(context->state, KEY_SIGN_WAIT_FOR_SIGN);
287*758e9fbaSOystein Eftevaag             /* Perform the signing operation using a helper. */
288*758e9fbaSOystein Eftevaag             r = ifapi_key_sign(context, command->key_object,
289*758e9fbaSOystein Eftevaag                     command->padding, &command->digest, &command->tpm_signature,
290*758e9fbaSOystein Eftevaag                     publicKey, certificate);
291*758e9fbaSOystein Eftevaag             return_try_again(r);
292*758e9fbaSOystein Eftevaag             goto_if_error(r, "Fapi sign.", error_cleanup);
293*758e9fbaSOystein Eftevaag 
294*758e9fbaSOystein Eftevaag             /* Convert the TPM datatype signature to something useful for the caller. */
295*758e9fbaSOystein Eftevaag             r = ifapi_tpm_to_fapi_signature(command->key_object,
296*758e9fbaSOystein Eftevaag                      command->tpm_signature, signature, &resultSignatureSize);
297*758e9fbaSOystein Eftevaag             goto_if_error(r, "Create FAPI signature.", error_cleanup);
298*758e9fbaSOystein Eftevaag 
299*758e9fbaSOystein Eftevaag             if (signatureSize)
300*758e9fbaSOystein Eftevaag                 *signatureSize = resultSignatureSize;
301*758e9fbaSOystein Eftevaag             fallthrough;
302*758e9fbaSOystein Eftevaag 
303*758e9fbaSOystein Eftevaag         statecase(context->state, KEY_SIGN_CLEANUP)
304*758e9fbaSOystein Eftevaag             /* Cleanup the session used for authorization. */
305*758e9fbaSOystein Eftevaag             r = ifapi_cleanup_session(context);
306*758e9fbaSOystein Eftevaag             try_again_or_error_goto(r, "Cleanup", error_cleanup);
307*758e9fbaSOystein Eftevaag 
308*758e9fbaSOystein Eftevaag             context->state = _FAPI_STATE_INIT;
309*758e9fbaSOystein Eftevaag             break;
310*758e9fbaSOystein Eftevaag 
311*758e9fbaSOystein Eftevaag         statecasedefault(context->state);
312*758e9fbaSOystein Eftevaag     }
313*758e9fbaSOystein Eftevaag 
314*758e9fbaSOystein Eftevaag error_cleanup:
315*758e9fbaSOystein Eftevaag     /* Cleanup any intermediate results and state stored in the context. */
316*758e9fbaSOystein Eftevaag     SAFE_FREE(command->tpm_signature);
317*758e9fbaSOystein Eftevaag     SAFE_FREE(command->keyPath);
318*758e9fbaSOystein Eftevaag     SAFE_FREE(command->padding);
319*758e9fbaSOystein Eftevaag     ifapi_session_clean(context);
320*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(command->key_object);
321*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
322*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(context->loadKey.key_object);
323*758e9fbaSOystein Eftevaag     ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
324*758e9fbaSOystein Eftevaag     LOG_TRACE("finished");
325*758e9fbaSOystein Eftevaag     return r;
326*758e9fbaSOystein Eftevaag }
327