CryptoAuth_unit_test.c 8.1 KB

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