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