Member.hpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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: 2024-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. const TagCredential *const t = m_remoteTags.get(id);
  68. return (((t)&&(m_isCredentialTimestampValid(nconf, *t))) ? t : (TagCredential *)0);
  69. }
  70. /**
  71. * Clean internal databases of stale entries
  72. *
  73. * @param now Current time
  74. * @param nconf Current network configuration
  75. */
  76. void clean(int64_t now,const NetworkConfig &nconf);
  77. /**
  78. * Generates a key for internal use in indexing credentials by type and credential ID
  79. */
  80. static ZT_INLINE uint64_t credentialKey(const ZT_CredentialType &t,const uint32_t i) noexcept { return (((uint64_t)t << 32U) | (uint64_t)i); }
  81. /**
  82. * Check whether the peer represented by this Membership owns a given address
  83. *
  84. * @tparam Type of resource: InetAddress or MAC
  85. * @param nconf Our network config
  86. * @param r Resource to check
  87. * @return True if this peer has a certificate of ownership for the given resource
  88. */
  89. template<typename T>
  90. ZT_INLINE bool peerOwnsAddress(const NetworkConfig &nconf, const T &r) const noexcept
  91. {
  92. if (m_isUnspoofableAddress(nconf, r))
  93. return true;
  94. for(Map< uint32_t,OwnershipCredential >::const_iterator i(m_remoteCoos.begin()); i != m_remoteCoos.end(); ++i) {
  95. if (m_isCredentialTimestampValid(nconf, i->second) && (i->second.owns(r)))
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * Check if our local COM agrees with theirs, with possible memo-ization.
  102. *
  103. * @param localCom
  104. */
  105. ZT_INLINE bool certificateOfMembershipAgress(const MembershipCredential &localCom, const Identity &remoteIdentity)
  106. {
  107. if ((m_comAgreementLocalTimestamp == localCom.timestamp()) && (m_comAgreementRemoteTimestamp == m_com.timestamp()))
  108. return true;
  109. if (m_com.agreesWith(localCom)) {
  110. // SECURITY: newer network controllers embed the full fingerprint into the COM. If we are
  111. // joined to a network managed by one of these, our COM will contain one. If it's present
  112. // we compare vs the other and require them to match. If our COM does not contain a full
  113. // identity fingerprint we compare by address only, which is a legacy mode supported for
  114. // old network controllers. Note that this is safe because the controller issues us our COM
  115. // and in so doing indicates if it's new or old. However this will go away after a while
  116. // once we can be pretty sure there are no ancient controllers around.
  117. if (localCom.issuedTo().haveHash()) {
  118. if (localCom.issuedTo() != m_com.issuedTo())
  119. return false;
  120. } else {
  121. // LEGACY: support networks run by old controllers.
  122. if (localCom.issuedTo().address != m_com.issuedTo().address)
  123. return false;
  124. }
  125. // Remember that these two COMs agreed. If any are updated this is invalidated and a full
  126. // agreement check will be done again.
  127. m_comAgreementLocalTimestamp = localCom.timestamp();
  128. m_comAgreementRemoteTimestamp = m_com.timestamp();
  129. return true;
  130. }
  131. return false;
  132. }
  133. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const MembershipCredential &com);
  134. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const TagCredential &tag);
  135. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const CapabilityCredential &cap);
  136. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const OwnershipCredential &coo);
  137. AddCredentialResult addCredential(const RuntimeEnvironment *RR,void *tPtr,const Identity &sourcePeerIdentity,const NetworkConfig &nconf,const RevocationCredential &rev);
  138. private:
  139. // This returns true if a resource is an IPv6 NDP-emulated address. These embed the ZT
  140. // address of the peer and therefore cannot be spoofed, causing peerOwnsAddress() to
  141. // always return true for them. A certificate is not required for these.
  142. ZT_INLINE bool m_isUnspoofableAddress(const NetworkConfig &nconf, const MAC &m) const noexcept { return false; }
  143. bool m_isUnspoofableAddress(const NetworkConfig &nconf, const InetAddress &ip) const noexcept;
  144. // This compares the remote credential's timestamp to the timestamp in our network config
  145. // plus or minus the permitted maximum timestamp delta.
  146. template<typename C>
  147. ZT_INLINE bool m_isCredentialTimestampValid(const NetworkConfig &nconf, const C &remoteCredential) const noexcept
  148. {
  149. const int64_t ts = remoteCredential.timestamp();
  150. if (((ts >= nconf.timestamp) ? (ts - nconf.timestamp) : (nconf.timestamp - ts)) <= nconf.credentialTimeMaxDelta) {
  151. const int64_t *threshold = m_revocations.get(credentialKey(C::credentialType(), remoteCredential.id()));
  152. return ((!threshold)||(ts > *threshold));
  153. }
  154. return false;
  155. }
  156. template<typename C>
  157. ZT_INLINE void m_cleanCredImpl(const NetworkConfig &nconf, Map<uint32_t,C> &remoteCreds)
  158. {
  159. for(typename Map<uint32_t,C>::iterator i(remoteCreds.begin());i!=remoteCreds.end();) {
  160. if (!m_isCredentialTimestampValid(nconf, i->second))
  161. remoteCreds.erase(i++);
  162. else ++i;
  163. }
  164. }
  165. // Revocation threshold for COM or 0 if none
  166. int64_t m_comRevocationThreshold;
  167. // Time we last pushed credentials
  168. int64_t m_lastPushedCredentials;
  169. // COM timestamps at which we last agreed-- used to memo-ize agreement and avoid having to recompute constantly.
  170. int64_t m_comAgreementLocalTimestamp,m_comAgreementRemoteTimestamp;
  171. // Remote member's latest network COM
  172. MembershipCredential m_com;
  173. // Revocations by credentialKey()
  174. Map<uint64_t,int64_t> m_revocations;
  175. // Remote credentials that we have received from this member (and that are valid)
  176. Map<uint32_t,TagCredential> m_remoteTags;
  177. Map<uint32_t,CapabilityCredential> m_remoteCaps;
  178. Map<uint32_t,OwnershipCredential> m_remoteCoos;
  179. public:
  180. class CapabilityIterator
  181. {
  182. public:
  183. ZT_INLINE CapabilityIterator(Member &m, const NetworkConfig &nconf) noexcept :
  184. m_hti(m.m_remoteCaps.begin()),
  185. m_parent(m),
  186. m_nconf(nconf)
  187. {
  188. }
  189. ZT_INLINE CapabilityCredential *next() noexcept
  190. {
  191. while (m_hti != m_parent.m_remoteCaps.end()) {
  192. Map< uint32_t,CapabilityCredential >::iterator i(m_hti++);
  193. if (m_parent.m_isCredentialTimestampValid(m_nconf, i->second))
  194. return &(i->second);
  195. }
  196. return nullptr;
  197. }
  198. private:
  199. Map< uint32_t,CapabilityCredential >::iterator m_hti;
  200. Member &m_parent;
  201. const NetworkConfig &m_nconf;
  202. };
  203. };
  204. } // namespace ZeroTier
  205. #endif