|
| 1 | +// Aleth: Ethereum C++ client, tools and libraries. |
| 2 | +// Copyright 2019 Aleth Authors. |
| 3 | +// Licensed under the GNU General Public License, Version 3. |
| 4 | + |
| 5 | +#include "ENR.h" |
| 6 | +#include <libdevcore/SHA3.h> |
| 7 | + |
| 8 | +namespace dev |
| 9 | +{ |
| 10 | +namespace p2p |
| 11 | +{ |
| 12 | +namespace |
| 13 | +{ |
| 14 | +constexpr char c_keyID[] = "id"; |
| 15 | +constexpr char c_keySec256k1[] = "secp256k1"; |
| 16 | +constexpr char c_keyIP[] = "ip"; |
| 17 | +constexpr char c_keyTCP[] = "tcp"; |
| 18 | +constexpr char c_keyUDP[] = "udp"; |
| 19 | +constexpr char c_IDV4[] = "v4"; |
| 20 | +constexpr size_t c_ENRMaxSizeBytes = 300; |
| 21 | + |
| 22 | + |
| 23 | +// Address can be either boost::asio::ip::address_v4 or boost::asio::ip::address_v6 |
| 24 | +template <class Address> |
| 25 | +bytes addressToBytes(Address const& _address) |
| 26 | +{ |
| 27 | + auto const addressBytes = _address.to_bytes(); |
| 28 | + return bytes(addressBytes.begin(), addressBytes.end()); |
| 29 | +} |
| 30 | +} // namespace |
| 31 | + |
| 32 | +ENR::ENR(RLP _rlp, VerifyFunction const& _verifyFunction) |
| 33 | +{ |
| 34 | + if (_rlp.data().size() > c_ENRMaxSizeBytes) |
| 35 | + BOOST_THROW_EXCEPTION(ENRIsTooBig()); |
| 36 | + |
| 37 | + m_signature = _rlp[0].toBytes(RLP::VeryStrict); |
| 38 | + |
| 39 | + m_seq = _rlp[1].toInt<uint64_t>(RLP::VeryStrict); |
| 40 | + |
| 41 | + // read key-values into vector first, to check the order |
| 42 | + std::vector<std::pair<std::string const, bytes>> keyValuePairs; |
| 43 | + for (size_t i = 2; i < _rlp.itemCount(); i += 2) |
| 44 | + { |
| 45 | + auto const key = _rlp[i].toString(RLP::VeryStrict); |
| 46 | + auto const value = _rlp[i + 1].data().toBytes(); |
| 47 | + keyValuePairs.push_back({key, value}); |
| 48 | + } |
| 49 | + |
| 50 | + // transfer to map, this will order them |
| 51 | + m_map.insert(keyValuePairs.begin(), keyValuePairs.end()); |
| 52 | + |
| 53 | + if (!std::equal(keyValuePairs.begin(), keyValuePairs.end(), m_map.begin())) |
| 54 | + BOOST_THROW_EXCEPTION(ENRKeysAreNotUniqueSorted()); |
| 55 | + |
| 56 | + if (!_verifyFunction(m_map, dev::ref(m_signature), dev::ref(content()))) |
| 57 | + BOOST_THROW_EXCEPTION(ENRSignatureIsInvalid()); |
| 58 | +} |
| 59 | + |
| 60 | +ENR::ENR(uint64_t _seq, std::map<std::string, bytes> const& _keyValuePairs, |
| 61 | + SignFunction const& _signFunction) |
| 62 | + : m_seq{_seq}, m_map{_keyValuePairs}, m_signature{_signFunction(dev::ref(content()))} |
| 63 | +{ |
| 64 | +} |
| 65 | + |
| 66 | +bytes ENR::content() const |
| 67 | +{ |
| 68 | + RLPStream stream{contentRlpListItemCount()}; |
| 69 | + streamContent(stream); |
| 70 | + return stream.out(); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void ENR::streamRLP(RLPStream& _s) const |
| 75 | +{ |
| 76 | + _s.appendList(contentRlpListItemCount() + 1); |
| 77 | + _s << m_signature; |
| 78 | + streamContent(_s); |
| 79 | +} |
| 80 | + |
| 81 | +void ENR::streamContent(RLPStream& _s) const |
| 82 | +{ |
| 83 | + _s << m_seq; |
| 84 | + for (auto const& keyValue : m_map) |
| 85 | + { |
| 86 | + _s << keyValue.first; |
| 87 | + _s.appendRaw(keyValue.second); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +ENR createV4ENR(Secret const& _secret, boost::asio::ip::address const& _ip, uint16_t _tcpPort, uint16_t _udpPort) |
| 92 | +{ |
| 93 | + ENR::SignFunction signFunction = [&_secret](bytesConstRef _data) { |
| 94 | + // dev::sign returns 65 bytes signature containing r,s,v values |
| 95 | + Signature s = dev::sign(_secret, sha3(_data)); |
| 96 | + // The resulting 64-byte signature is encoded as the concatenation of the r and s signature values. |
| 97 | + return bytes(&s[0], &s[64]); |
| 98 | + }; |
| 99 | + |
| 100 | + PublicCompressed const publicKey = toPublicCompressed(_secret); |
| 101 | + |
| 102 | + auto const address = _ip.is_v4() ? addressToBytes(_ip.to_v4()) : addressToBytes(_ip.to_v6()); |
| 103 | + |
| 104 | + // Values are of different types (string, bytes, uint16_t), |
| 105 | + // so we store them as RLP representation |
| 106 | + std::map<std::string, bytes> const keyValuePairs = {{c_keyID, rlp(c_IDV4)}, |
| 107 | + {c_keySec256k1, rlp(publicKey.asBytes())}, {c_keyIP, rlp(address)}, |
| 108 | + {c_keyTCP, rlp(_tcpPort)}, {c_keyUDP, rlp(_udpPort)}}; |
| 109 | + |
| 110 | + return ENR{0 /* sequence number */, keyValuePairs, signFunction}; |
| 111 | +} |
| 112 | + |
| 113 | +ENR parseV4ENR(RLP _rlp) |
| 114 | +{ |
| 115 | + ENR::VerifyFunction verifyFunction = [](std::map<std::string, bytes> const& _keyValuePairs, |
| 116 | + bytesConstRef _signature, bytesConstRef _data) { |
| 117 | + auto itID = _keyValuePairs.find(c_keyID); |
| 118 | + if (itID == _keyValuePairs.end()) |
| 119 | + return false; |
| 120 | + auto const id = RLP(itID->second).toString(RLP::VeryStrict); |
| 121 | + if (id != c_IDV4) |
| 122 | + return false; |
| 123 | + |
| 124 | + auto itKey = _keyValuePairs.find(c_keySec256k1); |
| 125 | + if (itKey == _keyValuePairs.end()) |
| 126 | + return false; |
| 127 | + |
| 128 | + auto const key = RLP(itKey->second).toHash<PublicCompressed>(RLP::VeryStrict); |
| 129 | + h512 const signature{_signature}; |
| 130 | + |
| 131 | + return dev::verify(key, signature, sha3(_data)); |
| 132 | + }; |
| 133 | + |
| 134 | + return ENR{_rlp, verifyFunction}; |
| 135 | +} |
| 136 | + |
| 137 | +std::ostream& operator<<(std::ostream& _out, ENR const& _enr) |
| 138 | +{ |
| 139 | + _out << "[ " << toHexPrefixed(_enr.signature()) << " seq=" << _enr.sequenceNumber() << " "; |
| 140 | + for (auto const& keyValue : _enr.keyValuePairs()) |
| 141 | + { |
| 142 | + _out << keyValue.first << "="; |
| 143 | + _out << toHexPrefixed(RLP{keyValue.second}.toBytes()) << " "; |
| 144 | + } |
| 145 | + _out << "]"; |
| 146 | + return _out; |
| 147 | +} |
| 148 | + |
| 149 | +} // namespace p2p |
| 150 | +} // namespace dev |
0 commit comments