test_link_handshake.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  1. /* Copyright (c) 2014-2018, 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. #define TORTLS_PRIVATE
  8. #include "or/or.h"
  9. #include "or/config.h"
  10. #include "or/connection.h"
  11. #include "or/connection_or.h"
  12. #include "or/channeltls.h"
  13. #include "trunnel/link_handshake.h"
  14. #include "or/router.h"
  15. #include "or/routerkeys.h"
  16. #include "or/scheduler.h"
  17. #include "or/torcert.h"
  18. #include "or/or_connection_st.h"
  19. #include "or/or_handshake_certs_st.h"
  20. #include "or/or_handshake_state_st.h"
  21. #include "or/var_cell_st.h"
  22. #include "test/test.h"
  23. #include "test/log_test_helpers.h"
  24. static var_cell_t *mock_got_var_cell = NULL;
  25. static void
  26. mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
  27. {
  28. (void)conn;
  29. var_cell_t *newcell = var_cell_new(vc->payload_len);
  30. memcpy(newcell, vc, sizeof(var_cell_t));
  31. memcpy(newcell->payload, vc->payload, vc->payload_len);
  32. mock_got_var_cell = newcell;
  33. }
  34. static int
  35. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  36. {
  37. (void) tls;
  38. (void) cert; // XXXX look at this.
  39. return 1;
  40. }
  41. static tor_tls_t *mock_peer_cert_expect_tortls = NULL;
  42. static tor_x509_cert_t *mock_peer_cert = NULL;
  43. static tor_x509_cert_t *
  44. mock_get_peer_cert(tor_tls_t *tls)
  45. {
  46. if (mock_peer_cert_expect_tortls &&
  47. mock_peer_cert_expect_tortls != tls)
  48. return NULL;
  49. return tor_x509_cert_dup(mock_peer_cert);
  50. }
  51. static int mock_send_netinfo_called = 0;
  52. static int
  53. mock_send_netinfo(or_connection_t *conn)
  54. {
  55. (void) conn;
  56. ++mock_send_netinfo_called;// XXX check_this
  57. return 0;
  58. }
  59. static int mock_close_called = 0;
  60. static void
  61. mock_close_for_err(or_connection_t *orconn, int flush)
  62. {
  63. (void)orconn;
  64. (void)flush;
  65. ++mock_close_called;
  66. }
  67. static int mock_send_authenticate_called = 0;
  68. static int mock_send_authenticate_called_with_type = 0;
  69. static int
  70. mock_send_authenticate(or_connection_t *conn, int type)
  71. {
  72. (void) conn;
  73. mock_send_authenticate_called_with_type = type;
  74. ++mock_send_authenticate_called;// XXX check_this
  75. return 0;
  76. }
  77. static int
  78. mock_export_key_material(tor_tls_t *tls, uint8_t *secrets_out,
  79. const uint8_t *context,
  80. size_t context_len,
  81. const char *label)
  82. {
  83. (void) tls;
  84. (void)secrets_out;
  85. (void)context;
  86. (void)context_len;
  87. (void)label;
  88. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  89. return 0;
  90. }
  91. static tor_x509_cert_t *mock_own_cert = NULL;
  92. static tor_x509_cert_t *
  93. mock_get_own_cert(tor_tls_t *tls)
  94. {
  95. (void)tls;
  96. return tor_x509_cert_dup(mock_own_cert);
  97. }
  98. /* Test good certs cells */
  99. static void
  100. test_link_handshake_certs_ok(void *arg)
  101. {
  102. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  103. or_connection_t *c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  104. var_cell_t *cell1 = NULL, *cell2 = NULL;
  105. certs_cell_t *cc1 = NULL, *cc2 = NULL;
  106. channel_tls_t *chan1 = NULL, *chan2 = NULL;
  107. crypto_pk_t *key1 = NULL, *key2 = NULL;
  108. const int with_ed = !strcmp((const char *)arg, "Ed25519");
  109. tor_addr_from_ipv4h(&c1->base_.addr, 0x7f000001);
  110. tor_addr_from_ipv4h(&c2->base_.addr, 0x7f000001);
  111. scheduler_init();
  112. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  113. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  114. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  115. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  116. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  117. key1 = pk_generate(2);
  118. key2 = pk_generate(3);
  119. /* We need to make sure that our TLS certificates are set up before we can
  120. * actually generate a CERTS cell.
  121. */
  122. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  123. key1, key2, 86400), OP_EQ, 0);
  124. if (with_ed) {
  125. /* If we're making a CERTS cell for an ed handshake, let's make sure we
  126. * have some Ed25519 certificates and keys. */
  127. init_mock_ed_keys(key2);
  128. } else {
  129. certs_cell_ed25519_disabled_for_testing = 1;
  130. }
  131. /* c1 has started_here == 1 */
  132. {
  133. const tor_x509_cert_t *link_cert = NULL;
  134. tt_assert(!tor_tls_get_my_certs(1, &link_cert, NULL));
  135. mock_own_cert = tor_x509_cert_dup(link_cert);
  136. }
  137. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  138. c1->link_proto = 3;
  139. tt_int_op(connection_init_or_handshake_state(c1, 1), OP_EQ, 0);
  140. /* c2 has started_here == 0 */
  141. c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  142. c2->link_proto = 3;
  143. tt_int_op(connection_init_or_handshake_state(c2, 0), OP_EQ, 0);
  144. tt_int_op(0, OP_EQ, connection_or_send_certs_cell(c1));
  145. tt_assert(mock_got_var_cell);
  146. cell1 = mock_got_var_cell;
  147. tt_int_op(0, OP_EQ, connection_or_send_certs_cell(c2));
  148. tt_assert(mock_got_var_cell);
  149. cell2 = mock_got_var_cell;
  150. tt_int_op(cell1->command, OP_EQ, CELL_CERTS);
  151. tt_int_op(cell1->payload_len, OP_GT, 1);
  152. tt_int_op(cell2->command, OP_EQ, CELL_CERTS);
  153. tt_int_op(cell2->payload_len, OP_GT, 1);
  154. tt_int_op(cell1->payload_len, OP_EQ,
  155. certs_cell_parse(&cc1, cell1->payload, cell1->payload_len));
  156. tt_int_op(cell2->payload_len, OP_EQ,
  157. certs_cell_parse(&cc2, cell2->payload, cell2->payload_len));
  158. if (with_ed) {
  159. tt_int_op(5, OP_EQ, cc1->n_certs);
  160. tt_int_op(5, OP_EQ, cc2->n_certs);
  161. } else {
  162. tt_int_op(2, OP_EQ, cc1->n_certs);
  163. tt_int_op(2, OP_EQ, cc2->n_certs);
  164. }
  165. tt_int_op(certs_cell_get_certs(cc1, 0)->cert_type, OP_EQ,
  166. CERTTYPE_RSA1024_ID_AUTH);
  167. tt_int_op(certs_cell_get_certs(cc1, 1)->cert_type, OP_EQ,
  168. CERTTYPE_RSA1024_ID_ID);
  169. tt_int_op(certs_cell_get_certs(cc2, 0)->cert_type, OP_EQ,
  170. CERTTYPE_RSA1024_ID_LINK);
  171. tt_int_op(certs_cell_get_certs(cc2, 1)->cert_type, OP_EQ,
  172. CERTTYPE_RSA1024_ID_ID);
  173. if (with_ed) {
  174. tt_int_op(certs_cell_get_certs(cc1, 2)->cert_type, OP_EQ,
  175. CERTTYPE_ED_ID_SIGN);
  176. tt_int_op(certs_cell_get_certs(cc1, 3)->cert_type, OP_EQ,
  177. CERTTYPE_ED_SIGN_AUTH);
  178. tt_int_op(certs_cell_get_certs(cc1, 4)->cert_type, OP_EQ,
  179. CERTTYPE_RSA1024_ID_EDID);
  180. tt_int_op(certs_cell_get_certs(cc2, 2)->cert_type, OP_EQ,
  181. CERTTYPE_ED_ID_SIGN);
  182. tt_int_op(certs_cell_get_certs(cc2, 3)->cert_type, OP_EQ,
  183. CERTTYPE_ED_SIGN_LINK);
  184. tt_int_op(certs_cell_get_certs(cc2, 4)->cert_type, OP_EQ,
  185. CERTTYPE_RSA1024_ID_EDID);
  186. }
  187. chan1 = tor_malloc_zero(sizeof(*chan1));
  188. channel_tls_common_init(chan1);
  189. c1->chan = chan1;
  190. chan1->conn = c1;
  191. c1->base_.address = tor_strdup("C1");
  192. c1->tls = tor_tls_new(-1, 0);
  193. c1->link_proto = 4;
  194. c1->base_.conn_array_index = -1;
  195. crypto_pk_get_digest(key2, c1->identity_digest);
  196. if (with_ed) {
  197. const tor_x509_cert_t *linkc, *idc;
  198. tor_tls_get_my_certs(1, &linkc, &idc);
  199. mock_peer_cert_expect_tortls = c1->tls; /* We should see this tls... */
  200. mock_peer_cert = tor_x509_cert_dup(linkc); /* and when we do, the peer's
  201. * cert is this... */
  202. }
  203. channel_tls_process_certs_cell(cell2, chan1);
  204. mock_peer_cert_expect_tortls = NULL;
  205. tor_x509_cert_free(mock_peer_cert);
  206. mock_peer_cert = NULL;
  207. tor_assert(c1->handshake_state->authenticated);
  208. tt_assert(c1->handshake_state->received_certs_cell);
  209. tt_ptr_op(c1->handshake_state->certs->auth_cert, OP_EQ, NULL);
  210. tt_ptr_op(c1->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  211. tt_assert(c1->handshake_state->certs->id_cert);
  212. if (with_ed) {
  213. tt_assert(c1->handshake_state->certs->ed_sign_link);
  214. tt_assert(c1->handshake_state->certs->ed_rsa_crosscert);
  215. tt_assert(c1->handshake_state->certs->ed_id_sign);
  216. tt_assert(c1->handshake_state->authenticated_rsa);
  217. tt_assert(c1->handshake_state->authenticated_ed25519);
  218. } else {
  219. tt_ptr_op(c1->handshake_state->certs->ed_sign_link, OP_EQ, NULL);
  220. tt_ptr_op(c1->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  221. tt_ptr_op(c1->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  222. tt_assert(c1->handshake_state->authenticated_rsa);
  223. tt_assert(! c1->handshake_state->authenticated_ed25519);
  224. }
  225. tt_assert(! tor_mem_is_zero(
  226. (char*)c1->handshake_state->authenticated_rsa_peer_id, 20));
  227. chan2 = tor_malloc_zero(sizeof(*chan2));
  228. channel_tls_common_init(chan2);
  229. c2->chan = chan2;
  230. chan2->conn = c2;
  231. c2->base_.address = tor_strdup("C2");
  232. c2->tls = tor_tls_new(-1, 1);
  233. c2->link_proto = 4;
  234. c2->base_.conn_array_index = -1;
  235. crypto_pk_get_digest(key1, c2->identity_digest);
  236. channel_tls_process_certs_cell(cell1, chan2);
  237. tt_assert(c2->handshake_state->received_certs_cell);
  238. if (with_ed) {
  239. tt_assert(c2->handshake_state->certs->ed_sign_auth);
  240. tt_assert(c2->handshake_state->certs->ed_rsa_crosscert);
  241. tt_assert(c2->handshake_state->certs->ed_id_sign);
  242. } else {
  243. tt_assert(c2->handshake_state->certs->auth_cert);
  244. tt_ptr_op(c2->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  245. tt_ptr_op(c2->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  246. tt_ptr_op(c2->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  247. }
  248. tt_assert(c2->handshake_state->certs->id_cert);
  249. tt_assert(tor_mem_is_zero(
  250. (char*)c2->handshake_state->authenticated_rsa_peer_id, 20));
  251. /* no authentication has happened yet, since we haen't gotten an AUTH cell.
  252. */
  253. tt_assert(! c2->handshake_state->authenticated);
  254. tt_assert(! c2->handshake_state->authenticated_rsa);
  255. tt_assert(! c2->handshake_state->authenticated_ed25519);
  256. done:
  257. UNMOCK(tor_tls_cert_matches_key);
  258. UNMOCK(connection_or_write_var_cell_to_buf);
  259. UNMOCK(connection_or_send_netinfo);
  260. UNMOCK(tor_tls_get_peer_cert);
  261. UNMOCK(tor_tls_get_own_cert);
  262. tor_x509_cert_free(mock_own_cert);
  263. tor_x509_cert_free(mock_peer_cert);
  264. mock_own_cert = mock_peer_cert = NULL;
  265. memset(c1->identity_digest, 0, sizeof(c1->identity_digest));
  266. memset(c2->identity_digest, 0, sizeof(c2->identity_digest));
  267. connection_free_minimal(TO_CONN(c1));
  268. connection_free_minimal(TO_CONN(c2));
  269. tor_free(cell1);
  270. tor_free(cell2);
  271. certs_cell_free(cc1);
  272. certs_cell_free(cc2);
  273. if (chan1)
  274. circuitmux_free(chan1->base_.cmux);
  275. tor_free(chan1);
  276. if (chan2)
  277. circuitmux_free(chan2->base_.cmux);
  278. tor_free(chan2);
  279. crypto_pk_free(key1);
  280. crypto_pk_free(key2);
  281. }
  282. typedef struct certs_data_s {
  283. int is_ed;
  284. int is_link_cert;
  285. or_connection_t *c;
  286. channel_tls_t *chan;
  287. certs_cell_t *ccell;
  288. var_cell_t *cell;
  289. crypto_pk_t *key1, *key2;
  290. } certs_data_t;
  291. static int
  292. recv_certs_cleanup(const struct testcase_t *test, void *obj)
  293. {
  294. (void)test;
  295. certs_data_t *d = obj;
  296. UNMOCK(tor_tls_cert_matches_key);
  297. UNMOCK(connection_or_send_netinfo);
  298. UNMOCK(connection_or_close_for_error);
  299. UNMOCK(tor_tls_get_peer_cert);
  300. UNMOCK(tor_tls_get_own_cert);
  301. if (d) {
  302. tor_free(d->cell);
  303. certs_cell_free(d->ccell);
  304. connection_or_clear_identity(d->c);
  305. connection_free_minimal(TO_CONN(d->c));
  306. circuitmux_free(d->chan->base_.cmux);
  307. tor_free(d->chan);
  308. crypto_pk_free(d->key1);
  309. crypto_pk_free(d->key2);
  310. tor_free(d);
  311. }
  312. routerkeys_free_all();
  313. return 1;
  314. }
  315. static void *
  316. recv_certs_setup(const struct testcase_t *test)
  317. {
  318. (void)test;
  319. certs_data_t *d = tor_malloc_zero(sizeof(*d));
  320. certs_cell_cert_t *ccc1 = NULL;
  321. certs_cell_cert_t *ccc2 = NULL;
  322. ssize_t n;
  323. int is_ed = d->is_ed = !strcmpstart(test->setup_data, "Ed25519");
  324. int is_rsa = !strcmpstart(test->setup_data, "RSA");
  325. int is_link = d->is_link_cert = !strcmpend(test->setup_data, "-Link");
  326. int is_auth = !strcmpend(test->setup_data, "-Auth");
  327. tor_assert(is_ed != is_rsa);
  328. tor_assert(is_link != is_auth);
  329. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  330. d->chan = tor_malloc_zero(sizeof(*d->chan));
  331. d->c->chan = d->chan;
  332. d->c->base_.address = tor_strdup("HaveAnAddress");
  333. tor_addr_from_ipv4h(&d->c->base_.addr, 0x801f0127);
  334. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  335. d->chan->conn = d->c;
  336. tt_int_op(connection_init_or_handshake_state(d->c, 1), OP_EQ, 0);
  337. d->c->link_proto = 4;
  338. d->key1 = pk_generate(2);
  339. d->key2 = pk_generate(3);
  340. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  341. d->key1, d->key2, 86400), OP_EQ, 0);
  342. if (is_ed) {
  343. init_mock_ed_keys(d->key2);
  344. } else {
  345. routerkeys_free_all();
  346. }
  347. d->ccell = certs_cell_new();
  348. ccc1 = certs_cell_cert_new();
  349. certs_cell_add_certs(d->ccell, ccc1);
  350. ccc2 = certs_cell_cert_new();
  351. certs_cell_add_certs(d->ccell, ccc2);
  352. d->ccell->n_certs = 2;
  353. ccc1->cert_type = is_link ? 1 : 3;
  354. ccc2->cert_type = 2;
  355. const tor_x509_cert_t *a,*b;
  356. const uint8_t *enca, *encb;
  357. size_t lena, lenb;
  358. tor_tls_get_my_certs(is_link ? 1 : 0, &a, &b);
  359. tor_x509_cert_get_der(a, &enca, &lena);
  360. tor_x509_cert_get_der(b, &encb, &lenb);
  361. certs_cell_cert_setlen_body(ccc1, lena);
  362. ccc1->cert_len = lena;
  363. certs_cell_cert_setlen_body(ccc2, lenb);
  364. ccc2->cert_len = lenb;
  365. memcpy(certs_cell_cert_getarray_body(ccc1), enca, lena);
  366. memcpy(certs_cell_cert_getarray_body(ccc2), encb, lenb);
  367. if (is_ed) {
  368. certs_cell_cert_t *ccc3 = NULL; /* Id->Sign */
  369. certs_cell_cert_t *ccc4 = NULL; /* Sign->Link or Sign->Auth. */
  370. certs_cell_cert_t *ccc5 = NULL; /* RSAId->Ed Id. */
  371. const tor_cert_t *id_sign = get_master_signing_key_cert();
  372. const tor_cert_t *secondary =
  373. is_link ? get_current_link_cert_cert() : get_current_auth_key_cert();
  374. const uint8_t *cc = NULL;
  375. size_t cc_sz;
  376. get_master_rsa_crosscert(&cc, &cc_sz);
  377. ccc3 = certs_cell_cert_new();
  378. ccc4 = certs_cell_cert_new();
  379. ccc5 = certs_cell_cert_new();
  380. certs_cell_add_certs(d->ccell, ccc3);
  381. certs_cell_add_certs(d->ccell, ccc4);
  382. certs_cell_add_certs(d->ccell, ccc5);
  383. ccc3->cert_len = id_sign->encoded_len;
  384. ccc4->cert_len = secondary->encoded_len;
  385. ccc5->cert_len = cc_sz;
  386. certs_cell_cert_setlen_body(ccc3, ccc3->cert_len);
  387. certs_cell_cert_setlen_body(ccc4, ccc4->cert_len);
  388. certs_cell_cert_setlen_body(ccc5, ccc5->cert_len);
  389. memcpy(certs_cell_cert_getarray_body(ccc3), id_sign->encoded,
  390. ccc3->cert_len);
  391. memcpy(certs_cell_cert_getarray_body(ccc4), secondary->encoded,
  392. ccc4->cert_len);
  393. memcpy(certs_cell_cert_getarray_body(ccc5), cc, ccc5->cert_len);
  394. ccc3->cert_type = 4;
  395. ccc4->cert_type = is_link ? 5 : 6;
  396. ccc5->cert_type = 7;
  397. d->ccell->n_certs = 5;
  398. }
  399. d->cell = var_cell_new(4096);
  400. d->cell->command = CELL_CERTS;
  401. n = certs_cell_encode(d->cell->payload, 4096, d->ccell);
  402. tt_int_op(n, OP_GT, 0);
  403. d->cell->payload_len = n;
  404. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  405. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  406. MOCK(connection_or_close_for_error, mock_close_for_err);
  407. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  408. if (is_link) {
  409. /* Say that this is the peer's certificate */
  410. mock_peer_cert = tor_x509_cert_dup(a);
  411. }
  412. tt_int_op(0, OP_EQ, d->c->handshake_state->received_certs_cell);
  413. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  414. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  415. return d;
  416. done:
  417. recv_certs_cleanup(test, d);
  418. return NULL;
  419. }
  420. static struct testcase_setup_t setup_recv_certs = {
  421. .setup_fn = recv_certs_setup,
  422. .cleanup_fn = recv_certs_cleanup
  423. };
  424. static void
  425. test_link_handshake_recv_certs_ok(void *arg)
  426. {
  427. certs_data_t *d = arg;
  428. channel_tls_process_certs_cell(d->cell, d->chan);
  429. tt_int_op(0, OP_EQ, mock_close_called);
  430. tt_int_op(d->c->handshake_state->authenticated, OP_EQ, 1);
  431. tt_int_op(d->c->handshake_state->authenticated_rsa, OP_EQ, 1);
  432. tt_int_op(d->c->handshake_state->received_certs_cell, OP_EQ, 1);
  433. tt_ptr_op(d->c->handshake_state->certs->id_cert, OP_NE, NULL);
  434. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_EQ, NULL);
  435. if (d->is_ed) {
  436. tt_ptr_op(d->c->handshake_state->certs->ed_id_sign, OP_NE, NULL);
  437. tt_ptr_op(d->c->handshake_state->certs->ed_sign_link, OP_NE, NULL);
  438. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  439. tt_ptr_op(d->c->handshake_state->certs->ed_rsa_crosscert, OP_NE, NULL);
  440. tt_int_op(d->c->handshake_state->authenticated_ed25519, OP_EQ, 1);
  441. } else {
  442. tt_ptr_op(d->c->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  443. tt_ptr_op(d->c->handshake_state->certs->ed_sign_link, OP_EQ, NULL);
  444. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  445. tt_ptr_op(d->c->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  446. tt_int_op(d->c->handshake_state->authenticated_ed25519, OP_EQ, 0);
  447. }
  448. done:
  449. ;
  450. }
  451. static void
  452. test_link_handshake_recv_certs_ok_server(void *arg)
  453. {
  454. certs_data_t *d = arg;
  455. d->c->handshake_state->started_here = 0;
  456. d->c->handshake_state->certs->started_here = 0;
  457. channel_tls_process_certs_cell(d->cell, d->chan);
  458. tt_int_op(0, OP_EQ, mock_close_called);
  459. tt_int_op(d->c->handshake_state->authenticated, OP_EQ, 0);
  460. tt_int_op(d->c->handshake_state->received_certs_cell, OP_EQ, 1);
  461. tt_ptr_op(d->c->handshake_state->certs->id_cert, OP_NE, NULL);
  462. tt_ptr_op(d->c->handshake_state->certs->link_cert, OP_EQ, NULL);
  463. if (d->is_ed) {
  464. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_NE, NULL);
  465. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_EQ, NULL);
  466. } else {
  467. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  468. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_NE, NULL);
  469. }
  470. done:
  471. ;
  472. }
  473. #define CERTS_FAIL(name, code) \
  474. static void \
  475. test_link_handshake_recv_certs_ ## name(void *arg) \
  476. { \
  477. certs_data_t *d = arg; \
  478. const char *require_failure_message = NULL; \
  479. setup_capture_of_logs(LOG_INFO); \
  480. { code ; } \
  481. channel_tls_process_certs_cell(d->cell, d->chan); \
  482. tt_int_op(1, OP_EQ, mock_close_called); \
  483. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  484. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  485. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_rsa); \
  486. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_ed25519); \
  487. if (require_failure_message) { \
  488. expect_log_msg_containing(require_failure_message); \
  489. } \
  490. done: \
  491. teardown_capture_of_logs(); \
  492. }
  493. CERTS_FAIL(badstate,
  494. require_failure_message = "We're not doing a v3 handshake!";
  495. d->c->base_.state = OR_CONN_STATE_CONNECTING;)
  496. CERTS_FAIL(badproto,
  497. require_failure_message = "not using link protocol >= 3";
  498. d->c->link_proto = 2)
  499. CERTS_FAIL(duplicate,
  500. require_failure_message = "We already got one";
  501. d->c->handshake_state->received_certs_cell = 1)
  502. CERTS_FAIL(already_authenticated,
  503. require_failure_message = "We're already authenticated!";
  504. d->c->handshake_state->authenticated = 1)
  505. CERTS_FAIL(empty,
  506. require_failure_message = "It had no body";
  507. d->cell->payload_len = 0)
  508. CERTS_FAIL(bad_circid,
  509. require_failure_message = "It had a nonzero circuit ID";
  510. d->cell->circ_id = 1)
  511. CERTS_FAIL(truncated_1,
  512. require_failure_message = "It couldn't be parsed";
  513. d->cell->payload[0] = 5)
  514. CERTS_FAIL(truncated_2,
  515. {
  516. require_failure_message = "It couldn't be parsed";
  517. d->cell->payload_len = 4;
  518. memcpy(d->cell->payload, "\x01\x01\x00\x05", 4);
  519. })
  520. CERTS_FAIL(truncated_3,
  521. {
  522. require_failure_message = "It couldn't be parsed";
  523. d->cell->payload_len = 7;
  524. memcpy(d->cell->payload, "\x01\x01\x00\x05""abc", 7);
  525. })
  526. CERTS_FAIL(truncated_4, /* ed25519 */
  527. {
  528. require_failure_message = "It couldn't be parsed";
  529. d->cell->payload_len -= 10;
  530. })
  531. CERTS_FAIL(truncated_5, /* ed25519 */
  532. {
  533. require_failure_message = "It couldn't be parsed";
  534. d->cell->payload_len -= 100;
  535. })
  536. #define REENCODE() do { \
  537. const char *msg = certs_cell_check(d->ccell); \
  538. if (msg) puts(msg); \
  539. ssize_t n = certs_cell_encode(d->cell->payload, 4096, d->ccell); \
  540. tt_int_op(n, OP_GT, 0); \
  541. d->cell->payload_len = n; \
  542. } while (0)
  543. CERTS_FAIL(truncated_6, /* ed25519 */
  544. {
  545. /* truncate the link certificate */
  546. require_failure_message = "undecodable Ed certificate";
  547. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 3), 7);
  548. certs_cell_get_certs(d->ccell, 3)->cert_len = 7;
  549. REENCODE();
  550. })
  551. CERTS_FAIL(truncated_7, /* ed25519 */
  552. {
  553. /* truncate the crosscert */
  554. require_failure_message = "Unparseable or overlong crosscert";
  555. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 4), 7);
  556. certs_cell_get_certs(d->ccell, 4)->cert_len = 7;
  557. REENCODE();
  558. })
  559. CERTS_FAIL(not_x509,
  560. {
  561. require_failure_message = "Received undecodable certificate";
  562. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 0), 3);
  563. certs_cell_get_certs(d->ccell, 0)->cert_len = 3;
  564. REENCODE();
  565. })
  566. CERTS_FAIL(both_link,
  567. {
  568. require_failure_message = "Duplicate x509 certificate";
  569. certs_cell_get_certs(d->ccell, 0)->cert_type = 1;
  570. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  571. REENCODE();
  572. })
  573. CERTS_FAIL(both_id_rsa,
  574. {
  575. require_failure_message = "Duplicate x509 certificate";
  576. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  577. certs_cell_get_certs(d->ccell, 1)->cert_type = 2;
  578. REENCODE();
  579. })
  580. CERTS_FAIL(both_auth,
  581. {
  582. require_failure_message = "Duplicate x509 certificate";
  583. certs_cell_get_certs(d->ccell, 0)->cert_type = 3;
  584. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  585. REENCODE();
  586. })
  587. CERTS_FAIL(duplicate_id, /* ed25519 */
  588. {
  589. require_failure_message = "Duplicate Ed25519 certificate";
  590. certs_cell_get_certs(d->ccell, 2)->cert_type = 4;
  591. certs_cell_get_certs(d->ccell, 3)->cert_type = 4;
  592. REENCODE();
  593. })
  594. CERTS_FAIL(duplicate_link, /* ed25519 */
  595. {
  596. require_failure_message = "Duplicate Ed25519 certificate";
  597. certs_cell_get_certs(d->ccell, 2)->cert_type = 5;
  598. certs_cell_get_certs(d->ccell, 3)->cert_type = 5;
  599. REENCODE();
  600. })
  601. CERTS_FAIL(duplicate_crosscert, /* ed25519 */
  602. {
  603. require_failure_message = "Duplicate RSA->Ed25519 crosscert";
  604. certs_cell_get_certs(d->ccell, 2)->cert_type = 7;
  605. certs_cell_get_certs(d->ccell, 3)->cert_type = 7;
  606. REENCODE();
  607. })
  608. static void
  609. test_link_handshake_recv_certs_missing_id(void *arg) /* ed25519 */
  610. {
  611. certs_data_t *d = arg;
  612. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  613. certs_cell_set_certs(d->ccell, 2, certs_cell_get_certs(d->ccell, 4));
  614. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  615. certs_cell_setlen_certs(d->ccell, 4);
  616. d->ccell->n_certs = 4;
  617. REENCODE();
  618. /* This handshake succeeds, but since we have no ID cert, we will
  619. * just do the RSA handshake. */
  620. channel_tls_process_certs_cell(d->cell, d->chan);
  621. tt_int_op(0, OP_EQ, mock_close_called);
  622. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_ed25519);
  623. tt_int_op(1, OP_EQ, d->c->handshake_state->authenticated_rsa);
  624. done:
  625. ;
  626. }
  627. CERTS_FAIL(missing_signing_key, /* ed25519 */
  628. {
  629. require_failure_message = "No Ed25519 signing key";
  630. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  631. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 2);
  632. tt_int_op(cert->cert_type, OP_EQ, CERTTYPE_ED_ID_SIGN);
  633. /* replace this with a valid master->signing cert, but with no
  634. * signing key. */
  635. const ed25519_keypair_t *mk = get_master_identity_keypair();
  636. const ed25519_keypair_t *sk = get_master_signing_keypair();
  637. tor_cert_t *bad_cert = tor_cert_create(mk, CERT_TYPE_ID_SIGNING,
  638. &sk->pubkey, time(NULL), 86400,
  639. 0 /* don't include signer */);
  640. certs_cell_cert_setlen_body(cert, bad_cert->encoded_len);
  641. memcpy(certs_cell_cert_getarray_body(cert),
  642. bad_cert->encoded, bad_cert->encoded_len);
  643. cert->cert_len = bad_cert->encoded_len;
  644. tor_cert_free(bad_cert);
  645. REENCODE();
  646. })
  647. CERTS_FAIL(missing_link, /* ed25519 */
  648. {
  649. require_failure_message = "No Ed25519 link key";
  650. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  651. certs_cell_set_certs(d->ccell, 3, certs_cell_get_certs(d->ccell, 4));
  652. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  653. certs_cell_setlen_certs(d->ccell, 4);
  654. d->ccell->n_certs = 4;
  655. REENCODE();
  656. })
  657. CERTS_FAIL(missing_auth, /* ed25519 */
  658. {
  659. d->c->handshake_state->started_here = 0;
  660. d->c->handshake_state->certs->started_here = 0;
  661. require_failure_message = "No Ed25519 link authentication key";
  662. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  663. certs_cell_set_certs(d->ccell, 3, certs_cell_get_certs(d->ccell, 4));
  664. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  665. certs_cell_setlen_certs(d->ccell, 4);
  666. d->ccell->n_certs = 4;
  667. REENCODE();
  668. })
  669. CERTS_FAIL(missing_crosscert, /* ed25519 */
  670. {
  671. require_failure_message = "Missing RSA->Ed25519 crosscert";
  672. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  673. certs_cell_setlen_certs(d->ccell, 4);
  674. d->ccell->n_certs = 4;
  675. REENCODE();
  676. })
  677. CERTS_FAIL(missing_rsa_id, /* ed25519 */
  678. {
  679. require_failure_message = "Missing legacy RSA ID cert";
  680. tt_int_op(certs_cell_getlen_certs(d->ccell), OP_EQ, 5);
  681. certs_cell_set_certs(d->ccell, 1, certs_cell_get_certs(d->ccell, 4));
  682. certs_cell_set0_certs(d->ccell, 4, NULL); /* prevent free */
  683. certs_cell_setlen_certs(d->ccell, 4);
  684. d->ccell->n_certs = 4;
  685. REENCODE();
  686. })
  687. CERTS_FAIL(link_mismatch, /* ed25519 */
  688. {
  689. require_failure_message = "Link certificate does not match "
  690. "TLS certificate";
  691. const tor_x509_cert_t *idc;
  692. tor_tls_get_my_certs(1, NULL, &idc);
  693. tor_x509_cert_free(mock_peer_cert);
  694. /* Pretend that the peer cert was something else. */
  695. mock_peer_cert = tor_x509_cert_dup(idc);
  696. /* No reencode needed. */
  697. })
  698. CERTS_FAIL(bad_ed_sig, /* ed25519 */
  699. {
  700. require_failure_message = "At least one Ed25519 certificate was "
  701. "badly signed";
  702. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 3);
  703. uint8_t *body = certs_cell_cert_getarray_body(cert);
  704. ssize_t body_len = certs_cell_cert_getlen_body(cert);
  705. /* Frob a byte in the signature */
  706. body[body_len - 13] ^= 7;
  707. REENCODE();
  708. })
  709. CERTS_FAIL(bad_crosscert, /*ed25519*/
  710. {
  711. require_failure_message = "Invalid RSA->Ed25519 crosscert";
  712. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 4);
  713. uint8_t *body = certs_cell_cert_getarray_body(cert);
  714. ssize_t body_len = certs_cell_cert_getlen_body(cert);
  715. /* Frob a byte in the signature */
  716. body[body_len - 13] ^= 7;
  717. REENCODE();
  718. })
  719. CERTS_FAIL(bad_rsa_id_cert, /*ed25519*/
  720. {
  721. require_failure_message = "legacy RSA ID certificate was not valid";
  722. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 1);
  723. uint8_t *body = certs_cell_cert_getarray_body(cert);
  724. ssize_t body_len = certs_cell_cert_getlen_body(cert);
  725. /* Frob a byte in the signature */
  726. body[body_len - 13] ^= 7;
  727. REENCODE();
  728. })
  729. CERTS_FAIL(expired_rsa_id, /* both */
  730. {
  731. require_failure_message = "Certificate already expired";
  732. /* we're going to replace the identity cert with an expired one. */
  733. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 1);
  734. const tor_x509_cert_t *idc;
  735. tor_tls_get_my_certs(1, NULL, &idc);
  736. tor_x509_cert_t *newc;
  737. time_t new_end = time(NULL) - 86400 * 10;
  738. newc = tor_x509_cert_replace_expiration(idc, new_end, d->key2);
  739. certs_cell_cert_setlen_body(cert, newc->encoded_len);
  740. memcpy(certs_cell_cert_getarray_body(cert),
  741. newc->encoded, newc->encoded_len);
  742. REENCODE();
  743. tor_x509_cert_free(newc);
  744. })
  745. CERTS_FAIL(expired_ed_id, /* ed25519 */
  746. {
  747. /* we're going to replace the Ed Id->sign cert with an expired one. */
  748. require_failure_message = "At least one certificate expired";
  749. /* We don't need to re-sign, since we check for expiration first. */
  750. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 2);
  751. uint8_t *body = certs_cell_cert_getarray_body(cert);
  752. /* The expiration field is bytes [2..5]. It is in HOURS since the
  753. * epoch. */
  754. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  755. REENCODE();
  756. })
  757. CERTS_FAIL(expired_ed_link, /* ed25519 */
  758. {
  759. /* we're going to replace the Ed Sign->link cert with an expired one. */
  760. require_failure_message = "At least one certificate expired";
  761. /* We don't need to re-sign, since we check for expiration first. */
  762. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 3);
  763. uint8_t *body = certs_cell_cert_getarray_body(cert);
  764. /* The expiration field is bytes [2..5]. It is in HOURS since the
  765. * epoch. */
  766. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  767. REENCODE();
  768. })
  769. CERTS_FAIL(expired_crosscert, /* ed25519 */
  770. {
  771. /* we're going to replace the Ed Sign->link cert with an expired one. */
  772. require_failure_message = "Crosscert is expired";
  773. /* We don't need to re-sign, since we check for expiration first. */
  774. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 4);
  775. uint8_t *body = certs_cell_cert_getarray_body(cert);
  776. /* The expiration field is bytes [32..35]. once again, HOURS. */
  777. set_uint32(body+32, htonl(24)); /* Back to jan 2, 1970. */
  778. REENCODE();
  779. })
  780. CERTS_FAIL(wrong_labels_1,
  781. {
  782. require_failure_message = "The link certificate was not valid";
  783. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  784. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  785. REENCODE();
  786. })
  787. CERTS_FAIL(wrong_labels_2,
  788. {
  789. const tor_x509_cert_t *a;
  790. const tor_x509_cert_t *b;
  791. const uint8_t *enca;
  792. size_t lena;
  793. require_failure_message = "The link certificate was not valid";
  794. tor_tls_get_my_certs(1, &a, &b);
  795. tor_x509_cert_get_der(a, &enca, &lena);
  796. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  797. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  798. enca, lena);
  799. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  800. REENCODE();
  801. })
  802. CERTS_FAIL(wrong_labels_3,
  803. {
  804. require_failure_message =
  805. "The certs we wanted (ID, Link) were missing";
  806. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  807. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  808. REENCODE();
  809. })
  810. CERTS_FAIL(server_missing_certs,
  811. {
  812. require_failure_message =
  813. "The certs we wanted (ID, Auth) were missing";
  814. d->c->handshake_state->started_here = 0;
  815. d->c->handshake_state->certs->started_here = 0;
  816. })
  817. CERTS_FAIL(server_wrong_labels_1,
  818. {
  819. require_failure_message =
  820. "The authentication certificate was not valid";
  821. d->c->handshake_state->started_here = 0;
  822. d->c->handshake_state->certs->started_here = 0;
  823. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  824. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  825. REENCODE();
  826. })
  827. static void
  828. test_link_handshake_send_authchallenge(void *arg)
  829. {
  830. (void)arg;
  831. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  832. var_cell_t *cell1=NULL, *cell2=NULL;
  833. crypto_pk_t *rsa0 = pk_generate(0), *rsa1 = pk_generate(1);
  834. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  835. rsa0, rsa1, 86400), OP_EQ, 0);
  836. init_mock_ed_keys(rsa0);
  837. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  838. tt_int_op(connection_init_or_handshake_state(c1, 0), OP_EQ, 0);
  839. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  840. tt_ptr_op(mock_got_var_cell, OP_EQ, NULL);
  841. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  842. cell1 = mock_got_var_cell;
  843. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  844. cell2 = mock_got_var_cell;
  845. tt_int_op(38, OP_EQ, cell1->payload_len);
  846. tt_int_op(38, OP_EQ, cell2->payload_len);
  847. tt_int_op(0, OP_EQ, cell1->circ_id);
  848. tt_int_op(0, OP_EQ, cell2->circ_id);
  849. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell1->command);
  850. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell2->command);
  851. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell1->payload + 32, 6);
  852. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell2->payload + 32, 6);
  853. tt_mem_op(cell1->payload, OP_NE, cell2->payload, 32);
  854. done:
  855. UNMOCK(connection_or_write_var_cell_to_buf);
  856. connection_free_minimal(TO_CONN(c1));
  857. tor_free(cell1);
  858. tor_free(cell2);
  859. crypto_pk_free(rsa0);
  860. crypto_pk_free(rsa1);
  861. }
  862. typedef struct authchallenge_data_s {
  863. or_connection_t *c;
  864. channel_tls_t *chan;
  865. var_cell_t *cell;
  866. } authchallenge_data_t;
  867. static int
  868. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  869. {
  870. (void)test;
  871. authchallenge_data_t *d = obj;
  872. UNMOCK(connection_or_send_netinfo);
  873. UNMOCK(connection_or_close_for_error);
  874. UNMOCK(connection_or_send_authenticate_cell);
  875. if (d) {
  876. tor_free(d->cell);
  877. connection_free_minimal(TO_CONN(d->c));
  878. circuitmux_free(d->chan->base_.cmux);
  879. tor_free(d->chan);
  880. tor_free(d);
  881. }
  882. return 1;
  883. }
  884. static void *
  885. recv_authchallenge_setup(const struct testcase_t *test)
  886. {
  887. (void)test;
  888. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  889. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  890. d->chan = tor_malloc_zero(sizeof(*d->chan));
  891. d->c->chan = d->chan;
  892. d->c->base_.address = tor_strdup("HaveAnAddress");
  893. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  894. d->chan->conn = d->c;
  895. tt_int_op(connection_init_or_handshake_state(d->c, 1), OP_EQ, 0);
  896. d->c->link_proto = 4;
  897. d->c->handshake_state->received_certs_cell = 1;
  898. d->cell = var_cell_new(128);
  899. d->cell->payload_len = 38;
  900. d->cell->payload[33] = 2; /* 2 methods */
  901. d->cell->payload[35] = 7; /* This one isn't real */
  902. d->cell->payload[37] = 1; /* This is the old RSA one. */
  903. d->cell->command = CELL_AUTH_CHALLENGE;
  904. get_options_mutable()->ORPort_set = 1;
  905. MOCK(connection_or_close_for_error, mock_close_for_err);
  906. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  907. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  908. tt_int_op(0, OP_EQ, d->c->handshake_state->received_auth_challenge);
  909. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  910. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  911. return d;
  912. done:
  913. recv_authchallenge_cleanup(test, d);
  914. return NULL;
  915. }
  916. static struct testcase_setup_t setup_recv_authchallenge = {
  917. .setup_fn = recv_authchallenge_setup,
  918. .cleanup_fn = recv_authchallenge_cleanup
  919. };
  920. static void
  921. test_link_handshake_recv_authchallenge_ok(void *arg)
  922. {
  923. authchallenge_data_t *d = arg;
  924. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  925. tt_int_op(0, OP_EQ, mock_close_called);
  926. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  927. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  928. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  929. tt_int_op(1, OP_EQ, mock_send_authenticate_called_with_type); /* RSA */
  930. done:
  931. ;
  932. }
  933. static void
  934. test_link_handshake_recv_authchallenge_ok_ed25519(void *arg)
  935. {
  936. authchallenge_data_t *d = arg;
  937. /* Add the ed25519 authentication mechanism here. */
  938. d->cell->payload[33] = 3; /* 3 types are supported now. */
  939. d->cell->payload[39] = 3;
  940. d->cell->payload_len += 2;
  941. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  942. tt_int_op(0, OP_EQ, mock_close_called);
  943. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  944. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  945. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  946. tt_int_op(3, OP_EQ, mock_send_authenticate_called_with_type); /* Ed25519 */
  947. done:
  948. ;
  949. }
  950. static void
  951. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  952. {
  953. authchallenge_data_t *d = arg;
  954. get_options_mutable()->ORPort_set = 0;
  955. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  956. tt_int_op(0, OP_EQ, mock_close_called);
  957. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  958. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  959. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  960. done:
  961. ;
  962. }
  963. static void
  964. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  965. {
  966. authchallenge_data_t *d = arg;
  967. d->cell->payload[37] = 99;
  968. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  969. tt_int_op(0, OP_EQ, mock_close_called);
  970. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  971. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  972. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  973. done:
  974. ;
  975. }
  976. #define AUTHCHALLENGE_FAIL(name, code) \
  977. static void \
  978. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  979. { \
  980. authchallenge_data_t *d = arg; \
  981. const char *require_failure_message = NULL; \
  982. setup_capture_of_logs(LOG_INFO); \
  983. { code ; } \
  984. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  985. tt_int_op(1, OP_EQ, mock_close_called); \
  986. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  987. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  988. if (require_failure_message) { \
  989. expect_log_msg_containing(require_failure_message); \
  990. } \
  991. done: \
  992. teardown_capture_of_logs(); \
  993. }
  994. AUTHCHALLENGE_FAIL(badstate,
  995. require_failure_message = "We're not currently doing a "
  996. "v3 handshake";
  997. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  998. AUTHCHALLENGE_FAIL(badproto,
  999. require_failure_message = "not using link protocol >= 3";
  1000. d->c->link_proto = 2)
  1001. AUTHCHALLENGE_FAIL(as_server,
  1002. require_failure_message = "We didn't originate this "
  1003. "connection";
  1004. d->c->handshake_state->started_here = 0;
  1005. d->c->handshake_state->certs->started_here = 0;)
  1006. AUTHCHALLENGE_FAIL(duplicate,
  1007. require_failure_message = "We already received one";
  1008. d->c->handshake_state->received_auth_challenge = 1)
  1009. AUTHCHALLENGE_FAIL(nocerts,
  1010. require_failure_message = "We haven't gotten a CERTS "
  1011. "cell yet";
  1012. d->c->handshake_state->received_certs_cell = 0)
  1013. AUTHCHALLENGE_FAIL(tooshort,
  1014. require_failure_message = "It was not well-formed";
  1015. d->cell->payload_len = 33)
  1016. AUTHCHALLENGE_FAIL(truncated,
  1017. require_failure_message = "It was not well-formed";
  1018. d->cell->payload_len = 34)
  1019. AUTHCHALLENGE_FAIL(nonzero_circid,
  1020. require_failure_message = "It had a nonzero circuit ID";
  1021. d->cell->circ_id = 1337)
  1022. static int
  1023. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  1024. {
  1025. (void)tls;
  1026. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  1027. return 0;
  1028. }
  1029. static void
  1030. mock_set_circid_type(channel_t *chan,
  1031. crypto_pk_t *identity_rcvd,
  1032. int consider_identity)
  1033. {
  1034. (void) chan;
  1035. (void) identity_rcvd;
  1036. (void) consider_identity;
  1037. }
  1038. typedef struct authenticate_data_s {
  1039. int is_ed;
  1040. or_connection_t *c1, *c2;
  1041. channel_tls_t *chan2;
  1042. var_cell_t *cell;
  1043. crypto_pk_t *key1, *key2;
  1044. } authenticate_data_t;
  1045. static int
  1046. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  1047. {
  1048. (void) test;
  1049. UNMOCK(connection_or_write_var_cell_to_buf);
  1050. UNMOCK(tor_tls_get_peer_cert);
  1051. UNMOCK(tor_tls_get_own_cert);
  1052. UNMOCK(tor_tls_get_tlssecrets);
  1053. UNMOCK(connection_or_close_for_error);
  1054. UNMOCK(channel_set_circid_type);
  1055. UNMOCK(tor_tls_export_key_material);
  1056. authenticate_data_t *d = arg;
  1057. if (d) {
  1058. tor_free(d->cell);
  1059. connection_or_clear_identity(d->c1);
  1060. connection_or_clear_identity(d->c2);
  1061. connection_free_minimal(TO_CONN(d->c1));
  1062. connection_free_minimal(TO_CONN(d->c2));
  1063. circuitmux_free(d->chan2->base_.cmux);
  1064. tor_free(d->chan2);
  1065. crypto_pk_free(d->key1);
  1066. crypto_pk_free(d->key2);
  1067. tor_free(d);
  1068. }
  1069. tor_x509_cert_free(mock_peer_cert);
  1070. tor_x509_cert_free(mock_own_cert);
  1071. mock_peer_cert = NULL;
  1072. mock_own_cert = NULL;
  1073. return 1;
  1074. }
  1075. static void *
  1076. authenticate_data_setup(const struct testcase_t *test)
  1077. {
  1078. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  1079. int is_ed = d->is_ed = (test->setup_data == (void*)3);
  1080. scheduler_init();
  1081. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  1082. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  1083. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  1084. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  1085. MOCK(connection_or_close_for_error, mock_close_for_err);
  1086. MOCK(channel_set_circid_type, mock_set_circid_type);
  1087. MOCK(tor_tls_export_key_material, mock_export_key_material);
  1088. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1089. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1090. tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
  1091. tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
  1092. d->key1 = pk_generate(2);
  1093. d->key2 = pk_generate(3);
  1094. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  1095. d->key1, d->key2, 86400), OP_EQ, 0);
  1096. init_mock_ed_keys(d->key2);
  1097. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1098. d->c1->link_proto = 3;
  1099. tt_int_op(connection_init_or_handshake_state(d->c1, 1), OP_EQ, 0);
  1100. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1101. d->c2->link_proto = 3;
  1102. tt_int_op(connection_init_or_handshake_state(d->c2, 0), OP_EQ, 0);
  1103. var_cell_t *cell = var_cell_new(16);
  1104. cell->command = CELL_CERTS;
  1105. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  1106. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  1107. memset(cell->payload, 0xf0, 16);
  1108. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  1109. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  1110. tor_free(cell);
  1111. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  1112. channel_tls_common_init(d->chan2);
  1113. d->c2->chan = d->chan2;
  1114. d->chan2->conn = d->c2;
  1115. d->c2->base_.address = tor_strdup("C2");
  1116. d->c2->tls = tor_tls_new(-1, 1);
  1117. d->c2->handshake_state->received_certs_cell = 1;
  1118. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  1119. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  1120. const uint8_t *der;
  1121. size_t sz;
  1122. tor_x509_cert_get_der(id_cert, &der, &sz);
  1123. d->c1->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1124. d->c2->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1125. if (is_ed) {
  1126. d->c1->handshake_state->certs->ed_id_sign =
  1127. tor_cert_dup(get_master_signing_key_cert());
  1128. d->c2->handshake_state->certs->ed_id_sign =
  1129. tor_cert_dup(get_master_signing_key_cert());
  1130. d->c2->handshake_state->certs->ed_sign_auth =
  1131. tor_cert_dup(get_current_auth_key_cert());
  1132. } else {
  1133. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  1134. tor_x509_cert_get_der(auth_cert, &der, &sz);
  1135. d->c2->handshake_state->certs->auth_cert = tor_x509_cert_decode(der, sz);
  1136. }
  1137. tor_x509_cert_get_der(link_cert, &der, &sz);
  1138. mock_peer_cert = tor_x509_cert_decode(der, sz);
  1139. tt_assert(mock_peer_cert);
  1140. mock_own_cert = tor_x509_cert_decode(der, sz);
  1141. tt_assert(mock_own_cert);
  1142. /* Make an authenticate cell ... */
  1143. int authtype;
  1144. if (is_ed)
  1145. authtype = AUTHTYPE_ED25519_SHA256_RFC5705;
  1146. else
  1147. authtype = AUTHTYPE_RSA_SHA256_TLSSECRET;
  1148. tt_int_op(0, OP_EQ, connection_or_send_authenticate_cell(d->c1, authtype));
  1149. tt_assert(mock_got_var_cell);
  1150. d->cell = mock_got_var_cell;
  1151. mock_got_var_cell = NULL;
  1152. return d;
  1153. done:
  1154. authenticate_data_cleanup(test, d);
  1155. return NULL;
  1156. }
  1157. static struct testcase_setup_t setup_authenticate = {
  1158. .setup_fn = authenticate_data_setup,
  1159. .cleanup_fn = authenticate_data_cleanup
  1160. };
  1161. static void
  1162. test_link_handshake_auth_cell(void *arg)
  1163. {
  1164. authenticate_data_t *d = arg;
  1165. auth1_t *auth1 = NULL;
  1166. crypto_pk_t *auth_pubkey = NULL;
  1167. /* Is the cell well-formed on the outer layer? */
  1168. tt_int_op(d->cell->command, OP_EQ, CELL_AUTHENTICATE);
  1169. tt_int_op(d->cell->payload[0], OP_EQ, 0);
  1170. if (d->is_ed)
  1171. tt_int_op(d->cell->payload[1], OP_EQ, 3);
  1172. else
  1173. tt_int_op(d->cell->payload[1], OP_EQ, 1);
  1174. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), OP_EQ,
  1175. d->cell->payload_len - 4);
  1176. /* Check it out for plausibility... */
  1177. auth_ctx_t ctx;
  1178. ctx.is_ed = d->is_ed;
  1179. tt_int_op(d->cell->payload_len-4, OP_EQ, auth1_parse(&auth1,
  1180. d->cell->payload+4,
  1181. d->cell->payload_len - 4, &ctx));
  1182. tt_assert(auth1);
  1183. if (d->is_ed) {
  1184. tt_mem_op(auth1->type, OP_EQ, "AUTH0003", 8);
  1185. } else {
  1186. tt_mem_op(auth1->type, OP_EQ, "AUTH0001", 8);
  1187. }
  1188. tt_mem_op(auth1->tlssecrets, OP_EQ, "int getRandomNumber(){return 4;}", 32);
  1189. /* Is the signature okay? */
  1190. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  1191. if (d->is_ed) {
  1192. ed25519_signature_t sig;
  1193. tt_int_op(auth1_getlen_sig(auth1), OP_EQ, ED25519_SIG_LEN);
  1194. memcpy(&sig.sig, auth1_getarray_sig(auth1), ED25519_SIG_LEN);
  1195. tt_assert(!ed25519_checksig(&sig, start, end-start,
  1196. &get_current_auth_keypair()->pubkey));
  1197. } else {
  1198. uint8_t sig[128];
  1199. uint8_t digest[32];
  1200. tt_int_op(auth1_getlen_sig(auth1), OP_GT, 120);
  1201. auth_pubkey = tor_tls_cert_get_key(
  1202. d->c2->handshake_state->certs->auth_cert);
  1203. int n = crypto_pk_public_checksig(
  1204. auth_pubkey,
  1205. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  1206. auth1_getlen_sig(auth1));
  1207. tt_int_op(n, OP_EQ, 32);
  1208. crypto_digest256((char*)digest,
  1209. (const char*)start, end-start, DIGEST_SHA256);
  1210. tt_mem_op(sig, OP_EQ, digest, 32);
  1211. }
  1212. /* Then feed it to c2. */
  1213. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0);
  1214. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1215. tt_int_op(mock_close_called, OP_EQ, 0);
  1216. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1217. if (d->is_ed) {
  1218. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 1);
  1219. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1220. } else {
  1221. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 0);
  1222. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1223. }
  1224. done:
  1225. auth1_free(auth1);
  1226. crypto_pk_free(auth_pubkey);
  1227. }
  1228. #define AUTHENTICATE_FAIL(name, code) \
  1229. static void \
  1230. test_link_handshake_auth_ ## name(void *arg) \
  1231. { \
  1232. authenticate_data_t *d = arg; \
  1233. const char *require_failure_message = NULL; \
  1234. setup_capture_of_logs(LOG_INFO); \
  1235. { code ; } \
  1236. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  1237. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  1238. tt_int_op(mock_close_called, OP_EQ, 1); \
  1239. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  1240. if (require_failure_message) { \
  1241. expect_log_msg_containing(require_failure_message); \
  1242. } \
  1243. done: \
  1244. teardown_capture_of_logs(); \
  1245. }
  1246. AUTHENTICATE_FAIL(badstate,
  1247. require_failure_message = "We're not doing a v3 handshake";
  1248. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  1249. AUTHENTICATE_FAIL(badproto,
  1250. require_failure_message = "not using link protocol >= 3";
  1251. d->c2->link_proto = 2)
  1252. AUTHENTICATE_FAIL(atclient,
  1253. require_failure_message = "We originated this connection";
  1254. d->c2->handshake_state->started_here = 1;
  1255. d->c2->handshake_state->certs->started_here = 1;)
  1256. AUTHENTICATE_FAIL(duplicate,
  1257. require_failure_message = "We already got one";
  1258. d->c2->handshake_state->received_authenticate = 1)
  1259. static void
  1260. test_link_handshake_auth_already_authenticated(void *arg)
  1261. {
  1262. authenticate_data_t *d = arg;
  1263. setup_capture_of_logs(LOG_INFO);
  1264. d->c2->handshake_state->authenticated = 1;
  1265. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1266. tt_int_op(mock_close_called, OP_EQ, 1);
  1267. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1268. expect_log_msg_containing("The peer is already authenticated");
  1269. done:
  1270. teardown_capture_of_logs();
  1271. }
  1272. AUTHENTICATE_FAIL(nocerts,
  1273. require_failure_message = "We never got a certs cell";
  1274. d->c2->handshake_state->received_certs_cell = 0)
  1275. AUTHENTICATE_FAIL(noidcert,
  1276. require_failure_message = "We never got an identity "
  1277. "certificate";
  1278. tor_x509_cert_free(d->c2->handshake_state->certs->id_cert);
  1279. d->c2->handshake_state->certs->id_cert = NULL)
  1280. AUTHENTICATE_FAIL(noauthcert,
  1281. require_failure_message = "We never got an RSA "
  1282. "authentication certificate";
  1283. tor_x509_cert_free(d->c2->handshake_state->certs->auth_cert);
  1284. d->c2->handshake_state->certs->auth_cert = NULL)
  1285. AUTHENTICATE_FAIL(tooshort,
  1286. require_failure_message = "Cell was way too short";
  1287. d->cell->payload_len = 3)
  1288. AUTHENTICATE_FAIL(badtype,
  1289. require_failure_message = "Authenticator type was not "
  1290. "recognized";
  1291. d->cell->payload[0] = 0xff)
  1292. AUTHENTICATE_FAIL(truncated_1,
  1293. require_failure_message = "Authenticator was truncated";
  1294. d->cell->payload[2]++)
  1295. AUTHENTICATE_FAIL(truncated_2,
  1296. require_failure_message = "Authenticator was truncated";
  1297. d->cell->payload[3]++)
  1298. AUTHENTICATE_FAIL(tooshort_1,
  1299. require_failure_message = "Authenticator was too short";
  1300. tt_int_op(d->cell->payload_len, OP_GE, 260);
  1301. d->cell->payload[2] -= 1;
  1302. d->cell->payload_len -= 256;)
  1303. AUTHENTICATE_FAIL(badcontent,
  1304. require_failure_message = "Some field in the AUTHENTICATE "
  1305. "cell body was not as expected";
  1306. d->cell->payload[10] ^= 0xff)
  1307. AUTHENTICATE_FAIL(badsig_1,
  1308. if (d->is_ed)
  1309. require_failure_message = "Ed25519 signature wasn't valid";
  1310. else
  1311. require_failure_message = "RSA signature wasn't valid";
  1312. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  1313. AUTHENTICATE_FAIL(missing_ed_id,
  1314. {
  1315. tor_cert_free(d->c2->handshake_state->certs->ed_id_sign);
  1316. d->c2->handshake_state->certs->ed_id_sign = NULL;
  1317. require_failure_message = "Ed authenticate without Ed ID "
  1318. "cert from peer";
  1319. })
  1320. AUTHENTICATE_FAIL(missing_ed_auth,
  1321. {
  1322. tor_cert_free(d->c2->handshake_state->certs->ed_sign_auth);
  1323. d->c2->handshake_state->certs->ed_sign_auth = NULL;
  1324. require_failure_message = "We never got an Ed25519 "
  1325. "authentication certificate";
  1326. })
  1327. #define TEST_RSA(name, flags) \
  1328. { #name , test_link_handshake_ ## name, (flags), \
  1329. &passthrough_setup, (void*)"RSA" }
  1330. #define TEST_ED(name, flags) \
  1331. { #name "_ed25519" , test_link_handshake_ ## name, (flags), \
  1332. &passthrough_setup, (void*)"Ed25519" }
  1333. #define TEST_RCV_AUTHCHALLENGE(name) \
  1334. { "recv_authchallenge/" #name , \
  1335. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  1336. &setup_recv_authchallenge, NULL }
  1337. #define TEST_RCV_CERTS(name) \
  1338. { "recv_certs/" #name , \
  1339. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1340. &setup_recv_certs, (void*)"RSA-Link" }
  1341. #define TEST_RCV_CERTS_RSA(name,type) \
  1342. { "recv_certs/" #name , \
  1343. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1344. &setup_recv_certs, (void*)type }
  1345. #define TEST_RCV_CERTS_ED(name, type) \
  1346. { "recv_certs/" #name "_ed25519", \
  1347. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1348. &setup_recv_certs, (void*)type }
  1349. #define TEST_AUTHENTICATE(name) \
  1350. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  1351. &setup_authenticate, NULL }
  1352. #define TEST_AUTHENTICATE_ED(name) \
  1353. { "authenticate/" #name "_ed25519" , test_link_handshake_auth_ ## name, \
  1354. TT_FORK, &setup_authenticate, (void*)3 }
  1355. struct testcase_t link_handshake_tests[] = {
  1356. TEST_RSA(certs_ok, TT_FORK),
  1357. TEST_ED(certs_ok, TT_FORK),
  1358. TEST_RCV_CERTS(ok),
  1359. TEST_RCV_CERTS_ED(ok, "Ed25519-Link"),
  1360. TEST_RCV_CERTS_RSA(ok_server, "RSA-Auth"),
  1361. TEST_RCV_CERTS_ED(ok_server, "Ed25519-Auth"),
  1362. TEST_RCV_CERTS(badstate),
  1363. TEST_RCV_CERTS(badproto),
  1364. TEST_RCV_CERTS(duplicate),
  1365. TEST_RCV_CERTS(already_authenticated),
  1366. TEST_RCV_CERTS(empty),
  1367. TEST_RCV_CERTS(bad_circid),
  1368. TEST_RCV_CERTS(truncated_1),
  1369. TEST_RCV_CERTS(truncated_2),
  1370. TEST_RCV_CERTS(truncated_3),
  1371. TEST_RCV_CERTS_ED(truncated_4, "Ed25519-Link"),
  1372. TEST_RCV_CERTS_ED(truncated_5, "Ed25519-Link"),
  1373. TEST_RCV_CERTS_ED(truncated_6, "Ed25519-Link"),
  1374. TEST_RCV_CERTS_ED(truncated_7, "Ed25519-Link"),
  1375. TEST_RCV_CERTS(not_x509),
  1376. TEST_RCV_CERTS(both_link),
  1377. TEST_RCV_CERTS(both_id_rsa),
  1378. TEST_RCV_CERTS(both_auth),
  1379. TEST_RCV_CERTS_ED(duplicate_id, "Ed25519-Link"),
  1380. TEST_RCV_CERTS_ED(duplicate_link, "Ed25519-Link"),
  1381. TEST_RCV_CERTS_ED(duplicate_crosscert, "Ed25519-Link"),
  1382. TEST_RCV_CERTS_ED(missing_crosscert, "Ed25519-Link"),
  1383. TEST_RCV_CERTS_ED(missing_id, "Ed25519-Link"),
  1384. TEST_RCV_CERTS_ED(missing_signing_key, "Ed25519-Link"),
  1385. TEST_RCV_CERTS_ED(missing_link, "Ed25519-Link"),
  1386. TEST_RCV_CERTS_ED(missing_auth, "Ed25519-Auth"),
  1387. TEST_RCV_CERTS_ED(missing_rsa_id, "Ed25519-Link"),
  1388. TEST_RCV_CERTS_ED(link_mismatch, "Ed25519-Link"),
  1389. TEST_RCV_CERTS_ED(bad_ed_sig, "Ed25519-Link"),
  1390. TEST_RCV_CERTS_ED(bad_rsa_id_cert, "Ed25519-Link"),
  1391. TEST_RCV_CERTS_ED(bad_crosscert, "Ed25519-Link"),
  1392. TEST_RCV_CERTS_RSA(expired_rsa_id, "RSA-Link"),
  1393. TEST_RCV_CERTS_ED(expired_rsa_id, "Ed25519-Link"),
  1394. TEST_RCV_CERTS_ED(expired_ed_id, "Ed25519-Link"),
  1395. TEST_RCV_CERTS_ED(expired_ed_link, "Ed25519-Link"),
  1396. TEST_RCV_CERTS_ED(expired_crosscert, "Ed25519-Link"),
  1397. TEST_RCV_CERTS(wrong_labels_1),
  1398. TEST_RCV_CERTS(wrong_labels_2),
  1399. TEST_RCV_CERTS(wrong_labels_3),
  1400. TEST_RCV_CERTS(server_missing_certs),
  1401. TEST_RCV_CERTS(server_wrong_labels_1),
  1402. TEST_RSA(send_authchallenge, TT_FORK),
  1403. TEST_RCV_AUTHCHALLENGE(ok),
  1404. TEST_RCV_AUTHCHALLENGE(ok_ed25519),
  1405. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  1406. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  1407. TEST_RCV_AUTHCHALLENGE(badstate),
  1408. TEST_RCV_AUTHCHALLENGE(badproto),
  1409. TEST_RCV_AUTHCHALLENGE(as_server),
  1410. TEST_RCV_AUTHCHALLENGE(duplicate),
  1411. TEST_RCV_AUTHCHALLENGE(nocerts),
  1412. TEST_RCV_AUTHCHALLENGE(tooshort),
  1413. TEST_RCV_AUTHCHALLENGE(truncated),
  1414. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  1415. TEST_AUTHENTICATE(cell),
  1416. TEST_AUTHENTICATE_ED(cell),
  1417. TEST_AUTHENTICATE(badstate),
  1418. TEST_AUTHENTICATE(badproto),
  1419. TEST_AUTHENTICATE(atclient),
  1420. TEST_AUTHENTICATE(duplicate),
  1421. TEST_AUTHENTICATE(already_authenticated),
  1422. TEST_AUTHENTICATE(nocerts),
  1423. TEST_AUTHENTICATE(noidcert),
  1424. TEST_AUTHENTICATE(noauthcert),
  1425. TEST_AUTHENTICATE(tooshort),
  1426. TEST_AUTHENTICATE(badtype),
  1427. TEST_AUTHENTICATE(truncated_1),
  1428. TEST_AUTHENTICATE(truncated_2),
  1429. TEST_AUTHENTICATE(tooshort_1),
  1430. TEST_AUTHENTICATE(badcontent),
  1431. TEST_AUTHENTICATE(badsig_1),
  1432. TEST_AUTHENTICATE_ED(badsig_1),
  1433. TEST_AUTHENTICATE_ED(missing_ed_id),
  1434. TEST_AUTHENTICATE_ED(missing_ed_auth),
  1435. //TEST_AUTHENTICATE(),
  1436. END_OF_TESTCASES
  1437. };