test_link_handshake.c 34 KB

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