From 531d630b5cfe0c50de122f0387a65473b4746bf8 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Mon, 18 Jun 2001 06:22:33 +0000 Subject: [PATCH] Provide an application-common setup function for engines and use it everywhere. --- apps/apps.c | 29 +++++++++++++++++++++++++++++ apps/apps.h | 1 + apps/ca.c | 18 +----------------- apps/dgst.c | 18 +----------------- apps/dh.c | 18 +----------------- apps/dhparam.c | 18 +----------------- apps/dsa.c | 18 +----------------- apps/dsaparam.c | 18 +----------------- apps/enc.c | 18 +----------------- apps/gendh.c | 18 +----------------- apps/gendsa.c | 18 +----------------- apps/genrsa.c | 18 +----------------- apps/pkcs12.c | 14 +------------- apps/pkcs7.c | 18 +----------------- apps/pkcs8.c | 18 +----------------- apps/rand.c | 18 +----------------- apps/req.c | 18 +----------------- apps/rsa.c | 18 +----------------- apps/rsautl.c | 18 +----------------- apps/s_client.c | 23 +---------------------- apps/s_server.c | 23 +---------------------- apps/smime.c | 18 +----------------- apps/speed.c | 17 ++--------------- apps/spkac.c | 18 +----------------- apps/verify.c | 18 +----------------- apps/x509.c | 18 +----------------- 26 files changed, 55 insertions(+), 412 deletions(-) diff --git a/apps/apps.c b/apps/apps.c index 659a3ad7f..dd19a4cc3 100644 --- a/apps/apps.c +++ b/apps/apps.c @@ -1037,3 +1037,32 @@ X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath) X509_STORE_free(store); return NULL; } + +ENGINE *setup_engine(BIO *err, const char *engine, int debug) + { + ENGINE *e = NULL; + + if (engine) + { + if((e = ENGINE_by_id(engine)) == NULL) + { + BIO_printf(err,"invalid engine \"%s\"\n", engine); + return NULL; + } + if (debug) + { + ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, + 0, err, 0); + } + ENGINE_ctrl_cmd(e, "SET_USER_INTERFACE", 0, UI_OpenSSL(), 0, 1); + if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) + { + BIO_printf(err,"can't use that engine\n"); + return NULL; + } + BIO_printf(err,"engine \"%s\" set.\n", engine); + /* Free our "structural" reference. */ + ENGINE_free(e); + } + return e; + } diff --git a/apps/apps.h b/apps/apps.h index a2b72f087..7fbd41769 100644 --- a/apps/apps.h +++ b/apps/apps.h @@ -176,6 +176,7 @@ EVP_PKEY *load_pubkey(BIO *err, const char *file, int format, STACK_OF(X509) *load_certs(BIO *err, const char *file, int format, const char *pass, ENGINE *e, const char *cert_descrip); X509_STORE *setup_verify(BIO *bp, char *CAfile, char *CApath); +ENGINE *setup_engine(BIO *err, const char *engine, int debug); #define FORMAT_UNDEF 0 #define FORMAT_ASN1 1 diff --git a/apps/ca.c b/apps/ca.c index 921e1f184..4c67b121b 100644 --- a/apps/ca.c +++ b/apps/ca.c @@ -549,23 +549,7 @@ bad: ERR_load_crypto_strings(); - if (engine != NULL) - { - if ((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto err; - } - if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto err; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); /*****************************************************************/ if (configfile == NULL) configfile = getenv("OPENSSL_CONF"); diff --git a/apps/dgst.c b/apps/dgst.c index a010ba071..1fbef6bed 100644 --- a/apps/dgst.c +++ b/apps/dgst.c @@ -225,23 +225,7 @@ int MAIN(int argc, char **argv) goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); in=BIO_new(BIO_s_file()); bmd=BIO_new(BIO_f_md()); diff --git a/apps/dh.c b/apps/dh.c index 6162e442b..20581f664 100644 --- a/apps/dh.c +++ b/apps/dh.c @@ -174,23 +174,7 @@ bad: ERR_load_crypto_strings(); - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); in=BIO_new(BIO_s_file()); out=BIO_new(BIO_s_file()); diff --git a/apps/dhparam.c b/apps/dhparam.c index bbc64d5eb..51aee97d2 100644 --- a/apps/dhparam.c +++ b/apps/dhparam.c @@ -257,23 +257,7 @@ bad: ERR_load_crypto_strings(); - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if (g && !num) num = DEFBITS; diff --git a/apps/dsa.c b/apps/dsa.c index 6e9e6c756..4f45edac6 100644 --- a/apps/dsa.c +++ b/apps/dsa.c @@ -198,23 +198,7 @@ bad: ERR_load_crypto_strings(); - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = ENGINE_setup(engine, bio_err); if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { BIO_printf(bio_err, "Error getting passwords\n"); diff --git a/apps/dsaparam.c b/apps/dsaparam.c index e923f10db..7ce49d90c 100644 --- a/apps/dsaparam.c +++ b/apps/dsaparam.c @@ -232,23 +232,7 @@ bad: } } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = ENGINE_setup(engine, bio_err); if (need_rand) { diff --git a/apps/enc.c b/apps/enc.c index 332c2ba9d..fd25a2122 100644 --- a/apps/enc.c +++ b/apps/enc.c @@ -285,23 +285,7 @@ bad: argv++; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if (bufsize != NULL) { diff --git a/apps/gendh.c b/apps/gendh.c index 0b47867c2..14928d84a 100644 --- a/apps/gendh.c +++ b/apps/gendh.c @@ -143,23 +143,7 @@ bad: goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); out=BIO_new(BIO_s_file()); if (out == NULL) diff --git a/apps/gendsa.c b/apps/gendsa.c index 1166be441..3fdbf3772 100644 --- a/apps/gendsa.c +++ b/apps/gendsa.c @@ -162,23 +162,7 @@ bad: goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = ENGINE_setup(engine, bio_err); if(!app_passwd(bio_err, NULL, passargout, NULL, &passout)) { BIO_printf(bio_err, "Error getting password\n"); diff --git a/apps/genrsa.c b/apps/genrsa.c index d67880811..5d7fca404 100644 --- a/apps/genrsa.c +++ b/apps/genrsa.c @@ -176,23 +176,7 @@ bad: goto err; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto err; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto err; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if (outfile == NULL) { diff --git a/apps/pkcs12.c b/apps/pkcs12.c index f277956cd..e24a8401e 100644 --- a/apps/pkcs12.c +++ b/apps/pkcs12.c @@ -300,19 +300,7 @@ int MAIN(int argc, char **argv) goto end; } - if (engine != NULL) { - if((e = ENGINE_by_id(engine)) == NULL) { - BIO_printf(bio_err,"invalid engine \"%s\"\n", engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if(passarg) { if(export_cert) passargout = passarg; diff --git a/apps/pkcs7.c b/apps/pkcs7.c index 63ec8a574..f3df25fe8 100644 --- a/apps/pkcs7.c +++ b/apps/pkcs7.c @@ -168,23 +168,7 @@ bad: ERR_load_crypto_strings(); - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); in=BIO_new(BIO_s_file()); out=BIO_new(BIO_s_file()); diff --git a/apps/pkcs8.c b/apps/pkcs8.c index 852a43558..5e20a2be7 100644 --- a/apps/pkcs8.c +++ b/apps/pkcs8.c @@ -185,23 +185,7 @@ int MAIN(int argc, char **argv) return (1); } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - return (1); - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - return (1); - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { BIO_printf(bio_err, "Error getting passwords\n"); diff --git a/apps/rand.c b/apps/rand.c index 8a216fbc7..10adf0e95 100644 --- a/apps/rand.c +++ b/apps/rand.c @@ -101,23 +101,7 @@ int MAIN(int argc, char **argv) goto err; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto err; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto err; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); app_RAND_load_file(NULL, bio_err, (inrand != NULL)); if (inrand != NULL) diff --git a/apps/req.c b/apps/req.c index 429eb9d4a..2999d509b 100644 --- a/apps/req.c +++ b/apps/req.c @@ -586,23 +586,7 @@ bad: if ((in == NULL) || (out == NULL)) goto end; - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if (keyfile != NULL) { diff --git a/apps/rsa.c b/apps/rsa.c index bdfe2189b..5b671e135 100644 --- a/apps/rsa.c +++ b/apps/rsa.c @@ -209,23 +209,7 @@ bad: ERR_load_crypto_strings(); - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if(!app_passwd(bio_err, passargin, passargout, &passin, &passout)) { BIO_printf(bio_err, "Error getting passwords\n"); diff --git a/apps/rsautl.c b/apps/rsautl.c index 86aa95d38..56025c5f0 100644 --- a/apps/rsautl.c +++ b/apps/rsautl.c @@ -157,23 +157,7 @@ int MAIN(int argc, char **argv) goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); /* FIXME: seed PRNG only if needed */ app_RAND_load_file(NULL, bio_err, 0); diff --git a/apps/s_client.c b/apps/s_client.c index bad59f646..009aa83e4 100644 --- a/apps/s_client.c +++ b/apps/s_client.c @@ -382,28 +382,7 @@ bad: OpenSSL_add_ssl_algorithms(); SSL_load_error_strings(); - if (engine_id != NULL) - { - if((e = ENGINE_by_id(engine_id)) == NULL) - { - BIO_printf(bio_err,"invalid engine\n"); - ERR_print_errors(bio_err); - goto end; - } - if (c_debug) - { - ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, - 0, bio_err, 0); - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - ERR_print_errors(bio_err); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine_id); - ENGINE_free(e); - } + e = setup_engine(bio_err, engine_id, 1); ctx=SSL_CTX_new(meth); if (ctx == NULL) diff --git a/apps/s_server.c b/apps/s_server.c index 8d76c8f3f..11971cb8c 100644 --- a/apps/s_server.c +++ b/apps/s_server.c @@ -657,28 +657,7 @@ bad: SSL_load_error_strings(); OpenSSL_add_ssl_algorithms(); - if (engine_id != NULL) - { - if((e = ENGINE_by_id(engine_id)) == NULL) - { - BIO_printf(bio_err,"invalid engine\n"); - ERR_print_errors(bio_err); - goto end; - } - if (s_debug) - { - ENGINE_ctrl(e, ENGINE_CTRL_SET_LOGSTREAM, - 0, bio_err, 0); - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - ERR_print_errors(bio_err); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine_id); - ENGINE_free(e); - } + e = setup_engine(bio_err, engine_id, 1); ctx=SSL_CTX_new(meth); if (ctx == NULL) diff --git a/apps/smime.c b/apps/smime.c index 869933459..1ff4ed02f 100644 --- a/apps/smime.c +++ b/apps/smime.c @@ -319,23 +319,7 @@ int MAIN(int argc, char **argv) goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) { BIO_printf(bio_err, "Error getting password\n"); diff --git a/apps/speed.c b/apps/speed.c index 27e6c4329..dd3270f6e 100644 --- a/apps/speed.c +++ b/apps/speed.c @@ -505,21 +505,8 @@ int MAIN(int argc, char **argv) BIO_printf(bio_err,"no engine given\n"); goto end; } - if((e = ENGINE_by_id(*argv)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - *argv); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", *argv); - /* Free our "structural" reference. */ - ENGINE_free(e); - /* It will be increased again further down. We just + e = setup_engine(bio_err, *argv, 0); + /* j will be increased again further down. We just don't want speed to confuse an engine with an algorithm, especially when none is given (which means all of them should be run) */ diff --git a/apps/spkac.c b/apps/spkac.c index 5ac9b14c5..538a41934 100644 --- a/apps/spkac.c +++ b/apps/spkac.c @@ -179,23 +179,7 @@ bad: goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if(keyfile) { if(strcmp(keyfile, "-")) key = BIO_new_file(keyfile, "r"); diff --git a/apps/verify.c b/apps/verify.c index 5b9d719d9..d5f07c2db 100644 --- a/apps/verify.c +++ b/apps/verify.c @@ -166,23 +166,7 @@ int MAIN(int argc, char **argv) break; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); lookup=X509_STORE_add_lookup(cert_ctx,X509_LOOKUP_file()); if (lookup == NULL) abort(); diff --git a/apps/x509.c b/apps/x509.c index 5be90740c..b2e4ec631 100644 --- a/apps/x509.c +++ b/apps/x509.c @@ -448,23 +448,7 @@ bad: goto end; } - if (engine != NULL) - { - if((e = ENGINE_by_id(engine)) == NULL) - { - BIO_printf(bio_err,"invalid engine \"%s\"\n", - engine); - goto end; - } - if(!ENGINE_set_default(e, ENGINE_METHOD_ALL)) - { - BIO_printf(bio_err,"can't use that engine\n"); - goto end; - } - BIO_printf(bio_err,"engine \"%s\" set.\n", engine); - /* Free our "structural" reference. */ - ENGINE_free(e); - } + e = setup_engine(bio_err, engine, 0); if (need_rand) app_RAND_load_file(NULL, bio_err, 0);