Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSL fixes for validator list (RIPD-1558) #2275

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Builds/CMake/CMakeFuncs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,6 @@ macro(link_common_libraries cur_project)
$<$<OR:$<CONFIG:Release>,$<CONFIG:ReleaseClassic>>:VC/static/libeay32MT>)
target_link_libraries(${cur_project}
legacy_stdio_definitions.lib Shlwapi kernel32 user32 gdi32 winspool comdlg32
advapi32 shell32 ole32 oleaut32 uuid odbc32 odbccp32)
advapi32 shell32 ole32 oleaut32 uuid odbc32 odbccp32 crypt32)
endif (NOT MSVC)
endmacro()
14 changes: 10 additions & 4 deletions Builds/VisualStudio2015/RippleD.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
<AdditionalOptions>/bigobj /FS %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MTd.lib;VC/static/ssleay32MTd.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;crypt32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MTd.lib;VC/static/ssleay32MTd.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SuppressStartupBanner>True</SuppressStartupBanner>
<ErrorReporting>NoErrorReport</ErrorReporting>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -147,7 +147,7 @@
<AdditionalOptions>/bigobj /FS %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MTd.lib;VC/static/ssleay32MTd.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;crypt32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MTd.lib;VC/static/ssleay32MTd.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SuppressStartupBanner>True</SuppressStartupBanner>
<ErrorReporting>NoErrorReport</ErrorReporting>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -182,7 +182,7 @@
<AdditionalOptions>/bigobj /FS %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MT.lib;VC/static/ssleay32MT.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;crypt32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MT.lib;VC/static/ssleay32MT.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SuppressStartupBanner>True</SuppressStartupBanner>
<ErrorReporting>NoErrorReport</ErrorReporting>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -217,7 +217,7 @@
<AdditionalOptions>/bigobj /FS %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MT.lib;VC/static/ssleay32MT.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>advapi32.lib;comdlg32.lib;crypt32.lib;gdi32.lib;kernel32.lib;legacy_stdio_definitions.lib;odbc32.lib;odbccp32.lib;ole32.lib;oleaut32.lib;shell32.lib;Shlwapi.lib;user32.lib;uuid.lib;VC/static/libeay32MT.lib;VC/static/ssleay32MT.lib;winspool.lib;%(AdditionalDependencies)</AdditionalDependencies>
<SuppressStartupBanner>True</SuppressStartupBanner>
<ErrorReporting>NoErrorReport</ErrorReporting>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -2199,6 +2199,10 @@
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\net\impl\RegisterSSLCerts.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\..\src\ripple\net\impl\RPCCall.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='debug|x64'">True</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='release|x64'">True</ExcludedFromBuild>
Expand All @@ -2213,6 +2217,8 @@
</ClCompile>
<ClInclude Include="..\..\src\ripple\net\InfoSub.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\net\RegisterSSLCerts.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\net\RPCCall.h">
</ClInclude>
<ClInclude Include="..\..\src\ripple\net\RPCErr.h">
Expand Down
6 changes: 6 additions & 0 deletions Builds/VisualStudio2015/RippleD.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,9 @@
<ClCompile Include="..\..\src\ripple\net\impl\InfoSub.cpp">
<Filter>ripple\net\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\net\impl\RegisterSSLCerts.cpp">
<Filter>ripple\net\impl</Filter>
</ClCompile>
<ClCompile Include="..\..\src\ripple\net\impl\RPCCall.cpp">
<Filter>ripple\net\impl</Filter>
</ClCompile>
Expand All @@ -2847,6 +2850,9 @@
<ClInclude Include="..\..\src\ripple\net\InfoSub.h">
<Filter>ripple\net</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\net\RegisterSSLCerts.h">
<Filter>ripple\net</Filter>
</ClInclude>
<ClInclude Include="..\..\src\ripple\net\RPCCall.h">
<Filter>ripple\net</Filter>
</ClInclude>
Expand Down
1 change: 1 addition & 0 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ def config_env(toolchain, variant, env):
'uuid.lib',
'odbc32.lib',
'odbccp32.lib',
'crypt32.lib'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: misaligned

])
env.Append(LINKFLAGS=[
'/DEBUG',
Expand Down
14 changes: 7 additions & 7 deletions src/ripple/app/misc/detail/WorkSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define RIPPLE_APP_MISC_DETAIL_WORKSSL_H_INCLUDED

#include <ripple/app/misc/detail/WorkBase.h>
#include <ripple/net/RegisterSSLCerts.h>
#include <ripple/basics/contract.h>
#include <boost/asio/ssl.hpp>
#include <boost/bind.hpp>
Expand All @@ -37,8 +38,7 @@ class SSLContext : public boost::asio::ssl::context
: boost::asio::ssl::context(boost::asio::ssl::context::sslv23)
{
boost::system::error_code ec;
set_default_verify_paths (ec);

registerSSLCerts(*this, ec);
if (ec)
{
Throw<std::runtime_error> (
Expand Down Expand Up @@ -82,13 +82,12 @@ class WorkSSL : public WorkBase<WorkSSL>
onHandshake(error_code const& ec);

static bool
rfc2818_verify (
rfc2818_verify(
std::string const& domain,
bool preverified,
boost::asio::ssl::verify_context& ctx)
{
return
boost::asio::ssl::rfc2818_verification (domain) (preverified, ctx);
return boost::asio::ssl::rfc2818_verification(domain)(preverified, ctx);
}
};

Expand All @@ -102,9 +101,10 @@ WorkSSL::WorkSSL(
, context_()
, stream_ (socket_, context_)
{
// Set SNI hostname
SSL_set_tlsext_host_name(stream_.native_handle(), host.c_str());
stream_.set_verify_mode (boost::asio::ssl::verify_peer);
stream_.set_verify_callback (
std::bind (
stream_.set_verify_callback( std::bind (
&WorkSSL::rfc2818_verify, host_,
std::placeholders::_1, std::placeholders::_2));
}
Expand Down
4 changes: 4 additions & 0 deletions src/ripple/net/AutoSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ class AutoSocket
return ec;
}

void setTLSHostName(std::string const & host)
{
SSL_set_tlsext_host_name(mSocket->native_handle(), host.c_str());
}
/*
template <typename HandshakeHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(HandshakeHandler,
Expand Down
38 changes: 38 additions & 0 deletions src/ripple/net/RegisterSSLCerts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2016 Ripple Labs Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale date


Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#ifndef RIPPLE_NET_REGISTER_SSL_CERTS_H_INCLUDED
#define RIPPLE_NET_REGISTER_SSL_CERTS_H_INCLUDED

#include <boost/asio/ssl/context.hpp>

namespace ripple {
/** Register default SSL certificates.

Register the system default SSL root certificates. On linux/mac,
this just calls asio's `set_default_verify_paths` to look in standard
operating system locations. On windows, it uses the OS certificate
store accessible via CryptoAPI.
*/
void
registerSSLCerts(boost::asio::ssl::context&, boost::system::error_code&);

} // namespace ripple

#endif
9 changes: 8 additions & 1 deletion src/ripple/net/impl/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <ripple/basics/StringUtilities.h>
#include <ripple/net/HTTPClient.h>
#include <ripple/net/AutoSocket.h>
#include <ripple/net/RegisterSSLCerts.h>
#include <ripple/beast/core/LexicalCast.h>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
Expand All @@ -48,7 +49,7 @@ class HTTPClientSSLContext

if (config.SSL_VERIFY_FILE.empty ())
{
m_context.set_default_verify_paths (ec);
registerSSLCerts(m_context, ec);

if (ec && config.SSL_VERIFY_DIR.empty ())
Throw<std::runtime_error> (
Expand Down Expand Up @@ -278,6 +279,12 @@ class HTTPClientImp
{
JLOG (j_.trace()) << "Resolve complete.";

// If we intend to verify the SSL connection, we need to
// set the default domain for server name indication *prior* to
// connecting
if (httpClientSSLContext->sslVerify())
mSocket.setTLSHostName(mDeqSites[0]);

boost::asio::async_connect (
mSocket.lowest_layer (),
itrEndpoint,
Expand Down
103 changes: 103 additions & 0 deletions src/ripple/net/impl/RegisterSSLCerts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2012, 2013 Ripple Labs Inc.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <BeastConfig.h>
#include <ripple/net/RegisterSSLCerts.h>
#include <boost/predef.h>
#if BOOST_OS_WINDOWS
#include <boost/asio/ssl/error.hpp>
#include <boost/system/error_code.hpp>
#include <memory>
#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <wincrypt.h>
#endif

namespace ripple {

void
registerSSLCerts(boost::asio::ssl::context& ctx, boost::system::error_code& ec)
{
#if BOOST_OS_WINDOWS
auto certStoreDelete = [](void* h) {
if (h != nullptr)
CertCloseStore(h, 0);
};
std::unique_ptr<void, decltype(certStoreDelete)> hStore{
CertOpenSystemStore(0, "ROOT"), certStoreDelete};

if (!hStore)
{
ec = boost::system::error_code(
GetLastError(), boost::system::system_category());
return;
}

ERR_clear_error();

std::unique_ptr<X509_STORE, decltype(X509_STORE_free)*> store{
X509_STORE_new(), X509_STORE_free};

if (!store)
{
ec = boost::system::error_code(
static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category());
return;
}

PCCERT_CONTEXT pContext = NULL;
while ((pContext = CertEnumCertificatesInStore(hStore.get(), pContext)) !=
NULL)
{
const unsigned char* pbCertEncoded = pContext->pbCertEncoded;
std::unique_ptr<X509, decltype(X509_free)*> x509{
d2i_X509(NULL, &pbCertEncoded, pContext->cbCertEncoded), X509_free};
if (!x509)
{
ec = boost::system::error_code(
static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category());
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok as is (returning with ec and throwing if anything goes wrong), but I'm wondering if we could alternatively be ok calling continue here (and on line 85) and just having the caller log ec.

}

if (X509_STORE_add_cert(store.get(), x509.get()) != 1)
{
ec = boost::system::error_code(
static_cast<int>(::ERR_get_error()),
boost::asio::error::get_ssl_category());
return;
}
else
{
// Successfully adding to the store took ownership
x509.release();
}
}

// This takes ownership of the store
SSL_CTX_set_cert_store(ctx.native_handle(), store.release());

#else

ctx.set_default_verify_paths(ec);
#endif
}

} // namespace ripple
1 change: 1 addition & 0 deletions src/ripple/unity/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
#include <ripple/net/impl/RPCCall.cpp>
#include <ripple/net/impl/RPCErr.cpp>
#include <ripple/net/impl/RPCSub.cpp>
#include <ripple/net/impl/RegisterSSLCerts.cpp>