test_link_handshake.c 29 KB

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