mkworld.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. /*
  28. * This utility makes the World from the configuration specified below.
  29. * It probably won't be much use to anyone outside ZeroTier, Inc. except
  30. * for testing and experimentation purposes.
  31. *
  32. * If you want to make your own World you must edit this file.
  33. *
  34. * When run, it expects two files in the current directory:
  35. *
  36. * previous.c25519 - key pair to sign this world (key from previous world)
  37. * current.c25519 - key pair whose public key should be embedded in this world
  38. *
  39. * If these files do not exist, they are both created with the same key pair
  40. * and a self-signed initial World is born.
  41. */
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <stdint.h>
  46. #include <string>
  47. #include <vector>
  48. #include <algorithm>
  49. #include <node/Constants.hpp>
  50. #include <node/World.hpp>
  51. #include <node/C25519.hpp>
  52. #include <node/Identity.hpp>
  53. #include <node/InetAddress.hpp>
  54. #include <osdep/OSUtils.hpp>
  55. using namespace ZeroTier;
  56. class WorldMaker : public World
  57. {
  58. public:
  59. static inline World make(uint64_t id,uint64_t ts,const C25519::Public &sk,const std::vector<World::Root> &roots,const C25519::Pair &signWith)
  60. {
  61. WorldMaker w;
  62. w._id = id;
  63. w._ts = ts;
  64. w._updateSigningKey = sk;
  65. w._roots = roots;
  66. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> tmp;
  67. w.serialize(tmp,true);
  68. w._signature = C25519::sign(signWith,tmp.data(),tmp.size());
  69. return w;
  70. }
  71. };
  72. int main(int argc,char **argv)
  73. {
  74. std::string previous,current;
  75. if ((!OSUtils::readFile("previous.c25519",previous))||(!OSUtils::readFile("current.c25519",current))) {
  76. C25519::Pair np(C25519::generate());
  77. previous = std::string();
  78. previous.append((const char *)np.pub.data,ZT_C25519_PUBLIC_KEY_LEN);
  79. previous.append((const char *)np.priv.data,ZT_C25519_PRIVATE_KEY_LEN);
  80. current = previous;
  81. OSUtils::writeFile("previous.c25519",previous);
  82. OSUtils::writeFile("current.c25519",current);
  83. fprintf(stderr,"INFO: created initial world keys: previous.c25519 and current.c25519 (both initially the same)"ZT_EOL_S);
  84. }
  85. 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))) {
  86. fprintf(stderr,"FATAL: previous.c25519 or current.c25519 empty or invalid"ZT_EOL_S);
  87. return 1;
  88. }
  89. C25519::Pair previousKP;
  90. memcpy(previousKP.pub.data,previous.data(),ZT_C25519_PUBLIC_KEY_LEN);
  91. memcpy(previousKP.priv.data,previous.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN);
  92. C25519::Pair currentKP;
  93. memcpy(currentKP.pub.data,current.data(),ZT_C25519_PUBLIC_KEY_LEN);
  94. memcpy(currentKP.priv.data,current.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN);
  95. // =========================================================================
  96. // EDIT BELOW HERE
  97. std::vector<World::Root> roots;
  98. //
  99. // The initial version of the World uses the old root server infrastructure.
  100. // The new "Alice and Bob" infrastructure will replace this gradually, with
  101. // Paris probably being the first node to be taken over and clusterized.
  102. //
  103. // ZeroTier does actual World generation on an air-gapped machine by copying
  104. // this code over, building it there and running, then saving the results
  105. // onto a USB key.
  106. //
  107. const uint64_t id = ZT_WORLD_ID_EARTH;
  108. const uint64_t ts = 1447696577275ULL; // November 16th, 2015 ~9:56AM
  109. // old US-SFO
  110. roots.push_back(World::Root());
  111. roots.back().identity = Identity("7e19876aba:0:2a6e2b2318930f60eb097f70d0f4b028b2cd6d3d0c63c014b9039ff35390e41181f216fb2e6fa8d95c1ee9667156411905c3dccfea78d8c6dfafba688170b3fa");
  112. roots.back().stableEndpoints.push_back(InetAddress("198.199.97.220/9993"));
  113. // old EU-PARIS
  114. roots.push_back(World::Root());
  115. roots.back().identity = Identity("8841408a2e:0:bb1d31f2c323e264e9e64172c1a74f77899555ed10751cd56e86405cde118d02dffe555d462ccf6a85b5631c12350c8d5dc409ba10b9025d0f445cf449d92b1c");
  116. roots.back().stableEndpoints.push_back(InetAddress("107.191.46.210/9993"));
  117. // old US-NYC
  118. roots.push_back(World::Root());
  119. roots.back().identity = Identity("8acf059fe3:0:482f6ee5dfe902319b419de5bdc765209c0ecda38c4d6e4fcf0d33658398b4527dcd22f93112fb9befd02fd78bf7261b333fc105d192a623ca9e50fc60b374a5");
  120. roots.back().stableEndpoints.push_back(InetAddress("162.243.77.111/9993"));
  121. // old AP-SNG
  122. roots.push_back(World::Root());
  123. roots.back().identity = Identity("9d219039f3:0:01f0922a98e3b34ebcbff333269dc265d7a020aab69d72be4d4acc9c8c9294785771256cd1d942a90d1bd1d2dca3ea84ef7d85afe6611fb43ff0b74126d90a6e");
  124. roots.back().stableEndpoints.push_back(InetAddress("128.199.197.217/9993"));
  125. // END WORLD DEFINITION
  126. // =========================================================================
  127. fprintf(stderr,"INFO: generating and signing id==%llu ts==%llu"ZT_EOL_S,(unsigned long long)id,(unsigned long long)ts);
  128. World nw = WorldMaker::make(id,ts,currentKP.pub,roots,previousKP);
  129. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> outtmp;
  130. nw.serialize(outtmp,false);
  131. World testw;
  132. testw.deserialize(outtmp,0);
  133. if (testw != nw) {
  134. fprintf(stderr,"FATAL: serialization test failed!"ZT_EOL_S);
  135. return 1;
  136. }
  137. //fwrite(outtmp.data(),outtmp.size(),1,stdout);
  138. //fflush(stdout);
  139. //fprintf(stderr,"INFO: wrote %u bytes to stdout"ZT_EOL_S,outtmp.size());
  140. fprintf(stdout,ZT_EOL_S);
  141. fprintf(stdout,"#define ZT_DEFAULT_WORLD_LENGTH %u"ZT_EOL_S,outtmp.size());
  142. fprintf(stdout,"static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {");
  143. for(unsigned int i=0;i<outtmp.size();++i) {
  144. const unsigned char *d = (const unsigned char *)outtmp.data();
  145. if (i > 0)
  146. fprintf(stdout,",");
  147. fprintf(stdout,"0x%.2x",(unsigned int)d[i]);
  148. }
  149. fprintf(stdout,"};"ZT_EOL_S);
  150. return 0;
  151. }