OpenVPN 3 Core Library
Loading...
Searching...
No Matches
cryptodc.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Base class for OpenVPN data channel encryption/decryption
13
14#ifndef OPENVPN_CRYPTO_CRYPTODC_H
15#define OPENVPN_CRYPTO_CRYPTODC_H
16
17#include <utility> // for std::move
18#include <cstdint> // for std::uint32_t, etc.
19
23#include <openvpn/common/rc.hpp>
29
30namespace openvpn {
31
32// Base class for encryption/decryption of data channel
33class CryptoDCInstance : public RC<thread_unsafe_refcount>
34{
35 public:
37
38 // Encrypt/Decrypt
39
40 // returns true if packet ID is close to wrapping
41 virtual bool encrypt(BufferAllocated &buf, const unsigned char *op32) = 0;
42
43 virtual Error::Type decrypt(BufferAllocated &buf, std::time_t now, const unsigned char *op32) = 0;
44
45 // Initialization
46
47 // return value of defined()
48 enum
49 {
50 CIPHER_DEFINED = (1 << 0), // may call init_cipher method
51 HMAC_DEFINED = (1 << 1), // may call init_hmac method
52 CRYPTO_DEFINED = (1 << 2), // may call encrypt or decrypt methods
53 EXPLICIT_EXIT_NOTIFY_DEFINED = (1 << 3) // may call explicit_exit_notify method
54 };
55 virtual unsigned int defined() const = 0;
56
57
62 virtual void init_cipher(StaticKey &&encrypt_key,
63 StaticKey &&decrypt_key) = 0;
64
65 virtual void init_hmac(StaticKey &&encrypt_key,
66 StaticKey &&decrypt_key) = 0;
67
68 virtual void init_pid(const char *recv_name,
69 const int recv_unit,
70 const SessionStats::Ptr &recv_stats_arg) = 0;
71
72 virtual void init_remote_peer_id(const int remote_peer_id)
73 {
74 }
75
76 virtual bool consider_compression(const CompressContext &comp_ctx) = 0;
77
78 virtual void explicit_exit_notify()
79 {
80 }
81
82 // Rekeying
83
93
94 virtual void rekey(const RekeyType type) = 0;
95};
96
99{
100 public:
101 OPENVPN_SIMPLE_EXCEPTION(no_data_channel_factory);
102
104
109
114
115 void set_use_epoch_keys(bool use_epoch)
116 {
117 use_epoch_keys = use_epoch;
118 }
119
121 {
122 return cipher_;
123 }
124
136
137 bool useEpochKeys() const
138 {
139 return use_epoch_keys;
140 }
141
143 {
144 key_derivation_ = method;
145 }
146
151
152
153 private:
157 bool use_epoch_keys = false;
158};
159
160// Factory for CryptoDCInstance objects
161class CryptoDCContext : public RC<thread_unsafe_refcount>
162{
163 public:
165 : key_derivation(method)
166 {
167 }
168
170
171 virtual CryptoDCInstance::Ptr new_obj(const unsigned int key_id) = 0;
172
174
175 // Info for ProtoContext::link_mtu_adjust
176 virtual size_t encap_overhead() const = 0;
177
178 protected:
180};
181
182// Factory for CryptoDCContext objects
183class CryptoDCFactory : public RC<thread_unsafe_refcount>
184{
185 public:
187
189};
190
191
192// Manage cipher/digest settings, DC factory, and DC context.
194{
195 public:
196 OPENVPN_SIMPLE_EXCEPTION(no_data_channel_factory);
197
198 CryptoDCSettings() = default;
199
201 {
203 context_.reset();
204 dirty = false;
205 }
206
207 void set_cipher(const CryptoAlgs::Type new_cipher)
208 {
209 if (new_cipher != cipher())
210 {
212 dirty = true;
213 }
214 }
215
216 void set_digest(const CryptoAlgs::Type new_digest)
217 {
218 if (new_digest != digest())
219 {
221 dirty = true;
222 }
223 }
224
225 void set_use_epoch_keys(bool at_the_end)
226 {
227 if (at_the_end != useEpochKeys())
228 {
230 dirty = true;
231 }
232 }
233
235 {
236 if (!context_ || dirty)
237 {
238 if (!factory_)
239 throw no_data_channel_factory();
240 context_ = factory_->new_obj(*this);
241 dirty = false;
242 }
243 return *context_;
244 }
245
246 void reset()
247 {
248 factory_.reset();
249 context_.reset();
250 dirty = false;
251 }
252
253 [[nodiscard]] CryptoDCFactory::Ptr factory() const
254 {
255 return factory_;
256 }
257
258 private:
259 bool dirty = false;
262};
263} // namespace openvpn
264
265#endif
virtual CryptoDCSettingsData crypto_info()=0
virtual CryptoDCInstance::Ptr new_obj(const unsigned int key_id)=0
CryptoDCContext(const CryptoAlgs::KeyDerivation method)
Definition cryptodc.hpp:164
CryptoAlgs::KeyDerivation key_derivation
Definition cryptodc.hpp:179
RCPtr< CryptoDCContext > Ptr
Definition cryptodc.hpp:169
virtual size_t encap_overhead() const =0
RCPtr< CryptoDCFactory > Ptr
Definition cryptodc.hpp:186
virtual CryptoDCContext::Ptr new_obj(const CryptoDCSettingsData)=0
virtual void explicit_exit_notify()
Definition cryptodc.hpp:78
virtual void init_pid(const char *recv_name, const int recv_unit, const SessionStats::Ptr &recv_stats_arg)=0
virtual unsigned int defined() const =0
virtual void init_remote_peer_id(const int remote_peer_id)
Definition cryptodc.hpp:72
virtual void init_hmac(StaticKey &&encrypt_key, StaticKey &&decrypt_key)=0
virtual void rekey(const RekeyType type)=0
virtual bool encrypt(BufferAllocated &buf, const unsigned char *op32)=0
virtual Error::Type decrypt(BufferAllocated &buf, std::time_t now, const unsigned char *op32)=0
virtual bool consider_compression(const CompressContext &comp_ctx)=0
RCPtr< CryptoDCInstance > Ptr
Definition cryptodc.hpp:36
virtual void init_cipher(StaticKey &&encrypt_key, StaticKey &&decrypt_key)=0
OPENVPN_SIMPLE_EXCEPTION(no_data_channel_factory)
void set_digest(CryptoAlgs::Type digest)
Definition cryptodc.hpp:110
CryptoAlgs::Type cipher() const
Definition cryptodc.hpp:120
void set_cipher(CryptoAlgs::Type cipher)
Definition cryptodc.hpp:105
CryptoAlgs::KeyDerivation key_derivation() const
Definition cryptodc.hpp:147
CryptoAlgs::Type digest() const
Definition cryptodc.hpp:132
CryptoAlgs::KeyDerivation key_derivation_
Definition cryptodc.hpp:156
void set_key_derivation(CryptoAlgs::KeyDerivation method)
Definition cryptodc.hpp:142
void set_use_epoch_keys(bool use_epoch)
Definition cryptodc.hpp:115
CryptoDCContext & context()
Definition cryptodc.hpp:234
void set_use_epoch_keys(bool at_the_end)
Definition cryptodc.hpp:225
CryptoDCFactory::Ptr factory_
Definition cryptodc.hpp:260
OPENVPN_SIMPLE_EXCEPTION(no_data_channel_factory)
CryptoDCContext::Ptr context_
Definition cryptodc.hpp:261
void set_digest(const CryptoAlgs::Type new_digest)
Definition cryptodc.hpp:216
void set_factory(const CryptoDCFactory::Ptr &factory)
Definition cryptodc.hpp:200
CryptoDCFactory::Ptr factory() const
Definition cryptodc.hpp:253
void set_cipher(const CryptoAlgs::Type new_cipher)
Definition cryptodc.hpp:207
The smart pointer class.
Definition rc.hpp:119
void reset() noexcept
Points this RCPtr<T> to nullptr safely.
Definition rc.hpp:290
Reference count base class for objects tracked by RCPtr. Disallows copying and assignment.
Definition rc.hpp:912
bool use_cipher_digest(const Type type)