Skip to content

Commit

Permalink
Merge ebbc2c3 into adeecce
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Apr 12, 2019
2 parents adeecce + ebbc2c3 commit 8026224
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 123 deletions.
5 changes: 5 additions & 0 deletions changes/ticket29660
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
o Code simplification and refactoring:
- Remove redundant return values in crypto_format, and the associated
return value checks elsewhere in the code. Make the implementations in
crypto_format consistent, and remove redundant code.
Resolves ticket 29660.
3 changes: 1 addition & 2 deletions src/feature/dirauth/dirvote.c
Original file line number Diff line number Diff line change
Expand Up @@ -3914,8 +3914,7 @@ dirvote_format_microdesc_vote_line(char *out_buf, size_t out_buf_len,
",");
tor_assert(microdesc_consensus_methods);

if (digest256_to_base64(d64, md->digest)<0)
goto out;
digest256_to_base64(d64, md->digest);

if (tor_snprintf(out_buf, out_buf_len, "m %s sha256=%s\n",
microdesc_consensus_methods, d64)<0)
Expand Down
16 changes: 3 additions & 13 deletions src/feature/hs/hs_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ purge_hid_serv_request(const ed25519_public_key_t *identity_pk)
* some point and we don't care about those anymore. */
hs_build_blinded_pubkey(identity_pk, NULL, 0,
hs_get_time_period_num(0), &blinded_pk);
if (BUG(ed25519_public_to_base64(base64_blinded_pk, &blinded_pk) < 0)) {
return;
}
ed25519_public_to_base64(base64_blinded_pk, &blinded_pk);
/* Purge last hidden service request from cache for this blinded key. */
hs_purge_hid_serv_from_last_hid_serv_requests(base64_blinded_pk);
}
Expand Down Expand Up @@ -354,7 +352,6 @@ directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
ed25519_public_key_t blinded_pubkey;
char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
hs_ident_dir_conn_t hs_conn_dir_ident;
int retval;

tor_assert(hsdir);
tor_assert(onion_identity_pk);
Expand All @@ -363,10 +360,7 @@ directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
current_time_period, &blinded_pubkey);
/* ...and base64 it. */
retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
if (BUG(retval < 0)) {
return HS_CLIENT_FETCH_ERROR;
}
ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);

/* Copy onion pk to a dir_ident so that we attach it to the dir conn */
hs_ident_dir_conn_init(onion_identity_pk, &blinded_pubkey,
Expand Down Expand Up @@ -405,7 +399,6 @@ directory_launch_v3_desc_fetch(const ed25519_public_key_t *onion_identity_pk,
STATIC routerstatus_t *
pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
{
int retval;
char base64_blinded_pubkey[ED25519_BASE64_LEN + 1];
uint64_t current_time_period = hs_get_time_period_num(0);
smartlist_t *responsible_hsdirs = NULL;
Expand All @@ -418,10 +411,7 @@ pick_hsdir_v3(const ed25519_public_key_t *onion_identity_pk)
hs_build_blinded_pubkey(onion_identity_pk, NULL, 0,
current_time_period, &blinded_pubkey);
/* ...and base64 it. */
retval = ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);
if (BUG(retval < 0)) {
return NULL;
}
ed25519_public_to_base64(base64_blinded_pubkey, &blinded_pubkey);

/* Get responsible hsdirs of service for this time period */
responsible_hsdirs = smartlist_new();
Expand Down
23 changes: 5 additions & 18 deletions src/feature/hs/hs_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ hs_control_desc_event_failed(const hs_ident_dir_conn_t *ident,
tor_assert(reason);

/* Build onion address and encoded blinded key. */
IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
&ident->blinded_pk) < 0) {
return;
}
ed25519_public_to_base64(base64_blinded_pk, &ident->blinded_pk);
hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

control_event_hsv3_descriptor_failed(onion_address, base64_blinded_pk,
Expand All @@ -99,10 +96,7 @@ hs_control_desc_event_received(const hs_ident_dir_conn_t *ident,
tor_assert(hsdir_id_digest);

/* Build onion address and encoded blinded key. */
IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
&ident->blinded_pk) < 0) {
return;
}
ed25519_public_to_base64(base64_blinded_pk, &ident->blinded_pk);
hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

control_event_hsv3_descriptor_received(onion_address, base64_blinded_pk,
Expand All @@ -123,9 +117,7 @@ hs_control_desc_event_created(const char *onion_address,
tor_assert(blinded_pk);

/* Build base64 encoded blinded key. */
IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
return;
}
ed25519_public_to_base64(base64_blinded_pk, blinded_pk);

/* Version 3 doesn't use the replica number in its descriptor ID computation
* so we pass negative value so the control port subsystem can ignore it. */
Expand All @@ -151,9 +143,7 @@ hs_control_desc_event_upload(const char *onion_address,
tor_assert(hsdir_index);

/* Build base64 encoded blinded key. */
IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk, blinded_pk) < 0) {
return;
}
ed25519_public_to_base64(base64_blinded_pk, blinded_pk);

control_event_hs_descriptor_upload(onion_address, hsdir_id_digest,
base64_blinded_pk,
Expand Down Expand Up @@ -196,10 +186,7 @@ hs_control_desc_event_content(const hs_ident_dir_conn_t *ident,
tor_assert(hsdir_id_digest);

/* Build onion address and encoded blinded key. */
IF_BUG_ONCE(ed25519_public_to_base64(base64_blinded_pk,
&ident->blinded_pk) < 0) {
return;
}
ed25519_public_to_base64(base64_blinded_pk, &ident->blinded_pk);
hs_build_address(&ident->identity_pk, HS_VERSION_THREE, onion_address);

control_event_hs_descriptor_content(onion_address, base64_blinded_pk,
Expand Down
27 changes: 7 additions & 20 deletions src/feature/hs/hs_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,7 @@ encode_enc_key(const hs_desc_intro_point_t *ip)
tor_assert(ip);

/* Base64 encode the encryption key for the "enc-key" field. */
if (curve25519_public_to_base64(key_b64, &ip->enc_key) < 0) {
goto done;
}
curve25519_public_to_base64(key_b64, &ip->enc_key);
if (tor_cert_encode_ed22519(ip->enc_key_cert, &encoded_cert) < 0) {
goto done;
}
Expand All @@ -421,7 +419,7 @@ encode_enc_key(const hs_desc_intro_point_t *ip)
}

/* Encode an introduction point onion key. Return a newly allocated string
* with it. On failure, return NULL. */
* with it. Can not fail. */
static char *
encode_onion_key(const hs_desc_intro_point_t *ip)
{
Expand All @@ -431,12 +429,9 @@ encode_onion_key(const hs_desc_intro_point_t *ip)
tor_assert(ip);

/* Base64 encode the encryption key for the "onion-key" field. */
if (curve25519_public_to_base64(key_b64, &ip->onion_key) < 0) {
goto done;
}
curve25519_public_to_base64(key_b64, &ip->onion_key);
tor_asprintf(&encoded, "%s ntor %s", str_ip_onion_key, key_b64);

done:
return encoded;
}

Expand Down Expand Up @@ -797,8 +792,8 @@ get_inner_encrypted_layer_plaintext(const hs_descriptor_t *desc)
/* Create the middle layer of the descriptor, which includes the client auth
* data and the encrypted inner layer (provided as a base64 string at
* <b>layer2_b64_ciphertext</b>). Return a newly-allocated string with the
* layer plaintext, or NULL if an error occurred. It's the responsibility of
* the caller to free the returned string. */
* layer plaintext. It's the responsibility of the caller to free the returned
* string. Can not fail. */
static char *
get_outer_encrypted_layer_plaintext(const hs_descriptor_t *desc,
const char *layer2_b64_ciphertext)
Expand All @@ -817,10 +812,7 @@ get_outer_encrypted_layer_plaintext(const hs_descriptor_t *desc,
tor_assert(!tor_mem_is_zero((char *) ephemeral_pubkey->public_key,
CURVE25519_PUBKEY_LEN));

if (curve25519_public_to_base64(ephemeral_key_base64,
ephemeral_pubkey) < 0) {
goto done;
}
curve25519_public_to_base64(ephemeral_key_base64, ephemeral_pubkey);
smartlist_add_asprintf(lines, "%s %s\n",
str_desc_auth_key, ephemeral_key_base64);

Expand All @@ -845,7 +837,6 @@ get_outer_encrypted_layer_plaintext(const hs_descriptor_t *desc,

layer1_str = smartlist_join_strings(lines, "", 0, NULL);

done:
/* We need to memwipe all lines because it contains the ephemeral key */
SMARTLIST_FOREACH(lines, char *, a, memwipe(a, 0, strlen(a)));
SMARTLIST_FOREACH(lines, char *, a, tor_free(a));
Expand Down Expand Up @@ -1091,11 +1082,7 @@ desc_encode_v3(const hs_descriptor_t *desc,
tor_free(encoded_str);
goto err;
}
if (ed25519_signature_to_base64(ed_sig_b64, &sig) < 0) {
log_warn(LD_BUG, "Can't base64 encode descriptor signature!");
tor_free(encoded_str);
goto err;
}
ed25519_signature_to_base64(ed_sig_b64, &sig);
/* Create the signature line. */
smartlist_add_asprintf(lines, "%s %s", str_signature, ed_sig_b64);
}
Expand Down
13 changes: 4 additions & 9 deletions src/feature/relay/router.c
Original file line number Diff line number Diff line change
Expand Up @@ -2728,11 +2728,8 @@ router_dump_router_to_string(routerinfo_t *router,
log_err(LD_BUG,"Couldn't base64-encode signing key certificate!");
goto err;
}
if (ed25519_public_to_base64(ed_fp_base64,
&router->cache_info.signing_key_cert->signing_key)<0) {
log_err(LD_BUG,"Couldn't base64-encode identity key\n");
goto err;
}
ed25519_public_to_base64(ed_fp_base64,
&router->cache_info.signing_key_cert->signing_key);
tor_asprintf(&ed_cert_line, "identity-ed25519\n"
"-----BEGIN ED25519 CERT-----\n"
"%s"
Expand Down Expand Up @@ -2977,8 +2974,7 @@ router_dump_router_to_string(routerinfo_t *router,
if (ed25519_sign(&sig, (const uint8_t*)digest, DIGEST256_LEN,
signing_keypair) < 0)
goto err;
if (ed25519_signature_to_base64(buf, &sig) < 0)
goto err;
ed25519_signature_to_base64(buf, &sig);

smartlist_add_asprintf(chunks, "%s\n", buf);
}
Expand Down Expand Up @@ -3252,8 +3248,7 @@ extrainfo_dump_to_string(char **s_out, extrainfo_t *extrainfo,
if (ed25519_sign(&ed_sig, (const uint8_t*)sha256_digest, DIGEST256_LEN,
signing_keypair) < 0)
goto err;
if (ed25519_signature_to_base64(buf, &ed_sig) < 0)
goto err;
ed25519_signature_to_base64(buf, &ed_sig);

smartlist_add_asprintf(chunks, "%s\n", buf);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/crypt_ops/crypto_curve25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ STATIC int curve25519_basepoint_impl(uint8_t *output, const uint8_t *secret);

int curve25519_public_from_base64(curve25519_public_key_t *pkey,
const char *input);
int curve25519_public_to_base64(char *output,
const curve25519_public_key_t *pkey);
void curve25519_public_to_base64(char *output,
const curve25519_public_key_t *pkey);

void curve25519_set_impl_params(int use_ed);
void curve25519_init(void);
Expand Down

0 comments on commit 8026224

Please sign in to comment.