test_link_handshake.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define CHANNELTLS_PRIVATE
  5. #define CONNECTION_PRIVATE
  6. #define TOR_CHANNEL_INTERNAL_
  7. #define TORTLS_PRIVATE
  8. #include "common/compat.h"
  9. #include "or/or.h"
  10. #include "or/config.h"
  11. #include "or/connection.h"
  12. #include "or/connection_or.h"
  13. #include "or/channeltls.h"
  14. #include "trunnel/link_handshake.h"
  15. #include "or/router.h"
  16. #include "or/routerkeys.h"
  17. #include "or/scheduler.h"
  18. #include "or/torcert.h"
  19. #include "or/or_connection_st.h"
  20. #include "or/or_handshake_certs_st.h"
  21. #include "or/or_handshake_state_st.h"
  22. #include "or/var_cell_st.h"
  23. #include "test/test.h"
  24. #include "test/log_test_helpers.h"
  25. static var_cell_t *mock_got_var_cell = NULL;
  26. static void
  27. mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
  28. {
  29. (void)conn;
  30. var_cell_t *newcell = var_cell_new(vc->payload_len);
  31. memcpy(newcell, vc, sizeof(var_cell_t));
  32. memcpy(newcell->payload, vc->payload, vc->payload_len);
  33. mock_got_var_cell = newcell;
  34. }
  35. static int
  36. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  37. {
  38. (void) tls;
  39. (void) cert; // XXXX look at this.
  40. return 1;
  41. }
  42. static tor_tls_t *mock_peer_cert_expect_tortls = NULL;
  43. static tor_x509_cert_t *mock_peer_cert = NULL;
  44. static tor_x509_cert_t *
  45. mock_get_peer_cert(tor_tls_t *tls)
  46. {
  47. if (mock_peer_cert_expect_tortls &&
  48. mock_peer_cert_expect_tortls != tls)
  49. return NULL;
  50. return tor_x509_cert_dup(mock_peer_cert);
  51. }
  52. static int mock_send_netinfo_called = 0;
  53. static int
  54. mock_send_netinfo(or_connection_t *conn)
  55. {
  56. (void) conn;
  57. ++mock_send_netinfo_called;// XXX check_this
  58. return 0;
  59. }
  60. static int mock_close_called = 0;
  61. static void
  62. mock_close_for_err(or_connection_t *orconn, int flush)
  63. {
  64. (void)orconn;
  65. (void)flush;
  66. ++mock_close_called;
  67. }
  68. static int mock_send_authenticate_called = 0;
  69. static int mock_send_authenticate_called_with_type = 0;
  70. static int
  71. mock_send_authenticate(or_connection_t *conn, int type)
  72. {
  73. (void) conn;
  74. mock_send_authenticate_called_with_type = type;
  75. ++mock_send_authenticate_called;// XXX check_this
  76. return 0;
  77. }
  78. static int
  79. mock_export_key_material(tor_tls_t *tls, uint8_t *secrets_out,
  80. const uint8_t *context,
  81. size_t context_len,
  82. const char *label)
  83. {
  84. (void) tls;
  85. (void)secrets_out;
  86. (void)context;
  87. (void)context_len;
  88. (void)label;
  89. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  90. return 0;
  91. }
  92. static tor_x509_cert_t *mock_own_cert = NULL;
  93. static tor_x509_cert_t *
  94. mock_get_own_cert(tor_tls_t *tls)
  95. {
  96. (void)tls;
  97. return tor_x509_cert_dup(mock_own_cert);
  98. }
  99. /* Test good certs cells */
  100. static void
  101. test_link_handshake_certs_ok(void *arg)
  102. {
  103. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  104. or_connection_t *c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  105. var_cell_t *cell1 = NULL, *cell2 = NULL;
  106. certs_cell_t *cc1 = NULL, *cc2 = NULL;
  107. channel_tls_t *chan1 = NULL, *chan2 = NULL;
  108. crypto_pk_t *key1 = NULL, *key2 = NULL;
  109. const int with_ed = !strcmp((const char *)arg, "Ed25519");
  110. tor_addr_from_ipv4h(&c1->base_.addr, 0x7f000001);
  111. tor_addr_from_ipv4h(&c2->base_.addr, 0x7f000001);
  112. scheduler_init();
  113. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  114. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  115. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  116. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  117. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  118. key1 = pk_generate(2);
  119. key2 = pk_generate(3);
  120. /* We need to make sure that our TLS certificates are set up before we can
  121. * actually generate a CERTS cell.
  122. */
  123. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  124. key1, key2, 86400), OP_EQ, 0);
  125. if (with_ed) {
  126. /* If we're making a CERTS cell for an ed handshake, let's make sure we
  127. * have some Ed25519 certificates and keys. */
  128. init_mock_ed_keys(key2);
  129. } else {
  130. certs_cell_ed25519_disabled_for_testing = 1;
  131. }
  132. /* c1 has started_here == 1 */
  133. {
  134. const tor_x509_cert_t *link_cert = NULL;
  135. tt_assert(!tor_tls_get_my_certs(1, &link_cert, NULL));
  136. mock_own_cert = tor_x509_cert_dup(link_cert);
  137. }
  138. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  139. c1->link_proto = 3;
  140. tt_int_op(connection_init_or_handshake_state(c1, 1), OP_EQ, 0);
  141. /* c2 has started_here == 0 */
  142. c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  143. c2->link_proto = 3;
  144. tt_int_op(connection_init_or_handshake_state(c2, 0), OP_EQ, 0);
  145. tt_int_op(0, OP_EQ, connection_or_send_certs_cell(c1));
  146. tt_assert(mock_got_var_cell);
  147. cell1 = mock_got_var_cell;
  148. tt_int_op(0, OP_EQ, connection_or_send_certs_cell(c2));
  149. tt_assert(mock_got_var_cell);
  150. cell2 = mock_got_var_cell;
  151. tt_int_op(cell1->command, OP_EQ, CELL_CERTS);
  152. tt_int_op(cell1->payload_len, OP_GT, 1);
  153. tt_int_op(cell2->command, OP_EQ, CELL_CERTS);
  154. tt_int_op(cell2->payload_len, OP_GT, 1);
  155. tt_int_op(cell1->payload_len, OP_EQ,
  156. certs_cell_parse(&cc1, cell1->payload, cell1->payload_len));
  157. tt_int_op(cell2->payload_len, OP_EQ,
  158. certs_cell_parse(&cc2, cell2->payload, cell2->payload_len));
  159. if (with_ed) {
  160. tt_int_op(5, OP_EQ, cc1->n_certs);
  161. tt_int_op(5, OP_EQ, cc2->n_certs);
  162. } else {
  163. tt_int_op(2, OP_EQ, cc1->n_certs);
  164. tt_int_op(2, OP_EQ, cc2->n_certs);
  165. }
  166. tt_int_op(certs_cell_get_certs(cc1, 0)->cert_type, OP_EQ,
  167. CERTTYPE_RSA1024_ID_AUTH);
  168. tt_int_op(certs_cell_get_certs(cc1, 1)->cert_type, OP_EQ,
  169. CERTTYPE_RSA1024_ID_ID);
  170. tt_int_op(certs_cell_get_certs(cc2, 0)->cert_type, OP_EQ,
  171. CERTTYPE_RSA1024_ID_LINK);
  172. tt_int_op(certs_cell_get_certs(cc2, 1)->cert_type, OP_EQ,
  173. CERTTYPE_RSA1024_ID_ID);
  174. if (with_ed) {
  175. tt_int_op(certs_cell_get_certs(cc1, 2)->cert_type, OP_EQ,
  176. CERTTYPE_ED_ID_SIGN);
  177. tt_int_op(certs_cell_get_certs(cc1, 3)->cert_type, OP_EQ,
  178. CERTTYPE_ED_SIGN_AUTH);
  179. tt_int_op(certs_cell_get_certs(cc1, 4)->cert_type, OP_EQ,
  180. CERTTYPE_RSA1024_ID_EDID);
  181. tt_int_op(certs_cell_get_certs(cc2, 2)->cert_type, OP_EQ,
  182. CERTTYPE_ED_ID_SIGN);
  183. tt_int_op(certs_cell_get_certs(cc2, 3)->cert_type, OP_EQ,
  184. CERTTYPE_ED_SIGN_LINK);
  185. tt_int_op(certs_cell_get_certs(cc2, 4)->cert_type, OP_EQ,
  186. CERTTYPE_RSA1024_ID_EDID);
  187. }
  188. chan1 = tor_malloc_zero(sizeof(*chan1));
  189. channel_tls_common_init(chan1);
  190. c1->chan = chan1;
  191. chan1->conn = c1;
  192. c1->base_.address = tor_strdup("C1");
  193. c1->tls = tor_tls_new(-1, 0);
  194. c1->link_proto = 4;
  195. c1->base_.conn_array_index = -1;
  196. crypto_pk_get_digest(key2, c1->identity_digest);
  197. if (with_ed) {
  198. const tor_x509_cert_t *linkc, *idc;
  199. tor_tls_get_my_certs(1, &linkc, &idc);
  200. mock_peer_cert_expect_tortls = c1->tls; /* We should see this tls... */
  201. mock_peer_cert = tor_x509_cert_dup(linkc); /* and when we do, the peer's
  202. * cert is this... */
  203. }
  204. channel_tls_process_certs_cell(cell2, chan1);
  205. mock_peer_cert_expect_tortls = NULL;
  206. tor_x509_cert_free(mock_peer_cert);
  207. mock_peer_cert = NULL;
  208. tor_assert(c1->handshake_state->authenticated);
  209. tt_assert(c1->handshake_state->received_certs_cell);
  210. tt_ptr_op(c1->handshake_state->certs->auth_cert, OP_EQ, NULL);
  211. tt_ptr_op(c1->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  212. tt_assert(c1->handshake_state->certs->id_cert);
  213. if (with_ed) {
  214. tt_assert(c1->handshake_state->certs->ed_sign_link);
  215. tt_assert(c1->handshake_state->certs->ed_rsa_crosscert);
  216. tt_assert(c1->handshake_state->certs->ed_id_sign);
  217. tt_assert(c1->handshake_state->authenticated_rsa);
  218. tt_assert(c1->handshake_state->authenticated_ed25519);
  219. } else {
  220. tt_ptr_op(c1->handshake_state->certs->ed_sign_link, OP_EQ, NULL);
  221. tt_ptr_op(c1->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  222. tt_ptr_op(c1->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  223. tt_assert(c1->handshake_state->authenticated_rsa);
  224. tt_assert(! c1->handshake_state->authenticated_ed25519);
  225. }
  226. tt_assert(! tor_mem_is_zero(
  227. (char*)c1->handshake_state->authenticated_rsa_peer_id, 20));
  228. chan2 = tor_malloc_zero(sizeof(*chan2));
  229. channel_tls_common_init(chan2);
  230. c2->chan = chan2;
  231. chan2->conn = c2;
  232. c2->base_.address = tor_strdup("C2");
  233. c2->tls = tor_tls_new(-1, 1);
  234. c2->link_proto = 4;
  235. c2->base_.conn_array_index = -1;
  236. crypto_pk_get_digest(key1, c2->identity_digest);
  237. channel_tls_process_certs_cell(cell1, chan2);
  238. tt_assert(c2->handshake_state->received_certs_cell);
  239. if (with_ed) {
  240. tt_assert(c2->handshake_state->certs->ed_sign_auth);
  241. tt_assert(c2->handshake_state->certs->ed_rsa_crosscert);
  242. tt_assert(c2->handshake_state->certs->ed_id_sign);
  243. } else {
  244. tt_assert(c2->handshake_state->certs->auth_cert);
  245. tt_ptr_op(c2->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  246. tt_ptr_op(c2->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  247. tt_ptr_op(c2->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  248. }
  249. tt_assert(c2->handshake_state->certs->id_cert);
  250. tt_assert(tor_mem_is_zero(
  251. (char*)c2->handshake_state->authenticated_rsa_peer_id, 20));
  252. /* no authentication has happened yet, since we haen't gotten an AUTH cell.
  253. */
  254. tt_assert(! c2->handshake_state->authenticated);
  255. tt_assert(! c2->handshake_state->authenticated_rsa);
  256. tt_assert(! c2->handshake_state->authenticated_ed25519);
  257. done:
  258. UNMOCK(tor_tls_cert_matches_key);
  259. UNMOCK(connection_or_write_var_cell_to_buf);
  260. UNMOCK(connection_or_send_netinfo);
  261. UNMOCK(tor_tls_get_peer_cert);
  262. UNMOCK(tor_tls_get_own_cert);
  263. tor_x509_cert_free(mock_own_cert);
  264. tor_x509_cert_free(mock_peer_cert);
  265. mock_own_cert = mock_peer_cert = NULL;
  266. memset(c1->identity_digest, 0, sizeof(c1->identity_digest));
  267. memset(c2->identity_digest, 0, sizeof(c2->identity_digest));
  268. connection_free_minimal(TO_CONN(c1));
  269. connection_free_minimal(TO_CONN(c2));
  270. tor_free(cell1);
  271. tor_free(cell2);
  272. certs_cell_free(cc1);
  273. certs_cell_free(cc2);
  274. if (chan1)
  275. circuitmux_free(chan1->base_.cmux);
  276. tor_free(chan1);
  277. if (chan2)
  278. circuitmux_free(chan2->base_.cmux);
  279. tor_free(chan2);
  280. crypto_pk_free(key1);
  281. crypto_pk_free(key2);
  282. }
  283. typedef struct certs_data_s {
  284. int is_ed;
  285. int is_link_cert;
  286. or_connection_t *c;
  287. channel_tls_t *chan;
  288. certs_cell_t *ccell;
  289. var_cell_t *cell;
  290. crypto_pk_t *key1, *key2;
  291. } certs_data_t;
  292. static int
  293. recv_certs_cleanup(const struct testcase_t *test, void *obj)
  294. {
  295. (void)test;
  296. certs_data_t *d = obj;
  297. UNMOCK(tor_tls_cert_matches_key);
  298. UNMOCK(connection_or_send_netinfo);
  299. UNMOCK(connection_or_close_for_error);
  300. UNMOCK(tor_tls_get_peer_cert);
  301. UNMOCK(tor_tls_get_own_cert);
  302. if (d) {
  303. tor_free(d->cell);
  304. certs_cell_free(d->ccell);
  305. connection_or_clear_identity(d->c);
  306. connection_free_minimal(TO_CONN(d->c));
  307. circuitmux_free(d->chan->base_.cmux);
  308. tor_free(d->chan);
  309. crypto_pk_free(d->key1);
  310. crypto_pk_free(d->key2);
  311. tor_free(d);
  312. }
  313. routerkeys_free_all();
  314. return 1;
  315. }
  316. static void *
  317. recv_certs_setup(const struct testcase_t *test)
  318. {
  319. (void)test;
  320. certs_data_t *d = tor_malloc_zero(sizeof(*d));
  321. certs_cell_cert_t *ccc1 = NULL;
  322. certs_cell_cert_t *ccc2 = NULL;
  323. ssize_t n;
  324. int is_ed = d->is_ed = !strcmpstart(test->setup_data, "Ed25519");
  325. int is_rsa = !strcmpstart(test->setup_data, "RSA");
  326. int is_link = d->is_link_cert = !strcmpend(test->setup_data, "-Link");
  327. int is_auth = !strcmpend(test->setup_data, "-Auth");
  328. tor_assert(is_ed != is_rsa);
  329. tor_assert(is_link != is_auth);
  330. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  331. d->chan = tor_malloc_zero(sizeof(*d->chan));
  332. d->c->chan = d->chan;
  333. d->c->base_.address = tor_strdup("HaveAnAddress");
  334. tor_addr_from_ipv4h(&d->c->base_.addr, 0x801f0127);
  335. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  336. d->chan->conn = d->c;
  337. tt_int_op(connection_init_or_handshake_state(d->c, 1), OP_EQ, 0);
  338. d->c->link_proto = 4;
  339. d->key1 = pk_generate(2);
  340. d->key2 = pk_generate(3);
  341. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  342. d->key1, d->key2, 86400), OP_EQ, 0);
  343. if (is_ed) {
  344. init_mock_ed_keys(d->key2);
  345. } else {
  346. routerkeys_free_all();
  347. }
  348. d->ccell = certs_cell_new();
  349. ccc1 = certs_cell_cert_new();
  350. certs_cell_add_certs(d->ccell, ccc1);
  351. ccc2 = certs_cell_cert_new();
  352. certs_cell_add_certs(d->ccell, ccc2);
  353. d->ccell->n_certs = 2;
  354. ccc1->cert_type = is_link ? 1 : 3;
  355. ccc2->cert_type = 2;
  356. const tor_x509_cert_t *a,*b;
  357. const uint8_t *enca, *encb;
  358. size_t lena, lenb;
  359. tor_tls_get_my_certs(is_link ? 1 : 0, &a, &b);
  360. tor_x509_cert_get_der(a, &enca, &lena);
  361. tor_x509_cert_get_der(b, &encb, &lenb);
  362. certs_cell_cert_setlen_body(ccc1, lena);
  363. ccc1->cert_len = lena;
  364. certs_cell_cert_setlen_body(ccc2, lenb);
  365. ccc2->cert_len = lenb;
  366. memcpy(certs_cell_cert_getarray_body(ccc1), enca, lena);
  367. memcpy(certs_cell_cert_getarray_body(ccc2), encb, lenb);
  368. if (is_ed) {
  369. certs_cell_cert_t *ccc3 = NULL; /* Id->Sign */
  370. certs_cell_cert_t *ccc4 = NULL; /* Sign->Link or Sign->Auth. */
  371. certs_cell_cert_t *ccc5 = NULL; /* RSAId->Ed Id. */
  372. const tor_cert_t *id_sign = get_master_signing_key_cert();
  373. const tor_cert_t *secondary =
  374. is_link ? get_current_link_cert_cert() : get_current_auth_key_cert();
  375. const uint8_t *cc = NULL;
  376. size_t cc_sz;
  377. get_master_rsa_crosscert(&cc, &cc_sz);
  378. ccc3 = certs_cell_cert_new();
  379. ccc4 = certs_cell_cert_new();
  380. ccc5 = certs_cell_cert_new();
  381. certs_cell_add_certs(d->ccell, ccc3);
  382. certs_cell_add_certs(d->ccell, ccc4);
  383. certs_cell_add_certs(d->ccell, ccc5);
  384. ccc3->cert_len = id_sign->encoded_len;
  385. ccc4->cert_len = secondary->encoded_len;
  386. ccc5->cert_len = cc_sz;
  387. certs_cell_cert_setlen_body(ccc3, ccc3->cert_len);
  388. certs_cell_cert_setlen_body(ccc4, ccc4->cert_len);
  389. certs_cell_cert_setlen_body(ccc5, ccc5->cert_len);
  390. memcpy(certs_cell_cert_getarray_body(ccc3), id_sign->encoded,
  391. ccc3->cert_len);
  392. memcpy(certs_cell_cert_getarray_body(ccc4), secondary->encoded,
  393. ccc4->cert_len);
  394. memcpy(certs_cell_cert_getarray_body(ccc5), cc, ccc5->cert_len);
  395. ccc3->cert_type = 4;
  396. ccc4->cert_type = is_link ? 5 : 6;
  397. ccc5->cert_type = 7;
  398. d->ccell->n_certs = 5;
  399. }
  400. d->cell = var_cell_new(4096);
  401. d->cell->command = CELL_CERTS;
  402. n = certs_cell_encode(d->cell->payload, 4096, d->ccell);
  403. tt_int_op(n, OP_GT, 0);
  404. d->cell->payload_len = n;
  405. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  406. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  407. MOCK(connection_or_close_for_error, mock_close_for_err);
  408. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  409. if (is_link) {
  410. /* Say that this is the peer's certificate */
  411. mock_peer_cert = tor_x509_cert_dup(a);
  412. }
  413. tt_int_op(0, OP_EQ, d->c->handshake_state->received_certs_cell);
  414. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  415. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  416. return d;
  417. done:
  418. recv_certs_cleanup(test, d);
  419. return NULL;
  420. }
  421. static struct testcase_setup_t setup_recv_certs = {
  422. .setup_fn = recv_certs_setup,
  423. .cleanup_fn = recv_certs_cleanup
  424. };
  425. static void
  426. test_link_handshake_recv_certs_ok(void *arg)
  427. {
  428. certs_data_t *d = arg;
  429. channel_tls_process_certs_cell(d->cell, d->chan);
  430. tt_int_op(0, OP_EQ, mock_close_called);
  431. tt_int_op(d->c->handshake_state->authenticated, OP_EQ, 1);
  432. tt_int_op(d->c->handshake_state->authenticated_rsa, OP_EQ, 1);
  433. tt_int_op(d->c->handshake_state->received_certs_cell, OP_EQ, 1);
  434. tt_ptr_op(d->c->handshake_state->certs->id_cert, OP_NE, NULL);
  435. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_EQ, NULL);
  436. if (d->is_ed) {
  437. tt_ptr_op(d->c->handshake_state->certs->ed_id_sign, OP_NE, NULL);
  438. tt_ptr_op(d->c->handshake_state->certs->ed_sign_link, OP_NE, NULL);
  439. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  440. tt_ptr_op(d->c->handshake_state->certs->ed_rsa_crosscert, OP_NE, NULL);
  441. tt_int_op(d->c->handshake_state->authenticated_ed25519, OP_EQ, 1);
  442. } else {
  443. tt_ptr_op(d->c->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  444. tt_ptr_op(d->c->handshake_state->certs->ed_sign_link, OP_EQ, NULL);
  445. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  446. tt_ptr_op(d->c->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  447. tt_int_op(d->c->handshake_state->authenticated_ed25519, OP_EQ, 0);
  448. }
  449. done:
  450. ;
  451. }
  452. static void
  453. test_link_handshake_recv_certs_ok_server(void *arg)
  454. {
  455. certs_data_t *d = arg;
  456. d->c->handshake_state->started_here = 0;
  457. d->c->handshake_state->certs->started_here = 0;
  458. channel_tls_process_certs_cell(d->cell, d->chan);
  459. tt_int_op(0, OP_EQ, mock_close_called);
  460. tt_int_op(d->c->handshake_state->authenticated, OP_EQ, 0);
  461. tt_int_op(d->c->handshake_state->received_certs_cell, OP_EQ, 1);
  462. tt_ptr_op(d->c->handshake_state->certs->id_cert, OP_NE, NULL);
  463. tt_ptr_op(d->c->handshake_state->certs->link_cert, OP_EQ, NULL);
  464. if (d->is_ed) {
  465. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_NE, NULL);
  466. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_EQ, NULL);
  467. } else {
  468. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  469. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_NE, NULL);
  470. }
  471. done:
  472. ;
  473. }
  474. #define CERTS_FAIL(name, code) \
  475. static void \
  476. test_link_handshake_recv_certs_ ## name(void *arg) \
  477. { \
  478. certs_data_t *d = arg; \
  479. const char *require_failure_message = NULL; \
  480. setup_capture_of_logs(LOG_INFO); \
  481. { code ; } \
  482. channel_tls_process_certs_cell(d->cell, d->chan); \
  483. tt_int_op(1, OP_EQ, mock_close_called); \
  484. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  485. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  486. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_rsa); \
  487. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_ed25519); \
  488. if (require_failure_message) { \
  489. expect_log_msg_containing(require_failure_message); \
  490. } \
  491. done: \
  492. teardown_capture_of_logs(); \
  493. }
  494. CERTS_FAIL(badstate,
  495. require_failure_message = "We're not doing a v3 handshake!";
  496. d->c->base_.state = OR_CONN_STATE_CONNECTING;)
  497. CERTS_FAIL(badproto,
  498. require_failure_message = "not using link protocol >= 3";
  499. d->c->link_proto = 2)
  500. CERTS_FAIL(duplicate,
  501. require_failure_message = "We already got one";
  502. d->c->handshake_state->received_certs_cell = 1)
  503. CERTS_FAIL(already_authenticated,
  504. require_failure_message = "We're already authenticated!";
  505. d->c->handshake_state->authenticated = 1)
  506. CERTS_FAIL(empty,
  507. require_failure_message = "It had no body";
  508. d->cell->payload_len = 0)
  509. CERTS_FAIL(bad_circid,
  510. require_failure_message = "It had a nonzero circuit ID";
  511. d->cell->circ_id = 1)
  512. CERTS_FAIL(truncated_1,
  513. require_failure_message = "It couldn't be parsed";
  514. d->cell->payload[0] = 5)
  515. CERTS_FAIL(truncated_2,
  516. {
  517. require_failure_message = "It couldn't be parsed";
  518. d->cell->payload_len = 4;
  519. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);
  520. })
  521. CERTS_FAIL(truncated_3,
  522. {
  523. require_failure_message = "It couldn't be parsed";
  524. d->cell->payload_len = 7;
  525. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);
  526. })
  527. CERTS_FAIL(truncated_4, /* ed25519 */
  528. {
  529. require_failure_message = "It couldn't be parsed";
  530. d->cell->payload_len -= 10;
  531. })
  532. CERTS_FAIL(truncated_5, /* ed25519 */
  533. {
  534. require_failure_message = "It couldn't be parsed";
  535. d->cell->payload_len -= 100;
  536. })
  537. #define REENCODE() do { \
  538. const char *msg = certs_cell_check(d->ccell); \
  539. if (msg) puts(msg); \
  540. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  541. tt_int_op(n, OP_GT, 0); \
  542. d->cell->payload_len = n; \
  543. } while (0)
  544. CERTS_FAIL(truncated_6, /* ed25519 */
  545. {
  546. /* truncate the link certificate */
  547. require_failure_message = "undecodable Ed certificate";
  548. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 3), 7);
  549. certs_cell_get_certs(d->ccell, 3)->cert_len = 7;
  550. REENCODE();
  551. })
  552. CERTS_FAIL(truncated_7, /* ed25519 */
  553. {
  554. /* truncate the crosscert */
  555. require_failure_message = "Unparseable or overlong crosscert";
  556. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 4), 7);
  557. certs_cell_get_certs(d->ccell, 4)->cert_len = 7;
  558. REENCODE();
  559. })
  560. CERTS_FAIL(not_x509,
  561. {
  562. require_failure_message = "Received undecodable certificate";
  563. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  564. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  565. REENCODE();
  566. })
  567. CERTS_FAIL(both_link,
  568. {
  569. require_failure_message = "Duplicate x509 certificate";
  570. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  571. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  572. REENCODE();
  573. })
  574. CERTS_FAIL(both_id_rsa,
  575. {
  576. require_failure_message = "Duplicate x509 certificate";
  577. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  578. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  579. REENCODE();
  580. })
  581. CERTS_FAIL(both_auth,
  582. {
  583. require_failure_message = "Duplicate x509 certificate";
  584. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  585. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  586. REENCODE();
  587. })
  588. CERTS_FAIL(duplicate_id, /* ed25519 */
  589. {
  590. require_failure_message = "Duplicate Ed25519 certificate";
  591. certs_cell_get_certs(d->ccell, 2)->cert_type = 4;
  592. certs_cell_get_certs(d->ccell, 3)->cert_type = 4;
  593. REENCODE();
  594. })
  595. CERTS_FAIL(duplicate_link, /* ed25519 */
  596. {
  597. require_failure_message = "Duplicate Ed25519 certificate";
  598. certs_cell_get_certs(d->ccell, 2)->cert_type = 5;
  599. certs_cell_get_certs(d->ccell, 3)->cert_type = 5;
  600. REENCODE();
  601. })
  602. CERTS_FAIL(duplicate_crosscert, /* ed25519 */
  603. {
  604. require_failure_message = "Duplicate RSA->Ed25519 crosscert";
  605. certs_cell_get_certs(d->ccell, 2)->cert_type = 7;
  606. certs_cell_get_certs(d->ccell, 3)->cert_type = 7;
  607. REENCODE();
  608. })
  609. static void
  610. test_link_handshake_recv_certs_missing_id(void *arg) /* ed25519 */
  611. {
  612. certs_data_t *d = arg;
  613. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  614. certs_cell_set_certs(d->ccell, 2, certs_cell_get_certs(d->ccell, 4));
  615. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  616. certs_cell_setlen_certs(d->ccell, 4);
  617. d->ccell->n_certs = 4;
  618. REENCODE();
  619. /* This handshake succeeds, but since we have no ID cert, we will
  620. * just do the RSA handshake. */
  621. channel_tls_process_certs_cell(d->cell, d->chan);
  622. tt_int_op(0, OP_EQ, mock_close_called);
  623. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_ed25519);
  624. tt_int_op(1, OP_EQ, d->c->handshake_state->authenticated_rsa);
  625. done:
  626. ;
  627. }
  628. CERTS_FAIL(missing_signing_key, /* ed25519 */
  629. {
  630. require_failure_message = "No Ed25519 signing key";
  631. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  632. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 2);
  633. tt_int_op(cert->cert_type, OP_EQ, CERTTYPE_ED_ID_SIGN);
  634. /* replace this with a valid master->signing cert, but with no
  635. * signing key. */
  636. const ed25519_keypair_t *mk = get_master_identity_keypair();
  637. const ed25519_keypair_t *sk = get_master_signing_keypair();
  638. tor_cert_t *bad_cert = tor_cert_create(mk, CERT_TYPE_ID_SIGNING,
  639. &sk->pubkey, time(NULL), 86400,
  640. 0 /* don't include signer */);
  641. certs_cell_cert_setlen_body(cert, bad_cert->encoded_len);
  642. memcpy(certs_cell_cert_getarray_body(cert),
  643. bad_cert->encoded, bad_cert->encoded_len);
  644. cert->cert_len = bad_cert->encoded_len;
  645. tor_cert_free(bad_cert);
  646. REENCODE();
  647. })
  648. CERTS_FAIL(missing_link, /* ed25519 */
  649. {
  650. require_failure_message = "No Ed25519 link key";
  651. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  652. certs_cell_set_certs(d->ccell, 3, certs_cell_get_certs(d->ccell, 4));
  653. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  654. certs_cell_setlen_certs(d->ccell, 4);
  655. d->ccell->n_certs = 4;
  656. REENCODE();
  657. })
  658. CERTS_FAIL(missing_auth, /* ed25519 */
  659. {
  660. d->c->handshake_state->started_here = 0;
  661. d->c->handshake_state->certs->started_here = 0;
  662. require_failure_message = "No Ed25519 link authentication key";
  663. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  664. certs_cell_set_certs(d->ccell, 3, certs_cell_get_certs(d->ccell, 4));
  665. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  666. certs_cell_setlen_certs(d->ccell, 4);
  667. d->ccell->n_certs = 4;
  668. REENCODE();
  669. })
  670. CERTS_FAIL(missing_crosscert, /* ed25519 */
  671. {
  672. require_failure_message = "Missing RSA->Ed25519 crosscert";
  673. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  674. certs_cell_setlen_certs(d->ccell, 4);
  675. d->ccell->n_certs = 4;
  676. REENCODE();
  677. })
  678. CERTS_FAIL(missing_rsa_id, /* ed25519 */
  679. {
  680. require_failure_message = "Missing legacy RSA ID cert";
  681. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  682. certs_cell_set_certs(d->ccell, 1, certs_cell_get_certs(d->ccell, 4));
  683. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  684. certs_cell_setlen_certs(d->ccell, 4);
  685. d->ccell->n_certs = 4;
  686. REENCODE();
  687. })
  688. CERTS_FAIL(link_mismatch, /* ed25519 */
  689. {
  690. require_failure_message = "Link certificate does not match "
  691. "TLS certificate";
  692. const tor_x509_cert_t *idc;
  693. tor_tls_get_my_certs(1, NULL, &idc);
  694. tor_x509_cert_free(mock_peer_cert);
  695. /* Pretend that the peer cert was something else. */
  696. mock_peer_cert = tor_x509_cert_dup(idc);
  697. /* No reencode needed. */
  698. })
  699. CERTS_FAIL(bad_ed_sig, /* ed25519 */
  700. {
  701. require_failure_message = "At least one Ed25519 certificate was "
  702. "badly signed";
  703. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 3);
  704. uint8_t *body = certs_cell_cert_getarray_body(cert);
  705. ssize_t body_len = certs_cell_cert_getlen_body(cert);
  706. /* Frob a byte in the signature */
  707. body[body_len - 13] ^= 7;
  708. REENCODE();
  709. })
  710. CERTS_FAIL(bad_crosscert, /*ed25519*/
  711. {
  712. require_failure_message = "Invalid RSA->Ed25519 crosscert";
  713. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 4);
  714. uint8_t *body = certs_cell_cert_getarray_body(cert);
  715. ssize_t body_len = certs_cell_cert_getlen_body(cert);
  716. /* Frob a byte in the signature */
  717. body[body_len - 13] ^= 7;
  718. REENCODE();
  719. })
  720. CERTS_FAIL(bad_rsa_id_cert, /*ed25519*/
  721. {
  722. require_failure_message = "legacy RSA ID certificate was not valid";
  723. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 1);
  724. uint8_t *body = certs_cell_cert_getarray_body(cert);
  725. ssize_t body_len = certs_cell_cert_getlen_body(cert);
  726. /* Frob a byte in the signature */
  727. body[body_len - 13] ^= 7;
  728. REENCODE();
  729. })
  730. CERTS_FAIL(expired_rsa_id, /* both */
  731. {
  732. require_failure_message = "Certificate already expired";
  733. /* we're going to replace the identity cert with an expired one. */
  734. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 1);
  735. const tor_x509_cert_t *idc;
  736. tor_tls_get_my_certs(1, NULL, &idc);
  737. tor_x509_cert_t *newc;
  738. time_t new_end = time(NULL) - 86400 * 10;
  739. newc = tor_x509_cert_replace_expiration(idc, new_end, d->key2);
  740. certs_cell_cert_setlen_body(cert, newc->encoded_len);
  741. memcpy(certs_cell_cert_getarray_body(cert),
  742. newc->encoded, newc->encoded_len);
  743. REENCODE();
  744. tor_x509_cert_free(newc);
  745. })
  746. CERTS_FAIL(expired_ed_id, /* ed25519 */
  747. {
  748. /* we're going to replace the Ed Id->sign cert with an expired one. */
  749. require_failure_message = "At least one certificate expired";
  750. /* We don't need to re-sign, since we check for expiration first. */
  751. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 2);
  752. uint8_t *body = certs_cell_cert_getarray_body(cert);
  753. /* The expiration field is bytes [2..5]. It is in HOURS since the
  754. * epoch. */
  755. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  756. REENCODE();
  757. })
  758. CERTS_FAIL(expired_ed_link, /* ed25519 */
  759. {
  760. /* we're going to replace the Ed Sign->link cert with an expired one. */
  761. require_failure_message = "At least one certificate expired";
  762. /* We don't need to re-sign, since we check for expiration first. */
  763. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 3);
  764. uint8_t *body = certs_cell_cert_getarray_body(cert);
  765. /* The expiration field is bytes [2..5]. It is in HOURS since the
  766. * epoch. */
  767. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  768. REENCODE();
  769. })
  770. CERTS_FAIL(expired_crosscert, /* ed25519 */
  771. {
  772. /* we're going to replace the Ed Sign->link cert with an expired one. */
  773. require_failure_message = "Crosscert is expired";
  774. /* We don't need to re-sign, since we check for expiration first. */
  775. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 4);
  776. uint8_t *body = certs_cell_cert_getarray_body(cert);
  777. /* The expiration field is bytes [32..35]. once again, HOURS. */
  778. set_uint32(body+32, htonl(24)); /* Back to jan 2, 1970. */
  779. REENCODE();
  780. })
  781. CERTS_FAIL(wrong_labels_1,
  782. {
  783. require_failure_message = "The link certificate was not valid";
  784. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  785. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  786. REENCODE();
  787. })
  788. CERTS_FAIL(wrong_labels_2,
  789. {
  790. const tor_x509_cert_t *a;
  791. const tor_x509_cert_t *b;
  792. const uint8_t *enca;
  793. size_t lena;
  794. require_failure_message = "The link certificate was not valid";
  795. tor_tls_get_my_certs(1, &a, &b);
  796. tor_x509_cert_get_der(a, &enca, &lena);
  797. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  798. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  799. enca, lena);
  800. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  801. REENCODE();
  802. })
  803. CERTS_FAIL(wrong_labels_3,
  804. {
  805. require_failure_message =
  806. "The certs we wanted (ID, Link) were missing";
  807. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  808. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  809. REENCODE();
  810. })
  811. CERTS_FAIL(server_missing_certs,
  812. {
  813. require_failure_message =
  814. "The certs we wanted (ID, Auth) were missing";
  815. d->c->handshake_state->started_here = 0;
  816. d->c->handshake_state->certs->started_here = 0;
  817. })
  818. CERTS_FAIL(server_wrong_labels_1,
  819. {
  820. require_failure_message =
  821. "The authentication certificate was not valid";
  822. d->c->handshake_state->started_here = 0;
  823. d->c->handshake_state->certs->started_here = 0;
  824. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  825. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  826. REENCODE();
  827. })
  828. static void
  829. test_link_handshake_send_authchallenge(void *arg)
  830. {
  831. (void)arg;
  832. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  833. var_cell_t *cell1=NULL, *cell2=NULL;
  834. crypto_pk_t *rsa0 = pk_generate(0), *rsa1 = pk_generate(1);
  835. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  836. rsa0, rsa1, 86400), OP_EQ, 0);
  837. init_mock_ed_keys(rsa0);
  838. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  839. tt_int_op(connection_init_or_handshake_state(c1, 0), OP_EQ, 0);
  840. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  841. tt_ptr_op(mock_got_var_cell, OP_EQ, NULL);
  842. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  843. cell1 = mock_got_var_cell;
  844. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  845. cell2 = mock_got_var_cell;
  846. tt_int_op(38, OP_EQ, cell1->payload_len);
  847. tt_int_op(38, OP_EQ, cell2->payload_len);
  848. tt_int_op(0, OP_EQ, cell1->circ_id);
  849. tt_int_op(0, OP_EQ, cell2->circ_id);
  850. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell1->command);
  851. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell2->command);
  852. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell1->payload + 32, 6);
  853. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell2->payload + 32, 6);
  854. tt_mem_op(cell1->payload, OP_NE, cell2->payload, 32);
  855. done:
  856. UNMOCK(connection_or_write_var_cell_to_buf);
  857. connection_free_minimal(TO_CONN(c1));
  858. tor_free(cell1);
  859. tor_free(cell2);
  860. crypto_pk_free(rsa0);
  861. crypto_pk_free(rsa1);
  862. }
  863. typedef struct authchallenge_data_s {
  864. or_connection_t *c;
  865. channel_tls_t *chan;
  866. var_cell_t *cell;
  867. } authchallenge_data_t;
  868. static int
  869. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  870. {
  871. (void)test;
  872. authchallenge_data_t *d = obj;
  873. UNMOCK(connection_or_send_netinfo);
  874. UNMOCK(connection_or_close_for_error);
  875. UNMOCK(connection_or_send_authenticate_cell);
  876. if (d) {
  877. tor_free(d->cell);
  878. connection_free_minimal(TO_CONN(d->c));
  879. circuitmux_free(d->chan->base_.cmux);
  880. tor_free(d->chan);
  881. tor_free(d);
  882. }
  883. return 1;
  884. }
  885. static void *
  886. recv_authchallenge_setup(const struct testcase_t *test)
  887. {
  888. (void)test;
  889. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  890. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  891. d->chan = tor_malloc_zero(sizeof(*d->chan));
  892. d->c->chan = d->chan;
  893. d->c->base_.address = tor_strdup("HaveAnAddress");
  894. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  895. d->chan->conn = d->c;
  896. tt_int_op(connection_init_or_handshake_state(d->c, 1), OP_EQ, 0);
  897. d->c->link_proto = 4;
  898. d->c->handshake_state->received_certs_cell = 1;
  899. d->cell = var_cell_new(128);
  900. d->cell->payload_len = 38;
  901. d->cell->payload[33] = 2; /* 2 methods */
  902. d->cell->payload[35] = 7; /* This one isn't real */
  903. d->cell->payload[37] = 1; /* This is the old RSA one. */
  904. d->cell->command = CELL_AUTH_CHALLENGE;
  905. get_options_mutable()->ORPort_set = 1;
  906. MOCK(connection_or_close_for_error, mock_close_for_err);
  907. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  908. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  909. tt_int_op(0, OP_EQ, d->c->handshake_state->received_auth_challenge);
  910. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  911. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  912. return d;
  913. done:
  914. recv_authchallenge_cleanup(test, d);
  915. return NULL;
  916. }
  917. static struct testcase_setup_t setup_recv_authchallenge = {
  918. .setup_fn = recv_authchallenge_setup,
  919. .cleanup_fn = recv_authchallenge_cleanup
  920. };
  921. static void
  922. test_link_handshake_recv_authchallenge_ok(void *arg)
  923. {
  924. authchallenge_data_t *d = arg;
  925. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  926. tt_int_op(0, OP_EQ, mock_close_called);
  927. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  928. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  929. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  930. tt_int_op(1, OP_EQ, mock_send_authenticate_called_with_type); /* RSA */
  931. done:
  932. ;
  933. }
  934. static void
  935. test_link_handshake_recv_authchallenge_ok_ed25519(void *arg)
  936. {
  937. authchallenge_data_t *d = arg;
  938. /* Add the ed25519 authentication mechanism here. */
  939. d->cell->payload[33] = 3; /* 3 types are supported now. */
  940. d->cell->payload[39] = 3;
  941. d->cell->payload_len += 2;
  942. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  943. tt_int_op(0, OP_EQ, mock_close_called);
  944. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  945. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  946. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  947. tt_int_op(3, OP_EQ, mock_send_authenticate_called_with_type); /* Ed25519 */
  948. done:
  949. ;
  950. }
  951. static void
  952. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  953. {
  954. authchallenge_data_t *d = arg;
  955. get_options_mutable()->ORPort_set = 0;
  956. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  957. tt_int_op(0, OP_EQ, mock_close_called);
  958. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  959. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  960. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  961. done:
  962. ;
  963. }
  964. static void
  965. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  966. {
  967. authchallenge_data_t *d = arg;
  968. d->cell->payload[37] = 99;
  969. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  970. tt_int_op(0, OP_EQ, mock_close_called);
  971. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  972. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  973. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  974. done:
  975. ;
  976. }
  977. #define AUTHCHALLENGE_FAIL(name, code) \
  978. static void \
  979. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  980. { \
  981. authchallenge_data_t *d = arg; \
  982. const char *require_failure_message = NULL; \
  983. setup_capture_of_logs(LOG_INFO); \
  984. { code ; } \
  985. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  986. tt_int_op(1, OP_EQ, mock_close_called); \
  987. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  988. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  989. if (require_failure_message) { \
  990. expect_log_msg_containing(require_failure_message); \
  991. } \
  992. done: \
  993. teardown_capture_of_logs(); \
  994. }
  995. AUTHCHALLENGE_FAIL(badstate,
  996. require_failure_message = "We're not currently doing a "
  997. "v3 handshake";
  998. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  999. AUTHCHALLENGE_FAIL(badproto,
  1000. require_failure_message = "not using link protocol >= 3";
  1001. d->c->link_proto = 2)
  1002. AUTHCHALLENGE_FAIL(as_server,
  1003. require_failure_message = "We didn't originate this "
  1004. "connection";
  1005. d->c->handshake_state->started_here = 0;
  1006. d->c->handshake_state->certs->started_here = 0;)
  1007. AUTHCHALLENGE_FAIL(duplicate,
  1008. require_failure_message = "We already received one";
  1009. d->c->handshake_state->received_auth_challenge = 1)
  1010. AUTHCHALLENGE_FAIL(nocerts,
  1011. require_failure_message = "We haven't gotten a CERTS "
  1012. "cell yet";
  1013. d->c->handshake_state->received_certs_cell = 0)
  1014. AUTHCHALLENGE_FAIL(tooshort,
  1015. require_failure_message = "It was not well-formed";
  1016. d->cell->payload_len = 33)
  1017. AUTHCHALLENGE_FAIL(truncated,
  1018. require_failure_message = "It was not well-formed";
  1019. d->cell->payload_len = 34)
  1020. AUTHCHALLENGE_FAIL(nonzero_circid,
  1021. require_failure_message = "It had a nonzero circuit ID";
  1022. d->cell->circ_id = 1337)
  1023. static int
  1024. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  1025. {
  1026. (void)tls;
  1027. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  1028. return 0;
  1029. }
  1030. static void
  1031. mock_set_circid_type(channel_t *chan,
  1032. crypto_pk_t *identity_rcvd,
  1033. int consider_identity)
  1034. {
  1035. (void) chan;
  1036. (void) identity_rcvd;
  1037. (void) consider_identity;
  1038. }
  1039. typedef struct authenticate_data_s {
  1040. int is_ed;
  1041. or_connection_t *c1, *c2;
  1042. channel_tls_t *chan2;
  1043. var_cell_t *cell;
  1044. crypto_pk_t *key1, *key2;
  1045. } authenticate_data_t;
  1046. static int
  1047. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  1048. {
  1049. (void) test;
  1050. UNMOCK(connection_or_write_var_cell_to_buf);
  1051. UNMOCK(tor_tls_get_peer_cert);
  1052. UNMOCK(tor_tls_get_own_cert);
  1053. UNMOCK(tor_tls_get_tlssecrets);
  1054. UNMOCK(connection_or_close_for_error);
  1055. UNMOCK(channel_set_circid_type);
  1056. UNMOCK(tor_tls_export_key_material);
  1057. authenticate_data_t *d = arg;
  1058. if (d) {
  1059. tor_free(d->cell);
  1060. connection_or_clear_identity(d->c1);
  1061. connection_or_clear_identity(d->c2);
  1062. connection_free_minimal(TO_CONN(d->c1));
  1063. connection_free_minimal(TO_CONN(d->c2));
  1064. circuitmux_free(d->chan2->base_.cmux);
  1065. tor_free(d->chan2);
  1066. crypto_pk_free(d->key1);
  1067. crypto_pk_free(d->key2);
  1068. tor_free(d);
  1069. }
  1070. tor_x509_cert_free(mock_peer_cert);
  1071. tor_x509_cert_free(mock_own_cert);
  1072. mock_peer_cert = NULL;
  1073. mock_own_cert = NULL;
  1074. return 1;
  1075. }
  1076. static void *
  1077. authenticate_data_setup(const struct testcase_t *test)
  1078. {
  1079. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  1080. int is_ed = d->is_ed = (test->setup_data == (void*)3);
  1081. scheduler_init();
  1082. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  1083. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  1084. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  1085. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  1086. MOCK(connection_or_close_for_error, mock_close_for_err);
  1087. MOCK(channel_set_circid_type, mock_set_circid_type);
  1088. MOCK(tor_tls_export_key_material, mock_export_key_material);
  1089. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1090. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1091. tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
  1092. tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
  1093. d->key1 = pk_generate(2);
  1094. d->key2 = pk_generate(3);
  1095. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  1096. d->key1, d->key2, 86400), OP_EQ, 0);
  1097. init_mock_ed_keys(d->key2);
  1098. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1099. d->c1->link_proto = 3;
  1100. tt_int_op(connection_init_or_handshake_state(d->c1, 1), OP_EQ, 0);
  1101. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1102. d->c2->link_proto = 3;
  1103. tt_int_op(connection_init_or_handshake_state(d->c2, 0), OP_EQ, 0);
  1104. var_cell_t *cell = var_cell_new(16);
  1105. cell->command = CELL_CERTS;
  1106. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  1107. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  1108. memset(cell->payload, 0xf0, 16);
  1109. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  1110. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  1111. tor_free(cell);
  1112. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  1113. channel_tls_common_init(d->chan2);
  1114. d->c2->chan = d->chan2;
  1115. d->chan2->conn = d->c2;
  1116. d->c2->base_.address = tor_strdup("C2");
  1117. d->c2->tls = tor_tls_new(-1, 1);
  1118. d->c2->handshake_state->received_certs_cell = 1;
  1119. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  1120. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  1121. const uint8_t *der;
  1122. size_t sz;
  1123. tor_x509_cert_get_der(id_cert, &der, &sz);
  1124. d->c1->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1125. d->c2->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1126. if (is_ed) {
  1127. d->c1->handshake_state->certs->ed_id_sign =
  1128. tor_cert_dup(get_master_signing_key_cert());
  1129. d->c2->handshake_state->certs->ed_id_sign =
  1130. tor_cert_dup(get_master_signing_key_cert());
  1131. d->c2->handshake_state->certs->ed_sign_auth =
  1132. tor_cert_dup(get_current_auth_key_cert());
  1133. } else {
  1134. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  1135. tor_x509_cert_get_der(auth_cert, &der, &sz);
  1136. d->c2->handshake_state->certs->auth_cert = tor_x509_cert_decode(der, sz);
  1137. }
  1138. tor_x509_cert_get_der(link_cert, &der, &sz);
  1139. mock_peer_cert = tor_x509_cert_decode(der, sz);
  1140. tt_assert(mock_peer_cert);
  1141. mock_own_cert = tor_x509_cert_decode(der, sz);
  1142. tt_assert(mock_own_cert);
  1143. /* Make an authenticate cell ... */
  1144. int authtype;
  1145. if (is_ed)
  1146. authtype = AUTHTYPE_ED25519_SHA256_RFC5705;
  1147. else
  1148. authtype = AUTHTYPE_RSA_SHA256_TLSSECRET;
  1149. tt_int_op(0, OP_EQ, connection_or_send_authenticate_cell(d->c1, authtype));
  1150. tt_assert(mock_got_var_cell);
  1151. d->cell = mock_got_var_cell;
  1152. mock_got_var_cell = NULL;
  1153. return d;
  1154. done:
  1155. authenticate_data_cleanup(test, d);
  1156. return NULL;
  1157. }
  1158. static struct testcase_setup_t setup_authenticate = {
  1159. .setup_fn = authenticate_data_setup,
  1160. .cleanup_fn = authenticate_data_cleanup
  1161. };
  1162. static void
  1163. test_link_handshake_auth_cell(void *arg)
  1164. {
  1165. authenticate_data_t *d = arg;
  1166. auth1_t *auth1 = NULL;
  1167. crypto_pk_t *auth_pubkey = NULL;
  1168. /* Is the cell well-formed on the outer layer? */
  1169. tt_int_op(d->cell->command, OP_EQ, CELL_AUTHENTICATE);
  1170. tt_int_op(d->cell->payload[0], OP_EQ, 0);
  1171. if (d->is_ed)
  1172. tt_int_op(d->cell->payload[1], OP_EQ, 3);
  1173. else
  1174. tt_int_op(d->cell->payload[1], OP_EQ, 1);
  1175. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), OP_EQ,
  1176. d->cell->payload_len - 4);
  1177. /* Check it out for plausibility... */
  1178. auth_ctx_t ctx;
  1179. ctx.is_ed = d->is_ed;
  1180. tt_int_op(d->cell->payload_len-4, OP_EQ, auth1_parse(&auth1,
  1181. d->cell->payload+4,
  1182. d->cell->payload_len - 4, &ctx));
  1183. tt_assert(auth1);
  1184. if (d->is_ed) {
  1185. tt_mem_op(auth1->type, OP_EQ, "AUTH0003", 8);
  1186. } else {
  1187. tt_mem_op(auth1->type, OP_EQ, "AUTH0001", 8);
  1188. }
  1189. tt_mem_op(auth1->tlssecrets, OP_EQ, "int getRandomNumber(){return 4;}", 32);
  1190. /* Is the signature okay? */
  1191. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  1192. if (d->is_ed) {
  1193. ed25519_signature_t sig;
  1194. tt_int_op(auth1_getlen_sig(auth1), OP_EQ, ED25519_SIG_LEN);
  1195. memcpy(&sig.sig, auth1_getarray_sig(auth1), ED25519_SIG_LEN);
  1196. tt_assert(!ed25519_checksig(&sig, start, end-start,
  1197. &get_current_auth_keypair()->pubkey));
  1198. } else {
  1199. uint8_t sig[128];
  1200. uint8_t digest[32];
  1201. tt_int_op(auth1_getlen_sig(auth1), OP_GT, 120);
  1202. auth_pubkey = tor_tls_cert_get_key(
  1203. d->c2->handshake_state->certs->auth_cert);
  1204. int n = crypto_pk_public_checksig(
  1205. auth_pubkey,
  1206. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  1207. auth1_getlen_sig(auth1));
  1208. tt_int_op(n, OP_EQ, 32);
  1209. crypto_digest256((char*)digest,
  1210. (const char*)start, end-start, DIGEST_SHA256);
  1211. tt_mem_op(sig, OP_EQ, digest, 32);
  1212. }
  1213. /* Then feed it to c2. */
  1214. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0);
  1215. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1216. tt_int_op(mock_close_called, OP_EQ, 0);
  1217. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1218. if (d->is_ed) {
  1219. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 1);
  1220. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1221. } else {
  1222. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 0);
  1223. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1224. }
  1225. done:
  1226. auth1_free(auth1);
  1227. crypto_pk_free(auth_pubkey);
  1228. }
  1229. #define AUTHENTICATE_FAIL(name, code) \
  1230. static void \
  1231. test_link_handshake_auth_ ## name(void *arg) \
  1232. { \
  1233. authenticate_data_t *d = arg; \
  1234. const char *require_failure_message = NULL; \
  1235. setup_capture_of_logs(LOG_INFO); \
  1236. { code ; } \
  1237. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  1238. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  1239. tt_int_op(mock_close_called, OP_EQ, 1); \
  1240. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  1241. if (require_failure_message) { \
  1242. expect_log_msg_containing(require_failure_message); \
  1243. } \
  1244. done: \
  1245. teardown_capture_of_logs(); \
  1246. }
  1247. AUTHENTICATE_FAIL(badstate,
  1248. require_failure_message = "We're not doing a v3 handshake";
  1249. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  1250. AUTHENTICATE_FAIL(badproto,
  1251. require_failure_message = "not using link protocol >= 3";
  1252. d->c2->link_proto = 2)
  1253. AUTHENTICATE_FAIL(atclient,
  1254. require_failure_message = "We originated this connection";
  1255. d->c2->handshake_state->started_here = 1;
  1256. d->c2->handshake_state->certs->started_here = 1;)
  1257. AUTHENTICATE_FAIL(duplicate,
  1258. require_failure_message = "We already got one";
  1259. d->c2->handshake_state->received_authenticate = 1)
  1260. static void
  1261. test_link_handshake_auth_already_authenticated(void *arg)
  1262. {
  1263. authenticate_data_t *d = arg;
  1264. setup_capture_of_logs(LOG_INFO);
  1265. d->c2->handshake_state->authenticated = 1;
  1266. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1267. tt_int_op(mock_close_called, OP_EQ, 1);
  1268. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1269. expect_log_msg_containing("The peer is already authenticated");
  1270. done:
  1271. teardown_capture_of_logs();
  1272. }
  1273. AUTHENTICATE_FAIL(nocerts,
  1274. require_failure_message = "We never got a certs cell";
  1275. d->c2->handshake_state->received_certs_cell = 0)
  1276. AUTHENTICATE_FAIL(noidcert,
  1277. require_failure_message = "We never got an identity "
  1278. "certificate";
  1279. tor_x509_cert_free(d->c2->handshake_state->certs->id_cert);
  1280. d->c2->handshake_state->certs->id_cert = NULL)
  1281. AUTHENTICATE_FAIL(noauthcert,
  1282. require_failure_message = "We never got an RSA "
  1283. "authentication certificate";
  1284. tor_x509_cert_free(d->c2->handshake_state->certs->auth_cert);
  1285. d->c2->handshake_state->certs->auth_cert = NULL)
  1286. AUTHENTICATE_FAIL(tooshort,
  1287. require_failure_message = "Cell was way too short";
  1288. d->cell->payload_len = 3)
  1289. AUTHENTICATE_FAIL(badtype,
  1290. require_failure_message = "Authenticator type was not "
  1291. "recognized";
  1292. d->cell->payload[0] = 0xff)
  1293. AUTHENTICATE_FAIL(truncated_1,
  1294. require_failure_message = "Authenticator was truncated";
  1295. d->cell->payload[2]++)
  1296. AUTHENTICATE_FAIL(truncated_2,
  1297. require_failure_message = "Authenticator was truncated";
  1298. d->cell->payload[3]++)
  1299. AUTHENTICATE_FAIL(tooshort_1,
  1300. require_failure_message = "Authenticator was too short";
  1301. tt_int_op(d->cell->payload_len, OP_GE, 260);
  1302. d->cell->payload[2] -= 1;
  1303. d->cell->payload_len -= 256;)
  1304. AUTHENTICATE_FAIL(badcontent,
  1305. require_failure_message = "Some field in the AUTHENTICATE "
  1306. "cell body was not as expected";
  1307. d->cell->payload[10] ^= 0xff)
  1308. AUTHENTICATE_FAIL(badsig_1,
  1309. if (d->is_ed)
  1310. require_failure_message = "Ed25519 signature wasn't valid";
  1311. else
  1312. require_failure_message = "RSA signature wasn't valid";
  1313. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  1314. AUTHENTICATE_FAIL(missing_ed_id,
  1315. {
  1316. tor_cert_free(d->c2->handshake_state->certs->ed_id_sign);
  1317. d->c2->handshake_state->certs->ed_id_sign = NULL;
  1318. require_failure_message = "Ed authenticate without Ed ID "
  1319. "cert from peer";
  1320. })
  1321. AUTHENTICATE_FAIL(missing_ed_auth,
  1322. {
  1323. tor_cert_free(d->c2->handshake_state->certs->ed_sign_auth);
  1324. d->c2->handshake_state->certs->ed_sign_auth = NULL;
  1325. require_failure_message = "We never got an Ed25519 "
  1326. "authentication certificate";
  1327. })
  1328. #define TEST_RSA(name, flags) \
  1329. { #name , test_link_handshake_ ## name, (flags), \
  1330. &passthrough_setup, (void*)"RSA" }
  1331. #define TEST_ED(name, flags) \
  1332. { #name "_ed25519" , test_link_handshake_ ## name, (flags), \
  1333. &passthrough_setup, (void*)"Ed25519" }
  1334. #define TEST_RCV_AUTHCHALLENGE(name) \
  1335. { "recv_authchallenge/" #name , \
  1336. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  1337. &setup_recv_authchallenge, NULL }
  1338. #define TEST_RCV_CERTS(name) \
  1339. { "recv_certs/" #name , \
  1340. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1341. &setup_recv_certs, (void*)"RSA-Link" }
  1342. #define TEST_RCV_CERTS_RSA(name,type) \
  1343. { "recv_certs/" #name , \
  1344. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1345. &setup_recv_certs, (void*)type }
  1346. #define TEST_RCV_CERTS_ED(name, type) \
  1347. { "recv_certs/" #name "_ed25519", \
  1348. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1349. &setup_recv_certs, (void*)type }
  1350. #define TEST_AUTHENTICATE(name) \
  1351. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  1352. &setup_authenticate, NULL }
  1353. #define TEST_AUTHENTICATE_ED(name) \
  1354. { "authenticate/" #name "_ed25519" , test_link_handshake_auth_ ## name, \
  1355. TT_FORK, &setup_authenticate, (void*)3 }
  1356. struct testcase_t link_handshake_tests[] = {
  1357. TEST_RSA(certs_ok, TT_FORK),
  1358. TEST_ED(certs_ok, TT_FORK),
  1359. TEST_RCV_CERTS(ok),
  1360. TEST_RCV_CERTS_ED(ok, "Ed25519-Link"),
  1361. TEST_RCV_CERTS_RSA(ok_server, "RSA-Auth"),
  1362. TEST_RCV_CERTS_ED(ok_server, "Ed25519-Auth"),
  1363. TEST_RCV_CERTS(badstate),
  1364. TEST_RCV_CERTS(badproto),
  1365. TEST_RCV_CERTS(duplicate),
  1366. TEST_RCV_CERTS(already_authenticated),
  1367. TEST_RCV_CERTS(empty),
  1368. TEST_RCV_CERTS(bad_circid),
  1369. TEST_RCV_CERTS(truncated_1),
  1370. TEST_RCV_CERTS(truncated_2),
  1371. TEST_RCV_CERTS(truncated_3),
  1372. TEST_RCV_CERTS_ED(truncated_4, "Ed25519-Link"),
  1373. TEST_RCV_CERTS_ED(truncated_5, "Ed25519-Link"),
  1374. TEST_RCV_CERTS_ED(truncated_6, "Ed25519-Link"),
  1375. TEST_RCV_CERTS_ED(truncated_7, "Ed25519-Link"),
  1376. TEST_RCV_CERTS(not_x509),
  1377. TEST_RCV_CERTS(both_link),
  1378. TEST_RCV_CERTS(both_id_rsa),
  1379. TEST_RCV_CERTS(both_auth),
  1380. TEST_RCV_CERTS_ED(duplicate_id, "Ed25519-Link"),
  1381. TEST_RCV_CERTS_ED(duplicate_link, "Ed25519-Link"),
  1382. TEST_RCV_CERTS_ED(duplicate_crosscert, "Ed25519-Link"),
  1383. TEST_RCV_CERTS_ED(missing_crosscert, "Ed25519-Link"),
  1384. TEST_RCV_CERTS_ED(missing_id, "Ed25519-Link"),
  1385. TEST_RCV_CERTS_ED(missing_signing_key, "Ed25519-Link"),
  1386. TEST_RCV_CERTS_ED(missing_link, "Ed25519-Link"),
  1387. TEST_RCV_CERTS_ED(missing_auth, "Ed25519-Auth"),
  1388. TEST_RCV_CERTS_ED(missing_rsa_id, "Ed25519-Link"),
  1389. TEST_RCV_CERTS_ED(link_mismatch, "Ed25519-Link"),
  1390. TEST_RCV_CERTS_ED(bad_ed_sig, "Ed25519-Link"),
  1391. TEST_RCV_CERTS_ED(bad_rsa_id_cert, "Ed25519-Link"),
  1392. TEST_RCV_CERTS_ED(bad_crosscert, "Ed25519-Link"),
  1393. TEST_RCV_CERTS_RSA(expired_rsa_id, "RSA-Link"),
  1394. TEST_RCV_CERTS_ED(expired_rsa_id, "Ed25519-Link"),
  1395. TEST_RCV_CERTS_ED(expired_ed_id, "Ed25519-Link"),
  1396. TEST_RCV_CERTS_ED(expired_ed_link, "Ed25519-Link"),
  1397. TEST_RCV_CERTS_ED(expired_crosscert, "Ed25519-Link"),
  1398. TEST_RCV_CERTS(wrong_labels_1),
  1399. TEST_RCV_CERTS(wrong_labels_2),
  1400. TEST_RCV_CERTS(wrong_labels_3),
  1401. TEST_RCV_CERTS(server_missing_certs),
  1402. TEST_RCV_CERTS(server_wrong_labels_1),
  1403. TEST_RSA(send_authchallenge, TT_FORK),
  1404. TEST_RCV_AUTHCHALLENGE(ok),
  1405. TEST_RCV_AUTHCHALLENGE(ok_ed25519),
  1406. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  1407. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  1408. TEST_RCV_AUTHCHALLENGE(badstate),
  1409. TEST_RCV_AUTHCHALLENGE(badproto),
  1410. TEST_RCV_AUTHCHALLENGE(as_server),
  1411. TEST_RCV_AUTHCHALLENGE(duplicate),
  1412. TEST_RCV_AUTHCHALLENGE(nocerts),
  1413. TEST_RCV_AUTHCHALLENGE(tooshort),
  1414. TEST_RCV_AUTHCHALLENGE(truncated),
  1415. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  1416. TEST_AUTHENTICATE(cell),
  1417. TEST_AUTHENTICATE_ED(cell),
  1418. TEST_AUTHENTICATE(badstate),
  1419. TEST_AUTHENTICATE(badproto),
  1420. TEST_AUTHENTICATE(atclient),
  1421. TEST_AUTHENTICATE(duplicate),
  1422. TEST_AUTHENTICATE(already_authenticated),
  1423. TEST_AUTHENTICATE(nocerts),
  1424. TEST_AUTHENTICATE(noidcert),
  1425. TEST_AUTHENTICATE(noauthcert),
  1426. TEST_AUTHENTICATE(tooshort),
  1427. TEST_AUTHENTICATE(badtype),
  1428. TEST_AUTHENTICATE(truncated_1),
  1429. TEST_AUTHENTICATE(truncated_2),
  1430. TEST_AUTHENTICATE(tooshort_1),
  1431. TEST_AUTHENTICATE(badcontent),
  1432. TEST_AUTHENTICATE(badsig_1),
  1433. TEST_AUTHENTICATE_ED(badsig_1),
  1434. TEST_AUTHENTICATE_ED(missing_ed_id),
  1435. TEST_AUTHENTICATE_ED(missing_ed_auth),
  1436. //TEST_AUTHENTICATE(),
  1437. END_OF_TESTCASES
  1438. };