Locator.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_LOCATOR_HPP
  14. #define ZT_LOCATOR_HPP
  15. #include "Constants.hpp"
  16. #include "Identity.hpp"
  17. #include "InetAddress.hpp"
  18. #include "Utils.hpp"
  19. #include "Buffer.hpp"
  20. #include "SHA512.hpp"
  21. #include "Str.hpp"
  22. #include "ScopedPtr.hpp"
  23. #include <algorithm>
  24. #include <vector>
  25. // These are absolute maximums -- real locators are never this big
  26. #define ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES 255
  27. #define ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES 255
  28. namespace ZeroTier {
  29. /**
  30. * Signed information about a node's location on the network
  31. *
  32. * A locator is a signed record that contains information about where a node
  33. * may be found. It can contain static physical addresses or virtual ZeroTier
  34. * addresses of nodes that can forward to the target node. Locator records
  35. * can be stored in signed DNS TXT record sets, in LF by roots, in caches,
  36. * etc.
  37. */
  38. class Locator
  39. {
  40. public:
  41. ZT_ALWAYS_INLINE Locator() : _ts(0),_signatureLength(0) {}
  42. ZT_ALWAYS_INLINE int64_t timestamp() const { return _ts; }
  43. ZT_ALWAYS_INLINE const Identity &id() const { return _id; }
  44. ZT_ALWAYS_INLINE const std::vector<InetAddress> &phy() const { return _physical; }
  45. ZT_ALWAYS_INLINE const std::vector<Identity> &virt() const { return _virtual; }
  46. /**
  47. * Add a physical address to this locator (call before finish() to build a new Locator)
  48. */
  49. ZT_ALWAYS_INLINE void add(const InetAddress &ip)
  50. {
  51. if (_physical.size() < ZT_LOCATOR_MAX_PHYSICAL_ADDRESSES)
  52. _physical.push_back(ip);
  53. }
  54. /**
  55. * Add a forwarding ZeroTier node to this locator (call before finish() to build a new Locator)
  56. */
  57. ZT_ALWAYS_INLINE void add(const Identity &zt)
  58. {
  59. if (_virtual.size() < ZT_LOCATOR_MAX_VIRTUAL_ADDRESSES)
  60. _virtual.push_back(zt);
  61. }
  62. /**
  63. * Method to be called after add() is called for each address or forwarding node
  64. *
  65. * @param id Identity that this locator describes (must contain private key)
  66. * @param ts Current time
  67. * @return True if completion and signature were successful
  68. */
  69. ZT_ALWAYS_INLINE bool finish(const Identity &id,const int64_t ts)
  70. {
  71. _ts = ts;
  72. _id = id;
  73. std::sort(_physical.begin(),_physical.end());
  74. _physical.erase(std::unique(_physical.begin(),_physical.end()),_physical.end());
  75. std::sort(_virtual.begin(),_virtual.end());
  76. _virtual.erase(std::unique(_virtual.begin(),_virtual.end()),_virtual.end());
  77. try {
  78. ScopedPtr< Buffer<65536> > tmp(new Buffer<65536>());
  79. serialize(*tmp,true);
  80. _signatureLength = id.sign(tmp->data(),tmp->size(),_signature,ZT_SIGNATURE_BUFFER_SIZE);
  81. return (_signatureLength > 0);
  82. } catch ( ... ) {
  83. return false;
  84. }
  85. }
  86. /**
  87. * Verify this locator's signature against its embedded signing identity
  88. */
  89. ZT_ALWAYS_INLINE bool verify() const
  90. {
  91. if ((_signatureLength == 0)||(_signatureLength > sizeof(_signature)))
  92. return false;
  93. try {
  94. ScopedPtr< Buffer<65536> > tmp(new Buffer<65536>());
  95. serialize(*tmp,true);
  96. return _id.verify(tmp->data(),tmp->size(),_signature,_signatureLength);
  97. } catch ( ... ) {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Make a DNS name contiaining a public key that can sign DNS entries
  103. *
  104. * This generates the initial fields of a DNS name that contains an
  105. * encoded public key. Users may append any domain suffix to this name.
  106. *
  107. * @return First field(s) of DNS name
  108. */
  109. static inline Str makeSecureDnsName(const uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE])
  110. {
  111. uint8_t tmp[ZT_ECC384_PUBLIC_KEY_SIZE+2];
  112. memcpy(tmp,p384SigningKeyPublic,ZT_ECC384_PUBLIC_KEY_SIZE);
  113. const uint16_t crc = Utils::crc16(tmp,ZT_ECC384_PUBLIC_KEY_SIZE);
  114. tmp[ZT_ECC384_PUBLIC_KEY_SIZE-2] = (uint8_t)(crc >> 8);
  115. tmp[ZT_ECC384_PUBLIC_KEY_SIZE-1] = (uint8_t)(crc);
  116. Str name;
  117. char b32[128];
  118. Utils::b32e(tmp,35,b32,sizeof(b32));
  119. name << "ztl-";
  120. name << b32;
  121. Utils::b32e(tmp + 35,(ZT_ECC384_PUBLIC_KEY_SIZE+2) - 35,b32,sizeof(b32));
  122. name << ".ztl-";
  123. name << b32;
  124. return name;
  125. }
  126. /**
  127. * This searches for an extracts a public key from a DNS name, if one is present.
  128. *
  129. * @return True if a key was found and successfully decoded
  130. */
  131. static inline bool decodeSecureDnsName(const char *name,uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE])
  132. {
  133. uint8_t b32[128];
  134. unsigned int b32ptr = 0;
  135. char tmp[1024];
  136. Utils::scopy(tmp,sizeof(tmp),name);
  137. bool ok = false;
  138. for(char *saveptr=(char *)0,*p=Utils::stok(tmp,".",&saveptr);p;p=Utils::stok((char *)0,".",&saveptr)) {
  139. if (b32ptr >= sizeof(b32))
  140. break;
  141. if ((strlen(p) <= 4)||(memcmp(p,"ztl-",4) != 0))
  142. continue;
  143. int s = Utils::b32d(p + 4,b32 + b32ptr,sizeof(b32) - b32ptr);
  144. if (s > 0) {
  145. b32ptr += (unsigned int)s;
  146. if (b32ptr > 2) {
  147. const uint16_t crc = Utils::crc16(b32,b32ptr);
  148. if ((b32[b32ptr-2] == (uint8_t)(crc >> 8))&&(b32[b32ptr-1] == (uint8_t)(crc & 0xff))) {
  149. ok = true;
  150. break;
  151. }
  152. }
  153. } else break;
  154. }
  155. if (ok) {
  156. if (b32ptr == (ZT_ECC384_PUBLIC_KEY_SIZE + 2)) {
  157. memcpy(p384SigningKeyPublic,b32,ZT_ECC384_PUBLIC_KEY_SIZE);
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * Make DNS TXT records for this locator
  165. *
  166. * DNS TXT records are signed by an entirely separate key that is added along
  167. * with DNS names to nodes to allow them to verify DNS results. It's separate
  168. * from the locator's signature so that a single DNS record can point to more
  169. * than one locator or be served by things like geo-aware DNS.
  170. *
  171. * Right now only NIST P-384 is supported for signing DNS records. NIST EDDSA
  172. * is used here so that FIPS-only nodes can always use DNS to locate roots as
  173. * FIPS-only nodes may be required to disable non-FIPS algorithms.
  174. */
  175. inline std::vector<Str> makeTxtRecords(const uint8_t p384SigningKeyPrivate[ZT_ECC384_PUBLIC_KEY_SIZE])
  176. {
  177. uint8_t s384[48];
  178. char enc[256];
  179. ScopedPtr< Buffer<65536> > tmp(new Buffer<65536>());
  180. serialize(*tmp,false);
  181. SHA384(s384,tmp->data(),tmp->size());
  182. const unsigned int sigLocation = tmp->size();
  183. tmp->addSize(ZT_ECC384_SIGNATURE_SIZE);
  184. ECC384ECDSASign(p384SigningKeyPrivate,s384,((uint8_t *)tmp->unsafeData()) + sigLocation);
  185. // Blob must be broken into multiple TXT records that must remain sortable so they are prefixed by a hex value.
  186. // 186-byte chunks yield 248-byte base64 chunks which leaves some margin below the limit of 255.
  187. std::vector<Str> txtRecords;
  188. unsigned int txtRecNo = 0;
  189. for(unsigned int p=0;p<tmp->size();) {
  190. unsigned int chunkSize = tmp->size() - p;
  191. if (chunkSize > 186) chunkSize = 186;
  192. Utils::b64e(((const uint8_t *)tmp->data()) + p,chunkSize,enc,sizeof(enc));
  193. p += chunkSize;
  194. txtRecords.push_back(Str());
  195. txtRecords.back() << Utils::HEXCHARS[(txtRecNo >> 4) & 0xf] << Utils::HEXCHARS[txtRecNo & 0xf] << enc;
  196. ++txtRecNo;
  197. }
  198. return txtRecords;
  199. }
  200. /**
  201. * Decode TXT records
  202. *
  203. * TXT records can be provided as an iterator over std::string, Str, or char *
  204. * values, and TXT records can be provided in any order. Any oversize or empty
  205. * entries will be ignored.
  206. *
  207. * This method checks the decoded locator's signature using the supplied DNS TXT
  208. * record signing public key. False is returned if the TXT records are invalid,
  209. * incomplete, or fail signature check. If true is returned this Locator object
  210. * now contains the contents of the supplied TXT records.
  211. *
  212. * @return True if new Locator is valid
  213. */
  214. template<typename I>
  215. inline bool decodeTxtRecords(const Str &dnsName,I start,I end)
  216. {
  217. uint8_t dec[256],s384[48];
  218. try {
  219. std::vector<Str> txtRecords;
  220. while (start != end) {
  221. try {
  222. if (start->length() > 2)
  223. txtRecords.push_back(*start);
  224. } catch ( ... ) {} // skip any records that trigger out of bounds exceptions
  225. ++start;
  226. }
  227. if (txtRecords.empty())
  228. return false;
  229. std::sort(txtRecords.begin(),txtRecords.end());
  230. ScopedPtr< Buffer<65536> > tmp(new Buffer<65536>());
  231. for(std::vector<Str>::const_iterator i(txtRecords.begin());i!=txtRecords.end();++i)
  232. tmp->append(dec,Utils::b64d(i->c_str() + 2,dec,sizeof(dec)));
  233. uint8_t p384SigningKeyPublic[ZT_ECC384_PUBLIC_KEY_SIZE];
  234. if (decodeSecureDnsName(dnsName.c_str(),p384SigningKeyPublic)) {
  235. if (tmp->size() <= ZT_ECC384_SIGNATURE_SIZE)
  236. return false;
  237. SHA384(s384,tmp->data(),tmp->size() - ZT_ECC384_SIGNATURE_SIZE);
  238. if (!ECC384ECDSAVerify(p384SigningKeyPublic,s384,((const uint8_t *)tmp->data()) + (tmp->size() - ZT_ECC384_SIGNATURE_SIZE)))
  239. return false;
  240. }
  241. deserialize(*tmp,0);
  242. return verify();
  243. } catch ( ... ) {
  244. return false;
  245. }
  246. }
  247. inline bool deserialize(const void *data,unsigned int len)
  248. {
  249. ScopedPtr< Buffer<65536> > tmp(new Buffer<65536>());
  250. tmp->append(data,len);
  251. try {
  252. deserialize(*tmp,0);
  253. return true;
  254. } catch ( ... ) {
  255. return false;
  256. }
  257. }
  258. template<unsigned int C>
  259. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  260. {
  261. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  262. b.append((uint8_t)0); // version/flags, currently 0
  263. b.append((uint64_t)_ts);
  264. _id.serialize(b,false);
  265. b.append((uint8_t)_physical.size());
  266. for(std::vector<InetAddress>::const_iterator i(_physical.begin());i!=_physical.end();++i)
  267. i->serialize(b);
  268. b.append((uint8_t)_virtual.size());
  269. for(std::vector<Identity>::const_iterator i(_virtual.begin());i!=_virtual.end();++i)
  270. i->serialize(b,false);
  271. if (!forSign) {
  272. b.append((uint16_t)_signatureLength);
  273. b.append(_signature,_signatureLength);
  274. }
  275. b.append((uint16_t)0); // length of additional fields, currently 0
  276. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  277. }
  278. template<unsigned int C>
  279. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  280. {
  281. unsigned int p = startAt;
  282. if (b[p++] != 0)
  283. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  284. _ts = (int64_t)b.template at<uint64_t>(p); p += 8;
  285. p += _id.deserialize(b,p);
  286. const unsigned int physicalCount = b[p++];
  287. _physical.resize(physicalCount);
  288. for(unsigned int i=0;i<physicalCount;++i)
  289. p += _physical[i].deserialize(b,p);
  290. const unsigned int virtualCount = b[p++];
  291. _virtual.resize(virtualCount);
  292. for(unsigned int i=0;i<virtualCount;++i)
  293. p += _virtual[i].deserialize(b,p);
  294. _signatureLength = b.template at<uint16_t>(p); p += 2;
  295. if (_signatureLength > ZT_SIGNATURE_BUFFER_SIZE)
  296. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  297. memcpy(_signature,b.field(p,_signatureLength),_signatureLength);
  298. p += _signatureLength;
  299. p += b.template at<uint16_t>(p) + 2;
  300. return (p - startAt);
  301. }
  302. ZT_ALWAYS_INLINE operator bool() const { return (_id); }
  303. ZT_ALWAYS_INLINE bool addressesEqual(const Locator &l) const { return ((_physical == l._physical)&&(_virtual == l._virtual)); }
  304. ZT_ALWAYS_INLINE bool operator==(const Locator &l) const
  305. {
  306. return (
  307. (_ts == l._ts)&&
  308. (_id == l._id)&&
  309. (_physical == l._physical)&&
  310. (_virtual == l._virtual)&&
  311. (_signatureLength == l._signatureLength)&&
  312. (memcmp(_signature,l._signature,_signatureLength) == 0));
  313. }
  314. ZT_ALWAYS_INLINE bool operator!=(const Locator &l) const { return (!(*this == l)); }
  315. ZT_ALWAYS_INLINE bool operator<(const Locator &l) const
  316. {
  317. if (_ts < l._ts) return true; else if (_ts > l._ts) return false;
  318. if (_id < l._id) return true; else if (_id > l._id) return false;
  319. if (_physical < l._physical) return true; else if (_physical > l._physical) return false;
  320. if (_virtual < l._virtual) return true; else if (_virtual > l._virtual) return false;
  321. if (_signatureLength < l._signatureLength) return true;
  322. return (_signatureLength == l._signatureLength) ? (memcmp(_signature,l._signature,_signatureLength) < 0) : false;
  323. }
  324. ZT_ALWAYS_INLINE bool operator>(const Locator &l) const { return (l < *this); }
  325. ZT_ALWAYS_INLINE bool operator<=(const Locator &l) const { return (!(l < *this)); }
  326. ZT_ALWAYS_INLINE bool operator>=(const Locator &l) const { return (!(*this < l)); }
  327. ZT_ALWAYS_INLINE unsigned long hashCode() const { return (unsigned long)(_id.address().toInt() ^ (uint64_t)_ts); }
  328. private:
  329. int64_t _ts;
  330. Identity _id;
  331. std::vector<InetAddress> _physical;
  332. std::vector<Identity> _virtual;
  333. unsigned int _signatureLength;
  334. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  335. };
  336. } // namespace ZeroTier
  337. #endif