20 #include <ripple/overlay/impl/Handshake.h>
21 #include <ripple/app/ledger/LedgerMaster.h>
22 #include <ripple/app/main/Application.h>
23 #include <ripple/basics/base64.h>
24 #include <ripple/basics/safe_cast.h>
25 #include <ripple/beast/rfc2616.h>
26 #include <ripple/beast/core/LexicalCast.h>
27 #include <ripple/protocol/digest.h>
28 #include <boost/regex.hpp>
53 boost::optional<base_uint<512>>
55 size_t (*
get)(
const SSL *,
void *,
size_t))
57 constexpr
std::size_t sslMinimumFinishedLength = 12;
59 unsigned char buf[1024];
60 size_t len =
get(ssl, buf,
sizeof(buf));
62 if(len < sslMinimumFinishedLength)
68 SHA512 (buf, len, cookie.
data());
72 boost::optional<uint256>
76 ssl.native_handle(), SSL_get_finished);
79 JLOG (journal.
error()) <<
"Cookie generation: local setup not complete";
84 ssl.native_handle(), SSL_get_peer_finished);
87 JLOG (journal.
error()) <<
"Cookie generation: peer setup not complete";
91 auto const result = (*cookie1 ^ *cookie2);
95 if (result == beast::zero)
97 JLOG(journal.
error()) <<
"Cookie generation: identical finished messages";
106 boost::beast::http::fields& h,
108 boost::optional<std::uint32_t> networkID,
121 h.insert (
"Network-Time",
124 h.insert (
"Public-Key",
130 h.insert(
"Session-Signature",
135 h.insert (
"Remote-IP", remote_ip.to_string());
137 if (!public_ip.is_unspecified())
138 h.insert (
"Local-IP", public_ip.to_string());
144 cl->info().hash.begin(), cl->info().hash.size()));
146 cl->info().parentHash.begin(), cl->info().parentHash.size()));
152 boost::beast::http::fields
const& headers,
154 boost::optional<std::uint32_t> networkID,
161 if (
auto const iter = headers.find(
"Network-ID"); iter != headers.end())
168 if(nid != *networkID)
173 if (
auto const iter = headers.find(
"Network-Time"); iter != headers.end())
178 TimeKeeper::duration::rep val;
191 auto const tolerance = 20s;
197 auto calculateOffset = [](
201 return duration_cast<std::chrono::seconds>(a - b);
202 return - duration_cast<std::chrono::seconds>(b - a);
205 auto const offset = calculateOffset(netTime, ourTime);
207 if (date::abs(offset) > tolerance)
213 if (
auto const iter = headers.find (
"Public-Key"); iter != headers.end())
215 auto pk = parseBase58<PublicKey>(
240 auto const iter = headers.find(
"Session-Signature");
242 if (iter == headers.end())
251 if (
auto const iter = headers.find (
"Local-IP"); iter != headers.end())
253 boost::system::error_code ec;
254 auto const local_ip = boost::asio::ip::address::from_string(
255 iter->value().to_string(), ec);
262 remote.to_string() +
" instead of " + local_ip.to_string());
265 if (
auto const iter = headers.find(
"Remote-IP"); iter != headers.end())
267 boost::system::error_code ec;
268 auto const remote_ip = boost::asio::ip::address::from_string(
269 iter->value().to_string(), ec);
278 if (remote_ip != public_ip)
280 public_ip.to_string() +
" 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.
boost::beast::ssl_stream< socket_type > stream_type
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.
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)