diff options
author | Nick Mathewson <nickm@torproject.org> | 2004-10-06 13:31:48 +0000 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2004-10-06 13:31:48 +0000 |
commit | 2bba65148bd8b427bf3bb5753c371da8395552fc (patch) | |
tree | cf475793e53f8ac4e78c0ae3f3113e1368e67c78 /src/or | |
parent | b08ff7b1f72ce89a013c17cd7915c3af7c9e476e (diff) | |
download | tor-2bba65148bd8b427bf3bb5753c371da8395552fc.tar.gz tor-2bba65148bd8b427bf3bb5753c371da8395552fc.zip |
Refactor fingerprint handling: remember hex digests (no spaces) instead of beautified fingerprints
svn:r2422
Diffstat (limited to 'src/or')
-rw-r--r-- | src/or/dirserv.c | 46 | ||||
-rw-r--r-- | src/or/router.c | 4 | ||||
-rw-r--r-- | src/or/test.c | 16 |
3 files changed, 32 insertions, 34 deletions
diff --git a/src/or/dirserv.c b/src/or/dirserv.c index a0eaf28ac2..0ff8959ec2 100644 --- a/src/or/dirserv.c +++ b/src/or/dirserv.c @@ -23,13 +23,12 @@ static int runningrouters_is_dirty = 1; static int list_running_servers(char **nicknames_out); static void directory_remove_unrecognized(void); static int dirserv_regenerate_directory(void); -static void encode_digest_to_fingerprint(char *fp, const char *digest); /************** Fingerprint handling code ************/ typedef struct fingerprint_entry_t { char *nickname; - char *fingerprint; + char *fingerprint; /**< Stored as HEX_DIGEST_LEN characters, followed by a NUL */ } fingerprint_entry_t; /** List of nickname-\>identity fingerprint mappings for all the routers @@ -58,6 +57,7 @@ add_fingerprint_to_dir(const char *nickname, const char *fp) ent = tor_malloc(sizeof(fingerprint_entry_t)); ent->nickname = tor_strdup(nickname); ent->fingerprint = tor_strdup(fp); + tor_strstrip(ent->fingerprint, " "); smartlist_add(fingerprint_list, ent); } @@ -67,7 +67,7 @@ int dirserv_add_own_fingerprint(const char *nickname, crypto_pk_env_t *pk) { char fp[FINGERPRINT_LEN+1]; - if (crypto_pk_get_fingerprint(pk, fp)<0) { + if (crypto_pk_get_fingerprint(pk, fp, 0)<0) { log_fn(LOG_ERR, "Error computing fingerprint"); return -1; } @@ -118,6 +118,7 @@ dirserv_parse_fingerprint_file(const char *fname) ent = tor_malloc(sizeof(fingerprint_entry_t)); ent->nickname = tor_strdup(nickname); ent->fingerprint = tor_strdup(fingerprint); + tor_strstrip(ent->fingerprint, " "); smartlist_add(fingerprint_list_new, ent); } } @@ -169,7 +170,7 @@ dirserv_router_fingerprint_is_known(const routerinfo_t *router) log_fn(LOG_INFO,"no fingerprint found for %s",router->nickname); return 0; } - if (crypto_pk_get_fingerprint(router->identity_pkey, fp)) { + if (crypto_pk_get_fingerprint(router->identity_pkey, fp, 0)) { log_fn(LOG_WARN,"error computing fingerprint"); return -1; } @@ -187,37 +188,16 @@ dirserv_router_fingerprint_is_known(const routerinfo_t *router) * return that router's nickname. Otherwise return NULL. */ const char *dirserv_get_nickname_by_digest(const char *digest) { - char fp[FINGERPRINT_LEN+1]; if (!fingerprint_list) return NULL; tor_assert(digest); - encode_digest_to_fingerprint(fp, digest); SMARTLIST_FOREACH(fingerprint_list, fingerprint_entry_t*, ent, - { if (!strcasecmp(fp, ent->fingerprint)) + { if (!strcasecmp(digest, ent->fingerprint)) return ent->nickname; } ); return NULL; } -/** Set fp to contain the hex encoding of <b>digest</b>, with every 4 - * hex digits separated by a space. The digest must be DIGEST_LEN bytes long; - * fp must have FINGERPRINT_LEN+1 bytes free. */ -static void encode_digest_to_fingerprint(char *fp, const char *digest) -{ - char hexdigest[HEX_DIGEST_LEN+1]; - int i,j; - - tor_assert(fp&&digest); - - base16_encode(hexdigest, sizeof(hexdigest), digest, DIGEST_LEN); - for (i=j=0;j<HEX_DIGEST_LEN;++i,++j) { - fp[i]=hexdigest[j]; - if ((j%4)==3 && j != 39) - fp[++i]=' '; - } - fp[i]='\0'; -} - /** Return true iff any router named <b>nickname</b> with <b>digest</b> * is in the verified fingerprint list. */ static int @@ -348,7 +328,7 @@ dirserv_add_descriptor(const char **desc) char fp[FINGERPRINT_LEN+1]; log_fn(LOG_INFO, "Unknown nickname %s (%s:%d). Adding.", ri->nickname, ri->address, ri->or_port); - if (crypto_pk_get_fingerprint(ri->identity_pkey, fp) < 0) { + if (crypto_pk_get_fingerprint(ri->identity_pkey, fp, 1) < 0) { log_fn(LOG_WARN, "Error computing fingerprint for %s", ri->nickname); } else { log_fn(LOG_INFO, "Fingerprint line: %s %s", ri->nickname, fp); @@ -572,11 +552,12 @@ dirserv_dump_directory_to_string(char *s, unsigned int maxlen, crypto_pk_env_t *private_key) { char *cp, *eos; + char *identity_pkey; /* Identity key, PEM-encoded. */ char digest[20]; char signature[128]; char published[33]; time_t published_on; - int i; + int i, identity_pkeylen; eos = s+maxlen; if (!descriptor_list) @@ -584,6 +565,14 @@ dirserv_dump_directory_to_string(char *s, unsigned int maxlen, if (list_running_servers(&cp)) return -1; +#if 0 + /* PEM-encode the identity key key */ + if(crypto_pk_write_public_key_to_string(private_key, + &identity_pkey,&identity_pkeylen)<0) { + log_fn(LOG_WARN,"write identity_pkey to string failed!"); + return -1; + } +#endif dirserv_remove_old_servers(ROUTER_MAX_AGE); published_on = time(NULL); format_iso_time(published, published_on); @@ -595,6 +584,7 @@ dirserv_dump_directory_to_string(char *s, unsigned int maxlen, published, options.RecommendedVersions, cp); tor_free(cp); + tor_free(identity_pkey); i = strlen(s); cp = s+i; diff --git a/src/or/router.c b/src/or/router.c index e37c20902e..032f3969e0 100644 --- a/src/or/router.c +++ b/src/or/router.c @@ -329,7 +329,7 @@ int init_keys(void) { strcpy(fingerprint, options.Nickname); strcat(fingerprint, " "); if (crypto_pk_get_fingerprint(get_identity_key(), - fingerprint+strlen(fingerprint))<0) { + fingerprint+strlen(fingerprint), 1)<0) { log_fn(LOG_ERR, "Error computing fingerprint"); return -1; } @@ -605,7 +605,7 @@ int router_dump_router_to_string(char *s, int maxlen, routerinfo_t *router, } /* record our fingerprint, so we can include it in the descriptor */ - if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint)<0) { + if (crypto_pk_get_fingerprint(router->identity_pkey, fingerprint, 1)<0) { log_fn(LOG_ERR, "Error computing fingerprint"); return -1; } diff --git a/src/or/test.c b/src/or/test.c index 2482f3028c..800ad72cb0 100644 --- a/src/or/test.c +++ b/src/or/test.c @@ -483,6 +483,7 @@ test_util() { struct tm a_time; smartlist_t *sl; char timestr[RFC1123_TIME_LEN+1]; + char buf[1024]; time_t t_res; int i; @@ -596,6 +597,13 @@ test_util() { test_streq("z", smartlist_get(sl, 3)); test_streq("zhasd <> <> bnud<>", smartlist_get(sl, 4)); + /* Test tor_strstrip() */ + strcpy(buf, "Testing 1 2 3"); + test_eq(0, tor_strstrip(buf, ",!")); + test_streq(buf, "Testing 1 2 3"); + strcpy(buf, "!Testing 1 2 3?"); + test_eq(5, tor_strstrip(buf, "!? ")); + test_streq(buf, "Testing123"); /* XXXX test older functions. */ smartlist_free(sl); @@ -887,7 +895,7 @@ test_dir_format() strcat(buf2, "\n" "published 1970-01-01 00:00:00\n" "opt fingerprint "); - crypto_pk_get_fingerprint(pk2, fingerprint); + crypto_pk_get_fingerprint(pk2, fingerprint, 1); strcat(buf2, fingerprint); strcat(buf2, "\nopt uptime 0\n" /* XXX the "0" above is hardcoded, but even if we made it reflect @@ -952,9 +960,9 @@ test_dir_format() #endif /* Okay, now for the directories. */ - crypto_pk_get_fingerprint(pk2, buf); + crypto_pk_get_fingerprint(pk2, buf, 1); add_fingerprint_to_dir("Magri", buf); - crypto_pk_get_fingerprint(pk1, buf); + crypto_pk_get_fingerprint(pk1, buf, 1); add_fingerprint_to_dir("Fred", buf); /* Make sure routers aren't too far in the past any more. */ r1.published_on = time(NULL); @@ -1097,7 +1105,7 @@ main(int c, char**v){ test_onion(); test_onion_handshake(); puts("\n========================= Directory Formats ==============="); - /* add_stream_log(LOG_DEBUG, NULL, stdout); */ + /* add_stream_log(LOG_DEBUG, LOG_ERR, "<stdout>", stdout); */ test_dir_format(); puts("\n========================= Rendezvous functionality ========"); test_rend_fns(); |