CertificateOfMembership.cpp 4.1 KB

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