mirror of
https://github.com/deneraraujo/OpenVPNAdapter.git
synced 2026-04-24 00:00:05 +08:00
Merge commit '86cc97e55fe346502462284d2e636a2b3708163e' as 'Sources/OpenVPN3'
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
// 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_RANDOM_DEVURAND_H
|
||||
#define OPENVPN_RANDOM_DEVURAND_H
|
||||
|
||||
#include <sys/types.h> // for open()
|
||||
#include <sys/stat.h> // for open()
|
||||
#include <fcntl.h> // for open()
|
||||
|
||||
#include <unistd.h> // for read()
|
||||
|
||||
#include <openvpn/common/scoped_fd.hpp>
|
||||
#include <openvpn/random/randapi.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
class DevURand : public RandomAPI
|
||||
{
|
||||
public:
|
||||
OPENVPN_EXCEPTION(dev_urand_error);
|
||||
|
||||
typedef RCPtr<DevURand> Ptr;
|
||||
|
||||
DevURand()
|
||||
: dev_urandom(::open("/dev/urandom", O_RDONLY))
|
||||
{
|
||||
if (!dev_urandom.defined())
|
||||
throw dev_urand_error("init failed");
|
||||
}
|
||||
|
||||
// Random algorithm name
|
||||
virtual std::string name() const
|
||||
{
|
||||
return "DevURand";
|
||||
}
|
||||
|
||||
// Return true if algorithm is crypto-strength
|
||||
virtual bool is_crypto() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Fill buffer with random bytes
|
||||
virtual void rand_bytes(unsigned char *buf, size_t size)
|
||||
{
|
||||
if (!rndbytes(buf, size))
|
||||
throw dev_urand_error("rand_bytes failed");
|
||||
}
|
||||
|
||||
// Like rand_bytes, but don't throw exception.
|
||||
// Return true on successs, false on fail.
|
||||
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size)
|
||||
{
|
||||
return rndbytes(buf, size);
|
||||
}
|
||||
|
||||
private:
|
||||
bool rndbytes(unsigned char *buf, size_t size)
|
||||
{
|
||||
const ssize_t actual = ::read(dev_urandom(), buf, size);
|
||||
return size == actual;
|
||||
}
|
||||
|
||||
ScopedFD dev_urandom;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
// 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/>.
|
||||
|
||||
// Non-cryptographic random number generator
|
||||
|
||||
#ifndef OPENVPN_RANDOM_MTRANDAPI_H
|
||||
#define OPENVPN_RANDOM_MTRANDAPI_H
|
||||
|
||||
#include <random>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/random/randapi.hpp>
|
||||
#include <openvpn/random/randbytestore.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
class MTRand : public RandomAPI
|
||||
{
|
||||
public:
|
||||
OPENVPN_EXCEPTION(mtrand_error);
|
||||
|
||||
typedef RCPtr<MTRand> Ptr;
|
||||
typedef std::mt19937_64 rand_type;
|
||||
|
||||
MTRand(RandomAPI& seed)
|
||||
: rng(gen_seed(seed))
|
||||
{
|
||||
}
|
||||
|
||||
MTRand()
|
||||
: rng(gen_seed())
|
||||
{
|
||||
}
|
||||
|
||||
MTRand(const rand_type::result_type seed)
|
||||
: rng(seed)
|
||||
{
|
||||
}
|
||||
|
||||
// Random algorithm name
|
||||
virtual std::string name() const
|
||||
{
|
||||
return "MTRand";
|
||||
}
|
||||
|
||||
// Return true if algorithm is crypto-strength
|
||||
virtual bool is_crypto() const
|
||||
{
|
||||
#ifdef OPENVPN_INSECURE_RANDOM // DO NOT enable in production!
|
||||
return true;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Fill buffer with random bytes
|
||||
virtual void rand_bytes(unsigned char *buf, size_t size)
|
||||
{
|
||||
if (!rndbytes(buf, size))
|
||||
throw mtrand_error("rand_bytes failed");
|
||||
}
|
||||
|
||||
// Like rand_bytes, but don't throw exception.
|
||||
// Return true on successs, false on fail.
|
||||
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size)
|
||||
{
|
||||
return rndbytes(buf, size);
|
||||
}
|
||||
|
||||
rand_type::result_type rand()
|
||||
{
|
||||
return rng();
|
||||
}
|
||||
|
||||
private:
|
||||
bool rndbytes(unsigned char *buf, size_t size)
|
||||
{
|
||||
while (size--)
|
||||
*buf++ = rbs.get_byte(rng);
|
||||
return true;
|
||||
}
|
||||
|
||||
static rand_type::result_type gen_seed(RandomAPI& seed)
|
||||
{
|
||||
return seed.rand_get<rand_type::result_type>();
|
||||
}
|
||||
|
||||
static rand_type::result_type gen_seed()
|
||||
{
|
||||
std::random_device rd;
|
||||
RandomByteStore<decltype(rd)> rbs;
|
||||
rand_type::result_type ret;
|
||||
rbs.fill(ret, rd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
rand_type rng;
|
||||
RandomByteStore<rand_type> rbs;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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 <utility>
|
||||
|
||||
#include <openvpn/random/randapi.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
// By convention, rng is crypto-strength while prng is
|
||||
// not. Be sure to always call RandomAPI::assert_crypto()
|
||||
// before using an rng for crypto purposes, to verify that
|
||||
// it is crypto-capable.
|
||||
struct Rand2
|
||||
{
|
||||
Rand2() {}
|
||||
|
||||
Rand2(RandomAPI::Ptr rng_arg,
|
||||
RandomAPI::Ptr prng_arg)
|
||||
: rng(std::move(rng_arg)),
|
||||
prng(std::move(prng_arg))
|
||||
{
|
||||
}
|
||||
|
||||
Rand2(RandomAPI::Ptr rng_arg)
|
||||
: rng(std::move(rng_arg)),
|
||||
prng(std::move(rng_arg))
|
||||
{
|
||||
}
|
||||
|
||||
RandomAPI::Ptr rng;
|
||||
RandomAPI::Ptr prng;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
// 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/>.
|
||||
|
||||
// API for random number implementations.
|
||||
|
||||
#ifndef OPENVPN_MBEDTLS_UTIL_RANDAPI_H
|
||||
#define OPENVPN_MBEDTLS_UTIL_RANDAPI_H
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
#include <openvpn/common/rc.hpp>
|
||||
#include <openvpn/common/exception.hpp>
|
||||
#include <openvpn/random/randistrib.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
class RandomAPI : public RC<thread_unsafe_refcount>
|
||||
{
|
||||
public:
|
||||
typedef RCPtr<RandomAPI> Ptr;
|
||||
|
||||
// Random algorithm name
|
||||
virtual std::string name() const = 0;
|
||||
|
||||
// Return true if algorithm is crypto-strength
|
||||
virtual bool is_crypto() const = 0;
|
||||
|
||||
// Fill buffer with random bytes
|
||||
virtual void rand_bytes(unsigned char *buf, size_t size) = 0;
|
||||
|
||||
// Like rand_bytes, but don't throw exception.
|
||||
// Return true on successs, false on fail.
|
||||
virtual bool rand_bytes_noexcept(unsigned char *buf, size_t size) = 0;
|
||||
|
||||
// Fill a data object with random bits
|
||||
template <typename T>
|
||||
void rand_fill(T& obj)
|
||||
{
|
||||
rand_bytes(reinterpret_cast<unsigned char *>(&obj), sizeof(T));
|
||||
}
|
||||
|
||||
// Return a data object with random bits
|
||||
template <typename T>
|
||||
T rand_get()
|
||||
{
|
||||
T ret;
|
||||
rand_fill(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Return a data object with random bits, always >= 0 for signed types
|
||||
template <typename T>
|
||||
T rand_get_positive()
|
||||
{
|
||||
T ret = rand_get<T>();
|
||||
if (ret < 0)
|
||||
ret = -ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [0, end).
|
||||
// end must be > 0.
|
||||
template <typename T>
|
||||
T randrange(const T end)
|
||||
{
|
||||
return rand_get_positive<T>() % end;
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [start, end].
|
||||
template <typename T>
|
||||
T randrange(const T start, const T end)
|
||||
{
|
||||
if (start >= end)
|
||||
return start;
|
||||
else
|
||||
return start + rand_get_positive<T>() % (end - start + 1);
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [0, end).
|
||||
// This version is strictly 32-bit only and optimizes by avoiding
|
||||
// integer division.
|
||||
std::uint32_t randrange32(const std::uint32_t end)
|
||||
{
|
||||
std::uint32_t r;
|
||||
rand_fill(r);
|
||||
return rand32_distribute(r, end);
|
||||
}
|
||||
|
||||
// Return a uniformly distributed random number in the range [start, end].
|
||||
// This version is strictly 32-bit only and optimizes by avoiding
|
||||
// integer division.
|
||||
std::uint32_t randrange32(const std::uint32_t start, const std::uint32_t end)
|
||||
{
|
||||
if (start >= end)
|
||||
return start;
|
||||
else
|
||||
return start + randrange32(end - start + 1);
|
||||
}
|
||||
|
||||
// Return a random byte
|
||||
std::uint8_t randbyte()
|
||||
{
|
||||
std::uint8_t byte;
|
||||
rand_fill(byte);
|
||||
return byte;
|
||||
}
|
||||
|
||||
// Return a random boolean
|
||||
bool randbool()
|
||||
{
|
||||
return bool(randbyte() & 1);
|
||||
}
|
||||
|
||||
// Throw an exception if algorithm is not crypto-strength.
|
||||
// Be sure to always call this method before using an rng
|
||||
// for crypto purposes.
|
||||
void assert_crypto() const
|
||||
{
|
||||
if (!is_crypto())
|
||||
throw Exception("RandomAPI: " + name() + " algorithm is not crypto-strength");
|
||||
}
|
||||
|
||||
// UniformRandomBitGenerator for std::shuffle
|
||||
typedef unsigned int result_type;
|
||||
static constexpr result_type min() { return result_type(0); }
|
||||
static constexpr result_type max() { return ~result_type(0); }
|
||||
result_type operator()() { return rand_get<result_type>(); }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#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_RANDOM_RANDBYTESTORE_H
|
||||
#define OPENVPN_RANDOM_RANDBYTESTORE_H
|
||||
|
||||
#include <openvpn/common/size.hpp>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
template <typename RAND_TYPE>
|
||||
class RandomByteStore
|
||||
{
|
||||
public:
|
||||
static constexpr size_t SIZE = sizeof(typename RAND_TYPE::result_type);
|
||||
|
||||
unsigned char get_byte(RAND_TYPE& rng)
|
||||
{
|
||||
if (n_bytes == 0)
|
||||
{
|
||||
res.rt = rng();
|
||||
n_bytes = SIZE;
|
||||
}
|
||||
unsigned char ret = res.bytes[0];
|
||||
res.rt >>= 8;
|
||||
--n_bytes;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void fill(T& obj, RAND_TYPE& rng)
|
||||
{
|
||||
unsigned char *data = reinterpret_cast<unsigned char *>(&obj);
|
||||
for (size_t i = 0; i < sizeof(obj); ++i)
|
||||
data[i] = get_byte(rng);
|
||||
}
|
||||
|
||||
private:
|
||||
union Result {
|
||||
unsigned char bytes[SIZE];
|
||||
typename RAND_TYPE::result_type rt;
|
||||
};
|
||||
|
||||
Result res;
|
||||
unsigned int n_bytes = 0;
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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 <cstdint>
|
||||
|
||||
namespace openvpn {
|
||||
|
||||
// Return a uniformly distributed random number in the range [0, end)
|
||||
// using seed as a random seed. This version is strictly 32-bit only
|
||||
// and optimizes by avoiding integer division.
|
||||
inline std::uint32_t rand32_distribute(const std::uint32_t seed,
|
||||
const std::uint32_t end)
|
||||
{
|
||||
return (std::uint64_t(seed) * end) >> 32;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user