test_link_handshake.c 57 KB

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