test_link_handshake.c 56 KB

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