Commit 85e8a2d4 authored by Kyle Nekritz's avatar Kyle Nekritz Committed by facebook-github-bot-0

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
parent b2818624
...@@ -213,6 +213,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -213,6 +213,7 @@ nobase_follyinclude_HEADERS = \
io/async/EventUtil.h \ io/async/EventUtil.h \
io/async/NotificationQueue.h \ io/async/NotificationQueue.h \
io/async/HHWheelTimer.h \ io/async/HHWheelTimer.h \
io/async/OpenSSLPtrTypes.h \
io/async/Request.h \ io/async/Request.h \
io/async/SSLContext.h \ io/async/SSLContext.h \
io/async/ScopedEventBaseThread.h \ io/async/ScopedEventBaseThread.h \
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <folly/io/async/AsyncSocket.h> #include <folly/io/async/AsyncSocket.h>
#include <folly/io/async/SSLContext.h> #include <folly/io/async/SSLContext.h>
#include <folly/io/async/AsyncTimeout.h> #include <folly/io/async/AsyncTimeout.h>
#include <folly/io/async/OpenSSLPtrTypes.h>
#include <folly/io/async/TimeoutManager.h> #include <folly/io/async/TimeoutManager.h>
#include <folly/Bits.h> #include <folly/Bits.h>
...@@ -739,13 +740,13 @@ class AsyncSSLSocket : public virtual AsyncSocket { ...@@ -739,13 +740,13 @@ class AsyncSSLSocket : public virtual AsyncSocket {
/** /**
* Returns the peer certificate, or nullptr if no peer certificate received. * 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_) { if (!ssl_) {
return nullptr; return nullptr;
} }
X509* cert = SSL_get_peer_certificate(ssl_); X509* cert = SSL_get_peer_certificate(ssl_);
return std::unique_ptr<X509, X509_deleter>(cert); return X509_UniquePtr(cert);
} }
private: private:
......
/*
* 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>;
}
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <folly/Format.h> #include <folly/Format.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/SpinLock.h> #include <folly/SpinLock.h>
#include <folly/io/async/OpenSSLPtrTypes.h>
// --------------------------------------------------------------------- // ---------------------------------------------------------------------
// SSLContext implementation // SSLContext implementation
...@@ -46,9 +47,6 @@ std::mutex& initMutex() { ...@@ -46,9 +47,6 @@ std::mutex& initMutex() {
inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); } 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 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 } // anonymous namespace
...@@ -208,8 +206,7 @@ void SSLContext::loadCertificateFromBufferPEM(folly::StringPiece cert) { ...@@ -208,8 +206,7 @@ void SSLContext::loadCertificateFromBufferPEM(folly::StringPiece cert) {
throw std::runtime_error("BIO_write: " + getErrors()); throw std::runtime_error("BIO_write: " + getErrors());
} }
std::unique_ptr<X509, X509_deleter> x509( X509_UniquePtr x509(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
if (x509 == nullptr) { if (x509 == nullptr) {
throw std::runtime_error("PEM_read_bio_X509: " + getErrors()); throw std::runtime_error("PEM_read_bio_X509: " + getErrors());
} }
...@@ -248,7 +245,7 @@ void SSLContext::loadPrivateKeyFromBufferPEM(folly::StringPiece pkey) { ...@@ -248,7 +245,7 @@ void SSLContext::loadPrivateKeyFromBufferPEM(folly::StringPiece pkey) {
throw std::runtime_error("BIO_write: " + getErrors()); 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)); PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
if (key == nullptr) { if (key == nullptr) {
throw std::runtime_error("PEM_read_bio_PrivateKey: " + getErrors()); throw std::runtime_error("PEM_read_bio_PrivateKey: " + getErrors());
......
...@@ -59,10 +59,6 @@ constexpr size_t SSLClient::kMaxReadsPerEvent; ...@@ -59,10 +59,6 @@ constexpr size_t SSLClient::kMaxReadsPerEvent;
inline void BIO_free_fb(BIO* bio) { CHECK_EQ(1, BIO_free(bio)); } 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 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) TestSSLServer::TestSSLServer(SSLServerAcceptCallbackBase* acb)
: ctx_(new folly::SSLContext), : ctx_(new folly::SSLContext),
...@@ -1394,9 +1390,9 @@ TEST(AsyncSSLSocketTest, LoadCertFromMemory) { ...@@ -1394,9 +1390,9 @@ TEST(AsyncSSLSocketTest, LoadCertFromMemory) {
BIO_write(keyBio.get(), key.data(), key.size()); BIO_write(keyBio.get(), key.data(), key.size());
// Create SSL structs from buffers to get properties // 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)); 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)); PEM_read_bio_PrivateKey(keyBio.get(), nullptr, nullptr, nullptr));
certBio = nullptr; certBio = nullptr;
keyBio = nullptr; keyBio = nullptr;
...@@ -1411,7 +1407,7 @@ TEST(AsyncSSLSocketTest, LoadCertFromMemory) { ...@@ -1411,7 +1407,7 @@ TEST(AsyncSSLSocketTest, LoadCertFromMemory) {
ctx->loadCertificateFromBufferPEM(cert); ctx->loadCertificateFromBufferPEM(cert);
ctx->loadTrustedCertificates(testCA); 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 newCert = SSL_get_certificate(ssl.get());
auto newKey = SSL_get_privatekey(ssl.get()); auto newKey = SSL_get_privatekey(ssl.get());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment