summaryrefslogtreecommitdiff
path: root/crypto
diff options
context:
space:
mode:
authorDr. David von Oheimb <David.von.Oheimb@siemens.com>2021-03-22 14:16:56 +0100
committerDr. David von Oheimb <dev@ddvo.net>2021-04-08 15:18:58 +0200
commitc1fd710297a21336ec0410fe86784c322945b805 (patch)
treea03b44f007e132f873630fab2bb36d153d92a8d2 /crypto
parent321ac1f2973c01f4a4a2719e4400c26ff01c3231 (diff)
downloadopenssl-new-c1fd710297a21336ec0410fe86784c322945b805.tar.gz
d2i_PrivateKey{,_ex}() and PEM_X509_INFO_read_bio_ex(): Fix handling of RSA/DSA/EC private key
This is needed to correct d2i_PrivateKey() after it was changed by commit 576892d78f80cf9a. Reviewed-by: Tomas Mraz <tomas@openssl.org> (Merged from https://github.com/openssl/openssl/pull/14647)
Diffstat (limited to 'crypto')
-rw-r--r--crypto/asn1/d2i_pr.c19
-rw-r--r--crypto/pem/pem_info.c5
2 files changed, 15 insertions, 9 deletions
diff --git a/crypto/asn1/d2i_pr.c b/crypto/asn1/d2i_pr.c
index fb0ae08356..9d9c1898cb 100644
--- a/crypto/asn1/d2i_pr.c
+++ b/crypto/asn1/d2i_pr.c
@@ -29,7 +29,7 @@ d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
{
OSSL_DECODER_CTX *dctx = NULL;
size_t len = length;
- EVP_PKEY *pkey = NULL;
+ EVP_PKEY *pkey = NULL, *bak_a = NULL;
EVP_PKEY **ppkey = &pkey;
const char *key_name = NULL;
const char *input_structures[] = { "type-specific", "pkcs8", NULL };
@@ -40,15 +40,17 @@ d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
if (key_name == NULL)
return NULL;
}
- if (a != NULL && *a != NULL)
- ppkey = a;
for (i = 0; i < (int)OSSL_NELEM(input_structures); ++i) {
const unsigned char *p = *pp;
+ if (a != NULL && (bak_a = *a) != NULL)
+ ppkey = a;
dctx = OSSL_DECODER_CTX_new_for_pkey(ppkey, "DER",
input_structures[i], key_name,
EVP_PKEY_KEYPAIR, libctx, propq);
+ if (a != NULL)
+ *a = bak_a;
if (dctx == NULL)
return NULL;
@@ -56,8 +58,11 @@ d2i_PrivateKey_decoder(int keytype, EVP_PKEY **a, const unsigned char **pp,
OSSL_DECODER_CTX_free(dctx);
if (ret) {
if (*ppkey != NULL
- && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY))
+ && evp_keymgmt_util_has(*ppkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY)) {
+ if (a != NULL)
+ *a = *ppkey;
return *ppkey;
+ }
*pp = p;
goto err;
}
@@ -76,7 +81,7 @@ d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp,
EVP_PKEY *ret;
const unsigned char *p = *pp;
- if ((a == NULL) || (*a == NULL)) {
+ if (a == NULL || *a == NULL) {
if ((ret = EVP_PKEY_new()) == NULL) {
ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
return NULL;
@@ -127,7 +132,7 @@ d2i_PrivateKey_legacy(int keytype, EVP_PKEY **a, const unsigned char **pp,
}
*pp = p;
if (a != NULL)
- (*a) = ret;
+ *a = ret;
return ret;
err:
if (a == NULL || *a != ret)
@@ -195,7 +200,7 @@ static EVP_PKEY *d2i_AutoPrivateKey_legacy(EVP_PKEY **a,
if (ret == NULL)
return NULL;
*pp = p;
- if (a) {
+ if (a != NULL) {
*a = ret;
}
return ret;
diff --git a/crypto/pem/pem_info.c b/crypto/pem/pem_info.c
index 54e29ab41f..2714009103 100644
--- a/crypto/pem/pem_info.c
+++ b/crypto/pem/pem_info.c
@@ -209,7 +209,8 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio_ex(BIO *bp, STACK_OF(X509_INFO) *sk,
goto err;
p = data;
if (ptype) {
- if (!d2i_PrivateKey(ptype, pp, &p, len)) {
+ if (d2i_PrivateKey_ex(ptype, pp, &p, len,
+ libctx, propq) == NULL) {
ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
goto err;
}
@@ -217,7 +218,7 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio_ex(BIO *bp, STACK_OF(X509_INFO) *sk,
ERR_raise(ERR_LIB_PEM, ERR_R_ASN1_LIB);
goto err;
}
- } else { /* encrypted RSA data */
+ } else { /* encrypted key data */
if (!PEM_get_EVP_CIPHER_INFO(header, &xi->enc_cipher))
goto err;
xi->enc_data = (char *)data;