CertificateOfMembership.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. namespace ZeroTier {
  23. void CertificateOfMembership::setQualifier(uint64_t id,uint64_t value,uint64_t maxDelta)
  24. {
  25. _signedBy.zero();
  26. for(unsigned int i=0;i<_qualifierCount;++i) {
  27. if (_qualifiers[i].id == id) {
  28. _qualifiers[i].value = value;
  29. _qualifiers[i].maxDelta = maxDelta;
  30. return;
  31. }
  32. }
  33. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  34. _qualifiers[_qualifierCount].id = id;
  35. _qualifiers[_qualifierCount].value = value;
  36. _qualifiers[_qualifierCount].maxDelta = maxDelta;
  37. ++_qualifierCount;
  38. std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
  39. }
  40. }
  41. #ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
  42. std::string CertificateOfMembership::toString() const
  43. {
  44. std::string s;
  45. s.append("1:"); // COM_UINT64_ED25519
  46. uint64_t *const buf = new uint64_t[_qualifierCount * 3];
  47. try {
  48. unsigned int ptr = 0;
  49. for(unsigned int i=0;i<_qualifierCount;++i) {
  50. buf[ptr++] = Utils::hton(_qualifiers[i].id);
  51. buf[ptr++] = Utils::hton(_qualifiers[i].value);
  52. buf[ptr++] = Utils::hton(_qualifiers[i].maxDelta);
  53. }
  54. s.append(Utils::hex(buf,ptr * sizeof(uint64_t)));
  55. delete [] buf;
  56. } catch ( ... ) {
  57. delete [] buf;
  58. throw;
  59. }
  60. s.push_back(':');
  61. s.append(_signedBy.toString());
  62. if (_signedBy) {
  63. s.push_back(':');
  64. s.append(Utils::hex(_signature.data,(unsigned int)_signature.size()));
  65. }
  66. return s;
  67. }
  68. void CertificateOfMembership::fromString(const char *s)
  69. {
  70. _qualifierCount = 0;
  71. _signedBy.zero();
  72. memset(_signature.data,0,_signature.size());
  73. if (!*s)
  74. return;
  75. unsigned int colonAt = 0;
  76. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  77. if (!((colonAt == 1)&&(s[0] == '1'))) // COM_UINT64_ED25519?
  78. return;
  79. s += colonAt + 1;
  80. colonAt = 0;
  81. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  82. if (colonAt) {
  83. const unsigned int buflen = colonAt / 2;
  84. char *const buf = new char[buflen];
  85. unsigned int bufactual = Utils::unhex(s,colonAt,buf,buflen);
  86. char *bufptr = buf;
  87. try {
  88. while (bufactual >= 24) {
  89. if (_qualifierCount < ZT_NETWORK_COM_MAX_QUALIFIERS) {
  90. _qualifiers[_qualifierCount].id = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  91. _qualifiers[_qualifierCount].value = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  92. _qualifiers[_qualifierCount].maxDelta = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  93. ++_qualifierCount;
  94. } else {
  95. bufptr += 24;
  96. }
  97. bufactual -= 24;
  98. }
  99. } catch ( ... ) {}
  100. delete [] buf;
  101. }
  102. if (s[colonAt]) {
  103. s += colonAt + 1;
  104. colonAt = 0;
  105. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  106. if (colonAt) {
  107. char addrbuf[ZT_ADDRESS_LENGTH];
  108. if (Utils::unhex(s,colonAt,addrbuf,sizeof(addrbuf)) == ZT_ADDRESS_LENGTH)
  109. _signedBy.setTo(addrbuf,ZT_ADDRESS_LENGTH);
  110. if ((_signedBy)&&(s[colonAt])) {
  111. s += colonAt + 1;
  112. colonAt = 0;
  113. while ((s[colonAt])&&(s[colonAt] != ':')) ++colonAt;
  114. if (colonAt) {
  115. if (Utils::unhex(s,colonAt,_signature.data,(unsigned int)_signature.size()) != _signature.size())
  116. _signedBy.zero();
  117. } else {
  118. _signedBy.zero();
  119. }
  120. } else {
  121. _signedBy.zero();
  122. }
  123. }
  124. }
  125. std::sort(&(_qualifiers[0]),&(_qualifiers[_qualifierCount]));
  126. }
  127. #endif // ZT_SUPPORT_OLD_STYLE_NETCONF
  128. bool CertificateOfMembership::agreesWith(const CertificateOfMembership &other) const
  129. {
  130. unsigned int myidx = 0;
  131. unsigned int otheridx = 0;
  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) const
  174. {
  175. if ((!_signedBy)||(_qualifierCount > ZT_NETWORK_COM_MAX_QUALIFIERS))
  176. return -1;
  177. const Identity id(RR->topology->getIdentity(_signedBy));
  178. if (!id) {
  179. RR->sw->requestWhois(_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