2011-11-30 20:47:30 +01:00
|
|
|
#ifndef OPENVPN_SSL_PROTOSTACK_H
|
|
|
|
#define OPENVPN_SSL_PROTOSTACK_H
|
|
|
|
|
2011-12-02 23:00:56 +01:00
|
|
|
#include <deque>
|
|
|
|
|
2011-11-30 20:47:30 +01:00
|
|
|
#include <openvpn/common/exception.hpp>
|
|
|
|
#include <openvpn/common/types.hpp>
|
|
|
|
#include <openvpn/common/usecount.hpp>
|
|
|
|
#include <openvpn/buffer/buffer.hpp>
|
|
|
|
#include <openvpn/time/time.hpp>
|
2011-12-11 09:28:55 +01:00
|
|
|
#include <openvpn/crypto/protostats.hpp>
|
2011-11-30 20:47:30 +01:00
|
|
|
#include <openvpn/reliable/relrecv.hpp>
|
|
|
|
#include <openvpn/reliable/relsend.hpp>
|
|
|
|
#include <openvpn/reliable/relack.hpp>
|
|
|
|
#include <openvpn/frame/frame.hpp>
|
|
|
|
|
|
|
|
namespace openvpn {
|
|
|
|
|
2011-12-04 11:55:28 +01:00
|
|
|
// PACKET type must define the following methods:
|
|
|
|
//
|
|
|
|
// Default constructor:
|
|
|
|
// PACKET()
|
|
|
|
//
|
|
|
|
// Constructor for BufferPtr:
|
|
|
|
// explicit PACKET(const BufferPtr& buf)
|
|
|
|
//
|
|
|
|
// Test if defined:
|
|
|
|
// operator bool() const
|
|
|
|
//
|
|
|
|
// Return true if packet is raw, or false if packet is SSL ciphertext:
|
|
|
|
// bool is_raw() const
|
|
|
|
//
|
|
|
|
// Reset back to post-default-constructor state:
|
|
|
|
// void reset()
|
|
|
|
//
|
|
|
|
// Return internal BufferPtr:
|
|
|
|
// const BufferPtr& buffer_ptr() const
|
|
|
|
//
|
|
|
|
// Call frame.prepare on internal buffer:
|
|
|
|
// void frame_prepare(const Frame& frame, const unsigned int context)
|
|
|
|
|
|
|
|
template <typename SSL_CONTEXT, typename PACKET>
|
2011-11-30 20:47:30 +01:00
|
|
|
class ProtoStackBase
|
|
|
|
{
|
|
|
|
public:
|
2011-12-04 11:55:28 +01:00
|
|
|
typedef SSL_CONTEXT SSLContext;
|
|
|
|
typedef reliable::id_t id_t;
|
|
|
|
typedef ReliableSendTemplate<PACKET> ReliableSend;
|
|
|
|
typedef ReliableRecvTemplate<PACKET> ReliableRecv;
|
2011-11-30 20:47:30 +01:00
|
|
|
|
|
|
|
OPENVPN_SIMPLE_EXCEPTION(proto_stack_invalidated);
|
|
|
|
|
|
|
|
ProtoStackBase(SSLContext& ctx,
|
2011-12-11 09:28:55 +01:00
|
|
|
TimePtr now_arg,
|
|
|
|
const Frame::Ptr& frame,
|
|
|
|
const ProtoStats::Ptr& stats_arg,
|
2011-12-02 23:00:56 +01:00
|
|
|
const id_t span,
|
|
|
|
const size_t max_ack_list)
|
2011-11-30 20:47:30 +01:00
|
|
|
: ssl_(ctx.ssl()),
|
|
|
|
frame_(frame),
|
|
|
|
up_stack_reentry_level(0),
|
2011-12-02 23:00:56 +01:00
|
|
|
invalidate(false),
|
2011-12-04 21:50:24 +01:00
|
|
|
ssl_started_(false),
|
2011-12-05 07:11:51 +01:00
|
|
|
next_retransmit_(Time::infinite()),
|
2011-12-11 09:28:55 +01:00
|
|
|
stats(stats_arg),
|
|
|
|
now(now_arg),
|
2011-12-05 07:11:51 +01:00
|
|
|
rel_recv(span),
|
|
|
|
rel_send(span),
|
|
|
|
xmit_acks(max_ack_list)
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-11 09:28:55 +01:00
|
|
|
// Start SSL handshake on underlying SSL connection object.
|
|
|
|
void start_handshake()
|
2011-12-02 23:00:56 +01:00
|
|
|
{
|
|
|
|
test_invalidated();
|
|
|
|
ssl_->start_handshake();
|
2011-12-04 21:50:24 +01:00
|
|
|
ssl_started_ = true;
|
2011-12-11 09:28:55 +01:00
|
|
|
up_sequenced();
|
2011-12-02 23:00:56 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 21:50:24 +01:00
|
|
|
// Incoming ciphertext packet arriving from network,
|
|
|
|
// we will take ownership of pkt.
|
2011-12-11 09:28:55 +01:00
|
|
|
void net_recv(PACKET& pkt)
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
|
|
|
test_invalidated();
|
2011-12-11 09:28:55 +01:00
|
|
|
up_stack(pkt);
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Outgoing application-level cleartext packet ready to send
|
2011-12-04 21:50:24 +01:00
|
|
|
// (will be encrypted via SSL), we will take ownership
|
|
|
|
// of buf.
|
2011-12-11 09:28:55 +01:00
|
|
|
void app_send(BufferPtr& buf)
|
2011-12-02 23:00:56 +01:00
|
|
|
{
|
2011-12-04 21:50:24 +01:00
|
|
|
test_invalidated();
|
2011-12-02 23:00:56 +01:00
|
|
|
app_write_queue.push_back(buf);
|
|
|
|
}
|
|
|
|
|
2011-12-04 02:34:32 +01:00
|
|
|
// Outgoing raw packet ready to send (will NOT be encrypted
|
2011-12-04 21:50:24 +01:00
|
|
|
// via SSL, but will still be encapsulated, sequentialized,
|
|
|
|
// and tracked via reliability layer).
|
2011-12-11 09:28:55 +01:00
|
|
|
void raw_send(const PACKET& pkt)
|
2011-12-04 02:34:32 +01:00
|
|
|
{
|
2011-12-04 21:50:24 +01:00
|
|
|
test_invalidated();
|
2011-12-04 11:55:28 +01:00
|
|
|
raw_write_queue.push_back(pkt);
|
2011-12-04 02:34:32 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 21:50:24 +01:00
|
|
|
// Write any pending data to network and update retransmit
|
|
|
|
// timer. Should be called as a final step after one or more
|
|
|
|
// net_recv, app_send, raw_send, or start_handshake calls.
|
2011-12-11 09:28:55 +01:00
|
|
|
void flush()
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
|
|
|
test_invalidated();
|
|
|
|
if (!up_stack_reentry_level)
|
2011-12-02 23:00:56 +01:00
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
down_stack_raw();
|
|
|
|
down_stack_app();
|
|
|
|
update_retransmit();
|
2011-12-02 23:00:56 +01:00
|
|
|
}
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
2011-12-02 23:00:56 +01:00
|
|
|
// Send pending ACKs back to sender for packets already received
|
2011-12-11 09:28:55 +01:00
|
|
|
void send_pending_acks()
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-02 23:00:56 +01:00
|
|
|
test_invalidated();
|
|
|
|
while (!xmit_acks.empty())
|
|
|
|
{
|
2011-12-04 11:55:28 +01:00
|
|
|
ack_send_buf.frame_prepare(*frame_, Frame::WRITE_ACK_STANDALONE);
|
2011-12-02 23:00:56 +01:00
|
|
|
|
|
|
|
// encapsulate standalone ACK
|
2011-12-11 09:28:55 +01:00
|
|
|
generate_ack(ack_send_buf);
|
2011-12-02 23:00:56 +01:00
|
|
|
|
|
|
|
// transmit it
|
2011-12-11 09:28:55 +01:00
|
|
|
net_send(ack_send_buf);
|
2011-12-02 23:00:56 +01:00
|
|
|
}
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send any pending retransmissions
|
2011-12-11 09:28:55 +01:00
|
|
|
void retransmit()
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-02 23:00:56 +01:00
|
|
|
test_invalidated();
|
2011-11-30 20:47:30 +01:00
|
|
|
for (id_t i = rel_send.head_id(); i < rel_send.tail_id(); ++i)
|
|
|
|
{
|
2011-12-04 11:55:28 +01:00
|
|
|
typename ReliableSend::Message& m = rel_send.ref_by_id(i);
|
2011-12-11 09:28:55 +01:00
|
|
|
if (m.ready_retransmit(*now))
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
net_send(m.packet);
|
|
|
|
m.reset_retransmit(*now);
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
2011-12-11 09:28:55 +01:00
|
|
|
update_retransmit();
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 02:34:32 +01:00
|
|
|
// When should we next call retransmit()
|
2011-12-02 23:00:56 +01:00
|
|
|
Time next_retransmit() const { return next_retransmit_; }
|
|
|
|
|
2011-12-04 21:50:24 +01:00
|
|
|
// Has SSL handshake been started yet?
|
|
|
|
bool ssl_started() const { return ssl_started_; }
|
|
|
|
|
2011-12-04 02:34:32 +01:00
|
|
|
// Was session invalidated by an exception?
|
2011-11-30 20:47:30 +01:00
|
|
|
bool invalidated() const { return invalidate; }
|
|
|
|
|
|
|
|
virtual ~ProtoStackBase() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// VIRTUAL METHODS -- derived class must define these virtual methods
|
|
|
|
|
2011-12-05 07:11:51 +01:00
|
|
|
// Encapsulate packet, use id as sequence number. If xmit_acks is non-empty,
|
|
|
|
// try to piggy-back ACK replies from xmit_acks to sender in encapsulated
|
|
|
|
// packet. Any exceptions thrown will invalidate session, i.e. this object
|
|
|
|
// can no longer be used.
|
2011-12-11 09:28:55 +01:00
|
|
|
virtual void encapsulate(id_t id, PACKET& pkt) = 0;
|
2011-11-30 20:47:30 +01:00
|
|
|
|
2011-12-05 07:11:51 +01:00
|
|
|
// Perform integrity check on packet. If packet is good, unencapsulate it and
|
|
|
|
// pass it into the rel_recv object. Any ACKs received for messages previously
|
|
|
|
// sent should be marked in rel_send. Message sequence number should be recorded
|
|
|
|
// in xmit_acks. Exceptions may be thrown here and they will be passed up to
|
|
|
|
// caller of net_recv and will not invalidate the session.
|
2011-12-11 09:28:55 +01:00
|
|
|
// Method should return true if packet was placed into rel_recv.
|
|
|
|
virtual bool decapsulate(PACKET& pkt) = 0;
|
2011-11-30 20:47:30 +01:00
|
|
|
|
2011-12-05 07:11:51 +01:00
|
|
|
// Generate a standalone ACK message in buf based on ACKs in xmit_acks
|
|
|
|
// (PACKET will be already be initialized by frame_prepare()).
|
2011-12-11 09:28:55 +01:00
|
|
|
virtual void generate_ack(PACKET& pkt) = 0;
|
2011-11-30 20:47:30 +01:00
|
|
|
|
2011-12-04 11:55:28 +01:00
|
|
|
// Transmit encapsulated ciphertext packet to peer. Method may not modify
|
2011-12-05 07:11:51 +01:00
|
|
|
// or take ownership of net_pkt or underlying data unless it copies it.
|
2011-12-11 09:28:55 +01:00
|
|
|
virtual void net_send(const PACKET& net_pkt) = 0;
|
2011-11-30 20:47:30 +01:00
|
|
|
|
|
|
|
// Pass cleartext data up to application. Method may take ownership
|
2011-12-02 23:00:56 +01:00
|
|
|
// of to_app_buf by making private copy of BufferPtr then calling
|
|
|
|
// reset on to_app_buf.
|
2011-12-11 09:28:55 +01:00
|
|
|
virtual void app_recv(BufferPtr& to_app_buf) = 0;
|
2011-11-30 20:47:30 +01:00
|
|
|
|
2011-12-04 11:55:28 +01:00
|
|
|
// Pass raw data up to application. A packet is considered to be raw
|
2011-12-04 21:50:24 +01:00
|
|
|
// if is_raw() method returns true. Method may take ownership
|
|
|
|
// of raw_pkt underlying data as long as it resets raw_pkt so that
|
|
|
|
// a subsequent call to PACKET::frame_prepare will revert it to
|
2011-12-05 07:11:51 +01:00
|
|
|
// a ready-to-use state.
|
2011-12-11 09:28:55 +01:00
|
|
|
virtual void raw_recv(PACKET& raw_pkt) = 0;
|
2011-12-04 11:55:28 +01:00
|
|
|
|
2011-11-30 20:47:30 +01:00
|
|
|
// END of VIRTUAL METHODS
|
|
|
|
|
|
|
|
|
|
|
|
// app data -> SSL -> protocol encapsulation -> reliability layer -> network
|
2011-12-11 09:28:55 +01:00
|
|
|
void down_stack_app()
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-04 21:50:24 +01:00
|
|
|
if (ssl_started_)
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-04 21:50:24 +01:00
|
|
|
// push app-layer cleartext through SSL object
|
|
|
|
while (!app_write_queue.empty())
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-04 21:50:24 +01:00
|
|
|
BufferPtr& buf = app_write_queue.front();
|
|
|
|
try {
|
|
|
|
const ssize_t size = ssl_->write_cleartext_unbuffered(buf->data(), buf->size());
|
|
|
|
if (size == SSLContext::SSL::SHOULD_RETRY)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
if (stats)
|
|
|
|
stats->error(ProtoStats::SSL_ERRORS);
|
2011-12-04 21:50:24 +01:00
|
|
|
invalidate = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
app_write_queue.pop_front();
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
2011-12-04 21:50:24 +01:00
|
|
|
// encapsulate SSL ciphertext packets
|
|
|
|
while (ssl_->read_ciphertext_ready() && rel_send.ready())
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
typename ReliableSend::Message& m = rel_send.send(*now);
|
2011-12-04 21:50:24 +01:00
|
|
|
m.packet = PACKET(ssl_->read_ciphertext());
|
2011-11-30 20:47:30 +01:00
|
|
|
|
2011-12-04 21:50:24 +01:00
|
|
|
// encapsulate packet
|
|
|
|
try {
|
2011-12-11 09:28:55 +01:00
|
|
|
encapsulate(m.id(), m.packet);
|
2011-12-04 21:50:24 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
invalidate = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transmit it
|
2011-12-11 09:28:55 +01:00
|
|
|
net_send(m.packet);
|
2011-12-04 21:50:24 +01:00
|
|
|
}
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-04 11:55:28 +01:00
|
|
|
// raw app data -> protocol encapsulation -> reliability layer -> network
|
2011-12-11 09:28:55 +01:00
|
|
|
void down_stack_raw()
|
2011-12-04 02:34:32 +01:00
|
|
|
{
|
|
|
|
while (!raw_write_queue.empty() && rel_send.ready())
|
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
typename ReliableSend::Message& m = rel_send.send(*now);
|
2011-12-04 11:55:28 +01:00
|
|
|
m.packet = raw_write_queue.front();
|
2011-12-04 02:34:32 +01:00
|
|
|
raw_write_queue.pop_front();
|
|
|
|
|
2011-12-04 11:55:28 +01:00
|
|
|
// encapsulate packet
|
2011-12-04 02:34:32 +01:00
|
|
|
try {
|
2011-12-11 09:28:55 +01:00
|
|
|
encapsulate(m.id(), m.packet);
|
2011-12-04 02:34:32 +01:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
invalidate = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
// transmit it
|
2011-12-11 09:28:55 +01:00
|
|
|
net_send(m.packet);
|
2011-12-04 02:34:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 20:47:30 +01:00
|
|
|
// network -> reliability layer -> protocol decapsulation -> SSL -> app
|
2011-12-11 09:28:55 +01:00
|
|
|
void up_stack(PACKET& recv)
|
2011-11-30 20:47:30 +01:00
|
|
|
{
|
|
|
|
UseCount use_count(up_stack_reentry_level);
|
2011-12-11 09:28:55 +01:00
|
|
|
if (decapsulate(recv))
|
|
|
|
up_sequenced();
|
2011-12-04 21:50:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if a sequenced packet is available from reliability layer,
|
|
|
|
// move it up the stack
|
2011-12-11 09:28:55 +01:00
|
|
|
void up_sequenced()
|
2011-12-04 21:50:24 +01:00
|
|
|
{
|
2011-11-30 20:47:30 +01:00
|
|
|
// is sequenced receive packet available?
|
|
|
|
while (rel_recv.ready())
|
|
|
|
{
|
2011-12-04 11:55:28 +01:00
|
|
|
typename ReliableRecv::Message& m = rel_recv.next_sequenced();
|
|
|
|
if (m.packet.is_raw())
|
2011-12-11 09:28:55 +01:00
|
|
|
raw_recv(m.packet);
|
2011-12-04 21:50:24 +01:00
|
|
|
else // SSL packet
|
2011-12-04 11:55:28 +01:00
|
|
|
{
|
2011-12-04 21:50:24 +01:00
|
|
|
if (ssl_started_)
|
|
|
|
ssl_->write_ciphertext(m.packet.buffer_ptr());
|
|
|
|
else
|
|
|
|
break;
|
2011-12-04 11:55:28 +01:00
|
|
|
}
|
2011-11-30 20:47:30 +01:00
|
|
|
rel_recv.advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read cleartext data from SSL object
|
2011-12-04 21:50:24 +01:00
|
|
|
if (ssl_started_)
|
|
|
|
while (ssl_->write_ciphertext_ready())
|
2011-12-04 11:55:28 +01:00
|
|
|
{
|
|
|
|
ssize_t size;
|
|
|
|
if (!to_app_buf)
|
|
|
|
to_app_buf.reset(new BufferAllocated());
|
|
|
|
frame_->prepare(Frame::READ_SSL_CLEARTEXT, *to_app_buf);
|
|
|
|
try {
|
|
|
|
size = ssl_->read_cleartext(to_app_buf->data(), to_app_buf->max_size());
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
2011-12-04 11:55:28 +01:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
// SSL fatal errors will invalidate the session
|
2011-12-11 09:28:55 +01:00
|
|
|
if (stats)
|
|
|
|
stats->error(ProtoStats::SSL_ERRORS);
|
2011-12-04 11:55:28 +01:00
|
|
|
invalidate = true;
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
if (size == SSLContext::SSL::SHOULD_RETRY)
|
|
|
|
break;
|
|
|
|
to_app_buf->set_size(size);
|
2011-11-30 20:47:30 +01:00
|
|
|
|
2011-12-04 11:55:28 +01:00
|
|
|
// pass cleartext data to app
|
2011-12-11 09:28:55 +01:00
|
|
|
app_recv(to_app_buf);
|
2011-12-04 11:55:28 +01:00
|
|
|
}
|
2011-11-30 20:47:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_invalidated() const
|
|
|
|
{
|
|
|
|
if (invalidate)
|
|
|
|
throw proto_stack_invalidated();
|
|
|
|
}
|
|
|
|
|
2011-12-11 09:28:55 +01:00
|
|
|
void update_retransmit()
|
2011-12-02 23:00:56 +01:00
|
|
|
{
|
2011-12-11 09:28:55 +01:00
|
|
|
const Time::Duration d = rel_send.until_retransmit(*now);
|
2011-12-02 23:00:56 +01:00
|
|
|
if (d.is_infinite())
|
|
|
|
next_retransmit_ = Time::infinite();
|
|
|
|
else
|
2011-12-11 09:28:55 +01:00
|
|
|
next_retransmit_ = *now + d;
|
2011-12-02 23:00:56 +01:00
|
|
|
}
|
|
|
|
|
2011-12-05 07:11:51 +01:00
|
|
|
private:
|
2011-11-30 20:47:30 +01:00
|
|
|
typename SSLContext::SSLPtr ssl_;
|
2011-12-11 09:28:55 +01:00
|
|
|
Frame::Ptr frame_;
|
2011-11-30 20:47:30 +01:00
|
|
|
int up_stack_reentry_level;
|
|
|
|
bool invalidate;
|
2011-12-04 21:50:24 +01:00
|
|
|
bool ssl_started_;
|
2011-12-02 23:00:56 +01:00
|
|
|
Time next_retransmit_;
|
2011-12-04 11:55:28 +01:00
|
|
|
BufferPtr to_app_buf; // cleartext data decrypted by SSL that is to be passed to app via app_recv method
|
|
|
|
PACKET ack_send_buf; // only used for standalone ACKs to be sent to peer
|
2011-12-02 23:00:56 +01:00
|
|
|
std::deque<BufferPtr> app_write_queue;
|
2011-12-04 11:55:28 +01:00
|
|
|
std::deque<PACKET> raw_write_queue;
|
2011-12-11 09:28:55 +01:00
|
|
|
ProtoStats::Ptr stats;
|
2011-12-05 07:11:51 +01:00
|
|
|
|
|
|
|
protected:
|
2011-12-11 09:28:55 +01:00
|
|
|
TimePtr now;
|
2011-12-05 07:11:51 +01:00
|
|
|
ReliableRecv rel_recv;
|
|
|
|
ReliableSend rel_send;
|
|
|
|
ReliableAck xmit_acks;
|
2011-11-30 20:47:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace openvpn
|
|
|
|
|
|
|
|
#endif // OPENVPN_SSL_PROTOSTACK_H
|