Blame SOURCES/Fix-softpkcs11-build-issues-with-openssl-3.0.patch

cb4cef
From f85a818fe1a7438db7e1ea579818da67e0be017d Mon Sep 17 00:00:00 2001
cb4cef
From: Robbie Harwood <rharwood@redhat.com>
cb4cef
Date: Sat, 15 May 2021 17:35:25 -0400
cb4cef
Subject: [PATCH] Fix softpkcs11 build issues with openssl 3.0
cb4cef
cb4cef
EVP_PKEY_get0_RSA() has been modified to have const return type.  Remove
cb4cef
its usages in favor of the EVP_PKEY interface.  Also remove calls to
cb4cef
RSA_blinding_off(), which we don't need and would require a non-const
cb4cef
object.  Similarly, remove RSA_set_method() calls that set a pre-existing
cb4cef
default.
cb4cef
cb4cef
Since softpkcs11 doesn't link against krb5 and can't use zap(), allocate
cb4cef
buffers with OPENSSL_malloc() so can use OPENSSL_clear_free().
cb4cef
cb4cef
Move several argument validation checks to the top of their functions.
cb4cef
cb4cef
Fix some incorrect/inconsistent log messages.
cb4cef
cb4cef
(cherry picked from commit 00de1aad7b3647b91017c7009b0bc65cd0c8b2e0)
cb4cef
(cherry picked from commit a86b780ef275b35e8dc1e6d1886ec8e8d941f7c4)
cb4cef
---
cb4cef
 src/tests/softpkcs11/main.c | 360 ++++++++++++++----------------------
cb4cef
 1 file changed, 141 insertions(+), 219 deletions(-)
cb4cef
cb4cef
diff --git a/src/tests/softpkcs11/main.c b/src/tests/softpkcs11/main.c
cb4cef
index 1cccdfb43..caa537b68 100644
cb4cef
--- a/src/tests/softpkcs11/main.c
cb4cef
+++ b/src/tests/softpkcs11/main.c
cb4cef
@@ -375,10 +375,9 @@ add_st_object(void)
cb4cef
         return NULL;
cb4cef
     soft_token.object.objs = objs;
cb4cef
 
cb4cef
-    o = malloc(sizeof(*o));
cb4cef
+    o = calloc(1, sizeof(*o));
cb4cef
     if (o == NULL)
cb4cef
         return NULL;
cb4cef
-    memset(o, 0, sizeof(*o));
cb4cef
     o->attrs = NULL;
cb4cef
     o->num_attributes = 0;
cb4cef
     o->object_handle = soft_token.object.num_objs;
cb4cef
@@ -424,7 +423,7 @@ add_pubkey_info(struct st_object *o, CK_KEY_TYPE key_type, EVP_PKEY *key)
cb4cef
         CK_ULONG modulus_bits = 0;
cb4cef
         CK_BYTE *exponent = NULL;
cb4cef
         size_t exponent_len = 0;
cb4cef
-        RSA *rsa;
cb4cef
+        const RSA *rsa;
cb4cef
         const BIGNUM *n, *e;
cb4cef
 
cb4cef
         rsa = EVP_PKEY_get0_RSA(key);
cb4cef
@@ -445,8 +444,6 @@ add_pubkey_info(struct st_object *o, CK_KEY_TYPE key_type, EVP_PKEY *key)
cb4cef
         add_object_attribute(o, 0, CKA_PUBLIC_EXPONENT,
cb4cef
                              exponent, exponent_len);
cb4cef
 
cb4cef
-        RSA_set_method(rsa, RSA_PKCS1_OpenSSL());
cb4cef
-
cb4cef
         free(modulus);
cb4cef
         free(exponent);
cb4cef
     }
cb4cef
@@ -679,10 +676,6 @@ add_certificate(char *label,
cb4cef
         } else {
cb4cef
             /* XXX verify keytype */
cb4cef
 
cb4cef
-            if (key_type == CKK_RSA)
cb4cef
-                RSA_set_method(EVP_PKEY_get0_RSA(o->u.private_key.key),
cb4cef
-                               RSA_PKCS1_OpenSSL());
cb4cef
-
cb4cef
             if (X509_check_private_key(cert, o->u.private_key.key) != 1) {
cb4cef
                 EVP_PKEY_free(o->u.private_key.key);
cb4cef
                 o->u.private_key.key = NULL;
cb4cef
@@ -695,7 +688,7 @@ add_certificate(char *label,
cb4cef
     }
cb4cef
 
cb4cef
     ret = CKR_OK;
cb4cef
- out:
cb4cef
+out:
cb4cef
     if (ret != CKR_OK) {
cb4cef
         st_logf("something went wrong when adding cert!\n");
cb4cef
 
cb4cef
@@ -1224,8 +1217,6 @@ C_Login(CK_SESSION_HANDLE hSession,
cb4cef
         }
cb4cef
 
cb4cef
         /* XXX check keytype */
cb4cef
-        RSA_set_method(EVP_PKEY_get0_RSA(o->u.private_key.key),
cb4cef
-                       RSA_PKCS1_OpenSSL());
cb4cef
 
cb4cef
         if (X509_check_private_key(o->u.private_key.cert, o->u.private_key.key) != 1) {
cb4cef
             EVP_PKEY_free(o->u.private_key.key);
cb4cef
@@ -1495,8 +1486,9 @@ C_Encrypt(CK_SESSION_HANDLE hSession,
cb4cef
     struct st_object *o;
cb4cef
     void *buffer = NULL;
cb4cef
     CK_RV ret;
cb4cef
-    RSA *rsa;
cb4cef
-    int padding, len, buffer_len, padding_len;
cb4cef
+    size_t buffer_len = 0;
cb4cef
+    int padding;
cb4cef
+    EVP_PKEY_CTX *ctx = NULL;
cb4cef
 
cb4cef
     st_logf("Encrypt\n");
cb4cef
 
cb4cef
@@ -1512,70 +1504,58 @@ C_Encrypt(CK_SESSION_HANDLE hSession,
cb4cef
         return CKR_ARGUMENTS_BAD;
cb4cef
     }
cb4cef
 
cb4cef
-    rsa = EVP_PKEY_get0_RSA(o->u.public_key);
cb4cef
-
cb4cef
-    if (rsa == NULL)
cb4cef
-        return CKR_ARGUMENTS_BAD;
cb4cef
-
cb4cef
-    RSA_blinding_off(rsa); /* XXX RAND is broken while running in mozilla ? */
cb4cef
-
cb4cef
-    buffer_len = RSA_size(rsa);
cb4cef
-
cb4cef
-    buffer = malloc(buffer_len);
cb4cef
-    if (buffer == NULL) {
cb4cef
-        ret = CKR_DEVICE_MEMORY;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    ret = CKR_OK;
cb4cef
-    switch(state->encrypt_mechanism->mechanism) {
cb4cef
-    case CKM_RSA_PKCS:
cb4cef
-        padding = RSA_PKCS1_PADDING;
cb4cef
-        padding_len = RSA_PKCS1_PADDING_SIZE;
cb4cef
-        break;
cb4cef
-    case CKM_RSA_X_509:
cb4cef
-        padding = RSA_NO_PADDING;
cb4cef
-        padding_len = 0;
cb4cef
-        break;
cb4cef
-    default:
cb4cef
-        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    if ((CK_ULONG)buffer_len + padding_len < ulDataLen) {
cb4cef
-        ret = CKR_ARGUMENTS_BAD;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
     if (pulEncryptedDataLen == NULL) {
cb4cef
         st_logf("pulEncryptedDataLen NULL\n");
cb4cef
         ret = CKR_ARGUMENTS_BAD;
cb4cef
         goto out;
cb4cef
     }
cb4cef
 
cb4cef
-    if (pData == NULL_PTR) {
cb4cef
+    if (pData == NULL) {
cb4cef
         st_logf("data NULL\n");
cb4cef
         ret = CKR_ARGUMENTS_BAD;
cb4cef
         goto out;
cb4cef
     }
cb4cef
 
cb4cef
-    len = RSA_public_encrypt(ulDataLen, pData, buffer, rsa, padding);
cb4cef
-    if (len <= 0) {
cb4cef
+    switch(state->encrypt_mechanism->mechanism) {
cb4cef
+    case CKM_RSA_PKCS:
cb4cef
+        padding = RSA_PKCS1_PADDING;
cb4cef
+        break;
cb4cef
+    case CKM_RSA_X_509:
cb4cef
+        padding = RSA_NO_PADDING;
cb4cef
+        break;
cb4cef
+    default:
cb4cef
+        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+
cb4cef
+    ctx = EVP_PKEY_CTX_new(o->u.public_key, NULL);
cb4cef
+    if (ctx == NULL || EVP_PKEY_encrypt_init(ctx) <= 0 ||
cb4cef
+        EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0 ||
cb4cef
+        EVP_PKEY_encrypt(ctx, NULL, &buffer_len, pData, ulDataLen) <= 0) {
cb4cef
         ret = CKR_DEVICE_ERROR;
cb4cef
         goto out;
cb4cef
     }
cb4cef
-    if (len > buffer_len)
cb4cef
-        abort();
cb4cef
 
cb4cef
-    if (pEncryptedData != NULL_PTR)
cb4cef
-        memcpy(pEncryptedData, buffer, len);
cb4cef
-    *pulEncryptedDataLen = len;
cb4cef
-
cb4cef
- out:
cb4cef
-    if (buffer) {
cb4cef
-        memset(buffer, 0, buffer_len);
cb4cef
-        free(buffer);
cb4cef
+    buffer = OPENSSL_malloc(buffer_len);
cb4cef
+    if (buffer == NULL) {
cb4cef
+        ret = CKR_DEVICE_MEMORY;
cb4cef
+        goto out;
cb4cef
     }
cb4cef
+
cb4cef
+    if (EVP_PKEY_encrypt(ctx, buffer, &buffer_len, pData, ulDataLen) <= 0) {
cb4cef
+        ret = CKR_DEVICE_ERROR;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+    st_logf("Encrypt done\n");
cb4cef
+
cb4cef
+    if (pEncryptedData != NULL)
cb4cef
+        memcpy(pEncryptedData, buffer, buffer_len);
cb4cef
+    *pulEncryptedDataLen = buffer_len;
cb4cef
+
cb4cef
+    ret = CKR_OK;
cb4cef
+out:
cb4cef
+    OPENSSL_clear_free(buffer, buffer_len);
cb4cef
+    EVP_PKEY_CTX_free(ctx);
cb4cef
     return ret;
cb4cef
 }
cb4cef
 
cb4cef
@@ -1646,8 +1626,9 @@ C_Decrypt(CK_SESSION_HANDLE hSession,
cb4cef
     struct st_object *o;
cb4cef
     void *buffer = NULL;
cb4cef
     CK_RV ret;
cb4cef
-    RSA *rsa;
cb4cef
-    int padding, len, buffer_len, padding_len;
cb4cef
+    size_t buffer_len = 0;
cb4cef
+    int padding;
cb4cef
+    EVP_PKEY_CTX *ctx = NULL;
cb4cef
 
cb4cef
     st_logf("Decrypt\n");
cb4cef
 
cb4cef
@@ -1663,41 +1644,6 @@ C_Decrypt(CK_SESSION_HANDLE hSession,
cb4cef
         return CKR_ARGUMENTS_BAD;
cb4cef
     }
cb4cef
 
cb4cef
-    rsa = EVP_PKEY_get0_RSA(o->u.private_key.key);
cb4cef
-
cb4cef
-    if (rsa == NULL)
cb4cef
-        return CKR_ARGUMENTS_BAD;
cb4cef
-
cb4cef
-    RSA_blinding_off(rsa); /* XXX RAND is broken while running in mozilla ? */
cb4cef
-
cb4cef
-    buffer_len = RSA_size(rsa);
cb4cef
-
cb4cef
-    buffer = malloc(buffer_len);
cb4cef
-    if (buffer == NULL) {
cb4cef
-        ret = CKR_DEVICE_MEMORY;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    ret = CKR_OK;
cb4cef
-    switch(state->decrypt_mechanism->mechanism) {
cb4cef
-    case CKM_RSA_PKCS:
cb4cef
-        padding = RSA_PKCS1_PADDING;
cb4cef
-        padding_len = RSA_PKCS1_PADDING_SIZE;
cb4cef
-        break;
cb4cef
-    case CKM_RSA_X_509:
cb4cef
-        padding = RSA_NO_PADDING;
cb4cef
-        padding_len = 0;
cb4cef
-        break;
cb4cef
-    default:
cb4cef
-        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    if ((CK_ULONG)buffer_len + padding_len < ulEncryptedDataLen) {
cb4cef
-        ret = CKR_ARGUMENTS_BAD;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
     if (pulDataLen == NULL) {
cb4cef
         st_logf("pulDataLen NULL\n");
cb4cef
         ret = CKR_ARGUMENTS_BAD;
cb4cef
@@ -1710,24 +1656,48 @@ C_Decrypt(CK_SESSION_HANDLE hSession,
cb4cef
         goto out;
cb4cef
     }
cb4cef
 
cb4cef
-    len = RSA_private_decrypt(ulEncryptedDataLen, pEncryptedData, buffer,
cb4cef
-                              rsa, padding);
cb4cef
-    if (len <= 0) {
cb4cef
+    switch(state->decrypt_mechanism->mechanism) {
cb4cef
+    case CKM_RSA_PKCS:
cb4cef
+        padding = RSA_PKCS1_PADDING;
cb4cef
+        break;
cb4cef
+    case CKM_RSA_X_509:
cb4cef
+        padding = RSA_NO_PADDING;
cb4cef
+        break;
cb4cef
+    default:
cb4cef
+        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+
cb4cef
+    ctx = EVP_PKEY_CTX_new(o->u.private_key.key, NULL);
cb4cef
+    if (ctx == NULL || EVP_PKEY_decrypt_init(ctx) <= 0 ||
cb4cef
+        EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0 ||
cb4cef
+        EVP_PKEY_decrypt(ctx, NULL, &buffer_len, pEncryptedData,
cb4cef
+                         ulEncryptedDataLen) <= 0) {
cb4cef
         ret = CKR_DEVICE_ERROR;
cb4cef
         goto out;
cb4cef
     }
cb4cef
-    if (len > buffer_len)
cb4cef
-        abort();
cb4cef
+
cb4cef
+    buffer = OPENSSL_malloc(buffer_len);
cb4cef
+    if (buffer == NULL) {
cb4cef
+        ret = CKR_DEVICE_MEMORY;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+
cb4cef
+    if (EVP_PKEY_decrypt(ctx, buffer, &buffer_len, pEncryptedData,
cb4cef
+                         ulEncryptedDataLen) <= 0) {
cb4cef
+        ret = CKR_DEVICE_ERROR;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+    st_logf("Decrypt done\n");
cb4cef
 
cb4cef
     if (pData != NULL_PTR)
cb4cef
-        memcpy(pData, buffer, len);
cb4cef
-    *pulDataLen = len;
cb4cef
+        memcpy(pData, buffer, buffer_len);
cb4cef
+    *pulDataLen = buffer_len;
cb4cef
 
cb4cef
- out:
cb4cef
-    if (buffer) {
cb4cef
-        memset(buffer, 0, buffer_len);
cb4cef
-        free(buffer);
cb4cef
-    }
cb4cef
+    ret = CKR_OK;
cb4cef
+out:
cb4cef
+    OPENSSL_clear_free(buffer, buffer_len);
cb4cef
+    EVP_PKEY_CTX_free(ctx);
cb4cef
     return ret;
cb4cef
 }
cb4cef
 
cb4cef
@@ -1806,8 +1776,9 @@ C_Sign(CK_SESSION_HANDLE hSession,
cb4cef
     struct st_object *o;
cb4cef
     void *buffer = NULL;
cb4cef
     CK_RV ret;
cb4cef
-    RSA *rsa;
cb4cef
-    int padding, len, buffer_len, padding_len;
cb4cef
+    int padding;
cb4cef
+    size_t buffer_len = 0;
cb4cef
+    EVP_PKEY_CTX *ctx = NULL;
cb4cef
 
cb4cef
     st_logf("Sign\n");
cb4cef
     VERIFY_SESSION_HANDLE(hSession, &state);
cb4cef
@@ -1822,40 +1793,6 @@ C_Sign(CK_SESSION_HANDLE hSession,
cb4cef
         return CKR_ARGUMENTS_BAD;
cb4cef
     }
cb4cef
 
cb4cef
-    rsa = EVP_PKEY_get0_RSA(o->u.private_key.key);
cb4cef
-
cb4cef
-    if (rsa == NULL)
cb4cef
-        return CKR_ARGUMENTS_BAD;
cb4cef
-
cb4cef
-    RSA_blinding_off(rsa); /* XXX RAND is broken while running in mozilla ? */
cb4cef
-
cb4cef
-    buffer_len = RSA_size(rsa);
cb4cef
-
cb4cef
-    buffer = malloc(buffer_len);
cb4cef
-    if (buffer == NULL) {
cb4cef
-        ret = CKR_DEVICE_MEMORY;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    switch(state->sign_mechanism->mechanism) {
cb4cef
-    case CKM_RSA_PKCS:
cb4cef
-        padding = RSA_PKCS1_PADDING;
cb4cef
-        padding_len = RSA_PKCS1_PADDING_SIZE;
cb4cef
-        break;
cb4cef
-    case CKM_RSA_X_509:
cb4cef
-        padding = RSA_NO_PADDING;
cb4cef
-        padding_len = 0;
cb4cef
-        break;
cb4cef
-    default:
cb4cef
-        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    if ((CK_ULONG)buffer_len < ulDataLen + padding_len) {
cb4cef
-        ret = CKR_ARGUMENTS_BAD;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
     if (pulSignatureLen == NULL) {
cb4cef
         st_logf("signature len NULL\n");
cb4cef
         ret = CKR_ARGUMENTS_BAD;
cb4cef
@@ -1868,26 +1805,46 @@ C_Sign(CK_SESSION_HANDLE hSession,
cb4cef
         goto out;
cb4cef
     }
cb4cef
 
cb4cef
-    len = RSA_private_encrypt(ulDataLen, pData, buffer, rsa, padding);
cb4cef
-    st_logf("private encrypt done\n");
cb4cef
-    if (len <= 0) {
cb4cef
+    switch(state->sign_mechanism->mechanism) {
cb4cef
+    case CKM_RSA_PKCS:
cb4cef
+        padding = RSA_PKCS1_PADDING;
cb4cef
+        break;
cb4cef
+    case CKM_RSA_X_509:
cb4cef
+        padding = RSA_NO_PADDING;
cb4cef
+        break;
cb4cef
+    default:
cb4cef
+        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+
cb4cef
+    ctx = EVP_PKEY_CTX_new(o->u.private_key.key, NULL);
cb4cef
+    if (ctx == NULL || EVP_PKEY_sign_init(ctx) <= 0 ||
cb4cef
+        EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0 ||
cb4cef
+        EVP_PKEY_sign(ctx, NULL, &buffer_len, pData, ulDataLen) <= 0) {
cb4cef
         ret = CKR_DEVICE_ERROR;
cb4cef
         goto out;
cb4cef
     }
cb4cef
-    if (len > buffer_len)
cb4cef
-        abort();
cb4cef
 
cb4cef
-    if (pSignature != NULL_PTR)
cb4cef
-        memcpy(pSignature, buffer, len);
cb4cef
-    *pulSignatureLen = len;
cb4cef
+    buffer = OPENSSL_malloc(buffer_len);
cb4cef
+    if (buffer == NULL) {
cb4cef
+        ret = CKR_DEVICE_MEMORY;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+
cb4cef
+    if (EVP_PKEY_sign(ctx, buffer, &buffer_len, pData, ulDataLen) <= 0) {
cb4cef
+        ret = CKR_DEVICE_ERROR;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+    st_logf("Sign done\n");
cb4cef
+
cb4cef
+    if (pSignature != NULL)
cb4cef
+        memcpy(pSignature, buffer, buffer_len);
cb4cef
+    *pulSignatureLen = buffer_len;
cb4cef
 
cb4cef
     ret = CKR_OK;
cb4cef
-
cb4cef
- out:
cb4cef
-    if (buffer) {
cb4cef
-        memset(buffer, 0, buffer_len);
cb4cef
-        free(buffer);
cb4cef
-    }
cb4cef
+out:
cb4cef
+    OPENSSL_clear_free(buffer, buffer_len);
cb4cef
+    EVP_PKEY_CTX_free(ctx);
cb4cef
     return ret;
cb4cef
 }
cb4cef
 
cb4cef
@@ -1951,10 +1908,9 @@ C_Verify(CK_SESSION_HANDLE hSession,
cb4cef
 {
cb4cef
     struct session_state *state;
cb4cef
     struct st_object *o;
cb4cef
-    void *buffer = NULL;
cb4cef
     CK_RV ret;
cb4cef
-    RSA *rsa;
cb4cef
-    int padding, len, buffer_len;
cb4cef
+    int padding;
cb4cef
+    EVP_PKEY_CTX *ctx = NULL;
cb4cef
 
cb4cef
     st_logf("Verify\n");
cb4cef
     VERIFY_SESSION_HANDLE(hSession, &state);
cb4cef
@@ -1969,39 +1925,6 @@ C_Verify(CK_SESSION_HANDLE hSession,
cb4cef
         return CKR_ARGUMENTS_BAD;
cb4cef
     }
cb4cef
 
cb4cef
-    rsa = EVP_PKEY_get0_RSA(o->u.public_key);
cb4cef
-
cb4cef
-    if (rsa == NULL)
cb4cef
-        return CKR_ARGUMENTS_BAD;
cb4cef
-
cb4cef
-    RSA_blinding_off(rsa); /* XXX RAND is broken while running in mozilla ? */
cb4cef
-
cb4cef
-    buffer_len = RSA_size(rsa);
cb4cef
-
cb4cef
-    buffer = malloc(buffer_len);
cb4cef
-    if (buffer == NULL) {
cb4cef
-        ret = CKR_DEVICE_MEMORY;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    ret = CKR_OK;
cb4cef
-    switch(state->verify_mechanism->mechanism) {
cb4cef
-    case CKM_RSA_PKCS:
cb4cef
-        padding = RSA_PKCS1_PADDING;
cb4cef
-        break;
cb4cef
-    case CKM_RSA_X_509:
cb4cef
-        padding = RSA_NO_PADDING;
cb4cef
-        break;
cb4cef
-    default:
cb4cef
-        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    if ((CK_ULONG)buffer_len < ulDataLen) {
cb4cef
-        ret = CKR_ARGUMENTS_BAD;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
     if (pSignature == NULL) {
cb4cef
         st_logf("signature NULL\n");
cb4cef
         ret = CKR_ARGUMENTS_BAD;
cb4cef
@@ -2014,34 +1937,34 @@ C_Verify(CK_SESSION_HANDLE hSession,
cb4cef
         goto out;
cb4cef
     }
cb4cef
 
cb4cef
-    len = RSA_public_decrypt(ulDataLen, pData, buffer, rsa, padding);
cb4cef
-    st_logf("private encrypt done\n");
cb4cef
-    if (len <= 0) {
cb4cef
+    switch(state->verify_mechanism->mechanism) {
cb4cef
+    case CKM_RSA_PKCS:
cb4cef
+        padding = RSA_PKCS1_PADDING;
cb4cef
+        break;
cb4cef
+    case CKM_RSA_X_509:
cb4cef
+        padding = RSA_NO_PADDING;
cb4cef
+        break;
cb4cef
+    default:
cb4cef
+        ret = CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
+        goto out;
cb4cef
+    }
cb4cef
+
cb4cef
+    ctx = EVP_PKEY_CTX_new(o->u.public_key, NULL);
cb4cef
+    if (ctx == NULL || EVP_PKEY_verify_init(ctx) <= 0 ||
cb4cef
+        EVP_PKEY_CTX_set_rsa_padding(ctx, padding) <= 0 ||
cb4cef
+        EVP_PKEY_verify(ctx, pSignature, ulSignatureLen, pData,
cb4cef
+                        ulDataLen) <= 0) {
cb4cef
         ret = CKR_DEVICE_ERROR;
cb4cef
         goto out;
cb4cef
     }
cb4cef
-    if (len > buffer_len)
cb4cef
-        abort();
cb4cef
+    st_logf("Verify done\n");
cb4cef
 
cb4cef
-    if ((CK_ULONG)len != ulSignatureLen) {
cb4cef
-        ret = CKR_GENERAL_ERROR;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
-    if (memcmp(pSignature, buffer, len) != 0) {
cb4cef
-        ret = CKR_GENERAL_ERROR;
cb4cef
-        goto out;
cb4cef
-    }
cb4cef
-
cb4cef
- out:
cb4cef
-    if (buffer) {
cb4cef
-        memset(buffer, 0, buffer_len);
cb4cef
-        free(buffer);
cb4cef
-    }
cb4cef
+    ret = CKR_OK;
cb4cef
+out:
cb4cef
+    EVP_PKEY_CTX_free(ctx);
cb4cef
     return ret;
cb4cef
 }
cb4cef
 
cb4cef
-
cb4cef
 CK_RV
cb4cef
 C_VerifyUpdate(CK_SESSION_HANDLE hSession,
cb4cef
                CK_BYTE_PTR pPart,
cb4cef
@@ -2072,7 +1995,6 @@ C_GenerateRandom(CK_SESSION_HANDLE hSession,
cb4cef
     return CKR_FUNCTION_NOT_SUPPORTED;
cb4cef
 }
cb4cef
 
cb4cef
-
cb4cef
 CK_FUNCTION_LIST funcs = {
cb4cef
     { 2, 11 },
cb4cef
     C_Initialize,