-
Notifications
You must be signed in to change notification settings - Fork 408
/
Copy pathcryptodc.hpp
265 lines (212 loc) · 6.66 KB
/
cryptodc.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// 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- OpenVPN Inc.
//
// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
//
// Base class for OpenVPN data channel encryption/decryption
#ifndef OPENVPN_CRYPTO_CRYPTODC_H
#define OPENVPN_CRYPTO_CRYPTODC_H
#include <utility> // for std::move
#include <cstdint> // for std::uint32_t, etc.
#include <openvpn/common/exception.hpp>
#include <openvpn/buffer/buffer.hpp>
#include <openvpn/error/error.hpp>
#include <openvpn/common/rc.hpp>
#include <openvpn/frame/frame.hpp>
#include <openvpn/crypto/static_key.hpp>
#include <openvpn/crypto/packet_id_control.hpp>
#include <openvpn/crypto/cryptoalgs.hpp>
#include <openvpn/compress/compress.hpp>
namespace openvpn {
// Base class for encryption/decryption of data channel
class CryptoDCInstance : public RC<thread_unsafe_refcount>
{
public:
typedef RCPtr<CryptoDCInstance> Ptr;
// Encrypt/Decrypt
// returns true if packet ID is close to wrapping
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;
// Initialization
// return value of defined()
enum
{
CIPHER_DEFINED = (1 << 0), // may call init_cipher method
HMAC_DEFINED = (1 << 1), // may call init_hmac method
CRYPTO_DEFINED = (1 << 2), // may call encrypt or decrypt methods
EXPLICIT_EXIT_NOTIFY_DEFINED = (1 << 3) // may call explicit_exit_notify method
};
virtual unsigned int defined() const = 0;
/**
* Initialised the encryption/decryption cipher of the classs. Note that this is
* and init_hmac need to typically called before encrypt/decrypt can be called.
*/
virtual void init_cipher(StaticKey &&encrypt_key,
StaticKey &&decrypt_key) = 0;
virtual void init_hmac(StaticKey &&encrypt_key,
StaticKey &&decrypt_key) = 0;
virtual void init_pid(const char *recv_name,
const int recv_unit,
const SessionStats::Ptr &recv_stats_arg) = 0;
virtual void init_remote_peer_id(const int remote_peer_id)
{
}
virtual bool consider_compression(const CompressContext &comp_ctx) = 0;
virtual void explicit_exit_notify()
{
}
// Rekeying
enum RekeyType
{
ACTIVATE_PRIMARY,
ACTIVATE_PRIMARY_MOVE,
NEW_SECONDARY,
PRIMARY_SECONDARY_SWAP,
DEACTIVATE_SECONDARY,
DEACTIVATE_ALL,
};
virtual void rekey(const RekeyType type) = 0;
};
/** class that holds settings for a data channel encryption */
class CryptoDCSettingsData
{
public:
OPENVPN_SIMPLE_EXCEPTION(no_data_channel_factory);
CryptoDCSettingsData() = default;
void set_cipher(CryptoAlgs::Type cipher)
{
cipher_ = cipher;
}
void set_digest(CryptoAlgs::Type digest)
{
digest_ = digest;
}
void set_use_epoch_keys(bool use_epoch)
{
use_epoch_keys = use_epoch;
}
CryptoAlgs::Type cipher() const
{
return cipher_;
}
/**
* Retrieve the digest configured for the data channel.
* If the configured data channel cipher does not use any
* additional digest, CryptoAlgs::NONE is returned.
*
* @return Returns the cipher digest in use
*/
CryptoAlgs::Type digest() const
{
return (CryptoAlgs::use_cipher_digest(cipher_) ? digest_ : CryptoAlgs::NONE);
}
bool useEpochKeys() const
{
return use_epoch_keys;
}
void set_key_derivation(CryptoAlgs::KeyDerivation method)
{
key_derivation_ = method;
}
CryptoAlgs::KeyDerivation key_derivation() const
{
return key_derivation_;
}
private:
CryptoAlgs::Type cipher_ = CryptoAlgs::NONE;
CryptoAlgs::Type digest_ = CryptoAlgs::NONE;
CryptoAlgs::KeyDerivation key_derivation_ = CryptoAlgs::KeyDerivation::OPENVPN_PRF;
bool use_epoch_keys = false;
};
// Factory for CryptoDCInstance objects
class CryptoDCContext : public RC<thread_unsafe_refcount>
{
public:
explicit CryptoDCContext(const CryptoAlgs::KeyDerivation method)
: key_derivation(method)
{
}
typedef RCPtr<CryptoDCContext> Ptr;
virtual CryptoDCInstance::Ptr new_obj(const unsigned int key_id) = 0;
virtual CryptoDCSettingsData crypto_info() = 0;
// Info for ProtoContext::link_mtu_adjust
virtual size_t encap_overhead() const = 0;
protected:
CryptoAlgs::KeyDerivation key_derivation = CryptoAlgs::KeyDerivation::OPENVPN_PRF;
};
// Factory for CryptoDCContext objects
class CryptoDCFactory : public RC<thread_unsafe_refcount>
{
public:
typedef RCPtr<CryptoDCFactory> Ptr;
virtual CryptoDCContext::Ptr new_obj(const CryptoDCSettingsData) = 0;
};
// Manage cipher/digest settings, DC factory, and DC context.
class CryptoDCSettings : public CryptoDCSettingsData
{
public:
OPENVPN_SIMPLE_EXCEPTION(no_data_channel_factory);
CryptoDCSettings() = default;
void set_factory(const CryptoDCFactory::Ptr &factory)
{
factory_ = factory;
context_.reset();
dirty = false;
}
void set_cipher(const CryptoAlgs::Type new_cipher)
{
if (new_cipher != cipher())
{
CryptoDCSettingsData::set_cipher(new_cipher);
dirty = true;
}
}
void set_digest(const CryptoAlgs::Type new_digest)
{
if (new_digest != digest())
{
CryptoDCSettingsData::set_digest(new_digest);
dirty = true;
}
}
void set_use_epoch_keys(bool at_the_end)
{
if (at_the_end != useEpochKeys())
{
CryptoDCSettingsData::set_use_epoch_keys(at_the_end);
dirty = true;
}
}
CryptoDCContext &context()
{
if (!context_ || dirty)
{
if (!factory_)
throw no_data_channel_factory();
context_ = factory_->new_obj(*this);
dirty = false;
}
return *context_;
}
void reset()
{
factory_.reset();
context_.reset();
dirty = false;
}
[[nodiscard]] CryptoDCFactory::Ptr factory() const
{
return factory_;
}
private:
bool dirty = false;
CryptoDCFactory::Ptr factory_;
CryptoDCContext::Ptr context_;
};
} // namespace openvpn
#endif