CertificateOfMembership.cpp 6.2 KB

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