2
0

CertificateOfMembership.cpp 4.2 KB

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