test_link_handshake.c 56 KB

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