onion.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /********* START VARIABLES **********/
  6. tracked_onion_t *tracked_onions = NULL; /* linked list of tracked onions */
  7. tracked_onion_t *last_tracked_onion = NULL;
  8. /********* END VARIABLES ************/
  9. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  10. uint32_t remote_addr, uint16_t remote_port) {
  11. if(local_addr > remote_addr)
  12. return ACI_TYPE_HIGHER;
  13. if(local_addr < remote_addr)
  14. return ACI_TYPE_LOWER;
  15. if(local_port > remote_port)
  16. return ACI_TYPE_HIGHER;
  17. /* else */
  18. return ACI_TYPE_LOWER;
  19. }
  20. int process_onion(circuit_t *circ, connection_t *conn) {
  21. aci_t aci_type;
  22. if(!decrypt_onion((onion_layer_t *)circ->onion,circ->onionlen,conn->prkey)) {
  23. log(LOG_DEBUG,"command_process_create_cell(): decrypt_onion() failed, closing circuit.");
  24. return -1;
  25. }
  26. log(LOG_DEBUG,"command_process_create_cell(): Onion decrypted.");
  27. /* check freshness */
  28. if (((onion_layer_t *)circ->onion)->expire < time(NULL)) /* expired onion */
  29. {
  30. log(LOG_NOTICE,"I have just received an expired onion. This could be a replay attack.");
  31. return -1;
  32. }
  33. aci_type = decide_aci_type(conn->local.sin_addr.s_addr, conn->local.sin_port,
  34. ((onion_layer_t *)circ->onion)->addr,((onion_layer_t *)circ->onion)->port);
  35. if(circuit_init(circ, aci_type) < 0) {
  36. log(LOG_ERR,"process_onion(): init_circuit() failed.");
  37. return -1;
  38. }
  39. /* check for replay */
  40. if(id_tracked_onion(circ->onion, circ->onionlen, tracked_onions)) {
  41. log(LOG_NOTICE,"process_onion(): I have just received a replayed onion. This could be a replay attack.");
  42. return -1;
  43. }
  44. /* track the new onion */
  45. if(!new_tracked_onion(circ->onion,circ->onionlen, &tracked_onions, &last_tracked_onion)) {
  46. log(LOG_DEBUG,"process_onion(): Onion tracking failed. Will ignore.");
  47. }
  48. return 0;
  49. }
  50. /* uses a weighted coin with weight cw to choose a route length */
  51. int chooselen(double cw)
  52. {
  53. int len = 2;
  54. int retval = 0;
  55. unsigned char coin;
  56. if ((cw < 0) || (cw >= 1)) /* invalid parameter */
  57. return -1;
  58. while(1)
  59. {
  60. retval = RAND_pseudo_bytes(&coin,1);
  61. if (retval == -1)
  62. return -1;
  63. if (coin > cw*255) /* don't extend */
  64. break;
  65. else
  66. len++;
  67. }
  68. return len;
  69. }
  70. /* returns an array of pointers to routent that define a new route through the OR network
  71. * int cw is the coin weight to use when choosing the route
  72. * order of routers is from last to first
  73. */
  74. unsigned int *new_route(double cw, routerinfo_t **rarray, size_t rarray_len, size_t *rlen)
  75. {
  76. int routelen = 0;
  77. int i = 0;
  78. int retval = 0;
  79. unsigned int *route = NULL;
  80. unsigned int oldchoice, choice;
  81. assert((cw >= 0) && (cw < 1) && (rarray) && (rlen) ); /* valid parameters */
  82. routelen = chooselen(cw);
  83. if (routelen == -1)
  84. {
  85. log(LOG_ERR,"Choosing route length failed.");
  86. return NULL;
  87. }
  88. log(LOG_DEBUG,"new_route(): Chosen route length %u.",routelen);
  89. /* FIXME need to figure out how many routers we can actually choose from.
  90. * We can get into an infinite loop if there are too few. */
  91. /* allocate memory for the new route */
  92. route = (unsigned int *)malloc(routelen * sizeof(unsigned int));
  93. if (!route)
  94. {
  95. log(LOG_ERR,"Memory allocation failed.");
  96. return NULL;
  97. }
  98. oldchoice = rarray_len;
  99. for(i=0;i<routelen;i++)
  100. {
  101. log(LOG_DEBUG,"new_route() : Choosing hop %u.",i);
  102. retval = RAND_pseudo_bytes((unsigned char *)&choice,sizeof(unsigned int));
  103. if (retval == -1)
  104. {
  105. free((void *)route);
  106. return NULL;
  107. }
  108. choice = choice % (rarray_len);
  109. log(LOG_DEBUG,"new_route() : Chosen router %u.",choice);
  110. if (choice == oldchoice ||
  111. (oldchoice < rarray_len && !pkey_cmp(rarray[choice]->pkey, rarray[oldchoice]->pkey)) ||
  112. !connection_twin_get_by_addr_port(rarray[choice]->addr, rarray[choice]->or_port)) {
  113. /* Same router as last choice, or router twin,
  114. * or no routers with that key are connected to us.
  115. * Try again. */
  116. i--;
  117. continue;
  118. }
  119. oldchoice = choice;
  120. route[i] = choice;
  121. }
  122. *rlen = routelen;
  123. return route;
  124. }
  125. /* creates a new onion from route, stores it and its length into bufp and lenp respectively */
  126. unsigned char *create_onion(routerinfo_t **rarray, size_t rarray_len, unsigned int *route, size_t routelen, size_t *lenp, crypt_path_t **cpathp)
  127. {
  128. int i,j;
  129. int retval = 0;
  130. onion_layer_t *layer = NULL;
  131. crypt_path_t *hop = NULL;
  132. unsigned char *retbuf = NULL;
  133. unsigned char *bufp;
  134. routerinfo_t *router;
  135. assert(rarray && route && lenp && routelen);
  136. /* calculate the size of the onion */
  137. *lenp = routelen * 28 + 100; /* 28 bytes per layer + 100 bytes padding for the innermost layer */
  138. log(LOG_DEBUG,"create_onion() : Size of the onion is %u.",*lenp);
  139. /* allocate memory for the onion */
  140. bufp = (unsigned char *)malloc(*lenp);
  141. if (!bufp)
  142. {
  143. log(LOG_ERR,"Error allocating memory.");
  144. return NULL;
  145. }
  146. log(LOG_DEBUG,"create_onion() : Allocated memory for the onion.");
  147. for (retval=0; retval<routelen;retval++)
  148. {
  149. log(LOG_DEBUG,"create_onion() : %u : %s:%u, %u/%u",routelen-retval,inet_ntoa(*((struct in_addr *)&((rarray[route[retval]])->addr))),ntohs((rarray[route[retval]])->or_port),(rarray[route[retval]])->pkey,RSA_size((rarray[route[retval]])->pkey));
  150. }
  151. layer = (onion_layer_t *)(bufp + *lenp - 128); /* pointer to innermost layer */
  152. /* create the onion layer by layer, starting with the innermost */
  153. for (i=0;i<routelen;i++)
  154. {
  155. router = rarray[route[i]];
  156. log(LOG_DEBUG,"create_onion() : %u",router);
  157. log(LOG_DEBUG,"create_onion() : This router is %s:%u",inet_ntoa(*((struct in_addr *)&router->addr)),ntohs(router->or_port));
  158. log(LOG_DEBUG,"create_onion() : Key pointer = %u.",router->pkey);
  159. log(LOG_DEBUG,"create_onion() : Key size = %u.",RSA_size(router->pkey));
  160. /* 0 bit */
  161. layer->zero = 0;
  162. /* version */
  163. layer->version = VERSION;
  164. /* Back F + Forw F both use DES OFB*/
  165. layer->backf = ONION_DEFAULT_CIPHER;
  166. layer->forwf = ONION_DEFAULT_CIPHER;
  167. /* Dest Port */
  168. if (i) /* not last hop */
  169. layer->port = rarray[route[i-1]]->or_port;
  170. else
  171. layer->port = 0;
  172. /* Dest Addr */
  173. if (i) /* not last hop */
  174. layer->addr = rarray[route[i-1]]->addr;
  175. else
  176. layer->addr = 0;
  177. /* Expiration Time */
  178. layer->expire = time(NULL) + 3600; /* NOW + 1 hour */
  179. /* Key Seed Material */
  180. retval = RAND_bytes(layer->keyseed,16);
  181. if (retval < 1) /* error */
  182. {
  183. log(LOG_ERR,"Error generating random data.");
  184. free((void *)bufp);
  185. if (cpathp)
  186. {
  187. for (j=0;j<i;j++)
  188. free((void *)cpathp[i]);
  189. }
  190. return NULL;
  191. }
  192. log(LOG_DEBUG,"create_onion() : Onion layer %u built : %u, %u, %u, %s, %u.",i+1,layer->zero,layer->backf,layer->forwf,inet_ntoa(*((struct in_addr *)&layer->addr)),ntohs(layer->port));
  193. /* build up the crypt_path */
  194. if (cpathp)
  195. {
  196. cpathp[i] = (crypt_path_t *)malloc(sizeof(crypt_path_t));
  197. if (!cpathp[i])
  198. {
  199. log(LOG_ERR,"Error allocating memory.");
  200. free((void *)bufp);
  201. for (j=0;j<i;j++)
  202. free((void *)cpathp[i]);
  203. }
  204. log(LOG_DEBUG,"create_onion() : Building hop %u of crypt path.",i+1);
  205. hop = cpathp[i];
  206. /* set crypto functions */
  207. hop->backf = layer->backf;
  208. hop->forwf = layer->forwf;
  209. /* calculate keys */
  210. SHA1(layer->keyseed,16,hop->digest3);
  211. log(LOG_DEBUG,"create_onion() : First SHA pass performed.");
  212. SHA1(hop->digest3,20,hop->digest2);
  213. log(LOG_DEBUG,"create_onion() : Second SHA pass performed.");
  214. SHA1(hop->digest2,20,hop->digest3);
  215. log(LOG_DEBUG,"create_onion() : Third SHA pass performed.");
  216. log(LOG_DEBUG,"create_onion() : Keys generated.");
  217. /* set IVs */
  218. memset((void *)hop->f_iv,0,16);
  219. memset((void *)hop->b_iv,0,16);
  220. /* initialize cipher contexts */
  221. EVP_CIPHER_CTX_init(&hop->f_ctx);
  222. EVP_CIPHER_CTX_init(&hop->b_ctx);
  223. /* initialize cipher engines */
  224. switch(layer->forwf)
  225. {
  226. case ONION_CIPHER_DES :
  227. retval = EVP_EncryptInit(&hop->f_ctx, EVP_des_ofb(), hop->digest3, hop->f_iv);
  228. break;
  229. case ONION_CIPHER_RC4 :
  230. retval = EVP_EncryptInit(&hop->f_ctx, EVP_rc4(), hop->digest3, hop->f_iv);
  231. break;
  232. case ONION_CIPHER_IDENTITY :
  233. retval = EVP_EncryptInit(&hop->f_ctx, EVP_enc_null(), hop->digest3, hop->f_iv);
  234. break;
  235. }
  236. if (!retval) /* cipher initialization failed */
  237. {
  238. log(LOG_ERR,"Could not initialize crypto engines.");
  239. free((void *)bufp);
  240. for (j=0;j<i;j++)
  241. free((void *)cpathp[i]);
  242. return NULL;
  243. }
  244. switch(layer->backf)
  245. {
  246. case ONION_CIPHER_DES :
  247. retval = EVP_DecryptInit(&hop->b_ctx, EVP_des_ofb(), hop->digest2, hop->b_iv);
  248. break;
  249. case ONION_CIPHER_RC4 :
  250. retval = EVP_DecryptInit(&hop->b_ctx, EVP_rc4(), hop->digest2, hop->b_iv);
  251. break;
  252. case ONION_CIPHER_IDENTITY :
  253. retval = EVP_DecryptInit(&hop->b_ctx, EVP_enc_null(), hop->digest2, hop->b_iv);
  254. break;
  255. }
  256. if (!retval) /* cipher initialization failed */
  257. {
  258. log(LOG_ERR,"Could not initialize crypto engines.");
  259. free((void *)bufp);
  260. for (j=0;j<i;j++)
  261. free((void *)cpathp[i]);
  262. return NULL;
  263. }
  264. log(LOG_DEBUG,"create_onion() : Built corresponding crypt path hop.");
  265. }
  266. /* padding if this is the innermost layer */
  267. if (!i)
  268. {
  269. retval=RAND_pseudo_bytes((unsigned char *)layer + 28,100);
  270. if (retval == -1) /* error */
  271. {
  272. log(LOG_ERR,"Error generating pseudo-random data.");
  273. free((void *)bufp);
  274. if (cpathp)
  275. {
  276. for (j=0;j<i;j++)
  277. free((void *)cpathp[i]);
  278. }
  279. return NULL;
  280. }
  281. log(LOG_DEBUG,"create_onion() : This is the innermost layer. Adding 100 bytes of padding.");
  282. }
  283. /* encrypt */
  284. retbuf = encrypt_onion(layer,128+(i*28),router->pkey);
  285. if (!retbuf)
  286. {
  287. log(LOG_ERR,"Error encrypting onion layer.");
  288. free((void *)bufp);
  289. if (cpathp)
  290. {
  291. for (j=0;j<i;j++)
  292. free((void *)cpathp[i]);
  293. }
  294. return NULL;
  295. }
  296. log(LOG_DEBUG,"create_onion() : Encrypted layer.");
  297. /* calculate pointer to next layer */
  298. layer = (onion_layer_t *)(bufp + (routelen-i-2)*sizeof(onion_layer_t));
  299. }
  300. return bufp;
  301. }
  302. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  303. * DES OFB with the key as defined in the outter layer */
  304. unsigned char *encrypt_onion(onion_layer_t *onion, uint32_t onionlen, RSA *pkey)
  305. {
  306. unsigned char *tmpbuf = NULL; /* temporary buffer for crypto operations */
  307. unsigned char digest[20]; /* stores SHA1 output - 160 bits */
  308. unsigned char *retbuf = NULL;
  309. unsigned char iv[8];
  310. int retval = 0;
  311. int outlen = 0;
  312. EVP_CIPHER_CTX ctx; /* cipher context */
  313. if ( (onion) && (pkey) ) /* valid parameters */
  314. {
  315. memset((void *)iv,0,8);
  316. log(LOG_DEBUG,"Onion layer : %u, %u, %u, %s, %u.",onion->zero,onion->backf,onion->forwf,inet_ntoa(*((struct in_addr *)&onion->addr)),ntohs(onion->port));
  317. /* allocate space for tmpbuf */
  318. tmpbuf = (unsigned char *)malloc(onionlen);
  319. if (!tmpbuf)
  320. {
  321. log(LOG_ERR,"Could not allocate memory.");
  322. return NULL;
  323. }
  324. log(LOG_DEBUG,"encrypt_onion() : allocated %u bytes of memory for the encrypted onion (at %u).",onionlen,tmpbuf);
  325. /* get key1 = SHA1(KeySeed) */
  326. retbuf = SHA1(((onion_layer_t *)onion)->keyseed,16,digest);
  327. if (!retbuf)
  328. {
  329. log(LOG_ERR,"Error computing SHA1 digest.");
  330. free((void *)tmpbuf);
  331. return NULL;
  332. }
  333. log(LOG_DEBUG,"encrypt_onion() : Computed DES key.");
  334. log(LOG_DEBUG,"encrypt_onion() : Trying to RSA encrypt.");
  335. /* encrypt 128 bytes with RSA *pkey */
  336. retval = RSA_public_encrypt(128, (unsigned char *)onion, tmpbuf, pkey, RSA_NO_PADDING);
  337. if (retval == -1)
  338. {
  339. log(LOG_ERR,"Error RSA-encrypting data :%s",ERR_reason_error_string(ERR_get_error()));
  340. free((void *)tmpbuf);
  341. return NULL;
  342. }
  343. log(LOG_DEBUG,"encrypt_onion() : RSA encrypted first 128 bytes of the onion.");
  344. /* now encrypt the rest with DES OFB */
  345. EVP_CIPHER_CTX_init(&ctx);
  346. retval = EVP_EncryptInit(&ctx,EVP_des_ofb(),digest,iv);
  347. if (!retval) /* error */
  348. {
  349. log(LOG_ERR,"Error initializing DES engine:%s",ERR_reason_error_string(ERR_get_error()));
  350. free((void *)tmpbuf);
  351. return NULL;
  352. }
  353. retval = EVP_EncryptUpdate(&ctx,(unsigned char *)tmpbuf+128,&outlen,(unsigned char *)onion+128,onionlen-128);
  354. if (!retval) /* error */
  355. {
  356. log(LOG_ERR,"Error performing DES encryption:%s",ERR_reason_error_string(ERR_get_error()));
  357. free((void *)tmpbuf);
  358. return NULL;
  359. }
  360. log(LOG_DEBUG,"encrypt_onion() : DES OFB encrypted the rest of the onion.");
  361. EVP_CIPHER_CTX_cleanup(&ctx);
  362. /* now copy tmpbuf to onion */
  363. memcpy((void *)onion,(void *)tmpbuf,onionlen);
  364. log(LOG_DEBUG,"encrypt_onion() : Copied cipher to original onion buffer.");
  365. free((void *)tmpbuf);
  366. return (unsigned char *)onion;
  367. } /* valid parameters */
  368. else
  369. return NULL;
  370. }
  371. /* decrypts the first 128 bytes using RSA and prkey, decrypts the rest with DES OFB with key1 */
  372. unsigned char *decrypt_onion(onion_layer_t *onion, uint32_t onionlen, RSA *prkey)
  373. {
  374. void *tmpbuf = NULL; /* temporary buffer for crypto operations */
  375. unsigned char digest[20]; /* stores SHA1 output - 160 bits */
  376. unsigned char *retbuf = NULL;
  377. unsigned char iv[8];
  378. int retval = 0;
  379. int outlen = 0;
  380. EVP_CIPHER_CTX ctx; /* cipher context */
  381. if ( (onion) && (prkey) ) /* valid parameters */
  382. {
  383. memset((void *)iv,0,8);
  384. /* allocate space for tmpbuf */
  385. tmpbuf = malloc(onionlen);
  386. if (!tmpbuf)
  387. {
  388. log(LOG_ERR,"Could not allocate memory.");
  389. return NULL;
  390. }
  391. log(LOG_DEBUG,"decrypt_onion() : Allocated memory for the temporary buffer.");
  392. /* decrypt 128 bytes with RSA *prkey */
  393. retval = RSA_private_decrypt(128, (unsigned char*)onion, (unsigned char *)tmpbuf, prkey, RSA_NO_PADDING);
  394. if (retval == -1)
  395. {
  396. log(LOG_ERR,"Error RSA-decrypting data :%s",ERR_reason_error_string(ERR_get_error()));
  397. free((void *)tmpbuf);
  398. return NULL;
  399. }
  400. log(LOG_DEBUG,"decrypt_onion() : RSA decryption complete.");
  401. /* get key1 = SHA1(KeySeed) */
  402. retbuf = SHA1(((onion_layer_t *)tmpbuf)->keyseed,16,digest);
  403. if (!retbuf)
  404. {
  405. log(LOG_ERR,"Error computing SHA1 digest.");
  406. free((void *)tmpbuf);
  407. return NULL;
  408. }
  409. log(LOG_DEBUG,"decrypt_onion() : Computed DES key.");
  410. /* now decrypt the rest with DES OFB */
  411. EVP_CIPHER_CTX_init(&ctx);
  412. retval = EVP_DecryptInit(&ctx,EVP_des_ofb(),digest,iv);
  413. if (!retval) /* error */
  414. {
  415. log(LOG_ERR,"Error initializing DES engine:%s",ERR_reason_error_string(ERR_get_error()));
  416. free((void *)tmpbuf);
  417. return NULL;
  418. }
  419. retval = EVP_DecryptUpdate(&ctx,(unsigned char *)tmpbuf+128,&outlen,(unsigned char *)onion+128,onionlen-128);
  420. if (!retval) /* error */
  421. {
  422. log(LOG_ERR,"Error performing DES decryption:%s",ERR_reason_error_string(ERR_get_error()));
  423. free((void *)tmpbuf);
  424. return NULL;
  425. }
  426. EVP_CIPHER_CTX_cleanup(&ctx);
  427. log(LOG_DEBUG,"decrypt_onion() : DES decryption complete.");
  428. /* now copy tmpbuf to onion */
  429. memcpy((void *)onion,(void *)tmpbuf,onionlen);
  430. free((void *)tmpbuf);
  431. return (unsigned char *)onion;
  432. } /* valid parameters */
  433. else
  434. return NULL;
  435. }
  436. /* delete first n bytes of the onion and pads the end with n bytes of random data */
  437. void pad_onion(unsigned char *onion, uint32_t onionlen, size_t n)
  438. {
  439. if (onion) /* valid parameter */
  440. {
  441. memmove((void *)onion,(void *)(onion+n),onionlen-n);
  442. RAND_pseudo_bytes(onion+onionlen-n,n);
  443. }
  444. }
  445. /* create a new tracked_onion entry */
  446. tracked_onion_t *new_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion)
  447. {
  448. tracked_onion_t *to = NULL;
  449. if (!onion || !tracked_onions || !last_tracked_onion) /* invalid parameters */
  450. return NULL;
  451. to = (tracked_onion_t *)malloc(sizeof(tracked_onion_t));
  452. if (!to)
  453. return NULL;
  454. to->expire = ((onion_layer_t *)onion)->expire; /* set the expiration date */
  455. /* compute the SHA digest */
  456. SHA1(onion, onionlen, to->digest);
  457. if (!to->digest)
  458. {
  459. log(LOG_DEBUG,"new_tracked_onion() : Failed to compute a SHA1 digest of the onion.");
  460. free((void *)to);
  461. return NULL;
  462. }
  463. to->next = NULL;
  464. if (!*tracked_onions)
  465. {
  466. to->prev = NULL;
  467. *tracked_onions = to;
  468. }
  469. else
  470. {
  471. to->prev = (void *)*last_tracked_onion;
  472. (*last_tracked_onion)->next = (void *)to;
  473. }
  474. *last_tracked_onion = to;
  475. return to;
  476. }
  477. /* delete a tracked onion entry */
  478. void remove_tracked_onion(tracked_onion_t *to, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion)
  479. {
  480. if (!*tracked_onions || !*last_tracked_onion || !to)
  481. return;
  482. if (to->prev)
  483. ((tracked_onion_t *)to->prev)->next = to->next;
  484. if (to->next)
  485. ((tracked_onion_t *)to->next)->prev = to->prev;
  486. if (to == *tracked_onions)
  487. *tracked_onions = (tracked_onion_t *)to->next;
  488. if (to == *last_tracked_onion)
  489. *last_tracked_onion = (tracked_onion_t *)to->prev;
  490. free((void *)to);
  491. return;
  492. }
  493. /* find a tracked onion in the linked list of tracked onions */
  494. tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t *tracked_onions)
  495. {
  496. tracked_onion_t *to = tracked_onions;
  497. unsigned char digest[20];
  498. /* compute the SHA digest of the onion */
  499. SHA1(onion,onionlen, digest);
  500. while(to)
  501. {
  502. if (!memcmp((void *)digest, (void *)to->digest, 20))
  503. return to;
  504. to = (tracked_onion_t *)to->next;
  505. }
  506. return NULL;
  507. }