CertificateOfMembership.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <algorithm>
  28. #include "CertificateOfMembership.hpp"
  29. namespace ZeroTier {
  30. void CertificateOfMembership::setQualifier(uint64_t id,uint64_t value,uint64_t maxDelta)
  31. {
  32. _signedBy.zero();
  33. for(std::vector<_Qualifier>::iterator q(_qualifiers.begin());q!=_qualifiers.end();++q) {
  34. if (q->id == id) {
  35. q->value = value;
  36. q->maxDelta = maxDelta;
  37. return;
  38. }
  39. }
  40. _qualifiers.push_back(_Qualifier(id,value,maxDelta));
  41. std::sort(_qualifiers.begin(),_qualifiers.end());
  42. }
  43. std::string CertificateOfMembership::toString() const
  44. {
  45. std::string s;
  46. s.append("1:"); // COM_UINT64_ED25519
  47. uint64_t *buf = new uint64_t[_qualifiers.size() * 3];
  48. try {
  49. unsigned int ptr = 0;
  50. for(std::vector<_Qualifier>::const_iterator q(_qualifiers.begin());q!=_qualifiers.end();++q) {
  51. buf[ptr++] = Utils::hton(q->id);
  52. buf[ptr++] = Utils::hton(q->value);
  53. buf[ptr++] = Utils::hton(q->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. _qualifiers.clear();
  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. unsigned int buflen = colonAt / 2;
  85. char *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. _qualifiers.push_back(_Qualifier());
  91. _qualifiers.back().id = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  92. _qualifiers.back().value = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  93. _qualifiers.back().maxDelta = Utils::ntoh(*((uint64_t *)bufptr)); bufptr += 8;
  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 _signedBy.zero();
  115. } else _signedBy.zero();
  116. }
  117. }
  118. std::sort(_qualifiers.begin(),_qualifiers.end());
  119. std::unique(_qualifiers.begin(),_qualifiers.end());
  120. }
  121. bool CertificateOfMembership::agreesWith(const CertificateOfMembership &other) const
  122. throw()
  123. {
  124. unsigned long myidx = 0;
  125. unsigned long otheridx = 0;
  126. while (myidx < _qualifiers.size()) {
  127. // Fail if we're at the end of other, since this means the field is
  128. // missing.
  129. if (otheridx >= other._qualifiers.size())
  130. return false;
  131. // Seek to corresponding tuple in other, ignoring tuples that
  132. // we may not have. If we run off the end of other, the tuple is
  133. // missing. This works because tuples are sorted by ID.
  134. while (other._qualifiers[otheridx].id != _qualifiers[myidx].id) {
  135. ++otheridx;
  136. if (otheridx >= other._qualifiers.size())
  137. return false;
  138. }
  139. // Compare to determine if the absolute value of the difference
  140. // between these two parameters is within our maxDelta.
  141. const uint64_t a = _qualifiers[myidx].value;
  142. const uint64_t b = other._qualifiers[myidx].value;
  143. if (((a >= b) ? (a - b) : (b - a)) > _qualifiers[myidx].maxDelta)
  144. return false;
  145. ++myidx;
  146. }
  147. return true;
  148. }
  149. bool CertificateOfMembership::sign(const Identity &with)
  150. {
  151. uint64_t *buf = new uint64_t[_qualifiers.size() * 3];
  152. unsigned int ptr = 0;
  153. for(std::vector<_Qualifier>::const_iterator q(_qualifiers.begin());q!=_qualifiers.end();++q) {
  154. buf[ptr++] = Utils::hton(q->id);
  155. buf[ptr++] = Utils::hton(q->value);
  156. buf[ptr++] = Utils::hton(q->maxDelta);
  157. }
  158. try {
  159. _signature = with.sign(buf,ptr * sizeof(uint64_t));
  160. _signedBy = with.address();
  161. delete [] buf;
  162. return true;
  163. } catch ( ... ) {
  164. _signedBy.zero();
  165. delete [] buf;
  166. return false;
  167. }
  168. }
  169. bool CertificateOfMembership::verify(const Identity &id) const
  170. {
  171. if (!_signedBy)
  172. return false;
  173. if (id.address() != _signedBy)
  174. return false;
  175. uint64_t *buf = new uint64_t[_qualifiers.size() * 3];
  176. unsigned int ptr = 0;
  177. for(std::vector<_Qualifier>::const_iterator q(_qualifiers.begin());q!=_qualifiers.end();++q) {
  178. buf[ptr++] = Utils::hton(q->id);
  179. buf[ptr++] = Utils::hton(q->value);
  180. buf[ptr++] = Utils::hton(q->maxDelta);
  181. }
  182. bool valid = false;
  183. try {
  184. valid = id.verify(buf,ptr * sizeof(uint64_t),_signature);
  185. delete [] buf;
  186. } catch ( ... ) {
  187. delete [] buf;
  188. }
  189. return valid;
  190. }
  191. } // namespace ZeroTier