test_link_handshake.c 56 KB

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