CryptoAuth_unit_test.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 "benc/String.h"
  16. #include "crypto/random/Random.h"
  17. #include "crypto/random/test/DeterminentRandomSeed.h"
  18. #include "memory/Allocator.h"
  19. #include "util/events/EventBase.h"
  20. #include "util/Assert.h"
  21. #include "util/Bits.h"
  22. #include "util/Hex.h"
  23. #include "util/log/FileWriterLog.h"
  24. #include "wire/Message.h"
  25. #include "wire/CryptoHeader.h"
  26. #include "crypto/test/TestCa.h"
  27. #include <stdio.h>
  28. #define PRIVATEKEY "0123456789abcdefghijklmnopqrstuv"
  29. #define HERPUBKEY "wxyzabcdefghijklmnopqrstuv987654"
  30. #define HELLOWORLD "Hello World"
  31. #define HELLOWORLDLOWER "hello world"
  32. #define HELLOWORLDLEN 12
  33. static struct Random* evilRandom(struct Allocator* alloc, struct Log* logger)
  34. {
  35. RandomSeed_t* evilSeed = DeterminentRandomSeed_new(alloc, NULL);
  36. struct Random* out = NULL;
  37. Err_assert(Random_newWithSeed(&out, alloc, logger, evilSeed));
  38. return out;
  39. }
  40. struct Context
  41. {
  42. struct Allocator* alloc;
  43. TestCa_t* ca;
  44. TestCa_Session_t* sess;
  45. struct Iface plaintext;
  46. struct Iface ciphertext;
  47. struct Log* log;
  48. EventBase_t* base;
  49. };
  50. static Iface_DEFUN doNothingSuccessfully(Message_t* msg, struct Iface* iface)
  51. {
  52. return NULL;
  53. }
  54. static struct Context* setUp(uint8_t* myPrivateKey,
  55. uint8_t* herPublicKey,
  56. uint8_t* authPassword,
  57. struct Allocator* alloc,
  58. enum TestCa_Config cfg)
  59. {
  60. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  61. struct Log* log = ctx->log = FileWriterLog_new(stdout, alloc);
  62. EventBase_t* base = ctx->base = EventBase_new(alloc);
  63. ctx->ciphertext.send = doNothingSuccessfully;
  64. ctx->plaintext.send = doNothingSuccessfully;
  65. TestCa_t* ca = ctx->ca =
  66. TestCa_new(alloc, myPrivateKey, base, log,
  67. evilRandom(alloc, log), evilRandom(alloc, log), cfg);
  68. TestCa_Session_t* sess = ctx->sess =
  69. TestCa_newSession(ca, alloc, herPublicKey, false, Gcc_FILE, true);
  70. Iface_plumb(&ctx->plaintext, &sess->plaintext);
  71. Iface_plumb(&ctx->ciphertext, &sess->ciphertext);
  72. if (authPassword) {
  73. TestCa_setAuth(String_CONST(authPassword), NULL, sess);
  74. }
  75. return ctx;
  76. }
  77. static void testHello(uint8_t* password, uint8_t* expectedOutput, enum TestCa_Config cfg)
  78. {
  79. Assert_true(CString_strlen((char*)expectedOutput) == 264);
  80. struct Allocator* alloc = Allocator_new(1<<20);
  81. struct Context* ctx = setUp(NULL, HERPUBKEY, password, alloc, cfg);
  82. Message_t* msg = Message_new(0, CryptoHeader_SIZE + 32, alloc);
  83. Err_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  84. Iface_send(&ctx->plaintext, msg);
  85. char* actual = Hex_print(Message_bytes(msg), Message_getLength(msg), alloc);
  86. if (CString_strcmp(actual, expectedOutput)) {
  87. Assert_failure("Test failed.\n"
  88. "Expected %s\n"
  89. " Got %s\n", expectedOutput, actual);
  90. }
  91. Allocator_free(alloc);
  92. }
  93. static void helloNoAuth(enum TestCa_Config cfg)
  94. {
  95. testHello(NULL,
  96. "00000000007691d3802a9d047c400000497a185dabda71739c1f35465fac3448"
  97. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  98. "74bee22a90642a6b4188f374afd90ccc97bb61873b5d8a3b4a6071b60b26a8c7"
  99. "2d6484634df315c4d3ad63de42fe3e4ebfd83bcdab2e1f5f40dc5a08eda4e6c6"
  100. "b7067d3b", cfg);
  101. }
  102. static void helloWithAuth(enum TestCa_Config cfg)
  103. {
  104. testHello("password",
  105. "0000000001641c99f7719f5700000000497a185dabda71739c1f35465fac3448"
  106. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  107. "74bee22a90642a6b022e089e0550ca84b86884af6a0263fa5fff9ba07583aea4"
  108. "acb000dbe4115623cf335c63981b9645b6c89fbdc3ad757744879751de0f215d"
  109. "2479131d", cfg);
  110. }
  111. static void receiveHelloWithNoAuth(enum TestCa_Config cfg)
  112. {
  113. uint8_t herPublic[32];
  114. Assert_true(Hex_decode(herPublic, 32,
  115. "847c0d2c375234f365e660955187a3735a0f7613d1609d3a6a4d8c53aeaa5a22", 64) > 0);
  116. struct Allocator* alloc = Allocator_new(1<<20);
  117. struct Context* ctx = setUp(PRIVATEKEY, herPublic, NULL, alloc, cfg);
  118. Message_t* msg = Message_new(132, 32, alloc);
  119. Assert_true(Hex_decode(Message_bytes(msg), Message_getLength(msg),
  120. "0000000000ffffffffffffff7fffffffffffffffffffffffffffffffffffffff"
  121. "ffffffffffffffff847c0d2c375234f365e660955187a3735a0f7613d1609d3a"
  122. "6a4d8c53aeaa5a22ea9cf275eee0185edf7f211192f12e8e642a325ed76925fe"
  123. "3c76d313b767a10aca584ca0b979dee990a737da7d68366fa3846d43d541de91"
  124. "29ea3e12", 132*2) > 0);
  125. Err_assert(Message_epush(msg, NULL, 16)); // peer ipv6
  126. Iface_send(&ctx->ciphertext, msg);
  127. uint32_t err = 0;
  128. Err_assert(Message_epop32h(&err, msg));
  129. Assert_true(!err);
  130. Assert_true(Message_getLength(msg) == HELLOWORLDLEN);
  131. Assert_true(Bits_memcmp(HELLOWORLD, Message_bytes(msg), HELLOWORLDLEN) == 0);
  132. Allocator_free(alloc);
  133. //printf("bytes=%s length=%u\n", finalOut->bytes, finalOut->length);
  134. }
  135. static void repeatHello(enum TestCa_Config cfg)
  136. {
  137. uint8_t* expectedOutput =
  138. "0000000101641c99f7719f5700000000a693a9fd3f0e27e81ab1100b57b37259"
  139. "4c2adca8671f1fdd050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  140. "74bee22a90642a6ba8555be84c5e35970c5270e8f31f2a5978e0fbdee4542882"
  141. "97568f25a3fc2801aa707d954c78eccb970bcc8cb26867e9dbf0c9d6ef1b3f27"
  142. "24e7e550";
  143. struct Allocator* alloc = Allocator_new(1<<20);
  144. struct Context* ctx = setUp(NULL, HERPUBKEY, "password", alloc, cfg);
  145. Message_t* msg = Message_new(0, CryptoHeader_SIZE + HELLOWORLDLEN + 32, alloc);
  146. Err_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  147. Iface_send(&ctx->plaintext, msg);
  148. Message_reset(msg);
  149. Err_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  150. Iface_send(&ctx->plaintext, msg);
  151. char* actual = Hex_print(Message_bytes(msg), Message_getLength(msg), alloc);
  152. if (CString_strcmp(actual, expectedOutput)) {
  153. Assert_failure("Test failed.\n"
  154. "Expected %s\n"
  155. " Got %s\n", expectedOutput, actual);
  156. }
  157. Allocator_free(alloc);
  158. }
  159. static void testGetUsers(enum TestCa_Config cfg)
  160. {
  161. struct Allocator* allocator = Allocator_new(1<<20);
  162. EventBase_t* base = EventBase_new(allocator);
  163. TestCa_t* ca = TestCa_new(allocator, NULL, base, NULL,
  164. evilRandom(allocator, NULL), evilRandom(allocator, NULL), cfg);
  165. RTypes_StrList_t* users = NULL;
  166. users = TestCa_getUsers(ca, allocator);
  167. Assert_true(users->len == 0);
  168. TestCa_addUser_ipv6(String_CONST("pass1"), String_CONST("user1"), NULL, ca);
  169. users = TestCa_getUsers(ca, allocator);
  170. Assert_true(users->len == 1);
  171. Assert_true(String_equals(String_CONST("user1"), users->items[0]));
  172. TestCa_addUser_ipv6(String_CONST("pass2"), String_CONST("user2"), NULL, ca);
  173. users = TestCa_getUsers(ca, allocator);
  174. Assert_true(users->len == 2);
  175. Assert_true(String_equals(String_CONST("user2"), users->items[1]));
  176. Assert_true(String_equals(String_CONST("user1"), users->items[0]));
  177. Allocator_free(allocator);
  178. }
  179. static void iteration(enum TestCa_Config cfg)
  180. {
  181. testGetUsers(cfg);
  182. helloNoAuth(cfg);
  183. helloWithAuth(cfg);
  184. receiveHelloWithNoAuth(cfg);
  185. repeatHello(cfg);
  186. }
  187. int main()
  188. {
  189. iteration(TestCa_Config_OLD);
  190. iteration(TestCa_Config_OLD_NEW);
  191. // This will always fail because we are expecting particular results
  192. // which are specific to the old CryptoAuth
  193. // iteration(TestCa_Config_NOISE);
  194. return 0;
  195. }