test_link_handshake.c 35 KB

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