rippled
Loading...
Searching...
No Matches
HTTPClientSSLContext.h
1#pragma once
2
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/beast/utility/Journal.h>
6#include <xrpl/net/RegisterSSLCerts.h>
7
8#include <boost/asio.hpp>
9#include <boost/asio/ip/tcp.hpp>
10#include <boost/asio/ssl.hpp>
11#include <boost/format.hpp>
12
13namespace xrpl {
14
16{
17public:
19 std::string const& sslVerifyDir,
20 std::string const& sslVerifyFile,
21 bool sslVerify,
23 boost::asio::ssl::context_base::method method = boost::asio::ssl::context::sslv23)
24 : ssl_context_{method}, j_(j), verify_{sslVerify}
25 {
26 boost::system::error_code ec;
27
28 if (sslVerifyFile.empty())
29 {
31
32 if (ec && sslVerifyDir.empty())
33 Throw<std::runtime_error>(boost::str(
34 boost::format("Failed to set_default_verify_paths: %s") % ec.message()));
35 }
36 else
37 {
38 ssl_context_.load_verify_file(sslVerifyFile);
39 }
40
41 if (!sslVerifyDir.empty())
42 {
43 ssl_context_.add_verify_path(sslVerifyDir, ec);
44
45 if (ec)
46 Throw<std::runtime_error>(
47 boost::str(boost::format("Failed to add verify path: %s") % ec.message()));
48 }
49 }
50
51 boost::asio::ssl::context&
53 {
54 return ssl_context_;
55 }
56
57 bool
58 sslVerify() const
59 {
60 return verify_;
61 }
62
75 template <
76 class T,
77 class = std::enable_if_t<
80 boost::system::error_code
81 preConnectVerify(T& strm, std::string const& host)
82 {
83 boost::system::error_code ec;
84 if (!SSL_set_tlsext_host_name(strm.native_handle(), host.c_str()))
85 {
86 ec.assign(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
87 }
88 else if (!sslVerify())
89 {
90 strm.set_verify_mode(boost::asio::ssl::verify_none, ec);
91 }
92 return ec;
93 }
94
95 template <
96 class T,
97 class = std::enable_if_t<
107 boost::system::error_code
108 postConnectVerify(T& strm, std::string const& host)
109 {
110 boost::system::error_code ec;
111
112 if (sslVerify())
113 {
114 strm.set_verify_mode(boost::asio::ssl::verify_peer, ec);
115 if (!ec)
116 {
117 strm.set_verify_callback(
118 std::bind(
119 &rfc6125_verify, host, std::placeholders::_1, std::placeholders::_2, j_),
120 ec);
121 }
122 }
123
124 return ec;
125 }
126
136 static bool
138 std::string const& domain,
139 bool preverified,
140 boost::asio::ssl::verify_context& ctx,
142 {
143 if (boost::asio::ssl::host_name_verification(domain)(preverified, ctx))
144 return true;
145
146 JLOG(j.warn()) << "Outbound SSL connection to " << domain
147 << " fails certificate verification";
148 return false;
149 }
150
151private:
152 boost::asio::ssl::context ssl_context_;
154 bool const verify_;
155};
156
157} // namespace xrpl
T bind(T... args)
T c_str(T... args)
A generic endpoint for log messages.
Definition Journal.h:40
Stream warn() const
Definition Journal.h:313
boost::system::error_code preConnectVerify(T &strm, std::string const &host)
invoked before connect/async_connect on an ssl stream to setup name verification.
boost::system::error_code postConnectVerify(T &strm, std::string const &host)
invoked after connect/async_connect but before sending data on an ssl stream - to setup name verifica...
static bool rfc6125_verify(std::string const &domain, bool preverified, boost::asio::ssl::verify_context &ctx, beast::Journal j)
callback invoked for name verification - just passes through to the asio host_name_verification (rfc6...
boost::asio::ssl::context ssl_context_
HTTPClientSSLContext(std::string const &sslVerifyDir, std::string const &sslVerifyFile, bool sslVerify, beast::Journal j, boost::asio::ssl::context_base::method method=boost::asio::ssl::context::sslv23)
boost::asio::ssl::context & context()
T empty(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
void registerSSLCerts(boost::asio::ssl::context &, boost::system::error_code &, beast::Journal j)
Register default SSL certificates.