test_link_handshake.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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->certs->auth_cert == NULL);
  122. tt_assert(c1->handshake_state->certs->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->certs->auth_cert);
  137. tt_assert(c2->handshake_state->certs->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->certs->id_cert != NULL);
  258. tt_assert(d->c->handshake_state->certs->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->certs->id_cert != NULL);
  277. tt_assert(d->c->handshake_state->certs->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. 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. expect_log_msg_containing(require_failure_message); \
  295. } \
  296. done: \
  297. teardown_capture_of_logs(); \
  298. }
  299. CERTS_FAIL(badstate,
  300. require_failure_message = "We're not doing a v3 handshake!";
  301. d->c->base_.state = OR_CONN_STATE_CONNECTING;)
  302. CERTS_FAIL(badproto,
  303. require_failure_message = "not using link protocol >= 3";
  304. d->c->link_proto = 2)
  305. CERTS_FAIL(duplicate,
  306. require_failure_message = "We already got one";
  307. d->c->handshake_state->received_certs_cell = 1)
  308. CERTS_FAIL(already_authenticated,
  309. require_failure_message = "We're already authenticated!";
  310. d->c->handshake_state->authenticated = 1)
  311. CERTS_FAIL(empty,
  312. require_failure_message = "It had no body";
  313. d->cell->payload_len = 0)
  314. CERTS_FAIL(bad_circid,
  315. require_failure_message = "It had a nonzero circuit ID";
  316. d->cell->circ_id = 1)
  317. CERTS_FAIL(truncated_1,
  318. require_failure_message = "It couldn't be parsed";
  319. d->cell->payload[0] = 5)
  320. CERTS_FAIL(truncated_2,
  321. {
  322. require_failure_message = "It couldn't be parsed";
  323. d->cell->payload_len = 4;
  324. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);
  325. })
  326. CERTS_FAIL(truncated_3,
  327. {
  328. require_failure_message = "It couldn't be parsed";
  329. d->cell->payload_len = 7;
  330. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);
  331. })
  332. #define REENCODE() do { \
  333. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  334. tt_int_op(n, >, 0); \
  335. d->cell->payload_len = n; \
  336. } while (0)
  337. CERTS_FAIL(not_x509,
  338. {
  339. require_failure_message = "Received undecodable certificate";
  340. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  341. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  342. REENCODE();
  343. })
  344. CERTS_FAIL(both_link,
  345. {
  346. require_failure_message = "Duplicate x509 certificate";
  347. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  348. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  349. REENCODE();
  350. })
  351. CERTS_FAIL(both_id_rsa,
  352. {
  353. require_failure_message = "Duplicate x509 certificate";
  354. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  355. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  356. REENCODE();
  357. })
  358. CERTS_FAIL(both_auth,
  359. {
  360. require_failure_message = "Duplicate x509 certificate";
  361. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  362. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  363. REENCODE();
  364. })
  365. CERTS_FAIL(wrong_labels_1,
  366. {
  367. require_failure_message = "The link certificate was not valid";
  368. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  369. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  370. REENCODE();
  371. })
  372. CERTS_FAIL(wrong_labels_2,
  373. {
  374. const tor_x509_cert_t *a;
  375. const tor_x509_cert_t *b;
  376. const uint8_t *enca;
  377. size_t lena;
  378. require_failure_message = "The link certificate was not valid";
  379. tor_tls_get_my_certs(1, &a, &b);
  380. tor_x509_cert_get_der(a, &enca, &lena);
  381. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  382. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  383. enca, lena);
  384. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  385. REENCODE();
  386. })
  387. CERTS_FAIL(wrong_labels_3,
  388. {
  389. require_failure_message = "The certs we wanted were missing";
  390. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  391. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  392. REENCODE();
  393. })
  394. CERTS_FAIL(server_missing_certs,
  395. {
  396. require_failure_message = "The certs we wanted were missing";
  397. d->c->handshake_state->started_here = 0;
  398. })
  399. CERTS_FAIL(server_wrong_labels_1,
  400. {
  401. require_failure_message =
  402. "The authentication certificate was not valid";
  403. d->c->handshake_state->started_here = 0;
  404. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  405. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  406. REENCODE();
  407. })
  408. static void
  409. test_link_handshake_send_authchallenge(void *arg)
  410. {
  411. (void)arg;
  412. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  413. var_cell_t *cell1=NULL, *cell2=NULL;
  414. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  415. tt_int_op(connection_init_or_handshake_state(c1, 0), ==, 0);
  416. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  417. tt_assert(! mock_got_var_cell);
  418. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  419. cell1 = mock_got_var_cell;
  420. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  421. cell2 = mock_got_var_cell;
  422. tt_int_op(36, ==, cell1->payload_len);
  423. tt_int_op(36, ==, cell2->payload_len);
  424. tt_int_op(0, ==, cell1->circ_id);
  425. tt_int_op(0, ==, cell2->circ_id);
  426. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell1->command);
  427. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell2->command);
  428. tt_mem_op("\x00\x01\x00\x01", ==, cell1->payload + 32, 4);
  429. tt_mem_op("\x00\x01\x00\x01", ==, cell2->payload + 32, 4);
  430. tt_mem_op(cell1->payload, !=, cell2->payload, 32);
  431. done:
  432. UNMOCK(connection_or_write_var_cell_to_buf);
  433. connection_free_(TO_CONN(c1));
  434. tor_free(cell1);
  435. tor_free(cell2);
  436. }
  437. typedef struct authchallenge_data_s {
  438. or_connection_t *c;
  439. channel_tls_t *chan;
  440. var_cell_t *cell;
  441. } authchallenge_data_t;
  442. static int
  443. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  444. {
  445. (void)test;
  446. authchallenge_data_t *d = obj;
  447. UNMOCK(connection_or_send_netinfo);
  448. UNMOCK(connection_or_close_for_error);
  449. UNMOCK(connection_or_send_authenticate_cell);
  450. if (d) {
  451. tor_free(d->cell);
  452. connection_free_(TO_CONN(d->c));
  453. circuitmux_free(d->chan->base_.cmux);
  454. tor_free(d->chan);
  455. tor_free(d);
  456. }
  457. return 1;
  458. }
  459. static void *
  460. recv_authchallenge_setup(const struct testcase_t *test)
  461. {
  462. (void)test;
  463. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  464. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  465. d->chan = tor_malloc_zero(sizeof(*d->chan));
  466. d->c->chan = d->chan;
  467. d->c->base_.address = tor_strdup("HaveAnAddress");
  468. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  469. d->chan->conn = d->c;
  470. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  471. d->c->link_proto = 4;
  472. d->c->handshake_state->received_certs_cell = 1;
  473. d->cell = var_cell_new(128);
  474. d->cell->payload_len = 38;
  475. d->cell->payload[33] = 2;
  476. d->cell->payload[35] = 7;
  477. d->cell->payload[37] = 1;
  478. d->cell->command = CELL_AUTH_CHALLENGE;
  479. get_options_mutable()->ORPort_set = 1;
  480. MOCK(connection_or_close_for_error, mock_close_for_err);
  481. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  482. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  483. tt_int_op(0, ==, d->c->handshake_state->received_auth_challenge);
  484. tt_int_op(0, ==, mock_send_authenticate_called);
  485. tt_int_op(0, ==, mock_send_netinfo_called);
  486. return d;
  487. done:
  488. recv_authchallenge_cleanup(test, d);
  489. return NULL;
  490. }
  491. static struct testcase_setup_t setup_recv_authchallenge = {
  492. .setup_fn = recv_authchallenge_setup,
  493. .cleanup_fn = recv_authchallenge_cleanup
  494. };
  495. static void
  496. test_link_handshake_recv_authchallenge_ok(void *arg)
  497. {
  498. authchallenge_data_t *d = arg;
  499. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  500. tt_int_op(0, ==, mock_close_called);
  501. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  502. tt_int_op(1, ==, mock_send_authenticate_called);
  503. tt_int_op(1, ==, mock_send_netinfo_called);
  504. done:
  505. ;
  506. }
  507. static void
  508. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  509. {
  510. authchallenge_data_t *d = arg;
  511. get_options_mutable()->ORPort_set = 0;
  512. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  513. tt_int_op(0, ==, mock_close_called);
  514. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  515. tt_int_op(0, ==, mock_send_authenticate_called);
  516. tt_int_op(0, ==, mock_send_netinfo_called);
  517. done:
  518. ;
  519. }
  520. static void
  521. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  522. {
  523. authchallenge_data_t *d = arg;
  524. d->cell->payload[37] = 99;
  525. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  526. tt_int_op(0, ==, mock_close_called);
  527. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  528. tt_int_op(0, ==, mock_send_authenticate_called);
  529. tt_int_op(1, ==, mock_send_netinfo_called);
  530. done:
  531. ;
  532. }
  533. #define AUTHCHALLENGE_FAIL(name, code) \
  534. static void \
  535. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  536. { \
  537. authchallenge_data_t *d = arg; \
  538. const char *require_failure_message = NULL; \
  539. setup_capture_of_logs(LOG_INFO); \
  540. { code ; } \
  541. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  542. tt_int_op(1, ==, mock_close_called); \
  543. tt_int_op(0, ==, mock_send_authenticate_called); \
  544. tt_int_op(0, ==, mock_send_netinfo_called); \
  545. if (require_failure_message) { \
  546. expect_log_msg_containing(require_failure_message); \
  547. } \
  548. done: \
  549. teardown_capture_of_logs(); \
  550. }
  551. AUTHCHALLENGE_FAIL(badstate,
  552. require_failure_message = "We're not currently doing a "
  553. "v3 handshake";
  554. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  555. AUTHCHALLENGE_FAIL(badproto,
  556. require_failure_message = "not using link protocol >= 3";
  557. d->c->link_proto = 2)
  558. AUTHCHALLENGE_FAIL(as_server,
  559. require_failure_message = "We didn't originate this "
  560. "connection";
  561. d->c->handshake_state->started_here = 0;)
  562. AUTHCHALLENGE_FAIL(duplicate,
  563. require_failure_message = "We already received one";
  564. d->c->handshake_state->received_auth_challenge = 1)
  565. AUTHCHALLENGE_FAIL(nocerts,
  566. require_failure_message = "We haven't gotten a CERTS "
  567. "cell yet";
  568. d->c->handshake_state->received_certs_cell = 0)
  569. AUTHCHALLENGE_FAIL(tooshort,
  570. require_failure_message = "It was not well-formed";
  571. d->cell->payload_len = 33)
  572. AUTHCHALLENGE_FAIL(truncated,
  573. require_failure_message = "It was not well-formed";
  574. d->cell->payload_len = 34)
  575. AUTHCHALLENGE_FAIL(nonzero_circid,
  576. require_failure_message = "It had a nonzero circuit ID";
  577. d->cell->circ_id = 1337)
  578. static tor_x509_cert_t *mock_peer_cert = NULL;
  579. static tor_x509_cert_t *
  580. mock_get_peer_cert(tor_tls_t *tls)
  581. {
  582. (void)tls;
  583. return mock_peer_cert;
  584. }
  585. static int
  586. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  587. {
  588. (void)tls;
  589. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  590. return 0;
  591. }
  592. static void
  593. mock_set_circid_type(channel_t *chan,
  594. crypto_pk_t *identity_rcvd,
  595. int consider_identity)
  596. {
  597. (void) chan;
  598. (void) identity_rcvd;
  599. (void) consider_identity;
  600. }
  601. typedef struct authenticate_data_s {
  602. or_connection_t *c1, *c2;
  603. channel_tls_t *chan2;
  604. var_cell_t *cell;
  605. crypto_pk_t *key1, *key2;
  606. } authenticate_data_t;
  607. static int
  608. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  609. {
  610. (void) test;
  611. UNMOCK(connection_or_write_var_cell_to_buf);
  612. UNMOCK(tor_tls_get_peer_cert);
  613. UNMOCK(tor_tls_get_tlssecrets);
  614. UNMOCK(connection_or_close_for_error);
  615. UNMOCK(channel_set_circid_type);
  616. authenticate_data_t *d = arg;
  617. if (d) {
  618. tor_free(d->cell);
  619. connection_or_remove_from_identity_map(d->c1);
  620. connection_or_remove_from_identity_map(d->c2);
  621. connection_free_(TO_CONN(d->c1));
  622. connection_free_(TO_CONN(d->c2));
  623. circuitmux_free(d->chan2->base_.cmux);
  624. tor_free(d->chan2);
  625. crypto_pk_free(d->key1);
  626. crypto_pk_free(d->key2);
  627. tor_free(d);
  628. }
  629. mock_peer_cert = NULL;
  630. return 1;
  631. }
  632. static void *
  633. authenticate_data_setup(const struct testcase_t *test)
  634. {
  635. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  636. scheduler_init();
  637. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  638. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  639. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  640. MOCK(connection_or_close_for_error, mock_close_for_err);
  641. MOCK(channel_set_circid_type, mock_set_circid_type);
  642. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  643. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  644. tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
  645. tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
  646. d->key1 = pk_generate(2);
  647. d->key2 = pk_generate(3);
  648. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  649. d->key1, d->key2, 86400), ==, 0);
  650. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  651. d->c1->link_proto = 3;
  652. tt_int_op(connection_init_or_handshake_state(d->c1, 1), ==, 0);
  653. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  654. d->c2->link_proto = 3;
  655. tt_int_op(connection_init_or_handshake_state(d->c2, 0), ==, 0);
  656. var_cell_t *cell = var_cell_new(16);
  657. cell->command = CELL_CERTS;
  658. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  659. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  660. memset(cell->payload, 0xf0, 16);
  661. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  662. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  663. tor_free(cell);
  664. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  665. channel_tls_common_init(d->chan2);
  666. d->c2->chan = d->chan2;
  667. d->chan2->conn = d->c2;
  668. d->c2->base_.address = tor_strdup("C2");
  669. d->c2->tls = tor_tls_new(-1, 1);
  670. d->c2->handshake_state->received_certs_cell = 1;
  671. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  672. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  673. const uint8_t *der;
  674. size_t sz;
  675. tor_x509_cert_get_der(id_cert, &der, &sz);
  676. d->c1->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  677. d->c2->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  678. tor_x509_cert_get_der(link_cert, &der, &sz);
  679. mock_peer_cert = tor_x509_cert_decode(der, sz);
  680. tt_assert(mock_peer_cert);
  681. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  682. tor_x509_cert_get_der(auth_cert, &der, &sz);
  683. d->c2->handshake_state->certs->auth_cert = tor_x509_cert_decode(der, sz);
  684. /* Make an authenticate cell ... */
  685. tt_int_op(0, ==, connection_or_send_authenticate_cell(d->c1,
  686. AUTHTYPE_RSA_SHA256_TLSSECRET));
  687. tt_assert(mock_got_var_cell);
  688. d->cell = mock_got_var_cell;
  689. mock_got_var_cell = NULL;
  690. return d;
  691. done:
  692. authenticate_data_cleanup(test, d);
  693. return NULL;
  694. }
  695. static struct testcase_setup_t setup_authenticate = {
  696. .setup_fn = authenticate_data_setup,
  697. .cleanup_fn = authenticate_data_cleanup
  698. };
  699. static void
  700. test_link_handshake_auth_cell(void *arg)
  701. {
  702. authenticate_data_t *d = arg;
  703. auth1_t *auth1 = NULL;
  704. crypto_pk_t *auth_pubkey = NULL;
  705. /* Is the cell well-formed on the outer layer? */
  706. tt_int_op(d->cell->command, ==, CELL_AUTHENTICATE);
  707. tt_int_op(d->cell->payload[0], ==, 0);
  708. tt_int_op(d->cell->payload[1], ==, 1);
  709. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), ==,
  710. d->cell->payload_len - 4);
  711. /* Check it out for plausibility... */
  712. auth_ctx_t ctx;
  713. ctx.is_ed = 0;
  714. tt_int_op(d->cell->payload_len-4, ==, auth1_parse(&auth1,
  715. d->cell->payload+4,
  716. d->cell->payload_len - 4, &ctx));
  717. tt_assert(auth1);
  718. tt_mem_op(auth1->type, ==, "AUTH0001", 8);
  719. tt_mem_op(auth1->tlssecrets, ==, "int getRandomNumber(){return 4;}", 32);
  720. tt_int_op(auth1_getlen_sig(auth1), >, 120);
  721. /* Is the signature okay? */
  722. uint8_t sig[128];
  723. uint8_t digest[32];
  724. auth_pubkey = tor_tls_cert_get_key(d->c2->handshake_state->certs->auth_cert);
  725. int n = crypto_pk_public_checksig(
  726. auth_pubkey,
  727. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  728. auth1_getlen_sig(auth1));
  729. tt_int_op(n, ==, 32);
  730. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  731. crypto_digest256((char*)digest,
  732. (const char*)start, end-start, DIGEST_SHA256);
  733. tt_mem_op(sig, ==, digest, 32);
  734. /* Then feed it to c2. */
  735. tt_int_op(d->c2->handshake_state->authenticated, ==, 0);
  736. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  737. tt_int_op(mock_close_called, ==, 0);
  738. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  739. done:
  740. auth1_free(auth1);
  741. crypto_pk_free(auth_pubkey);
  742. }
  743. #define AUTHENTICATE_FAIL(name, code) \
  744. static void \
  745. test_link_handshake_auth_ ## name(void *arg) \
  746. { \
  747. authenticate_data_t *d = arg; \
  748. const char *require_failure_message = NULL; \
  749. setup_capture_of_logs(LOG_INFO); \
  750. { code ; } \
  751. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  752. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  753. tt_int_op(mock_close_called, ==, 1); \
  754. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  755. if (require_failure_message) { \
  756. expect_log_msg_containing(require_failure_message); \
  757. } \
  758. done: \
  759. teardown_capture_of_logs(); \
  760. }
  761. AUTHENTICATE_FAIL(badstate,
  762. require_failure_message = "We're not doing a v3 handshake";
  763. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  764. AUTHENTICATE_FAIL(badproto,
  765. require_failure_message = "not using link protocol >= 3";
  766. d->c2->link_proto = 2)
  767. AUTHENTICATE_FAIL(atclient,
  768. require_failure_message = "We originated this connection";
  769. d->c2->handshake_state->started_here = 1)
  770. AUTHENTICATE_FAIL(duplicate,
  771. require_failure_message = "We already got one";
  772. d->c2->handshake_state->received_authenticate = 1)
  773. static void
  774. test_link_handshake_auth_already_authenticated(void *arg)
  775. {
  776. authenticate_data_t *d = arg;
  777. setup_capture_of_logs(LOG_INFO);
  778. d->c2->handshake_state->authenticated = 1;
  779. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  780. tt_int_op(mock_close_called, ==, 1);
  781. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  782. expect_log_msg_containing("The peer is already authenticated");
  783. done:
  784. teardown_capture_of_logs();
  785. }
  786. AUTHENTICATE_FAIL(nocerts,
  787. require_failure_message = "We never got a certs cell";
  788. d->c2->handshake_state->received_certs_cell = 0)
  789. AUTHENTICATE_FAIL(noidcert,
  790. require_failure_message = "We never got an identity "
  791. "certificate";
  792. tor_x509_cert_free(d->c2->handshake_state->certs->id_cert);
  793. d->c2->handshake_state->certs->id_cert = NULL)
  794. AUTHENTICATE_FAIL(noauthcert,
  795. require_failure_message = "We never got an authentication "
  796. "certificate";
  797. tor_x509_cert_free(d->c2->handshake_state->certs->auth_cert);
  798. d->c2->handshake_state->certs->auth_cert = NULL)
  799. AUTHENTICATE_FAIL(tooshort,
  800. require_failure_message = "Cell was way too short";
  801. d->cell->payload_len = 3)
  802. AUTHENTICATE_FAIL(badtype,
  803. require_failure_message = "Authenticator type was not "
  804. "recognized";
  805. d->cell->payload[0] = 0xff)
  806. AUTHENTICATE_FAIL(truncated_1,
  807. require_failure_message = "Authenticator was truncated";
  808. d->cell->payload[2]++)
  809. AUTHENTICATE_FAIL(truncated_2,
  810. require_failure_message = "Authenticator was truncated";
  811. d->cell->payload[3]++)
  812. AUTHENTICATE_FAIL(tooshort_1,
  813. require_failure_message = "Authenticator was too short";
  814. tt_int_op(d->cell->payload_len, >=, 260);
  815. d->cell->payload[2] -= 1;
  816. d->cell->payload_len -= 256;)
  817. AUTHENTICATE_FAIL(badcontent,
  818. require_failure_message = "Some field in the AUTHENTICATE "
  819. "cell body was not as expected";
  820. d->cell->payload[10] ^= 0xff)
  821. AUTHENTICATE_FAIL(badsig_1,
  822. require_failure_message = "Signature wasn't valid";
  823. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  824. #define TEST(name, flags) \
  825. { #name , test_link_handshake_ ## name, (flags), NULL, NULL }
  826. #define TEST_RCV_AUTHCHALLENGE(name) \
  827. { "recv_authchallenge/" #name , \
  828. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  829. &setup_recv_authchallenge, NULL }
  830. #define TEST_RCV_CERTS(name) \
  831. { "recv_certs/" #name , \
  832. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  833. &setup_recv_certs, NULL }
  834. #define TEST_AUTHENTICATE(name) \
  835. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  836. &setup_authenticate, NULL }
  837. struct testcase_t link_handshake_tests[] = {
  838. TEST(certs_ok, TT_FORK),
  839. //TEST(certs_bad, TT_FORK),
  840. TEST_RCV_CERTS(ok),
  841. TEST_RCV_CERTS(ok_server),
  842. TEST_RCV_CERTS(badstate),
  843. TEST_RCV_CERTS(badproto),
  844. TEST_RCV_CERTS(duplicate),
  845. TEST_RCV_CERTS(already_authenticated),
  846. TEST_RCV_CERTS(empty),
  847. TEST_RCV_CERTS(bad_circid),
  848. TEST_RCV_CERTS(truncated_1),
  849. TEST_RCV_CERTS(truncated_2),
  850. TEST_RCV_CERTS(truncated_3),
  851. TEST_RCV_CERTS(not_x509),
  852. TEST_RCV_CERTS(both_link),
  853. TEST_RCV_CERTS(both_id_rsa),
  854. TEST_RCV_CERTS(both_auth),
  855. TEST_RCV_CERTS(wrong_labels_1),
  856. TEST_RCV_CERTS(wrong_labels_2),
  857. TEST_RCV_CERTS(wrong_labels_3),
  858. TEST_RCV_CERTS(server_missing_certs),
  859. TEST_RCV_CERTS(server_wrong_labels_1),
  860. TEST(send_authchallenge, TT_FORK),
  861. TEST_RCV_AUTHCHALLENGE(ok),
  862. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  863. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  864. TEST_RCV_AUTHCHALLENGE(badstate),
  865. TEST_RCV_AUTHCHALLENGE(badproto),
  866. TEST_RCV_AUTHCHALLENGE(as_server),
  867. TEST_RCV_AUTHCHALLENGE(duplicate),
  868. TEST_RCV_AUTHCHALLENGE(nocerts),
  869. TEST_RCV_AUTHCHALLENGE(tooshort),
  870. TEST_RCV_AUTHCHALLENGE(truncated),
  871. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  872. TEST_AUTHENTICATE(cell),
  873. TEST_AUTHENTICATE(badstate),
  874. TEST_AUTHENTICATE(badproto),
  875. TEST_AUTHENTICATE(atclient),
  876. TEST_AUTHENTICATE(duplicate),
  877. TEST_AUTHENTICATE(already_authenticated),
  878. TEST_AUTHENTICATE(nocerts),
  879. TEST_AUTHENTICATE(noidcert),
  880. TEST_AUTHENTICATE(noauthcert),
  881. TEST_AUTHENTICATE(tooshort),
  882. TEST_AUTHENTICATE(badtype),
  883. TEST_AUTHENTICATE(truncated_1),
  884. TEST_AUTHENTICATE(truncated_2),
  885. TEST_AUTHENTICATE(tooshort_1),
  886. TEST_AUTHENTICATE(badcontent),
  887. TEST_AUTHENTICATE(badsig_1),
  888. //TEST_AUTHENTICATE(),
  889. END_OF_TESTCASES
  890. };