onion.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /* Copyright 2001,2002,2003 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. struct cpath_build_state_t {
  7. int desired_path_len;
  8. char *chosen_exit; /* nicknames */
  9. };
  10. static int count_acceptable_routers(routerinfo_t **rarray, int rarray_len);
  11. int decide_circ_id_type(char *local_nick, char *remote_nick) {
  12. int result;
  13. assert(remote_nick);
  14. if(!local_nick)
  15. return CIRC_ID_TYPE_LOWER;
  16. result = strcmp(local_nick, remote_nick);
  17. assert(result);
  18. if(result < 0)
  19. return CIRC_ID_TYPE_LOWER;
  20. return CIRC_ID_TYPE_HIGHER;
  21. }
  22. struct onion_queue_t {
  23. circuit_t *circ;
  24. struct onion_queue_t *next;
  25. };
  26. /* global (within this file) variables used by the next few functions */
  27. static struct onion_queue_t *ol_list=NULL;
  28. static struct onion_queue_t *ol_tail=NULL;
  29. static int ol_length=0;
  30. int onion_pending_add(circuit_t *circ) {
  31. struct onion_queue_t *tmp;
  32. tmp = tor_malloc(sizeof(struct onion_queue_t));
  33. memset(tmp, 0, sizeof(struct onion_queue_t));
  34. tmp->circ = circ;
  35. if(!ol_tail) {
  36. assert(!ol_list);
  37. assert(!ol_length);
  38. ol_list = tmp;
  39. ol_tail = tmp;
  40. ol_length++;
  41. return 0;
  42. }
  43. assert(ol_list);
  44. assert(!ol_tail->next);
  45. if(ol_length >= options.MaxOnionsPending) {
  46. log_fn(LOG_WARN,"Already have %d onions queued. Closing.", ol_length);
  47. free(tmp);
  48. return -1;
  49. }
  50. ol_length++;
  51. ol_tail->next = tmp;
  52. ol_tail = tmp;
  53. return 0;
  54. }
  55. circuit_t *onion_next_task(void) {
  56. circuit_t *circ;
  57. if(!ol_list)
  58. return NULL; /* no onions pending, we're done */
  59. assert(ol_list->circ);
  60. if(!ol_list->circ->p_conn) {
  61. log_fn(LOG_INFO,"ol_list->circ->p_conn null, must have died?");
  62. onion_pending_remove(ol_list->circ);
  63. return onion_next_task(); /* recurse: how about the next one? */
  64. }
  65. assert(ol_length > 0);
  66. circ = ol_list->circ;
  67. onion_pending_remove(ol_list->circ);
  68. return circ;
  69. }
  70. /* go through ol_list, find the onion_queue_t element which points to
  71. * circ, remove and free that element. leave circ itself alone.
  72. */
  73. void onion_pending_remove(circuit_t *circ) {
  74. struct onion_queue_t *tmpo, *victim;
  75. if(!ol_list)
  76. return; /* nothing here. */
  77. /* first check to see if it's the first entry */
  78. tmpo = ol_list;
  79. if(tmpo->circ == circ) {
  80. /* it's the first one. remove it from the list. */
  81. ol_list = tmpo->next;
  82. if(!ol_list)
  83. ol_tail = NULL;
  84. ol_length--;
  85. victim = tmpo;
  86. } else { /* we need to hunt through the rest of the list */
  87. for( ;tmpo->next && tmpo->next->circ != circ; tmpo=tmpo->next) ;
  88. if(!tmpo->next) {
  89. log_fn(LOG_DEBUG,"circ (p_circ_id %d) not in list, probably at cpuworker.",circ->p_circ_id);
  90. return;
  91. }
  92. /* now we know tmpo->next->circ == circ */
  93. victim = tmpo->next;
  94. tmpo->next = victim->next;
  95. if(ol_tail == victim)
  96. ol_tail = tmpo;
  97. ol_length--;
  98. }
  99. /* now victim points to the element that needs to be removed */
  100. free(victim);
  101. }
  102. /* given a response payload and keys, initialize, then send a created cell back */
  103. int onionskin_answer(circuit_t *circ, unsigned char *payload, unsigned char *keys) {
  104. unsigned char iv[16];
  105. cell_t cell;
  106. memset(iv, 0, 16);
  107. memset(&cell, 0, sizeof(cell_t));
  108. cell.command = CELL_CREATED;
  109. cell.circ_id = circ->p_circ_id;
  110. cell.length = DH_KEY_LEN;
  111. circ->state = CIRCUIT_STATE_OPEN;
  112. log_fn(LOG_DEBUG,"Entering.");
  113. memcpy(cell.payload, payload, DH_KEY_LEN);
  114. log_fn(LOG_DEBUG,"init cipher forward %d, backward %d.", *(int*)keys, *(int*)(keys+16));
  115. if (!(circ->n_crypto =
  116. crypto_create_init_cipher(CIRCUIT_CIPHER,keys,iv,0))) {
  117. log_fn(LOG_WARN,"Cipher initialization failed (n).");
  118. return -1;
  119. }
  120. if (!(circ->p_crypto =
  121. crypto_create_init_cipher(CIRCUIT_CIPHER,keys+16,iv,1))) {
  122. log_fn(LOG_WARN,"Cipher initialization failed (p).");
  123. return -1;
  124. }
  125. connection_or_write_cell_to_buf(&cell, circ->p_conn);
  126. log_fn(LOG_DEBUG,"Finished sending 'created' cell.");
  127. return 0;
  128. }
  129. char **parse_nickname_list(char *list, int *num) {
  130. char **out;
  131. char *start,*end;
  132. int i;
  133. while(isspace(*list)) list++;
  134. i=0, start = list;
  135. while(*start) {
  136. while(*start && !isspace(*start)) start++;
  137. i++;
  138. while(isspace(*start)) start++;
  139. }
  140. out = tor_malloc(i * sizeof(char *));
  141. i=0, start=list;
  142. while(*start) {
  143. end=start; while(*end && !isspace(*end)) end++;
  144. out[i] = tor_malloc(MAX_NICKNAME_LEN);
  145. strncpy(out[i],start,end-start);
  146. out[i][end-start] = 0; /* null terminate it */
  147. i++;
  148. while(isspace(*end)) end++;
  149. start = end;
  150. }
  151. *num = i;
  152. return out;
  153. }
  154. static int new_route_len(double cw, routerinfo_t **rarray, int rarray_len) {
  155. int num_acceptable_routers;
  156. int routelen;
  157. assert((cw >= 0) && (cw < 1) && rarray); /* valid parameters */
  158. for(routelen=3; ; routelen++) { /* 3, increment until coinflip says we're done */
  159. if (crypto_pseudo_rand_int(255) >= cw*255) /* don't extend */
  160. break;
  161. }
  162. log_fn(LOG_DEBUG,"Chosen route length %d (%d routers available).",routelen, rarray_len);
  163. num_acceptable_routers = count_acceptable_routers(rarray, rarray_len);
  164. if(num_acceptable_routers < 2) {
  165. log_fn(LOG_INFO,"Not enough acceptable routers. Failing.");
  166. return -1;
  167. }
  168. if(num_acceptable_routers < routelen) {
  169. log_fn(LOG_INFO,"Not enough routers: cutting routelen from %d to %d.",routelen, num_acceptable_routers);
  170. routelen = num_acceptable_routers;
  171. }
  172. if (routelen < 1) {
  173. log_fn(LOG_WARN,"Didn't find any acceptable routers. Failing.");
  174. return -1;
  175. }
  176. return routelen;
  177. }
  178. static routerinfo_t *choose_good_exit_server(directory_t *dir)
  179. {
  180. int *n_supported;
  181. int *n_maybe_supported;
  182. int i, j;
  183. int n_pending_connections = 0;
  184. connection_t **carray;
  185. int n_connections;
  186. int best_support = -1;
  187. int best_maybe_support = -1;
  188. int best_support_idx = -1;
  189. int best_maybe_support_idx = -1;
  190. int n_best_support=0, n_best_maybe_support=0;
  191. get_connection_array(&carray, &n_connections);
  192. for (i = 0; i < n_connections; ++i) {
  193. if (carray[i]->type == CONN_TYPE_AP && carray[i]->state == AP_CONN_STATE_CIRCUIT_WAIT)
  194. ++n_pending_connections;
  195. }
  196. log_fn(LOG_DEBUG, "Choosing exit node; %d connections are pending",
  197. n_pending_connections);
  198. n_supported = tor_malloc(sizeof(int)*dir->n_routers);
  199. n_maybe_supported = tor_malloc(sizeof(int)*dir->n_routers);
  200. for (i = 0; i < dir->n_routers; ++i) {
  201. n_supported[i] = n_maybe_supported[i] = 0;
  202. for (j = 0; j < n_pending_connections; ++j) {
  203. if (carray[i]->type != CONN_TYPE_AP ||
  204. carray[i]->state == AP_CONN_STATE_CIRCUIT_WAIT ||
  205. carray[i]->marked_for_close)
  206. continue;
  207. switch (connection_ap_can_use_exit(carray[j], dir->routers[i]))
  208. {
  209. case -1:
  210. break;
  211. case 0:
  212. ++n_supported[i];
  213. case 1:
  214. ++n_maybe_supported[i];
  215. }
  216. }
  217. if (n_supported[i] > best_support) {
  218. best_support = n_supported[i]; best_support_idx = i; n_best_support=1;
  219. } else if (n_supported[i] == best_support) {
  220. ++n_best_support;
  221. }
  222. if (n_maybe_supported[i] > best_maybe_support) {
  223. best_maybe_support = n_maybe_supported[i]; best_maybe_support_idx = i;
  224. n_best_maybe_support = 1;
  225. } else if (n_maybe_supported[i] == best_maybe_support) {
  226. ++n_best_maybe_support;
  227. }
  228. }
  229. log_fn(LOG_WARN, "Found %d servers that will definitely support %d/%d pending connections, and %d that might support %d/%d.",
  230. n_best_support, best_support, n_pending_connections,
  231. n_best_maybe_support, best_maybe_support, n_pending_connections);
  232. if (best_support) {
  233. i = crypto_pseudo_rand_int(n_best_support);
  234. for (j = best_support_idx; j < dir->n_routers; ++j) {
  235. if (n_supported[j] == best_support) {
  236. if (i)
  237. --i;
  238. else {
  239. tor_free(n_supported); tor_free(n_maybe_supported);
  240. return dir->routers[i];
  241. }
  242. }
  243. }
  244. }
  245. if (best_maybe_support) {
  246. i = crypto_pseudo_rand_int(n_best_maybe_support);
  247. for (j = best_maybe_support_idx; j < dir->n_routers; ++j) {
  248. if (n_maybe_supported[j] == best_maybe_support) {
  249. if (i)
  250. --i;
  251. else {
  252. tor_free(n_supported); tor_free(n_maybe_supported);
  253. return dir->routers[i];
  254. }
  255. }
  256. }
  257. }
  258. tor_free(n_supported);
  259. tor_free(n_maybe_supported);
  260. i = crypto_pseudo_rand_int(dir->n_routers);
  261. log_fn(LOG_DEBUG, "Chose exit server '%s'", dir->routers[i]->nickname);
  262. return dir->routers[i];
  263. }
  264. cpath_build_state_t *onion_new_cpath_build_state(void) {
  265. directory_t *dir;
  266. int r;
  267. cpath_build_state_t *info;
  268. router_get_directory(&dir);
  269. r = new_route_len(options.PathlenCoinWeight, dir->routers, dir->n_routers);
  270. if (r < 0)
  271. return NULL;
  272. info = tor_malloc(sizeof(cpath_build_state_t));
  273. info->desired_path_len = r;
  274. /* XXX This is leaked */
  275. info->chosen_exit = tor_strdup(choose_good_exit_server(dir)->nickname);
  276. return info;
  277. }
  278. static int count_acceptable_routers(routerinfo_t **rarray, int rarray_len) {
  279. int i, j;
  280. int num=0;
  281. connection_t *conn;
  282. for(i=0;i<rarray_len;i++) {
  283. log_fn(LOG_DEBUG,"Contemplating whether router %d is a new option...",i);
  284. if(options.OnionRouter) {
  285. conn = connection_exact_get_by_addr_port(rarray[i]->addr, rarray[i]->or_port);
  286. if(!conn || conn->type != CONN_TYPE_OR || conn->state != OR_CONN_STATE_OPEN) {
  287. log_fn(LOG_DEBUG,"Nope, %d is not connected.",i);
  288. goto next_i_loop;
  289. }
  290. }
  291. for(j=0;j<i;j++) {
  292. if(!crypto_pk_cmp_keys(rarray[i]->onion_pkey, rarray[j]->onion_pkey)) {
  293. /* these guys are twins. so we've already counted him. */
  294. log_fn(LOG_DEBUG,"Nope, %d is a twin of %d.",i,j);
  295. goto next_i_loop;
  296. }
  297. }
  298. num++;
  299. log_fn(LOG_DEBUG,"I like %d. num_acceptable_routers now %d.",i, num);
  300. next_i_loop:
  301. ; /* our compiler may need an explicit statement after the label */
  302. }
  303. return num;
  304. }
  305. int onion_extend_cpath(crypt_path_t **head_ptr, cpath_build_state_t *state, routerinfo_t **router_out)
  306. {
  307. int cur_len;
  308. crypt_path_t *cpath, *hop;
  309. routerinfo_t **rarray, *r;
  310. routerinfo_t *choice;
  311. int rarray_len;
  312. int i;
  313. directory_t *dir;
  314. int n_failures;
  315. assert(head_ptr);
  316. assert(router_out);
  317. router_get_directory(&dir);
  318. rarray = dir->routers;
  319. rarray_len = dir->n_routers;
  320. if (!*head_ptr) {
  321. cur_len = 0;
  322. } else {
  323. cur_len = 1;
  324. for (cpath = *head_ptr; cpath->next != *head_ptr; cpath = cpath->next) {
  325. ++cur_len;
  326. }
  327. }
  328. if (cur_len >= state->desired_path_len) {
  329. log_fn(LOG_DEBUG, "Path is complete: %d steps long",
  330. state->desired_path_len);
  331. return 1;
  332. }
  333. log_fn(LOG_DEBUG, "Path is %d long; we want %d", cur_len,
  334. state->desired_path_len);
  335. n_failures = 0;
  336. goto start;
  337. again:
  338. log_fn(LOG_DEBUG, "Picked an already-selected router for hop %d; retrying.",
  339. cur_len);
  340. ++n_failures;
  341. if (n_failures == 25) {
  342. /* This actually happens with P=1/30,000,000 when we _could_ build a
  343. * circuit. For now, let's leave it in.
  344. */
  345. log_fn(LOG_ERR, "Infinite loop in exit path selection");
  346. exit(1);
  347. }
  348. start:
  349. if(cur_len == 0) { /* picking entry node */
  350. log_fn(LOG_DEBUG, "Contemplating first hop: random choice.");
  351. choice = rarray[crypto_pseudo_rand_int(rarray_len)];
  352. } else if (cur_len == state->desired_path_len - 1) { /* Picking last node */
  353. log_fn(LOG_DEBUG, "Contemplating last hop: choice already made.");
  354. choice = router_get_by_nickname(state->chosen_exit);
  355. } else {
  356. log_fn(LOG_DEBUG, "Contemplating intermediate hop: random choice.");
  357. choice = rarray[crypto_pseudo_rand_int(rarray_len)];
  358. }
  359. log_fn(LOG_DEBUG,"Contemplating router %s for hop %d (exit is %s)",
  360. choice->nickname, cur_len, state->chosen_exit);
  361. if (cur_len != state->desired_path_len-1 &&
  362. !strcasecmp(choice->nickname, state->chosen_exit)) {
  363. goto again;
  364. }
  365. for (i = 0, cpath = *head_ptr; i < cur_len; ++i, cpath=cpath->next) {
  366. r = router_get_by_addr_port(cpath->addr, cpath->port);
  367. if ((r && !crypto_pk_cmp_keys(r->onion_pkey, choice->onion_pkey))
  368. || (cur_len != state->desired_path_len-1 &&
  369. !strcasecmp(choice->nickname, state->chosen_exit))
  370. || (cpath->addr == choice->addr &&
  371. cpath->port == choice->or_port)
  372. || (options.OnionRouter &&
  373. !(connection_twin_get_by_addr_port(choice->addr,
  374. choice->or_port)))) {
  375. goto again;
  376. }
  377. }
  378. /* Okay, so we haven't used 'choice' before. */
  379. hop = (crypt_path_t *)tor_malloc(sizeof(crypt_path_t));
  380. memset(hop, 0, sizeof(crypt_path_t));
  381. /* link hop into the cpath, at the end. */
  382. if (*head_ptr) {
  383. hop->next = (*head_ptr);
  384. hop->prev = (*head_ptr)->prev;
  385. (*head_ptr)->prev->next = hop;
  386. (*head_ptr)->prev = hop;
  387. } else {
  388. *head_ptr = hop;
  389. hop->prev = hop->next = hop;
  390. }
  391. hop->state = CPATH_STATE_CLOSED;
  392. hop->port = choice->or_port;
  393. hop->addr = choice->addr;
  394. hop->package_window = CIRCWINDOW_START;
  395. hop->deliver_window = CIRCWINDOW_START;
  396. log_fn(LOG_DEBUG, "Extended circuit path with %s for hop %d",
  397. choice->nickname, cur_len);
  398. *router_out = choice;
  399. return 0;
  400. }
  401. /*----------------------------------------------------------------------*/
  402. /* Given a router's public key, generates a 144-byte encrypted DH pubkey,
  403. * and stores it into onion_skin out. Stores the DH private key into
  404. * handshake_state_out for later completion of the handshake.
  405. *
  406. * The encrypted pubkey is formed as follows:
  407. * 16 bytes of symmetric key
  408. * 128 bytes of g^x for DH.
  409. * The first 128 bytes are RSA-encrypted with the server's public key,
  410. * and the last 16 are encrypted with the symmetric key.
  411. */
  412. int
  413. onion_skin_create(crypto_pk_env_t *dest_router_key,
  414. crypto_dh_env_t **handshake_state_out,
  415. char *onion_skin_out) /* Must be DH_ONIONSKIN_LEN bytes long */
  416. {
  417. char iv[16];
  418. char *pubkey = NULL;
  419. crypto_dh_env_t *dh = NULL;
  420. crypto_cipher_env_t *cipher = NULL;
  421. int dhbytes, pkbytes;
  422. *handshake_state_out = NULL;
  423. memset(onion_skin_out, 0, DH_ONIONSKIN_LEN);
  424. memset(iv, 0, 16);
  425. if (!(dh = crypto_dh_new()))
  426. goto err;
  427. dhbytes = crypto_dh_get_bytes(dh);
  428. pkbytes = crypto_pk_keysize(dest_router_key);
  429. assert(dhbytes+16 == DH_ONIONSKIN_LEN);
  430. pubkey = (char *)tor_malloc(dhbytes+16);
  431. if (crypto_rand(16, pubkey))
  432. goto err;
  433. /* XXXX You can't just run around RSA-encrypting any bitstream: if it's
  434. * greater than the RSA key, then OpenSSL will happily encrypt,
  435. * and later decrypt to the wrong value. So we set the first bit
  436. * of 'pubkey' to 0. This means that our symmetric key is really only
  437. * 127 bits long, but since it shouldn't be necessary to encrypt
  438. * DH public keys values in the first place, we should be fine.
  439. */
  440. pubkey[0] &= 0x7f;
  441. if (crypto_dh_get_public(dh, pubkey+16, dhbytes))
  442. goto err;
  443. #ifdef DEBUG_ONION_SKINS
  444. #define PA(a,n) \
  445. { int _i; for (_i = 0; _i<n; ++_i) printf("%02x ",((int)(a)[_i])&0xFF); }
  446. printf("Client: client g^x:");
  447. PA(pubkey+16,3);
  448. printf("...");
  449. PA(pubkey+141,3);
  450. puts("");
  451. printf("Client: client symkey:");
  452. PA(pubkey+0,16);
  453. puts("");
  454. #endif
  455. cipher = crypto_create_init_cipher(ONION_CIPHER, pubkey, iv, 1);
  456. if (!cipher)
  457. goto err;
  458. if (crypto_pk_public_encrypt(dest_router_key, pubkey, pkbytes,
  459. onion_skin_out, RSA_NO_PADDING)==-1)
  460. goto err;
  461. if (crypto_cipher_encrypt(cipher, pubkey+pkbytes, dhbytes+16-pkbytes,
  462. onion_skin_out+pkbytes))
  463. goto err;
  464. free(pubkey);
  465. crypto_free_cipher_env(cipher);
  466. *handshake_state_out = dh;
  467. return 0;
  468. err:
  469. tor_free(pubkey);
  470. if (dh) crypto_dh_free(dh);
  471. if (cipher) crypto_free_cipher_env(cipher);
  472. return -1;
  473. }
  474. /* Given an encrypted DH public key as generated by onion_skin_create,
  475. * and the private key for this onion router, generate the 128-byte DH
  476. * reply, and key_out_len bytes of key material, stored in key_out.
  477. */
  478. int
  479. onion_skin_server_handshake(char *onion_skin, /* DH_ONIONSKIN_LEN bytes long */
  480. crypto_pk_env_t *private_key,
  481. char *handshake_reply_out, /* DH_KEY_LEN bytes long */
  482. char *key_out,
  483. int key_out_len)
  484. {
  485. char buf[DH_ONIONSKIN_LEN];
  486. char iv[16];
  487. crypto_dh_env_t *dh = NULL;
  488. crypto_cipher_env_t *cipher = NULL;
  489. int pkbytes;
  490. int len;
  491. memset(iv, 0, 16);
  492. pkbytes = crypto_pk_keysize(private_key);
  493. if (crypto_pk_private_decrypt(private_key,
  494. onion_skin, pkbytes,
  495. buf, RSA_NO_PADDING) == -1)
  496. goto err;
  497. #ifdef DEBUG_ONION_SKINS
  498. printf("Server: client symkey:");
  499. PA(buf+0,16);
  500. puts("");
  501. #endif
  502. cipher = crypto_create_init_cipher(ONION_CIPHER, buf, iv, 0);
  503. if (crypto_cipher_decrypt(cipher, onion_skin+pkbytes, DH_ONIONSKIN_LEN-pkbytes,
  504. buf+pkbytes))
  505. goto err;
  506. #ifdef DEBUG_ONION_SKINS
  507. printf("Server: client g^x:");
  508. PA(buf+16,3);
  509. printf("...");
  510. PA(buf+141,3);
  511. puts("");
  512. #endif
  513. dh = crypto_dh_new();
  514. if (crypto_dh_get_public(dh, handshake_reply_out, DH_KEY_LEN))
  515. goto err;
  516. #ifdef DEBUG_ONION_SKINS
  517. printf("Server: server g^y:");
  518. PA(handshake_reply_out+0,3);
  519. printf("...");
  520. PA(handshake_reply_out+125,3);
  521. puts("");
  522. #endif
  523. len = crypto_dh_compute_secret(dh, buf+16, DH_KEY_LEN, key_out, key_out_len);
  524. if (len < 0)
  525. goto err;
  526. #ifdef DEBUG_ONION_SKINS
  527. printf("Server: key material:");
  528. PA(buf, DH_KEY_LEN);
  529. puts("");
  530. printf("Server: keys out:");
  531. PA(key_out, key_out_len);
  532. puts("");
  533. #endif
  534. crypto_free_cipher_env(cipher);
  535. crypto_dh_free(dh);
  536. return 0;
  537. err:
  538. if (cipher) crypto_free_cipher_env(cipher);
  539. if (dh) crypto_dh_free(dh);
  540. return -1;
  541. }
  542. /* Finish the client side of the DH handshake.
  543. * Given the 128 byte DH reply as generated by onion_skin_server_handshake
  544. * and the handshake state generated by onion_skin_create, generate
  545. * key_out_len bytes of shared key material and store them in key_out.
  546. *
  547. * After the invocation, call crypto_dh_free on handshake_state.
  548. */
  549. int
  550. onion_skin_client_handshake(crypto_dh_env_t *handshake_state,
  551. char *handshake_reply,/* Must be DH_KEY_LEN bytes long*/
  552. char *key_out,
  553. int key_out_len)
  554. {
  555. int len;
  556. assert(crypto_dh_get_bytes(handshake_state) == DH_KEY_LEN);
  557. #ifdef DEBUG_ONION_SKINS
  558. printf("Client: server g^y:");
  559. PA(handshake_reply+0,3);
  560. printf("...");
  561. PA(handshake_reply+125,3);
  562. puts("");
  563. #endif
  564. len = crypto_dh_compute_secret(handshake_state, handshake_reply, DH_KEY_LEN,
  565. key_out, key_out_len);
  566. if (len < 0)
  567. return -1;
  568. #ifdef DEBUG_ONION_SKINS
  569. printf("Client: keys out:");
  570. PA(key_out, key_out_len);
  571. puts("");
  572. #endif
  573. return 0;
  574. }
  575. /*
  576. Local Variables:
  577. mode:c
  578. indent-tabs-mode:nil
  579. c-basic-offset:2
  580. End:
  581. */