connection_or.c 21 KB

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