World.hpp 8.5 KB

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