#ifndef TOR_HS_DESCRIPTOR_H
#define TOR_HS_DESCRIPTOR_H
#include <stdint.h>
#include "core/or/or.h"
#include "trunnel/ed25519_cert.h"
#include "feature/nodelist/torcert.h"
#include "core/crypto/hs_ntor.h"
struct link_specifier_t;
#define HS_DESC_SUPPORTED_FORMAT_VERSION_MIN 3
#define HS_DESC_SUPPORTED_FORMAT_VERSION_MAX 3
#define HS_DESC_DEFAULT_LIFETIME (3 * 60 * 60)
#define HS_DESC_MAX_LIFETIME (12 * 60 * 60)
#define HS_DESC_CERT_LIFETIME (54 * 60 * 60)
#define HS_DESC_ENCRYPTED_SALT_LEN 16
#define HS_DESC_ENCRYPTED_KDF_OUTPUT_LEN \
CIPHER256_KEY_LEN + CIPHER_IV_LEN + DIGEST256_LEN
#define HS_DESC_SUPERENC_PLAINTEXT_PAD_MULTIPLE 10000
#define HS_DESC_MAX_LEN 50000
#define HS_DESC_ENCRYPTED_KEY_LEN CIPHER256_KEY_LEN
#define HS_DESC_ENCRYPTED_BIT_SIZE (HS_DESC_ENCRYPTED_KEY_LEN * 8)
#define HS_DESC_CLIENT_ID_LEN 8
#define HS_DESC_DESCRIPTOR_COOKIE_LEN 16
#define HS_DESC_COOKIE_KEY_LEN 32
#define HS_DESC_COOKIE_KEY_BIT_SIZE (HS_DESC_COOKIE_KEY_LEN * 8)
#define HS_DESC_ENCRYPED_COOKIE_LEN HS_DESC_DESCRIPTOR_COOKIE_LEN
#define HS_DESC_AUTH_CLIENT_MULTIPLE 16
typedef enum {
HS_DESC_AUTH_ED25519 = 1
} hs_desc_auth_type_t;
typedef enum {
HS_DESC_DECODE_BAD_CLIENT_AUTH = -6,
HS_DESC_DECODE_NEED_CLIENT_AUTH = -5,
HS_DESC_DECODE_ENCRYPTED_ERROR = -4,
HS_DESC_DECODE_SUPERENC_ERROR = -3,
HS_DESC_DECODE_PLAINTEXT_ERROR = -2,
HS_DESC_DECODE_GENERIC_ERROR = -1,
HS_DESC_DECODE_OK = 0,
} hs_desc_decode_status_t;
typedef struct hs_desc_intro_point_t {
smartlist_t *link_specifiers;
curve25519_public_key_t onion_key;
tor_cert_t *auth_key_cert;
curve25519_public_key_t enc_key;
tor_cert_t *enc_key_cert;
struct {
crypto_pk_t *key;
struct {
uint8_t *encoded;
size_t len;
} cert;
} legacy;
unsigned int cross_certified : 1;
} hs_desc_intro_point_t;
typedef struct hs_desc_authorized_client_t {
uint8_t client_id[HS_DESC_CLIENT_ID_LEN];
uint8_t iv[CIPHER_IV_LEN];
uint8_t encrypted_cookie[HS_DESC_ENCRYPED_COOKIE_LEN];
} hs_desc_authorized_client_t;
typedef struct hs_desc_encrypted_data_t {
unsigned int create2_ntor : 1;
smartlist_t *intro_auth_types;
unsigned int single_onion_service : 1;
smartlist_t *intro_points;
} hs_desc_encrypted_data_t;
typedef struct hs_desc_superencrypted_data_t {
curve25519_public_key_t auth_ephemeral_pubkey;
smartlist_t *clients;
uint8_t *encrypted_blob;
size_t encrypted_blob_size;
} hs_desc_superencrypted_data_t;
typedef struct hs_desc_plaintext_data_t {
uint32_t version;
uint32_t lifetime_sec;
tor_cert_t *signing_key_cert;
ed25519_public_key_t signing_pubkey;
ed25519_public_key_t blinded_pubkey;
uint64_t revision_counter;
uint8_t *superencrypted_blob;
size_t superencrypted_blob_size;
} hs_desc_plaintext_data_t;
typedef struct hs_descriptor_t {
hs_desc_plaintext_data_t plaintext_data;
hs_desc_superencrypted_data_t superencrypted_data;
hs_desc_encrypted_data_t encrypted_data;
hs_subcredential_t subcredential;
} hs_descriptor_t;
static inline int
hs_desc_is_supported_version(uint32_t version)
{
if (version < HS_DESC_SUPPORTED_FORMAT_VERSION_MIN ||
version > HS_DESC_SUPPORTED_FORMAT_VERSION_MAX) {
return 0;
}
return 1;
}
void hs_descriptor_free_(hs_descriptor_t *desc);
#define hs_descriptor_free(desc) \
FREE_AND_NULL(hs_descriptor_t, hs_descriptor_free_, (desc))
void hs_desc_plaintext_data_free_(hs_desc_plaintext_data_t *desc);
#define hs_desc_plaintext_data_free(desc) \
FREE_AND_NULL(hs_desc_plaintext_data_t, hs_desc_plaintext_data_free_, (desc))
void hs_desc_superencrypted_data_free_(hs_desc_superencrypted_data_t *desc);
#define hs_desc_superencrypted_data_free(desc) \
FREE_AND_NULL(hs_desc_superencrypted_data_t, \
hs_desc_superencrypted_data_free_, (desc))
void hs_desc_encrypted_data_free_(hs_desc_encrypted_data_t *desc);
#define hs_desc_encrypted_data_free(desc) \
FREE_AND_NULL(hs_desc_encrypted_data_t, hs_desc_encrypted_data_free_, (desc))
void hs_descriptor_clear_intro_points(hs_descriptor_t *desc);
MOCK_DECL(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 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);
int hs_desc_decode_plaintext(const char *encoded,
hs_desc_plaintext_data_t *plaintext);
int hs_desc_decode_superencrypted(const hs_descriptor_t *desc,
hs_desc_superencrypted_data_t *desc_out);
int hs_desc_decode_encrypted(const hs_descriptor_t *desc,
const curve25519_secret_key_t *client_auth_sk,
hs_desc_encrypted_data_t *desc_out);
size_t hs_desc_obj_size(const hs_descriptor_t *data);
size_t hs_desc_plaintext_obj_size(const hs_desc_plaintext_data_t *data);
hs_desc_intro_point_t *hs_desc_intro_point_new(void);
void hs_desc_intro_point_free_(hs_desc_intro_point_t *ip);
#define hs_desc_intro_point_free(ip) \
FREE_AND_NULL(hs_desc_intro_point_t, hs_desc_intro_point_free_, (ip))
void hs_desc_authorized_client_free_(hs_desc_authorized_client_t *client);
#define hs_desc_authorized_client_free(client) \
FREE_AND_NULL(hs_desc_authorized_client_t, \
hs_desc_authorized_client_free_, (client))
hs_desc_authorized_client_t *hs_desc_build_fake_authorized_client(void);
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);
void hs_desc_plaintext_data_free_contents(hs_desc_plaintext_data_t *desc);
void hs_desc_superencrypted_data_free_contents(
hs_desc_superencrypted_data_t *desc);
void hs_desc_encrypted_data_free_contents(hs_desc_encrypted_data_t *desc);
#ifdef HS_DESCRIPTOR_PRIVATE
STATIC char *encode_link_specifiers(const smartlist_t *specs);
STATIC size_t build_plaintext_padding(const char *plaintext,
size_t plaintext_len,
uint8_t **padded_out);
STATIC smartlist_t *decode_link_specifiers(const char *encoded);
STATIC hs_desc_intro_point_t *decode_introduction_point(
const hs_descriptor_t *desc,
const char *text);
STATIC int encrypted_data_length_is_valid(size_t len);
STATIC int cert_is_valid(tor_cert_t *cert, uint8_t type,
const char *log_obj_type);
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);
MOCK_DECL(STATIC size_t, decrypt_desc_layer,(const hs_descriptor_t *desc,
const uint8_t *descriptor_cookie,
bool is_superencrypted_layer,
char **decrypted_out));
#endif
#endif