CertificateOfMembership.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. void CertificateOfMembership::setQualifier(uint64_t id,uint64_t value,uint64_t maxDelta)
  21. {
  22. _signedBy.zero();
  23. for(unsigned int i=0;i<_qualifierCount;++i) {
  24. if (_qualifiers[i].id == id) {
  25. _qualifiers[i].value = value;
  26. _qualifiers[i].maxDelta = maxDelta;
  27. return;
  28. }
  29. }
  30. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  31. _qualifiers[_qualifierCount].id = id;
  32. _qualifiers[_qualifierCount].value = value;
  33. _qualifiers[_qualifierCount].maxDelta = maxDelta;
  34. ++_qualifierCount;
  35. std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
  36. }
  37. }
  38. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  39. std::string CertificateOfMembership::toString() const
  40. {
  41. char tmp[ZT_NETWORK_COM_MAX_QUALIFIERS * 32];
  42. std::string s;
  43. s.append("1:"); // COM_UINT64_ED25519
  44. uint64_t *const buf = new uint64_t[_qualifierCount * 3];
  45. try {
  46. unsigned int ptr = 0;
  47. for(unsigned int i=0;i<_qualifierCount;++i) {
  48. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  49. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  50. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  51. }
  52. s.append(Utils::hex(buf,ptr * sizeof(uint64_t),tmp));
  53. delete [] buf;
  54. } catch ( ... ) {
  55. delete [] buf;
  56. throw;
  57. }
  58. s.push_back(':');
  59. s.append(_signedBy.toString(tmp));
  60. if (_signedBy) {
  61. s.push_back(':');
  62. s.append(Utils::hex(_signature.data,ZT_C25519_SIGNATURE_LEN,tmp));
  63. }
  64. return s;
  65. }
  66. void CertificateOfMembership::fromString(const char *s)
  67. {
  68. _qualifierCount = 0;
  69. _signedBy.zero();
  70. memset(_signature.data,0,ZT_C25519_SIGNATURE_LEN);
  71. if (!*s)
  72. return;
  73. unsigned int colonAt = 0;
  74. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  75. if (!((colonAt == 1)&&(s[0] == '1'))) // COM_UINT64_ED25519?
  76. return;
  77. s += colonAt + 1;
  78. colonAt = 0;
  79. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  80. if (colonAt) {
  81. const unsigned int buflen = colonAt / 2;
  82. char *const buf = new char[buflen];
  83. unsigned int bufactual = Utils::unhex(s,colonAt,buf,buflen);
  84. char *bufptr = buf;
  85. try {
  86. while (bufactual >= 24) {
  87. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  88. _qualifiers[_qualifierCount].id = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  89. _qualifiers[_qualifierCount].value = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  90. _qualifiers[_qualifierCount].maxDelta = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  91. ++_qualifierCount;
  92. } else {
  93. bufptr += 24;
  94. }
  95. bufactual -= 24;
  96. }
  97. } catch ( ... ) {}
  98. delete [] buf;
  99. }
  100. if (s[colonAt]) {
  101. s += colonAt + 1;
  102. colonAt = 0;
  103. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  104. if (colonAt) {
  105. char addrbuf[ZT_ADDRESS_LENGTH];
  106. if (Utils::unhex(s,colonAt,addrbuf,sizeof(addrbuf)) == ZT_ADDRESS_LENGTH)
  107. _signedBy.setTo(addrbuf,ZT_ADDRESS_LENGTH);
  108. if ((_signedBy)&&(s[colonAt])) {
  109. s += colonAt + 1;
  110. colonAt = 0;
  111. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  112. if (colonAt) {
  113. if (Utils::unhex(s,colonAt,_signature.data,ZT_C25519_SIGNATURE_LEN) != ZT_C25519_SIGNATURE_LEN)
  114. _signedBy.zero();
  115. } else {
  116. _signedBy.zero();
  117. }
  118. } else {
  119. _signedBy.zero();
  120. }
  121. }
  122. }
  123. std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
  124. }
  125. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  126. bool CertificateOfMembership::agreesWith(const CertificateOfMembership &other) const
  127. {
  128. unsigned int myidx = 0;
  129. unsigned int otheridx = 0;
  130. if ((_qualifierCount == 0)||(other._qualifierCount == 0))
  131. return false;
  132. while (myidx < _qualifierCount) {
  133. // Fail if we're at the end of other, since this means the field is
  134. // missing.
  135. if (otheridx >= other._qualifierCount)
  136. return false;
  137. // Seek to corresponding tuple in other, ignoring tuples that
  138. // we may not have. If we run off the end of other, the tuple is
  139. // missing. This works because tuples are sorted by ID.
  140. while (other._qualifiers[otheridx].id != _qualifiers[myidx].id) {
  141. ++otheridx;
  142. if (otheridx >= other._qualifierCount)
  143. return false;
  144. }
  145. // Compare to determine if the absolute value of the difference
  146. // between these two parameters is within our maxDelta.
  147. const uint64_t a = _qualifiers[myidx].value;
  148. const uint64_t b = other._qualifiers[myidx].value;
  149. if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[myidx].maxDelta)
  150. return false;
  151. ++myidx;
  152. }
  153. return true;
  154. }
  155. bool CertificateOfMembership::sign(const Identity &with)
  156. {
  157. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  158. unsigned int ptr = 0;
  159. for(unsigned int i=0;i<_qualifierCount;++i) {
  160. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  161. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  162. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  163. }
  164. try {
  165. _signature = with.sign(buf,ptr * sizeof(uint64_t));
  166. _signedBy = with.address();
  167. return true;
  168. } catch ( ... ) {
  169. _signedBy.zero();
  170. return false;
  171. }
  172. }
  173. int CertificateOfMembership::verify(const RuntimeEnvironment *RR,void *tPtr) const
  174. {
  175. if ((!_signedBy)||(_signedBy != Network::controllerFor(networkId()))||(_qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  176. return -1;
  177. const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
  178. if (!id) {
  179. RR->sw->requestWhois(tPtr,RR->node->now(),_signedBy);
  180. return 1;
  181. }
  182. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  183. unsigned int ptr = 0;
  184. for(unsigned int i=0;i<_qualifierCount;++i) {
  185. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  186. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  187. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  188. }
  189. return (id.verify(buf,ptr * sizeof(uint64_t),_signature) ? 0 : -1);
  190. }
  191. } // namespace ZeroTier