connection_or.c 21 KB

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