@@ -13,17 +13,50 @@ using namespace TUI::Network;
1313using namespace TUI ::Application;
1414using namespace TUI ::Application::SecureSession;
1515
16+ namespace
17+ {
18+
19+ class EncryptionCodec : public IMessageCodec
20+ {
21+ public:
22+ EncryptionCodec (
23+ Cipher::ChaCha20Poly1305::Encryptor encryptor,
24+ Cipher::ChaCha20Poly1305::Decryptor decryptor)
25+ : _encryptor(std::move(encryptor)), _decryptor(std::move(decryptor))
26+ {
27+ }
28+ ~EncryptionCodec () override = default ;
29+ EncryptionCodec (const EncryptionCodec&) = delete ;
30+ EncryptionCodec& operator =(const EncryptionCodec&) = delete ;
31+ EncryptionCodec (EncryptionCodec&&) = delete ;
32+ EncryptionCodec& operator =(EncryptionCodec&&) = delete ;
33+
34+ std::vector<std::uint8_t > Encode (const std::vector<std::uint8_t >& data) override
35+ {
36+ return _encryptor.Encrypt (data);
37+ }
38+
39+ std::vector<std::uint8_t > Decode (const std::vector<std::uint8_t >& data) override
40+ {
41+ return _decryptor.Decrypt (data);
42+ }
43+
44+ private:
45+ Cipher::ChaCha20Poly1305::Encryptor _encryptor;
46+ Cipher::ChaCha20Poly1305::Decryptor _decryptor;
47+ };
48+
49+ }
50+
1651/* * Session */
1752
1853Connection::Connection (
1954 std::shared_ptr<IConnection<void >> connection,
2055 const CallerId& callerId,
21- Cipher::ChaCha20Poly1305::Encryptor encryptor,
22- Cipher::ChaCha20Poly1305::Decryptor decryptor,
23- bool turnOffEncryption,
24- std::function<void (CallerId)> onClose)
25- : _connection(std::move(connection)), _callerId(callerId), _encryptor(std::move(encryptor)),
26- _decryptor(std::move(decryptor)), _turnOffEncryption(turnOffEncryption), _onClose(std::move(onClose))
56+ std::function<void (CallerId)> onClose,
57+ const std::vector<std::shared_ptr<IMessageCodec>>& messageCodecs)
58+ : _connection(std::move(connection)), _callerId(callerId), _onClose(std::move(onClose)),
59+ _messageCodecs(messageCodecs)
2760{
2861 if (_connection == nullptr )
2962 {
@@ -62,9 +95,9 @@ void Connection::Send(std::vector<std::uint8_t> message)
6295 {
6396 throw std::runtime_error (" Connection is closed" );
6497 }
65- if (!_turnOffEncryption )
98+ for ( const auto & codec : _messageCodecs )
6699 {
67- message = _encryptor. Encrypt (message);
100+ message = codec-> Encode (message);
68101 }
69102 _connection->Send (std::move (message));
70103}
@@ -82,9 +115,9 @@ JS::Promise<std::optional<std::vector<std::uint8_t>>> Connection::ReceiveAsync()
82115 co_return std::nullopt ;
83116 }
84117 auto data = std::move (dataOpt.value ());
85- if (!_turnOffEncryption )
118+ for ( auto it = _messageCodecs. rbegin (); it != _messageCodecs. rend (); ++it )
86119 {
87- data = _decryptor. Decrypt (data);
120+ data = (*it)-> Decode (data);
88121 }
89122 co_return std::move (data);
90123}
@@ -99,16 +132,18 @@ CallerId Connection::GetId() const
99132std::shared_ptr<IServer<CallerId>> Server::Create (
100133 Tev& tev,
101134 std::shared_ptr<IServer<void >> server,
102- GetUserCredentialFunc getUserCredential)
135+ GetUserCredentialFunc getUserCredential,
136+ const std::vector<std::shared_ptr<IMessageCodec>>& messageCodecs)
103137{
104- return std::shared_ptr<Server>(new Server (tev, server, getUserCredential));
138+ return std::shared_ptr<Server>(new Server (tev, server, getUserCredential, messageCodecs ));
105139}
106140
107141Server::Server (
108142 Tev& tev,
109143 std::shared_ptr<IServer<void >> server,
110- GetUserCredentialFunc getUserCredential)
111- : _tev(tev), _server(server), _getUserCredential(getUserCredential)
144+ GetUserCredentialFunc getUserCredential,
145+ const std::vector<std::shared_ptr<IMessageCodec>>& messageCodecs)
146+ : _tev(tev), _server(server), _getUserCredential(getUserCredential), _messageCodecs(messageCodecs)
112147{
113148 if (server == nullptr || getUserCredential == nullptr )
114149 {
@@ -346,12 +381,14 @@ JS::Promise<void> Server::HandleHandshakeAsync(std::shared_ptr<IConnection<void>
346381 connection->Send (std::move (negotiationResponseCipher));
347382 /* * Create the secure connection */
348383 std::weak_ptr<Server> weakThis = shared_from_this ();
349- auto secureConnection = std::make_shared<Connection>(
350- connection,
384+ std::vector<std::shared_ptr<IMessageCodec>> codecs = _messageCodecs;
385+ if (!negotiationRequest.get_turn_off_encryption ())
386+ {
387+ codecs.push_back (std::make_shared<EncryptionCodec>(std::move (encryptor), std::move (decryptor)));
388+ }
389+ auto secureConnection = std::shared_ptr<Connection>(new Connection (
390+ std::move (connection),
351391 callerId,
352- encryptor,
353- decryptor,
354- negotiationRequest.get_turn_off_encryption (),
355392 [weakThis, resumptionKeyIndex](CallerId id) {
356393 auto self = weakThis.lock ();
357394 if (!self)
@@ -379,7 +416,8 @@ JS::Promise<void> Server::HandleHandshakeAsync(std::shared_ptr<IConnection<void>
379416 self->_resumptionKeyTimeouts .emplace (
380417 resumptionKeyIndexStr,
381418 std::move (resumptionKeyTimeout));
382- });
419+ },
420+ std::move (codecs)));
383421 _connections.emplace (callerId, secureConnection);
384422 /* * Add the connection to the generator */
385423 _connectionGenerator.Feed (secureConnection);
0 commit comments