test_link_handshake.c 57 KB

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