Skip to content

Commit

Permalink
global: use base32 encoding by default
Browse files Browse the repository at this point in the history
* base32 is needed since URL segments can be
  up to 63 characters long and base16 is too long
* unify location of id parsing code
  • Loading branch information
mwarning committed Oct 31, 2024
1 parent 6357985 commit 51c3935
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 229 deletions.
6 changes: 3 additions & 3 deletions src/announces.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void announces_print(FILE *fp)
struct announcement_t *announces_add(FILE *fp, const char query[], time_t lifetime)
{
char squery[QUERY_MAX_SIZE];
uint8_t id[SHA1_BIN_LENGTH];
uint8_t id[ID_BINARY_LENGTH];
struct announcement_t *cur;
struct announcement_t *new;
int port = gconf->dht_port;
Expand Down Expand Up @@ -130,7 +130,7 @@ struct announcement_t *announces_add(FILE *fp, const char query[], time_t lifeti

// Prepend new entry
new = (struct announcement_t*) calloc(1, sizeof(struct announcement_t));
memcpy(new->id, id, SHA1_BIN_LENGTH);
memcpy(new->id, id, ID_BINARY_LENGTH);
memcpy(new->query, squery, strlen(query));
new->port = port;
new->refresh = now - 1; // Send first announcement as soon as possible
Expand Down Expand Up @@ -168,7 +168,7 @@ static void announcement_free(struct announcement_t *value)
void announces_remove(FILE *fp, const char query[])
{
char squery[QUERY_MAX_SIZE];
uint8_t id[SHA1_BIN_LENGTH];
uint8_t id[ID_BINARY_LENGTH];
struct announcement_t *pre;
struct announcement_t *cur;

Expand Down
2 changes: 1 addition & 1 deletion src/announces.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

struct announcement_t {
struct announcement_t *next;
uint8_t id[SHA1_BIN_LENGTH];
uint8_t id[ID_BINARY_LENGTH];
char query[QUERY_MAX_SIZE];
int port;
time_t lifetime; // Keep entry refreshed until the lifetime expires
Expand Down
26 changes: 16 additions & 10 deletions src/ext-bob.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,20 @@ void bob_auth_end(struct bob_resource *resource, int state)
}

// Try to create a DHT id from a sanitized key query
bool bob_parse_id(uint8_t id[], size_t idlen, const char query[], size_t querylen)
bool bob_parse_id(uint8_t id[], const char query[], size_t querylen)
{
uint8_t bin[32] = {0};
uint8_t bin[ECPARAMS_SIZE];

if (bytes_from_base32(bin, sizeof(bin), query, querylen)
|| bytes_from_base16(bin, sizeof(bin), query, querylen)) {
memcpy(id, bin, idlen);
return true;
if (base16decsize(querylen) == sizeof(bin)
&& base16dec(bin, sizeof(bin), query, querylen)) {
memcpy(id, bin, ID_BINARY_LENGTH);
return true;
}

if (base32decsize(querylen) == sizeof(bin)
&& base32dec(bin, sizeof(bin), query, querylen)) {
memcpy(id, bin, ID_BINARY_LENGTH);
return true;
}

return false;
Expand Down Expand Up @@ -133,7 +139,7 @@ static void bob_send_challenge(int sock, struct bob_resource *resource)
resource->challenges_send += 1;
log_debug("Send challenge to %s: %s (try %d)",
str_addr(&resource->addr),
bytes_to_base32(hexbuf, sizeof(hexbuf), buf, sizeof(buf)),
base32enc(hexbuf, sizeof(hexbuf), buf, sizeof(buf)),
resource->challenges_send
);

Expand Down Expand Up @@ -166,7 +172,7 @@ void bob_trigger_auth(void)
// Hex to binary and compressed form (assuming even Y => 0x02)
compressed[0] = 0x02;

if (!bytes_from_base32(compressed + 1, sizeof(compressed) - 1, query, strlen(query))) {
if (!base32dec(compressed + 1, sizeof(compressed) - 1, query, strlen(query))) {
log_error("BOB: Unexpected query length: %s", query);
bob_auth_end(resource, AUTH_ERROR);
return;
Expand Down Expand Up @@ -242,7 +248,7 @@ static const char *get_pkey_base32(const mbedtls_pk_context *ctx)

mbedtls_mpi_write_binary(&mbedtls_pk_ec(*ctx)->MBEDTLS_PRIVATE(Q).MBEDTLS_PRIVATE(X), buf, sizeof(buf));

return bytes_to_base32(hexbuf, sizeof(hexbuf), buf, sizeof(buf));
return base32enc(hexbuf, sizeof(hexbuf), buf, sizeof(buf));
}

static bool bob_init()
Expand Down Expand Up @@ -482,7 +488,7 @@ void bob_encrypt_challenge(int sock, uint8_t buf[], size_t buflen, IP *addr)
}
} else {
log_debug("BOB: Secret key not found for public key: %s",
bytes_to_base32(hexbuf, sizeof(hexbuf), pkey, ECPARAMS_SIZE)
base32enc(hexbuf, sizeof(hexbuf), pkey, ECPARAMS_SIZE)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ext-bob.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdio.h>

// Decide if the query is meant to be authorized via BOB
bool bob_parse_id(uint8_t id[], size_t ilen, const char query[], size_t querylen);
bool bob_parse_id(uint8_t id[], const char query[], size_t querylen);
void bob_trigger_auth(void);

// .. for kad.c - remove?
Expand Down
5 changes: 2 additions & 3 deletions src/ext-tls-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ static void tls_handle(int rc, int fd)
}

// Try to create a DHT id from sanitized domain query
bool tls_client_parse_id(uint8_t id[], size_t idlen, const char query[], size_t querylen)
bool tls_client_parse_id(uint8_t id[], const char query[], size_t querylen)
{
uint8_t hash[32] = {0};

Expand All @@ -223,8 +223,7 @@ bool tls_client_parse_id(uint8_t id[], size_t idlen, const char query[], size_t
mbedtls_sha256_update(&ctx, (uint8_t*) &query[0], querylen);
mbedtls_sha256_finish(&ctx, hash);

memset(id, 0, idlen);
memcpy(id, hash, MIN(idlen, sizeof(hash)));
memcpy(id, hash, ID_BINARY_LENGTH);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ext-tls-client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bool tls_client_add_ca(const char ca_path[]);

// Decide if the query is meant to be authorized via an CA
bool tls_client_parse_id(uint8_t id[], size_t idlen, const char query[], size_t querylen);
bool tls_client_parse_id(uint8_t id[], const char query[], size_t querylen);

// Trigger authorisation of results; need to be called multiple times.
void tls_client_trigger_auth(void);
Expand Down
8 changes: 4 additions & 4 deletions src/kad.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ int dht_random_bytes(void *buf, size_t size)

int kad_setup(void)
{
uint8_t node_id[SHA1_BIN_LENGTH];
uint8_t node_id[ID_BINARY_LENGTH];

#ifdef DEBUG
// Let the DHT output debug text
dht_debug = stdout;
#endif

bytes_random(node_id, SHA1_BIN_LENGTH);
bytes_random(node_id, ID_BINARY_LENGTH);

g_dht_socket4 = net_bind("KAD", "0.0.0.0", gconf->dht_port, gconf->dht_ifname, IPPROTO_UDP);
g_dht_socket6 = net_bind("KAD", "::", gconf->dht_port, gconf->dht_ifname, IPPROTO_UDP);
Expand Down Expand Up @@ -515,12 +515,12 @@ const struct search_t *kad_lookup(const char query[])
*/
bool kad_lookup_node(const char query[], IP *addr_return)
{
uint8_t id[SHA1_BIN_LENGTH];
uint8_t id[ID_BINARY_LENGTH];
struct search *sr;
int i;
bool rc;

if (!bytes_from_base16hex(id, query, SHA1_HEX_LENGTH) {
if (base16dec(id, sizeof(id), query, ID_BASE16_LENGTH) != sizeof(id)) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#define PROGRAM_NAME "kadnode"
#define PROGRAM_VERSION "3.0.0"

#define SHA1_BIN_LENGTH 20
#define SHA1_HEX_LENGTH (2 * SHA1_BIN_LENGTH)
#define ID_BINARY_LENGTH 20
#define ID_BASE16_LENGTH 40
#define ID_BASE32_LENGTH 32

// Default addresses and ports
#define LPD_ADDR4 "239.192.152.143"
Expand Down
29 changes: 22 additions & 7 deletions src/searches.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct search_t *searches_find_by_id(const uint8_t id[])

search = g_searches;
while (search) {
if (memcmp(search->id, id, SHA1_BIN_LENGTH) == 0) {
if (memcmp(search->id, id, ID_BINARY_LENGTH) == 0) {
return search;
}
search = search->next;
Expand Down Expand Up @@ -133,7 +133,7 @@ void searches_remove_by_id(const uint8_t id[])
search = g_searches;
prev = g_searches;
while (search) {
if (0 == memcmp(&search->id, id, SHA1_BIN_LENGTH)) {
if (0 == memcmp(&search->id, id, ID_BINARY_LENGTH)) {
if (search == g_searches) {
g_searches = search->next;
} else {
Expand Down Expand Up @@ -312,6 +312,21 @@ static void search_restart(struct search_t *search)
}
}

static bool hex_parse_id(uint8_t id[], const char query[], size_t querylen)
{
if (base16decsize(querylen) == ID_BINARY_LENGTH
&& base16dec(id, ID_BINARY_LENGTH, query, querylen)) {
return true;
}

if (base32decsize(querylen) == ID_BINARY_LENGTH
&& base32dec(id, ID_BINARY_LENGTH, query, querylen)) {
return true;
}

return false;
}

int parse_query(uint8_t id_ret[], char squery_ret[], int *port_ret, const char query[])
{
const char *colon = strchr(query, ':');
Expand All @@ -334,21 +349,21 @@ int parse_query(uint8_t id_ret[], char squery_ret[], int *port_ret, const char q
}
squery_len = strlen(squery_ret);

#ifdef TLS
if (tls_client_parse_id(id_ret, SHA1_BIN_LENGTH, squery_ret, squery_len)) {
#ifdef TLS
if (tls_client_parse_id(id_ret, squery_ret, squery_len)) {
// Use TLS authentication
// For e.g. example.com.p2p
return QUERY_TYPE_TLS;
} else
#endif
#ifdef BOB
if (bob_parse_id(id_ret, SHA1_BIN_LENGTH, squery_ret, squery_len)) {
if (bob_parse_id(id_ret, squery_ret, squery_len)) {
// Use Bob authentication
// For e.g. <32BytePublicKey>.p2p
return QUERY_TYPE_BOB;
} else
#endif
if (hex_parse_id(id_ret, SHA1_BIN_LENGTH, squery_ret, squery_len)) {
if (hex_parse_id(id_ret, squery_ret, squery_len)) {
// Use no authentication
// For e.g. <20ByteHashKey>.p2p
return QUERY_TYPE_NONE;
Expand All @@ -362,7 +377,7 @@ int parse_query(uint8_t id_ret[], char squery_ret[], int *port_ret, const char q
struct search_t* searches_start(const char query[])
{
char squery[QUERY_MAX_SIZE];
uint8_t id[SHA1_BIN_LENGTH];
uint8_t id[ID_BINARY_LENGTH];
auth_callback_t *cb;
struct search_t* search;

Expand Down
2 changes: 1 addition & 1 deletion src/searches.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct result_t {
// A bucket of results received when in search of an id
struct search_t {
struct search_t *next;
uint8_t id[SHA1_BIN_LENGTH];
uint8_t id[ID_BINARY_LENGTH];
char query[QUERY_MAX_SIZE]; // sanitized query (lower case, not .p2p TLD)
bool done;
time_t start_time;
Expand Down
Loading

0 comments on commit 51c3935

Please sign in to comment.