Update dependencies.
[openssl.git] / crypto / pem / pem_pkey.c
index 7747a05..cb208a7 100644 (file)
@@ -65,6 +65,9 @@
 #include <openssl/x509.h>
 #include <openssl/pkcs12.h>
 #include <openssl/pem.h>
+#ifndef OPENSSL_NO_ENGINE
+#include <openssl/engine.h>
+#endif
 #include "asn1_locl.h"
 
 int pem_check_suffix(const char *pem_str, const char *suffix);
@@ -82,15 +85,7 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, vo
                return NULL;
        p = data;
 
-       if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0)
-               {
-               const EVP_PKEY_ASN1_METHOD *ameth;
-               ameth = EVP_PKEY_asn1_find_str(nm, slen);
-               if (!ameth || !ameth->old_priv_decode)
-                       goto p8err;
-               ret=d2i_PrivateKey(ameth->pkey_id,x,&p,len);
-               }
-       else if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {
+       if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {
                PKCS8_PRIV_KEY_INFO *p8inf;
                p8inf=d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
                if(!p8inf) goto p8err;
@@ -124,12 +119,20 @@ EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, vo
                        *x = ret;
                }
                PKCS8_PRIV_KEY_INFO_free(p8inf);
-       }
+       } else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0)
+               {
+               const EVP_PKEY_ASN1_METHOD *ameth;
+               ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
+               if (!ameth || !ameth->old_priv_decode)
+                       goto p8err;
+               ret=d2i_PrivateKey(ameth->pkey_id,x,&p,len);
+               }
 p8err:
        if (ret == NULL)
                PEMerr(PEM_F_PEM_READ_BIO_PRIVATEKEY,ERR_R_ASN1_LIB);
 err:
        OPENSSL_free(nm);
+       OPENSSL_cleanse(data, len);
        OPENSSL_free(data);
        return(ret);
        }
@@ -145,8 +148,8 @@ int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
                                                        cb, u);
 
        BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
-       return PEM_ASN1_write_bio((i2d_of_void *)openssl_fcast(i2d_PrivateKey),
-                               pem_str,bp,(char *)x,enc,kstr,klen,cb,u);
+       return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
+                               pem_str,bp,x,enc,kstr,klen,cb,u);
        }
 
 EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
@@ -165,14 +168,12 @@ EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
 
        if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0)
                {
-               const EVP_PKEY_ASN1_METHOD *ameth;
-               ameth = EVP_PKEY_asn1_find_str(nm, slen);
-               if (!ameth || !ameth->param_decode)
-                       goto err;
                ret = EVP_PKEY_new();
                if (!ret)
                        goto err;
-               if (!ameth->param_decode(ret, &p, len))
+               if (!EVP_PKEY_set_type_str(ret, nm, slen)
+                       || !ret->ameth->param_decode
+                       || !ret->ameth->param_decode(ret, &p, len))
                        {
                        EVP_PKEY_free(ret);
                        ret = NULL;
@@ -200,8 +201,8 @@ int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
 
        BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
        return PEM_ASN1_write_bio(
-               (i2d_of_void *)openssl_fcast(x->ameth->param_encode),
-                               pem_str,bp,(char *)x,NULL,NULL,0,0,NULL);
+               (i2d_of_void *)x->ameth->param_encode,
+                               pem_str,bp,x,NULL,NULL,0,0,NULL);
        }
 
 #ifndef OPENSSL_NO_FP_API