channeltls.c 61 KB

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