test_link_handshake.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /* Copyright (c) 2014-2018, 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 "core/or/or.h"
  9. #include "app/config/config.h"
  10. #include "core/mainloop/connection.h"
  11. #include "core/or/connection_or.h"
  12. #include "core/or/channeltls.h"
  13. #include "trunnel/link_handshake.h"
  14. #include "feature/relay/router.h"
  15. #include "feature/relay/routerkeys.h"
  16. #include "core/or/scheduler.h"
  17. #include "feature/nodelist/torcert.h"
  18. #include "core/or/or_connection_st.h"
  19. #include "core/or/or_handshake_certs_st.h"
  20. #include "core/or/or_handshake_state_st.h"
  21. #include "core/or/var_cell_st.h"
  22. #include "lib/tls/tortls.h"
  23. #include "test/test.h"
  24. #include "test/log_test_helpers.h"
  25. static var_cell_t *mock_got_var_cell = NULL;
  26. static void
  27. mock_write_var_cell(const var_cell_t *vc, or_connection_t *conn)
  28. {
  29. (void)conn;
  30. var_cell_t *newcell = var_cell_new(vc->payload_len);
  31. memcpy(newcell, vc, sizeof(var_cell_t));
  32. memcpy(newcell->payload, vc->payload, vc->payload_len);
  33. mock_got_var_cell = newcell;
  34. }
  35. static int
  36. mock_tls_cert_matches_key(const tor_tls_t *tls, const tor_x509_cert_t *cert)
  37. {
  38. (void) tls;
  39. (void) cert; // XXXX look at this.
  40. return 1;
  41. }
  42. static tor_tls_t *mock_peer_cert_expect_tortls = NULL;
  43. static tor_x509_cert_t *mock_peer_cert = NULL;
  44. static tor_x509_cert_t *
  45. mock_get_peer_cert(tor_tls_t *tls)
  46. {
  47. if (mock_peer_cert_expect_tortls &&
  48. mock_peer_cert_expect_tortls != tls)
  49. return NULL;
  50. return tor_x509_cert_dup(mock_peer_cert);
  51. }
  52. static int mock_send_netinfo_called = 0;
  53. static int
  54. mock_send_netinfo(or_connection_t *conn)
  55. {
  56. (void) conn;
  57. ++mock_send_netinfo_called;// XXX check_this
  58. return 0;
  59. }
  60. static int mock_close_called = 0;
  61. static void
  62. mock_close_for_err(or_connection_t *orconn, int flush)
  63. {
  64. (void)orconn;
  65. (void)flush;
  66. ++mock_close_called;
  67. }
  68. static int mock_send_authenticate_called = 0;
  69. static int mock_send_authenticate_called_with_type = 0;
  70. static int
  71. mock_send_authenticate(or_connection_t *conn, int type)
  72. {
  73. (void) conn;
  74. mock_send_authenticate_called_with_type = type;
  75. ++mock_send_authenticate_called;// XXX check_this
  76. return 0;
  77. }
  78. static int
  79. mock_export_key_material(tor_tls_t *tls, uint8_t *secrets_out,
  80. const uint8_t *context,
  81. size_t context_len,
  82. const char *label)
  83. {
  84. (void) tls;
  85. (void)secrets_out;
  86. (void)context;
  87. (void)context_len;
  88. (void)label;
  89. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  90. return 0;
  91. }
  92. static tor_x509_cert_t *mock_own_cert = NULL;
  93. static tor_x509_cert_t *
  94. mock_get_own_cert(tor_tls_t *tls)
  95. {
  96. (void)tls;
  97. return tor_x509_cert_dup(mock_own_cert);
  98. }
  99. /* Test good certs cells */
  100. static void
  101. test_link_handshake_certs_ok(void *arg)
  102. {
  103. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  104. or_connection_t *c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  105. var_cell_t *cell1 = NULL, *cell2 = NULL;
  106. certs_cell_t *cc1 = NULL, *cc2 = NULL;
  107. channel_tls_t *chan1 = NULL, *chan2 = NULL;
  108. crypto_pk_t *key1 = NULL, *key2 = NULL;
  109. const int with_ed = !strcmp((const char *)arg, "Ed25519");
  110. tor_addr_from_ipv4h(&c1->base_.addr, 0x7f000001);
  111. tor_addr_from_ipv4h(&c2->base_.addr, 0x7f000001);
  112. scheduler_init();
  113. MOCK(tor_tls_cert_matches_key, mock_tls_cert_matches_key);
  114. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  115. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  116. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  117. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  118. key1 = pk_generate(2);
  119. key2 = pk_generate(3);
  120. /* We need to make sure that our TLS certificates are set up before we can
  121. * actually generate a CERTS cell.
  122. */
  123. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  124. key1, key2, 86400), OP_EQ, 0);
  125. if (with_ed) {
  126. /* If we're making a CERTS cell for an ed handshake, let's make sure we
  127. * have some Ed25519 certificates and keys. */
  128. init_mock_ed_keys(key2);
  129. } else {
  130. certs_cell_ed25519_disabled_for_testing = 1;
  131. }
  132. /* c1 has started_here == 1 */
  133. {
  134. const tor_x509_cert_t *link_cert = NULL;
  135. tt_assert(!tor_tls_get_my_certs(1, &link_cert, NULL));
  136. mock_own_cert = tor_x509_cert_dup(link_cert);
  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), OP_EQ, 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), OP_EQ, 0);
  145. tt_int_op(0, OP_EQ, connection_or_send_certs_cell(c1));
  146. tt_assert(mock_got_var_cell);
  147. cell1 = mock_got_var_cell;
  148. tt_int_op(0, OP_EQ, 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, OP_EQ, CELL_CERTS);
  152. tt_int_op(cell1->payload_len, OP_GT, 1);
  153. tt_int_op(cell2->command, OP_EQ, CELL_CERTS);
  154. tt_int_op(cell2->payload_len, OP_GT, 1);
  155. tt_int_op(cell1->payload_len, OP_EQ,
  156. certs_cell_parse(&cc1, cell1->payload, cell1->payload_len));
  157. tt_int_op(cell2->payload_len, OP_EQ,
  158. certs_cell_parse(&cc2, cell2->payload, cell2->payload_len));
  159. if (with_ed) {
  160. tt_int_op(5, OP_EQ, cc1->n_certs);
  161. tt_int_op(5, OP_EQ, cc2->n_certs);
  162. } else {
  163. tt_int_op(2, OP_EQ, cc1->n_certs);
  164. tt_int_op(2, OP_EQ, cc2->n_certs);
  165. }
  166. tt_int_op(certs_cell_get_certs(cc1, 0)->cert_type, OP_EQ,
  167. CERTTYPE_RSA1024_ID_AUTH);
  168. tt_int_op(certs_cell_get_certs(cc1, 1)->cert_type, OP_EQ,
  169. CERTTYPE_RSA1024_ID_ID);
  170. tt_int_op(certs_cell_get_certs(cc2, 0)->cert_type, OP_EQ,
  171. CERTTYPE_RSA1024_ID_LINK);
  172. tt_int_op(certs_cell_get_certs(cc2, 1)->cert_type, OP_EQ,
  173. CERTTYPE_RSA1024_ID_ID);
  174. if (with_ed) {
  175. tt_int_op(certs_cell_get_certs(cc1, 2)->cert_type, OP_EQ,
  176. CERTTYPE_ED_ID_SIGN);
  177. tt_int_op(certs_cell_get_certs(cc1, 3)->cert_type, OP_EQ,
  178. CERTTYPE_ED_SIGN_AUTH);
  179. tt_int_op(certs_cell_get_certs(cc1, 4)->cert_type, OP_EQ,
  180. CERTTYPE_RSA1024_ID_EDID);
  181. tt_int_op(certs_cell_get_certs(cc2, 2)->cert_type, OP_EQ,
  182. CERTTYPE_ED_ID_SIGN);
  183. tt_int_op(certs_cell_get_certs(cc2, 3)->cert_type, OP_EQ,
  184. CERTTYPE_ED_SIGN_LINK);
  185. tt_int_op(certs_cell_get_certs(cc2, 4)->cert_type, OP_EQ,
  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. tor_x509_cert_free(mock_peer_cert);
  207. mock_peer_cert = NULL;
  208. tor_assert(c1->handshake_state->authenticated);
  209. tt_assert(c1->handshake_state->received_certs_cell);
  210. tt_ptr_op(c1->handshake_state->certs->auth_cert, OP_EQ, NULL);
  211. tt_ptr_op(c1->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  212. tt_assert(c1->handshake_state->certs->id_cert);
  213. if (with_ed) {
  214. tt_assert(c1->handshake_state->certs->ed_sign_link);
  215. tt_assert(c1->handshake_state->certs->ed_rsa_crosscert);
  216. tt_assert(c1->handshake_state->certs->ed_id_sign);
  217. tt_assert(c1->handshake_state->authenticated_rsa);
  218. tt_assert(c1->handshake_state->authenticated_ed25519);
  219. } else {
  220. tt_ptr_op(c1->handshake_state->certs->ed_sign_link, OP_EQ, NULL);
  221. tt_ptr_op(c1->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  222. tt_ptr_op(c1->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  223. tt_assert(c1->handshake_state->authenticated_rsa);
  224. tt_assert(! c1->handshake_state->authenticated_ed25519);
  225. }
  226. tt_assert(! tor_mem_is_zero(
  227. (char*)c1->handshake_state->authenticated_rsa_peer_id, 20));
  228. chan2 = tor_malloc_zero(sizeof(*chan2));
  229. channel_tls_common_init(chan2);
  230. c2->chan = chan2;
  231. chan2->conn = c2;
  232. c2->base_.address = tor_strdup("C2");
  233. c2->tls = tor_tls_new(-1, 1);
  234. c2->link_proto = 4;
  235. c2->base_.conn_array_index = -1;
  236. crypto_pk_get_digest(key1, c2->identity_digest);
  237. channel_tls_process_certs_cell(cell1, chan2);
  238. tt_assert(c2->handshake_state->received_certs_cell);
  239. if (with_ed) {
  240. tt_assert(c2->handshake_state->certs->ed_sign_auth);
  241. tt_assert(c2->handshake_state->certs->ed_rsa_crosscert);
  242. tt_assert(c2->handshake_state->certs->ed_id_sign);
  243. } else {
  244. tt_assert(c2->handshake_state->certs->auth_cert);
  245. tt_ptr_op(c2->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  246. tt_ptr_op(c2->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  247. tt_ptr_op(c2->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  248. }
  249. tt_assert(c2->handshake_state->certs->id_cert);
  250. tt_assert(tor_mem_is_zero(
  251. (char*)c2->handshake_state->authenticated_rsa_peer_id, 20));
  252. /* no authentication has happened yet, since we haen't gotten an AUTH cell.
  253. */
  254. tt_assert(! c2->handshake_state->authenticated);
  255. tt_assert(! c2->handshake_state->authenticated_rsa);
  256. tt_assert(! c2->handshake_state->authenticated_ed25519);
  257. done:
  258. UNMOCK(tor_tls_cert_matches_key);
  259. UNMOCK(connection_or_write_var_cell_to_buf);
  260. UNMOCK(connection_or_send_netinfo);
  261. UNMOCK(tor_tls_get_peer_cert);
  262. UNMOCK(tor_tls_get_own_cert);
  263. tor_x509_cert_free(mock_own_cert);
  264. tor_x509_cert_free(mock_peer_cert);
  265. mock_own_cert = mock_peer_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_minimal(TO_CONN(c1));
  269. connection_free_minimal(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_minimal(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), OP_EQ, 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), OP_EQ, 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, OP_GT, 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, OP_EQ, d->c->handshake_state->received_certs_cell);
  414. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  415. tt_int_op(0, OP_EQ, 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, OP_EQ, mock_close_called);
  431. tt_int_op(d->c->handshake_state->authenticated, OP_EQ, 1);
  432. tt_int_op(d->c->handshake_state->authenticated_rsa, OP_EQ, 1);
  433. tt_int_op(d->c->handshake_state->received_certs_cell, OP_EQ, 1);
  434. tt_ptr_op(d->c->handshake_state->certs->id_cert, OP_NE, NULL);
  435. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_EQ, NULL);
  436. if (d->is_ed) {
  437. tt_ptr_op(d->c->handshake_state->certs->ed_id_sign, OP_NE, NULL);
  438. tt_ptr_op(d->c->handshake_state->certs->ed_sign_link, OP_NE, NULL);
  439. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  440. tt_ptr_op(d->c->handshake_state->certs->ed_rsa_crosscert, OP_NE, NULL);
  441. tt_int_op(d->c->handshake_state->authenticated_ed25519, OP_EQ, 1);
  442. } else {
  443. tt_ptr_op(d->c->handshake_state->certs->ed_id_sign, OP_EQ, NULL);
  444. tt_ptr_op(d->c->handshake_state->certs->ed_sign_link, OP_EQ, NULL);
  445. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  446. tt_ptr_op(d->c->handshake_state->certs->ed_rsa_crosscert, OP_EQ, NULL);
  447. tt_int_op(d->c->handshake_state->authenticated_ed25519, OP_EQ, 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, OP_EQ, mock_close_called);
  460. tt_int_op(d->c->handshake_state->authenticated, OP_EQ, 0);
  461. tt_int_op(d->c->handshake_state->received_certs_cell, OP_EQ, 1);
  462. tt_ptr_op(d->c->handshake_state->certs->id_cert, OP_NE, NULL);
  463. tt_ptr_op(d->c->handshake_state->certs->link_cert, OP_EQ, NULL);
  464. if (d->is_ed) {
  465. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_NE, NULL);
  466. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_EQ, NULL);
  467. } else {
  468. tt_ptr_op(d->c->handshake_state->certs->ed_sign_auth, OP_EQ, NULL);
  469. tt_ptr_op(d->c->handshake_state->certs->auth_cert, OP_NE, 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, OP_EQ, mock_close_called); \
  484. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  485. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  486. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_rsa); \
  487. tt_int_op(0, OP_EQ, 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, OP_GT, 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, OP_EQ, mock_close_called);
  623. tt_int_op(0, OP_EQ, d->c->handshake_state->authenticated_ed25519);
  624. tt_int_op(1, OP_EQ, 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, OP_EQ, 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. tor_x509_cert_t *newc;
  738. time_t new_end = time(NULL) - 86400 * 10;
  739. newc = tor_x509_cert_replace_expiration(idc, new_end, d->key2);
  740. certs_cell_cert_setlen_body(cert, newc->encoded_len);
  741. memcpy(certs_cell_cert_getarray_body(cert),
  742. newc->encoded, newc->encoded_len);
  743. REENCODE();
  744. tor_x509_cert_free(newc);
  745. })
  746. CERTS_FAIL(expired_ed_id, /* ed25519 */
  747. {
  748. /* we're going to replace the Ed Id->sign cert with an expired one. */
  749. require_failure_message = "At least one certificate expired";
  750. /* We don't need to re-sign, since we check for expiration first. */
  751. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 2);
  752. uint8_t *body = certs_cell_cert_getarray_body(cert);
  753. /* The expiration field is bytes [2..5]. It is in HOURS since the
  754. * epoch. */
  755. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  756. REENCODE();
  757. })
  758. CERTS_FAIL(expired_ed_link, /* ed25519 */
  759. {
  760. /* we're going to replace the Ed Sign->link cert with an expired one. */
  761. require_failure_message = "At least one certificate expired";
  762. /* We don't need to re-sign, since we check for expiration first. */
  763. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 3);
  764. uint8_t *body = certs_cell_cert_getarray_body(cert);
  765. /* The expiration field is bytes [2..5]. It is in HOURS since the
  766. * epoch. */
  767. set_uint32(body+2, htonl(24)); /* Back to jan 2, 1970. */
  768. REENCODE();
  769. })
  770. CERTS_FAIL(expired_crosscert, /* ed25519 */
  771. {
  772. /* we're going to replace the Ed Sign->link cert with an expired one. */
  773. require_failure_message = "Crosscert is expired";
  774. /* We don't need to re-sign, since we check for expiration first. */
  775. certs_cell_cert_t *cert = certs_cell_get_certs(d->ccell, 4);
  776. uint8_t *body = certs_cell_cert_getarray_body(cert);
  777. /* The expiration field is bytes [32..35]. once again, HOURS. */
  778. set_uint32(body+32, htonl(24)); /* Back to jan 2, 1970. */
  779. REENCODE();
  780. })
  781. CERTS_FAIL(wrong_labels_1,
  782. {
  783. require_failure_message = "The link certificate was not valid";
  784. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  785. certs_cell_get_certs(d->ccell, 1)->cert_type = 1;
  786. REENCODE();
  787. })
  788. CERTS_FAIL(wrong_labels_2,
  789. {
  790. const tor_x509_cert_t *a;
  791. const tor_x509_cert_t *b;
  792. const uint8_t *enca;
  793. size_t lena;
  794. require_failure_message = "The link certificate was not valid";
  795. tor_tls_get_my_certs(1, &a, &b);
  796. tor_x509_cert_get_der(a, &enca, &lena);
  797. certs_cell_cert_setlen_body(certs_cell_get_certs(d->ccell, 1), lena);
  798. memcpy(certs_cell_cert_getarray_body(certs_cell_get_certs(d->ccell, 1)),
  799. enca, lena);
  800. certs_cell_get_certs(d->ccell, 1)->cert_len = lena;
  801. REENCODE();
  802. })
  803. CERTS_FAIL(wrong_labels_3,
  804. {
  805. require_failure_message =
  806. "The certs we wanted (ID, Link) were missing";
  807. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  808. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  809. REENCODE();
  810. })
  811. CERTS_FAIL(server_missing_certs,
  812. {
  813. require_failure_message =
  814. "The certs we wanted (ID, Auth) were missing";
  815. d->c->handshake_state->started_here = 0;
  816. d->c->handshake_state->certs->started_here = 0;
  817. })
  818. CERTS_FAIL(server_wrong_labels_1,
  819. {
  820. require_failure_message =
  821. "The authentication certificate was not valid";
  822. d->c->handshake_state->started_here = 0;
  823. d->c->handshake_state->certs->started_here = 0;
  824. certs_cell_get_certs(d->ccell, 0)->cert_type = 2;
  825. certs_cell_get_certs(d->ccell, 1)->cert_type = 3;
  826. REENCODE();
  827. })
  828. static void
  829. test_link_handshake_send_authchallenge(void *arg)
  830. {
  831. (void)arg;
  832. or_connection_t *c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  833. var_cell_t *cell1=NULL, *cell2=NULL;
  834. crypto_pk_t *rsa0 = pk_generate(0), *rsa1 = pk_generate(1);
  835. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  836. rsa0, rsa1, 86400), OP_EQ, 0);
  837. init_mock_ed_keys(rsa0);
  838. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  839. tt_int_op(connection_init_or_handshake_state(c1, 0), OP_EQ, 0);
  840. c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  841. tt_ptr_op(mock_got_var_cell, OP_EQ, NULL);
  842. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  843. cell1 = mock_got_var_cell;
  844. tt_int_op(0, OP_EQ, connection_or_send_auth_challenge_cell(c1));
  845. cell2 = mock_got_var_cell;
  846. tt_int_op(38, OP_EQ, cell1->payload_len);
  847. tt_int_op(38, OP_EQ, cell2->payload_len);
  848. tt_int_op(0, OP_EQ, cell1->circ_id);
  849. tt_int_op(0, OP_EQ, cell2->circ_id);
  850. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell1->command);
  851. tt_int_op(CELL_AUTH_CHALLENGE, OP_EQ, cell2->command);
  852. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell1->payload + 32, 6);
  853. tt_mem_op("\x00\x02\x00\x01\x00\x03", OP_EQ, cell2->payload + 32, 6);
  854. tt_mem_op(cell1->payload, OP_NE, cell2->payload, 32);
  855. done:
  856. UNMOCK(connection_or_write_var_cell_to_buf);
  857. connection_free_minimal(TO_CONN(c1));
  858. tor_free(cell1);
  859. tor_free(cell2);
  860. crypto_pk_free(rsa0);
  861. crypto_pk_free(rsa1);
  862. }
  863. typedef struct authchallenge_data_s {
  864. or_connection_t *c;
  865. channel_tls_t *chan;
  866. var_cell_t *cell;
  867. } authchallenge_data_t;
  868. static int
  869. recv_authchallenge_cleanup(const struct testcase_t *test, void *obj)
  870. {
  871. (void)test;
  872. authchallenge_data_t *d = obj;
  873. UNMOCK(connection_or_send_netinfo);
  874. UNMOCK(connection_or_close_for_error);
  875. UNMOCK(connection_or_send_authenticate_cell);
  876. if (d) {
  877. tor_free(d->cell);
  878. connection_free_minimal(TO_CONN(d->c));
  879. circuitmux_free(d->chan->base_.cmux);
  880. tor_free(d->chan);
  881. tor_free(d);
  882. }
  883. return 1;
  884. }
  885. static void *
  886. recv_authchallenge_setup(const struct testcase_t *test)
  887. {
  888. (void)test;
  889. authchallenge_data_t *d = tor_malloc_zero(sizeof(*d));
  890. d->c = or_connection_new(CONN_TYPE_OR, AF_INET);
  891. d->chan = tor_malloc_zero(sizeof(*d->chan));
  892. d->c->chan = d->chan;
  893. d->c->base_.address = tor_strdup("HaveAnAddress");
  894. d->c->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  895. d->chan->conn = d->c;
  896. tt_int_op(connection_init_or_handshake_state(d->c, 1), OP_EQ, 0);
  897. d->c->link_proto = 4;
  898. d->c->handshake_state->received_certs_cell = 1;
  899. d->cell = var_cell_new(128);
  900. d->cell->payload_len = 38;
  901. d->cell->payload[33] = 2; /* 2 methods */
  902. d->cell->payload[35] = 7; /* This one isn't real */
  903. d->cell->payload[37] = 1; /* This is the old RSA one. */
  904. d->cell->command = CELL_AUTH_CHALLENGE;
  905. get_options_mutable()->ORPort_set = 1;
  906. MOCK(connection_or_close_for_error, mock_close_for_err);
  907. MOCK(connection_or_send_netinfo, mock_send_netinfo);
  908. MOCK(connection_or_send_authenticate_cell, mock_send_authenticate);
  909. tt_int_op(0, OP_EQ, d->c->handshake_state->received_auth_challenge);
  910. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  911. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  912. return d;
  913. done:
  914. recv_authchallenge_cleanup(test, d);
  915. return NULL;
  916. }
  917. static struct testcase_setup_t setup_recv_authchallenge = {
  918. .setup_fn = recv_authchallenge_setup,
  919. .cleanup_fn = recv_authchallenge_cleanup
  920. };
  921. static void
  922. test_link_handshake_recv_authchallenge_ok(void *arg)
  923. {
  924. authchallenge_data_t *d = arg;
  925. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  926. tt_int_op(0, OP_EQ, mock_close_called);
  927. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  928. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  929. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  930. tt_int_op(1, OP_EQ, mock_send_authenticate_called_with_type); /* RSA */
  931. done:
  932. ;
  933. }
  934. static void
  935. test_link_handshake_recv_authchallenge_ok_ed25519(void *arg)
  936. {
  937. authchallenge_data_t *d = arg;
  938. /* Add the ed25519 authentication mechanism here. */
  939. d->cell->payload[33] = 3; /* 3 types are supported now. */
  940. d->cell->payload[39] = 3;
  941. d->cell->payload_len += 2;
  942. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  943. tt_int_op(0, OP_EQ, mock_close_called);
  944. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  945. tt_int_op(1, OP_EQ, mock_send_authenticate_called);
  946. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  947. tt_int_op(3, OP_EQ, mock_send_authenticate_called_with_type); /* Ed25519 */
  948. done:
  949. ;
  950. }
  951. static void
  952. test_link_handshake_recv_authchallenge_ok_noserver(void *arg)
  953. {
  954. authchallenge_data_t *d = arg;
  955. get_options_mutable()->ORPort_set = 0;
  956. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  957. tt_int_op(0, OP_EQ, mock_close_called);
  958. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  959. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  960. tt_int_op(0, OP_EQ, mock_send_netinfo_called);
  961. done:
  962. ;
  963. }
  964. static void
  965. test_link_handshake_recv_authchallenge_ok_unrecognized(void *arg)
  966. {
  967. authchallenge_data_t *d = arg;
  968. d->cell->payload[37] = 99;
  969. channel_tls_process_auth_challenge_cell(d->cell, d->chan);
  970. tt_int_op(0, OP_EQ, mock_close_called);
  971. tt_int_op(1, OP_EQ, d->c->handshake_state->received_auth_challenge);
  972. tt_int_op(0, OP_EQ, mock_send_authenticate_called);
  973. tt_int_op(1, OP_EQ, mock_send_netinfo_called);
  974. done:
  975. ;
  976. }
  977. #define AUTHCHALLENGE_FAIL(name, code) \
  978. static void \
  979. test_link_handshake_recv_authchallenge_ ## name(void *arg) \
  980. { \
  981. authchallenge_data_t *d = arg; \
  982. const char *require_failure_message = NULL; \
  983. setup_capture_of_logs(LOG_INFO); \
  984. { code ; } \
  985. channel_tls_process_auth_challenge_cell(d->cell, d->chan); \
  986. tt_int_op(1, OP_EQ, mock_close_called); \
  987. tt_int_op(0, OP_EQ, mock_send_authenticate_called); \
  988. tt_int_op(0, OP_EQ, mock_send_netinfo_called); \
  989. if (require_failure_message) { \
  990. expect_log_msg_containing(require_failure_message); \
  991. } \
  992. done: \
  993. teardown_capture_of_logs(); \
  994. }
  995. AUTHCHALLENGE_FAIL(badstate,
  996. require_failure_message = "We're not currently doing a "
  997. "v3 handshake";
  998. d->c->base_.state = OR_CONN_STATE_CONNECTING)
  999. AUTHCHALLENGE_FAIL(badproto,
  1000. require_failure_message = "not using link protocol >= 3";
  1001. d->c->link_proto = 2)
  1002. AUTHCHALLENGE_FAIL(as_server,
  1003. require_failure_message = "We didn't originate this "
  1004. "connection";
  1005. d->c->handshake_state->started_here = 0;
  1006. d->c->handshake_state->certs->started_here = 0;)
  1007. AUTHCHALLENGE_FAIL(duplicate,
  1008. require_failure_message = "We already received one";
  1009. d->c->handshake_state->received_auth_challenge = 1)
  1010. AUTHCHALLENGE_FAIL(nocerts,
  1011. require_failure_message = "We haven't gotten a CERTS "
  1012. "cell yet";
  1013. d->c->handshake_state->received_certs_cell = 0)
  1014. AUTHCHALLENGE_FAIL(tooshort,
  1015. require_failure_message = "It was not well-formed";
  1016. d->cell->payload_len = 33)
  1017. AUTHCHALLENGE_FAIL(truncated,
  1018. require_failure_message = "It was not well-formed";
  1019. d->cell->payload_len = 34)
  1020. AUTHCHALLENGE_FAIL(nonzero_circid,
  1021. require_failure_message = "It had a nonzero circuit ID";
  1022. d->cell->circ_id = 1337)
  1023. static int
  1024. mock_get_tlssecrets(tor_tls_t *tls, uint8_t *secrets_out)
  1025. {
  1026. (void)tls;
  1027. memcpy(secrets_out, "int getRandomNumber(){return 4;}", 32);
  1028. return 0;
  1029. }
  1030. static void
  1031. mock_set_circid_type(channel_t *chan,
  1032. crypto_pk_t *identity_rcvd,
  1033. int consider_identity)
  1034. {
  1035. (void) chan;
  1036. (void) identity_rcvd;
  1037. (void) consider_identity;
  1038. }
  1039. typedef struct authenticate_data_s {
  1040. int is_ed;
  1041. or_connection_t *c1, *c2;
  1042. channel_tls_t *chan2;
  1043. var_cell_t *cell;
  1044. crypto_pk_t *key1, *key2;
  1045. } authenticate_data_t;
  1046. static int
  1047. authenticate_data_cleanup(const struct testcase_t *test, void *arg)
  1048. {
  1049. (void) test;
  1050. UNMOCK(connection_or_write_var_cell_to_buf);
  1051. UNMOCK(tor_tls_get_peer_cert);
  1052. UNMOCK(tor_tls_get_own_cert);
  1053. UNMOCK(tor_tls_get_tlssecrets);
  1054. UNMOCK(connection_or_close_for_error);
  1055. UNMOCK(channel_set_circid_type);
  1056. UNMOCK(tor_tls_export_key_material);
  1057. authenticate_data_t *d = arg;
  1058. if (d) {
  1059. tor_free(d->cell);
  1060. connection_or_clear_identity(d->c1);
  1061. connection_or_clear_identity(d->c2);
  1062. connection_free_minimal(TO_CONN(d->c1));
  1063. connection_free_minimal(TO_CONN(d->c2));
  1064. circuitmux_free(d->chan2->base_.cmux);
  1065. tor_free(d->chan2);
  1066. crypto_pk_free(d->key1);
  1067. crypto_pk_free(d->key2);
  1068. tor_free(d);
  1069. }
  1070. tor_x509_cert_free(mock_peer_cert);
  1071. tor_x509_cert_free(mock_own_cert);
  1072. mock_peer_cert = NULL;
  1073. mock_own_cert = NULL;
  1074. return 1;
  1075. }
  1076. static void *
  1077. authenticate_data_setup(const struct testcase_t *test)
  1078. {
  1079. authenticate_data_t *d = tor_malloc_zero(sizeof(*d));
  1080. int is_ed = d->is_ed = (test->setup_data == (void*)3);
  1081. scheduler_init();
  1082. MOCK(connection_or_write_var_cell_to_buf, mock_write_var_cell);
  1083. MOCK(tor_tls_get_peer_cert, mock_get_peer_cert);
  1084. MOCK(tor_tls_get_own_cert, mock_get_own_cert);
  1085. MOCK(tor_tls_get_tlssecrets, mock_get_tlssecrets);
  1086. MOCK(connection_or_close_for_error, mock_close_for_err);
  1087. MOCK(channel_set_circid_type, mock_set_circid_type);
  1088. MOCK(tor_tls_export_key_material, mock_export_key_material);
  1089. d->c1 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1090. d->c2 = or_connection_new(CONN_TYPE_OR, AF_INET);
  1091. tor_addr_from_ipv4h(&d->c1->base_.addr, 0x01020304);
  1092. tor_addr_from_ipv4h(&d->c2->base_.addr, 0x05060708);
  1093. d->key1 = pk_generate(2);
  1094. d->key2 = pk_generate(3);
  1095. tt_int_op(tor_tls_context_init(TOR_TLS_CTX_IS_PUBLIC_SERVER,
  1096. d->key1, d->key2, 86400), OP_EQ, 0);
  1097. init_mock_ed_keys(d->key2);
  1098. d->c1->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1099. d->c1->link_proto = 3;
  1100. tt_int_op(connection_init_or_handshake_state(d->c1, 1), OP_EQ, 0);
  1101. d->c2->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1102. d->c2->link_proto = 3;
  1103. tt_int_op(connection_init_or_handshake_state(d->c2, 0), OP_EQ, 0);
  1104. var_cell_t *cell = var_cell_new(16);
  1105. cell->command = CELL_CERTS;
  1106. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 1);
  1107. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 0);
  1108. memset(cell->payload, 0xf0, 16);
  1109. or_handshake_state_record_var_cell(d->c1, d->c1->handshake_state, cell, 0);
  1110. or_handshake_state_record_var_cell(d->c2, d->c2->handshake_state, cell, 1);
  1111. tor_free(cell);
  1112. d->chan2 = tor_malloc_zero(sizeof(*d->chan2));
  1113. channel_tls_common_init(d->chan2);
  1114. d->c2->chan = d->chan2;
  1115. d->chan2->conn = d->c2;
  1116. d->c2->base_.address = tor_strdup("C2");
  1117. d->c2->tls = tor_tls_new(-1, 1);
  1118. d->c2->handshake_state->received_certs_cell = 1;
  1119. const tor_x509_cert_t *id_cert=NULL, *link_cert=NULL, *auth_cert=NULL;
  1120. tt_assert(! tor_tls_get_my_certs(1, &link_cert, &id_cert));
  1121. const uint8_t *der;
  1122. size_t sz;
  1123. tor_x509_cert_get_der(id_cert, &der, &sz);
  1124. d->c1->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1125. d->c2->handshake_state->certs->id_cert = tor_x509_cert_decode(der, sz);
  1126. if (is_ed) {
  1127. d->c1->handshake_state->certs->ed_id_sign =
  1128. tor_cert_dup(get_master_signing_key_cert());
  1129. d->c2->handshake_state->certs->ed_id_sign =
  1130. tor_cert_dup(get_master_signing_key_cert());
  1131. d->c2->handshake_state->certs->ed_sign_auth =
  1132. tor_cert_dup(get_current_auth_key_cert());
  1133. } else {
  1134. tt_assert(! tor_tls_get_my_certs(0, &auth_cert, &id_cert));
  1135. tor_x509_cert_get_der(auth_cert, &der, &sz);
  1136. d->c2->handshake_state->certs->auth_cert = tor_x509_cert_decode(der, sz);
  1137. }
  1138. tor_x509_cert_get_der(link_cert, &der, &sz);
  1139. mock_peer_cert = tor_x509_cert_decode(der, sz);
  1140. tt_assert(mock_peer_cert);
  1141. mock_own_cert = tor_x509_cert_decode(der, sz);
  1142. tt_assert(mock_own_cert);
  1143. /* Make an authenticate cell ... */
  1144. int authtype;
  1145. if (is_ed)
  1146. authtype = AUTHTYPE_ED25519_SHA256_RFC5705;
  1147. else
  1148. authtype = AUTHTYPE_RSA_SHA256_TLSSECRET;
  1149. tt_int_op(0, OP_EQ, connection_or_send_authenticate_cell(d->c1, authtype));
  1150. tt_assert(mock_got_var_cell);
  1151. d->cell = mock_got_var_cell;
  1152. mock_got_var_cell = NULL;
  1153. return d;
  1154. done:
  1155. authenticate_data_cleanup(test, d);
  1156. return NULL;
  1157. }
  1158. static struct testcase_setup_t setup_authenticate = {
  1159. .setup_fn = authenticate_data_setup,
  1160. .cleanup_fn = authenticate_data_cleanup
  1161. };
  1162. static void
  1163. test_link_handshake_auth_cell(void *arg)
  1164. {
  1165. authenticate_data_t *d = arg;
  1166. auth1_t *auth1 = NULL;
  1167. crypto_pk_t *auth_pubkey = NULL;
  1168. /* Is the cell well-formed on the outer layer? */
  1169. tt_int_op(d->cell->command, OP_EQ, CELL_AUTHENTICATE);
  1170. tt_int_op(d->cell->payload[0], OP_EQ, 0);
  1171. if (d->is_ed)
  1172. tt_int_op(d->cell->payload[1], OP_EQ, 3);
  1173. else
  1174. tt_int_op(d->cell->payload[1], OP_EQ, 1);
  1175. tt_int_op(ntohs(get_uint16(d->cell->payload + 2)), OP_EQ,
  1176. d->cell->payload_len - 4);
  1177. /* Check it out for plausibility... */
  1178. auth_ctx_t ctx;
  1179. ctx.is_ed = d->is_ed;
  1180. tt_int_op(d->cell->payload_len-4, OP_EQ, auth1_parse(&auth1,
  1181. d->cell->payload+4,
  1182. d->cell->payload_len - 4, &ctx));
  1183. tt_assert(auth1);
  1184. if (d->is_ed) {
  1185. tt_mem_op(auth1->type, OP_EQ, "AUTH0003", 8);
  1186. } else {
  1187. tt_mem_op(auth1->type, OP_EQ, "AUTH0001", 8);
  1188. }
  1189. tt_mem_op(auth1->tlssecrets, OP_EQ, "int getRandomNumber(){return 4;}", 32);
  1190. /* Is the signature okay? */
  1191. const uint8_t *start = d->cell->payload+4, *end = auth1->end_of_signed;
  1192. if (d->is_ed) {
  1193. ed25519_signature_t sig;
  1194. tt_int_op(auth1_getlen_sig(auth1), OP_EQ, ED25519_SIG_LEN);
  1195. memcpy(&sig.sig, auth1_getarray_sig(auth1), ED25519_SIG_LEN);
  1196. tt_assert(!ed25519_checksig(&sig, start, end-start,
  1197. &get_current_auth_keypair()->pubkey));
  1198. } else {
  1199. uint8_t sig[128];
  1200. uint8_t digest[32];
  1201. tt_int_op(auth1_getlen_sig(auth1), OP_GT, 120);
  1202. auth_pubkey = tor_tls_cert_get_key(
  1203. d->c2->handshake_state->certs->auth_cert);
  1204. int n = crypto_pk_public_checksig(
  1205. auth_pubkey,
  1206. (char*)sig, sizeof(sig), (char*)auth1_getarray_sig(auth1),
  1207. auth1_getlen_sig(auth1));
  1208. tt_int_op(n, OP_EQ, 32);
  1209. crypto_digest256((char*)digest,
  1210. (const char*)start, end-start, DIGEST_SHA256);
  1211. tt_mem_op(sig, OP_EQ, digest, 32);
  1212. }
  1213. /* Then feed it to c2. */
  1214. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0);
  1215. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1216. tt_int_op(mock_close_called, OP_EQ, 0);
  1217. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1218. if (d->is_ed) {
  1219. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 1);
  1220. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1221. } else {
  1222. tt_int_op(d->c2->handshake_state->authenticated_ed25519, OP_EQ, 0);
  1223. tt_int_op(d->c2->handshake_state->authenticated_rsa, OP_EQ, 1);
  1224. }
  1225. done:
  1226. auth1_free(auth1);
  1227. crypto_pk_free(auth_pubkey);
  1228. }
  1229. #define AUTHENTICATE_FAIL(name, code) \
  1230. static void \
  1231. test_link_handshake_auth_ ## name(void *arg) \
  1232. { \
  1233. authenticate_data_t *d = arg; \
  1234. const char *require_failure_message = NULL; \
  1235. setup_capture_of_logs(LOG_INFO); \
  1236. { code ; } \
  1237. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  1238. channel_tls_process_authenticate_cell(d->cell, d->chan2); \
  1239. tt_int_op(mock_close_called, OP_EQ, 1); \
  1240. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 0); \
  1241. if (require_failure_message) { \
  1242. expect_log_msg_containing(require_failure_message); \
  1243. } \
  1244. done: \
  1245. teardown_capture_of_logs(); \
  1246. }
  1247. AUTHENTICATE_FAIL(badstate,
  1248. require_failure_message = "We're not doing a v3 handshake";
  1249. d->c2->base_.state = OR_CONN_STATE_CONNECTING)
  1250. AUTHENTICATE_FAIL(badproto,
  1251. require_failure_message = "not using link protocol >= 3";
  1252. d->c2->link_proto = 2)
  1253. AUTHENTICATE_FAIL(atclient,
  1254. require_failure_message = "We originated this connection";
  1255. d->c2->handshake_state->started_here = 1;
  1256. d->c2->handshake_state->certs->started_here = 1;)
  1257. AUTHENTICATE_FAIL(duplicate,
  1258. require_failure_message = "We already got one";
  1259. d->c2->handshake_state->received_authenticate = 1)
  1260. static void
  1261. test_link_handshake_auth_already_authenticated(void *arg)
  1262. {
  1263. authenticate_data_t *d = arg;
  1264. setup_capture_of_logs(LOG_INFO);
  1265. d->c2->handshake_state->authenticated = 1;
  1266. channel_tls_process_authenticate_cell(d->cell, d->chan2);
  1267. tt_int_op(mock_close_called, OP_EQ, 1);
  1268. tt_int_op(d->c2->handshake_state->authenticated, OP_EQ, 1);
  1269. expect_log_msg_containing("The peer is already authenticated");
  1270. done:
  1271. teardown_capture_of_logs();
  1272. }
  1273. AUTHENTICATE_FAIL(nocerts,
  1274. require_failure_message = "We never got a certs cell";
  1275. d->c2->handshake_state->received_certs_cell = 0)
  1276. AUTHENTICATE_FAIL(noidcert,
  1277. require_failure_message = "We never got an identity "
  1278. "certificate";
  1279. tor_x509_cert_free(d->c2->handshake_state->certs->id_cert);
  1280. d->c2->handshake_state->certs->id_cert = NULL)
  1281. AUTHENTICATE_FAIL(noauthcert,
  1282. require_failure_message = "We never got an RSA "
  1283. "authentication certificate";
  1284. tor_x509_cert_free(d->c2->handshake_state->certs->auth_cert);
  1285. d->c2->handshake_state->certs->auth_cert = NULL)
  1286. AUTHENTICATE_FAIL(tooshort,
  1287. require_failure_message = "Cell was way too short";
  1288. d->cell->payload_len = 3)
  1289. AUTHENTICATE_FAIL(badtype,
  1290. require_failure_message = "Authenticator type was not "
  1291. "recognized";
  1292. d->cell->payload[0] = 0xff)
  1293. AUTHENTICATE_FAIL(truncated_1,
  1294. require_failure_message = "Authenticator was truncated";
  1295. d->cell->payload[2]++)
  1296. AUTHENTICATE_FAIL(truncated_2,
  1297. require_failure_message = "Authenticator was truncated";
  1298. d->cell->payload[3]++)
  1299. AUTHENTICATE_FAIL(tooshort_1,
  1300. require_failure_message = "Authenticator was too short";
  1301. tt_int_op(d->cell->payload_len, OP_GE, 260);
  1302. d->cell->payload[2] -= 1;
  1303. d->cell->payload_len -= 256;)
  1304. AUTHENTICATE_FAIL(badcontent,
  1305. require_failure_message = "Some field in the AUTHENTICATE "
  1306. "cell body was not as expected";
  1307. d->cell->payload[10] ^= 0xff)
  1308. AUTHENTICATE_FAIL(badsig_1,
  1309. if (d->is_ed)
  1310. require_failure_message = "Ed25519 signature wasn't valid";
  1311. else
  1312. require_failure_message = "RSA signature wasn't valid";
  1313. d->cell->payload[d->cell->payload_len - 5] ^= 0xff)
  1314. AUTHENTICATE_FAIL(missing_ed_id,
  1315. {
  1316. tor_cert_free(d->c2->handshake_state->certs->ed_id_sign);
  1317. d->c2->handshake_state->certs->ed_id_sign = NULL;
  1318. require_failure_message = "Ed authenticate without Ed ID "
  1319. "cert from peer";
  1320. })
  1321. AUTHENTICATE_FAIL(missing_ed_auth,
  1322. {
  1323. tor_cert_free(d->c2->handshake_state->certs->ed_sign_auth);
  1324. d->c2->handshake_state->certs->ed_sign_auth = NULL;
  1325. require_failure_message = "We never got an Ed25519 "
  1326. "authentication certificate";
  1327. })
  1328. #define TEST_RSA(name, flags) \
  1329. { #name , test_link_handshake_ ## name, (flags), \
  1330. &passthrough_setup, (void*)"RSA" }
  1331. #define TEST_ED(name, flags) \
  1332. { #name "_ed25519" , test_link_handshake_ ## name, (flags), \
  1333. &passthrough_setup, (void*)"Ed25519" }
  1334. #define TEST_RCV_AUTHCHALLENGE(name) \
  1335. { "recv_authchallenge/" #name , \
  1336. test_link_handshake_recv_authchallenge_ ## name, TT_FORK, \
  1337. &setup_recv_authchallenge, NULL }
  1338. #define TEST_RCV_CERTS(name) \
  1339. { "recv_certs/" #name , \
  1340. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1341. &setup_recv_certs, (void*)"RSA-Link" }
  1342. #define TEST_RCV_CERTS_RSA(name,type) \
  1343. { "recv_certs/" #name , \
  1344. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1345. &setup_recv_certs, (void*)type }
  1346. #define TEST_RCV_CERTS_ED(name, type) \
  1347. { "recv_certs/" #name "_ed25519", \
  1348. test_link_handshake_recv_certs_ ## name, TT_FORK, \
  1349. &setup_recv_certs, (void*)type }
  1350. #define TEST_AUTHENTICATE(name) \
  1351. { "authenticate/" #name , test_link_handshake_auth_ ## name, TT_FORK, \
  1352. &setup_authenticate, NULL }
  1353. #define TEST_AUTHENTICATE_ED(name) \
  1354. { "authenticate/" #name "_ed25519" , test_link_handshake_auth_ ## name, \
  1355. TT_FORK, &setup_authenticate, (void*)3 }
  1356. struct testcase_t link_handshake_tests[] = {
  1357. TEST_RSA(certs_ok, TT_FORK),
  1358. TEST_ED(certs_ok, TT_FORK),
  1359. TEST_RCV_CERTS(ok),
  1360. TEST_RCV_CERTS_ED(ok, "Ed25519-Link"),
  1361. TEST_RCV_CERTS_RSA(ok_server, "RSA-Auth"),
  1362. TEST_RCV_CERTS_ED(ok_server, "Ed25519-Auth"),
  1363. TEST_RCV_CERTS(badstate),
  1364. TEST_RCV_CERTS(badproto),
  1365. TEST_RCV_CERTS(duplicate),
  1366. TEST_RCV_CERTS(already_authenticated),
  1367. TEST_RCV_CERTS(empty),
  1368. TEST_RCV_CERTS(bad_circid),
  1369. TEST_RCV_CERTS(truncated_1),
  1370. TEST_RCV_CERTS(truncated_2),
  1371. TEST_RCV_CERTS(truncated_3),
  1372. TEST_RCV_CERTS_ED(truncated_4, "Ed25519-Link"),
  1373. TEST_RCV_CERTS_ED(truncated_5, "Ed25519-Link"),
  1374. TEST_RCV_CERTS_ED(truncated_6, "Ed25519-Link"),
  1375. TEST_RCV_CERTS_ED(truncated_7, "Ed25519-Link"),
  1376. TEST_RCV_CERTS(not_x509),
  1377. TEST_RCV_CERTS(both_link),
  1378. TEST_RCV_CERTS(both_id_rsa),
  1379. TEST_RCV_CERTS(both_auth),
  1380. TEST_RCV_CERTS_ED(duplicate_id, "Ed25519-Link"),
  1381. TEST_RCV_CERTS_ED(duplicate_link, "Ed25519-Link"),
  1382. TEST_RCV_CERTS_ED(duplicate_crosscert, "Ed25519-Link"),
  1383. TEST_RCV_CERTS_ED(missing_crosscert, "Ed25519-Link"),
  1384. TEST_RCV_CERTS_ED(missing_id, "Ed25519-Link"),
  1385. TEST_RCV_CERTS_ED(missing_signing_key, "Ed25519-Link"),
  1386. TEST_RCV_CERTS_ED(missing_link, "Ed25519-Link"),
  1387. TEST_RCV_CERTS_ED(missing_auth, "Ed25519-Auth"),
  1388. TEST_RCV_CERTS_ED(missing_rsa_id, "Ed25519-Link"),
  1389. TEST_RCV_CERTS_ED(link_mismatch, "Ed25519-Link"),
  1390. TEST_RCV_CERTS_ED(bad_ed_sig, "Ed25519-Link"),
  1391. TEST_RCV_CERTS_ED(bad_rsa_id_cert, "Ed25519-Link"),
  1392. TEST_RCV_CERTS_ED(bad_crosscert, "Ed25519-Link"),
  1393. TEST_RCV_CERTS_RSA(expired_rsa_id, "RSA-Link"),
  1394. TEST_RCV_CERTS_ED(expired_rsa_id, "Ed25519-Link"),
  1395. TEST_RCV_CERTS_ED(expired_ed_id, "Ed25519-Link"),
  1396. TEST_RCV_CERTS_ED(expired_ed_link, "Ed25519-Link"),
  1397. TEST_RCV_CERTS_ED(expired_crosscert, "Ed25519-Link"),
  1398. TEST_RCV_CERTS(wrong_labels_1),
  1399. TEST_RCV_CERTS(wrong_labels_2),
  1400. TEST_RCV_CERTS(wrong_labels_3),
  1401. TEST_RCV_CERTS(server_missing_certs),
  1402. TEST_RCV_CERTS(server_wrong_labels_1),
  1403. TEST_RSA(send_authchallenge, TT_FORK),
  1404. TEST_RCV_AUTHCHALLENGE(ok),
  1405. TEST_RCV_AUTHCHALLENGE(ok_ed25519),
  1406. TEST_RCV_AUTHCHALLENGE(ok_noserver),
  1407. TEST_RCV_AUTHCHALLENGE(ok_unrecognized),
  1408. TEST_RCV_AUTHCHALLENGE(badstate),
  1409. TEST_RCV_AUTHCHALLENGE(badproto),
  1410. TEST_RCV_AUTHCHALLENGE(as_server),
  1411. TEST_RCV_AUTHCHALLENGE(duplicate),
  1412. TEST_RCV_AUTHCHALLENGE(nocerts),
  1413. TEST_RCV_AUTHCHALLENGE(tooshort),
  1414. TEST_RCV_AUTHCHALLENGE(truncated),
  1415. TEST_RCV_AUTHCHALLENGE(nonzero_circid),
  1416. TEST_AUTHENTICATE(cell),
  1417. TEST_AUTHENTICATE_ED(cell),
  1418. TEST_AUTHENTICATE(badstate),
  1419. TEST_AUTHENTICATE(badproto),
  1420. TEST_AUTHENTICATE(atclient),
  1421. TEST_AUTHENTICATE(duplicate),
  1422. TEST_AUTHENTICATE(already_authenticated),
  1423. TEST_AUTHENTICATE(nocerts),
  1424. TEST_AUTHENTICATE(noidcert),
  1425. TEST_AUTHENTICATE(noauthcert),
  1426. TEST_AUTHENTICATE(tooshort),
  1427. TEST_AUTHENTICATE(badtype),
  1428. TEST_AUTHENTICATE(truncated_1),
  1429. TEST_AUTHENTICATE(truncated_2),
  1430. TEST_AUTHENTICATE(tooshort_1),
  1431. TEST_AUTHENTICATE(badcontent),
  1432. TEST_AUTHENTICATE(badsig_1),
  1433. TEST_AUTHENTICATE_ED(badsig_1),
  1434. TEST_AUTHENTICATE_ED(missing_ed_id),
  1435. TEST_AUTHENTICATE_ED(missing_ed_auth),
  1436. //TEST_AUTHENTICATE(),
  1437. END_OF_TESTCASES
  1438. };