onion.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 = crypto_pseudo_rand(1, &coin);
  61. if (retval)
  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 = crypto_pseudo_rand(sizeof(unsigned int),(unsigned char *)&choice);
  103. if (retval)
  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. unsigned char iv[16];
  136. assert(rarray && route && lenp && routelen);
  137. /* calculate the size of the onion */
  138. *lenp = routelen * 28 + 100; /* 28 bytes per layer + 100 bytes padding for the innermost layer */
  139. log(LOG_DEBUG,"create_onion() : Size of the onion is %u.",*lenp);
  140. /* allocate memory for the onion */
  141. bufp = (unsigned char *)malloc(*lenp);
  142. if (!bufp)
  143. {
  144. log(LOG_ERR,"Error allocating memory.");
  145. return NULL;
  146. }
  147. log(LOG_DEBUG,"create_onion() : Allocated memory for the onion.");
  148. for (retval=0; retval<routelen;retval++)
  149. {
  150. 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,crypto_pk_keysize((rarray[route[retval]])->pkey));
  151. }
  152. layer = (onion_layer_t *)(bufp + *lenp - 128); /* pointer to innermost layer */
  153. /* create the onion layer by layer, starting with the innermost */
  154. for (i=0;i<routelen;i++)
  155. {
  156. router = rarray[route[i]];
  157. log(LOG_DEBUG,"create_onion() : %u",router);
  158. log(LOG_DEBUG,"create_onion() : This router is %s:%u",inet_ntoa(*((struct in_addr *)&router->addr)),ntohs(router->or_port));
  159. log(LOG_DEBUG,"create_onion() : Key pointer = %u.",router->pkey);
  160. log(LOG_DEBUG,"create_onion() : Key size = %u.",crypto_pk_keysize(router->pkey));
  161. /* 0 bit */
  162. layer->zero = 0;
  163. /* version */
  164. layer->version = VERSION;
  165. /* Back F + Forw F both use DES OFB*/
  166. layer->backf = ONION_DEFAULT_CIPHER;
  167. layer->forwf = ONION_DEFAULT_CIPHER;
  168. /* Dest Port */
  169. if (i) /* not last hop */
  170. layer->port = rarray[route[i-1]]->or_port;
  171. else
  172. layer->port = 0;
  173. /* Dest Addr */
  174. if (i) /* not last hop */
  175. layer->addr = rarray[route[i-1]]->addr;
  176. else
  177. layer->addr = 0;
  178. /* Expiration Time */
  179. layer->expire = time(NULL) + 3600; /* NOW + 1 hour */
  180. /* Key Seed Material */
  181. retval = crypto_rand(16, layer->keyseed);
  182. if (retval) /* error */
  183. {
  184. log(LOG_ERR,"Error generating random data.");
  185. free((void *)bufp);
  186. if (cpathp)
  187. {
  188. for (j=0;j<i;j++) {
  189. if (cpathp[i]->f_crypto)
  190. crypto_free_cipher_env(cpathp[i]->f_crypto);
  191. if (cpathp[i]->b_crypto)
  192. crypto_free_cipher_env(cpathp[i]->b_crypto);
  193. free((void *)cpathp[i]);
  194. }
  195. }
  196. return NULL;
  197. }
  198. 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));
  199. /* build up the crypt_path */
  200. if (cpathp)
  201. {
  202. cpathp[i] = (crypt_path_t *)malloc(sizeof(crypt_path_t));
  203. if (!cpathp[i])
  204. {
  205. log(LOG_ERR,"Error allocating memory.");
  206. free((void *)bufp);
  207. for (j=0;j<i;j++) {
  208. if (cpathp[i]->f_crypto)
  209. crypto_free_cipher_env(cpathp[i]->f_crypto);
  210. if (cpathp[i]->b_crypto)
  211. crypto_free_cipher_env(cpathp[i]->b_crypto);
  212. free((void *)cpathp[i]);
  213. }
  214. }
  215. log(LOG_DEBUG,"create_onion() : Building hop %u of crypt path.",i+1);
  216. hop = cpathp[i];
  217. /* set crypto functions */
  218. hop->backf = layer->backf;
  219. hop->forwf = layer->forwf;
  220. /* calculate keys */
  221. crypto_SHA_digest(layer->keyseed,16,hop->digest3);
  222. log(LOG_DEBUG,"create_onion() : First SHA pass performed.");
  223. crypto_SHA_digest(hop->digest3,20,hop->digest2);
  224. log(LOG_DEBUG,"create_onion() : Second SHA pass performed.");
  225. crypto_SHA_digest(hop->digest2,20,hop->digest3);
  226. log(LOG_DEBUG,"create_onion() : Third SHA pass performed.");
  227. log(LOG_DEBUG,"create_onion() : Keys generated.");
  228. /* set IV to zero */
  229. memset((void *)iv,0,16);
  230. /* initialize cipher engines */
  231. switch(layer->forwf)
  232. {
  233. case ONION_CIPHER_DES :
  234. hop->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  235. break;
  236. case ONION_CIPHER_RC4 :
  237. hop->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_RC4);
  238. break;
  239. case ONION_CIPHER_IDENTITY :
  240. hop->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  241. break;
  242. }
  243. if (!hop->f_crypto) /* cipher initialization failed */
  244. {
  245. log(LOG_ERR,"Could not create a crypto environment.");
  246. free((void *)bufp);
  247. for (j=0;j<i;j++) {
  248. if (cpathp[i]->f_crypto)
  249. crypto_free_cipher_env(cpathp[i]->f_crypto);
  250. if (cpathp[i]->b_crypto)
  251. crypto_free_cipher_env(cpathp[i]->b_crypto);
  252. free((void *)cpathp[i]);
  253. }
  254. return NULL;
  255. }
  256. /* set the key and IV */
  257. if (crypto_cipher_set_key(hop->f_crypto, hop->digest3) ||
  258. crypto_cipher_set_iv(hop->f_crypto, iv)) {
  259. log(LOG_ERR,"Could not initialize the crypto engine.");
  260. free((void *)bufp);
  261. for (j=0;j<i;j++) {
  262. if (cpathp[i]->f_crypto)
  263. crypto_free_cipher_env(cpathp[i]->f_crypto);
  264. if (cpathp[i]->b_crypto)
  265. crypto_free_cipher_env(cpathp[i]->b_crypto);
  266. free((void *)cpathp[i]);
  267. }
  268. return NULL;
  269. }
  270. switch(layer->backf)
  271. {
  272. case ONION_CIPHER_DES :
  273. hop->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  274. break;
  275. case ONION_CIPHER_RC4 :
  276. hop->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_RC4);
  277. break;
  278. case ONION_CIPHER_IDENTITY :
  279. hop->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  280. break;
  281. }
  282. if (!hop->b_crypto) /* cipher initialization failed */
  283. {
  284. log(LOG_ERR,"Could not create a crypto environment.");
  285. free((void *)bufp);
  286. for (j=0;j<i;j++) {
  287. if (cpathp[i]->f_crypto)
  288. crypto_free_cipher_env(cpathp[i]->f_crypto);
  289. if (cpathp[i]->b_crypto)
  290. crypto_free_cipher_env(cpathp[i]->b_crypto);
  291. free((void *)cpathp[i]);
  292. }
  293. return NULL;
  294. }
  295. /* set the key and IV */
  296. if (crypto_cipher_set_key(hop->b_crypto, hop->digest2) ||
  297. crypto_cipher_set_iv(hop->b_crypto, iv)) {
  298. log(LOG_ERR,"Could not initialize the crypto engine.");
  299. free((void *)bufp);
  300. for (j=0;j<i;j++) {
  301. if (cpathp[i]->f_crypto)
  302. crypto_free_cipher_env(cpathp[i]->f_crypto);
  303. if (cpathp[i]->b_crypto)
  304. crypto_free_cipher_env(cpathp[i]->b_crypto);
  305. free((void *)cpathp[i]);
  306. }
  307. return NULL;
  308. }
  309. /* initialize */
  310. if (crypto_cipher_encrypt_init_cipher(hop->f_crypto) || crypto_cipher_decrypt_init_cipher(hop->b_crypto)) {
  311. log(LOG_ERR,"Could not initialize the crypto engine.");
  312. free((void *)bufp);
  313. for (j=0;j<i;j++) {
  314. if (cpathp[i]->f_crypto)
  315. crypto_free_cipher_env(cpathp[i]->f_crypto);
  316. if (cpathp[i]->b_crypto)
  317. crypto_free_cipher_env(cpathp[i]->b_crypto);
  318. free((void *)cpathp[i]);
  319. }
  320. return NULL;
  321. }
  322. log(LOG_DEBUG,"create_onion() : Built corresponding crypt path hop.");
  323. }
  324. /* padding if this is the innermost layer */
  325. if (!i)
  326. {
  327. retval=crypto_pseudo_rand(100, (unsigned char *)layer + 28);
  328. if (retval) /* error */
  329. {
  330. log(LOG_ERR,"Error generating pseudo-random data.");
  331. free((void *)bufp);
  332. if (cpathp)
  333. {
  334. for (j=0;j<i;j++) {
  335. if (cpathp[i]->f_crypto)
  336. crypto_free_cipher_env(cpathp[i]->f_crypto);
  337. if (cpathp[i]->b_crypto)
  338. crypto_free_cipher_env(cpathp[i]->b_crypto);
  339. free((void *)cpathp[i]);
  340. }
  341. }
  342. return NULL;
  343. }
  344. log(LOG_DEBUG,"create_onion() : This is the innermost layer. Adding 100 bytes of padding.");
  345. }
  346. /* encrypt */
  347. retbuf = encrypt_onion(layer,128+(i*28),router->pkey);
  348. if (!retbuf)
  349. {
  350. log(LOG_ERR,"Error encrypting onion layer.");
  351. free((void *)bufp);
  352. if (cpathp)
  353. {
  354. for (j=0;j<i;j++) {
  355. if (cpathp[i]->f_crypto)
  356. crypto_free_cipher_env(cpathp[i]->f_crypto);
  357. if (cpathp[i]->b_crypto)
  358. crypto_free_cipher_env(cpathp[i]->b_crypto);
  359. free((void *)cpathp[i]);
  360. }
  361. }
  362. return NULL;
  363. }
  364. log(LOG_DEBUG,"create_onion() : Encrypted layer.");
  365. /* calculate pointer to next layer */
  366. layer = (onion_layer_t *)(bufp + (routelen-i-2)*sizeof(onion_layer_t));
  367. }
  368. return bufp;
  369. }
  370. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  371. * DES OFB with the key as defined in the outter layer */
  372. unsigned char *encrypt_onion(onion_layer_t *onion, uint32_t onionlen, crypto_pk_env_t *pkey)
  373. {
  374. unsigned char *tmpbuf = NULL; /* temporary buffer for crypto operations */
  375. unsigned char digest[20]; /* stores SHA1 output - 160 bits */
  376. unsigned char iv[8];
  377. int retval = 0;
  378. crypto_cipher_env_t *crypt_env; /* crypto environment */
  379. if ( (onion) && (pkey) ) /* valid parameters */
  380. {
  381. memset((void *)iv,0,8);
  382. 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));
  383. /* allocate space for tmpbuf */
  384. tmpbuf = (unsigned char *)malloc(onionlen);
  385. if (!tmpbuf)
  386. {
  387. log(LOG_ERR,"Could not allocate memory.");
  388. return NULL;
  389. }
  390. log(LOG_DEBUG,"encrypt_onion() : allocated %u bytes of memory for the encrypted onion (at %u).",onionlen,tmpbuf);
  391. /* get key1 = SHA1(KeySeed) */
  392. if (crypto_SHA_digest(((onion_layer_t *)onion)->keyseed,16,digest))
  393. {
  394. log(LOG_ERR,"Error computing SHA1 digest.");
  395. free((void *)tmpbuf);
  396. return NULL;
  397. }
  398. log(LOG_DEBUG,"encrypt_onion() : Computed DES key.");
  399. log(LOG_DEBUG,"encrypt_onion() : Trying to RSA encrypt.");
  400. /* encrypt 128 bytes with RSA *pkey */
  401. retval = crypto_pk_public_encrypt(pkey, (unsigned char *)onion, 128, tmpbuf, RSA_NO_PADDING);
  402. if (retval == -1)
  403. {
  404. log(LOG_ERR,"Error RSA-encrypting data :%s",crypto_perror());
  405. free((void *)tmpbuf);
  406. return NULL;
  407. }
  408. log(LOG_DEBUG,"encrypt_onion() : RSA encrypted first 128 bytes of the onion.");
  409. /* now encrypt the rest with DES OFB */
  410. crypt_env = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  411. if (!crypt_env)
  412. {
  413. log(LOG_ERR,"Error creating the crypto environment.");
  414. free((void *)tmpbuf);
  415. return NULL;
  416. }
  417. if (crypto_cipher_set_key(crypt_env, digest)) /* error */
  418. {
  419. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  420. free((void *)tmpbuf);
  421. return NULL;
  422. }
  423. if (crypto_cipher_set_iv(crypt_env, iv))
  424. {
  425. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  426. free((void *)tmpbuf);
  427. return NULL;
  428. }
  429. if (crypto_cipher_encrypt_init_cipher(crypt_env)) {
  430. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  431. free((void *)tmpbuf);
  432. return NULL;
  433. }
  434. retval = crypto_cipher_encrypt(crypt_env,(unsigned char *)onion+128, onionlen-128, (unsigned char *)tmpbuf+128);
  435. if (retval) /* error */
  436. {
  437. log(LOG_ERR,"Error performing DES encryption:%s",crypto_perror());
  438. free((void *)tmpbuf);
  439. return NULL;
  440. }
  441. log(LOG_DEBUG,"encrypt_onion() : DES OFB encrypted the rest of the onion.");
  442. crypto_free_cipher_env(crypt_env);
  443. /* now copy tmpbuf to onion */
  444. memcpy((void *)onion,(void *)tmpbuf,onionlen);
  445. log(LOG_DEBUG,"encrypt_onion() : Copied cipher to original onion buffer.");
  446. free((void *)tmpbuf);
  447. return (unsigned char *)onion;
  448. } /* valid parameters */
  449. else
  450. return NULL;
  451. }
  452. /* decrypts the first 128 bytes using RSA and prkey, decrypts the rest with DES OFB with key1 */
  453. unsigned char *decrypt_onion(onion_layer_t *onion, uint32_t onionlen, crypto_pk_env_t *prkey)
  454. {
  455. void *tmpbuf = NULL; /* temporary buffer for crypto operations */
  456. unsigned char digest[20]; /* stores SHA1 output - 160 bits */
  457. unsigned char iv[8];
  458. int retval = 0;
  459. crypto_cipher_env_t *crypt_env; /* crypto environment */
  460. if ( (onion) && (prkey) ) /* valid parameters */
  461. {
  462. memset((void *)iv,0,8);
  463. /* allocate space for tmpbuf */
  464. tmpbuf = malloc(onionlen);
  465. if (!tmpbuf)
  466. {
  467. log(LOG_ERR,"Could not allocate memory.");
  468. return NULL;
  469. }
  470. log(LOG_DEBUG,"decrypt_onion() : Allocated memory for the temporary buffer.");
  471. /* decrypt 128 bytes with RSA *prkey */
  472. retval = crypto_pk_private_decrypt(prkey, (unsigned char*)onion, 128, (unsigned char *)tmpbuf, RSA_NO_PADDING);
  473. if (retval == -1)
  474. {
  475. log(LOG_ERR,"Error RSA-decrypting data :%s",crypto_perror());
  476. free((void *)tmpbuf);
  477. return NULL;
  478. }
  479. log(LOG_DEBUG,"decrypt_onion() : RSA decryption complete.");
  480. /* get key1 = SHA1(KeySeed) */
  481. retval = crypto_SHA_digest(((onion_layer_t *)tmpbuf)->keyseed,16,digest);
  482. if (retval)
  483. {
  484. log(LOG_ERR,"Error computing SHA1 digest.");
  485. free((void *)tmpbuf);
  486. return NULL;
  487. }
  488. log(LOG_DEBUG,"decrypt_onion() : Computed DES key.");
  489. /* now decrypt the rest with DES OFB */
  490. crypt_env = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  491. if (!crypt_env)
  492. {
  493. log(LOG_ERR,"Error creating the crypto environment.");
  494. free((void *)tmpbuf);
  495. return NULL;
  496. }
  497. if (crypto_cipher_set_key(crypt_env, digest)) /* error */
  498. {
  499. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  500. free((void *)tmpbuf);
  501. return NULL;
  502. }
  503. if (crypto_cipher_set_iv(crypt_env, iv))
  504. {
  505. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  506. free((void *)tmpbuf);
  507. return NULL;
  508. }
  509. if (crypto_cipher_decrypt_init_cipher(crypt_env)) {
  510. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  511. free((void *)tmpbuf);
  512. return NULL;
  513. }
  514. retval = crypto_cipher_decrypt(crypt_env,(unsigned char *)onion+128, onionlen-128,(unsigned char *)tmpbuf+128);
  515. if (retval) /* error */
  516. {
  517. log(LOG_ERR,"Error performing DES decryption:%s",crypto_perror());
  518. free((void *)tmpbuf);
  519. return NULL;
  520. }
  521. crypto_free_cipher_env(crypt_env);
  522. log(LOG_DEBUG,"decrypt_onion() : DES decryption complete.");
  523. /* now copy tmpbuf to onion */
  524. memcpy((void *)onion,(void *)tmpbuf,onionlen);
  525. free((void *)tmpbuf);
  526. return (unsigned char *)onion;
  527. } /* valid parameters */
  528. else
  529. return NULL;
  530. }
  531. /* delete first n bytes of the onion and pads the end with n bytes of random data */
  532. void pad_onion(unsigned char *onion, uint32_t onionlen, size_t n)
  533. {
  534. if (onion) /* valid parameter */
  535. {
  536. memmove((void *)onion,(void *)(onion+n),onionlen-n);
  537. crypto_pseudo_rand(n, onion+onionlen-n);
  538. }
  539. }
  540. /* create a new tracked_onion entry */
  541. tracked_onion_t *new_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion)
  542. {
  543. tracked_onion_t *to = NULL;
  544. if (!onion || !tracked_onions || !last_tracked_onion) /* invalid parameters */
  545. return NULL;
  546. to = (tracked_onion_t *)malloc(sizeof(tracked_onion_t));
  547. if (!to)
  548. return NULL;
  549. to->expire = ((onion_layer_t *)onion)->expire; /* set the expiration date */
  550. /* compute the SHA digest */
  551. if (crypto_SHA_digest(onion, onionlen, to->digest))
  552. {
  553. log(LOG_DEBUG,"new_tracked_onion() : Failed to compute a SHA1 digest of the onion.");
  554. free((void *)to);
  555. return NULL;
  556. }
  557. to->next = NULL;
  558. if (!*tracked_onions)
  559. {
  560. to->prev = NULL;
  561. *tracked_onions = to;
  562. }
  563. else
  564. {
  565. to->prev = (void *)*last_tracked_onion;
  566. (*last_tracked_onion)->next = (void *)to;
  567. }
  568. *last_tracked_onion = to;
  569. return to;
  570. }
  571. /* delete a tracked onion entry */
  572. void remove_tracked_onion(tracked_onion_t *to, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion)
  573. {
  574. if (!*tracked_onions || !*last_tracked_onion || !to)
  575. return;
  576. if (to->prev)
  577. ((tracked_onion_t *)to->prev)->next = to->next;
  578. if (to->next)
  579. ((tracked_onion_t *)to->next)->prev = to->prev;
  580. if (to == *tracked_onions)
  581. *tracked_onions = (tracked_onion_t *)to->next;
  582. if (to == *last_tracked_onion)
  583. *last_tracked_onion = (tracked_onion_t *)to->prev;
  584. free((void *)to);
  585. return;
  586. }
  587. /* find a tracked onion in the linked list of tracked onions */
  588. tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t *tracked_onions)
  589. {
  590. tracked_onion_t *to = tracked_onions;
  591. unsigned char digest[20];
  592. /* compute the SHA digest of the onion */
  593. crypto_SHA_digest(onion,onionlen, digest);
  594. while(to)
  595. {
  596. if (!memcmp((void *)digest, (void *)to->digest, 20))
  597. return to;
  598. to = (tracked_onion_t *)to->next;
  599. }
  600. return NULL;
  601. }