Locator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 "Str.hpp"
  35. #include <algorithm>
  36. #include <vector>
  37. #define ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES 255
  38. #define ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES 255
  39. namespace ZeroTier {
  40. /**
  41. * Signed information about a node's location on the network
  42. *
  43. * A locator is a signed record that contains information about where a node
  44. * may be found. It can contain static physical addresses or virtual ZeroTier
  45. * addresses of nodes that can forward to the target node. Locator records
  46. * can be stored in signed DNS TXT record sets, in LF by roots, in caches,
  47. * etc. Version 2.x nodes can sign their own locators. Roots can create
  48. * signed locators using their own signature for version 1.x nodes. Locators
  49. * signed by the node whose location they describe always take precedence
  50. * over locators signed by other nodes.
  51. */
  52. class Locator
  53. {
  54. public:
  55. inline Locator() : _ts(0),_signatureLength(0) {}
  56. inline const Identity &id() const { return _id; }
  57. inline const Identity &signer() const { return ((_signedBy) ? _signedBy : _id); }
  58. inline int64_t timestamp() const { return _ts; }
  59. inline const std::vector<InetAddress> &phy() const { return _physical; }
  60. inline const std::vector<Identity> &virt() const { return _virtual; }
  61. /**
  62. * Add a physical address to this locator (call before finish() to build a new Locator)
  63. */
  64. inline void add(const InetAddress &ip)
  65. {
  66. if (_physical.size() < ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES)
  67. _physical.push_back(ip);
  68. }
  69. /**
  70. * Add a forwarding ZeroTier node to this locator (call before finish() to build a new Locator)
  71. */
  72. inline void add(const Identity &zt)
  73. {
  74. if (_virtual.size() < ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES)
  75. _virtual.push_back(zt);
  76. }
  77. /**
  78. * Method to be called after add() is called for each address or forwarding node
  79. *
  80. * This sets timestamp and ID information and sorts and deduplicates target
  81. * lists but does not sign the locator. The sign() method should be used after
  82. * finish().
  83. */
  84. inline void finish(const Identity &id,const int64_t ts)
  85. {
  86. _ts = ts;
  87. _id = id;
  88. std::sort(_physical.begin(),_physical.end());
  89. _physical.erase(std::unique(_physical.begin(),_physical.end()),_physical.end());
  90. std::sort(_virtual.begin(),_virtual.end());
  91. _virtual.erase(std::unique(_virtual.begin(),_virtual.end()),_virtual.end());
  92. }
  93. /**
  94. * Sign this locator (must be called after finish())
  95. */
  96. inline bool sign(const Identity &signingId)
  97. {
  98. if (!signingId.hasPrivate())
  99. return false;
  100. if (signingId == _id) {
  101. _signedBy.zero();
  102. } else {
  103. _signedBy = signingId;
  104. }
  105. Buffer<65536> *tmp = new Buffer<65536>();
  106. try {
  107. serialize(*tmp,true);
  108. _signatureLength = signingId.sign(tmp->data(),tmp->size(),_signature,ZT_SIGNATURE_BUFFER_SIZE);
  109. delete tmp;
  110. return (_signatureLength > 0);
  111. } catch ( ... ) {
  112. delete tmp;
  113. return false;
  114. }
  115. }
  116. /**
  117. * Verify this locator's signature against its embedded signing identity
  118. */
  119. inline bool verify() const
  120. {
  121. if ((_signatureLength == 0)||(_signatureLength > sizeof(_signature)))
  122. return false;
  123. Buffer<65536> *tmp = nullptr;
  124. try {
  125. tmp = new Buffer<65536>();
  126. serialize(*tmp,true);
  127. const bool ok = (_signedBy) ? _signedBy.verify(tmp->data(),tmp->size(),_signature,_signatureLength) : _id.verify(tmp->data(),tmp->size(),_signature,_signatureLength);
  128. delete tmp;
  129. return ok;
  130. } catch ( ... ) {
  131. if (tmp) delete tmp;
  132. return false;
  133. }
  134. }
  135. /**
  136. * Make DNS TXT records for this locator
  137. *
  138. * DNS TXT records are signed by an entirely separate key that is added along
  139. * with DNS names to nodes to allow them to verify DNS results. It's separate
  140. * from the locator's signature so that a single DNS record can point to more
  141. * than one locator or be served by things like geo-aware DNS.
  142. *
  143. * Right now only NIST P-384 is supported for signing DNS records. NIST EDDSA
  144. * is used here so that FIPS-only nodes can always use DNS to locate roots as
  145. * FIPS-only nodes may be required to disable non-FIPS algorithms.
  146. */
  147. inline std::vector<Str> makeTxtRecords(const uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE],const uint8_t p384SigningKeyPrivate[ZT_ECC384_PUBLIC_KEY_SIZE])
  148. {
  149. uint8_t s384[48],dnsSig[ZT_ECC384_SIGNATURE_SIZE];
  150. char enc[512];
  151. Buffer<65536> *const tmp = new Buffer<65536>();
  152. serialize(*tmp,false);
  153. SHA384(s384,tmp->data(),tmp->size());
  154. ECC384ECDSASign(p384SigningKeyPrivate,s384,dnsSig);
  155. tmp->append(dnsSig,ZT_ECC384_SIGNATURE_SIZE);
  156. // Blob must be broken into multiple TXT records that must remain sortable so they are prefixed by a hex value.
  157. // 186-byte chunks yield 248-byte base64 chunks which leaves some margin below the limit of 255.
  158. std::vector<Str> txtRecords;
  159. unsigned int txtRecNo = 0;
  160. for(unsigned int p=0;p<tmp->size();) {
  161. unsigned int chunkSize = tmp->size() - p;
  162. if (chunkSize > 186) chunkSize = 186;
  163. Utils::b64e(((const uint8_t *)tmp->data()) + p,chunkSize,enc,sizeof(enc));
  164. p += chunkSize;
  165. txtRecords.push_back(Str());
  166. txtRecords.back() << Utils::HEXCHARS[(txtRecNo >> 4) & 0xf] << Utils::HEXCHARS[txtRecNo & 0xf] << enc;
  167. ++txtRecNo;
  168. }
  169. delete tmp;
  170. return txtRecords;
  171. }
  172. /**
  173. * Decode TXT records
  174. *
  175. * TXT records can be provided as an iterator over std::string, Str, or char *
  176. * values, and TXT records can be provided in any order. Any oversize or empty
  177. * entries will be ignored.
  178. *
  179. * This method checks the decoded locator's signature using the supplied DNS TXT
  180. * record signing public key. False is returned if the TXT records are invalid,
  181. * incomplete, or fail signature check. If true is returned this Locator object
  182. * now contains the contents of the supplied TXT records.
  183. */
  184. template<typename I>
  185. inline bool decodeTxtRecords(I start,I end,const uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE])
  186. {
  187. uint8_t dec[512],s384[48];
  188. Buffer<65536> *tmp = nullptr;
  189. try {
  190. std::vector<Str> txtRecords;
  191. while (start != end) {
  192. try {
  193. if (start->length() > 2)
  194. txtRecords.push_back(*start);
  195. } catch ( ... ) {} // skip any records that trigger out of bounds exceptions
  196. ++start;
  197. }
  198. if (txtRecords.empty())
  199. return false;
  200. std::sort(txtRecords.begin(),txtRecords.end());
  201. tmp = new Buffer<65536>();
  202. for(std::vector<Str>::const_iterator i(txtRecords.begin());i!=txtRecords.end();++i)
  203. tmp->append(dec,Utils::b64d(i->c_str() + 2,dec,sizeof(dec)));
  204. if (tmp->size() <= ZT_ECC384_SIGNATURE_SIZE) {
  205. delete tmp;
  206. return false;
  207. }
  208. SHA384(s384,tmp->data(),tmp->size() - ZT_ECC384_SIGNATURE_SIZE);
  209. if (!ECC384ECDSAVerify(p384SigningKeyPublic,s384,((const uint8_t *)tmp->data()) + (tmp->size() - ZT_ECC384_SIGNATURE_SIZE))) {
  210. delete tmp;
  211. return false;
  212. }
  213. deserialize(*tmp,0);
  214. delete tmp;
  215. return verify();
  216. } catch ( ... ) {
  217. if (tmp) delete tmp;
  218. return false;
  219. }
  220. }
  221. template<unsigned int C>
  222. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  223. {
  224. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  225. b.append((uint8_t)0); // version/flags, currently 0
  226. b.append((uint64_t)_ts);
  227. _id.serialize(b,false);
  228. if (_signedBy) {
  229. b.append((uint8_t)1); // number of signers, current max is 1
  230. _signedBy.serialize(b,false); // be sure not to include private key!
  231. } else {
  232. b.append((uint8_t)0); // signer is _id
  233. }
  234. b.append((uint8_t)_physical.size());
  235. for(std::vector<InetAddress>::const_iterator i(_physical.begin());i!=_physical.end();++i)
  236. i->serialize(b);
  237. b.append((uint8_t)_virtual.size());
  238. for(std::vector<Identity>::const_iterator i(_virtual.begin());i!=_virtual.end();++i)
  239. i->serialize(b,false);
  240. if (!forSign) {
  241. b.append((uint16_t)_signatureLength);
  242. b.append(_signature,_signatureLength);
  243. }
  244. b.append((uint16_t)0); // length of additional fields, currently 0
  245. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  246. }
  247. template<unsigned int C>
  248. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  249. {
  250. unsigned int p = startAt;
  251. if (b[p++] != 0)
  252. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  253. _ts = (int64_t)b.template at<uint64_t>(p); p += 8;
  254. p += _id.deserialize(b,p);
  255. const unsigned int signerCount = b[p++];
  256. if (signerCount > 1) /* only one third party signer is currently supported */
  257. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  258. if (signerCount == 1) {
  259. p += _signedBy.deserialize(b,p);
  260. } else {
  261. _signedBy.zero();
  262. }
  263. const unsigned int physicalCount = b[p++];
  264. _physical.resize(physicalCount);
  265. for(unsigned int i=0;i<physicalCount;++i)
  266. p += _physical[i].deserialize(b,p);
  267. const unsigned int virtualCount = b[p++];
  268. _virtual.resize(virtualCount);
  269. for(unsigned int i=0;i<virtualCount;++i)
  270. p += _virtual[i].deserialize(b,p);
  271. _signatureLength = b.template at<uint16_t>(p); p += 2;
  272. if (_signatureLength > ZT_SIGNATURE_BUFFER_SIZE)
  273. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  274. memcpy(_signature,b.field(p,_signatureLength),_signatureLength);
  275. p += _signatureLength;
  276. p += b.template at<uint16_t>(p); p += 2;
  277. if (p > b.size())
  278. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  279. return (p - startAt);
  280. }
  281. inline operator bool() const { return (_id); }
  282. inline bool addressesEqual(const Locator &l) const { return ((_physical == l._physical)&&(_virtual == l._virtual)); }
  283. inline bool operator==(const Locator &l) const { return ((_ts == l._ts)&&(_id == l._id)&&(_signedBy == l._signedBy)&&(_physical == l._physical)&&(_virtual == l._virtual)&&(_signatureLength == l._signatureLength)&&(memcmp(_signature,l._signature,_signatureLength) == 0)); }
  284. inline bool operator!=(const Locator &l) const { return (!(*this == l)); }
  285. inline bool operator<(const Locator &l) const
  286. {
  287. if (_id < l._id) return true;
  288. if (_ts < l._ts) return true;
  289. if (_signedBy < l._signedBy) return true;
  290. if (_physical < l._physical) return true;
  291. if (_virtual < l._virtual) return true;
  292. return false;
  293. }
  294. inline bool operator>(const Locator &l) const { return (l < *this); }
  295. inline bool operator<=(const Locator &l) const { return (!(l < *this)); }
  296. inline bool operator>=(const Locator &l) const { return (!(*this < l)); }
  297. inline unsigned long hashCode() const { return (unsigned long)(_id.address().toInt() ^ (uint64_t)_ts); }
  298. private:
  299. int64_t _ts;
  300. Identity _id;
  301. Identity _signedBy; // signed by _id if nil/zero
  302. std::vector<InetAddress> _physical;
  303. std::vector<Identity> _virtual;
  304. unsigned int _signatureLength;
  305. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  306. };
  307. } // namespace ZeroTier
  308. #endif