channeltls.c 62 KB

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