channeltls.c 67 KB

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