Locator.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_LOCATOR_HPP
  27. #define ZT_LOCATOR_HPP
  28. #include "Constants.hpp"
  29. #include "Identity.hpp"
  30. #include "InetAddress.hpp"
  31. #include "Utils.hpp"
  32. #include "Buffer.hpp"
  33. #include "SHA512.hpp"
  34. #include <algorithm>
  35. #include <vector>
  36. #define ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES 255
  37. #define ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES 255
  38. namespace ZeroTier {
  39. /**
  40. * Signed information about a node's location on the network
  41. *
  42. * A locator can be stored in DNS as a series of TXT records with a DNS name
  43. * that includes a public key that can be used to validate the locator's
  44. * signature. That way DNS records can't be spoofed even if no DNSSEC or
  45. * anything else is present to secure DNS.
  46. */
  47. class Locator
  48. {
  49. public:
  50. Locator() : _signatureLength(0) {}
  51. inline const std::vector<InetAddress> &phy() const { return _physical; }
  52. inline const std::vector<Identity> &virt() const { return _virtual; }
  53. inline void add(const InetAddress &ip)
  54. {
  55. if (_physical.size() < ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES)
  56. _physical.push_back(ip);
  57. }
  58. inline void add(const Identity &zt)
  59. {
  60. if (_virtual.size() < ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES)
  61. _virtual.push_back(zt);
  62. }
  63. inline void finish(const Identity &id,const int64_t ts)
  64. {
  65. _ts = ts;
  66. _id = id;
  67. std::sort(_physical.begin(),_physical.end());
  68. _physical.erase(std::unique(_physical.begin(),_physical.end()),_physical.end());
  69. std::sort(_virtual.begin(),_virtual.end());
  70. _virtual.erase(std::unique(_virtual.begin(),_virtual.end()),_virtual.end());
  71. }
  72. inline bool sign(const Identity &signingId)
  73. {
  74. if (!signingId.hasPrivate())
  75. return false;
  76. if (signingId == _id) {
  77. _signedBy.zero();
  78. } else {
  79. _signedBy = signingId;
  80. }
  81. Buffer<65536> *tmp = new Buffer<65536>();
  82. try {
  83. serialize(*tmp,true);
  84. _signatureLength = signingId.sign(tmp->data(),tmp->size(),_signature,ZT_SIGNATURE_BUFFER_SIZE);
  85. delete tmp;
  86. return (_signatureLength > 0);
  87. } catch ( ... ) {
  88. delete tmp;
  89. return false;
  90. }
  91. }
  92. inline bool verify() const
  93. {
  94. if ((_signatureLength == 0)||(_signatureLength > sizeof(_signature)))
  95. return false;
  96. Buffer<16384> *tmp;
  97. try {
  98. tmp = new Buffer<16384>(); // 16384 would be huge
  99. serialize(*tmp,true);
  100. const bool ok = (_signedBy) ? _signedBy.verify(tmp->data(),tmp->size(),_signature,_signatureLength) : _id.verify(tmp->data(),tmp->size(),_signature,_signatureLength);
  101. delete tmp;
  102. return ok;
  103. } catch ( ... ) {
  104. delete tmp;
  105. return false;
  106. }
  107. }
  108. template<unsigned int C>
  109. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  110. {
  111. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  112. b.append((uint8_t)0; // version/flags, currently 0
  113. b.append((uint64_t)_ts);
  114. _id.serialise(b,false);
  115. if (_signedBy) {
  116. b.append((uint8_t)1); // number of signers
  117. _signedBy.serialize(b,false);
  118. } else {
  119. b.append((uint8_t)0); // signer is _id
  120. }
  121. b.append((uint8_t)_physical.size());
  122. for(std::vector<InetAddress>::const_iterator i(_physical.begin());i!=_physical.end();++i)
  123. i->serialize(b);
  124. b.append((uint8_t)_virtual.size());
  125. for(std::vector<Identity>::const_iterator i(_virtual.begin());i!=_virtual.end();++i)
  126. i->serialize(b,false);
  127. if (!forSign) {
  128. b.append((uint16_t)_signatureLength);
  129. b.append(_signature,_signatureLength);
  130. }
  131. b.append((uint16_t)0); // length of additional fields, currently 0
  132. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  133. }
  134. template<unsigned int C>
  135. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  136. {
  137. unsigned int p = startAt;
  138. if (b[p++] != 0)
  139. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  140. _ts = (int64_t)b.template at<uint64_t>(p); p += 8;
  141. p += _id.deserialize(b,p);
  142. const unsigned int signerCount = b[p++];
  143. if (signerCount > 1)
  144. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  145. if (signerCount == 1) {
  146. p += _signedBy.deserialize(b,p);
  147. } else {
  148. _signedBy.zero();
  149. }
  150. const unsigned int physicalCount = b[p++];
  151. _physical.resize(physicalCount);
  152. for(unsigned int i=0;i<physicalCount;++i)
  153. p += _physical[i].deserialize(b,p);
  154. const unsigned int virtualCount = b[p++];
  155. _virtual.resize(virtualCount);
  156. for(unsigned int i=0;i<virtualCount;++i)
  157. p += _virtual[i].deserialize(b,p);
  158. _signatureLen = b.template at<uint16_t>(p); p += 2;
  159. if (_signatureLength > ZT_SIGNATURE_BUFFER_SIZE)
  160. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  161. memcpy(_signature,b.field(p,_signatureLength),_signatureLength);
  162. p += _signatureLength;
  163. p += b.template at<uint16_t>(p); p += 2;
  164. if (p > b.size())
  165. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  166. return (p - startAt);
  167. }
  168. private:
  169. int64_t _ts;
  170. Identity _id;
  171. Identity _signedBy; // signed by _id if nil/zero
  172. std::vector<InetAddress> _physical;
  173. std::vector<Identity> _virtual;
  174. unsigned int _signatureLength;
  175. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  176. };
  177. } // namespace ZeroTier
  178. #endif