or.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /* Copyright (c) 2002 Roger Dingledine. See LICENSE for licensing information */
  2. /* $Id$ */
  3. #ifndef __OR_H
  4. #define __OR_H
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <signal.h>
  10. #include <netdb.h>
  11. #include <ctype.h>
  12. #include <sys/poll.h>
  13. #include <sys/types.h>
  14. #include <sys/fcntl.h>
  15. #include <sys/ioctl.h>
  16. #include <sys/socket.h>
  17. #include <sys/time.h>
  18. #include <netinet/in.h>
  19. #include <arpa/inet.h>
  20. #include <errno.h>
  21. #include <assert.h>
  22. #include <openssl/err.h>
  23. #include <openssl/rsa.h>
  24. #include <openssl/pem.h>
  25. #include <openssl/evp.h>
  26. #include <openssl/rand.h>
  27. #include "../common/cell.h"
  28. #include "../common/config.h"
  29. #include "../common/key.h"
  30. #include "../common/log.h"
  31. #include "../common/ss.h"
  32. #include "../common/version.h"
  33. #define MAXCONNECTIONS 200 /* upper bound on max connections.
  34. can be overridden by config file */
  35. #define MAX_BUF_SIZE (64*1024)
  36. #define DEFAULT_BANDWIDTH_OP 1
  37. #define ACI_TYPE_LOWER 0
  38. #define ACI_TYPE_HIGHER 1
  39. #define ACI_TYPE_BOTH 2
  40. /* bitvector of the roles that we might want to play. You can or (|) them together */
  41. #define ROLE_OR_LISTEN 1
  42. #define ROLE_OR_CONNECT_ALL 2
  43. #define ROLE_OP_LISTEN 4
  44. #define ROLE_AP_LISTEN 8
  45. #define CONN_TYPE_OP_LISTENER 1
  46. #define CONN_TYPE_OP 2
  47. #define CONN_TYPE_OR_LISTENER 3
  48. #define CONN_TYPE_OR 4
  49. #define CONN_TYPE_EXIT 5
  50. #define CONN_TYPE_AP_LISTENER 6
  51. #define CONN_TYPE_AP 7
  52. #define LISTENER_STATE_READY 0
  53. #define OP_CONN_STATE_AWAITING_KEYS 0
  54. #define OP_CONN_STATE_OPEN 1
  55. #if 0
  56. #define OP_CONN_STATE_CLOSE 2 /* flushing the buffer, then will close */
  57. #define OP_CONN_STATE_CLOSE_WAIT 3 /* have sent a destroy, awaiting a confirmation */
  58. #endif
  59. /* how to read these states:
  60. * foo_CONN_STATE_bar_baz:
  61. * "I am acting as a bar, currently in stage baz of talking with a foo."
  62. */
  63. #define OR_CONN_STATE_OP_CONNECTING 0 /* an application proxy wants me to connect to this OR */
  64. #define OR_CONN_STATE_OP_SENDING_KEYS 1
  65. #define OR_CONN_STATE_CLIENT_CONNECTING 2 /* I'm connecting to this OR as an OR */
  66. #define OR_CONN_STATE_CLIENT_SENDING_AUTH 3 /* sending address and info */
  67. #define OR_CONN_STATE_CLIENT_AUTH_WAIT 4 /* have sent address and info, waiting */
  68. #define OR_CONN_STATE_CLIENT_SENDING_NONCE 5 /* sending nonce, last piece of handshake */
  69. #define OR_CONN_STATE_SERVER_AUTH_WAIT 6 /* waiting for address and info */
  70. #define OR_CONN_STATE_SERVER_SENDING_AUTH 7 /* writing auth and nonce */
  71. #define OR_CONN_STATE_SERVER_NONCE_WAIT 8 /* waiting for confirmation of nonce */
  72. #define OR_CONN_STATE_OPEN 9 /* ready to send/receive cells. */
  73. #define EXIT_CONN_STATE_CONNECTING_WAIT 0 /* waiting for standard structure or dest info */
  74. #define EXIT_CONN_STATE_CONNECTING 1
  75. #define EXIT_CONN_STATE_OPEN 2
  76. #if 0
  77. #define EXIT_CONN_STATE_CLOSE 3 /* flushing the buffer, then will close */
  78. #define EXIT_CONN_STATE_CLOSE_WAIT 4 /* have sent a destroy, awaiting a confirmation */
  79. #endif
  80. #define AP_CONN_STATE_SS_WAIT 0
  81. #define AP_CONN_STATE_OR_WAIT 1
  82. #define AP_CONN_STATE_OPEN 2
  83. #define CIRCUIT_STATE_OPEN_WAIT 0 /* receiving/processing the onion */
  84. #define CIRCUIT_STATE_OR_WAIT 1 /* I'm at the beginning of the path, my firsthop is still connecting */
  85. #define CIRCUIT_STATE_OPEN 2 /* onion processed, ready to send data along the connection */
  86. #define CIRCUIT_STATE_CLOSE_WAIT1 3 /* sent two "destroy" signals, waiting for acks */
  87. #define CIRCUIT_STATE_CLOSE_WAIT2 4 /* received one ack, waiting for one more
  88. (or if just one was sent, waiting for that one */
  89. //#define CIRCUIT_STATE_CLOSE 4 /* both acks received, connection is dead */ /* NOT USED */
  90. /* available cipher functions */
  91. #define ONION_CIPHER_IDENTITY 0
  92. #define ONION_CIPHER_DES 1
  93. #define ONION_CIPHER_RC4 2
  94. /* default cipher function */
  95. #define ONION_DEFAULT_CIPHER ONION_CIPHER_DES
  96. typedef uint16_t aci_t;
  97. typedef struct
  98. {
  99. /* Used by all types: */
  100. unsigned char type;
  101. int state;
  102. int s; /* our socket */
  103. int poll_index;
  104. int marked_for_close;
  105. char *inbuf;
  106. size_t inbuflen;
  107. size_t inbuf_datalen;
  108. int inbuf_reached_eof;
  109. char *outbuf;
  110. size_t outbuflen;
  111. size_t outbuf_datalen;
  112. // uint16_t aci; /* anonymous connection identifier */
  113. /* used by OR and OP: */
  114. uint32_t bandwidth; /* connection bandwidth */
  115. int window_sent; /* how many cells can i still send? */
  116. int window_received; /* how many cells do i still expect to receive? */
  117. /* link encryption */
  118. unsigned char f_session_key[8];
  119. unsigned char b_session_key[8];
  120. unsigned char f_session_iv[8];
  121. unsigned char b_session_iv[8];
  122. EVP_CIPHER_CTX f_ctx;
  123. EVP_CIPHER_CTX b_ctx;
  124. // struct timeval lastsend; /* time of last transmission to the client */
  125. // struct timeval interval; /* transmission interval */
  126. uint32_t addr; /* these two uniquely identify a router */
  127. uint16_t port;
  128. /* used by exit and ap: */
  129. ss_t ss; /* standard structure */
  130. int ss_received; /* size of ss, received so far */
  131. char *dest_addr, *dest_port;
  132. uint16_t dest_addr_len, dest_port_len;
  133. uint16_t dest_addr_received, dest_port_received;
  134. /* used by OR, to keep state while connect()ing: Kludge. */
  135. RSA *prkey;
  136. struct sockaddr_in local;
  137. /* link info */
  138. uint32_t min;
  139. uint32_t max;
  140. char *address; /* strdup into this, because free_connection frees it */
  141. RSA *pkey; /* public RSA key for the other side */
  142. char nonce[8];
  143. } connection_t;
  144. /* config stuff we know about the other ORs in the network */
  145. typedef struct
  146. {
  147. char *address;
  148. uint32_t addr;
  149. uint16_t or_port;
  150. uint16_t op_port;
  151. uint16_t ap_port;
  152. RSA *pkey; /* public RSA key */
  153. /* link info */
  154. uint32_t min;
  155. uint32_t max;
  156. // struct timeval min_interval;
  157. /* time when last data was sent to that router */
  158. // struct timeval lastsend;
  159. /* socket */
  160. // int s;
  161. void *next;
  162. } routerinfo_t;
  163. typedef struct
  164. {
  165. unsigned int forwf;
  166. unsigned int backf;
  167. char digest2[20]; /* second SHA output for onion_layer_t.keyseed */
  168. char digest3[20]; /* third SHA output for onion_layer_t.keyseed */
  169. /* IVs */
  170. char f_iv[16];
  171. char b_iv[16];
  172. /* cipher contexts */
  173. EVP_CIPHER_CTX f_ctx;
  174. EVP_CIPHER_CTX b_ctx;
  175. } crypt_path_t;
  176. /* per-anonymous-connection struct */
  177. typedef struct
  178. {
  179. #if 0
  180. uint32_t p_addr; /* all in network order */
  181. uint16_t p_port;
  182. #endif
  183. uint32_t n_addr;
  184. uint16_t n_port;
  185. connection_t *p_conn;
  186. connection_t *n_conn;
  187. aci_t p_aci; /* connection identifiers */
  188. aci_t n_aci;
  189. unsigned char p_f; /* crypto functions */
  190. unsigned char n_f;
  191. unsigned char p_key[16]; /* crypto keys */
  192. unsigned char n_key[16];
  193. unsigned char p_iv[16]; /* initialization vectors */
  194. unsigned char n_iv[16];
  195. EVP_CIPHER_CTX p_ctx; /* cipher context */
  196. EVP_CIPHER_CTX n_ctx;
  197. crypt_path_t **cpath;
  198. size_t cpathlen;
  199. uint32_t expire; /* expiration time for the corresponding onion */
  200. int state;
  201. unsigned char *onion; /* stores the onion when state is CONN_STATE_OPEN_WAIT */
  202. uint32_t onionlen; /* total onion length */
  203. uint32_t recvlen; /* length of the onion so far */
  204. void *next;
  205. } circuit_t;
  206. typedef struct
  207. {
  208. int zero:1;
  209. int version:7;
  210. int backf:4;
  211. int forwf:4;
  212. uint16_t port;
  213. uint32_t addr;
  214. time_t expire;
  215. unsigned char keyseed[16];
  216. } onion_layer_t;
  217. typedef struct
  218. {
  219. time_t expire;
  220. char digest[20]; /* SHA digest of the onion */
  221. void *prev;
  222. void *next;
  223. } tracked_onion_t;
  224. /* all the function prototypes go here */
  225. /********************************* args.c ***************************/
  226. /* print help*/
  227. void print_usage();
  228. /* get command-line arguments */
  229. int getargs(int argc,char *argv[], char *args,char **conf_filename, int *loglevel);
  230. /********************************* buffers.c ***************************/
  231. int buf_new(char **buf, size_t *buflen, size_t *buf_datalen);
  232. void buf_free(char *buf);
  233. int read_to_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen, int *reached_eof);
  234. /* grab from s, put onto buf, return how many bytes read */
  235. int flush_buf(int s, char **buf, size_t *buflen, size_t *buf_datalen);
  236. /* push from buf onto s
  237. * then memmove to front of buf
  238. * return -1 or how many bytes remain on the buf */
  239. int write_to_buf(char *string, size_t string_len,
  240. char **buf, size_t *buflen, size_t *buf_datalen);
  241. /* append string to buf (growing as needed, return -1 if "too big")
  242. * return total number of bytes on the buf
  243. */
  244. int fetch_from_buf(char *string, size_t string_len,
  245. char **buf, size_t *buflen, size_t *buf_datalen);
  246. /* if there is string_len bytes in buf, write them onto string,
  247. * * then memmove buf back (that is, remove them from buf) */
  248. /********************************* cell.c ***************************/
  249. int check_sane_cell(cell_t *cell);
  250. /********************************* circuit.c ***************************/
  251. void circuit_add(circuit_t *circ);
  252. void circuit_remove(circuit_t *circ);
  253. circuit_t *circuit_new(aci_t p_aci, connection_t *p_conn);
  254. /* internal */
  255. aci_t get_unique_aci_by_addr_port(uint32_t addr, uint16_t port, int aci_type);
  256. circuit_t *circuit_get_by_aci_conn(aci_t aci, connection_t *conn);
  257. circuit_t *circuit_get_by_conn(connection_t *conn);
  258. circuit_t *circuit_get_by_naddr_nport(uint32_t naddr, uint16_t nport);
  259. int circuit_deliver_data_cell(cell_t *cell, circuit_t *circ, connection_t *conn, int crypt_type);
  260. int circuit_crypt(circuit_t *circ, char *in, size_t inlen, char crypt_type);
  261. int circuit_init(circuit_t *circ, int aci_type);
  262. void circuit_free(circuit_t *circ);
  263. void circuit_free_cpath(crypt_path_t **cpath, size_t cpathlen);
  264. void circuit_close(circuit_t *circ);
  265. void circuit_about_to_close_connection(connection_t *conn);
  266. /* flush and send destroys for all circuits using conn */
  267. /********************************* command.c ***************************/
  268. void command_process_cell(cell_t *cell, connection_t *conn);
  269. void command_process_create_cell(cell_t *cell, connection_t *conn);
  270. void command_process_data_cell(cell_t *cell, connection_t *conn);
  271. void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  272. /********************************* config.c ***************************/
  273. /* loads the configuration file */
  274. int getconfig(char *filename, config_opt_t *options);
  275. /********************************* connection.c ***************************/
  276. connection_t *connection_new(int type);
  277. void connection_free(connection_t *conn);
  278. int connection_create_listener(RSA *prkey, struct sockaddr_in *local, int type);
  279. int connection_handle_listener_read(connection_t *conn, int new_type, int new_state);
  280. /* start all connections that should be up but aren't */
  281. int retry_all_connections(int role, routerinfo_t **router_array, int rarray_len,
  282. RSA *prkey, uint16_t or_port, uint16_t op_port, uint16_t ap_port);
  283. connection_t *connection_connect_to_router_as_op(routerinfo_t *router, RSA *prkey, uint16_t local_or_port);
  284. int connection_read_to_buf(connection_t *conn);
  285. int connection_fetch_from_buf(char *string, int len, connection_t *conn);
  286. int connection_flush_buf(connection_t *conn);
  287. int connection_write_to_buf(char *string, int len, connection_t *conn);
  288. int connection_send_destroy(aci_t aci, connection_t *conn);
  289. int connection_encrypt_cell_header(cell_t *cellp, connection_t *conn);
  290. int connection_write_cell_to_buf(cell_t *cellp, connection_t *conn);
  291. int connection_process_inbuf(connection_t *conn);
  292. int connection_package_raw_inbuf(connection_t *conn);
  293. int connection_process_cell_from_inbuf(connection_t *conn);
  294. int connection_finished_flushing(connection_t *conn);
  295. /********************************* connection_ap.c ****************************/
  296. int connection_ap_process_inbuf(connection_t *conn);
  297. int ap_handshake_process_ss(connection_t *conn);
  298. int ap_handshake_create_onion(connection_t *conn);
  299. int ap_handshake_establish_circuit(connection_t *conn, unsigned int *route, int routelen, char *onion,
  300. int onionlen, crypt_path_t **cpath);
  301. /* find the circ that's waiting on me, if any, and get it to send its onion */
  302. int ap_handshake_n_conn_open(connection_t *or_conn);
  303. int ap_handshake_send_onion(connection_t *ap_conn, connection_t *or_conn, circuit_t *circ);
  304. int connection_ap_process_data_cell(cell_t *cell, connection_t *conn);
  305. int connection_ap_finished_flushing(connection_t *conn);
  306. int connection_ap_create_listener(RSA *prkey, struct sockaddr_in *local);
  307. int connection_ap_handle_listener_read(connection_t *conn);
  308. /********************************* connection_exit.c ***************************/
  309. int connection_exit_process_inbuf(connection_t *conn);
  310. int connection_exit_package_inbuf(connection_t *conn);
  311. int connection_exit_process_data_cell(cell_t *cell, connection_t *conn);
  312. int connection_exit_finished_flushing(connection_t *conn);
  313. /********************************* connection_op.c ***************************/
  314. int op_handshake_process_keys(connection_t *conn);
  315. int connection_op_process_inbuf(connection_t *conn);
  316. int connection_op_finished_flushing(connection_t *conn);
  317. int connection_op_create_listener(RSA *prkey, struct sockaddr_in *local);
  318. int connection_op_handle_listener_read(connection_t *conn);
  319. /********************************* connection_or.c ***************************/
  320. int connection_or_process_inbuf(connection_t *conn);
  321. int connection_or_finished_flushing(connection_t *conn);
  322. void conn_or_init_crypto(connection_t *conn);
  323. int or_handshake_op_send_keys(connection_t *conn);
  324. int or_handshake_op_finished_sending_keys(connection_t *conn);
  325. int or_handshake_client_process_auth(connection_t *conn);
  326. int or_handshake_client_send_auth(connection_t *conn);
  327. int or_handshake_server_process_auth(connection_t *conn);
  328. int or_handshake_server_process_nonce(connection_t *conn);
  329. connection_t *connect_to_router_as_or(routerinfo_t *router, RSA *prkey, struct sockaddr_in *local);
  330. connection_t *connection_or_connect_as_or(routerinfo_t *router, RSA *prkey, struct sockaddr_in *local);
  331. int connection_or_create_listener(RSA *prkey, struct sockaddr_in *local);
  332. int connection_or_handle_listener_read(connection_t *conn);
  333. /********************************* main.c ***************************/
  334. int connection_add(connection_t *conn);
  335. int connection_remove(connection_t *conn);
  336. void connection_set_poll_socket(connection_t *conn);
  337. connection_t *connection_get_by_addr_port(uint32_t addr, uint16_t port);
  338. connection_t *connection_get_by_type(int type);
  339. routerinfo_t *router_get_by_addr_port(uint32_t addr, uint16_t port);
  340. unsigned int *router_new_route(size_t *rlen);
  341. unsigned char *router_create_onion(unsigned int *route, size_t routelen, size_t *lenp, crypt_path_t **cpathp);
  342. routerinfo_t *router_get_first_in_route(unsigned int *route, size_t routelen);
  343. connection_t *connect_to_router_as_op(routerinfo_t *router);
  344. void connection_watch_events(connection_t *conn, short events);
  345. void check_conn_read(int i);
  346. void check_conn_marked(int i);
  347. void check_conn_write(int i);
  348. void check_conn_hup(int i);
  349. int do_main_loop(void);
  350. int main(int argc, char *argv[]);
  351. /********************************* onion.c ***************************/
  352. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  353. uint32_t remote_addr, uint16_t remote_port);
  354. int process_onion(circuit_t *circ, connection_t *conn);
  355. /* uses a weighted coin with weight cw to choose a route length */
  356. int chooselen(double cw);
  357. /* returns an array of pointers to routent that define a new route through the OR network
  358. * int cw is the coin weight to use when choosing the route
  359. * order of routers is from last to first
  360. */
  361. unsigned int *new_route(double cw, routerinfo_t **rarray, size_t rarray_len, size_t *rlen);
  362. /* creates a new onion from route, stores it and its length into bufp and lenp respectively */
  363. unsigned char *create_onion(routerinfo_t **rarray, size_t rarray_len, unsigned int *route, size_t routelen, size_t *lenp, crypt_path_t **cpathp);
  364. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  365. * DES OFB with the key as defined in the outter layer */
  366. unsigned char *encrypt_onion(onion_layer_t *onion, uint32_t onionlen, RSA *pkey);
  367. /* decrypts the first 128 bytes using RSA and prkey, decrypts the rest with DES OFB with key1 */
  368. unsigned char *decrypt_onion(onion_layer_t *onion, uint32_t onionlen, RSA *prkey);
  369. /* delete first n bytes of the onion and pads the end with n bytes of random data */
  370. void pad_onion(unsigned char *onion, uint32_t onionlen, size_t n);
  371. /* create a new tracked_onion entry */
  372. tracked_onion_t *new_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion);
  373. /* delete a tracked onion entry */
  374. void remove_tracked_onion(tracked_onion_t *to, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion);
  375. /* find a tracked onion in the linked list of tracked onions */
  376. tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t *tracked_onions);
  377. /********************************* routers.c ***************************/
  378. routerinfo_t **getrouters(char *routerfile, size_t *listlenp);
  379. void delete_routerlist(routerinfo_t *list);
  380. /* create an NULL-terminated array of pointers pointing to elements of a router list */
  381. routerinfo_t **make_rarray(routerinfo_t* list, size_t *listlenp);
  382. #endif