test_link_handshake.c 32 KB

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