channeltls.c 61 KB

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