From 07807526c6741989fcfa697d539db2f2468307c0 Mon Sep 17 00:00:00 2001 From: TakayukiMatsuo Date: Wed, 17 Mar 2021 05:47:45 +0900 Subject: [PATCH] Change the following functions to behave the same as opeSSL: - EVP_CIPHER_CTX_cleanup - BIO_free - EVP_PKEY_cmp --- src/ssl.c | 3 ++- tests/api.c | 21 ++++++++++++++++++++- wolfcrypt/src/evp.c | 32 ++++++++++++++++++++++++-------- wolfssl/ssl.h | 1 + 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index 4fb9ebdb0..3ec3512f9 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -16286,8 +16286,9 @@ int wolfSSL_set_compression(WOLFSSL* ssl) } XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL); + return WOLFSSL_SUCCESS; } - return 1; + return WOLFSSL_FAILURE; } /* like BIO_free, but no return value */ diff --git a/tests/api.c b/tests/api.c index 37cdc2bd4..9f492a6e2 100644 --- a/tests/api.c +++ b/tests/api.c @@ -37690,6 +37690,7 @@ static void test_EVP_PKEY_cmp(void) EVP_PKEY *a, *b; const unsigned char *in; + printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()"); #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) in = client_key_der_2048; AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, @@ -37699,7 +37700,11 @@ static void test_EVP_PKEY_cmp(void) &in, (long)sizeof_client_key_der_2048)); /* Test success case RSA */ +#if defined(WOLFSSL_ERROR_CODE_OPENSSL) + AssertIntEQ(EVP_PKEY_cmp(a, b), 1); +#else AssertIntEQ(EVP_PKEY_cmp(a, b), 0); +#endif /* WOLFSSL_ERROR_CODE_OPENSSL */ EVP_PKEY_free(b); EVP_PKEY_free(a); @@ -37714,7 +37719,11 @@ static void test_EVP_PKEY_cmp(void) &in, (long)sizeof_ecc_clikey_der_256)); /* Test success case ECC */ +#if defined(WOLFSSL_ERROR_CODE_OPENSSL) + AssertIntEQ(EVP_PKEY_cmp(a, b), 1); +#else AssertIntEQ(EVP_PKEY_cmp(a, b), 0); +#endif /* WOLFSSL_ERROR_CODE_OPENSSL */ EVP_PKEY_free(b); EVP_PKEY_free(a); @@ -37731,8 +37740,11 @@ static void test_EVP_PKEY_cmp(void) AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &in, (long)sizeof_ecc_clikey_der_256)); +#if defined(WOLFSSL_ERROR_CODE_OPENSSL) + AssertIntEQ(EVP_PKEY_cmp(a, b), -1); +#else AssertIntNE(EVP_PKEY_cmp(a, b), 0); - +#endif /* WOLFSSL_ERROR_CODE_OPENSSL */ EVP_PKEY_free(b); EVP_PKEY_free(a); #endif @@ -37740,10 +37752,17 @@ static void test_EVP_PKEY_cmp(void) /* invalid or empty failure cases */ a = EVP_PKEY_new(); b = EVP_PKEY_new(); +#if defined(WOLFSSL_ERROR_CODE_OPENSSL) + AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0); + AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0); + AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0); + AssertIntEQ(EVP_PKEY_cmp(a, b), 0); +#else AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0); AssertIntNE(EVP_PKEY_cmp(a, NULL), 0); AssertIntNE(EVP_PKEY_cmp(NULL, b), 0); AssertIntNE(EVP_PKEY_cmp(a, b), 0); +#endif EVP_PKEY_free(b); EVP_PKEY_free(a); diff --git a/wolfcrypt/src/evp.c b/wolfcrypt/src/evp.c index 75c165c42..c97169a4d 100644 --- a/wolfcrypt/src/evp.c +++ b/wolfcrypt/src/evp.c @@ -1975,17 +1975,29 @@ WOLFSSL_API int wolfSSL_EVP_PKEY_missing_parameters(WOLFSSL_EVP_PKEY *pkey) } #endif +/* wolfSSL_EVP_PKEY_cmp + * returns 0 on success, -1 on failure. + * + * This behavior is different from openssl. + * EVP_PKEY_cmp returns: + * 1 : two keys match + * 0 : do not match + * -1: key types are different + * -2: the operation is not supported + * If you mant this function behave the same as openSSL, + * define WOLFSSL_ERROR_CODE_OPENSSL so that WS_RETURN_CODE fills the gap. + */ WOLFSSL_API int wolfSSL_EVP_PKEY_cmp(const WOLFSSL_EVP_PKEY *a, const WOLFSSL_EVP_PKEY *b) { int ret = -1; /* failure */ int a_sz = 0, b_sz = 0; if (a == NULL || b == NULL) - return ret; + return WS_RETURN_CODE(ret, WOLFSSL_FAILURE); /* check its the same type of key */ if (a->type != b->type) - return ret; + return WS_RETURN_CODE(ret, -1); /* get size based on key type */ switch (a->type) { @@ -2006,27 +2018,30 @@ WOLFSSL_API int wolfSSL_EVP_PKEY_cmp(const WOLFSSL_EVP_PKEY *a, const WOLFSSL_EV break; #endif /* HAVE_ECC */ default: - return ret; + return WS_RETURN_CODE(ret, -2); } /* switch (a->type) */ /* check size */ if (a_sz <= 0 || b_sz <= 0 || a_sz != b_sz) { - return ret; + return WS_RETURN_CODE(ret, WOLFSSL_FAILURE); } /* check public key size */ if (a->pkey_sz > 0 && b->pkey_sz > 0 && a->pkey_sz != b->pkey_sz) { - return ret; + return WS_RETURN_CODE(ret, WOLFSSL_FAILURE); } /* check public key */ if (a->pkey.ptr && b->pkey.ptr) { if (XMEMCMP(a->pkey.ptr, b->pkey.ptr, a->pkey_sz) != 0) { - return ret; + return WS_RETURN_CODE(ret, WOLFSSL_FAILURE); } } +#if defined(WOLFSSL_ERROR_CODE_OPENSSL) + ret = 1; /* the keys match */ +#else ret = 0; /* success */ - +#endif return ret; } @@ -4124,9 +4139,10 @@ int wolfSSL_EVP_MD_type(const WOLFSSL_EVP_MD *md) } ctx->gcmAuthInSz = 0; #endif + return WOLFSSL_SUCCESS; } - return WOLFSSL_SUCCESS; + return WOLFSSL_FAILURE; } /* Permanent stub for Qt compilation. */ diff --git a/wolfssl/ssl.h b/wolfssl/ssl.h index a6697e8bc..1397fb5f6 100644 --- a/wolfssl/ssl.h +++ b/wolfssl/ssl.h @@ -696,6 +696,7 @@ enum AlertLevel { * Since wolfSSL 4.7.0, the following functions use this macro: * - wolfSSL_CTX_load_verify_locations * - wolfSSL_X509_LOOKUP_load_file + * - wolfSSL_EVP_PKEY_cmp */ #if defined(WOLFSSL_ERROR_CODE_OPENSSL) #define WS_RETURN_CODE(item1,item2) \