OpenTTD Source: src/network/network_crypto.cpp Source File

OpenTTD Source 15.3

network_crypto.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11
12#include "network_crypto_internal.h"
13#include "core/packet.h"
14
15#include "../3rdparty/monocypher/monocypher.h"
16#include "../core/random_func.hpp"
17#include "../debug.h"
18#include "../string_func.h"
19
20#include "../safeguards.h"
21
26 static void crypto_wipe(std::span<uint8_t> span)
27{
28 crypto_wipe(span.data(), span.size());
29}
30
32X25519DerivedKeys::~X25519DerivedKeys()
33{
34 crypto_wipe(keys);
35}
36
41std::span<const uint8_t> X25519DerivedKeys::ClientToServer() const
42{
43 return std::span(this->keys.data(), X25519_KEY_SIZE);
44}
45
50std::span<const uint8_t> X25519DerivedKeys::ServerToClient() const
51{
52 return std::span(this->keys.data() + X25519_KEY_SIZE, X25519_KEY_SIZE);
53}
54
64 bool X25519DerivedKeys::Exchange(const X25519PublicKey &peer_public_key, X25519KeyExchangeSide side,
65 const X25519SecretKey &our_secret_key, const X25519PublicKey &our_public_key, std::string_view extra_payload)
66{
67 X25519Key shared_secret;
68 crypto_x25519(shared_secret.data(), our_secret_key.data(), peer_public_key.data());
69 if (std::all_of(shared_secret.begin(), shared_secret.end(), [](auto v) { return v == 0; })) {
70 /* A shared secret of all zeros means that the peer tried to force the shared secret to a known constant. */
71 return false;
72 }
73
74 crypto_blake2b_ctx ctx;
75 crypto_blake2b_init(&ctx, this->keys.size());
76 crypto_blake2b_update(&ctx, shared_secret.data(), shared_secret.size());
77 switch (side) {
78 case X25519KeyExchangeSide::SERVER:
79 crypto_blake2b_update(&ctx, our_public_key.data(), our_public_key.size());
80 crypto_blake2b_update(&ctx, peer_public_key.data(), peer_public_key.size());
81 break;
82 case X25519KeyExchangeSide::CLIENT:
83 crypto_blake2b_update(&ctx, peer_public_key.data(), peer_public_key.size());
84 crypto_blake2b_update(&ctx, our_public_key.data(), our_public_key.size());
85 break;
86 default:
87 NOT_REACHED();
88 }
89 crypto_blake2b_update(&ctx, reinterpret_cast<const uint8_t *>(extra_payload.data()), extra_payload.size());
90 crypto_blake2b_final(&ctx, this->keys.data());
91 return true;
92}
93
97 class X25519EncryptionHandler : public NetworkEncryptionHandler {
98private:
99 crypto_aead_ctx context;
100
101public:
107 X25519EncryptionHandler(const std::span<const uint8_t> key, const X25519Nonce &nonce)
108 {
109 assert(key.size() == X25519_KEY_SIZE);
110 crypto_aead_init_x(&this->context, key.data(), nonce.data());
111 }
112
114 ~X25519EncryptionHandler()
115 {
116 crypto_wipe(&this->context, sizeof(this->context));
117 }
118
119 size_t MACSize() const override
120 {
121 return X25519_MAC_SIZE;
122 }
123
124 bool Decrypt(std::span<std::uint8_t> mac, std::span<std::uint8_t> message) override
125 {
126 return crypto_aead_read(&this->context, message.data(), mac.data(), nullptr, 0, message.data(), message.size()) == 0;
127 }
128
129 void Encrypt(std::span<std::uint8_t> mac, std::span<std::uint8_t> message) override
130 {
131 crypto_aead_write(&this->context, message.data(), mac.data(), nullptr, 0, message.data(), message.size());
132 }
133};
134
136X25519Key::~X25519Key()
137{
138 crypto_wipe(*this);
139}
140
145/* static */ X25519SecretKey X25519SecretKey::CreateRandom()
146{
147 X25519SecretKey secret_key;
148 RandomBytesWithFallback(secret_key);
149 return secret_key;
150}
151
156 X25519PublicKey X25519SecretKey::CreatePublicKey() const
157{
158 X25519PublicKey public_key;
159 crypto_x25519_public_key(public_key.data(), this->data());
160 return public_key;
161}
162
167/* static */ X25519Nonce X25519Nonce::CreateRandom()
168{
169 X25519Nonce nonce;
170 RandomBytesWithFallback(nonce);
171 return nonce;
172}
173
175X25519Nonce::~X25519Nonce()
176{
177 crypto_wipe(*this);
178}
179
184X25519AuthenticationHandler::X25519AuthenticationHandler(const X25519SecretKey &secret_key) :
185 our_secret_key(secret_key), our_public_key(secret_key.CreatePublicKey()),
186 key_exchange_nonce(X25519Nonce::CreateRandom()), encryption_nonce(X25519Nonce::CreateRandom())
187{
188}
189
190/* virtual */ void X25519AuthenticationHandler::SendRequest(Packet &p)
191{
192 p.Send_bytes(this->our_public_key);
193 p.Send_bytes(this->key_exchange_nonce);
194}
195
201 bool X25519AuthenticationHandler::ReceiveRequest(Packet &p)
202{
203 if (p.RemainingBytesToTransfer() != X25519_KEY_SIZE + X25519_NONCE_SIZE) {
204 Debug(net, 1, "[crypto] Received auth response of illegal size; authentication aborted.");
205 return false;
206 }
207
208 p.Recv_bytes(this->peer_public_key);
209 p.Recv_bytes(this->key_exchange_nonce);
210 return true;
211}
212
219 bool X25519AuthenticationHandler::SendResponse(Packet &p, std::string_view derived_key_extra_payload)
220{
221 if (!this->derived_keys.Exchange(this->peer_public_key, X25519KeyExchangeSide::CLIENT,
222 this->our_secret_key, this->our_public_key, derived_key_extra_payload)) {
223 Debug(net, 0, "[crypto] Peer sent an illegal public key; authentication aborted.");
224 return false;
225 }
226
227 X25519KeyExchangeMessage message;
228 RandomBytesWithFallback(message);
229 X25519Mac mac;
230
231 crypto_aead_lock(message.data(), mac.data(), this->derived_keys.ClientToServer().data(), this->key_exchange_nonce.data(),
232 this->our_public_key.data(), this->our_public_key.size(), message.data(), message.size());
233
234 p.Send_bytes(this->our_public_key);
235 p.Send_bytes(mac);
236 p.Send_bytes(message);
237 return true;
238}
239
244std::string X25519AuthenticationHandler::GetPeerPublicKey() const
245{
246 return FormatArrayAsHex(this->peer_public_key);
247}
248
253 void X25519AuthenticationHandler::SendEnableEncryption(struct Packet &p) const
254{
255 p.Send_bytes(this->encryption_nonce);
256}
257
263 bool X25519AuthenticationHandler::ReceiveEnableEncryption(struct Packet &p)
264{
265 return p.Recv_bytes(this->encryption_nonce) == this->encryption_nonce.size();
266}
267
268std::unique_ptr<NetworkEncryptionHandler> X25519AuthenticationHandler::CreateClientToServerEncryptionHandler() const
269{
270 return std::make_unique<X25519EncryptionHandler>(this->derived_keys.ClientToServer(), this->encryption_nonce);
271}
272
273std::unique_ptr<NetworkEncryptionHandler> X25519AuthenticationHandler::CreateServerToClientEncryptionHandler() const
274{
275 return std::make_unique<X25519EncryptionHandler>(this->derived_keys.ServerToClient(), this->encryption_nonce);
276}
277
285 NetworkAuthenticationServerHandler::ResponseResult X25519AuthenticationHandler::ReceiveResponse(Packet &p, std::string_view derived_key_extra_payload)
286{
287 if (p.RemainingBytesToTransfer() != X25519_KEY_SIZE + X25519_MAC_SIZE + X25519_KEY_EXCHANGE_MESSAGE_SIZE) {
288 Debug(net, 1, "[crypto] Received auth response of illegal size; authentication aborted.");
289 return NetworkAuthenticationServerHandler::ResponseResult::NotAuthenticated;
290 }
291
292 X25519KeyExchangeMessage message{};
293 X25519Mac mac;
294
295 p.Recv_bytes(this->peer_public_key);
296 p.Recv_bytes(mac);
297 p.Recv_bytes(message);
298
299 if (!this->derived_keys.Exchange(this->peer_public_key, X25519KeyExchangeSide::SERVER,
300 this->our_secret_key, this->our_public_key, derived_key_extra_payload)) {
301 Debug(net, 0, "[crypto] Peer sent an illegal public key; authentication aborted.");
302 return NetworkAuthenticationServerHandler::ResponseResult::NotAuthenticated;
303 }
304
305 if (crypto_aead_unlock(message.data(), mac.data(), this->derived_keys.ClientToServer().data(), this->key_exchange_nonce.data(),
306 this->peer_public_key.data(), this->peer_public_key.size(), message.data(), message.size()) != 0) {
307 /*
308 * The ciphertext and the message authentication code do not match with the encryption key.
309 * This is most likely an invalid password, or possibly a bug in the client.
310 */
311 return NetworkAuthenticationServerHandler::ResponseResult::NotAuthenticated;
312 }
313
314 return NetworkAuthenticationServerHandler::ResponseResult::Authenticated;
315}
316
317
318/* virtual */ NetworkAuthenticationClientHandler::RequestResult X25519PAKEClientHandler::ReceiveRequest(struct Packet &p)
319{
320 bool success = this->X25519AuthenticationHandler::ReceiveRequest(p);
321 if (!success) return NetworkAuthenticationClientHandler::RequestResult::Invalid;
322
323 this->handler->AskUserForPassword(this->handler);
324 return NetworkAuthenticationClientHandler::RequestResult::AwaitUserInput;
325}
326
334/* static */ X25519SecretKey X25519AuthorizedKeyClientHandler::GetValidSecretKeyAndUpdatePublicKey(std::string &secret_key, std::string &public_key)
335{
336 X25519SecretKey key{};
337 if (!ConvertHexToBytes(secret_key, key)) {
338 if (secret_key.empty()) {
339 Debug(net, 3, "[crypto] Creating a new random key");
340 } else {
341 Debug(net, 0, "[crypto] Found invalid secret key, creating a new random key");
342 }
343 key = X25519SecretKey::CreateRandom();
344 secret_key = FormatArrayAsHex(key);
345 }
346
347 public_key = FormatArrayAsHex(key.CreatePublicKey());
348 return key;
349}
350
351/* virtual */ NetworkAuthenticationServerHandler::ResponseResult X25519AuthorizedKeyServerHandler::ReceiveResponse(Packet &p)
352{
353 ResponseResult result = this->X25519AuthenticationHandler::ReceiveResponse(p, {});
354 if (result != ResponseResult::Authenticated) return result;
355
356 std::string peer_public_key = this->GetPeerPublicKey();
357 return this->authorized_key_handler->IsAllowed(peer_public_key) ? ResponseResult::Authenticated : ResponseResult::NotAuthenticated;
358}
359
360
361/* virtual */ NetworkAuthenticationClientHandler::RequestResult CombinedAuthenticationClientHandler::ReceiveRequest(struct Packet &p)
362{
363 NetworkAuthenticationMethod method = static_cast<NetworkAuthenticationMethod>(p.Recv_uint8());
364
365 auto is_of_method = [method](Handler &handler) { return handler->GetAuthenticationMethod() == method; };
366 auto it = std::ranges::find_if(handlers, is_of_method);
367 if (it == handlers.end()) return RequestResult::Invalid;
368
369 this->current_handler = it->get();
370
371 Debug(net, 9, "Received {} authentication request", this->GetName());
372 return this->current_handler->ReceiveRequest(p);
373}
374
375/* virtual */ bool CombinedAuthenticationClientHandler::SendResponse(struct Packet &p)
376{
377 Debug(net, 9, "Sending {} authentication response", this->GetName());
378
379 return this->current_handler->SendResponse(p);
380}
381
382/* virtual */ std::string_view CombinedAuthenticationClientHandler::GetName() const
383{
384 return this->current_handler != nullptr ? this->current_handler->GetName() : "Unknown";
385}
386
387/* virtual */ NetworkAuthenticationMethod CombinedAuthenticationClientHandler::GetAuthenticationMethod() const
388{
389 return this->current_handler != nullptr ? this->current_handler->GetAuthenticationMethod() : NetworkAuthenticationMethod::End;
390}
391
392
397 void CombinedAuthenticationServerHandler::Add(CombinedAuthenticationServerHandler::Handler &&handler)
398{
399 /* Is the handler configured correctly, e.g. does it have a password? */
400 if (!handler->CanBeUsed()) return;
401
402 this->handlers.push_back(std::move(handler));
403}
404
405/* virtual */ void CombinedAuthenticationServerHandler::SendRequest(struct Packet &p)
406{
407 Debug(net, 9, "Sending {} authentication request", this->GetName());
408
409 p.Send_uint8(to_underlying(this->handlers.back()->GetAuthenticationMethod()));
410 this->handlers.back()->SendRequest(p);
411}
412
413/* virtual */ NetworkAuthenticationServerHandler::ResponseResult CombinedAuthenticationServerHandler::ReceiveResponse(struct Packet &p)
414{
415 Debug(net, 9, "Receiving {} authentication response", this->GetName());
416
417 ResponseResult result = this->handlers.back()->ReceiveResponse(p);
418 if (result != ResponseResult::NotAuthenticated) return result;
419
420 this->handlers.pop_back();
421 return this->CanBeUsed() ? ResponseResult::RetryNextMethod : ResponseResult::NotAuthenticated;
422}
423
424/* virtual */ std::string_view CombinedAuthenticationServerHandler::GetName() const
425{
426 return this->CanBeUsed() ? this->handlers.back()->GetName() : "Unknown";
427}
428
429/* virtual */ NetworkAuthenticationMethod CombinedAuthenticationServerHandler::GetAuthenticationMethod() const
430{
431 return this->CanBeUsed() ? this->handlers.back()->GetAuthenticationMethod() : NetworkAuthenticationMethod::End;
432}
433
434/* virtual */ bool CombinedAuthenticationServerHandler::CanBeUsed() const
435{
436 return !this->handlers.empty();
437}
438
439
440/* virtual */ void NetworkAuthenticationPasswordRequestHandler::Reply(const std::string &password)
441{
442 this->password = password;
443 this->SendResponse();
444}
445
451/* static */ void NetworkAuthenticationClientHandler::EnsureValidSecretKeyAndUpdatePublicKey(std::string &secret_key, std::string &public_key)
452{
453 X25519AuthorizedKeyClientHandler::GetValidSecretKeyAndUpdatePublicKey(secret_key, public_key);
454}
455
462/* static */ std::unique_ptr<NetworkAuthenticationClientHandler> NetworkAuthenticationClientHandler::Create(std::shared_ptr<NetworkAuthenticationPasswordRequestHandler> password_handler, std::string &secret_key, std::string &public_key)
463{
464 auto secret = X25519AuthorizedKeyClientHandler::GetValidSecretKeyAndUpdatePublicKey(secret_key, public_key);
465 auto handler = std::make_unique<CombinedAuthenticationClientHandler>();
466 handler->Add(std::make_unique<X25519KeyExchangeOnlyClientHandler>(secret));
467 handler->Add(std::make_unique<X25519PAKEClientHandler>(secret, std::move(password_handler)));
468 handler->Add(std::make_unique<X25519AuthorizedKeyClientHandler>(secret));
469 return handler;
470}
471
478std::unique_ptr<NetworkAuthenticationServerHandler> NetworkAuthenticationServerHandler::Create(const NetworkAuthenticationPasswordProvider *password_provider, const NetworkAuthenticationAuthorizedKeyHandler *authorized_key_handler, NetworkAuthenticationMethodMask client_supported_method_mask)
479{
480 auto secret = X25519SecretKey::CreateRandom();
481 auto handler = std::make_unique<CombinedAuthenticationServerHandler>();
482 if (password_provider != nullptr && client_supported_method_mask.Test(NetworkAuthenticationMethod::X25519_PAKE)) {
483 handler->Add(std::make_unique<X25519PAKEServerHandler>(secret, password_provider));
484 }
485
486 if (authorized_key_handler != nullptr && client_supported_method_mask.Test(NetworkAuthenticationMethod::X25519_AuthorizedKey)) {
487 handler->Add(std::make_unique<X25519AuthorizedKeyServerHandler>(secret, authorized_key_handler));
488 }
489
490 if (!handler->CanBeUsed() && client_supported_method_mask.Test(NetworkAuthenticationMethod::X25519_KeyExchangeOnly)) {
491 /* Fall back to the plain handler when neither password, nor authorized keys are configured. */
492 handler->Add(std::make_unique<X25519KeyExchangeOnlyServerHandler>(secret));
493 }
494 return handler;
495}
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
CombinedAuthenticationClientHandler::handlers
std::vector< Handler > handlers
The handlers that we can authenticate with.
Definition network_crypto_internal.h:305
CombinedAuthenticationClientHandler::GetAuthenticationMethod
virtual NetworkAuthenticationMethod GetAuthenticationMethod() const override
Get the method this handler is providing functionality for.
Definition network_crypto.cpp:387
CombinedAuthenticationClientHandler::current_handler
NetworkAuthenticationClientHandler * current_handler
The currently active handler.
Definition network_crypto_internal.h:306
CombinedAuthenticationClientHandler::SendResponse
virtual bool SendResponse(struct Packet &p) override
Create the response to send to the server.
Definition network_crypto.cpp:375
CombinedAuthenticationClientHandler::Handler
std::unique_ptr< NetworkAuthenticationClientHandler > Handler
The type of the inner handlers.
Definition network_crypto_internal.h:302
CombinedAuthenticationClientHandler::ReceiveRequest
virtual RequestResult ReceiveRequest(struct Packet &p) override
Read a request from the server.
Definition network_crypto.cpp:361
CombinedAuthenticationClientHandler::GetName
virtual std::string_view GetName() const override
Get the name of the handler for debug messages.
Definition network_crypto.cpp:382
CombinedAuthenticationServerHandler::GetName
virtual std::string_view GetName() const override
Get the name of the handler for debug messages.
Definition network_crypto.cpp:424
CombinedAuthenticationServerHandler::Add
void Add(Handler &&handler)
Add the given sub-handler to this handler, if the handler can be used (e.g.
Definition network_crypto.cpp:397
CombinedAuthenticationServerHandler::handlers
std::vector< Handler > handlers
The handlers that we can (still) authenticate with.
Definition network_crypto_internal.h:335
CombinedAuthenticationServerHandler::Handler
std::unique_ptr< NetworkAuthenticationServerHandler > Handler
The type of the inner handlers.
Definition network_crypto_internal.h:332
CombinedAuthenticationServerHandler::ReceiveResponse
virtual ResponseResult ReceiveResponse(struct Packet &p) override
Read the response from the client.
Definition network_crypto.cpp:413
CombinedAuthenticationServerHandler::CanBeUsed
virtual bool CanBeUsed() const override
Checks whether this handler can be used with the current configuration.
Definition network_crypto.cpp:434
CombinedAuthenticationServerHandler::GetAuthenticationMethod
virtual NetworkAuthenticationMethod GetAuthenticationMethod() const override
Get the method this handler is providing functionality for.
Definition network_crypto.cpp:429
CombinedAuthenticationServerHandler::SendRequest
virtual void SendRequest(struct Packet &p) override
Create the request to send to the client.
Definition network_crypto.cpp:405
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
NetworkAuthenticationAuthorizedKeyHandler
Callback interface for server implementations to provide the authorized key validation.
Definition network_crypto.h:140
NetworkAuthenticationAuthorizedKeyHandler::IsAllowed
virtual bool IsAllowed(std::string_view peer_public_key) const =0
Check whether the given public key of the peer is allowed in.
NetworkAuthenticationClientHandler::RequestResult
RequestResult
The processing result of receiving a request.
Definition network_crypto.h:225
NetworkAuthenticationClientHandler::RequestResult::AwaitUserInput
@ AwaitUserInput
We have requested some user input, but must wait on that.
NetworkAuthenticationClientHandler::RequestResult::Invalid
@ Invalid
We have received an invalid request.
NetworkAuthenticationClientHandler::EnsureValidSecretKeyAndUpdatePublicKey
static void EnsureValidSecretKeyAndUpdatePublicKey(std::string &secret_key, std::string &public_key)
Ensures that the given secret key is valid, and when not overwrite it with a valid secret key.
Definition network_crypto.cpp:451
NetworkAuthenticationClientHandler::ReceiveRequest
virtual RequestResult ReceiveRequest(struct Packet &p)=0
Read a request from the server.
NetworkAuthenticationClientHandler::SendResponse
virtual bool SendResponse(struct Packet &p)=0
Create the response to send to the server.
NetworkAuthenticationClientHandler::Create
static std::unique_ptr< NetworkAuthenticationClientHandler > Create(std::shared_ptr< NetworkAuthenticationPasswordRequestHandler > password_handler, std::string &secret_key, std::string &public_key)
Create a NetworkAuthenticationClientHandler.
Definition network_crypto.cpp:462
NetworkAuthenticationHandler::GetName
virtual std::string_view GetName() const =0
Get the name of the handler for debug messages.
NetworkAuthenticationHandler::GetAuthenticationMethod
virtual NetworkAuthenticationMethod GetAuthenticationMethod() const =0
Get the method this handler is providing functionality for.
NetworkAuthenticationPasswordProvider
Callback interface for server implementations to provide the current password.
Definition network_crypto.h:109
NetworkAuthenticationPasswordRequestHandler::SendResponse
virtual void SendResponse()=0
Callback to trigger sending the response for the password request.
NetworkAuthenticationPasswordRequestHandler::Reply
virtual void Reply(const std::string &password) override
Reply to the request with the given password.
Definition network_crypto.cpp:440
NetworkAuthenticationPasswordRequestHandler::password
std::string password
The entered password.
Definition network_crypto.h:88
NetworkAuthenticationServerHandler::Create
static std::unique_ptr< NetworkAuthenticationServerHandler > Create(const NetworkAuthenticationPasswordProvider *password_provider, const NetworkAuthenticationAuthorizedKeyHandler *authorized_key_handler, NetworkAuthenticationMethodMask client_supported_method_mask={NetworkAuthenticationMethod::X25519_KeyExchangeOnly, NetworkAuthenticationMethod::X25519_PAKE, NetworkAuthenticationMethod::X25519_AuthorizedKey})
Create a NetworkAuthenticationServerHandler.
Definition network_crypto.cpp:478
NetworkAuthenticationServerHandler::ResponseResult
ResponseResult
The processing result of receiving a response.
Definition network_crypto.h:261
NetworkAuthenticationServerHandler::ResponseResult::RetryNextMethod
@ RetryNextMethod
The client failed to authenticate, but there is another method to try.
NetworkAuthenticationServerHandler::ResponseResult::NotAuthenticated
@ NotAuthenticated
All authentications for this handler have been exhausted.
NetworkAuthenticationServerHandler::ResponseResult::Authenticated
@ Authenticated
The client was authenticated successfully.
NetworkEncryptionHandler
Base class for handling the encryption (or decryption) of a network connection.
Definition network_crypto.h:41
X25519AuthenticationHandler::ReceiveEnableEncryption
bool ReceiveEnableEncryption(struct Packet &p)
Receive the initial nonce for the encrypted connection.
Definition network_crypto.cpp:263
X25519AuthenticationHandler::X25519AuthenticationHandler
X25519AuthenticationHandler(const X25519SecretKey &secret_key)
Create the handler, and generate the public keys accordingly.
Definition network_crypto.cpp:184
X25519AuthenticationHandler::peer_public_key
X25519PublicKey peer_public_key
The public key used by our peer.
Definition network_crypto_internal.h:110
X25519AuthenticationHandler::encryption_nonce
X25519Nonce encryption_nonce
The nonce to prevent replay attacks the encrypted connection.
Definition network_crypto_internal.h:112
X25519AuthenticationHandler::ReceiveResponse
NetworkAuthenticationServerHandler::ResponseResult ReceiveResponse(struct Packet &p, std::string_view derived_key_extra_payload)
Read the key exchange data from a Packet that came from the client, and check whether the client pass...
Definition network_crypto.cpp:285
X25519AuthenticationHandler::key_exchange_nonce
X25519Nonce key_exchange_nonce
The nonce to prevent replay attacks of the key exchange.
Definition network_crypto_internal.h:108
X25519AuthenticationHandler::SendResponse
bool SendResponse(struct Packet &p, std::string_view derived_key_extra_payload)
Perform the key exchange, and when that is correct fill the Packet with the appropriate data.
Definition network_crypto.cpp:219
X25519AuthenticationHandler::derived_keys
X25519DerivedKeys derived_keys
Keys derived from the authentication process.
Definition network_crypto_internal.h:109
X25519AuthenticationHandler::GetPeerPublicKey
std::string GetPeerPublicKey() const
Get the public key the peer provided for the key exchange.
Definition network_crypto.cpp:244
X25519AuthenticationHandler::ReceiveRequest
bool ReceiveRequest(struct Packet &p)
Read the key exchange data from a Packet that came from the server,.
Definition network_crypto.cpp:201
X25519AuthenticationHandler::SendEnableEncryption
void SendEnableEncryption(struct Packet &p) const
Send the initial nonce for the encrypted connection.
Definition network_crypto.cpp:253
X25519AuthenticationHandler::our_public_key
X25519PublicKey our_public_key
The public key used by us.
Definition network_crypto_internal.h:107
X25519AuthorizedKeyClientHandler::GetValidSecretKeyAndUpdatePublicKey
static X25519SecretKey GetValidSecretKeyAndUpdatePublicKey(std::string &secret_key, std::string &public_key)
Get the secret key from the given string.
Definition network_crypto.cpp:334
X25519AuthorizedKeyServerHandler::GetPeerPublicKey
virtual std::string GetPeerPublicKey() const override
Get the public key the peer provided during the authentication.
Definition network_crypto_internal.h:289
X25519AuthorizedKeyServerHandler::ReceiveResponse
virtual ResponseResult ReceiveResponse(struct Packet &p) override
Read the response from the client.
Definition network_crypto.cpp:351
X25519AuthorizedKeyServerHandler::authorized_key_handler
const NetworkAuthenticationAuthorizedKeyHandler * authorized_key_handler
The handler of the authorized keys.
Definition network_crypto_internal.h:273
X25519DerivedKeys::keys
std::array< uint8_t, X25519_KEY_SIZE+X25519_KEY_SIZE > keys
Single contiguous buffer to store the derived keys in, as they are generated as a single hash.
Definition network_crypto_internal.h:64
X25519DerivedKeys::Exchange
bool Exchange(const X25519PublicKey &peer_public_key, X25519KeyExchangeSide side, const X25519SecretKey &our_secret_key, const X25519PublicKey &our_public_key, std::string_view extra_payload)
Perform the actual key exchange.
Definition network_crypto.cpp:64
X25519DerivedKeys::ClientToServer
std::span< const uint8_t > ClientToServer() const
Get the key to encrypt or decrypt a message sent from the client to the server.
Definition network_crypto.cpp:41
X25519DerivedKeys::ServerToClient
std::span< const uint8_t > ServerToClient() const
Get the key to encrypt or decrypt a message sent from the server to the client.
Definition network_crypto.cpp:50
X25519DerivedKeys::~X25519DerivedKeys
~X25519DerivedKeys()
Ensure the derived keys do not get leaked when we're done with it.
Definition network_crypto.cpp:32
X25519EncryptionHandler
Encryption handler implementation for monocypher encryption after a X25519 key exchange.
Definition network_crypto.cpp:97
X25519EncryptionHandler::~X25519EncryptionHandler
~X25519EncryptionHandler()
Ensure the encryption context is wiped!
Definition network_crypto.cpp:114
X25519EncryptionHandler::MACSize
size_t MACSize() const override
Get the size of the MAC (Message Authentication Code) used by the underlying encryption protocol.
Definition network_crypto.cpp:119
X25519EncryptionHandler::Encrypt
void Encrypt(std::span< std::uint8_t > mac, std::span< std::uint8_t > message) override
Encrypt the given message in-place, and write the associated MAC.
Definition network_crypto.cpp:129
X25519EncryptionHandler::context
crypto_aead_ctx context
The actual encryption context.
Definition network_crypto.cpp:99
X25519EncryptionHandler::Decrypt
bool Decrypt(std::span< std::uint8_t > mac, std::span< std::uint8_t > message) override
Decrypt the given message in-place, validating against the given MAC.
Definition network_crypto.cpp:124
X25519EncryptionHandler::X25519EncryptionHandler
X25519EncryptionHandler(const std::span< const uint8_t > key, const X25519Nonce &nonce)
Create the encryption handler.
Definition network_crypto.cpp:107
X25519PAKEClientHandler::ReceiveRequest
virtual RequestResult ReceiveRequest(struct Packet &p) override
Read a request from the server.
Definition network_crypto.cpp:318
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
to_underlying
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
crypto_wipe
static void crypto_wipe(std::span< uint8_t > span)
Call crypto_wipe for all the data in the given span.
Definition network_crypto.cpp:26
NetworkAuthenticationMethod
NetworkAuthenticationMethod
The authentication method that can be used.
Definition network_crypto.h:177
NetworkAuthenticationMethod::X25519_KeyExchangeOnly
@ X25519_KeyExchangeOnly
No actual authentication is taking place, just perform a x25519 key exchange. This method is not supp...
NetworkAuthenticationMethod::X25519_PAKE
@ X25519_PAKE
Authentication using x25519 password-authenticated key agreement.
NetworkAuthenticationMethod::End
@ End
Must ALWAYS be on the end of this list!! (period)
NetworkAuthenticationMethod::X25519_AuthorizedKey
@ X25519_AuthorizedKey
Authentication using x22519 key exchange and authorized keys.
network_crypto_internal.h
Internal bits to the crypto of the network handling.
X25519_MAC_SIZE
constexpr size_t X25519_MAC_SIZE
The number of bytes the message authentication codes are in X25519.
Definition network_crypto_internal.h:20
X25519_NONCE_SIZE
constexpr size_t X25519_NONCE_SIZE
The number of bytes the nonces are in X25519.
Definition network_crypto_internal.h:18
X25519KeyExchangeMessage
std::array< uint8_t, X25519_KEY_EXCHANGE_MESSAGE_SIZE > X25519KeyExchangeMessage
Container for a X25519 key exchange message.
Definition network_crypto_internal.h:49
X25519_KEY_EXCHANGE_MESSAGE_SIZE
constexpr size_t X25519_KEY_EXCHANGE_MESSAGE_SIZE
The number of bytes the (random) payload of the authentication message has.
Definition network_crypto_internal.h:22
X25519_KEY_SIZE
constexpr size_t X25519_KEY_SIZE
The number of bytes the public and secret keys are in X25519.
Definition network_crypto_internal.h:16
X25519Mac
std::array< uint8_t, X25519_MAC_SIZE > X25519Mac
Container for a X25519 message authentication code.
Definition network_crypto_internal.h:46
X25519KeyExchangeSide
X25519KeyExchangeSide
The side of the key exchange.
Definition network_crypto_internal.h:52
X25519KeyExchangeSide::SERVER
@ SERVER
We are the server.
X25519KeyExchangeSide::CLIENT
@ CLIENT
We are the client.
packet.h
Basic functions to create, fill and read packets.
RandomBytesWithFallback
void RandomBytesWithFallback(std::span< uint8_t > buf)
Fill the given buffer with random bytes.
Definition random_func.cpp:94
ConvertHexToBytes
bool ConvertHexToBytes(std::string_view hex, std::span< uint8_t > bytes)
Convert a hex-string to a byte-array, while validating it was actually hex.
Definition string.cpp:570
FormatArrayAsHex
std::string FormatArrayAsHex(std::span< const uint8_t > data)
Format a byte array into a continuous hex string.
Definition string.cpp:77
Packet
Internal entity of a packet.
Definition packet.h:43
Packet::Recv_bytes
size_t Recv_bytes(std::span< uint8_t > span)
Extract at most the length of the span bytes from the packet into the span.
Definition packet.cpp:403
Packet::Send_bytes
std::span< const uint8_t > Send_bytes(const std::span< const uint8_t > span)
Send as many of the bytes as possible in the packet.
Definition packet.cpp:197
Packet::Recv_uint8
uint8_t Recv_uint8()
Read a 8 bits integer from the packet.
Definition packet.cpp:318
Packet::Send_uint8
void Send_uint8(uint8_t data)
Package a 8 bits integer in the packet.
Definition packet.cpp:120
Packet::RemainingBytesToTransfer
size_t RemainingBytesToTransfer() const
Get the amount of bytes that are still available for the Transfer functions.
Definition packet.cpp:447
X25519Key
Container for a X25519 key that is automatically crypto-wiped when destructed.
Definition network_crypto_internal.h:25
X25519Key::~X25519Key
~X25519Key()
Ensure the key does not get leaked when we're done with it.
Definition network_crypto.cpp:136
X25519Nonce
Container for a X25519 nonce that is automatically crypto-wiped when destructed.
Definition network_crypto_internal.h:40
X25519Nonce::CreateRandom
static X25519Nonce CreateRandom()
Create a new nonce that's filled with random bytes.
Definition network_crypto.cpp:167
X25519Nonce::~X25519Nonce
~X25519Nonce()
Ensure the nonce does not get leaked when we're done with it.
Definition network_crypto.cpp:175
X25519PublicKey
Container for a X25519 public key.
Definition network_crypto_internal.h:30
X25519SecretKey
Container for a X25519 secret key.
Definition network_crypto_internal.h:34
X25519SecretKey::CreatePublicKey
X25519PublicKey CreatePublicKey() const
Create the public key associated with this secret key.
Definition network_crypto.cpp:156
X25519SecretKey::CreateRandom
static X25519SecretKey CreateRandom()
Create a new secret key that's filled with random bytes.
Definition network_crypto.cpp:145