test_link_handshake.c 57 KB

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