channeltls.c 65 KB

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