CertificateOfMembership.cpp 5.9 KB

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