channeltls.c 63 KB

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