ext_orport.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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. tor_free(fname);
  150. return 0;
  151. }
  152. /** Read data from <b>conn</b> and see if the client sent us the
  153. * authentication type that she prefers to use in this session.
  154. *
  155. * Return -1 if we received corrupted data or if we don't support the
  156. * authentication type. Return 0 if we need more data in
  157. * <b>conn</b>. Return 1 if the authentication type negotiation was
  158. * successful. */
  159. static int
  160. connection_ext_or_auth_neg_auth_type(connection_t *conn)
  161. {
  162. char authtype[1] = {0};
  163. if (connection_get_inbuf_len(conn) < 1)
  164. return 0;
  165. if (connection_fetch_from_buf(authtype, 1, conn) < 0)
  166. return -1;
  167. log_debug(LD_GENERAL, "Client wants us to use %d auth type", authtype[0]);
  168. if (authtype[0] != 1) /* '1' is the only auth type supported atm */
  169. return -1;
  170. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE;
  171. return 1;
  172. }
  173. /** Read the client's nonce out of <b>conn</b>, setup the safe-cookie
  174. * crypto, and then send our own hash and nonce to the client
  175. *
  176. * Return -1 if there was an error; return 0 if we need more data in
  177. * <b>conn</b>, and return 1 if we successfully retrieved the
  178. * client's nonce and sent our own. */
  179. static int
  180. connection_ext_or_auth_handle_client_nonce(connection_t *conn)
  181. {
  182. char server_hash[EXT_OR_PORT_AUTH_HASH_LEN] = {0};
  183. char client_nonce[EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  184. char server_nonce[EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  185. char reply[EXT_OR_PORT_AUTH_COOKIE_LEN+EXT_OR_PORT_AUTH_NONCE_LEN] = {0};
  186. if (!ext_or_auth_cookie_is_set) { /* this should not happen */
  187. log_warn(LD_BUG, "Extended ORPort authentication cookie was not set. "
  188. "That's weird since we should have done that on startup. "
  189. "This might be a Tor bug, please file a bug report. ");
  190. return -1;
  191. }
  192. if (connection_get_inbuf_len(conn) < EXT_OR_PORT_AUTH_NONCE_LEN)
  193. return 0;
  194. if (connection_fetch_from_buf(client_nonce,
  195. EXT_OR_PORT_AUTH_NONCE_LEN, conn) < 0)
  196. return -1;
  197. /* Get our nonce */
  198. if (crypto_rand(server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN) < 0)
  199. return -1;
  200. { /* set up macs */
  201. size_t hmac_s_msg_len = strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST) +
  202. 2*EXT_OR_PORT_AUTH_NONCE_LEN;
  203. size_t hmac_c_msg_len = strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST) +
  204. 2*EXT_OR_PORT_AUTH_NONCE_LEN;
  205. char *hmac_s_msg = tor_malloc_zero(hmac_s_msg_len);
  206. char *hmac_c_msg = tor_malloc_zero(hmac_c_msg_len);
  207. char *correct_client_hash = tor_malloc_zero(EXT_OR_PORT_AUTH_HASH_LEN);
  208. memcpy(hmac_s_msg,
  209. EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST,
  210. strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST));
  211. memcpy(hmac_s_msg + strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST),
  212. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  213. memcpy(hmac_s_msg + strlen(EXT_OR_PORT_AUTH_SERVER_TO_CLIENT_CONST) +
  214. EXT_OR_PORT_AUTH_NONCE_LEN,
  215. server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  216. memcpy(hmac_c_msg,
  217. EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST,
  218. strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST));
  219. memcpy(hmac_c_msg + strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST),
  220. client_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  221. memcpy(hmac_c_msg + strlen(EXT_OR_PORT_AUTH_CLIENT_TO_SERVER_CONST) +
  222. EXT_OR_PORT_AUTH_NONCE_LEN,
  223. server_nonce, EXT_OR_PORT_AUTH_NONCE_LEN);
  224. crypto_hmac_sha256(server_hash,
  225. ext_or_auth_cookie,
  226. EXT_OR_PORT_AUTH_COOKIE_LEN,
  227. hmac_s_msg,
  228. hmac_s_msg_len);
  229. crypto_hmac_sha256(correct_client_hash,
  230. ext_or_auth_cookie,
  231. EXT_OR_PORT_AUTH_COOKIE_LEN,
  232. hmac_c_msg,
  233. hmac_c_msg_len);
  234. /* Store the client hash we generated. We will need to compare it
  235. with the hash sent by the client. */
  236. TO_OR_CONN(conn)->ext_or_auth_correct_client_hash = correct_client_hash;
  237. tor_free(hmac_s_msg);
  238. tor_free(hmac_c_msg);
  239. }
  240. { /* debug logging */ /* XXX disable this codepath if not logging on debug?*/
  241. char server_hash_encoded[(2*EXT_OR_PORT_AUTH_HASH_LEN) + 1];
  242. char server_nonce_encoded[(2*EXT_OR_PORT_AUTH_NONCE_LEN) + 1];
  243. char client_nonce_encoded[(2*EXT_OR_PORT_AUTH_NONCE_LEN) + 1];
  244. base16_encode(server_hash_encoded, sizeof(server_hash_encoded),
  245. server_hash, sizeof(server_hash));
  246. base16_encode(server_nonce_encoded, sizeof(server_nonce_encoded),
  247. server_nonce, sizeof(server_nonce));
  248. base16_encode(client_nonce_encoded, sizeof(client_nonce_encoded),
  249. client_nonce, sizeof(client_nonce));
  250. log_debug(LD_GENERAL,
  251. "server_hash: '%s'\nserver_nonce: '%s'\nclient_nonce: '%s'",
  252. server_hash_encoded, server_nonce_encoded, client_nonce_encoded);
  253. }
  254. { /* write reply: (server_hash, server_nonce) */
  255. memcpy(reply, server_hash, EXT_OR_PORT_AUTH_HASH_LEN);
  256. memcpy(reply + EXT_OR_PORT_AUTH_HASH_LEN, server_nonce,
  257. EXT_OR_PORT_AUTH_NONCE_LEN);
  258. connection_write_to_buf(reply, sizeof(reply), conn);
  259. }
  260. log_debug(LD_GENERAL, "Got client nonce, and sent our own nonce and hash.");
  261. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH;
  262. return 1;
  263. }
  264. #define connection_ext_or_auth_send_result_success(c) \
  265. connection_ext_or_auth_send_result(c, 1)
  266. #define connection_ext_or_auth_send_result_fail(c) \
  267. connection_ext_or_auth_send_result(c, 0)
  268. /** Send authentication results to <b>conn</b>. Successful results if
  269. * <b>success</b> is set; failure results otherwise. */
  270. static void
  271. connection_ext_or_auth_send_result(connection_t *conn, int success)
  272. {
  273. if (success)
  274. connection_write_to_buf("\x01", 1, conn);
  275. else
  276. connection_write_to_buf("\x00", 1, conn);
  277. }
  278. /** Receive the client's hash from <b>conn</b>, validate that it's
  279. * correct, and then send the authentication results to the client.
  280. *
  281. * Return -1 if there was an error during validation; return 0 if we
  282. * need more data in <b>conn</b>, and return 1 if we successfully
  283. * validated the client's hash and sent a happy authentication
  284. * result. */
  285. static int
  286. connection_ext_or_auth_handle_client_hash(connection_t *conn)
  287. {
  288. char provided_client_hash[EXT_OR_PORT_AUTH_HASH_LEN] = {0};
  289. if (connection_get_inbuf_len(conn) < EXT_OR_PORT_AUTH_HASH_LEN)
  290. return 0;
  291. if (connection_fetch_from_buf(provided_client_hash,
  292. EXT_OR_PORT_AUTH_HASH_LEN, conn) < 0)
  293. return -1;
  294. if (tor_memneq(TO_OR_CONN(conn)->ext_or_auth_correct_client_hash,
  295. provided_client_hash, EXT_OR_PORT_AUTH_HASH_LEN)) {
  296. log_warn(LD_GENERAL, "Incorrect client hash. Authentication failed.");
  297. connection_ext_or_auth_send_result_fail(conn);
  298. return -1;
  299. }
  300. log_debug(LD_GENERAL, "Got client's hash and it was legit.");
  301. /* send positive auth result */
  302. connection_ext_or_auth_send_result_success(conn);
  303. conn->state = EXT_OR_CONN_STATE_OPEN;
  304. return 1;
  305. }
  306. /** Handle data from <b>or_conn</b> received on Extended ORPort.
  307. * Return -1 on error. 0 on unsufficient data. 1 on correct. */
  308. static int
  309. connection_ext_or_auth_process_inbuf(or_connection_t *or_conn)
  310. {
  311. connection_t *conn = TO_CONN(or_conn);
  312. switch (conn->state) { /* Functionify */
  313. case EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE:
  314. return connection_ext_or_auth_neg_auth_type(conn);
  315. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_NONCE:
  316. return connection_ext_or_auth_handle_client_nonce(conn);
  317. case EXT_OR_CONN_STATE_AUTH_WAIT_CLIENT_HASH:
  318. return connection_ext_or_auth_handle_client_hash(conn);
  319. default:
  320. log_warn(LD_BUG, "Encountered unexpected connection state %d while trying "
  321. "to process Extended ORPort authentication data.", conn->state);
  322. return -1;
  323. }
  324. }
  325. /** Extended ORPort commands (Transport-to-Bridge) */
  326. #define EXT_OR_CMD_TB_DONE 0x0000
  327. #define EXT_OR_CMD_TB_USERADDR 0x0001
  328. #define EXT_OR_CMD_TB_TRANSPORT 0x0002
  329. /** Extended ORPort commands (Bridge-to-Transport) */
  330. #define EXT_OR_CMD_BT_OKAY 0x1000
  331. #define EXT_OR_CMD_BT_DENY 0x1001
  332. #define EXT_OR_CMD_BT_CONTROL 0x1002
  333. /** Process a USERADDR command from the Extended
  334. * ORPort. <b>payload</b> is a payload of size <b>len</b>.
  335. *
  336. * If the USERADDR command was well formed, change the address of
  337. * <b>conn</b> to the address on the USERADDR command.
  338. *
  339. * Return 0 on success and -1 on error. */
  340. static int
  341. connection_ext_or_handle_cmd_useraddr(connection_t *conn,
  342. const char *payload, uint16_t len)
  343. {
  344. /* Copy address string. */
  345. tor_addr_t addr;
  346. uint16_t port;
  347. char *addr_str;
  348. char *address_part=NULL;
  349. int res;
  350. addr_str = tor_malloc(len + 1);
  351. memcpy(addr_str, payload, len);
  352. addr_str[len] = 0;
  353. res = tor_addr_port_split(LOG_INFO, addr_str, &address_part, &port);
  354. tor_free(addr_str);
  355. if (res<0)
  356. return -1;
  357. res = tor_addr_parse(&addr, address_part);
  358. tor_free(address_part);
  359. if (res<0)
  360. return -1;
  361. { /* do some logging */
  362. char *old_address = tor_dup_addr(&conn->addr);
  363. char *new_address = tor_dup_addr(&addr);
  364. log_debug(LD_NET, "Received USERADDR."
  365. "We rewrite our address from '%s:%u' to '%s:%u'.",
  366. safe_str(old_address), conn->port, safe_str(new_address), port);
  367. tor_free(old_address);
  368. tor_free(new_address);
  369. }
  370. /* record the address */
  371. tor_addr_copy(&conn->addr, &addr);
  372. conn->port = port;
  373. return 0;
  374. }
  375. /** Process a TRANSPORT command from the Extended
  376. * ORPort. <b>payload</b> is a payload of size <b>len</b>.
  377. *
  378. * If the TRANSPORT command was well formed, register the name of the
  379. * transport on <b>conn</b>.
  380. *
  381. * Return 0 on success and -1 on error. */
  382. static int
  383. connection_ext_or_handle_cmd_transport(or_connection_t *conn,
  384. const char *payload, uint16_t len)
  385. {
  386. char *transport_str = tor_malloc(len + 1); /* NUL-terminate the string */
  387. memcpy(transport_str, payload, len);
  388. transport_str[len] = 0;
  389. /* Transport names MUST be C-identifiers. */
  390. if (!string_is_C_identifier(transport_str)) {
  391. tor_free(transport_str);
  392. return -1;
  393. }
  394. /* If ext_or_transport is already occupied (because the PT sent two
  395. * TRANSPORT commands), deallocate the old name and keep the new
  396. * one */
  397. if (conn->ext_or_transport)
  398. tor_free(conn->ext_or_transport);
  399. conn->ext_or_transport = transport_str;
  400. return 0;
  401. }
  402. /** Process Extended ORPort messages from <b>or_conn</b>. */
  403. int
  404. connection_ext_or_process_inbuf(or_connection_t *or_conn)
  405. {
  406. connection_t *conn = TO_CONN(or_conn);
  407. ext_or_cmd_t *command;
  408. int r;
  409. /* If we are still in the authentication stage, process traffic as
  410. authentication data: */
  411. while (conn->state <= EXT_OR_CONN_STATE_AUTH_MAX) {
  412. log_debug(LD_GENERAL, "Got Extended ORPort authentication data (%u).",
  413. (unsigned int) connection_get_inbuf_len(conn));
  414. r = connection_ext_or_auth_process_inbuf(or_conn);
  415. if (r < 0) {
  416. connection_mark_for_close(conn);
  417. return -1;
  418. } else if (r == 0) {
  419. return 0;
  420. }
  421. /* if r > 0, loop and process more data (if any). */
  422. }
  423. while (1) {
  424. log_debug(LD_GENERAL, "Got Extended ORPort data.");
  425. command = NULL;
  426. r = connection_fetch_ext_or_cmd_from_buf(conn, &command);
  427. if (r < 0)
  428. goto err;
  429. else if (r == 0)
  430. return 0; /* need to wait for more data */
  431. /* Got a command! */
  432. tor_assert(command);
  433. if (command->cmd == EXT_OR_CMD_TB_DONE) {
  434. if (connection_get_inbuf_len(conn)) {
  435. /* The inbuf isn't empty; the client is misbehaving. */
  436. goto err;
  437. }
  438. log_debug(LD_NET, "Received DONE.");
  439. /* If the transport proxy did not use the TRANSPORT command to
  440. * specify the transport name, mark this as unknown transport. */
  441. if (!or_conn->ext_or_transport)
  442. or_conn->ext_or_transport = tor_strdup("<?\?>");
  443. connection_write_ext_or_command(conn, EXT_OR_CMD_BT_OKAY, NULL, 0);
  444. /* can't transition immediately; need to flush first. */
  445. conn->state = EXT_OR_CONN_STATE_FLUSHING;
  446. connection_stop_reading(conn);
  447. } else if (command->cmd == EXT_OR_CMD_TB_USERADDR) {
  448. if (connection_ext_or_handle_cmd_useraddr(conn,
  449. command->body, command->len) < 0)
  450. goto err;
  451. } else if (command->cmd == EXT_OR_CMD_TB_TRANSPORT) {
  452. if (connection_ext_or_handle_cmd_transport(or_conn,
  453. command->body, command->len) < 0)
  454. goto err;
  455. } else {
  456. log_notice(LD_NET,"Got Extended ORPort command we don't regognize (%u).",
  457. command->cmd);
  458. }
  459. ext_or_cmd_free(command);
  460. }
  461. return 0;
  462. err:
  463. ext_or_cmd_free(command);
  464. connection_mark_for_close(conn);
  465. return -1;
  466. }
  467. /** <b>conn</b> finished flushing Extended ORPort messages to the
  468. * network, and is now ready to accept OR traffic. This function
  469. * does the transition. */
  470. int
  471. connection_ext_or_finished_flushing(or_connection_t *conn)
  472. {
  473. if (conn->base_.state == EXT_OR_CONN_STATE_FLUSHING) {
  474. connection_start_reading(TO_CONN(conn));
  475. connection_ext_or_transition(conn);
  476. }
  477. return 0;
  478. }
  479. /** Initiate Extended ORPort authentication, by sending the list of
  480. * supported authentication types to the client. */
  481. int
  482. connection_ext_or_start_auth(or_connection_t *or_conn)
  483. {
  484. connection_t *conn = TO_CONN(or_conn);
  485. char authtypes[2] = "\x01\x00"; /* We only support authtype '1' for now. */
  486. log_debug(LD_GENERAL,
  487. "ExtORPort authentication: Sending supported authentication types");
  488. connection_write_to_buf(authtypes, sizeof(authtypes), conn);
  489. conn->state = EXT_OR_CONN_STATE_AUTH_WAIT_AUTH_TYPE;
  490. return 0;
  491. }