Async 1.8.0
AsyncEncryptedUdpSocket.h
Go to the documentation of this file.
1
31#ifndef ASYNC_ENCRYPTED_UDP_SOCKET_INCLUDED
32#define ASYNC_ENCRYPTED_UDP_SOCKET_INCLUDED
33
34
35/****************************************************************************
36 *
37 * System Includes
38 *
39 ****************************************************************************/
40
41#include <openssl/evp.h>
42#include <vector>
43
44
45/****************************************************************************
46 *
47 * Project Includes
48 *
49 ****************************************************************************/
50
51#include <AsyncUdpSocket.h>
52
53
54/****************************************************************************
55 *
56 * Local Includes
57 *
58 ****************************************************************************/
59
60
61
62/****************************************************************************
63 *
64 * Forward declarations
65 *
66 ****************************************************************************/
67
68
69
70/****************************************************************************
71 *
72 * Namespace
73 *
74 ****************************************************************************/
75
76namespace Async
77{
78
79
80/****************************************************************************
81 *
82 * Forward declarations of classes inside of the declared namespace
83 *
84 ****************************************************************************/
85
86
87
88/****************************************************************************
89 *
90 * Defines & typedefs
91 *
92 ****************************************************************************/
93
94
95
96/****************************************************************************
97 *
98 * Exported Global Variables
99 *
100 ****************************************************************************/
101
102
103
104/****************************************************************************
105 *
106 * Class definitions
107 *
108 ****************************************************************************/
109
122{
123 public:
124 using Cipher = EVP_CIPHER;
125
136 static const Cipher* fetchCipher(const std::string& name);
137
142 static void freeCipher(Cipher* cipher);
143
149 static const std::string cipherName(const Cipher* cipher);
150
162 static bool randomBytes(std::vector<uint8_t>& bytes);
163
169 EncryptedUdpSocket(uint16_t local_port=0,
170 const IpAddress &bind_ip=IpAddress());
171
175 //EncryptedUdpSocket(const EncryptedUdpSocket&) = delete;
176
180 //EncryptedUdpSocket& operator=(const EncryptedUdpSocket&) = delete;
181
185 ~EncryptedUdpSocket(void) override;
186
195 bool initOk(void) const override
196 {
197 return UdpSocket::initOk() && (m_cipher_ctx != nullptr);
198 }
199
209 bool setCipher(const std::string& type);
210
219 bool setCipher(const Cipher* cipher);
220
231 bool setCipherIV(std::vector<uint8_t> iv);
232
237 const std::vector<uint8_t> cipherIV(void) const;
238
249 bool setCipherKey(std::vector<uint8_t> key);
250
260 bool setCipherKey(void);
261
266 const std::vector<uint8_t> cipherKey(void) const;
267
277 void setTagLength(int taglen) { m_taglen = taglen; }
278
283 int tagLength(void) const { return m_taglen; }
284
296 void setCipherAADLength(int aadlen) { m_aadlen = aadlen; }
297
302 size_t cipherAADLength(void) const { return m_aadlen; }
303
312 bool write(const IpAddress& remote_ip, int remote_port,
313 const void *buf, int count) override;
314
324 bool write(const IpAddress& remote_ip, int remote_port,
325 const void *aad, int aadlen, const void *buf, int cnt);
326
334 sigc::signal<bool, const IpAddress&, uint16_t,
336
345 sigc::signal<void, const IpAddress&, uint16_t,
346 void*, void*, int> dataReceived;
347
348 protected:
349 void onDataReceived(const IpAddress& ip, uint16_t port, void* buf,
350 int count) override;
351
352 private:
353 EVP_CIPHER_CTX* m_cipher_ctx = nullptr;
354 std::vector<uint8_t> m_cipher_iv;
355 std::vector<uint8_t> m_cipher_key;
356 size_t m_taglen = 0;
357 size_t m_aadlen = 0;
358
359}; /* class EncryptedUdpSocket */
360
361
362} /* namespace Async */
363
364#endif /* ASYNC_ENCRYPTED_UDP_SOCKET_INCLUDED */
365
366/*
367 * This file has not been truncated
368 */
Contains a class for using UDP sockets.
A class for sending encrypted UDP datagrams.
~EncryptedUdpSocket(void) override
Disallow copy construction.
sigc::signal< void, const IpAddress &, uint16_t, void *, void *, int > dataReceived
A signal that is emitted when cipher data has been decrypted.
void setCipherAADLength(int aadlen)
Set the length of the associated data for AEAD ciphers.
bool write(const IpAddress &remote_ip, int remote_port, const void *aad, int aadlen, const void *buf, int cnt)
Write data to the remote host.
const std::vector< uint8_t > cipherIV(void) const
Get a previously set initialization vector (IV)
static const std::string cipherName(const Cipher *cipher)
Get the name of a cipher from a cipher object.
void onDataReceived(const IpAddress &ip, uint16_t port, void *buf, int count) override
bool setCipher(const Cipher *cipher)
Set which cipher algorithm type to use.
bool initOk(void) const override
Check if the initialization was ok.
bool write(const IpAddress &remote_ip, int remote_port, const void *buf, int count) override
Write data to the remote host.
bool setCipher(const std::string &type)
Set which cipher algorithm type to use.
void setTagLength(int taglen)
Set the length of the AEAD tag.
EncryptedUdpSocket(uint16_t local_port=0, const IpAddress &bind_ip=IpAddress())
Constructor.
bool setCipherKey(void)
Set a random cipher key to use.
static const Cipher * fetchCipher(const std::string &name)
Fetch a named cipher object.
bool setCipherIV(std::vector< uint8_t > iv)
Set the initialization vector to use with the cipher.
static void freeCipher(Cipher *cipher)
Free memory for a previously allocated cipher object.
int tagLength(void) const
Get the currently set up tag length.
size_t cipherAADLength(void) const
The currently set up length of the additional associated data.
bool setCipherKey(std::vector< uint8_t > key)
Set the cipher key to use.
sigc::signal< bool, const IpAddress &, uint16_t, void *, int > cipherDataReceived
A signal that is emitted when cipher data has been received.
static bool randomBytes(std::vector< uint8_t > &bytes)
Fill a vector with random bytes.
const std::vector< uint8_t > cipherKey(void) const
Get the currently set cipher key.
A class for representing an IP address in an OS independent way.
A class for working with UDP sockets.
virtual bool initOk(void) const
Check if the initialization was ok.
Namespace for the asynchronous programming classes.