channeltls.c 59 KB

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