test_link_handshake.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. /* Copyright (c) 2014-2016, 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 "scheduler.h"
  14. #include "test.h"
  15. static var_cell_t *mock_got_var_cell = NULL;
  16. static void
  17. mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
  18. {
  19. (void)conn;
  20. var_cell_t *newcell = var_cell_new(vc->payload_len);
  21. memcpy(newcell, vc, sizeof(var_cell_t));
  22. memcpy(newcell->payload, vc->payload, vc->payload_len);
  23. mock_got_var_cell = newcell;
  24. }
  25. static int
  26. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  27. {
  28. (void) tls;
  29. (void) cert; // XXXX look at this.
  30. return 1;
  31. }
  32. static int mock_send_netinfo_called = 0;
  33. static int
  34. mock_send_netinfo(or_connection_t *conn)
  35. {
  36. (void) conn;
  37. ++mock_send_netinfo_called;// XXX check_this
  38. return 0;
  39. }
  40. static int mock_close_called = 0;
  41. static void
  42. mock_close_for_err(or_connection_t *orconn, int flush)
  43. {
  44. (void)orconn;
  45. (void)flush;
  46. ++mock_close_called;
  47. }
  48. static int mock_send_authenticate_called = 0;
  49. static int
  50. mock_send_authenticate(or_connection_t *conn, int type)
  51. {
  52. (void) conn;
  53. (void) type;
  54. ++mock_send_authenticate_called;// XXX check_this
  55. return 0;
  56. }
  57. /* Test good certs cells */
  58. static void
  59. test_link_handshake_certs_ok(void *arg)
  60. {
  61. (void) arg;
  62. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  63. or_connection_t *c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  64. var_cell_t *cell1 = NULL, *cell2 = NULL;
  65. certs_cell_t *cc1 = NULL, *cc2 = NULL;
  66. channel_tls_t *chan1 = NULL, *chan2 = NULL;
  67. crypto_pk_t *key1 = NULL, *key2 = NULL;
  68. scheduler_init();
  69. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  70. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  71. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  72. key1 = pk_generate(2);
  73. key2 = pk_generate(3);
  74. /* We need to make sure that our TLS certificates are set up before we can
  75. * actually generate a CERTS cell.
  76. */
  77. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  78. key1, key2, 86400), ==, 0);
  79. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  80. c1->link_proto = 3;
  81. tt_int_op(connection_init_or_handshake_state(c1, 1), ==, 0);
  82. c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  83. c2->link_proto = 3;
  84. tt_int_op(connection_init_or_handshake_state(c2, 0), ==, 0);
  85. tt_int_op(0, ==, connection_or_send_certs_cell(c1));
  86. tt_assert(mock_got_var_cell);
  87. cell1 = mock_got_var_cell;
  88. tt_int_op(0, ==, connection_or_send_certs_cell(c2));
  89. tt_assert(mock_got_var_cell);
  90. cell2 = mock_got_var_cell;
  91. tt_int_op(cell1->command, ==, CELL_CERTS);
  92. tt_int_op(cell1->payload_len, >, 1);
  93. tt_int_op(cell2->command, ==, CELL_CERTS);
  94. tt_int_op(cell2->payload_len, >, 1);
  95. tt_int_op(cell1->payload_len, ==,
  96. certs_cell_parse(&cc1, cell1->payload, cell1->payload_len));
  97. tt_int_op(cell2->payload_len, ==,
  98. certs_cell_parse(&cc2, cell2->payload, cell2->payload_len));
  99. tt_int_op(2, ==, cc1->n_certs);
  100. tt_int_op(2, ==, cc2->n_certs);
  101. tt_int_op(certs_cell_get_certs(cc1, 0)->cert_type, ==,
  102. CERTTYPE_RSA1024_ID_AUTH);
  103. tt_int_op(certs_cell_get_certs(cc1, 1)->cert_type, ==,
  104. CERTTYPE_RSA1024_ID_ID);
  105. tt_int_op(certs_cell_get_certs(cc2, 0)->cert_type, ==,
  106. CERTTYPE_RSA1024_ID_LINK);
  107. tt_int_op(certs_cell_get_certs(cc2, 1)->cert_type, ==,
  108. CERTTYPE_RSA1024_ID_ID);
  109. chan1 = tor_malloc_zero(sizeof(*chan1));
  110. channel_tls_common_init(chan1);
  111. c1->chan = chan1;
  112. chan1->conn = c1;
  113. c1->base_.address = tor_strdup("C1");
  114. c1->tls = tor_tls_new(-1, 0);
  115. c1->link_proto = 4;
  116. c1->base_.conn_array_index = -1;
  117. crypto_pk_get_digest(key2, c1->identity_digest);
  118. channel_tls_process_certs_cell(cell2, chan1);
  119. tt_assert(c1->handshake_state->received_certs_cell);
  120. tt_assert(c1->handshake_state->auth_cert == NULL);
  121. tt_assert(c1->handshake_state->id_cert);
  122. tt_assert(! tor_mem_is_zero(
  123. (char*)c1->handshake_state->authenticated_peer_id, 20));
  124. chan2 = tor_malloc_zero(sizeof(*chan2));
  125. channel_tls_common_init(chan2);
  126. c2->chan = chan2;
  127. chan2->conn = c2;
  128. c2->base_.address = tor_strdup("C2");
  129. c2->tls = tor_tls_new(-1, 1);
  130. c2->link_proto = 4;
  131. c2->base_.conn_array_index = -1;
  132. crypto_pk_get_digest(key1, c2->identity_digest);
  133. channel_tls_process_certs_cell(cell1, chan2);
  134. tt_assert(c2->handshake_state->received_certs_cell);
  135. tt_assert(c2->handshake_state->auth_cert);
  136. tt_assert(c2->handshake_state->id_cert);
  137. tt_assert(tor_mem_is_zero(
  138. (char*)c2->handshake_state->authenticated_peer_id, 20));
  139. done:
  140. UNMOCK(tor_tls_cert_matches_key);
  141. UNMOCK(connection_or_write_var_cell_to_buf);
  142. UNMOCK(connection_or_send_netinfo);
  143. connection_free_(TO_CONN(c1));
  144. connection_free_(TO_CONN(c2));
  145. tor_free(cell1);
  146. tor_free(cell2);
  147. certs_cell_free(cc1);
  148. certs_cell_free(cc2);
  149. if (chan1)
  150. circuitmux_free(chan1->base_.cmux);
  151. tor_free(chan1);
  152. if (chan2)
  153. circuitmux_free(chan2->base_.cmux);
  154. tor_free(chan2);
  155. crypto_pk_free(key1);
  156. crypto_pk_free(key2);
  157. }
  158. typedef struct certs_data_s {
  159. or_connection_t *c;
  160. channel_tls_t *chan;
  161. certs_cell_t *ccell;
  162. var_cell_t *cell;
  163. crypto_pk_t *key1, *key2;
  164. } certs_data_t;
  165. static int
  166. recv_certs_cleanup(const struct testcase_t *test, void *obj)
  167. {
  168. (void)test;
  169. certs_data_t *d = obj;
  170. UNMOCK(tor_tls_cert_matches_key);
  171. UNMOCK(connection_or_send_netinfo);
  172. UNMOCK(connection_or_close_for_error);
  173. if (d) {
  174. tor_free(d->cell);
  175. certs_cell_free(d->ccell);
  176. connection_free_(TO_CONN(d->c));
  177. circuitmux_free(d->chan->base_.cmux);
  178. tor_free(d->chan);
  179. crypto_pk_free(d->key1);
  180. crypto_pk_free(d->key2);
  181. tor_free(d);
  182. }
  183. return 1;
  184. }
  185. static void *
  186. recv_certs_setup(const struct testcase_t *test)
  187. {
  188. (void)test;
  189. certs_data_t *d = tor_malloc_zero(sizeof(*d));
  190. certs_cell_cert_t *ccc1 = NULL;
  191. certs_cell_cert_t *ccc2 = NULL;
  192. ssize_t n;
  193. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  194. d->chan = tor_malloc_zero(sizeof(*d->chan));
  195. d->c->chan = d->chan;
  196. d->c->base_.address = tor_strdup("HaveAnAddress");
  197. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  198. d->chan->conn = d->c;
  199. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  200. d->c->link_proto = 4;
  201. d->key1 = pk_generate(2);
  202. d->key2 = pk_generate(3);
  203. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  204. d->key1, d->key2, 86400), ==, 0);
  205. d->ccell = certs_cell_new();
  206. ccc1 = certs_cell_cert_new();
  207. certs_cell_add_certs(d->ccell, ccc1);
  208. ccc2 = certs_cell_cert_new();
  209. certs_cell_add_certs(d->ccell, ccc2);
  210. d->ccell->n_certs = 2;
  211. ccc1->cert_type = 1;
  212. ccc2->cert_type = 2;
  213. const tor_x509_cert_t *a,*b;
  214. const uint8_t *enca, *encb;
  215. size_t lena, lenb;
  216. tor_tls_get_my_certs(1, &a, &b);
  217. tor_x509_cert_get_der(a, &enca, &lena);
  218. tor_x509_cert_get_der(b, &encb, &lenb);
  219. certs_cell_cert_setlen_body(ccc1, lena);
  220. ccc1->cert_len = lena;
  221. certs_cell_cert_setlen_body(ccc2, lenb);
  222. ccc2->cert_len = lenb;
  223. memcpy(certs_cell_cert_getarray_body(ccc1), enca, lena);
  224. memcpy(certs_cell_cert_getarray_body(ccc2), encb, lenb);
  225. d->cell = var_cell_new(4096);
  226. d->cell->command = CELL_CERTS;
  227. n = certs_cell_encode(d->cell->payload, 4096, d->ccell);
  228. tt_int_op(n, >, 0);
  229. d->cell->payload_len = n;
  230. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  231. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  232. MOCK(connection_or_close_for_error, mock_close_for_err);
  233. tt_int_op(0, ==, d->c->handshake_state->received_certs_cell);
  234. tt_int_op(0, ==, mock_send_authenticate_called);
  235. tt_int_op(0, ==, mock_send_netinfo_called);
  236. return d;
  237. done:
  238. recv_certs_cleanup(test, d);
  239. return NULL;
  240. }
  241. static struct testcase_setup_t setup_recv_certs = {
  242. .setup_fn = recv_certs_setup,
  243. .cleanup_fn = recv_certs_cleanup
  244. };
  245. static void
  246. test_link_handshake_recv_certs_ok(void *arg)
  247. {
  248. certs_data_t *d = arg;
  249. channel_tls_process_certs_cell(d->cell, d->chan);
  250. tt_int_op(0, ==, mock_close_called);
  251. tt_int_op(d->c->handshake_state->authenticated, ==, 1);
  252. tt_int_op(d->c->handshake_state->received_certs_cell, ==, 1);
  253. tt_assert(d->c->handshake_state->id_cert != NULL);
  254. tt_assert(d->c->handshake_state->auth_cert == NULL);
  255. done:
  256. ;
  257. }
  258. static void
  259. test_link_handshake_recv_certs_ok_server(void *arg)
  260. {
  261. certs_data_t *d = arg;
  262. d->c->handshake_state->started_here = 0;
  263. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  264. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  265. ssize_t n = certs_cell_encode(d->cell->payload, 2048, d->ccell);
  266. tt_int_op(n, >, 0);
  267. d->cell->payload_len = n;
  268. channel_tls_process_certs_cell(d->cell, d->chan);
  269. tt_int_op(0, ==, mock_close_called);
  270. tt_int_op(d->c->handshake_state->authenticated, ==, 0);
  271. tt_int_op(d->c->handshake_state->received_certs_cell, ==, 1);
  272. tt_assert(d->c->handshake_state->id_cert != NULL);
  273. tt_assert(d->c->handshake_state->auth_cert != NULL);
  274. done:
  275. ;
  276. }
  277. #define CERTS_FAIL(name, code) \
  278. static void \
  279. test_link_handshake_recv_certs_ ## name(void *arg) \
  280. { \
  281. certs_data_t *d = arg; \
  282. { code ; } \
  283. channel_tls_process_certs_cell(d->cell, d->chan); \
  284. tt_int_op(1, ==, mock_close_called); \
  285. tt_int_op(0, ==, mock_send_authenticate_called); \
  286. tt_int_op(0, ==, mock_send_netinfo_called); \
  287. done: \
  288. ; \
  289. }
  290. CERTS_FAIL(badstate, d->c->base_.state = OR_CONN_STATE_CONNECTING)
  291. CERTS_FAIL(badproto, d->c->link_proto = 2)
  292. CERTS_FAIL(duplicate, d->c->handshake_state->received_certs_cell = 1)
  293. CERTS_FAIL(already_authenticated,
  294. d->c->handshake_state->authenticated = 1)
  295. CERTS_FAIL(empty, d->cell->payload_len = 0)
  296. CERTS_FAIL(bad_circid, d->cell->circ_id = 1)
  297. CERTS_FAIL(truncated_1, d->cell->payload[0] = 5)
  298. CERTS_FAIL(truncated_2,
  299. {
  300. d->cell->payload_len = 4;
  301. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);
  302. })
  303. CERTS_FAIL(truncated_3,
  304. {
  305. d->cell->payload_len = 7;
  306. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);
  307. })
  308. #define REENCODE() do { \
  309. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  310. tt_int_op(n, >, 0); \
  311. d->cell->payload_len = n; \
  312. } while (0)
  313. CERTS_FAIL(not_x509,
  314. {
  315. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  316. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  317. REENCODE();
  318. })
  319. CERTS_FAIL(both_link,
  320. {
  321. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  322. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  323. REENCODE();
  324. })
  325. CERTS_FAIL(both_id_rsa,
  326. {
  327. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  328. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  329. REENCODE();
  330. })
  331. CERTS_FAIL(both_auth,
  332. {
  333. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  334. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  335. REENCODE();
  336. })
  337. CERTS_FAIL(wrong_labels_1,
  338. {
  339. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  340. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  341. REENCODE();
  342. })
  343. CERTS_FAIL(wrong_labels_2,
  344. {
  345. const tor_x509_cert_t *a;
  346. const tor_x509_cert_t *b;
  347. const uint8_t *enca;
  348. size_t lena;
  349. tor_tls_get_my_certs(1, &a, &b);
  350. tor_x509_cert_get_der(a, &enca, &lena);
  351. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  352. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  353. enca, lena);
  354. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  355. REENCODE();
  356. })
  357. CERTS_FAIL(wrong_labels_3,
  358. {
  359. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  360. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  361. REENCODE();
  362. })
  363. CERTS_FAIL(server_missing_certs,
  364. {
  365. d->c->handshake_state->started_here = 0;
  366. })
  367. CERTS_FAIL(server_wrong_labels_1,
  368. {
  369. d->c->handshake_state->started_here = 0;
  370. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  371. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  372. REENCODE();
  373. })
  374. static void
  375. test_link_handshake_send_authchallenge(void *arg)
  376. {
  377. (void)arg;
  378. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  379. var_cell_t *cell1=NULL, *cell2=NULL;
  380. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  381. tt_int_op(connection_init_or_handshake_state(c1, 0), ==, 0);
  382. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  383. tt_assert(! mock_got_var_cell);
  384. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  385. cell1 = mock_got_var_cell;
  386. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  387. cell2 = mock_got_var_cell;
  388. tt_int_op(36, ==, cell1->payload_len);
  389. tt_int_op(36, ==, cell2->payload_len);
  390. tt_int_op(0, ==, cell1->circ_id);
  391. tt_int_op(0, ==, cell2->circ_id);
  392. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell1->command);
  393. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell2->command);
  394. tt_mem_op("\x00\x01\x00\x01", ==, cell1->payload + 32, 4);
  395. tt_mem_op("\x00\x01\x00\x01", ==, cell2->payload + 32, 4);
  396. tt_mem_op(cell1->payload, !=, cell2->payload, 32);
  397. done:
  398. UNMOCK(connection_or_write_var_cell_to_buf);
  399. connection_free_(TO_CONN(c1));
  400. tor_free(cell1);
  401. tor_free(cell2);
  402. }
  403. typedef struct authchallenge_data_s {
  404. or_connection_t *c;
  405. channel_tls_t *chan;
  406. var_cell_t *cell;
  407. } authchallenge_data_t;
  408. static int
  409. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  410. {
  411. (void)test;
  412. authchallenge_data_t *d = obj;
  413. UNMOCK(connection_or_send_netinfo);
  414. UNMOCK(connection_or_close_for_error);
  415. UNMOCK(connection_or_send_authenticate_cell);
  416. if (d) {
  417. tor_free(d->cell);
  418. connection_free_(TO_CONN(d->c));
  419. circuitmux_free(d->chan->base_.cmux);
  420. tor_free(d->chan);
  421. tor_free(d);
  422. }
  423. return 1;
  424. }
  425. static void *
  426. recv_authchallenge_setup(const struct testcase_t *test)
  427. {
  428. (void)test;
  429. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  430. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  431. d->chan = tor_malloc_zero(sizeof(*d->chan));
  432. d->c->chan = d->chan;
  433. d->c->base_.address = tor_strdup("HaveAnAddress");
  434. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  435. d->chan->conn = d->c;
  436. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  437. d->c->link_proto = 4;
  438. d->c->handshake_state->received_certs_cell = 1;
  439. d->cell = var_cell_new(128);
  440. d->cell->payload_len = 38;
  441. d->cell->payload[33] = 2;
  442. d->cell->payload[35] = 7;
  443. d->cell->payload[37] = 1;
  444. d->cell->command = CELL_AUTH_CHALLENGE;
  445. get_options_mutable()->ORPort_set = 1;
  446. MOCK(connection_or_close_for_error, mock_close_for_err);
  447. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  448. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  449. tt_int_op(0, ==, d->c->handshake_state->received_auth_challenge);
  450. tt_int_op(0, ==, mock_send_authenticate_called);
  451. tt_int_op(0, ==, mock_send_netinfo_called);
  452. return d;
  453. done:
  454. recv_authchallenge_cleanup(test, d);
  455. return NULL;
  456. }
  457. static struct testcase_setup_t setup_recv_authchallenge = {
  458. .setup_fn = recv_authchallenge_setup,
  459. .cleanup_fn = recv_authchallenge_cleanup
  460. };
  461. static void
  462. test_link_handshake_recv_authchallenge_ok(void *arg)
  463. {
  464. authchallenge_data_t *d = arg;
  465. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  466. tt_int_op(0, ==, mock_close_called);
  467. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  468. tt_int_op(1, ==, mock_send_authenticate_called);
  469. tt_int_op(1, ==, mock_send_netinfo_called);
  470. done:
  471. ;
  472. }
  473. static void
  474. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  475. {
  476. authchallenge_data_t *d = arg;
  477. get_options_mutable()->ORPort_set = 0;
  478. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  479. tt_int_op(0, ==, mock_close_called);
  480. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  481. tt_int_op(0, ==, mock_send_authenticate_called);
  482. tt_int_op(0, ==, mock_send_netinfo_called);
  483. done:
  484. ;
  485. }
  486. static void
  487. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  488. {
  489. authchallenge_data_t *d = arg;
  490. d->cell->payload[37] = 99;
  491. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  492. tt_int_op(0, ==, mock_close_called);
  493. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  494. tt_int_op(0, ==, mock_send_authenticate_called);
  495. tt_int_op(1, ==, mock_send_netinfo_called);
  496. done:
  497. ;
  498. }
  499. #define AUTHCHALLENGE_FAIL(name, code) \
  500. static void \
  501. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  502. { \
  503. authchallenge_data_t *d = arg; \
  504. { code ; } \
  505. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  506. tt_int_op(1, ==, mock_close_called); \
  507. tt_int_op(0, ==, mock_send_authenticate_called); \
  508. tt_int_op(0, ==, mock_send_netinfo_called); \
  509. done: \
  510. ; \
  511. }
  512. AUTHCHALLENGE_FAIL(badstate,
  513. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  514. AUTHCHALLENGE_FAIL(badproto,
  515. d->c->link_proto = 2)
  516. AUTHCHALLENGE_FAIL(as_server,
  517. d->c->handshake_state->started_here = 0;)
  518. AUTHCHALLENGE_FAIL(duplicate,
  519. d->c->handshake_state->received_auth_challenge = 1)
  520. AUTHCHALLENGE_FAIL(nocerts,
  521. d->c->handshake_state->received_certs_cell = 0)
  522. AUTHCHALLENGE_FAIL(tooshort,
  523. d->cell->payload_len = 33)
  524. AUTHCHALLENGE_FAIL(truncated,
  525. d->cell->payload_len = 34)
  526. AUTHCHALLENGE_FAIL(nonzero_circid,
  527. d->cell->circ_id = 1337)
  528. static tor_x509_cert_t *mock_peer_cert = NULL;
  529. static tor_x509_cert_t *
  530. mock_get_peer_cert(tor_tls_t *tls)
  531. {
  532. (void)tls;
  533. return mock_peer_cert;
  534. }
  535. static int
  536. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  537. {
  538. (void)tls;
  539. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  540. return 0;
  541. }
  542. static void
  543. mock_set_circid_type(channel_t *chan,
  544. crypto_pk_t *identity_rcvd,
  545. int consider_identity)
  546. {
  547. (void) chan;
  548. (void) identity_rcvd;
  549. (void) consider_identity;
  550. }
  551. typedef struct authenticate_data_s {
  552. or_connection_t *c1, *c2;
  553. channel_tls_t *chan2;
  554. var_cell_t *cell;
  555. crypto_pk_t *key1, *key2;
  556. } authenticate_data_t;
  557. static int
  558. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  559. {
  560. (void) test;
  561. UNMOCK(connection_or_write_var_cell_to_buf);
  562. UNMOCK(tor_tls_get_peer_cert);
  563. UNMOCK(tor_tls_get_tlssecrets);
  564. UNMOCK(connection_or_close_for_error);
  565. UNMOCK(channel_set_circid_type);
  566. authenticate_data_t *d = arg;
  567. if (d) {
  568. tor_free(d->cell);
  569. connection_free_(TO_CONN(d->c1));
  570. connection_free_(TO_CONN(d->c2));
  571. circuitmux_free(d->chan2->base_.cmux);
  572. tor_free(d->chan2);
  573. crypto_pk_free(d->key1);
  574. crypto_pk_free(d->key2);
  575. tor_free(d);
  576. }
  577. mock_peer_cert = NULL;
  578. return 1;
  579. }
  580. static void *
  581. authenticate_data_setup(const struct testcase_t *test)
  582. {
  583. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  584. scheduler_init();
  585. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  586. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  587. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  588. MOCK(connection_or_close_for_error, mock_close_for_err);
  589. MOCK(channel_set_circid_type, mock_set_circid_type);
  590. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  591. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  592. d->key1 = pk_generate(2);
  593. d->key2 = pk_generate(3);
  594. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  595. d->key1, d->key2, 86400), ==, 0);
  596. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  597. d->c1->link_proto = 3;
  598. tt_int_op(connection_init_or_handshake_state(d->c1, 1), ==, 0);
  599. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  600. d->c2->link_proto = 3;
  601. tt_int_op(connection_init_or_handshake_state(d->c2, 0), ==, 0);
  602. var_cell_t *cell = var_cell_new(16);
  603. cell->command = CELL_CERTS;
  604. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  605. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  606. memset(cell->payload, 0xf0, 16);
  607. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  608. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  609. tor_free(cell);
  610. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  611. channel_tls_common_init(d->chan2);
  612. d->c2->chan = d->chan2;
  613. d->chan2->conn = d->c2;
  614. d->c2->base_.address = tor_strdup("C2");
  615. d->c2->tls = tor_tls_new(-1, 1);
  616. d->c2->handshake_state->received_certs_cell = 1;
  617. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  618. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  619. const uint8_t *der;
  620. size_t sz;
  621. tor_x509_cert_get_der(id_cert, &der, &sz);
  622. d->c1->handshake_state->id_cert = tor_x509_cert_decode(der, sz);
  623. d->c2->handshake_state->id_cert = tor_x509_cert_decode(der, sz);
  624. tor_x509_cert_get_der(link_cert, &der, &sz);
  625. mock_peer_cert = tor_x509_cert_decode(der, sz);
  626. tt_assert(mock_peer_cert);
  627. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  628. tor_x509_cert_get_der(auth_cert, &der, &sz);
  629. d->c2->handshake_state->auth_cert = tor_x509_cert_decode(der, sz);
  630. /* Make an authenticate cell ... */
  631. tt_int_op(0, ==, connection_or_send_authenticate_cell(d->c1,
  632. AUTHTYPE_RSA_SHA256_TLSSECRET));
  633. tt_assert(mock_got_var_cell);
  634. d->cell = mock_got_var_cell;
  635. mock_got_var_cell = NULL;
  636. return d;
  637. done:
  638. authenticate_data_cleanup(test, d);
  639. return NULL;
  640. }
  641. static struct testcase_setup_t setup_authenticate = {
  642. .setup_fn = authenticate_data_setup,
  643. .cleanup_fn = authenticate_data_cleanup
  644. };
  645. static void
  646. test_link_handshake_auth_cell(void *arg)
  647. {
  648. authenticate_data_t *d = arg;
  649. auth1_t *auth1 = NULL;
  650. crypto_pk_t *auth_pubkey = NULL;
  651. /* Is the cell well-formed on the outer layer? */
  652. tt_int_op(d->cell->command, ==, CELL_AUTHENTICATE);
  653. tt_int_op(d->cell->payload[0], ==, 0);
  654. tt_int_op(d->cell->payload[1], ==, 1);
  655. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), ==,
  656. d->cell->payload_len - 4);
  657. /* Check it out for plausibility... */
  658. auth_ctx_t ctx;
  659. ctx.is_ed = 0;
  660. tt_int_op(d->cell->payload_len-4, ==, auth1_parse(&auth1,
  661. d->cell->payload+4,
  662. d->cell->payload_len - 4, &ctx));
  663. tt_assert(auth1);
  664. tt_mem_op(auth1->type, ==, "AUTH0001", 8);
  665. tt_mem_op(auth1->tlssecrets, ==, "int getRandomNumber(){return 4;}", 32);
  666. tt_int_op(auth1_getlen_sig(auth1), >, 120);
  667. /* Is the signature okay? */
  668. uint8_t sig[128];
  669. uint8_t digest[32];
  670. auth_pubkey = tor_tls_cert_get_key(d->c2->handshake_state->auth_cert);
  671. int n = crypto_pk_public_checksig(
  672. auth_pubkey,
  673. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  674. auth1_getlen_sig(auth1));
  675. tt_int_op(n, ==, 32);
  676. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  677. crypto_digest256((char*)digest,
  678. (const char*)start, end-start, DIGEST_SHA256);
  679. tt_mem_op(sig, ==, digest, 32);
  680. /* Then feed it to c2. */
  681. tt_int_op(d->c2->handshake_state->authenticated, ==, 0);
  682. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  683. tt_int_op(mock_close_called, ==, 0);
  684. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  685. done:
  686. auth1_free(auth1);
  687. crypto_pk_free(auth_pubkey);
  688. }
  689. #define AUTHENTICATE_FAIL(name, code) \
  690. static void \
  691. test_link_handshake_auth_ ## name(void *arg) \
  692. { \
  693. authenticate_data_t *d = arg; \
  694. { code ; } \
  695. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  696. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  697. tt_int_op(mock_close_called, ==, 1); \
  698. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  699. done: \
  700. ; \
  701. }
  702. AUTHENTICATE_FAIL(badstate,
  703. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  704. AUTHENTICATE_FAIL(badproto,
  705. d->c2->link_proto = 2)
  706. AUTHENTICATE_FAIL(atclient,
  707. d->c2->handshake_state->started_here = 1)
  708. AUTHENTICATE_FAIL(duplicate,
  709. d->c2->handshake_state->received_authenticate = 1)
  710. static void
  711. test_link_handshake_auth_already_authenticated(void *arg)
  712. {
  713. authenticate_data_t *d = arg;
  714. d->c2->handshake_state->authenticated = 1;
  715. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  716. tt_int_op(mock_close_called, ==, 1);
  717. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  718. done:
  719. ;
  720. }
  721. AUTHENTICATE_FAIL(nocerts,
  722. d->c2->handshake_state->received_certs_cell = 0)
  723. AUTHENTICATE_FAIL(noidcert,
  724. tor_x509_cert_free(d->c2->handshake_state->id_cert);
  725. d->c2->handshake_state->id_cert = NULL)
  726. AUTHENTICATE_FAIL(noauthcert,
  727. tor_x509_cert_free(d->c2->handshake_state->auth_cert);
  728. d->c2->handshake_state->auth_cert = NULL)
  729. AUTHENTICATE_FAIL(tooshort,
  730. d->cell->payload_len = 3)
  731. AUTHENTICATE_FAIL(badtype,
  732. d->cell->payload[0] = 0xff)
  733. AUTHENTICATE_FAIL(truncated_1,
  734. d->cell->payload[2]++)
  735. AUTHENTICATE_FAIL(truncated_2,
  736. d->cell->payload[3]++)
  737. AUTHENTICATE_FAIL(tooshort_1,
  738. tt_int_op(d->cell->payload_len, >=, 260);
  739. d->cell->payload[2] -= 1;
  740. d->cell->payload_len -= 256;)
  741. AUTHENTICATE_FAIL(badcontent,
  742. d->cell->payload[10] ^= 0xff)
  743. AUTHENTICATE_FAIL(badsig_1,
  744. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  745. #define TEST(name, flags) \
  746. { #name , test_link_handshake_ ## name, (flags), NULL, NULL }
  747. #define TEST_RCV_AUTHCHALLENGE(name) \
  748. { "recv_authchallenge/" #name , \
  749. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  750. &setup_recv_authchallenge, NULL }
  751. #define TEST_RCV_CERTS(name) \
  752. { "recv_certs/" #name , \
  753. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  754. &setup_recv_certs, NULL }
  755. #define TEST_AUTHENTICATE(name) \
  756. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  757. &setup_authenticate, NULL }
  758. struct testcase_t link_handshake_tests[] = {
  759. TEST(certs_ok, TT_FORK),
  760. //TEST(certs_bad, TT_FORK),
  761. TEST_RCV_CERTS(ok),
  762. TEST_RCV_CERTS(ok_server),
  763. TEST_RCV_CERTS(badstate),
  764. TEST_RCV_CERTS(badproto),
  765. TEST_RCV_CERTS(duplicate),
  766. TEST_RCV_CERTS(already_authenticated),
  767. TEST_RCV_CERTS(empty),
  768. TEST_RCV_CERTS(bad_circid),
  769. TEST_RCV_CERTS(truncated_1),
  770. TEST_RCV_CERTS(truncated_2),
  771. TEST_RCV_CERTS(truncated_3),
  772. TEST_RCV_CERTS(not_x509),
  773. TEST_RCV_CERTS(both_link),
  774. TEST_RCV_CERTS(both_id_rsa),
  775. TEST_RCV_CERTS(both_auth),
  776. TEST_RCV_CERTS(wrong_labels_1),
  777. TEST_RCV_CERTS(wrong_labels_2),
  778. TEST_RCV_CERTS(wrong_labels_3),
  779. TEST_RCV_CERTS(server_missing_certs),
  780. TEST_RCV_CERTS(server_wrong_labels_1),
  781. TEST(send_authchallenge, TT_FORK),
  782. TEST_RCV_AUTHCHALLENGE(ok),
  783. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  784. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  785. TEST_RCV_AUTHCHALLENGE(badstate),
  786. TEST_RCV_AUTHCHALLENGE(badproto),
  787. TEST_RCV_AUTHCHALLENGE(as_server),
  788. TEST_RCV_AUTHCHALLENGE(duplicate),
  789. TEST_RCV_AUTHCHALLENGE(nocerts),
  790. TEST_RCV_AUTHCHALLENGE(tooshort),
  791. TEST_RCV_AUTHCHALLENGE(truncated),
  792. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  793. TEST_AUTHENTICATE(cell),
  794. TEST_AUTHENTICATE(badstate),
  795. TEST_AUTHENTICATE(badproto),
  796. TEST_AUTHENTICATE(atclient),
  797. TEST_AUTHENTICATE(duplicate),
  798. TEST_AUTHENTICATE(already_authenticated),
  799. TEST_AUTHENTICATE(nocerts),
  800. TEST_AUTHENTICATE(noidcert),
  801. TEST_AUTHENTICATE(noauthcert),
  802. TEST_AUTHENTICATE(tooshort),
  803. TEST_AUTHENTICATE(badtype),
  804. TEST_AUTHENTICATE(truncated_1),
  805. TEST_AUTHENTICATE(truncated_2),
  806. TEST_AUTHENTICATE(tooshort_1),
  807. TEST_AUTHENTICATE(badcontent),
  808. TEST_AUTHENTICATE(badsig_1),
  809. //TEST_AUTHENTICATE(),
  810. END_OF_TESTCASES
  811. };