2018-05-21 11:16:56 +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.
|
|
|
|
//
|
2022-09-29 11:41:13 +02:00
|
|
|
// Copyright (C) 2012-2022 OpenVPN Inc.
|
2018-05-21 11:16:56 +02:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
// OpenVPN 3 wrapper for kovpn crypto
|
|
|
|
|
|
|
|
#ifndef OPENVPN_KOVPN_KOREKEY_H
|
|
|
|
#define OPENVPN_KOVPN_KOREKEY_H
|
|
|
|
|
2020-09-29 14:18:43 +02:00
|
|
|
#include <openvpn/dco/kocrypto.hpp>
|
2018-05-21 11:16:56 +02:00
|
|
|
|
2024-06-25 18:45:07 +02:00
|
|
|
namespace openvpn::KoRekey {
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
class Receiver : public virtual RC<thread_unsafe_refcount>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef RCPtr<Receiver> Ptr;
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
virtual void rekey(const CryptoDCInstance::RekeyType type,
|
|
|
|
const Info &info)
|
|
|
|
= 0;
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
virtual void explicit_exit_notify()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
class Instance : public CryptoDCInstance
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Instance(const Receiver::Ptr &rcv_arg,
|
|
|
|
const CryptoDCContext::Ptr &dc_context_delegate,
|
|
|
|
const unsigned int key_id,
|
|
|
|
const Frame::Ptr &frame)
|
|
|
|
: rcv(rcv_arg),
|
|
|
|
info(dc_context_delegate, key_id, frame)
|
|
|
|
{
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
// Initialization
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
unsigned int defined() const override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
return CIPHER_DEFINED | HMAC_DEFINED | EXPLICIT_EXIT_NOTIFY_DEFINED;
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
void init_cipher(StaticKey &&encrypt_key,
|
|
|
|
StaticKey &&decrypt_key) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
info.encrypt_cipher = std::move(encrypt_key);
|
|
|
|
info.decrypt_cipher = std::move(decrypt_key);
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
void init_hmac(StaticKey &&encrypt_key,
|
|
|
|
StaticKey &&decrypt_key) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
info.encrypt_hmac = std::move(encrypt_key);
|
|
|
|
info.decrypt_hmac = std::move(decrypt_key);
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-15 16:37:18 +02:00
|
|
|
void init_pid(const char *recv_name,
|
2024-08-13 01:38:05 +02:00
|
|
|
const int recv_unit,
|
|
|
|
const SessionStats::Ptr &recv_stats_arg) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
void init_remote_peer_id(const int remote_peer_id) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
info.remote_peer_id = remote_peer_id;
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
bool consider_compression(const CompressContext &comp_ctx) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
info.comp_ctx = comp_ctx;
|
|
|
|
return false;
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
// Rekeying
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
void rekey(const RekeyType type) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
rcv->rekey(type, info);
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
void explicit_exit_notify() override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
rcv->explicit_exit_notify();
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
// Encrypt/Decrypt -- data channel handled by kernel, so these methods
|
|
|
|
// should never be reached.
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
// returns true if packet ID is close to wrapping
|
2024-08-16 11:52:42 +02:00
|
|
|
bool encrypt(BufferAllocated &buf, const unsigned char *op32) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
throw korekey_error("encrypt");
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-16 11:52:42 +02:00
|
|
|
Error::Type decrypt(BufferAllocated &buf, const std::time_t now, const unsigned char *op32) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
throw korekey_error("decrypt");
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-07-12 01:08:39 +02:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
private:
|
|
|
|
Receiver::Ptr rcv;
|
|
|
|
Info info;
|
|
|
|
};
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
class Context : public CryptoDCContext
|
|
|
|
{
|
|
|
|
public:
|
2024-07-12 01:08:39 +02:00
|
|
|
Context(CryptoDCSettingsData dc_settings_data,
|
2018-05-21 11:16:56 +02:00
|
|
|
CryptoDCFactory &dc_factory_delegate,
|
|
|
|
const Receiver::Ptr &rcv_arg,
|
|
|
|
const Frame::Ptr &frame_arg)
|
2024-07-12 01:08:39 +02:00
|
|
|
: CryptoDCContext(dc_settings_data.key_derivation()),
|
2020-08-13 15:47:42 +02:00
|
|
|
rcv(rcv_arg),
|
2024-07-12 01:08:39 +02:00
|
|
|
dc_context_delegate(dc_factory_delegate.new_obj(dc_settings_data)),
|
2018-05-21 11:16:56 +02:00
|
|
|
frame(frame_arg)
|
|
|
|
{
|
2024-07-12 01:08:39 +02:00
|
|
|
Key::validate(dc_settings_data.cipher(), dc_settings_data.digest());
|
2018-05-21 11:16:56 +02:00
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
CryptoDCInstance::Ptr new_obj(const unsigned int key_id) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
return new Instance(rcv, dc_context_delegate, key_id, frame);
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
// Info for ProtoContext::options_string
|
2024-07-12 01:08:39 +02:00
|
|
|
CryptoDCSettingsData crypto_info() override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
return dc_context_delegate->crypto_info();
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
// Info for ProtoContext::link_mtu_adjust
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-08-13 01:38:05 +02:00
|
|
|
size_t encap_overhead() const override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
|
|
|
return dc_context_delegate->encap_overhead();
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
private:
|
|
|
|
Receiver::Ptr rcv;
|
|
|
|
CryptoDCContext::Ptr dc_context_delegate;
|
|
|
|
Frame::Ptr frame;
|
|
|
|
};
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
class Factory : public CryptoDCFactory
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Factory(const CryptoDCFactory::Ptr &dc_factory_delegate_arg,
|
|
|
|
const Receiver::Ptr &rcv_arg,
|
|
|
|
const Frame::Ptr &frame_arg)
|
|
|
|
: dc_factory_delegate(dc_factory_delegate_arg),
|
|
|
|
rcv(rcv_arg),
|
|
|
|
frame(frame_arg)
|
|
|
|
{
|
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-07-12 01:08:39 +02:00
|
|
|
CryptoDCContext::Ptr new_obj(const CryptoDCSettingsData dc_settings_data) override
|
2018-05-21 11:16:56 +02:00
|
|
|
{
|
2024-07-12 01:08:39 +02:00
|
|
|
return new Context(dc_settings_data, *dc_factory_delegate, rcv, frame);
|
2018-05-21 11:16:56 +02:00
|
|
|
}
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2018-05-21 11:16:56 +02:00
|
|
|
private:
|
|
|
|
CryptoDCFactory::Ptr dc_factory_delegate;
|
|
|
|
Receiver::Ptr rcv;
|
|
|
|
Frame::Ptr frame;
|
|
|
|
};
|
2023-01-11 20:43:22 +01:00
|
|
|
|
2024-06-25 18:45:07 +02:00
|
|
|
} // namespace openvpn::KoRekey
|
2018-05-21 11:16:56 +02:00
|
|
|
|
|
|
|
#endif
|