ext_orport.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /* Copyright (c) 2012-2016, 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. #define EXT_ORPORT_PRIVATE
  8. #include "or.h"
  9. #include "connection.h"
  10. #include "connection_or.h"
  11. #include "ext_orport.h"
  12. #include "control.h"
  13. #include "config.h"
  14. #include "util.h"
  15. #include "main.h"
  16. /** Allocate and return a structure capable of holding an Extended
  17. * ORPort message of body length <b>len</b>. */
  18. ext_or_cmd_t *
  19. ext_or_cmd_new(uint16_t len)
  20. {
  21. size_t size = STRUCT_OFFSET(ext_or_cmd_t, body) + len;
  22. ext_or_cmd_t *cmd = tor_malloc(size);
  23. cmd->len = len;
  24. return cmd;
  25. }
  26. /** Deallocate the Extended ORPort message in <b>cmd</b>. */
  27. void
  28. ext_or_cmd_free(ext_or_cmd_t *cmd)
  29. {
  30. tor_free(cmd);
  31. }
  32. /** Get an Extended ORPort message from <b>conn</b>, and place it in
  33. * <b>out</b>. Return -1 on fail, 0 if we need more data, and 1 if we
  34. * successfully extracted an Extended ORPort command from the
  35. * buffer. */
  36. static int
  37. connection_fetch_ext_or_cmd_from_buf(connection_t *conn, ext_or_cmd_t **out)
  38. {
  39. return fetch_ext_or_command_from_buf(conn->inbuf, out);
  40. }
  41. /** Write an Extended ORPort message to <b>conn</b>. Use
  42. * <b>command</b> as the command type, <b>bodylen</b> as the body
  43. * length, and <b>body</b>, if it's present, as the body of the
  44. * message. */
  45. STATIC int
  46. connection_write_ext_or_command(connection_t *conn,
  47. uint16_t command,
  48. const char *body,
  49. size_t bodylen)
  50. {
  51. char header[4];
  52. if (bodylen > UINT16_MAX)
  53. return -1;
  54. set_uint16(header, htons(command));
  55. set_uint16(header+2, htons(bodylen));
  56. connection_write_to_buf(header, 4, conn);
  57. if (bodylen) {
  58. tor_assert(body);
  59. connection_write_to_buf(body, bodylen, conn);
  60. }
  61. return 0;
  62. }
  63. /** Transition from an Extended ORPort which accepts Extended ORPort
  64. * messages, to an Extended ORport which accepts OR traffic. */
  65. static void
  66. connection_ext_or_transition(or_connection_t *conn)
  67. {
  68. tor_assert(conn->base_.type == CONN_TYPE_EXT_OR);
  69. conn->base_.type = CONN_TYPE_OR;
  70. TO_CONN(conn)->state = 0; // set the state to a neutral value
  71. control_event_or_conn_status(conn, OR_CONN_EVENT_NEW, 0);
  72. connection_tls_start_handshake(conn, 1);
  73. }
  74. /** Length of authentication cookie. */
  75. #define EXT_OR_PORT_AUTH_COOKIE_LEN 32
  76. /** Length of the header of the cookie file. */
  77. #define EXT_OR_PORT_AUTH_COOKIE_HEADER_LEN 32
  78. /** Static cookie file header. */
  79. #define EXT_OR_PORT_AUTH_COOKIE_HEADER "! Extended ORPort Auth Cookie !\x0a"
  80. /** Length of safe-cookie protocol hashes. */
  81. #define EXT_OR_PORT_AUTH_HASH_LEN DIGEST256_LEN
  82. /** Length of safe-cookie protocol nonces. */
  83. #define EXT_OR_PORT_AUTH_NONCE_LEN 32
  84. /** Safe-cookie protocol constants. */
  85. #define EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST \
  86. "ExtORPort authentication server-to-client hash"
  87. #define EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST \
  88. "ExtORPort authentication client-to-server hash"
  89. /* Code to indicate cookie authentication */
  90. #define EXT_OR_AUTHTYPE_SAFECOOKIE 0x01
  91. /** If true, we've set ext_or_auth_cookie to a secret code and stored
  92. * it to disk. */
  93. STATIC int ext_or_auth_cookie_is_set = 0;
  94. /** If ext_or_auth_cookie_is_set, a secret cookie that we've stored to disk
  95. * and which we're using to authenticate controllers. (If the controller can
  96. * read it off disk, it has permission to connect.) */
  97. STATIC uint8_t *ext_or_auth_cookie = NULL;
  98. /** Helper: Return a newly allocated string containing a path to the
  99. * file where we store our authentication cookie. */
  100. char *
  101. get_ext_or_auth_cookie_file_name(void)
  102. {
  103. const or_options_t *options = get_options();
  104. if (options->ExtORPortCookieAuthFile &&
  105. strlen(options->ExtORPortCookieAuthFile)) {
  106. return tor_strdup(options->ExtORPortCookieAuthFile);
  107. } else {
  108. return get_datadir_fname("extended_orport_auth_cookie");
  109. }
  110. }
  111. /* Initialize the cookie-based authentication system of the
  112. * Extended ORPort. If <b>is_enabled</b> is 0, then disable the cookie
  113. * authentication system. */
  114. int
  115. init_ext_or_cookie_authentication(int is_enabled)
  116. {
  117. char *fname = NULL;
  118. int retval;
  119. if (!is_enabled) {
  120. ext_or_auth_cookie_is_set = 0;
  121. return 0;
  122. }
  123. fname = get_ext_or_auth_cookie_file_name();
  124. retval = init_cookie_authentication(fname, EXT_OR_PORT_AUTH_COOKIE_HEADER,
  125. EXT_OR_PORT_AUTH_COOKIE_HEADER_LEN,
  126. get_options()->ExtORPortCookieAuthFileGroupReadable,
  127. &ext_or_auth_cookie,
  128. &ext_or_auth_cookie_is_set);
  129. tor_free(fname);
  130. return retval;
  131. }
  132. /** Read data from <b>conn</b> and see if the client sent us the
  133. * authentication type that they prefer to use in this session.
  134. *
  135. * Return -1 if we received corrupted data or if we don't support the
  136. * authentication type. Return 0 if we need more data in
  137. * <b>conn</b>. Return 1 if the authentication type negotiation was
  138. * successful. */
  139. static int
  140. connection_ext_or_auth_neg_auth_type(connection_t *conn)
  141. {
  142. char authtype[1] = {0};
  143. if (connection_get_inbuf_len(conn) < 1)
  144. return 0;
  145. if (connection_fetch_from_buf(authtype, 1, conn) < 0)
  146. return -1;
  147. log_debug(LD_GENERAL, "Client wants us to use %d auth type", authtype[0]);
  148. if (authtype[0] != EXT_OR_AUTHTYPE_SAFECOOKIE) {
  149. /* '1' is the only auth type supported atm */
  150. return -1;
  151. }
  152. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE;
  153. return 1;
  154. }
  155. /* DOCDOC */
  156. STATIC int
  157. handle_client_auth_nonce(const char *client_nonce, size_t client_nonce_len,
  158. char **client_hash_out,
  159. char **reply_out, size_t *reply_len_out)
  160. {
  161. char server_hash[EXT_OR_PORT_AUTH_HASH_LEN] = {0};
  162. char server_nonce[EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  163. char *reply;
  164. size_t reply_len;
  165. if (client_nonce_len != EXT_OR_PORT_AUTH_NONCE_LEN)
  166. return -1;
  167. /* Get our nonce */
  168. crypto_rand(server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  169. { /* set up macs */
  170. size_t hmac_s_msg_len = strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST) +
  171. 2*EXT_OR_PORT_AUTH_NONCE_LEN;
  172. size_t hmac_c_msg_len = strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST) +
  173. 2*EXT_OR_PORT_AUTH_NONCE_LEN;
  174. char *hmac_s_msg = tor_malloc_zero(hmac_s_msg_len);
  175. char *hmac_c_msg = tor_malloc_zero(hmac_c_msg_len);
  176. char *correct_client_hash = tor_malloc_zero(EXT_OR_PORT_AUTH_HASH_LEN);
  177. memcpy(hmac_s_msg,
  178. EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST,
  179. strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST));
  180. memcpy(hmac_s_msg + strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST),
  181. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  182. memcpy(hmac_s_msg + strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST) +
  183. EXT_OR_PORT_AUTH_NONCE_LEN,
  184. server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  185. memcpy(hmac_c_msg,
  186. EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST,
  187. strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST));
  188. memcpy(hmac_c_msg + strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST),
  189. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  190. memcpy(hmac_c_msg + strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST) +
  191. EXT_OR_PORT_AUTH_NONCE_LEN,
  192. server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  193. crypto_hmac_sha256(server_hash,
  194. (char*)ext_or_auth_cookie,
  195. EXT_OR_PORT_AUTH_COOKIE_LEN,
  196. hmac_s_msg,
  197. hmac_s_msg_len);
  198. crypto_hmac_sha256(correct_client_hash,
  199. (char*)ext_or_auth_cookie,
  200. EXT_OR_PORT_AUTH_COOKIE_LEN,
  201. hmac_c_msg,
  202. hmac_c_msg_len);
  203. /* Store the client hash we generated. We will need to compare it
  204. with the hash sent by the client. */
  205. *client_hash_out = correct_client_hash;
  206. memwipe(hmac_s_msg, 0, hmac_s_msg_len);
  207. memwipe(hmac_c_msg, 0, hmac_c_msg_len);
  208. tor_free(hmac_s_msg);
  209. tor_free(hmac_c_msg);
  210. }
  211. { /* debug logging */ /* XXX disable this codepath if not logging on debug?*/
  212. char server_hash_encoded[(2*EXT_OR_PORT_AUTH_HASH_LEN) + 1];
  213. char server_nonce_encoded[(2*EXT_OR_PORT_AUTH_NONCE_LEN) + 1];
  214. char client_nonce_encoded[(2*EXT_OR_PORT_AUTH_NONCE_LEN) + 1];
  215. base16_encode(server_hash_encoded, sizeof(server_hash_encoded),
  216. server_hash, sizeof(server_hash));
  217. base16_encode(server_nonce_encoded, sizeof(server_nonce_encoded),
  218. server_nonce, sizeof(server_nonce));
  219. base16_encode(client_nonce_encoded, sizeof(client_nonce_encoded),
  220. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  221. log_debug(LD_GENERAL,
  222. "server_hash: '%s'\nserver_nonce: '%s'\nclient_nonce: '%s'",
  223. server_hash_encoded, server_nonce_encoded, client_nonce_encoded);
  224. memwipe(server_hash_encoded, 0, sizeof(server_hash_encoded));
  225. memwipe(server_nonce_encoded, 0, sizeof(server_nonce_encoded));
  226. memwipe(client_nonce_encoded, 0, sizeof(client_nonce_encoded));
  227. }
  228. { /* write reply: (server_hash, server_nonce) */
  229. reply_len = EXT_OR_PORT_AUTH_COOKIE_LEN+EXT_OR_PORT_AUTH_NONCE_LEN;
  230. reply = tor_malloc_zero(reply_len);
  231. memcpy(reply, server_hash, EXT_OR_PORT_AUTH_HASH_LEN);
  232. memcpy(reply + EXT_OR_PORT_AUTH_HASH_LEN, server_nonce,
  233. EXT_OR_PORT_AUTH_NONCE_LEN);
  234. }
  235. *reply_out = reply;
  236. *reply_len_out = reply_len;
  237. return 0;
  238. }
  239. /** Read the client's nonce out of <b>conn</b>, setup the safe-cookie
  240. * crypto, and then send our own hash and nonce to the client
  241. *
  242. * Return -1 if there was an error; return 0 if we need more data in
  243. * <b>conn</b>, and return 1 if we successfully retrieved the
  244. * client's nonce and sent our own. */
  245. static int
  246. connection_ext_or_auth_handle_client_nonce(connection_t *conn)
  247. {
  248. char client_nonce[EXT_OR_PORT_AUTH_NONCE_LEN];
  249. char *reply=NULL;
  250. size_t reply_len=0;
  251. if (!ext_or_auth_cookie_is_set) { /* this should not happen */
  252. log_warn(LD_BUG, "Extended ORPort authentication cookie was not set. "
  253. "That's weird since we should have done that on startup. "
  254. "This might be a Tor bug, please file a bug report. ");
  255. return -1;
  256. }
  257. if (connection_get_inbuf_len(conn) < EXT_OR_PORT_AUTH_NONCE_LEN)
  258. return 0;
  259. if (connection_fetch_from_buf(client_nonce,
  260. EXT_OR_PORT_AUTH_NONCE_LEN, conn) < 0)
  261. return -1;
  262. /* We extract the ClientNonce from the received data, and use it to
  263. calculate ServerHash and ServerNonce according to proposal 217.
  264. We also calculate our own ClientHash value and save it in the
  265. connection state. We validate it later against the ClientHash
  266. sent by the client. */
  267. if (handle_client_auth_nonce(client_nonce, sizeof(client_nonce),
  268. &TO_OR_CONN(conn)->ext_or_auth_correct_client_hash,
  269. &reply, &reply_len) < 0)
  270. return -1;
  271. connection_write_to_buf(reply, reply_len, conn);
  272. memwipe(reply, 0, reply_len);
  273. tor_free(reply);
  274. log_debug(LD_GENERAL, "Got client nonce, and sent our own nonce and hash.");
  275. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH;
  276. return 1;
  277. }
  278. #define connection_ext_or_auth_send_result_success(c) \
  279. connection_ext_or_auth_send_result(c, 1)
  280. #define connection_ext_or_auth_send_result_fail(c) \
  281. connection_ext_or_auth_send_result(c, 0)
  282. /** Send authentication results to <b>conn</b>. Successful results if
  283. * <b>success</b> is set; failure results otherwise. */
  284. static void
  285. connection_ext_or_auth_send_result(connection_t *conn, int success)
  286. {
  287. if (success)
  288. connection_write_to_buf("\x01", 1, conn);
  289. else
  290. connection_write_to_buf("\x00", 1, conn);
  291. }
  292. /** Receive the client's hash from <b>conn</b>, validate that it's
  293. * correct, and then send the authentication results to the client.
  294. *
  295. * Return -1 if there was an error during validation; return 0 if we
  296. * need more data in <b>conn</b>, and return 1 if we successfully
  297. * validated the client's hash and sent a happy authentication
  298. * result. */
  299. static int
  300. connection_ext_or_auth_handle_client_hash(connection_t *conn)
  301. {
  302. char provided_client_hash[EXT_OR_PORT_AUTH_HASH_LEN] = {0};
  303. if (connection_get_inbuf_len(conn) < EXT_OR_PORT_AUTH_HASH_LEN)
  304. return 0;
  305. if (connection_fetch_from_buf(provided_client_hash,
  306. EXT_OR_PORT_AUTH_HASH_LEN, conn) < 0)
  307. return -1;
  308. if (tor_memneq(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash,
  309. provided_client_hash, EXT_OR_PORT_AUTH_HASH_LEN)) {
  310. log_warn(LD_GENERAL, "Incorrect client hash. Authentication failed.");
  311. connection_ext_or_auth_send_result_fail(conn);
  312. return -1;
  313. }
  314. log_debug(LD_GENERAL, "Got client's hash and it was legit.");
  315. /* send positive auth result */
  316. connection_ext_or_auth_send_result_success(conn);
  317. conn->state = EXT_OR_CONN_STATE_OPEN;
  318. return 1;
  319. }
  320. /** Handle data from <b>or_conn</b> received on Extended ORPort.
  321. * Return -1 on error. 0 on unsufficient data. 1 on correct. */
  322. static int
  323. connection_ext_or_auth_process_inbuf(or_connection_t *or_conn)
  324. {
  325. connection_t *conn = TO_CONN(or_conn);
  326. /* State transitions of the Extended ORPort authentication protocol:
  327. EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE (start state) ->
  328. EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE ->
  329. EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH ->
  330. EXT_OR_CONN_STATE_OPEN
  331. During EXT_OR_CONN_STATE_OPEN, data is handled by
  332. connection_ext_or_process_inbuf().
  333. */
  334. switch (conn->state) { /* Functionify */
  335. case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
  336. return connection_ext_or_auth_neg_auth_type(conn);
  337. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
  338. return connection_ext_or_auth_handle_client_nonce(conn);
  339. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
  340. return connection_ext_or_auth_handle_client_hash(conn);
  341. default:
  342. log_warn(LD_BUG, "Encountered unexpected connection state %d while trying "
  343. "to process Extended ORPort authentication data.", conn->state);
  344. return -1;
  345. }
  346. }
  347. /** Extended ORPort commands (Transport-to-Bridge) */
  348. #define EXT_OR_CMD_TB_DONE 0x0000
  349. #define EXT_OR_CMD_TB_USERADDR 0x0001
  350. #define EXT_OR_CMD_TB_TRANSPORT 0x0002
  351. /** Extended ORPort commands (Bridge-to-Transport) */
  352. #define EXT_OR_CMD_BT_OKAY 0x1000
  353. #define EXT_OR_CMD_BT_DENY 0x1001
  354. #define EXT_OR_CMD_BT_CONTROL 0x1002
  355. /** Process a USERADDR command from the Extended
  356. * ORPort. <b>payload</b> is a payload of size <b>len</b>.
  357. *
  358. * If the USERADDR command was well formed, change the address of
  359. * <b>conn</b> to the address on the USERADDR command.
  360. *
  361. * Return 0 on success and -1 on error. */
  362. static int
  363. connection_ext_or_handle_cmd_useraddr(connection_t *conn,
  364. const char *payload, uint16_t len)
  365. {
  366. /* Copy address string. */
  367. tor_addr_t addr;
  368. uint16_t port;
  369. char *addr_str;
  370. char *address_part=NULL;
  371. int res;
  372. if (memchr(payload, '\0', len)) {
  373. log_fn(LOG_PROTOCOL_WARN, LD_NET, "Unexpected NUL in ExtORPort UserAddr");
  374. return -1;
  375. }
  376. addr_str = tor_memdup_nulterm(payload, len);
  377. res = tor_addr_port_split(LOG_INFO, addr_str, &address_part, &port);
  378. tor_free(addr_str);
  379. if (res<0)
  380. return -1;
  381. res = tor_addr_parse(&addr, address_part);
  382. tor_free(address_part);
  383. if (res<0)
  384. return -1;
  385. { /* do some logging */
  386. char *old_address = tor_addr_to_str_dup(&conn->addr);
  387. char *new_address = tor_addr_to_str_dup(&addr);
  388. log_debug(LD_NET, "Received USERADDR."
  389. "We rewrite our address from '%s:%u' to '%s:%u'.",
  390. safe_str(old_address), conn->port, safe_str(new_address), port);
  391. tor_free(old_address);
  392. tor_free(new_address);
  393. }
  394. /* record the address */
  395. tor_addr_copy(&conn->addr, &addr);
  396. conn->port = port;
  397. if (conn->address) {
  398. tor_free(conn->address);
  399. }
  400. conn->address = tor_addr_to_str_dup(&addr);
  401. return 0;
  402. }
  403. /** Process a TRANSPORT command from the Extended
  404. * ORPort. <b>payload</b> is a payload of size <b>len</b>.
  405. *
  406. * If the TRANSPORT command was well formed, register the name of the
  407. * transport on <b>conn</b>.
  408. *
  409. * Return 0 on success and -1 on error. */
  410. static int
  411. connection_ext_or_handle_cmd_transport(or_connection_t *conn,
  412. const char *payload, uint16_t len)
  413. {
  414. char *transport_str;
  415. if (memchr(payload, '\0', len)) {
  416. log_fn(LOG_PROTOCOL_WARN, LD_NET, "Unexpected NUL in ExtORPort Transport");
  417. return -1;
  418. }
  419. transport_str = tor_memdup_nulterm(payload, len);
  420. /* Transport names MUST be C-identifiers. */
  421. if (!string_is_C_identifier(transport_str)) {
  422. tor_free(transport_str);
  423. return -1;
  424. }
  425. /* If ext_or_transport is already occupied (because the PT sent two
  426. * TRANSPORT commands), deallocate the old name and keep the new
  427. * one */
  428. if (conn->ext_or_transport)
  429. tor_free(conn->ext_or_transport);
  430. conn->ext_or_transport = transport_str;
  431. return 0;
  432. }
  433. #define EXT_OR_CONN_STATE_IS_AUTHENTICATING(st) \
  434. ((st) <= EXT_OR_CONN_STATE_AUTH_MAX)
  435. /** Process Extended ORPort messages from <b>or_conn</b>. */
  436. int
  437. connection_ext_or_process_inbuf(or_connection_t *or_conn)
  438. {
  439. connection_t *conn = TO_CONN(or_conn);
  440. ext_or_cmd_t *command;
  441. int r;
  442. /* DOCDOC Document the state machine and transitions in this function */
  443. /* If we are still in the authentication stage, process traffic as
  444. authentication data: */
  445. while (EXT_OR_CONN_STATE_IS_AUTHENTICATING(conn->state)) {
  446. log_debug(LD_GENERAL, "Got Extended ORPort authentication data (%u).",
  447. (unsigned int) connection_get_inbuf_len(conn));
  448. r = connection_ext_or_auth_process_inbuf(or_conn);
  449. if (r < 0) {
  450. connection_mark_for_close(conn);
  451. return -1;
  452. } else if (r == 0) {
  453. return 0;
  454. }
  455. /* if r > 0, loop and process more data (if any). */
  456. }
  457. while (1) {
  458. log_debug(LD_GENERAL, "Got Extended ORPort data.");
  459. command = NULL;
  460. r = connection_fetch_ext_or_cmd_from_buf(conn, &command);
  461. if (r < 0)
  462. goto err;
  463. else if (r == 0)
  464. return 0; /* need to wait for more data */
  465. /* Got a command! */
  466. tor_assert(command);
  467. if (command->cmd == EXT_OR_CMD_TB_DONE) {
  468. if (connection_get_inbuf_len(conn)) {
  469. /* The inbuf isn't empty; the client is misbehaving. */
  470. goto err;
  471. }
  472. log_debug(LD_NET, "Received DONE.");
  473. /* If the transport proxy did not use the TRANSPORT command to
  474. * specify the transport name, mark this as unknown transport. */
  475. if (!or_conn->ext_or_transport) {
  476. /* We write this string this way to avoid ??>, which is a C
  477. * trigraph. */
  478. or_conn->ext_or_transport = tor_strdup("<?" "?>");
  479. }
  480. connection_write_ext_or_command(conn, EXT_OR_CMD_BT_OKAY, NULL, 0);
  481. /* can't transition immediately; need to flush first. */
  482. conn->state = EXT_OR_CONN_STATE_FLUSHING;
  483. connection_stop_reading(conn);
  484. } else if (command->cmd == EXT_OR_CMD_TB_USERADDR) {
  485. if (connection_ext_or_handle_cmd_useraddr(conn,
  486. command->body, command->len) < 0)
  487. goto err;
  488. } else if (command->cmd == EXT_OR_CMD_TB_TRANSPORT) {
  489. if (connection_ext_or_handle_cmd_transport(or_conn,
  490. command->body, command->len) < 0)
  491. goto err;
  492. } else {
  493. log_notice(LD_NET,"Got Extended ORPort command we don't regognize (%u).",
  494. command->cmd);
  495. }
  496. ext_or_cmd_free(command);
  497. }
  498. return 0;
  499. err:
  500. ext_or_cmd_free(command);
  501. connection_mark_for_close(conn);
  502. return -1;
  503. }
  504. /** <b>conn</b> finished flushing Extended ORPort messages to the
  505. * network, and is now ready to accept OR traffic. This function
  506. * does the transition. */
  507. int
  508. connection_ext_or_finished_flushing(or_connection_t *conn)
  509. {
  510. if (conn->base_.state == EXT_OR_CONN_STATE_FLUSHING) {
  511. connection_start_reading(TO_CONN(conn));
  512. connection_ext_or_transition(conn);
  513. }
  514. return 0;
  515. }
  516. /** Initiate Extended ORPort authentication, by sending the list of
  517. * supported authentication types to the client. */
  518. int
  519. connection_ext_or_start_auth(or_connection_t *or_conn)
  520. {
  521. connection_t *conn = TO_CONN(or_conn);
  522. const uint8_t authtypes[] = {
  523. /* We only support authtype '1' for now. */
  524. EXT_OR_AUTHTYPE_SAFECOOKIE,
  525. /* Marks the end of the list. */
  526. 0
  527. };
  528. log_debug(LD_GENERAL,
  529. "ExtORPort authentication: Sending supported authentication types");
  530. connection_write_to_buf((const char *)authtypes, sizeof(authtypes), conn);
  531. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE;
  532. return 0;
  533. }
  534. /** Free any leftover allocated memory of the ext_orport.c subsystem. */
  535. void
  536. ext_orport_free_all(void)
  537. {
  538. if (ext_or_auth_cookie) /* Free the auth cookie */
  539. tor_free(ext_or_auth_cookie);
  540. }