CertificateOfMembership.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #include "CertificateOfMembership.hpp"
  27. #include "RuntimeEnvironment.hpp"
  28. #include "Topology.hpp"
  29. #include "Switch.hpp"
  30. #include "Network.hpp"
  31. namespace ZeroTier {
  32. void CertificateOfMembership::setQualifier(uint64_t id,uint64_t value,uint64_t maxDelta)
  33. {
  34. _signedBy.zero();
  35. for(unsigned int i=0;i<_qualifierCount;++i) {
  36. if (_qualifiers[i].id == id) {
  37. _qualifiers[i].value = value;
  38. _qualifiers[i].maxDelta = maxDelta;
  39. return;
  40. }
  41. }
  42. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  43. _qualifiers[_qualifierCount].id = id;
  44. _qualifiers[_qualifierCount].value = value;
  45. _qualifiers[_qualifierCount].maxDelta = maxDelta;
  46. ++_qualifierCount;
  47. std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
  48. }
  49. }
  50. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  51. std::string CertificateOfMembership::toString() const
  52. {
  53. std::string s;
  54. s.append("1:"); // COM_UINT64_ED25519
  55. uint64_t *const buf = new uint64_t[_qualifierCount * 3];
  56. try {
  57. unsigned int ptr = 0;
  58. for(unsigned int i=0;i<_qualifierCount;++i) {
  59. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  60. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  61. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  62. }
  63. s.append(Utils::hex(buf,ptr * sizeof(uint64_t)));
  64. delete [] buf;
  65. } catch ( ... ) {
  66. delete [] buf;
  67. throw;
  68. }
  69. s.push_back(':');
  70. s.append(_signedBy.toString());
  71. if (_signedBy) {
  72. s.push_back(':');
  73. s.append(Utils::hex(_signature.data,(unsigned int)_signature.size()));
  74. }
  75. return s;
  76. }
  77. void CertificateOfMembership::fromString(const char *s)
  78. {
  79. _qualifierCount = 0;
  80. _signedBy.zero();
  81. memset(_signature.data,0,_signature.size());
  82. if (!*s)
  83. return;
  84. unsigned int colonAt = 0;
  85. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  86. if (!((colonAt == 1)&&(s[0] == '1'))) // COM_UINT64_ED25519?
  87. return;
  88. s += colonAt + 1;
  89. colonAt = 0;
  90. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  91. if (colonAt) {
  92. const unsigned int buflen = colonAt / 2;
  93. char *const buf = new char[buflen];
  94. unsigned int bufactual = Utils::unhex(s,colonAt,buf,buflen);
  95. char *bufptr = buf;
  96. try {
  97. while (bufactual >= 24) {
  98. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  99. _qualifiers[_qualifierCount].id = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  100. _qualifiers[_qualifierCount].value = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  101. _qualifiers[_qualifierCount].maxDelta = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  102. ++_qualifierCount;
  103. } else {
  104. bufptr += 24;
  105. }
  106. bufactual -= 24;
  107. }
  108. } catch ( ... ) {}
  109. delete [] buf;
  110. }
  111. if (s[colonAt]) {
  112. s += colonAt + 1;
  113. colonAt = 0;
  114. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  115. if (colonAt) {
  116. char addrbuf[ZT_ADDRESS_LENGTH];
  117. if (Utils::unhex(s,colonAt,addrbuf,sizeof(addrbuf)) == ZT_ADDRESS_LENGTH)
  118. _signedBy.setTo(addrbuf,ZT_ADDRESS_LENGTH);
  119. if ((_signedBy)&&(s[colonAt])) {
  120. s += colonAt + 1;
  121. colonAt = 0;
  122. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  123. if (colonAt) {
  124. if (Utils::unhex(s,colonAt,_signature.data,(unsigned int)_signature.size()) != _signature.size())
  125. _signedBy.zero();
  126. } else {
  127. _signedBy.zero();
  128. }
  129. } else {
  130. _signedBy.zero();
  131. }
  132. }
  133. }
  134. std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
  135. }
  136. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  137. bool CertificateOfMembership::agreesWith(const CertificateOfMembership &other) const
  138. {
  139. unsigned int myidx = 0;
  140. unsigned int otheridx = 0;
  141. if ((_qualifierCount == 0)||(other._qualifierCount == 0))
  142. return false;
  143. while (myidx < _qualifierCount) {
  144. // Fail if we're at the end of other, since this means the field is
  145. // missing.
  146. if (otheridx >= other._qualifierCount)
  147. return false;
  148. // Seek to corresponding tuple in other, ignoring tuples that
  149. // we may not have. If we run off the end of other, the tuple is
  150. // missing. This works because tuples are sorted by ID.
  151. while (other._qualifiers[otheridx].id != _qualifiers[myidx].id) {
  152. ++otheridx;
  153. if (otheridx >= other._qualifierCount)
  154. return false;
  155. }
  156. // Compare to determine if the absolute value of the difference
  157. // between these two parameters is within our maxDelta.
  158. const uint64_t a = _qualifiers[myidx].value;
  159. const uint64_t b = other._qualifiers[myidx].value;
  160. if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[myidx].maxDelta)
  161. return false;
  162. ++myidx;
  163. }
  164. return true;
  165. }
  166. bool CertificateOfMembership::sign(const Identity &with)
  167. {
  168. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  169. unsigned int ptr = 0;
  170. for(unsigned int i=0;i<_qualifierCount;++i) {
  171. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  172. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  173. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  174. }
  175. try {
  176. _signature = with.sign(buf,ptr * sizeof(uint64_t));
  177. _signedBy = with.address();
  178. return true;
  179. } catch ( ... ) {
  180. _signedBy.zero();
  181. return false;
  182. }
  183. }
  184. int CertificateOfMembership::verify(const RuntimeEnvironment *RR,void *tPtr) const
  185. {
  186. if ((!_signedBy)||(_signedBy != Network::controllerFor(networkId()))||(_qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  187. return -1;
  188. const Identity id(RR->topology->getIdentity(tPtr,_signedBy));
  189. if (!id) {
  190. RR->sw->requestWhois(tPtr,_signedBy);
  191. return 1;
  192. }
  193. uint64_t buf[ZT_NETWORK_COM_MAX_QUALIFIERS * 3];
  194. unsigned int ptr = 0;
  195. for(unsigned int i=0;i<_qualifierCount;++i) {
  196. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  197. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  198. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  199. }
  200. return (id.verify(buf,ptr * sizeof(uint64_t),_signature) ? 0 : -1);
  201. }
  202. } // namespace ZeroTier