Restore module loading

The module loading feature got broken a while ago, so restore it, but
have it a bit more explicit this time around.

Reviewed-by: Stephen Henson <steve@openssl.org>
This commit is contained in:
Richard Levitte 2015-05-29 08:07:10 +02:00
parent 2142519500
commit 296f54ee21
43 changed files with 186 additions and 14 deletions

View file

@ -496,20 +496,14 @@ static char *app_get_pass(char *arg, int keepbio)
return BUF_strdup(tpass); return BUF_strdup(tpass);
} }
CONF *app_load_config(const char *filename) static CONF *app_load_config_(BIO *in, const char *filename)
{ {
long errorline = -1; long errorline = -1;
CONF *conf; CONF *conf;
int i; int i;
BIO *in;
in = bio_open_default(filename, "r");
if (in == NULL)
return NULL;
conf = NCONF_new(NULL); conf = NCONF_new(NULL);
i = NCONF_load_bio(conf, in, &errorline); i = NCONF_load_bio(conf, in, &errorline);
BIO_free(in);
if (i > 0) if (i > 0)
return conf; return conf;
@ -522,6 +516,51 @@ CONF *app_load_config(const char *filename)
NCONF_free(conf); NCONF_free(conf);
return NULL; return NULL;
} }
CONF *app_load_config(const char *filename)
{
BIO *in;
CONF *conf;
in = bio_open_default(filename, "r");
if (in == NULL)
return NULL;
conf = app_load_config_(in, filename);
BIO_free(in);
return conf;
}
CONF *app_load_config_quiet(const char *filename)
{
BIO *in;
CONF *conf;
in = bio_open_default_quiet(filename, "r");
if (in == NULL)
return NULL;
conf = app_load_config_(in, filename);
BIO_free(in);
return conf;
}
int app_load_modules(const CONF *config)
{
CONF *to_free = NULL;
if (config == NULL)
config = to_free = app_load_config_quiet(default_config_file);
if (config == NULL)
return 1;
if (CONF_modules_load(config, NULL, 0) <= 0) {
BIO_printf(bio_err, "Error configuring OpenSSL modules\n");
ERR_print_errors(bio_err);
NCONF_free(to_free);
return 0;
}
NCONF_free(to_free);
return 1;
}
int add_oid_section(CONF *conf) int add_oid_section(CONF *conf)
{ {

View file

@ -154,7 +154,10 @@ extern BIO *bio_err;
BIO *dup_bio_in(void); BIO *dup_bio_in(void);
BIO *dup_bio_out(void); BIO *dup_bio_out(void);
BIO *bio_open_default(const char *filename, const char *mode); BIO *bio_open_default(const char *filename, const char *mode);
CONF *app_load_config(const char* filename); BIO *bio_open_default_quiet(const char *filename, const char *mode);
CONF *app_load_config(const char *filename);
CONF *app_load_config_quiet(const char *filename);
int app_load_modules(const CONF *config);
void unbuffer(FILE *fp); void unbuffer(FILE *fp);
/* Often used in calls to bio_open_default. */ /* Often used in calls to bio_open_default. */

View file

@ -186,8 +186,11 @@ int asn1parse_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (oidfile != NULL) { if (oidfile != NULL) {
in = bio_open_default(oidfile, "r"); in = bio_open_default(oidfile, "r");
if (in == NULL) if (in == NULL)
goto end; goto end;
OBJ_create_objects(in); OBJ_create_objects(in);

View file

@ -485,6 +485,8 @@ end_of_options:
BIO_printf(bio_err, "Using configuration from %s\n", configfile); BIO_printf(bio_err, "Using configuration from %s\n", configfile);
if ((conf = app_load_config(configfile)) == NULL) if ((conf = app_load_config(configfile)) == NULL)
goto end; goto end;
if (!app_load_modules(conf))
goto end;
/* Lets get the config section we are using */ /* Lets get the config section we are using */
if (section == NULL) { if (section == NULL) {

View file

@ -148,6 +148,9 @@ int ciphers_main(int argc, char **argv)
else if (argc != 0) else if (argc != 0)
goto opthelp; goto opthelp;
if (!app_load_modules(NULL))
goto end;
ctx = SSL_CTX_new(meth); ctx = SSL_CTX_new(meth);
if (ctx == NULL) if (ctx == NULL)
goto err; goto err;

View file

@ -664,12 +664,14 @@ int cms_main(int argc, char **argv)
} else if (!operation) } else if (!operation)
goto opthelp; goto opthelp;
if (!app_passwd(passinarg, NULL, &passin, NULL)) { if (!app_passwd(passinarg, NULL, &passin, NULL)) {
BIO_printf(bio_err, "Error getting password\n"); BIO_printf(bio_err, "Error getting password\n");
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
if (need_rand) { if (need_rand) {
app_RAND_load_file(NULL, (inrand != NULL)); app_RAND_load_file(NULL, (inrand != NULL));
if (inrand != NULL) if (inrand != NULL)

View file

@ -217,6 +217,9 @@ int crl_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
x = load_crl(infile, informat); x = load_crl(infile, informat);
if (x == NULL) if (x == NULL)
goto end; goto end;

View file

@ -148,6 +148,9 @@ int crl2pkcs7_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (!nocrl) { if (!nocrl) {
in = bio_open_default(infile, RB(informat)); in = bio_open_default(infile, RB(informat));
if (in == NULL) if (in == NULL)

View file

@ -236,6 +236,9 @@ int dgst_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (do_verify && !sigfile) { if (do_verify && !sigfile) {
BIO_printf(bio_err, BIO_printf(bio_err,
"No signature to verify: use the -signature option\n"); "No signature to verify: use the -signature option\n");

View file

@ -230,6 +230,9 @@ int dhparam_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (argv[0] && (!opt_int(argv[0], &num) || num <= 0)) if (argv[0] && (!opt_int(argv[0], &num) || num <= 0))
goto end; goto end;

View file

@ -196,6 +196,9 @@ int dsa_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
BIO_printf(bio_err, "read DSA key\n"); BIO_printf(bio_err, "read DSA key\n");
{ {
EVP_PKEY *pkey; EVP_PKEY *pkey;

View file

@ -185,6 +185,9 @@ int dsaparam_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (argc == 1) { if (argc == 1) {
if (!opt_int(argv[0], &num)) if (!opt_int(argv[0], &num))
goto end; goto end;

View file

@ -199,6 +199,9 @@ int ec_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
in = bio_open_default(infile, RB(informat)); in = bio_open_default(infile, RB(informat));
if (in == NULL) if (in == NULL)
goto end; goto end;

View file

@ -220,6 +220,9 @@ int ecparam_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
in = bio_open_default(infile, RB(informat)); in = bio_open_default(infile, RB(informat));
if (in == NULL) if (in == NULL)
goto end; goto end;

View file

@ -294,6 +294,9 @@ int enc_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (cipher && EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) { if (cipher && EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
BIO_printf(bio_err, "%s: AEAD ciphers not supported\n", prog); BIO_printf(bio_err, "%s: AEAD ciphers not supported\n", prog);
goto end; goto end;

View file

@ -369,6 +369,9 @@ int engine_main(int argc, char **argv)
} }
} }
if (!app_load_modules(NULL))
goto end;
for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) { for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
const char *id = sk_OPENSSL_STRING_value(engines, i); const char *id = sk_OPENSSL_STRING_value(engines, i);
if ((e = ENGINE_by_id(id)) != NULL) { if ((e = ENGINE_by_id(id)) != NULL) {

View file

@ -143,6 +143,9 @@ int gendsa_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
in = bio_open_default(dsaparams, "r"); in = bio_open_default(dsaparams, "r");
if (in == NULL) if (in == NULL)
goto end2; goto end2;

View file

@ -179,6 +179,9 @@ int genpkey_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
out = bio_open_default(outfile, "wb"); out = bio_open_default(outfile, "wb");
if (out == NULL) if (out == NULL)
goto end; goto end;

View file

@ -166,6 +166,9 @@ int genrsa_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
out = bio_open_default(outfile, "w"); out = bio_open_default(outfile, "w");
if (out == NULL) if (out == NULL)
goto end; goto end;

View file

@ -109,6 +109,9 @@ int nseq_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
in = bio_open_default(infile, "r"); in = bio_open_default(infile, "r");
if (in == NULL) if (in == NULL)
goto end; goto end;

View file

@ -482,6 +482,9 @@ int ocsp_main(int argc, char **argv)
if (!req && !reqin && !respin && !(port && ridx_filename)) if (!req && !reqin && !respin && !(port && ridx_filename))
goto opthelp; goto opthelp;
if (!app_load_modules(NULL))
goto end;
out = bio_open_default(outfile, "w"); out = bio_open_default(outfile, "w");
if (out == NULL) if (out == NULL)
goto end; goto end;

View file

@ -289,12 +289,16 @@ void unbuffer(FILE *fp)
setbuf(fp, NULL); setbuf(fp, NULL);
} }
BIO *bio_open_default(const char *filename, const char *mode) static BIO *bio_open_default_(const char *filename, const char *mode, int quiet)
{ {
BIO *ret; BIO *ret;
if (filename == NULL || strcmp(filename, "-") == 0) { if (filename == NULL || strcmp(filename, "-") == 0) {
ret = *mode == 'r' ? dup_bio_in() : dup_bio_out(); ret = *mode == 'r' ? dup_bio_in() : dup_bio_out();
if (quiet) {
ERR_clear_error();
return ret;
}
if (ret != NULL) if (ret != NULL)
return ret; return ret;
BIO_printf(bio_err, BIO_printf(bio_err,
@ -302,6 +306,10 @@ BIO *bio_open_default(const char *filename, const char *mode)
*mode == 'r' ? "stdin" : "stdout", strerror(errno)); *mode == 'r' ? "stdin" : "stdout", strerror(errno));
} else { } else {
ret = BIO_new_file(filename, mode); ret = BIO_new_file(filename, mode);
if (quiet) {
ERR_clear_error();
return ret;
}
if (ret != NULL) if (ret != NULL)
return ret; return ret;
BIO_printf(bio_err, BIO_printf(bio_err,
@ -312,6 +320,14 @@ BIO *bio_open_default(const char *filename, const char *mode)
ERR_print_errors(bio_err); ERR_print_errors(bio_err);
return NULL; return NULL;
} }
BIO *bio_open_default(const char *filename, const char *mode)
{
return bio_open_default_(filename, mode, 0);
}
BIO *bio_open_default_quiet(const char *filename, const char *mode)
{
return bio_open_default_(filename, mode, 1);
}
#if defined( OPENSSL_SYS_VMS) #if defined( OPENSSL_SYS_VMS)
extern char **copy_argv(int *argc, char **argv); extern char **copy_argv(int *argc, char **argv);

View file

@ -202,6 +202,9 @@ int passwd_main(int argc, char **argv)
goto opthelp; goto opthelp;
# endif # endif
if (!app_load_modules(NULL))
goto end;
if (infile && in_stdin) { if (infile && in_stdin) {
BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog); BIO_printf(bio_err, "%s: Can't combine -in and -stdin\n", prog);
goto end; goto end;

View file

@ -342,6 +342,9 @@ int pkcs12_main(int argc, char **argv)
mpass = macpass; mpass = macpass;
} }
if (!app_load_modules(NULL))
goto end;
if (export_cert || inrand) { if (export_cert || inrand) {
app_RAND_load_file(NULL, (inrand != NULL)); app_RAND_load_file(NULL, (inrand != NULL));
if (inrand != NULL) if (inrand != NULL)

View file

@ -193,6 +193,9 @@ int pkcs7_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
in = bio_open_default(infile, RB(informat)); in = bio_open_default(infile, RB(informat));
if (in == NULL) if (in == NULL)
goto end; goto end;

View file

@ -223,6 +223,9 @@ int pkcs8_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
if ((pbe_nid == -1) && !cipher) if ((pbe_nid == -1) && !cipher)
pbe_nid = NID_pbeWithMD5AndDES_CBC; pbe_nid = NID_pbeWithMD5AndDES_CBC;

View file

@ -165,6 +165,9 @@ int pkey_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
out = bio_open_default(outfile, "wb"); out = bio_open_default(outfile, "wb");
if (out == NULL) if (out == NULL)
goto end; goto end;

View file

@ -118,6 +118,9 @@ int pkeyparam_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
in = bio_open_default(infile, "r"); in = bio_open_default(infile, "r");
if (in == NULL) if (in == NULL)
goto end; goto end;

View file

@ -229,6 +229,9 @@ int pkeyutl_main(int argc, char **argv)
if (ctx == NULL) if (ctx == NULL)
goto opthelp; goto opthelp;
if (!app_load_modules(NULL))
goto end;
if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) { if (sigfile && (pkey_op != EVP_PKEY_OP_VERIFY)) {
BIO_printf(bio_err, BIO_printf(bio_err,
"%s: Signature file specified for non verify\n", prog); "%s: Signature file specified for non verify\n", prog);

View file

@ -109,6 +109,9 @@ int prime_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (argc == 0 && !generate) { if (argc == 0 && !generate) {
BIO_printf(bio_err, "%s: No prime specified\n", prog); BIO_printf(bio_err, "%s: No prime specified\n", prog);
goto end; goto end;

View file

@ -126,6 +126,9 @@ int rand_main(int argc, char **argv)
if (sscanf(argv[0], "%d", &num) != 1 || num < 0) if (sscanf(argv[0], "%d", &num) != 1 || num < 0)
goto opthelp; goto opthelp;
if (!app_load_modules(NULL))
goto end;
app_RAND_load_file(NULL, (inrand != NULL)); app_RAND_load_file(NULL, (inrand != NULL));
if (inrand != NULL) if (inrand != NULL)
BIO_printf(bio_err, "%ld semi-random bytes loaded\n", BIO_printf(bio_err, "%ld semi-random bytes loaded\n",

View file

@ -380,6 +380,9 @@ int req_main(int argc, char **argv)
if (verbose) if (verbose)
BIO_printf(bio_err, "Using configuration from %s\n", template); BIO_printf(bio_err, "Using configuration from %s\n", template);
req_conf = app_load_config(template); req_conf = app_load_config(template);
if (!app_load_modules(req_conf))
goto end;
if (req_conf != NULL) { if (req_conf != NULL) {
p = NCONF_get_string(req_conf, NULL, "oid_file"); p = NCONF_get_string(req_conf, NULL, "oid_file");
if (p == NULL) if (p == NULL)

View file

@ -254,6 +254,9 @@ int rsa_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
if (check && pubin) { if (check && pubin) {
BIO_printf(bio_err, "Only private keys can be checked\n"); BIO_printf(bio_err, "Only private keys can be checked\n");
goto end; goto end;

View file

@ -214,6 +214,9 @@ int rsautl_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
/* FIXME: seed PRNG only if needed */ /* FIXME: seed PRNG only if needed */
app_RAND_load_file(NULL, 0); app_RAND_load_file(NULL, 0);

View file

@ -1059,6 +1059,9 @@ int s_client_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if (proxystr) { if (proxystr) {
if (connectstr == NULL) { if (connectstr == NULL) {
BIO_printf(bio_err, "%s: -proxy requires use of -connect\n", prog); BIO_printf(bio_err, "%s: -proxy requires use of -connect\n", prog);

View file

@ -1438,6 +1438,9 @@ int s_server_main(int argc, char *argv[])
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
if (s_key_file == NULL) if (s_key_file == NULL)
s_key_file = s_cert_file; s_key_file = s_cert_file;

View file

@ -411,6 +411,9 @@ int smime_main(int argc, char **argv)
goto end; goto end;
} }
if (!app_load_modules(NULL))
goto end;
if (need_rand) { if (need_rand) {
app_RAND_load_file(NULL, (inrand != NULL)); app_RAND_load_file(NULL, (inrand != NULL));
if (inrand != NULL) if (inrand != NULL)

View file

@ -856,6 +856,9 @@ int speed_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
/* Remaining arguments are algorithms. */ /* Remaining arguments are algorithms. */
for ( ; *argv; argv++) { for ( ; *argv; argv++) {
if (found(*argv, doit_choices, &i)) { if (found(*argv, doit_choices, &i)) {

View file

@ -186,6 +186,8 @@ int spkac_main(int argc, char **argv)
if ((conf = app_load_config(infile)) == NULL) if ((conf = app_load_config(infile)) == NULL)
goto end; goto end;
if (!app_load_modules(conf))
goto end;
spkstr = NCONF_get_string(conf, spksect, spkac); spkstr = NCONF_get_string(conf, spksect, spkac);

View file

@ -354,6 +354,8 @@ int srp_main(int argc, char **argv)
conf = app_load_config(configfile); conf = app_load_config(configfile);
if (conf == NULL) if (conf == NULL)
goto end; goto end;
if (!app_load_modules(conf))
goto end;
/* Lets get the config section we are using */ /* Lets get the config section we are using */
if (section == NULL) { if (section == NULL) {

View file

@ -316,6 +316,10 @@ int ts_main(int argc, char **argv)
goto end; goto end;
} }
conf = load_config_file(configfile);
if (!app_load_modules(conf))
goto end;
/* /*
* Check consistency of parameters and execute the appropriate function. * Check consistency of parameters and execute the appropriate function.
*/ */
@ -331,13 +335,10 @@ int ts_main(int argc, char **argv)
ret = data != NULL && digest != NULL; ret = data != NULL && digest != NULL;
if (ret) if (ret)
goto opthelp; goto opthelp;
/* Load the config file for possible policy OIDs. */
conf = load_config_file(configfile);
ret = !query_command(data, digest, md, policy, no_nonce, cert, ret = !query_command(data, digest, md, policy, no_nonce, cert,
in, out, text); in, out, text);
break; break;
case OPT_REPLY: case OPT_REPLY:
conf = load_config_file(configfile);
if (in == NULL) { if (in == NULL) {
ret = !(queryfile != NULL && conf != NULL && !token_in); ret = !(queryfile != NULL && conf != NULL && !token_in);
if (ret) if (ret)

View file

@ -177,6 +177,9 @@ int verify_main(int argc, char **argv)
argc = opt_num_rest(); argc = opt_num_rest();
argv = opt_rest(); argv = opt_rest();
if (!app_load_modules(NULL))
goto end;
if ((store = setup_verify(CAfile, CApath)) == NULL) if ((store = setup_verify(CAfile, CApath)) == NULL)
goto end; goto end;
X509_STORE_set_verify_cb(store, cb); X509_STORE_set_verify_cb(store, cb);

View file

@ -489,6 +489,9 @@ int x509_main(int argc, char **argv)
goto opthelp; goto opthelp;
} }
if (!app_load_modules(NULL))
goto end;
out = bio_open_default(outfile, "w"); out = bio_open_default(outfile, "w");
if (out == NULL) if (out == NULL)
goto end; goto end;