test_link_handshake.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  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 "routerkeys.h"
  14. #include "scheduler.h"
  15. #include "torcert.h"
  16. #include "test.h"
  17. #include "log_test_helpers.h"
  18. static var_cell_t *mock_got_var_cell = NULL;
  19. static void
  20. mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
  21. {
  22. (void)conn;
  23. var_cell_t *newcell = var_cell_new(vc->payload_len);
  24. memcpy(newcell, vc, sizeof(var_cell_t));
  25. memcpy(newcell->payload, vc->payload, vc->payload_len);
  26. mock_got_var_cell = newcell;
  27. }
  28. static int
  29. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  30. {
  31. (void) tls;
  32. (void) cert; // XXXX look at this.
  33. return 1;
  34. }
  35. static tor_tls_t *mock_peer_cert_expect_tortls = NULL;
  36. static tor_x509_cert_t *mock_peer_cert = NULL;
  37. static tor_x509_cert_t *
  38. mock_get_peer_cert(tor_tls_t *tls)
  39. {
  40. if (mock_peer_cert_expect_tortls &&
  41. mock_peer_cert_expect_tortls != tls)
  42. return NULL;
  43. return mock_peer_cert;
  44. }
  45. static int mock_send_netinfo_called = 0;
  46. static int
  47. mock_send_netinfo(or_connection_t *conn)
  48. {
  49. (void) conn;
  50. ++mock_send_netinfo_called;// XXX check_this
  51. return 0;
  52. }
  53. static int mock_close_called = 0;
  54. static void
  55. mock_close_for_err(or_connection_t *orconn, int flush)
  56. {
  57. (void)orconn;
  58. (void)flush;
  59. ++mock_close_called;
  60. }
  61. static int mock_send_authenticate_called = 0;
  62. static int mock_send_authenticate_called_with_type = 0;
  63. static int
  64. mock_send_authenticate(or_connection_t *conn, int type)
  65. {
  66. (void) conn;
  67. mock_send_authenticate_called_with_type = type;
  68. ++mock_send_authenticate_called;// XXX check_this
  69. return 0;
  70. }
  71. static int
  72. mock_export_key_material(tor_tls_t *tls, uint8_t *secrets_out,
  73. const uint8_t *context,
  74. size_t context_len,
  75. const char *label)
  76. {
  77. (void) tls;
  78. (void)secrets_out;
  79. (void)context;
  80. (void)context_len;
  81. (void)label;
  82. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  83. return 0;
  84. }
  85. /* Test good certs cells */
  86. static void
  87. test_link_handshake_certs_ok(void *arg)
  88. {
  89. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  90. or_connection_t *c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  91. var_cell_t *cell1 = NULL, *cell2 = NULL;
  92. certs_cell_t *cc1 = NULL, *cc2 = NULL;
  93. channel_tls_t *chan1 = NULL, *chan2 = NULL;
  94. crypto_pk_t *key1 = NULL, *key2 = NULL;
  95. const int with_ed = !strcmp((const char *)arg, "Ed25519");
  96. scheduler_init();
  97. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  98. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  99. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  100. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  101. key1 = pk_generate(2);
  102. key2 = pk_generate(3);
  103. /* We need to make sure that our TLS certificates are set up before we can
  104. * actually generate a CERTS cell.
  105. */
  106. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  107. key1, key2, 86400), ==, 0);
  108. if (with_ed) {
  109. /* If we're making a CERTS cell for an ed handshake, let's make sure we
  110. * have some Ed25519 certificates and keys. */
  111. init_mock_ed_keys(key2);
  112. }
  113. /* c1 has started_here == 1 */
  114. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  115. c1->link_proto = 3;
  116. tt_int_op(connection_init_or_handshake_state(c1, 1), ==, 0);
  117. /* c2 has started_here == 0 */
  118. c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  119. c2->link_proto = 3;
  120. tt_int_op(connection_init_or_handshake_state(c2, 0), ==, 0);
  121. tt_int_op(0, ==, connection_or_send_certs_cell(c1));
  122. tt_assert(mock_got_var_cell);
  123. cell1 = mock_got_var_cell;
  124. tt_int_op(0, ==, connection_or_send_certs_cell(c2));
  125. tt_assert(mock_got_var_cell);
  126. cell2 = mock_got_var_cell;
  127. tt_int_op(cell1->command, ==, CELL_CERTS);
  128. tt_int_op(cell1->payload_len, >, 1);
  129. tt_int_op(cell2->command, ==, CELL_CERTS);
  130. tt_int_op(cell2->payload_len, >, 1);
  131. tt_int_op(cell1->payload_len, ==,
  132. certs_cell_parse(&cc1, cell1->payload, cell1->payload_len));
  133. tt_int_op(cell2->payload_len, ==,
  134. certs_cell_parse(&cc2, cell2->payload, cell2->payload_len));
  135. if (with_ed) {
  136. tt_int_op(5, ==, cc1->n_certs);
  137. tt_int_op(5, ==, cc2->n_certs);
  138. } else {
  139. tt_int_op(2, ==, cc1->n_certs);
  140. tt_int_op(2, ==, cc2->n_certs);
  141. }
  142. tt_int_op(certs_cell_get_certs(cc1, 0)->cert_type, ==,
  143. CERTTYPE_RSA1024_ID_AUTH);
  144. tt_int_op(certs_cell_get_certs(cc1, 1)->cert_type, ==,
  145. CERTTYPE_RSA1024_ID_ID);
  146. tt_int_op(certs_cell_get_certs(cc2, 0)->cert_type, ==,
  147. CERTTYPE_RSA1024_ID_LINK);
  148. tt_int_op(certs_cell_get_certs(cc2, 1)->cert_type, ==,
  149. CERTTYPE_RSA1024_ID_ID);
  150. if (with_ed) {
  151. tt_int_op(certs_cell_get_certs(cc1, 2)->cert_type, ==,
  152. CERTTYPE_ED_ID_SIGN);
  153. tt_int_op(certs_cell_get_certs(cc1, 3)->cert_type, ==,
  154. CERTTYPE_ED_SIGN_AUTH);
  155. tt_int_op(certs_cell_get_certs(cc1, 4)->cert_type, ==,
  156. CERTTYPE_RSA1024_ID_EDID);
  157. tt_int_op(certs_cell_get_certs(cc2, 2)->cert_type, ==,
  158. CERTTYPE_ED_ID_SIGN);
  159. tt_int_op(certs_cell_get_certs(cc2, 3)->cert_type, ==,
  160. CERTTYPE_ED_SIGN_LINK);
  161. tt_int_op(certs_cell_get_certs(cc2, 4)->cert_type, ==,
  162. CERTTYPE_RSA1024_ID_EDID);
  163. }
  164. chan1 = tor_malloc_zero(sizeof(*chan1));
  165. channel_tls_common_init(chan1);
  166. c1->chan = chan1;
  167. chan1->conn = c1;
  168. c1->base_.address = tor_strdup("C1");
  169. c1->tls = tor_tls_new(-1, 0);
  170. c1->link_proto = 4;
  171. c1->base_.conn_array_index = -1;
  172. crypto_pk_get_digest(key2, c1->identity_digest);
  173. if (with_ed) {
  174. const tor_x509_cert_t *linkc, *idc;
  175. tor_tls_get_my_certs(1, &linkc, &idc);
  176. mock_peer_cert_expect_tortls = c1->tls; /* We should see this tls... */
  177. mock_peer_cert = tor_x509_cert_dup(linkc); /* and when we do, the peer's
  178. * cert is this... */
  179. }
  180. channel_tls_process_certs_cell(cell2, chan1);
  181. mock_peer_cert_expect_tortls = NULL;
  182. mock_peer_cert = NULL;
  183. tor_assert(c1->handshake_state->authenticated);
  184. tt_assert(c1->handshake_state->received_certs_cell);
  185. tt_assert(c1->handshake_state->certs->auth_cert == NULL);
  186. tt_assert(c1->handshake_state->certs->ed_sign_auth == NULL);
  187. tt_assert(c1->handshake_state->certs->id_cert);
  188. if (with_ed) {
  189. tt_assert(c1->handshake_state->certs->ed_sign_link);
  190. tt_assert(c1->handshake_state->certs->ed_rsa_crosscert);
  191. tt_assert(c1->handshake_state->certs->ed_id_sign);
  192. tt_assert(c1->handshake_state->authenticated_rsa);
  193. tt_assert(c1->handshake_state->authenticated_ed25519);
  194. } else {
  195. tt_assert(c1->handshake_state->certs->ed_sign_link == NULL);
  196. tt_assert(c1->handshake_state->certs->ed_rsa_crosscert == NULL);
  197. tt_assert(c1->handshake_state->certs->ed_id_sign == NULL);
  198. tt_assert(c1->handshake_state->authenticated_rsa);
  199. tt_assert(! c1->handshake_state->authenticated_ed25519);
  200. }
  201. tt_assert(! tor_mem_is_zero(
  202. (char*)c1->handshake_state->authenticated_rsa_peer_id, 20));
  203. chan2 = tor_malloc_zero(sizeof(*chan2));
  204. channel_tls_common_init(chan2);
  205. c2->chan = chan2;
  206. chan2->conn = c2;
  207. c2->base_.address = tor_strdup("C2");
  208. c2->tls = tor_tls_new(-1, 1);
  209. c2->link_proto = 4;
  210. c2->base_.conn_array_index = -1;
  211. crypto_pk_get_digest(key1, c2->identity_digest);
  212. channel_tls_process_certs_cell(cell1, chan2);
  213. tt_assert(c2->handshake_state->received_certs_cell);
  214. if (with_ed) {
  215. tt_assert(c2->handshake_state->certs->ed_sign_auth);
  216. tt_assert(c2->handshake_state->certs->ed_rsa_crosscert);
  217. tt_assert(c2->handshake_state->certs->ed_id_sign);
  218. } else {
  219. tt_assert(c2->handshake_state->certs->auth_cert);
  220. tt_assert(c2->handshake_state->certs->ed_sign_auth == NULL);
  221. tt_assert(c2->handshake_state->certs->ed_rsa_crosscert == NULL);
  222. tt_assert(c2->handshake_state->certs->ed_id_sign == NULL);
  223. }
  224. tt_assert(c2->handshake_state->certs->id_cert);
  225. tt_assert(tor_mem_is_zero(
  226. (char*)c2->handshake_state->authenticated_rsa_peer_id, 20));
  227. /* no authentication has happened yet, since we haen't gotten an AUTH cell.
  228. */
  229. tt_assert(! c2->handshake_state->authenticated);
  230. tt_assert(! c2->handshake_state->authenticated_rsa);
  231. tt_assert(! c2->handshake_state->authenticated_ed25519);
  232. done:
  233. UNMOCK(tor_tls_cert_matches_key);
  234. UNMOCK(connection_or_write_var_cell_to_buf);
  235. UNMOCK(connection_or_send_netinfo);
  236. UNMOCK(tor_tls_get_peer_cert);
  237. memset(c1->identity_digest, 0, sizeof(c1->identity_digest));
  238. memset(c2->identity_digest, 0, sizeof(c2->identity_digest));
  239. connection_free_(TO_CONN(c1));
  240. connection_free_(TO_CONN(c2));
  241. tor_free(cell1);
  242. tor_free(cell2);
  243. certs_cell_free(cc1);
  244. certs_cell_free(cc2);
  245. if (chan1)
  246. circuitmux_free(chan1->base_.cmux);
  247. tor_free(chan1);
  248. if (chan2)
  249. circuitmux_free(chan2->base_.cmux);
  250. tor_free(chan2);
  251. crypto_pk_free(key1);
  252. crypto_pk_free(key2);
  253. }
  254. typedef struct certs_data_s {
  255. int is_ed;
  256. int is_link_cert;
  257. or_connection_t *c;
  258. channel_tls_t *chan;
  259. certs_cell_t *ccell;
  260. var_cell_t *cell;
  261. crypto_pk_t *key1, *key2;
  262. } certs_data_t;
  263. static int
  264. recv_certs_cleanup(const struct testcase_t *test, void *obj)
  265. {
  266. (void)test;
  267. certs_data_t *d = obj;
  268. UNMOCK(tor_tls_cert_matches_key);
  269. UNMOCK(connection_or_send_netinfo);
  270. UNMOCK(connection_or_close_for_error);
  271. UNMOCK(tor_tls_get_peer_cert);
  272. if (d) {
  273. tor_free(d->cell);
  274. certs_cell_free(d->ccell);
  275. connection_or_remove_from_identity_map(d->c);
  276. connection_free_(TO_CONN(d->c));
  277. circuitmux_free(d->chan->base_.cmux);
  278. tor_free(d->chan);
  279. crypto_pk_free(d->key1);
  280. crypto_pk_free(d->key2);
  281. tor_free(d);
  282. }
  283. routerkeys_free_all();
  284. return 1;
  285. }
  286. static void *
  287. recv_certs_setup(const struct testcase_t *test)
  288. {
  289. (void)test;
  290. certs_data_t *d = tor_malloc_zero(sizeof(*d));
  291. certs_cell_cert_t *ccc1 = NULL;
  292. certs_cell_cert_t *ccc2 = NULL;
  293. ssize_t n;
  294. int is_ed = d->is_ed = !strcmpstart(test->setup_data, "Ed25519");
  295. int is_rsa = !strcmpstart(test->setup_data, "RSA");
  296. int is_link = d->is_link_cert = !strcmpend(test->setup_data, "-Link");
  297. int is_auth = !strcmpend(test->setup_data, "-Auth");
  298. tor_assert(is_ed != is_rsa);
  299. tor_assert(is_link != is_auth);
  300. printf("rsa == %d; is_link == %d\n", is_rsa, is_link);
  301. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  302. d->chan = tor_malloc_zero(sizeof(*d->chan));
  303. d->c->chan = d->chan;
  304. d->c->base_.address = tor_strdup("HaveAnAddress");
  305. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  306. d->chan->conn = d->c;
  307. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  308. d->c->link_proto = 4;
  309. d->key1 = pk_generate(2);
  310. d->key2 = pk_generate(3);
  311. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  312. d->key1, d->key2, 86400), ==, 0);
  313. if (is_ed) {
  314. init_mock_ed_keys(d->key2);
  315. } else {
  316. routerkeys_free_all();
  317. }
  318. d->ccell = certs_cell_new();
  319. ccc1 = certs_cell_cert_new();
  320. certs_cell_add_certs(d->ccell, ccc1);
  321. ccc2 = certs_cell_cert_new();
  322. certs_cell_add_certs(d->ccell, ccc2);
  323. d->ccell->n_certs = 2;
  324. ccc1->cert_type = is_link ? 1 : 3;
  325. ccc2->cert_type = 2;
  326. const tor_x509_cert_t *a,*b;
  327. const uint8_t *enca, *encb;
  328. size_t lena, lenb;
  329. tor_tls_get_my_certs(is_link ? 1 : 0, &a, &b);
  330. tor_x509_cert_get_der(a, &enca, &lena);
  331. tor_x509_cert_get_der(b, &encb, &lenb);
  332. certs_cell_cert_setlen_body(ccc1, lena);
  333. ccc1->cert_len = lena;
  334. certs_cell_cert_setlen_body(ccc2, lenb);
  335. ccc2->cert_len = lenb;
  336. memcpy(certs_cell_cert_getarray_body(ccc1), enca, lena);
  337. memcpy(certs_cell_cert_getarray_body(ccc2), encb, lenb);
  338. if (is_ed) {
  339. certs_cell_cert_t *ccc3 = NULL; /* Id->Sign */
  340. certs_cell_cert_t *ccc4 = NULL; /* Sign->Link or Sign->Auth. */
  341. certs_cell_cert_t *ccc5 = NULL; /* RSAId->Ed Id. */
  342. const tor_cert_t *id_sign = get_master_signing_key_cert();
  343. const tor_cert_t *secondary =
  344. is_link ? get_current_link_cert_cert() : get_current_auth_key_cert();
  345. const uint8_t *cc = NULL;
  346. size_t cc_sz;
  347. get_master_rsa_crosscert(&cc, &cc_sz);
  348. ccc3 = certs_cell_cert_new();
  349. ccc4 = certs_cell_cert_new();
  350. ccc5 = certs_cell_cert_new();
  351. certs_cell_add_certs(d->ccell, ccc3);
  352. certs_cell_add_certs(d->ccell, ccc4);
  353. certs_cell_add_certs(d->ccell, ccc5);
  354. ccc3->cert_len = id_sign->encoded_len;
  355. ccc4->cert_len = secondary->encoded_len;
  356. ccc5->cert_len = cc_sz;
  357. certs_cell_cert_setlen_body(ccc3, ccc3->cert_len);
  358. certs_cell_cert_setlen_body(ccc4, ccc4->cert_len);
  359. certs_cell_cert_setlen_body(ccc5, ccc5->cert_len);
  360. memcpy(certs_cell_cert_getarray_body(ccc3), id_sign->encoded,
  361. ccc3->cert_len);
  362. memcpy(certs_cell_cert_getarray_body(ccc4), secondary->encoded,
  363. ccc4->cert_len);
  364. memcpy(certs_cell_cert_getarray_body(ccc5), cc, ccc5->cert_len);
  365. ccc3->cert_type = 4;
  366. ccc4->cert_type = is_link ? 5 : 6;
  367. ccc5->cert_type = 7;
  368. d->ccell->n_certs = 5;
  369. }
  370. d->cell = var_cell_new(4096);
  371. d->cell->command = CELL_CERTS;
  372. n = certs_cell_encode(d->cell->payload, 4096, d->ccell);
  373. tt_int_op(n, >, 0);
  374. d->cell->payload_len = n;
  375. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  376. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  377. MOCK(connection_or_close_for_error, mock_close_for_err);
  378. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  379. if (is_link) {
  380. /* Say that this is the peer's certificate */
  381. mock_peer_cert = tor_x509_cert_dup(a);
  382. }
  383. tt_int_op(0, ==, d->c->handshake_state->received_certs_cell);
  384. tt_int_op(0, ==, mock_send_authenticate_called);
  385. tt_int_op(0, ==, mock_send_netinfo_called);
  386. return d;
  387. done:
  388. recv_certs_cleanup(test, d);
  389. return NULL;
  390. }
  391. static struct testcase_setup_t setup_recv_certs = {
  392. .setup_fn = recv_certs_setup,
  393. .cleanup_fn = recv_certs_cleanup
  394. };
  395. static void
  396. test_link_handshake_recv_certs_ok(void *arg)
  397. {
  398. certs_data_t *d = arg;
  399. channel_tls_process_certs_cell(d->cell, d->chan);
  400. tt_int_op(0, ==, mock_close_called);
  401. tt_int_op(d->c->handshake_state->authenticated, ==, 1);
  402. tt_int_op(d->c->handshake_state->authenticated_rsa, ==, 1);
  403. tt_int_op(d->c->handshake_state->received_certs_cell, ==, 1);
  404. tt_assert(d->c->handshake_state->certs->id_cert != NULL);
  405. tt_assert(d->c->handshake_state->certs->auth_cert == NULL);
  406. if (d->is_ed) {
  407. tt_assert(d->c->handshake_state->certs->ed_id_sign != NULL);
  408. tt_assert(d->c->handshake_state->certs->ed_sign_link != NULL);
  409. tt_assert(d->c->handshake_state->certs->ed_sign_auth == NULL);
  410. tt_assert(d->c->handshake_state->certs->ed_rsa_crosscert != NULL);
  411. tt_int_op(d->c->handshake_state->authenticated_ed25519, ==, 1);
  412. } else {
  413. tt_assert(d->c->handshake_state->certs->ed_id_sign == NULL);
  414. tt_assert(d->c->handshake_state->certs->ed_sign_link == NULL);
  415. tt_assert(d->c->handshake_state->certs->ed_sign_auth == NULL);
  416. tt_assert(d->c->handshake_state->certs->ed_rsa_crosscert == NULL);
  417. tt_int_op(d->c->handshake_state->authenticated_ed25519, ==, 0);
  418. }
  419. done:
  420. ;
  421. }
  422. static void
  423. test_link_handshake_recv_certs_ok_server(void *arg)
  424. {
  425. certs_data_t *d = arg;
  426. d->c->handshake_state->started_here = 0;
  427. d->c->handshake_state->certs->started_here = 0;
  428. channel_tls_process_certs_cell(d->cell, d->chan);
  429. tt_int_op(0, ==, mock_close_called);
  430. tt_int_op(d->c->handshake_state->authenticated, ==, 0);
  431. tt_int_op(d->c->handshake_state->received_certs_cell, ==, 1);
  432. tt_assert(d->c->handshake_state->certs->id_cert != NULL);
  433. if (d->is_ed) {
  434. tt_assert(d->c->handshake_state->certs->ed_sign_auth != NULL);
  435. tt_assert(d->c->handshake_state->certs->auth_cert == NULL);
  436. } else {
  437. tt_assert(d->c->handshake_state->certs->ed_sign_auth == NULL);
  438. tt_assert(d->c->handshake_state->certs->auth_cert != NULL);
  439. }
  440. done:
  441. ;
  442. }
  443. #define CERTS_FAIL(name, code) \
  444. static void \
  445. test_link_handshake_recv_certs_ ## name(void *arg) \
  446. { \
  447. certs_data_t *d = arg; \
  448. const char *require_failure_message = NULL; \
  449. setup_capture_of_logs(LOG_INFO); \
  450. { code ; } \
  451. channel_tls_process_certs_cell(d->cell, d->chan); \
  452. tt_int_op(1, ==, mock_close_called); \
  453. tt_int_op(0, ==, mock_send_authenticate_called); \
  454. tt_int_op(0, ==, mock_send_netinfo_called); \
  455. if (require_failure_message) { \
  456. expect_log_msg_containing(require_failure_message); \
  457. } \
  458. done: \
  459. teardown_capture_of_logs(); \
  460. }
  461. CERTS_FAIL(badstate,
  462. require_failure_message = "We're not doing a v3 handshake!";
  463. d->c->base_.state = OR_CONN_STATE_CONNECTING;)
  464. CERTS_FAIL(badproto,
  465. require_failure_message = "not using link protocol >= 3";
  466. d->c->link_proto = 2)
  467. CERTS_FAIL(duplicate,
  468. require_failure_message = "We already got one";
  469. d->c->handshake_state->received_certs_cell = 1)
  470. CERTS_FAIL(already_authenticated,
  471. require_failure_message = "We're already authenticated!";
  472. d->c->handshake_state->authenticated = 1)
  473. CERTS_FAIL(empty,
  474. require_failure_message = "It had no body";
  475. d->cell->payload_len = 0)
  476. CERTS_FAIL(bad_circid,
  477. require_failure_message = "It had a nonzero circuit ID";
  478. d->cell->circ_id = 1)
  479. CERTS_FAIL(truncated_1,
  480. require_failure_message = "It couldn't be parsed";
  481. d->cell->payload[0] = 5)
  482. CERTS_FAIL(truncated_2,
  483. {
  484. require_failure_message = "It couldn't be parsed";
  485. d->cell->payload_len = 4;
  486. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);
  487. })
  488. CERTS_FAIL(truncated_3,
  489. {
  490. require_failure_message = "It couldn't be parsed";
  491. d->cell->payload_len = 7;
  492. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);
  493. })
  494. #define REENCODE() do { \
  495. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  496. tt_int_op(n, >, 0); \
  497. d->cell->payload_len = n; \
  498. } while (0)
  499. CERTS_FAIL(not_x509,
  500. {
  501. require_failure_message = "Received undecodable certificate";
  502. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  503. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  504. REENCODE();
  505. })
  506. CERTS_FAIL(both_link,
  507. {
  508. require_failure_message = "Duplicate x509 certificate";
  509. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  510. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  511. REENCODE();
  512. })
  513. CERTS_FAIL(both_id_rsa,
  514. {
  515. require_failure_message = "Duplicate x509 certificate";
  516. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  517. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  518. REENCODE();
  519. })
  520. CERTS_FAIL(both_auth,
  521. {
  522. require_failure_message = "Duplicate x509 certificate";
  523. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  524. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  525. REENCODE();
  526. })
  527. CERTS_FAIL(wrong_labels_1,
  528. {
  529. require_failure_message = "The link certificate was not valid";
  530. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  531. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  532. REENCODE();
  533. })
  534. CERTS_FAIL(wrong_labels_2,
  535. {
  536. const tor_x509_cert_t *a;
  537. const tor_x509_cert_t *b;
  538. const uint8_t *enca;
  539. size_t lena;
  540. require_failure_message = "The link certificate was not valid";
  541. tor_tls_get_my_certs(1, &a, &b);
  542. tor_x509_cert_get_der(a, &enca, &lena);
  543. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  544. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  545. enca, lena);
  546. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  547. REENCODE();
  548. })
  549. CERTS_FAIL(wrong_labels_3,
  550. {
  551. require_failure_message = "The certs we wanted were missing";
  552. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  553. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  554. REENCODE();
  555. })
  556. CERTS_FAIL(server_missing_certs,
  557. {
  558. require_failure_message = "The certs we wanted were missing";
  559. d->c->handshake_state->started_here = 0;
  560. d->c->handshake_state->certs->started_here = 0;
  561. })
  562. CERTS_FAIL(server_wrong_labels_1,
  563. {
  564. require_failure_message =
  565. "The authentication certificate was not valid";
  566. d->c->handshake_state->started_here = 0;
  567. d->c->handshake_state->certs->started_here = 0;
  568. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  569. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  570. REENCODE();
  571. })
  572. static void
  573. test_link_handshake_send_authchallenge(void *arg)
  574. {
  575. (void)arg;
  576. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  577. var_cell_t *cell1=NULL, *cell2=NULL;
  578. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  579. tt_int_op(connection_init_or_handshake_state(c1, 0), ==, 0);
  580. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  581. tt_assert(! mock_got_var_cell);
  582. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  583. cell1 = mock_got_var_cell;
  584. tt_int_op(0, ==, connection_or_send_auth_challenge_cell(c1));
  585. cell2 = mock_got_var_cell;
  586. tt_int_op(40, ==, cell1->payload_len);
  587. tt_int_op(40, ==, cell2->payload_len);
  588. tt_int_op(0, ==, cell1->circ_id);
  589. tt_int_op(0, ==, cell2->circ_id);
  590. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell1->command);
  591. tt_int_op(CELL_AUTH_CHALLENGE, ==, cell2->command);
  592. tt_mem_op("\x00\x03\x00\x01\x00\x02\x00\x03", ==, cell1->payload + 32, 8);
  593. tt_mem_op("\x00\x03\x00\x01\x00\x02\x00\x03", ==, cell2->payload + 32, 8);
  594. tt_mem_op(cell1->payload, !=, cell2->payload, 32);
  595. done:
  596. UNMOCK(connection_or_write_var_cell_to_buf);
  597. connection_free_(TO_CONN(c1));
  598. tor_free(cell1);
  599. tor_free(cell2);
  600. }
  601. typedef struct authchallenge_data_s {
  602. or_connection_t *c;
  603. channel_tls_t *chan;
  604. var_cell_t *cell;
  605. } authchallenge_data_t;
  606. static int
  607. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  608. {
  609. (void)test;
  610. authchallenge_data_t *d = obj;
  611. UNMOCK(connection_or_send_netinfo);
  612. UNMOCK(connection_or_close_for_error);
  613. UNMOCK(connection_or_send_authenticate_cell);
  614. if (d) {
  615. tor_free(d->cell);
  616. connection_free_(TO_CONN(d->c));
  617. circuitmux_free(d->chan->base_.cmux);
  618. tor_free(d->chan);
  619. tor_free(d);
  620. }
  621. return 1;
  622. }
  623. static void *
  624. recv_authchallenge_setup(const struct testcase_t *test)
  625. {
  626. (void)test;
  627. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  628. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  629. d->chan = tor_malloc_zero(sizeof(*d->chan));
  630. d->c->chan = d->chan;
  631. d->c->base_.address = tor_strdup("HaveAnAddress");
  632. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  633. d->chan->conn = d->c;
  634. tt_int_op(connection_init_or_handshake_state(d->c, 1), ==, 0);
  635. d->c->link_proto = 4;
  636. d->c->handshake_state->received_certs_cell = 1;
  637. d->cell = var_cell_new(128);
  638. d->cell->payload_len = 38;
  639. d->cell->payload[33] = 2; /* 2 methods */
  640. d->cell->payload[35] = 7; /* This one isn't real */
  641. d->cell->payload[37] = 1; /* This is the old RSA one. */
  642. d->cell->command = CELL_AUTH_CHALLENGE;
  643. get_options_mutable()->ORPort_set = 1;
  644. MOCK(connection_or_close_for_error, mock_close_for_err);
  645. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  646. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  647. tt_int_op(0, ==, d->c->handshake_state->received_auth_challenge);
  648. tt_int_op(0, ==, mock_send_authenticate_called);
  649. tt_int_op(0, ==, mock_send_netinfo_called);
  650. return d;
  651. done:
  652. recv_authchallenge_cleanup(test, d);
  653. return NULL;
  654. }
  655. static struct testcase_setup_t setup_recv_authchallenge = {
  656. .setup_fn = recv_authchallenge_setup,
  657. .cleanup_fn = recv_authchallenge_cleanup
  658. };
  659. static void
  660. test_link_handshake_recv_authchallenge_ok(void *arg)
  661. {
  662. authchallenge_data_t *d = arg;
  663. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  664. tt_int_op(0, ==, mock_close_called);
  665. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  666. tt_int_op(1, ==, mock_send_authenticate_called);
  667. tt_int_op(1, ==, mock_send_netinfo_called);
  668. tt_int_op(1, ==, mock_send_authenticate_called_with_type); /* RSA */
  669. done:
  670. ;
  671. }
  672. static void
  673. test_link_handshake_recv_authchallenge_ok_ed25519(void *arg)
  674. {
  675. authchallenge_data_t *d = arg;
  676. /* Add the ed25519 authentication mechanism here. */
  677. d->cell->payload[33] = 3; /* 3 types are supported now. */
  678. d->cell->payload[39] = 3;
  679. d->cell->payload_len += 2;
  680. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  681. tt_int_op(0, ==, mock_close_called);
  682. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  683. tt_int_op(1, ==, mock_send_authenticate_called);
  684. tt_int_op(1, ==, mock_send_netinfo_called);
  685. tt_int_op(3, ==, mock_send_authenticate_called_with_type); /* Ed25519 */
  686. done:
  687. ;
  688. }
  689. static void
  690. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  691. {
  692. authchallenge_data_t *d = arg;
  693. get_options_mutable()->ORPort_set = 0;
  694. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  695. tt_int_op(0, ==, mock_close_called);
  696. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  697. tt_int_op(0, ==, mock_send_authenticate_called);
  698. tt_int_op(0, ==, mock_send_netinfo_called);
  699. done:
  700. ;
  701. }
  702. static void
  703. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  704. {
  705. authchallenge_data_t *d = arg;
  706. d->cell->payload[37] = 99;
  707. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  708. tt_int_op(0, ==, mock_close_called);
  709. tt_int_op(1, ==, d->c->handshake_state->received_auth_challenge);
  710. tt_int_op(0, ==, mock_send_authenticate_called);
  711. tt_int_op(1, ==, mock_send_netinfo_called);
  712. done:
  713. ;
  714. }
  715. #define AUTHCHALLENGE_FAIL(name, code) \
  716. static void \
  717. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  718. { \
  719. authchallenge_data_t *d = arg; \
  720. const char *require_failure_message = NULL; \
  721. setup_capture_of_logs(LOG_INFO); \
  722. { code ; } \
  723. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  724. tt_int_op(1, ==, mock_close_called); \
  725. tt_int_op(0, ==, mock_send_authenticate_called); \
  726. tt_int_op(0, ==, mock_send_netinfo_called); \
  727. if (require_failure_message) { \
  728. expect_log_msg_containing(require_failure_message); \
  729. } \
  730. done: \
  731. teardown_capture_of_logs(); \
  732. }
  733. AUTHCHALLENGE_FAIL(badstate,
  734. require_failure_message = "We're not currently doing a "
  735. "v3 handshake";
  736. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  737. AUTHCHALLENGE_FAIL(badproto,
  738. require_failure_message = "not using link protocol >= 3";
  739. d->c->link_proto = 2)
  740. AUTHCHALLENGE_FAIL(as_server,
  741. require_failure_message = "We didn't originate this "
  742. "connection";
  743. d->c->handshake_state->started_here = 0;
  744. d->c->handshake_state->certs->started_here = 0;)
  745. AUTHCHALLENGE_FAIL(duplicate,
  746. require_failure_message = "We already received one";
  747. d->c->handshake_state->received_auth_challenge = 1)
  748. AUTHCHALLENGE_FAIL(nocerts,
  749. require_failure_message = "We haven't gotten a CERTS "
  750. "cell yet";
  751. d->c->handshake_state->received_certs_cell = 0)
  752. AUTHCHALLENGE_FAIL(tooshort,
  753. require_failure_message = "It was not well-formed";
  754. d->cell->payload_len = 33)
  755. AUTHCHALLENGE_FAIL(truncated,
  756. require_failure_message = "It was not well-formed";
  757. d->cell->payload_len = 34)
  758. AUTHCHALLENGE_FAIL(nonzero_circid,
  759. require_failure_message = "It had a nonzero circuit ID";
  760. d->cell->circ_id = 1337)
  761. static int
  762. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  763. {
  764. (void)tls;
  765. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  766. return 0;
  767. }
  768. static void
  769. mock_set_circid_type(channel_t *chan,
  770. crypto_pk_t *identity_rcvd,
  771. int consider_identity)
  772. {
  773. (void) chan;
  774. (void) identity_rcvd;
  775. (void) consider_identity;
  776. }
  777. typedef struct authenticate_data_s {
  778. int is_ed;
  779. or_connection_t *c1, *c2;
  780. channel_tls_t *chan2;
  781. var_cell_t *cell;
  782. crypto_pk_t *key1, *key2;
  783. } authenticate_data_t;
  784. static int
  785. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  786. {
  787. (void) test;
  788. UNMOCK(connection_or_write_var_cell_to_buf);
  789. UNMOCK(tor_tls_get_peer_cert);
  790. UNMOCK(tor_tls_get_tlssecrets);
  791. UNMOCK(connection_or_close_for_error);
  792. UNMOCK(channel_set_circid_type);
  793. UNMOCK(tor_tls_export_key_material);
  794. authenticate_data_t *d = arg;
  795. if (d) {
  796. tor_free(d->cell);
  797. connection_or_remove_from_identity_map(d->c1);
  798. connection_or_remove_from_identity_map(d->c2);
  799. connection_free_(TO_CONN(d->c1));
  800. connection_free_(TO_CONN(d->c2));
  801. circuitmux_free(d->chan2->base_.cmux);
  802. tor_free(d->chan2);
  803. crypto_pk_free(d->key1);
  804. crypto_pk_free(d->key2);
  805. tor_free(d);
  806. }
  807. mock_peer_cert = NULL;
  808. return 1;
  809. }
  810. static void *
  811. authenticate_data_setup(const struct testcase_t *test)
  812. {
  813. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  814. int is_ed = d->is_ed = (test->setup_data == (void*)3);
  815. scheduler_init();
  816. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  817. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  818. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  819. MOCK(connection_or_close_for_error, mock_close_for_err);
  820. MOCK(channel_set_circid_type, mock_set_circid_type);
  821. MOCK(tor_tls_export_key_material, mock_export_key_material);
  822. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  823. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  824. tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
  825. tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
  826. d->key1 = pk_generate(2);
  827. d->key2 = pk_generate(3);
  828. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  829. d->key1, d->key2, 86400), ==, 0);
  830. init_mock_ed_keys(d->key2);
  831. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  832. d->c1->link_proto = 3;
  833. tt_int_op(connection_init_or_handshake_state(d->c1, 1), ==, 0);
  834. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  835. d->c2->link_proto = 3;
  836. tt_int_op(connection_init_or_handshake_state(d->c2, 0), ==, 0);
  837. var_cell_t *cell = var_cell_new(16);
  838. cell->command = CELL_CERTS;
  839. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  840. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  841. memset(cell->payload, 0xf0, 16);
  842. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  843. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  844. tor_free(cell);
  845. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  846. channel_tls_common_init(d->chan2);
  847. d->c2->chan = d->chan2;
  848. d->chan2->conn = d->c2;
  849. d->c2->base_.address = tor_strdup("C2");
  850. d->c2->tls = tor_tls_new(-1, 1);
  851. d->c2->handshake_state->received_certs_cell = 1;
  852. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  853. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  854. const uint8_t *der;
  855. size_t sz;
  856. tor_x509_cert_get_der(id_cert, &der, &sz);
  857. d->c1->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  858. d->c2->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  859. if (is_ed) {
  860. d->c1->handshake_state->certs->ed_id_sign =
  861. tor_cert_dup(get_master_signing_key_cert());
  862. d->c2->handshake_state->certs->ed_id_sign =
  863. tor_cert_dup(get_master_signing_key_cert());
  864. d->c2->handshake_state->certs->ed_sign_auth =
  865. tor_cert_dup(get_current_auth_key_cert());
  866. } else {
  867. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  868. tor_x509_cert_get_der(auth_cert, &der, &sz);
  869. d->c2->handshake_state->certs->auth_cert = tor_x509_cert_decode(der, sz);
  870. }
  871. tor_x509_cert_get_der(link_cert, &der, &sz);
  872. mock_peer_cert = tor_x509_cert_decode(der, sz);
  873. tt_assert(mock_peer_cert);
  874. /* Make an authenticate cell ... */
  875. int authtype;
  876. if (is_ed)
  877. authtype = AUTHTYPE_ED25519_SHA256_RFC5705;
  878. else
  879. authtype = AUTHTYPE_RSA_SHA256_TLSSECRET;
  880. tt_int_op(0, ==, connection_or_send_authenticate_cell(d->c1, authtype));
  881. tt_assert(mock_got_var_cell);
  882. d->cell = mock_got_var_cell;
  883. mock_got_var_cell = NULL;
  884. return d;
  885. done:
  886. authenticate_data_cleanup(test, d);
  887. return NULL;
  888. }
  889. static struct testcase_setup_t setup_authenticate = {
  890. .setup_fn = authenticate_data_setup,
  891. .cleanup_fn = authenticate_data_cleanup
  892. };
  893. static void
  894. test_link_handshake_auth_cell(void *arg)
  895. {
  896. authenticate_data_t *d = arg;
  897. auth1_t *auth1 = NULL;
  898. crypto_pk_t *auth_pubkey = NULL;
  899. /* Is the cell well-formed on the outer layer? */
  900. tt_int_op(d->cell->command, ==, CELL_AUTHENTICATE);
  901. tt_int_op(d->cell->payload[0], ==, 0);
  902. if (d->is_ed)
  903. tt_int_op(d->cell->payload[1], ==, 3);
  904. else
  905. tt_int_op(d->cell->payload[1], ==, 1);
  906. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), ==,
  907. d->cell->payload_len - 4);
  908. /* Check it out for plausibility... */
  909. auth_ctx_t ctx;
  910. ctx.is_ed = d->is_ed;
  911. tt_int_op(d->cell->payload_len-4, ==, auth1_parse(&auth1,
  912. d->cell->payload+4,
  913. d->cell->payload_len - 4, &ctx));
  914. tt_assert(auth1);
  915. if (d->is_ed) {
  916. tt_mem_op(auth1->type, ==, "AUTH0003", 8);
  917. } else {
  918. tt_mem_op(auth1->type, ==, "AUTH0001", 8);
  919. }
  920. tt_mem_op(auth1->tlssecrets, ==, "int getRandomNumber(){return 4;}", 32);
  921. /* Is the signature okay? */
  922. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  923. if (d->is_ed) {
  924. ed25519_signature_t sig;
  925. tt_int_op(auth1_getlen_sig(auth1), ==, ED25519_SIG_LEN);
  926. memcpy(&sig.sig, auth1_getarray_sig(auth1), ED25519_SIG_LEN);
  927. tt_assert(!ed25519_checksig(&sig, start, end-start,
  928. &get_current_auth_keypair()->pubkey));
  929. } else {
  930. uint8_t sig[128];
  931. uint8_t digest[32];
  932. tt_int_op(auth1_getlen_sig(auth1), >, 120);
  933. auth_pubkey = tor_tls_cert_get_key(d->c2->handshake_state->certs->auth_cert);
  934. int n = crypto_pk_public_checksig(
  935. auth_pubkey,
  936. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  937. auth1_getlen_sig(auth1));
  938. tt_int_op(n, ==, 32);
  939. crypto_digest256((char*)digest,
  940. (const char*)start, end-start, DIGEST_SHA256);
  941. tt_mem_op(sig, ==, digest, 32);
  942. }
  943. /* Then feed it to c2. */
  944. tt_int_op(d->c2->handshake_state->authenticated, ==, 0);
  945. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  946. tt_int_op(mock_close_called, ==, 0);
  947. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  948. if (d->is_ed) {
  949. tt_int_op(d->c2->handshake_state->authenticated_ed25519, ==, 1);
  950. tt_int_op(d->c2->handshake_state->authenticated_rsa, ==, 1);
  951. } else {
  952. tt_int_op(d->c2->handshake_state->authenticated_ed25519, ==, 0);
  953. tt_int_op(d->c2->handshake_state->authenticated_rsa, ==, 1);
  954. }
  955. done:
  956. auth1_free(auth1);
  957. crypto_pk_free(auth_pubkey);
  958. }
  959. #define AUTHENTICATE_FAIL(name, code) \
  960. static void \
  961. test_link_handshake_auth_ ## name(void *arg) \
  962. { \
  963. authenticate_data_t *d = arg; \
  964. const char *require_failure_message = NULL; \
  965. setup_capture_of_logs(LOG_INFO); \
  966. { code ; } \
  967. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  968. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  969. tt_int_op(mock_close_called, ==, 1); \
  970. tt_int_op(d->c2->handshake_state->authenticated, ==, 0); \
  971. if (require_failure_message) { \
  972. expect_log_msg_containing(require_failure_message); \
  973. } \
  974. done: \
  975. teardown_capture_of_logs(); \
  976. }
  977. AUTHENTICATE_FAIL(badstate,
  978. require_failure_message = "We're not doing a v3 handshake";
  979. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  980. AUTHENTICATE_FAIL(badproto,
  981. require_failure_message = "not using link protocol >= 3";
  982. d->c2->link_proto = 2)
  983. AUTHENTICATE_FAIL(atclient,
  984. require_failure_message = "We originated this connection";
  985. d->c2->handshake_state->started_here = 1;
  986. d->c2->handshake_state->certs->started_here = 1;)
  987. AUTHENTICATE_FAIL(duplicate,
  988. require_failure_message = "We already got one";
  989. d->c2->handshake_state->received_authenticate = 1)
  990. static void
  991. test_link_handshake_auth_already_authenticated(void *arg)
  992. {
  993. authenticate_data_t *d = arg;
  994. setup_capture_of_logs(LOG_INFO);
  995. d->c2->handshake_state->authenticated = 1;
  996. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  997. tt_int_op(mock_close_called, ==, 1);
  998. tt_int_op(d->c2->handshake_state->authenticated, ==, 1);
  999. expect_log_msg_containing("The peer is already authenticated");
  1000. done:
  1001. teardown_capture_of_logs();
  1002. }
  1003. AUTHENTICATE_FAIL(nocerts,
  1004. require_failure_message = "We never got a certs cell";
  1005. d->c2->handshake_state->received_certs_cell = 0)
  1006. AUTHENTICATE_FAIL(noidcert,
  1007. require_failure_message = "We never got an identity "
  1008. "certificate";
  1009. tor_x509_cert_free(d->c2->handshake_state->certs->id_cert);
  1010. d->c2->handshake_state->certs->id_cert = NULL)
  1011. AUTHENTICATE_FAIL(noauthcert,
  1012. require_failure_message = "We never got an RSA "
  1013. "authentication certificate";
  1014. tor_x509_cert_free(d->c2->handshake_state->certs->auth_cert);
  1015. d->c2->handshake_state->certs->auth_cert = NULL)
  1016. AUTHENTICATE_FAIL(tooshort,
  1017. require_failure_message = "Cell was way too short";
  1018. d->cell->payload_len = 3)
  1019. AUTHENTICATE_FAIL(badtype,
  1020. require_failure_message = "Authenticator type was not "
  1021. "recognized";
  1022. d->cell->payload[0] = 0xff)
  1023. AUTHENTICATE_FAIL(truncated_1,
  1024. require_failure_message = "Authenticator was truncated";
  1025. d->cell->payload[2]++)
  1026. AUTHENTICATE_FAIL(truncated_2,
  1027. require_failure_message = "Authenticator was truncated";
  1028. d->cell->payload[3]++)
  1029. AUTHENTICATE_FAIL(tooshort_1,
  1030. require_failure_message = "Authenticator was too short";
  1031. tt_int_op(d->cell->payload_len, >=, 260);
  1032. d->cell->payload[2] -= 1;
  1033. d->cell->payload_len -= 256;)
  1034. AUTHENTICATE_FAIL(badcontent,
  1035. require_failure_message = "Some field in the AUTHENTICATE "
  1036. "cell body was not as expected";
  1037. d->cell->payload[10] ^= 0xff)
  1038. AUTHENTICATE_FAIL(badsig_1,
  1039. require_failure_message = "RSA signature wasn't valid";
  1040. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  1041. #define TEST_RSA(name, flags) \
  1042. { #name , test_link_handshake_ ## name, (flags), \
  1043. &passthrough_setup, (void*)"RSA" }
  1044. #define TEST_ED(name, flags) \
  1045. { #name "_ed25519" , test_link_handshake_ ## name, (flags), \
  1046. &passthrough_setup, (void*)"Ed25519" }
  1047. #define TEST_RCV_AUTHCHALLENGE(name) \
  1048. { "recv_authchallenge/" #name , \
  1049. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  1050. &setup_recv_authchallenge, NULL }
  1051. #define TEST_RCV_CERTS(name) \
  1052. { "recv_certs/" #name , \
  1053. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1054. &setup_recv_certs, (void*)"RSA-Link" }
  1055. #define TEST_RCV_CERTS_RSA(name,type) \
  1056. { "recv_certs/" #name , \
  1057. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1058. &setup_recv_certs, (void*)type }
  1059. #define TEST_RCV_CERTS_ED(name, type) \
  1060. { "recv_certs/" #name "_ed25519", \
  1061. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1062. &setup_recv_certs, (void*)type }
  1063. #define TEST_AUTHENTICATE(name) \
  1064. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  1065. &setup_authenticate, NULL }
  1066. #define TEST_AUTHENTICATE_ED(name) \
  1067. { "authenticate/" #name "_ed25519" , test_link_handshake_auth_ ## name, \
  1068. TT_FORK, &setup_authenticate, (void*)3 }
  1069. struct testcase_t link_handshake_tests[] = {
  1070. TEST_RSA(certs_ok, TT_FORK),
  1071. TEST_ED(certs_ok, TT_FORK),
  1072. TEST_RCV_CERTS(ok),
  1073. TEST_RCV_CERTS_ED(ok, "Ed25519-Link"),
  1074. TEST_RCV_CERTS_RSA(ok_server, "RSA-Auth"),
  1075. TEST_RCV_CERTS_ED(ok_server, "Ed25519-Auth"),
  1076. TEST_RCV_CERTS(badstate),
  1077. TEST_RCV_CERTS(badproto),
  1078. TEST_RCV_CERTS(duplicate),
  1079. TEST_RCV_CERTS(already_authenticated),
  1080. TEST_RCV_CERTS(empty),
  1081. TEST_RCV_CERTS(bad_circid),
  1082. TEST_RCV_CERTS(truncated_1),
  1083. TEST_RCV_CERTS(truncated_2),
  1084. TEST_RCV_CERTS(truncated_3),
  1085. TEST_RCV_CERTS(not_x509),
  1086. TEST_RCV_CERTS(both_link),
  1087. TEST_RCV_CERTS(both_id_rsa),
  1088. TEST_RCV_CERTS(both_auth),
  1089. TEST_RCV_CERTS(wrong_labels_1),
  1090. TEST_RCV_CERTS(wrong_labels_2),
  1091. TEST_RCV_CERTS(wrong_labels_3),
  1092. TEST_RCV_CERTS(server_missing_certs),
  1093. TEST_RCV_CERTS(server_wrong_labels_1),
  1094. TEST_RSA(send_authchallenge, TT_FORK),
  1095. TEST_RCV_AUTHCHALLENGE(ok),
  1096. TEST_RCV_AUTHCHALLENGE(ok_ed25519),
  1097. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  1098. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  1099. TEST_RCV_AUTHCHALLENGE(badstate),
  1100. TEST_RCV_AUTHCHALLENGE(badproto),
  1101. TEST_RCV_AUTHCHALLENGE(as_server),
  1102. TEST_RCV_AUTHCHALLENGE(duplicate),
  1103. TEST_RCV_AUTHCHALLENGE(nocerts),
  1104. TEST_RCV_AUTHCHALLENGE(tooshort),
  1105. TEST_RCV_AUTHCHALLENGE(truncated),
  1106. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  1107. TEST_AUTHENTICATE(cell),
  1108. TEST_AUTHENTICATE_ED(cell),
  1109. TEST_AUTHENTICATE(badstate),
  1110. TEST_AUTHENTICATE(badproto),
  1111. TEST_AUTHENTICATE(atclient),
  1112. TEST_AUTHENTICATE(duplicate),
  1113. TEST_AUTHENTICATE(already_authenticated),
  1114. TEST_AUTHENTICATE(nocerts),
  1115. TEST_AUTHENTICATE(noidcert),
  1116. TEST_AUTHENTICATE(noauthcert),
  1117. TEST_AUTHENTICATE(tooshort),
  1118. TEST_AUTHENTICATE(badtype),
  1119. TEST_AUTHENTICATE(truncated_1),
  1120. TEST_AUTHENTICATE(truncated_2),
  1121. TEST_AUTHENTICATE(tooshort_1),
  1122. TEST_AUTHENTICATE(badcontent),
  1123. TEST_AUTHENTICATE(badsig_1),
  1124. //TEST_AUTHENTICATE(),
  1125. END_OF_TESTCASES
  1126. };