onion.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern int global_role; /* from main.c */
  6. /********* START VARIABLES **********/
  7. tracked_onion_t *tracked_onions = NULL; /* linked list of tracked onions */
  8. tracked_onion_t *last_tracked_onion = NULL;
  9. /********* END VARIABLES ************/
  10. int decide_aci_type(uint32_t local_addr, uint16_t local_port,
  11. uint32_t remote_addr, uint16_t remote_port) {
  12. if(local_addr > remote_addr)
  13. return ACI_TYPE_HIGHER;
  14. if(local_addr < remote_addr)
  15. return ACI_TYPE_LOWER;
  16. if(local_port > remote_port)
  17. return ACI_TYPE_HIGHER;
  18. /* else */
  19. return ACI_TYPE_LOWER;
  20. }
  21. int process_onion(circuit_t *circ, connection_t *conn) {
  22. aci_t aci_type;
  23. if(!decrypt_onion((onion_layer_t *)circ->onion,circ->onionlen,conn->prkey)) {
  24. log(LOG_DEBUG,"command_process_create_cell(): decrypt_onion() failed, closing circuit.");
  25. return -1;
  26. }
  27. log(LOG_DEBUG,"command_process_create_cell(): Onion decrypted.");
  28. /* check freshness */
  29. if (((onion_layer_t *)circ->onion)->expire < time(NULL)) /* expired onion */
  30. {
  31. log(LOG_NOTICE,"I have just received an expired onion. This could be a replay attack.");
  32. return -1;
  33. }
  34. aci_type = decide_aci_type(conn->local.sin_addr.s_addr, ntohs(conn->local.sin_port),
  35. ((onion_layer_t *)circ->onion)->addr,((onion_layer_t *)circ->onion)->port);
  36. if(circuit_init(circ, aci_type) < 0) {
  37. log(LOG_ERR,"process_onion(): init_circuit() failed.");
  38. return -1;
  39. }
  40. /* check for replay */
  41. if(id_tracked_onion(circ->onion, circ->onionlen, tracked_onions)) {
  42. log(LOG_NOTICE,"process_onion(): I have just received a replayed onion. This could be a replay attack.");
  43. return -1;
  44. }
  45. /* track the new onion */
  46. if(!new_tracked_onion(circ->onion,circ->onionlen, &tracked_onions, &last_tracked_onion)) {
  47. log(LOG_DEBUG,"process_onion(): Onion tracking failed. Will ignore.");
  48. }
  49. return 0;
  50. }
  51. /* uses a weighted coin with weight cw to choose a route length */
  52. int chooselen(double cw)
  53. {
  54. int len = 2;
  55. int retval = 0;
  56. unsigned char coin;
  57. if ((cw < 0) || (cw >= 1)) /* invalid parameter */
  58. return -1;
  59. while(1)
  60. {
  61. retval = crypto_pseudo_rand(1, &coin);
  62. if (retval)
  63. return -1;
  64. if (coin > cw*255) /* don't extend */
  65. break;
  66. else
  67. len++;
  68. }
  69. return len;
  70. }
  71. /* returns an array of pointers to routent that define a new route through the OR network
  72. * int cw is the coin weight to use when choosing the route
  73. * order of routers is from last to first
  74. */
  75. unsigned int *new_route(double cw, routerinfo_t **rarray, int rarray_len, int *routelen)
  76. {
  77. int i, j;
  78. int num_acceptable_routers = 0;
  79. unsigned int *route = NULL;
  80. unsigned int oldchoice, choice;
  81. assert((cw >= 0) && (cw < 1) && (rarray) && (routelen) ); /* valid parameters */
  82. *routelen = chooselen(cw);
  83. if (*routelen == -1) {
  84. log(LOG_ERR,"Choosing route length failed.");
  85. return NULL;
  86. }
  87. log(LOG_DEBUG,"new_route(): Chosen route length %d.",*routelen);
  88. for(i=0;i<rarray_len;i++) {
  89. log(LOG_DEBUG,"Contemplating whether router %d is a new option...",i);
  90. if( (global_role & ROLE_OR_CONNECT_ALL) &&
  91. !connection_exact_get_by_addr_port(rarray[i]->addr, rarray[i]->or_port)) {
  92. log(LOG_DEBUG,"Nope, %d is not connected.",i);
  93. goto next_i_loop;
  94. }
  95. for(j=0;j<i;j++) {
  96. if(!pkey_cmp(rarray[i]->pkey, rarray[j]->pkey)) {
  97. /* these guys are twins. so we've already counted him. */
  98. log(LOG_DEBUG,"Nope, %d is a twin of %d.",i,j);
  99. goto next_i_loop;
  100. }
  101. }
  102. num_acceptable_routers++;
  103. log(LOG_DEBUG,"I like %d. num_acceptable_routers now %d.",i, num_acceptable_routers);
  104. next_i_loop:
  105. ; /* our compiler may need an explicit statement after the label */
  106. }
  107. if(num_acceptable_routers < *routelen) {
  108. log(LOG_DEBUG,"new_route(): Cutting routelen from %d to %d.",*routelen, num_acceptable_routers);
  109. *routelen = num_acceptable_routers;
  110. }
  111. if(*routelen < 1) {
  112. log(LOG_ERR,"new_route(): Didn't find any acceptable routers. Failing.");
  113. return NULL;
  114. }
  115. /* allocate memory for the new route */
  116. route = (unsigned int *)malloc(*routelen * sizeof(unsigned int));
  117. if (!route) {
  118. log(LOG_ERR,"Memory allocation failed.");
  119. return NULL;
  120. }
  121. oldchoice = rarray_len;
  122. for(i=0;i<*routelen;i++) {
  123. log(LOG_DEBUG,"new_route(): Choosing hop %u.",i);
  124. if(crypto_pseudo_rand(sizeof(unsigned int),(unsigned char *)&choice)) {
  125. free((void *)route);
  126. return NULL;
  127. }
  128. choice = choice % (rarray_len);
  129. log(LOG_DEBUG,"new_route(): Contemplating router %u.",choice);
  130. if(choice == oldchoice ||
  131. (oldchoice < rarray_len && !pkey_cmp(rarray[choice]->pkey, rarray[oldchoice]->pkey)) ||
  132. ((global_role & ROLE_OR_CONNECT_ALL) && !connection_twin_get_by_addr_port(rarray[choice]->addr, rarray[choice]->or_port))) {
  133. /* Same router as last choice, or router twin,
  134. * or no routers with that key are connected to us.
  135. * Try again. */
  136. log(LOG_DEBUG,"new_route(): Picked a router %d that won't work as next hop.",choice);
  137. i--;
  138. continue;
  139. }
  140. log(LOG_DEBUG,"new_route(): Chosen router %u for hop %u.",choice,i);
  141. oldchoice = choice;
  142. route[i] = choice;
  143. }
  144. return route;
  145. }
  146. /* creates a new onion from route, stores it and its length into buf and len respectively */
  147. unsigned char *create_onion(routerinfo_t **rarray, int rarray_len, unsigned int *route, int routelen, int *len, crypt_path_t **cpath)
  148. {
  149. int i,j;
  150. int retval = 0;
  151. onion_layer_t *layer = NULL;
  152. crypt_path_t *hop = NULL;
  153. unsigned char *retbuf = NULL;
  154. unsigned char *buf;
  155. routerinfo_t *router;
  156. unsigned char iv[16];
  157. assert(rarray && route && len && routelen);
  158. /* calculate the size of the onion */
  159. *len = routelen * 28 + 100; /* 28 bytes per layer + 100 bytes padding for the innermost layer */
  160. log(LOG_DEBUG,"create_onion() : Size of the onion is %u.",*len);
  161. /* allocate memory for the onion */
  162. buf = (unsigned char *)malloc(*len);
  163. if (!buf)
  164. {
  165. log(LOG_ERR,"Error allocating memory.");
  166. return NULL;
  167. }
  168. log(LOG_DEBUG,"create_onion() : Allocated memory for the onion.");
  169. for (retval=0; retval<routelen;retval++)
  170. {
  171. log(LOG_DEBUG,"create_onion() : %u : %s:%u, %u/%u",routelen-retval,inet_ntoa(*((struct in_addr *)&((rarray[route[retval]])->addr))),(rarray[route[retval]])->or_port,(rarray[route[retval]])->pkey,crypto_pk_keysize((rarray[route[retval]])->pkey));
  172. }
  173. layer = (onion_layer_t *)(buf + *len - 128); /* pointer to innermost layer */
  174. /* create the onion layer by layer, starting with the innermost */
  175. for (i=0;i<routelen;i++)
  176. {
  177. router = rarray[route[i]];
  178. log(LOG_DEBUG,"create_onion() : %u",router);
  179. log(LOG_DEBUG,"create_onion() : This router is %s:%u",inet_ntoa(*((struct in_addr *)&router->addr)),router->or_port);
  180. log(LOG_DEBUG,"create_onion() : Key pointer = %u.",router->pkey);
  181. log(LOG_DEBUG,"create_onion() : Key size = %u.",crypto_pk_keysize(router->pkey));
  182. /* 0 bit */
  183. layer->zero = 0;
  184. /* version */
  185. layer->version = VERSION;
  186. /* Back F + Forw F both use DES OFB*/
  187. layer->backf = ONION_DEFAULT_CIPHER;
  188. layer->forwf = ONION_DEFAULT_CIPHER;
  189. /* Dest Port */
  190. if (i) /* not last hop */
  191. layer->port = rarray[route[i-1]]->or_port;
  192. else
  193. layer->port = 0;
  194. /* Dest Addr */
  195. if (i) /* not last hop */
  196. layer->addr = rarray[route[i-1]]->addr;
  197. else
  198. layer->addr = 0;
  199. /* Expiration Time */
  200. layer->expire = time(NULL) + 3600; /* NOW + 1 hour */
  201. /* Key Seed Material */
  202. retval = crypto_rand(16, layer->keyseed);
  203. if (retval) /* error */
  204. {
  205. log(LOG_ERR,"Error generating random data.");
  206. free((void *)buf);
  207. if (cpath)
  208. {
  209. for (j=0;j<i;j++) {
  210. if (cpath[i]->f_crypto)
  211. crypto_free_cipher_env(cpath[i]->f_crypto);
  212. if (cpath[i]->b_crypto)
  213. crypto_free_cipher_env(cpath[i]->b_crypto);
  214. free((void *)cpath[i]);
  215. }
  216. }
  217. return NULL;
  218. }
  219. 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)),layer->port);
  220. /* build up the crypt_path */
  221. if (cpath)
  222. {
  223. cpath[i] = (crypt_path_t *)malloc(sizeof(crypt_path_t));
  224. if (!cpath[i])
  225. {
  226. log(LOG_ERR,"Error allocating memory.");
  227. free((void *)buf);
  228. for (j=0;j<i;j++) {
  229. if (cpath[i]->f_crypto)
  230. crypto_free_cipher_env(cpath[i]->f_crypto);
  231. if (cpath[i]->b_crypto)
  232. crypto_free_cipher_env(cpath[i]->b_crypto);
  233. free((void *)cpath[i]);
  234. }
  235. }
  236. log(LOG_DEBUG,"create_onion() : Building hop %u of crypt path.",i+1);
  237. hop = cpath[i];
  238. /* set crypto functions */
  239. hop->backf = layer->backf;
  240. hop->forwf = layer->forwf;
  241. /* calculate keys */
  242. crypto_SHA_digest(layer->keyseed,16,hop->digest3);
  243. log(LOG_DEBUG,"create_onion() : First SHA pass performed.");
  244. crypto_SHA_digest(hop->digest3,20,hop->digest2);
  245. log(LOG_DEBUG,"create_onion() : Second SHA pass performed.");
  246. crypto_SHA_digest(hop->digest2,20,hop->digest3);
  247. log(LOG_DEBUG,"create_onion() : Third SHA pass performed.");
  248. log(LOG_DEBUG,"create_onion() : Keys generated.");
  249. /* set IV to zero */
  250. memset((void *)iv,0,16);
  251. /* initialize cipher engines */
  252. switch(layer->forwf)
  253. {
  254. case ONION_CIPHER_DES :
  255. hop->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  256. break;
  257. case ONION_CIPHER_RC4 :
  258. hop->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_RC4);
  259. break;
  260. case ONION_CIPHER_IDENTITY :
  261. hop->f_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  262. break;
  263. }
  264. if (!hop->f_crypto) /* cipher initialization failed */
  265. {
  266. log(LOG_ERR,"Could not create a crypto environment.");
  267. free((void *)buf);
  268. for (j=0;j<i;j++) {
  269. if (cpath[i]->f_crypto)
  270. crypto_free_cipher_env(cpath[i]->f_crypto);
  271. if (cpath[i]->b_crypto)
  272. crypto_free_cipher_env(cpath[i]->b_crypto);
  273. free((void *)cpath[i]);
  274. }
  275. return NULL;
  276. }
  277. /* set the key and IV */
  278. if (crypto_cipher_set_key(hop->f_crypto, hop->digest3) ||
  279. crypto_cipher_set_iv(hop->f_crypto, iv)) {
  280. log(LOG_ERR,"Could not initialize the crypto engine.");
  281. free((void *)buf);
  282. for (j=0;j<i;j++) {
  283. if (cpath[i]->f_crypto)
  284. crypto_free_cipher_env(cpath[i]->f_crypto);
  285. if (cpath[i]->b_crypto)
  286. crypto_free_cipher_env(cpath[i]->b_crypto);
  287. free((void *)cpath[i]);
  288. }
  289. return NULL;
  290. }
  291. switch(layer->backf)
  292. {
  293. case ONION_CIPHER_DES :
  294. hop->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  295. break;
  296. case ONION_CIPHER_RC4 :
  297. hop->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_RC4);
  298. break;
  299. case ONION_CIPHER_IDENTITY :
  300. hop->b_crypto = crypto_new_cipher_env(CRYPTO_CIPHER_IDENTITY);
  301. break;
  302. }
  303. if (!hop->b_crypto) /* cipher initialization failed */
  304. {
  305. log(LOG_ERR,"Could not create a crypto environment.");
  306. free((void *)buf);
  307. for (j=0;j<i;j++) {
  308. if (cpath[i]->f_crypto)
  309. crypto_free_cipher_env(cpath[i]->f_crypto);
  310. if (cpath[i]->b_crypto)
  311. crypto_free_cipher_env(cpath[i]->b_crypto);
  312. free((void *)cpath[i]);
  313. }
  314. return NULL;
  315. }
  316. /* set the key and IV */
  317. if (crypto_cipher_set_key(hop->b_crypto, hop->digest2) ||
  318. crypto_cipher_set_iv(hop->b_crypto, iv)) {
  319. log(LOG_ERR,"Could not initialize the crypto engine.");
  320. free((void *)buf);
  321. for (j=0;j<i;j++) {
  322. if (cpath[i]->f_crypto)
  323. crypto_free_cipher_env(cpath[i]->f_crypto);
  324. if (cpath[i]->b_crypto)
  325. crypto_free_cipher_env(cpath[i]->b_crypto);
  326. free((void *)cpath[i]);
  327. }
  328. return NULL;
  329. }
  330. /* initialize */
  331. if (crypto_cipher_encrypt_init_cipher(hop->f_crypto) || crypto_cipher_decrypt_init_cipher(hop->b_crypto)) {
  332. log(LOG_ERR,"Could not initialize the crypto engine.");
  333. free((void *)buf);
  334. for (j=0;j<i;j++) {
  335. if (cpath[i]->f_crypto)
  336. crypto_free_cipher_env(cpath[i]->f_crypto);
  337. if (cpath[i]->b_crypto)
  338. crypto_free_cipher_env(cpath[i]->b_crypto);
  339. free((void *)cpath[i]);
  340. }
  341. return NULL;
  342. }
  343. log(LOG_DEBUG,"create_onion() : Built corresponding crypt path hop.");
  344. }
  345. /* padding if this is the innermost layer */
  346. if (!i)
  347. {
  348. retval=crypto_pseudo_rand(100, (unsigned char *)layer + 28);
  349. if (retval) /* error */
  350. {
  351. log(LOG_ERR,"Error generating pseudo-random data.");
  352. free((void *)buf);
  353. if (cpath)
  354. {
  355. for (j=0;j<i;j++) {
  356. if (cpath[i]->f_crypto)
  357. crypto_free_cipher_env(cpath[i]->f_crypto);
  358. if (cpath[i]->b_crypto)
  359. crypto_free_cipher_env(cpath[i]->b_crypto);
  360. free((void *)cpath[i]);
  361. }
  362. }
  363. return NULL;
  364. }
  365. log(LOG_DEBUG,"create_onion() : This is the innermost layer. Adding 100 bytes of padding.");
  366. }
  367. /* encrypt */
  368. retbuf = encrypt_onion(layer,128+(i*28),router->pkey);
  369. if (!retbuf)
  370. {
  371. log(LOG_ERR,"Error encrypting onion layer.");
  372. free((void *)buf);
  373. if (cpath)
  374. {
  375. for (j=0;j<i;j++) {
  376. if (cpath[i]->f_crypto)
  377. crypto_free_cipher_env(cpath[i]->f_crypto);
  378. if (cpath[i]->b_crypto)
  379. crypto_free_cipher_env(cpath[i]->b_crypto);
  380. free((void *)cpath[i]);
  381. }
  382. }
  383. return NULL;
  384. }
  385. log(LOG_DEBUG,"create_onion() : Encrypted layer.");
  386. /* calculate pointer to next layer */
  387. layer = (onion_layer_t *)(buf + (routelen-i-2)*sizeof(onion_layer_t));
  388. }
  389. return buf;
  390. }
  391. /* encrypts 128 bytes of the onion with the specified public key, the rest with
  392. * DES OFB with the key as defined in the outter layer */
  393. unsigned char *encrypt_onion(onion_layer_t *onion, uint32_t onionlen, crypto_pk_env_t *pkey)
  394. {
  395. unsigned char *tmpbuf = NULL; /* temporary buffer for crypto operations */
  396. unsigned char digest[20]; /* stores SHA1 output - 160 bits */
  397. unsigned char iv[8];
  398. int retval = 0;
  399. crypto_cipher_env_t *crypt_env; /* crypto environment */
  400. if ( (onion) && (pkey) ) /* valid parameters */
  401. {
  402. memset((void *)iv,0,8);
  403. log(LOG_DEBUG,"Onion layer : %u, %u, %u, %s, %u.",onion->zero,onion->backf,onion->forwf,inet_ntoa(*((struct in_addr *)&onion->addr)),onion->port);
  404. /* allocate space for tmpbuf */
  405. tmpbuf = (unsigned char *)malloc(onionlen);
  406. if (!tmpbuf)
  407. {
  408. log(LOG_ERR,"Could not allocate memory.");
  409. return NULL;
  410. }
  411. log(LOG_DEBUG,"encrypt_onion() : allocated %u bytes of memory for the encrypted onion (at %u).",onionlen,tmpbuf);
  412. /* get key1 = SHA1(KeySeed) */
  413. if (crypto_SHA_digest(((onion_layer_t *)onion)->keyseed,16,digest))
  414. {
  415. log(LOG_ERR,"Error computing SHA1 digest.");
  416. free((void *)tmpbuf);
  417. return NULL;
  418. }
  419. log(LOG_DEBUG,"encrypt_onion() : Computed DES key.");
  420. log(LOG_DEBUG,"encrypt_onion() : Trying to RSA encrypt.");
  421. /* encrypt 128 bytes with RSA *pkey */
  422. retval = crypto_pk_public_encrypt(pkey, (unsigned char *)onion, 128, tmpbuf, RSA_NO_PADDING);
  423. if (retval == -1)
  424. {
  425. log(LOG_ERR,"Error RSA-encrypting data :%s",crypto_perror());
  426. free((void *)tmpbuf);
  427. return NULL;
  428. }
  429. log(LOG_DEBUG,"encrypt_onion() : RSA encrypted first 128 bytes of the onion.");
  430. /* now encrypt the rest with DES OFB */
  431. crypt_env = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  432. if (!crypt_env)
  433. {
  434. log(LOG_ERR,"Error creating the crypto environment.");
  435. free((void *)tmpbuf);
  436. return NULL;
  437. }
  438. if (crypto_cipher_set_key(crypt_env, digest)) /* error */
  439. {
  440. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  441. free((void *)tmpbuf);
  442. return NULL;
  443. }
  444. if (crypto_cipher_set_iv(crypt_env, iv))
  445. {
  446. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  447. free((void *)tmpbuf);
  448. return NULL;
  449. }
  450. if (crypto_cipher_encrypt_init_cipher(crypt_env)) {
  451. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  452. free((void *)tmpbuf);
  453. return NULL;
  454. }
  455. retval = crypto_cipher_encrypt(crypt_env,(unsigned char *)onion+128, onionlen-128, (unsigned char *)tmpbuf+128);
  456. if (retval) /* error */
  457. {
  458. log(LOG_ERR,"Error performing DES encryption:%s",crypto_perror());
  459. free((void *)tmpbuf);
  460. return NULL;
  461. }
  462. log(LOG_DEBUG,"encrypt_onion() : DES OFB encrypted the rest of the onion.");
  463. crypto_free_cipher_env(crypt_env);
  464. /* now copy tmpbuf to onion */
  465. memcpy((void *)onion,(void *)tmpbuf,onionlen);
  466. log(LOG_DEBUG,"encrypt_onion() : Copied cipher to original onion buffer.");
  467. free((void *)tmpbuf);
  468. return (unsigned char *)onion;
  469. } /* valid parameters */
  470. else
  471. return NULL;
  472. }
  473. /* decrypts the first 128 bytes using RSA and prkey, decrypts the rest with DES OFB with key1 */
  474. unsigned char *decrypt_onion(onion_layer_t *onion, uint32_t onionlen, crypto_pk_env_t *prkey)
  475. {
  476. void *tmpbuf = NULL; /* temporary buffer for crypto operations */
  477. unsigned char digest[20]; /* stores SHA1 output - 160 bits */
  478. unsigned char iv[8];
  479. int retval = 0;
  480. crypto_cipher_env_t *crypt_env; /* crypto environment */
  481. if ( (onion) && (prkey) ) /* valid parameters */
  482. {
  483. memset((void *)iv,0,8);
  484. /* allocate space for tmpbuf */
  485. tmpbuf = malloc(onionlen);
  486. if (!tmpbuf)
  487. {
  488. log(LOG_ERR,"Could not allocate memory.");
  489. return NULL;
  490. }
  491. log(LOG_DEBUG,"decrypt_onion() : Allocated memory for the temporary buffer.");
  492. /* decrypt 128 bytes with RSA *prkey */
  493. retval = crypto_pk_private_decrypt(prkey, (unsigned char*)onion, 128, (unsigned char *)tmpbuf, RSA_NO_PADDING);
  494. if (retval == -1)
  495. {
  496. log(LOG_ERR,"Error RSA-decrypting data :%s",crypto_perror());
  497. free((void *)tmpbuf);
  498. return NULL;
  499. }
  500. log(LOG_DEBUG,"decrypt_onion() : RSA decryption complete.");
  501. /* get key1 = SHA1(KeySeed) */
  502. retval = crypto_SHA_digest(((onion_layer_t *)tmpbuf)->keyseed,16,digest);
  503. if (retval)
  504. {
  505. log(LOG_ERR,"Error computing SHA1 digest.");
  506. free((void *)tmpbuf);
  507. return NULL;
  508. }
  509. log(LOG_DEBUG,"decrypt_onion() : Computed DES key.");
  510. /* now decrypt the rest with DES OFB */
  511. crypt_env = crypto_new_cipher_env(CRYPTO_CIPHER_DES);
  512. if (!crypt_env)
  513. {
  514. log(LOG_ERR,"Error creating the crypto environment.");
  515. free((void *)tmpbuf);
  516. return NULL;
  517. }
  518. if (crypto_cipher_set_key(crypt_env, digest)) /* error */
  519. {
  520. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  521. free((void *)tmpbuf);
  522. return NULL;
  523. }
  524. if (crypto_cipher_set_iv(crypt_env, iv))
  525. {
  526. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  527. free((void *)tmpbuf);
  528. return NULL;
  529. }
  530. if (crypto_cipher_decrypt_init_cipher(crypt_env)) {
  531. log(LOG_ERR,"Error initializing DES engine:%s",crypto_perror());
  532. free((void *)tmpbuf);
  533. return NULL;
  534. }
  535. retval = crypto_cipher_decrypt(crypt_env,(unsigned char *)onion+128, onionlen-128,(unsigned char *)tmpbuf+128);
  536. if (retval) /* error */
  537. {
  538. log(LOG_ERR,"Error performing DES decryption:%s",crypto_perror());
  539. free((void *)tmpbuf);
  540. return NULL;
  541. }
  542. crypto_free_cipher_env(crypt_env);
  543. log(LOG_DEBUG,"decrypt_onion() : DES decryption complete.");
  544. /* now copy tmpbuf to onion */
  545. memcpy((void *)onion,(void *)tmpbuf,onionlen);
  546. free((void *)tmpbuf);
  547. return (unsigned char *)onion;
  548. } /* valid parameters */
  549. else
  550. return NULL;
  551. }
  552. /* delete first n bytes of the onion and pads the end with n bytes of random data */
  553. void pad_onion(unsigned char *onion, uint32_t onionlen, int n)
  554. {
  555. if (onion) /* valid parameter */
  556. {
  557. memmove((void *)onion,(void *)(onion+n),onionlen-n);
  558. crypto_pseudo_rand(n, onion+onionlen-n);
  559. }
  560. }
  561. /* create a new tracked_onion entry */
  562. tracked_onion_t *new_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion)
  563. {
  564. tracked_onion_t *to = NULL;
  565. if (!onion || !tracked_onions || !last_tracked_onion) /* invalid parameters */
  566. return NULL;
  567. to = (tracked_onion_t *)malloc(sizeof(tracked_onion_t));
  568. if (!to)
  569. return NULL;
  570. to->expire = ((onion_layer_t *)onion)->expire; /* set the expiration date */
  571. /* compute the SHA digest */
  572. if (crypto_SHA_digest(onion, onionlen, to->digest))
  573. {
  574. log(LOG_DEBUG,"new_tracked_onion() : Failed to compute a SHA1 digest of the onion.");
  575. free((void *)to);
  576. return NULL;
  577. }
  578. to->next = NULL;
  579. if (!*tracked_onions)
  580. {
  581. to->prev = NULL;
  582. *tracked_onions = to;
  583. }
  584. else
  585. {
  586. to->prev = (void *)*last_tracked_onion;
  587. (*last_tracked_onion)->next = (void *)to;
  588. }
  589. *last_tracked_onion = to;
  590. return to;
  591. }
  592. /* delete a tracked onion entry */
  593. void remove_tracked_onion(tracked_onion_t *to, tracked_onion_t **tracked_onions, tracked_onion_t **last_tracked_onion)
  594. {
  595. if (!*tracked_onions || !*last_tracked_onion || !to)
  596. return;
  597. if (to->prev)
  598. ((tracked_onion_t *)to->prev)->next = to->next;
  599. if (to->next)
  600. ((tracked_onion_t *)to->next)->prev = to->prev;
  601. if (to == *tracked_onions)
  602. *tracked_onions = (tracked_onion_t *)to->next;
  603. if (to == *last_tracked_onion)
  604. *last_tracked_onion = (tracked_onion_t *)to->prev;
  605. free((void *)to);
  606. return;
  607. }
  608. /* find a tracked onion in the linked list of tracked onions */
  609. tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, tracked_onion_t *tracked_onions)
  610. {
  611. tracked_onion_t *to = tracked_onions;
  612. unsigned char digest[20];
  613. /* compute the SHA digest of the onion */
  614. crypto_SHA_digest(onion,onionlen, digest);
  615. while(to)
  616. {
  617. if (!memcmp((void *)digest, (void *)to->digest, 20))
  618. return to;
  619. to = (tracked_onion_t *)to->next;
  620. }
  621. return NULL;
  622. }