[or-cvs] Refactor fingerprint handling: remember hex digests (no spa...
Nick Mathewson
nickm at seul.org
Wed Oct 6 13:31:50 UTC 2004
Update of /home/or/cvsroot/src/or
In directory moria.mit.edu:/tmp/cvs-serv24913/src/or
Modified Files:
dirserv.c router.c test.c
Log Message:
Refactor fingerprint handling: remember hex digests (no spaces) instead of beautified fingerprints
Index: dirserv.c
===================================================================
RCS file: /home/or/cvsroot/src/or/dirserv.c,v
retrieving revision 1.88
retrieving revision 1.89
diff -u -d -r1.88 -r1.89
--- dirserv.c 3 Oct 2004 02:37:51 -0000 1.88
+++ dirserv.c 6 Oct 2004 13:31:48 -0000 1.89
@@ -23,13 +23,12 @@
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 @@
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 @@
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 @@
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 @@
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 @@
* 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 @@
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 @@
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 @@
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 @@
published, options.RecommendedVersions, cp);
tor_free(cp);
+ tor_free(identity_pkey);
i = strlen(s);
cp = s+i;
Index: router.c
===================================================================
RCS file: /home/or/cvsroot/src/or/router.c,v
retrieving revision 1.89
retrieving revision 1.90
diff -u -d -r1.89 -r1.90
--- router.c 29 Sep 2004 06:52:35 -0000 1.89
+++ router.c 6 Oct 2004 13:31:48 -0000 1.90
@@ -329,7 +329,7 @@
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 @@
}
/* 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;
}
Index: test.c
===================================================================
RCS file: /home/or/cvsroot/src/or/test.c,v
retrieving revision 1.117
retrieving revision 1.118
diff -u -d -r1.117 -r1.118
--- test.c 3 Oct 2004 02:37:52 -0000 1.117
+++ test.c 6 Oct 2004 13:31:48 -0000 1.118
@@ -483,6 +483,7 @@
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_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 @@
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 @@
#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 @@
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();
More information about the tor-commits
mailing list