#define HS_DESCRIPTOR_PRIVATE
#include "core/or/or.h"
#include "app/config/config.h"
#include "trunnel/ed25519_cert.h"
#include "feature/hs/hs_descriptor.h"
#include "core/or/circuitbuild.h"
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/crypt_ops/crypto_util.h"
#include "feature/dirparse/parsecommon.h"
#include "feature/rend/rendcache.h"
#include "feature/hs/hs_cache.h"
#include "feature/hs/hs_config.h"
#include "feature/nodelist/torcert.h"
#include "lib/memarea/memarea.h"
#include "lib/crypt_ops/crypto_format.h"
#include "core/or/extend_info_st.h"
#define str_hs_desc "hs-descriptor"
#define str_desc_cert "descriptor-signing-key-cert"
#define str_rev_counter "revision-counter"
#define str_superencrypted "superencrypted"
#define str_encrypted "encrypted"
#define str_signature "signature"
#define str_lifetime "descriptor-lifetime"
#define str_create2_formats "create2-formats"
#define str_intro_auth_required "intro-auth-required"
#define str_single_onion "single-onion-service"
#define str_intro_point "introduction-point"
#define str_ip_onion_key "onion-key"
#define str_ip_auth_key "auth-key"
#define str_ip_enc_key "enc-key"
#define str_ip_enc_key_cert "enc-key-cert"
#define str_ip_legacy_key "legacy-key"
#define str_ip_legacy_key_cert "legacy-key-cert"
#define str_intro_point_start "\n" str_intro_point " "
#define str_enc_const_superencryption "hsdir-superencrypted-data"
#define str_enc_const_encryption "hsdir-encrypted-data"
#define str_desc_sig_prefix "Tor onion service descriptor sig v3"
#define str_desc_auth_type "desc-auth-type"
#define str_desc_auth_key "desc-auth-ephemeral-key"
#define str_desc_auth_client "auth-client"
#define str_encrypted "encrypted"
static const struct {
hs_desc_auth_type_t type;
const char *identifier;
} intro_auth_types[] = {
{ HS_DESC_AUTH_ED25519, "ed25519" },
{ 0, NULL }
};
static token_rule_t hs_desc_v3_token_table[] = {
T1_START(str_hs_desc, R_HS_DESCRIPTOR, EQ(1), NO_OBJ),
T1(str_lifetime, R3_DESC_LIFETIME, EQ(1), NO_OBJ),
T1(str_desc_cert, R3_DESC_SIGNING_CERT, NO_ARGS, NEED_OBJ),
T1(str_rev_counter, R3_REVISION_COUNTER, EQ(1), NO_OBJ),
T1(str_superencrypted, R3_SUPERENCRYPTED, NO_ARGS, NEED_OBJ),
T1_END(str_signature, R3_SIGNATURE, EQ(1), NO_OBJ),
END_OF_TABLE
};
static token_rule_t hs_desc_superencrypted_v3_token_table[] = {
T1_START(str_desc_auth_type, R3_DESC_AUTH_TYPE, GE(1), NO_OBJ),
T1(str_desc_auth_key, R3_DESC_AUTH_KEY, GE(1), NO_OBJ),
T1N(str_desc_auth_client, R3_DESC_AUTH_CLIENT, GE(3), NO_OBJ),
T1(str_encrypted, R3_ENCRYPTED, NO_ARGS, NEED_OBJ),
END_OF_TABLE
};
static token_rule_t hs_desc_encrypted_v3_token_table[] = {
T1_START(str_create2_formats, R3_CREATE2_FORMATS, CONCAT_ARGS, NO_OBJ),
T01(str_intro_auth_required, R3_INTRO_AUTH_REQUIRED, ARGS, NO_OBJ),
T01(str_single_onion, R3_SINGLE_ONION_SERVICE, ARGS, NO_OBJ),
END_OF_TABLE
};
static token_rule_t hs_desc_intro_point_v3_token_table[] = {
T1_START(str_intro_point, R3_INTRODUCTION_POINT, EQ(1), NO_OBJ),
T1N(str_ip_onion_key, R3_INTRO_ONION_KEY, GE(2), OBJ_OK),
T1(str_ip_auth_key, R3_INTRO_AUTH_KEY, NO_ARGS, NEED_OBJ),
T1(str_ip_enc_key, R3_INTRO_ENC_KEY, GE(2), OBJ_OK),
T1(str_ip_enc_key_cert, R3_INTRO_ENC_KEY_CERT, ARGS, OBJ_OK),
T01(str_ip_legacy_key, R3_INTRO_LEGACY_KEY, ARGS, NEED_KEY_1024),
T01(str_ip_legacy_key_cert, R3_INTRO_LEGACY_KEY_CERT, ARGS, OBJ_OK),
END_OF_TABLE
};
static void
build_mac(const uint8_t *mac_key, size_t mac_key_len,
const uint8_t *salt, size_t salt_len,
const uint8_t *encrypted, size_t encrypted_len,
uint8_t *mac_out, size_t mac_len)
{
crypto_digest_t *digest;
const uint64_t mac_len_netorder = tor_htonll(mac_key_len);
const uint64_t salt_len_netorder = tor_htonll(salt_len);
tor_assert(mac_key);
tor_assert(salt);
tor_assert(encrypted);
tor_assert(mac_out);
digest = crypto_digest256_new(DIGEST_SHA3_256);
crypto_digest_add_bytes(digest, (const char *) &mac_len_netorder, 8);
crypto_digest_add_bytes(digest, (const char *) mac_key, mac_key_len);
crypto_digest_add_bytes(digest, (const char *) &salt_len_netorder, 8);
crypto_digest_add_bytes(digest, (const char *) salt, salt_len);
crypto_digest_add_bytes(digest, (const char *) encrypted, encrypted_len);
crypto_digest_get_digest(digest, (char *) mac_out, mac_len);
crypto_digest_free(digest);
}
static size_t
build_secret_input(const hs_descriptor_t *desc,
const uint8_t *secret_data,
size_t secret_data_len,
uint8_t **secret_input_out)
{
size_t offset = 0;
size_t secret_input_len = secret_data_len + DIGEST256_LEN + sizeof(uint64_t);
uint8_t *secret_input = NULL;
tor_assert(desc);
tor_assert(secret_data);
tor_assert(secret_input_out);
secret_input = tor_malloc_zero(secret_input_len);
memcpy(secret_input, secret_data, secret_data_len);
offset += secret_data_len;
memcpy(secret_input + offset, desc->subcredential.subcred, DIGEST256_LEN);
offset += DIGEST256_LEN;
set_uint64(secret_input + offset,
tor_htonll(desc->plaintext_data.revision_counter));
offset += sizeof(uint64_t);
tor_assert(secret_input_len == offset);
*secret_input_out = secret_input;
return secret_input_len;
}
static void
build_kdf_key(const hs_descriptor_t *desc,
const uint8_t *secret_data,
size_t secret_data_len,
const uint8_t *salt, size_t salt_len,
uint8_t *key_out, size_t key_out_len,
int is_superencrypted_layer)
{
uint8_t *secret_input = NULL;
size_t secret_input_len;
crypto_xof_t *xof;
tor_assert(desc);
tor_assert(secret_data);
tor_assert(salt);
tor_assert(key_out);
secret_input_len = build_secret_input(desc, secret_data,
secret_data_len, &secret_input);
xof = crypto_xof_new();
crypto_xof_add_bytes(xof, secret_input, secret_input_len);
crypto_xof_add_bytes(xof, salt, salt_len);
if (is_superencrypted_layer) {
crypto_xof_add_bytes(xof, (const uint8_t *) str_enc_const_superencryption,
strlen(str_enc_const_superencryption));
} else {
crypto_xof_add_bytes(xof, (const uint8_t *) str_enc_const_encryption,
strlen(str_enc_const_encryption));
}
crypto_xof_squeeze_bytes(xof, key_out, key_out_len);
crypto_xof_free(xof);
memwipe(secret_input, 0, secret_input_len);
tor_free(secret_input);
}
static void
build_secret_key_iv_mac(const hs_descriptor_t *desc,
const uint8_t *secret_data,
size_t secret_data_len,
const uint8_t *salt, size_t salt_len,
uint8_t *key_out, size_t key_len,
uint8_t *iv_out, size_t iv_len,
uint8_t *mac_out, size_t mac_len,
int is_superencrypted_layer)
{
size_t offset = 0;
uint8_t kdf_key[HS_DESC_ENCRYPTED_KDF_OUTPUT_LEN];
tor_assert(desc);
tor_assert(secret_data);
tor_assert(salt);
tor_assert(key_out);
tor_assert(iv_out);
tor_assert(mac_out);
build_kdf_key(desc, secret_data, secret_data_len,
salt, salt_len, kdf_key, sizeof(kdf_key),
is_superencrypted_layer);
memcpy(key_out, kdf_key, key_len);
offset += key_len;
memcpy(iv_out, kdf_key + offset, iv_len);
offset += iv_len;
memcpy(mac_out, kdf_key + offset, mac_len);
tor_assert((offset + mac_len) == sizeof(kdf_key));
memwipe(kdf_key, 0, sizeof(kdf_key));
}
STATIC char *
encode_link_specifiers(const smartlist_t *specs)
{
char *encoded_b64 = NULL;
link_specifier_list_t *lslist = link_specifier_list_new();
tor_assert(specs);
tor_assert(smartlist_len(specs) > 0);
tor_assert(smartlist_len(specs) <= UINT8_MAX);
link_specifier_list_set_n_spec(lslist, smartlist_len(specs));
SMARTLIST_FOREACH_BEGIN(specs, const link_specifier_t *,
spec) {
link_specifier_t *ls = link_specifier_dup(spec);
tor_assert(ls);
link_specifier_list_add_spec(lslist, ls);
} SMARTLIST_FOREACH_END(spec);
{
uint8_t *encoded;
ssize_t encoded_len, encoded_b64_len, ret;
encoded_len = link_specifier_list_encoded_len(lslist);
tor_assert(encoded_len > 0);
encoded = tor_malloc_zero(encoded_len);
ret = link_specifier_list_encode(encoded, encoded_len, lslist);
tor_assert(ret == encoded_len);
encoded_b64_len = base64_encode_size(encoded_len, 0) + 1;
encoded_b64 = tor_malloc_zero(encoded_b64_len);
ret = base64_encode(encoded_b64, encoded_b64_len, (const char *) encoded,
encoded_len, 0);
tor_assert(ret == (encoded_b64_len - 1));
tor_free(encoded);
}
link_specifier_list_free(lslist);
return encoded_b64;
}
static char *
encode_legacy_key(const hs_desc_intro_point_t *ip)
{
char *key_str, b64_cert[256], *encoded = NULL;
size_t key_str_len;
tor_assert(ip);
if (base64_encode(b64_cert, sizeof(b64_cert),
(const char *) ip->legacy.cert.encoded,
ip->legacy.cert.len, BASE64_ENCODE_MULTILINE) < 0) {
log_warn(LD_REND, "Unable to encode legacy crosscert.");
goto done;
}
if (crypto_pk_write_public_key_to_string(ip->legacy.key, &key_str,
&key_str_len) < 0) {
log_warn(LD_REND, "Unable to encode legacy encryption key.");
goto done;
}
tor_asprintf(&encoded,
"%s \n%s"
"%s\n"
"-----BEGIN CROSSCERT-----\n"
"%s"
"-----END CROSSCERT-----",
str_ip_legacy_key, key_str,
str_ip_legacy_key_cert, b64_cert);
tor_free(key_str);
done:
return encoded;
}
static char *
encode_enc_key(const hs_desc_intro_point_t *ip)
{
char *encoded = NULL, *encoded_cert;
char key_b64[CURVE25519_BASE64_PADDED_LEN + 1];
tor_assert(ip);
curve25519_public_to_base64(key_b64, &ip->enc_key);
if (tor_cert_encode_ed22519(ip->enc_key_cert, &encoded_cert) < 0) {
goto done;
}
tor_asprintf(&encoded,
"%s ntor %s\n"
"%s\n%s",
str_ip_enc_key, key_b64,
str_ip_enc_key_cert, encoded_cert);
tor_free(encoded_cert);
done:
return encoded;
}
static char *
encode_onion_key(const hs_desc_intro_point_t *ip)
{
char *encoded = NULL;
char key_b64[CURVE25519_BASE64_PADDED_LEN + 1];
tor_assert(ip);
curve25519_public_to_base64(key_b64, &ip->onion_key);
tor_asprintf(&encoded, "%s ntor %s", str_ip_onion_key, key_b64);
return encoded;
}
static char *
encode_intro_point(const ed25519_public_key_t *sig_key,
const hs_desc_intro_point_t *ip)
{
char *encoded_ip = NULL;
smartlist_t *lines = smartlist_new();
tor_assert(ip);
tor_assert(sig_key);
{
char *ls_str = encode_link_specifiers(ip->link_specifiers);
smartlist_add_asprintf(lines, "%s %s", str_intro_point, ls_str);
tor_free(ls_str);
}
{
char *encoded_onion_key = encode_onion_key(ip);
if (encoded_onion_key == NULL) {
goto err;
}
smartlist_add_asprintf(lines, "%s", encoded_onion_key);
tor_free(encoded_onion_key);
}
{
char *encoded_cert;
if (tor_cert_encode_ed22519(ip->auth_key_cert, &encoded_cert) < 0) {
goto err;
}
smartlist_add_asprintf(lines, "%s\n%s", str_ip_auth_key, encoded_cert);
tor_free(encoded_cert);
}
{
char *encoded_enc_key = encode_enc_key(ip);
if (encoded_enc_key == NULL) {
goto err;
}
smartlist_add_asprintf(lines, "%s", encoded_enc_key);
tor_free(encoded_enc_key);
}
if (ip->legacy.key != NULL) {
tor_assert(ip->legacy.cert.encoded);
char *encoded_legacy_key = encode_legacy_key(ip);
if (encoded_legacy_key == NULL) {
goto err;
}
smartlist_add_asprintf(lines, "%s", encoded_legacy_key);
tor_free(encoded_legacy_key);
}
encoded_ip = smartlist_join_strings(lines, "\n", 1, NULL);
err:
SMARTLIST_FOREACH(lines, char *, l, tor_free(l));
smartlist_free(lines);
return encoded_ip;
}
static size_t
compute_padded_plaintext_length(size_t plaintext_len)
{
size_t plaintext_padded_len;
const int padding_block_length = HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE;
tor_assert(plaintext_len <= (SIZE_T_CEILING - padding_block_length));
plaintext_padded_len = CEIL_DIV(plaintext_len, padding_block_length) *
padding_block_length;
tor_assert(!(plaintext_padded_len % padding_block_length));
return plaintext_padded_len;
}
STATIC size_t
build_plaintext_padding(const char *plaintext, size_t plaintext_len,
uint8_t **padded_out)
{
size_t padded_len;
uint8_t *padded;
tor_assert(plaintext);
tor_assert(padded_out);
padded_len = compute_padded_plaintext_length(plaintext_len);
tor_assert(padded_len >= plaintext_len);
padded = tor_malloc_zero(padded_len);
memcpy(padded, plaintext, plaintext_len);
*padded_out = padded;
return padded_len;
}
static size_t
build_encrypted(const uint8_t *key, const uint8_t *iv, const char *plaintext,
size_t plaintext_len, uint8_t **encrypted_out,
int is_superencrypted_layer)
{
size_t encrypted_len;
uint8_t *padded_plaintext, *encrypted;
crypto_cipher_t *cipher;
tor_assert(key);
tor_assert(iv);
tor_assert(plaintext);
tor_assert(encrypted_out);
if (is_superencrypted_layer) {
encrypted_len = build_plaintext_padding(plaintext, plaintext_len,
&padded_plaintext);
tor_assert(!(encrypted_len % HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE));
} else {
padded_plaintext = tor_memdup(plaintext, plaintext_len);
encrypted_len = plaintext_len;
}
cipher = crypto_cipher_new_with_iv_and_bits(key, iv,
HS_DESC_ENCRYPTED_BIT_SIZE);
encrypted = tor_malloc_zero(encrypted_len);
crypto_cipher_encrypt(cipher, (char *) encrypted,
(const char *) padded_plaintext, encrypted_len);
*encrypted_out = encrypted;
crypto_cipher_free(cipher);
tor_free(padded_plaintext);
return encrypted_len;
}
static size_t
encrypt_descriptor_data(const hs_descriptor_t *desc,
const uint8_t *secret_data,
size_t secret_data_len,
const char *plaintext,
char **encrypted_out, int is_superencrypted_layer)
{
char *final_blob;
size_t encrypted_len, final_blob_len, offset = 0;
uint8_t *encrypted;
uint8_t salt[HS_DESC_ENCRYPTED_SALT_LEN];
uint8_t secret_key[HS_DESC_ENCRYPTED_KEY_LEN], secret_iv[CIPHER_IV_LEN];
uint8_t mac_key[DIGEST256_LEN], mac[DIGEST256_LEN];
tor_assert(desc);
tor_assert(secret_data);
tor_assert(plaintext);
tor_assert(encrypted_out);
crypto_strongest_rand(salt, sizeof(salt));
build_secret_key_iv_mac(desc, secret_data, secret_data_len,
salt, sizeof(salt),
secret_key, sizeof(secret_key),
secret_iv, sizeof(secret_iv),
mac_key, sizeof(mac_key),
is_superencrypted_layer);
encrypted_len = build_encrypted(secret_key, secret_iv, plaintext,
strlen(plaintext), &encrypted,
is_superencrypted_layer);
memwipe(secret_key, 0, sizeof(secret_key));
memwipe(secret_iv, 0, sizeof(secret_iv));
final_blob_len = sizeof(salt) + encrypted_len + DIGEST256_LEN;
final_blob = tor_malloc_zero(final_blob_len);
build_mac(mac_key, sizeof(mac_key), salt, sizeof(salt),
encrypted, encrypted_len, mac, sizeof(mac));
memwipe(mac_key, 0, sizeof(mac_key));
memcpy(final_blob, salt, sizeof(salt));
offset = sizeof(salt);
memcpy(final_blob + offset, encrypted, encrypted_len);
offset += encrypted_len;
memcpy(final_blob + offset, mac, sizeof(mac));
offset += sizeof(mac);
memwipe(salt, 0, sizeof(salt));
memwipe(encrypted, 0, encrypted_len);
tor_free(encrypted);
tor_assert(offset == final_blob_len);
*encrypted_out = final_blob;
return final_blob_len;
}
static char *
get_auth_client_str(const hs_desc_authorized_client_t *client)
{
int ret;
char *auth_client_str = NULL;
char client_id_b64[HS_DESC_CLIENT_ID_LEN * 2];
char iv_b64[CIPHER_IV_LEN * 2];
char encrypted_cookie_b64[HS_DESC_ENCRYPED_COOKIE_LEN * 2];
#define ASSERT_AND_BASE64(field) STMT_BEGIN \
tor_assert(!fast_mem_is_zero((char *) client->field, \
sizeof(client->field))); \
ret = base64_encode_nopad(field##_b64, sizeof(field##_b64), \
client->field, sizeof(client->field)); \
tor_assert(ret > 0); \
STMT_END
ASSERT_AND_BASE64(client_id);
ASSERT_AND_BASE64(iv);
ASSERT_AND_BASE64(encrypted_cookie);
tor_asprintf(&auth_client_str, "%s %s %s %s", str_desc_auth_client,
client_id_b64, iv_b64, encrypted_cookie_b64);
#undef ASSERT_AND_BASE64
return auth_client_str;
}
static char *
get_all_auth_client_lines(const hs_descriptor_t *desc)
{
smartlist_t *auth_client_lines = smartlist_new();
char *auth_client_lines_str = NULL;
tor_assert(desc);
tor_assert(desc->superencrypted_data.clients);
tor_assert(smartlist_len(desc->superencrypted_data.clients) != 0);
tor_assert(smartlist_len(desc->superencrypted_data.clients)
% HS_DESC_AUTH_CLIENT_MULTIPLE == 0);
SMARTLIST_FOREACH_BEGIN(desc->superencrypted_data.clients,
const hs_desc_authorized_client_t *, client) {
char *auth_client_str = NULL;
auth_client_str = get_auth_client_str(client);
smartlist_add(auth_client_lines, auth_client_str);
} SMARTLIST_FOREACH_END(client);
auth_client_lines_str = smartlist_join_strings(auth_client_lines,
"\n", 1, NULL);
SMARTLIST_FOREACH(auth_client_lines, char *, a, tor_free(a));
smartlist_free(auth_client_lines);
return auth_client_lines_str;
}
static char *
get_inner_encrypted_layer_plaintext(const hs_descriptor_t *desc)
{
char *encoded_str = NULL;
smartlist_t *lines = smartlist_new();
{
if (!desc->encrypted_data.create2_ntor) {
log_err(LD_BUG, "HS desc doesn't have recognized handshake type.");
goto err;
}
smartlist_add_asprintf(lines, "%s %d\n", str_create2_formats,
ONION_HANDSHAKE_TYPE_NTOR);
if (desc->encrypted_data.intro_auth_types &&
smartlist_len(desc->encrypted_data.intro_auth_types)) {
char *buf = smartlist_join_strings(desc->encrypted_data.intro_auth_types,
" ", 0, NULL);
smartlist_add_asprintf(lines, "%s %s\n", str_intro_auth_required, buf);
tor_free(buf);
}
if (desc->encrypted_data.single_onion_service) {
smartlist_add_asprintf(lines, "%s\n", str_single_onion);
}
}
SMARTLIST_FOREACH_BEGIN(desc->encrypted_data.intro_points,
const hs_desc_intro_point_t *, ip) {
char *encoded_ip = encode_intro_point(&desc->plaintext_data.signing_pubkey,
ip);
if (encoded_ip == NULL) {
log_err(LD_BUG, "HS desc intro point is malformed.");
goto err;
}
smartlist_add(lines, encoded_ip);
} SMARTLIST_FOREACH_END(ip);
encoded_str = smartlist_join_strings(lines, "", 0, NULL);
err:
SMARTLIST_FOREACH(lines, char *, l, tor_free(l));
smartlist_free(lines);
return encoded_str;
}
static char *
get_outer_encrypted_layer_plaintext(const hs_descriptor_t *desc,
const char *layer2_b64_ciphertext)
{
char *layer1_str = NULL;
smartlist_t *lines = smartlist_new();
smartlist_add_asprintf(lines, "%s %s\n", str_desc_auth_type, "x25519");
{
char ephemeral_key_base64[CURVE25519_BASE64_PADDED_LEN + 1];
const curve25519_public_key_t *ephemeral_pubkey;
ephemeral_pubkey = &desc->superencrypted_data.auth_ephemeral_pubkey;
tor_assert(!fast_mem_is_zero((char *) ephemeral_pubkey->public_key,
CURVE25519_PUBKEY_LEN));
curve25519_public_to_base64(ephemeral_key_base64, ephemeral_pubkey);
smartlist_add_asprintf(lines, "%s %s\n",
str_desc_auth_key, ephemeral_key_base64);
memwipe(ephemeral_key_base64, 0, sizeof(ephemeral_key_base64));
}
{
char *auth_client_lines = get_all_auth_client_lines(desc);
tor_assert(auth_client_lines);
smartlist_add(lines, auth_client_lines);
}
{
smartlist_add_asprintf(lines,
"%s\n"
"-----BEGIN MESSAGE-----\n"
"%s"
"-----END MESSAGE-----",
str_encrypted, layer2_b64_ciphertext);
}
layer1_str = smartlist_join_strings(lines, "", 0, NULL);
SMARTLIST_FOREACH(lines, char *, a, memwipe(a, 0, strlen(a)));
SMARTLIST_FOREACH(lines, char *, a, tor_free(a));
smartlist_free(lines);
return layer1_str;
}
static char *
encrypt_desc_data_and_base64(const hs_descriptor_t *desc,
const uint8_t *secret_data,
size_t secret_data_len,
const char *encoded_str,
int is_superencrypted_layer)
{
char *enc_b64;
ssize_t enc_b64_len, ret_len, enc_len;
char *encrypted_blob = NULL;
enc_len = encrypt_descriptor_data(desc, secret_data, secret_data_len,
encoded_str, &encrypted_blob,
is_superencrypted_layer);
enc_b64_len = base64_encode_size(enc_len, BASE64_ENCODE_MULTILINE) + 1;
enc_b64 = tor_malloc_zero(enc_b64_len);
ret_len = base64_encode(enc_b64, enc_b64_len, encrypted_blob, enc_len,
BASE64_ENCODE_MULTILINE);
tor_assert(ret_len == (enc_b64_len - 1));
tor_free(encrypted_blob);
return enc_b64;
}
static size_t
build_secret_data(const ed25519_public_key_t *blinded_pubkey,
const uint8_t *descriptor_cookie,
uint8_t **secret_data_out)
{
size_t secret_data_len;
uint8_t *secret_data;
tor_assert(blinded_pubkey);
tor_assert(secret_data_out);
if (descriptor_cookie) {
secret_data_len = ED25519_PUBKEY_LEN + HS_DESC_DESCRIPTOR_COOKIE_LEN;
secret_data = tor_malloc(secret_data_len);
memcpy(secret_data,
blinded_pubkey->pubkey,
ED25519_PUBKEY_LEN);
memcpy(secret_data + ED25519_PUBKEY_LEN,
descriptor_cookie,
HS_DESC_DESCRIPTOR_COOKIE_LEN);
} else {
secret_data_len = ED25519_PUBKEY_LEN;
secret_data = tor_malloc(secret_data_len);
memcpy(secret_data,
blinded_pubkey->pubkey,
ED25519_PUBKEY_LEN);
}
*secret_data_out = secret_data;
return secret_data_len;
}
static int
encode_superencrypted_data(const hs_descriptor_t *desc,
const uint8_t *descriptor_cookie,
char **encrypted_blob_out)
{
int ret = -1;
uint8_t *secret_data = NULL;
size_t secret_data_len = 0;
char *layer2_str = NULL;
char *layer2_b64_ciphertext = NULL;
char *layer1_str = NULL;
char *layer1_b64_ciphertext = NULL;
tor_assert(desc);
tor_assert(encrypted_blob_out);
layer2_str = get_inner_encrypted_layer_plaintext(desc);
if (!layer2_str) {
goto err;
}
secret_data_len = build_secret_data(&desc->plaintext_data.blinded_pubkey,
descriptor_cookie,
&secret_data);
layer2_b64_ciphertext =
encrypt_desc_data_and_base64(desc, secret_data, secret_data_len,
layer2_str, 0);
if (!layer2_b64_ciphertext) {
goto err;
}
layer1_str = get_outer_encrypted_layer_plaintext(desc,layer2_b64_ciphertext);
if (!layer1_str) {
goto err;
}
layer1_b64_ciphertext =
encrypt_desc_data_and_base64(desc,
desc->plaintext_data.blinded_pubkey.pubkey,
ED25519_PUBKEY_LEN,
layer1_str, 1);
if (!layer1_b64_ciphertext) {
goto err;
}
ret = 0;
err:
memwipe(secret_data, 0, secret_data_len);
tor_free(secret_data);
tor_free(layer1_str);
tor_free(layer2_str);
tor_free(layer2_b64_ciphertext);
*encrypted_blob_out = layer1_b64_ciphertext;
return ret;
}
static int
desc_encode_v3(const hs_descriptor_t *desc,
const ed25519_keypair_t *signing_kp,
const uint8_t *descriptor_cookie,
char **encoded_out)
{
int ret = -1;
char *encoded_str = NULL;
size_t encoded_len;
smartlist_t *lines = smartlist_new();
tor_assert(desc);
tor_assert(signing_kp);
tor_assert(encoded_out);
tor_assert(desc->plaintext_data.version == 3);
{
char *encoded_cert;
if (desc->plaintext_data.signing_key_cert->cert_type
!= CERT_TYPE_SIGNING_HS_DESC) {
log_err(LD_BUG, "HS descriptor signing key has an unexpected cert type "
"(%d)", (int) desc->plaintext_data.signing_key_cert->cert_type);
goto err;
}
if (tor_cert_encode_ed22519(desc->plaintext_data.signing_key_cert,
&encoded_cert) < 0) {
goto err;
}
smartlist_add_asprintf(lines, "%s %" PRIu32, str_hs_desc,
desc->plaintext_data.version);
smartlist_add_asprintf(lines, "%s %" PRIu32, str_lifetime,
desc->plaintext_data.lifetime_sec / 60);
smartlist_add_asprintf(lines, "%s\n%s", str_desc_cert, encoded_cert);
tor_free(encoded_cert);
smartlist_add_asprintf(lines, "%s %" PRIu64, str_rev_counter,
desc->plaintext_data.revision_counter);
}
{
char *enc_b64_blob=NULL;
if (encode_superencrypted_data(desc, descriptor_cookie,
&enc_b64_blob) < 0) {
goto err;
}
smartlist_add_asprintf(lines,
"%s\n"
"-----BEGIN MESSAGE-----\n"
"%s"
"-----END MESSAGE-----",
str_superencrypted, enc_b64_blob);
tor_free(enc_b64_blob);
}
encoded_str = smartlist_join_strings(lines, "\n", 1, &encoded_len);
{
ed25519_signature_t sig;
char ed_sig_b64[ED25519_SIG_BASE64_LEN + 1];
if (ed25519_sign_prefixed(&sig,
(const uint8_t *) encoded_str, encoded_len,
str_desc_sig_prefix, signing_kp) < 0) {
log_warn(LD_BUG, "Can't sign encoded HS descriptor!");
tor_free(encoded_str);
goto err;
}
ed25519_signature_to_base64(ed_sig_b64, &sig);
smartlist_add_asprintf(lines, "%s %s", str_signature, ed_sig_b64);
}
tor_free(encoded_str);
encoded_str = smartlist_join_strings(lines, "\n", 1, NULL);
*encoded_out = encoded_str;
if (strlen(encoded_str) >= hs_cache_get_max_descriptor_size()) {
log_warn(LD_GENERAL, "We just made an HS descriptor that's too big (%d)."
"Failing.", (int)strlen(encoded_str));
tor_free(encoded_str);
goto err;
}
ret = 0;
err:
SMARTLIST_FOREACH(lines, char *, l, tor_free(l));
smartlist_free(lines);
return ret;
}
static int
decode_auth_client(const directory_token_t *tok,
hs_desc_authorized_client_t *client)
{
int ret = -1;
tor_assert(tok);
tor_assert(tok->n_args >= 3);
tor_assert(client);
if (base64_decode((char *) client->client_id, sizeof(client->client_id),
tok->args[0], strlen(tok->args[0])) !=
sizeof(client->client_id)) {
goto done;
}
if (base64_decode((char *) client->iv, sizeof(client->iv),
tok->args[1], strlen(tok->args[1])) !=
sizeof(client->iv)) {
goto done;
}
if (base64_decode((char *) client->encrypted_cookie,
sizeof(client->encrypted_cookie),
tok->args[2], strlen(tok->args[2])) !=
sizeof(client->encrypted_cookie)) {
goto done;
}
ret = 0;
done:
return ret;
}
STATIC smartlist_t *
decode_link_specifiers(const char *encoded)
{
int decoded_len;
size_t encoded_len, i;
uint8_t *decoded;
smartlist_t *results = NULL;
link_specifier_list_t *specs = NULL;
tor_assert(encoded);
encoded_len = strlen(encoded);
decoded = tor_malloc(encoded_len);
decoded_len = base64_decode((char *) decoded, encoded_len, encoded,
encoded_len);
if (decoded_len < 0) {
goto err;
}
if (link_specifier_list_parse(&specs, decoded,
(size_t) decoded_len) < decoded_len) {
goto err;
}
tor_assert(specs);
results = smartlist_new();
for (i = 0; i < link_specifier_list_getlen_spec(specs); i++) {
link_specifier_t *ls = link_specifier_list_get_spec(specs, i);
if (BUG(!ls)) {
goto err;
}
link_specifier_t *ls_dup = link_specifier_dup(ls);
if (BUG(!ls_dup)) {
goto err;
}
smartlist_add(results, ls_dup);
}
goto done;
err:
if (results) {
SMARTLIST_FOREACH(results, link_specifier_t *, s,
link_specifier_free(s));
smartlist_free(results);
results = NULL;
}
done:
link_specifier_list_free(specs);
tor_free(decoded);
return results;
}
static int
decode_auth_type(hs_desc_encrypted_data_t *desc, const char *list)
{
int match = 0;
tor_assert(desc);
tor_assert(list);
desc->intro_auth_types = smartlist_new();
smartlist_split_string(desc->intro_auth_types, list, " ", 0, 0);
SMARTLIST_FOREACH_BEGIN(desc->intro_auth_types, const char *, auth) {
for (int idx = 0; intro_auth_types[idx].identifier; idx++) {
if (!strncmp(auth, intro_auth_types[idx].identifier,
strlen(intro_auth_types[idx].identifier))) {
match = 1;
break;
}
}
} SMARTLIST_FOREACH_END(auth);
return match;
}
static void
decode_create2_list(hs_desc_encrypted_data_t *desc, const char *list)
{
smartlist_t *tokens;
tor_assert(desc);
tor_assert(list);
tokens = smartlist_new();
smartlist_split_string(tokens, list, " ", 0, 0);
SMARTLIST_FOREACH_BEGIN(tokens, char *, s) {
int ok;
unsigned long type = tor_parse_ulong(s, 10, 1, UINT16_MAX, &ok, NULL);
if (!ok) {
log_warn(LD_REND, "Unparseable value %s in create2 list", escaped(s));
continue;
}
switch (type) {
case ONION_HANDSHAKE_TYPE_NTOR:
desc->create2_ntor = 1;
break;
default:
continue;
}
} SMARTLIST_FOREACH_END(s);
SMARTLIST_FOREACH(tokens, char *, s, tor_free(s));
smartlist_free(tokens);
}
STATIC int
cert_is_valid(tor_cert_t *cert, uint8_t type, const char *log_obj_type)
{
tor_assert(log_obj_type);
if (cert == NULL) {
log_warn(LD_REND, "Certificate for %s couldn't be parsed.", log_obj_type);
goto err;
}
if (cert->cert_type != type) {
log_warn(LD_REND, "Invalid cert type %02x for %s.", cert->cert_type,
log_obj_type);
goto err;
}
if (!cert->signing_key_included) {
log_warn(LD_REND, "Signing key is NOT included for %s.", log_obj_type);
goto err;
}
if (tor_cert_checksig(cert, &cert->signing_key, approx_time()) < 0) {
if (cert->cert_expired) {
char expiration_str[ISO_TIME_LEN+1];
format_iso_time(expiration_str, cert->valid_until);
log_fn(LOG_PROTOCOL_WARN, LD_REND, "Invalid signature for %s: %s (%s)",
log_obj_type, tor_cert_describe_signature_status(cert),
expiration_str);
} else {
log_warn(LD_REND, "Invalid signature for %s: %s",
log_obj_type, tor_cert_describe_signature_status(cert));
}
goto err;
}
return 1;
err:
return 0;
}
static int
cert_parse_and_validate(tor_cert_t **cert_out, const char *data,
size_t data_len, unsigned int cert_type_wanted,
const char *err_msg)
{
tor_cert_t *cert;
tor_assert(cert_out);
tor_assert(data);
tor_assert(err_msg);
cert = tor_cert_parse((const uint8_t *) data, data_len);
if (!cert) {
log_warn(LD_REND, "Certificate for %s couldn't be parsed.", err_msg);
goto err;
}
if (!cert_is_valid(cert, cert_type_wanted, err_msg)) {
goto err;
}
*cert_out = cert;
return 0;
err:
tor_cert_free(cert);
*cert_out = NULL;
return -1;
}
STATIC int
encrypted_data_length_is_valid(size_t len)
{
if (len <= HS_DESC_ENCRYPTED_SALT_LEN + DIGEST256_LEN) {
log_warn(LD_REND, "Length of descriptor's encrypted data is too small. "
"Got %lu but minimum value is %d",
(unsigned long)len, HS_DESC_ENCRYPTED_SALT_LEN + DIGEST256_LEN);
goto err;
}
return 1;
err:
return 0;
}
static size_t
build_descriptor_cookie_keys(const hs_subcredential_t *subcredential,
const curve25519_secret_key_t *sk,
const curve25519_public_key_t *pk,
uint8_t **keys_out)
{
uint8_t secret_seed[CURVE25519_OUTPUT_LEN];
uint8_t *keystream;
size_t keystream_len = HS_DESC_CLIENT_ID_LEN + HS_DESC_COOKIE_KEY_LEN;
crypto_xof_t *xof;
tor_assert(subcredential);
tor_assert(sk);
tor_assert(pk);
tor_assert(keys_out);
keystream = tor_malloc_zero(keystream_len);
curve25519_handshake(secret_seed, sk, pk);
xof = crypto_xof_new();
crypto_xof_add_bytes(xof, subcredential->subcred, SUBCRED_LEN);
crypto_xof_add_bytes(xof, secret_seed, sizeof(secret_seed));
crypto_xof_squeeze_bytes(xof, keystream, keystream_len);
crypto_xof_free(xof);
memwipe(secret_seed, 0, sizeof(secret_seed));
*keys_out = keystream;
return keystream_len;
}
static int
decrypt_descriptor_cookie(const hs_descriptor_t *desc,
const hs_desc_authorized_client_t *client,
const curve25519_secret_key_t *client_auth_sk,
uint8_t **descriptor_cookie_out)
{
int ret = -1;
uint8_t *keystream = NULL;
size_t keystream_length = 0;
uint8_t *descriptor_cookie = NULL;
const uint8_t *cookie_key = NULL;
crypto_cipher_t *cipher = NULL;
tor_assert(desc);
tor_assert(client);
tor_assert(client_auth_sk);
tor_assert(!fast_mem_is_zero(
(char *) &desc->superencrypted_data.auth_ephemeral_pubkey,
sizeof(desc->superencrypted_data.auth_ephemeral_pubkey)));
tor_assert(!fast_mem_is_zero((char *) desc->subcredential.subcred,
DIGEST256_LEN));
if (BUG(fast_mem_is_zero((char *)client_auth_sk, sizeof(*client_auth_sk)))) {
goto done;
}
keystream_length =
build_descriptor_cookie_keys(&desc->subcredential,
client_auth_sk,
&desc->superencrypted_data.auth_ephemeral_pubkey,
&keystream);
tor_assert(keystream_length > 0);
if (tor_memneq(client->client_id, keystream, HS_DESC_CLIENT_ID_LEN)) {
goto done;
}
cookie_key = keystream + HS_DESC_CLIENT_ID_LEN;
cipher = crypto_cipher_new_with_iv_and_bits(cookie_key, client->iv,
HS_DESC_COOKIE_KEY_BIT_SIZE);
descriptor_cookie = tor_malloc_zero(HS_DESC_DESCRIPTOR_COOKIE_LEN);
crypto_cipher_decrypt(cipher, (char *) descriptor_cookie,
(const char *) client->encrypted_cookie,
sizeof(client->encrypted_cookie));
ret = 0;
done:
*descriptor_cookie_out = descriptor_cookie;
if (cipher) {
crypto_cipher_free(cipher);
}
memwipe(keystream, 0, keystream_length);
tor_free(keystream);
return ret;
}
MOCK_IMPL(STATIC size_t,
decrypt_desc_layer,(const hs_descriptor_t *desc,
const uint8_t *descriptor_cookie,
bool is_superencrypted_layer,
char **decrypted_out))
{
uint8_t *decrypted = NULL;
uint8_t secret_key[HS_DESC_ENCRYPTED_KEY_LEN], secret_iv[CIPHER_IV_LEN];
uint8_t *secret_data = NULL;
size_t secret_data_len = 0;
uint8_t mac_key[DIGEST256_LEN], our_mac[DIGEST256_LEN];
const uint8_t *salt, *encrypted, *desc_mac;
size_t encrypted_len, result_len = 0;
const uint8_t *encrypted_blob = (is_superencrypted_layer)
? desc->plaintext_data.superencrypted_blob
: desc->superencrypted_data.encrypted_blob;
size_t encrypted_blob_size = (is_superencrypted_layer)
? desc->plaintext_data.superencrypted_blob_size
: desc->superencrypted_data.encrypted_blob_size;
tor_assert(decrypted_out);
tor_assert(desc);
tor_assert(encrypted_blob);
if (!encrypted_data_length_is_valid(encrypted_blob_size)) {
goto err;
}
salt = encrypted_blob;
encrypted = encrypted_blob + HS_DESC_ENCRYPTED_SALT_LEN;
encrypted_len = encrypted_blob_size -
(HS_DESC_ENCRYPTED_SALT_LEN + DIGEST256_LEN);
tor_assert(encrypted_len > 0);
desc_mac = encrypted_blob + encrypted_blob_size - DIGEST256_LEN;
secret_data_len = build_secret_data(&desc->plaintext_data.blinded_pubkey,
descriptor_cookie,
&secret_data);
build_secret_key_iv_mac(desc, secret_data, secret_data_len,
salt, HS_DESC_ENCRYPTED_SALT_LEN,
secret_key, sizeof(secret_key),
secret_iv, sizeof(secret_iv),
mac_key, sizeof(mac_key),
is_superencrypted_layer);
build_mac(mac_key, sizeof(mac_key), salt, HS_DESC_ENCRYPTED_SALT_LEN,
encrypted, encrypted_len, our_mac, sizeof(our_mac));
memwipe(mac_key, 0, sizeof(mac_key));
if (!tor_memeq(our_mac, desc_mac, sizeof(our_mac))) {
log_info(LD_REND, "Encrypted service descriptor MAC check failed");
goto err;
}
{
crypto_cipher_t *cipher;
cipher = crypto_cipher_new_with_iv_and_bits(secret_key, secret_iv,
HS_DESC_ENCRYPTED_BIT_SIZE);
decrypted = tor_malloc_zero(encrypted_len + 1);
crypto_cipher_decrypt(cipher, (char *) decrypted,
(const char *) encrypted, encrypted_len);
crypto_cipher_free(cipher);
}
{
uint8_t *end = memchr(decrypted, 0, encrypted_len);
result_len = encrypted_len;
if (end) {
result_len = end - decrypted;
}
}
if (result_len == 0) {
goto err;
}
decrypted[encrypted_len] = '\0';
*decrypted_out = (char *) decrypted;
goto done;
err:
if (decrypted) {
tor_free(decrypted);
}
*decrypted_out = NULL;
result_len = 0;
done:
memwipe(secret_data, 0, secret_data_len);
memwipe(secret_key, 0, sizeof(secret_key));
memwipe(secret_iv, 0, sizeof(secret_iv));
tor_free(secret_data);
return result_len;
}
static size_t
desc_decrypt_superencrypted(const hs_descriptor_t *desc, char **decrypted_out)
{
size_t superencrypted_len = 0;
char *superencrypted_plaintext = NULL;
tor_assert(desc);
tor_assert(decrypted_out);
superencrypted_len = decrypt_desc_layer(desc,
NULL,
true, &superencrypted_plaintext);
if (!superencrypted_len) {
log_warn(LD_REND, "Decrypting superencrypted desc failed.");
goto done;
}
tor_assert(superencrypted_plaintext);
done:
*decrypted_out = superencrypted_plaintext;
return superencrypted_len;
}
static size_t
desc_decrypt_encrypted(const hs_descriptor_t *desc,
const curve25519_secret_key_t *client_auth_sk,
char **decrypted_out)
{
size_t encrypted_len = 0;
char *encrypted_plaintext = NULL;
uint8_t *descriptor_cookie = NULL;
tor_assert(desc);
tor_assert(desc->superencrypted_data.clients);
tor_assert(decrypted_out);
if (client_auth_sk) {
SMARTLIST_FOREACH_BEGIN(desc->superencrypted_data.clients,
hs_desc_authorized_client_t *, client) {
if (!decrypt_descriptor_cookie(desc, client, client_auth_sk,
&descriptor_cookie)) {
break;
}
} SMARTLIST_FOREACH_END(client);
}
encrypted_len = decrypt_desc_layer(desc,
descriptor_cookie,
false, &encrypted_plaintext);
if (!encrypted_len) {
goto err;
}
tor_assert(encrypted_plaintext);
err:
*decrypted_out = encrypted_plaintext;
if (descriptor_cookie) {
memwipe(descriptor_cookie, 0, HS_DESC_DESCRIPTOR_COOKIE_LEN);
}
tor_free(descriptor_cookie);
return encrypted_len;
}
static int
decode_intro_legacy_key(const directory_token_t *tok,
smartlist_t *tokens,
hs_desc_intro_point_t *ip,
const hs_descriptor_t *desc)
{
tor_assert(tok);
tor_assert(tokens);
tor_assert(ip);
tor_assert(desc);
if (!crypto_pk_public_exponent_ok(tok->key)) {
log_warn(LD_REND, "Introduction point legacy key is invalid");
goto err;
}
ip->legacy.key = crypto_pk_dup_key(tok->key);
tok = find_opt_by_keyword(tokens, R3_INTRO_LEGACY_KEY_CERT);
if (!tok) {
log_warn(LD_REND, "Introduction point legacy key cert is missing");
goto err;
}
tor_assert(tok->object_body);
if (strcmp(tok->object_type, "CROSSCERT")) {
log_info(LD_REND, "Introduction point legacy encryption key "
"cross-certification has an unknown format.");
goto err;
}
ip->legacy.cert.encoded = tor_memdup(tok->object_body, tok->object_size);
ip->legacy.cert.len = tok->object_size;
if (rsa_ed25519_crosscert_check(ip->legacy.cert.encoded,
ip->legacy.cert.len, ip->legacy.key,
&desc->plaintext_data.signing_pubkey,
approx_time() - HS_DESC_CERT_LIFETIME)) {
log_warn(LD_REND, "Unable to check cross-certification on the "
"introduction point legacy encryption key.");
ip->cross_certified = 0;
goto err;
}
return 0;
err:
return -1;
}
static int
set_intro_point_onion_key(curve25519_public_key_t *onion_key_out,
const smartlist_t *tokens)
{
int retval = -1;
smartlist_t *onion_keys = NULL;
tor_assert(onion_key_out);
onion_keys = find_all_by_keyword(tokens, R3_INTRO_ONION_KEY);
if (!onion_keys) {
log_warn(LD_REND, "Descriptor did not contain intro onion keys");
goto err;
}
SMARTLIST_FOREACH_BEGIN(onion_keys, directory_token_t *, tok) {
tor_assert(tok->n_args >= 2);
if (!strcmp(tok->args[0], "ntor")) {
if (curve25519_public_from_base64(onion_key_out, tok->args[1]) < 0) {
log_warn(LD_REND, "Introduction point ntor onion-key is invalid");
goto err;
}
retval = 0;
}
} SMARTLIST_FOREACH_END(tok);
if (retval < 0) {
log_warn(LD_REND, "Descriptor did not contain ntor onion keys");
}
err:
smartlist_free(onion_keys);
return retval;
}
STATIC hs_desc_intro_point_t *
decode_introduction_point(const hs_descriptor_t *desc, const char *start)
{
hs_desc_intro_point_t *ip = NULL;
memarea_t *area = NULL;
smartlist_t *tokens = NULL;
const directory_token_t *tok;
tor_assert(desc);
tor_assert(start);
area = memarea_new();
tokens = smartlist_new();
if (tokenize_string(area, start, start + strlen(start),
tokens, hs_desc_intro_point_v3_token_table, 0) < 0) {
log_warn(LD_REND, "Introduction point is not parseable");
goto err;
}
ip = hs_desc_intro_point_new();
tok = find_by_keyword(tokens, R3_INTRODUCTION_POINT);
tor_assert(tok->n_args == 1);
smartlist_free(ip->link_specifiers);
ip->link_specifiers = decode_link_specifiers(tok->args[0]);
if (!ip->link_specifiers) {
log_warn(LD_REND, "Introduction point has invalid link specifiers");
goto err;
}
if (set_intro_point_onion_key(&ip->onion_key, tokens) < 0) {
goto err;
}
tok = find_by_keyword(tokens, R3_INTRO_AUTH_KEY);
tor_assert(tok->object_body);
if (strcmp(tok->object_type, "ED25519 CERT")) {
log_warn(LD_REND, "Unexpected object type for introduction auth key");
goto err;
}
if (cert_parse_and_validate(&ip->auth_key_cert, tok->object_body,
tok->object_size, CERT_TYPE_AUTH_HS_IP_KEY,
"introduction point auth-key") < 0) {
goto err;
}
if (tor_cert_checksig(ip->auth_key_cert,
&desc->plaintext_data.signing_pubkey, 0) < 0) {
log_warn(LD_REND, "Invalid authentication key signature: %s",
tor_cert_describe_signature_status(ip->auth_key_cert));
goto err;
}
tok = find_by_keyword(tokens, R3_INTRO_ENC_KEY);
if (!strcmp(tok->args[0], "ntor")) {
tor_assert(tok->n_args >= 2);
if (curve25519_public_from_base64(&ip->enc_key, tok->args[1]) < 0) {
log_warn(LD_REND, "Introduction point ntor enc-key is invalid");
goto err;
}
} else {
log_warn(LD_REND, "Introduction point encryption key is unrecognized.");
goto err;
}
tok = find_by_keyword(tokens, R3_INTRO_ENC_KEY_CERT);
tor_assert(tok->object_body);
if (strcmp(tok->object_type, "ED25519 CERT")) {
log_warn(LD_REND, "Introduction point ntor encryption key "
"cross-certification has an unknown format.");
goto err;
}
if (cert_parse_and_validate(&ip->enc_key_cert, tok->object_body,
tok->object_size, CERT_TYPE_CROSS_HS_IP_KEYS,
"introduction point enc-key-cert") < 0) {
goto err;
}
if (tor_cert_checksig(ip->enc_key_cert,
&desc->plaintext_data.signing_pubkey, 0) < 0) {
log_warn(LD_REND, "Invalid encryption key signature: %s",
tor_cert_describe_signature_status(ip->enc_key_cert));
goto err;
}
ip->cross_certified = 1;
tok = find_opt_by_keyword(tokens, R3_INTRO_LEGACY_KEY);
if (tok) {
if (decode_intro_legacy_key(tok, tokens, ip, desc) < 0) {
goto err;
}
}
goto done;
err:
hs_desc_intro_point_free(ip);
ip = NULL;
done:
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens);
if (area) {
memarea_drop_all(area);
}
return ip;
}
static void
decode_intro_points(const hs_descriptor_t *desc,
hs_desc_encrypted_data_t *desc_enc,
const char *data)
{
smartlist_t *chunked_desc = smartlist_new();
smartlist_t *intro_points = smartlist_new();
tor_assert(desc);
tor_assert(desc_enc);
tor_assert(data);
tor_assert(desc_enc->intro_points);
{
smartlist_split_string(chunked_desc, data, str_intro_point_start, 0, 0);
if (smartlist_len(chunked_desc) < 2) {
goto done;
}
}
{
int i = 0;
SMARTLIST_FOREACH_BEGIN(chunked_desc, char *, chunk) {
if (i++ == 0) {
continue;
}
smartlist_add_asprintf(intro_points, "%s %s", str_intro_point, chunk);
} SMARTLIST_FOREACH_END(chunk);
}
SMARTLIST_FOREACH_BEGIN(intro_points, const char *, intro_point) {
hs_desc_intro_point_t *ip = decode_introduction_point(desc, intro_point);
if (!ip) {
continue;
}
smartlist_add(desc_enc->intro_points, ip);
} SMARTLIST_FOREACH_END(intro_point);
done:
SMARTLIST_FOREACH(chunked_desc, char *, a, tor_free(a));
smartlist_free(chunked_desc);
SMARTLIST_FOREACH(intro_points, char *, a, tor_free(a));
smartlist_free(intro_points);
}
STATIC int
desc_sig_is_valid(const char *b64_sig,
const ed25519_public_key_t *signing_pubkey,
const char *encoded_desc, size_t encoded_len)
{
int ret = 0;
ed25519_signature_t sig;
const char *sig_start;
tor_assert(b64_sig);
tor_assert(signing_pubkey);
tor_assert(encoded_desc);
tor_assert(encoded_len > 0);
if (strlen(b64_sig) != ED25519_SIG_BASE64_LEN) {
log_warn(LD_REND, "Service descriptor has an invalid signature length."
"Exptected %d but got %lu",
ED25519_SIG_BASE64_LEN, (unsigned long) strlen(b64_sig));
goto err;
}
if (ed25519_signature_from_base64(&sig, b64_sig) != 0) {
log_warn(LD_REND, "Service descriptor does not contain a valid "
"signature");
goto err;
}
sig_start = tor_memstr(encoded_desc, encoded_len, "\n" str_signature " ");
if (!sig_start) {
log_warn(LD_GENERAL, "Malformed signature line. Rejecting.");
goto err;
}
sig_start++;
if (ed25519_checksig_prefixed(&sig,
(const uint8_t *) encoded_desc,
sig_start - encoded_desc,
str_desc_sig_prefix,
signing_pubkey) != 0) {
log_warn(LD_REND, "Invalid signature on service descriptor");
goto err;
}
ret = 1;
err:
return ret;
}
static hs_desc_decode_status_t
desc_decode_plaintext_v3(smartlist_t *tokens,
hs_desc_plaintext_data_t *desc,
const char *encoded_desc, size_t encoded_len)
{
int ok;
directory_token_t *tok;
tor_assert(tokens);
tor_assert(desc);
tor_assert(desc->version >= 3);
tok = find_by_keyword(tokens, R3_DESC_LIFETIME);
tor_assert(tok->n_args == 1);
desc->lifetime_sec = (uint32_t) tor_parse_ulong(tok->args[0], 10, 0,
UINT32_MAX, &ok, NULL);
if (!ok) {
log_warn(LD_REND, "Service descriptor lifetime value is invalid");
goto err;
}
desc->lifetime_sec *= 60;
if (desc->lifetime_sec > HS_DESC_MAX_LIFETIME) {
log_warn(LD_REND, "Service descriptor lifetime is too big. "
"Got %" PRIu32 " but max is %d",
desc->lifetime_sec, HS_DESC_MAX_LIFETIME);
goto err;
}
tok = find_by_keyword(tokens, R3_DESC_SIGNING_CERT);
tor_assert(tok->object_body);
if (strcmp(tok->object_type, "ED25519 CERT") != 0) {
log_warn(LD_REND, "Service descriptor signing cert wrong type (%s)",
escaped(tok->object_type));
goto err;
}
if (cert_parse_and_validate(&desc->signing_key_cert, tok->object_body,
tok->object_size, CERT_TYPE_SIGNING_HS_DESC,
"service descriptor signing key") < 0) {
goto err;
}
memcpy(&desc->signing_pubkey, &desc->signing_key_cert->signed_key,
sizeof(ed25519_public_key_t));
memcpy(&desc->blinded_pubkey, &desc->signing_key_cert->signing_key,
sizeof(ed25519_public_key_t));
tok = find_by_keyword(tokens, R3_REVISION_COUNTER);
tor_assert(tok->n_args == 1);
desc->revision_counter = tor_parse_uint64(tok->args[0], 10, 0,
UINT64_MAX, &ok, NULL);
if (!ok) {
log_warn(LD_REND, "Service descriptor revision-counter is invalid");
goto err;
}
tok = find_by_keyword(tokens, R3_SUPERENCRYPTED);
tor_assert(tok->object_body);
if (strcmp(tok->object_type, "MESSAGE") != 0) {
log_warn(LD_REND, "Desc superencrypted data section is invalid");
goto err;
}
if (!encrypted_data_length_is_valid(tok->object_size)) {
goto err;
}
desc->superencrypted_blob = tor_memdup(tok->object_body, tok->object_size);
desc->superencrypted_blob_size = tok->object_size;
tok = find_by_keyword(tokens, R3_SIGNATURE);
tor_assert(tok->n_args == 1);
if (!desc_sig_is_valid(tok->args[0], &desc->signing_pubkey,
encoded_desc, encoded_len)) {
goto err;
}
return HS_DESC_DECODE_OK;
err:
return HS_DESC_DECODE_PLAINTEXT_ERROR;
}
static hs_desc_decode_status_t
desc_decode_superencrypted_v3(const hs_descriptor_t *desc,
hs_desc_superencrypted_data_t *
desc_superencrypted_out)
{
int ret = HS_DESC_DECODE_SUPERENC_ERROR;
char *message = NULL;
size_t message_len;
memarea_t *area = NULL;
directory_token_t *tok;
smartlist_t *tokens = NULL;
hs_desc_superencrypted_data_t *superencrypted = desc_superencrypted_out;
tor_assert(desc);
tor_assert(desc_superencrypted_out);
message_len = desc_decrypt_superencrypted(desc, &message);
if (!message_len) {
log_warn(LD_REND, "Service descriptor decryption failed.");
goto err;
}
tor_assert(message);
area = memarea_new();
tokens = smartlist_new();
if (tokenize_string(area, message, message + message_len,
tokens, hs_desc_superencrypted_v3_token_table, 0) < 0) {
log_warn(LD_REND, "Superencrypted service descriptor is not parseable.");
goto err;
}
tok = find_by_keyword(tokens, R3_DESC_AUTH_TYPE);
tor_assert(tok->n_args >= 1);
if (strcmp(tok->args[0], "x25519")) {
log_warn(LD_DIR, "Unrecognized desc auth type");
goto err;
}
tok = find_by_keyword(tokens, R3_DESC_AUTH_KEY);
tor_assert(tok->n_args >= 1);
if (curve25519_public_from_base64(&superencrypted->auth_ephemeral_pubkey,
tok->args[0]) < 0) {
log_warn(LD_DIR, "Bogus desc auth ephemeral key in HS desc");
goto err;
}
if (!superencrypted->clients) {
superencrypted->clients = smartlist_new();
}
SMARTLIST_FOREACH_BEGIN(tokens, const directory_token_t *, token) {
if (token->tp == R3_DESC_AUTH_CLIENT) {
tor_assert(token->n_args >= 3);
hs_desc_authorized_client_t *client =
tor_malloc_zero(sizeof(hs_desc_authorized_client_t));
if (decode_auth_client(token, client) < 0) {
log_warn(LD_REND, "Descriptor client authorization section can't "
"be decoded.");
tor_free(client);
goto err;
}
smartlist_add(superencrypted->clients, client);
}
} SMARTLIST_FOREACH_END(token);
tok = find_by_keyword(tokens, R3_ENCRYPTED);
tor_assert(tok->object_body);
if (strcmp(tok->object_type, "MESSAGE") != 0) {
log_warn(LD_REND, "Desc encrypted data section is invalid");
goto err;
}
if (!encrypted_data_length_is_valid(tok->object_size)) {
goto err;
}
tor_assert(tok->object_size <= INT_MAX);
superencrypted->encrypted_blob = tor_memdup(tok->object_body,
tok->object_size);
superencrypted->encrypted_blob_size = tok->object_size;
ret = HS_DESC_DECODE_OK;
goto done;
err:
tor_assert(ret < HS_DESC_DECODE_OK);
hs_desc_superencrypted_data_free_contents(desc_superencrypted_out);
done:
if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens);
}
if (area) {
memarea_drop_all(area);
}
if (message) {
tor_free(message);
}
return ret;
}
static hs_desc_decode_status_t
desc_decode_encrypted_v3(const hs_descriptor_t *desc,
const curve25519_secret_key_t *client_auth_sk,
hs_desc_encrypted_data_t *desc_encrypted_out)
{
int ret = HS_DESC_DECODE_ENCRYPTED_ERROR;
char *message = NULL;
size_t message_len;
memarea_t *area = NULL;
directory_token_t *tok;
smartlist_t *tokens = NULL;
tor_assert(desc);
tor_assert(desc_encrypted_out);
message_len = desc_decrypt_encrypted(desc, client_auth_sk, &message);
if (!message_len) {
if (client_auth_sk) {
log_warn(LD_REND, "Client authorization for requested onion address "
"is invalid. Can't decrypt the descriptor.");
ret = HS_DESC_DECODE_BAD_CLIENT_AUTH;
} else {
log_notice(LD_REND, "Fail to decrypt descriptor for requested onion "
"address. It is likely requiring client "
"authorization.");
ret = HS_DESC_DECODE_NEED_CLIENT_AUTH;
}
goto err;
}
tor_assert(message);
area = memarea_new();
tokens = smartlist_new();
if (tokenize_string(area, message, message + message_len,
tokens, hs_desc_encrypted_v3_token_table, 0) < 0) {
log_warn(LD_REND, "Encrypted service descriptor is not parseable.");
goto err;
}
tok = find_by_keyword(tokens, R3_CREATE2_FORMATS);
tor_assert(tok);
decode_create2_list(desc_encrypted_out, tok->args[0]);
if (!desc_encrypted_out->create2_ntor) {
log_warn(LD_REND, "Service create2-formats does not include ntor.");
goto err;
}
tok = find_opt_by_keyword(tokens, R3_INTRO_AUTH_REQUIRED);
if (tok) {
if (!decode_auth_type(desc_encrypted_out, tok->args[0])) {
log_warn(LD_REND, "Service descriptor authentication type has "
"invalid entry(ies).");
goto err;
}
}
tok = find_opt_by_keyword(tokens, R3_SINGLE_ONION_SERVICE);
if (tok) {
desc_encrypted_out->single_onion_service = 1;
}
desc_encrypted_out->intro_points = smartlist_new();
decode_intro_points(desc, desc_encrypted_out, message);
if (smartlist_len(desc_encrypted_out->intro_points) >
HS_CONFIG_V3_MAX_INTRO_POINTS) {
log_warn(LD_REND, "Service descriptor contains too many introduction "
"points. Maximum allowed is %d but we have %d",
HS_CONFIG_V3_MAX_INTRO_POINTS,
smartlist_len(desc_encrypted_out->intro_points));
goto err;
}
ret = HS_DESC_DECODE_OK;
goto done;
err:
tor_assert(ret < HS_DESC_DECODE_OK);
hs_desc_encrypted_data_free_contents(desc_encrypted_out);
done:
if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens);
}
if (area) {
memarea_drop_all(area);
}
if (message) {
tor_free(message);
}
return ret;
}
static hs_desc_decode_status_t
(*decode_encrypted_handlers[])(
const hs_descriptor_t *desc,
const curve25519_secret_key_t *client_auth_sk,
hs_desc_encrypted_data_t *desc_encrypted) =
{
NULL, NULL, NULL,
desc_decode_encrypted_v3,
};
hs_desc_decode_status_t
hs_desc_decode_encrypted(const hs_descriptor_t *desc,
const curve25519_secret_key_t *client_auth_sk,
hs_desc_encrypted_data_t *desc_encrypted)
{
int ret = HS_DESC_DECODE_ENCRYPTED_ERROR;
uint32_t version;
tor_assert(desc);
version = desc->plaintext_data.version;
tor_assert(desc_encrypted);
tor_assert(desc->superencrypted_data.encrypted_blob);
if (BUG(!hs_desc_is_supported_version(version))) {
goto err;
}
tor_assert(ARRAY_LENGTH(decode_encrypted_handlers) >= version);
tor_assert(decode_encrypted_handlers[version]);
ret = decode_encrypted_handlers[version](desc, client_auth_sk,
desc_encrypted);
if (ret < 0) {
goto err;
}
err:
return ret;
}
static hs_desc_decode_status_t
(*decode_superencrypted_handlers[])(
const hs_descriptor_t *desc,
hs_desc_superencrypted_data_t *desc_superencrypted) =
{
NULL, NULL, NULL,
desc_decode_superencrypted_v3,
};
hs_desc_decode_status_t
hs_desc_decode_superencrypted(const hs_descriptor_t *desc,
hs_desc_superencrypted_data_t *
desc_superencrypted)
{
int ret = HS_DESC_DECODE_SUPERENC_ERROR;
uint32_t version;
tor_assert(desc);
version = desc->plaintext_data.version;
tor_assert(desc_superencrypted);
tor_assert(desc->plaintext_data.superencrypted_blob);
if (BUG(!hs_desc_is_supported_version(version))) {
goto err;
}
tor_assert(ARRAY_LENGTH(decode_superencrypted_handlers) >= version);
tor_assert(decode_superencrypted_handlers[version]);
ret = decode_superencrypted_handlers[version](desc, desc_superencrypted);
if (ret < 0) {
goto err;
}
err:
return ret;
}
static hs_desc_decode_status_t
(*decode_plaintext_handlers[])(
smartlist_t *tokens,
hs_desc_plaintext_data_t *desc,
const char *encoded_desc,
size_t encoded_len) =
{
NULL, NULL, NULL,
desc_decode_plaintext_v3,
};
hs_desc_decode_status_t
hs_desc_decode_plaintext(const char *encoded,
hs_desc_plaintext_data_t *plaintext)
{
int ok = 0, ret = HS_DESC_DECODE_PLAINTEXT_ERROR;
memarea_t *area = NULL;
smartlist_t *tokens = NULL;
size_t encoded_len;
directory_token_t *tok;
tor_assert(encoded);
tor_assert(plaintext);
encoded_len = strlen(encoded);
if (encoded_len >= hs_cache_get_max_descriptor_size()) {
log_warn(LD_REND, "Service descriptor is too big (%lu bytes)",
(unsigned long) encoded_len);
goto err;
}
area = memarea_new();
tokens = smartlist_new();
if (tokenize_string(area, encoded, encoded + encoded_len, tokens,
hs_desc_v3_token_table, 0) < 0) {
log_warn(LD_REND, "Service descriptor is not parseable");
goto err;
}
tok = find_by_keyword(tokens, R_HS_DESCRIPTOR);
tor_assert(tok->n_args == 1);
plaintext->version = (uint32_t) tor_parse_ulong(tok->args[0], 10, 0,
UINT32_MAX, &ok, NULL);
if (!ok) {
log_warn(LD_REND, "Service descriptor has unparseable version %s",
escaped(tok->args[0]));
goto err;
}
if (!hs_desc_is_supported_version(plaintext->version)) {
log_warn(LD_REND, "Service descriptor has unsupported version %" PRIu32,
plaintext->version);
goto err;
}
tor_assert(ARRAY_LENGTH(decode_plaintext_handlers) >= plaintext->version);
tor_assert(decode_plaintext_handlers[plaintext->version]);
ret = decode_plaintext_handlers[plaintext->version](tokens, plaintext,
encoded, encoded_len);
if (ret != HS_DESC_DECODE_OK) {
goto err;
}
ret = HS_DESC_DECODE_OK;
err:
if (tokens) {
SMARTLIST_FOREACH(tokens, directory_token_t *, t, token_clear(t));
smartlist_free(tokens);
}
if (area) {
memarea_drop_all(area);
}
return ret;
}
hs_desc_decode_status_t
hs_desc_decode_descriptor(const char *encoded,
const hs_subcredential_t *subcredential,
const curve25519_secret_key_t *client_auth_sk,
hs_descriptor_t **desc_out)
{
hs_desc_decode_status_t ret = HS_DESC_DECODE_GENERIC_ERROR;
hs_descriptor_t *desc;
tor_assert(encoded);
desc = tor_malloc_zero(sizeof(hs_descriptor_t));
if (BUG(!subcredential ||
fast_mem_is_zero((char*)subcredential, DIGEST256_LEN))) {
log_warn(LD_GENERAL, "Tried to decrypt without subcred. Impossible!");
goto err;
}
memcpy(&desc->subcredential, subcredential, sizeof(desc->subcredential));
ret = hs_desc_decode_plaintext(encoded, &desc->plaintext_data);
if (ret != HS_DESC_DECODE_OK) {
goto err;
}
ret = hs_desc_decode_superencrypted(desc, &desc->superencrypted_data);
if (ret != HS_DESC_DECODE_OK) {
goto err;
}
ret = hs_desc_decode_encrypted(desc, client_auth_sk, &desc->encrypted_data);
if (ret != HS_DESC_DECODE_OK) {
goto err;
}
if (desc_out) {
*desc_out = desc;
} else {
hs_descriptor_free(desc);
}
return ret;
err:
hs_descriptor_free(desc);
if (desc_out) {
*desc_out = NULL;
}
tor_assert(ret < 0);
return ret;
}
static int
(*encode_handlers[])(
const hs_descriptor_t *desc,
const ed25519_keypair_t *signing_kp,
const uint8_t *descriptor_cookie,
char **encoded_out) =
{
NULL, NULL, NULL,
desc_encode_v3,
};
MOCK_IMPL(int,
hs_desc_encode_descriptor,(const hs_descriptor_t *desc,
const ed25519_keypair_t *signing_kp,
const uint8_t *descriptor_cookie,
char **encoded_out))
{
int ret = -1;
uint32_t version;
tor_assert(desc);
tor_assert(encoded_out);
version = desc->plaintext_data.version;
if (!hs_desc_is_supported_version(version)) {
goto err;
}
tor_assert(ARRAY_LENGTH(encode_handlers) >= version);
tor_assert(encode_handlers[version]);
ret = encode_handlers[version](desc, signing_kp,
descriptor_cookie, encoded_out);
if (ret < 0) {
goto err;
}
if (!descriptor_cookie) {
ret = hs_desc_decode_descriptor(*encoded_out, &desc->subcredential,
NULL, NULL);
if (BUG(ret != HS_DESC_DECODE_OK)) {
ret = -1;
goto err;
}
}
return 0;
err:
*encoded_out = NULL;
return ret;
}
void
hs_desc_plaintext_data_free_contents(hs_desc_plaintext_data_t *desc)
{
if (!desc) {
return;
}
if (desc->superencrypted_blob) {
tor_free(desc->superencrypted_blob);
}
tor_cert_free(desc->signing_key_cert);
memwipe(desc, 0, sizeof(*desc));
}
void
hs_desc_superencrypted_data_free_contents(hs_desc_superencrypted_data_t *desc)
{
if (!desc) {
return;
}
if (desc->encrypted_blob) {
tor_free(desc->encrypted_blob);
}
if (desc->clients) {
SMARTLIST_FOREACH(desc->clients, hs_desc_authorized_client_t *, client,
hs_desc_authorized_client_free(client));
smartlist_free(desc->clients);
}
memwipe(desc, 0, sizeof(*desc));
}
void
hs_desc_encrypted_data_free_contents(hs_desc_encrypted_data_t *desc)
{
if (!desc) {
return;
}
if (desc->intro_auth_types) {
SMARTLIST_FOREACH(desc->intro_auth_types, char *, a, tor_free(a));
smartlist_free(desc->intro_auth_types);
}
if (desc->intro_points) {
SMARTLIST_FOREACH(desc->intro_points, hs_desc_intro_point_t *, ip,
hs_desc_intro_point_free(ip));
smartlist_free(desc->intro_points);
}
memwipe(desc, 0, sizeof(*desc));
}
void
hs_desc_plaintext_data_free_(hs_desc_plaintext_data_t *desc)
{
hs_desc_plaintext_data_free_contents(desc);
tor_free(desc);
}
void
hs_desc_superencrypted_data_free_(hs_desc_superencrypted_data_t *desc)
{
hs_desc_superencrypted_data_free_contents(desc);
tor_free(desc);
}
void
hs_desc_encrypted_data_free_(hs_desc_encrypted_data_t *desc)
{
hs_desc_encrypted_data_free_contents(desc);
tor_free(desc);
}
void
hs_descriptor_free_(hs_descriptor_t *desc)
{
if (!desc) {
return;
}
hs_desc_plaintext_data_free_contents(&desc->plaintext_data);
hs_desc_superencrypted_data_free_contents(&desc->superencrypted_data);
hs_desc_encrypted_data_free_contents(&desc->encrypted_data);
tor_free(desc);
}
size_t
hs_desc_plaintext_obj_size(const hs_desc_plaintext_data_t *data)
{
tor_assert(data);
return (sizeof(*data) + sizeof(*data->signing_key_cert) +
data->superencrypted_blob_size);
}
static size_t
hs_desc_encrypted_obj_size(const hs_desc_encrypted_data_t *data)
{
tor_assert(data);
size_t intro_size = 0;
if (data->intro_auth_types) {
intro_size +=
smartlist_len(data->intro_auth_types) * sizeof(intro_auth_types);
}
if (data->intro_points) {
intro_size +=
smartlist_len(data->intro_points) * sizeof(hs_desc_intro_point_t);
}
return sizeof(*data) + intro_size;
}
size_t
hs_desc_obj_size(const hs_descriptor_t *data)
{
if (data == NULL) {
return 0;
}
return (hs_desc_plaintext_obj_size(&data->plaintext_data) +
hs_desc_encrypted_obj_size(&data->encrypted_data) +
sizeof(data->subcredential));
}
hs_desc_intro_point_t *
hs_desc_intro_point_new(void)
{
hs_desc_intro_point_t *ip = tor_malloc_zero(sizeof(*ip));
ip->link_specifiers = smartlist_new();
return ip;
}
void
hs_desc_intro_point_free_(hs_desc_intro_point_t *ip)
{
if (ip == NULL) {
return;
}
if (ip->link_specifiers) {
SMARTLIST_FOREACH(ip->link_specifiers, link_specifier_t *,
ls, link_specifier_free(ls));
smartlist_free(ip->link_specifiers);
}
tor_cert_free(ip->auth_key_cert);
tor_cert_free(ip->enc_key_cert);
crypto_pk_free(ip->legacy.key);
tor_free(ip->legacy.cert.encoded);
tor_free(ip);
}
hs_desc_authorized_client_t *
hs_desc_build_fake_authorized_client(void)
{
hs_desc_authorized_client_t *client_auth =
tor_malloc_zero(sizeof(*client_auth));
crypto_rand((char *) client_auth->client_id,
sizeof(client_auth->client_id));
crypto_rand((char *) client_auth->iv,
sizeof(client_auth->iv));
crypto_rand((char *) client_auth->encrypted_cookie,
sizeof(client_auth->encrypted_cookie));
return client_auth;
}
void
hs_desc_build_authorized_client(const hs_subcredential_t *subcredential,
const curve25519_public_key_t *client_auth_pk,
const curve25519_secret_key_t *
auth_ephemeral_sk,
const uint8_t *descriptor_cookie,
hs_desc_authorized_client_t *client_out)
{
uint8_t *keystream = NULL;
size_t keystream_length = 0;
const uint8_t *cookie_key;
crypto_cipher_t *cipher;
tor_assert(client_auth_pk);
tor_assert(auth_ephemeral_sk);
tor_assert(descriptor_cookie);
tor_assert(client_out);
tor_assert(subcredential);
tor_assert(!fast_mem_is_zero((char *) auth_ephemeral_sk,
sizeof(*auth_ephemeral_sk)));
tor_assert(!fast_mem_is_zero((char *) client_auth_pk,
sizeof(*client_auth_pk)));
tor_assert(!fast_mem_is_zero((char *) descriptor_cookie,
HS_DESC_DESCRIPTOR_COOKIE_LEN));
tor_assert(!fast_mem_is_zero((char *) subcredential,
DIGEST256_LEN));
keystream_length =
build_descriptor_cookie_keys(subcredential,
auth_ephemeral_sk, client_auth_pk,
&keystream);
tor_assert(keystream_length > 0);
memcpy(client_out->client_id, keystream, HS_DESC_CLIENT_ID_LEN);
cookie_key = keystream + HS_DESC_CLIENT_ID_LEN;
crypto_strongest_rand(client_out->iv, sizeof(client_out->iv));
cipher = crypto_cipher_new_with_iv_and_bits(cookie_key, client_out->iv,
HS_DESC_COOKIE_KEY_BIT_SIZE);
crypto_cipher_encrypt(cipher, (char *) client_out->encrypted_cookie,
(const char *) descriptor_cookie,
HS_DESC_DESCRIPTOR_COOKIE_LEN);
memwipe(keystream, 0, keystream_length);
tor_free(keystream);
crypto_cipher_free(cipher);
}
void
hs_desc_authorized_client_free_(hs_desc_authorized_client_t *client)
{
tor_free(client);
}
void
hs_descriptor_clear_intro_points(hs_descriptor_t *desc)
{
smartlist_t *ips;
tor_assert(desc);
ips = desc->encrypted_data.intro_points;
if (ips) {
SMARTLIST_FOREACH(ips, hs_desc_intro_point_t *,
ip, hs_desc_intro_point_free(ip));
smartlist_clear(ips);
}
}