ext_orport.c 21 KB

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