2
0

Fingerprint.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_FINGERPRINT_HPP
  14. #define ZT_FINGERPRINT_HPP
  15. #include "Constants.hpp"
  16. #include "TriviallyCopyable.hpp"
  17. #include "Address.hpp"
  18. #include "Utils.hpp"
  19. #define ZT_FINGERPRINT_STRING_SIZE_MAX 128
  20. #define ZT_FINGERPRINT_MARSHAL_SIZE 53
  21. namespace ZeroTier {
  22. /**
  23. * Address and full hash of an identity's public keys.
  24. *
  25. * This is the same size as ZT_Fingerprint and should be cast-able back and forth.
  26. * This is checked in Tests.cpp.
  27. */
  28. class Fingerprint : public ZT_Fingerprint, public TriviallyCopyable
  29. {
  30. public:
  31. ZT_INLINE Fingerprint() noexcept
  32. { memoryZero(this); }
  33. ZT_INLINE Fingerprint(const ZT_Fingerprint &fp) noexcept
  34. { Utils::copy< sizeof(ZT_Fingerprint) >(this, &fp); }
  35. /**
  36. * @return True if hash is not all zero (missing/unspecified)
  37. */
  38. ZT_INLINE bool haveHash() const noexcept
  39. { return (!Utils::allZero(this->hash, ZT_FINGERPRINT_HASH_SIZE)); }
  40. /**
  41. * Get a base32-encoded representation of this fingerprint
  42. *
  43. * @param s Base32 string
  44. */
  45. ZT_INLINE char *toString(char s[ZT_FINGERPRINT_STRING_SIZE_MAX]) const noexcept
  46. {
  47. Address(this->address).toString(s);
  48. if (haveHash()) {
  49. s[ZT_ADDRESS_LENGTH_HEX] = '-';
  50. Utils::b32e(this->hash, ZT_FINGERPRINT_HASH_SIZE, s + (ZT_ADDRESS_LENGTH_HEX + 1), ZT_FINGERPRINT_STRING_SIZE_MAX - (ZT_ADDRESS_LENGTH_HEX + 1));
  51. }
  52. return s;
  53. }
  54. ZT_INLINE String toString() const
  55. {
  56. char tmp[ZT_FINGERPRINT_STRING_SIZE_MAX];
  57. return String(toString(tmp));
  58. }
  59. /**
  60. * Set this fingerprint to a base32-encoded string
  61. *
  62. * @param s String to decode
  63. * @return True if string appears to be valid and of the proper length (no other checking is done)
  64. */
  65. ZT_INLINE bool fromString(const char *const s) noexcept
  66. {
  67. if (!s)
  68. return false;
  69. const int l = (int)strlen(s);
  70. if (l < ZT_ADDRESS_LENGTH_HEX)
  71. return false;
  72. char a[ZT_ADDRESS_LENGTH_HEX + 1];
  73. Utils::copy< ZT_ADDRESS_LENGTH_HEX >(a, s);
  74. a[ZT_ADDRESS_LENGTH_HEX] = 0;
  75. this->address = Utils::hexStrToU64(a) & ZT_ADDRESS_MASK;
  76. if (l > (ZT_ADDRESS_LENGTH_HEX + 1)) {
  77. if (Utils::b32d(s + (ZT_ADDRESS_LENGTH_HEX + 1), this->hash, ZT_FINGERPRINT_HASH_SIZE) != ZT_FINGERPRINT_HASH_SIZE)
  78. return false;
  79. } else {
  80. Utils::zero< ZT_FINGERPRINT_HASH_SIZE >(this->hash);
  81. }
  82. return true;
  83. }
  84. /**
  85. * Check for equality with best possible specificity.
  86. *
  87. * If both fingerprints have a hash, that is compared. Otherwise just the
  88. * addresses are compared.
  89. *
  90. * @param fp Fingerprint to test
  91. */
  92. ZT_INLINE bool bestSpecificityEquals(const ZT_Fingerprint &fp) const noexcept
  93. {
  94. if (address == fp.address) {
  95. if (Utils::allZero(fp.hash, ZT_FINGERPRINT_HASH_SIZE) || Utils::allZero(hash, ZT_FINGERPRINT_HASH_SIZE))
  96. return true;
  97. return (memcmp(hash, fp.hash, ZT_FINGERPRINT_HASH_SIZE) == 0);
  98. }
  99. return false;
  100. }
  101. ZT_INLINE void zero() noexcept
  102. { memoryZero(this); }
  103. ZT_INLINE unsigned long hashCode() const noexcept
  104. { return (unsigned long)this->address; }
  105. ZT_INLINE operator bool() const noexcept
  106. { return this->address != 0; }
  107. static constexpr int marshalSizeMax() noexcept
  108. { return ZT_FINGERPRINT_MARSHAL_SIZE; }
  109. ZT_INLINE int marshal(uint8_t data[ZT_FINGERPRINT_MARSHAL_SIZE]) const noexcept
  110. {
  111. Address(this->address).copyTo(data);
  112. Utils::copy< ZT_FINGERPRINT_HASH_SIZE >(data + ZT_ADDRESS_LENGTH, this->hash);
  113. return ZT_FINGERPRINT_MARSHAL_SIZE;
  114. }
  115. ZT_INLINE int unmarshal(const uint8_t *const data, int len) noexcept
  116. {
  117. if (unlikely(len < ZT_FINGERPRINT_MARSHAL_SIZE))
  118. return -1;
  119. this->address = Address(data);
  120. Utils::copy< ZT_FINGERPRINT_HASH_SIZE >(hash, data + ZT_ADDRESS_LENGTH);
  121. return ZT_FINGERPRINT_MARSHAL_SIZE;
  122. }
  123. ZT_INLINE bool operator==(const ZT_Fingerprint &h) const noexcept
  124. { return ((this->address == h.address) && (memcmp(this->hash, h.hash, ZT_FINGERPRINT_HASH_SIZE) == 0)); }
  125. ZT_INLINE bool operator!=(const ZT_Fingerprint &h) const noexcept
  126. { return !(*this == h); }
  127. ZT_INLINE bool operator<(const ZT_Fingerprint &h) const noexcept
  128. { return ((this->address < h.address) || ((this->address == h.address) && (memcmp(this->hash, h.hash, ZT_FINGERPRINT_HASH_SIZE) < 0))); }
  129. ZT_INLINE bool operator>(const ZT_Fingerprint &h) const noexcept
  130. { return (*reinterpret_cast<const Fingerprint *>(&h) < *this); }
  131. ZT_INLINE bool operator<=(const ZT_Fingerprint &h) const noexcept
  132. { return !(*reinterpret_cast<const Fingerprint *>(&h) < *this); }
  133. ZT_INLINE bool operator>=(const ZT_Fingerprint &h) const noexcept
  134. { return !(*this < h); }
  135. };
  136. static_assert(sizeof(Fingerprint) == sizeof(ZT_Fingerprint), "size mismatch");
  137. } // namespace ZeroTier
  138. #endif