mkworld.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. * This utility makes the World from the configuration specified below.
  20. * It probably won't be much use to anyone outside ZeroTier, Inc. except
  21. * for testing and experimentation purposes.
  22. *
  23. * If you want to make your own World you must edit this file.
  24. *
  25. * When run, it expects two files in the current directory:
  26. *
  27. * previous.c25519 - key pair to sign this world (key from previous world)
  28. * current.c25519 - key pair whose public key should be embedded in this world
  29. *
  30. * If these files do not exist, they are both created with the same key pair
  31. * and a self-signed initial World is born.
  32. */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <stdint.h>
  37. #include <string>
  38. #include <vector>
  39. #include <algorithm>
  40. #include <node/Constants.hpp>
  41. #include <node/World.hpp>
  42. #include <node/C25519.hpp>
  43. #include <node/Identity.hpp>
  44. #include <node/InetAddress.hpp>
  45. #include <osdep/OSUtils.hpp>
  46. using namespace ZeroTier;
  47. class WorldMaker : public World
  48. {
  49. public:
  50. static inline World make(uint64_t id,uint64_t ts,const C25519::Public &sk,const std::vector<World::Root> &roots,const C25519::Pair &signWith)
  51. {
  52. WorldMaker w;
  53. w._id = id;
  54. w._ts = ts;
  55. w._updateSigningKey = sk;
  56. w._roots = roots;
  57. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  58. w.serialize(tmp,true);
  59. w._signature = C25519::sign(signWith,tmp.data(),tmp.size());
  60. return w;
  61. }
  62. };
  63. int main(int argc,char **argv)
  64. {
  65. std::string previous,current;
  66. if ((!OSUtils::readFile("previous.c25519",previous))||(!OSUtils::readFile("current.c25519",current))) {
  67. C25519::Pair np(C25519::generate());
  68. previous = std::string();
  69. previous.append((const char *)np.pub.data,ZT_C25519_PUBLIC_KEY_LEN);
  70. previous.append((const char *)np.priv.data,ZT_C25519_PRIVATE_KEY_LEN);
  71. current = previous;
  72. OSUtils::writeFile("previous.c25519",previous);
  73. OSUtils::writeFile("current.c25519",current);
  74. fprintf(stderr,"INFO: created initial world keys: previous.c25519 and current.c25519 (both initially the same)"ZT_EOL_S);
  75. }
  76. if ((previous.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))||(current.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))) {
  77. fprintf(stderr,"FATAL: previous.c25519 or current.c25519 empty or invalid"ZT_EOL_S);
  78. return 1;
  79. }
  80. C25519::Pair previousKP;
  81. memcpy(previousKP.pub.data,previous.data(),ZT_C25519_PUBLIC_KEY_LEN);
  82. memcpy(previousKP.priv.data,previous.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN);
  83. C25519::Pair currentKP;
  84. memcpy(currentKP.pub.data,current.data(),ZT_C25519_PUBLIC_KEY_LEN);
  85. memcpy(currentKP.priv.data,current.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN);
  86. // =========================================================================
  87. // EDIT BELOW HERE
  88. std::vector<World::Root> roots;
  89. const uint64_t id = ZT_WORLD_ID_EARTH;
  90. const uint64_t ts = 1452708876314ULL; // January 13th, 2016
  91. // Alice
  92. roots.push_back(World::Root());
  93. roots.back().identity = Identity("9d219039f3:0:01f0922a98e3b34ebcbff333269dc265d7a020aab69d72be4d4acc9c8c9294785771256cd1d942a90d1bd1d2dca3ea84ef7d85afe6611fb43ff0b74126d90a6e");
  94. roots.back().stableEndpoints.push_back(InetAddress("188.166.94.177/9993")); // Amsterdam
  95. roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:2:d0::7d:1/9993")); // Amsterdam
  96. roots.back().stableEndpoints.push_back(InetAddress("154.66.197.33/9993")); // Johannesburg
  97. roots.back().stableEndpoints.push_back(InetAddress("2c0f:f850:154:197::33/9993")); // Johannesburg
  98. roots.back().stableEndpoints.push_back(InetAddress("159.203.97.171/9993")); // New York
  99. roots.back().stableEndpoints.push_back(InetAddress("2604:a880:800:a1::54:6001/9993")); // New York
  100. roots.back().stableEndpoints.push_back(InetAddress("169.57.143.104/9993")); // Sao Paolo
  101. roots.back().stableEndpoints.push_back(InetAddress("2607:f0d0:1d01:57::2/9993")); // Sao Paolo
  102. roots.back().stableEndpoints.push_back(InetAddress("107.170.197.14/9993")); // San Francisco
  103. roots.back().stableEndpoints.push_back(InetAddress("2604:a880:1:20::200:e001/9993")); // San Francisco
  104. roots.back().stableEndpoints.push_back(InetAddress("128.199.197.217/9993")); // Singapore
  105. roots.back().stableEndpoints.push_back(InetAddress("2400:6180:0:d0::b7:4001/9993")); // Singapore
  106. // Bob
  107. roots.push_back(World::Root());
  108. roots.back().identity = Identity("8841408a2e:0:bb1d31f2c323e264e9e64172c1a74f77899555ed10751cd56e86405cde118d02dffe555d462ccf6a85b5631c12350c8d5dc409ba10b9025d0f445cf449d92b1c");
  109. roots.back().stableEndpoints.push_back(InetAddress("45.32.198.130/9993")); // Dallas
  110. roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6400:81c3:5400:00ff:fe18:1d61/9993")); // Dallas
  111. roots.back().stableEndpoints.push_back(InetAddress("46.101.160.249/9993")); // Frankfurt
  112. roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:3:d0::6a:3001/9993")); // Frankfurt
  113. roots.back().stableEndpoints.push_back(InetAddress("107.191.46.210/9993")); // Paris
  114. roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6800:83a4::64/9993")); // Paris
  115. roots.back().stableEndpoints.push_back(InetAddress("45.32.246.179/9993")); // Sydney
  116. roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:5800:8bf8:5400:ff:fe15:b39a/9993")); // Sydney
  117. roots.back().stableEndpoints.push_back(InetAddress("45.32.248.87/9993")); // Tokyo
  118. roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:7000:9bc9:5400:00ff:fe15:c4f5/9993")); // Tokyo
  119. roots.back().stableEndpoints.push_back(InetAddress("159.203.2.154/9993")); // Toronto
  120. roots.back().stableEndpoints.push_back(InetAddress("2604:a880:cad:d0::26:7001/9993")); // Toronto
  121. // END WORLD DEFINITION
  122. // =========================================================================
  123. fprintf(stderr,"INFO: generating and signing id==%llu ts==%llu"ZT_EOL_S,(unsigned long long)id,(unsigned long long)ts);
  124. World nw = WorldMaker::make(id,ts,currentKP.pub,roots,previousKP);
  125. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> outtmp;
  126. nw.serialize(outtmp,false);
  127. World testw;
  128. testw.deserialize(outtmp,0);
  129. if (testw != nw) {
  130. fprintf(stderr,"FATAL: serialization test failed!"ZT_EOL_S);
  131. return 1;
  132. }
  133. OSUtils::writeFile("world.bin",std::string((const char *)outtmp.data(),outtmp.size()));
  134. fprintf(stderr,"INFO: world.bin written with %u bytes of binary world data."ZT_EOL_S,outtmp.size());
  135. fprintf(stdout,ZT_EOL_S);
  136. fprintf(stdout,"#define ZT_DEFAULT_WORLD_LENGTH %u"ZT_EOL_S,outtmp.size());
  137. fprintf(stdout,"static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {");
  138. for(unsigned int i=0;i<outtmp.size();++i) {
  139. const unsigned char *d = (const unsigned char *)outtmp.data();
  140. if (i > 0)
  141. fprintf(stdout,",");
  142. fprintf(stdout,"0x%.2x",(unsigned int)d[i]);
  143. }
  144. fprintf(stdout,"};"ZT_EOL_S);
  145. return 0;
  146. }