|
| 1 | +#if HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC |
| 2 | +#include "cid.h" |
| 3 | +#include <crypto/crypto_util.h> |
| 4 | +#include <memory_tracker-inl.h> |
| 5 | +#include <node_mutex.h> |
| 6 | +#include <string_bytes.h> |
| 7 | + |
| 8 | +namespace node { |
| 9 | +namespace quic { |
| 10 | + |
| 11 | +// ============================================================================ |
| 12 | +// CID |
| 13 | + |
| 14 | +CID::CID() : ptr_(&cid_) { |
| 15 | + cid_.datalen = 0; |
| 16 | +} |
| 17 | + |
| 18 | +CID::CID(const ngtcp2_cid& cid) : CID(cid.data, cid.datalen) {} |
| 19 | + |
| 20 | +CID::CID(const uint8_t* data, size_t len) : CID() { |
| 21 | + DCHECK_GE(len, kMinLength); |
| 22 | + DCHECK_LE(len, kMaxLength); |
| 23 | + ngtcp2_cid_init(&cid_, data, len); |
| 24 | +} |
| 25 | + |
| 26 | +CID::CID(const ngtcp2_cid* cid) : ptr_(cid) { |
| 27 | + CHECK_NOT_NULL(cid); |
| 28 | + DCHECK_GE(cid->datalen, kMinLength); |
| 29 | + DCHECK_LE(cid->datalen, kMaxLength); |
| 30 | +} |
| 31 | + |
| 32 | +CID::CID(const CID& other) : ptr_(&cid_) { |
| 33 | + CHECK_NOT_NULL(other.ptr_); |
| 34 | + ngtcp2_cid_init(&cid_, other.ptr_->data, other.ptr_->datalen); |
| 35 | +} |
| 36 | + |
| 37 | +bool CID::operator==(const CID& other) const noexcept { |
| 38 | + if (this == &other || (length() == 0 && other.length() == 0)) return true; |
| 39 | + if (length() != other.length()) return false; |
| 40 | + return memcmp(ptr_->data, other.ptr_->data, ptr_->datalen) == 0; |
| 41 | +} |
| 42 | + |
| 43 | +bool CID::operator!=(const CID& other) const noexcept { |
| 44 | + return !(*this == other); |
| 45 | +} |
| 46 | + |
| 47 | +CID::operator const uint8_t*() const { |
| 48 | + return ptr_->data; |
| 49 | +} |
| 50 | +CID::operator const ngtcp2_cid&() const { |
| 51 | + return *ptr_; |
| 52 | +} |
| 53 | +CID::operator const ngtcp2_cid*() const { |
| 54 | + return ptr_; |
| 55 | +} |
| 56 | +CID::operator bool() const { |
| 57 | + return ptr_->datalen >= kMinLength; |
| 58 | +} |
| 59 | + |
| 60 | +size_t CID::length() const { |
| 61 | + return ptr_->datalen; |
| 62 | +} |
| 63 | + |
| 64 | +std::string CID::ToString() const { |
| 65 | + char dest[kMaxLength * 2]; |
| 66 | + size_t written = |
| 67 | + StringBytes::hex_encode(reinterpret_cast<const char*>(ptr_->data), |
| 68 | + ptr_->datalen, |
| 69 | + dest, |
| 70 | + arraysize(dest)); |
| 71 | + return std::string(dest, written); |
| 72 | +} |
| 73 | + |
| 74 | +CID CID::kInvalid{}; |
| 75 | + |
| 76 | +// ============================================================================ |
| 77 | +// CID::Hash |
| 78 | + |
| 79 | +size_t CID::Hash::operator()(const CID& cid) const { |
| 80 | + size_t hash = 0; |
| 81 | + for (size_t n = 0; n < cid.length(); n++) { |
| 82 | + hash ^= std::hash<uint8_t>{}(cid.ptr_->data[n] + 0x9e3779b9 + (hash << 6) + |
| 83 | + (hash >> 2)); |
| 84 | + } |
| 85 | + return hash; |
| 86 | +} |
| 87 | + |
| 88 | +// ============================================================================ |
| 89 | +// CID::Factory |
| 90 | + |
| 91 | +namespace { |
| 92 | +class RandomCIDFactory : public CID::Factory { |
| 93 | + public: |
| 94 | + RandomCIDFactory() = default; |
| 95 | + RandomCIDFactory(const RandomCIDFactory&) = delete; |
| 96 | + RandomCIDFactory(RandomCIDFactory&&) = delete; |
| 97 | + RandomCIDFactory& operator=(const RandomCIDFactory&) = delete; |
| 98 | + RandomCIDFactory& operator=(RandomCIDFactory&&) = delete; |
| 99 | + |
| 100 | + CID Generate(size_t length_hint) const override { |
| 101 | + DCHECK_GE(length_hint, CID::kMinLength); |
| 102 | + DCHECK_LE(length_hint, CID::kMaxLength); |
| 103 | + Mutex::ScopedLock lock(mutex_); |
| 104 | + maybe_refresh_pool(length_hint); |
| 105 | + auto start = pool_ + pos_; |
| 106 | + pos_ += length_hint; |
| 107 | + return CID(start, length_hint); |
| 108 | + } |
| 109 | + |
| 110 | + void GenerateInto(ngtcp2_cid* cid, |
| 111 | + size_t length_hint = CID::kMaxLength) const override { |
| 112 | + DCHECK_GE(length_hint, CID::kMinLength); |
| 113 | + DCHECK_LE(length_hint, CID::kMaxLength); |
| 114 | + Mutex::ScopedLock lock(mutex_); |
| 115 | + maybe_refresh_pool(length_hint); |
| 116 | + auto start = pool_ + pos_; |
| 117 | + pos_ += length_hint; |
| 118 | + ngtcp2_cid_init(cid, start, length_hint); |
| 119 | + } |
| 120 | + |
| 121 | + private: |
| 122 | + void maybe_refresh_pool(size_t length_hint) const { |
| 123 | + // We generate a pool of random data kPoolSize in length |
| 124 | + // and pull our random CID from that. If we don't have |
| 125 | + // enough random random remaining in the pool to generate |
| 126 | + // a CID of the requested size, we regenerate the pool |
| 127 | + // and reset it to zero. |
| 128 | + if (pos_ + length_hint > kPoolSize) { |
| 129 | + CHECK(crypto::CSPRNG(pool_, kPoolSize).is_ok()); |
| 130 | + pos_ = 0; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + static constexpr int kPoolSize = 4096; |
| 135 | + mutable int pos_ = kPoolSize; |
| 136 | + mutable uint8_t pool_[kPoolSize]; |
| 137 | + mutable Mutex mutex_; |
| 138 | +}; |
| 139 | +} // namespace |
| 140 | + |
| 141 | +const CID::Factory& CID::Factory::random() { |
| 142 | + static RandomCIDFactory instance; |
| 143 | + return instance; |
| 144 | +} |
| 145 | + |
| 146 | +} // namespace quic |
| 147 | +} // namespace node |
| 148 | +#endif // HAVE_OPENSSL && NODE_OPENSSL_HAS_QUIC |
0 commit comments