Merge commit '86cc97e55fe346502462284d2e636a2b3708163e' as 'Sources/OpenVPN3'

This commit is contained in:
Sergey Abramchuk
2020-02-24 14:43:11 +03:00
655 changed files with 146468 additions and 0 deletions
@@ -0,0 +1,56 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_ASIOBUF_H
#define OPENVPN_BUFFER_ASIOBUF_H
#include <openvpn/io/io.hpp>
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
class AsioConstBufferSeq2
{
public:
AsioConstBufferSeq2(const Buffer& b1, const Buffer& b2)
: buf({{b1.c_data(), b1.size()},
{b2.c_data(), b2.size()}})
{
}
// Implement the ConstBufferSequence requirements.
typedef openvpn_io::const_buffer value_type;
typedef const openvpn_io::const_buffer* const_iterator;
const openvpn_io::const_buffer* begin() const { return buf; }
const openvpn_io::const_buffer* end() const { return buf + 2; }
const size_t size() const
{
return openvpn_io::buffer_size(buf[0])
+ openvpn_io::buffer_size(buf[1]);
}
private:
const openvpn_io::const_buffer buf[2];
};
}
#endif
@@ -0,0 +1,52 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// For debugging, reduce effective buffer size for I/O.
// Enable by defining OPENVPN_BUF_CLAMP_READ and/or OPENVPN_BUF_CLAMP_WRITE
#ifndef OPENVPN_BUFFER_BUFCLAMP_H
#define OPENVPN_BUFFER_BUFCLAMP_H
#include <algorithm>
#include <openvpn/common/size.hpp>
namespace openvpn {
inline size_t buf_clamp_read(const size_t size)
{
#ifdef OPENVPN_BUF_CLAMP_READ
return std::min(size, size_t(OPENVPN_BUF_CLAMP_READ));
#else
return size;
#endif
}
inline size_t buf_clamp_write(const size_t size)
{
#ifdef OPENVPN_BUF_CLAMP_WRITE
return std::min(size, size_t(OPENVPN_BUF_CLAMP_WRITE));
#else
return size;
#endif
}
}
#endif
@@ -0,0 +1,112 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFCOMPLETE_H
#define OPENVPN_BUFFER_BUFCOMPLETE_H
#include <cstdint> // for std::uint32_t, uint16_t, uint8_t
#include <algorithm> // for std::min
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
class BufferComplete
{
public:
/* each advance/get method returns false if message is incomplete */
bool advance(size_t size)
{
while (size)
{
if (!fetch_buffer())
return false;
const size_t s = std::min(size, buf.size());
buf.advance(s);
size -= s;
}
return true;
}
// assumes embedded big-endian uint16_t length in the stream
bool advance_string()
{
std::uint8_t h, l;
if (!get(h))
return false;
if (!get(l))
return false;
return advance(size_t(h) << 8 | size_t(l));
}
bool advance_to_null()
{
std::uint8_t c;
while (get(c))
{
if (!c)
return true;
}
return false;
}
bool get(std::uint8_t& c)
{
if (!fetch_buffer())
return false;
c = buf.pop_front();
return true;
}
bool defined() const
{
return buf.defined();
}
protected:
void reset_buf(const Buffer& buf_arg)
{
buf = buf_arg;
}
void reset_buf()
{
buf.reset_content();
}
private:
virtual void next_buffer() = 0;
bool fetch_buffer()
{
if (buf.defined())
return true;
next_buffer();
return buf.defined();
}
Buffer buf;
};
}
#endif
@@ -0,0 +1,94 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFCOMPOSED_H
#define OPENVPN_BUFFER_BUFCOMPOSED_H
#include <openvpn/common/exception.hpp>
#include <openvpn/buffer/bufcomplete.hpp>
#include <openvpn/buffer/buflist.hpp>
namespace openvpn {
class BufferComposed
{
public:
class Complete : public BufferComplete
{
public:
BufferPtr get()
{
#if 0 // don't include for production
if (iter_defined())
throw Exception("BufferComposed::Complete: residual data");
#endif
BufferPtr ret = bc.bv.join();
bc.bv.clear();
return ret;
}
private:
friend class BufferComposed;
Complete(BufferComposed& bc_arg)
: bc(bc_arg),
iter(bc.bv.cbegin())
{
next_buffer();
}
bool iter_defined()
{
return iter != bc.bv.end();
}
virtual void next_buffer() override
{
if (iter_defined())
reset_buf(**iter++);
else
reset_buf();
}
BufferComposed& bc;
BufferVector::const_iterator iter;
};
size_t size() const
{
return bv.join_size();
}
void put(BufferPtr bp)
{
bv.push_back(std::move(bp));
}
Complete complete()
{
return Complete(*this);
}
private:
BufferVector bv;
};
}
#endif
+911
View File
@@ -0,0 +1,911 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// These templates define the fundamental data buffer classes used by the
// OpenVPN core. Normally OpenVPN uses buffers of unsigned chars, but the
// templatization of the classes would allow buffers of other types to
// be defined.
//
// Fundamentally a buffer is an object with 4 fields:
//
// 1. a pointer to underlying data array
// 2. the capacity of the underlying data array
// 3. an offset into the data array
// 4. the size of the referenced data within the array
//
// The BufferType template is the lowest-level buffer class template. It refers
// to a buffer but without any notion of ownership of the underlying data.
//
// The BufferAllocatedType template is a higher-level template that inherits
// from BufferType but which asserts ownership over the resources of the buffer --
// for example, it will free the underlying buffer in its destructor.
//
// Since most of the time, we want our buffers to be made out of unsigned chars,
// some typedefs at the end of the file define common instantations for the
// BufferType and BufferAllocatedType templates.
//
// Buffer : a simple buffer of unsigned char without ownership semantics
// ConstBuffer : like buffer but where the data pointed to by the buffer is const
// BufferAllocated : an allocated Buffer with ownership semantics
// BufferPtr : a smart, reference-counted pointer to a BufferAllocated
#ifndef OPENVPN_BUFFER_BUFFER_H
#define OPENVPN_BUFFER_BUFFER_H
#include <string>
#include <cstring>
#include <algorithm>
#include <type_traits> // for std::is_nothrow_move_constructible, std::remove_const
#ifndef OPENVPN_NO_IO
#include <openvpn/io/io.hpp>
#endif
#include <openvpn/common/size.hpp>
#include <openvpn/common/abort.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/buffer/bufclamp.hpp>
#ifdef OPENVPN_BUFFER_ABORT
#define OPENVPN_BUFFER_THROW(exc) { std::abort(); }
#else
#define OPENVPN_BUFFER_THROW(exc) { throw BufferException(BufferException::exc); }
#endif
namespace openvpn {
// special-purpose exception class for Buffer classes
class BufferException : public std::exception
{
public:
enum Status {
buffer_full,
buffer_headroom,
buffer_underflow,
buffer_overflow,
buffer_offset,
buffer_index,
buffer_const_index,
buffer_push_front_headroom,
buffer_no_reset_impl,
buffer_pop_back,
buffer_set_size,
buffer_range,
};
BufferException(Status status)
: status_(status) {}
Status status() const { return status_; }
const char *status_string() const
{
switch (status_)
{
case buffer_full:
return "buffer_full";
case buffer_headroom:
return "buffer_headroom";
case buffer_underflow:
return "buffer_underflow";
case buffer_overflow:
return "buffer_overflow";
case buffer_offset:
return "buffer_offset";
case buffer_index:
return "buffer_index";
case buffer_const_index:
return "buffer_const_index";
case buffer_push_front_headroom:
return "buffer_push_front_headroom";
case buffer_no_reset_impl:
return "buffer_no_reset_impl";
case buffer_pop_back:
return "buffer_pop_back";
case buffer_set_size:
return "buffer_set_size";
case buffer_range:
return "buffer_range";
default:
return "buffer_???";
}
}
virtual const char* what() const throw() {
return status_string();
}
virtual ~BufferException() throw() {}
private:
Status status_;
};
template <typename T, typename R>
class BufferAllocatedType;
template <typename T>
class BufferType {
template <typename, typename> friend class BufferAllocatedType;
public:
typedef T value_type;
typedef T* type;
typedef const T* const_type;
typedef typename std::remove_const<T>::type NCT; // non-const type
BufferType()
{
static_assert(std::is_nothrow_move_constructible<BufferType>::value, "class BufferType not noexcept move constructable");
data_ = nullptr;
offset_ = size_ = capacity_ = 0;
}
BufferType(T* data, const size_t size, const bool filled)
{
data_ = data;
offset_ = 0;
capacity_ = size;
size_ = filled ? size : 0;
}
void reserve(const size_t n)
{
if (n > capacity_)
resize(n);
}
void init_headroom(const size_t headroom)
{
if (headroom > capacity_)
OPENVPN_BUFFER_THROW(buffer_headroom);
offset_ = headroom;
size_ = 0;
}
void reset_offset(const size_t offset)
{
const size_t size = size_ + offset_ - offset;
if (offset > capacity_ || size > capacity_ || offset + size > capacity_)
OPENVPN_BUFFER_THROW(buffer_offset);
offset_ = offset;
size_ = size;
}
void reset_size()
{
size_ = 0;
}
void reset_content()
{
offset_ = size_ = 0;
}
// std::string compatible methods
const T* c_str() const { return c_data(); }
size_t length() const { return size(); }
// return a const pointer to start of array
const T* c_data() const { return data_ + offset_; }
// return a mutable pointer to start of array
T* data() { return data_ + offset_; }
// return a const pointer to end of array
const T* c_data_end() const { return data_ + offset_ + size_; }
// return a mutable pointer to end of array
T* data_end() { return data_ + offset_ + size_; }
// return a const pointer to start of raw data
const T* c_data_raw() const { return data_; }
// return a mutable pointer to start of raw data
T* data_raw() { return data_; }
// return size of array in T objects
size_t size() const { return size_; }
// return raw size of allocated buffer in T objects
size_t capacity() const { return capacity_; }
// return current offset (headroom) into buffer
size_t offset() const { return offset_; }
// return true if array is not empty
bool defined() const { return size_ > 0; }
// return true if data memory is defined
bool allocated() const { return data_ != nullptr; }
// return true if array is empty
bool empty() const { return !size_; }
// return the number of additional T objects that can be added before capacity is reached (without considering resize)
size_t remaining(const size_t tailroom = 0) const {
const size_t r = capacity_ - (offset_ + size_ + tailroom);
return r <= capacity_ ? r : 0;
}
// return the maximum allowable size value in T objects given the current offset (without considering resize)
size_t max_size() const {
const size_t r = capacity_ - offset_;
return r <= capacity_ ? r : 0;
}
// like max_size, but take tailroom into account
size_t max_size_tailroom(const size_t tailroom) const {
const size_t r = capacity_ - (offset_ + tailroom);
return r <= capacity_ ? r : 0;
}
// After an external method, operating on the array as
// a mutable unsigned char buffer, has written data to the
// array, use this method to set the array length in terms
// of T objects.
void set_size(const size_t size)
{
if (size > max_size())
OPENVPN_BUFFER_THROW(buffer_set_size);
size_ = size;
}
// Increment size (usually used in a similar context
// to set_size such as after mutable_buffer_append).
void inc_size(const size_t delta)
{
set_size(size_ + delta);
}
// append a T object to array, with possible resize
void push_back(const T& value)
{
if (!remaining())
resize(offset_ + size_ + 1);
*(data()+size_++) = value;
}
// append a T object to array, with possible resize
void push_front(const T& value)
{
if (!offset_)
OPENVPN_BUFFER_THROW(buffer_push_front_headroom);
--offset_;
++size_;
*data() = value;
}
T pop_back()
{
if (!size_)
OPENVPN_BUFFER_THROW(buffer_pop_back);
return *(data()+(--size_));
}
T pop_front()
{
T ret = (*this)[0];
++offset_;
--size_;
return ret;
}
T front()
{
return (*this)[0];
}
T back()
{
return (*this)[size_-1];
}
// Place a T object after the last object in the
// array, with possible resize to contain it,
// however don't actually change the size of the
// array to reflect the added object. Useful
// for maintaining null-terminated strings.
void set_trailer(const T& value)
{
if (!remaining())
resize(offset_ + size_ + 1);
*(data()+size_) = value;
}
void null_terminate()
{
if (empty() || back())
push_back(0);
}
void advance(const size_t delta)
{
if (delta > size_)
OPENVPN_BUFFER_THROW(buffer_overflow);
offset_ += delta;
size_ -= delta;
}
bool contains_null() const
{
const T* end = c_data_end();
for (const T* p = c_data(); p < end; ++p)
{
if (!*p)
return true;
}
return false;
}
bool is_zeroed() const
{
const T* end = c_data_end();
for (const T* p = c_data(); p < end; ++p)
{
if (*p)
return false;
}
return true;
}
// mutable index into array
T& operator[](const size_t index)
{
if (index >= size_)
OPENVPN_BUFFER_THROW(buffer_index);
return data()[index];
}
// const index into array
const T& operator[](const size_t index) const
{
if (index >= size_)
OPENVPN_BUFFER_THROW(buffer_const_index);
return c_data()[index];
}
// mutable index into array
T* index(const size_t index)
{
if (index >= size_)
OPENVPN_BUFFER_THROW(buffer_index);
return &data()[index];
}
// const index into array
const T* c_index(const size_t index) const
{
if (index >= size_)
OPENVPN_BUFFER_THROW(buffer_const_index);
return &c_data()[index];
}
bool operator==(const BufferType& other) const
{
if (size_ != other.size_)
return false;
return std::memcmp(c_data(), other.c_data(), size_) == 0;
}
bool operator!=(const BufferType& other) const
{
return !(*this == other);
}
#ifndef OPENVPN_NO_IO
// return a openvpn_io::mutable_buffer object used by
// asio read methods, starting from data()
openvpn_io::mutable_buffer mutable_buffer(const size_t tailroom = 0)
{
return openvpn_io::mutable_buffer(data(), max_size_tailroom(tailroom));
}
// return a openvpn_io::mutable_buffer object used by
// asio read methods, starting from data_end()
openvpn_io::mutable_buffer mutable_buffer_append(const size_t tailroom = 0)
{
return openvpn_io::mutable_buffer(data_end(), remaining(tailroom));
}
// return a openvpn_io::const_buffer object used by
// asio write methods.
openvpn_io::const_buffer const_buffer() const
{
return openvpn_io::const_buffer(c_data(), size());
}
// clamped versions of mutable_buffer(), mutable_buffer_append(),
// and const_buffer()
openvpn_io::mutable_buffer mutable_buffer_clamp(const size_t tailroom = 0)
{
return openvpn_io::mutable_buffer(data(), buf_clamp_read(max_size_tailroom(tailroom)));
}
openvpn_io::mutable_buffer mutable_buffer_append_clamp(const size_t tailroom = 0)
{
return openvpn_io::mutable_buffer(data_end(), buf_clamp_read(remaining(tailroom)));
}
openvpn_io::const_buffer const_buffer_clamp() const
{
return openvpn_io::const_buffer(c_data(), buf_clamp_write(size()));
}
openvpn_io::const_buffer const_buffer_limit(const size_t limit) const
{
return openvpn_io::const_buffer(c_data(), std::min(buf_clamp_write(size()), limit));
}
#endif
void realign(size_t headroom)
{
if (headroom != offset_)
{
if (headroom + size_ > capacity_)
OPENVPN_BUFFER_THROW(buffer_headroom);
std::memmove(data_ + headroom, data_ + offset_, size_);
offset_ = headroom;
}
}
void write(const T* data, const size_t size)
{
std::memcpy(write_alloc(size), data, size * sizeof(T));
}
void write(const void* data, const size_t size)
{
write((const T*)data, size);
}
void prepend(const T* data, const size_t size)
{
std::memcpy(prepend_alloc(size), data, size * sizeof(T));
}
void prepend(const void* data, const size_t size)
{
prepend((const T*)data, size);
}
void read(NCT* data, const size_t size)
{
std::memcpy(data, read_alloc(size), size * sizeof(T));
}
void read(void* data, const size_t size)
{
read((NCT*)data, size);
}
T* write_alloc(const size_t size)
{
if (size > remaining())
resize(offset_ + size_ + size);
T* ret = data() + size_;
size_ += size;
return ret;
}
T* prepend_alloc(const size_t size)
{
if (size <= offset_)
{
offset_ -= size;
size_ += size;
return data();
}
else
OPENVPN_BUFFER_THROW(buffer_headroom);
}
T* read_alloc(const size_t size)
{
if (size <= size_)
{
T* ret = data();
offset_ += size;
size_ -= size;
return ret;
}
else
OPENVPN_BUFFER_THROW(buffer_underflow);
}
BufferType read_alloc_buf(const size_t size)
{
if (size <= size_)
{
BufferType ret(data_, offset_, size, capacity_);
offset_ += size;
size_ -= size;
return ret;
}
else
OPENVPN_BUFFER_THROW(buffer_underflow);
}
void reset(const size_t min_capacity, const unsigned int flags)
{
if (min_capacity > capacity_)
reset_impl(min_capacity, flags);
}
void reset(const size_t headroom, const size_t min_capacity, const unsigned int flags)
{
reset(min_capacity, flags);
init_headroom(headroom);
}
template <typename B>
void append(const B& other)
{
write(other.c_data(), other.size());
}
BufferType range(size_t offset, size_t len) const
{
if (offset + len > size())
{
if (offset < size())
len = size() - offset;
else
len = 0;
}
return BufferType(datac(), offset, len, len);
}
protected:
BufferType(T* data, const size_t offset, const size_t size, const size_t capacity)
: data_(data), offset_(offset), size_(size), capacity_(capacity)
{
}
// return a mutable pointer to start of array but
// remain const with respect to *this.
T* datac() const { return data_ + offset_; }
// Called when reset method needs to expand the buffer size
virtual void reset_impl(const size_t min_capacity, const unsigned int flags)
{
OPENVPN_BUFFER_THROW(buffer_no_reset_impl);
}
// Derived classes can implement buffer growing semantics
// by overloading this method. In the default implementation,
// buffers are non-growable, so we throw an exception.
virtual void resize(const size_t new_capacity)
{
if (new_capacity > capacity_)
{
OPENVPN_BUFFER_THROW(buffer_full);
}
}
T* data_; // pointer to data
size_t offset_; // offset from data_ of beginning of T array (to allow for headroom)
size_t size_; // number of T objects in array starting at data_ + offset_
size_t capacity_; // maximum number of array objects of type T for which memory is allocated, starting at data_
};
template <typename T, typename R>
class BufferAllocatedType : public BufferType<T>, public RC<R>
{
using BufferType<T>::data_;
using BufferType<T>::offset_;
using BufferType<T>::size_;
using BufferType<T>::capacity_;
template <typename, typename> friend class BufferAllocatedType;
public:
enum {
CONSTRUCT_ZERO = (1<<0), // if enabled, constructors/init will zero allocated space
DESTRUCT_ZERO = (1<<1), // if enabled, destructor will zero data before deletion
GROW = (1<<2), // if enabled, buffer will grow (otherwise buffer_full exception will be thrown)
ARRAY = (1<<3), // if enabled, use as array
};
BufferAllocatedType()
{
static_assert(std::is_nothrow_move_constructible<BufferAllocatedType>::value, "class BufferAllocatedType not noexcept move constructable");
flags_ = 0;
}
BufferAllocatedType(const size_t capacity, const unsigned int flags)
{
flags_ = flags;
capacity_ = capacity;
if (capacity)
{
data_ = new T[capacity];
if (flags & CONSTRUCT_ZERO)
std::memset(data_, 0, capacity * sizeof(T));
if (flags & ARRAY)
size_ = capacity;
}
}
BufferAllocatedType(const T* data, const size_t size, const unsigned int flags)
{
flags_ = flags;
size_ = capacity_ = size;
if (size)
{
data_ = new T[size];
std::memcpy(data_, data, size * sizeof(T));
}
}
BufferAllocatedType(const BufferAllocatedType& other)
{
offset_ = other.offset_;
size_ = other.size_;
capacity_ = other.capacity_;
flags_ = other.flags_;
if (capacity_)
{
data_ = new T[capacity_];
if (size_)
std::memcpy(data_ + offset_, other.data_ + offset_, size_ * sizeof(T));
}
}
template <typename T_>
BufferAllocatedType(const BufferType<T_>& other, const unsigned int flags)
{
static_assert(sizeof(T) == sizeof(T_), "size inconsistency");
offset_ = other.offset_;
size_ = other.size_;
capacity_ = other.capacity_;
flags_ = flags;
if (capacity_)
{
data_ = new T[capacity_];
if (size_)
std::memcpy(data_ + offset_, other.data_ + offset_, size_ * sizeof(T));
}
}
void operator=(const BufferAllocatedType& other)
{
if (this != &other)
{
offset_ = size_ = 0;
if (capacity_ != other.capacity_)
{
erase_();
if (other.capacity_)
data_ = new T[other.capacity_];
capacity_ = other.capacity_;
}
offset_ = other.offset_;
size_ = other.size_;
flags_ = other.flags_;
if (size_)
std::memcpy(data_ + offset_, other.data_ + offset_, size_ * sizeof(T));
}
}
void init(const size_t capacity, const unsigned int flags)
{
offset_ = size_ = 0;
flags_ = flags;
if (capacity_ != capacity)
{
erase_();
if (capacity)
{
data_ = new T[capacity];
}
capacity_ = capacity;
}
if ((flags & CONSTRUCT_ZERO) && capacity)
std::memset(data_, 0, capacity * sizeof(T));
if (flags & ARRAY)
size_ = capacity;
}
void init(const T* data, const size_t size, const unsigned int flags)
{
offset_ = size_ = 0;
flags_ = flags;
if (size != capacity_)
{
erase_();
if (size)
data_ = new T[size];
capacity_ = size;
}
size_ = size;
std::memcpy(data_, data, size * sizeof(T));
}
void realloc(const size_t newcap)
{
if (newcap > capacity_)
realloc_(newcap);
}
void reset(const size_t min_capacity, const unsigned int flags)
{
if (min_capacity > capacity_)
init (min_capacity, flags);
}
void reset(const size_t headroom, const size_t min_capacity, const unsigned int flags)
{
reset(min_capacity, flags);
BufferType<T>::init_headroom(headroom);
}
template <typename T_, typename R_>
void move(BufferAllocatedType<T_, R_>& other)
{
if (data_)
delete_(data_, capacity_, flags_);
move_(other);
}
RCPtr<BufferAllocatedType<T, R>> move_to_ptr()
{
RCPtr<BufferAllocatedType<T, R>> bp = new BufferAllocatedType<T, R>();
bp->move(*this);
return bp;
}
void swap(BufferAllocatedType& other)
{
std::swap(data_, other.data_);
std::swap(offset_, other.offset_);
std::swap(size_, other.size_);
std::swap(capacity_, other.capacity_);
std::swap(flags_, other.flags_);
}
template <typename T_, typename R_>
BufferAllocatedType(BufferAllocatedType<T_, R_>&& other) noexcept
{
move_(other);
}
BufferAllocatedType& operator=(BufferAllocatedType&& other) noexcept
{
move(other);
return *this;
}
void clear()
{
erase_();
flags_ = 0;
size_ = offset_ = 0;
}
void or_flags(const unsigned int flags)
{
flags_ |= flags;
}
void and_flags(const unsigned int flags)
{
flags_ &= flags;
}
~BufferAllocatedType()
{
if (data_)
delete_(data_, capacity_, flags_);
}
protected:
// Called when reset method needs to expand the buffer size
virtual void reset_impl(const size_t min_capacity, const unsigned int flags)
{
init(min_capacity, flags);
}
// Set current capacity to at least new_capacity.
virtual void resize(const size_t new_capacity)
{
const size_t newcap = std::max(new_capacity, capacity_ * 2);
if (newcap > capacity_)
{
if (flags_ & GROW)
realloc_(newcap);
else
OPENVPN_BUFFER_THROW(buffer_full);
}
}
void realloc_(const size_t newcap)
{
T* data = new T[newcap];
if (size_)
std::memcpy(data + offset_, data_ + offset_, size_ * sizeof(T));
delete_(data_, capacity_, flags_);
data_ = data;
//std::cout << "*** RESIZE " << capacity_ << " -> " << newcap << std::endl; // fixme
capacity_ = newcap;
}
template <typename T_, typename R_>
void move_(BufferAllocatedType<T_, R_>& other)
{
data_ = other.data_;
offset_ = other.offset_;
size_ = other.size_;
capacity_ = other.capacity_;
flags_ = other.flags_;
other.data_ = nullptr;
other.offset_ = other.size_ = other.capacity_ = 0;
}
void erase_()
{
if (data_)
{
delete_(data_, capacity_, flags_);
data_ = nullptr;
}
capacity_ = 0;
}
static void delete_(T* data, const size_t size, const unsigned int flags)
{
if (size && (flags & DESTRUCT_ZERO))
std::memset(data, 0, size * sizeof(T));
delete [] data;
}
unsigned int flags_;
};
// specializations of BufferType for unsigned char
typedef BufferType<unsigned char> Buffer;
typedef BufferType<const unsigned char> ConstBuffer;
typedef BufferAllocatedType<unsigned char, thread_unsafe_refcount> BufferAllocated;
typedef RCPtr<BufferAllocated> BufferPtr;
// BufferAllocated with thread-safe refcount
typedef BufferAllocatedType<unsigned char, thread_safe_refcount> BufferAllocatedTS;
typedef RCPtr<BufferAllocatedTS> BufferPtrTS;
// cast BufferType<T> to BufferType<const T>
template <typename T>
inline BufferType<const T>& const_buffer_ref(BufferType<T>& src)
{
return (BufferType<const T>&)src;
}
template <typename T>
inline const BufferType<const T>& const_buffer_ref(const BufferType<T>& src)
{
return (const BufferType<const T>&)src;
}
} // namespace openvpn
#endif // OPENVPN_BUFFER_BUFFER_H
@@ -0,0 +1,61 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFHEX_H
#define OPENVPN_BUFFER_BUFHEX_H
#include <openvpn/common/hexstr.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
namespace BufHex {
OPENVPN_EXCEPTION(buf_hex);
template <typename T>
inline std::string render(const T obj)
{
const ConstBuffer buf((const unsigned char *)&obj, sizeof(obj), true);
return render_hex_generic(buf);
}
template <typename T>
inline T parse(const std::string& hex, const std::string& title)
{
T obj;
Buffer buf((unsigned char *)&obj, sizeof(obj), false);
try {
parse_hex(buf, hex);
}
catch (const BufferException& e)
{
OPENVPN_THROW(buf_hex, title << ": buffer issue: " << e.what());
}
if (buf.size() != sizeof(obj))
OPENVPN_THROW(buf_hex, title << ": unexpected size");
return obj;
}
}
}
#endif
@@ -0,0 +1,92 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFLIMIT_H
#define OPENVPN_BUFFER_BUFLIMIT_H
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
template <typename T>
class BufferLimit
{
public:
BufferLimit()
{
set_max(0, 0);
reset();
}
BufferLimit(const T max_lines_arg,
const T max_bytes_arg)
{
set_max(max_lines_arg, max_bytes_arg);
reset();
}
void set_max(const T max_lines_arg,
const T max_bytes_arg)
{
max_lines = max_lines_arg;
max_bytes = max_bytes_arg;
}
void reset()
{
n_bytes = n_lines = 0;
}
void add(const Buffer& buf)
{
T size = (T)buf.size();
n_bytes += size;
if (max_bytes && n_bytes > max_bytes)
bytes_exceeded();
if (max_lines)
{
const unsigned char *p = buf.c_data();
while (size--)
{
const unsigned char c = *p++;
if (c == '\n')
{
++n_lines;
if (n_lines > max_lines)
lines_exceeded();
}
}
}
}
virtual void bytes_exceeded() = 0;
virtual void lines_exceeded() = 0;
protected:
T max_lines;
T max_bytes;
T n_bytes;
T n_lines;
};
}
#endif
@@ -0,0 +1,58 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2019 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
// Iterate over the lines in a buffer by returning
// a sub-buffer for each line. Zero-copy.
class BufferLineIterator
{
public:
BufferLineIterator(const ConstBuffer& buf)
: src(buf)
{
}
// Returns a zero-length buffer at end of iteration
ConstBuffer next()
{
return src.read_alloc_buf(line_len());
}
private:
size_t line_len() const
{
const unsigned char *const data = src.c_data();
size_t i = 0;
while (i < src.size())
if (data[i++] == '\n')
break;
return i;
}
ConstBuffer src;
};
}
+122
View File
@@ -0,0 +1,122 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFLIST_H
#define OPENVPN_BUFFER_BUFLIST_H
#include <list>
#include <utility>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/buffer/bufstr.hpp>
namespace openvpn {
template <template <typename...> class COLLECTION>
struct BufferCollection : public COLLECTION<BufferPtr>
{
using COLLECTION<BufferPtr>::size;
using COLLECTION<BufferPtr>::front;
using COLLECTION<BufferPtr>::empty;
using COLLECTION<BufferPtr>::back;
using COLLECTION<BufferPtr>::emplace_back;
BufferPtr join(const size_t headroom,
const size_t tailroom,
const bool size_1_optim) const
{
// special optimization if list contains
// a single element that satisfies our
// headroom/tailroom constraints.
if (size_1_optim
&& size() == 1
&& front()->offset() >= headroom
&& front()->remaining() >= tailroom)
return front();
// first pass -- measure total size
const size_t size = join_size();
// allocate buffer
BufferPtr big = new BufferAllocated(size + headroom + tailroom, 0);
big->init_headroom(headroom);
// second pass -- copy data
for (auto &b : *this)
big->write(b->c_data(), b->size());
return big;
}
BufferPtr join() const
{
return join(0, 0, true);
}
size_t join_size() const
{
size_t size = 0;
for (auto &b : *this)
size += b->size();
return size;
}
std::string to_string() const
{
BufferPtr bp = join();
return buf_to_string(*bp);
}
BufferCollection copy() const
{
BufferCollection ret;
for (auto &b : *this)
ret.emplace_back(new BufferAllocated(*b));
return ret;
}
void put_consume(BufferAllocated& buf, const size_t tailroom = 0)
{
const size_t s = buf.size();
if (!s)
return;
if (!empty())
{
// special optimization if buf data fits in
// back() unused tail capacity -- if so, append
// buf to existing back().
BufferPtr& b = back();
const size_t r = b->remaining(tailroom);
if (s < r)
{
b->write(buf.read_alloc(s), s);
return;
}
}
emplace_back(new BufferAllocated(std::move(buf)));
}
};
typedef BufferCollection<std::list> BufferList;
typedef BufferCollection<std::vector> BufferVector;
}
#endif
@@ -0,0 +1,67 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFREAD_H
#define OPENVPN_BUFFER_BUFREAD_H
#include <unistd.h>
#include <errno.h>
#include <string>
#include <cstring>
#include <openvpn/common/size.hpp>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/strerror.hpp>
#include <openvpn/buffer/buflist.hpp>
namespace openvpn {
OPENVPN_EXCEPTION(buf_read_error);
inline bool buf_read(const int fd, Buffer& buf, const std::string& title)
{
const ssize_t status = ::read(fd, buf.data_end(), buf.remaining(0));
if (status < 0)
{
const int eno = errno;
OPENVPN_THROW(buf_read_error, "on " << title << " : " << strerror_str(eno));
}
else if (!status)
return false;
buf.inc_size(status);
return true;
}
inline BufferList buf_read(const int fd, const std::string& title)
{
BufferList buflist;
while (true)
{
BufferAllocated buf(1024, 0);
if (!buf_read(fd, buf, title))
break;
buflist.put_consume(buf);
}
return buflist;
}
}
#endif
@@ -0,0 +1,45 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2019 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
// constant-length Buffer for writing that cannot be extended
template <std::size_t N>
class StaticBuffer : public Buffer
{
public:
StaticBuffer()
: Buffer(data, N, false)
{
}
StaticBuffer(const StaticBuffer&) = delete;
StaticBuffer& operator=(const StaticBuffer&) = delete;
private:
unsigned char data[N];
};
}
+110
View File
@@ -0,0 +1,110 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// String methods on Buffer objects
#ifndef OPENVPN_BUFFER_BUFSTR_H
#define OPENVPN_BUFFER_BUFSTR_H
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
// return contents of Buffer as a std::string
inline std::string buf_to_string(const Buffer& buf)
{
return std::string((const char *)buf.c_data(), buf.size());
}
// return contents of ConstBuffer as a std::string
inline std::string buf_to_string(const ConstBuffer& buf)
{
return std::string((const char *)buf.c_data(), buf.size());
}
// write std::string to Buffer
inline void buf_write_string(Buffer& buf, const std::string& str)
{
buf.write((unsigned char *)str.c_str(), str.length());
}
// write C string to buffer
inline void buf_write_string(Buffer& buf, const char *str)
{
buf.write((unsigned char *)str, std::strlen(str));
}
// return BufferPtr from std::string
inline BufferPtr buf_from_string(const std::string& str)
{
const size_t len = str.length();
BufferPtr buf(new BufferAllocated(len, 0));
buf->write((unsigned char *)str.c_str(), len);
return buf;
}
// return BufferPtr from C string
inline BufferPtr buf_from_string(const char *str)
{
const size_t len = std::strlen(str);
BufferPtr buf(new BufferAllocated(len, 0));
buf->write((unsigned char *)str, len);
return buf;
}
// return BufferAllocated from std::string
inline BufferAllocated buf_alloc_from_string(const std::string& str)
{
const size_t len = str.length();
BufferAllocated buf(len, 0);
buf.write((unsigned char *)str.c_str(), len);
return buf;
}
// return BufferAllocated from C string
inline BufferAllocated buf_alloc_from_string(const char *str)
{
const size_t len = std::strlen(str);
BufferAllocated buf(len, 0);
buf.write((unsigned char *)str, len);
return buf;
}
// append str to buf
inline void buf_append_string(Buffer& buf, const std::string& str)
{
buf.write((unsigned char *)str.c_str(), str.length());
}
// append str to buf
inline void buf_append_string(Buffer& buf, const char *str)
{
buf.write((unsigned char *)str, std::strlen(str));
}
// Note: ConstBuffer deep links to str, so returned ConstBuffer
// is only defined while str is in scope.
inline ConstBuffer const_buf_from_string(const std::string& str)
{
return ConstBuffer((const unsigned char *)str.c_str(), str.size(), true);
}
}
#endif
@@ -0,0 +1,83 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_BUFSTREAM_H
#define OPENVPN_BUFFER_BUFSTREAM_H
#include <streambuf>
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
class BufferStream : public std::streambuf
{
public:
BufferStream(Buffer& buffer) : buf(buffer) {}
protected:
#if 0 // not implemented yet
// input
virtual std::streamsize showmanyc();
virtual std::streamsize xsgetn(char* s, std::streamsize n);
virtual int underflow();
virtual int uflow();
virtual int pbackfail(int c = EOF);
#endif
// output
virtual std::streamsize xsputn(const char* s, std::streamsize n)
{
buf.write((unsigned char *)s, (size_t)n);
return n;
}
virtual int overflow(int c = EOF)
{
if (c != EOF)
{
unsigned char uc = (unsigned char)c;
buf.push_back(uc);
}
return c;
}
private:
Buffer& buf;
};
class BufferStreamOut : public std::ostream
{
public:
BufferStreamOut(Buffer& buffer)
: std::ostream(new BufferStream(buffer))
{
}
~BufferStreamOut()
{
delete rdbuf();
}
};
}
#endif
+96
View File
@@ -0,0 +1,96 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include <cstdint> // for std::uint32_t, uint64_t, etc.
#include <lz4.h>
#include <openvpn/common/exception.hpp>
#include <openvpn/common/socktypes.hpp> // for ntohl/htonl
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
namespace LZ4 {
OPENVPN_EXCEPTION(lz4_error);
inline BufferPtr compress(const ConstBuffer& src,
const size_t headroom,
const size_t tailroom)
{
// sanity check
if (src.size() > LZ4_MAX_INPUT_SIZE)
OPENVPN_THROW(lz4_error, "compress buffer size=" << src.size() << " exceeds LZ4_MAX_INPUT_SIZE=" << LZ4_MAX_INPUT_SIZE);
// allocate dest buffer
BufferPtr dest = new BufferAllocated(sizeof(std::uint32_t) + headroom + tailroom + LZ4_COMPRESSBOUND(src.size()), 0);
dest->init_headroom(headroom);
// as a hint to receiver, write the decompressed size
{
const std::uint32_t size = htonl(src.size());
dest->write(&size, sizeof(size));
}
// compress
const int comp_size = ::LZ4_compress_default((const char *)src.c_data(), (char *)dest->data_end(),
(int)src.size(), (int)dest->remaining(tailroom));
if (comp_size <= 0)
OPENVPN_THROW(lz4_error, "LZ4_compress_default returned error status=" << comp_size);
dest->inc_size(comp_size);
return dest;
}
inline BufferPtr decompress(const ConstBuffer& source,
const size_t headroom,
const size_t tailroom,
size_t max_decompressed_size=LZ4_MAX_INPUT_SIZE)
{
// get the decompressed size
ConstBuffer src(source);
if (src.size() < sizeof(std::uint32_t))
OPENVPN_THROW(lz4_error, "decompress buffer size=" << src.size() << " is too small");
std::uint32_t size;
src.read(&size, sizeof(size));
size = ntohl(size);
if (max_decompressed_size > LZ4_MAX_INPUT_SIZE)
max_decompressed_size = LZ4_MAX_INPUT_SIZE;
if (max_decompressed_size && size > max_decompressed_size)
OPENVPN_THROW(lz4_error, "decompress expansion size=" << size << " is too large (must be <= " << max_decompressed_size << ')');
// allocate dest buffer
BufferPtr dest = new BufferAllocated(headroom + tailroom + size, 0);
dest->init_headroom(headroom);
// decompress
const int decomp_size = LZ4_decompress_safe((const char *)src.c_data(), (char *)dest->data(),
(int)src.size(), size);
if (decomp_size <= 0)
OPENVPN_THROW(lz4_error, "LZ4_decompress_safe returned error status=" << decomp_size);
if (decomp_size != size)
OPENVPN_THROW(lz4_error, "decompress size inconsistency expected_size=" << size << " actual_size=" << decomp_size);
dest->inc_size(decomp_size);
return dest;
}
}
}
+96
View File
@@ -0,0 +1,96 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
// A queue of buffers, implemented as std::deque<BufferPtr>.
#ifndef OPENVPN_BUFFER_MEMQ_H
#define OPENVPN_BUFFER_MEMQ_H
#include <deque>
#include <openvpn/common/size.hpp>
#include <openvpn/buffer/buffer.hpp>
namespace openvpn {
class MemQBase
{
public:
MemQBase() : length(0) {}
size_t size() const
{
return q.size();
}
bool empty() const
{
return q.empty();
}
size_t total_length() const { return length; }
void clear()
{
while (!q.empty())
q.pop_back();
length = 0;
}
void write_buf(const BufferPtr& bp)
{
q.push_back(bp);
length += bp->size();
}
BufferPtr read_buf()
{
BufferPtr ret = q.front();
q.pop_front();
length -= ret->size();
return ret;
}
BufferPtr& peek()
{
return q.front();
}
void pop()
{
length -= q.front()->size();
q.pop_front();
}
void resize(const size_t cap)
{
q.resize(cap);
}
protected:
typedef std::deque<BufferPtr> q_type;
size_t length;
q_type q;
};
} // namespace openvpn
#endif // OPENVPN_BUFFER_MEMQ_H
+186
View File
@@ -0,0 +1,186 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_SAFESTR_H
#define OPENVPN_BUFFER_SAFESTR_H
#include <cstring> // for std::strlen, and std::memset
#include <ostream>
#include <openvpn/common/memneq.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/buffer/bufstr.hpp>
namespace openvpn {
class SafeString
{
static constexpr size_t INITIAL_CAPACITY = 32;
static constexpr unsigned int BUF_FLAGS = BufferAllocated::DESTRUCT_ZERO|BufferAllocated::GROW;
public:
SafeString()
{
}
SafeString(const char *str, const size_t size)
: data(size+1, BUF_FLAGS)
{
data.write((unsigned char *)str, size);
trail();
}
SafeString(const char *str)
: SafeString(str, std::strlen(str))
{
}
SafeString(const std::string& str)
: SafeString(str.c_str(), str.length())
{
}
const char *c_str() const
{
if (data.defined())
return (const char *)data.c_data();
else
return "";
}
// Note: unsafe because of conversion to std::string
std::string to_string() const
{
return buf_to_string(data);
}
size_t length() const
{
return data.size();
}
bool empty() const
{
return !length();
}
char& operator[](size_t pos)
{
return *reinterpret_cast<char *>(data.index(pos));
}
const char& operator[](size_t pos) const
{
return *reinterpret_cast<const char *>(data.c_index(pos));
}
bool operator==(const char *str) const
{
const size_t len = std::strlen(str);
if (len != length())
return false;
return !crypto::memneq(str, c_str(), len);
}
bool operator!=(const char *str) const
{
return !operator==(str);
}
SafeString& operator+=(char c)
{
alloc();
data.push_back((unsigned char)c);
trail();
return *this;
}
SafeString& operator+=(const char* s)
{
return append(s);
}
SafeString& operator+=(const SafeString& str)
{
return append(str);
}
SafeString& append(const char* s)
{
alloc();
data.write((unsigned char *)s, std::strlen(s));
trail();
return *this;
}
SafeString& append(const SafeString& str)
{
alloc();
data.append(str.data);
trail();
return *this;
}
SafeString& append(const SafeString& str, size_t subpos, size_t sublen)
{
alloc();
data.append(str.data.range(subpos, sublen));
trail();
return *this;
}
void reserve(const size_t n)
{
if (data.allocated())
data.reserve(n+1);
else
data.init(n+1, BUF_FLAGS);
}
void wipe()
{
data.clear();
}
private:
void alloc()
{
if (!data.allocated())
data.init(INITIAL_CAPACITY, BUF_FLAGS);
}
void trail()
{
data.set_trailer(0);
}
BufferAllocated data;
};
template <typename Elem, typename Traits>
std::basic_ostream<Elem, Traits>& operator<<(
std::basic_ostream<Elem, Traits>& os, const SafeString& ss)
{
os << ss.c_str();
return os;
}
}
#endif
+145
View File
@@ -0,0 +1,145 @@
// OpenVPN -- An application to securely tunnel IP networks
// over a single port, with support for SSL/TLS-based
// session authentication and key exchange,
// packet encryption, packet authentication, and
// packet compression.
//
// Copyright (C) 2012-2017 OpenVPN Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License Version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program in the COPYING file.
// If not, see <http://www.gnu.org/licenses/>.
#ifndef OPENVPN_BUFFER_ZLIB_H
#define OPENVPN_BUFFER_ZLIB_H
#ifdef HAVE_ZLIB
#include <cstring> // for std::memset
#include <zlib.h>
#include <openvpn/common/exception.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/buffer/buflist.hpp>
namespace openvpn {
namespace ZLib {
OPENVPN_EXCEPTION(zlib_error);
class ZStreamBase // used internally by compress_gzip/decompress_gzip
{
public:
z_stream s;
protected:
ZStreamBase() { std::memset(&s, 0, sizeof(s)); }
private:
ZStreamBase(const ZStreamBase&) = delete;
ZStreamBase& operator=(const ZStreamBase&) = delete;
};
inline BufferPtr compress_gzip(BufferPtr src,
const size_t headroom,
const size_t tailroom,
const int level,
const int window_bits=15,
const int mem_level=8)
{
constexpr int GZIP_ENCODING = 16;
struct ZStream : public ZStreamBase {
~ZStream() { ::deflateEnd(&s); }
};
if (src)
{
int status;
ZStream zs;
zs.s.next_in = src->data();
zs.s.avail_in = src->size();
status = ::deflateInit2(&zs.s,
level,
Z_DEFLATED,
GZIP_ENCODING + window_bits,
mem_level,
Z_DEFAULT_STRATEGY);
if (status != Z_OK)
OPENVPN_THROW(zlib_error, "zlib deflateinit2 failed, error=" << status);
const uLong outcap = ::deflateBound(&zs.s, src->size());
BufferPtr b = new BufferAllocated(outcap + headroom + tailroom, 0);
b->init_headroom(headroom);
zs.s.next_out = b->data();
zs.s.avail_out = outcap;
status = ::deflate(&zs.s, Z_FINISH);
if (status != Z_STREAM_END)
OPENVPN_THROW(zlib_error, "zlib deflate failed, error=" << status);
b->set_size(zs.s.total_out);
return b;
}
else
return BufferPtr();
}
inline BufferPtr decompress_gzip(BufferPtr src,
const size_t headroom,
const size_t tailroom,
const size_t max_size,
const size_t block_size=4096,
const int window_bits=15)
{
constexpr int GZIP_ENCODING = 16;
struct ZStream : public ZStreamBase {
~ZStream() { ::inflateEnd(&s); }
};
if (src)
{
int status;
ZStream zs;
zs.s.next_in = src->data();
zs.s.avail_in = src->size();
status = ::inflateInit2(&zs.s, GZIP_ENCODING + window_bits);
if (status != Z_OK)
OPENVPN_THROW(zlib_error, "zlib inflateinit2 failed, error=" << status);
BufferList blist;
size_t hr = headroom;
size_t tr = tailroom;
do {
// use headroom/tailroom on first block to take advantage
// of BufferList::join() optimization for one-block lists
BufferPtr b = new BufferAllocated(block_size + hr + tr, 0);
b->init_headroom(hr);
const size_t avail = b->remaining(tr);
zs.s.next_out = b->data();
zs.s.avail_out = avail;
status = ::inflate(&zs.s, Z_SYNC_FLUSH);
if (status != Z_OK && status != Z_STREAM_END)
OPENVPN_THROW(zlib_error, "zlib inflate failed, error=" << status);
b->set_size(avail - zs.s.avail_out);
blist.push_back(std::move(b));
if (max_size && zs.s.total_out > max_size)
OPENVPN_THROW(zlib_error, "zlib inflate max_size " << max_size << " exceeded");
hr = tr = 0;
} while (status == Z_OK);
return blist.join(headroom, tailroom, true);
}
else
return BufferPtr();
}
}
}
#endif
#endif