selftest.cpp 12 KB

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