ext_orport.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* Copyright (c) 2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file ext_orport.c
  5. * \brief Code implementing the Extended ORPort.
  6. */
  7. #include "or.h"
  8. #include "connection.h"
  9. #include "connection_or.h"
  10. #include "ext_orport.h"
  11. #include "control.h"
  12. #include "config.h"
  13. #include "main.h"
  14. /** Allocate and return a structure capable of holding an Extended
  15. * ORPort message of body length <b>len</b>. */
  16. ext_or_cmd_t *
  17. ext_or_cmd_new(uint16_t len)
  18. {
  19. size_t size = STRUCT_OFFSET(ext_or_cmd_t, body) + len;
  20. ext_or_cmd_t *cmd = tor_malloc(size);
  21. cmd->len = len;
  22. return cmd;
  23. }
  24. /** Deallocate the Extended ORPort message in <b>cmd</b>. */
  25. void
  26. ext_or_cmd_free(ext_or_cmd_t *cmd)
  27. {
  28. tor_free(cmd);
  29. }
  30. /** Get an Extended ORPort message from <b>conn</b>, and place it in
  31. * <b>out</b>. Return -1 on fail, 0 if we need more data, and 1 if we
  32. * successfully extracted an Extended ORPort command from the
  33. * buffer. */
  34. static int
  35. connection_fetch_ext_or_cmd_from_buf(connection_t *conn, ext_or_cmd_t **out)
  36. {
  37. IF_HAS_BUFFEREVENT(conn, {
  38. struct evbuffer *input = bufferevent_get_input(conn->bufev);
  39. return fetch_ext_or_command_from_evbuffer(input, out);
  40. }) ELSE_IF_NO_BUFFEREVENT {
  41. return fetch_ext_or_command_from_buf(conn->inbuf, out);
  42. }
  43. }
  44. /** Write an Extended ORPort message to <b>conn</b>. Use
  45. * <b>command</b> as the command type, <b>bodylen</b> as the body
  46. * length, and <b>body</b>, if it's present, as the body of the
  47. * message. */
  48. static int
  49. connection_write_ext_or_command(connection_t *conn,
  50. uint16_t command,
  51. const char *body,
  52. size_t bodylen)
  53. {
  54. char header[4];
  55. if (bodylen > UINT16_MAX)
  56. return -1;
  57. set_uint16(header, htons(command));
  58. set_uint16(header+2, htons(bodylen));
  59. connection_write_to_buf(header, 4, conn);
  60. if (bodylen) {
  61. tor_assert(body);
  62. connection_write_to_buf(body, bodylen, conn);
  63. }
  64. return 0;
  65. }
  66. /** Transition from an Extended ORPort which accepts Extended ORPort
  67. * messages, to an Extended ORport which accepts OR traffic. */
  68. static void
  69. connection_ext_or_transition(or_connection_t *conn)
  70. {
  71. tor_assert(conn->base_.type == CONN_TYPE_EXT_OR);
  72. conn->base_.type = CONN_TYPE_OR;
  73. control_event_or_conn_status(conn, OR_CONN_EVENT_NEW, 0);
  74. connection_tls_start_handshake(conn, 1);
  75. }
  76. /** Length of authentication cookie. */
  77. #define EXT_OR_PORT_AUTH_COOKIE_LEN 32
  78. /** Length of the header of the cookie file. */
  79. #define EXT_OR_PORT_AUTH_COOKIE_HEADER_LEN 32
  80. /** Total length of the cookie file. */
  81. #define EXT_OR_PORT_AUTH_COOKIE_FILE_LEN \
  82. EXT_OR_PORT_AUTH_COOKIE_LEN+EXT_OR_PORT_AUTH_COOKIE_HEADER_LEN
  83. /** Static cookie file header. */
  84. #define EXT_OR_PORT_AUTH_COOKIE_HEADER "! Extended ORPort Auth Cookie !\x0a"
  85. /** Length of safe-cookie protocol hashes. */
  86. #define EXT_OR_PORT_AUTH_HASH_LEN DIGEST256_LEN
  87. /** Length of safe-cookie protocol nonces. */
  88. #define EXT_OR_PORT_AUTH_NONCE_LEN 32
  89. /** Safe-cookie protocol constants. */
  90. #define EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST \
  91. "ExtORPort authentication server-to-client hash"
  92. #define EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST \
  93. "ExtORPort authentication client-to-server hash"
  94. /** If true, we've set ext_or_auth_cookie to a secret code and stored
  95. * it to disk. */
  96. static int ext_or_auth_cookie_is_set = 0;
  97. /** If ext_or_auth_cookie_is_set, a secret cookie that we've stored to disk
  98. * and which we're using to authenticate controllers. (If the controller can
  99. * read it off disk, it has permission to connect.) */
  100. static char ext_or_auth_cookie[EXT_OR_PORT_AUTH_COOKIE_LEN] = {0};
  101. /** Helper: Return a newly allocated string containing a path to the
  102. * file where we store our authentication cookie. */
  103. char *
  104. get_ext_or_auth_cookie_file(void)
  105. {
  106. const or_options_t *options = get_options();
  107. if (options->ExtORPortCookieAuthFile &&
  108. strlen(options->ExtORPortCookieAuthFile)) {
  109. return tor_strdup(options->ExtORPortCookieAuthFile);
  110. } else {
  111. return get_datadir_fname("extended_orport_auth_cookie");
  112. }
  113. }
  114. /** Choose a random authentication cookie and write it to disk.
  115. * Anybody who can read the cookie from disk will be considered
  116. * authorized to use the control connection. Return -1 if we can't
  117. * write the file, or 0 on success. */
  118. int
  119. init_ext_or_cookie_authentication(int is_enabled)
  120. {
  121. char *fname;
  122. char cookie_file_string[EXT_OR_PORT_AUTH_COOKIE_FILE_LEN];
  123. if (!is_enabled) {
  124. ext_or_auth_cookie_is_set = 0;
  125. return 0;
  126. }
  127. /* We don't want to generate a new cookie every time we call
  128. * options_act(). One should be enough. */
  129. if (ext_or_auth_cookie_is_set)
  130. return 0; /* all set */
  131. if (crypto_rand(ext_or_auth_cookie, EXT_OR_PORT_AUTH_COOKIE_LEN) < 0)
  132. return -1;
  133. ext_or_auth_cookie_is_set = 1;
  134. memcpy(cookie_file_string, EXT_OR_PORT_AUTH_COOKIE_HEADER,
  135. EXT_OR_PORT_AUTH_COOKIE_HEADER_LEN);
  136. memcpy(cookie_file_string+EXT_OR_PORT_AUTH_COOKIE_HEADER_LEN,
  137. ext_or_auth_cookie, EXT_OR_PORT_AUTH_COOKIE_LEN);
  138. fname = get_ext_or_auth_cookie_file();
  139. if (write_bytes_to_file(fname, cookie_file_string,
  140. EXT_OR_PORT_AUTH_COOKIE_FILE_LEN, 1)) {
  141. log_warn(LD_FS,"Error writing authentication cookie to %s.",
  142. escaped(fname));
  143. tor_free(fname);
  144. return -1;
  145. }
  146. log_warn(LD_GENERAL, "Generated Extended ORPort cookie file in '%s'.",
  147. fname);
  148. tor_free(fname);
  149. return 0;
  150. }
  151. /** Read data from <b>conn</b> and see if the client sent us the
  152. * authentication type that she prefers to use in this session.
  153. *
  154. * Return -1 if we received corrupted data or if we don't support the
  155. * authentication type. Return 0 if we need more data in
  156. * <b>conn</b>. Return 1 if the authentication type negotiation was
  157. * successful. */
  158. static int
  159. connection_ext_or_auth_neg_auth_type(connection_t *conn)
  160. {
  161. char authtype[1] = {0};
  162. if (connection_get_inbuf_len(conn) < 1)
  163. return 0;
  164. if (connection_fetch_from_buf(authtype, 1, conn) < 0)
  165. return -1;
  166. log_warn(LD_GENERAL, "Client wants us to use %d auth type", authtype[0]);
  167. if (authtype[0] != 1) /* '1' is the only auth type supported atm */
  168. return -1;
  169. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE;
  170. return 1;
  171. }
  172. /** Read the client's nonce out of <b>conn</b>, setup the safe-cookie
  173. * crypto, and then send our own hash and nonce to the client
  174. *
  175. * Return -1 if there was an error; return 0 if we need more data in
  176. * <b>conn</b>, and return 1 if we successfully retrieved the
  177. * client's nonce and sent our own. */
  178. static int
  179. connection_ext_or_auth_handle_client_nonce(connection_t *conn)
  180. {
  181. char server_hash[EXT_OR_PORT_AUTH_HASH_LEN] = {0};
  182. char client_nonce[EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  183. char server_nonce[EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  184. char reply[EXT_OR_PORT_AUTH_COOKIE_LEN+EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  185. if (!ext_or_auth_cookie_is_set) { /* this should not happen */
  186. log_warn(LD_BUG, "Extended ORPort authentication cookie was not set. "
  187. "That's weird since we should have done that on startup. "
  188. "This might be a Tor bug, please file a bug report. ");
  189. return -1;
  190. }
  191. if (connection_get_inbuf_len(conn) < EXT_OR_PORT_AUTH_NONCE_LEN)
  192. return 0;
  193. if (connection_fetch_from_buf(client_nonce,
  194. EXT_OR_PORT_AUTH_NONCE_LEN, conn) < 0) /* XXX check-spaces */
  195. return -1;
  196. /* Get our nonce */
  197. if (crypto_rand(server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN) < 0)
  198. return -1;
  199. { /* set up macs */
  200. size_t hmac_s_msg_len = strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST) +
  201. 2*EXT_OR_PORT_AUTH_NONCE_LEN;
  202. size_t hmac_c_msg_len = strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST) +
  203. 2*EXT_OR_PORT_AUTH_NONCE_LEN;
  204. char *hmac_s_msg = tor_malloc_zero(hmac_s_msg_len);
  205. char *hmac_c_msg = tor_malloc_zero(hmac_c_msg_len);
  206. char *correct_client_hash = tor_malloc_zero(EXT_OR_PORT_AUTH_HASH_LEN);
  207. memcpy(hmac_s_msg,
  208. EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST,
  209. strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST));
  210. memcpy(hmac_s_msg + strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST),
  211. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  212. memcpy(hmac_s_msg + strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST) +
  213. EXT_OR_PORT_AUTH_NONCE_LEN,
  214. server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  215. memcpy(hmac_c_msg,
  216. EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST,
  217. strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST));
  218. memcpy(hmac_c_msg + strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST),
  219. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  220. memcpy(hmac_c_msg + strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST) +
  221. EXT_OR_PORT_AUTH_NONCE_LEN,
  222. server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  223. crypto_hmac_sha256(server_hash,
  224. ext_or_auth_cookie,
  225. EXT_OR_PORT_AUTH_COOKIE_LEN,
  226. hmac_s_msg,
  227. hmac_s_msg_len);
  228. crypto_hmac_sha256(correct_client_hash,
  229. ext_or_auth_cookie,
  230. EXT_OR_PORT_AUTH_COOKIE_LEN,
  231. hmac_c_msg,
  232. hmac_c_msg_len);
  233. /* Store the client hash we generated. We will need to compare it
  234. with the hash sent by the client. */
  235. TO_OR_CONN(conn)->ext_or_auth_correct_client_hash = correct_client_hash;
  236. tor_free(hmac_s_msg);
  237. tor_free(hmac_c_msg);
  238. }
  239. { /* debug logging */ /* XXX disable this codepath if not logging on debug?*/
  240. char server_hash_encoded[(2*EXT_OR_PORT_AUTH_HASH_LEN) + 1];
  241. char server_nonce_encoded[(2*EXT_OR_PORT_AUTH_NONCE_LEN) + 1];
  242. char client_nonce_encoded[(2*EXT_OR_PORT_AUTH_NONCE_LEN) + 1];
  243. base16_encode(server_hash_encoded, sizeof(server_hash_encoded),
  244. server_hash, sizeof(server_hash));
  245. base16_encode(server_nonce_encoded, sizeof(server_nonce_encoded),
  246. server_nonce, sizeof(server_nonce));
  247. base16_encode(client_nonce_encoded, sizeof(client_nonce_encoded),
  248. client_nonce, sizeof(client_nonce));
  249. log_warn(LD_GENERAL,
  250. "server_hash: '%s'\nserver_nonce: '%s'\nclient_nonce: '%s'",
  251. server_hash_encoded, server_nonce_encoded, client_nonce_encoded);
  252. }
  253. { /* write reply: (server_hash, server_nonce) */
  254. memcpy(reply, server_hash, EXT_OR_PORT_AUTH_HASH_LEN);
  255. memcpy(reply + EXT_OR_PORT_AUTH_HASH_LEN, server_nonce,
  256. EXT_OR_PORT_AUTH_NONCE_LEN);
  257. connection_write_to_buf(reply, sizeof(reply), conn);
  258. }
  259. log_warn(LD_GENERAL, "Got client nonce, and sent our own nonce and hash.");
  260. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH;
  261. return 1;
  262. }
  263. #define connection_ext_or_auth_send_result_success(c) \
  264. connection_ext_or_auth_send_result(c, 1)
  265. #define connection_ext_or_auth_send_result_fail(c) \
  266. connection_ext_or_auth_send_result(c, 0)
  267. /** Send authentication results to <b>conn</b>. Successful results if
  268. * <b>success</b> is set; failure results otherwise. */
  269. static void
  270. connection_ext_or_auth_send_result(connection_t *conn, int success)
  271. {
  272. if (success)
  273. connection_write_to_buf("\x01", 1, conn);
  274. else
  275. connection_write_to_buf("\x00", 1, conn);
  276. }
  277. /** Receive the client's hash from <b>conn</b>, validate that it's
  278. * correct, and then send the authentication results to the client.
  279. *
  280. * Return -1 if there was an error during validation; return 0 if we
  281. * need more data in <b>conn</b>, and return 1 if we successfully
  282. * validated the client's hash and sent a happy authentication
  283. * result. */
  284. static int
  285. connection_ext_or_auth_handle_client_hash(connection_t *conn)
  286. {
  287. char provided_client_hash[EXT_OR_PORT_AUTH_HASH_LEN] = {0};
  288. if (connection_get_inbuf_len(conn) < EXT_OR_PORT_AUTH_HASH_LEN)
  289. return 0;
  290. if (connection_fetch_from_buf(provided_client_hash,
  291. EXT_OR_PORT_AUTH_HASH_LEN, conn) < 0)
  292. return -1;
  293. if (tor_memneq(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash,
  294. provided_client_hash, EXT_OR_PORT_AUTH_HASH_LEN)) {
  295. log_warn(LD_GENERAL, "Incorrect client hash. Authentication failed.");
  296. connection_ext_or_auth_send_result_fail(conn);
  297. return -1;
  298. }
  299. log_warn(LD_GENERAL, "Got client's hash and it was legit.");
  300. /* send positive auth result */
  301. connection_ext_or_auth_send_result_success(conn);
  302. conn->state = EXT_OR_CONN_STATE_OPEN;
  303. return 1;
  304. }
  305. /** Handle data from <b>or_conn</b> received on Extended ORPort.
  306. * Return -1 on error. 0 on unsufficient data. 1 on correct. */
  307. static int
  308. connection_ext_or_auth_process_inbuf(or_connection_t *or_conn)
  309. {
  310. connection_t *conn = TO_CONN(or_conn);
  311. switch (conn->state) { /* Functionify */
  312. case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
  313. return connection_ext_or_auth_neg_auth_type(conn);
  314. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
  315. return connection_ext_or_auth_handle_client_nonce(conn);
  316. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
  317. return connection_ext_or_auth_handle_client_hash(conn);
  318. default:
  319. log_warn(LD_BUG, "Encountered unexpected connection state %d while trying "
  320. "to process Extended ORPort authentication data.", conn->state);
  321. return -1;
  322. }
  323. }
  324. /** Extended ORPort commands (Transport-to-Bridge) */
  325. #define EXT_OR_CMD_TB_DONE 0x0000
  326. #define EXT_OR_CMD_TB_USERADDR 0x0001
  327. /** Extended ORPort commands (Bridge-to-Transport) */
  328. #define EXT_OR_CMD_BT_OKAY 0x1000
  329. #define EXT_OR_CMD_BT_DENY 0x1001
  330. #define EXT_OR_CMD_BT_CONTROL 0x1002
  331. /** Process a USERADDR command from the Extended
  332. * ORPort. <b>payload</b> is a payload of size <b>len</b>.
  333. *
  334. * If the USERADDR command was well formed, change the address of
  335. * <b>conn</b> to the address on the USERADDR command.
  336. *
  337. * Return 0 on success and -1 on error. */
  338. static int
  339. connection_ext_or_handle_useraddr(connection_t *conn,
  340. const char *payload, uint16_t len)
  341. {
  342. /* Copy address string. */
  343. tor_addr_t addr;
  344. uint16_t port;
  345. char *addr_str;
  346. char *address_part=NULL;
  347. int res;
  348. addr_str = tor_malloc(len + 1);
  349. memcpy(addr_str, payload, len);
  350. addr_str[len] = 0;
  351. res = tor_addr_port_split(LOG_INFO, addr_str, &address_part, &port);
  352. tor_free(addr_str);
  353. if (res<0)
  354. return -1;
  355. res = tor_addr_parse(&addr, address_part);
  356. tor_free(address_part);
  357. if (res<0)
  358. return -1;
  359. { /* do some logging */
  360. char *old_address = tor_dup_addr(&conn->addr);
  361. char *new_address = tor_dup_addr(&addr);
  362. log_warn(LD_NET, "Received USERADDR." /* XXX Fix log severities/messages */
  363. "We rewrite our address from '%s:%u' to '%s:%u'.",
  364. safe_str(old_address), conn->port, safe_str(new_address), port);
  365. tor_free(old_address);
  366. tor_free(new_address);
  367. }
  368. /* record the address */
  369. tor_addr_copy(&conn->addr, &addr);
  370. conn->port = port;
  371. return 0;
  372. }
  373. /** Process Extended ORPort messages from <b>or_conn</b>. */
  374. int
  375. connection_ext_or_process_inbuf(or_connection_t *or_conn)
  376. {
  377. connection_t *conn = TO_CONN(or_conn);
  378. ext_or_cmd_t *command;
  379. int r;
  380. /* If we are still in the authentication stage, process traffic as
  381. authentication data: */
  382. while (conn->state <= EXT_OR_CONN_STATE_AUTH_MAX) {
  383. log_warn(LD_GENERAL, "Got Extended ORPort authentication data (%u).",
  384. (unsigned int) connection_get_inbuf_len(conn));
  385. r = connection_ext_or_auth_process_inbuf(or_conn);
  386. if (r < 0) {
  387. connection_mark_for_close(conn);
  388. return -1;
  389. } else if (r == 0) {
  390. return 0;
  391. }
  392. /* if r > 0, loop and process more data (if any). */
  393. }
  394. while (1) {
  395. log_warn(LD_GENERAL, "Got Extended ORPort data.");
  396. command = NULL;
  397. r = connection_fetch_ext_or_cmd_from_buf(conn, &command);
  398. if (r < 0)
  399. goto err;
  400. else if (r == 0)
  401. return 0; /* need to wait for more data */
  402. /* Got a command! */
  403. tor_assert(command);
  404. if (command->cmd == EXT_OR_CMD_TB_DONE) {
  405. if (connection_get_inbuf_len(conn)) {
  406. /* The inbuf isn't empty; the client is misbehaving. */
  407. goto err;
  408. }
  409. log_debug(LD_NET, "Received DONE.");
  410. connection_write_ext_or_command(conn, EXT_OR_CMD_BT_OKAY, NULL, 0);
  411. /* can't transition immediately; need to flush first. */
  412. conn->state = EXT_OR_CONN_STATE_FLUSHING;
  413. connection_stop_reading(conn);
  414. } else if (command->cmd == EXT_OR_CMD_TB_USERADDR) {
  415. if (connection_ext_or_handle_useraddr(conn,
  416. command->body, command->len) < 0)
  417. goto err;
  418. } else {
  419. log_notice(LD_NET,"Got Extended ORPort command we don't regognize (%u).",
  420. command->cmd);
  421. }
  422. ext_or_cmd_free(command);
  423. }
  424. return 0;
  425. err:
  426. ext_or_cmd_free(command);
  427. connection_mark_for_close(conn);
  428. return -1;
  429. }
  430. /** <b>conn</b> finished flushing Extended ORPort messages to the
  431. * network, and is now ready to accept OR traffic. This function
  432. * does the transition. */
  433. int
  434. connection_ext_or_finished_flushing(or_connection_t *conn)
  435. {
  436. if (conn->base_.state == EXT_OR_CONN_STATE_FLUSHING) {
  437. connection_start_reading(TO_CONN(conn));
  438. connection_ext_or_transition(conn);
  439. }
  440. return 0;
  441. }
  442. /** Initiate Extended ORPort authentication, by sending the list of
  443. * supported authentication types to the client. */
  444. int
  445. connection_ext_or_start_auth(or_connection_t *or_conn)
  446. {
  447. connection_t *conn = TO_CONN(or_conn);
  448. char authtypes[2] = "\x01\x00"; /* We only support authtype '1' for now. */
  449. log_warn(LD_GENERAL,
  450. "ExtORPort authentication: Sending supported authentication types");
  451. connection_write_to_buf(authtypes, sizeof(authtypes), conn);
  452. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE;
  453. return 0;
  454. }