Adding OpenSSLPtrTypes.h.
authorKyle Nekritz <knekritz@fb.com>
Thu, 21 Jan 2016 21:54:26 +0000 (13:54 -0800)
committerfacebook-github-bot-0 <folly-bot@fb.com>
Thu, 21 Jan 2016 23:20:55 +0000 (15:20 -0800)
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
folly/io/async/AsyncSSLSocket.h
folly/io/async/OpenSSLPtrTypes.h [new file with mode: 0644]
folly/io/async/SSLContext.cpp
folly/io/async/test/AsyncSSLSocketTest.cpp

index d6a46bb8a6166593d525f1aab051dc4ea2b9cbe0..a9704ea6b79bd0f7abe4ccc4ca2648a3b559f3de 100644 (file)
@@ -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 \
index ad2a979965df4eec7ad4f71fafd110314e248cf9..2836544f5ad4f357f2cd6df57f497914410cab52 100644 (file)
@@ -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 (file)
index 0000000..57d73b8
--- /dev/null
@@ -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>;
+}
index 4d2dfed0144f59c03e8bc7df09f56e42b3f57b80..24184f7a3648163b4b1a34730e9c582670f54c34 100644 (file)
@@ -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());
index c4ad75d6cc849b2ec9ef2356cd68b71a1cc1f2aa..19403b1a19ee0547f5f8a03091e60fe22902dbbf 100644 (file)
@@ -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());