Skip to content

Commit

Permalink
Merge "[HICN-595] Bring TLS up to date"
Browse files Browse the repository at this point in the history
  • Loading branch information
muscariello authored and Gerrit Code Review committed May 13, 2020
2 parents 76c0e9a + eb91199 commit dc5a429
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 625 deletions.
8 changes: 4 additions & 4 deletions libtransport/includes/hicn/transport/core/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ class Packet : public std::enable_shared_from_this<Packet> {

std::pair<const uint8_t *, std::size_t> getPayloadReference() const {
int signature_size = 0;

if (_is_ah(format_)) {
signature_size = (uint32_t)getSignatureSize();
}

auto header_size = getHeaderSizeFromFormat(format_, signature_size);
auto payload_length = packet_->length() - header_size;
auto payload_length = payloadSize();

return std::make_pair(packet_->data() + header_size,
payload_length);
return std::make_pair(packet_->data() + header_size, payload_length);
}

Packet &updateLength(std::size_t length = 0);
Expand Down Expand Up @@ -229,6 +229,7 @@ class Packet : public std::enable_shared_from_this<Packet> {
Packet &setTTL(uint8_t hops);
uint8_t getTTL() const;

void separateHeaderPayload();
void resetPayload();

private:
Expand All @@ -248,7 +249,6 @@ class Packet : public std::enable_shared_from_this<Packet> {
}

uint8_t *getSignature() const;
void separateHeaderPayload();

protected:
Name name_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace interface {

class P2PSecureConsumerSocket : public ConsumerSocket {
public:
P2PSecureConsumerSocket(int handshake_protocol, int protocol);
P2PSecureConsumerSocket(int handshake_protocol, int transport_protocol);
~P2PSecureConsumerSocket() = default;
void registerPrefix(const Prefix &producer_namespace);
};

} // namespace interface

} // end namespace transport
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class ConsumerSocket {

/**
* This method will be called by the transport for understanding how many
* bytes it should read (at most) before notifying the application.
* bytes it should read before notifying the application.
*
* By default it reads 64 KB.
*/
Expand Down
4 changes: 2 additions & 2 deletions libtransport/src/core/prefix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ Name Prefix::getName(const core::Name &mask, const core::Name &components,
}
}

if (this->contains(name_ip))
throw errors::RuntimeException("Mask overrides the prefix");
// if (this->contains(name_ip))
// throw errors::RuntimeException("Mask overrides the prefix");
return Name(ip_prefix_.family, (uint8_t *)&name_ip);
}

Expand Down
149 changes: 74 additions & 75 deletions libtransport/src/implementation/p2psecure_socket_consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ void P2PSecureConsumerSocket::setInterestPayload(
if (payload_ != NULL) int2.appendPayload(std::move(payload_));
}

// implement void readBufferAvailable(), size_t maxBufferSize() const override,
// void readError(), void readSuccess(). getReadBuffer() and readDataAvailable()
// must be implemented even if empty.

/* Return the number of read bytes in the return param */
int readOld(BIO *b, char *buf, int size) {
if (size < 0) return size;
Expand All @@ -51,11 +47,13 @@ int readOld(BIO *b, char *buf, int size) {
socket->network_name_.setSuffix(socket->random_suffix_);
socket->ConsumerSocket::asyncConsume(socket->network_name_);
}

if (!socket->something_to_read_) socket->cv_.wait(lck);
}

size_t size_to_read, read;
size_t chain_size = socket->head_->length();

if (socket->head_->isChained())
chain_size = socket->head_->computeChainDataLength();

Expand Down Expand Up @@ -106,6 +104,7 @@ int writeOld(BIO *b, const char *buf, int num) {
socket = (P2PSecureConsumerSocket *)BIO_get_data(b);

socket->payload_ = utils::MemBuf::copyBuffer(buf, num);

socket->ConsumerSocket::setSocketOption(
ConsumerCallbacksOptions::INTEREST_OUTPUT,
(ConsumerInterestCallback)std::bind(
Expand Down Expand Up @@ -173,9 +172,9 @@ int P2PSecureConsumerSocket::parseHicnKeyIdCb(SSL *s, unsigned int ext_type,
P2PSecureConsumerSocket::P2PSecureConsumerSocket(
interface::ConsumerSocket *consumer, int handshake_protocol,
int transport_protocol)
: ConsumerSocket(consumer, transport_protocol),
: ConsumerSocket(consumer, handshake_protocol),
name_(),
tls_consumer_(),
tls_consumer_(nullptr),
buf_pool_(),
decrypted_content_(),
payload_(),
Expand Down Expand Up @@ -224,12 +223,6 @@ P2PSecureConsumerSocket::P2PSecureConsumerSocket(
BIO_set_data(bio, this);
SSL_set_bio(ssl_, bio, bio);

ConsumerSocket::getSocketOption(MAX_WINDOW_SIZE, old_max_win_);
ConsumerSocket::setSocketOption(MAX_WINDOW_SIZE, (double)1.0);

ConsumerSocket::getSocketOption(CURRENT_WINDOW_SIZE, old_current_win_);
ConsumerSocket::setSocketOption(CURRENT_WINDOW_SIZE, (double)1.0);

std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(
1, std::numeric_limits<uint32_t>::max());
Expand All @@ -244,76 +237,29 @@ P2PSecureConsumerSocket::~P2PSecureConsumerSocket() {
SSL_shutdown(ssl_);
}

int P2PSecureConsumerSocket::consume(const Name &name) {
if (transport_protocol_->isRunning()) {
return CONSUMER_BUSY;
}
int P2PSecureConsumerSocket::handshake() {
int result = 1;

if ((SSL_in_before(this->ssl_) || SSL_in_init(this->ssl_))) {
ConsumerSocket::setSocketOption(MAX_WINDOW_SIZE, (double)1.0);
network_name_ = producer_namespace_.getRandomName();
network_name_.setSuffix(0);
int result = SSL_connect(this->ssl_);
ConsumerSocket::setSocketOption(MAX_WINDOW_SIZE, old_max_win_);
ConsumerSocket::setSocketOption(CURRENT_WINDOW_SIZE, old_current_win_);
if (result != 1)
throw errors::RuntimeException("Unable to perform client handshake");
if (!(SSL_in_before(this->ssl_) || SSL_in_init(this->ssl_))) {
return 1;
}
std::shared_ptr<Name> prefix_name = std::make_shared<Name>(
secure_prefix_.family,
ip_address_get_buffer(&(secure_prefix_.address), secure_prefix_.family));
std::shared_ptr<Prefix> prefix =
std::make_shared<Prefix>(*prefix_name, secure_prefix_.len);
TLSConsumerSocket tls_consumer(nullptr, this->protocol_, this->ssl_);
tls_consumer.setInterface(new interface::TLSConsumerSocket(&tls_consumer));

ConsumerTimerCallback *stats_summary_callback = nullptr;
this->getSocketOption(ConsumerCallbacksOptions::STATS_SUMMARY,
&stats_summary_callback);
ConsumerSocket::getSocketOption(MAX_WINDOW_SIZE, old_max_win_);
ConsumerSocket::getSocketOption(CURRENT_WINDOW_SIZE, old_current_win_);

uint32_t lifetime;
this->getSocketOption(GeneralTransportOptions::INTEREST_LIFETIME, lifetime);
tls_consumer.setSocketOption(GeneralTransportOptions::INTEREST_LIFETIME,
lifetime);
tls_consumer.setSocketOption(ConsumerCallbacksOptions::READ_CALLBACK,
read_callback_decrypted_);
tls_consumer.setSocketOption(ConsumerCallbacksOptions::STATS_SUMMARY,
*stats_summary_callback);
tls_consumer.setSocketOption(GeneralTransportOptions::STATS_INTERVAL,
this->timer_interval_milliseconds_);
tls_consumer.setSocketOption(MAX_WINDOW_SIZE, old_max_win_);
tls_consumer.setSocketOption(CURRENT_WINDOW_SIZE, old_current_win_);
tls_consumer.connect();
ConsumerSocket::setSocketOption(MAX_WINDOW_SIZE, (double)1.0);
ConsumerSocket::setSocketOption(CURRENT_WINDOW_SIZE, (double)1.0);

if (payload_ != NULL)
return tls_consumer.consume((prefix->mapName(name)), std::move(payload_));
else
return tls_consumer.consume((prefix->mapName(name)));
}
network_name_ = producer_namespace_.getRandomName();
network_name_.setSuffix(0);

int P2PSecureConsumerSocket::asyncConsume(const Name &name) {
if ((SSL_in_before(this->ssl_) || SSL_in_init(this->ssl_))) {
ConsumerSocket::setSocketOption(CURRENT_WINDOW_SIZE, (double)1.0);
ConsumerSocket::setSocketOption(MAX_WINDOW_SIZE, (double)1.0);
network_name_ = producer_namespace_.getRandomName();
network_name_.setSuffix(0);
TRANSPORT_LOGD("Start handshake at %s", network_name_.toString().c_str());
interface::ConsumerSocket::ReadCallback *on_payload = VOID_HANDLER;
this->getSocketOption(ConsumerCallbacksOptions::READ_CALLBACK, &on_payload);
int result = SSL_connect(this->ssl_);
ConsumerSocket::setSocketOption(MAX_WINDOW_SIZE, old_max_win_);
ConsumerSocket::setSocketOption(CURRENT_WINDOW_SIZE, old_current_win_);
if (result != 1)
throw errors::RuntimeException("Unable to perform client handshake");
TRANSPORT_LOGD("Handshake performed!");
}
TRANSPORT_LOGD("Start handshake at %s", network_name_.toString().c_str());
result = SSL_connect(this->ssl_);

std::shared_ptr<Name> prefix_name = std::make_shared<Name>(
secure_prefix_.family,
ip_address_get_buffer(&(secure_prefix_.address), secure_prefix_.family));
std::shared_ptr<Prefix> prefix =
std::make_shared<Prefix>(*prefix_name, secure_prefix_.len);
return result;
}

void P2PSecureConsumerSocket::initSessionSocket() {
tls_consumer_ =
std::make_shared<TLSConsumerSocket>(nullptr, this->protocol_, this->ssl_);
tls_consumer_->setInterface(
Expand All @@ -325,6 +271,7 @@ int P2PSecureConsumerSocket::asyncConsume(const Name &name) {

uint32_t lifetime;
this->getSocketOption(GeneralTransportOptions::INTEREST_LIFETIME, lifetime);

tls_consumer_->setSocketOption(GeneralTransportOptions::INTEREST_LIFETIME,
lifetime);
tls_consumer_->setSocketOption(ConsumerCallbacksOptions::READ_CALLBACK,
Expand All @@ -336,6 +283,59 @@ int P2PSecureConsumerSocket::asyncConsume(const Name &name) {
tls_consumer_->setSocketOption(MAX_WINDOW_SIZE, old_max_win_);
tls_consumer_->setSocketOption(CURRENT_WINDOW_SIZE, old_current_win_);
tls_consumer_->connect();
}

int P2PSecureConsumerSocket::consume(const Name &name) {
if (transport_protocol_->isRunning()) {
return CONSUMER_BUSY;
}

if (handshake() != 1) {
throw errors::RuntimeException("Unable to perform client handshake");
} else {
TRANSPORT_LOGD("Handshake performed!");
}

initSessionSocket();

if (tls_consumer_ == nullptr) {
throw errors::RuntimeException("TLS socket does not exist");
}

std::shared_ptr<Name> prefix_name = std::make_shared<Name>(
secure_prefix_.family,
ip_address_get_buffer(&(secure_prefix_.address), secure_prefix_.family));
std::shared_ptr<Prefix> prefix =
std::make_shared<Prefix>(*prefix_name, secure_prefix_.len);

if (payload_ != nullptr)
return tls_consumer_->consume((prefix->mapName(name)), std::move(payload_));
else
return tls_consumer_->consume((prefix->mapName(name)));
}

int P2PSecureConsumerSocket::asyncConsume(const Name &name) {
if (transport_protocol_->isRunning()) {
return CONSUMER_BUSY;
}

if (handshake() != 1) {
throw errors::RuntimeException("Unable to perform client handshake");
} else {
TRANSPORT_LOGD("Handshake performed!");
}

initSessionSocket();

if (tls_consumer_ == nullptr) {
throw errors::RuntimeException("TLS socket does not exist");
}

std::shared_ptr<Name> prefix_name = std::make_shared<Name>(
secure_prefix_.family,
ip_address_get_buffer(&(secure_prefix_.address), secure_prefix_.family));
std::shared_ptr<Prefix> prefix =
std::make_shared<Prefix>(*prefix_name, secure_prefix_.len);

if (payload_ != NULL)
return tls_consumer_->asyncConsume((prefix->mapName(name)),
Expand Down Expand Up @@ -399,5 +399,4 @@ void P2PSecureConsumerSocket::readSuccess(std::size_t total_size) noexcept {
bool P2PSecureConsumerSocket::isBufferMovable() noexcept { return true; }

} // namespace implementation

} // namespace transport
18 changes: 4 additions & 14 deletions libtransport/src/implementation/p2psecure_socket_consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,26 @@ class P2PSecureConsumerSocket : public ConsumerSocket,
private:
Name name_;
std::shared_ptr<TLSConsumerSocket> tls_consumer_;

/* SSL handle */
SSL *ssl_;
SSL_CTX *ctx_;
BIO_METHOD *bio_meth_;

/* Chain of MemBuf to be used as a temporary buffer to pass descypted data
* from the underlying layer to the application */
utils::ObjectPool<utils::MemBuf> buf_pool_;
std::unique_ptr<utils::MemBuf> decrypted_content_;

/* Chain of MemBuf holding the payload to be written into interest or data */
std::unique_ptr<utils::MemBuf> payload_;

/* Chain of MemBuf holding the data retrieved from the underlying layer */
std::unique_ptr<utils::MemBuf> head_;

bool something_to_read_;

bool content_downloaded_;

double old_max_win_;

double old_current_win_;

uint32_t random_suffix_;

ip_prefix_t secure_prefix_;

Prefix producer_namespace_;

interface::ConsumerSocket::ReadCallback *read_callback_decrypted_;

std::mutex mtx_;

/* Condition variable for the wait */
Expand Down Expand Up @@ -138,9 +125,12 @@ class P2PSecureConsumerSocket : public ConsumerSocket,
virtual void readError(const std::error_code ec) noexcept override;

virtual void readSuccess(std::size_t total_size) noexcept override;

virtual bool isBufferMovable() noexcept override;

int download_content(const Name &name);
int handshake();

void initSessionSocket();
};

} // namespace implementation
Expand Down
Loading

0 comments on commit dc5a429

Please sign in to comment.