World.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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: 2025-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_WORLD_HPP
  14. #define ZT_WORLD_HPP
  15. #include <vector>
  16. #include <string>
  17. #include "Constants.hpp"
  18. #include "InetAddress.hpp"
  19. #include "Identity.hpp"
  20. #include "Buffer.hpp"
  21. #include "C25519.hpp"
  22. /**
  23. * Maximum number of roots (sanity limit, okay to increase)
  24. *
  25. * A given root can (through multi-homing) be distributed across any number of
  26. * physical endpoints, but having more than one is good to permit total failure
  27. * of one root or its withdrawal due to compromise without taking the whole net
  28. * down.
  29. */
  30. #define ZT_WORLD_MAX_ROOTS 4
  31. /**
  32. * Maximum number of stable endpoints per root (sanity limit, okay to increase)
  33. */
  34. #define ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT 32
  35. /**
  36. * The (more than) maximum length of a serialized World
  37. */
  38. #define ZT_WORLD_MAX_SERIALIZED_LENGTH (((1024 + (32 * ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT)) * ZT_WORLD_MAX_ROOTS) + ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_SIGNATURE_LEN + 128)
  39. /**
  40. * World ID for Earth
  41. *
  42. * This is the ID for the ZeroTier World used on planet Earth. It is unrelated
  43. * to the public network 8056c2e21c000001 of the same name. It was chosen
  44. * from Earth's approximate distance from the sun in kilometers.
  45. */
  46. #define ZT_WORLD_ID_EARTH 149604618
  47. /**
  48. * World ID for Mars -- for future use by SpaceX or others
  49. */
  50. #define ZT_WORLD_ID_MARS 227883110
  51. namespace ZeroTier {
  52. /**
  53. * A world definition (formerly known as a root topology)
  54. *
  55. * Think of a World as a single data center. Within this data center a set
  56. * of distributed fault tolerant root servers provide stable anchor points
  57. * for a peer to peer network that provides VLAN service. Updates to a world
  58. * definition can be published by signing them with the previous revision's
  59. * signing key, and should be very infrequent.
  60. *
  61. * The maximum data center size is approximately 2.5 cubic light seconds,
  62. * since many protocols have issues with >5s RTT latencies.
  63. *
  64. * ZeroTier operates a World for Earth capable of encompassing the planet, its
  65. * orbits, the Moon (about 1.3 light seconds), and nearby Lagrange points. A
  66. * world ID for Mars and nearby space is defined but not yet used, and a test
  67. * world ID is provided for testing purposes.
  68. */
  69. class World
  70. {
  71. public:
  72. /**
  73. * World type -- do not change IDs
  74. */
  75. enum Type
  76. {
  77. TYPE_NULL = 0,
  78. TYPE_PLANET = 1, // Planets, of which there is currently one (Earth)
  79. TYPE_MOON = 127 // Moons, which are user-created and many
  80. };
  81. /**
  82. * Upstream server definition in world/moon
  83. */
  84. struct Root
  85. {
  86. Identity identity;
  87. std::vector<InetAddress> stableEndpoints;
  88. inline bool operator==(const Root &r) const { return ((identity == r.identity)&&(stableEndpoints == r.stableEndpoints)); }
  89. inline bool operator!=(const Root &r) const { return (!(*this == r)); }
  90. inline bool operator<(const Root &r) const { return (identity < r.identity); } // for sorting
  91. };
  92. /**
  93. * Construct an empty / null World
  94. */
  95. World() :
  96. _id(0),
  97. _ts(0),
  98. _type(TYPE_NULL) {}
  99. /**
  100. * @return Root servers for this world and their stable endpoints
  101. */
  102. inline const std::vector<World::Root> &roots() const { return _roots; }
  103. /**
  104. * @return World type: planet or moon
  105. */
  106. inline Type type() const { return _type; }
  107. /**
  108. * @return World unique identifier
  109. */
  110. inline uint64_t id() const { return _id; }
  111. /**
  112. * @return World definition timestamp
  113. */
  114. inline uint64_t timestamp() const { return _ts; }
  115. /**
  116. * @return C25519 signature
  117. */
  118. inline const C25519::Signature &signature() const { return _signature; }
  119. /**
  120. * @return Public key that must sign next update
  121. */
  122. inline const C25519::Public &updatesMustBeSignedBy() const { return _updatesMustBeSignedBy; }
  123. /**
  124. * Check whether a world update should replace this one
  125. *
  126. * @param update Candidate update
  127. * @return True if update is newer than current, matches its ID and type, and is properly signed (or if current is NULL)
  128. */
  129. inline bool shouldBeReplacedBy(const World &update)
  130. {
  131. if ((_id == 0)||(_type == TYPE_NULL))
  132. return true;
  133. if ((_id == update._id)&&(_ts < update._ts)&&(_type == update._type)) {
  134. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  135. update.serialize(tmp,true);
  136. return C25519::verify(_updatesMustBeSignedBy,tmp.data(),tmp.size(),update._signature);
  137. }
  138. return false;
  139. }
  140. /**
  141. * @return True if this World is non-empty
  142. */
  143. inline operator bool() const { return (_type != TYPE_NULL); }
  144. template<unsigned int C>
  145. inline void serialize(Buffer<C> &b,bool forSign = false) const
  146. {
  147. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  148. b.append((uint8_t)_type);
  149. b.append((uint64_t)_id);
  150. b.append((uint64_t)_ts);
  151. b.append(_updatesMustBeSignedBy.data,ZT_C25519_PUBLIC_KEY_LEN);
  152. if (!forSign)
  153. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  154. b.append((uint8_t)_roots.size());
  155. for(std::vector<Root>::const_iterator r(_roots.begin());r!=_roots.end();++r) {
  156. r->identity.serialize(b);
  157. b.append((uint8_t)r->stableEndpoints.size());
  158. for(std::vector<InetAddress>::const_iterator ep(r->stableEndpoints.begin());ep!=r->stableEndpoints.end();++ep)
  159. ep->serialize(b);
  160. }
  161. if (_type == TYPE_MOON)
  162. b.append((uint16_t)0); // no attached dictionary (for future use)
  163. if (forSign) b.append((uint64_t)0xf7f7f7f7f7f7f7f7ULL);
  164. }
  165. template<unsigned int C>
  166. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  167. {
  168. unsigned int p = startAt;
  169. _roots.clear();
  170. switch((Type)b[p++]) {
  171. case TYPE_NULL: _type = TYPE_NULL; break; // shouldn't ever really happen in serialized data but it's not invalid
  172. case TYPE_PLANET: _type = TYPE_PLANET; break;
  173. case TYPE_MOON: _type = TYPE_MOON; break;
  174. default:
  175. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  176. }
  177. _id = b.template at<uint64_t>(p); p += 8;
  178. _ts = b.template at<uint64_t>(p); p += 8;
  179. memcpy(_updatesMustBeSignedBy.data,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN); p += ZT_C25519_PUBLIC_KEY_LEN;
  180. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN); p += ZT_C25519_SIGNATURE_LEN;
  181. const unsigned int numRoots = (unsigned int)b[p++];
  182. if (numRoots > ZT_WORLD_MAX_ROOTS)
  183. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  184. for(unsigned int k=0;k<numRoots;++k) {
  185. _roots.push_back(Root());
  186. Root &r = _roots.back();
  187. p += r.identity.deserialize(b,p);
  188. unsigned int numStableEndpoints = b[p++];
  189. if (numStableEndpoints > ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT)
  190. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  191. for(unsigned int kk=0;kk<numStableEndpoints;++kk) {
  192. r.stableEndpoints.push_back(InetAddress());
  193. p += r.stableEndpoints.back().deserialize(b,p);
  194. }
  195. }
  196. if (_type == TYPE_MOON)
  197. p += b.template at<uint16_t>(p) + 2;
  198. return (p - startAt);
  199. }
  200. inline bool operator==(const World &w) const { return ((_id == w._id)&&(_ts == w._ts)&&(memcmp(_updatesMustBeSignedBy.data,w._updatesMustBeSignedBy.data,ZT_C25519_PUBLIC_KEY_LEN) == 0)&&(memcmp(_signature.data,w._signature.data,ZT_C25519_SIGNATURE_LEN) == 0)&&(_roots == w._roots)&&(_type == w._type)); }
  201. inline bool operator!=(const World &w) const { return (!(*this == w)); }
  202. /**
  203. * Create a World object signed with a key pair
  204. *
  205. * @param t World type
  206. * @param id World ID
  207. * @param ts World timestamp / revision
  208. * @param sk Key that must be used to sign the next future update to this world
  209. * @param roots Roots and their stable endpoints
  210. * @param signWith Key to sign this World with (can have the same public as the next-update signing key, but doesn't have to)
  211. * @return Signed World object
  212. */
  213. static inline World make(World::Type t,uint64_t id,uint64_t ts,const C25519::Public &sk,const std::vector<World::Root> &roots,const C25519::Pair &signWith)
  214. {
  215. World w;
  216. w._id = id;
  217. w._ts = ts;
  218. w._type = t;
  219. w._updatesMustBeSignedBy = sk;
  220. w._roots = roots;
  221. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  222. w.serialize(tmp,true);
  223. w._signature = C25519::sign(signWith,tmp.data(),tmp.size());
  224. return w;
  225. }
  226. protected:
  227. uint64_t _id;
  228. uint64_t _ts;
  229. Type _type;
  230. C25519::Public _updatesMustBeSignedBy;
  231. C25519::Signature _signature;
  232. std::vector<Root> _roots;
  233. };
  234. } // namespace ZeroTier
  235. #endif