CertificateOfMembership.cpp 6.6 KB

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