From 4f6e47f989a654f54367044d8726ec0b4a10b189 Mon Sep 17 00:00:00 2001 From: Ankit Shah Date: Thu, 13 Jul 2017 10:06:49 -0700 Subject: [PATCH] Getters and setters for ECDSA_SIG Summary: Added getters and setters for ECDSA_SIG to allow for compatibility between OpenSSL 1.1.0 and 1.0.2 Reviewed By: knekritz Differential Revision: D5408934 fbshipit-source-id: 7a3d9df774728c81270cc4da34c75202018e430d --- folly/portability/OpenSSL.cpp | 24 +++++++++++++++++++ folly/portability/OpenSSL.h | 2 ++ .../test/OpenSSLPortabilityTest.cpp | 16 +++++++++++++ 3 files changed, 42 insertions(+) diff --git a/folly/portability/OpenSSL.cpp b/folly/portability/OpenSSL.cpp index 492b8306..f0dc9d72 100644 --- a/folly/portability/OpenSSL.cpp +++ b/folly/portability/OpenSSL.cpp @@ -333,6 +333,30 @@ void RSA_get0_crt_params( } } +int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s) { + // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes + if (r == nullptr || s == nullptr) { + return 0; + } + BN_clear_free(sig->r); + BN_clear_free(sig->s); + sig->r = r; + sig->s = s; + return 1; +} + +void ECDSA_SIG_get0( + const ECDSA_SIG* sig, + const BIGNUM** pr, + const BIGNUM** ps) { + // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes + if (pr != nullptr) { + *pr = sig->r; + } + if (ps != nullptr) { + *ps = sig->s; + } +} #endif } } diff --git a/folly/portability/OpenSSL.h b/folly/portability/OpenSSL.h index 5d6da778..113a9175 100644 --- a/folly/portability/OpenSSL.h +++ b/folly/portability/OpenSSL.h @@ -164,6 +164,8 @@ void RSA_get0_crt_params( const BIGNUM** dmp1, const BIGNUM** dmq1, const BIGNUM** iqmp); +int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s); +void ECDSA_SIG_get0(const ECDSA_SIG* sig, const BIGNUM** pr, const BIGNUM** ps); #endif #if FOLLY_OPENSSL_IS_110 diff --git a/folly/portability/test/OpenSSLPortabilityTest.cpp b/folly/portability/test/OpenSSLPortabilityTest.cpp index 7c1bbf87..a0cdc607 100644 --- a/folly/portability/test/OpenSSLPortabilityTest.cpp +++ b/folly/portability/test/OpenSSLPortabilityTest.cpp @@ -59,3 +59,19 @@ TEST(OpenSSLPortabilityTest, TestRSASetter) { EXPECT_FALSE(BN_cmp(n_public, n_public_actual)); EXPECT_FALSE(BN_cmp(e_public, e_public_actual)); } + +TEST(OpenSSLPortabilityTest, TestEcdsaSigPortability) { + EcdsaSigUniquePtr ecdsa(ECDSA_SIG_new()); + BIGNUM* r = BN_new(); + BIGNUM* s = BN_new(); + BIGNUM* r_actual; + BIGNUM* s_actual; + EXPECT_TRUE(BN_set_bit(r, 1)); + EXPECT_TRUE(BN_set_bit(s, 2)); + EXPECT_TRUE(ECDSA_SIG_set0(ecdsa.get(), r, s)); + ECDSA_SIG_get0( + ecdsa.get(), (const BIGNUM**)&r_actual, (const BIGNUM**)&s_actual); + // BN_cmp returns 0 if the two BIGNUMs are equal + EXPECT_FALSE(BN_cmp(r, r_actual)); + EXPECT_FALSE(BN_cmp(s, s_actual)); +} -- 2.34.1