Complete EVP_PKEY_ASN1_METHOD ENGINE support.
[openssl.git] / crypto / asn1 / d2i_pr.c
index 512d11d6f6d56ffbd710466acca466e8c665a69f..b2791ea66e351be76d1c71abb9d403cdbf24c9de 100644 (file)
 #include <openssl/bn.h>
 #include <openssl/evp.h>
 #include <openssl/objects.h>
+#include <openssl/engine.h>
 #include <openssl/asn1.h>
-#ifndef OPENSSL_NO_RSA
-#include <openssl/rsa.h>
-#endif
-#ifndef OPENSSL_NO_DSA
-#include <openssl/dsa.h>
-#endif
-#ifndef OPENSSL_NO_EC
-#include <openssl/ec.h>
-#endif
+#include "asn1_locl.h"
 
 EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
             long length)
@@ -85,47 +78,27 @@ EVP_PKEY *d2i_PrivateKey(int type, EVP_PKEY **a, const unsigned char **pp,
                        return(NULL);
                        }
                }
-       else    ret= *a;
-
-       ret->save_type=type;
-       ret->type=EVP_PKEY_type(type);
-       ret->ameth = EVP_PKEY_ASN1_find(type);
-       switch (ret->type)
+       else
                {
-#ifndef OPENSSL_NO_RSA
-       case EVP_PKEY_RSA:
-               if ((ret->pkey.rsa=d2i_RSAPrivateKey(NULL,
-                       (const unsigned char **)pp,length)) == NULL) /* TMP UGLY CAST */
-                       {
-                       ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
-                       goto err;
-                       }
-               break;
-#endif
-#ifndef OPENSSL_NO_DSA
-       case EVP_PKEY_DSA:
-               if ((ret->pkey.dsa=d2i_DSAPrivateKey(NULL,
-                       (const unsigned char **)pp,length)) == NULL) /* TMP UGLY CAST */
+               ret= *a;
+               if (ret->engine)
                        {
-                       ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
-                       goto err;
+                       ENGINE_finish(ret->engine);
+                       ret->engine = NULL;
                        }
-               break;
-#endif
-#ifndef OPENSSL_NO_EC
-       case EVP_PKEY_EC:
-               if ((ret->pkey.ec = d2i_ECPrivateKey(NULL, 
-                       (const unsigned char **)pp, length)) == NULL)
-                       {
-                       ASN1err(ASN1_F_D2I_PRIVATEKEY, ERR_R_ASN1_LIB);
-                       goto err;
-                       }
-               break;
-#endif
-       default:
+               }
+
+       if (!EVP_PKEY_set_type(ret, type))
+               {
                ASN1err(ASN1_F_D2I_PRIVATEKEY,ASN1_R_UNKNOWN_PUBLIC_KEY_TYPE);
                goto err;
-               /* break; */
+               }
+
+       if (!ret->ameth->old_priv_decode ||
+                       !ret->ameth->old_priv_decode(ret, pp, length))
+               {
+               ASN1err(ASN1_F_D2I_PRIVATEKEY,ERR_R_ASN1_LIB);
+               goto err;
                }
        if (a != NULL) (*a)=ret;
        return(ret);