test_link_handshake.c 34 KB

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