Member.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. #include <algorithm>
  14. #include "Member.hpp"
  15. #include "RuntimeEnvironment.hpp"
  16. #include "Peer.hpp"
  17. #include "Topology.hpp"
  18. namespace ZeroTier {
  19. Member::Member() :
  20. m_comRevocationThreshold(0),
  21. m_lastPushedCredentials(0),
  22. m_comAgreementLocalTimestamp(0),
  23. m_comAgreementRemoteTimestamp(0)
  24. {
  25. }
  26. void Member::pushCredentials(const RuntimeEnvironment *RR, void *tPtr, const int64_t now, const SharedPtr< Peer > &to, const NetworkConfig &nconf)
  27. {
  28. if (!nconf.com) // sanity check
  29. return;
  30. #if 0
  31. SharedPtr<Buf> outp(new Buf());
  32. Protocol::Header &ph = outp->as<Protocol::Header>();
  33. unsigned int capPtr = 0,tagPtr = 0,cooPtr = 0;
  34. bool sendCom = true;
  35. bool complete = false;
  36. while (!complete) {
  37. ph.packetId = Protocol::getPacketId();
  38. to->address().copyTo(ph.destination);
  39. RR->identity.address().copyTo(ph.source);
  40. ph.flags = 0;
  41. ph.verb = Protocol::VERB_NETWORK_CREDENTIALS;
  42. int outl = sizeof(Protocol::Header);
  43. if (sendCom) {
  44. sendCom = false;
  45. outp->wO(outl,nconf.com);
  46. }
  47. outp->wI8(outl,0);
  48. if ((outl + ZT_CAPABILITY_MARSHAL_SIZE_MAX + 2) < ZT_PROTO_MAX_PACKET_LENGTH) {
  49. void *const capCountAt = outp->unsafeData + outl;
  50. outl += 2;
  51. unsigned int capCount = 0;
  52. while (capPtr < nconf.capabilityCount) {
  53. outp->wO(outl,nconf.capabilities[capPtr++]);
  54. ++capCount;
  55. if ((outl + ZT_CAPABILITY_MARSHAL_SIZE_MAX) >= ZT_PROTO_MAX_PACKET_LENGTH)
  56. break;
  57. }
  58. Utils::storeBigEndian(capCountAt,(uint16_t)capCount);
  59. if ((outl + ZT_TAG_MARSHAL_SIZE_MAX + 4) < ZT_PROTO_MAX_PACKET_LENGTH) {
  60. void *const tagCountAt = outp->unsafeData + outl;
  61. outl += 2;
  62. unsigned int tagCount = 0;
  63. while (tagPtr < nconf.tagCount) {
  64. outp->wO(outl,nconf.tags[tagPtr++]);
  65. ++tagCount;
  66. if ((outl + ZT_TAG_MARSHAL_SIZE_MAX) >= ZT_PROTO_MAX_PACKET_LENGTH)
  67. break;
  68. }
  69. Utils::storeBigEndian(tagCountAt,(uint16_t)tagCount);
  70. outp->wI16(outl,0); // no revocations sent here as these propagate differently
  71. if ((outl + ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX + 2) < ZT_PROTO_MAX_PACKET_LENGTH) {
  72. void *const cooCountAt = outp->unsafeData + outl;
  73. outl += 2;
  74. unsigned int cooCount = 0;
  75. while (cooPtr < nconf.certificateOfOwnershipCount) {
  76. outp->wO(outl,nconf.certificatesOfOwnership[cooPtr++]);
  77. ++cooCount;
  78. if ((outl + ZT_CERTIFICATEOFOWNERSHIP_MARSHAL_SIZE_MAX) >= ZT_PROTO_MAX_PACKET_LENGTH)
  79. break;
  80. }
  81. Utils::storeBigEndian(cooCountAt,(uint16_t)cooCount);
  82. complete = true;
  83. } else {
  84. outp->wI16(outl,0);
  85. }
  86. } else {
  87. outp->wI32(outl,0);
  88. outp->wI16(outl,0); // three zero 16-bit integers
  89. }
  90. } else {
  91. outp->wI64(outl,0); // four zero 16-bit integers
  92. }
  93. if (outl > (int)sizeof(Protocol::Header)) {
  94. outl = Protocol::compress(outp,outl);
  95. // TODO
  96. }
  97. }
  98. #endif
  99. m_lastPushedCredentials = now;
  100. }
  101. void Member::clean(const NetworkConfig &nconf)
  102. {
  103. m_cleanCredImpl< TagCredential >(nconf, m_remoteTags);
  104. m_cleanCredImpl< CapabilityCredential >(nconf, m_remoteCaps);
  105. m_cleanCredImpl< OwnershipCredential >(nconf, m_remoteCoos);
  106. }
  107. Member::AddCredentialResult Member::addCredential(const RuntimeEnvironment *RR, void *tPtr, const Identity &sourcePeerIdentity, const NetworkConfig &nconf, const MembershipCredential &com)
  108. {
  109. const int64_t newts = com.timestamp();
  110. if (newts <= m_comRevocationThreshold) {
  111. RR->t->credentialRejected(tPtr, 0xd9992121, com.networkId(), sourcePeerIdentity, com.id(), com.timestamp(), ZT_CREDENTIAL_TYPE_COM, ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED);
  112. return ADD_REJECTED;
  113. }
  114. const int64_t oldts = m_com.timestamp();
  115. if (newts < oldts) {
  116. RR->t->credentialRejected(tPtr, 0xd9928192, com.networkId(), sourcePeerIdentity, com.id(), com.timestamp(), ZT_CREDENTIAL_TYPE_COM, ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST);
  117. return ADD_REJECTED;
  118. }
  119. if ((newts == oldts) && (m_com == com))
  120. return ADD_ACCEPTED_REDUNDANT;
  121. switch (com.verify(RR, tPtr)) {
  122. default:
  123. RR->t->credentialRejected(tPtr, 0x0f198241, com.networkId(), sourcePeerIdentity, com.id(), com.timestamp(), ZT_CREDENTIAL_TYPE_COM, ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  124. return Member::ADD_REJECTED;
  125. case Credential::VERIFY_OK:
  126. m_com = com;
  127. return ADD_ACCEPTED_NEW;
  128. case Credential::VERIFY_BAD_SIGNATURE:
  129. RR->t->credentialRejected(tPtr, 0xbaf0aaaa, com.networkId(), sourcePeerIdentity, com.id(), com.timestamp(), ZT_CREDENTIAL_TYPE_COM, ZT_TRACE_CREDENTIAL_REJECTION_REASON_SIGNATURE_VERIFICATION_FAILED);
  130. return ADD_REJECTED;
  131. case Credential::VERIFY_NEED_IDENTITY:
  132. return ADD_DEFERRED_FOR_WHOIS;
  133. }
  134. }
  135. // 3/5 of the credential types have identical addCredential() code
  136. template< typename C >
  137. static ZT_INLINE Member::AddCredentialResult _addCredImpl(
  138. Map< uint32_t, C > &remoteCreds,
  139. const Map< uint64_t, int64_t > &revocations,
  140. const RuntimeEnvironment *const RR,
  141. void *const tPtr,
  142. const Identity &sourcePeerIdentity,
  143. const NetworkConfig &nconf,
  144. const C &cred)
  145. {
  146. typename Map< uint32_t, C >::const_iterator rc(remoteCreds.find(cred.id()));
  147. if (rc != remoteCreds.end()) {
  148. if (rc->second.timestamp() > cred.timestamp()) {
  149. RR->t->credentialRejected(tPtr, 0x40000001, nconf.networkId, sourcePeerIdentity, cred.id(), cred.timestamp(), C::credentialType(), ZT_TRACE_CREDENTIAL_REJECTION_REASON_OLDER_THAN_LATEST);
  150. return Member::ADD_REJECTED;
  151. }
  152. if (rc->second == cred)
  153. return Member::ADD_ACCEPTED_REDUNDANT;
  154. }
  155. typename Map< uint64_t, int64_t >::const_iterator rt(revocations.find(Member::credentialKey(C::credentialType(), cred.id())));
  156. if ((rt != revocations.end()) && (rt->second >= cred.timestamp())) {
  157. RR->t->credentialRejected(tPtr, 0x24248124, nconf.networkId, sourcePeerIdentity, cred.id(), cred.timestamp(), C::credentialType(), ZT_TRACE_CREDENTIAL_REJECTION_REASON_REVOKED);
  158. return Member::ADD_REJECTED;
  159. }
  160. switch (cred.verify(RR, tPtr)) {
  161. default:
  162. RR->t->credentialRejected(tPtr, 0x01feba012, nconf.networkId, sourcePeerIdentity, cred.id(), cred.timestamp(), C::credentialType(), ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  163. return Member::ADD_REJECTED;
  164. case 0:
  165. if (rc == remoteCreds.end())
  166. remoteCreds[cred.id()] = cred;
  167. return Member::ADD_ACCEPTED_NEW;
  168. case 1:
  169. return Member::ADD_DEFERRED_FOR_WHOIS;
  170. }
  171. }
  172. Member::AddCredentialResult Member::addCredential(const RuntimeEnvironment *RR, void *tPtr, const Identity &sourcePeerIdentity, const NetworkConfig &nconf, const TagCredential &tag)
  173. { return _addCredImpl< TagCredential >(m_remoteTags, m_revocations, RR, tPtr, sourcePeerIdentity, nconf, tag); }
  174. Member::AddCredentialResult Member::addCredential(const RuntimeEnvironment *RR, void *tPtr, const Identity &sourcePeerIdentity, const NetworkConfig &nconf, const CapabilityCredential &cap)
  175. { return _addCredImpl< CapabilityCredential >(m_remoteCaps, m_revocations, RR, tPtr, sourcePeerIdentity, nconf, cap); }
  176. Member::AddCredentialResult Member::addCredential(const RuntimeEnvironment *RR, void *tPtr, const Identity &sourcePeerIdentity, const NetworkConfig &nconf, const OwnershipCredential &coo)
  177. { return _addCredImpl< OwnershipCredential >(m_remoteCoos, m_revocations, RR, tPtr, sourcePeerIdentity, nconf, coo); }
  178. Member::AddCredentialResult Member::addCredential(const RuntimeEnvironment *RR, void *tPtr, const Identity &sourcePeerIdentity, const NetworkConfig &nconf, const RevocationCredential &rev)
  179. {
  180. int64_t *rt;
  181. switch (rev.verify(RR, tPtr)) {
  182. default:
  183. RR->t->credentialRejected(tPtr, 0x938fffff, nconf.networkId, sourcePeerIdentity, rev.id(), 0, ZT_CREDENTIAL_TYPE_REVOCATION, ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  184. return ADD_REJECTED;
  185. case 0: {
  186. const ZT_CredentialType ct = rev.typeBeingRevoked();
  187. switch (ct) {
  188. case ZT_CREDENTIAL_TYPE_COM:
  189. if (rev.threshold() > m_comRevocationThreshold) {
  190. m_comRevocationThreshold = rev.threshold();
  191. return ADD_ACCEPTED_NEW;
  192. }
  193. return ADD_ACCEPTED_REDUNDANT;
  194. case ZT_CREDENTIAL_TYPE_CAPABILITY:
  195. case ZT_CREDENTIAL_TYPE_TAG:
  196. case ZT_CREDENTIAL_TYPE_COO:
  197. rt = &(m_revocations[credentialKey(ct, rev.credentialId())]);
  198. if (*rt < rev.threshold()) {
  199. *rt = rev.threshold();
  200. m_comRevocationThreshold = rev.threshold();
  201. return ADD_ACCEPTED_NEW;
  202. }
  203. return ADD_ACCEPTED_REDUNDANT;
  204. default:
  205. RR->t->credentialRejected(tPtr, 0x0bbbb1a4, nconf.networkId, sourcePeerIdentity, rev.id(), 0, ZT_CREDENTIAL_TYPE_REVOCATION, ZT_TRACE_CREDENTIAL_REJECTION_REASON_INVALID);
  206. return ADD_REJECTED;
  207. }
  208. }
  209. case 1:
  210. return ADD_DEFERRED_FOR_WHOIS;
  211. }
  212. }
  213. bool Member::m_isUnspoofableAddress(const NetworkConfig &nconf, const InetAddress &ip) const noexcept
  214. {
  215. return (
  216. ip.isV6() &&
  217. nconf.ndpEmulation() &&
  218. (
  219. (ip == InetAddress::makeIpv66plane(nconf.networkId, m_com.issuedTo().address)) ||
  220. (ip == InetAddress::makeIpv6rfc4193(nconf.networkId, m_com.issuedTo().address))
  221. )
  222. );
  223. }
  224. } // namespace ZeroTier