safe_connection.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. #include "core/or/safe_connection.h"
  2. #include "app/config/config.h"
  3. #include "lib/net/buffers_net.h"
  4. #include "lib/tls/tortls.h"
  5. #include "lib/tls/buffers_tls.h"
  6. event_label_t safe_or_conn_tcp_connecting_ev = EVENT_LABEL_UNSET;
  7. event_label_t safe_or_conn_tls_handshaking_ev = EVENT_LABEL_UNSET;
  8. event_label_t safe_or_conn_link_handshaking_ev = EVENT_LABEL_UNSET;
  9. event_label_t safe_or_conn_open_ev = EVENT_LABEL_UNSET;
  10. event_label_t safe_or_conn_closed_ev = EVENT_LABEL_UNSET;
  11. event_label_t safe_or_conn_has_buffered_data_ev = EVENT_LABEL_UNSET;
  12. static void
  13. safe_connection_refresh_events(safe_connection_t *safe_conn);
  14. static void
  15. safe_or_connection_refresh_bucket_rw_states(safe_or_connection_t *safe_or_conn);
  16. static tor_error_t
  17. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  18. or_conn_state_t new_state);
  19. static bool
  20. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn);
  21. static bool
  22. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn);
  23. static void
  24. safe_or_connection_read_cb(safe_connection_t *safe_conn);
  25. static void
  26. safe_or_connection_write_cb(safe_connection_t *safe_conn);
  27. static void
  28. safe_or_connection_socket_added_cb(safe_connection_t *safe_conn);
  29. static void
  30. safe_or_connection_outbuf_modified_cb(safe_connection_t *safe_conn);
  31. /********************************************************/
  32. safe_or_connection_t *
  33. TO_SAFE_OR_CONN(safe_connection_t *safe_conn)
  34. {
  35. tor_assert(safe_conn != NULL);
  36. tor_assert(safe_conn->magic == SAFE_OR_CONN_MAGIC);
  37. return DOWNCAST(safe_or_connection_t, safe_conn);
  38. }
  39. void
  40. safe_or_conn_register_events(event_registry_t *registry)
  41. {
  42. tor_assert(safe_or_conn_tcp_connecting_ev == EVENT_LABEL_UNSET);
  43. tor_assert(safe_or_conn_tls_handshaking_ev == EVENT_LABEL_UNSET);
  44. tor_assert(safe_or_conn_link_handshaking_ev == EVENT_LABEL_UNSET);
  45. tor_assert(safe_or_conn_open_ev == EVENT_LABEL_UNSET);
  46. tor_assert(safe_or_conn_closed_ev == EVENT_LABEL_UNSET);
  47. tor_assert(safe_or_conn_has_buffered_data_ev == EVENT_LABEL_UNSET);
  48. safe_or_conn_tcp_connecting_ev = \
  49. event_registry_register_event(registry, "OR Connection Connecting");
  50. safe_or_conn_tls_handshaking_ev = \
  51. event_registry_register_event(registry, "Starting OR TLS Handshake");
  52. safe_or_conn_link_handshaking_ev = \
  53. event_registry_register_event(registry, "Starting OR Link Handshake");
  54. safe_or_conn_open_ev = \
  55. event_registry_register_event(registry, "OR Connection Open");
  56. safe_or_conn_closed_ev = \
  57. event_registry_register_event(registry, "OR Connection Closed");
  58. safe_or_conn_has_buffered_data_ev = \
  59. event_registry_register_event(registry, "OR Connection Has Data In Buffer");
  60. }
  61. /********************************************************/
  62. void
  63. link_handshaking_ev_free(void *ptr)
  64. {
  65. // we don't need to free the certs since we passed the ownership
  66. tor_free(ptr);
  67. }
  68. /********************************************************/
  69. static void
  70. socket_rw_state_init(socket_rw_state_t *rw_state,
  71. bool initial_state)
  72. {
  73. tor_assert(rw_state != NULL);
  74. rw_state->state = initial_state;
  75. }
  76. static bool
  77. socket_rw_state_get(socket_rw_state_t *rw_state)
  78. {
  79. tor_assert(rw_state != NULL);
  80. return rw_state->state;
  81. }
  82. static void
  83. socket_rw_state_set(socket_rw_state_t *rw_state,
  84. bool new_state,
  85. safe_connection_t *safe_conn)
  86. {
  87. tor_assert(rw_state != NULL);
  88. tor_assert(safe_conn != NULL);
  89. if (new_state != rw_state->state) {
  90. rw_state->state = new_state;
  91. safe_connection_refresh_events(safe_conn);
  92. }
  93. }
  94. /********************************************************/
  95. void
  96. safe_connection_init(safe_connection_t *safe_conn, uint32_t type_magic,
  97. bool (*is_read_wanted)(safe_connection_t *),
  98. bool (*is_write_wanted)(safe_connection_t *),
  99. void (*read_cb)(safe_connection_t *),
  100. void (*write_cb)(safe_connection_t *),
  101. void (*socket_added_cb)(safe_connection_t *),
  102. void (*inbuf_modified_cb)(safe_connection_t *),
  103. void (*outbuf_modified_cb)(safe_connection_t *),
  104. bool requires_buffers, bool linked)
  105. {
  106. tor_assert(safe_conn != NULL);
  107. tor_assert(is_read_wanted != NULL);
  108. tor_assert(is_write_wanted != NULL);
  109. tor_assert(read_cb != NULL);
  110. tor_assert(write_cb != NULL);
  111. memset(safe_conn, 0, sizeof(*safe_conn));
  112. safe_conn->magic = type_magic;
  113. safe_conn->socket = TOR_INVALID_SOCKET;
  114. safe_conn->linked = linked;
  115. safe_conn->event_source = event_source_new();
  116. socket_rw_state_init(&safe_conn->read_allowed, true);
  117. socket_rw_state_init(&safe_conn->write_allowed, true);
  118. tor_mutex_init(&safe_conn->lock);
  119. safe_conn->is_read_wanted = is_read_wanted;
  120. safe_conn->is_write_wanted = is_write_wanted;
  121. safe_conn->read_cb = read_cb;
  122. safe_conn->write_cb = write_cb;
  123. safe_conn->socket_added_cb = socket_added_cb;
  124. safe_conn->inbuf_modified_cb = inbuf_modified_cb;
  125. safe_conn->outbuf_modified_cb = outbuf_modified_cb;
  126. if (requires_buffers) {
  127. safe_conn->inbuf = buf_new();
  128. safe_conn->outbuf = buf_new();
  129. }
  130. }
  131. void
  132. safe_connection_set_socket(safe_connection_t *safe_conn, tor_socket_t socket)
  133. {
  134. tor_assert(safe_conn != NULL);
  135. tor_mutex_acquire(&safe_conn->lock);
  136. tor_assert(!safe_conn->linked);
  137. tor_assert(SOCKET_OK(socket));
  138. if (safe_conn->socket != TOR_INVALID_SOCKET) {
  139. log_warn(LD_BUG, "We're overwriting a previous socket");
  140. }
  141. safe_conn->socket = socket;
  142. if (safe_conn->socket_added_cb != NULL) {
  143. safe_conn->socket_added_cb(safe_conn);
  144. }
  145. tor_mutex_release(&safe_conn->lock);
  146. }
  147. static void
  148. safe_connection_read_cb(evutil_socket_t ev_sock, short fd, void *void_safe_conn)
  149. {
  150. (void)ev_sock;
  151. (void)fd;
  152. safe_connection_t *safe_conn = void_safe_conn;
  153. tor_assert(safe_conn != NULL);
  154. tor_mutex_acquire(&safe_conn->lock);
  155. tor_assert(safe_conn->read_cb != NULL);
  156. //tor_assert(safe_conn->read_event != NULL);
  157. // NOTE: the below requires obtaining a lock on the event base, which adds
  158. // unnecessary slowness
  159. // XX: Is the above true?
  160. //if (!event_pending(safe_conn->read_event, EV_READ, NULL)) {
  161. // // another thread may have disabled this event between when the
  162. // // callback started, and when we acquired the lock above
  163. // return;
  164. //}
  165. //if (!safe_conn->read_allowed || !safe_conn->read_wanted) {
  166. // // we shouldn't be reading
  167. // return;
  168. //}
  169. safe_conn->read_cb(safe_conn);
  170. tor_mutex_release(&safe_conn->lock);
  171. }
  172. static void
  173. safe_connection_write_cb(evutil_socket_t ev_sock, short fd, void *void_safe_conn)
  174. {
  175. (void)ev_sock;
  176. (void)fd;
  177. safe_connection_t *safe_conn = void_safe_conn;
  178. tor_assert(safe_conn != NULL);
  179. tor_mutex_acquire(&safe_conn->lock);
  180. tor_assert(safe_conn->write_cb != NULL);
  181. //tor_assert(safe_conn->write_event != NULL);
  182. // NOTE: the below requires obtaining a lock on the event base, which adds
  183. // unnecessary slowness
  184. // XX: Is the above true?
  185. //if (!event_pending(safe_conn->write_event, EV_WRITE, NULL)) {
  186. // // another thread may have disabled this event between when the
  187. // // callback started, and when we acquired the lock above
  188. // return;
  189. //}
  190. //if (!safe_conn->write_allowed || !safe_conn->write_wanted) {
  191. // // we shouldn't be writing
  192. // return;
  193. //}
  194. safe_conn->write_cb(safe_conn);
  195. tor_mutex_release(&safe_conn->lock);
  196. }
  197. void
  198. safe_connection_subscribe(safe_connection_t *safe_conn,
  199. event_listener_t *listener, event_label_t label,
  200. bool send_full_event)
  201. {
  202. tor_assert(safe_conn != NULL);
  203. tor_mutex_acquire(&safe_conn->lock);
  204. event_source_subscribe(safe_conn->event_source, listener, label,
  205. send_full_event);
  206. tor_mutex_release(&safe_conn->lock);
  207. }
  208. void
  209. safe_connection_unsubscribe_all(safe_connection_t *safe_conn,
  210. event_listener_t *listener)
  211. {
  212. tor_assert(safe_conn != NULL);
  213. tor_mutex_acquire(&safe_conn->lock);
  214. event_source_unsubscribe_all(safe_conn->event_source, listener);
  215. tor_mutex_release(&safe_conn->lock);
  216. }
  217. void
  218. safe_connection_unregister_events(safe_connection_t *safe_conn)
  219. {
  220. tor_assert(safe_conn != NULL);
  221. tor_mutex_acquire(&safe_conn->lock);
  222. if (safe_conn->read_event != NULL) {
  223. tor_event_free(safe_conn->read_event);
  224. }
  225. if (safe_conn->write_event != NULL) {
  226. tor_event_free(safe_conn->write_event);
  227. }
  228. tor_mutex_release(&safe_conn->lock);
  229. }
  230. tor_error_t
  231. safe_connection_register_events(safe_connection_t *safe_conn,
  232. struct event_base *event_base)
  233. {
  234. tor_assert(safe_conn != NULL);
  235. tor_mutex_acquire(&safe_conn->lock);
  236. tor_assert(safe_conn->read_cb != NULL);
  237. tor_assert(safe_conn->write_cb != NULL);
  238. tor_assert(safe_conn->linked != SOCKET_OK(safe_conn->socket));
  239. // is either linked or has a socket, but not both (or neither)
  240. safe_connection_unregister_events(safe_conn);
  241. safe_conn->read_event = tor_event_new(event_base, safe_conn->socket,
  242. EV_READ|EV_PERSIST,
  243. safe_connection_read_cb, safe_conn);
  244. safe_conn->write_event = tor_event_new(event_base, safe_conn->socket,
  245. EV_WRITE|EV_PERSIST,
  246. safe_connection_write_cb, safe_conn);
  247. if (safe_conn->read_event == NULL || safe_conn->write_event == NULL) {
  248. log_warn(LD_BUG, "Could not set events for %d", (int)safe_conn->socket);
  249. safe_connection_unregister_events(safe_conn);
  250. tor_mutex_release(&safe_conn->lock);
  251. return E_ERROR;
  252. }
  253. safe_connection_refresh_events(safe_conn);
  254. tor_mutex_release(&safe_conn->lock);
  255. return E_SUCCESS;
  256. }
  257. static void
  258. safe_connection_refresh_events(safe_connection_t *safe_conn)
  259. {
  260. tor_assert(safe_conn != NULL);
  261. tor_mutex_acquire(&safe_conn->lock);
  262. tor_assert(safe_conn->is_read_wanted != NULL);
  263. tor_assert(safe_conn->is_write_wanted != NULL);
  264. if (safe_conn->read_event != NULL) {
  265. if (socket_rw_state_get(&safe_conn->read_allowed) &&
  266. safe_conn->is_read_wanted(safe_conn)) {
  267. event_add(safe_conn->read_event, NULL);
  268. } else {
  269. event_del(safe_conn->read_event);
  270. }
  271. }
  272. if (safe_conn->write_event != NULL) {
  273. if (socket_rw_state_get(&safe_conn->write_allowed) &&
  274. safe_conn->is_write_wanted(safe_conn)) {
  275. event_add(safe_conn->write_event, NULL);
  276. } else {
  277. event_del(safe_conn->write_event);
  278. }
  279. }
  280. tor_mutex_release(&safe_conn->lock);
  281. }
  282. void
  283. safe_connection_set_read_permission(safe_connection_t *safe_conn,
  284. bool read_allowed)
  285. {
  286. tor_assert(safe_conn != NULL);
  287. tor_mutex_acquire(&safe_conn->lock);
  288. socket_rw_state_set(&safe_conn->read_allowed, read_allowed, safe_conn);
  289. tor_mutex_release(&safe_conn->lock);
  290. }
  291. void
  292. safe_connection_set_write_permission(safe_connection_t *safe_conn,
  293. bool write_allowed)
  294. {
  295. tor_assert(safe_conn != NULL);
  296. tor_mutex_acquire(&safe_conn->lock);
  297. socket_rw_state_set(&safe_conn->write_allowed, write_allowed, safe_conn);
  298. tor_mutex_release(&safe_conn->lock);
  299. }
  300. void
  301. safe_connection_inbuf_modified(safe_connection_t *safe_conn)
  302. {
  303. tor_assert(safe_conn != NULL);
  304. tor_mutex_acquire(&safe_conn->lock);
  305. if (safe_conn->inbuf_modified_cb != NULL) {
  306. safe_conn->inbuf_modified_cb(safe_conn);
  307. }
  308. tor_mutex_release(&safe_conn->lock);
  309. }
  310. void
  311. safe_connection_outbuf_modified(safe_connection_t *safe_conn)
  312. {
  313. tor_assert(safe_conn != NULL);
  314. tor_mutex_acquire(&safe_conn->lock);
  315. if (safe_conn->outbuf_modified_cb != NULL) {
  316. safe_conn->outbuf_modified_cb(safe_conn);
  317. }
  318. tor_mutex_release(&safe_conn->lock);
  319. }
  320. //void
  321. //safe_connection_use_inbuf(safe_connection_t *safe_conn,
  322. // int (*f)(struct buf_t *, void *, void **),
  323. // void *data,
  324. // void **ret_val)
  325. //{
  326. // tor_assert(safe_conn != NULL);
  327. // tor_assert(f != NULL);
  328. // tor_mutex_acquire(&safe_conn->lock);
  329. //
  330. // int rv = f(safe_conn->inbuf, data, ret_val);
  331. //
  332. // tor_mutex_release(&safe_conn->lock);
  333. //
  334. // return rv;
  335. //}
  336. /********************************************************/
  337. safe_or_connection_t *
  338. safe_or_connection_new(bool requires_buffers, bool is_outgoing,
  339. const char *remote_address_str)
  340. {
  341. safe_or_connection_t *safe_or_conn = \
  342. tor_malloc_zero(sizeof(safe_or_connection_t));
  343. safe_connection_init(TO_SAFE_CONN(safe_or_conn),
  344. SAFE_OR_CONN_MAGIC,
  345. safe_or_connection_is_read_wanted,
  346. safe_or_connection_is_write_wanted,
  347. safe_or_connection_read_cb,
  348. safe_or_connection_write_cb,
  349. safe_or_connection_socket_added_cb,
  350. NULL,
  351. safe_or_connection_outbuf_modified_cb,
  352. requires_buffers, false);
  353. token_bucket_rw_init(&safe_or_conn->bucket, 1, 1, time(NULL));
  354. safe_or_conn->is_outgoing = is_outgoing;
  355. if (remote_address_str != NULL) {
  356. safe_or_conn->remote_address_str = \
  357. tor_strdup(escaped_safe_str(remote_address_str));
  358. // the function 'escaped_safe_str' must be run in the main thread
  359. } else {
  360. safe_or_conn->remote_address_str = NULL;
  361. log_warn(LD_OR, "No remote address string was provided");
  362. }
  363. // these states should be set by 'safe_or_connection_update_state()'
  364. socket_rw_state_init(&safe_or_conn->tor_read_wanted, false);
  365. socket_rw_state_init(&safe_or_conn->tor_write_wanted, false);
  366. socket_rw_state_init(&safe_or_conn->tls_read_wanted, false);
  367. socket_rw_state_init(&safe_or_conn->tls_write_wanted, false);
  368. socket_rw_state_init(&safe_or_conn->bucket_read_allowed, false);
  369. socket_rw_state_init(&safe_or_conn->bucket_write_allowed, false);
  370. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  371. tor_assert(safe_or_connection_update_state(safe_or_conn,
  372. SAFE_OR_CONN_STATE_NO_SOCKET) == E_SUCCESS);
  373. return safe_or_conn;
  374. }
  375. static void
  376. safe_or_connection_socket_added_cb(safe_connection_t *safe_conn)
  377. {
  378. tor_assert(safe_conn != NULL);
  379. tor_assert(safe_or_connection_update_state(TO_SAFE_OR_CONN(safe_conn),
  380. SAFE_OR_CONN_STATE_TCP_CONNECTING) == E_SUCCESS);
  381. // it might already be connected, but it should be fine to transition
  382. // through this state first
  383. }
  384. static void
  385. safe_or_connection_outbuf_modified_cb(safe_connection_t *safe_conn)
  386. {
  387. tor_assert(safe_conn != NULL);
  388. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  389. if (safe_or_conn->state == SAFE_OR_CONN_STATE_LINK_HANDSHAKING ||
  390. safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN) {
  391. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) > 0) {
  392. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  393. TO_SAFE_CONN(safe_or_conn));
  394. }
  395. } else {
  396. log_warn(LD_OR, "The outbuf was modified when in a state where it "
  397. "shouldn't be modified (state %d)", safe_or_conn->state);
  398. }
  399. }
  400. static void
  401. safe_or_connection_refresh_bucket_rw_states(safe_or_connection_t *safe_or_conn)
  402. {
  403. if (token_bucket_rw_get_read(&safe_or_conn->bucket) > 0) {
  404. // token bucket is not empty, so we can read now
  405. socket_rw_state_set(&safe_or_conn->bucket_read_allowed, true,
  406. TO_SAFE_CONN(safe_or_conn));
  407. log_debug(LD_OR, "Token bucket for %p read is non-empty", safe_or_conn);
  408. } else {
  409. // token bucket is empty, so can't read now
  410. socket_rw_state_set(&safe_or_conn->bucket_read_allowed, false,
  411. TO_SAFE_CONN(safe_or_conn));
  412. log_debug(LD_OR, "Token bucket for %p read is empty", safe_or_conn);
  413. }
  414. if (token_bucket_rw_get_write(&safe_or_conn->bucket) > 0) {
  415. // token bucket is not empty, so we can write now
  416. socket_rw_state_set(&safe_or_conn->bucket_write_allowed, true,
  417. TO_SAFE_CONN(safe_or_conn));
  418. log_debug(LD_OR, "Token bucket for %p write is non-empty", safe_or_conn);
  419. } else {
  420. // token bucket is empty, so can't write now
  421. socket_rw_state_set(&safe_or_conn->bucket_write_allowed, false,
  422. TO_SAFE_CONN(safe_or_conn));
  423. log_debug(LD_OR, "Token bucket for %p write is empty", safe_or_conn);
  424. }
  425. }
  426. // TODO: we should get rid of this at some point
  427. void
  428. safe_or_connection_get_tls_desc(safe_or_connection_t *safe_or_conn,
  429. char *buf, size_t buf_size)
  430. {
  431. tor_assert(safe_or_conn != NULL);
  432. tor_assert(buf != NULL);
  433. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  434. tor_assert(safe_or_conn->tls != NULL);
  435. tor_tls_get_state_description(safe_or_conn->tls, buf, buf_size);
  436. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  437. }
  438. int
  439. safe_or_connection_tls_secrets(safe_or_connection_t *safe_or_conn,
  440. uint8_t *secrets_out)
  441. {
  442. tor_assert(safe_or_conn != NULL);
  443. tor_assert(secrets_out != NULL);
  444. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  445. tor_assert(safe_or_conn->tls != NULL);
  446. int rv = tor_tls_get_tlssecrets(safe_or_conn->tls, secrets_out);
  447. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  448. return rv;
  449. }
  450. int
  451. safe_or_connection_key_material(safe_or_connection_t *safe_or_conn,
  452. uint8_t *secrets_out,
  453. const uint8_t *context,
  454. size_t context_len, const char *label)
  455. {
  456. tor_assert(safe_or_conn != NULL);
  457. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  458. tor_assert(safe_or_conn->tls != NULL);
  459. int rv = tor_tls_export_key_material(safe_or_conn->tls, secrets_out,
  460. context, context_len, label);
  461. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  462. return rv;
  463. }
  464. void
  465. safe_or_connection_refill_buckets(safe_or_connection_t *safe_or_conn,
  466. uint32_t now_ts)
  467. {
  468. tor_assert(safe_or_conn != NULL);
  469. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  470. tor_assert(&safe_or_conn->bucket != NULL);
  471. token_bucket_rw_refill(&safe_or_conn->bucket, now_ts);
  472. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  473. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  474. }
  475. // TODO: this might be better implemented as a message so that we don't need
  476. // to wait for the lock (but would require us to add a listener to the
  477. // safe conn
  478. void
  479. safe_or_connection_adjust_buckets(safe_or_connection_t *safe_or_conn,
  480. uint32_t rate, uint32_t burst,
  481. bool reset, uint32_t now_ts)
  482. {
  483. tor_assert(safe_or_conn != NULL);
  484. tor_mutex_acquire(&TO_SAFE_CONN(safe_or_conn)->lock);
  485. tor_assert(&safe_or_conn->bucket != NULL);
  486. token_bucket_rw_adjust(&safe_or_conn->bucket, rate, burst);
  487. if (reset) {
  488. token_bucket_rw_reset(&safe_or_conn->bucket, now_ts);
  489. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  490. }
  491. tor_mutex_release(&TO_SAFE_CONN(safe_or_conn)->lock);
  492. }
  493. static void
  494. safe_or_connection_decrement_buckets(safe_or_connection_t *safe_or_conn,
  495. size_t num_read, size_t num_written,
  496. bool use_conn_buckets)
  497. {
  498. if (use_conn_buckets) {
  499. token_bucket_rw_dec(&safe_or_conn->bucket, num_read, num_written);
  500. }
  501. safe_or_connection_refresh_bucket_rw_states(safe_or_conn);
  502. }
  503. static size_t
  504. safe_or_connection_max_bytes_can_read(safe_or_connection_t *safe_or_conn,
  505. bool use_conn_buckets)
  506. {
  507. // this function may become more complicated if we add support for global
  508. // buckets in the future
  509. // note: that would be a bad way to do it, since instead we should borrow
  510. // some space from the global bucket, and then commit it once the read
  511. // is actually finished
  512. if (use_conn_buckets) {
  513. return token_bucket_rw_get_read(&safe_or_conn->bucket);
  514. } else {
  515. return SIZE_MAX;
  516. }
  517. }
  518. static size_t
  519. safe_or_connection_max_bytes_can_write(safe_or_connection_t *safe_or_conn,
  520. bool use_conn_buckets)
  521. {
  522. // this function may become more complicated if we add support for global
  523. // buckets in the future
  524. // note: that would be a bad way to do it, since instead we should borrow
  525. // some space from the global bucket, and then commit it once the write
  526. // is actually finished
  527. if (use_conn_buckets) {
  528. return token_bucket_rw_get_write(&safe_or_conn->bucket);
  529. } else {
  530. return SIZE_MAX;
  531. }
  532. }
  533. static bool
  534. safe_or_connection_is_read_wanted(safe_connection_t *safe_conn)
  535. {
  536. tor_assert(safe_conn != NULL);
  537. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  538. return socket_rw_state_get(&safe_or_conn->tls_read_wanted) ||
  539. (socket_rw_state_get(&safe_or_conn->tor_read_wanted) &&
  540. socket_rw_state_get(&safe_or_conn->bucket_read_allowed));
  541. }
  542. static bool
  543. safe_or_connection_is_write_wanted(safe_connection_t *safe_conn)
  544. {
  545. tor_assert(safe_conn != NULL);
  546. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  547. return socket_rw_state_get(&safe_or_conn->tls_write_wanted) ||
  548. (socket_rw_state_get(&safe_or_conn->tor_write_wanted) &&
  549. socket_rw_state_get(&safe_or_conn->bucket_write_allowed));
  550. }
  551. static tor_error_t
  552. safe_or_connection_update_state(safe_or_connection_t *safe_or_conn,
  553. or_conn_state_t new_state)
  554. {
  555. if (new_state == safe_or_conn->state) {
  556. log_warn(LD_OR, "Trying to change to the current state (or_conn_state_t) "
  557. "of %d", new_state);
  558. }
  559. event_data_t null_data = { .ptr = NULL };
  560. // this is used by several cases below
  561. switch (new_state) {
  562. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  563. tor_assert_unreached();
  564. break;
  565. case SAFE_OR_CONN_STATE_NO_SOCKET:
  566. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  567. TO_SAFE_CONN(safe_or_conn));
  568. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  569. TO_SAFE_CONN(safe_or_conn));
  570. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  571. TO_SAFE_CONN(safe_or_conn));
  572. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  573. TO_SAFE_CONN(safe_or_conn));
  574. break;
  575. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  576. // the socket was EINPROGRESS, so wait for the socket to become
  577. // writable
  578. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  579. TO_SAFE_CONN(safe_or_conn));
  580. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  581. TO_SAFE_CONN(safe_or_conn));
  582. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  583. TO_SAFE_CONN(safe_or_conn));
  584. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  585. TO_SAFE_CONN(safe_or_conn));
  586. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  587. safe_or_conn_tcp_connecting_ev,
  588. null_data, NULL);
  589. break;
  590. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  591. log_warn(LD_OR, "Relay connection proxy handshake state has not yet "
  592. "been implemented");
  593. tor_assert(0);
  594. break;
  595. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  596. {
  597. // begin the handshake when either the socket is readable or
  598. // writable
  599. if (safe_or_conn->tls != NULL) {
  600. log_warn(LD_OR, "safe_or_conn->tls should not be set");
  601. return E_ERROR;
  602. }
  603. bool is_receiving = !safe_or_conn->is_outgoing;
  604. if (TO_SAFE_CONN(safe_or_conn)->socket == TOR_INVALID_SOCKET) {
  605. log_warn(LD_OR, "No socket was set yet");
  606. return E_ERROR;
  607. }
  608. safe_or_conn->tls = tor_tls_new(TO_SAFE_CONN(safe_or_conn)->socket,
  609. is_receiving);
  610. if (safe_or_conn->tls == NULL) {
  611. log_warn(LD_OR, "Could not create a new tor TLS object");
  612. return E_ERROR;
  613. }
  614. if (safe_or_conn->remote_address_str != NULL) {
  615. tor_tls_set_logged_address(safe_or_conn->tls,
  616. safe_or_conn->remote_address_str);
  617. }
  618. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  619. TO_SAFE_CONN(safe_or_conn));
  620. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  621. TO_SAFE_CONN(safe_or_conn));
  622. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  623. TO_SAFE_CONN(safe_or_conn));
  624. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  625. TO_SAFE_CONN(safe_or_conn));
  626. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  627. safe_or_conn_tls_handshaking_ev,
  628. null_data, NULL);
  629. break;
  630. }
  631. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  632. {
  633. if (safe_or_conn->tls == NULL) {
  634. log_warn(LD_OR, "safe_or_conn->tls was not set");
  635. return E_ERROR;
  636. }
  637. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  638. TO_SAFE_CONN(safe_or_conn));
  639. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  640. TO_SAFE_CONN(safe_or_conn));
  641. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  642. TO_SAFE_CONN(safe_or_conn));
  643. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  644. TO_SAFE_CONN(safe_or_conn));
  645. link_handshaking_ev_data *handshake_data = \
  646. tor_malloc_zero(sizeof(link_handshaking_ev_data));
  647. handshake_data->tls_own_cert = tor_tls_get_own_cert(safe_or_conn->tls);
  648. handshake_data->tls_peer_cert = tor_tls_get_peer_cert(safe_or_conn->tls);
  649. event_data_t ev_data = { .ptr = handshake_data };
  650. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  651. safe_or_conn_link_handshaking_ev,
  652. ev_data, link_handshaking_ev_free);
  653. break;
  654. }
  655. case SAFE_OR_CONN_STATE_OPEN:
  656. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  657. TO_SAFE_CONN(safe_or_conn));
  658. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  659. TO_SAFE_CONN(safe_or_conn));
  660. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  661. TO_SAFE_CONN(safe_or_conn));
  662. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  663. TO_SAFE_CONN(safe_or_conn));
  664. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  665. safe_or_conn_open_ev, null_data, NULL);
  666. break;
  667. case SAFE_OR_CONN_STATE_CLOSED:
  668. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  669. TO_SAFE_CONN(safe_or_conn));
  670. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  671. TO_SAFE_CONN(safe_or_conn));
  672. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  673. TO_SAFE_CONN(safe_or_conn));
  674. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  675. TO_SAFE_CONN(safe_or_conn));
  676. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  677. safe_or_conn_closed_ev, null_data, NULL);
  678. break;
  679. default:
  680. log_warn(LD_OR, "Unexpected state");
  681. tor_assert(0);
  682. break;
  683. }
  684. log_warn(LD_OR, "Safe OR conn changed from state %d to state %d",
  685. safe_or_conn->state, new_state);
  686. // TODO: change back to debug ^^
  687. safe_or_conn->state = new_state;
  688. return E_SUCCESS;
  689. }
  690. static tor_error_t
  691. safe_or_connection_check_tcp_connection(safe_or_connection_t *safe_or_conn)
  692. {
  693. tor_assert(safe_or_conn != NULL);
  694. int e;
  695. socklen_t len = (socklen_t)sizeof(e);
  696. if (getsockopt(TO_SAFE_CONN(safe_or_conn)->socket, SOL_SOCKET, SO_ERROR,
  697. (void *)&e, &len) < 0) {
  698. log_warn(LD_BUG, "getsockopt() syscall failed");
  699. return E_ERROR;
  700. }
  701. if (e != 0) {
  702. // some sort of error, but maybe just inprogress still
  703. if (!ERRNO_IS_CONN_EINPROGRESS(e)) {
  704. log_info(LD_NET, "In-progress connect failed. Removing. (%s)",
  705. tor_socket_strerror(e));
  706. return E_ERROR;
  707. } else {
  708. // no change, see if next time is better
  709. return E_SUCCESS;
  710. }
  711. }
  712. // there was no error
  713. return safe_or_connection_update_state(safe_or_conn,
  714. SAFE_OR_CONN_STATE_TLS_HANDSHAKING);
  715. }
  716. static int
  717. safe_or_connection_read_tls(safe_or_connection_t *safe_or_conn,
  718. size_t suggested_bytes_to_read,
  719. size_t *total_bytes_read)
  720. {
  721. tor_assert(safe_or_conn != NULL);
  722. tor_assert(suggested_bytes_to_read > 0);
  723. *total_bytes_read = 0;
  724. {
  725. size_t bytes_read = 0;
  726. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  727. safe_or_conn->tls,
  728. suggested_bytes_to_read,
  729. &bytes_read);
  730. *total_bytes_read += bytes_read;
  731. if (tls_rv != TOR_TLS_DONE) {
  732. return tls_rv;
  733. }
  734. }
  735. int pending_bytes_to_read = tor_tls_get_pending_bytes(safe_or_conn->tls);
  736. if (pending_bytes_to_read > 0) {
  737. size_t bytes_read = 0;
  738. int tls_rv = buf_read_from_tls(TO_SAFE_CONN(safe_or_conn)->inbuf,
  739. safe_or_conn->tls,
  740. pending_bytes_to_read,
  741. &bytes_read);
  742. if (PREDICT_LIKELY(SIZE_MAX-(*total_bytes_read) > bytes_read)) {
  743. *total_bytes_read += bytes_read;
  744. } else {
  745. *total_bytes_read = SIZE_MAX;
  746. }
  747. tor_assert(tls_rv != TOR_TLS_WANTREAD && tls_rv != TOR_TLS_WANTWRITE);
  748. // we don't expect either of these when reading pending bytes
  749. if (tls_rv != TOR_TLS_DONE) {
  750. return tls_rv;
  751. }
  752. }
  753. return TOR_TLS_DONE;
  754. }
  755. static int
  756. safe_or_connection_write_tls(safe_or_connection_t *safe_or_conn,
  757. size_t max_bytes_to_write,
  758. size_t *total_bytes_written)
  759. {
  760. tor_assert(safe_or_conn != NULL);
  761. tor_assert(max_bytes_to_write > 0);
  762. *total_bytes_written = 0;
  763. size_t bytes_written = 0;
  764. int tls_rv = buf_flush_to_tls(TO_SAFE_CONN(safe_or_conn)->outbuf,
  765. safe_or_conn->tls,
  766. max_bytes_to_write,
  767. &bytes_written);
  768. *total_bytes_written += bytes_written;
  769. return tls_rv;
  770. }
  771. // this function will be needed when proxies are supported
  772. /*
  773. static tor_error_t
  774. safe_or_connection_read_plaintext(safe_or_connection_t *safe_or_conn)
  775. {
  776. tor_assert(safe_or_conn != NULL);
  777. uint32_t coarse_time = monotime_coarse_get_stamp();
  778. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  779. size_t bytes_to_read = safe_or_connection_max_bytes_can_read(safe_or_conn);
  780. if (bytes_to_read == 0) {
  781. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  782. return E_SUCCESS;
  783. }
  784. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  785. size_t bytes_read = 0;
  786. int reached_eof = 0;
  787. int socket_error = 0;
  788. // STEVE: if reusing this with control connections, then need to wrap
  789. // with 'CONN_LOG_PROTECT' (see connection.c,
  790. // !connection_speaks_cells, !conn->linked_conn. )
  791. int rv = buf_read_from_socket(TO_SAFE_CONN(safe_or_conn)->inbuf,
  792. TO_SAFE_CONN(safe_or_conn)->socket,
  793. bytes_to_read, &reached_eof,
  794. &socket_error);
  795. if (rv < 0) {
  796. log_debug(LD_NET, "OR plaintext connection closed on read error.");
  797. // TODO: need to send the socket_error back to the main thread
  798. return E_ERROR;
  799. } else if(rv == 0 && reached_eof != 0) {
  800. // close the connection normally
  801. log_debug(LD_NET, "OR plaintext connection closed on read eof.");
  802. // return an error so that the calling function will close it
  803. return E_ERROR;
  804. } else {
  805. bytes_read = rv;
  806. }
  807. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  808. tor_assert(bytes_read == \
  809. buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size);
  810. } else {
  811. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  812. ">= SIZE_MAX",
  813. bytes_read);
  814. }
  815. log_debug(LD_NET, "OR plaintext read of %ld", (long)bytes_read);
  816. safe_or_connection_decrement_buckets(safe_or_conn, bytes_read, 0);
  817. return E_SUCCESS;
  818. }
  819. */
  820. static tor_error_t
  821. safe_or_connection_read_encrypted(safe_or_connection_t *safe_or_conn,
  822. bool use_conn_buckets)
  823. {
  824. tor_assert(safe_or_conn != NULL);
  825. uint32_t coarse_time = monotime_coarse_get_stamp();
  826. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  827. size_t suggested_bytes_to_read = \
  828. safe_or_connection_max_bytes_can_read(safe_or_conn, use_conn_buckets);
  829. // we may read slightly more than this due to pending TLS bytes
  830. log_warn(LD_NET, "suggested_bytes_to_read: %ld", suggested_bytes_to_read);
  831. // TODO: remove this ^^
  832. if (suggested_bytes_to_read == 0) {
  833. log_debug(LD_NET, "Read callback running, but not supposed to read bytes.");
  834. return E_SUCCESS;
  835. }
  836. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf);
  837. size_t bytes_read = 0;
  838. int tls_rv = safe_or_connection_read_tls(safe_or_conn,
  839. suggested_bytes_to_read,
  840. &bytes_read);
  841. switch (tls_rv) {
  842. case TOR_TLS_CLOSE:
  843. case TOR_TLS_ERROR_IO:
  844. log_debug(LD_NET, "TLS connection closed %son read. Closing.",
  845. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  846. return E_ERROR;
  847. CASE_TOR_TLS_ERROR_ANY_NONIO:
  848. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  849. tor_tls_err_to_string(tls_rv));
  850. return E_ERROR;
  851. case TOR_TLS_WANTWRITE:
  852. // we need to wait for the socket to become writable
  853. // before we can do another read
  854. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  855. TO_SAFE_CONN(safe_or_conn));
  856. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  857. TO_SAFE_CONN(safe_or_conn));
  858. break;
  859. case TOR_TLS_WANTREAD:
  860. // we need to wait for the socket to become readable
  861. // again, then do another read
  862. break;
  863. default:
  864. break;
  865. }
  866. if (PREDICT_LIKELY(bytes_read < SIZE_MAX)) {
  867. size_t buf_len_diff = buf_datalen(TO_SAFE_CONN(safe_or_conn)->inbuf)-buf_initial_size;
  868. if (bytes_read != buf_len_diff) {
  869. log_warn(LD_OR, "Doesn't match! bytes_read: %ld, buf_len_diff: %ld",
  870. bytes_read, buf_len_diff);
  871. tor_assert_nonfatal_unreached_once();
  872. }
  873. } else {
  874. log_warn(LD_NET, "We read an unexpectedly large number of bytes: %zu "
  875. ">= SIZE_MAX",
  876. bytes_read);
  877. }
  878. // let any listeners know that we have new data in our incoming buffer
  879. if (bytes_read > 0) {
  880. event_data_t null_data = { .ptr = NULL };
  881. event_source_publish(TO_SAFE_CONN(safe_or_conn)->event_source,
  882. safe_or_conn_has_buffered_data_ev, null_data, NULL);
  883. }
  884. size_t tls_bytes_read = 0;
  885. size_t tls_bytes_written = 0;
  886. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  887. &tls_bytes_written);
  888. log_warn(LD_NET, "After TLS read of %ld: %ld read, %ld written",
  889. (long)bytes_read, (long)tls_bytes_read, (long)tls_bytes_written);
  890. // TODO: change this back to debug ^^
  891. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  892. tls_bytes_written, use_conn_buckets);
  893. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  894. return E_SUCCESS;
  895. }
  896. static tor_error_t
  897. safe_or_connection_write_encrypted(safe_or_connection_t *safe_or_conn,
  898. bool use_conn_buckets)
  899. {
  900. tor_assert(safe_or_conn != NULL);
  901. uint32_t coarse_time = monotime_coarse_get_stamp();
  902. safe_or_connection_refill_buckets(safe_or_conn, coarse_time);
  903. size_t max_bytes_to_write = \
  904. safe_or_connection_max_bytes_can_write(safe_or_conn, use_conn_buckets);
  905. log_warn(LD_NET, "max_bytes_to_write: %ld", max_bytes_to_write);
  906. // TODO: remove this ^^
  907. if (max_bytes_to_write == 0) {
  908. log_debug(LD_NET, "Write callback running, but not supposed to write bytes.");
  909. return E_SUCCESS;
  910. }
  911. size_t buf_initial_size = buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  912. size_t bytes_written = 0;
  913. max_bytes_to_write = MIN(max_bytes_to_write, buf_initial_size);
  914. int tls_rv = safe_or_connection_write_tls(safe_or_conn,
  915. max_bytes_to_write,
  916. &bytes_written);
  917. switch (tls_rv) {
  918. case TOR_TLS_CLOSE:
  919. case TOR_TLS_ERROR_IO:
  920. log_debug(LD_NET, "TLS connection closed %son write. Closing.",
  921. tls_rv == TOR_TLS_CLOSE ? "cleanly " : "");
  922. return E_ERROR;
  923. CASE_TOR_TLS_ERROR_ANY_NONIO:
  924. log_debug(LD_NET, "TLS error [%s]. Breaking.",
  925. tor_tls_err_to_string(tls_rv));
  926. return E_ERROR;
  927. case TOR_TLS_WANTWRITE:
  928. // we need to wait for the socket to become writable
  929. // again, then do another write
  930. log_warn(LD_NET, "Wants write");
  931. // TODO: remove this ^^
  932. break;
  933. case TOR_TLS_WANTREAD:
  934. // we need to wait for the socket to become readable
  935. // before we can do another write
  936. log_warn(LD_NET, "Wants read");
  937. // TODO: remove this ^^
  938. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  939. TO_SAFE_CONN(safe_or_conn));
  940. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  941. TO_SAFE_CONN(safe_or_conn));
  942. break;
  943. default:
  944. break;
  945. }
  946. if (PREDICT_LIKELY(bytes_written < SIZE_MAX)) {
  947. size_t buf_len_diff = buf_initial_size-buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf);
  948. if (bytes_written != buf_len_diff) {
  949. log_warn(LD_OR, "Doesn't match! bytes_written: %ld, buf_len_diff: %ld",
  950. bytes_written, buf_len_diff);
  951. tor_assert_nonfatal_unreached_once();
  952. }
  953. } else {
  954. log_warn(LD_NET, "We wrote an unexpectedly large number of bytes: %zu "
  955. ">= SIZE_MAX",
  956. bytes_written);
  957. }
  958. // fixes a throughput problem in old versions of Windows
  959. // TODO: we should still include this, but needs to be moved here since it's
  960. // currently static
  961. //update_send_buffer_size(TO_SAFE_CONN(safe_or_conn)->socket);
  962. if (buf_datalen(TO_SAFE_CONN(safe_or_conn)->outbuf) == 0) {
  963. // we have no more data to write
  964. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  965. TO_SAFE_CONN(safe_or_conn));
  966. }
  967. size_t tls_bytes_read = 0;
  968. size_t tls_bytes_written = 0;
  969. tor_tls_get_n_raw_bytes(safe_or_conn->tls, &tls_bytes_read,
  970. &tls_bytes_written);
  971. log_warn(LD_NET, "After TLS write of %ld: %ld read, %ld written",
  972. (long)bytes_written, (long)tls_bytes_read, (long)tls_bytes_written);
  973. // TODO: change this back to debug ^^
  974. safe_or_connection_decrement_buckets(safe_or_conn, tls_bytes_read,
  975. tls_bytes_written, use_conn_buckets);
  976. // TODO: if get_options()->TestingEnableConnBwEvent, increase conn stats?
  977. return E_SUCCESS;
  978. }
  979. static tor_error_t
  980. safe_or_connection_tls_handshake(safe_or_connection_t *safe_or_conn)
  981. {
  982. tor_assert(safe_or_conn != NULL);
  983. check_no_tls_errors();
  984. int result = tor_tls_handshake(safe_or_conn->tls);
  985. switch (result) {
  986. CASE_TOR_TLS_ERROR_ANY:
  987. log_info(LD_OR, "TLS error [%s]",
  988. tor_tls_err_to_string(result));
  989. return E_ERROR;
  990. case TOR_TLS_CLOSE:
  991. log_info(LD_OR, "TLS closed");
  992. return E_ERROR;
  993. case TOR_TLS_WANTWRITE:
  994. // we need to wait for the socket to become writable
  995. // before we can continue the handshake
  996. socket_rw_state_set(&safe_or_conn->tls_write_wanted, true,
  997. TO_SAFE_CONN(safe_or_conn));
  998. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  999. TO_SAFE_CONN(safe_or_conn));
  1000. return E_SUCCESS;
  1001. case TOR_TLS_WANTREAD:
  1002. // we need to wait for the socket to become readable
  1003. // before we can continue the handshake
  1004. socket_rw_state_set(&safe_or_conn->tls_read_wanted, true,
  1005. TO_SAFE_CONN(safe_or_conn));
  1006. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1007. TO_SAFE_CONN(safe_or_conn));
  1008. return E_SUCCESS;
  1009. case TOR_TLS_DONE:
  1010. // the TLS handshake has finished, but not the entire link handshake
  1011. if (tor_tls_is_server(safe_or_conn->tls)) {
  1012. // we didn't start the handshake, so prepare for a v3 handshake
  1013. log_debug(LD_OR, "Done with initial SSL handshake (receiver-side)");
  1014. } else {
  1015. // we need to start the v3 handshake
  1016. log_debug(LD_OR, "Done with initial SSL handshake (initiator-side)");
  1017. //if (connection_or_launch_v3_or_handshake(conn) < 0) {
  1018. // return E_ERROR;
  1019. //}
  1020. }
  1021. return safe_or_connection_update_state(safe_or_conn,
  1022. SAFE_OR_CONN_STATE_LINK_HANDSHAKING);
  1023. default:
  1024. log_warn(LD_OR, "Unexpected return value from handshake");
  1025. return E_ERROR;
  1026. }
  1027. }
  1028. /*
  1029. static int
  1030. safe_or_connection_tls_finish_v1_handshake(safe_or_connection_t *safe_or_conn)
  1031. {
  1032. tor_assert(safe_or_conn != NULL);
  1033. tor_assert(tor_tls_used_v1_handshake(safe_or_conn->tls));
  1034. tor_assert(tor_tls_is_server(safe_or_conn->tls));
  1035. tor_assert(!safe_or_conn->is_outgoing);
  1036. // we should not be making v1 handshakes, but we may receive v1 handshakes
  1037. log_debug(LD_HANDSHAKE, "%s tls v1 handshake on %p with %s done, using "
  1038. "ciphersuite %s. verifying.",
  1039. safe_or_conn->is_outgoing?"Outgoing":"Incoming",
  1040. safe_or_conn,
  1041. safe_or_conn->remote_address_str,
  1042. tor_tls_get_ciphersuite_name(safe_or_conn->tls));
  1043. //tor_tls_block_renegotiation(safe_or_conn->tls);
  1044. char digest_rcvd[DIGEST_LEN] = {0};
  1045. // TODO fix below
  1046. if (connection_or_check_valid_tls_handshake(conn, started_here,
  1047. digest_rcvd) < 0) {
  1048. return -1;
  1049. }
  1050. // TODO in main thread
  1051. //circuit_build_times_network_is_live(get_circuit_build_times_mutable());
  1052. //conn->link_proto = 1;
  1053. //connection_or_init_conn_from_address(conn, &conn->base_.addr,
  1054. // conn->base_.port, digest_rcvd,
  1055. // NULL, 0);
  1056. //rep_hist_note_negotiated_link_proto(1, started_here);
  1057. //return connection_or_set_state_open(conn);
  1058. return 0;
  1059. }
  1060. */
  1061. static void
  1062. safe_or_connection_read_cb(safe_connection_t *safe_conn)
  1063. {
  1064. tor_assert(safe_conn != NULL);
  1065. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1066. log_warn(LD_OR, "OR Connection Read (state=%d, obj=%p, %s)!!!!",
  1067. safe_or_conn->state, safe_or_conn,
  1068. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1069. // TODO: remove this ^^
  1070. //if (safe_or_conn->tls_write_waiting_on_socket_readable) {
  1071. // // since the socket is now readable, we can re-enable TLS write again
  1072. // safe_or_conn->tls_write_waiting_on_socket_readable = false;
  1073. // safe_connection_set_write_state(TO_SAFE_CONN(safe_or_conn), true);
  1074. //}
  1075. switch (safe_or_conn->state) {
  1076. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1077. tor_assert_unreached();
  1078. break;
  1079. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1080. // we shouldn't get here, so make sure we're not wanting to read
  1081. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1082. TO_SAFE_CONN(safe_or_conn));
  1083. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1084. TO_SAFE_CONN(safe_or_conn));
  1085. log_warn(LD_OR, "Connecting OR conection wants to read");
  1086. break;
  1087. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1088. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1089. "been implemented");
  1090. tor_assert(0);
  1091. // we are performing the proxy handshake
  1092. //tor_error_t rv = safe_or_connection_plaintext(safe_or_conn);
  1093. //if (rv != E_SUCCESS) {
  1094. // tor_assert(safe_or_connection_update_state(safe_or_conn,
  1095. // SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1096. //}
  1097. break;
  1098. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1099. {
  1100. // we are performing the initial TLS handshake
  1101. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1102. if (rv != E_SUCCESS) {
  1103. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1104. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1105. }
  1106. break;
  1107. }
  1108. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1109. case SAFE_OR_CONN_STATE_OPEN:
  1110. {
  1111. // performing the link handshake, or the handshake has already
  1112. // completed and we're sending/receiving cells
  1113. if (socket_rw_state_get(&safe_or_conn->tls_read_wanted)) {
  1114. // since the socket is now readable, we can re-enable writing again
  1115. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1116. TO_SAFE_CONN(safe_or_conn));
  1117. socket_rw_state_set(&safe_or_conn->tor_write_wanted, true,
  1118. TO_SAFE_CONN(safe_or_conn));
  1119. }
  1120. // TODO: we may not actually want to read here now that the states are
  1121. // updated, should we re-check?
  1122. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1123. tor_error_t rv = safe_or_connection_read_encrypted(safe_or_conn,
  1124. use_conn_buckets);
  1125. if (rv != E_SUCCESS) {
  1126. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1127. SAFE_OR_CONN_STATE_CLOSED) == E_SUCCESS);
  1128. }
  1129. break;
  1130. }
  1131. case SAFE_OR_CONN_STATE_CLOSED:
  1132. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1133. // we shouldn't get here, so make sure we're not wanting to read
  1134. socket_rw_state_set(&safe_or_conn->tls_read_wanted, false,
  1135. TO_SAFE_CONN(safe_or_conn));
  1136. socket_rw_state_set(&safe_or_conn->tor_read_wanted, false,
  1137. TO_SAFE_CONN(safe_or_conn));
  1138. log_warn(LD_OR, "Closed OR conection wants to read");
  1139. break;
  1140. default:
  1141. log_warn(LD_OR, "Unexpected safe OR connection state");
  1142. tor_assert(0);
  1143. break;
  1144. }
  1145. }
  1146. static void
  1147. safe_or_connection_write_cb(safe_connection_t *safe_conn)
  1148. {
  1149. tor_assert(safe_conn != NULL);
  1150. safe_or_connection_t *safe_or_conn = TO_SAFE_OR_CONN(safe_conn);
  1151. log_warn(LD_OR, "OR Connection Write (state=%d, obj=%p, %s)!!!!",
  1152. safe_or_conn->state, safe_or_conn,
  1153. safe_or_conn->is_outgoing?"outgoing":"incoming");
  1154. // TODO: remove this ^^
  1155. switch (safe_or_conn->state) {
  1156. case SAFE_OR_CONN_STATE_UNINITIALIZED:
  1157. tor_assert_unreached();
  1158. break;
  1159. case SAFE_OR_CONN_STATE_TCP_CONNECTING:
  1160. {
  1161. // the socket was connecting and is now ready to write, so we
  1162. // should check for errors before using the socket
  1163. tor_error_t rv = safe_or_connection_check_tcp_connection(safe_or_conn);
  1164. if (rv != E_SUCCESS) {
  1165. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1166. SAFE_OR_CONN_STATE_CLOSED));
  1167. }
  1168. break;
  1169. }
  1170. case SAFE_OR_CONN_STATE_PROXY_HANDSHAKING:
  1171. log_warn(LD_OR, "Relay connection proxy handshaking state has not yet "
  1172. "been implemented");
  1173. tor_assert(0);
  1174. // we are performing the proxy handshake
  1175. break;
  1176. case SAFE_OR_CONN_STATE_TLS_HANDSHAKING:
  1177. {
  1178. // we are performing the initial TLS handshake
  1179. tor_error_t rv = safe_or_connection_tls_handshake(safe_or_conn);
  1180. if (rv != E_SUCCESS) {
  1181. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1182. SAFE_OR_CONN_STATE_CLOSED));
  1183. }
  1184. break;
  1185. }
  1186. case SAFE_OR_CONN_STATE_LINK_HANDSHAKING:
  1187. case SAFE_OR_CONN_STATE_OPEN:
  1188. {
  1189. // performing the link handshake, or the handshake has already
  1190. // completed and we're sending/receiving cells
  1191. if (socket_rw_state_get(&safe_or_conn->tls_write_wanted)) {
  1192. // since the socket is now writable, we can re-enable reading again
  1193. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1194. TO_SAFE_CONN(safe_or_conn));
  1195. socket_rw_state_set(&safe_or_conn->tor_read_wanted, true,
  1196. TO_SAFE_CONN(safe_or_conn));
  1197. }
  1198. // TODO: we may not actually want to write here now that the states are
  1199. // updated, should we re-check?
  1200. bool use_conn_buckets = (safe_or_conn->state == SAFE_OR_CONN_STATE_OPEN);
  1201. tor_error_t rv = safe_or_connection_write_encrypted(safe_or_conn,
  1202. use_conn_buckets);
  1203. if (rv != E_SUCCESS) {
  1204. tor_assert(safe_or_connection_update_state(safe_or_conn,
  1205. SAFE_OR_CONN_STATE_CLOSED));
  1206. }
  1207. break;
  1208. }
  1209. case SAFE_OR_CONN_STATE_CLOSED:
  1210. case SAFE_OR_CONN_STATE_NO_SOCKET:
  1211. // we shouldn't get here, so make sure we're not wanting to write
  1212. socket_rw_state_set(&safe_or_conn->tls_write_wanted, false,
  1213. TO_SAFE_CONN(safe_or_conn));
  1214. socket_rw_state_set(&safe_or_conn->tor_write_wanted, false,
  1215. TO_SAFE_CONN(safe_or_conn));
  1216. log_warn(LD_OR, "Closed OR conection wants to write");
  1217. break;
  1218. default:
  1219. log_warn(LD_OR, "Unexpected safe OR connection state");
  1220. tor_assert(0);
  1221. break;
  1222. }
  1223. }