connection_or.c 23 KB

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