diff options
| author | Tobias Markmann <tm@ayena.de> | 2018-07-28 11:45:35 (GMT) |
|---|---|---|
| committer | Tobias Markmann <tm@ayena.de> | 2018-07-30 13:27:15 (GMT) |
| commit | 798f8ec3331043a92f6ca3bc810b9477c3f8261e (patch) | |
| tree | 868bde2ddcbad1a66d4256edd6050f486860eb2b | |
| parent | 48596613cfe0f45c0916beabbcc3a27e01752c4b (diff) | |
| download | swift-798f8ec3331043a92f6ca3bc810b9477c3f8261e.zip swift-798f8ec3331043a92f6ca3bc810b9477c3f8261e.tar.bz2 | |
Use std::unique_ptr to have TLS classes own the TLSContext
TLSLayer and TLSConnection now own the TLSContext they use.
The TLSContextFactory interface is adjusted to use
std::unique_ptr.
Test-Information:
Builds and all tests pass on macOS with clang-7-master.
Change-Id: I14e33c98b48445094f404b73ea41af0a51d2dde6
| -rw-r--r-- | Swiften/Network/BOSHConnection.cpp | 6 | ||||
| -rw-r--r-- | Swiften/Network/TLSConnection.cpp | 5 | ||||
| -rw-r--r-- | Swiften/Network/TLSConnection.h | 4 | ||||
| -rw-r--r-- | Swiften/Session/BasicSessionStream.cpp | 5 | ||||
| -rw-r--r-- | Swiften/StreamStack/TLSLayer.cpp | 30 | ||||
| -rw-r--r-- | Swiften/StreamStack/TLSLayer.h | 8 | ||||
| -rw-r--r-- | Swiften/TLS/OpenSSL/OpenSSLContextFactory.cpp | 4 | ||||
| -rw-r--r-- | Swiften/TLS/OpenSSL/OpenSSLContextFactory.h | 4 | ||||
| -rw-r--r-- | Swiften/TLS/Schannel/SchannelContextFactory.cpp | 4 | ||||
| -rw-r--r-- | Swiften/TLS/Schannel/SchannelContextFactory.h | 4 | ||||
| -rw-r--r-- | Swiften/TLS/SecureTransport/SecureTransportContextFactory.cpp | 4 | ||||
| -rw-r--r-- | Swiften/TLS/SecureTransport/SecureTransportContextFactory.h | 4 | ||||
| -rw-r--r-- | Swiften/TLS/TLSContextFactory.h | 4 |
13 files changed, 48 insertions, 38 deletions
diff --git a/Swiften/Network/BOSHConnection.cpp b/Swiften/Network/BOSHConnection.cpp index b4ffa7d..4bbb121 100644 --- a/Swiften/Network/BOSHConnection.cpp +++ b/Swiften/Network/BOSHConnection.cpp @@ -3,11 +3,11 @@ * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ /* - * Copyright (c) 2011-2017 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Network/BOSHConnection.h> @@ -25,10 +25,11 @@ #include <Swiften/Network/HostAddressPort.h> #include <Swiften/Parser/BOSHBodyExtractor.h> #include <Swiften/StreamStack/DummyStreamLayer.h> #include <Swiften/StreamStack/TLSLayer.h> #include <Swiften/TLS/TLSContext.h> +#include <Swiften/TLS/TLSContextFactory.h> #include <Swiften/TLS/TLSOptions.h> namespace Swift { BOSHConnection::BOSHConnection(const URL& boshURL, Connector::ref connector, XMLParserFactory* parserFactory, TLSContextFactory* tlsContextFactory, const TLSOptions& tlsOptions) @@ -40,11 +41,12 @@ BOSHConnection::BOSHConnection(const URL& boshURL, Connector::ref connector, XML rid_(~0ULL), pending_(false), connectionReady_(false) { if (boshURL_.getScheme() == "https") { - tlsLayer_ = std::make_shared<TLSLayer>(tlsContextFactory, tlsOptions); + auto tlsContext = tlsContextFactory->createTLSContext(tlsOptions); + tlsLayer_ = std::make_shared<TLSLayer>(std::move(tlsContext)); // The following dummyLayer_ is needed as the TLSLayer will pass the decrypted data to its parent layer. // The dummyLayer_ will serve as the parent layer. dummyLayer_ = std::make_shared<DummyStreamLayer>(tlsLayer_.get()); } } diff --git a/Swiften/Network/TLSConnection.cpp b/Swiften/Network/TLSConnection.cpp index 7c293d1..82bf114 100644 --- a/Swiften/Network/TLSConnection.cpp +++ b/Swiften/Network/TLSConnection.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Network/TLSConnection.h> @@ -30,11 +30,10 @@ TLSConnection::TLSConnection(Connection::ref connection, TLSContextFactory* tlsF TLSConnection::~TLSConnection() { connection->onConnectFinished.disconnect(boost::bind(&TLSConnection::handleRawConnectFinished, this, _1)); connection->onDataRead.disconnect(boost::bind(&TLSConnection::handleRawDataRead, this, _1)); connection->onDataWritten.disconnect(boost::bind(&TLSConnection::handleRawDataWritten, this)); connection->onDisconnected.disconnect(boost::bind(&TLSConnection::handleRawDisconnected, this, _1)); - delete context; } void TLSConnection::handleTLSConnectFinished(bool error) { onConnectFinished(error); if (error) { @@ -69,11 +68,11 @@ HostAddressPort TLSConnection::getLocalAddress() const { HostAddressPort TLSConnection::getRemoteAddress() const { return connection->getRemoteAddress(); } TLSContext* TLSConnection::getTLSContext() const { - return context; + return context.get(); } void TLSConnection::handleRawConnectFinished(bool error) { connection->onConnectFinished.disconnect(boost::bind(&TLSConnection::handleRawConnectFinished, this, _1)); if (error) { diff --git a/Swiften/Network/TLSConnection.h b/Swiften/Network/TLSConnection.h index 0c395d1..1ab1ec6 100644 --- a/Swiften/Network/TLSConnection.h +++ b/Swiften/Network/TLSConnection.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2011-2016 Isode Limited. + * Copyright (c) 2011-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once @@ -44,9 +44,9 @@ namespace Swift { void handleTLSConnectFinished(bool error); void handleTLSDataForNetwork(const SafeByteArray& data); void handleTLSDataForApplication(const SafeByteArray& data); private: - TLSContext* context; + std::unique_ptr<TLSContext> context; Connection::ref connection; }; } diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp index 10c6ad0..54cd225 100644 --- a/Swiften/Session/BasicSessionStream.cpp +++ b/Swiften/Session/BasicSessionStream.cpp @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/Session/BasicSessionStream.h> @@ -109,11 +109,12 @@ bool BasicSessionStream::supportsTLSEncryption() { return tlsContextFactory && tlsContextFactory->canCreate(); } void BasicSessionStream::addTLSEncryption() { assert(available); - tlsLayer = new TLSLayer(tlsContextFactory, tlsOptions_); + auto tlsContext = tlsContextFactory->createTLSContext(tlsOptions_); + tlsLayer = new TLSLayer(std::move(tlsContext)); if (hasTLSCertificate() && !tlsLayer->setClientCertificate(getTLSCertificate())) { onClosed(std::make_shared<SessionStreamError>(SessionStreamError::InvalidTLSCertificateError)); } else { streamStack->addLayer(tlsLayer); diff --git a/Swiften/StreamStack/TLSLayer.cpp b/Swiften/StreamStack/TLSLayer.cpp index ced879e..9f84889 100644 --- a/Swiften/StreamStack/TLSLayer.cpp +++ b/Swiften/StreamStack/TLSLayer.cpp @@ -1,56 +1,56 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swiften/StreamStack/TLSLayer.h> +#include <memory> + #include <boost/bind.hpp> #include <Swiften/TLS/TLSContext.h> #include <Swiften/TLS/TLSContextFactory.h> namespace Swift { -TLSLayer::TLSLayer(TLSContextFactory* factory, const TLSOptions& tlsOptions) { - context = factory->createTLSContext(tlsOptions); - context->onDataForNetwork.connect(boost::bind(&TLSLayer::writeDataToChildLayer, this, _1)); - context->onDataForApplication.connect(boost::bind(&TLSLayer::writeDataToParentLayer, this, _1)); - context->onConnected.connect(onConnected); - context->onError.connect(onError); +TLSLayer::TLSLayer(std::unique_ptr<TLSContext> tlsContext) : context_(std::move(tlsContext)) { + context_->onDataForNetwork.connect(boost::bind(&TLSLayer::writeDataToChildLayer, this, _1)); + context_->onDataForApplication.connect(boost::bind(&TLSLayer::writeDataToParentLayer, this, _1)); + context_->onConnected.connect(onConnected); + context_->onError.connect(onError); } TLSLayer::~TLSLayer() { - delete context; } void TLSLayer::connect() { - context->connect(); + context_->connect(); } void TLSLayer::writeData(const SafeByteArray& data) { - context->handleDataFromApplication(data); + context_->handleDataFromApplication(data); } void TLSLayer::handleDataRead(const SafeByteArray& data) { - context->handleDataFromNetwork(data); + context_->handleDataFromNetwork(data); } bool TLSLayer::setClientCertificate(CertificateWithKey::ref certificate) { - return context->setClientCertificate(certificate); + return context_->setClientCertificate(certificate); } Certificate::ref TLSLayer::getPeerCertificate() const { - return context->getPeerCertificate(); + return context_->getPeerCertificate(); } std::vector<Certificate::ref> TLSLayer::getPeerCertificateChain() const { - return context->getPeerCertificateChain(); + return context_->getPeerCertificateChain(); } std::shared_ptr<CertificateVerificationError> TLSLayer::getPeerCertificateVerificationError() const { - return context->getPeerCertificateVerificationError(); + return context_->getPeerCertificateVerificationError(); } } diff --git a/Swiften/StreamStack/TLSLayer.h b/Swiften/StreamStack/TLSLayer.h index 415a3f0..89588e3 100644 --- a/Swiften/StreamStack/TLSLayer.h +++ b/Swiften/StreamStack/TLSLayer.h @@ -1,7 +1,7 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once @@ -21,11 +21,11 @@ namespace Swift { class TLSContext; class TLSContextFactory; class SWIFTEN_API TLSLayer : public StreamLayer { public: - TLSLayer(TLSContextFactory*, const TLSOptions&); + TLSLayer(std::unique_ptr<TLSContext> tlsContext); virtual ~TLSLayer(); void connect(); bool setClientCertificate(CertificateWithKey::ref cert); @@ -35,16 +35,16 @@ namespace Swift { void writeData(const SafeByteArray& data); void handleDataRead(const SafeByteArray& data); TLSContext* getContext() const { - return context; + return context_.get(); } public: boost::signals2::signal<void (std::shared_ptr<TLSError>)> onError; boost::signals2::signal<void ()> onConnected; private: - TLSContext* context; + std::unique_ptr<TLSContext> context_; }; } diff --git a/Swiften/TLS/OpenSSL/OpenSSLContextFactory.cpp b/Swiften/TLS/OpenSSL/OpenSSLContextFactory.cpp index 9d0ad72..a9ba5ab 100644 --- a/Swiften/TLS/OpenSSL/OpenSSLContextFactory.cpp +++ b/Swiften/TLS/OpenSSL/OpenSSLContextFactory.cpp @@ -19,12 +19,12 @@ namespace Swift { bool OpenSSLContextFactory::canCreate() const { return true; } -TLSContext* OpenSSLContextFactory::createTLSContext(const TLSOptions&, TLSContext::Mode mode) { - return new OpenSSLContext(mode); +std::unique_ptr<TLSContext> OpenSSLContextFactory::createTLSContext(const TLSOptions&, TLSContext::Mode mode) { + return std::unique_ptr<TLSContext>(new OpenSSLContext(mode)); } ByteArray OpenSSLContextFactory::convertDHParametersFromPEMToDER(const std::string& dhParametersInPEM) { ByteArray dhParametersInDER; diff --git a/Swiften/TLS/OpenSSL/OpenSSLContextFactory.h b/Swiften/TLS/OpenSSL/OpenSSLContextFactory.h index db7fa34..95a2b0c 100644 --- a/Swiften/TLS/OpenSSL/OpenSSLContextFactory.h +++ b/Swiften/TLS/OpenSSL/OpenSSLContextFactory.h @@ -4,17 +4,19 @@ * See the COPYING file for more information. */ #pragma once +#include <memory> + #include <Swiften/TLS/TLSContextFactory.h> namespace Swift { class OpenSSLContextFactory : public TLSContextFactory { public: bool canCreate() const override final; - virtual TLSContext* createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode) override final; + virtual std::unique_ptr<TLSContext> createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode) override final; virtual ByteArray convertDHParametersFromPEMToDER(const std::string& dhParametersInPEM) override final; // Not supported virtual void setCheckCertificateRevocation(bool b) override final; diff --git a/Swiften/TLS/Schannel/SchannelContextFactory.cpp b/Swiften/TLS/Schannel/SchannelContextFactory.cpp index 0015fbe..d029730 100644 --- a/Swiften/TLS/Schannel/SchannelContextFactory.cpp +++ b/Swiften/TLS/Schannel/SchannelContextFactory.cpp @@ -21,17 +21,17 @@ SchannelContextFactory::SchannelContextFactory() : checkCertificateRevocation(tr bool SchannelContextFactory::canCreate() const { return true; } -TLSContext* SchannelContextFactory::createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode) { +std::unique_ptr<TLSContext> SchannelContextFactory::createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode) { // TLS server mode is not supported for the SecureTransport backend yet. assert(mode == TLSContext::Mode::Client); SchannelContext* context = new SchannelContext(tlsOptions.schannelTLS1_0Workaround); context->setCheckCertificateRevocation(checkCertificateRevocation); context->setDisconnectOnCardRemoval(disconnectOnCardRemoval); - return context; + return std::unique_ptr<TLSContext>(context); } void SchannelContextFactory::setCheckCertificateRevocation(bool b) { checkCertificateRevocation = b; } diff --git a/Swiften/TLS/Schannel/SchannelContextFactory.h b/Swiften/TLS/Schannel/SchannelContextFactory.h index f878037..76ff365 100644 --- a/Swiften/TLS/Schannel/SchannelContextFactory.h +++ b/Swiften/TLS/Schannel/SchannelContextFactory.h @@ -10,19 +10,21 @@ * See the COPYING file for more information. */ #pragma once +#include <memory> + #include <Swiften/TLS/TLSContextFactory.h> namespace Swift { class SchannelContextFactory : public TLSContextFactory { public: SchannelContextFactory(); bool canCreate() const; - virtual TLSContext* createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode = TLSContext::Mode::Client); + virtual std::unique_ptr<TLSContext> createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode = TLSContext::Mode::Client); virtual void setCheckCertificateRevocation(bool b); virtual void setDisconnectOnCardRemoval(bool b); public: diff --git a/Swiften/TLS/SecureTransport/SecureTransportContextFactory.cpp b/Swiften/TLS/SecureTransport/SecureTransportContextFactory.cpp index dfb9d67..cc10987 100644 --- a/Swiften/TLS/SecureTransport/SecureTransportContextFactory.cpp +++ b/Swiften/TLS/SecureTransport/SecureTransportContextFactory.cpp @@ -24,14 +24,14 @@ SecureTransportContextFactory::~SecureTransportContextFactory() { bool SecureTransportContextFactory::canCreate() const { return true; } -TLSContext* SecureTransportContextFactory::createTLSContext(const TLSOptions& /* tlsOptions */, TLSContext::Mode mode) { +std::unique_ptr<TLSContext> SecureTransportContextFactory::createTLSContext(const TLSOptions& /* tlsOptions */, TLSContext::Mode mode) { // TLS server mode is not supported for the SecureTransport backend yet. assert(mode == TLSContext::Mode::Client); - return new SecureTransportContext(checkCertificateRevocation_); + return std::unique_ptr<TLSContext>(new SecureTransportContext(checkCertificateRevocation_)); } void SecureTransportContextFactory::setCheckCertificateRevocation(bool b) { checkCertificateRevocation_ = b; } diff --git a/Swiften/TLS/SecureTransport/SecureTransportContextFactory.h b/Swiften/TLS/SecureTransport/SecureTransportContextFactory.h index 5962424..b86639a 100644 --- a/Swiften/TLS/SecureTransport/SecureTransportContextFactory.h +++ b/Swiften/TLS/SecureTransport/SecureTransportContextFactory.h @@ -4,10 +4,12 @@ * See the COPYING file for more information. */ #pragma once +#include <memory> + #include <Swiften/TLS/TLSContextFactory.h> namespace Swift { class SecureTransportContextFactory : public TLSContextFactory { @@ -15,11 +17,11 @@ class SecureTransportContextFactory : public TLSContextFactory { SecureTransportContextFactory(); virtual ~SecureTransportContextFactory(); virtual bool canCreate() const; - virtual TLSContext* createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode = TLSContext::Mode::Client); + virtual std::unique_ptr<TLSContext> createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode mode = TLSContext::Mode::Client); virtual void setCheckCertificateRevocation(bool b); virtual void setDisconnectOnCardRemoval(bool b); private: bool checkCertificateRevocation_; diff --git a/Swiften/TLS/TLSContextFactory.h b/Swiften/TLS/TLSContextFactory.h index 9da3392..5f70541 100644 --- a/Swiften/TLS/TLSContextFactory.h +++ b/Swiften/TLS/TLSContextFactory.h @@ -4,10 +4,12 @@ * See the COPYING file for more information. */ #pragma once +#include <memory> + #include <Swiften/Base/API.h> #include <Swiften/Base/ByteArray.h> #include <Swiften/TLS/TLSContext.h> #include <Swiften/TLS/TLSOptions.h> @@ -16,11 +18,11 @@ namespace Swift { public: virtual ~TLSContextFactory(); virtual bool canCreate() const = 0; - virtual TLSContext* createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode = TLSContext::Mode::Client) = 0; + virtual std::unique_ptr<TLSContext> createTLSContext(const TLSOptions& tlsOptions, TLSContext::Mode = TLSContext::Mode::Client) = 0; virtual void setCheckCertificateRevocation(bool b) = 0; virtual void setDisconnectOnCardRemoval(bool b) = 0; virtual ByteArray convertDHParametersFromPEMToDER(const std::string& pem); }; |
Swift