test_link_handshake.c 58 KB

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