CryptoAuth_unit_test.c 8.1 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/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. return Random_newWithSeed(alloc, logger, evilSeed, NULL);
  40. }
  41. struct Context
  42. {
  43. struct Allocator* alloc;
  44. TestCa_t* ca;
  45. TestCa_Session_t* sess;
  46. struct Iface plaintext;
  47. struct Iface ciphertext;
  48. struct Log* log;
  49. struct EventBase* base;
  50. };
  51. static Iface_DEFUN doNothingSuccessfully(struct Message* msg, struct Iface* iface)
  52. {
  53. return NULL;
  54. }
  55. static struct Context* setUp(uint8_t* myPrivateKey,
  56. uint8_t* herPublicKey,
  57. uint8_t* authPassword,
  58. struct Allocator* alloc,
  59. enum TestCa_Config cfg)
  60. {
  61. struct Context* ctx = Allocator_calloc(alloc, sizeof(struct Context), 1);
  62. struct Log* log = ctx->log = FileWriterLog_new(stdout, alloc);
  63. struct EventBase* base = ctx->base = EventBase_new(alloc);
  64. ctx->ciphertext.send = doNothingSuccessfully;
  65. ctx->plaintext.send = doNothingSuccessfully;
  66. TestCa_t* ca = ctx->ca =
  67. TestCa_new(alloc, myPrivateKey, base, log,
  68. evilRandom(alloc, log), evilRandom(alloc, log), cfg);
  69. TestCa_Session_t* sess = ctx->sess =
  70. TestCa_newSession(ca, alloc, herPublicKey, false, Gcc_FILE, true);
  71. Iface_plumb(&ctx->plaintext, &sess->plaintext);
  72. Iface_plumb(&ctx->ciphertext, &sess->ciphertext);
  73. if (authPassword) {
  74. TestCa_setAuth(String_CONST(authPassword), NULL, sess);
  75. }
  76. return ctx;
  77. }
  78. static void testHello(uint8_t* password, uint8_t* expectedOutput, enum TestCa_Config cfg)
  79. {
  80. Assert_true(CString_strlen((char*)expectedOutput) == 264);
  81. struct Allocator* alloc = Allocator_new(1<<20);
  82. struct Context* ctx = setUp(NULL, HERPUBKEY, password, alloc, cfg);
  83. struct Message* msg = Message_new(0, CryptoHeader_SIZE + 32, alloc);
  84. Er_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  85. Iface_send(&ctx->plaintext, msg);
  86. char* actual = Hex_print(msg->msgbytes, Message_getLength(msg), alloc);
  87. if (CString_strcmp(actual, expectedOutput)) {
  88. Assert_failure("Test failed.\n"
  89. "Expected %s\n"
  90. " Got %s\n", expectedOutput, actual);
  91. }
  92. Allocator_free(alloc);
  93. }
  94. static void helloNoAuth(enum TestCa_Config cfg)
  95. {
  96. testHello(NULL,
  97. "00000000007691d3802a9d047c400000497a185dabda71739c1f35465fac3448"
  98. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  99. "74bee22a90642a6b4188f374afd90ccc97bb61873b5d8a3b4a6071b60b26a8c7"
  100. "2d6484634df315c4d3ad63de42fe3e4ebfd83bcdab2e1f5f40dc5a08eda4e6c6"
  101. "b7067d3b", cfg);
  102. }
  103. static void helloWithAuth(enum TestCa_Config cfg)
  104. {
  105. testHello("password",
  106. "0000000001641c99f7719f5700000000497a185dabda71739c1f35465fac3448"
  107. "b92a0c36ebff1cf7050383c91e7d56ec2336c09739fa8e91d8dc5bec63e8fad0"
  108. "74bee22a90642a6b022e089e0550ca84b86884af6a0263fa5fff9ba07583aea4"
  109. "acb000dbe4115623cf335c63981b9645b6c89fbdc3ad757744879751de0f215d"
  110. "2479131d", cfg);
  111. }
  112. static void receiveHelloWithNoAuth(enum TestCa_Config cfg)
  113. {
  114. uint8_t herPublic[32];
  115. Assert_true(Hex_decode(herPublic, 32,
  116. "847c0d2c375234f365e660955187a3735a0f7613d1609d3a6a4d8c53aeaa5a22", 64) > 0);
  117. struct Allocator* alloc = Allocator_new(1<<20);
  118. struct Context* ctx = setUp(PRIVATEKEY, herPublic, NULL, alloc, cfg);
  119. struct Message* msg = Message_new(132, 32, alloc);
  120. Assert_true(Hex_decode(msg->msgbytes, Message_getLength(msg),
  121. "0000000000ffffffffffffff7fffffffffffffffffffffffffffffffffffffff"
  122. "ffffffffffffffff847c0d2c375234f365e660955187a3735a0f7613d1609d3a"
  123. "6a4d8c53aeaa5a22ea9cf275eee0185edf7f211192f12e8e642a325ed76925fe"
  124. "3c76d313b767a10aca584ca0b979dee990a737da7d68366fa3846d43d541de91"
  125. "29ea3e12", 132*2) > 0);
  126. Er_assert(Message_epush(msg, NULL, 16)); // peer ipv6
  127. Iface_send(&ctx->ciphertext, msg);
  128. uint32_t err = Er_assert(Message_epop32h(msg));
  129. Assert_true(!err);
  130. Assert_true(Message_getLength(msg) == HELLOWORLDLEN);
  131. Assert_true(Bits_memcmp(HELLOWORLD, msg->msgbytes, 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. struct Message* msg = Message_new(0, CryptoHeader_SIZE + HELLOWORLDLEN + 32, alloc);
  146. Er_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  147. Iface_send(&ctx->plaintext, msg);
  148. Message_reset(msg);
  149. Er_assert(Message_epush(msg, HELLOWORLD, HELLOWORLDLEN));
  150. Iface_send(&ctx->plaintext, msg);
  151. char* actual = Hex_print(msg->msgbytes, 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. struct EventBase* 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. }