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()));
133 h.insert(
"Remote-IP", remote_ip.to_string());
135 if (!public_ip.is_unspecified())
136 h.insert(
"Local-IP", public_ip.to_string());
143 base64_encode(cl->info().hash.begin(), cl->info().hash.size()));
147 cl->info().parentHash.begin(), cl->info().parentHash.size()));
153 boost::beast::http::fields
const& headers,
155 boost::optional<std::uint32_t> networkID,
162 if (
auto const iter = headers.find(
"Network-ID"); iter != headers.end())
169 if (nid != *networkID)
174 if (
auto const iter = headers.find(
"Network-Time"); iter != headers.end())
178 TimeKeeper::duration::rep val;
191 auto const tolerance = 20s;
200 return duration_cast<std::chrono::seconds>(a - b);
201 return -duration_cast<std::chrono::seconds>(b - a);
204 auto const offset = calculateOffset(netTime, ourTime);
206 if (date::abs(offset) > tolerance)
211 if (
auto const iter = headers.find(
"Public-Key"); iter != headers.end())
213 auto pk = parseBase58<PublicKey>(
238 auto const iter = headers.find(
"Session-Signature");
240 if (iter == headers.end())
249 if (
auto const iter = headers.find(
"Local-IP"); iter != headers.end())
251 boost::system::error_code ec;
252 auto const local_ip = boost::asio::ip::address::from_string(
253 iter->value().to_string(), ec);
260 "Incorrect Local-IP: " + remote.to_string() +
" instead of " +
261 local_ip.to_string());
264 if (
auto const iter = headers.find(
"Remote-IP"); iter != headers.end())
266 boost::system::error_code ec;
267 auto const remote_ip = boost::asio::ip::address::from_string(
268 iter->value().to_string(), ec);
278 if (remote_ip != public_ip)
280 "Incorrect Remote-IP: " + public_ip.to_string() +
281 " 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.
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 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.
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 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)