CryptoAuth_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/Random.h"
  16. #include "rust/cjdns_sys/Rffi.h"
  17. #include "crypto/CryptoAuth.h"
  18. #include "benc/String.h"
  19. #include "memory/Allocator.h"
  20. #include "util/events/EventBase.h"
  21. #include "util/Assert.h"
  22. #include "util/Bits.h"
  23. #include "util/log/FileWriterLog.h"
  24. #include "wire/CryptoHeader.h"
  25. #include "crypto/test/TestCa.h"
  26. #include "crypto/random/test/DeterminentRandomSeed.h"
  27. #include "util/CString.h"
  28. // console.log(x.replace(/[0-9a-f]{2}/g, (x)=>'\\x'+x))
  29. #define PRIVATEKEY_A \
  30. "\x53\xff\x22\xb2\xeb\x94\xce\x8c\x5f\x18\x52\xc0\xf5\x57\xeb\x90\x1f\x06\x7e\x52\x73\xd5\x41\xe0\xa2\x1e\x14\x3c\x20\xdf\xf9\xda"
  31. // "53ff22b2eb94ce8c5f1852c0f557eb901f067e5273d541e0a21e143c20dff9da"
  32. #define PUBLICKEY_A \
  33. "\xe3\xff\x75\xaf\x6e\x44\x14\x49\x4d\xf2\x2f\x20\x0f\xfe\xaa\x56\xe7\x97\x6d\x99\x1d\x33\xcc\x87\xf5\x24\x27\xe2\x7f\x83\x23\x5d"
  34. // "e3ff75af6e4414494df22f200ffeaa56e7976d991d33cc87f52427e27f83235d"
  35. #define PRIVATEKEY_B \
  36. "\xb7\x1c\x4f\x43\xe3\xd4\xb1\x87\x9b\x50\x65\xd4\x4a\x1c\xb4\x3e\xaf\x07\xdd\xba\x96\xde\x6a\x72\xca\x76\x1c\x4e\xf4\xbd\x29\x88"
  37. // "b71c4f43e3d4b1879b5065d44a1cb43eaf07ddba96de6a72ca761c4ef4bd2988"
  38. #define PUBLICKEY_B \
  39. "\x27\xc3\x03\xcd\xc1\xf9\x6e\x4b\x28\xd5\x1c\x75\x13\x0a\xff\x6c\xad\x52\x09\x8f\x2d\x75\x26\x15\xb7\xb6\x50\x9e\xd6\xa8\x94\x77"
  40. // "27c303cdc1f96e4b28d51c75130aff6cad52098f2d752615b7b6509ed6a89477"
  41. #define USEROBJ "This represents a user"
  42. struct Node
  43. {
  44. TestCa_t* ca;
  45. TestCa_Session_t* sess;
  46. struct Iface plaintext;
  47. struct Iface ciphertext;
  48. const char* expectPlaintext;
  49. enum CryptoAuth_DecryptErr expectErr;
  50. struct Log* log;
  51. Identity
  52. };
  53. struct Context
  54. {
  55. struct Node node1;
  56. struct Node node2;
  57. struct Allocator* alloc;
  58. struct Log* log;
  59. EventBase_t* base;
  60. Identity
  61. };
  62. static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger, const char* seed)
  63. {
  64. uint8_t buf[64] = {0};
  65. Assert_true(CString_strlen(seed) < 60);
  66. CString_strcpy(buf, seed);
  67. RandomSeed_t* evilSeed = DeterminentRandomSeed_new(alloc, buf);
  68. struct Random* out = NULL;
  69. Err_assert(Random_newWithSeed(&out, alloc, logger, evilSeed));
  70. return out;
  71. }
  72. static Iface_DEFUN afterDecrypt(Message_t* msg, struct Iface* if1)
  73. {
  74. struct Node* n = Identity_containerOf(if1, struct Node, plaintext);
  75. Log_debug(n->log, "Got message from afterDecrypt");
  76. enum CryptoAuth_DecryptErr e = 0;
  77. Err(Message_epop32h(&e, msg));
  78. if (e != n->expectErr) {
  79. Assert_failure("expected decrypt error [%d], got [%d]\n", n->expectErr, e);
  80. }
  81. n->expectErr = CryptoAuth_DecryptErr_NONE;
  82. if (!n->expectPlaintext) {
  83. if (e) {
  84. return NULL;
  85. }
  86. Assert_failure("expected <NULL>, got [%s](%d)\n", Message_bytes(msg), Message_getLength(msg));
  87. }
  88. if ((int)CString_strlen(n->expectPlaintext) != Message_getLength(msg) ||
  89. CString_strncmp(Message_bytes(msg), n->expectPlaintext, Message_getLength(msg)))
  90. {
  91. Assert_failure("expected [%s](%d), got [%s](%d)\n",
  92. n->expectPlaintext, (int)CString_strlen(n->expectPlaintext), Message_bytes(msg), Message_getLength(msg));
  93. }
  94. n->expectPlaintext = NULL;
  95. return NULL;
  96. }
  97. static Iface_DEFUN afterEncrypt(Message_t* msg, struct Iface* if1)
  98. {
  99. return NULL;
  100. }
  101. static struct Context* init(uint8_t* privateKeyA,
  102. uint8_t* publicKeyA,
  103. uint8_t* password,
  104. uint8_t* privateKeyB,
  105. uint8_t* publicKeyB,
  106. enum TestCa_Config cfg)
  107. {
  108. struct Allocator* alloc = Allocator_new(1048576);
  109. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  110. Identity_set(ctx);
  111. Identity_set(&ctx->node1);
  112. Identity_set(&ctx->node2);
  113. ctx->alloc = alloc;
  114. ctx->node1.plaintext.send = afterDecrypt;
  115. ctx->node2.plaintext.send = afterDecrypt;
  116. ctx->node1.ciphertext.send = afterEncrypt;
  117. ctx->node2.ciphertext.send = afterEncrypt;
  118. struct Log* logger = ctx->log = FileWriterLog_new(stdout, alloc);
  119. struct Random* randA = evilRandom(alloc, logger, "ALPHA");
  120. struct Random* randB = evilRandom(alloc, logger, "ALPHA");
  121. struct Random* randC = evilRandom(alloc, logger, "BRAVO");
  122. struct Random* randD = evilRandom(alloc, logger, "BRAVO");
  123. EventBase_t* base = ctx->base = EventBase_new(alloc);
  124. ctx->node1.log = logger;
  125. ctx->node2.log = logger;
  126. ctx->node1.ca = TestCa_new(alloc, privateKeyA, base, logger, randA, randB, cfg);
  127. ctx->node1.sess = TestCa_newSession(ctx->node1.ca, alloc, publicKeyB, false, "cif1", true);
  128. ctx->node2.ca = TestCa_new(alloc, privateKeyB, base, logger, randC, randD, cfg);
  129. if (password) {
  130. String* passStr = String_CONST(password);
  131. TestCa_setAuth(passStr, NULL, ctx->node1.sess);
  132. TestCa_addUser_ipv6(passStr, String_new(USEROBJ, alloc), NULL, ctx->node2.ca);
  133. }
  134. ctx->node2.sess = TestCa_newSession(ctx->node2.ca, alloc, publicKeyA, false, "cif2", true);
  135. Iface_plumb(&ctx->node1.sess->plaintext, &ctx->node1.plaintext);
  136. Iface_plumb(&ctx->node1.sess->ciphertext, &ctx->node1.ciphertext);
  137. Iface_plumb(&ctx->node2.sess->plaintext, &ctx->node2.plaintext);
  138. Iface_plumb(&ctx->node2.sess->ciphertext, &ctx->node2.ciphertext);
  139. return ctx;
  140. }
  141. static struct Context* simpleInit(enum TestCa_Config cfg)
  142. {
  143. return init(PRIVATEKEY_A, PUBLICKEY_A, NULL, PRIVATEKEY_B, PUBLICKEY_B, cfg);
  144. }
  145. static Message_t* encryptMsg(struct Context* ctx,
  146. struct Node* n,
  147. const char* x)
  148. {
  149. struct Allocator* alloc = Allocator_child(ctx->alloc);
  150. int len = (((CString_strlen(x)+1) / 8) + 1) * 8;
  151. Message_t* msg = Message_new(len, CryptoHeader_SIZE + 32, alloc);
  152. CString_strcpy(Message_bytes(msg), x);
  153. Err_assert(Message_truncate(msg, CString_strlen(x)));
  154. //msg->bytes[Message_getLength(msg)] = 0;
  155. struct RTypes_Error_t* e = Iface_send(&n->plaintext, msg);
  156. if (e) {
  157. printf("%s\n", Rffi_printError(e, ctx->alloc));
  158. Assert_failure("error was not null");
  159. }
  160. Assert_true(Message_getLength(msg) > ((int)CString_strlen(x) + 4));
  161. return msg;
  162. }
  163. static Message_t* decryptMsg(struct Context* ctx,
  164. Message_t* msg,
  165. struct Node* n,
  166. const char* expectResult,
  167. enum CryptoAuth_DecryptErr expectErr)
  168. {
  169. Assert_true(!n->expectPlaintext && !n->expectErr);
  170. n->expectPlaintext = expectResult;
  171. n->expectErr = expectErr;
  172. Err_assert(Message_epush(msg, NULL, 16)); // peer ipv6
  173. Iface_send(&n->ciphertext, msg);
  174. Assert_true(!n->expectPlaintext && !n->expectErr);
  175. return msg;
  176. }
  177. static void sendToIf1(struct Context* ctx, const char* x)
  178. {
  179. Message_t* msg = encryptMsg(ctx, &ctx->node2, x);
  180. decryptMsg(ctx, msg, &ctx->node1, x, CryptoAuth_DecryptErr_NONE);
  181. Allocator_free(Message_getAlloc(msg));
  182. }
  183. static void sendToIf2(struct Context* ctx, const char* x)
  184. {
  185. Message_t* msg = encryptMsg(ctx, &ctx->node1, x);
  186. decryptMsg(ctx, msg, &ctx->node2, x, CryptoAuth_DecryptErr_NONE);
  187. Allocator_free(Message_getAlloc(msg));
  188. }
  189. static void normal(enum TestCa_Config cfg)
  190. {
  191. struct Context* ctx = simpleInit(cfg);
  192. sendToIf2(ctx, "hello world");
  193. sendToIf1(ctx, "hello cjdns");
  194. sendToIf2(ctx, "hai");
  195. sendToIf1(ctx, "goodbye");
  196. Allocator_free(ctx->alloc);
  197. }
  198. static void repeatKey(enum TestCa_Config cfg)
  199. {
  200. struct Context* ctx = simpleInit(cfg);
  201. sendToIf2(ctx, "hello world");
  202. sendToIf2(ctx, "r u thar?");
  203. sendToIf1(ctx, "hello cjdns");
  204. sendToIf2(ctx, "hai");
  205. sendToIf1(ctx, "goodbye");
  206. Allocator_free(ctx->alloc);
  207. }
  208. static void repeatHello(enum TestCa_Config cfg)
  209. {
  210. struct Context* ctx = simpleInit(cfg);
  211. sendToIf2(ctx, "hello world");
  212. sendToIf2(ctx, "r u thar?");
  213. sendToIf1(ctx, "hello cjdns");
  214. sendToIf2(ctx, "hai");
  215. sendToIf1(ctx, "goodbye");
  216. Allocator_free(ctx->alloc);
  217. }
  218. static void chatter(enum TestCa_Config cfg)
  219. {
  220. struct Context* ctx = simpleInit(cfg);
  221. sendToIf2(ctx, "hello world");
  222. sendToIf1(ctx, "hello cjdns");
  223. sendToIf2(ctx, "hai");
  224. sendToIf1(ctx, "goodbye");
  225. sendToIf1(ctx, "hello cjdns");
  226. sendToIf2(ctx, "hai");
  227. sendToIf1(ctx, "goodbye");
  228. sendToIf1(ctx, "hello cjdns");
  229. sendToIf2(ctx, "hai");
  230. sendToIf1(ctx, "goodbye");
  231. sendToIf1(ctx, "hello cjdns");
  232. sendToIf2(ctx, "hai");
  233. sendToIf1(ctx, "goodbye");
  234. Allocator_free(ctx->alloc);
  235. }
  236. static void auth(enum TestCa_Config cfg)
  237. {
  238. struct Context* ctx = init(
  239. PRIVATEKEY_A, PUBLICKEY_A, "password", PRIVATEKEY_B, PUBLICKEY_B, cfg);
  240. sendToIf2(ctx, "hello world");
  241. sendToIf1(ctx, "hello cjdns");
  242. sendToIf2(ctx, "hai");
  243. sendToIf1(ctx, "goodbye");
  244. Allocator_free(ctx->alloc);
  245. }
  246. static void replayKeyPacket(int scenario, enum TestCa_Config cfg)
  247. {
  248. struct Context* ctx = simpleInit(cfg);
  249. sendToIf2(ctx, "hello world");
  250. Message_t* msg = encryptMsg(ctx, &ctx->node2, "hello replay key");
  251. Message_t* toReplay = Message_clone(msg, ctx->alloc);
  252. decryptMsg(ctx, msg, &ctx->node1, "hello replay key", CryptoAuth_DecryptErr_NONE);
  253. if (scenario == 1) {
  254. // the packet is failed because we know it's a dupe from the temp key.
  255. decryptMsg(ctx, toReplay, &ctx->node1, NULL, CryptoAuth_DecryptErr_INVALID_PACKET);
  256. }
  257. sendToIf2(ctx, "first traffic packet");
  258. if (scenario == 2) {
  259. decryptMsg(ctx, toReplay, &ctx->node1, NULL, CryptoAuth_DecryptErr_INVALID_PACKET);
  260. }
  261. sendToIf1(ctx, "second traffic packet");
  262. if (scenario == 3) {
  263. // If we replay at this stage, the packet is dropped as a stray key
  264. decryptMsg(ctx, toReplay, &ctx->node1, NULL, CryptoAuth_DecryptErr_KEY_PKT_ESTABLISHED_SESSION);
  265. }
  266. Allocator_free(ctx->alloc);
  267. }
  268. /**
  269. * Alice and Bob both decided they wanted to talk to eachother at precisely the same time.
  270. * This means two Hello packets crossed on the wire. Both arrived at their destination but
  271. * if each triggers a re-initialization of the CA session, nobody will be synchronized!
  272. */
  273. static void hellosCrossedOnTheWire(enum TestCa_Config cfg)
  274. {
  275. struct Context* ctx = simpleInit(cfg);
  276. uint8_t pk1[32];
  277. TestCa_getPubKey(ctx->node1.ca, pk1);
  278. uint8_t hpk2[32];
  279. TestCa_getHerPubKey(ctx->node2.sess, hpk2);
  280. Assert_true(!Bits_memcmp(pk1, hpk2, 32));
  281. Message_t* hello2 = encryptMsg(ctx, &ctx->node2, "hello2");
  282. Message_t* hello1 = encryptMsg(ctx, &ctx->node1, "hello1");
  283. decryptMsg(ctx, hello2, &ctx->node1, "hello2", 0);
  284. decryptMsg(ctx, hello1, &ctx->node2, "hello1", 0);
  285. sendToIf2(ctx, "hello world");
  286. sendToIf1(ctx, "hello cjdns");
  287. sendToIf2(ctx, "hai");
  288. sendToIf1(ctx, "goodbye");
  289. Allocator_free(ctx->alloc);
  290. }
  291. static void reset(enum TestCa_Config cfg)
  292. {
  293. struct Context* ctx = simpleInit(cfg);
  294. sendToIf2(ctx, "hello world");
  295. sendToIf1(ctx, "hello cjdns");
  296. sendToIf2(ctx, "hai");
  297. sendToIf1(ctx, "brb");
  298. Assert_true(TestCa_getState(ctx->node1.sess) == CryptoAuth_State_ESTABLISHED);
  299. Assert_true(TestCa_getState(ctx->node2.sess) == CryptoAuth_State_ESTABLISHED);
  300. TestCa_reset(ctx->node1.sess);
  301. // sess2 still talking to sess1 but sess1 is reset and cannot read the packets.
  302. decryptMsg(ctx, encryptMsg(ctx, &ctx->node2, "will be lost"), &ctx->node1, NULL,
  303. CryptoAuth_DecryptErr_NO_SESSION);
  304. decryptMsg(ctx, encryptMsg(ctx, &ctx->node2, "lost"), &ctx->node1, NULL,
  305. CryptoAuth_DecryptErr_NO_SESSION);
  306. // This is because we want to prevent replay attacks from tearing down a session.
  307. decryptMsg(ctx, encryptMsg(ctx, &ctx->node1, "hello"), &ctx->node2, "hello", 0);
  308. sendToIf1(ctx, "hello again");
  309. sendToIf2(ctx, "hai");
  310. sendToIf1(ctx, "ok works");
  311. sendToIf2(ctx, "yup");
  312. Assert_true(TestCa_getState(ctx->node1.sess) == CryptoAuth_State_ESTABLISHED);
  313. Assert_true(TestCa_getState(ctx->node2.sess) == CryptoAuth_State_ESTABLISHED);
  314. Allocator_free(ctx->alloc);
  315. }
  316. // This is slightly different from replayKeyPacket because the second key packet is valid,
  317. // it's just delayed.
  318. static void twoKeyPackets(int scenario, enum TestCa_Config cfg)
  319. {
  320. struct Context* ctx = simpleInit(cfg);
  321. sendToIf2(ctx, "hello world");
  322. sendToIf1(ctx, "key packet 1");
  323. Message_t* key2 = encryptMsg(ctx, &ctx->node2, "key packet 2");
  324. if (scenario == 1) {
  325. sendToIf1(ctx, "key packet 3");
  326. decryptMsg(ctx, key2, &ctx->node1, "key packet 2", 0);
  327. } else if (scenario == 2) {
  328. sendToIf2(ctx, "initial data packet");
  329. decryptMsg(ctx, key2, &ctx->node1, "key packet 2", 0);
  330. sendToIf1(ctx, "second data packet");
  331. sendToIf2(ctx, "third data packet");
  332. } else if (scenario == 3) {
  333. sendToIf2(ctx, "initial data packet");
  334. sendToIf1(ctx, "second data packet");
  335. decryptMsg(ctx, key2, &ctx->node1, NULL, CryptoAuth_DecryptErr_KEY_PKT_ESTABLISHED_SESSION);
  336. }
  337. Allocator_free(ctx->alloc);
  338. }
  339. static void iteration(enum TestCa_Config cfg)
  340. {
  341. normal(cfg);
  342. repeatKey(cfg);
  343. repeatHello(cfg);
  344. chatter(cfg);
  345. auth(cfg);
  346. replayKeyPacket(1, cfg);
  347. replayKeyPacket(2, cfg);
  348. replayKeyPacket(3, cfg);
  349. hellosCrossedOnTheWire(cfg);
  350. reset(cfg);
  351. twoKeyPackets(1, cfg);
  352. twoKeyPackets(2, cfg);
  353. twoKeyPackets(3, cfg);
  354. }
  355. int main()
  356. {
  357. iteration(TestCa_Config_OLD);
  358. iteration(TestCa_Config_OLD_NEW);
  359. //iteration(TestCa_Config_NOISE); // TODO(cjd): re-enable this
  360. return 0;
  361. }