World.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. }
  134. if ((_id == update._id)&&(_ts < update._ts)&&(_type == update._type)) {
  135. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  136. update.serialize(tmp,true);
  137. return C25519::verify(_updatesMustBeSignedBy,tmp.data(),tmp.size(),update._signature);
  138. }
  139. return false;
  140. }
  141. /**
  142. * @return True if this World is non-empty
  143. */
  144. inline operator bool() const { return (_type != TYPE_NULL); }
  145. template<unsigned int C>
  146. inline void serialize(Buffer<C> &b,bool forSign = false) const
  147. {
  148. if (forSign) {
  149. b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  150. }
  151. b.append((uint8_t)_type);
  152. b.append((uint64_t)_id);
  153. b.append((uint64_t)_ts);
  154. b.append(_updatesMustBeSignedBy.data,ZT_C25519_PUBLIC_KEY_LEN);
  155. if (!forSign) {
  156. b.append(_signature.data,ZT_C25519_SIGNATURE_LEN);
  157. }
  158. b.append((uint8_t)_roots.size());
  159. for(std::vector<Root>::const_iterator r(_roots.begin());r!=_roots.end();++r) {
  160. r->identity.serialize(b);
  161. b.append((uint8_t)r->stableEndpoints.size());
  162. for(std::vector<InetAddress>::const_iterator ep(r->stableEndpoints.begin());ep!=r->stableEndpoints.end();++ep) {
  163. ep->serialize(b);
  164. }
  165. }
  166. if (_type == TYPE_MOON) {
  167. b.append((uint16_t)0); // no attached dictionary (for future use)
  168. }
  169. if (forSign) {
  170. b.append((uint64_t)0xf7f7f7f7f7f7f7f7ULL);
  171. }
  172. }
  173. template<unsigned int C>
  174. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  175. {
  176. unsigned int p = startAt;
  177. _roots.clear();
  178. switch((Type)b[p++]) {
  179. case TYPE_NULL: // shouldn't ever really happen in serialized data but it's not invalid
  180. _type = TYPE_NULL;
  181. break;
  182. case TYPE_PLANET:
  183. _type = TYPE_PLANET;
  184. break;
  185. case TYPE_MOON:
  186. _type = TYPE_MOON;
  187. break;
  188. default:
  189. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_INVALID_TYPE;
  190. }
  191. _id = b.template at<uint64_t>(p);
  192. p += 8;
  193. _ts = b.template at<uint64_t>(p);
  194. p += 8;
  195. memcpy(_updatesMustBeSignedBy.data,b.field(p,ZT_C25519_PUBLIC_KEY_LEN),ZT_C25519_PUBLIC_KEY_LEN);
  196. p += ZT_C25519_PUBLIC_KEY_LEN;
  197. memcpy(_signature.data,b.field(p,ZT_C25519_SIGNATURE_LEN),ZT_C25519_SIGNATURE_LEN);
  198. p += ZT_C25519_SIGNATURE_LEN;
  199. const unsigned int numRoots = (unsigned int)b[p++];
  200. if (numRoots > ZT_WORLD_MAX_ROOTS) {
  201. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  202. }
  203. for(unsigned int k=0;k<numRoots;++k) {
  204. _roots.push_back(Root());
  205. Root &r = _roots.back();
  206. p += r.identity.deserialize(b,p);
  207. unsigned int numStableEndpoints = b[p++];
  208. if (numStableEndpoints > ZT_WORLD_MAX_STABLE_ENDPOINTS_PER_ROOT) {
  209. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  210. }
  211. for(unsigned int kk=0;kk<numStableEndpoints;++kk) {
  212. r.stableEndpoints.push_back(InetAddress());
  213. p += r.stableEndpoints.back().deserialize(b,p);
  214. }
  215. }
  216. if (_type == TYPE_MOON) {
  217. p += b.template at<uint16_t>(p) + 2;
  218. }
  219. return (p - startAt);
  220. }
  221. 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)); }
  222. inline bool operator!=(const World &w) const { return (!(*this == w)); }
  223. /**
  224. * Create a World object signed with a key pair
  225. *
  226. * @param t World type
  227. * @param id World ID
  228. * @param ts World timestamp / revision
  229. * @param sk Key that must be used to sign the next future update to this world
  230. * @param roots Roots and their stable endpoints
  231. * @param signWith Key to sign this World with (can have the same public as the next-update signing key, but doesn't have to)
  232. * @return Signed World object
  233. */
  234. 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)
  235. {
  236. World w;
  237. w._id = id;
  238. w._ts = ts;
  239. w._type = t;
  240. w._updatesMustBeSignedBy = sk;
  241. w._roots = roots;
  242. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  243. w.serialize(tmp,true);
  244. w._signature = C25519::sign(signWith,tmp.data(),tmp.size());
  245. return w;
  246. }
  247. protected:
  248. uint64_t _id;
  249. uint64_t _ts;
  250. Type _type;
  251. C25519::Public _updatesMustBeSignedBy;
  252. C25519::Signature _signature;
  253. std::vector<Root> _roots;
  254. };
  255. } // namespace ZeroTier
  256. #endif