summaryrefslogtreecommitdiffstats
path: root/engines
diff options
context:
space:
mode:
authorRichard Levitte <levitte@openssl.org>2020-11-30 10:44:34 +0100
committerRichard Levitte <levitte@openssl.org>2020-12-02 20:19:41 +0100
commit0a3b330cf09dd3746f4f9c5bb82d9bbcfff809c1 (patch)
tree614dea92e921b5b56142fa96e6cc704388b23df2 /engines
parentf91d003a0ef0c748a11ccdb19c7661a3f2df9ab0 (diff)
Add test to demonstrate the app's new engine key loading
This adds a bit of functionality in ossltest, so it can now be used to load PEM files. It takes the file name as key ID, but just to make sure faults aren't ignored, it requires all file names to be prefixed with 'ot:'. Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com> (Merged from https://github.com/openssl/openssl/pull/13570)
Diffstat (limited to 'engines')
-rw-r--r--engines/e_ossltest.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/engines/e_ossltest.c b/engines/e_ossltest.c
index df2a3e14e8..15a7d75f1e 100644
--- a/engines/e_ossltest.c
+++ b/engines/e_ossltest.c
@@ -37,9 +37,14 @@
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/crypto.h>
+#include <openssl/pem.h>
#include "e_ossltest_err.c"
+#ifdef _WIN32
+# define strncasecmp _strnicmp
+#endif
+
/* Engine Id and Name */
static const char *engine_ossltest_id = "ossltest";
static const char *engine_ossltest_name = "OpenSSL Test engine support";
@@ -317,6 +322,43 @@ static void destroy_ciphers(void)
_hidden_aes_128_cbc = NULL;
}
+/* Key loading */
+static EVP_PKEY *load_key(ENGINE *eng, const char *key_id, int pub,
+ UI_METHOD *ui_method, void *ui_data)
+{
+ BIO *in;
+ EVP_PKEY *key;
+
+ if (strncasecmp(key_id, "ot:", 3) != 0)
+ return NULL;
+ key_id += 3;
+
+ fprintf(stderr, "[ossltest]Loading %s key %s\n",
+ pub ? "Public" : "Private", key_id);
+ in = BIO_new_file(key_id, "r");
+ if (!in)
+ return NULL;
+ if (pub)
+ key = PEM_read_bio_PUBKEY(in, NULL, 0, NULL);
+ else
+ key = PEM_read_bio_PrivateKey(in, NULL, 0, NULL);
+ BIO_free(in);
+ return key;
+}
+
+static EVP_PKEY *ossltest_load_privkey(ENGINE *eng, const char *key_id,
+ UI_METHOD *ui_method, void *ui_data)
+{
+ return load_key(eng, key_id, 0, ui_method, ui_data);
+}
+
+static EVP_PKEY *ossltest_load_pubkey(ENGINE *eng, const char *key_id,
+ UI_METHOD *ui_method, void *ui_data)
+{
+ return load_key(eng, key_id, 1, ui_method, ui_data);
+}
+
+
static int bind_ossltest(ENGINE *e)
{
/* Ensure the ossltest error handling is set up */
@@ -328,6 +370,8 @@ static int bind_ossltest(ENGINE *e)
|| !ENGINE_set_ciphers(e, ossltest_ciphers)
|| !ENGINE_set_RAND(e, ossltest_rand_method())
|| !ENGINE_set_destroy_function(e, ossltest_destroy)
+ || !ENGINE_set_load_privkey_function(e, ossltest_load_privkey)
+ || !ENGINE_set_load_pubkey_function(e, ossltest_load_pubkey)
|| !ENGINE_set_init_function(e, ossltest_init)
|| !ENGINE_set_finish_function(e, ossltest_finish)) {
OSSLTESTerr(OSSLTEST_F_BIND_OSSLTEST, OSSLTEST_R_INIT_FAILED);