Flag to disable automatic copying of contexts.
[openssl.git] / crypto / evp / p_verify.c
index 21a40a375e1b71b6e3a2c17bd0a8ee840cde1ec2..a3df5dfc3d11b6d51784265a2a289b591bd339ab 100644 (file)
@@ -67,8 +67,42 @@ int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf,
        {
        unsigned char m[EVP_MAX_MD_SIZE];
        unsigned int m_len;
-       int i,ok=0,v;
-       MS_STATIC EVP_MD_CTX tmp_ctx;
+       int i = 0,ok = 0,v;
+       EVP_PKEY_CTX *pkctx = NULL;
+
+       if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE)
+               {
+               if (!EVP_DigestFinal_ex(ctx, m, &m_len))
+                       goto err;
+               }
+       else
+               {
+               int rv;
+               EVP_MD_CTX tmp_ctx;
+               EVP_MD_CTX_init(&tmp_ctx);
+               rv = EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);
+               if (rv)
+                       rv = EVP_DigestFinal_ex(&tmp_ctx, m, &m_len);
+               EVP_MD_CTX_cleanup(&tmp_ctx);
+               if (!rv)
+                       return 0;
+               }
+
+       if (ctx->digest->flags & EVP_MD_FLAG_PKEY_METHOD_SIGNATURE)
+               {
+               i = -1;
+               pkctx = EVP_PKEY_CTX_new(pkey, NULL);
+               if (!pkctx)
+                       goto err;
+               if (EVP_PKEY_verify_init(pkctx) <= 0)
+                       goto err;
+               if (EVP_PKEY_CTX_set_signature_md(pkctx, ctx->digest) <= 0)
+                       goto err;
+               i = EVP_PKEY_verify(pkctx, sigbuf, siglen, m, m_len);
+               err:
+               EVP_PKEY_CTX_free(pkctx);
+               return i;
+               }
 
        for (i=0; i<4; i++)
                {
@@ -85,10 +119,6 @@ int EVP_VerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sigbuf,
                EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_WRONG_PUBLIC_KEY_TYPE);
                return(-1);
                }
-       EVP_MD_CTX_init(&tmp_ctx);
-       EVP_MD_CTX_copy_ex(&tmp_ctx,ctx);     
-       EVP_DigestFinal_ex(&tmp_ctx,&(m[0]),&m_len);
-       EVP_MD_CTX_cleanup(&tmp_ctx);
         if (ctx->digest->verify == NULL)
                 {
                EVPerr(EVP_F_EVP_VERIFYFINAL,EVP_R_NO_VERIFY_FUNCTION_CONFIGURED);