CertificateOfMembership.cpp 6.0 KB

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