From: Ankit Shah Date: Wed, 28 Jun 2017 15:25:32 +0000 (-0700) Subject: Adding getter and setter methods for RSA X-Git-Tag: v2017.07.03.00~23 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=04cf6b8f5f46d00de45d09774ef0733a2510bb07;p=folly.git Adding getter and setter methods for RSA Summary: Added getter and setter methods for the RSA struct in OpenSSL. This is needed for compatibility between OpenSSl 1.1.0 and other versions. Reviewed By: yfeldblum Differential Revision: D5331948 fbshipit-source-id: ab52ffd38bb5e0bd59e058bcbc6ec6122839844e --- diff --git a/folly/portability/OpenSSL.cpp b/folly/portability/OpenSSL.cpp index 7fdb7b72..bc16af6e 100644 --- a/folly/portability/OpenSSL.cpp +++ b/folly/portability/OpenSSL.cpp @@ -207,6 +207,31 @@ void HMAC_CTX_free(HMAC_CTX* ctx) { } } +bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) { + // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes + /** + * If the fields n and e in r are NULL, the corresponding input parameters + * MUST be non-NULL for n and e. d may be left NULL (in case only the public + * key is used). + */ + if ((r->n == nullptr && n == nullptr) || (r->e == nullptr && e == nullptr)) { + return false; + } + if (n != nullptr) { + BN_free(r->n); + r->n = n; + } + if (e != nullptr) { + BN_free(r->e); + r->e = e; + } + if (d != nullptr) { + BN_free(r->d); + r->d = d; + } + return true; +} + #endif } } diff --git a/folly/portability/OpenSSL.h b/folly/portability/OpenSSL.h index 7fee24fa..9e1241e6 100644 --- a/folly/portability/OpenSSL.h +++ b/folly/portability/OpenSSL.h @@ -139,6 +139,7 @@ int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g); X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx); STACK_OF(X509) * X509_STORE_CTX_get0_chain(X509_STORE_CTX* ctx); STACK_OF(X509) * X509_STORE_CTX_get0_untrusted(X509_STORE_CTX* ctx); +bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d); #endif #if FOLLY_OPENSSL_IS_110 diff --git a/folly/portability/test/OpenSSLPortabilityTest.cpp b/folly/portability/test/OpenSSLPortabilityTest.cpp new file mode 100644 index 00000000..bb95a755 --- /dev/null +++ b/folly/portability/test/OpenSSLPortabilityTest.cpp @@ -0,0 +1,48 @@ +/* + * Copyright 2017 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include + +using namespace folly; +using namespace testing; + +TEST(OpenSSLPortabilityTest, TestRSASetter) { + RSA* r = RSA_new(); + RSA* public_key = RSA_new(); + BIGNUM* n = BN_new(); + BIGNUM* e = BN_new(); + BIGNUM* d = BN_new(); + const BIGNUM* n_actual = BN_new(); + const BIGNUM* e_actual = BN_new(); + const BIGNUM* d_actual = BN_new(); + EXPECT_TRUE(BN_set_bit(n, 1)); + EXPECT_TRUE(BN_set_bit(e, 3)); + EXPECT_TRUE(BN_set_bit(d, 2)); + RSA_set0_key(r, n, e, d); + RSA_get0_key(r, &n_actual, &e_actual, &d_actual); + // BN_cmp returns 0 if the two BIGNUMs are equal + EXPECT_FALSE(BN_cmp(n, n_actual)); + EXPECT_FALSE(BN_cmp(e, e_actual)); + EXPECT_FALSE(BN_cmp(d, d_actual)); + + RSA_set0_key(public_key, n, e, nullptr); + const BIGNUM* n_public = BN_new(); + const BIGNUM* e_public = BN_new(); + RSA_get0_key(public_key, &n_public, &e_public, nullptr); + EXPECT_FALSE(BN_cmp(n, n_public)); + EXPECT_FALSE(BN_cmp(e, e_public)); +} diff --git a/folly/test/Makefile.am b/folly/test/Makefile.am index 539b6000..04dfb671 100644 --- a/folly/test/Makefile.am +++ b/folly/test/Makefile.am @@ -249,6 +249,11 @@ portability_constexpr_test_SOURCES = ../portability/test/ConstexprTest.cpp portability_constexpr_test_LDADD = libfollytestmain.la TESTS += portability_constexpr_test +portability_openssl_test_SOURCES = ../portability/test/OpenSSLPortabilityTest.cpp +portability_openssl_test_LDADD = libfollytestmain.la +TESTS += portability_openssl_test + + try_test_SOURCES = TryTest.cpp try_test_LDADD = libfollytestmain.la TESTS += try_test