CertificateOfMembership.cpp 5.8 KB

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