router.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /**
  6. * \file router.c
  7. * \brief OR functionality, including key maintenance, generating
  8. * and uploading server descriptors, retrying OR connections.
  9. **/
  10. extern or_options_t options; /* command-line and config-file options */
  11. /** Exposed for test.c. */ void get_platform_str(char *platform, int len);
  12. /************************************************************/
  13. /*****
  14. * Key management: ORs only.
  15. *****/
  16. /** Private keys for this OR. There is also an SSL key managed by tortls.c.
  17. */
  18. static tor_mutex_t *key_lock=NULL;
  19. static time_t onionkey_set_at=0; /* When was onionkey last changed? */
  20. static crypto_pk_env_t *onionkey=NULL;
  21. static crypto_pk_env_t *lastonionkey=NULL;
  22. static crypto_pk_env_t *identitykey=NULL;
  23. /** Replace the current onion key with <b>k</b>. Does not affect lastonionkey;
  24. * to update onionkey correctly, call rotate_onion_key().
  25. */
  26. void set_onion_key(crypto_pk_env_t *k) {
  27. tor_mutex_acquire(key_lock);
  28. onionkey = k;
  29. onionkey_set_at = time(NULL);
  30. tor_mutex_release(key_lock);
  31. }
  32. /** Return the current onion key. Requires that the onion key has been
  33. * loaded or generated. */
  34. crypto_pk_env_t *get_onion_key(void) {
  35. tor_assert(onionkey);
  36. return onionkey;
  37. }
  38. /** Return the onion key that was current before the most recent onion
  39. * key rotation. If no rotation has been performed since this process
  40. * started, return NULL.
  41. */
  42. crypto_pk_env_t *get_previous_onion_key(void) {
  43. return lastonionkey;
  44. }
  45. void dup_onion_keys(crypto_pk_env_t **key, crypto_pk_env_t **last)
  46. {
  47. tor_assert(key && last);
  48. tor_mutex_acquire(key_lock);
  49. *key = crypto_pk_dup_key(onionkey);
  50. if (lastonionkey)
  51. *last = crypto_pk_dup_key(lastonionkey);
  52. else
  53. *last = NULL;
  54. tor_mutex_release(key_lock);
  55. }
  56. /** Return the time when the onion key was last set. This is either the time
  57. * when the process launched, or the time of the most recent key rotation since
  58. * the process launched.
  59. */
  60. time_t get_onion_key_set_at(void) {
  61. return onionkey_set_at;
  62. }
  63. /** Set the current identity key to k.
  64. */
  65. void set_identity_key(crypto_pk_env_t *k) {
  66. identitykey = k;
  67. }
  68. /** Returns the current identity key; requires that the identity key has been
  69. * set.
  70. */
  71. crypto_pk_env_t *get_identity_key(void) {
  72. tor_assert(identitykey);
  73. return identitykey;
  74. }
  75. /** Replace the previous onion key with the current onion key, and generate
  76. * a new previous onion key. Immediately after calling this function,
  77. * the OR should:
  78. * - schedule all previous cpuworkers to shut down _after_ processing
  79. * pending work. (This will cause fresh cpuworkers to be generated.)
  80. * - generate and upload a fresh routerinfo.
  81. */
  82. void rotate_onion_key(void)
  83. {
  84. char fname[512];
  85. crypto_pk_env_t *prkey;
  86. sprintf(fname,"%s/keys/onion.key",options.DataDirectory);
  87. if (!(prkey = crypto_new_pk_env())) {
  88. log(LOG_ERR, "Error creating crypto environment.");
  89. goto error;
  90. }
  91. if (crypto_pk_generate_key(prkey)) {
  92. log(LOG_ERR, "Error generating onion key");
  93. goto error;
  94. }
  95. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  96. log(LOG_ERR, "Couldn't write generated key to %s.", fname);
  97. goto error;
  98. }
  99. tor_mutex_acquire(key_lock);
  100. if (lastonionkey)
  101. crypto_free_pk_env(lastonionkey);
  102. log_fn(LOG_INFO, "Rotating onion key");
  103. lastonionkey = onionkey;
  104. set_onion_key(prkey);
  105. tor_mutex_release(key_lock);
  106. return;
  107. error:
  108. log_fn(LOG_WARN, "Couldn't rotate onion key.");
  109. }
  110. /** Try to read an RSA key from <b>fname</b>. If <b>fname</b> doesn't exist,
  111. * create a new RSA key and save it in <b>fname</b>. Return the read/created
  112. * key, or NULL on error.
  113. */
  114. crypto_pk_env_t *init_key_from_file(const char *fname)
  115. {
  116. crypto_pk_env_t *prkey = NULL;
  117. FILE *file = NULL;
  118. if (!(prkey = crypto_new_pk_env())) {
  119. log(LOG_ERR, "Error creating crypto environment.");
  120. goto error;
  121. }
  122. switch(file_status(fname)) {
  123. case FN_DIR:
  124. case FN_ERROR:
  125. log(LOG_ERR, "Can't read key from %s", fname);
  126. goto error;
  127. case FN_NOENT:
  128. log(LOG_INFO, "No key found in %s; generating fresh key.", fname);
  129. if (crypto_pk_generate_key(prkey)) {
  130. log(LOG_ERR, "Error generating onion key");
  131. goto error;
  132. }
  133. if (crypto_pk_check_key(prkey) <= 0) {
  134. log(LOG_ERR, "Generated key seems invalid");
  135. goto error;
  136. }
  137. log(LOG_INFO, "Generated key seems valid");
  138. if (crypto_pk_write_private_key_to_filename(prkey, fname)) {
  139. log(LOG_ERR, "Couldn't write generated key to %s.", fname);
  140. goto error;
  141. }
  142. return prkey;
  143. case FN_FILE:
  144. if (crypto_pk_read_private_key_from_filename(prkey, fname)) {
  145. log(LOG_ERR, "Error loading private key.");
  146. goto error;
  147. }
  148. return prkey;
  149. default:
  150. tor_assert(0);
  151. }
  152. error:
  153. if (prkey)
  154. crypto_free_pk_env(prkey);
  155. if (file)
  156. fclose(file);
  157. return NULL;
  158. }
  159. /** Initialize all OR private keys, and the TLS context, as necessary.
  160. * On OPs, this only initializes the tls context.
  161. */
  162. int init_keys(void) {
  163. char keydir[512];
  164. char fingerprint[FINGERPRINT_LEN+MAX_NICKNAME_LEN+3];
  165. char *cp;
  166. const char *tmp, *mydesc;
  167. crypto_pk_env_t *prkey;
  168. if (!key_lock)
  169. key_lock = tor_mutex_new();
  170. /* OP's don't need keys. Just initialize the TLS context.*/
  171. if (!options.ORPort) {
  172. tor_assert(!options.DirPort);
  173. if (tor_tls_context_new(NULL, 0, NULL, 0)<0) {
  174. log_fn(LOG_ERR, "Error creating TLS context for OP.");
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /* Make sure DataDirectory exists, and is private. */
  180. tor_assert(options.DataDirectory);
  181. if (strlen(options.DataDirectory) > (512-128)) {
  182. log_fn(LOG_ERR, "DataDirectory is too long.");
  183. return -1;
  184. }
  185. if (check_private_dir(options.DataDirectory, 1)) {
  186. return -1;
  187. }
  188. /* Check the key directory. */
  189. sprintf(keydir,"%s/keys",options.DataDirectory);
  190. if (check_private_dir(keydir, 1)) {
  191. return -1;
  192. }
  193. cp = keydir + strlen(keydir); /* End of string. */
  194. /* 1. Read identity key. Make it if none is found. */
  195. strcpy(cp, "/identity.key");
  196. log_fn(LOG_INFO,"Reading/making identity key %s...",keydir);
  197. prkey = init_key_from_file(keydir);
  198. if (!prkey) return -1;
  199. set_identity_key(prkey);
  200. /* 2. Read onion key. Make it if none is found. */
  201. strcpy(cp, "/onion.key");
  202. log_fn(LOG_INFO,"Reading/making onion key %s...",keydir);
  203. prkey = init_key_from_file(keydir);
  204. if (!prkey) return -1;
  205. set_onion_key(prkey);
  206. /* 3. Initialize link key and TLS context. */
  207. if (tor_tls_context_new(get_identity_key(), 1, options.Nickname,
  208. MAX_SSL_KEY_LIFETIME) < 0) {
  209. log_fn(LOG_ERR, "Error initializing TLS context");
  210. return -1;
  211. }
  212. /* 4. Dump router descriptor to 'router.desc' */
  213. /* Must be called after keys are initialized. */
  214. if (!(router_get_my_descriptor())) {
  215. log_fn(LOG_ERR, "Error initializing descriptor.");
  216. return -1;
  217. }
  218. /* We need to add our own fingerprint so it gets recognized. */
  219. if (dirserv_add_own_fingerprint(options.Nickname, get_identity_key())) {
  220. log_fn(LOG_ERR, "Error adding own fingerprint to approved set");
  221. return -1;
  222. }
  223. tmp = mydesc = router_get_my_descriptor();
  224. if (dirserv_add_descriptor(&tmp) != 1) {
  225. log(LOG_ERR, "Unable to add own descriptor to directory.");
  226. return -1;
  227. }
  228. sprintf(keydir,"%s/router.desc", options.DataDirectory);
  229. log_fn(LOG_INFO,"Dumping descriptor to %s...",keydir);
  230. if (write_str_to_file(keydir, mydesc)) {
  231. return -1;
  232. }
  233. /* 5. Dump fingerprint to 'fingerprint' */
  234. sprintf(keydir,"%s/fingerprint", options.DataDirectory);
  235. log_fn(LOG_INFO,"Dumping fingerprint to %s...",keydir);
  236. tor_assert(strlen(options.Nickname) <= MAX_NICKNAME_LEN);
  237. strcpy(fingerprint, options.Nickname);
  238. strcat(fingerprint, " ");
  239. if (crypto_pk_get_fingerprint(get_identity_key(),
  240. fingerprint+strlen(fingerprint))<0) {
  241. log_fn(LOG_ERR, "Error computing fingerprint");
  242. return -1;
  243. }
  244. strcat(fingerprint, "\n");
  245. if (write_str_to_file(keydir, fingerprint))
  246. return -1;
  247. if(!options.DirPort)
  248. return 0;
  249. /* 6. [dirserver only] load approved-routers file */
  250. sprintf(keydir,"%s/approved-routers", options.DataDirectory);
  251. log_fn(LOG_INFO,"Loading approved fingerprints from %s...",keydir);
  252. if(dirserv_parse_fingerprint_file(keydir) < 0) {
  253. log_fn(LOG_ERR, "Error loading fingerprints");
  254. return -1;
  255. }
  256. /* 7. [dirserver only] load old directory, if it's there */
  257. sprintf(keydir,"%s/cached-directory", options.DataDirectory);
  258. log_fn(LOG_INFO,"Loading cached directory from %s...",keydir);
  259. cp = read_file_to_str(keydir);
  260. if(!cp) {
  261. log_fn(LOG_INFO,"Cached directory %s not present. Ok.",keydir);
  262. } else {
  263. if(dirserv_init_from_directory_string(cp) < 0) {
  264. log_fn(LOG_ERR, "Cached directory %s is corrupt", keydir);
  265. free(cp);
  266. return -1;
  267. }
  268. free(cp);
  269. }
  270. /* success */
  271. return 0;
  272. }
  273. /*
  274. * Clique maintenance
  275. */
  276. /** OR only: try to open connections to all of the other ORs we know about.
  277. */
  278. void router_retry_connections(void) {
  279. int i;
  280. routerinfo_t *router;
  281. routerlist_t *rl;
  282. router_get_routerlist(&rl);
  283. for (i=0;i < smartlist_len(rl->routers);i++) {
  284. router = smartlist_get(rl->routers, i);
  285. if(router_is_me(router))
  286. continue;
  287. if(!connection_exact_get_by_addr_port(router->addr,router->or_port)) {
  288. /* not in the list */
  289. log_fn(LOG_DEBUG,"connecting to OR %s:%u.",router->address,router->or_port);
  290. connection_or_connect(router);
  291. }
  292. }
  293. }
  294. /*
  295. * OR descriptor generation.
  296. */
  297. /** My routerinfo. */
  298. static routerinfo_t *desc_routerinfo = NULL;
  299. /** String representation of my descriptor, signed by me. */
  300. static char descriptor[8192];
  301. /** OR only: try to upload our signed descriptor to all the directory servers
  302. * we know about.
  303. */
  304. void router_upload_dir_desc_to_dirservers(void) {
  305. const char *s;
  306. s = router_get_my_descriptor();
  307. if (!s) {
  308. log_fn(LOG_WARN, "No descriptor; skipping upload");
  309. return;
  310. }
  311. directory_post_to_dirservers(DIR_PURPOSE_UPLOAD_DIR, s, strlen(s));
  312. }
  313. #define DEFAULT_EXIT_POLICY "reject 0.0.0.0/8,reject 169.254.0.0/16,reject 127.0.0.0/8,reject 192.168.0.0/16,reject 10.0.0.0/8,reject 172.16.0.0/12,accept *:20-22,accept *:53,accept *:79-81,accept *:110,accept *:143,accept *:443,accept *:873,accept *:993,accept *:995,accept *:1024-65535,reject *:*"
  314. /** Set the exit policy on <b>router</b> to match the exit policy in the
  315. * current configuration file. If the exit policy doesn't have a catch-all
  316. * rule, then append the default exit policy as well.
  317. */
  318. static void router_add_exit_policy_from_config(routerinfo_t *router) {
  319. struct exit_policy_t *ep;
  320. struct config_line_t default_policy;
  321. config_parse_exit_policy(options.ExitPolicy, &router->exit_policy);
  322. for (ep = router->exit_policy; ep; ep = ep->next) {
  323. if (ep->msk == 0 && ep->prt_min <= 1 && ep->prt_max >= 65535) {
  324. /* if exitpolicy includes a *:* line, then we're done. */
  325. return;
  326. }
  327. }
  328. /* Else, append the default exitpolicy. */
  329. default_policy.key = NULL;
  330. default_policy.value = DEFAULT_EXIT_POLICY;
  331. default_policy.next = NULL;
  332. config_parse_exit_policy(&default_policy, &router->exit_policy);
  333. }
  334. /** OR only: Return false if my exit policy says to allow connection to
  335. * conn. Else return true.
  336. */
  337. int router_compare_to_my_exit_policy(connection_t *conn)
  338. {
  339. tor_assert(desc_routerinfo);
  340. tor_assert(conn->addr); /* make sure it's resolved to something. this
  341. way we can't get a 'maybe' below. */
  342. return router_compare_addr_to_exit_policy(conn->addr, conn->port,
  343. desc_routerinfo->exit_policy);
  344. }
  345. /** Return true iff <b>router</b> has the same nickname as this OR. (For an
  346. * OP, always returns false.)
  347. */
  348. int router_is_me(routerinfo_t *router)
  349. {
  350. tor_assert(router);
  351. return options.Nickname && !strcasecmp(router->nickname, options.Nickname);
  352. }
  353. /** Return a routerinfo for this OR, rebuilding a fresh one if
  354. * necessary. Return NULL on error, or if called on an OP. */
  355. routerinfo_t *router_get_my_routerinfo(void)
  356. {
  357. if (!options.ORPort)
  358. return NULL;
  359. if (!desc_routerinfo) {
  360. if (router_rebuild_descriptor())
  361. return NULL;
  362. }
  363. return desc_routerinfo;
  364. }
  365. /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh
  366. * one if necessary. Return NULL on error.
  367. */
  368. const char *router_get_my_descriptor(void) {
  369. if (!desc_routerinfo) {
  370. if (router_rebuild_descriptor())
  371. return NULL;
  372. }
  373. log_fn(LOG_DEBUG,"my desc is '%s'",descriptor);
  374. return descriptor;
  375. }
  376. /** Rebuild a fresh routerinfo and signed server descriptor for this
  377. * OR. Return 0 on success, -1 on error.
  378. */
  379. int router_rebuild_descriptor(void) {
  380. routerinfo_t *ri;
  381. struct in_addr addr;
  382. char platform[256];
  383. if (!tor_inet_aton(options.Address, &addr)) {
  384. log_fn(LOG_ERR, "options.Address didn't hold an IP.");
  385. return -1;
  386. }
  387. ri = tor_malloc_zero(sizeof(routerinfo_t));
  388. ri->address = tor_strdup(options.Address);
  389. ri->nickname = tor_strdup(options.Nickname);
  390. ri->addr = (uint32_t) ntohl(addr.s_addr);
  391. ri->or_port = options.ORPort;
  392. ri->socks_port = options.SocksPort;
  393. ri->dir_port = options.DirPort;
  394. ri->published_on = time(NULL);
  395. ri->onion_pkey = crypto_pk_dup_key(get_onion_key()); /* must invoke from main thread */
  396. ri->identity_pkey = crypto_pk_dup_key(get_identity_key());
  397. get_platform_str(platform, sizeof(platform));
  398. ri->platform = tor_strdup(platform);
  399. ri->bandwidthrate = options.BandwidthRate;
  400. ri->bandwidthburst = options.BandwidthBurst;
  401. ri->exit_policy = NULL; /* zero it out first */
  402. router_add_exit_policy_from_config(ri);
  403. if (desc_routerinfo)
  404. routerinfo_free(desc_routerinfo);
  405. desc_routerinfo = ri;
  406. if (router_dump_router_to_string(descriptor, 8192, ri, get_identity_key())<0) {
  407. log_fn(LOG_WARN, "Couldn't dump router to string.");
  408. return -1;
  409. }
  410. if (ri->dir_port)
  411. ri->is_trusted_dir = 1;
  412. return 0;
  413. }
  414. /** Set <b>platform</b> (max length <b>len</b>) to a NUL-terminated short
  415. * string describing the version of Tor and the operating system we're
  416. * currently running on.
  417. */
  418. void get_platform_str(char *platform, int len)
  419. {
  420. snprintf(platform, len-1, "Tor %s on %s", VERSION, get_uname());
  421. platform[len-1] = '\0';
  422. return;
  423. }
  424. /* XXX need to audit this thing and count fenceposts. maybe
  425. * refactor so we don't have to keep asking if we're
  426. * near the end of maxlen?
  427. */
  428. #define DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  429. /** OR only: Given a routerinfo for this router, and an identity key to sign
  430. * with, encode the routerinfo as a signed server descriptor and write the
  431. * result into <b>s</b>, using at most <b>maxlen</b> bytes. Return -1 on
  432. * failure, and the number of bytes used on success.
  433. */
  434. int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router,
  435. crypto_pk_env_t *ident_key) {
  436. char *onion_pkey; /* Onion key, PEM-encoded. */
  437. char *identity_pkey; /* Identity key, PEM-encoded. */
  438. char digest[20];
  439. char signature[128];
  440. char published[32];
  441. struct in_addr in;
  442. int onion_pkeylen, identity_pkeylen;
  443. int written;
  444. int result=0;
  445. struct exit_policy_t *tmpe;
  446. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  447. char *s_tmp, *s_dup;
  448. const char *cp;
  449. routerinfo_t *ri_tmp;
  450. #endif
  451. /* Make sure the identity key matches the one in the routerinfo. */
  452. if (crypto_pk_cmp_keys(ident_key, router->identity_pkey)) {
  453. log_fn(LOG_WARN,"Tried to sign a router with a private key that didn't match router's public key!");
  454. return -1;
  455. }
  456. /* PEM-encode the onion key */
  457. if(crypto_pk_write_public_key_to_string(router->onion_pkey,
  458. &onion_pkey,&onion_pkeylen)<0) {
  459. log_fn(LOG_WARN,"write onion_pkey to string failed!");
  460. return -1;
  461. }
  462. /* PEM-encode the identity key key */
  463. if(crypto_pk_write_public_key_to_string(router->identity_pkey,
  464. &identity_pkey,&identity_pkeylen)<0) {
  465. log_fn(LOG_WARN,"write identity_pkey to string failed!");
  466. tor_free(onion_pkey);
  467. return -1;
  468. }
  469. /* Encode the publication time. */
  470. strftime(published, 32, "%Y-%m-%d %H:%M:%S", gmtime(&router->published_on));
  471. /* Generate the easy portion of the router descriptor. */
  472. result = snprintf(s, maxlen,
  473. "router %s %s %d %d %d\n"
  474. "platform %s\n"
  475. "published %s\n"
  476. "bandwidth %d %d\n"
  477. "onion-key\n%s"
  478. "signing-key\n%s",
  479. router->nickname,
  480. router->address,
  481. router->or_port,
  482. router->socks_port,
  483. router->dir_port,
  484. router->platform,
  485. published,
  486. (int) router->bandwidthrate,
  487. (int) router->bandwidthburst,
  488. onion_pkey, identity_pkey);
  489. tor_free(onion_pkey);
  490. tor_free(identity_pkey);
  491. if(result < 0 || result >= maxlen) {
  492. /* apparently different glibcs do different things on snprintf error.. so check both */
  493. return -1;
  494. }
  495. /* From now on, we use 'written' to remember the current length of 's'. */
  496. written = result;
  497. /* Write the exit policy to the end of 's'. */
  498. for(tmpe=router->exit_policy; tmpe; tmpe=tmpe->next) {
  499. in.s_addr = htonl(tmpe->addr);
  500. /* Write: "accept 1.2.3.4" */
  501. result = snprintf(s+written, maxlen-written, "%s %s",
  502. tmpe->policy_type == EXIT_POLICY_ACCEPT ? "accept" : "reject",
  503. tmpe->msk == 0 ? "*" : inet_ntoa(in));
  504. if(result < 0 || result+written > maxlen) {
  505. /* apparently different glibcs do different things on snprintf error.. so check both */
  506. return -1;
  507. }
  508. written += result;
  509. if (tmpe->msk != 0xFFFFFFFFu && tmpe->msk != 0) {
  510. /* Write "/255.255.0.0" */
  511. in.s_addr = htonl(tmpe->msk);
  512. result = snprintf(s+written, maxlen-written, "/%s", inet_ntoa(in));
  513. if (result<0 || result+written > maxlen)
  514. return -1;
  515. written += result;
  516. }
  517. if (tmpe->prt_min == 0 && tmpe->prt_max == 65535) {
  518. /* There is no port set; write ":*" */
  519. if (written > maxlen-4)
  520. return -1;
  521. strcat(s+written, ":*\n");
  522. written += 3;
  523. } else if (tmpe->prt_min == tmpe->prt_max) {
  524. /* There is only one port; write ":80". */
  525. result = snprintf(s+written, maxlen-written, ":%d\n", tmpe->prt_min);
  526. if (result<0 || result+written > maxlen)
  527. return -1;
  528. written += result;
  529. } else {
  530. /* There is a range of ports; write ":79-80". */
  531. result = snprintf(s+written, maxlen-written, ":%d-%d\n", tmpe->prt_min,
  532. tmpe->prt_max);
  533. if (result<0 || result+written > maxlen)
  534. return -1;
  535. written += result;
  536. }
  537. } /* end for */
  538. if (written > maxlen-256) /* Not enough room for signature. */
  539. return -1;
  540. /* Sign the directory */
  541. strcat(s+written, "router-signature\n");
  542. written += strlen(s+written);
  543. s[written] = '\0';
  544. if (router_get_router_hash(s, digest) < 0)
  545. return -1;
  546. if (crypto_pk_private_sign(ident_key, digest, 20, signature) < 0) {
  547. log_fn(LOG_WARN, "Error signing digest");
  548. return -1;
  549. }
  550. strcat(s+written, "-----BEGIN SIGNATURE-----\n");
  551. written += strlen(s+written);
  552. if (base64_encode(s+written, maxlen-written, signature, 128) < 0) {
  553. log_fn(LOG_WARN, "Couldn't base64-encode signature");
  554. return -1;
  555. }
  556. written += strlen(s+written);
  557. strcat(s+written, "-----END SIGNATURE-----\n");
  558. written += strlen(s+written);
  559. if (written > maxlen-2)
  560. return -1;
  561. /* include a last '\n' */
  562. s[written] = '\n';
  563. s[written+1] = 0;
  564. #ifdef DEBUG_ROUTER_DUMP_ROUTER_TO_STRING
  565. cp = s_tmp = s_dup = tor_strdup(s);
  566. ri_tmp = router_parse_entry_from_string(cp, NULL);
  567. if (!ri_tmp) {
  568. log_fn(LOG_ERR, "We just generated a router descriptor we can't parse: <<%s>>",
  569. s);
  570. return -1;
  571. }
  572. free(s_dup);
  573. routerinfo_free(ri_tmp);
  574. #endif
  575. return written+1;
  576. }
  577. /*
  578. Local Variables:
  579. mode:c
  580. indent-tabs-mode:nil
  581. c-basic-offset:2
  582. End:
  583. */