World.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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: 2026-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 "Buffer.hpp"
  16. #include "C25519.hpp"
  17. #include "Constants.hpp"
  18. #include "Identity.hpp"
  19. #include "InetAddress.hpp"
  20. #include <string>
  21. #include <vector>
  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. public:
  71. /**
  72. * World type -- do not change IDs
  73. */
  74. enum Type {
  75. TYPE_NULL = 0,
  76. TYPE_PLANET = 1, // Planets, of which there is currently one (Earth)
  77. TYPE_MOON = 127 // Moons, which are user-created and many
  78. };
  79. /**
  80. * Upstream server definition in world/moon
  81. */
  82. struct Root {
  83. Identity identity;
  84. std::vector<InetAddress> stableEndpoints;
  85. inline bool operator==(const Root& r) const
  86. {
  87. return ((identity == r.identity) && (stableEndpoints == r.stableEndpoints));
  88. }
  89. inline bool operator!=(const Root& r) const
  90. {
  91. return (! (*this == r));
  92. }
  93. inline bool operator<(const Root& r) const
  94. {
  95. return (identity < r.identity);
  96. } // for sorting
  97. };
  98. /**
  99. * Construct an empty / null World
  100. */
  101. World() : _id(0), _ts(0), _type(TYPE_NULL)
  102. {
  103. }
  104. /**
  105. * @return Root servers for this world and their stable endpoints
  106. */
  107. inline const std::vector<World::Root>& roots() const
  108. {
  109. return _roots;
  110. }
  111. /**
  112. * @return World type: planet or moon
  113. */
  114. inline Type type() const
  115. {
  116. return _type;
  117. }
  118. /**
  119. * @return World unique identifier
  120. */
  121. inline uint64_t id() const
  122. {
  123. return _id;
  124. }
  125. /**
  126. * @return World definition timestamp
  127. */
  128. inline uint64_t timestamp() const
  129. {
  130. return _ts;
  131. }
  132. /**
  133. * @return C25519 signature
  134. */
  135. inline const C25519::Signature& signature() const
  136. {
  137. return _signature;
  138. }
  139. /**
  140. * @return Public key that must sign next update
  141. */
  142. inline const C25519::Public& updatesMustBeSignedBy() const
  143. {
  144. return _updatesMustBeSignedBy;
  145. }
  146. /**
  147. * Check whether a world update should replace this one
  148. *
  149. * @param update Candidate update
  150. * @return True if update is newer than current, matches its ID and type, and is properly signed (or if current is NULL)
  151. */
  152. inline bool shouldBeReplacedBy(const World& update)
  153. {
  154. if ((_id == 0) || (_type == TYPE_NULL)) {
  155. return true;
  156. }
  157. if ((_id == update._id) && (_ts < update._ts) && (_type == update._type)) {
  158. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  159. update.serialize(tmp, true);
  160. return C25519::verify(_updatesMustBeSignedBy, tmp.data(), tmp.size(), update._signature);
  161. }
  162. return false;
  163. }
  164. /**
  165. * @return True if this World is non-empty
  166. */
  167. inline operator bool() const
  168. {
  169. return (_type != TYPE_NULL);
  170. }
  171. template <unsigned int C> inline void serialize(Buffer<C>& b, bool forSign = false) const
  172. {
  173. if (forSign) {
  174. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  175. }
  176. b.append((uint8_t)_type);
  177. b.append((uint64_t)_id);
  178. b.append((uint64_t)_ts);
  179. b.append(_updatesMustBeSignedBy.data, ZT_C25519_PUBLIC_KEY_LEN);
  180. if (! forSign) {
  181. b.append(_signature.data, ZT_C25519_SIGNATURE_LEN);
  182. }
  183. b.append((uint8_t)_roots.size());
  184. for (std::vector<Root>::const_iterator r(_roots.begin()); r != _roots.end(); ++r) {
  185. r->identity.serialize(b);
  186. b.append((uint8_t)r->stableEndpoints.size());
  187. for (std::vector<InetAddress>::const_iterator ep(r->stableEndpoints.begin()); ep != r->stableEndpoints.end(); ++ep) {
  188. ep->serialize(b);
  189. }
  190. }
  191. if (_type == TYPE_MOON) {
  192. b.append((uint16_t)0); // no attached dictionary (for future use)
  193. }
  194. if (forSign) {
  195. b.append((uint64_t)0xf7f7f7f7f7f7f7f7ULL);
  196. }
  197. }
  198. template <unsigned int C> inline unsigned int deserialize(const Buffer<C>& b, unsigned int startAt = 0)
  199. {
  200. unsigned int p = startAt;
  201. _roots.clear();
  202. switch ((Type)b[p++]) {
  203. case TYPE_NULL: // shouldn't ever really happen in serialized data but it's not invalid
  204. _type = TYPE_NULL;
  205. break;
  206. case TYPE_PLANET:
  207. _type = TYPE_PLANET;
  208. break;
  209. case TYPE_MOON:
  210. _type = TYPE_MOON;
  211. break;
  212. default:
  213. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  214. }
  215. _id = b.template at<uint64_t>(p);
  216. p += 8;
  217. _ts = b.template at<uint64_t>(p);
  218. p += 8;
  219. memcpy(_updatesMustBeSignedBy.data, b.field(p, ZT_C25519_PUBLIC_KEY_LEN), ZT_C25519_PUBLIC_KEY_LEN);
  220. p += ZT_C25519_PUBLIC_KEY_LEN;
  221. memcpy(_signature.data, b.field(p, ZT_C25519_SIGNATURE_LEN), ZT_C25519_SIGNATURE_LEN);
  222. p += ZT_C25519_SIGNATURE_LEN;
  223. const unsigned int numRoots = (unsigned int)b[p++];
  224. if (numRoots > ZT_WORLD_MAX_ROOTS) {
  225. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  226. }
  227. for (unsigned int k = 0; k < numRoots; ++k) {
  228. _roots.push_back(Root());
  229. Root& r = _roots.back();
  230. p += r.identity.deserialize(b, p);
  231. unsigned int numStableEndpoints = b[p++];
  232. if (numStableEndpoints > ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT) {
  233. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  234. }
  235. for (unsigned int kk = 0; kk < numStableEndpoints; ++kk) {
  236. r.stableEndpoints.push_back(InetAddress());
  237. p += r.stableEndpoints.back().deserialize(b, p);
  238. }
  239. }
  240. if (_type == TYPE_MOON) {
  241. p += b.template at<uint16_t>(p) + 2;
  242. }
  243. return (p - startAt);
  244. }
  245. inline bool operator==(const World& w) const
  246. {
  247. return (
  248. (_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)
  249. && (_roots == w._roots) && (_type == w._type));
  250. }
  251. inline bool operator!=(const World& w) const
  252. {
  253. return (! (*this == w));
  254. }
  255. /**
  256. * Create a World object signed with a key pair
  257. *
  258. * @param t World type
  259. * @param id World ID
  260. * @param ts World timestamp / revision
  261. * @param sk Key that must be used to sign the next future update to this world
  262. * @param roots Roots and their stable endpoints
  263. * @param signWith Key to sign this World with (can have the same public as the next-update signing key, but doesn't have to)
  264. * @return Signed World object
  265. */
  266. 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)
  267. {
  268. World w;
  269. w._id = id;
  270. w._ts = ts;
  271. w._type = t;
  272. w._updatesMustBeSignedBy = sk;
  273. w._roots = roots;
  274. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  275. w.serialize(tmp, true);
  276. w._signature = C25519::sign(signWith, tmp.data(), tmp.size());
  277. return w;
  278. }
  279. protected:
  280. uint64_t _id;
  281. uint64_t _ts;
  282. Type _type;
  283. C25519::Public _updatesMustBeSignedBy;
  284. C25519::Signature _signature;
  285. std::vector<Root> _roots;
  286. };
  287. } // namespace ZeroTier
  288. #endif