Member.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_MEMBERSHIP_HPP
  14. #define ZT_MEMBERSHIP_HPP
  15. #include "Constants.hpp"
  16. #include "Credential.hpp"
  17. #include "Containers.hpp"
  18. #include "MembershipCredential.hpp"
  19. #include "CapabilityCredential.hpp"
  20. #include "TagCredential.hpp"
  21. #include "RevocationCredential.hpp"
  22. #include "NetworkConfig.hpp"
  23. namespace ZeroTier {
  24. class RuntimeEnvironment;
  25. class Network;
  26. /**
  27. * A container for certificates of membership and other network credentials
  28. *
  29. * This is essentially a relational join between Peer and Network.
  30. *
  31. * This class is not thread safe. It must be locked externally.
  32. */
  33. class Member
  34. {
  35. public:
  36. enum AddCredentialResult
  37. {
  38. ADD_REJECTED,
  39. ADD_ACCEPTED_NEW,
  40. ADD_ACCEPTED_REDUNDANT,
  41. ADD_DEFERRED_FOR_WHOIS
  42. };
  43. Member();
  44. /**
  45. * Send COM and other credentials to this peer
  46. *
  47. * @param RR Runtime environment
  48. * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call
  49. * @param now Current time
  50. * @param to Peer identity
  51. * @param nconf My network config
  52. */
  53. void pushCredentials(const RuntimeEnvironment *RR,void *tPtr,int64_t now,const SharedPtr<Peer> &to,const NetworkConfig &nconf);
  54. /**
  55. * @return Time we last pushed credentials to this member
  56. */
  57. ZT_INLINE int64_t lastPushedCredentials() const noexcept { return m_lastPushedCredentials; }
  58. /**
  59. * Get a remote member's tag (if we have it)
  60. *
  61. * @param nconf Network configuration
  62. * @param id Tag ID
  63. * @return Pointer to tag or NULL if not found
  64. */
  65. ZT_INLINE const TagCredential *getTag(const NetworkConfig &nconf, const uint32_t id) const noexcept
  66. {
  67. Map< uint32_t, TagCredential >::const_iterator t(m_remoteTags.find(id));
  68. return (((t != m_remoteTags.end())&&(m_isCredentialTimestampValid(nconf, t->second))) ? &(t->second) : (TagCredential *)0);
  69. }
  70. /**
  71. * Clean internal databases of stale entries
  72. *
  73. * @param nconf Current network configuration
  74. */
  75. void clean(const NetworkConfig &nconf);
  76. /**
  77. * Generates a key for internal use in indexing credentials by type and credential ID
  78. */
  79. static ZT_INLINE uint64_t credentialKey(const ZT_CredentialType &t,const uint32_t i) noexcept { return (((uint64_t)t << 32U) | (uint64_t)i); }
  80. /**
  81. * Check whether the peer represented by this Membership owns a given address
  82. *
  83. * @tparam Type of resource: InetAddress or MAC
  84. * @param nconf Our network config
  85. * @param r Resource to check
  86. * @return True if this peer has a certificate of ownership for the given resource
  87. */
  88. template<typename T>
  89. ZT_INLINE bool peerOwnsAddress(const NetworkConfig &nconf, const T &r) const noexcept
  90. {
  91. if (m_isUnspoofableAddress(nconf, r))
  92. return true;
  93. for(Map< uint32_t,OwnershipCredential >::const_iterator i(m_remoteCoos.begin()); i != m_remoteCoos.end(); ++i) {
  94. if (m_isCredentialTimestampValid(nconf, i->second) && (i->second.owns(r)))
  95. return true;
  96. }
  97. return false;
  98. }
  99. /**
  100. * Check if our local COM agrees with theirs, with possible memo-ization.
  101. *
  102. * @param localCom
  103. */
  104. ZT_INLINE bool certificateOfMembershipAgress(const MembershipCredential &localCom, const Identity &remoteIdentity)
  105. {
  106. if ((m_comAgreementLocalTimestamp == localCom.timestamp()) && (m_comAgreementRemoteTimestamp == m_com.timestamp()))
  107. return true;
  108. if (m_com.agreesWith(localCom)) {
  109. // SECURITY: newer network controllers embed the full fingerprint into the COM. If we are
  110. // joined to a network managed by one of these, our COM will contain one. If it's present
  111. // we compare vs the other and require them to match. If our COM does not contain a full
  112. // identity fingerprint we compare by address only, which is a legacy mode supported for
  113. // old network controllers. Note that this is safe because the controller issues us our COM
  114. // and in so doing indicates if it's new or old. However this will go away after a while
  115. // once we can be pretty sure there are no ancient controllers around.
  116. if (localCom.issuedTo().haveHash()) {
  117. if (localCom.issuedTo() != m_com.issuedTo())
  118. return false;
  119. } else {
  120. // LEGACY: support networks run by old controllers.
  121. if (localCom.issuedTo().address != m_com.issuedTo().address)
  122. return false;
  123. }
  124. // Remember that these two COMs agreed. If any are updated this is invalidated and a full
  125. // agreement check will be done again.
  126. m_comAgreementLocalTimestamp = localCom.timestamp();
  127. m_comAgreementRemoteTimestamp = m_com.timestamp();
  128. return true;
  129. }
  130. return false;
  131. }
  132. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const MembershipCredential &com);
  133. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const TagCredential &tag);
  134. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CapabilityCredential &cap);
  135. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const OwnershipCredential &coo);
  136. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const RevocationCredential &rev);
  137. private:
  138. // This returns true if a resource is an IPv6 NDP-emulated address. These embed the ZT
  139. // address of the peer and therefore cannot be spoofed, causing peerOwnsAddress() to
  140. // always return true for them. A certificate is not required for these.
  141. ZT_INLINE bool m_isUnspoofableAddress(const NetworkConfig &nconf, const MAC &m) const noexcept { return false; }
  142. bool m_isUnspoofableAddress(const NetworkConfig &nconf, const InetAddress &ip) const noexcept;
  143. // This compares the remote credential's timestamp to the timestamp in our network config
  144. // plus or minus the permitted maximum timestamp delta.
  145. template<typename C>
  146. ZT_INLINE bool m_isCredentialTimestampValid(const NetworkConfig &nconf, const C &remoteCredential) const noexcept
  147. {
  148. const int64_t ts = remoteCredential.timestamp();
  149. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  150. Map< uint64_t, int64_t >::const_iterator threshold(m_revocations.find(credentialKey(C::credentialType(), remoteCredential.id())));
  151. return ((threshold == m_revocations.end())||(ts > threshold->second));
  152. }
  153. return false;
  154. }
  155. template<typename C>
  156. ZT_INLINE void m_cleanCredImpl(const NetworkConfig &nconf, Map<uint32_t,C> &remoteCreds)
  157. {
  158. for(typename Map<uint32_t,C>::iterator i(remoteCreds.begin());i!=remoteCreds.end();) {
  159. if (!m_isCredentialTimestampValid(nconf, i->second))
  160. remoteCreds.erase(i++);
  161. else ++i;
  162. }
  163. }
  164. // Revocation threshold for COM or 0 if none
  165. int64_t m_comRevocationThreshold;
  166. // Time we last pushed credentials
  167. int64_t m_lastPushedCredentials;
  168. // COM timestamps at which we last agreed-- used to memo-ize agreement and avoid having to recompute constantly.
  169. int64_t m_comAgreementLocalTimestamp,m_comAgreementRemoteTimestamp;
  170. // Remote member's latest network COM
  171. MembershipCredential m_com;
  172. // Revocations by credentialKey()
  173. Map<uint64_t,int64_t> m_revocations;
  174. // Remote credentials that we have received from this member (and that are valid)
  175. Map<uint32_t,TagCredential> m_remoteTags;
  176. Map<uint32_t,CapabilityCredential> m_remoteCaps;
  177. Map<uint32_t,OwnershipCredential> m_remoteCoos;
  178. public:
  179. class CapabilityIterator
  180. {
  181. public:
  182. ZT_INLINE CapabilityIterator(Member &m, const NetworkConfig &nconf) noexcept :
  183. m_hti(m.m_remoteCaps.begin()),
  184. m_parent(m),
  185. m_nconf(nconf)
  186. {
  187. }
  188. ZT_INLINE CapabilityCredential *next() noexcept
  189. {
  190. while (m_hti != m_parent.m_remoteCaps.end()) {
  191. Map< uint32_t,CapabilityCredential >::iterator i(m_hti++);
  192. if (m_parent.m_isCredentialTimestampValid(m_nconf, i->second))
  193. return &(i->second);
  194. }
  195. return nullptr;
  196. }
  197. private:
  198. Map< uint32_t,CapabilityCredential >::iterator m_hti;
  199. Member &m_parent;
  200. const NetworkConfig &m_nconf;
  201. };
  202. };
  203. } // namespace ZeroTier
  204. #endif