rippled
Loading...
Searching...
No Matches
RegisterSSLCerts.cpp
1#include <xrpl/net/RegisterSSLCerts.h>
2
3#if BOOST_OS_WINDOWS
4#include <boost/asio/ssl/error.hpp>
5#include <boost/system/error_code.hpp>
6
7#include <openssl/err.h>
8#include <openssl/ssl.h>
9#include <openssl/x509.h>
10
11#include <wincrypt.h>
12
13#include <memory>
14#endif
15
16namespace xrpl {
17
18void
19registerSSLCerts(boost::asio::ssl::context& ctx, boost::system::error_code& ec, beast::Journal j)
20{
21#if BOOST_OS_WINDOWS
22 auto certStoreDelete = [](void* h) {
23 if (h != nullptr)
24 CertCloseStore(h, 0);
25 };
26 std::unique_ptr<void, decltype(certStoreDelete)> hStore{
27 CertOpenSystemStore(0, "ROOT"), certStoreDelete};
28
29 if (!hStore)
30 {
31 ec = boost::system::error_code(GetLastError(), boost::system::system_category());
32 return;
33 }
34
35 ERR_clear_error();
36
37 std::unique_ptr<X509_STORE, decltype(X509_STORE_free)*> store{
38 X509_STORE_new(), X509_STORE_free};
39
40 if (!store)
41 {
42 ec = boost::system::error_code(
43 static_cast<int>(::ERR_get_error()), boost::asio::error::get_ssl_category());
44 return;
45 }
46
47 auto warn = [&](std::string const& msg) {
48 // Buffer based on asio recommended size
49 char buf[256];
50 ::ERR_error_string_n(ec.value(), buf, sizeof(buf));
51 JLOG(j.warn()) << msg << " " << buf;
52 ::ERR_clear_error();
53 };
54
55 PCCERT_CONTEXT pContext = NULL;
56 while ((pContext = CertEnumCertificatesInStore(hStore.get(), pContext)) != NULL)
57 {
58 unsigned char const* pbCertEncoded = pContext->pbCertEncoded;
59 std::unique_ptr<X509, decltype(X509_free)*> x509{
60 d2i_X509(NULL, &pbCertEncoded, pContext->cbCertEncoded), X509_free};
61 if (!x509)
62 {
63 warn("Error decoding certificate");
64 continue;
65 }
66
67 if (X509_STORE_add_cert(store.get(), x509.get()) != 1)
68 {
69 warn("Error adding certificate");
70 }
71 else
72 {
73 // Successfully adding to the store took ownership
74 x509.release();
75 }
76 }
77
78 // This takes ownership of the store
79 SSL_CTX_set_cert_store(ctx.native_handle(), store.release());
80
81#else
82 // NOLINTNEXTLINE(bugprone-unused-return-value)
83 ctx.set_default_verify_paths(ec);
84#endif
85}
86
87} // namespace xrpl
88
89// There is a very unpleasant interaction between <wincrypt> and
90// openssl x509 types (namely the former has macros that stomp
91// on the latter), these undefs allow this TU to be safely used in
92// unity builds without messing up subsequent TUs.
93#if BOOST_OS_WINDOWS
94#undef X509_NAME
95#undef X509_EXTENSIONS
96#undef X509_CERT_PAIR
97#undef PKCS7_ISSUER_AND_SERIAL
98#undef OCSP_REQUEST
99#undef OCSP_RESPONSE
100#endif
A generic endpoint for log messages.
Definition Journal.h:40
Stream warn() const
Definition Journal.h:313
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.