test_link_handshake.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /* Copyright (c) 2014, 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. #include "or.h"
  8. #include "config.h"
  9. #include "connection.h"
  10. #include "connection_or.h"
  11. #include "channeltls.h"
  12. #include "link_handshake.h"
  13. #include "test.h"
  14. var_cell_t *mock_got_var_cell = NULL;
  15. static void
  16. mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
  17. {
  18. (void)conn;
  19. var_cell_t *newcell = var_cell_new(vc->payload_len);
  20. memcpy(newcell, vc, sizeof(var_cell_t));
  21. memcpy(newcell->payload, vc->payload, vc->payload_len);
  22. mock_got_var_cell = newcell;
  23. }
  24. static int
  25. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  26. {
  27. (void) tls;
  28. (void) cert; // XXXX look at this.
  29. return 1;
  30. }
  31. static int mock_send_netinfo_called = 0;
  32. static int
  33. mock_send_netinfo(or_connection_t *conn)
  34. {
  35. (void) conn;
  36. ++mock_send_netinfo_called;// XXX check_this
  37. return 0;
  38. }
  39. static int mock_close_called = 0;
  40. static void
  41. mock_close_for_err(or_connection_t *orconn, int flush)
  42. {
  43. (void)orconn;
  44. (void)flush;
  45. ++mock_close_called;
  46. }
  47. static int mock_send_authenticate_called = 0;
  48. static int
  49. mock_send_authenticate(or_connection_t *conn, int type)
  50. {
  51. (void) conn;
  52. (void) type;
  53. ++mock_send_authenticate_called;// XXX check_this
  54. return 0;
  55. }
  56. /* Test good certs cells */
  57. static void
  58. test_link_handshake_certs_ok(void *arg)
  59. {
  60. (void) arg;
  61. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  62. or_connection_t *c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  63. var_cell_t *cell1 = NULL, *cell2 = NULL;
  64. certs_cell_t *cc1 = NULL, *cc2 = NULL;
  65. channel_tls_t *chan1 = NULL, *chan2 = NULL;
  66. crypto_pk_t *key1 = NULL, *key2 = NULL;
  67. scheduler_init();
  68. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  69. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  70. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  71. key1 = pk_generate(2);
  72. key2 = pk_generate(3);
  73. /* We need to make sure that our TLS certificates are set up before we can
  74. * actually generate a CERTS cell.
  75. */
  76. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  77. key1, key2, 86400), ==, 0);
  78. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  79. c1->link_proto = 3;
  80. tt_int_op(connection_init_or_handshake_state(c1, 1), ==, 0);
  81. c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  82. c2->link_proto = 3;
  83. tt_int_op(connection_init_or_handshake_state(c2, 0), ==, 0);
  84. tt_int_op(0, ==, connection_or_send_certs_cell(c1));
  85. tt_assert(mock_got_var_cell);
  86. cell1 = mock_got_var_cell;
  87. tt_int_op(0, ==, connection_or_send_certs_cell(c2));
  88. tt_assert(mock_got_var_cell);
  89. cell2 = mock_got_var_cell;
  90. tt_int_op(cell1->command, ==, CELL_CERTS);
  91. tt_int_op(cell1->payload_len, >, 1);
  92. tt_int_op(cell2->command, ==, CELL_CERTS);
  93. tt_int_op(cell2->payload_len, >, 1);
  94. tt_int_op(cell1->payload_len, ==,
  95. certs_cell_parse(&cc1, cell1->payload, cell1->payload_len));
  96. tt_int_op(cell2->payload_len, ==,
  97. certs_cell_parse(&cc2, cell2->payload, cell2->payload_len));
  98. tt_int_op(2, ==, cc1->n_certs);
  99. tt_int_op(2, ==, cc2->n_certs);
  100. tt_int_op(certs_cell_get_certs(cc1, 0)->cert_type, ==,
  101. CERTTYPE_RSA1024_ID_AUTH);
  102. tt_int_op(certs_cell_get_certs(cc1, 1)->cert_type, ==,
  103. CERTTYPE_RSA1024_ID_ID);
  104. tt_int_op(certs_cell_get_certs(cc2, 0)->cert_type, ==,
  105. CERTTYPE_RSA1024_ID_LINK);
  106. tt_int_op(certs_cell_get_certs(cc2, 1)->cert_type, ==,
  107. CERTTYPE_RSA1024_ID_ID);
  108. chan1 = tor_malloc_zero(sizeof(*chan1));
  109. channel_tls_common_init(chan1);
  110. c1->chan = chan1;
  111. chan1->conn = c1;
  112. c1->base_.address = tor_strdup("C1");
  113. c1->tls = tor_tls_new(-1, 0);
  114. c1->link_proto = 4;
  115. c1->base_.conn_array_index = -1;
  116. crypto_pk_get_digest(key2, c1->identity_digest);
  117. channel_tls_process_certs_cell(cell2, chan1);
  118. tt_assert(c1->handshake_state->received_certs_cell);
  119. tt_assert(c1->handshake_state->auth_cert == NULL);
  120. tt_assert(c1->handshake_state->id_cert);
  121. tt_assert(! tor_mem_is_zero(
  122. (char*)c1->handshake_state->authenticated_peer_id, 20));
  123. chan2 = tor_malloc_zero(sizeof(*chan2));
  124. channel_tls_common_init(chan2);
  125. c2->chan = chan2;
  126. chan2->conn = c2;
  127. c2->base_.address = tor_strdup("C2");
  128. c2->tls = tor_tls_new(-1, 1);
  129. c2->link_proto = 4;
  130. c2->base_.conn_array_index = -1;
  131. crypto_pk_get_digest(key1, c2->identity_digest);
  132. channel_tls_process_certs_cell(cell1, chan2);
  133. tt_assert(c2->handshake_state->received_certs_cell);
  134. tt_assert(c2->handshake_state->auth_cert);
  135. tt_assert(c2->handshake_state->id_cert);
  136. tt_assert(tor_mem_is_zero(
  137. (char*)c2->handshake_state->authenticated_peer_id, 20));
  138. done:
  139. UNMOCK(tor_tls_cert_matches_key);
  140. UNMOCK(connection_or_write_var_cell_to_buf);
  141. UNMOCK(connection_or_send_netinfo);
  142. connection_free_(TO_CONN(c1));
  143. connection_free_(TO_CONN(c2));
  144. tor_free(cell1);
  145. tor_free(cell2);
  146. certs_cell_free(cc1);
  147. certs_cell_free(cc2);
  148. tor_free(chan1);
  149. tor_free(chan2);
  150. }
  151. typedef struct certs_data_s {
  152. or_connection_t *c;
  153. channel_tls_t *chan;
  154. certs_cell_t *ccell;
  155. var_cell_t *cell;
  156. } certs_data_t;
  157. static int
  158. recv_certs_cleanup(const struct testcase_t *test, void *obj)
  159. {
  160. (void)test;
  161. certs_data_t *d = obj;
  162. UNMOCK(tor_tls_cert_matches_key);
  163. UNMOCK(connection_or_send_netinfo);
  164. UNMOCK(connection_or_close_for_error);
  165. if (d) {
  166. tor_free(d->cell);
  167. certs_cell_free(d->ccell);
  168. connection_free_(TO_CONN(d->c));
  169. tor_free(d->chan);
  170. tor_free(d);
  171. }
  172. return 1;
  173. }
  174. static void *
  175. recv_certs_setup(const struct testcase_t *test)
  176. {
  177. (void)test;
  178. certs_data_t *d = tor_malloc_zero(sizeof(*d));
  179. certs_cell_cert_t *ccc1 = NULL;
  180. certs_cell_cert_t *ccc2 = NULL;
  181. ssize_t n;
  182. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  183. d->chan = tor_malloc_zero(sizeof(*d->chan));
  184. d->c->chan = d->chan;
  185. d->c->base_.address = tor_strdup("HaveAnAddress");
  186. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  187. d->chan->conn = d->c;
  188. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  189. d->c->link_proto = 4;
  190. crypto_pk_t *key1 = NULL, *key2 = NULL;
  191. key1 = pk_generate(2);
  192. key2 = pk_generate(3);
  193. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  194. key1, key2, 86400), ==, 0);
  195. d->ccell = certs_cell_new();
  196. ccc1 = certs_cell_cert_new();
  197. certs_cell_add_certs(d->ccell, ccc1);
  198. ccc2 = certs_cell_cert_new();
  199. certs_cell_add_certs(d->ccell, ccc2);
  200. d->ccell->n_certs = 2;
  201. ccc1->cert_type = 1;
  202. ccc2->cert_type = 2;
  203. const tor_x509_cert_t *a,*b;
  204. const uint8_t *enca, *encb;
  205. size_t lena, lenb;
  206. tor_tls_get_my_certs(1, &a, &b);
  207. tor_x509_cert_get_der(a, &enca, &lena);
  208. tor_x509_cert_get_der(b, &encb, &lenb);
  209. certs_cell_cert_setlen_body(ccc1, lena);
  210. ccc1->cert_len = lena;
  211. certs_cell_cert_setlen_body(ccc2, lenb);
  212. ccc2->cert_len = lenb;
  213. memcpy(certs_cell_cert_getarray_body(ccc1), enca, lena);
  214. memcpy(certs_cell_cert_getarray_body(ccc2), encb, lenb);
  215. d->cell = var_cell_new(4096);
  216. d->cell->command = CELL_CERTS;
  217. n = certs_cell_encode(d->cell->payload, 4096, d->ccell);
  218. tt_int_op(n, >, 0);
  219. d->cell->payload_len = n;
  220. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  221. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  222. MOCK(connection_or_close_for_error, mock_close_for_err);
  223. tt_int_op(0, ==, d->c->handshake_state->received_certs_cell);
  224. tt_int_op(0, ==, mock_send_authenticate_called);
  225. tt_int_op(0, ==, mock_send_netinfo_called);
  226. return d;
  227. done:
  228. recv_certs_cleanup(test, d);
  229. return NULL;
  230. }
  231. static struct testcase_setup_t setup_recv_certs = {
  232. .setup_fn = recv_certs_setup,
  233. .cleanup_fn = recv_certs_cleanup
  234. };
  235. static void
  236. test_link_handshake_recv_certs_ok(void *arg)
  237. {
  238. certs_data_t *d = arg;
  239. channel_tls_process_certs_cell(d->cell, d->chan);
  240. tt_int_op(0, ==, mock_close_called);
  241. tt_int_op(d->c->handshake_state->authenticated, ==, 1);
  242. tt_int_op(d->c->handshake_state->received_certs_cell, ==, 1);
  243. tt_assert(d->c->handshake_state->id_cert != NULL);
  244. tt_assert(d->c->handshake_state->auth_cert == NULL);
  245. done:
  246. ;
  247. }
  248. static void
  249. test_link_handshake_recv_certs_ok_server(void *arg)
  250. {
  251. certs_data_t *d = arg;
  252. d->c->handshake_state->started_here = 0;
  253. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  254. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  255. ssize_t n = certs_cell_encode(d->cell->payload, 2048, d->ccell);
  256. tt_int_op(n, >, 0);
  257. d->cell->payload_len = n;
  258. channel_tls_process_certs_cell(d->cell, d->chan);
  259. tt_int_op(0, ==, mock_close_called);
  260. tt_int_op(d->c->handshake_state->authenticated, ==, 0);
  261. tt_int_op(d->c->handshake_state->received_certs_cell, ==, 1);
  262. tt_assert(d->c->handshake_state->id_cert != NULL);
  263. tt_assert(d->c->handshake_state->auth_cert != NULL);
  264. done:
  265. ;
  266. }
  267. #define CERTS_FAIL(name, code) \
  268. static void \
  269. test_link_handshake_recv_certs_ ## name (void *arg) \
  270. { \
  271. certs_data_t *d = arg; \
  272. { code ; } \
  273. channel_tls_process_certs_cell(d->cell, d->chan); \
  274. tt_int_op(1, ==, mock_close_called); \
  275. tt_int_op(0, ==, mock_send_authenticate_called); \
  276. tt_int_op(0, ==, mock_send_netinfo_called); \
  277. done: \
  278. ; \
  279. }
  280. CERTS_FAIL(badstate, d->c->base_.state = OR_CONN_STATE_CONNECTING)
  281. CERTS_FAIL(badproto, d->c->link_proto = 2)
  282. CERTS_FAIL(duplicate, d->c->handshake_state->received_certs_cell = 1)
  283. CERTS_FAIL(already_authenticated,
  284. d->c->handshake_state->authenticated = 1)
  285. CERTS_FAIL(empty, d->cell->payload_len = 0)
  286. CERTS_FAIL(bad_circid, d->cell->circ_id = 1)
  287. CERTS_FAIL(truncated_1, d->cell->payload[0] = 5)
  288. CERTS_FAIL(truncated_2, {
  289. d->cell->payload_len = 4;
  290. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);})
  291. CERTS_FAIL(truncated_3, {
  292. d->cell->payload_len = 7;
  293. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);})
  294. #define REENCODE() do { \
  295. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  296. tt_int_op(n, >, 0); \
  297. d->cell->payload_len = n; \
  298. } while (0)
  299. CERTS_FAIL(not_x509, {
  300. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  301. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  302. REENCODE();
  303. })
  304. CERTS_FAIL(both_link, {
  305. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  306. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  307. REENCODE();
  308. })
  309. CERTS_FAIL(both_id_rsa, {
  310. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  311. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  312. REENCODE();
  313. })
  314. CERTS_FAIL(both_auth, {
  315. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  316. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  317. REENCODE();
  318. })
  319. CERTS_FAIL(wrong_labels_1, {
  320. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  321. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  322. REENCODE();
  323. })
  324. CERTS_FAIL(wrong_labels_2, {
  325. const tor_x509_cert_t *a;
  326. const tor_x509_cert_t *b;
  327. const uint8_t *enca;
  328. size_t lena;
  329. tor_tls_get_my_certs(1, &a, &b);
  330. tor_x509_cert_get_der(a, &enca, &lena);
  331. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  332. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  333. enca, lena);
  334. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  335. REENCODE();
  336. })
  337. CERTS_FAIL(wrong_labels_3, {
  338. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  339. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  340. REENCODE();
  341. })
  342. CERTS_FAIL(server_missing_certs, {
  343. d->c->handshake_state->started_here = 0;
  344. })
  345. CERTS_FAIL(server_wrong_labels_1, {
  346. d->c->handshake_state->started_here = 0;
  347. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  348. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  349. REENCODE();
  350. })
  351. static void
  352. test_link_handshake_send_authchallenge(void *arg)
  353. {
  354. (void)arg;
  355. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  356. var_cell_t *cell1=NULL, *cell2=NULL;
  357. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  358. tt_int_op(connection_init_or_handshake_state(c1, 0), ==, 0);
  359. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  360. tt_assert(! mock_got_var_cell);
  361. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  362. cell1 = mock_got_var_cell;
  363. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  364. cell2 = mock_got_var_cell;
  365. tt_int_op(36, ==, cell1->payload_len);
  366. tt_int_op(36, ==, cell2->payload_len);
  367. tt_int_op(0, ==, cell1->circ_id);
  368. tt_int_op(0, ==, cell2->circ_id);
  369. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell1->command);
  370. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell2->command);
  371. tt_mem_op("\x00\x01\x00\x01", ==, cell1->payload + 32, 4);
  372. tt_mem_op("\x00\x01\x00\x01", ==, cell2->payload + 32, 4);
  373. tt_mem_op(cell1->payload, !=, cell2->payload, 32);
  374. done:
  375. UNMOCK(connection_or_write_var_cell_to_buf);
  376. connection_free_(TO_CONN(c1));
  377. tor_free(cell1);
  378. tor_free(cell2);
  379. }
  380. typedef struct authchallenge_data_s {
  381. or_connection_t *c;
  382. channel_tls_t *chan;
  383. var_cell_t *cell;
  384. } authchallenge_data_t;
  385. static int
  386. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  387. {
  388. (void)test;
  389. authchallenge_data_t *d = obj;
  390. UNMOCK(connection_or_send_netinfo);
  391. UNMOCK(connection_or_close_for_error);
  392. UNMOCK(connection_or_send_authenticate_cell);
  393. if (d) {
  394. tor_free(d->cell);
  395. connection_free_(TO_CONN(d->c));
  396. tor_free(d->chan);
  397. tor_free(d);
  398. }
  399. return 1;
  400. }
  401. static void *
  402. recv_authchallenge_setup(const struct testcase_t *test)
  403. {
  404. (void)test;
  405. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  406. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  407. d->chan = tor_malloc_zero(sizeof(*d->chan));
  408. d->c->chan = d->chan;
  409. d->c->base_.address = tor_strdup("HaveAnAddress");
  410. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  411. d->chan->conn = d->c;
  412. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  413. d->c->link_proto = 4;
  414. d->c->handshake_state->received_certs_cell = 1;
  415. d->cell = var_cell_new(128);
  416. d->cell->payload_len = 38;
  417. d->cell->payload[33] = 2;
  418. d->cell->payload[35] = 7;
  419. d->cell->payload[37] = 1;
  420. d->cell->command = CELL_AUTH_CHALLENGE;
  421. get_options_mutable()->ORPort_set = 1;
  422. MOCK(connection_or_close_for_error, mock_close_for_err);
  423. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  424. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  425. tt_int_op(0, ==, d->c->handshake_state->received_auth_challenge);
  426. tt_int_op(0, ==, mock_send_authenticate_called);
  427. tt_int_op(0, ==, mock_send_netinfo_called);
  428. return d;
  429. done:
  430. recv_authchallenge_cleanup(test, d);
  431. return NULL;
  432. }
  433. static struct testcase_setup_t setup_recv_authchallenge = {
  434. .setup_fn = recv_authchallenge_setup,
  435. .cleanup_fn = recv_authchallenge_cleanup
  436. };
  437. static void
  438. test_link_handshake_recv_authchallenge_ok(void *arg)
  439. {
  440. authchallenge_data_t *d = arg;
  441. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  442. tt_int_op(0, ==, mock_close_called);
  443. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  444. tt_int_op(1, ==, mock_send_authenticate_called);
  445. tt_int_op(1, ==, mock_send_netinfo_called);
  446. done:
  447. ;
  448. }
  449. static void
  450. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  451. {
  452. authchallenge_data_t *d = arg;
  453. get_options_mutable()->ORPort_set = 0;
  454. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  455. tt_int_op(0, ==, mock_close_called);
  456. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  457. tt_int_op(0, ==, mock_send_authenticate_called);
  458. tt_int_op(0, ==, mock_send_netinfo_called);
  459. done:
  460. ;
  461. }
  462. static void
  463. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  464. {
  465. authchallenge_data_t *d = arg;
  466. d->cell->payload[37] = 99;
  467. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  468. tt_int_op(0, ==, mock_close_called);
  469. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  470. tt_int_op(0, ==, mock_send_authenticate_called);
  471. tt_int_op(1, ==, mock_send_netinfo_called);
  472. done:
  473. ;
  474. }
  475. #define AUTHCHALLENGE_FAIL(name, code) \
  476. static void \
  477. test_link_handshake_recv_authchallenge_ ## name (void *arg) \
  478. { \
  479. authchallenge_data_t *d = arg; \
  480. { code ; } \
  481. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  482. tt_int_op(1, ==, mock_close_called); \
  483. tt_int_op(0, ==, mock_send_authenticate_called); \
  484. tt_int_op(0, ==, mock_send_netinfo_called); \
  485. done: \
  486. ; \
  487. }
  488. AUTHCHALLENGE_FAIL(badstate,
  489. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  490. AUTHCHALLENGE_FAIL(badproto,
  491. d->c->link_proto = 2)
  492. AUTHCHALLENGE_FAIL(as_server,
  493. d->c->handshake_state->started_here = 0;)
  494. AUTHCHALLENGE_FAIL(duplicate,
  495. d->c->handshake_state->received_auth_challenge = 1)
  496. AUTHCHALLENGE_FAIL(nocerts,
  497. d->c->handshake_state->received_certs_cell = 0)
  498. AUTHCHALLENGE_FAIL(tooshort,
  499. d->cell->payload_len = 33)
  500. AUTHCHALLENGE_FAIL(truncated,
  501. d->cell->payload_len = 34)
  502. AUTHCHALLENGE_FAIL(nonzero_circid,
  503. d->cell->circ_id = 1337)
  504. #define TEST(name, flags) \
  505. { #name , test_link_handshake_ ## name, (flags), NULL, NULL }
  506. #define TEST_RCV_AUTHCHALLENGE(name) \
  507. { "recv_authchallenge/" #name , \
  508. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  509. &setup_recv_authchallenge, NULL }
  510. #define TEST_RCV_CERTS(name) \
  511. { "recv_certs/" #name , \
  512. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  513. &setup_recv_certs, NULL }
  514. struct testcase_t link_handshake_tests[] = {
  515. TEST(certs_ok, TT_FORK),
  516. //TEST(certs_bad, TT_FORK),
  517. TEST_RCV_CERTS(ok),
  518. TEST_RCV_CERTS(ok_server),
  519. TEST_RCV_CERTS(badstate),
  520. TEST_RCV_CERTS(badproto),
  521. TEST_RCV_CERTS(duplicate),
  522. TEST_RCV_CERTS(already_authenticated),
  523. TEST_RCV_CERTS(empty),
  524. TEST_RCV_CERTS(bad_circid),
  525. TEST_RCV_CERTS(truncated_1),
  526. TEST_RCV_CERTS(truncated_2),
  527. TEST_RCV_CERTS(truncated_3),
  528. TEST_RCV_CERTS(not_x509),
  529. TEST_RCV_CERTS(both_link),
  530. TEST_RCV_CERTS(both_id_rsa),
  531. TEST_RCV_CERTS(both_auth),
  532. TEST_RCV_CERTS(wrong_labels_1),
  533. TEST_RCV_CERTS(wrong_labels_2),
  534. TEST_RCV_CERTS(wrong_labels_3),
  535. TEST_RCV_CERTS(server_missing_certs),
  536. TEST_RCV_CERTS(server_wrong_labels_1),
  537. TEST(send_authchallenge, TT_FORK),
  538. TEST_RCV_AUTHCHALLENGE(ok),
  539. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  540. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  541. TEST_RCV_AUTHCHALLENGE(badstate),
  542. TEST_RCV_AUTHCHALLENGE(badproto),
  543. TEST_RCV_AUTHCHALLENGE(as_server),
  544. TEST_RCV_AUTHCHALLENGE(duplicate),
  545. TEST_RCV_AUTHCHALLENGE(nocerts),
  546. TEST_RCV_AUTHCHALLENGE(tooshort),
  547. TEST_RCV_AUTHCHALLENGE(truncated),
  548. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  549. END_OF_TESTCASES
  550. };