test_link_handshake.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617
  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;
  727. /* Frob a byte in the signature, after making a new cert. (NSS won't let
  728. * us just frob the old cert, since it will see that the issuer & serial
  729. * number are the same, which will make it fail at an earlier stage than
  730. * signature verification.) */
  731. const tor_x509_cert_t *idc;
  732. tor_x509_cert_t *newc;
  733. tor_tls_get_my_certs(1, NULL, &idc);
  734. time_t new_end = time(NULL) + 86400 * 10;
  735. newc = tor_x509_cert_replace_expiration(idc, new_end, d->key2);
  736. const uint8_t *encoded;
  737. size_t encoded_len;
  738. tor_x509_cert_get_der(newc, &encoded, &encoded_len);
  739. certs_cell_cert_setlen_body(cert, encoded_len);
  740. certs_cell_cert_set_cert_len(cert, encoded_len);
  741. body = certs_cell_cert_getarray_body(cert);
  742. memcpy(body, encoded, encoded_len);
  743. body[encoded_len - 13] ^= 7;
  744. REENCODE();
  745. tor_x509_cert_free(newc);
  746. })
  747. CERTS_FAIL(expired_rsa_id, /* both */
  748. {
  749. require_failure_message = "Certificate already expired";
  750. /* we're going to replace the identity cert with an expired one. */
  751. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 1);
  752. const tor_x509_cert_t *idc;
  753. tor_tls_get_my_certs(1, NULL, &idc);
  754. tor_x509_cert_t *newc;
  755. time_t new_end = time(NULL) - 86400 * 10;
  756. newc = tor_x509_cert_replace_expiration(idc, new_end, d->key2);
  757. const uint8_t *encoded;
  758. size_t encoded_len;
  759. tor_x509_cert_get_der(newc, &encoded, &encoded_len);
  760. certs_cell_cert_setlen_body(cert, encoded_len);
  761. certs_cell_cert_set_cert_len(cert, encoded_len);
  762. memcpy(certs_cell_cert_getarray_body(cert), encoded, encoded_len);
  763. REENCODE();
  764. tor_x509_cert_free(newc);
  765. })
  766. CERTS_FAIL(expired_ed_id, /* ed25519 */
  767. {
  768. /* we're going to replace the Ed Id->sign cert with an expired one. */
  769. require_failure_message = "At least one certificate expired";
  770. /* We don't need to re-sign, since we check for expiration first. */
  771. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 2);
  772. uint8_t *body = certs_cell_cert_getarray_body(cert);
  773. /* The expiration field is bytes [2..5]. It is in HOURS since the
  774. * epoch. */
  775. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  776. REENCODE();
  777. })
  778. CERTS_FAIL(expired_ed_link, /* ed25519 */
  779. {
  780. /* we're going to replace the Ed Sign->link cert with an expired one. */
  781. require_failure_message = "At least one certificate expired";
  782. /* We don't need to re-sign, since we check for expiration first. */
  783. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 3);
  784. uint8_t *body = certs_cell_cert_getarray_body(cert);
  785. /* The expiration field is bytes [2..5]. It is in HOURS since the
  786. * epoch. */
  787. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  788. REENCODE();
  789. })
  790. CERTS_FAIL(expired_crosscert, /* ed25519 */
  791. {
  792. /* we're going to replace the Ed Sign->link cert with an expired one. */
  793. require_failure_message = "Crosscert is expired";
  794. /* We don't need to re-sign, since we check for expiration first. */
  795. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 4);
  796. uint8_t *body = certs_cell_cert_getarray_body(cert);
  797. /* The expiration field is bytes [32..35]. once again, HOURS. */
  798. set_uint32(body+32, htonl(24)); /* Back to jan 2, 1970. */
  799. REENCODE();
  800. })
  801. CERTS_FAIL(wrong_labels_1,
  802. {
  803. require_failure_message = "The link certificate was not valid";
  804. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  805. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  806. REENCODE();
  807. })
  808. CERTS_FAIL(wrong_labels_2,
  809. {
  810. const tor_x509_cert_t *a;
  811. const tor_x509_cert_t *b;
  812. const uint8_t *enca;
  813. size_t lena;
  814. require_failure_message = "The link certificate was not valid";
  815. tor_tls_get_my_certs(1, &a, &b);
  816. tor_x509_cert_get_der(a, &enca, &lena);
  817. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  818. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  819. enca, lena);
  820. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  821. REENCODE();
  822. })
  823. CERTS_FAIL(wrong_labels_3,
  824. {
  825. require_failure_message =
  826. "The certs we wanted (ID, Link) were missing";
  827. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  828. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  829. REENCODE();
  830. })
  831. CERTS_FAIL(server_missing_certs,
  832. {
  833. require_failure_message =
  834. "The certs we wanted (ID, Auth) were missing";
  835. d->c->handshake_state->started_here = 0;
  836. d->c->handshake_state->certs->started_here = 0;
  837. })
  838. CERTS_FAIL(server_wrong_labels_1,
  839. {
  840. require_failure_message =
  841. "The authentication certificate was not valid";
  842. d->c->handshake_state->started_here = 0;
  843. d->c->handshake_state->certs->started_here = 0;
  844. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  845. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  846. REENCODE();
  847. })
  848. static void
  849. test_link_handshake_send_authchallenge(void *arg)
  850. {
  851. (void)arg;
  852. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  853. var_cell_t *cell1=NULL, *cell2=NULL;
  854. crypto_pk_t *rsa0 = pk_generate(0), *rsa1 = pk_generate(1);
  855. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  856. rsa0, rsa1, 86400), OP_EQ, 0);
  857. init_mock_ed_keys(rsa0);
  858. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  859. tt_int_op(connection_init_or_handshake_state(c1, 0), OP_EQ, 0);
  860. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  861. tt_ptr_op(mock_got_var_cell, OP_EQ, NULL);
  862. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  863. cell1 = mock_got_var_cell;
  864. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  865. cell2 = mock_got_var_cell;
  866. #ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
  867. tt_int_op(38, OP_EQ, cell1->payload_len);
  868. tt_int_op(38, OP_EQ, cell2->payload_len);
  869. #else
  870. tt_int_op(36, OP_EQ, cell1->payload_len);
  871. tt_int_op(36, OP_EQ, cell2->payload_len);
  872. #endif
  873. tt_int_op(0, OP_EQ, cell1->circ_id);
  874. tt_int_op(0, OP_EQ, cell2->circ_id);
  875. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell1->command);
  876. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell2->command);
  877. #ifdef HAVE_WORKING_TOR_TLS_GET_TLSSECRETS
  878. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell1->payload + 32, 6);
  879. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell2->payload + 32, 6);
  880. #else
  881. tt_mem_op("\x00\x01\x00\x03", OP_EQ, cell1->payload + 32, 4);
  882. tt_mem_op("\x00\x01\x00\x03", OP_EQ, cell2->payload + 32, 4);
  883. #endif
  884. tt_mem_op(cell1->payload, OP_NE, cell2->payload, 32);
  885. done:
  886. UNMOCK(connection_or_write_var_cell_to_buf);
  887. connection_free_minimal(TO_CONN(c1));
  888. tor_free(cell1);
  889. tor_free(cell2);
  890. crypto_pk_free(rsa0);
  891. crypto_pk_free(rsa1);
  892. }
  893. typedef struct authchallenge_data_s {
  894. or_connection_t *c;
  895. channel_tls_t *chan;
  896. var_cell_t *cell;
  897. } authchallenge_data_t;
  898. static int
  899. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  900. {
  901. (void)test;
  902. authchallenge_data_t *d = obj;
  903. UNMOCK(connection_or_send_netinfo);
  904. UNMOCK(connection_or_close_for_error);
  905. UNMOCK(connection_or_send_authenticate_cell);
  906. if (d) {
  907. tor_free(d->cell);
  908. connection_free_minimal(TO_CONN(d->c));
  909. circuitmux_free(d->chan->base_.cmux);
  910. tor_free(d->chan);
  911. tor_free(d);
  912. }
  913. return 1;
  914. }
  915. static void *
  916. recv_authchallenge_setup(const struct testcase_t *test)
  917. {
  918. (void)test;
  919. testing__connection_or_pretend_TLSSECRET_is_supported = 1;
  920. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  921. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  922. d->chan = tor_malloc_zero(sizeof(*d->chan));
  923. d->c->chan = d->chan;
  924. d->c->base_.address = tor_strdup("HaveAnAddress");
  925. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  926. d->chan->conn = d->c;
  927. tt_int_op(connection_init_or_handshake_state(d->c, 1), OP_EQ, 0);
  928. d->c->link_proto = 4;
  929. d->c->handshake_state->received_certs_cell = 1;
  930. d->cell = var_cell_new(128);
  931. d->cell->payload_len = 38;
  932. d->cell->payload[33] = 2; /* 2 methods */
  933. d->cell->payload[35] = 7; /* This one isn't real */
  934. d->cell->payload[37] = 1; /* This is the old RSA one. */
  935. d->cell->command = CELL_AUTH_CHALLENGE;
  936. get_options_mutable()->ORPort_set = 1;
  937. MOCK(connection_or_close_for_error, mock_close_for_err);
  938. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  939. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  940. tt_int_op(0, OP_EQ, d->c->handshake_state->received_auth_challenge);
  941. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  942. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  943. return d;
  944. done:
  945. recv_authchallenge_cleanup(test, d);
  946. return NULL;
  947. }
  948. static struct testcase_setup_t setup_recv_authchallenge = {
  949. .setup_fn = recv_authchallenge_setup,
  950. .cleanup_fn = recv_authchallenge_cleanup
  951. };
  952. static void
  953. test_link_handshake_recv_authchallenge_ok(void *arg)
  954. {
  955. authchallenge_data_t *d = arg;
  956. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  957. tt_int_op(0, OP_EQ, mock_close_called);
  958. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  959. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  960. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  961. tt_int_op(1, OP_EQ, mock_send_authenticate_called_with_type); /* RSA */
  962. done:
  963. ;
  964. }
  965. static void
  966. test_link_handshake_recv_authchallenge_ok_ed25519(void *arg)
  967. {
  968. authchallenge_data_t *d = arg;
  969. /* Add the ed25519 authentication mechanism here. */
  970. d->cell->payload[33] = 3; /* 3 types are supported now. */
  971. d->cell->payload[39] = 3;
  972. d->cell->payload_len += 2;
  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(1, OP_EQ, mock_send_authenticate_called);
  977. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  978. tt_int_op(3, OP_EQ, mock_send_authenticate_called_with_type); /* Ed25519 */
  979. done:
  980. ;
  981. }
  982. static void
  983. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  984. {
  985. authchallenge_data_t *d = arg;
  986. get_options_mutable()->ORPort_set = 0;
  987. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  988. tt_int_op(0, OP_EQ, mock_close_called);
  989. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  990. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  991. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  992. done:
  993. ;
  994. }
  995. static void
  996. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  997. {
  998. authchallenge_data_t *d = arg;
  999. d->cell->payload[37] = 99;
  1000. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  1001. tt_int_op(0, OP_EQ, mock_close_called);
  1002. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  1003. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  1004. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  1005. done:
  1006. ;
  1007. }
  1008. #define AUTHCHALLENGE_FAIL(name, code) \
  1009. static void \
  1010. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  1011. { \
  1012. authchallenge_data_t *d = arg; \
  1013. const char *require_failure_message = NULL; \
  1014. setup_capture_of_logs(LOG_INFO); \
  1015. { code ; } \
  1016. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  1017. tt_int_op(1, OP_EQ, mock_close_called); \
  1018. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  1019. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  1020. if (require_failure_message) { \
  1021. expect_log_msg_containing(require_failure_message); \
  1022. } \
  1023. done: \
  1024. teardown_capture_of_logs(); \
  1025. }
  1026. AUTHCHALLENGE_FAIL(badstate,
  1027. require_failure_message = "We're not currently doing a "
  1028. "v3 handshake";
  1029. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  1030. AUTHCHALLENGE_FAIL(badproto,
  1031. require_failure_message = "not using link protocol >= 3";
  1032. d->c->link_proto = 2)
  1033. AUTHCHALLENGE_FAIL(as_server,
  1034. require_failure_message = "We didn't originate this "
  1035. "connection";
  1036. d->c->handshake_state->started_here = 0;
  1037. d->c->handshake_state->certs->started_here = 0;)
  1038. AUTHCHALLENGE_FAIL(duplicate,
  1039. require_failure_message = "We already received one";
  1040. d->c->handshake_state->received_auth_challenge = 1)
  1041. AUTHCHALLENGE_FAIL(nocerts,
  1042. require_failure_message = "We haven't gotten a CERTS "
  1043. "cell yet";
  1044. d->c->handshake_state->received_certs_cell = 0)
  1045. AUTHCHALLENGE_FAIL(tooshort,
  1046. require_failure_message = "It was not well-formed";
  1047. d->cell->payload_len = 33)
  1048. AUTHCHALLENGE_FAIL(truncated,
  1049. require_failure_message = "It was not well-formed";
  1050. d->cell->payload_len = 34)
  1051. AUTHCHALLENGE_FAIL(nonzero_circid,
  1052. require_failure_message = "It had a nonzero circuit ID";
  1053. d->cell->circ_id = 1337)
  1054. static int
  1055. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  1056. {
  1057. (void)tls;
  1058. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  1059. return 0;
  1060. }
  1061. static void
  1062. mock_set_circid_type(channel_t *chan,
  1063. crypto_pk_t *identity_rcvd,
  1064. int consider_identity)
  1065. {
  1066. (void) chan;
  1067. (void) identity_rcvd;
  1068. (void) consider_identity;
  1069. }
  1070. typedef struct authenticate_data_s {
  1071. int is_ed;
  1072. or_connection_t *c1, *c2;
  1073. channel_tls_t *chan2;
  1074. var_cell_t *cell;
  1075. crypto_pk_t *key1, *key2;
  1076. } authenticate_data_t;
  1077. static int
  1078. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  1079. {
  1080. (void) test;
  1081. UNMOCK(connection_or_write_var_cell_to_buf);
  1082. UNMOCK(tor_tls_get_peer_cert);
  1083. UNMOCK(tor_tls_get_own_cert);
  1084. UNMOCK(tor_tls_get_tlssecrets);
  1085. UNMOCK(connection_or_close_for_error);
  1086. UNMOCK(channel_set_circid_type);
  1087. UNMOCK(tor_tls_export_key_material);
  1088. authenticate_data_t *d = arg;
  1089. if (d) {
  1090. tor_free(d->cell);
  1091. connection_or_clear_identity(d->c1);
  1092. connection_or_clear_identity(d->c2);
  1093. connection_free_minimal(TO_CONN(d->c1));
  1094. connection_free_minimal(TO_CONN(d->c2));
  1095. circuitmux_free(d->chan2->base_.cmux);
  1096. tor_free(d->chan2);
  1097. crypto_pk_free(d->key1);
  1098. crypto_pk_free(d->key2);
  1099. tor_free(d);
  1100. }
  1101. tor_x509_cert_free(mock_peer_cert);
  1102. tor_x509_cert_free(mock_own_cert);
  1103. mock_peer_cert = NULL;
  1104. mock_own_cert = NULL;
  1105. return 1;
  1106. }
  1107. static void *
  1108. authenticate_data_setup(const struct testcase_t *test)
  1109. {
  1110. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  1111. int is_ed = d->is_ed = (test->setup_data == (void*)3);
  1112. testing__connection_or_pretend_TLSSECRET_is_supported = 1;
  1113. scheduler_init();
  1114. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  1115. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  1116. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  1117. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  1118. MOCK(connection_or_close_for_error, mock_close_for_err);
  1119. MOCK(channel_set_circid_type, mock_set_circid_type);
  1120. MOCK(tor_tls_export_key_material, mock_export_key_material);
  1121. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1122. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1123. tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
  1124. tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
  1125. d->key1 = pk_generate(2);
  1126. d->key2 = pk_generate(3);
  1127. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  1128. d->key1, d->key2, 86400), OP_EQ, 0);
  1129. init_mock_ed_keys(d->key2);
  1130. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1131. d->c1->link_proto = 3;
  1132. tt_int_op(connection_init_or_handshake_state(d->c1, 1), OP_EQ, 0);
  1133. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1134. d->c2->link_proto = 3;
  1135. tt_int_op(connection_init_or_handshake_state(d->c2, 0), OP_EQ, 0);
  1136. var_cell_t *cell = var_cell_new(16);
  1137. cell->command = CELL_CERTS;
  1138. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  1139. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  1140. memset(cell->payload, 0xf0, 16);
  1141. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  1142. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  1143. tor_free(cell);
  1144. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  1145. channel_tls_common_init(d->chan2);
  1146. d->c2->chan = d->chan2;
  1147. d->chan2->conn = d->c2;
  1148. d->c2->base_.address = tor_strdup("C2");
  1149. d->c2->tls = tor_tls_new(-1, 1);
  1150. d->c2->handshake_state->received_certs_cell = 1;
  1151. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  1152. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  1153. const uint8_t *der;
  1154. size_t sz;
  1155. tor_x509_cert_get_der(id_cert, &der, &sz);
  1156. d->c1->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1157. d->c2->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1158. if (is_ed) {
  1159. d->c1->handshake_state->certs->ed_id_sign =
  1160. tor_cert_dup(get_master_signing_key_cert());
  1161. d->c2->handshake_state->certs->ed_id_sign =
  1162. tor_cert_dup(get_master_signing_key_cert());
  1163. d->c2->handshake_state->certs->ed_sign_auth =
  1164. tor_cert_dup(get_current_auth_key_cert());
  1165. } else {
  1166. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  1167. tor_x509_cert_get_der(auth_cert, &der, &sz);
  1168. d->c2->handshake_state->certs->auth_cert = tor_x509_cert_decode(der, sz);
  1169. }
  1170. tor_x509_cert_get_der(link_cert, &der, &sz);
  1171. mock_peer_cert = tor_x509_cert_decode(der, sz);
  1172. tt_assert(mock_peer_cert);
  1173. mock_own_cert = tor_x509_cert_decode(der, sz);
  1174. tt_assert(mock_own_cert);
  1175. /* Make an authenticate cell ... */
  1176. int authtype;
  1177. if (is_ed)
  1178. authtype = AUTHTYPE_ED25519_SHA256_RFC5705;
  1179. else
  1180. authtype = AUTHTYPE_RSA_SHA256_TLSSECRET;
  1181. tt_int_op(0, OP_EQ, connection_or_send_authenticate_cell(d->c1, authtype));
  1182. tt_assert(mock_got_var_cell);
  1183. d->cell = mock_got_var_cell;
  1184. mock_got_var_cell = NULL;
  1185. return d;
  1186. done:
  1187. authenticate_data_cleanup(test, d);
  1188. return NULL;
  1189. }
  1190. static struct testcase_setup_t setup_authenticate = {
  1191. .setup_fn = authenticate_data_setup,
  1192. .cleanup_fn = authenticate_data_cleanup
  1193. };
  1194. static void
  1195. test_link_handshake_auth_cell(void *arg)
  1196. {
  1197. authenticate_data_t *d = arg;
  1198. auth1_t *auth1 = NULL;
  1199. crypto_pk_t *auth_pubkey = NULL;
  1200. /* Is the cell well-formed on the outer layer? */
  1201. tt_int_op(d->cell->command, OP_EQ, CELL_AUTHENTICATE);
  1202. tt_int_op(d->cell->payload[0], OP_EQ, 0);
  1203. if (d->is_ed)
  1204. tt_int_op(d->cell->payload[1], OP_EQ, 3);
  1205. else
  1206. tt_int_op(d->cell->payload[1], OP_EQ, 1);
  1207. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), OP_EQ,
  1208. d->cell->payload_len - 4);
  1209. /* Check it out for plausibility... */
  1210. auth_ctx_t ctx;
  1211. ctx.is_ed = d->is_ed;
  1212. tt_int_op(d->cell->payload_len-4, OP_EQ, auth1_parse(&auth1,
  1213. d->cell->payload+4,
  1214. d->cell->payload_len - 4, &ctx));
  1215. tt_assert(auth1);
  1216. if (d->is_ed) {
  1217. tt_mem_op(auth1->type, OP_EQ, "AUTH0003", 8);
  1218. } else {
  1219. tt_mem_op(auth1->type, OP_EQ, "AUTH0001", 8);
  1220. }
  1221. tt_mem_op(auth1->tlssecrets, OP_EQ, "int getRandomNumber(){return 4;}", 32);
  1222. /* Is the signature okay? */
  1223. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  1224. if (d->is_ed) {
  1225. ed25519_signature_t sig;
  1226. tt_int_op(auth1_getlen_sig(auth1), OP_EQ, ED25519_SIG_LEN);
  1227. memcpy(&sig.sig, auth1_getarray_sig(auth1), ED25519_SIG_LEN);
  1228. tt_assert(!ed25519_checksig(&sig, start, end-start,
  1229. &get_current_auth_keypair()->pubkey));
  1230. } else {
  1231. uint8_t sig[128];
  1232. uint8_t digest[32];
  1233. tt_int_op(auth1_getlen_sig(auth1), OP_GT, 120);
  1234. auth_pubkey = tor_tls_cert_get_key(
  1235. d->c2->handshake_state->certs->auth_cert);
  1236. int n = crypto_pk_public_checksig(
  1237. auth_pubkey,
  1238. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  1239. auth1_getlen_sig(auth1));
  1240. tt_int_op(n, OP_EQ, 32);
  1241. crypto_digest256((char*)digest,
  1242. (const char*)start, end-start, DIGEST_SHA256);
  1243. tt_mem_op(sig, OP_EQ, digest, 32);
  1244. }
  1245. /* Then feed it to c2. */
  1246. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0);
  1247. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1248. tt_int_op(mock_close_called, OP_EQ, 0);
  1249. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1250. if (d->is_ed) {
  1251. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 1);
  1252. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1253. } else {
  1254. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 0);
  1255. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1256. }
  1257. done:
  1258. auth1_free(auth1);
  1259. crypto_pk_free(auth_pubkey);
  1260. }
  1261. #define AUTHENTICATE_FAIL(name, code) \
  1262. static void \
  1263. test_link_handshake_auth_ ## name(void *arg) \
  1264. { \
  1265. authenticate_data_t *d = arg; \
  1266. const char *require_failure_message = NULL; \
  1267. setup_capture_of_logs(LOG_INFO); \
  1268. { code ; } \
  1269. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  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, 0); \
  1273. if (require_failure_message) { \
  1274. expect_log_msg_containing(require_failure_message); \
  1275. } \
  1276. done: \
  1277. teardown_capture_of_logs(); \
  1278. }
  1279. AUTHENTICATE_FAIL(badstate,
  1280. require_failure_message = "We're not doing a v3 handshake";
  1281. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  1282. AUTHENTICATE_FAIL(badproto,
  1283. require_failure_message = "not using link protocol >= 3";
  1284. d->c2->link_proto = 2)
  1285. AUTHENTICATE_FAIL(atclient,
  1286. require_failure_message = "We originated this connection";
  1287. d->c2->handshake_state->started_here = 1;
  1288. d->c2->handshake_state->certs->started_here = 1;)
  1289. AUTHENTICATE_FAIL(duplicate,
  1290. require_failure_message = "We already got one";
  1291. d->c2->handshake_state->received_authenticate = 1)
  1292. static void
  1293. test_link_handshake_auth_already_authenticated(void *arg)
  1294. {
  1295. authenticate_data_t *d = arg;
  1296. setup_capture_of_logs(LOG_INFO);
  1297. d->c2->handshake_state->authenticated = 1;
  1298. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1299. tt_int_op(mock_close_called, OP_EQ, 1);
  1300. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1301. expect_log_msg_containing("The peer is already authenticated");
  1302. done:
  1303. teardown_capture_of_logs();
  1304. }
  1305. AUTHENTICATE_FAIL(nocerts,
  1306. require_failure_message = "We never got a certs cell";
  1307. d->c2->handshake_state->received_certs_cell = 0)
  1308. AUTHENTICATE_FAIL(noidcert,
  1309. require_failure_message = "We never got an identity "
  1310. "certificate";
  1311. tor_x509_cert_free(d->c2->handshake_state->certs->id_cert);
  1312. d->c2->handshake_state->certs->id_cert = NULL)
  1313. AUTHENTICATE_FAIL(noauthcert,
  1314. require_failure_message = "We never got an RSA "
  1315. "authentication certificate";
  1316. tor_x509_cert_free(d->c2->handshake_state->certs->auth_cert);
  1317. d->c2->handshake_state->certs->auth_cert = NULL)
  1318. AUTHENTICATE_FAIL(tooshort,
  1319. require_failure_message = "Cell was way too short";
  1320. d->cell->payload_len = 3)
  1321. AUTHENTICATE_FAIL(badtype,
  1322. require_failure_message = "Authenticator type was not "
  1323. "recognized";
  1324. d->cell->payload[0] = 0xff)
  1325. AUTHENTICATE_FAIL(truncated_1,
  1326. require_failure_message = "Authenticator was truncated";
  1327. d->cell->payload[2]++)
  1328. AUTHENTICATE_FAIL(truncated_2,
  1329. require_failure_message = "Authenticator was truncated";
  1330. d->cell->payload[3]++)
  1331. AUTHENTICATE_FAIL(tooshort_1,
  1332. require_failure_message = "Authenticator was too short";
  1333. tt_int_op(d->cell->payload_len, OP_GE, 260);
  1334. d->cell->payload[2] -= 1;
  1335. d->cell->payload_len -= 256;)
  1336. AUTHENTICATE_FAIL(badcontent,
  1337. require_failure_message = "Some field in the AUTHENTICATE "
  1338. "cell body was not as expected";
  1339. d->cell->payload[10] ^= 0xff)
  1340. AUTHENTICATE_FAIL(badsig_1,
  1341. if (d->is_ed)
  1342. require_failure_message = "Ed25519 signature wasn't valid";
  1343. else
  1344. require_failure_message = "RSA signature wasn't valid";
  1345. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  1346. AUTHENTICATE_FAIL(missing_ed_id,
  1347. {
  1348. tor_cert_free(d->c2->handshake_state->certs->ed_id_sign);
  1349. d->c2->handshake_state->certs->ed_id_sign = NULL;
  1350. require_failure_message = "Ed authenticate without Ed ID "
  1351. "cert from peer";
  1352. })
  1353. AUTHENTICATE_FAIL(missing_ed_auth,
  1354. {
  1355. tor_cert_free(d->c2->handshake_state->certs->ed_sign_auth);
  1356. d->c2->handshake_state->certs->ed_sign_auth = NULL;
  1357. require_failure_message = "We never got an Ed25519 "
  1358. "authentication certificate";
  1359. })
  1360. #define TEST_RSA(name, flags) \
  1361. { #name , test_link_handshake_ ## name, (flags), \
  1362. &passthrough_setup, (void*)"RSA" }
  1363. #define TEST_ED(name, flags) \
  1364. { #name "_ed25519" , test_link_handshake_ ## name, (flags), \
  1365. &passthrough_setup, (void*)"Ed25519" }
  1366. #define TEST_RCV_AUTHCHALLENGE(name) \
  1367. { "recv_authchallenge/" #name , \
  1368. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  1369. &setup_recv_authchallenge, NULL }
  1370. #define TEST_RCV_CERTS(name) \
  1371. { "recv_certs/" #name , \
  1372. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1373. &setup_recv_certs, (void*)"RSA-Link" }
  1374. #define TEST_RCV_CERTS_RSA(name,type) \
  1375. { "recv_certs/" #name , \
  1376. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1377. &setup_recv_certs, (void*)type }
  1378. #define TEST_RCV_CERTS_ED(name, type) \
  1379. { "recv_certs/" #name "_ed25519", \
  1380. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1381. &setup_recv_certs, (void*)type }
  1382. #define TEST_AUTHENTICATE(name) \
  1383. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  1384. &setup_authenticate, NULL }
  1385. #define TEST_AUTHENTICATE_ED(name) \
  1386. { "authenticate/" #name "_ed25519" , test_link_handshake_auth_ ## name, \
  1387. TT_FORK, &setup_authenticate, (void*)3 }
  1388. struct testcase_t link_handshake_tests[] = {
  1389. TEST_RSA(certs_ok, TT_FORK),
  1390. TEST_ED(certs_ok, TT_FORK),
  1391. TEST_RCV_CERTS(ok),
  1392. TEST_RCV_CERTS_ED(ok, "Ed25519-Link"),
  1393. TEST_RCV_CERTS_RSA(ok_server, "RSA-Auth"),
  1394. TEST_RCV_CERTS_ED(ok_server, "Ed25519-Auth"),
  1395. TEST_RCV_CERTS(badstate),
  1396. TEST_RCV_CERTS(badproto),
  1397. TEST_RCV_CERTS(duplicate),
  1398. TEST_RCV_CERTS(already_authenticated),
  1399. TEST_RCV_CERTS(empty),
  1400. TEST_RCV_CERTS(bad_circid),
  1401. TEST_RCV_CERTS(truncated_1),
  1402. TEST_RCV_CERTS(truncated_2),
  1403. TEST_RCV_CERTS(truncated_3),
  1404. TEST_RCV_CERTS_ED(truncated_4, "Ed25519-Link"),
  1405. TEST_RCV_CERTS_ED(truncated_5, "Ed25519-Link"),
  1406. TEST_RCV_CERTS_ED(truncated_6, "Ed25519-Link"),
  1407. TEST_RCV_CERTS_ED(truncated_7, "Ed25519-Link"),
  1408. TEST_RCV_CERTS(not_x509),
  1409. TEST_RCV_CERTS(both_link),
  1410. TEST_RCV_CERTS(both_id_rsa),
  1411. TEST_RCV_CERTS(both_auth),
  1412. TEST_RCV_CERTS_ED(duplicate_id, "Ed25519-Link"),
  1413. TEST_RCV_CERTS_ED(duplicate_link, "Ed25519-Link"),
  1414. TEST_RCV_CERTS_ED(duplicate_crosscert, "Ed25519-Link"),
  1415. TEST_RCV_CERTS_ED(missing_crosscert, "Ed25519-Link"),
  1416. TEST_RCV_CERTS_ED(missing_id, "Ed25519-Link"),
  1417. TEST_RCV_CERTS_ED(missing_signing_key, "Ed25519-Link"),
  1418. TEST_RCV_CERTS_ED(missing_link, "Ed25519-Link"),
  1419. TEST_RCV_CERTS_ED(missing_auth, "Ed25519-Auth"),
  1420. TEST_RCV_CERTS_ED(missing_rsa_id, "Ed25519-Link"),
  1421. TEST_RCV_CERTS_ED(link_mismatch, "Ed25519-Link"),
  1422. TEST_RCV_CERTS_ED(bad_ed_sig, "Ed25519-Link"),
  1423. TEST_RCV_CERTS_ED(bad_rsa_id_cert, "Ed25519-Link"),
  1424. TEST_RCV_CERTS_ED(bad_crosscert, "Ed25519-Link"),
  1425. TEST_RCV_CERTS_RSA(expired_rsa_id, "RSA-Link"),
  1426. TEST_RCV_CERTS_ED(expired_rsa_id, "Ed25519-Link"),
  1427. TEST_RCV_CERTS_ED(expired_ed_id, "Ed25519-Link"),
  1428. TEST_RCV_CERTS_ED(expired_ed_link, "Ed25519-Link"),
  1429. TEST_RCV_CERTS_ED(expired_crosscert, "Ed25519-Link"),
  1430. TEST_RCV_CERTS(wrong_labels_1),
  1431. TEST_RCV_CERTS(wrong_labels_2),
  1432. TEST_RCV_CERTS(wrong_labels_3),
  1433. TEST_RCV_CERTS(server_missing_certs),
  1434. TEST_RCV_CERTS(server_wrong_labels_1),
  1435. TEST_RSA(send_authchallenge, TT_FORK),
  1436. TEST_RCV_AUTHCHALLENGE(ok),
  1437. TEST_RCV_AUTHCHALLENGE(ok_ed25519),
  1438. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  1439. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  1440. TEST_RCV_AUTHCHALLENGE(badstate),
  1441. TEST_RCV_AUTHCHALLENGE(badproto),
  1442. TEST_RCV_AUTHCHALLENGE(as_server),
  1443. TEST_RCV_AUTHCHALLENGE(duplicate),
  1444. TEST_RCV_AUTHCHALLENGE(nocerts),
  1445. TEST_RCV_AUTHCHALLENGE(tooshort),
  1446. TEST_RCV_AUTHCHALLENGE(truncated),
  1447. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  1448. TEST_AUTHENTICATE(cell),
  1449. TEST_AUTHENTICATE_ED(cell),
  1450. TEST_AUTHENTICATE(badstate),
  1451. TEST_AUTHENTICATE(badproto),
  1452. TEST_AUTHENTICATE(atclient),
  1453. TEST_AUTHENTICATE(duplicate),
  1454. TEST_AUTHENTICATE(already_authenticated),
  1455. TEST_AUTHENTICATE(nocerts),
  1456. TEST_AUTHENTICATE(noidcert),
  1457. TEST_AUTHENTICATE(noauthcert),
  1458. TEST_AUTHENTICATE(tooshort),
  1459. TEST_AUTHENTICATE(badtype),
  1460. TEST_AUTHENTICATE(truncated_1),
  1461. TEST_AUTHENTICATE(truncated_2),
  1462. TEST_AUTHENTICATE(tooshort_1),
  1463. TEST_AUTHENTICATE(badcontent),
  1464. TEST_AUTHENTICATE(badsig_1),
  1465. TEST_AUTHENTICATE_ED(badsig_1),
  1466. TEST_AUTHENTICATE_ED(missing_ed_id),
  1467. TEST_AUTHENTICATE_ED(missing_ed_auth),
  1468. //TEST_AUTHENTICATE(),
  1469. END_OF_TESTCASES
  1470. };