connection_or.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. #ifndef USE_TLS
  7. static int or_handshake_op_send_keys(connection_t *conn);
  8. static int or_handshake_op_finished_sending_keys(connection_t *conn);
  9. static int or_handshake_client_process_auth(connection_t *conn);
  10. static int or_handshake_client_send_auth(connection_t *conn);
  11. static int or_handshake_server_process_auth(connection_t *conn);
  12. static int or_handshake_server_process_nonce(connection_t *conn);
  13. static void conn_or_init_crypto(connection_t *conn);
  14. static void connection_or_set_open(connection_t *conn);
  15. #endif
  16. /**************************************************************/
  17. static void cell_pack(char *dest, const cell_t *src) {
  18. *(uint16_t*)dest = htons(src->aci);
  19. *(uint8_t*)(dest+2) = src->command;
  20. *(uint8_t*)(dest+3) = src->length;
  21. *(uint32_t*)(dest+4) = 0; /* Reserved */
  22. memcpy(dest+8, src->payload, CELL_PAYLOAD_SIZE);
  23. }
  24. static void cell_unpack(cell_t *dest, const char *src) {
  25. dest->aci = ntohs(*(uint16_t*)(src));
  26. dest->command = *(uint8_t*)(src+2);
  27. dest->length = *(uint8_t*)(src+3);
  28. dest->seq = ntohl(*(uint32_t*)(src+4));
  29. memcpy(dest->payload, src+8, CELL_PAYLOAD_SIZE);
  30. }
  31. /**************************************************************/
  32. int connection_or_process_inbuf(connection_t *conn) {
  33. assert(conn && conn->type == CONN_TYPE_OR);
  34. if(conn->inbuf_reached_eof) {
  35. log_fn(LOG_DEBUG,"conn reached eof. Closing.");
  36. return -1;
  37. }
  38. #ifdef USE_TLS
  39. if(conn->state != OR_CONN_STATE_OPEN)
  40. return 0; /* don't do anything */
  41. return connection_process_cell_from_inbuf(conn);
  42. #else
  43. // log(LOG_DEBUG,"connection_or_process_inbuf(): state %d.",conn->state);
  44. switch(conn->state) {
  45. case OR_CONN_STATE_CLIENT_AUTH_WAIT:
  46. return or_handshake_client_process_auth(conn);
  47. case OR_CONN_STATE_SERVER_AUTH_WAIT:
  48. return or_handshake_server_process_auth(conn);
  49. case OR_CONN_STATE_SERVER_NONCE_WAIT:
  50. return or_handshake_server_process_nonce(conn);
  51. case OR_CONN_STATE_OPEN:
  52. return connection_process_cell_from_inbuf(conn);
  53. default:
  54. log_fn(LOG_DEBUG,"called in state where I'm writing. Ignoring buf for now.");
  55. }
  56. return 0;
  57. #endif
  58. }
  59. int connection_or_finished_flushing(connection_t *conn) {
  60. int e, len=sizeof(e);
  61. assert(conn && conn->type == CONN_TYPE_OR);
  62. switch(conn->state) {
  63. #ifndef USE_TLS
  64. case OR_CONN_STATE_OP_SENDING_KEYS:
  65. return or_handshake_op_finished_sending_keys(conn);
  66. case OR_CONN_STATE_CLIENT_CONNECTING:
  67. #else
  68. case OR_CONN_STATE_CONNECTING:
  69. #endif
  70. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  71. if(!ERRNO_CONN_EINPROGRESS(errno)){
  72. log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
  73. return -1;
  74. } else {
  75. return 0; /* no change, see if next time is better */
  76. }
  77. }
  78. /* the connect has finished. */
  79. log_fn(LOG_DEBUG,"OR connect() to router %s:%u finished.",
  80. conn->address,conn->port);
  81. #ifdef USE_TLS
  82. if(connection_tls_start_handshake(conn, 0) < 0)
  83. return -1;
  84. #else
  85. if(options.OnionRouter)
  86. return or_handshake_client_send_auth(conn);
  87. else
  88. return or_handshake_op_send_keys(conn);
  89. case OR_CONN_STATE_CLIENT_SENDING_AUTH:
  90. log_fn(LOG_DEBUG,"client finished sending auth.");
  91. conn->state = OR_CONN_STATE_CLIENT_AUTH_WAIT;
  92. connection_watch_events(conn, POLLIN);
  93. return 0;
  94. case OR_CONN_STATE_CLIENT_SENDING_NONCE:
  95. log_fn(LOG_DEBUG,"client finished sending nonce.");
  96. conn_or_init_crypto(conn);
  97. connection_or_set_open(conn);
  98. return connection_process_inbuf(conn); /* in case there's anything waiting on it */
  99. case OR_CONN_STATE_SERVER_SENDING_AUTH:
  100. log_fn(LOG_DEBUG,"server finished sending auth.");
  101. conn->state = OR_CONN_STATE_SERVER_NONCE_WAIT;
  102. connection_watch_events(conn, POLLIN);
  103. return 0;
  104. #endif
  105. case OR_CONN_STATE_OPEN:
  106. connection_stop_writing(conn);
  107. return 0;
  108. default:
  109. log_fn(LOG_ERR,"BUG: called in unexpected state.");
  110. return 0;
  111. }
  112. }
  113. /*********************/
  114. connection_t *connection_or_connect(routerinfo_t *router) {
  115. connection_t *conn;
  116. assert(router);
  117. if(router_is_me(router->addr, router->or_port)) {
  118. /* this is me! don't connect to me. */
  119. log(LOG_DEBUG,"connection_or_connect(): This is me. Skipping.");
  120. return NULL;
  121. }
  122. /* this function should never be called if we're already connected to router, but */
  123. /* check first to be sure */
  124. conn = connection_exact_get_by_addr_port(router->addr,router->or_port);
  125. if(conn)
  126. return conn;
  127. conn = connection_new(CONN_TYPE_OR);
  128. if(!conn) {
  129. return NULL;
  130. }
  131. /* set up conn so it's got all the data we need to remember */
  132. conn->addr = router->addr;
  133. conn->port = router->or_port;
  134. conn->bandwidth = router->bandwidth;
  135. conn->pkey = crypto_pk_dup_key(router->pkey);
  136. conn->address = strdup(router->address);
  137. if(connection_add(conn) < 0) { /* no space, forget it */
  138. connection_free(conn);
  139. return NULL;
  140. }
  141. switch(connection_connect(conn, router->address, router->addr, router->or_port)) {
  142. case -1:
  143. connection_remove(conn);
  144. connection_free(conn);
  145. return NULL;
  146. case 0:
  147. connection_set_poll_socket(conn);
  148. connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
  149. /* writable indicates finish, readable indicates broken link,
  150. error indicates broken link on windows */
  151. #ifdef USE_TLS
  152. conn->state = OR_CONN_STATE_CONNECTING;
  153. #else
  154. conn->state = OR_CONN_STATE_CLIENT_CONNECTING;
  155. #endif
  156. return conn;
  157. /* case 1: fall through */
  158. }
  159. connection_set_poll_socket(conn);
  160. #ifdef USE_TLS
  161. if(connection_tls_start_handshake(conn, 0) >= 0)
  162. return conn;
  163. #else
  164. if((options.OnionRouter && or_handshake_client_send_auth(conn) >= 0) ||
  165. (!options.OnionRouter && or_handshake_op_send_keys(conn) >= 0))
  166. return conn; /* success! */
  167. #endif
  168. /* failure */
  169. connection_remove(conn);
  170. connection_free(conn);
  171. return NULL;
  172. }
  173. /* ********************************** */
  174. #ifndef USE_TLS
  175. /* Helper functions to implement handshaking */
  176. #define FLAGS_LEN 2
  177. #define KEY_LEN 16
  178. #define ADDR_LEN 4
  179. #define PORT_LEN 2
  180. #define PKEY_LEN 128
  181. static int
  182. or_handshake_op_send_keys(connection_t *conn) {
  183. unsigned char message[FLAGS_LEN + KEY_LEN + KEY_LEN];
  184. unsigned char cipher[PKEY_LEN];
  185. int retval;
  186. assert(conn && conn->type == CONN_TYPE_OR);
  187. conn->bandwidth = DEFAULT_BANDWIDTH_OP;
  188. /* generate random keys */
  189. if(crypto_cipher_generate_key(conn->f_crypto) ||
  190. crypto_cipher_generate_key(conn->b_crypto)) {
  191. log(LOG_ERR,"Cannot generate a secure symmetric key.");
  192. return -1;
  193. }
  194. log(LOG_DEBUG,"or_handshake_op_send_keys() : Generated symmetric keys.");
  195. /* compose the message */
  196. *(uint16_t *)(message) = htons(HANDSHAKE_AS_OP);
  197. memcpy((void *)(message+FLAGS_LEN),
  198. (void *)crypto_cipher_get_key(conn->f_crypto), 16);
  199. memcpy((void *)(message+FLAGS_LEN+KEY_LEN),
  200. (void *)crypto_cipher_get_key(conn->b_crypto), 16);
  201. /* encrypt with RSA */
  202. if(crypto_pk_public_encrypt(conn->pkey, message, sizeof(message), cipher, RSA_PKCS1_PADDING) < 0) {
  203. log(LOG_ERR,"or_handshake_op_send_keys(): Public key encryption failed.");
  204. return -1;
  205. }
  206. log(LOG_DEBUG,"or_handshake_op_send_keys() : Encrypted authentication message.");
  207. /* send message */
  208. if(connection_write_to_buf(cipher, PKEY_LEN, conn) < 0) {
  209. log(LOG_DEBUG,"or_handshake_op_send_keys(): my outbuf is full. Oops.");
  210. return -1;
  211. }
  212. retval = connection_flush_buf(conn);
  213. if(retval < 0) {
  214. log(LOG_DEBUG,"or_handshake_op_send_keys(): bad socket while flushing.");
  215. return -1;
  216. }
  217. if(retval > 0) {
  218. /* still stuff on the buffer. */
  219. conn->state = OR_CONN_STATE_OP_SENDING_KEYS;
  220. connection_watch_events(conn, POLLOUT | POLLIN);
  221. return 0;
  222. }
  223. /* it finished sending */
  224. log(LOG_DEBUG,"or_handshake_op_send_keys(): Finished sending authentication message.");
  225. return or_handshake_op_finished_sending_keys(conn);
  226. }
  227. static int
  228. or_handshake_op_finished_sending_keys(connection_t *conn) {
  229. /* do crypto initialization, etc */
  230. conn_or_init_crypto(conn);
  231. connection_or_set_open(conn);
  232. circuit_n_conn_open(conn); /* send the pending onion(s) */
  233. return 0;
  234. }
  235. static int
  236. or_handshake_client_send_auth(connection_t *conn) {
  237. int retval;
  238. char buf[FLAGS_LEN+ADDR_LEN+PORT_LEN+ADDR_LEN+
  239. PORT_LEN+KEY_LEN+KEY_LEN];
  240. char cipher[PKEY_LEN];
  241. struct sockaddr_in me; /* my router identity */
  242. assert(conn);
  243. if(learn_my_address(&me) < 0)
  244. return -1;
  245. /* generate random keys */
  246. if(crypto_cipher_generate_key(conn->f_crypto) ||
  247. crypto_cipher_generate_key(conn->b_crypto)) {
  248. log(LOG_ERR,"Cannot generate a secure symmetric key.");
  249. return -1;
  250. }
  251. log(LOG_DEBUG,"or_handshake_client_send_auth() : Generated symmetric keys.");
  252. /* generate first message */
  253. *(uint16_t*)buf = htons(HANDSHAKE_AS_OR);
  254. *(uint32_t*)(buf+FLAGS_LEN) = me.sin_addr.s_addr; /* local address, network order */
  255. *(uint16_t*)(buf+FLAGS_LEN+ADDR_LEN) = me.sin_port; /* local port, network order */
  256. *(uint32_t*)(buf+FLAGS_LEN+ADDR_LEN+PORT_LEN) = htonl(conn->addr); /* remote address */
  257. *(uint16_t*)(buf+FLAGS_LEN+ADDR_LEN+PORT_LEN+ADDR_LEN) = htons(conn->port); /* remote port */
  258. memcpy(buf+FLAGS_LEN+ADDR_LEN+PORT_LEN+ADDR_LEN+PORT_LEN,
  259. crypto_cipher_get_key(conn->f_crypto),16); /* keys */
  260. memcpy(buf+FLAGS_LEN+ADDR_LEN+PORT_LEN+ADDR_LEN+PORT_LEN+KEY_LEN,
  261. crypto_cipher_get_key(conn->b_crypto),16);
  262. log(LOG_DEBUG,"or_handshake_client_send_auth() : Generated first authentication message.");
  263. /* encrypt message */
  264. retval = crypto_pk_public_encrypt(conn->pkey, buf, sizeof(buf), cipher,RSA_PKCS1_PADDING);
  265. if (retval == -1) /* error */
  266. {
  267. log(LOG_ERR,"Public-key encryption failed during authentication to %s:%u.",conn->address,conn->port);
  268. log(LOG_DEBUG,"or_handshake_client_send_auth() : Reason : %s.",crypto_perror());
  269. return -1;
  270. }
  271. log(LOG_DEBUG,"or_handshake_client_send_auth() : Encrypted authentication message.");
  272. /* send message */
  273. if(connection_write_to_buf(cipher, PKEY_LEN, conn) < 0) {
  274. log(LOG_DEBUG,"or_handshake_client_send_auth(): my outbuf is full. Oops.");
  275. return -1;
  276. }
  277. retval = connection_flush_buf(conn);
  278. if(retval < 0) {
  279. log(LOG_DEBUG,"or_handshake_client_send_auth(): bad socket while flushing.");
  280. return -1;
  281. }
  282. if(retval > 0) {
  283. /* still stuff on the buffer. */
  284. conn->state = OR_CONN_STATE_CLIENT_SENDING_AUTH;
  285. connection_watch_events(conn, POLLOUT | POLLIN);
  286. return 0;
  287. }
  288. /* it finished sending */
  289. log(LOG_DEBUG,"or_handshake_client_send_auth(): Finished sending authentication message.");
  290. conn->state = OR_CONN_STATE_CLIENT_AUTH_WAIT;
  291. connection_watch_events(conn, POLLIN);
  292. return 0;
  293. }
  294. static int
  295. or_handshake_client_process_auth(connection_t *conn) {
  296. char buf[128]; /* only 52 of this is expected to be used */
  297. char cipher[128];
  298. int retval;
  299. struct sockaddr_in me; /* my router identity */
  300. assert(conn);
  301. if(learn_my_address(&me) < 0)
  302. return -1;
  303. if(conn->inbuf_datalen < 128) /* entire response available? */
  304. return 0; /* not yet */
  305. connection_fetch_from_buf(cipher,128,conn);
  306. log(LOG_DEBUG,"or_handshake_client_process_auth() : Received auth.");
  307. /* decrypt response */
  308. retval = crypto_pk_private_decrypt(get_privatekey(), cipher, 128, buf, RSA_PKCS1_PADDING);
  309. if (retval == -1)
  310. {
  311. log(LOG_ERR,"Public-key decryption failed during authentication to %s:%u.",
  312. conn->address,conn->port);
  313. log(LOG_DEBUG,"or_handshake_client_process_auth() : Reason : %s.",
  314. crypto_perror());
  315. return -1;
  316. }
  317. else if (retval != 52)
  318. {
  319. log(LOG_ERR,"client_process_auth: incorrect response from router %s:%u.",
  320. conn->address,conn->port);
  321. return -1;
  322. }
  323. log(LOG_DEBUG,"or_handshake_client_process_auth() : Decrypted response.");
  324. /* check validity */
  325. if ( (*(uint32_t*)buf != me.sin_addr.s_addr) || /* local address, network order */
  326. (*(uint16_t*)(buf+4) != me.sin_port) || /* local port, network order */
  327. (ntohl(*(uint32_t*)(buf+6)) != conn->addr) || /* remote address */
  328. (ntohs(*(uint16_t*)(buf+10)) != conn->port) ) { /* remote port */
  329. log(LOG_ERR,"client_process_auth: Router %s:%u: bad address info.", conn->address,conn->port);
  330. return -1;
  331. }
  332. if ( (memcmp(crypto_cipher_get_key(conn->f_crypto), buf+12, 16)) ||/* keys */
  333. (memcmp(crypto_cipher_get_key(conn->b_crypto), buf+28, 16)) ) {
  334. log(LOG_ERR,"client_process_auth: Router %s:%u: bad key info.",conn->address,conn->port);
  335. return -1;
  336. }
  337. log(LOG_DEBUG,"or_handshake_client_process_auth() : Response valid.");
  338. /* reply is just local addr/port, remote addr/port, nonce */
  339. memcpy(buf+12, buf+44, 8);
  340. /* encrypt reply */
  341. retval = crypto_pk_public_encrypt(conn->pkey, buf, 20, cipher,RSA_PKCS1_PADDING);
  342. if (retval == -1) /* error */
  343. {
  344. log(LOG_ERR,"Public-key encryption failed during authentication to %s:%u.",conn->address,conn->port);
  345. log(LOG_DEBUG,"or_handshake_client_process_auth() : Reason : %s.",crypto_perror());
  346. return -1;
  347. }
  348. /* send the message */
  349. if(connection_write_to_buf(cipher, 128, conn) < 0) {
  350. log(LOG_DEBUG,"or_handshake_client_process_auth(): my outbuf is full. Oops.");
  351. return -1;
  352. }
  353. retval = connection_flush_buf(conn);
  354. if(retval < 0) {
  355. log(LOG_DEBUG,"or_handshake_client_process_auth(): bad socket while flushing.");
  356. return -1;
  357. }
  358. if(retval > 0) {
  359. /* still stuff on the buffer. */
  360. conn->state = OR_CONN_STATE_CLIENT_SENDING_NONCE;
  361. connection_watch_events(conn, POLLOUT | POLLIN);
  362. /* return(connection_process_inbuf(conn)); process the rest of the inbuf */
  363. return 0;
  364. }
  365. /* it finished sending */
  366. log(LOG_DEBUG,"or_handshake_client_process_auth(): Finished sending nonce.");
  367. conn_or_init_crypto(conn);
  368. connection_or_set_open(conn);
  369. return connection_process_inbuf(conn); /* process the rest of the inbuf */
  370. }
  371. /* auth handshake, as performed by OR *receiving* the connection */
  372. static int or_handshake_server_process_auth(connection_t *conn) {
  373. int retval;
  374. char buf[128]; /* 50 of this is expected to be used for OR, 38 for OP */
  375. char cipher[128];
  376. unsigned char iv[16];
  377. uint32_t addr;
  378. uint16_t port;
  379. routerinfo_t *router;
  380. assert(conn);
  381. log(LOG_DEBUG,"or_handshake_server_process_auth() entered.");
  382. if(conn->inbuf_datalen < 128) /* entire response available? */
  383. return 0; /* not yet */
  384. connection_fetch_from_buf(cipher,128,conn);
  385. log(LOG_DEBUG,"or_handshake_server_process_auth() : Received auth.");
  386. /* decrypt response */
  387. retval = crypto_pk_private_decrypt(get_privatekey(), cipher, 128, buf, RSA_PKCS1_PADDING);
  388. if (retval == -1) {
  389. log(LOG_ERR,"or_handshake_server_process_auth: Public-key decryption failed.");
  390. log(LOG_DEBUG,"or_handshake_server_process_auth() : Reason : %s.",
  391. crypto_perror());
  392. return -1;
  393. }
  394. if (retval == 46) {
  395. log(LOG_DEBUG,"or_handshake_server_process_auth(): Decrypted OR-style auth message.");
  396. if(ntohs(*(uint16_t*)buf) != HANDSHAKE_AS_OR) {
  397. log(LOG_DEBUG,"or_handshake_server_process_auth(): ...but wasn't labelled OR. Dropping.");
  398. return -1;
  399. }
  400. /* identify the router */
  401. addr = ntohl(*(uint32_t*)(buf+2)); /* save the IP address */
  402. port = ntohs(*(uint16_t*)(buf+6)); /* save the port */
  403. router = router_get_by_addr_port(addr,port);
  404. if (!router) {
  405. log(LOG_DEBUG,"or_handshake_server_process_auth() : unknown router '%s:%d'. Will drop.", conn->address, port);
  406. return -1;
  407. }
  408. log(LOG_DEBUG,"or_handshake_server_process_auth() : Router identified as %s:%u.",
  409. router->address,router->or_port);
  410. if(connection_exact_get_by_addr_port(addr,port)) {
  411. log(LOG_DEBUG,"or_handshake_server_process_auth(): That router is already connected. Dropping.");
  412. return -1;
  413. }
  414. /* save keys */
  415. crypto_cipher_set_key(conn->b_crypto,buf+14);
  416. crypto_cipher_set_key(conn->f_crypto,buf+30);
  417. conn->bandwidth = router->bandwidth;
  418. /* copy all relevant info to conn */
  419. conn->addr = router->addr, conn->port = router->or_port;
  420. conn->pkey = crypto_pk_dup_key(router->pkey);
  421. if(conn->address)
  422. free(conn->address);
  423. conn->address = strdup(router->address);
  424. /* generate a nonce */
  425. retval = crypto_rand(8, conn->nonce);
  426. if (retval) { /* error */
  427. log(LOG_ERR,"Cannot generate a nonce.");
  428. return -1;
  429. }
  430. log(LOG_DEBUG,"or_handshake_server_process_auth(): Nonce generated.");
  431. memmove(buf, buf+2, 44);
  432. memcpy(buf+44,conn->nonce,8); /* append the nonce to the end of the message */
  433. /* encrypt message */
  434. retval = crypto_pk_public_encrypt(conn->pkey, buf, 52, cipher,RSA_PKCS1_PADDING);
  435. if (retval == -1) { /* error */
  436. log(LOG_ERR,"Public-key encryption failed during authentication to %s:%u.",conn->address,conn->port);
  437. log(LOG_DEBUG,"or_handshake_server_process_auth() : Reason : %s.",crypto_perror());
  438. return -1;
  439. }
  440. log(LOG_DEBUG,"or_handshake_server_process_auth() : Reply encrypted.");
  441. /* send message */
  442. if(connection_write_to_buf(cipher, 128, conn) < 0) {
  443. log(LOG_DEBUG,"or_handshake_server_process_auth(): my outbuf is full. Oops.");
  444. return -1;
  445. }
  446. retval = connection_flush_buf(conn);
  447. if(retval < 0) {
  448. log(LOG_DEBUG,"or_handshake_server_process_auth(): bad socket while flushing.");
  449. return -1;
  450. }
  451. if(retval > 0) {
  452. /* still stuff on the buffer. */
  453. conn->state = OR_CONN_STATE_SERVER_SENDING_AUTH;
  454. connection_watch_events(conn, POLLOUT | POLLIN);
  455. return 0;
  456. }
  457. /* it finished sending */
  458. log(LOG_DEBUG,"or_handshake_server_process_auth(): Finished sending auth.");
  459. conn->state = OR_CONN_STATE_SERVER_NONCE_WAIT;
  460. connection_watch_events(conn, POLLIN);
  461. return 0;
  462. }
  463. if(retval == 34) {
  464. log(LOG_DEBUG,"or_handshake_server_process_auth(): Decrypted OP-style auth message.");
  465. if(ntohs(*(uint16_t*)buf) != HANDSHAKE_AS_OP) {
  466. log(LOG_DEBUG,"or_handshake_server_process_auth(): ...but wasn't labelled OP. Dropping.");
  467. return -1;
  468. }
  469. crypto_cipher_set_key(conn->b_crypto, buf+2);
  470. crypto_cipher_set_key(conn->f_crypto, buf+18);
  471. memset(iv, 0, 16);
  472. crypto_cipher_set_iv(conn->b_crypto, iv);
  473. crypto_cipher_set_iv(conn->f_crypto, iv);
  474. crypto_cipher_encrypt_init_cipher(conn->b_crypto);
  475. crypto_cipher_decrypt_init_cipher(conn->f_crypto);
  476. conn->state = OR_CONN_STATE_OPEN;
  477. connection_watch_events(conn, POLLIN);
  478. return connection_process_inbuf(conn); /* in case they sent some cells along with the keys */
  479. }
  480. log(LOG_ERR,"or_handshake_server_process_auth(): received an incorrect authentication request.");
  481. return -1;
  482. }
  483. static int
  484. or_handshake_server_process_nonce(connection_t *conn) {
  485. char buf[128];
  486. char cipher[128];
  487. int retval;
  488. struct sockaddr_in me; /* my router identity */
  489. assert(conn);
  490. if(learn_my_address(&me) < 0)
  491. return -1;
  492. if(conn->inbuf_datalen < 128) /* entire response available? */
  493. return 0; /* not yet */
  494. connection_fetch_from_buf(cipher,128,conn);
  495. log(LOG_DEBUG,"or_handshake_server_process_nonce() : Received auth.");
  496. /* decrypt response */
  497. retval = crypto_pk_private_decrypt(get_privatekey(), cipher, 128, buf,RSA_PKCS1_PADDING);
  498. if (retval == -1)
  499. {
  500. log(LOG_ERR,"Public-key decryption failed during authentication to %s:%u.",
  501. conn->address,conn->port);
  502. log(LOG_DEBUG,"or_handshake_server_process_nonce() : Reason : %s.",
  503. crypto_perror());
  504. return -1;
  505. }
  506. else if (retval != 20)
  507. {
  508. log(LOG_ERR,"server_process_nonce: incorrect response from router %s:%u.",
  509. conn->address,conn->port);
  510. return -1;
  511. }
  512. log(LOG_DEBUG,"or_handshake_server_process_nonce() : Response decrypted.");
  513. /* check validity */
  514. if ((ntohl(*(uint32_t*)buf) != conn->addr) || /* remote address */
  515. (ntohs(*(uint16_t*)(buf+4)) != conn->port) || /* remote port */
  516. (*(uint32_t*)(buf+6) != me.sin_addr.s_addr) || /* local address, network order */
  517. (*(uint16_t*)(buf+10) != me.sin_port) || /* local port, network order */
  518. (memcmp(conn->nonce,buf+12,8))) /* nonce */
  519. {
  520. log(LOG_ERR,"server_process_nonce: Router %s:%u gave bad response.",conn->address,conn->port);
  521. return -1;
  522. }
  523. log(LOG_DEBUG,"or_handshake_server_process_nonce() : Response valid. Authentication complete.");
  524. conn_or_init_crypto(conn);
  525. connection_or_set_open(conn);
  526. return connection_process_inbuf(conn); /* process the rest of the inbuf */
  527. }
  528. /*********************/
  529. static void
  530. connection_or_set_open(connection_t *conn) {
  531. conn->state = OR_CONN_STATE_OPEN;
  532. directory_set_dirty();
  533. connection_watch_events(conn, POLLIN);
  534. }
  535. static void
  536. conn_or_init_crypto(connection_t *conn) {
  537. unsigned char iv[16];
  538. assert(conn);
  539. memset((void *)iv, 0, 16);
  540. crypto_cipher_set_iv(conn->f_crypto, iv);
  541. crypto_cipher_set_iv(conn->b_crypto, iv);
  542. crypto_cipher_encrypt_init_cipher(conn->f_crypto);
  543. crypto_cipher_decrypt_init_cipher(conn->b_crypto);
  544. /* always encrypt with f, always decrypt with b */
  545. }
  546. #endif
  547. int connection_write_cell_to_buf(const cell_t *cellp, connection_t *conn) {
  548. char networkcell[CELL_NETWORK_SIZE];
  549. char *n = networkcell;
  550. cell_pack(n, cellp);
  551. #ifndef USE_TLS
  552. if(connection_encrypt_cell(n,conn)<0) {
  553. return -1;
  554. }
  555. #endif
  556. return connection_write_to_buf(n, CELL_NETWORK_SIZE, conn);
  557. }
  558. int connection_process_cell_from_inbuf(connection_t *conn) {
  559. /* check if there's a whole cell there.
  560. * * if yes, pull it off, decrypt it if we're not doing TLS, and process it.
  561. * */
  562. #ifndef USE_TLS
  563. char networkcell[CELL_NETWORK_SIZE];
  564. #endif
  565. char buf[CELL_NETWORK_SIZE];
  566. // int x;
  567. cell_t cell;
  568. if(conn->inbuf_datalen < CELL_NETWORK_SIZE) /* entire response available? */
  569. return 0; /* not yet */
  570. #ifdef USE_TLS
  571. connection_fetch_from_buf(buf, CELL_NETWORK_SIZE, conn);
  572. #else
  573. connection_fetch_from_buf(networkcell, CELL_NETWORK_SIZE, conn);
  574. #if 0
  575. printf("Cell header crypttext: ");
  576. for(x=0;x<8;x++) {
  577. printf("%u ",crypted[x]);
  578. }
  579. printf("\n");
  580. #endif
  581. /* decrypt */
  582. if(crypto_cipher_decrypt(conn->b_crypto, networkcell, CELL_NETWORK_SIZE, buf)) {
  583. log_fn(LOG_ERR,"Decryption failed, dropping.");
  584. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  585. }
  586. // log_fn(LOG_DEBUG,"Cell decrypted (%d bytes).",outlen);
  587. #if 0
  588. printf("Cell header plaintext: ");
  589. for(x=0;x<8;x++) {
  590. printf("%u ",outbuf[x]);
  591. }
  592. printf("\n");
  593. #endif
  594. #endif
  595. /* retrieve cell info from buf (create the host-order struct from the network-order string) */
  596. cell_unpack(&cell, buf);
  597. // log_fn(LOG_DEBUG,"Decrypted cell is of type %u (ACI %u).",cellp->command,cellp->aci);
  598. command_process_cell(&cell, conn);
  599. return connection_process_inbuf(conn); /* process the remainder of the buffer */
  600. }
  601. #ifndef USE_TLS
  602. int connection_encrypt_cell(char *cellp, connection_t *conn) {
  603. char cryptcell[CELL_NETWORK_SIZE];
  604. assert(conn);
  605. if(crypto_cipher_encrypt(conn->f_crypto, cellp, CELL_NETWORK_SIZE, cryptcell)) {
  606. log(LOG_ERR,"Could not encrypt cell for connection %s:%u.",conn->address,conn->port);
  607. return -1;
  608. }
  609. memcpy(cellp,cryptcell,CELL_NETWORK_SIZE);
  610. return 0;
  611. }
  612. #endif
  613. /*
  614. Local Variables:
  615. mode:c
  616. indent-tabs-mode:nil
  617. c-basic-offset:2
  618. End:
  619. */