20 #include <ripple/app/ledger/LedgerMaster.h>
21 #include <ripple/app/main/Application.h>
22 #include <ripple/basics/base64.h>
23 #include <ripple/basics/safe_cast.h>
24 #include <ripple/beast/core/LexicalCast.h>
25 #include <ripple/beast/rfc2616.h>
26 #include <ripple/overlay/impl/Handshake.h>
27 #include <ripple/protocol/digest.h>
28 #include <boost/regex.hpp>
51 static boost::optional<base_uint<512>>
54 constexpr
std::size_t sslMinimumFinishedLength = 12;
56 unsigned char buf[1024];
57 size_t len =
get(ssl, buf,
sizeof(buf));
59 if (len < sslMinimumFinishedLength)
65 SHA512(buf, len, cookie.
data());
69 boost::optional<uint256>
72 auto const cookie1 =
hashLastMessage(ssl.native_handle(), SSL_get_finished);
75 JLOG(journal.
error()) <<
"Cookie generation: local setup not complete";
83 JLOG(journal.
error()) <<
"Cookie generation: peer setup not complete";
87 auto const result = (*cookie1 ^ *cookie2);
91 if (result == beast::zero)
94 <<
"Cookie generation: identical finished messages";
103 boost::beast::http::fields& h,
105 boost::optional<std::uint32_t> networkID,
129 h.insert(
"Session-Signature",
base64_encode(sig.data(), sig.size()));
136 h.insert(
"Remote-IP", remote_ip.to_string());
138 if (!public_ip.is_unspecified())
139 h.insert(
"Local-IP", public_ip.to_string());
146 base64_encode(cl->info().hash.begin(), cl->info().hash.size()));
150 cl->info().parentHash.begin(), cl->info().parentHash.size()));
156 boost::beast::http::fields
const& headers,
158 boost::optional<std::uint32_t> networkID,
163 if (
auto const iter = headers.find(
"Server-Domain"); iter != headers.end())
169 if (
auto const iter = headers.find(
"Network-ID"); iter != headers.end())
176 if (networkID && nid != *networkID)
180 if (
auto const iter = headers.find(
"Network-Time"); iter != headers.end())
184 TimeKeeper::duration::rep val;
197 auto const tolerance = 20s;
206 return duration_cast<std::chrono::seconds>(a - b);
207 return -duration_cast<std::chrono::seconds>(b - a);
210 auto const offset = calculateOffset(netTime, ourTime);
212 if (date::abs(offset) > tolerance)
217 if (
auto const iter = headers.find(
"Public-Key"); iter != headers.end())
219 auto pk = parseBase58<PublicKey>(
244 auto const iter = headers.find(
"Session-Signature");
246 if (iter == headers.end())
255 if (
auto const iter = headers.find(
"Local-IP"); iter != headers.end())
257 boost::system::error_code ec;
258 auto const local_ip = boost::asio::ip::address::from_string(
259 iter->value().to_string(), ec);
266 "Incorrect Local-IP: " + remote.to_string() +
" instead of " +
267 local_ip.to_string());
270 if (
auto const iter = headers.find(
"Remote-IP"); iter != headers.end())
272 boost::system::error_code ec;
273 auto const remote_ip = boost::asio::ip::address::from_string(
274 iter->value().to_string(), ec);
284 if (remote_ip != public_ip)
286 "Incorrect Remote-IP: " + public_ip.to_string() +
287 " instead of " + remote_ip.to_string());
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)
boost::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
An immutable linear range of bytes.
std::string base64_encode(std::uint8_t const *data, std::size_t len)
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
virtual TimeKeeper & timeKeeper()=0
void buildHandshake(boost::beast::http::fields &h, ripple::uint256 const &sharedValue, boost::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote_ip, Application &app)
Insert fields headers necessary for upgrading the link to the peer protocol.
static boost::optional< base_uint< 512 > > hashLastMessage(SSL const *ssl, size_t(*get)(const SSL *, void *, size_t))
Hashes the latest finished message from an SSL stream.
PublicKey verifyHandshake(boost::beast::http::fields const &headers, ripple::uint256 const &sharedValue, boost::optional< std::uint32_t > networkID, beast::IP::Address public_ip, beast::IP::Address remote, Application &app)
Validate header fields necessary for upgrading the link to the peer protocol.
Integers of any length that is a multiple of 32-bits.
boost::asio::ip::address Address
virtual LedgerMaster & getLedgerMaster()=0
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
virtual Config & config()=0
virtual std::pair< PublicKey, SecretKey > const & nodeIdentity()=0
boost::optional< uint256 > makeSharedValue(stream_type &ssl, beast::Journal journal)
Computes a shared value based on the SSL connection state.
std::string base64_decode(std::string const &data)
A generic endpoint for log messages.
bool is_public(AddressV4 const &addr)
Returns true if the address is a public routable address.
bool verifyDigest(PublicKey const &publicKey, uint256 const &digest, Slice const &sig, bool mustBeFullyCanonical)
Verify a secp256k1 signature on the digest of a message.
boost::beast::ssl_stream< socket_type > stream_type
std::shared_ptr< Ledger const > getClosedLedger()
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
std::string SERVER_DOMAIN
bool lexicalCastChecked(Out &out, In in)
Intelligently convert from one type to another.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
virtual time_point now() const override=0
Returns the estimate of wall time, in network time.
bool isProperlyFormedTomlDomain(std::string const &domain)
Determines if the given string looks like a TOML-file hosting domain.
bool is_unspecified(Address const &addr)
Returns true if the address is unspecified.
typename NetClock ::time_point time_point
typename NetClock ::duration duration
T & get(EitherAmount &amt)