TestCa.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/test/TestCa.h"
  16. #include "crypto/CryptoAuth.h"
  17. #include "rust/cjdns_sys/Rffi.h"
  18. struct TestCa_s {
  19. Rffi_CryptoAuth2_t* ca2;
  20. struct CryptoAuth* ca;
  21. bool noise;
  22. };
  23. struct TestCa_Session_s {
  24. Rffi_CryptoAuth2_Session_t* s2;
  25. struct CryptoAuth_Session* s;
  26. };
  27. TestCa_t* TestCa_new(
  28. Allocator_t *allocator,
  29. const uint8_t *privateKey,
  30. struct EventBase* eventBase,
  31. struct Log* logger,
  32. struct Random* rand,
  33. enum TestCa_Config cfg)
  34. {
  35. TestCa_t* out = Allocator_calloc(allocator, sizeof(TestCa_t), 1);
  36. if (cfg == TestCa_Config_OLD || cfg == TestCa_Config_OLD_NEW) {
  37. out->ca = CryptoAuth_new(allocator, privateKey, eventBase, logger, rand);
  38. }
  39. if (cfg == TestCa_Config_OLD_NEW || cfg == TestCa_Config_NOISE) {
  40. out->ca2 = Rffi_CryptoAuth2_new(allocator, privateKey);
  41. }
  42. if (cfg == TestCa_Config_NOISE) {
  43. out->noise = true;
  44. }
  45. return out;
  46. }
  47. int TestCa_addUser_ipv6(
  48. String_t *password,
  49. String_t *login,
  50. uint8_t *ipv6,
  51. TestCa_t *ca)
  52. {
  53. int ret = 0;
  54. if (ca->ca) {
  55. ret = CryptoAuth_addUser_ipv6(password, login, ipv6, ca->ca);
  56. }
  57. if (ca->ca2) {
  58. int ret2 = Rffi_CryptoAuth2_addUser_ipv6(password, login, ipv6, ca->ca2);
  59. if (ca->ca) {
  60. Assert_true(ret == ret2);
  61. }
  62. return ret2;
  63. }
  64. return ret;
  65. }
  66. int TestCa_removeUsers(TestCa_t* ca, String_t* user)
  67. {
  68. int i1 = 0;
  69. if (ca->ca) {
  70. i1 = CryptoAuth_removeUsers(ca->ca, user);
  71. }
  72. if (ca->ca2) {
  73. int i2 = Rffi_CryptoAuth2_removeUsers(ca->ca2, user);
  74. if (ca->ca) {
  75. Assert_true(i1 == i2);
  76. }
  77. return i2;
  78. }
  79. return i1;
  80. }
  81. RTypes_StrList_t* TestCa_getUsers(const TestCa_t *ca, Allocator_t *alloc)
  82. {
  83. RTypes_StrList_t* l1 = NULL;
  84. if (ca->ca) {
  85. l1 = CryptoAuth_getUsers(ca->ca, alloc);
  86. }
  87. if (ca->ca2) {
  88. RTypes_StrList_t* l2 = Rffi_CryptoAuth2_getUsers(ca->ca2, alloc);
  89. Assert_true(l1->len == l2->len);
  90. for (uintptr_t i = 0; i < l1->len; i++) {
  91. Assert_true(String_equals(l1->items[i], l2->items[i]));
  92. }
  93. return l2;
  94. }
  95. return l1;
  96. }
  97. TestCa_Session_t* TestCa_newSession(
  98. TestCa_t *ca,
  99. Allocator_t *alloc,
  100. const uint8_t *herPublicKey,
  101. bool requireAuth,
  102. char *name,
  103. bool useNoise)
  104. {
  105. TestCa_Session_t* out = Allocator_calloc(alloc, sizeof(TestCa_Session_t), 1);
  106. if (ca->ca) {
  107. out->s = CryptoAuth_newSession(ca->ca, alloc, herPublicKey, requireAuth, name);
  108. }
  109. if (ca->ca2) {
  110. out->s2 = Rffi_CryptoAuth2_newSession(
  111. ca->ca2, alloc, herPublicKey, requireAuth, name, ca->noise && useNoise);
  112. }
  113. return out;
  114. }
  115. int TestCa_encrypt(TestCa_Session_t* sess, Message_t *msg)
  116. {
  117. Message_t* m2 = NULL;
  118. if (sess->s2) {
  119. if (sess->s) {
  120. m2 = Message_clone(msg, msg->alloc);
  121. } else {
  122. m2 = msg;
  123. msg = NULL;
  124. }
  125. }
  126. int i1 = 0;
  127. if (sess->s) {
  128. i1 = CryptoAuth_encrypt(sess->s, msg);
  129. }
  130. if (sess->s2) {
  131. int i2 = Rffi_CryptoAuth2_encrypt(sess->s2, m2);
  132. if (sess->s) {
  133. Assert_true(i2 == i1);
  134. Assert_true(msg->length == m2->length);
  135. Assert_true(!Bits_memcmp(msg->bytes, m2->bytes, msg->length));
  136. }
  137. return i2;
  138. }
  139. return i1;
  140. }
  141. int TestCa_decrypt(TestCa_Session_t *sess, Message_t *msg)
  142. {
  143. Message_t* m2 = NULL;
  144. if (sess->s2) {
  145. if (sess->s) {
  146. m2 = Message_clone(msg, msg->alloc);
  147. } else {
  148. m2 = msg;
  149. msg = NULL;
  150. }
  151. }
  152. int i1 = 0;
  153. if (sess->s) {
  154. i1 = CryptoAuth_decrypt(sess->s, msg);
  155. }
  156. if (sess->s2) {
  157. int i2 = Rffi_CryptoAuth2_decrypt(sess->s2, m2);
  158. if (sess->s) {
  159. Assert_true(i2 == i1);
  160. Assert_true(msg->length == m2->length);
  161. Assert_true(!Bits_memcmp(msg->bytes, m2->bytes, msg->length));
  162. }
  163. return i2;
  164. }
  165. return i1;
  166. }
  167. void TestCa_setAuth(const String_t* password, const String_t* login, TestCa_Session_t* sess)
  168. {
  169. if (sess->s) {
  170. CryptoAuth_setAuth(password, login, sess->s);
  171. }
  172. if (sess->s2) {
  173. Rffi_CryptoAuth2_setAuth(password, login, sess->s2);
  174. }
  175. }
  176. void TestCa_resetIfTimeout(TestCa_Session_t* sess)
  177. {
  178. if (sess->s) {
  179. CryptoAuth_resetIfTimeout(sess->s);
  180. }
  181. if (sess->s2) {
  182. Rffi_CryptoAuth2_resetIfTimeout(sess->s2);
  183. }
  184. }
  185. void TestCa_reset(TestCa_Session_t* sess)
  186. {
  187. if (sess->s) {
  188. CryptoAuth_reset(sess->s);
  189. }
  190. if (sess->s2) {
  191. Rffi_CryptoAuth2_reset(sess->s2);
  192. }
  193. }
  194. RTypes_CryptoAuth_State_t TestCa_getState(TestCa_Session_t* sess)
  195. {
  196. RTypes_CryptoAuth_State_t st = 0;
  197. if (sess->s) {
  198. st = CryptoAuth_getState(sess->s);
  199. }
  200. if (sess->s2) {
  201. RTypes_CryptoAuth_State_t st2 = Rffi_CryptoAuth2_getState(sess->s2);
  202. if (sess->s) {
  203. Assert_true(st2 == st);
  204. }
  205. return st2;
  206. }
  207. return st;
  208. }
  209. void TestCa_getHerPubKey(TestCa_Session_t* sess, uint8_t* buf)
  210. {
  211. uint8_t hpk1[32];
  212. if (sess->s) {
  213. CryptoAuth_getHerPubKey(sess->s, hpk1);
  214. }
  215. if (sess->s2) {
  216. Rffi_CryptoAuth2_getHerPubKey(sess->s2, buf);
  217. if (sess->s) {
  218. Assert_true(!Bits_memcmp(hpk1, buf, 32));
  219. }
  220. return;
  221. }
  222. Bits_memcpy(buf, hpk1, 32);
  223. }
  224. void TestCa_getPubKey(TestCa_t *ca, uint8_t* buf)
  225. {
  226. uint8_t pk1[32];
  227. if (ca->ca) {
  228. CryptoAuth_getPubKey(ca->ca, pk1);
  229. }
  230. if (ca->ca2) {
  231. Rffi_CryptoAuth2_getPubKey(ca->ca2, buf);
  232. if (ca->ca) {
  233. Assert_true(!Bits_memcmp(pk1, buf, 32));
  234. }
  235. return;
  236. }
  237. Bits_memcpy(buf, pk1, 32);
  238. }