connection_or.c 21 KB

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