CertificateOfMembership.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c)2019 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 "CertificateOfMembership.hpp"
  14. #include "RuntimeEnvironment.hpp"
  15. #include "Topology.hpp"
  16. #include "Switch.hpp"
  17. #include "Network.hpp"
  18. #include "Node.hpp"
  19. namespace ZeroTier {
  20. CertificateOfMembership::CertificateOfMembership(uint64_t timestamp,uint64_t timestampMaxDelta,uint64_t nwid,const Identity &issuedTo)
  21. {
  22. _qualifiers[0].id = COM_RESERVED_ID_TIMESTAMP;
  23. _qualifiers[0].value = timestamp;
  24. _qualifiers[0].maxDelta = timestampMaxDelta;
  25. _qualifiers[1].id = COM_RESERVED_ID_NETWORK_ID;
  26. _qualifiers[1].value = nwid;
  27. _qualifiers[1].maxDelta = 0;
  28. _qualifiers[2].id = COM_RESERVED_ID_ISSUED_TO;
  29. _qualifiers[2].value = issuedTo.address().toInt();
  30. _qualifiers[2].maxDelta = 0xffffffffffffffffULL;
  31. // Include hash of full identity public key in COM for hardening purposes. Pack it in
  32. // using the original COM format. Format may be revised in the future to make this cleaner.
  33. uint64_t idHash[6];
  34. issuedTo.publicKeyHash(idHash);
  35. for(unsigned long i=0;i<4;++i) {
  36. _qualifiers[i + 3].id = (uint64_t)(i + 3);
  37. _qualifiers[i + 3].value = Utils::ntoh(idHash[i]);
  38. _qualifiers[i + 3].maxDelta = 0xffffffffffffffffULL;
  39. }
  40. _qualifierCount = 7;
  41. memset(_signature.data,0,ZT_C25519_SIGNATURE_LEN);
  42. }
  43. bool CertificateOfMembership::agreesWith(const CertificateOfMembership &other, const Identity &otherIdentity) const
  44. {
  45. if ((_qualifierCount == 0)||(other._qualifierCount == 0))
  46. return false;
  47. std::map< uint64_t, uint64_t > otherFields;
  48. for(unsigned int i=0;i<other._qualifierCount;++i)
  49. otherFields[other._qualifiers[i].id] = other._qualifiers[i].value;
  50. bool fullIdentityVerification = false;
  51. for(unsigned int i=0;i<_qualifierCount;++i) {
  52. const uint64_t qid = _qualifiers[i].id;
  53. if ((qid >= 3)&&(qid <= 6))
  54. fullIdentityVerification = true;
  55. std::map< uint64_t, uint64_t >::iterator otherQ(otherFields.find(qid));
  56. if (otherQ == otherFields.end())
  57. return false;
  58. const uint64_t a = _qualifiers[i].value;
  59. const uint64_t b = otherQ->second;
  60. if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[i].maxDelta)
  61. return false;
  62. }
  63. // If this COM has a full hash of its identity, assume the other must have this as well.
  64. // Otherwise we are on a controller that does not incorporate these.
  65. if (fullIdentityVerification) {
  66. uint64_t idHash[6];
  67. otherIdentity.publicKeyHash(idHash);
  68. for(unsigned long i=0;i<4;++i) {
  69. std::map< uint64_t, uint64_t >::iterator otherQ(otherFields.find((uint64_t)(i + 3)));
  70. if (otherQ == otherFields.end())
  71. return false;
  72. if (otherQ->second != Utils::ntoh(idHash[i]))
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. bool CertificateOfMembership::sign(const Identity &with)
  79. {
  80. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  81. unsigned int ptr = 0;
  82. for(unsigned int i=0;i<_qualifierCount;++i) {
  83. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  84. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  85. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  86. }
  87. try {
  88. _signature = with.sign(buf,ptr * sizeof(uint64_t));
  89. _signedBy = with.address();
  90. return true;
  91. } catch ( ... ) {
  92. _signedBy.zero();
  93. return false;
  94. }
  95. }
  96. int CertificateOfMembership::verify(const RuntimeEnvironment *RR,void *tPtr) const
  97. {
  98. if ((!_signedBy)||(_signedBy != Network::controllerFor(networkId()))||(_qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  99. return -1;
  100. const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
  101. if (!id) {
  102. RR->sw->requestWhois(tPtr,RR->node->now(),_signedBy);
  103. return 1;
  104. }
  105. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  106. unsigned int ptr = 0;
  107. for(unsigned int i=0;i<_qualifierCount;++i) {
  108. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  109. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  110. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  111. }
  112. return (id.verify(buf,ptr * sizeof(uint64_t),_signature) ? 0 : -1);
  113. }
  114. } // namespace ZeroTier