2014-07-21 05:22:06 +02:00
|
|
|
// 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.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2016-09-04 07:29:23 +02:00
|
|
|
// Copyright (C) 2012-2016 OpenVPN Technologies, Inc.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2017-03-16 09:41:16 +01:00
|
|
|
// it under the terms of the GNU General Public License Version 3
|
2014-07-21 05:22:06 +02:00
|
|
|
// as published by the Free Software Foundation.
|
2012-08-24 23:13:42 +02:00
|
|
|
//
|
2014-07-21 05:22:06 +02:00
|
|
|
// 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
|
2017-03-16 09:41:16 +01:00
|
|
|
// GNU General Public License for more details.
|
2014-07-21 05:22:06 +02:00
|
|
|
//
|
2017-03-16 09:41:16 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
2014-07-21 05:22:06 +02:00
|
|
|
// along with this program in the COPYING file.
|
|
|
|
// If not, see <http://www.gnu.org/licenses/>.
|
2012-08-24 23:13:42 +02:00
|
|
|
|
2014-10-20 20:29:30 +02:00
|
|
|
// OpenVPN HMAC classes
|
2012-11-23 07:18:43 +01:00
|
|
|
|
2014-10-20 17:16:39 +02:00
|
|
|
#ifndef OPENVPN_CRYPTO_OVPNHMAC_H
|
|
|
|
#define OPENVPN_CRYPTO_OVPNHMAC_H
|
2012-03-12 13:24:40 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2015-05-17 21:27:34 +02:00
|
|
|
#include <openvpn/common/size.hpp>
|
2012-03-12 13:24:40 +01:00
|
|
|
#include <openvpn/common/exception.hpp>
|
2014-10-20 17:16:39 +02:00
|
|
|
#include <openvpn/common/rc.hpp>
|
2014-08-18 06:09:12 +02:00
|
|
|
#include <openvpn/common/memneq.hpp>
|
2012-03-12 13:24:40 +01:00
|
|
|
#include <openvpn/crypto/static_key.hpp>
|
2014-10-20 20:29:30 +02:00
|
|
|
#include <openvpn/crypto/cryptoalgs.hpp>
|
2012-03-12 13:24:40 +01:00
|
|
|
|
|
|
|
namespace openvpn {
|
2014-10-20 20:29:30 +02:00
|
|
|
|
|
|
|
// OpenVPN protocol HMAC usage for HMAC/CBC integrity checking and tls-auth
|
|
|
|
|
2012-03-12 13:24:40 +01:00
|
|
|
template <typename CRYPTO_API>
|
2014-10-20 17:16:39 +02:00
|
|
|
class OvpnHMAC
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
|
|
|
public:
|
2014-10-20 17:16:39 +02:00
|
|
|
OPENVPN_SIMPLE_EXCEPTION(ovpn_hmac_context_digest_size);
|
|
|
|
OPENVPN_SIMPLE_EXCEPTION(ovpn_hmac_context_bad_sizing);
|
2012-03-12 13:24:40 +01:00
|
|
|
|
|
|
|
public:
|
2014-10-20 17:16:39 +02:00
|
|
|
OvpnHMAC() {}
|
2012-03-12 13:24:40 +01:00
|
|
|
|
2014-10-24 03:19:08 +02:00
|
|
|
OvpnHMAC(const CryptoAlgs::Type digest, const StaticKey& key)
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
|
|
|
init(digest, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool defined() const { return ctx.is_initialized(); }
|
|
|
|
|
|
|
|
// size of out buffer to pass to hmac
|
|
|
|
size_t output_size() const
|
|
|
|
{
|
|
|
|
return ctx.size();
|
|
|
|
}
|
|
|
|
|
2014-10-24 03:19:08 +02:00
|
|
|
void init(const CryptoAlgs::Type digest, const StaticKey& key)
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
2014-10-24 03:19:08 +02:00
|
|
|
const CryptoAlgs::Alg& alg = CryptoAlgs::get(digest);
|
|
|
|
|
2012-03-12 13:24:40 +01:00
|
|
|
// check that key is large enough
|
2014-10-24 03:19:08 +02:00
|
|
|
if (key.size() < alg.size())
|
2014-10-20 17:16:39 +02:00
|
|
|
throw ovpn_hmac_context_digest_size();
|
2012-03-12 13:24:40 +01:00
|
|
|
|
|
|
|
// initialize HMAC context with digest type and key
|
2014-10-24 03:19:08 +02:00
|
|
|
ctx.init(digest, key.data(), alg.size());
|
2012-03-12 13:24:40 +01:00
|
|
|
}
|
|
|
|
|
2012-03-13 05:51:52 +01:00
|
|
|
void hmac(unsigned char *out, const size_t out_size,
|
|
|
|
const unsigned char *in, const size_t in_size)
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
|
|
|
ctx.reset();
|
|
|
|
ctx.update(in, in_size);
|
2012-03-13 05:51:52 +01:00
|
|
|
ctx.final(out);
|
2012-03-12 13:24:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Special HMAC for OpenVPN control packets
|
|
|
|
|
2014-10-20 17:16:39 +02:00
|
|
|
void ovpn_hmac_gen(unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3)
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
2014-10-20 17:16:39 +02:00
|
|
|
if (ovpn_hmac_pre(data, data_size, l1, l2, l3))
|
2012-03-12 13:24:40 +01:00
|
|
|
ctx.final(data + l1);
|
|
|
|
else
|
2014-10-20 17:16:39 +02:00
|
|
|
throw ovpn_hmac_context_bad_sizing();
|
2012-03-12 13:24:40 +01:00
|
|
|
}
|
|
|
|
|
2014-10-20 17:16:39 +02:00
|
|
|
// verify the HMAC generated by ovpn_hmac_gen, return true if verified
|
|
|
|
bool ovpn_hmac_cmp(const unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3)
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
|
|
|
unsigned char local_hmac[CRYPTO_API::HMACContext::MAX_HMAC_SIZE];
|
2014-10-20 17:16:39 +02:00
|
|
|
if (ovpn_hmac_pre(data, data_size, l1, l2, l3))
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
2012-03-13 05:51:52 +01:00
|
|
|
ctx.final(local_hmac);
|
2014-08-18 06:09:12 +02:00
|
|
|
return !crypto::memneq(data + l1, local_hmac, l2);
|
2012-03-12 13:24:40 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Convoluting OpenVPN control channel packets for HMAC:
|
|
|
|
// <-- L1 --> <-L2> <L3>
|
|
|
|
// [OP] [PSID] [HMAC] [PID] [...] -> canonical order
|
|
|
|
//
|
|
|
|
// [HMAC] [PID] [OP] [PSID] [...] -> HMAC order
|
|
|
|
|
2014-10-20 17:16:39 +02:00
|
|
|
bool ovpn_hmac_pre(const unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3)
|
2012-03-12 13:24:40 +01:00
|
|
|
{
|
|
|
|
const size_t lsum = l1 + l2 + l3;
|
|
|
|
if (lsum > data_size || l2 != ctx.size())
|
|
|
|
return false;
|
|
|
|
ctx.reset();
|
|
|
|
ctx.update(data + l1 + l2, l3);
|
|
|
|
ctx.update(data, l1);
|
|
|
|
ctx.update(data + lsum, data_size - lsum);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename CRYPTO_API::HMACContext ctx;
|
|
|
|
};
|
2014-10-20 20:29:30 +02:00
|
|
|
|
|
|
|
// OvpnHMAC wrapper API using dynamic polymorphism
|
|
|
|
|
|
|
|
class OvpnHMACInstance : public RC<thread_unsafe_refcount>
|
|
|
|
{
|
|
|
|
public:
|
2015-05-18 05:26:53 +02:00
|
|
|
typedef RCPtr<OvpnHMACInstance> Ptr;
|
2014-10-20 20:29:30 +02:00
|
|
|
|
|
|
|
virtual void init(const StaticKey& key) = 0;
|
|
|
|
|
|
|
|
virtual size_t output_size() const = 0;
|
|
|
|
|
|
|
|
virtual void ovpn_hmac_gen(unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3) = 0;
|
|
|
|
|
|
|
|
virtual bool ovpn_hmac_cmp(const unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class OvpnHMACContext : public RC<thread_unsafe_refcount>
|
|
|
|
{
|
|
|
|
public:
|
2015-05-18 05:26:53 +02:00
|
|
|
typedef RCPtr<OvpnHMACContext> Ptr;
|
2014-10-20 20:29:30 +02:00
|
|
|
|
|
|
|
virtual size_t size() const = 0;
|
|
|
|
|
|
|
|
virtual OvpnHMACInstance::Ptr new_obj() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class OvpnHMACFactory : public RC<thread_unsafe_refcount>
|
|
|
|
{
|
|
|
|
public:
|
2015-05-18 05:26:53 +02:00
|
|
|
typedef RCPtr<OvpnHMACFactory> Ptr;
|
2014-10-20 20:29:30 +02:00
|
|
|
|
|
|
|
virtual OvpnHMACContext::Ptr new_obj(const CryptoAlgs::Type digest_type) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// OvpnHMAC wrapper implementation using dynamic polymorphism
|
|
|
|
|
|
|
|
template <typename CRYPTO_API>
|
|
|
|
class CryptoOvpnHMACInstance : public OvpnHMACInstance
|
|
|
|
{
|
|
|
|
public:
|
2014-10-24 03:19:08 +02:00
|
|
|
CryptoOvpnHMACInstance(const CryptoAlgs::Type digest_arg)
|
2014-10-20 20:29:30 +02:00
|
|
|
: digest(digest_arg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void init(const StaticKey& key)
|
|
|
|
{
|
|
|
|
ovpn_hmac.init(digest, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t output_size() const
|
|
|
|
{
|
|
|
|
return ovpn_hmac.output_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ovpn_hmac_gen(unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3)
|
|
|
|
{
|
|
|
|
ovpn_hmac.ovpn_hmac_gen(data, data_size, l1, l2, l3);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool ovpn_hmac_cmp(const unsigned char *data, const size_t data_size,
|
|
|
|
const size_t l1, const size_t l2, const size_t l3)
|
|
|
|
{
|
|
|
|
return ovpn_hmac.ovpn_hmac_cmp(data, data_size, l1, l2, l3);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-24 03:19:08 +02:00
|
|
|
typename CryptoAlgs::Type digest;
|
2014-10-20 20:29:30 +02:00
|
|
|
OvpnHMAC<CRYPTO_API> ovpn_hmac;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename CRYPTO_API>
|
|
|
|
class CryptoOvpnHMACContext : public OvpnHMACContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CryptoOvpnHMACContext(const CryptoAlgs::Type digest_type)
|
|
|
|
: digest(CryptoAlgs::legal_dc_digest(digest_type))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t size() const
|
|
|
|
{
|
2014-10-24 03:19:08 +02:00
|
|
|
return CryptoAlgs::size(digest);
|
2014-10-20 20:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual OvpnHMACInstance::Ptr new_obj()
|
|
|
|
{
|
|
|
|
return new CryptoOvpnHMACInstance<CRYPTO_API>(digest);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-10-24 03:19:08 +02:00
|
|
|
CryptoAlgs::Type digest;
|
2014-10-20 20:29:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename CRYPTO_API>
|
|
|
|
class CryptoOvpnHMACFactory : public OvpnHMACFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual OvpnHMACContext::Ptr new_obj(const CryptoAlgs::Type digest_type)
|
|
|
|
{
|
|
|
|
return new CryptoOvpnHMACContext<CRYPTO_API>(digest_type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-12 13:24:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|