mkworld.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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, current.c25519"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. // EDIT BELOW HERE ---------------------------------------------------------
  96. std::vector<World::Root> roots;
  97. // old US-SFO
  98. roots.push_back(World::Root());
  99. roots.back().identity = Identity("7e19876aba:0:2a6e2b2318930f60eb097f70d0f4b028b2cd6d3d0c63c014b9039ff35390e41181f216fb2e6fa8d95c1ee9667156411905c3dccfea78d8c6dfafba688170b3fa");
  100. roots.back().stableEndpoints.push_back(InetAddress("198.199.97.220/9993"));
  101. std::sort(roots.back().stableEndpoints.begin(),roots.back().stableEndpoints.end());
  102. // old EU-PARIS
  103. roots.push_back(World::Root());
  104. roots.back().identity = Identity("8841408a2e:0:bb1d31f2c323e264e9e64172c1a74f77899555ed10751cd56e86405cde118d02dffe555d462ccf6a85b5631c12350c8d5dc409ba10b9025d0f445cf449d92b1c");
  105. roots.back().stableEndpoints.push_back(InetAddress("107.191.46.210/9993"));
  106. std::sort(roots.back().stableEndpoints.begin(),roots.back().stableEndpoints.end());
  107. // old US-NYC
  108. roots.push_back(World::Root());
  109. roots.back().identity = Identity("8acf059fe3:0:482f6ee5dfe902319b419de5bdc765209c0ecda38c4d6e4fcf0d33658398b4527dcd22f93112fb9befd02fd78bf7261b333fc105d192a623ca9e50fc60b374a5");
  110. roots.back().stableEndpoints.push_back(InetAddress("162.243.77.111/9993"));
  111. std::sort(roots.back().stableEndpoints.begin(),roots.back().stableEndpoints.end());
  112. // old AP-SNG
  113. roots.push_back(World::Root());
  114. roots.back().identity = Identity("9d219039f3:0:01f0922a98e3b34ebcbff333269dc265d7a020aab69d72be4d4acc9c8c9294785771256cd1d942a90d1bd1d2dca3ea84ef7d85afe6611fb43ff0b74126d90a6e");
  115. roots.back().stableEndpoints.push_back(InetAddress("128.199.197.217/9993"));
  116. std::sort(roots.back().stableEndpoints.begin(),roots.back().stableEndpoints.end());
  117. std::sort(roots.begin(),roots.end());
  118. const uint64_t id = ZT_WORLD_ID_EARTH;
  119. const uint64_t ts = OSUtils::now();
  120. // END WORLD SETUP ---------------------------------------------------------
  121. fprintf(stderr,"INFO: generating and signing id==%llu ts==%llu"ZT_EOL_S,(unsigned long long)id,(unsigned long long)ts);
  122. World nw = WorldMaker::make(id,ts,currentKP.pub,roots,previousKP);
  123. Buffer<ZT_WORLD_MAX_SERIALIZED_LENGTH> outtmp;
  124. nw.serialize(outtmp,false);
  125. World testw;
  126. testw.deserialize(outtmp,0);
  127. if (testw != nw) {
  128. fprintf(stderr,"FATAL: serialization test failed!"ZT_EOL_S);
  129. return 1;
  130. }
  131. fwrite(outtmp.data(),outtmp.size(),1,stdout);
  132. fflush(stdout);
  133. fprintf(stderr,"INFO: wrote %u bytes to stdout"ZT_EOL_S,outtmp.size());
  134. fprintf(stderr,ZT_EOL_S);
  135. fprintf(stderr,"#define ZT_DEFAULT_WORLD_LENGTH %u"ZT_EOL_S,outtmp.size());
  136. fprintf(stderr,"static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {");
  137. for(unsigned int i=0;i<outtmp.size();++i) {
  138. const unsigned char *d = (const unsigned char *)outtmp.data();
  139. if (i > 0)
  140. fprintf(stderr,",");
  141. fprintf(stderr,"0x%.2x",(unsigned int)d[i]);
  142. }
  143. fprintf(stderr,"};"ZT_EOL_S);
  144. return 0;
  145. }