selftest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  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. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <stdexcept>
  32. #include <iostream>
  33. #include <string>
  34. #include <vector>
  35. #include "node/Constants.hpp"
  36. #include "node/RuntimeEnvironment.hpp"
  37. #include "node/InetAddress.hpp"
  38. #include "node/EllipticCurveKey.hpp"
  39. #include "node/EllipticCurveKeyPair.hpp"
  40. #include "node/Utils.hpp"
  41. #include "node/Identity.hpp"
  42. #include "node/Packet.hpp"
  43. #include "node/Salsa20.hpp"
  44. #include "node/HMAC.hpp"
  45. #include "node/MAC.hpp"
  46. #include "node/Peer.hpp"
  47. #include "node/Condition.hpp"
  48. #include "node/NodeConfig.hpp"
  49. #include "node/Dictionary.hpp"
  50. #include "node/RateLimiter.hpp"
  51. #include "node/EthernetTap.hpp"
  52. #include <openssl/rand.h>
  53. #ifdef __WINDOWS__
  54. #include <tchar.h>
  55. #endif
  56. using namespace ZeroTier;
  57. // ---------------------------------------------------------------------------
  58. // Override libcrypto default RAND_ with Utils::getSecureRandom(), which uses
  59. // a system strong random source. This is because OpenSSL libcrypto's default
  60. // RAND_ implementation uses uninitialized memory as one of its entropy
  61. // sources, which plays havoc with all kinds of debuggers and auditing tools.
  62. static void _zeroTier_rand_cleanup() {}
  63. static void _zeroTier_rand_add(const void *buf, int num, double add_entropy) {}
  64. static int _zeroTier_rand_status() { return 1; }
  65. static void _zeroTier_rand_seed(const void *buf, int num) {}
  66. static int _zeroTier_rand_bytes(unsigned char *buf, int num)
  67. {
  68. Utils::getSecureRandom(buf,num);
  69. return 1;
  70. }
  71. static RAND_METHOD _zeroTierRandMethod = {
  72. _zeroTier_rand_seed,
  73. _zeroTier_rand_bytes,
  74. _zeroTier_rand_cleanup,
  75. _zeroTier_rand_add,
  76. _zeroTier_rand_bytes,
  77. _zeroTier_rand_status
  78. };
  79. static void _initLibCrypto()
  80. {
  81. RAND_set_rand_method(&_zeroTierRandMethod);
  82. }
  83. // ---------------------------------------------------------------------------
  84. static unsigned char fuzzbuf[1048576];
  85. static const char *hmacShaTV0Key = "key";
  86. static const char *hmacShaTV0Msg = "The quick brown fox jumps over the lazy dog";
  87. static const unsigned char hmacShaTV0Mac[32] = { 0xf7,0xbc,0x83,0xf4,0x30,0x53,0x84,0x24,0xb1,0x32,0x98,0xe6,0xaa,0x6f,0xb1,0x43,0xef,0x4d,0x59,0xa1,0x49,0x46,0x17,0x59,0x97,0x47,0x9d,0xbc,0x2d,0x1a,0x3c,0xd8 };
  88. static const unsigned char s20TV0Key[32] = { 0x0f,0x62,0xb5,0x08,0x5b,0xae,0x01,0x54,0xa7,0xfa,0x4d,0xa0,0xf3,0x46,0x99,0xec,0x3f,0x92,0xe5,0x38,0x8b,0xde,0x31,0x84,0xd7,0x2a,0x7d,0xd0,0x23,0x76,0xc9,0x1c };
  89. static const unsigned char s20TV0Iv[8] = { 0x28,0x8f,0xf6,0x5d,0xc4,0x2b,0x92,0xf9 };
  90. static const unsigned char s20TV0Ks[64] = { 0x5e,0x5e,0x71,0xf9,0x01,0x99,0x34,0x03,0x04,0xab,0xb2,0x2a,0x37,0xb6,0x62,0x5b,0xf8,0x83,0xfb,0x89,0xce,0x3b,0x21,0xf5,0x4a,0x10,0xb8,0x10,0x66,0xef,0x87,0xda,0x30,0xb7,0x76,0x99,0xaa,0x73,0x79,0xda,0x59,0x5c,0x77,0xdd,0x59,0x54,0x2d,0xa2,0x08,0xe5,0x95,0x4f,0x89,0xe4,0x0e,0xb7,0xaa,0x80,0xa8,0x4a,0x61,0x76,0x66,0x3f };
  91. static int testCrypto()
  92. {
  93. unsigned char buf1[16384];
  94. unsigned char buf2[sizeof(buf1)],buf3[sizeof(buf1)];
  95. //Utils::getSecureRandom(buf1,1024);
  96. //std::cout << "[crypto] getSecureRandom() -> " << Utils::hex(buf1,1024) << std::endl;
  97. std::cout << "[crypto] Testing ECDSA... "; std::cout.flush();
  98. for(unsigned int k=0;k<64;++k) {
  99. EllipticCurveKeyPair kp;
  100. kp.generate();
  101. for(int i=0;i<32;++i)
  102. buf1[i] = (unsigned char)rand();
  103. std::string sig = kp.sign(buf1);
  104. if (!EllipticCurveKeyPair::verify(buf1,kp.pub(),sig.data(),sig.length())) {
  105. std::cout << "FAIL" << std::endl;
  106. return -1;
  107. }
  108. }
  109. std::cout << "PASS" << std::endl;
  110. std::cout << "[crypto] Testing HMAC-SHA256... "; std::cout.flush();
  111. memset(buf1,0,sizeof(buf1));
  112. HMAC::sha256(hmacShaTV0Key,strlen(hmacShaTV0Key),hmacShaTV0Msg,strlen(hmacShaTV0Msg),buf1);
  113. if (memcmp(buf1,hmacShaTV0Mac,32)) {
  114. std::cout << "FAIL (test vector 0) (" << Utils::hex(buf1,32) << ")" << std::endl;
  115. return -1;
  116. }
  117. std::cout << "PASS" << std::endl;
  118. std::cout << "[crypto] Testing Salsa20... "; std::cout.flush();
  119. for(unsigned int i=0;i<4;++i) {
  120. for(unsigned int k=0;k<sizeof(buf1);++k)
  121. buf1[k] = (unsigned char)rand();
  122. memset(buf2,0,sizeof(buf2));
  123. memset(buf3,0,sizeof(buf3));
  124. Salsa20 s20;
  125. s20.init("12345678123456781234567812345678",256,"12345678");
  126. s20.encrypt(buf1,buf2,sizeof(buf1));
  127. s20.init("12345678123456781234567812345678",256,"12345678");
  128. s20.decrypt(buf2,buf3,sizeof(buf2));
  129. if (memcmp(buf1,buf3,sizeof(buf1))) {
  130. std::cout << "FAIL (encrypt/decrypt test)" << std::endl;
  131. return -1;
  132. }
  133. }
  134. Salsa20 s20(s20TV0Key,256,s20TV0Iv);
  135. memset(buf1,0,sizeof(buf1));
  136. memset(buf2,0,sizeof(buf2));
  137. s20.encrypt(buf1,buf2,64);
  138. if (memcmp(buf2,s20TV0Ks,64)) {
  139. std::cout << "FAIL (test vector 0)" << std::endl;
  140. return -1;
  141. }
  142. std::cout << "PASS" << std::endl;
  143. return 0;
  144. }
  145. static int testIdentity()
  146. {
  147. Identity id;
  148. Buffer<512> buf;
  149. std::cout << "[identity] Generate identity... "; std::cout.flush();
  150. uint64_t genstart = Utils::now();
  151. id.generate();
  152. uint64_t genend = Utils::now();
  153. std::cout << "(took " << (genend - genstart) << "ms): " << id.toString(true) << std::endl;
  154. std::cout << "[identity] Locally validate identity: ";
  155. if (id.locallyValidate(false)) {
  156. std::cout << "PASS" << std::endl;
  157. } else {
  158. std::cout << "FAIL" << std::endl;
  159. return -1;
  160. }
  161. {
  162. Identity id2;
  163. buf.clear();
  164. id.serialize(buf,true);
  165. id2.deserialize(buf);
  166. std::cout << "[identity] Serialize and deserialize (w/private): ";
  167. if ((id == id2)&&(id2.locallyValidate(false))) {
  168. std::cout << "PASS" << std::endl;
  169. } else {
  170. std::cout << "FAIL" << std::endl;
  171. return -1;
  172. }
  173. }
  174. {
  175. Identity id2;
  176. buf.clear();
  177. id.serialize(buf,false);
  178. id2.deserialize(buf);
  179. std::cout << "[identity] Serialize and deserialize (no private): ";
  180. if ((id == id2)&&(id2.locallyValidate(false))) {
  181. std::cout << "PASS" << std::endl;
  182. } else {
  183. std::cout << "FAIL" << std::endl;
  184. return -1;
  185. }
  186. }
  187. {
  188. Identity id2;
  189. id2.fromString(id.toString(true).c_str());
  190. std::cout << "[identity] Serialize and deserialize (ASCII w/private): ";
  191. if ((id == id2)&&(id2.locallyValidate(false))) {
  192. std::cout << "PASS" << std::endl;
  193. } else {
  194. std::cout << "FAIL" << std::endl;
  195. return -1;
  196. }
  197. }
  198. {
  199. Identity id2;
  200. id2.fromString(id.toString(false).c_str());
  201. std::cout << "[identity] Serialize and deserialize (ASCII no private): ";
  202. if ((id == id2)&&(id2.locallyValidate(false))) {
  203. std::cout << "PASS" << std::endl;
  204. } else {
  205. std::cout << "FAIL" << std::endl;
  206. return -1;
  207. }
  208. }
  209. return 0;
  210. }
  211. static int testPacket()
  212. {
  213. unsigned char salsaKey[32],hmacKey[32];
  214. Packet a,b;
  215. a.zeroAll();
  216. b.zeroAll();
  217. for(unsigned int i=0;i<32;++i) {
  218. salsaKey[i] = (unsigned char)rand();
  219. hmacKey[i] = (unsigned char)rand();
  220. }
  221. std::cout << "[packet] Testing Packet encoder/decoder... ";
  222. a.reset(Address(),Address(),Packet::VERB_HELLO);
  223. for(int i=0;i<32;++i)
  224. a.append("supercalifragilisticexpealidocious",strlen("supercalifragilisticexpealidocious"));
  225. b = a;
  226. if (a != b) {
  227. std::cout << "FAIL (assign)" << std::endl;
  228. return -1;
  229. }
  230. a.compress();
  231. unsigned int complen = a.size();
  232. a.uncompress();
  233. std::cout << "(compressed: " << complen << ", decompressed: " << a.size() << ") ";
  234. if (a != b) {
  235. std::cout << "FAIL (compresssion)" << std::endl;
  236. return -1;
  237. }
  238. a.compress();
  239. a.encrypt(salsaKey);
  240. a.decrypt(salsaKey);
  241. a.uncompress();
  242. if (a != b) {
  243. std::cout << "FAIL (encrypt-decrypt)" << std::endl;
  244. return -1;
  245. }
  246. a.hmacSet(hmacKey);
  247. if (!a.hmacVerify(hmacKey)) {
  248. std::cout << "FAIL (hmacVerify)" << std::endl;
  249. return -1;
  250. }
  251. std::cout << "PASS" << std::endl;
  252. return 0;
  253. }
  254. static int testOther()
  255. {
  256. std::cout << "[other] Testing Base64 encode/decode... "; std::cout.flush();
  257. for(unsigned int k=0;k<1000;++k) {
  258. unsigned int flen = (rand() % 8194) + 1;
  259. for(unsigned int i=0;i<flen;++i)
  260. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  261. std::string dec = Utils::base64Decode(Utils::base64Encode(fuzzbuf,flen));
  262. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  263. std::cout << "FAILED!" << std::endl;
  264. return -1;
  265. }
  266. }
  267. std::cout << "PASS" << std::endl;
  268. std::cout << "[other] Testing hex encode/decode... "; std::cout.flush();
  269. for(unsigned int k=0;k<1000;++k) {
  270. unsigned int flen = (rand() % 8194) + 1;
  271. for(unsigned int i=0;i<flen;++i)
  272. fuzzbuf[i] = (unsigned char)(rand() & 0xff);
  273. std::string dec = Utils::unhex(Utils::hex(fuzzbuf,flen).c_str());
  274. if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
  275. std::cout << "FAILED!" << std::endl;
  276. std::cout << Utils::hex(fuzzbuf,flen) << std::endl;
  277. std::cout << Utils::hex(dec.data(),dec.length()) << std::endl;
  278. return -1;
  279. }
  280. }
  281. std::cout << "PASS" << std::endl;
  282. std::cout << "[other] Testing command bus encode/decode... "; std::cout.flush();
  283. try {
  284. static char key[32] = { 0 };
  285. for(unsigned int k=0;k<1000;++k) {
  286. std::vector<std::string> original;
  287. for(unsigned int i=0,j=rand() % 256,l=(rand() % 1024)+1;i<j;++i)
  288. original.push_back(std::string(l,'x'));
  289. std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(key,1,original));
  290. //std::cout << packets.size() << ' '; std::cout.flush();
  291. std::vector<std::string> after;
  292. for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator i(packets.begin());i!=packets.end();++i) {
  293. unsigned long convId = 9999;
  294. if (!NodeConfig::decodeControlMessagePacket(key,i->data(),i->size(),convId,after)) {
  295. std::cout << "FAIL (decode)" << std::endl;
  296. return -1;
  297. }
  298. if (convId != 1) {
  299. std::cout << "FAIL (conversation ID)" << std::endl;
  300. return -1;
  301. }
  302. }
  303. if (after != original) {
  304. std::cout << "FAIL (compare)" << std::endl;
  305. return -1;
  306. }
  307. }
  308. } catch (std::exception &exc) {
  309. std::cout << "FAIL (" << exc.what() << ")" << std::endl;
  310. return -1;
  311. }
  312. std::cout << "PASS" << std::endl;
  313. std::cout << "[other] Testing Dictionary... "; std::cout.flush();
  314. for(int k=0;k<10000;++k) {
  315. Dictionary a,b;
  316. int nk = rand() % 32;
  317. for(int q=0;q<nk;++q) {
  318. std::string k,v;
  319. int kl = (rand() % 512);
  320. int vl = (rand() % 512);
  321. for(int i=0;i<kl;++i)
  322. k.push_back((char)rand());
  323. for(int i=0;i<vl;++i)
  324. v.push_back((char)rand());
  325. a[k] = v;
  326. }
  327. std::string aser = a.toString();
  328. b.fromString(aser);
  329. if (a != b) {
  330. std::cout << "FAIL!" << std::endl;
  331. return -1;
  332. }
  333. }
  334. std::cout << "PASS" << std::endl;
  335. return 0;
  336. }
  337. static int testRateLimiter()
  338. {
  339. RateLimiter limiter;
  340. RateLimiter::Limit limit;
  341. std::cout << "[ratelimiter] preload: 10000.0, rate: 1000.0/sec, max: 15000.0, min: -7500.0" << std::endl;
  342. limit.bytesPerSecond = 1000.0;
  343. limit.maxBalance = 15000.0;
  344. limit.minBalance = -7500.0;
  345. limiter.init(10000.0);
  346. for(int i=0;i<25;++i) {
  347. Thread::sleep(100);
  348. std::cout << "[ratelimiter] delayed 0.1s, gate(1000.0): " << (limiter.gate(limit,1000.0) ? "OK" : "BLOCK");
  349. std::cout << " (new balance afterwords: " << limiter.balance() << ")" << std::endl;
  350. }
  351. std::cout << "[ratelimiter] delaying 15s..." << std::endl;
  352. Thread::sleep(15000);
  353. for(int i=0;i<20;++i) {
  354. Thread::sleep(1000);
  355. std::cout << "[ratelimiter] delayed 1s, gate(2000.0): " << (limiter.gate(limit,2000.0) ? "OK" : "BLOCK");
  356. std::cout << " (new balance afterwords: " << limiter.balance() << ")" << std::endl;
  357. }
  358. return 0;
  359. }
  360. #ifdef __WINDOWS__
  361. int _tmain(int argc, _TCHAR* argv[])
  362. #else
  363. int main(int argc,char **argv)
  364. #endif
  365. {
  366. ///* For testing windows tap
  367. try {
  368. RuntimeEnvironment renv;
  369. renv.homePath = "C:";
  370. EthernetTap tap(&renv,"test12345",MAC(0x32),2800,NULL,NULL);
  371. Thread::sleep(100000000);
  372. } catch (std::exception &exc) {
  373. std::cout << exc.what() << std::endl;
  374. return 0;
  375. }
  376. //*/
  377. /*
  378. int r = 0;
  379. _initLibCrypto();
  380. srand((unsigned int)time(0));
  381. r |= testCrypto();
  382. r |= testPacket();
  383. r |= testOther();
  384. r |= testIdentity();
  385. r |= testRateLimiter();
  386. if (r)
  387. std::cout << std::endl << "SOMETHING FAILED!" << std::endl;
  388. return r;
  389. */
  390. }