test_link_handshake.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "orconfig.h"
  4. #define CHANNELTLS_PRIVATE
  5. #define CONNECTION_PRIVATE
  6. #define TOR_CHANNEL_INTERNAL_
  7. #include "or.h"
  8. #include "config.h"
  9. #include "connection.h"
  10. #include "connection_or.h"
  11. #include "channeltls.h"
  12. #include "link_handshake.h"
  13. #include "scheduler.h"
  14. #include "test.h"
  15. 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. d->cell->payload_len = 4;
  300. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);})
  301. CERTS_FAIL(truncated_3, {
  302. d->cell->payload_len = 7;
  303. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);})
  304. #define REENCODE() do { \
  305. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  306. tt_int_op(n, >, 0); \
  307. d->cell->payload_len = n; \
  308. } while (0)
  309. CERTS_FAIL(not_x509, {
  310. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  311. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  312. REENCODE();
  313. })
  314. CERTS_FAIL(both_link, {
  315. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  316. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  317. REENCODE();
  318. })
  319. CERTS_FAIL(both_id_rsa, {
  320. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  321. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  322. REENCODE();
  323. })
  324. CERTS_FAIL(both_auth, {
  325. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  326. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  327. REENCODE();
  328. })
  329. CERTS_FAIL(wrong_labels_1, {
  330. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  331. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  332. REENCODE();
  333. })
  334. CERTS_FAIL(wrong_labels_2, {
  335. const tor_x509_cert_t *a;
  336. const tor_x509_cert_t *b;
  337. const uint8_t *enca;
  338. size_t lena;
  339. tor_tls_get_my_certs(1, &a, &b);
  340. tor_x509_cert_get_der(a, &enca, &lena);
  341. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  342. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  343. enca, lena);
  344. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  345. REENCODE();
  346. })
  347. CERTS_FAIL(wrong_labels_3, {
  348. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  349. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  350. REENCODE();
  351. })
  352. CERTS_FAIL(server_missing_certs, {
  353. d->c->handshake_state->started_here = 0;
  354. })
  355. CERTS_FAIL(server_wrong_labels_1, {
  356. d->c->handshake_state->started_here = 0;
  357. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  358. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  359. REENCODE();
  360. })
  361. static void
  362. test_link_handshake_send_authchallenge(void *arg)
  363. {
  364. (void)arg;
  365. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  366. var_cell_t *cell1=NULL, *cell2=NULL;
  367. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  368. tt_int_op(connection_init_or_handshake_state(c1, 0), ==, 0);
  369. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  370. tt_assert(! mock_got_var_cell);
  371. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  372. cell1 = mock_got_var_cell;
  373. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  374. cell2 = mock_got_var_cell;
  375. tt_int_op(36, ==, cell1->payload_len);
  376. tt_int_op(36, ==, cell2->payload_len);
  377. tt_int_op(0, ==, cell1->circ_id);
  378. tt_int_op(0, ==, cell2->circ_id);
  379. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell1->command);
  380. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell2->command);
  381. tt_mem_op("\x00\x01\x00\x01", ==, cell1->payload + 32, 4);
  382. tt_mem_op("\x00\x01\x00\x01", ==, cell2->payload + 32, 4);
  383. tt_mem_op(cell1->payload, !=, cell2->payload, 32);
  384. done:
  385. UNMOCK(connection_or_write_var_cell_to_buf);
  386. connection_free_(TO_CONN(c1));
  387. tor_free(cell1);
  388. tor_free(cell2);
  389. }
  390. typedef struct authchallenge_data_s {
  391. or_connection_t *c;
  392. channel_tls_t *chan;
  393. var_cell_t *cell;
  394. } authchallenge_data_t;
  395. static int
  396. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  397. {
  398. (void)test;
  399. authchallenge_data_t *d = obj;
  400. UNMOCK(connection_or_send_netinfo);
  401. UNMOCK(connection_or_close_for_error);
  402. UNMOCK(connection_or_send_authenticate_cell);
  403. if (d) {
  404. tor_free(d->cell);
  405. connection_free_(TO_CONN(d->c));
  406. circuitmux_free(d->chan->base_.cmux);
  407. tor_free(d->chan);
  408. tor_free(d);
  409. }
  410. return 1;
  411. }
  412. static void *
  413. recv_authchallenge_setup(const struct testcase_t *test)
  414. {
  415. (void)test;
  416. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  417. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  418. d->chan = tor_malloc_zero(sizeof(*d->chan));
  419. d->c->chan = d->chan;
  420. d->c->base_.address = tor_strdup("HaveAnAddress");
  421. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  422. d->chan->conn = d->c;
  423. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  424. d->c->link_proto = 4;
  425. d->c->handshake_state->received_certs_cell = 1;
  426. d->cell = var_cell_new(128);
  427. d->cell->payload_len = 38;
  428. d->cell->payload[33] = 2;
  429. d->cell->payload[35] = 7;
  430. d->cell->payload[37] = 1;
  431. d->cell->command = CELL_AUTH_CHALLENGE;
  432. get_options_mutable()->ORPort_set = 1;
  433. MOCK(connection_or_close_for_error, mock_close_for_err);
  434. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  435. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  436. tt_int_op(0, ==, d->c->handshake_state->received_auth_challenge);
  437. tt_int_op(0, ==, mock_send_authenticate_called);
  438. tt_int_op(0, ==, mock_send_netinfo_called);
  439. return d;
  440. done:
  441. recv_authchallenge_cleanup(test, d);
  442. return NULL;
  443. }
  444. static struct testcase_setup_t setup_recv_authchallenge = {
  445. .setup_fn = recv_authchallenge_setup,
  446. .cleanup_fn = recv_authchallenge_cleanup
  447. };
  448. static void
  449. test_link_handshake_recv_authchallenge_ok(void *arg)
  450. {
  451. authchallenge_data_t *d = arg;
  452. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  453. tt_int_op(0, ==, mock_close_called);
  454. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  455. tt_int_op(1, ==, mock_send_authenticate_called);
  456. tt_int_op(1, ==, mock_send_netinfo_called);
  457. done:
  458. ;
  459. }
  460. static void
  461. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  462. {
  463. authchallenge_data_t *d = arg;
  464. get_options_mutable()->ORPort_set = 0;
  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(0, ==, mock_send_authenticate_called);
  469. tt_int_op(0, ==, mock_send_netinfo_called);
  470. done:
  471. ;
  472. }
  473. static void
  474. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  475. {
  476. authchallenge_data_t *d = arg;
  477. d->cell->payload[37] = 99;
  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(1, ==, mock_send_netinfo_called);
  483. done:
  484. ;
  485. }
  486. #define AUTHCHALLENGE_FAIL(name, code) \
  487. static void \
  488. test_link_handshake_recv_authchallenge_ ## name (void *arg) \
  489. { \
  490. authchallenge_data_t *d = arg; \
  491. { code ; } \
  492. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  493. tt_int_op(1, ==, mock_close_called); \
  494. tt_int_op(0, ==, mock_send_authenticate_called); \
  495. tt_int_op(0, ==, mock_send_netinfo_called); \
  496. done: \
  497. ; \
  498. }
  499. AUTHCHALLENGE_FAIL(badstate,
  500. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  501. AUTHCHALLENGE_FAIL(badproto,
  502. d->c->link_proto = 2)
  503. AUTHCHALLENGE_FAIL(as_server,
  504. d->c->handshake_state->started_here = 0;)
  505. AUTHCHALLENGE_FAIL(duplicate,
  506. d->c->handshake_state->received_auth_challenge = 1)
  507. AUTHCHALLENGE_FAIL(nocerts,
  508. d->c->handshake_state->received_certs_cell = 0)
  509. AUTHCHALLENGE_FAIL(tooshort,
  510. d->cell->payload_len = 33)
  511. AUTHCHALLENGE_FAIL(truncated,
  512. d->cell->payload_len = 34)
  513. AUTHCHALLENGE_FAIL(nonzero_circid,
  514. d->cell->circ_id = 1337)
  515. static tor_x509_cert_t *mock_peer_cert = NULL;
  516. static tor_x509_cert_t *
  517. mock_get_peer_cert(tor_tls_t *tls)
  518. {
  519. (void)tls;
  520. return mock_peer_cert;
  521. }
  522. static int
  523. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  524. {
  525. (void)tls;
  526. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  527. return 0;
  528. }
  529. static void
  530. mock_set_circid_type(channel_t *chan,
  531. crypto_pk_t *identity_rcvd,
  532. int consider_identity)
  533. {
  534. (void) chan;
  535. (void) identity_rcvd;
  536. (void) consider_identity;
  537. }
  538. typedef struct authenticate_data_s {
  539. or_connection_t *c1, *c2;
  540. channel_tls_t *chan2;
  541. var_cell_t *cell;
  542. crypto_pk_t *key1, *key2;
  543. } authenticate_data_t;
  544. static int
  545. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  546. {
  547. (void) test;
  548. UNMOCK(connection_or_write_var_cell_to_buf);
  549. UNMOCK(tor_tls_get_peer_cert);
  550. UNMOCK(tor_tls_get_tlssecrets);
  551. UNMOCK(connection_or_close_for_error);
  552. UNMOCK(channel_set_circid_type);
  553. authenticate_data_t *d = arg;
  554. if (d) {
  555. tor_free(d->cell);
  556. connection_free_(TO_CONN(d->c1));
  557. connection_free_(TO_CONN(d->c2));
  558. circuitmux_free(d->chan2->base_.cmux);
  559. tor_free(d->chan2);
  560. crypto_pk_free(d->key1);
  561. crypto_pk_free(d->key2);
  562. tor_free(d);
  563. }
  564. mock_peer_cert = NULL;
  565. return 1;
  566. }
  567. static void *
  568. authenticate_data_setup(const struct testcase_t *test)
  569. {
  570. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  571. scheduler_init();
  572. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  573. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  574. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  575. MOCK(connection_or_close_for_error, mock_close_for_err);
  576. MOCK(channel_set_circid_type, mock_set_circid_type);
  577. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  578. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  579. d->key1 = pk_generate(2);
  580. d->key2 = pk_generate(3);
  581. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  582. d->key1, d->key2, 86400), ==, 0);
  583. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  584. d->c1->link_proto = 3;
  585. tt_int_op(connection_init_or_handshake_state(d->c1, 1), ==, 0);
  586. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  587. d->c2->link_proto = 3;
  588. tt_int_op(connection_init_or_handshake_state(d->c2, 0), ==, 0);
  589. var_cell_t *cell = var_cell_new(16);
  590. cell->command = CELL_CERTS;
  591. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  592. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  593. memset(cell->payload, 0xf0, 16);
  594. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  595. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  596. tor_free(cell);
  597. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  598. channel_tls_common_init(d->chan2);
  599. d->c2->chan = d->chan2;
  600. d->chan2->conn = d->c2;
  601. d->c2->base_.address = tor_strdup("C2");
  602. d->c2->tls = tor_tls_new(-1, 1);
  603. d->c2->handshake_state->received_certs_cell = 1;
  604. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  605. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  606. const uint8_t *der;
  607. size_t sz;
  608. tor_x509_cert_get_der(id_cert, &der, &sz);
  609. d->c1->handshake_state->id_cert = tor_x509_cert_decode(der, sz);
  610. d->c2->handshake_state->id_cert = tor_x509_cert_decode(der, sz);
  611. tor_x509_cert_get_der(link_cert, &der, &sz);
  612. mock_peer_cert = tor_x509_cert_decode(der, sz);
  613. tt_assert(mock_peer_cert);
  614. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  615. tor_x509_cert_get_der(auth_cert, &der, &sz);
  616. d->c2->handshake_state->auth_cert = tor_x509_cert_decode(der, sz);
  617. /* Make an authenticate cell ... */
  618. tt_int_op(0, ==, connection_or_send_authenticate_cell(d->c1,
  619. AUTHTYPE_RSA_SHA256_TLSSECRET));
  620. tt_assert(mock_got_var_cell);
  621. d->cell = mock_got_var_cell;
  622. mock_got_var_cell = NULL;
  623. return d;
  624. done:
  625. authenticate_data_cleanup(test, d);
  626. return NULL;
  627. }
  628. static struct testcase_setup_t setup_authenticate = {
  629. .setup_fn = authenticate_data_setup,
  630. .cleanup_fn = authenticate_data_cleanup
  631. };
  632. static void
  633. test_link_handshake_auth_cell(void *arg)
  634. {
  635. authenticate_data_t *d = arg;
  636. auth1_t *auth1 = NULL;
  637. crypto_pk_t *auth_pubkey = NULL;
  638. /* Is the cell well-formed on the outer layer? */
  639. tt_int_op(d->cell->command, ==, CELL_AUTHENTICATE);
  640. tt_int_op(d->cell->payload[0], ==, 0);
  641. tt_int_op(d->cell->payload[1], ==, 1);
  642. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), ==,
  643. d->cell->payload_len - 4);
  644. /* Check it out for plausibility... */
  645. auth_ctx_t ctx;
  646. ctx.is_ed = 0;
  647. tt_int_op(d->cell->payload_len-4, ==, auth1_parse(&auth1,
  648. d->cell->payload+4,
  649. d->cell->payload_len - 4, &ctx));
  650. tt_assert(auth1);
  651. tt_mem_op(auth1->type, ==, "AUTH0001", 8);
  652. tt_mem_op(auth1->tlssecrets, ==, "int getRandomNumber(){return 4;}", 32);
  653. tt_int_op(auth1_getlen_sig(auth1), >, 120);
  654. /* Is the signature okay? */
  655. uint8_t sig[128];
  656. uint8_t digest[32];
  657. auth_pubkey = tor_tls_cert_get_key(d->c2->handshake_state->auth_cert);
  658. int n = crypto_pk_public_checksig(
  659. auth_pubkey,
  660. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  661. auth1_getlen_sig(auth1));
  662. tt_int_op(n, ==, 32);
  663. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  664. crypto_digest256((char*)digest,
  665. (const char*)start, end-start, DIGEST_SHA256);
  666. tt_mem_op(sig, ==, digest, 32);
  667. /* Then feed it to c2. */
  668. tt_int_op(d->c2->handshake_state->authenticated, ==, 0);
  669. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  670. tt_int_op(mock_close_called, ==, 0);
  671. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  672. done:
  673. auth1_free(auth1);
  674. crypto_pk_free(auth_pubkey);
  675. }
  676. #define AUTHENTICATE_FAIL(name, code) \
  677. static void \
  678. test_link_handshake_auth_ ## name (void *arg) \
  679. { \
  680. authenticate_data_t *d = arg; \
  681. { code ; } \
  682. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  683. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  684. tt_int_op(mock_close_called, ==, 1); \
  685. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  686. done: \
  687. ; \
  688. }
  689. AUTHENTICATE_FAIL(badstate,
  690. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  691. AUTHENTICATE_FAIL(badproto,
  692. d->c2->link_proto = 2)
  693. AUTHENTICATE_FAIL(atclient,
  694. d->c2->handshake_state->started_here = 1)
  695. AUTHENTICATE_FAIL(duplicate,
  696. d->c2->handshake_state->received_authenticate = 1)
  697. static void
  698. test_link_handshake_auth_already_authenticated(void *arg)
  699. {
  700. authenticate_data_t *d = arg;
  701. d->c2->handshake_state->authenticated = 1;
  702. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  703. tt_int_op(mock_close_called, ==, 1);
  704. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  705. done:
  706. ;
  707. }
  708. AUTHENTICATE_FAIL(nocerts,
  709. d->c2->handshake_state->received_certs_cell = 0)
  710. AUTHENTICATE_FAIL(noidcert,
  711. tor_x509_cert_free(d->c2->handshake_state->id_cert);
  712. d->c2->handshake_state->id_cert = NULL)
  713. AUTHENTICATE_FAIL(noauthcert,
  714. tor_x509_cert_free(d->c2->handshake_state->auth_cert);
  715. d->c2->handshake_state->auth_cert = NULL)
  716. AUTHENTICATE_FAIL(tooshort,
  717. d->cell->payload_len = 3)
  718. AUTHENTICATE_FAIL(badtype,
  719. d->cell->payload[0] = 0xff)
  720. AUTHENTICATE_FAIL(truncated_1,
  721. d->cell->payload[2]++)
  722. AUTHENTICATE_FAIL(truncated_2,
  723. d->cell->payload[3]++)
  724. AUTHENTICATE_FAIL(tooshort_1,
  725. tt_int_op(d->cell->payload_len, >=, 260);
  726. d->cell->payload[2] -= 1;
  727. d->cell->payload_len -= 256;)
  728. AUTHENTICATE_FAIL(badcontent,
  729. d->cell->payload[10] ^= 0xff)
  730. AUTHENTICATE_FAIL(badsig_1,
  731. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  732. #define TEST(name, flags) \
  733. { #name , test_link_handshake_ ## name, (flags), NULL, NULL }
  734. #define TEST_RCV_AUTHCHALLENGE(name) \
  735. { "recv_authchallenge/" #name , \
  736. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  737. &setup_recv_authchallenge, NULL }
  738. #define TEST_RCV_CERTS(name) \
  739. { "recv_certs/" #name , \
  740. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  741. &setup_recv_certs, NULL }
  742. #define TEST_AUTHENTICATE(name) \
  743. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  744. &setup_authenticate, NULL }
  745. struct testcase_t link_handshake_tests[] = {
  746. TEST(certs_ok, TT_FORK),
  747. //TEST(certs_bad, TT_FORK),
  748. TEST_RCV_CERTS(ok),
  749. TEST_RCV_CERTS(ok_server),
  750. TEST_RCV_CERTS(badstate),
  751. TEST_RCV_CERTS(badproto),
  752. TEST_RCV_CERTS(duplicate),
  753. TEST_RCV_CERTS(already_authenticated),
  754. TEST_RCV_CERTS(empty),
  755. TEST_RCV_CERTS(bad_circid),
  756. TEST_RCV_CERTS(truncated_1),
  757. TEST_RCV_CERTS(truncated_2),
  758. TEST_RCV_CERTS(truncated_3),
  759. TEST_RCV_CERTS(not_x509),
  760. TEST_RCV_CERTS(both_link),
  761. TEST_RCV_CERTS(both_id_rsa),
  762. TEST_RCV_CERTS(both_auth),
  763. TEST_RCV_CERTS(wrong_labels_1),
  764. TEST_RCV_CERTS(wrong_labels_2),
  765. TEST_RCV_CERTS(wrong_labels_3),
  766. TEST_RCV_CERTS(server_missing_certs),
  767. TEST_RCV_CERTS(server_wrong_labels_1),
  768. TEST(send_authchallenge, TT_FORK),
  769. TEST_RCV_AUTHCHALLENGE(ok),
  770. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  771. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  772. TEST_RCV_AUTHCHALLENGE(badstate),
  773. TEST_RCV_AUTHCHALLENGE(badproto),
  774. TEST_RCV_AUTHCHALLENGE(as_server),
  775. TEST_RCV_AUTHCHALLENGE(duplicate),
  776. TEST_RCV_AUTHCHALLENGE(nocerts),
  777. TEST_RCV_AUTHCHALLENGE(tooshort),
  778. TEST_RCV_AUTHCHALLENGE(truncated),
  779. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  780. TEST_AUTHENTICATE(cell),
  781. TEST_AUTHENTICATE(badstate),
  782. TEST_AUTHENTICATE(badproto),
  783. TEST_AUTHENTICATE(atclient),
  784. TEST_AUTHENTICATE(duplicate),
  785. TEST_AUTHENTICATE(already_authenticated),
  786. TEST_AUTHENTICATE(nocerts),
  787. TEST_AUTHENTICATE(noidcert),
  788. TEST_AUTHENTICATE(noauthcert),
  789. TEST_AUTHENTICATE(tooshort),
  790. TEST_AUTHENTICATE(badtype),
  791. TEST_AUTHENTICATE(truncated_1),
  792. TEST_AUTHENTICATE(truncated_2),
  793. TEST_AUTHENTICATE(tooshort_1),
  794. TEST_AUTHENTICATE(badcontent),
  795. TEST_AUTHENTICATE(badsig_1),
  796. //TEST_AUTHENTICATE(),
  797. END_OF_TESTCASES
  798. };