channeltls.c 61 KB

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