connection_or.c 21 KB

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