From 85e8a2d4a5041a2409f4eaf20df99abe8550686d Mon Sep 17 00:00:00 2001 From: Kyle Nekritz <knekritz@fb.com> Date: Thu, 21 Jan 2016 13:54:26 -0800 Subject: [PATCH] Adding OpenSSLPtrTypes.h. Summary: So that these deleter and unique_ptr types don't have to be redeclared every single place they are used. To be expanded on. Reviewed By: mzlee Differential Revision: D2850376 fb-gh-sync-id: e7f8bba320163b8b12a93b5cf3cd9a5921d38edc --- folly/Makefile.am | 1 + folly/io/async/AsyncSSLSocket.h | 5 ++-- folly/io/async/OpenSSLPtrTypes.h | 33 ++++++++++++++++++++++ folly/io/async/SSLContext.cpp | 9 ++---- folly/io/async/test/AsyncSSLSocketTest.cpp | 10 ++----- 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 folly/io/async/OpenSSLPtrTypes.h diff --git a/folly/Makefile.am b/folly/Makefile.am index d6a46bb8..a9704ea6 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -213,6 +213,7 @@ nobase_follyinclude_HEADERS = \ io/async/EventUtil.h \ io/async/NotificationQueue.h \ io/async/HHWheelTimer.h \ + io/async/OpenSSLPtrTypes.h \ io/async/Request.h \ io/async/SSLContext.h \ io/async/ScopedEventBaseThread.h \ diff --git a/folly/io/async/AsyncSSLSocket.h b/folly/io/async/AsyncSSLSocket.h index ad2a9799..2836544f 100644 --- a/folly/io/async/AsyncSSLSocket.h +++ b/folly/io/async/AsyncSSLSocket.h @@ -25,6 +25,7 @@ #include <folly/io/async/AsyncSocket.h> #include <folly/io/async/SSLContext.h> #include <folly/io/async/AsyncTimeout.h> +#include <folly/io/async/OpenSSLPtrTypes.h> #include <folly/io/async/TimeoutManager.h> #include <folly/Bits.h> @@ -739,13 +740,13 @@ class AsyncSSLSocket : public virtual AsyncSocket { /** * Returns the peer certificate, or nullptr if no peer certificate received. */ - virtual std::unique_ptr<X509, X509_deleter> getPeerCert() const { + virtual X509_UniquePtr getPeerCert() const { if (!ssl_) { return nullptr; } X509* cert = SSL_get_peer_certificate(ssl_); - return std::unique_ptr<X509, X509_deleter>(cert); + return X509_UniquePtr(cert); } private: diff --git a/folly/io/async/OpenSSLPtrTypes.h b/folly/io/async/OpenSSLPtrTypes.h new file mode 100644 index 00000000..57d73b8e --- /dev/null +++ b/folly/io/async/OpenSSLPtrTypes.h @@ -0,0 +1,33 @@ +/* + * Copyright 2016 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. + */ + +#pragma once + +#include <folly/Memory.h> +#include <openssl/ssl.h> + +namespace folly { + +using X509_deleter = static_function_deleter<X509, &X509_free>; +using X509_UniquePtr = std::unique_ptr<X509, X509_deleter>; + +using EVP_PKEY_deleter = + folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>; +using EVP_PKEY_UniquePtr = std::unique_ptr<EVP_PKEY, EVP_PKEY_deleter>; + +using SSL_deleter = folly::static_function_deleter<SSL, &SSL_free>; +using SSL_UniquePtr = std::unique_ptr<SSL, SSL_deleter>; +} diff --git a/folly/io/async/SSLContext.cpp b/folly/io/async/SSLContext.cpp index 4d2dfed0..24184f7a 100644 --- a/folly/io/async/SSLContext.cpp +++ b/folly/io/async/SSLContext.cpp @@ -24,6 +24,7 @@ #include <folly/Format.h> #include <folly/Memory.h> #include <folly/SpinLock.h> +#include <folly/io/async/OpenSSLPtrTypes.h> // --------------------------------------------------------------------- // SSLContext implementation @@ -46,9 +47,6 @@ std::mutex& initMutex() { inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); } using BIO_deleter = folly::static_function_deleter<BIO, &BIO_free_fb>; -using X509_deleter = folly::static_function_deleter<X509, &X509_free>; -using EVP_PKEY_deleter = - folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>; } // anonymous namespace @@ -208,8 +206,7 @@ void SSLContext::loadCertificateFromBufferPEM(folly::StringPiece cert) { throw std::runtime_error("BIO_write: " + getErrors()); } - std::unique_ptr<X509, X509_deleter> x509( - PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); + X509_UniquePtr x509(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr)); if (x509 == nullptr) { throw std::runtime_error("PEM_read_bio_X509: " + getErrors()); } @@ -248,7 +245,7 @@ void SSLContext::loadPrivateKeyFromBufferPEM(folly::StringPiece pkey) { throw std::runtime_error("BIO_write: " + getErrors()); } - std::unique_ptr<EVP_PKEY, EVP_PKEY_deleter> key( + EVP_PKEY_UniquePtr key( PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); if (key == nullptr) { throw std::runtime_error("PEM_read_bio_PrivateKey: " + getErrors()); diff --git a/folly/io/async/test/AsyncSSLSocketTest.cpp b/folly/io/async/test/AsyncSSLSocketTest.cpp index c4ad75d6..19403b1a 100644 --- a/folly/io/async/test/AsyncSSLSocketTest.cpp +++ b/folly/io/async/test/AsyncSSLSocketTest.cpp @@ -59,10 +59,6 @@ constexpr size_t SSLClient::kMaxReadsPerEvent; inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); } using BIO_deleter = folly::static_function_deleter<BIO, &BIO_free_fb>; -using X509_deleter = folly::static_function_deleter<X509, &X509_free>; -using SSL_deleter = folly::static_function_deleter<SSL, &SSL_free>; -using EVP_PKEY_deleter = - folly::static_function_deleter<EVP_PKEY, &EVP_PKEY_free>; TestSSLServer::TestSSLServer(SSLServerAcceptCallbackBase* acb) : ctx_(new folly::SSLContext), @@ -1394,9 +1390,9 @@ TEST(AsyncSSLSocketTest, LoadCertFromMemory) { BIO_write(keyBio.get(), key.data(), key.size()); // Create SSL structs from buffers to get properties - std::unique_ptr<X509, X509_deleter> certStruct( + X509_UniquePtr certStruct( PEM_read_bio_X509(certBio.get(), nullptr, nullptr, nullptr)); - std::unique_ptr<EVP_PKEY, EVP_PKEY_deleter> keyStruct( + EVP_PKEY_UniquePtr keyStruct( PEM_read_bio_PrivateKey(keyBio.get(), nullptr, nullptr, nullptr)); certBio = nullptr; keyBio = nullptr; @@ -1411,7 +1407,7 @@ TEST(AsyncSSLSocketTest, LoadCertFromMemory) { ctx->loadCertificateFromBufferPEM(cert); ctx->loadTrustedCertificates(testCA); - std::unique_ptr<SSL, SSL_deleter> ssl(ctx->createSSL()); + SSL_UniquePtr ssl(ctx->createSSL()); auto newCert = SSL_get_certificate(ssl.get()); auto newKey = SSL_get_privatekey(ssl.get()); -- 2.34.1