ext_orport.c 20 KB

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