connection_or.c 23 KB

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