test_link_handshake.c 55 KB

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