1#ifndef XRPL_TEST_TRUSTED_PUBLISHER_SERVER_H_INCLUDED
2#define XRPL_TEST_TRUSTED_PUBLISHER_SERVER_H_INCLUDED
4#include <test/jtx/envconfig.h>
6#include <xrpl/basics/base64.h>
7#include <xrpl/basics/random.h>
8#include <xrpl/basics/strHex.h>
9#include <xrpl/protocol/PublicKey.h>
10#include <xrpl/protocol/SecretKey.h>
11#include <xrpl/protocol/Sign.h>
13#include <boost/algorithm/string/predicate.hpp>
14#include <boost/asio.hpp>
15#include <boost/asio/ip/tcp.hpp>
16#include <boost/asio/ssl/stream.hpp>
17#include <boost/beast/core/flat_buffer.hpp>
18#include <boost/beast/http.hpp>
19#include <boost/beast/ssl.hpp>
20#include <boost/beast/version.hpp>
21#include <boost/lexical_cast.hpp>
37 boost::beast::http::request<boost::beast::http::string_body>;
39 boost::beast::http::response<boost::beast::http::string_body>;
54 boost::asio::ssl::context
sslCtx_{boost::asio::ssl::context::tlsv12};
65 [](
std::size_t, boost::asio::ssl::context_base::password_purpose) {
70 boost::asio::ssl::context::default_workarounds |
71 boost::asio::ssl::context::no_sslv2 |
72 boost::asio::ssl::context::single_dh_use);
78 boost::asio::buffer(
key().
data(),
key().size()),
79 boost::asio::ssl::context::file_format::pem);
113 st[sfSequence] =
seq;
114 st[sfPublicKey] = pk;
115 st[sfSigningPubKey] = spk;
156 boost::asio::io_context& ioc,
164 bool immediateStart =
true,
180 blobInfo.
reserve(futures.size() + 1);
188 for (
auto const& val : validators)
190 data +=
"{\"validation_public_key\":\"" +
191 strHex(val.masterPublic) +
"\",\"manifest\":\"" +
192 val.manifest +
"\"},";
204 l <<
"{\"blob\":\"" << blob <<
"\"" <<
",\"signature\":\"" <<
sig
205 <<
"\"" <<
",\"manifest\":\"" <<
manifest <<
"\""
206 <<
",\"refresh_interval\": " << interval
207 <<
",\"version\":" << version <<
'}';
210 for (
auto const& future : futures)
220 for (
auto const& val : validators)
222 data +=
"{\"validation_public_key\":\"" +
223 strHex(val.masterPublic) +
"\",\"manifest\":\"" +
224 val.manifest +
"\"},";
238 for (
auto const& info : blobInfo)
240 l <<
"{\"blob\":\"" << info.blob <<
"\"" <<
",\"signature\":\""
241 << info.signature <<
"\"},";
246 l <<
"{\"blobs_v2\": [ " << blobs <<
"],\"manifest\":\"" <<
manifest
247 <<
"\"" <<
",\"refresh_interval\": " << interval
248 <<
",\"version\":" << (version + 1) <<
'}';
265 boost::asio::ip::tcp::acceptor::reuse_address(
true), ec);
267 acceptor_.listen(boost::asio::socket_base::max_listen_connections);
272 if (
auto p = wp.lock())
371-----BEGIN CERTIFICATE-----
372MIIDczCCAlugAwIBAgIBATANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJVUzEL
373MAkGA1UECAwCQ0ExFDASBgNVBAcMC0xvcyBBbmdlbGVzMRswGQYDVQQKDBJyaXBw
374bGVkLXVuaXQtdGVzdHMxFDASBgNVBAMMC2V4YW1wbGUuY29tMB4XDTIyMDIwNTIz
375NDk0M1oXDTQ5MDYyMzIzNDk0M1owazELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNh
376bGlmb3JuaWExFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xGzAZBgNVBAoMEnJpcHBs
377ZWQtdW5pdC10ZXN0czESMBAGA1UEAwwJMTI3LjAuMC4xMIIBIjANBgkqhkiG9w0B
378AQEFAAOCAQ8AMIIBCgKCAQEAueZ1hgRxwPgfeVx2AdngUYx7zYcaxcGYXyqi7izJ
379qTuBUcVcTRC/9Ip67RAEhfcgGudRS/a4Sv1ljwiRknSCcD/ZjzOFDLgbqYGSZNEs
380+T/qkwmc/L+Pbzf85HM7RjeGOd6NDQy9+oOBbUtqpTxcSGa4ln+YBFUSeoS1Aa9f
381n9vrxnWX9LgTu5dSWzH5TqFIti+Zs/v0PFjEivBIAOHPslmnzg/wCr99I6z9CAR3
382zVDe7+sxR//ivpeVE7FWjgkGixnUpZAqn69zNkJjMLNXETgOYskZdMIgbVOMr+0q
383S1Uj77mhwxKfpnB6T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uLf5vR3q6OJ62CCvzqXgHkJ+hzVN/tAvyliGTdjJrK+xv1M5a+XipO2f
452c9lb4gRbFL/DyoeoWgb1Rkv3gFf0FlCYH+ZUcYb9ZYCRlGtFgOcxJI2g+T7jSLFp
4538+hSzQ6W5Sp9L6b5iJyCww1vjBvBqzNyZMNeB4gXGtd6z9vMDSvKboTdGD7wcFB+
454mRMyNekaRw+Npy4Hjou5sx272cXHHmPCSF5TjwdaibSaGjx1k0Q50mOf7S9KG5b5
4557X1e3FekJlaD02EBEhtkXURIxogOQALdFncj
456-----END CERTIFICATE-----
465-----BEGIN DH PARAMETERS-----
466MIIBCAKCAQEAp2I2fWEUZ3sCNfitSRC/MdAhJE/bS+NO0O2tWdIdlvmIFE6B5qhC
467sGW9ojrQT8DTxBvGAcbjr/jagmlE3BV4oSnxyhP37G2mDvMOJ29J3NvFD/ZFAW0d
468BvZJ1RNvMu29NmVCyt6/jgzcqrqnami9uD93aK+zaVrlPsPEYM8xB19HXwqsEYCL
469ux2B7sqXm9Ts74HPg/EV+pcVon9phxNWxxgHlOvFc2QjZ3hXH++kzmJ4vs7N/XDB
470xbEQ+TUZ5jbJGSeBqNFKFeuOUQGJ46Io0jBSYd4rSmKUXkvElQwR+n7KF3jy1uAt
471/8hzd8tHn9TyW7Q2/CPkOA6dCXzltpOSowIBAg==
472-----END DH PARAMETERS-----
481 TrustedPublisherServer&
self;
483 boost::asio::executor_work_guard<boost::asio::executor>
work;
488 TrustedPublisherServer& self_,
518 if (
auto p = wp.lock())
528 using namespace boost::beast;
540 ssl_stream->handshake(ssl::stream_base::server, ec);
552 http::read(*ssl_stream, sb, req, ec);
554 http::read(sock, sb, req, ec);
560 res.insert(
"Server",
"TrustedPublisherServer");
561 res.version(req.version());
562 res.keep_alive(req.keep_alive());
565 if (boost::starts_with(path,
"/validators2"))
567 res.result(http::status::ok);
568 res.insert(
"Content-Type",
"application/json");
569 if (path ==
"/validators2/bad")
570 res.body() =
"{ 'bad': \"2']";
571 else if (path ==
"/validators2/missing")
572 res.body() =
"{\"version\": 2}";
576 constexpr char const* refreshPrefix =
577 "/validators2/refresh/";
578 if (boost::starts_with(path, refreshPrefix))
579 refresh = boost::lexical_cast<unsigned int>(
584 else if (boost::starts_with(path,
"/validators"))
586 res.result(http::status::ok);
587 res.insert(
"Content-Type",
"application/json");
588 if (path ==
"/validators/bad")
589 res.body() =
"{ 'bad': \"1']";
590 else if (path ==
"/validators/missing")
591 res.body() =
"{\"version\": 1}";
595 constexpr char const* refreshPrefix =
596 "/validators/refresh/";
597 if (boost::starts_with(path, refreshPrefix))
598 refresh = boost::lexical_cast<unsigned int>(
603 else if (boost::starts_with(path,
"/textfile"))
606 res.result(http::status::ok);
607 res.insert(
"Content-Type",
"text/example");
610 boost::starts_with(path,
"/textfile/huge")
613 res.content_length(cl);
614 if (req.method() == http::verb::get)
617 for (
auto i = 0; i < 1024; ++i)
618 body <<
static_cast<char>(rand_int<short>(32, 126)),
619 res.body() = body.
str();
622 else if (boost::starts_with(path,
"/sleep/"))
624 auto const sleep_sec =
625 boost::lexical_cast<unsigned int>(path.
substr(7));
629 else if (boost::starts_with(path,
"/redirect"))
631 if (boost::ends_with(path,
"/301"))
632 res.result(http::status::moved_permanently);
633 else if (boost::ends_with(path,
"/302"))
634 res.result(http::status::found);
635 else if (boost::ends_with(path,
"/307"))
636 res.result(http::status::temporary_redirect);
637 else if (boost::ends_with(path,
"/308"))
638 res.result(http::status::permanent_redirect);
641 if (boost::starts_with(path,
"/redirect_to/"))
643 location << path.
substr(13);
645 else if (!boost::starts_with(path,
"/redirect_nolo"))
648 << (ssl ?
"https://" :
"http://")
650 << (boost::starts_with(path,
"/redirect_forever/")
654 if (!location.
str().empty())
655 res.insert(
"Location", location.
str());
660 res.result(boost::beast::http::status::not_found);
661 res.insert(
"Content-Type",
"text/html");
668 res.prepare_payload();
673 res.result(boost::beast::http::status::internal_server_error);
674 res.version(req.version());
675 res.
insert(
"Server",
"TrustedPublisherServer");
676 res.insert(
"Content-Type",
"text/html");
679 res.prepare_payload();
683 write(*ssl_stream, res, ec);
685 write(sock, res, ec);
687 if (ec || req.need_eof())
693 ssl_stream->shutdown(ec);
699 boost::asio::io_context& ioc,
706 bool immediateStart =
true,
710 ioc, validators, validUntil, futures, useSSL, version, sequence);
void add(Serializer &s) const override
std::size_t size() const noexcept
void const * data() const noexcept
boost::asio::ip::address address_type
static std::string const & dh()
static std::string makeManifestString(PublicKey const &pk, SecretKey const &sk, PublicKey const &spk, SecretKey const &ssk, int seq)
PublicKey const & publisherPublic() const
boost::beast::http::request< boost::beast::http::string_body > req_type
boost::asio::ssl::context sslCtx_
PublicKey publisherPublic_
void on_accept(error_code ec)
void do_peer(int id, socket_type &&s, bool ssl)
static std::string const & key()
boost::asio::ip::tcp::acceptor acceptor_
void load_server_certificate()
~TrustedPublisherServer()
static std::string const & cert()
static Validator randomValidator()
std::function< std::string(int)> getList_
static std::string const & ca_cert()
SecretKey publisherSecret_
endpoint_type local_endpoint() const
TrustedPublisherServer(boost::asio::io_context &ioc, std::vector< Validator > const &validators, NetClock::time_point validUntil, std::vector< std::pair< NetClock::time_point, NetClock::time_point > > const &futures, bool useSSL=false, int version=1, bool immediateStart=true, int sequence=1)
boost::asio::ip::tcp::endpoint endpoint_type
boost::system::error_code error_code
boost::asio::ip::tcp::socket socket_type
boost::beast::http::response< boost::beast::http::string_body > resp_type
std::function< std::string(int)> getList2_
Set the regular signature on a JTx.
T emplace_back(T... args)
void write(nudb::detail::ostream &os, std::size_t t)
auto const data
General field definitions, or fields used in multiple transaction namespaces.
void sign(Json::Value &jv, Account const &account, Json::Value &sigObject)
Sign automatically into a specific Json field of the jv object.
char const * getEnvLocalhostAddr()
std::shared_ptr< TrustedPublisherServer > make_TrustedPublisherServer(boost::asio::io_context &ioc, std::vector< TrustedPublisherServer::Validator > const &validators, NetClock::time_point validUntil, std::vector< std::pair< NetClock::time_point, NetClock::time_point > > const &futures, bool useSSL=false, int version=1, bool immediateStart=true, int sequence=1)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
std::string strHex(FwdIt begin, FwdIt end)
std::string base64_encode(std::uint8_t const *data, std::size_t len)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
T shared_from_this(T... args)
BlobInfo(std::string b, std::string s)
lambda(int id_, TrustedPublisherServer &self_, socket_type &&sock_, bool ssl_)
boost::asio::executor_work_guard< boost::asio::executor > work
TrustedPublisherServer & self
Set the sequence number on a JTx.
T time_since_epoch(T... args)