channeltls.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  1. /* * Copyright (c) 2012-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file channeltls.c
  5. * \brief channel_t concrete subclass using or_connection_t
  6. **/
  7. /*
  8. * Define this so channel.h gives us things only channel_t subclasses
  9. * should touch.
  10. */
  11. #define TOR_CHANNEL_INTERNAL_
  12. #include "or.h"
  13. #include "channel.h"
  14. #include "channeltls.h"
  15. #include "circuitmux.h"
  16. #include "circuitmux_ewma.h"
  17. #include "config.h"
  18. #include "connection.h"
  19. #include "connection_or.h"
  20. #include "control.h"
  21. #include "relay.h"
  22. #include "router.h"
  23. #include "routerlist.h"
  24. #include "scheduler.h"
  25. /** How many CELL_PADDING cells have we received, ever? */
  26. uint64_t stats_n_padding_cells_processed = 0;
  27. /** How many CELL_VERSIONS cells have we received, ever? */
  28. uint64_t stats_n_versions_cells_processed = 0;
  29. /** How many CELL_NETINFO cells have we received, ever? */
  30. uint64_t stats_n_netinfo_cells_processed = 0;
  31. /** How many CELL_VPADDING cells have we received, ever? */
  32. uint64_t stats_n_vpadding_cells_processed = 0;
  33. /** How many CELL_CERTS cells have we received, ever? */
  34. uint64_t stats_n_certs_cells_processed = 0;
  35. /** How many CELL_AUTH_CHALLENGE cells have we received, ever? */
  36. uint64_t stats_n_auth_challenge_cells_processed = 0;
  37. /** How many CELL_AUTHENTICATE cells have we received, ever? */
  38. uint64_t stats_n_authenticate_cells_processed = 0;
  39. /** How many CELL_AUTHORIZE cells have we received, ever? */
  40. uint64_t stats_n_authorize_cells_processed = 0;
  41. /** Active listener, if any */
  42. channel_listener_t *channel_tls_listener = NULL;
  43. /* Utility function declarations */
  44. static void channel_tls_common_init(channel_tls_t *tlschan);
  45. /* channel_tls_t method declarations */
  46. static void channel_tls_close_method(channel_t *chan);
  47. static const char * channel_tls_describe_transport_method(channel_t *chan);
  48. static void channel_tls_free_method(channel_t *chan);
  49. static int
  50. channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out);
  51. static int
  52. channel_tls_get_transport_name_method(channel_t *chan, char **transport_out);
  53. static const char *
  54. channel_tls_get_remote_descr_method(channel_t *chan, int flags);
  55. static int channel_tls_has_queued_writes_method(channel_t *chan);
  56. static int channel_tls_is_canonical_method(channel_t *chan, int req);
  57. static int
  58. channel_tls_matches_extend_info_method(channel_t *chan,
  59. extend_info_t *extend_info);
  60. static int channel_tls_matches_target_method(channel_t *chan,
  61. const tor_addr_t *target);
  62. static int channel_tls_num_cells_writeable_method(channel_t *chan);
  63. static int channel_tls_write_cell_method(channel_t *chan,
  64. cell_t *cell);
  65. static int channel_tls_write_packed_cell_method(channel_t *chan,
  66. packed_cell_t *packed_cell);
  67. static int channel_tls_write_var_cell_method(channel_t *chan,
  68. var_cell_t *var_cell);
  69. /* channel_listener_tls_t method declarations */
  70. static void channel_tls_listener_close_method(channel_listener_t *chan_l);
  71. static const char *
  72. channel_tls_listener_describe_transport_method(channel_listener_t *chan_l);
  73. /** Handle incoming cells for the handshake stuff here rather than
  74. * passing them on up. */
  75. static void channel_tls_process_versions_cell(var_cell_t *cell,
  76. channel_tls_t *tlschan);
  77. static void channel_tls_process_netinfo_cell(cell_t *cell,
  78. channel_tls_t *tlschan);
  79. static void channel_tls_process_certs_cell(var_cell_t *cell,
  80. channel_tls_t *tlschan);
  81. static void channel_tls_process_auth_challenge_cell(var_cell_t *cell,
  82. channel_tls_t *tlschan);
  83. static void channel_tls_process_authenticate_cell(var_cell_t *cell,
  84. channel_tls_t *tlschan);
  85. static int command_allowed_before_handshake(uint8_t command);
  86. static int enter_v3_handshake_with_cell(var_cell_t *cell,
  87. channel_tls_t *tlschan);
  88. /**
  89. * Do parts of channel_tls_t initialization common to channel_tls_connect()
  90. * and channel_tls_handle_incoming().
  91. */
  92. static void
  93. channel_tls_common_init(channel_tls_t *tlschan)
  94. {
  95. channel_t *chan;
  96. tor_assert(tlschan);
  97. chan = &(tlschan->base_);
  98. channel_init(chan);
  99. chan->magic = TLS_CHAN_MAGIC;
  100. chan->state = CHANNEL_STATE_OPENING;
  101. chan->close = channel_tls_close_method;
  102. chan->describe_transport = channel_tls_describe_transport_method;
  103. chan->free = channel_tls_free_method;
  104. chan->get_remote_addr = channel_tls_get_remote_addr_method;
  105. chan->get_remote_descr = channel_tls_get_remote_descr_method;
  106. chan->get_transport_name = channel_tls_get_transport_name_method;
  107. chan->has_queued_writes = channel_tls_has_queued_writes_method;
  108. chan->is_canonical = channel_tls_is_canonical_method;
  109. chan->matches_extend_info = channel_tls_matches_extend_info_method;
  110. chan->matches_target = channel_tls_matches_target_method;
  111. chan->num_cells_writeable = channel_tls_num_cells_writeable_method;
  112. chan->write_cell = channel_tls_write_cell_method;
  113. chan->write_packed_cell = channel_tls_write_packed_cell_method;
  114. chan->write_var_cell = channel_tls_write_var_cell_method;
  115. chan->cmux = circuitmux_alloc();
  116. if (cell_ewma_enabled()) {
  117. circuitmux_set_policy(chan->cmux, &ewma_policy);
  118. }
  119. }
  120. /**
  121. * Start a new TLS channel
  122. *
  123. * Launch a new OR connection to <b>addr</b>:<b>port</b> and expect to
  124. * handshake with an OR with identity digest <b>id_digest</b>, and wrap
  125. * it in a channel_tls_t.
  126. */
  127. channel_t *
  128. channel_tls_connect(const tor_addr_t *addr, uint16_t port,
  129. const char *id_digest)
  130. {
  131. channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
  132. channel_t *chan = &(tlschan->base_);
  133. channel_tls_common_init(tlschan);
  134. log_debug(LD_CHANNEL,
  135. "In channel_tls_connect() for channel %p "
  136. "(global id " U64_FORMAT ")",
  137. tlschan,
  138. U64_PRINTF_ARG(chan->global_identifier));
  139. if (is_local_addr(addr)) {
  140. log_debug(LD_CHANNEL,
  141. "Marking new outgoing channel " U64_FORMAT " at %p as local",
  142. U64_PRINTF_ARG(chan->global_identifier), chan);
  143. channel_mark_local(chan);
  144. } else {
  145. log_debug(LD_CHANNEL,
  146. "Marking new outgoing channel " U64_FORMAT " at %p as remote",
  147. U64_PRINTF_ARG(chan->global_identifier), chan);
  148. channel_mark_remote(chan);
  149. }
  150. channel_mark_outgoing(chan);
  151. /* Set up or_connection stuff */
  152. tlschan->conn = connection_or_connect(addr, port, id_digest, tlschan);
  153. /* connection_or_connect() will fill in tlschan->conn */
  154. if (!(tlschan->conn)) {
  155. chan->reason_for_closing = CHANNEL_CLOSE_FOR_ERROR;
  156. channel_change_state(chan, CHANNEL_STATE_ERROR);
  157. goto err;
  158. }
  159. log_debug(LD_CHANNEL,
  160. "Got orconn %p for channel with global id " U64_FORMAT,
  161. tlschan->conn, U64_PRINTF_ARG(chan->global_identifier));
  162. goto done;
  163. err:
  164. circuitmux_free(chan->cmux);
  165. tor_free(tlschan);
  166. chan = NULL;
  167. done:
  168. /* If we got one, we should register it */
  169. if (chan) channel_register(chan);
  170. return chan;
  171. }
  172. /**
  173. * Return the current channel_tls_t listener
  174. *
  175. * Returns the current channel listener for incoming TLS connections, or
  176. * NULL if none has been established
  177. */
  178. channel_listener_t *
  179. channel_tls_get_listener(void)
  180. {
  181. return channel_tls_listener;
  182. }
  183. /**
  184. * Start a channel_tls_t listener if necessary
  185. *
  186. * Return the current channel_tls_t listener, or start one if we haven't yet,
  187. * and return that.
  188. */
  189. channel_listener_t *
  190. channel_tls_start_listener(void)
  191. {
  192. channel_listener_t *listener;
  193. if (!channel_tls_listener) {
  194. listener = tor_malloc_zero(sizeof(*listener));
  195. channel_init_listener(listener);
  196. listener->state = CHANNEL_LISTENER_STATE_LISTENING;
  197. listener->close = channel_tls_listener_close_method;
  198. listener->describe_transport =
  199. channel_tls_listener_describe_transport_method;
  200. channel_tls_listener = listener;
  201. log_debug(LD_CHANNEL,
  202. "Starting TLS channel listener %p with global id " U64_FORMAT,
  203. listener, U64_PRINTF_ARG(listener->global_identifier));
  204. channel_listener_register(listener);
  205. } else listener = channel_tls_listener;
  206. return listener;
  207. }
  208. /**
  209. * Free everything on shutdown
  210. *
  211. * Not much to do here, since channel_free_all() takes care of a lot, but let's
  212. * get rid of the listener.
  213. */
  214. void
  215. channel_tls_free_all(void)
  216. {
  217. channel_listener_t *old_listener = NULL;
  218. log_debug(LD_CHANNEL,
  219. "Shutting down TLS channels...");
  220. if (channel_tls_listener) {
  221. /*
  222. * When we close it, channel_tls_listener will get nulled out, so save
  223. * a pointer so we can free it.
  224. */
  225. old_listener = channel_tls_listener;
  226. log_debug(LD_CHANNEL,
  227. "Closing channel_tls_listener with ID " U64_FORMAT
  228. " at %p.",
  229. U64_PRINTF_ARG(old_listener->global_identifier),
  230. old_listener);
  231. channel_listener_unregister(old_listener);
  232. channel_listener_mark_for_close(old_listener);
  233. channel_listener_free(old_listener);
  234. tor_assert(channel_tls_listener == NULL);
  235. }
  236. log_debug(LD_CHANNEL,
  237. "Done shutting down TLS channels");
  238. }
  239. /**
  240. * Create a new channel around an incoming or_connection_t
  241. */
  242. channel_t *
  243. channel_tls_handle_incoming(or_connection_t *orconn)
  244. {
  245. channel_tls_t *tlschan = tor_malloc_zero(sizeof(*tlschan));
  246. channel_t *chan = &(tlschan->base_);
  247. tor_assert(orconn);
  248. tor_assert(!(orconn->chan));
  249. channel_tls_common_init(tlschan);
  250. /* Link the channel and orconn to each other */
  251. tlschan->conn = orconn;
  252. orconn->chan = tlschan;
  253. if (is_local_addr(&(TO_CONN(orconn)->addr))) {
  254. log_debug(LD_CHANNEL,
  255. "Marking new incoming channel " U64_FORMAT " at %p as local",
  256. U64_PRINTF_ARG(chan->global_identifier), chan);
  257. channel_mark_local(chan);
  258. } else {
  259. log_debug(LD_CHANNEL,
  260. "Marking new incoming channel " U64_FORMAT " at %p as remote",
  261. U64_PRINTF_ARG(chan->global_identifier), chan);
  262. channel_mark_remote(chan);
  263. }
  264. channel_mark_incoming(chan);
  265. /* Register it */
  266. channel_register(chan);
  267. return chan;
  268. }
  269. /*********
  270. * Casts *
  271. ********/
  272. /**
  273. * Cast a channel_tls_t to a channel_t.
  274. */
  275. channel_t *
  276. channel_tls_to_base(channel_tls_t *tlschan)
  277. {
  278. if (!tlschan) return NULL;
  279. return &(tlschan->base_);
  280. }
  281. /**
  282. * Cast a channel_t to a channel_tls_t, with appropriate type-checking
  283. * asserts.
  284. */
  285. channel_tls_t *
  286. channel_tls_from_base(channel_t *chan)
  287. {
  288. if (!chan) return NULL;
  289. tor_assert(chan->magic == TLS_CHAN_MAGIC);
  290. return (channel_tls_t *)(chan);
  291. }
  292. /********************************************
  293. * Method implementations for channel_tls_t *
  294. *******************************************/
  295. /**
  296. * Close a channel_tls_t
  297. *
  298. * This implements the close method for channel_tls_t
  299. */
  300. static void
  301. channel_tls_close_method(channel_t *chan)
  302. {
  303. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  304. tor_assert(tlschan);
  305. if (tlschan->conn) connection_or_close_normally(tlschan->conn, 1);
  306. else {
  307. /* Weird - we'll have to change the state ourselves, I guess */
  308. log_info(LD_CHANNEL,
  309. "Tried to close channel_tls_t %p with NULL conn",
  310. tlschan);
  311. channel_change_state(chan, CHANNEL_STATE_ERROR);
  312. }
  313. }
  314. /**
  315. * Describe the transport for a channel_tls_t
  316. *
  317. * This returns the string "TLS channel on connection <id>" to the upper
  318. * layer.
  319. */
  320. static const char *
  321. channel_tls_describe_transport_method(channel_t *chan)
  322. {
  323. static char *buf = NULL;
  324. uint64_t id;
  325. channel_tls_t *tlschan;
  326. const char *rv = NULL;
  327. tor_assert(chan);
  328. tlschan = BASE_CHAN_TO_TLS(chan);
  329. if (tlschan->conn) {
  330. id = TO_CONN(tlschan->conn)->global_identifier;
  331. if (buf) tor_free(buf);
  332. tor_asprintf(&buf,
  333. "TLS channel (connection " U64_FORMAT ")",
  334. U64_PRINTF_ARG(id));
  335. rv = buf;
  336. } else {
  337. rv = "TLS channel (no connection)";
  338. }
  339. return rv;
  340. }
  341. /**
  342. * Free a channel_tls_t
  343. *
  344. * This is called by the generic channel layer when freeing a channel_tls_t;
  345. * this happens either on a channel which has already reached
  346. * CHANNEL_STATE_CLOSED or CHANNEL_STATE_ERROR from channel_run_cleanup() or
  347. * on shutdown from channel_free_all(). In the latter case we might still
  348. * have an orconn active (which connection_free_all() will get to later),
  349. * so we should null out its channel pointer now.
  350. */
  351. static void
  352. channel_tls_free_method(channel_t *chan)
  353. {
  354. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  355. tor_assert(tlschan);
  356. if (tlschan->conn) {
  357. tlschan->conn->chan = NULL;
  358. tlschan->conn = NULL;
  359. }
  360. }
  361. /**
  362. * Get the remote address of a channel_tls_t
  363. *
  364. * This implements the get_remote_addr method for channel_tls_t; copy the
  365. * remote endpoint of the channel to addr_out and return 1 (always
  366. * succeeds for this transport).
  367. */
  368. static int
  369. channel_tls_get_remote_addr_method(channel_t *chan, tor_addr_t *addr_out)
  370. {
  371. int rv = 0;
  372. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  373. tor_assert(tlschan);
  374. tor_assert(addr_out);
  375. if (tlschan->conn) {
  376. tor_addr_copy(addr_out, &(TO_CONN(tlschan->conn)->addr));
  377. rv = 1;
  378. } else tor_addr_make_unspec(addr_out);
  379. return rv;
  380. }
  381. /**
  382. * Get the name of the pluggable transport used by a channel_tls_t.
  383. *
  384. * This implements the get_transport_name for channel_tls_t. If the
  385. * channel uses a pluggable transport, copy its name to
  386. * <b>transport_out</b> and return 0. If the channel did not use a
  387. * pluggable transport, return -1. */
  388. static int
  389. channel_tls_get_transport_name_method(channel_t *chan, char **transport_out)
  390. {
  391. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  392. tor_assert(tlschan);
  393. tor_assert(transport_out);
  394. tor_assert(tlschan->conn);
  395. if (!tlschan->conn->ext_or_transport)
  396. return -1;
  397. *transport_out = tor_strdup(tlschan->conn->ext_or_transport);
  398. return 0;
  399. }
  400. /**
  401. * Get endpoint description of a channel_tls_t
  402. *
  403. * This implements the get_remote_descr method for channel_tls_t; it returns
  404. * a text description of the remote endpoint of the channel suitable for use
  405. * in log messages. The req parameter is 0 for the canonical address or 1 for
  406. * the actual address seen.
  407. */
  408. static const char *
  409. channel_tls_get_remote_descr_method(channel_t *chan, int flags)
  410. {
  411. #define MAX_DESCR_LEN 32
  412. static char buf[MAX_DESCR_LEN + 1];
  413. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  414. connection_t *conn;
  415. const char *answer = NULL;
  416. char *addr_str;
  417. tor_assert(tlschan);
  418. if (tlschan->conn) {
  419. conn = TO_CONN(tlschan->conn);
  420. switch (flags) {
  421. case 0:
  422. /* Canonical address with port*/
  423. tor_snprintf(buf, MAX_DESCR_LEN + 1,
  424. "%s:%u", conn->address, conn->port);
  425. answer = buf;
  426. break;
  427. case GRD_FLAG_ORIGINAL:
  428. /* Actual address with port */
  429. addr_str = tor_dup_addr(&(tlschan->conn->real_addr));
  430. tor_snprintf(buf, MAX_DESCR_LEN + 1,
  431. "%s:%u", addr_str, conn->port);
  432. tor_free(addr_str);
  433. answer = buf;
  434. break;
  435. case GRD_FLAG_ADDR_ONLY:
  436. /* Canonical address, no port */
  437. strlcpy(buf, conn->address, sizeof(buf));
  438. answer = buf;
  439. break;
  440. case GRD_FLAG_ORIGINAL|GRD_FLAG_ADDR_ONLY:
  441. /* Actual address, no port */
  442. addr_str = tor_dup_addr(&(tlschan->conn->real_addr));
  443. strlcpy(buf, addr_str, sizeof(buf));
  444. tor_free(addr_str);
  445. answer = buf;
  446. break;
  447. default:
  448. /* Something's broken in channel.c */
  449. tor_assert(1);
  450. }
  451. } else {
  452. strlcpy(buf, "(No connection)", sizeof(buf));
  453. answer = buf;
  454. }
  455. return answer;
  456. }
  457. /**
  458. * Tell the upper layer if we have queued writes
  459. *
  460. * This implements the has_queued_writes method for channel_tls t_; it returns
  461. * 1 iff we have queued writes on the outbuf of the underlying or_connection_t.
  462. */
  463. static int
  464. channel_tls_has_queued_writes_method(channel_t *chan)
  465. {
  466. size_t outbuf_len;
  467. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  468. tor_assert(tlschan);
  469. if (!(tlschan->conn)) {
  470. log_info(LD_CHANNEL,
  471. "something called has_queued_writes on a tlschan "
  472. "(%p with ID " U64_FORMAT " but no conn",
  473. chan, U64_PRINTF_ARG(chan->global_identifier));
  474. }
  475. outbuf_len = (tlschan->conn != NULL) ?
  476. connection_get_outbuf_len(TO_CONN(tlschan->conn)) :
  477. 0;
  478. return (outbuf_len > 0);
  479. }
  480. /**
  481. * Tell the upper layer if we're canonical
  482. *
  483. * This implements the is_canonical method for channel_tls_t; if req is zero,
  484. * it returns whether this is a canonical channel, and if it is one it returns
  485. * whether that can be relied upon.
  486. */
  487. static int
  488. channel_tls_is_canonical_method(channel_t *chan, int req)
  489. {
  490. int answer = 0;
  491. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  492. tor_assert(tlschan);
  493. if (tlschan->conn) {
  494. switch (req) {
  495. case 0:
  496. answer = tlschan->conn->is_canonical;
  497. break;
  498. case 1:
  499. /*
  500. * Is the is_canonical bit reliable? In protocols version 2 and up
  501. * we get the canonical address from a NETINFO cell, but in older
  502. * versions it might be based on an obsolete descriptor.
  503. */
  504. answer = (tlschan->conn->link_proto >= 2);
  505. break;
  506. default:
  507. /* This shouldn't happen; channel.c is broken if it does */
  508. tor_assert(1);
  509. }
  510. }
  511. /* else return 0 for tlschan->conn == NULL */
  512. return answer;
  513. }
  514. /**
  515. * Check if we match an extend_info_t
  516. *
  517. * This implements the matches_extend_info method for channel_tls_t; the upper
  518. * layer wants to know if this channel matches an extend_info_t.
  519. */
  520. static int
  521. channel_tls_matches_extend_info_method(channel_t *chan,
  522. extend_info_t *extend_info)
  523. {
  524. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  525. tor_assert(tlschan);
  526. tor_assert(extend_info);
  527. /* Never match if we have no conn */
  528. if (!(tlschan->conn)) {
  529. log_info(LD_CHANNEL,
  530. "something called matches_extend_info on a tlschan "
  531. "(%p with ID " U64_FORMAT " but no conn",
  532. chan, U64_PRINTF_ARG(chan->global_identifier));
  533. return 0;
  534. }
  535. return (tor_addr_eq(&(extend_info->addr),
  536. &(TO_CONN(tlschan->conn)->addr)) &&
  537. (extend_info->port == TO_CONN(tlschan->conn)->port));
  538. }
  539. /**
  540. * Check if we match a target address; return true iff we do.
  541. *
  542. * This implements the matches_target method for channel_tls t_; the upper
  543. * layer wants to know if this channel matches a target address when extending
  544. * a circuit.
  545. */
  546. static int
  547. channel_tls_matches_target_method(channel_t *chan,
  548. const tor_addr_t *target)
  549. {
  550. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  551. tor_assert(tlschan);
  552. tor_assert(target);
  553. /* Never match if we have no conn */
  554. if (!(tlschan->conn)) {
  555. log_info(LD_CHANNEL,
  556. "something called matches_target on a tlschan "
  557. "(%p with ID " U64_FORMAT " but no conn",
  558. chan, U64_PRINTF_ARG(chan->global_identifier));
  559. return 0;
  560. }
  561. return tor_addr_eq(&(tlschan->conn->real_addr), target);
  562. }
  563. /**
  564. * Tell the upper layer how many cells we can accept to write
  565. *
  566. * This implements the num_cells_writeable method for channel_tls_t; it
  567. * returns an estimate of the number of cells we can accept with
  568. * channel_tls_write_*_cell().
  569. */
  570. static int
  571. channel_tls_num_cells_writeable_method(channel_t *chan)
  572. {
  573. size_t outbuf_len;
  574. int n;
  575. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  576. size_t cell_network_size;
  577. tor_assert(tlschan);
  578. tor_assert(tlschan->conn);
  579. cell_network_size = get_cell_network_size(tlschan->conn->wide_circ_ids);
  580. outbuf_len = connection_get_outbuf_len(TO_CONN(tlschan->conn));
  581. /* Get the number of cells */
  582. n = CEIL_DIV(OR_CONN_HIGHWATER - outbuf_len, cell_network_size);
  583. if (n < 0) n = 0;
  584. return n;
  585. }
  586. /**
  587. * Write a cell to a channel_tls_t
  588. *
  589. * This implements the write_cell method for channel_tls_t; given a
  590. * channel_tls_t and a cell_t, transmit the cell_t.
  591. */
  592. static int
  593. channel_tls_write_cell_method(channel_t *chan, cell_t *cell)
  594. {
  595. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  596. int written = 0;
  597. tor_assert(tlschan);
  598. tor_assert(cell);
  599. if (tlschan->conn) {
  600. connection_or_write_cell_to_buf(cell, tlschan->conn);
  601. ++written;
  602. } else {
  603. log_info(LD_CHANNEL,
  604. "something called write_cell on a tlschan "
  605. "(%p with ID " U64_FORMAT " but no conn",
  606. chan, U64_PRINTF_ARG(chan->global_identifier));
  607. }
  608. return written;
  609. }
  610. /**
  611. * Write a packed cell to a channel_tls_t
  612. *
  613. * This implements the write_packed_cell method for channel_tls_t; given a
  614. * channel_tls_t and a packed_cell_t, transmit the packed_cell_t.
  615. */
  616. static int
  617. channel_tls_write_packed_cell_method(channel_t *chan,
  618. packed_cell_t *packed_cell)
  619. {
  620. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  621. size_t cell_network_size = get_cell_network_size(chan->wide_circ_ids);
  622. int written = 0;
  623. tor_assert(tlschan);
  624. tor_assert(packed_cell);
  625. if (tlschan->conn) {
  626. connection_write_to_buf(packed_cell->body, cell_network_size,
  627. TO_CONN(tlschan->conn));
  628. /* This is where the cell is finished; used to be done from relay.c */
  629. packed_cell_free(packed_cell);
  630. ++written;
  631. } else {
  632. log_info(LD_CHANNEL,
  633. "something called write_packed_cell on a tlschan "
  634. "(%p with ID " U64_FORMAT " but no conn",
  635. chan, U64_PRINTF_ARG(chan->global_identifier));
  636. }
  637. return written;
  638. }
  639. /**
  640. * Write a variable-length cell to a channel_tls_t
  641. *
  642. * This implements the write_var_cell method for channel_tls_t; given a
  643. * channel_tls_t and a var_cell_t, transmit the var_cell_t.
  644. */
  645. static int
  646. channel_tls_write_var_cell_method(channel_t *chan, var_cell_t *var_cell)
  647. {
  648. channel_tls_t *tlschan = BASE_CHAN_TO_TLS(chan);
  649. int written = 0;
  650. tor_assert(tlschan);
  651. tor_assert(var_cell);
  652. if (tlschan->conn) {
  653. connection_or_write_var_cell_to_buf(var_cell, tlschan->conn);
  654. ++written;
  655. } else {
  656. log_info(LD_CHANNEL,
  657. "something called write_var_cell on a tlschan "
  658. "(%p with ID " U64_FORMAT " but no conn",
  659. chan, U64_PRINTF_ARG(chan->global_identifier));
  660. }
  661. return written;
  662. }
  663. /*************************************************
  664. * Method implementations for channel_listener_t *
  665. ************************************************/
  666. /**
  667. * Close a channel_listener_t
  668. *
  669. * This implements the close method for channel_listener_t
  670. */
  671. static void
  672. channel_tls_listener_close_method(channel_listener_t *chan_l)
  673. {
  674. tor_assert(chan_l);
  675. /*
  676. * Listeners we just go ahead and change state through to CLOSED, but
  677. * make sure to check if they're channel_tls_listener to NULL it out.
  678. */
  679. if (chan_l == channel_tls_listener)
  680. channel_tls_listener = NULL;
  681. if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSING ||
  682. chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  683. chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
  684. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSING);
  685. }
  686. if (chan_l->incoming_list) {
  687. SMARTLIST_FOREACH_BEGIN(chan_l->incoming_list,
  688. channel_t *, ichan) {
  689. channel_mark_for_close(ichan);
  690. } SMARTLIST_FOREACH_END(ichan);
  691. smartlist_free(chan_l->incoming_list);
  692. chan_l->incoming_list = NULL;
  693. }
  694. if (!(chan_l->state == CHANNEL_LISTENER_STATE_CLOSED ||
  695. chan_l->state == CHANNEL_LISTENER_STATE_ERROR)) {
  696. channel_listener_change_state(chan_l, CHANNEL_LISTENER_STATE_CLOSED);
  697. }
  698. }
  699. /**
  700. * Describe the transport for a channel_listener_t
  701. *
  702. * This returns the string "TLS channel (listening)" to the upper
  703. * layer.
  704. */
  705. static const char *
  706. channel_tls_listener_describe_transport_method(channel_listener_t *chan_l)
  707. {
  708. tor_assert(chan_l);
  709. return "TLS channel (listening)";
  710. }
  711. /*******************************************************
  712. * Functions for handling events on an or_connection_t *
  713. ******************************************************/
  714. /**
  715. * Handle an orconn state change
  716. *
  717. * This function will be called by connection_or.c when the or_connection_t
  718. * associated with this channel_tls_t changes state.
  719. */
  720. void
  721. channel_tls_handle_state_change_on_orconn(channel_tls_t *chan,
  722. or_connection_t *conn,
  723. uint8_t old_state,
  724. uint8_t state)
  725. {
  726. channel_t *base_chan;
  727. tor_assert(chan);
  728. tor_assert(conn);
  729. tor_assert(conn->chan == chan);
  730. tor_assert(chan->conn == conn);
  731. /* -Werror appeasement */
  732. tor_assert(old_state == old_state);
  733. base_chan = TLS_CHAN_TO_BASE(chan);
  734. /* Make sure the base connection state makes sense - shouldn't be error,
  735. * closed or listening. */
  736. tor_assert(base_chan->state == CHANNEL_STATE_OPENING ||
  737. base_chan->state == CHANNEL_STATE_OPEN ||
  738. base_chan->state == CHANNEL_STATE_MAINT ||
  739. base_chan->state == CHANNEL_STATE_CLOSING);
  740. /* Did we just go to state open? */
  741. if (state == OR_CONN_STATE_OPEN) {
  742. /*
  743. * We can go to CHANNEL_STATE_OPEN from CHANNEL_STATE_OPENING or
  744. * CHANNEL_STATE_MAINT on this.
  745. */
  746. channel_change_state(base_chan, CHANNEL_STATE_OPEN);
  747. /* We might have just become writeable; check and tell the scheduler */
  748. if (connection_or_num_cells_writeable(conn) > 0) {
  749. scheduler_channel_wants_writes(base_chan);
  750. }
  751. } else {
  752. /*
  753. * Not open, so from CHANNEL_STATE_OPEN we go to CHANNEL_STATE_MAINT,
  754. * otherwise no change.
  755. */
  756. if (base_chan->state == CHANNEL_STATE_OPEN) {
  757. channel_change_state(base_chan, CHANNEL_STATE_MAINT);
  758. }
  759. }
  760. }
  761. #ifdef KEEP_TIMING_STATS
  762. /**
  763. * Timing states wrapper
  764. *
  765. * This is a wrapper function around the actual function that processes the
  766. * <b>cell</b> that just arrived on <b>chan</b>. Increment <b>*time</b>
  767. * by the number of microseconds used by the call to <b>*func(cell, chan)</b>.
  768. */
  769. static void
  770. channel_tls_time_process_cell(cell_t *cell, channel_tls_t *chan, int *time,
  771. void (*func)(cell_t *, channel_tls_t *))
  772. {
  773. struct timeval start, end;
  774. long time_passed;
  775. tor_gettimeofday(&start);
  776. (*func)(cell, chan);
  777. tor_gettimeofday(&end);
  778. time_passed = tv_udiff(&start, &end) ;
  779. if (time_passed > 10000) { /* more than 10ms */
  780. log_debug(LD_OR,"That call just took %ld ms.",time_passed/1000);
  781. }
  782. if (time_passed < 0) {
  783. log_info(LD_GENERAL,"That call took us back in time!");
  784. time_passed = 0;
  785. }
  786. *time += time_passed;
  787. }
  788. #endif
  789. /**
  790. * Handle an incoming cell on a channel_tls_t
  791. *
  792. * This is called from connection_or.c to handle an arriving cell; it checks
  793. * for cell types specific to the handshake for this transport protocol and
  794. * handles them, and queues all other cells to the channel_t layer, which
  795. * eventually will hand them off to command.c.
  796. */
  797. void
  798. channel_tls_handle_cell(cell_t *cell, or_connection_t *conn)
  799. {
  800. channel_tls_t *chan;
  801. int handshaking;
  802. #ifdef KEEP_TIMING_STATS
  803. #define PROCESS_CELL(tp, cl, cn) STMT_BEGIN { \
  804. ++num ## tp; \
  805. channel_tls_time_process_cell(cl, cn, & tp ## time , \
  806. channel_tls_process_ ## tp ## _cell); \
  807. } STMT_END
  808. #else
  809. #define PROCESS_CELL(tp, cl, cn) channel_tls_process_ ## tp ## _cell(cl, cn)
  810. #endif
  811. tor_assert(cell);
  812. tor_assert(conn);
  813. chan = conn->chan;
  814. if (!chan) {
  815. log_warn(LD_CHANNEL,
  816. "Got a cell_t on an OR connection with no channel");
  817. return;
  818. }
  819. handshaking = (TO_CONN(conn)->state != OR_CONN_STATE_OPEN);
  820. if (conn->base_.marked_for_close)
  821. return;
  822. /* Reject all but VERSIONS and NETINFO when handshaking. */
  823. /* (VERSIONS should actually be impossible; it's variable-length.) */
  824. if (handshaking && cell->command != CELL_VERSIONS &&
  825. cell->command != CELL_NETINFO) {
  826. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  827. "Received unexpected cell command %d in chan state %s / "
  828. "conn state %s; closing the connection.",
  829. (int)cell->command,
  830. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  831. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state));
  832. connection_or_close_for_error(conn, 0);
  833. return;
  834. }
  835. if (conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3)
  836. or_handshake_state_record_cell(conn, conn->handshake_state, cell, 1);
  837. switch (cell->command) {
  838. case CELL_PADDING:
  839. ++stats_n_padding_cells_processed;
  840. /* do nothing */
  841. break;
  842. case CELL_VERSIONS:
  843. tor_fragile_assert();
  844. break;
  845. case CELL_NETINFO:
  846. ++stats_n_netinfo_cells_processed;
  847. PROCESS_CELL(netinfo, cell, chan);
  848. break;
  849. case CELL_CREATE:
  850. case CELL_CREATE_FAST:
  851. case CELL_CREATED:
  852. case CELL_CREATED_FAST:
  853. case CELL_RELAY:
  854. case CELL_RELAY_EARLY:
  855. case CELL_DESTROY:
  856. case CELL_CREATE2:
  857. case CELL_CREATED2:
  858. /*
  859. * These are all transport independent and we pass them up through the
  860. * channel_t mechanism. They are ultimately handled in command.c.
  861. */
  862. channel_queue_cell(TLS_CHAN_TO_BASE(chan), cell);
  863. break;
  864. default:
  865. log_fn(LOG_INFO, LD_PROTOCOL,
  866. "Cell of unknown type (%d) received in channeltls.c. "
  867. "Dropping.",
  868. cell->command);
  869. break;
  870. }
  871. }
  872. /**
  873. * Handle an incoming variable-length cell on a channel_tls_t
  874. *
  875. * Process a <b>var_cell</b> that was just received on <b>conn</b>. Keep
  876. * internal statistics about how many of each cell we've processed so far
  877. * this second, and the total number of microseconds it took to
  878. * process each type of cell. All the var_cell commands are handshake-
  879. * related and live below the channel_t layer, so no variable-length
  880. * cells ever get delivered in the current implementation, but I've left
  881. * the mechanism in place for future use.
  882. */
  883. void
  884. channel_tls_handle_var_cell(var_cell_t *var_cell, or_connection_t *conn)
  885. {
  886. channel_tls_t *chan;
  887. #ifdef KEEP_TIMING_STATS
  888. /* how many of each cell have we seen so far this second? needs better
  889. * name. */
  890. static int num_versions = 0, num_certs = 0;
  891. static time_t current_second = 0; /* from previous calls to time */
  892. time_t now = time(NULL);
  893. if (current_second == 0) current_second = now;
  894. if (now > current_second) { /* the second has rolled over */
  895. /* print stats */
  896. log_info(LD_OR,
  897. "At end of second: %d versions (%d ms), %d certs (%d ms)",
  898. num_versions, versions_time / ((now - current_second) * 1000),
  899. num_certs, certs_time / ((now - current_second) * 1000));
  900. num_versions = num_certs = 0;
  901. versions_time = certs_time = 0;
  902. /* remember which second it is, for next time */
  903. current_second = now;
  904. }
  905. #endif
  906. tor_assert(var_cell);
  907. tor_assert(conn);
  908. chan = conn->chan;
  909. if (!chan) {
  910. log_warn(LD_CHANNEL,
  911. "Got a var_cell_t on an OR connection with no channel");
  912. return;
  913. }
  914. if (TO_CONN(conn)->marked_for_close)
  915. return;
  916. switch (TO_CONN(conn)->state) {
  917. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  918. if (var_cell->command != CELL_VERSIONS) {
  919. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  920. "Received a cell with command %d in unexpected "
  921. "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
  922. "closing the connection.",
  923. (int)(var_cell->command),
  924. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  925. TO_CONN(conn)->state,
  926. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  927. (int)(TLS_CHAN_TO_BASE(chan)->state));
  928. /*
  929. * The code in connection_or.c will tell channel_t to close for
  930. * error; it will go to CHANNEL_STATE_CLOSING, and then to
  931. * CHANNEL_STATE_ERROR when conn is closed.
  932. */
  933. connection_or_close_for_error(conn, 0);
  934. return;
  935. }
  936. break;
  937. case OR_CONN_STATE_TLS_HANDSHAKING:
  938. /* If we're using bufferevents, it's entirely possible for us to
  939. * notice "hey, data arrived!" before we notice "hey, the handshake
  940. * finished!" And we need to be accepting both at once to handle both
  941. * the v2 and v3 handshakes. */
  942. /* fall through */
  943. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  944. if (!(command_allowed_before_handshake(var_cell->command))) {
  945. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  946. "Received a cell with command %d in unexpected "
  947. "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
  948. "closing the connection.",
  949. (int)(var_cell->command),
  950. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  951. (int)(TO_CONN(conn)->state),
  952. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  953. (int)(TLS_CHAN_TO_BASE(chan)->state));
  954. /* see above comment about CHANNEL_STATE_ERROR */
  955. connection_or_close_for_error(conn, 0);
  956. return;
  957. } else {
  958. if (enter_v3_handshake_with_cell(var_cell, chan) < 0)
  959. return;
  960. }
  961. break;
  962. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  963. if (var_cell->command != CELL_AUTHENTICATE)
  964. or_handshake_state_record_var_cell(conn, conn->handshake_state,
  965. var_cell, 1);
  966. break; /* Everything is allowed */
  967. case OR_CONN_STATE_OPEN:
  968. if (conn->link_proto < 3) {
  969. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  970. "Received a variable-length cell with command %d in orconn "
  971. "state %s [%d], channel state %s [%d] with link protocol %d; "
  972. "ignoring it.",
  973. (int)(var_cell->command),
  974. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  975. (int)(TO_CONN(conn)->state),
  976. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  977. (int)(TLS_CHAN_TO_BASE(chan)->state),
  978. (int)(conn->link_proto));
  979. return;
  980. }
  981. break;
  982. default:
  983. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  984. "Received var-length cell with command %d in unexpected "
  985. "orconn state \"%s\" [%d], channel state \"%s\" [%d]; "
  986. "ignoring it.",
  987. (int)(var_cell->command),
  988. conn_state_to_string(CONN_TYPE_OR, TO_CONN(conn)->state),
  989. (int)(TO_CONN(conn)->state),
  990. channel_state_to_string(TLS_CHAN_TO_BASE(chan)->state),
  991. (int)(TLS_CHAN_TO_BASE(chan)->state));
  992. return;
  993. }
  994. /* Now handle the cell */
  995. switch (var_cell->command) {
  996. case CELL_VERSIONS:
  997. ++stats_n_versions_cells_processed;
  998. PROCESS_CELL(versions, var_cell, chan);
  999. break;
  1000. case CELL_VPADDING:
  1001. ++stats_n_vpadding_cells_processed;
  1002. /* Do nothing */
  1003. break;
  1004. case CELL_CERTS:
  1005. ++stats_n_certs_cells_processed;
  1006. PROCESS_CELL(certs, var_cell, chan);
  1007. break;
  1008. case CELL_AUTH_CHALLENGE:
  1009. ++stats_n_auth_challenge_cells_processed;
  1010. PROCESS_CELL(auth_challenge, var_cell, chan);
  1011. break;
  1012. case CELL_AUTHENTICATE:
  1013. ++stats_n_authenticate_cells_processed;
  1014. PROCESS_CELL(authenticate, var_cell, chan);
  1015. break;
  1016. case CELL_AUTHORIZE:
  1017. ++stats_n_authorize_cells_processed;
  1018. /* Ignored so far. */
  1019. break;
  1020. default:
  1021. log_fn(LOG_INFO, LD_PROTOCOL,
  1022. "Variable-length cell of unknown type (%d) received.",
  1023. (int)(var_cell->command));
  1024. break;
  1025. }
  1026. }
  1027. /**
  1028. * Update channel marks after connection_or.c has changed an address
  1029. *
  1030. * This is called from connection_or_init_conn_from_address() after the
  1031. * connection's _base.addr or real_addr fields have potentially been changed
  1032. * so we can recalculate the local mark. Notably, this happens when incoming
  1033. * connections are reverse-proxied and we only learn the real address of the
  1034. * remote router by looking it up in the consensus after we finish the
  1035. * handshake and know an authenticated identity digest.
  1036. */
  1037. void
  1038. channel_tls_update_marks(or_connection_t *conn)
  1039. {
  1040. channel_t *chan = NULL;
  1041. tor_assert(conn);
  1042. tor_assert(conn->chan);
  1043. chan = TLS_CHAN_TO_BASE(conn->chan);
  1044. if (is_local_addr(&(TO_CONN(conn)->addr))) {
  1045. if (!channel_is_local(chan)) {
  1046. log_debug(LD_CHANNEL,
  1047. "Marking channel " U64_FORMAT " at %p as local",
  1048. U64_PRINTF_ARG(chan->global_identifier), chan);
  1049. channel_mark_local(chan);
  1050. }
  1051. } else {
  1052. if (channel_is_local(chan)) {
  1053. log_debug(LD_CHANNEL,
  1054. "Marking channel " U64_FORMAT " at %p as remote",
  1055. U64_PRINTF_ARG(chan->global_identifier), chan);
  1056. channel_mark_remote(chan);
  1057. }
  1058. }
  1059. }
  1060. /**
  1061. * Check if this cell type is allowed before the handshake is finished
  1062. *
  1063. * Return true if <b>command</b> is a cell command that's allowed to start a
  1064. * V3 handshake.
  1065. */
  1066. static int
  1067. command_allowed_before_handshake(uint8_t command)
  1068. {
  1069. switch (command) {
  1070. case CELL_VERSIONS:
  1071. case CELL_VPADDING:
  1072. case CELL_AUTHORIZE:
  1073. return 1;
  1074. default:
  1075. return 0;
  1076. }
  1077. }
  1078. /**
  1079. * Start a V3 handshake on an incoming connection
  1080. *
  1081. * Called when we as a server receive an appropriate cell while waiting
  1082. * either for a cell or a TLS handshake. Set the connection's state to
  1083. * "handshaking_v3', initializes the or_handshake_state field as needed,
  1084. * and add the cell to the hash of incoming cells.)
  1085. */
  1086. static int
  1087. enter_v3_handshake_with_cell(var_cell_t *cell, channel_tls_t *chan)
  1088. {
  1089. int started_here = 0;
  1090. tor_assert(cell);
  1091. tor_assert(chan);
  1092. tor_assert(chan->conn);
  1093. started_here = connection_or_nonopen_was_started_here(chan->conn);
  1094. tor_assert(TO_CONN(chan->conn)->state == OR_CONN_STATE_TLS_HANDSHAKING ||
  1095. TO_CONN(chan->conn)->state ==
  1096. OR_CONN_STATE_TLS_SERVER_RENEGOTIATING);
  1097. if (started_here) {
  1098. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1099. "Received a cell while TLS-handshaking, not in "
  1100. "OR_HANDSHAKING_V3, on a connection we originated.");
  1101. }
  1102. connection_or_block_renegotiation(chan->conn);
  1103. chan->conn->base_.state = OR_CONN_STATE_OR_HANDSHAKING_V3;
  1104. if (connection_init_or_handshake_state(chan->conn, started_here) < 0) {
  1105. connection_or_close_for_error(chan->conn, 0);
  1106. return -1;
  1107. }
  1108. or_handshake_state_record_var_cell(chan->conn,
  1109. chan->conn->handshake_state, cell, 1);
  1110. return 0;
  1111. }
  1112. /**
  1113. * Process a 'versions' cell.
  1114. *
  1115. * This function is called to handle an incoming VERSIONS cell; the current
  1116. * link protocol version must be 0 to indicate that no version has yet been
  1117. * negotiated. We compare the versions in the cell to the list of versions
  1118. * we support, pick the highest version we have in common, and continue the
  1119. * negotiation from there.
  1120. */
  1121. static void
  1122. channel_tls_process_versions_cell(var_cell_t *cell, channel_tls_t *chan)
  1123. {
  1124. int highest_supported_version = 0;
  1125. int started_here = 0;
  1126. tor_assert(cell);
  1127. tor_assert(chan);
  1128. tor_assert(chan->conn);
  1129. if ((cell->payload_len % 2) == 1) {
  1130. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1131. "Received a VERSION cell with odd payload length %d; "
  1132. "closing connection.",cell->payload_len);
  1133. connection_or_close_for_error(chan->conn, 0);
  1134. return;
  1135. }
  1136. started_here = connection_or_nonopen_was_started_here(chan->conn);
  1137. if (chan->conn->link_proto != 0 ||
  1138. (chan->conn->handshake_state &&
  1139. chan->conn->handshake_state->received_versions)) {
  1140. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1141. "Received a VERSIONS cell on a connection with its version "
  1142. "already set to %d; dropping",
  1143. (int)(chan->conn->link_proto));
  1144. return;
  1145. }
  1146. switch (chan->conn->base_.state)
  1147. {
  1148. case OR_CONN_STATE_OR_HANDSHAKING_V2:
  1149. case OR_CONN_STATE_OR_HANDSHAKING_V3:
  1150. break;
  1151. case OR_CONN_STATE_TLS_HANDSHAKING:
  1152. case OR_CONN_STATE_TLS_SERVER_RENEGOTIATING:
  1153. default:
  1154. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1155. "VERSIONS cell while in unexpected state");
  1156. return;
  1157. }
  1158. tor_assert(chan->conn->handshake_state);
  1159. {
  1160. int i;
  1161. const uint8_t *cp = cell->payload;
  1162. for (i = 0; i < cell->payload_len / 2; ++i, cp += 2) {
  1163. uint16_t v = ntohs(get_uint16(cp));
  1164. if (is_or_protocol_version_known(v) && v > highest_supported_version)
  1165. highest_supported_version = v;
  1166. }
  1167. }
  1168. if (!highest_supported_version) {
  1169. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1170. "Couldn't find a version in common between my version list and the "
  1171. "list in the VERSIONS cell; closing connection.");
  1172. connection_or_close_for_error(chan->conn, 0);
  1173. return;
  1174. } else if (highest_supported_version == 1) {
  1175. /* Negotiating version 1 makes no sense, since version 1 has no VERSIONS
  1176. * cells. */
  1177. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1178. "Used version negotiation protocol to negotiate a v1 connection. "
  1179. "That's crazily non-compliant. Closing connection.");
  1180. connection_or_close_for_error(chan->conn, 0);
  1181. return;
  1182. } else if (highest_supported_version < 3 &&
  1183. chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
  1184. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1185. "Negotiated link protocol 2 or lower after doing a v3 TLS "
  1186. "handshake. Closing connection.");
  1187. connection_or_close_for_error(chan->conn, 0);
  1188. return;
  1189. } else if (highest_supported_version != 2 &&
  1190. chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V2) {
  1191. /* XXXX This should eventually be a log_protocol_warn */
  1192. log_fn(LOG_WARN, LD_OR,
  1193. "Negotiated link with non-2 protocol after doing a v2 TLS "
  1194. "handshake with %s. Closing connection.",
  1195. fmt_addr(&chan->conn->base_.addr));
  1196. connection_or_close_for_error(chan->conn, 0);
  1197. return;
  1198. }
  1199. chan->conn->link_proto = highest_supported_version;
  1200. chan->conn->handshake_state->received_versions = 1;
  1201. if (chan->conn->link_proto == 2) {
  1202. log_info(LD_OR,
  1203. "Negotiated version %d with %s:%d; sending NETINFO.",
  1204. highest_supported_version,
  1205. safe_str_client(chan->conn->base_.address),
  1206. chan->conn->base_.port);
  1207. if (connection_or_send_netinfo(chan->conn) < 0) {
  1208. connection_or_close_for_error(chan->conn, 0);
  1209. return;
  1210. }
  1211. } else {
  1212. const int send_versions = !started_here;
  1213. /* If we want to authenticate, send a CERTS cell */
  1214. const int send_certs = !started_here || public_server_mode(get_options());
  1215. /* If we're a host that got a connection, ask for authentication. */
  1216. const int send_chall = !started_here;
  1217. /* If our certs cell will authenticate us, we can send a netinfo cell
  1218. * right now. */
  1219. const int send_netinfo = !started_here;
  1220. const int send_any =
  1221. send_versions || send_certs || send_chall || send_netinfo;
  1222. tor_assert(chan->conn->link_proto >= 3);
  1223. log_info(LD_OR,
  1224. "Negotiated version %d with %s:%d; %s%s%s%s%s",
  1225. highest_supported_version,
  1226. safe_str_client(chan->conn->base_.address),
  1227. chan->conn->base_.port,
  1228. send_any ? "Sending cells:" : "Waiting for CERTS cell",
  1229. send_versions ? " VERSIONS" : "",
  1230. send_certs ? " CERTS" : "",
  1231. send_chall ? " AUTH_CHALLENGE" : "",
  1232. send_netinfo ? " NETINFO" : "");
  1233. #ifdef DISABLE_V3_LINKPROTO_SERVERSIDE
  1234. if (1) {
  1235. connection_or_close_normally(chan->conn, 1);
  1236. return;
  1237. }
  1238. #endif
  1239. if (send_versions) {
  1240. if (connection_or_send_versions(chan->conn, 1) < 0) {
  1241. log_warn(LD_OR, "Couldn't send versions cell");
  1242. connection_or_close_for_error(chan->conn, 0);
  1243. return;
  1244. }
  1245. }
  1246. /* We set this after sending the verions cell. */
  1247. /*XXXXX symbolic const.*/
  1248. chan->base_.wide_circ_ids =
  1249. chan->conn->link_proto >= MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS;
  1250. chan->conn->wide_circ_ids = chan->base_.wide_circ_ids;
  1251. if (send_certs) {
  1252. if (connection_or_send_certs_cell(chan->conn) < 0) {
  1253. log_warn(LD_OR, "Couldn't send certs cell");
  1254. connection_or_close_for_error(chan->conn, 0);
  1255. return;
  1256. }
  1257. }
  1258. if (send_chall) {
  1259. if (connection_or_send_auth_challenge_cell(chan->conn) < 0) {
  1260. log_warn(LD_OR, "Couldn't send auth_challenge cell");
  1261. connection_or_close_for_error(chan->conn, 0);
  1262. return;
  1263. }
  1264. }
  1265. if (send_netinfo) {
  1266. if (connection_or_send_netinfo(chan->conn) < 0) {
  1267. log_warn(LD_OR, "Couldn't send netinfo cell");
  1268. connection_or_close_for_error(chan->conn, 0);
  1269. return;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. /**
  1275. * Process a 'netinfo' cell
  1276. *
  1277. * This function is called to handle an incoming NETINFO cell; read and act
  1278. * on its contents, and set the connection state to "open".
  1279. */
  1280. static void
  1281. channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan)
  1282. {
  1283. time_t timestamp;
  1284. uint8_t my_addr_type;
  1285. uint8_t my_addr_len;
  1286. const uint8_t *my_addr_ptr;
  1287. const uint8_t *cp, *end;
  1288. uint8_t n_other_addrs;
  1289. time_t now = time(NULL);
  1290. long apparent_skew = 0;
  1291. tor_addr_t my_apparent_addr = TOR_ADDR_NULL;
  1292. tor_assert(cell);
  1293. tor_assert(chan);
  1294. tor_assert(chan->conn);
  1295. if (chan->conn->link_proto < 2) {
  1296. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1297. "Received a NETINFO cell on %s connection; dropping.",
  1298. chan->conn->link_proto == 0 ? "non-versioned" : "a v1");
  1299. return;
  1300. }
  1301. if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V2 &&
  1302. chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3) {
  1303. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1304. "Received a NETINFO cell on non-handshaking connection; dropping.");
  1305. return;
  1306. }
  1307. tor_assert(chan->conn->handshake_state &&
  1308. chan->conn->handshake_state->received_versions);
  1309. if (chan->conn->base_.state == OR_CONN_STATE_OR_HANDSHAKING_V3) {
  1310. tor_assert(chan->conn->link_proto >= 3);
  1311. if (chan->conn->handshake_state->started_here) {
  1312. if (!(chan->conn->handshake_state->authenticated)) {
  1313. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1314. "Got a NETINFO cell from server, "
  1315. "but no authentication. Closing the connection.");
  1316. connection_or_close_for_error(chan->conn, 0);
  1317. return;
  1318. }
  1319. } else {
  1320. /* we're the server. If the client never authenticated, we have
  1321. some housekeeping to do.*/
  1322. if (!(chan->conn->handshake_state->authenticated)) {
  1323. tor_assert(tor_digest_is_zero(
  1324. (const char*)(chan->conn->handshake_state->
  1325. authenticated_peer_id)));
  1326. channel_set_circid_type(TLS_CHAN_TO_BASE(chan), NULL,
  1327. chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
  1328. connection_or_init_conn_from_address(chan->conn,
  1329. &(chan->conn->base_.addr),
  1330. chan->conn->base_.port,
  1331. (const char*)(chan->conn->handshake_state->
  1332. authenticated_peer_id),
  1333. 0);
  1334. }
  1335. }
  1336. }
  1337. /* Decode the cell. */
  1338. timestamp = ntohl(get_uint32(cell->payload));
  1339. if (labs(now - chan->conn->handshake_state->sent_versions_at) < 180) {
  1340. apparent_skew = now - timestamp;
  1341. }
  1342. my_addr_type = (uint8_t) cell->payload[4];
  1343. my_addr_len = (uint8_t) cell->payload[5];
  1344. my_addr_ptr = (uint8_t*) cell->payload + 6;
  1345. end = cell->payload + CELL_PAYLOAD_SIZE;
  1346. cp = cell->payload + 6 + my_addr_len;
  1347. /* We used to check:
  1348. * if (my_addr_len >= CELL_PAYLOAD_SIZE - 6) {
  1349. *
  1350. * This is actually never going to happen, since my_addr_len is at most 255,
  1351. * and CELL_PAYLOAD_LEN - 6 is 503. So we know that cp is < end. */
  1352. if (my_addr_type == RESOLVED_TYPE_IPV4 && my_addr_len == 4) {
  1353. tor_addr_from_ipv4n(&my_apparent_addr, get_uint32(my_addr_ptr));
  1354. } else if (my_addr_type == RESOLVED_TYPE_IPV6 && my_addr_len == 16) {
  1355. tor_addr_from_ipv6_bytes(&my_apparent_addr, (const char *) my_addr_ptr);
  1356. }
  1357. n_other_addrs = (uint8_t) *cp++;
  1358. while (n_other_addrs && cp < end-2) {
  1359. /* Consider all the other addresses; if any matches, this connection is
  1360. * "canonical." */
  1361. tor_addr_t addr;
  1362. const uint8_t *next =
  1363. decode_address_from_payload(&addr, cp, (int)(end-cp));
  1364. if (next == NULL) {
  1365. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1366. "Bad address in netinfo cell; closing connection.");
  1367. connection_or_close_for_error(chan->conn, 0);
  1368. return;
  1369. }
  1370. if (tor_addr_eq(&addr, &(chan->conn->real_addr))) {
  1371. connection_or_set_canonical(chan->conn, 1);
  1372. break;
  1373. }
  1374. cp = next;
  1375. --n_other_addrs;
  1376. }
  1377. /* Act on apparent skew. */
  1378. /** Warn when we get a netinfo skew with at least this value. */
  1379. #define NETINFO_NOTICE_SKEW 3600
  1380. if (labs(apparent_skew) > NETINFO_NOTICE_SKEW &&
  1381. router_get_by_id_digest(chan->conn->identity_digest)) {
  1382. char dbuf[64];
  1383. int severity;
  1384. /*XXXX be smarter about when everybody says we are skewed. */
  1385. if (router_digest_is_trusted_dir(chan->conn->identity_digest))
  1386. severity = LOG_WARN;
  1387. else
  1388. severity = LOG_INFO;
  1389. format_time_interval(dbuf, sizeof(dbuf), apparent_skew);
  1390. log_fn(severity, LD_GENERAL,
  1391. "Received NETINFO cell with skewed time from "
  1392. "server at %s:%d. It seems that our clock is %s by %s, or "
  1393. "that theirs is %s. Tor requires an accurate clock to work: "
  1394. "please check your time and date settings.",
  1395. chan->conn->base_.address,
  1396. (int)(chan->conn->base_.port),
  1397. apparent_skew > 0 ? "ahead" : "behind",
  1398. dbuf,
  1399. apparent_skew > 0 ? "behind" : "ahead");
  1400. if (severity == LOG_WARN) /* only tell the controller if an authority */
  1401. control_event_general_status(LOG_WARN,
  1402. "CLOCK_SKEW SKEW=%ld SOURCE=OR:%s:%d",
  1403. apparent_skew,
  1404. chan->conn->base_.address,
  1405. chan->conn->base_.port);
  1406. }
  1407. /* XXX maybe act on my_apparent_addr, if the source is sufficiently
  1408. * trustworthy. */
  1409. if (! chan->conn->handshake_state->sent_netinfo) {
  1410. /* If we were prepared to authenticate, but we never got an AUTH_CHALLENGE
  1411. * cell, then we would not previously have sent a NETINFO cell. Do so
  1412. * now. */
  1413. if (connection_or_send_netinfo(chan->conn) < 0) {
  1414. connection_or_close_for_error(chan->conn, 0);
  1415. return;
  1416. }
  1417. }
  1418. if (connection_or_set_state_open(chan->conn) < 0) {
  1419. log_fn(LOG_PROTOCOL_WARN, LD_OR,
  1420. "Got good NETINFO cell from %s:%d; but "
  1421. "was unable to make the OR connection become open.",
  1422. safe_str_client(chan->conn->base_.address),
  1423. chan->conn->base_.port);
  1424. connection_or_close_for_error(chan->conn, 0);
  1425. } else {
  1426. log_info(LD_OR,
  1427. "Got good NETINFO cell from %s:%d; OR connection is now "
  1428. "open, using protocol version %d. Its ID digest is %s. "
  1429. "Our address is apparently %s.",
  1430. safe_str_client(chan->conn->base_.address),
  1431. chan->conn->base_.port,
  1432. (int)(chan->conn->link_proto),
  1433. hex_str(TLS_CHAN_TO_BASE(chan)->identity_digest,
  1434. DIGEST_LEN),
  1435. tor_addr_is_null(&my_apparent_addr) ?
  1436. "<none>" : fmt_and_decorate_addr(&my_apparent_addr));
  1437. }
  1438. assert_connection_ok(TO_CONN(chan->conn),time(NULL));
  1439. }
  1440. /**
  1441. * Process a CERTS cell from a channel.
  1442. *
  1443. * This function is called to process an incoming CERTS cell on a
  1444. * channel_tls_t:
  1445. *
  1446. * If the other side should not have sent us a CERTS cell, or the cell is
  1447. * malformed, or it is supposed to authenticate the TLS key but it doesn't,
  1448. * then mark the connection.
  1449. *
  1450. * If the cell has a good cert chain and we're doing a v3 handshake, then
  1451. * store the certificates in or_handshake_state. If this is the client side
  1452. * of the connection, we then authenticate the server or mark the connection.
  1453. * If it's the server side, wait for an AUTHENTICATE cell.
  1454. */
  1455. static void
  1456. channel_tls_process_certs_cell(var_cell_t *cell, channel_tls_t *chan)
  1457. {
  1458. tor_cert_t *link_cert = NULL;
  1459. tor_cert_t *id_cert = NULL;
  1460. tor_cert_t *auth_cert = NULL;
  1461. uint8_t *ptr;
  1462. int n_certs, i;
  1463. int send_netinfo = 0;
  1464. tor_assert(cell);
  1465. tor_assert(chan);
  1466. tor_assert(chan->conn);
  1467. #define ERR(s) \
  1468. do { \
  1469. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  1470. "Received a bad CERTS cell from %s:%d: %s", \
  1471. safe_str(chan->conn->base_.address), \
  1472. chan->conn->base_.port, (s)); \
  1473. connection_or_close_for_error(chan->conn, 0); \
  1474. goto err; \
  1475. } while (0)
  1476. if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  1477. ERR("We're not doing a v3 handshake!");
  1478. if (chan->conn->link_proto < 3)
  1479. ERR("We're not using link protocol >= 3");
  1480. if (chan->conn->handshake_state->received_certs_cell)
  1481. ERR("We already got one");
  1482. if (chan->conn->handshake_state->authenticated) {
  1483. /* Should be unreachable, but let's make sure. */
  1484. ERR("We're already authenticated!");
  1485. }
  1486. if (cell->payload_len < 1)
  1487. ERR("It had no body");
  1488. if (cell->circ_id)
  1489. ERR("It had a nonzero circuit ID");
  1490. n_certs = cell->payload[0];
  1491. ptr = cell->payload + 1;
  1492. for (i = 0; i < n_certs; ++i) {
  1493. uint8_t cert_type;
  1494. uint16_t cert_len;
  1495. if (cell->payload_len < 3)
  1496. goto truncated;
  1497. if (ptr > cell->payload + cell->payload_len - 3) {
  1498. goto truncated;
  1499. }
  1500. cert_type = *ptr;
  1501. cert_len = ntohs(get_uint16(ptr+1));
  1502. if (cell->payload_len < 3 + cert_len)
  1503. goto truncated;
  1504. if (ptr > cell->payload + cell->payload_len - cert_len - 3) {
  1505. goto truncated;
  1506. }
  1507. if (cert_type == OR_CERT_TYPE_TLS_LINK ||
  1508. cert_type == OR_CERT_TYPE_ID_1024 ||
  1509. cert_type == OR_CERT_TYPE_AUTH_1024) {
  1510. tor_cert_t *cert = tor_cert_decode(ptr + 3, cert_len);
  1511. if (!cert) {
  1512. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
  1513. "Received undecodable certificate in CERTS cell from %s:%d",
  1514. safe_str(chan->conn->base_.address),
  1515. chan->conn->base_.port);
  1516. } else {
  1517. if (cert_type == OR_CERT_TYPE_TLS_LINK) {
  1518. if (link_cert) {
  1519. tor_cert_free(cert);
  1520. ERR("Too many TLS_LINK certificates");
  1521. }
  1522. link_cert = cert;
  1523. } else if (cert_type == OR_CERT_TYPE_ID_1024) {
  1524. if (id_cert) {
  1525. tor_cert_free(cert);
  1526. ERR("Too many ID_1024 certificates");
  1527. }
  1528. id_cert = cert;
  1529. } else if (cert_type == OR_CERT_TYPE_AUTH_1024) {
  1530. if (auth_cert) {
  1531. tor_cert_free(cert);
  1532. ERR("Too many AUTH_1024 certificates");
  1533. }
  1534. auth_cert = cert;
  1535. } else {
  1536. tor_cert_free(cert);
  1537. }
  1538. }
  1539. }
  1540. ptr += 3 + cert_len;
  1541. continue;
  1542. truncated:
  1543. ERR("It ends in the middle of a certificate");
  1544. }
  1545. if (chan->conn->handshake_state->started_here) {
  1546. int severity;
  1547. if (! (id_cert && link_cert))
  1548. ERR("The certs we wanted were missing");
  1549. /* Okay. We should be able to check the certificates now. */
  1550. if (! tor_tls_cert_matches_key(chan->conn->tls, link_cert)) {
  1551. ERR("The link certificate didn't match the TLS public key");
  1552. }
  1553. /* Note that this warns more loudly about time and validity if we were
  1554. * _trying_ to connect to an authority, not necessarily if we _did_ connect
  1555. * to one. */
  1556. if (router_digest_is_trusted_dir(
  1557. TLS_CHAN_TO_BASE(chan)->identity_digest))
  1558. severity = LOG_WARN;
  1559. else
  1560. severity = LOG_PROTOCOL_WARN;
  1561. if (! tor_tls_cert_is_valid(severity, link_cert, id_cert, 0))
  1562. ERR("The link certificate was not valid");
  1563. if (! tor_tls_cert_is_valid(severity, id_cert, id_cert, 1))
  1564. ERR("The ID certificate was not valid");
  1565. chan->conn->handshake_state->authenticated = 1;
  1566. {
  1567. const digests_t *id_digests = tor_cert_get_id_digests(id_cert);
  1568. crypto_pk_t *identity_rcvd;
  1569. if (!id_digests)
  1570. ERR("Couldn't compute digests for key in ID cert");
  1571. identity_rcvd = tor_tls_cert_get_key(id_cert);
  1572. if (!identity_rcvd)
  1573. ERR("Internal error: Couldn't get RSA key from ID cert.");
  1574. memcpy(chan->conn->handshake_state->authenticated_peer_id,
  1575. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  1576. channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd,
  1577. chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
  1578. crypto_pk_free(identity_rcvd);
  1579. }
  1580. if (connection_or_client_learned_peer_id(chan->conn,
  1581. chan->conn->handshake_state->authenticated_peer_id) < 0)
  1582. ERR("Problem setting or checking peer id");
  1583. log_info(LD_OR,
  1584. "Got some good certificates from %s:%d: Authenticated it.",
  1585. safe_str(chan->conn->base_.address), chan->conn->base_.port);
  1586. chan->conn->handshake_state->id_cert = id_cert;
  1587. id_cert = NULL;
  1588. if (!public_server_mode(get_options())) {
  1589. /* If we initiated the connection and we are not a public server, we
  1590. * aren't planning to authenticate at all. At this point we know who we
  1591. * are talking to, so we can just send a netinfo now. */
  1592. send_netinfo = 1;
  1593. }
  1594. } else {
  1595. if (! (id_cert && auth_cert))
  1596. ERR("The certs we wanted were missing");
  1597. /* Remember these certificates so we can check an AUTHENTICATE cell */
  1598. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, auth_cert, id_cert, 1))
  1599. ERR("The authentication certificate was not valid");
  1600. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, id_cert, id_cert, 1))
  1601. ERR("The ID certificate was not valid");
  1602. log_info(LD_OR,
  1603. "Got some good certificates from %s:%d: "
  1604. "Waiting for AUTHENTICATE.",
  1605. safe_str(chan->conn->base_.address),
  1606. chan->conn->base_.port);
  1607. /* XXXX check more stuff? */
  1608. chan->conn->handshake_state->id_cert = id_cert;
  1609. chan->conn->handshake_state->auth_cert = auth_cert;
  1610. id_cert = auth_cert = NULL;
  1611. }
  1612. chan->conn->handshake_state->received_certs_cell = 1;
  1613. if (send_netinfo) {
  1614. if (connection_or_send_netinfo(chan->conn) < 0) {
  1615. log_warn(LD_OR, "Couldn't send netinfo cell");
  1616. connection_or_close_for_error(chan->conn, 0);
  1617. goto err;
  1618. }
  1619. }
  1620. err:
  1621. tor_cert_free(id_cert);
  1622. tor_cert_free(link_cert);
  1623. tor_cert_free(auth_cert);
  1624. #undef ERR
  1625. }
  1626. /**
  1627. * Process an AUTH_CHALLENGE cell from a channel_tls_t
  1628. *
  1629. * This function is called to handle an incoming AUTH_CHALLENGE cell on a
  1630. * channel_tls_t; if we weren't supposed to get one (for example, because we're
  1631. * not the originator of the channel), or it's ill-formed, or we aren't doing
  1632. * a v3 handshake, mark the channel. If the cell is well-formed but we don't
  1633. * want to authenticate, just drop it. If the cell is well-formed *and* we
  1634. * want to authenticate, send an AUTHENTICATE cell and then a NETINFO cell.
  1635. */
  1636. static void
  1637. channel_tls_process_auth_challenge_cell(var_cell_t *cell, channel_tls_t *chan)
  1638. {
  1639. int n_types, i, use_type = -1;
  1640. uint8_t *cp;
  1641. tor_assert(cell);
  1642. tor_assert(chan);
  1643. tor_assert(chan->conn);
  1644. #define ERR(s) \
  1645. do { \
  1646. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  1647. "Received a bad AUTH_CHALLENGE cell from %s:%d: %s", \
  1648. safe_str(chan->conn->base_.address), \
  1649. chan->conn->base_.port, (s)); \
  1650. connection_or_close_for_error(chan->conn, 0); \
  1651. return; \
  1652. } while (0)
  1653. if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  1654. ERR("We're not currently doing a v3 handshake");
  1655. if (chan->conn->link_proto < 3)
  1656. ERR("We're not using link protocol >= 3");
  1657. if (!(chan->conn->handshake_state->started_here))
  1658. ERR("We didn't originate this connection");
  1659. if (chan->conn->handshake_state->received_auth_challenge)
  1660. ERR("We already received one");
  1661. if (!(chan->conn->handshake_state->received_certs_cell))
  1662. ERR("We haven't gotten a CERTS cell yet");
  1663. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2)
  1664. ERR("It was too short");
  1665. if (cell->circ_id)
  1666. ERR("It had a nonzero circuit ID");
  1667. n_types = ntohs(get_uint16(cell->payload + OR_AUTH_CHALLENGE_LEN));
  1668. if (cell->payload_len < OR_AUTH_CHALLENGE_LEN + 2 + 2*n_types)
  1669. ERR("It looks truncated");
  1670. /* Now see if there is an authentication type we can use */
  1671. cp = cell->payload+OR_AUTH_CHALLENGE_LEN + 2;
  1672. for (i = 0; i < n_types; ++i, cp += 2) {
  1673. uint16_t authtype = ntohs(get_uint16(cp));
  1674. if (authtype == AUTHTYPE_RSA_SHA256_TLSSECRET)
  1675. use_type = authtype;
  1676. }
  1677. chan->conn->handshake_state->received_auth_challenge = 1;
  1678. if (! public_server_mode(get_options())) {
  1679. /* If we're not a public server then we don't want to authenticate on a
  1680. connection we originated, and we already sent a NETINFO cell when we
  1681. got the CERTS cell. We have nothing more to do. */
  1682. return;
  1683. }
  1684. if (use_type >= 0) {
  1685. log_info(LD_OR,
  1686. "Got an AUTH_CHALLENGE cell from %s:%d: Sending "
  1687. "authentication",
  1688. safe_str(chan->conn->base_.address),
  1689. chan->conn->base_.port);
  1690. if (connection_or_send_authenticate_cell(chan->conn, use_type) < 0) {
  1691. log_warn(LD_OR,
  1692. "Couldn't send authenticate cell");
  1693. connection_or_close_for_error(chan->conn, 0);
  1694. return;
  1695. }
  1696. } else {
  1697. log_info(LD_OR,
  1698. "Got an AUTH_CHALLENGE cell from %s:%d, but we don't "
  1699. "know any of its authentication types. Not authenticating.",
  1700. safe_str(chan->conn->base_.address),
  1701. chan->conn->base_.port);
  1702. }
  1703. if (connection_or_send_netinfo(chan->conn) < 0) {
  1704. log_warn(LD_OR, "Couldn't send netinfo cell");
  1705. connection_or_close_for_error(chan->conn, 0);
  1706. return;
  1707. }
  1708. #undef ERR
  1709. }
  1710. /**
  1711. * Process an AUTHENTICATE cell from a channel_tls_t
  1712. *
  1713. * If it's ill-formed or we weren't supposed to get one or we're not doing a
  1714. * v3 handshake, then mark the connection. If it does not authenticate the
  1715. * other side of the connection successfully (because it isn't signed right,
  1716. * we didn't get a CERTS cell, etc) mark the connection. Otherwise, accept
  1717. * the identity of the router on the other side of the connection.
  1718. */
  1719. static void
  1720. channel_tls_process_authenticate_cell(var_cell_t *cell, channel_tls_t *chan)
  1721. {
  1722. uint8_t expected[V3_AUTH_FIXED_PART_LEN];
  1723. const uint8_t *auth;
  1724. int authlen;
  1725. tor_assert(cell);
  1726. tor_assert(chan);
  1727. tor_assert(chan->conn);
  1728. #define ERR(s) \
  1729. do { \
  1730. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  1731. "Received a bad AUTHENTICATE cell from %s:%d: %s", \
  1732. safe_str(chan->conn->base_.address), \
  1733. chan->conn->base_.port, (s)); \
  1734. connection_or_close_for_error(chan->conn, 0); \
  1735. return; \
  1736. } while (0)
  1737. if (chan->conn->base_.state != OR_CONN_STATE_OR_HANDSHAKING_V3)
  1738. ERR("We're not doing a v3 handshake");
  1739. if (chan->conn->link_proto < 3)
  1740. ERR("We're not using link protocol >= 3");
  1741. if (chan->conn->handshake_state->started_here)
  1742. ERR("We originated this connection");
  1743. if (chan->conn->handshake_state->received_authenticate)
  1744. ERR("We already got one!");
  1745. if (chan->conn->handshake_state->authenticated) {
  1746. /* Should be impossible given other checks */
  1747. ERR("The peer is already authenticated");
  1748. }
  1749. if (!(chan->conn->handshake_state->received_certs_cell))
  1750. ERR("We never got a certs cell");
  1751. if (chan->conn->handshake_state->auth_cert == NULL)
  1752. ERR("We never got an authentication certificate");
  1753. if (chan->conn->handshake_state->id_cert == NULL)
  1754. ERR("We never got an identity certificate");
  1755. if (cell->payload_len < 4)
  1756. ERR("Cell was way too short");
  1757. auth = cell->payload;
  1758. {
  1759. uint16_t type = ntohs(get_uint16(auth));
  1760. uint16_t len = ntohs(get_uint16(auth+2));
  1761. if (4 + len > cell->payload_len)
  1762. ERR("Authenticator was truncated");
  1763. if (type != AUTHTYPE_RSA_SHA256_TLSSECRET)
  1764. ERR("Authenticator type was not recognized");
  1765. auth += 4;
  1766. authlen = len;
  1767. }
  1768. if (authlen < V3_AUTH_BODY_LEN + 1)
  1769. ERR("Authenticator was too short");
  1770. if (connection_or_compute_authenticate_cell_body(
  1771. chan->conn, expected, sizeof(expected), NULL, 1) < 0)
  1772. ERR("Couldn't compute expected AUTHENTICATE cell body");
  1773. if (tor_memneq(expected, auth, sizeof(expected)))
  1774. ERR("Some field in the AUTHENTICATE cell body was not as expected");
  1775. {
  1776. crypto_pk_t *pk = tor_tls_cert_get_key(
  1777. chan->conn->handshake_state->auth_cert);
  1778. char d[DIGEST256_LEN];
  1779. char *signed_data;
  1780. size_t keysize;
  1781. int signed_len;
  1782. if (!pk)
  1783. ERR("Internal error: couldn't get RSA key from AUTH cert.");
  1784. crypto_digest256(d, (char*)auth, V3_AUTH_BODY_LEN, DIGEST_SHA256);
  1785. keysize = crypto_pk_keysize(pk);
  1786. signed_data = tor_malloc(keysize);
  1787. signed_len = crypto_pk_public_checksig(pk, signed_data, keysize,
  1788. (char*)auth + V3_AUTH_BODY_LEN,
  1789. authlen - V3_AUTH_BODY_LEN);
  1790. crypto_pk_free(pk);
  1791. if (signed_len < 0) {
  1792. tor_free(signed_data);
  1793. ERR("Signature wasn't valid");
  1794. }
  1795. if (signed_len < DIGEST256_LEN) {
  1796. tor_free(signed_data);
  1797. ERR("Not enough data was signed");
  1798. }
  1799. /* Note that we deliberately allow *more* than DIGEST256_LEN bytes here,
  1800. * in case they're later used to hold a SHA3 digest or something. */
  1801. if (tor_memneq(signed_data, d, DIGEST256_LEN)) {
  1802. tor_free(signed_data);
  1803. ERR("Signature did not match data to be signed.");
  1804. }
  1805. tor_free(signed_data);
  1806. }
  1807. /* Okay, we are authenticated. */
  1808. chan->conn->handshake_state->received_authenticate = 1;
  1809. chan->conn->handshake_state->authenticated = 1;
  1810. chan->conn->handshake_state->digest_received_data = 0;
  1811. {
  1812. crypto_pk_t *identity_rcvd =
  1813. tor_tls_cert_get_key(chan->conn->handshake_state->id_cert);
  1814. const digests_t *id_digests =
  1815. tor_cert_get_id_digests(chan->conn->handshake_state->id_cert);
  1816. /* This must exist; we checked key type when reading the cert. */
  1817. tor_assert(id_digests);
  1818. memcpy(chan->conn->handshake_state->authenticated_peer_id,
  1819. id_digests->d[DIGEST_SHA1], DIGEST_LEN);
  1820. channel_set_circid_type(TLS_CHAN_TO_BASE(chan), identity_rcvd,
  1821. chan->conn->link_proto < MIN_LINK_PROTO_FOR_WIDE_CIRC_IDS);
  1822. crypto_pk_free(identity_rcvd);
  1823. connection_or_init_conn_from_address(chan->conn,
  1824. &(chan->conn->base_.addr),
  1825. chan->conn->base_.port,
  1826. (const char*)(chan->conn->handshake_state->
  1827. authenticated_peer_id),
  1828. 0);
  1829. log_info(LD_OR,
  1830. "Got an AUTHENTICATE cell from %s:%d: Looks good.",
  1831. safe_str(chan->conn->base_.address),
  1832. chan->conn->base_.port);
  1833. }
  1834. #undef ERR
  1835. }