CertificateOfMembership.cpp 6.0 KB

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