tolerate broken CMS/PKCS7 implementations using signature OID instead of digest
[openssl.git] / crypto / evp / pmeth_lib.c
index 500aad967b896578cde12c0f8c85872fed8e23af..4a05f0b1359d59c699f7c76bedae828d47220831 100644 (file)
@@ -1,5 +1,5 @@
 /* pmeth_lib.c */
-/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
+/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  * project 2006.
  */
 /* ====================================================================
 #include "evp_locl.h"
 
 typedef int sk_cmp_fn_type(const char * const *a, const char * const *b);
-STACK *app_pkey_methods = NULL;
 
-extern EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth, ec_pkey_meth;
+DECLARE_STACK_OF(EVP_PKEY_METHOD)
+STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL;
+
+extern const EVP_PKEY_METHOD rsa_pkey_meth, dh_pkey_meth, dsa_pkey_meth;
+extern const EVP_PKEY_METHOD ec_pkey_meth, hmac_pkey_meth;
 
 static const EVP_PKEY_METHOD *standard_methods[] =
        {
+#ifndef OPENSSL_NO_RSA
        &rsa_pkey_meth,
+#endif
+#ifndef OPENSSL_NO_DH
        &dh_pkey_meth,
+#endif
+#ifndef OPENSSL_NO_DSA
        &dsa_pkey_meth,
-       &ec_pkey_meth
+#endif
+#ifndef OPENSSL_NO_EC
+       &ec_pkey_meth,
+#endif
+       &hmac_pkey_meth,
        };
 
+DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
+                          pmeth);
+
 static int pmeth_cmp(const EVP_PKEY_METHOD * const *a,
-                const EVP_PKEY_METHOD * const *b)
+                    const EVP_PKEY_METHOD * const *b)
        {
         return ((*a)->pkey_id - (*b)->pkey_id);
        }
 
+IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, const EVP_PKEY_METHOD *,
+                            pmeth);
+
 const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type)
        {
-       EVP_PKEY_METHOD tmp, *t = &tmp, **ret;
+       EVP_PKEY_METHOD tmp;
+       const EVP_PKEY_METHOD *t = &tmp, **ret;
        tmp.pkey_id = type;
        if (app_pkey_methods)
                {
                int idx;
-               idx = sk_find(app_pkey_methods, (char *)&tmp);
+               idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp);
                if (idx >= 0)
-                       return (EVP_PKEY_METHOD *)
-                               sk_value(app_pkey_methods, idx);
+                       return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
                }
-       ret = (EVP_PKEY_METHOD **) OBJ_bsearch((char *)&t,
-                       (char *)standard_methods,
-                       sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *),
-                       sizeof(EVP_PKEY_METHOD *),
-                       (int (*)(const void *, const void *))pmeth_cmp);
+       ret = OBJ_bsearch_pmeth(&t, standard_methods,
+                         sizeof(standard_methods)/sizeof(EVP_PKEY_METHOD *));
        if (!ret || !*ret)
                return NULL;
        return *ret;
@@ -118,6 +133,7 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
                        return NULL;
                id = pkey->ameth->pkey_id;
                }
+#ifndef OPENSSL_NO_ENGINE
        /* Try to find an ENGINE which implements this method */
        if (e)
                {
@@ -137,6 +153,7 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
        if (e)
                pmeth = ENGINE_get_pkey_meth(e, id);
        else
+#endif
                pmeth = EVP_PKEY_meth_find(id);
 
        if (pmeth == NULL)
@@ -148,8 +165,10 @@ static EVP_PKEY_CTX *int_ctx_new(EVP_PKEY *pkey, ENGINE *e, int id)
        ret = OPENSSL_malloc(sizeof(EVP_PKEY_CTX));
        if (!ret)
                {
+#ifndef OPENSSL_NO_ENGINE
                if (e)
                        ENGINE_finish(e);
+#endif
                EVPerr(EVP_F_INT_CTX_NEW,ERR_R_MALLOC_FAILURE);
                return NULL;
                }
@@ -277,13 +296,13 @@ int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
        {
        if (app_pkey_methods == NULL)
                {
-               app_pkey_methods = sk_new((sk_cmp_fn_type *)pmeth_cmp);
+               app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
                if (!app_pkey_methods)
                        return 0;
                }
-       if (!sk_push(app_pkey_methods, (char *)pmeth))
+       if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth))
                return 0;
-       sk_sort(app_pkey_methods);
+       sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
        return 1;
        }
 
@@ -388,6 +407,11 @@ EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
        return ctx->pkey;
        }
 
+EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
+       {
+       return ctx->peerkey;
+       }
+       
 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
        {
        ctx->app_data = data;
@@ -480,11 +504,11 @@ void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
 
 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
        int (*encrypt_init)(EVP_PKEY_CTX *ctx),
-       int (*encrypt)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
+       int (*encryptfn)(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,
                                        const unsigned char *in, size_t inlen))
        {
        pmeth->encrypt_init = encrypt_init;
-       pmeth->encrypt = encrypt;
+       pmeth->encrypt = encryptfn;
        }
 
 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,