2017-03-15 21:13:17 +01:00
|
|
|
/* Copyright (c) 2016-2017, The Tor Project, Inc. */
|
2016-09-05 17:58:19 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file hs_service.c
|
|
|
|
* \brief Implement next generation hidden service functionality
|
|
|
|
**/
|
|
|
|
|
|
|
|
#include "or.h"
|
|
|
|
#include "relay.h"
|
|
|
|
#include "rendservice.h"
|
|
|
|
#include "circuitlist.h"
|
|
|
|
#include "circpathbias.h"
|
|
|
|
|
2016-12-22 22:59:18 +01:00
|
|
|
#include "hs_intropoint.h"
|
2016-09-05 17:58:19 +02:00
|
|
|
#include "hs_service.h"
|
|
|
|
#include "hs_common.h"
|
|
|
|
|
|
|
|
#include "hs/cell_establish_intro.h"
|
|
|
|
#include "hs/cell_common.h"
|
|
|
|
|
|
|
|
/* XXX We don't currently use these functions, apart from generating unittest
|
|
|
|
data. When we start implementing the service-side support for prop224 we
|
2017-04-04 14:35:31 +02:00
|
|
|
should revisit these functions and use them. */
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/** Given an ESTABLISH_INTRO <b>cell</b>, encode it and place its payload in
|
|
|
|
* <b>buf_out</b> which has size <b>buf_out_len</b>. Return the number of
|
|
|
|
* bytes written, or a negative integer if there was an error. */
|
2017-04-04 14:35:31 +02:00
|
|
|
ssize_t
|
2016-09-05 17:58:19 +02:00
|
|
|
get_establish_intro_payload(uint8_t *buf_out, size_t buf_out_len,
|
2017-04-11 19:46:41 +02:00
|
|
|
const trn_cell_establish_intro_t *cell)
|
2016-09-05 17:58:19 +02:00
|
|
|
{
|
|
|
|
ssize_t bytes_used = 0;
|
|
|
|
|
|
|
|
if (buf_out_len < RELAY_PAYLOAD_SIZE) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
bytes_used = trn_cell_establish_intro_encode(buf_out, buf_out_len,
|
2016-09-05 17:58:19 +02:00
|
|
|
cell);
|
|
|
|
return bytes_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the cell extensions of <b>cell</b>. */
|
|
|
|
static void
|
2017-04-11 19:46:41 +02:00
|
|
|
set_trn_cell_extensions(trn_cell_establish_intro_t *cell)
|
2016-09-05 17:58:19 +02:00
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extension_t *trn_cell_extensions = trn_cell_extension_new();
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/* For now, we don't use extensions at all. */
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_extensions->num = 0; /* It's already zeroed, but be explicit. */
|
|
|
|
trn_cell_establish_intro_set_extensions(cell, trn_cell_extensions);
|
2016-09-05 17:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Given the circuit handshake info in <b>circuit_key_material</b>, create and
|
|
|
|
* return an ESTABLISH_INTRO cell. Return NULL if something went wrong. The
|
|
|
|
* returned cell is allocated on the heap and it's the responsibility of the
|
|
|
|
* caller to free it. */
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_establish_intro_t *
|
2016-12-12 22:45:28 +01:00
|
|
|
generate_establish_intro_cell(const uint8_t *circuit_key_material,
|
2016-09-05 17:58:19 +02:00
|
|
|
size_t circuit_key_material_len)
|
|
|
|
{
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_establish_intro_t *cell = NULL;
|
2016-09-05 17:58:19 +02:00
|
|
|
ssize_t encoded_len;
|
|
|
|
|
|
|
|
log_warn(LD_GENERAL,
|
|
|
|
"Generating ESTABLISH_INTRO cell (key_material_len: %u)",
|
|
|
|
(unsigned) circuit_key_material_len);
|
|
|
|
|
|
|
|
/* Generate short-term keypair for use in ESTABLISH_INTRO */
|
|
|
|
ed25519_keypair_t key_struct;
|
|
|
|
if (ed25519_keypair_generate(&key_struct, 0) < 0) {
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
cell = trn_cell_establish_intro_new();
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/* Set AUTH_KEY_TYPE: 2 means ed25519 */
|
2017-04-13 22:43:13 +02:00
|
|
|
trn_cell_establish_intro_set_auth_key_type(cell,
|
|
|
|
HS_INTRO_AUTH_KEY_TYPE_ED25519);
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/* Set AUTH_KEY_LEN field */
|
|
|
|
/* Must also set byte-length of AUTH_KEY to match */
|
|
|
|
int auth_key_len = ED25519_PUBKEY_LEN;
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_establish_intro_set_auth_key_len(cell, auth_key_len);
|
|
|
|
trn_cell_establish_intro_setlen_auth_key(cell, auth_key_len);
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/* Set AUTH_KEY field */
|
2017-04-11 19:46:41 +02:00
|
|
|
uint8_t *auth_key_ptr = trn_cell_establish_intro_getarray_auth_key(cell);
|
2016-09-05 17:58:19 +02:00
|
|
|
memcpy(auth_key_ptr, key_struct.pubkey.pubkey, auth_key_len);
|
|
|
|
|
|
|
|
/* No cell extensions needed */
|
2017-04-11 19:46:41 +02:00
|
|
|
set_trn_cell_extensions(cell);
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/* Set signature size.
|
|
|
|
We need to do this up here, because _encode() needs it and we need to call
|
|
|
|
_encode() to calculate the MAC and signature.
|
|
|
|
*/
|
|
|
|
int sig_len = ED25519_SIG_LEN;
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_establish_intro_set_sig_len(cell, sig_len);
|
|
|
|
trn_cell_establish_intro_setlen_sig(cell, sig_len);
|
2016-09-05 17:58:19 +02:00
|
|
|
|
|
|
|
/* XXX How to make this process easier and nicer? */
|
|
|
|
|
|
|
|
/* Calculate the cell MAC (aka HANDSHAKE_AUTH). */
|
|
|
|
{
|
|
|
|
/* To calculate HANDSHAKE_AUTH, we dump the cell in bytes, and then derive
|
|
|
|
the MAC from it. */
|
|
|
|
uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
|
2016-12-12 22:45:28 +01:00
|
|
|
uint8_t mac[TRUNNEL_SHA3_256_LEN];
|
2016-09-05 17:58:19 +02:00
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
|
2016-09-05 17:58:19 +02:00
|
|
|
sizeof(cell_bytes_tmp),
|
|
|
|
cell);
|
|
|
|
if (encoded_len < 0) {
|
|
|
|
log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
tor_assert(encoded_len > ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN);
|
|
|
|
|
|
|
|
/* Calculate MAC of all fields before HANDSHAKE_AUTH */
|
|
|
|
crypto_mac_sha3_256(mac, sizeof(mac),
|
|
|
|
circuit_key_material, circuit_key_material_len,
|
2016-12-12 22:45:28 +01:00
|
|
|
cell_bytes_tmp,
|
2016-12-14 21:41:08 +01:00
|
|
|
encoded_len -
|
|
|
|
(ED25519_SIG_LEN + 2 + TRUNNEL_SHA3_256_LEN));
|
2016-09-05 17:58:19 +02:00
|
|
|
/* Write the MAC to the cell */
|
|
|
|
uint8_t *handshake_ptr =
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_establish_intro_getarray_handshake_mac(cell);
|
2016-09-05 17:58:19 +02:00
|
|
|
memcpy(handshake_ptr, mac, sizeof(mac));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate the cell signature */
|
|
|
|
{
|
|
|
|
/* To calculate the sig we follow the same procedure as above. We first
|
|
|
|
dump the cell up to the sig, and then calculate the sig */
|
|
|
|
uint8_t cell_bytes_tmp[RELAY_PAYLOAD_SIZE] = {0};
|
|
|
|
ed25519_signature_t sig;
|
|
|
|
|
2017-04-11 19:46:41 +02:00
|
|
|
encoded_len = trn_cell_establish_intro_encode(cell_bytes_tmp,
|
2016-09-05 17:58:19 +02:00
|
|
|
sizeof(cell_bytes_tmp),
|
|
|
|
cell);
|
|
|
|
if (encoded_len < 0) {
|
|
|
|
log_warn(LD_OR, "Unable to pre-encode ESTABLISH_INTRO cell (2).");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
tor_assert(encoded_len > ED25519_SIG_LEN);
|
|
|
|
|
|
|
|
if (ed25519_sign_prefixed(&sig,
|
2016-12-16 18:18:05 +01:00
|
|
|
cell_bytes_tmp,
|
|
|
|
encoded_len -
|
|
|
|
(ED25519_SIG_LEN + sizeof(cell->sig_len)),
|
2016-09-05 17:58:19 +02:00
|
|
|
ESTABLISH_INTRO_SIG_PREFIX,
|
|
|
|
&key_struct)) {
|
|
|
|
log_warn(LD_BUG, "Unable to gen signature for ESTABLISH_INTRO cell.");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And write the signature to the cell */
|
2017-04-11 19:46:41 +02:00
|
|
|
uint8_t *sig_ptr = trn_cell_establish_intro_getarray_sig(cell);
|
2016-09-05 17:58:19 +02:00
|
|
|
memcpy(sig_ptr, sig.sig, sig_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are done! Return the cell! */
|
|
|
|
return cell;
|
|
|
|
|
|
|
|
err:
|
2017-04-11 19:46:41 +02:00
|
|
|
trn_cell_establish_intro_free(cell);
|
2016-09-05 17:58:19 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|