channeltls.c 59 KB

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