diff options
author | Richard Levitte <levitte@openssl.org> | 2020-07-30 10:09:43 +0200 |
---|---|---|
committer | Richard Levitte <levitte@openssl.org> | 2020-08-20 12:37:35 +0200 |
commit | 2274d22d39fd65b83c9c969cba86c8f03b3e0bec (patch) | |
tree | dd86a9ea759bc5d2d1bd541afe0d28a929018e6d /apps/lib | |
parent | 6cc1dfca88c565ddacd9ea9aa8261ef9c0c37335 (diff) |
STORE: Distinguish public keys from private keys
While public keys and private keys use the same type (EVP_PKEY), just
with different contents, callers still need to distinguish between the
two to be able to know what functions to call with them (for example,
to be able to choose between EVP_PKEY_print_private() and
EVP_PKEY_print_public()).
The OSSL_STORE backend knows what it loaded, so it has the capacity to
inform.
Note that the same as usual still applies, that a private key EVP_PKEY
contains the public parts, but not necessarily the other way around.
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/12673)
Diffstat (limited to 'apps/lib')
-rw-r--r-- | apps/lib/apps.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/apps/lib/apps.c b/apps/lib/apps.c index e8592c4880..d19fdc2126 100644 --- a/apps/lib/apps.c +++ b/apps/lib/apps.c @@ -491,7 +491,8 @@ X509 *load_cert_pass(const char *uri, int maybe_stdin, if (desc == NULL) desc = "certificate"; - (void)load_key_cert_crl(uri, maybe_stdin, pass, desc, NULL, &cert, NULL); + (void)load_key_cert_crl(uri, maybe_stdin, pass, desc, + NULL, NULL, &cert, NULL); if (cert == NULL) { BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); @@ -512,7 +513,8 @@ X509_CRL *load_crl(const char *uri, int format, const char *desc) if (desc == NULL) desc = "CRL"; - (void)load_key_cert_crl(uri, 0, NULL, desc, NULL, NULL, &crl); + (void)load_key_cert_crl(uri, 0, NULL, desc, + NULL, NULL, NULL, &crl); if (crl == NULL) { BIO_printf(bio_err, "Unable to load %s\n", desc); ERR_print_errors(bio_err); @@ -591,7 +593,8 @@ EVP_PKEY *load_key(const char *uri, int format, int may_stdin, #endif } } else { - (void)load_key_cert_crl(uri, may_stdin, pass, desc, &pkey, NULL, NULL); + (void)load_key_cert_crl(uri, may_stdin, pass, desc, + &pkey, NULL, NULL, NULL); } if (pkey == NULL) { @@ -629,8 +632,8 @@ EVP_PKEY *load_pubkey(const char *uri, int format, int maybe_stdin, #endif } } else { - (void)load_key_cert_crl(uri, maybe_stdin, pass, desc, &pkey, - NULL, NULL); + (void)load_key_cert_crl(uri, maybe_stdin, pass, desc, + NULL, &pkey, NULL, NULL); } if (pkey == NULL) { BIO_printf(bio_err, "Unable to load %s\n", desc); @@ -769,7 +772,8 @@ int load_crls(const char *file, STACK_OF(X509_CRL) **crls, int format, */ int load_key_cert_crl(const char *uri, int maybe_stdin, const char *pass, const char *desc, - EVP_PKEY **ppkey, X509 **pcert, X509_CRL **pcrl) + EVP_PKEY **ppkey, EVP_PKEY **ppubkey, + X509 **pcert, X509_CRL **pcrl) { PW_CB_DATA uidata; OSSL_STORE_CTX *ctx = NULL; @@ -780,6 +784,8 @@ int load_key_cert_crl(const char *uri, int maybe_stdin, if (ppkey != NULL) *ppkey = NULL; + if (ppubkey != NULL) + *ppubkey = NULL; if (pcert != NULL) *pcert = NULL; if (pcrl != NULL) @@ -831,6 +837,18 @@ int load_key_cert_crl(const char *uri, int maybe_stdin, case OSSL_STORE_INFO_PKEY: if (ppkey != NULL && *ppkey == NULL) err = ((*ppkey = OSSL_STORE_INFO_get1_PKEY(info)) == NULL); + + /* + * An EVP_PKEY with private parts also holds the public parts, + * so if the caller asked for a public key, and we got a private + * key, we can still pass it back. + */ + if (ppubkey != NULL && *ppubkey == NULL) + err = ((*ppubkey = OSSL_STORE_INFO_get1_PKEY(info)) == NULL); + break; + case OSSL_STORE_INFO_PUBKEY: + if (ppubkey != NULL && *ppubkey == NULL) + err = ((*ppubkey = OSSL_STORE_INFO_get1_PUBKEY(info)) == NULL); break; case OSSL_STORE_INFO_CERT: if (pcert != NULL && *pcert == NULL) |