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>(
34 boost::str(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>(boost::str(boost::format("Failed to add verify path: %s") % ec.message()));
47 }
48 }
49
50 boost::asio::ssl::context&
52 {
53 return ssl_context_;
54 }
55
56 bool
57 sslVerify() const
58 {
59 return verify_;
60 }
61
74 template <
75 class T,
76 class = std::enable_if_t<
79 boost::system::error_code
80 preConnectVerify(T& strm, std::string const& host)
81 {
82 boost::system::error_code ec;
83 if (!SSL_set_tlsext_host_name(strm.native_handle(), host.c_str()))
84 {
85 ec.assign(static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
86 }
87 else if (!sslVerify())
88 {
89 strm.set_verify_mode(boost::asio::ssl::verify_none, ec);
90 }
91 return ec;
92 }
93
94 template <
95 class T,
96 class = std::enable_if_t<
106 boost::system::error_code
107 postConnectVerify(T& strm, std::string const& host)
108 {
109 boost::system::error_code ec;
110
111 if (sslVerify())
112 {
113 strm.set_verify_mode(boost::asio::ssl::verify_peer, ec);
114 if (!ec)
115 {
116 strm.set_verify_callback(
117 std::bind(&rfc6125_verify, host, std::placeholders::_1, std::placeholders::_2, j_), ec);
118 }
119 }
120
121 return ec;
122 }
123
133 static bool
134 rfc6125_verify(std::string const& domain, bool preverified, boost::asio::ssl::verify_context& ctx, beast::Journal j)
135 {
136 if (boost::asio::ssl::host_name_verification(domain)(preverified, ctx))
137 return true;
138
139 JLOG(j.warn()) << "Outbound SSL connection to " << domain << " fails certificate verification";
140 return false;
141 }
142
143private:
144 boost::asio::ssl::context ssl_context_;
146 bool const verify_;
147};
148
149} // 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:312
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.