test_link_handshake.c 57 KB

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