CertificateOfMembership.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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: 2026-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 "Network.hpp"
  15. #include "Node.hpp"
  16. #include "RuntimeEnvironment.hpp"
  17. #include "Switch.hpp"
  18. #include "Topology.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. }
  48. std::map<uint64_t, uint64_t> otherFields;
  49. for (unsigned int i = 0; i < other._qualifierCount; ++i) {
  50. otherFields[other._qualifiers[i].id] = other._qualifiers[i].value;
  51. }
  52. bool fullIdentityVerification = false;
  53. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  54. const uint64_t qid = _qualifiers[i].id;
  55. if ((qid >= 3) && (qid <= 6)) {
  56. fullIdentityVerification = true;
  57. }
  58. std::map<uint64_t, uint64_t>::iterator otherQ(otherFields.find(qid));
  59. if (otherQ == otherFields.end()) {
  60. return false;
  61. }
  62. const uint64_t a = _qualifiers[i].value;
  63. const uint64_t b = otherQ->second;
  64. if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[i].maxDelta) {
  65. return false;
  66. }
  67. }
  68. // If this COM has a full hash of its identity, assume the other must have this as well.
  69. // Otherwise we are on a controller that does not incorporate these.
  70. if (fullIdentityVerification) {
  71. uint64_t idHash[6];
  72. otherIdentity.publicKeyHash(idHash);
  73. for (unsigned long i = 0; i < 4; ++i) {
  74. std::map<uint64_t, uint64_t>::iterator otherQ(otherFields.find((uint64_t)(i + 3)));
  75. if (otherQ == otherFields.end()) {
  76. return false;
  77. }
  78. if (otherQ->second != Utils::ntoh(idHash[i])) {
  79. return false;
  80. }
  81. }
  82. }
  83. return true;
  84. }
  85. bool CertificateOfMembership::sign(const Identity& with)
  86. {
  87. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  88. unsigned int ptr = 0;
  89. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  90. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  91. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  92. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  93. }
  94. try {
  95. _signature = with.sign(buf, ptr * sizeof(uint64_t));
  96. _signedBy = with.address();
  97. return true;
  98. }
  99. catch (...) {
  100. _signedBy.zero();
  101. return false;
  102. }
  103. }
  104. int CertificateOfMembership::verify(const RuntimeEnvironment* RR, void* tPtr) const
  105. {
  106. if ((! _signedBy) || (_signedBy != Network::controllerFor(networkId())) || (_qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS)) {
  107. return -1;
  108. }
  109. const Identity id(RR->topology->getIdentity(tPtr, _signedBy));
  110. if (! id) {
  111. RR->sw->requestWhois(tPtr, RR->node->now(), _signedBy);
  112. return 1;
  113. }
  114. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  115. unsigned int ptr = 0;
  116. for (unsigned int i = 0; i < _qualifierCount; ++i) {
  117. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  118. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  119. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  120. }
  121. return (id.verify(buf, ptr * sizeof(uint64_t), _signature) ? 0 : -1);
  122. }
  123. } // namespace ZeroTier