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_crypto.h"
19*758e9fbaSOystein Eftevaag #include "fapi_int.h"
20*758e9fbaSOystein Eftevaag #include "fapi_util.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_VerifySignature
27*758e9fbaSOystein Eftevaag *
28*758e9fbaSOystein Eftevaag * Verifies a signature using a public key found in a keyPath.
29*758e9fbaSOystein Eftevaag *
30*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
31*758e9fbaSOystein Eftevaag * @param[in] keyPath The path to the verification public key
32*758e9fbaSOystein Eftevaag * @param[in] digest The that was signed. Must be already hashed
33*758e9fbaSOystein Eftevaag * @param[in] digestSize the size of digest in bytes
34*758e9fbaSOystein Eftevaag * @param[in] signature The signature to be verified
35*758e9fbaSOystein Eftevaag * @param[in] signatureSize The size of signature in bytes
36*758e9fbaSOystein Eftevaag *
37*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
38*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, keyPath, signature, or
39*758e9fbaSOystein Eftevaag * digest is NULL.
40*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
41*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if keyPath does not map to a FAPI object.
42*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_KEY: if the object at publicKeyPath is not a key, or
43*758e9fbaSOystein Eftevaag * is a key that is unsuitable for the requested operation.
44*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE: if signature is invalid (has the wrong format)
45*758e9fbaSOystein Eftevaag * or if digestSize is zero.
46*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_SIGNATURE_VERIFICATION_FAILED: if the signature fails to
47*758e9fbaSOystein Eftevaag * verify.
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_PATH_NOT_FOUND if a FAPI object path was not found
54*758e9fbaSOystein Eftevaag * during authorization.
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_GENERAL_FAILURE if an internal error occurred.
58*758e9fbaSOystein Eftevaag */
59*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_VerifySignature(FAPI_CONTEXT * context,char const * keyPath,uint8_t const * digest,size_t digestSize,uint8_t const * signature,size_t signatureSize)60*758e9fbaSOystein Eftevaag Fapi_VerifySignature(
61*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
62*758e9fbaSOystein Eftevaag char const *keyPath,
63*758e9fbaSOystein Eftevaag uint8_t const *digest,
64*758e9fbaSOystein Eftevaag size_t digestSize,
65*758e9fbaSOystein Eftevaag uint8_t const *signature,
66*758e9fbaSOystein Eftevaag size_t signatureSize)
67*758e9fbaSOystein Eftevaag {
68*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
69*758e9fbaSOystein Eftevaag
70*758e9fbaSOystein Eftevaag TSS2_RC r;
71*758e9fbaSOystein Eftevaag
72*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
73*758e9fbaSOystein Eftevaag check_not_null(context);
74*758e9fbaSOystein Eftevaag check_not_null(keyPath);
75*758e9fbaSOystein Eftevaag check_not_null(digest);
76*758e9fbaSOystein Eftevaag check_not_null(signature);
77*758e9fbaSOystein Eftevaag
78*758e9fbaSOystein Eftevaag r = Fapi_VerifySignature_Async(context, keyPath, digest, digestSize,
79*758e9fbaSOystein Eftevaag signature, signatureSize);
80*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Key_VerifySignature");
81*758e9fbaSOystein Eftevaag
82*758e9fbaSOystein Eftevaag do {
83*758e9fbaSOystein Eftevaag /* We wait for file I/O to be ready if the FAPI state automata
84*758e9fbaSOystein Eftevaag are in a file I/O state. */
85*758e9fbaSOystein Eftevaag r = ifapi_io_poll(&context->io);
86*758e9fbaSOystein Eftevaag return_if_error(r, "Something went wrong with IO polling");
87*758e9fbaSOystein Eftevaag
88*758e9fbaSOystein Eftevaag /* Repeatedly call the finish function, until FAPI has transitioned
89*758e9fbaSOystein Eftevaag through all execution stages / states of this invocation. */
90*758e9fbaSOystein Eftevaag r = Fapi_VerifySignature_Finish(context);
91*758e9fbaSOystein Eftevaag } while ((r & ~TSS2_RC_LAYER_MASK) == TSS2_BASE_RC_TRY_AGAIN);
92*758e9fbaSOystein Eftevaag
93*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "Key_VerifySignature");
94*758e9fbaSOystein Eftevaag
95*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
96*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
97*758e9fbaSOystein Eftevaag }
98*758e9fbaSOystein Eftevaag
99*758e9fbaSOystein Eftevaag /** Asynchronous function for Fapi_VerifySignature
100*758e9fbaSOystein Eftevaag *
101*758e9fbaSOystein Eftevaag * Verifies a signature using a public key found in a keyPath.
102*758e9fbaSOystein Eftevaag *
103*758e9fbaSOystein Eftevaag * Call Fapi_VerifySignature_Finish to finish the execution of this command.
104*758e9fbaSOystein Eftevaag *
105*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
106*758e9fbaSOystein Eftevaag * @param[in] keyPath The path to the verification public key
107*758e9fbaSOystein Eftevaag * @param[in] digest The that was signed. Must be already hashed
108*758e9fbaSOystein Eftevaag * @param[in] digestSize the size of digest in bytes
109*758e9fbaSOystein Eftevaag * @param[in] signature The signature to be verified
110*758e9fbaSOystein Eftevaag * @param[in] signatureSize The size of signature in bytes
111*758e9fbaSOystein Eftevaag *
112*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
113*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context, keyPath, signature, or
114*758e9fbaSOystein Eftevaag * digest is NULL.
115*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
116*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_KEY_NOT_FOUND: if keyPath does not map to a FAPI object.
117*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_KEY: if the object at publicKeyPath is not a key, or
118*758e9fbaSOystein Eftevaag * is a key that is unsuitable for the requested operation.
119*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE: if signature is invalid (has the wrong format)
120*758e9fbaSOystein Eftevaag * or if digestSize is zero.
121*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_SIGNATURE_VERIFICATION_FAILED: if the signature fails to
122*758e9fbaSOystein Eftevaag * verify.
123*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
124*758e9fbaSOystein Eftevaag * operation already pending.
125*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
126*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
127*758e9fbaSOystein Eftevaag * internal operations or return parameters.
128*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_PATH_NOT_FOUND if a FAPI object path was not found
129*758e9fbaSOystein Eftevaag * during authorization.
130*758e9fbaSOystein Eftevaag */
131*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_VerifySignature_Async(FAPI_CONTEXT * context,char const * keyPath,uint8_t const * digest,size_t digestSize,uint8_t const * signature,size_t signatureSize)132*758e9fbaSOystein Eftevaag Fapi_VerifySignature_Async(
133*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context,
134*758e9fbaSOystein Eftevaag char const *keyPath,
135*758e9fbaSOystein Eftevaag uint8_t const *digest,
136*758e9fbaSOystein Eftevaag size_t digestSize,
137*758e9fbaSOystein Eftevaag uint8_t const *signature,
138*758e9fbaSOystein Eftevaag size_t signatureSize)
139*758e9fbaSOystein Eftevaag {
140*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
141*758e9fbaSOystein Eftevaag LOG_TRACE("keyPath: %s", keyPath);
142*758e9fbaSOystein Eftevaag if (digest) {
143*758e9fbaSOystein Eftevaag LOGBLOB_TRACE(digest, digestSize, "digest");
144*758e9fbaSOystein Eftevaag } else {
145*758e9fbaSOystein Eftevaag LOG_TRACE("digset: (null) digestSize: %zi", digestSize);
146*758e9fbaSOystein Eftevaag }
147*758e9fbaSOystein Eftevaag if (signature) {
148*758e9fbaSOystein Eftevaag LOGBLOB_TRACE(signature, signatureSize, "signature");
149*758e9fbaSOystein Eftevaag } else {
150*758e9fbaSOystein Eftevaag LOG_TRACE("signature: (null) sigantureSize: %zi", signatureSize);
151*758e9fbaSOystein Eftevaag }
152*758e9fbaSOystein Eftevaag
153*758e9fbaSOystein Eftevaag TSS2_RC r;
154*758e9fbaSOystein Eftevaag
155*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
156*758e9fbaSOystein Eftevaag check_not_null(context);
157*758e9fbaSOystein Eftevaag check_not_null(keyPath);
158*758e9fbaSOystein Eftevaag check_not_null(digest);
159*758e9fbaSOystein Eftevaag check_not_null(signature);
160*758e9fbaSOystein Eftevaag
161*758e9fbaSOystein Eftevaag /* Helpful alias pointers */
162*758e9fbaSOystein Eftevaag IFAPI_Key_VerifySignature * command = &context->cmd.Key_VerifySignature;
163*758e9fbaSOystein Eftevaag
164*758e9fbaSOystein Eftevaag r = ifapi_non_tpm_mode_init(context);
165*758e9fbaSOystein Eftevaag return_if_error(r, "Initialize VerifySignature");
166*758e9fbaSOystein Eftevaag
167*758e9fbaSOystein Eftevaag /* Copy parameters to context for use during _Finish. */
168*758e9fbaSOystein Eftevaag uint8_t * signatureBuffer = malloc(signatureSize);
169*758e9fbaSOystein Eftevaag uint8_t * digestBuffer = malloc(digestSize);
170*758e9fbaSOystein Eftevaag goto_if_null2(signatureBuffer, "Out of memory", r, TSS2_FAPI_RC_MEMORY,
171*758e9fbaSOystein Eftevaag error_cleanup);
172*758e9fbaSOystein Eftevaag goto_if_null2(digestBuffer, "Out of memory", r, TSS2_FAPI_RC_MEMORY,
173*758e9fbaSOystein Eftevaag error_cleanup);
174*758e9fbaSOystein Eftevaag memcpy(signatureBuffer, signature, signatureSize);
175*758e9fbaSOystein Eftevaag memcpy(digestBuffer, digest, digestSize);
176*758e9fbaSOystein Eftevaag command->signature = signatureBuffer;
177*758e9fbaSOystein Eftevaag command->digest = digestBuffer;
178*758e9fbaSOystein Eftevaag command->signatureSize = signatureSize;
179*758e9fbaSOystein Eftevaag command->digestSize = digestSize;
180*758e9fbaSOystein Eftevaag memset(&command->key_object, 0, sizeof(IFAPI_OBJECT));
181*758e9fbaSOystein Eftevaag
182*758e9fbaSOystein Eftevaag /* Load the key for verification from the keystore. */
183*758e9fbaSOystein Eftevaag r = ifapi_keystore_load_async(&context->keystore, &context->io, keyPath);
184*758e9fbaSOystein Eftevaag goto_if_error2(r, "Could not open: %s", error_cleanup, keyPath);
185*758e9fbaSOystein Eftevaag
186*758e9fbaSOystein Eftevaag /* Initialize the context state for this operation. */
187*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
188*758e9fbaSOystein Eftevaag return TSS2_RC_SUCCESS;
189*758e9fbaSOystein Eftevaag
190*758e9fbaSOystein Eftevaag error_cleanup:
191*758e9fbaSOystein Eftevaag /* Cleanup duplicated input parameters that were copied before. */
192*758e9fbaSOystein Eftevaag SAFE_FREE(signatureBuffer);
193*758e9fbaSOystein Eftevaag command->signature = NULL;
194*758e9fbaSOystein Eftevaag SAFE_FREE(digestBuffer);
195*758e9fbaSOystein Eftevaag command->digest = NULL;
196*758e9fbaSOystein Eftevaag return r;
197*758e9fbaSOystein Eftevaag }
198*758e9fbaSOystein Eftevaag
199*758e9fbaSOystein Eftevaag /** Asynchronous finish function for Fapi_VerifySignature
200*758e9fbaSOystein Eftevaag *
201*758e9fbaSOystein Eftevaag * This function should be called after a previous Fapi_VerifySignature_Async.
202*758e9fbaSOystein Eftevaag *
203*758e9fbaSOystein Eftevaag * @param[in,out] context The FAPI_CONTEXT
204*758e9fbaSOystein Eftevaag *
205*758e9fbaSOystein Eftevaag * @retval TSS2_RC_SUCCESS: if the function call was a success.
206*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_REFERENCE: if context is NULL.
207*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_CONTEXT: if context corruption is detected.
208*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_SEQUENCE: if the context has an asynchronous
209*758e9fbaSOystein Eftevaag * operation already pending.
210*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_IO_ERROR: if the data cannot be saved.
211*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_MEMORY: if the FAPI cannot allocate enough memory for
212*758e9fbaSOystein Eftevaag * internal operations or return parameters.
213*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_TRY_AGAIN: if the asynchronous operation is not yet
214*758e9fbaSOystein Eftevaag * complete. Call this function again later.
215*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_GENERAL_FAILURE if an internal error occurred.
216*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_BAD_VALUE if an invalid value was passed into
217*758e9fbaSOystein Eftevaag * the function.
218*758e9fbaSOystein Eftevaag * @retval TSS2_FAPI_RC_SIGNATURE_VERIFICATION_FAILED if the signature could not
219*758e9fbaSOystein Eftevaag * be verified
220*758e9fbaSOystein Eftevaag */
221*758e9fbaSOystein Eftevaag TSS2_RC
Fapi_VerifySignature_Finish(FAPI_CONTEXT * context)222*758e9fbaSOystein Eftevaag Fapi_VerifySignature_Finish(
223*758e9fbaSOystein Eftevaag FAPI_CONTEXT *context)
224*758e9fbaSOystein Eftevaag {
225*758e9fbaSOystein Eftevaag LOG_TRACE("called for context:%p", context);
226*758e9fbaSOystein Eftevaag
227*758e9fbaSOystein Eftevaag TSS2_RC r;
228*758e9fbaSOystein Eftevaag
229*758e9fbaSOystein Eftevaag /* Check for NULL parameters */
230*758e9fbaSOystein Eftevaag check_not_null(context);
231*758e9fbaSOystein Eftevaag
232*758e9fbaSOystein Eftevaag /* Helpful alias pointers */
233*758e9fbaSOystein Eftevaag IFAPI_Key_VerifySignature * command = &context->cmd.Key_VerifySignature;
234*758e9fbaSOystein Eftevaag
235*758e9fbaSOystein Eftevaag r = ifapi_keystore_load_finish(&context->keystore, &context->io,
236*758e9fbaSOystein Eftevaag &command->key_object);
237*758e9fbaSOystein Eftevaag return_try_again(r);
238*758e9fbaSOystein Eftevaag return_if_error_reset_state(r, "read_finish failed");
239*758e9fbaSOystein Eftevaag
240*758e9fbaSOystein Eftevaag /* Verify the signature using a helper that tests all known signature schemes. */
241*758e9fbaSOystein Eftevaag r = ifapi_verify_signature(&command->key_object, command->signature,
242*758e9fbaSOystein Eftevaag command->signatureSize, command->digest, command->digestSize);
243*758e9fbaSOystein Eftevaag goto_if_error(r, "Verify signature.", cleanup);
244*758e9fbaSOystein Eftevaag
245*758e9fbaSOystein Eftevaag cleanup:
246*758e9fbaSOystein Eftevaag /* Cleanup any intermediate results and state stored in the context. */
247*758e9fbaSOystein Eftevaag if (command->key_object.objectType)
248*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&command->key_object);
249*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->loadKey.auth_object);
250*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(context->loadKey.key_object);
251*758e9fbaSOystein Eftevaag ifapi_cleanup_ifapi_object(&context->createPrimary.pkey_object);
252*758e9fbaSOystein Eftevaag SAFE_FREE(command->signature);
253*758e9fbaSOystein Eftevaag SAFE_FREE(command->digest);
254*758e9fbaSOystein Eftevaag LOG_TRACE("finished");
255*758e9fbaSOystein Eftevaag return r;
256*758e9fbaSOystein Eftevaag }
257