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 2 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
62 changes: 62 additions & 0 deletions src/ripple/net/impl/RegisterSSLCerts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//------------------------------------------------------------------------------
/*
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>
#if BEAST_WINDOWS
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like BEAST_WINDOWS is only used in src/ripple/beast/
Should a different macro be used here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can use boost predef instead.

#include <wincrypt.h>
#endif

namespace ripple {

void
registerSSLCerts(boost::asio::ssl::context& ctx, boost::system::error_code& ec)
{
#if BEAST_WINDOWS
HCERTSTORE hStore = CertOpenSystemStore(0, "ROOT");
if (hStore == NULL)
{
return;
}

X509_STORE* store = X509_STORE_new();
PCCERT_CONTEXT pContext = NULL;
while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != NULL)
{
X509* x509 = d2i_X509(
NULL,
(const unsigned char**)&pContext->pbCertEncoded,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

d2i_X509 actually modifies the pointer taken as the second argument; I will fix to use a local instead.

pContext->cbCertEncoded);
if (x509 != NULL)
{
X509_STORE_add_cert(store, x509);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure how this would fail, but should we log if it does?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Probably worth doing. I'll look into making an error_code.

X509_free(x509);
}
}

CertFreeCertificateContext(pContext);
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't pContext guaranteed to be NULL here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, will fix.

CertCloseStore(hStore, 0);

SSL_CTX_set_cert_store(ctx.native_handle(), store);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to confirm, the reason we don't use SSL_CTX_set1_cert_store here is because
https://www.openssl.org/docs/manmaster/man3/SSL_CTX_set_cert_store.html

SSL_CTX_set_cert_store() does not increment the store's reference count, so it should not be used to assign an X509_STORE that is owned by another SSL_CTX.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct. I believe since we create the store above, we do not need to increment the reference count.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, it looks like the reference counted version is not available in the 1.0.* versions we are using.

#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>