diff options
author | Richard Levitte <levitte@openssl.org> | 2020-11-30 07:25:46 +0100 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2020-12-02 20:19:31 +0100 |
commit | f91d003a0ef0c748a11ccdb19c7661a3f2df9ab0 (patch) | |
tree | 20ead7bd3ed8cee5bd59e1076644526959709a5b /apps/lib/apps.c | |
parent | 0b27381fd544beca44df905991923a7fa374d80a (diff) |
APPS: Adapt load_key() and load_pubkey() for the engine: loader
These two functions react when the FORMAT_ENGINE format is given, and
use the passed ENGINE |e| and the passed key argument to form a URI
suitable for the engine: loader.
Co-authored-by: David von Oheimb <david.von.oheimb@siemens.com>
Reviewed-by: David von Oheimb <david.von.oheimb@siemens.com>
(Merged from https://github.com/openssl/openssl/pull/13570)
Diffstat (limited to 'apps/lib/apps.c')
-rw-r--r-- | apps/lib/apps.c | 67 |
1 files changed, 39 insertions, 28 deletions
diff --git a/apps/lib/apps.c b/apps/lib/apps.c index 7d63ff4216..699802044d 100644 --- a/apps/lib/apps.c +++ b/apps/lib/apps.c @@ -560,25 +560,18 @@ EVP_PKEY *load_key(const char *uri, int format, int may_stdin, const char *pass, ENGINE *e, const char *desc) { EVP_PKEY *pkey = NULL; + char *allocated_uri = NULL; if (desc == NULL) desc = "private key"; if (format == FORMAT_ENGINE) { - if (e == NULL) { - BIO_printf(bio_err, "No engine specified for loading %s\n", desc); - } else { - pkey = load_engine_private_key(e, uri, pass, desc); - if (pkey == NULL) { - BIO_printf(bio_err, "Cannot load %s from engine\n", desc); - ERR_print_errors(bio_err); - } - } - } else { - (void)load_key_certs_crls(uri, may_stdin, pass, desc, - &pkey, NULL, NULL, NULL, NULL, NULL, NULL); + uri = allocated_uri = make_engine_uri(e, uri, desc); } + (void)load_key_certs_crls(uri, may_stdin, pass, desc, + &pkey, NULL, NULL, NULL, NULL, NULL, NULL); + OPENSSL_free(allocated_uri); return pkey; } @@ -586,25 +579,18 @@ EVP_PKEY *load_pubkey(const char *uri, int format, int maybe_stdin, const char *pass, ENGINE *e, const char *desc) { EVP_PKEY *pkey = NULL; + char *allocated_uri = NULL; if (desc == NULL) desc = "public key"; if (format == FORMAT_ENGINE) { - if (e == NULL) { - BIO_printf(bio_err, "No engine specified for loading %s\n", desc); - } else { - pkey = load_engine_public_key(e, uri, pass, desc); - if (pkey == NULL) { - BIO_printf(bio_err, "Cannot load %s from engine\n", desc); - ERR_print_errors(bio_err); - } - } - } else { - (void)load_key_certs_crls(uri, maybe_stdin, pass, desc, - NULL, &pkey, NULL, NULL, NULL, NULL, NULL); + uri = allocated_uri = make_engine_uri(e, uri, desc); } + (void)load_key_certs_crls(uri, maybe_stdin, pass, desc, + NULL, &pkey, NULL, NULL, NULL, NULL, NULL); + OPENSSL_free(allocated_uri); return pkey; } @@ -717,14 +703,25 @@ int load_key_certs_crls(const char *uri, int maybe_stdin, pparams != NULL ? "params" : pcert != NULL ? "cert" : pcrl != NULL ? "CRL" : pcerts != NULL ? "certs" : pcrls != NULL ? "CRLs" : NULL; + int cnt_expectations = 0; + int expect = 0; /* TODO make use of the engine reference 'eng' when loading pkeys */ - if (ppkey != NULL) + if (ppkey != NULL) { *ppkey = NULL; - if (ppubkey != NULL) + cnt_expectations++; + expect = OSSL_STORE_INFO_PKEY; + } + if (ppubkey != NULL) { *ppubkey = NULL; - if (pcert != NULL) + cnt_expectations++; + expect = OSSL_STORE_INFO_PUBKEY; + } + if (pcert != NULL) { *pcert = NULL; + cnt_expectations++; + expect = OSSL_STORE_INFO_CERT; + } if (failed == NULL) { BIO_printf(bio_err, "Internal error: nothing to load into from %s\n", uri != NULL ? uri : "<stdin>"); @@ -735,13 +732,22 @@ int load_key_certs_crls(const char *uri, int maybe_stdin, && (*pcerts = sk_X509_new_null()) == NULL) { BIO_printf(bio_err, "Out of memory loading"); goto end; + } else { + cnt_expectations++; + expect = OSSL_STORE_INFO_CERT; } - if (pcrl != NULL) + if (pcrl != NULL) { *pcrl = NULL; + cnt_expectations++; + expect = OSSL_STORE_INFO_CRL; + } if (pcrls != NULL && *pcrls == NULL && (*pcrls = sk_X509_CRL_new_null()) == NULL) { BIO_printf(bio_err, "Out of memory loading"); goto end; + } else { + cnt_expectations++; + expect = OSSL_STORE_INFO_CRL; } uidata.password = pass; @@ -769,6 +775,11 @@ int load_key_certs_crls(const char *uri, int maybe_stdin, goto end; } + if (cnt_expectations != 1) + expect = 0; + if (!OSSL_STORE_expect(ctx, expect)) + goto end; + failed = NULL; while (!OSSL_STORE_eof(ctx)) { OSSL_STORE_INFO *info = OSSL_STORE_load(ctx); |