OpenTTD Source 20260218-master-g2123fca5ea
network_crypto.h
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
32
33#ifndef NETWORK_CRYPTO_H
34#define NETWORK_CRYPTO_H
35
36#include "network_type.h"
37
42public:
43 virtual ~NetworkEncryptionHandler() = default;
44
49 virtual size_t MACSize() const = 0;
50
57 virtual bool Decrypt(std::span<std::uint8_t> mac, std::span<std::uint8_t> message) = 0;
58
64 virtual void Encrypt(std::span<std::uint8_t> mac, std::span<std::uint8_t> message) = 0;
65};
66
67
72public:
73 virtual ~NetworkAuthenticationPasswordRequest() = default;
74
79 virtual void Reply(const std::string &password) = 0;
80};
81
86protected:
87 friend class X25519PAKEClientHandler;
88
89 std::string password;
90public:
91
92 void Reply(const std::string &password) override;
93
97 virtual void SendResponse() = 0;
98
103 virtual void AskUserForPassword(std::shared_ptr<NetworkAuthenticationPasswordRequest> request) = 0;
104};
105
106
111public:
112 virtual ~NetworkAuthenticationPasswordProvider() = default;
113
118 virtual std::string_view GetPassword() const = 0;
119};
120
125private:
126 const std::string *password;
127public:
134
135 std::string_view GetPassword() const override { return *this->password; };
136};
137
142public:
144
149 virtual bool CanBeUsed() const = 0;
150
156 virtual bool IsAllowed(std::string_view peer_public_key) const = 0;
157};
158
163private:
165public:
171
172 bool CanBeUsed() const override { return !this->authorized_keys->empty(); }
173 bool IsAllowed(std::string_view peer_public_key) const override { return authorized_keys->Contains(peer_public_key); }
174};
175
176
184
187
192public:
193 virtual ~NetworkAuthenticationHandler() = default;
194
199 virtual std::string_view GetName() const = 0;
200
206
211 virtual std::unique_ptr<NetworkEncryptionHandler> CreateClientToServerEncryptionHandler() const = 0;
212
217 virtual std::unique_ptr<NetworkEncryptionHandler> CreateServerToClientEncryptionHandler() const = 0;
218};
219
224public:
231
237 virtual RequestResult ReceiveRequest(struct Packet &p) = 0;
238
244 virtual bool SendResponse(struct Packet &p) = 0;
245
251 virtual bool ReceiveEnableEncryption(struct Packet &p) = 0;
252
253 static void EnsureValidSecretKeyAndUpdatePublicKey(std::string &secret_key, std::string &public_key);
254 static std::unique_ptr<NetworkAuthenticationClientHandler> Create(std::shared_ptr<NetworkAuthenticationPasswordRequestHandler> password_handler, std::string &secret_key, std::string &public_key);
255};
256
261public:
268
273 virtual void SendRequest(struct Packet &p) = 0;
274
280 virtual ResponseResult ReceiveResponse(struct Packet &p) = 0;
281
286 virtual void SendEnableEncryption(struct Packet &p) = 0;
287
293 virtual bool CanBeUsed() const = 0;
294
299 virtual std::string GetPeerPublicKey() const = 0;
300
302};
303
304#endif /* NETWORK_CRYPTO_H */
Enum-as-bit-set wrapper.
Callback interface for server implementations to provide the authorized key validation.
virtual bool IsAllowed(std::string_view peer_public_key) const =0
Check whether the given public key of the peer is allowed in.
virtual bool CanBeUsed() const =0
Check whether the key handler can be used, i.e.
Base class for client side cryptographic authentication handlers.
RequestResult
The processing result of receiving a request.
@ AwaitUserInput
We have requested some user input, but must wait on that.
@ Invalid
We have received an invalid request.
@ ReadyForResponse
We do not have to wait for user input, and can immediately respond to the server.
virtual bool ReceiveEnableEncryption(struct Packet &p)=0
Read the request to enable encryption from the server.
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.
virtual RequestResult ReceiveRequest(struct Packet &p)=0
Read a request from the server.
virtual bool SendResponse(struct Packet &p)=0
Create the response to send to the server.
static std::unique_ptr< NetworkAuthenticationClientHandler > Create(std::shared_ptr< NetworkAuthenticationPasswordRequestHandler > password_handler, std::string &secret_key, std::string &public_key)
Create a NetworkAuthenticationClientHandler.
bool CanBeUsed() const override
Check whether the key handler can be used, i.e.
NetworkAuthenticationDefaultAuthorizedKeyHandler(const NetworkAuthorizedKeys &authorized_keys)
Create the handler that uses the given authorized keys to check against.
const NetworkAuthorizedKeys * authorized_keys
The authorized keys to check against.
bool IsAllowed(std::string_view peer_public_key) const override
Check whether the given public key of the peer is allowed in.
const std::string * password
The password to check against.
NetworkAuthenticationDefaultPasswordProvider(const std::string &password)
Create the provider with the pointer to the password that is to be used.
std::string_view GetPassword() const override
Callback to return the password where to validate against.
Base class for cryptographic authentication handlers.
virtual std::string_view GetName() const =0
Get the name of the handler for debug messages.
virtual std::unique_ptr< NetworkEncryptionHandler > CreateServerToClientEncryptionHandler() const =0
Create a NetworkEncryptionHandler to encrypt or decrypt messages from the server to the client.
virtual NetworkAuthenticationMethod GetAuthenticationMethod() const =0
Get the method this handler is providing functionality for.
virtual std::unique_ptr< NetworkEncryptionHandler > CreateClientToServerEncryptionHandler() const =0
Create a NetworkEncryptionHandler to encrypt or decrypt messages from the client to the server.
Callback interface for server implementations to provide the current password.
virtual std::string_view GetPassword() const =0
Callback to return the password where to validate against.
Callback interface for client implementations to provide the handling of the password requests.
virtual void AskUserForPassword(std::shared_ptr< NetworkAuthenticationPasswordRequest > request)=0
Callback to trigger asking the user for the password.
virtual void SendResponse()=0
Callback to trigger sending the response for the password request.
void Reply(const std::string &password) override
Reply to the request with the given password.
std::string password
The entered password.
Callback interface for requests for passwords in the context of network authentication.
virtual void Reply(const std::string &password)=0
Reply to the request with the given password.
Base class for server side cryptographic authentication handlers.
virtual ResponseResult ReceiveResponse(struct Packet &p)=0
Read the response from the client.
virtual void SendEnableEncryption(struct Packet &p)=0
Create the request to enable encryption to the client.
virtual bool CanBeUsed() const =0
Checks whether this handler can be used with the current configuration.
virtual std::string GetPeerPublicKey() const =0
Get the public key the peer provided during the authentication.
virtual void SendRequest(struct Packet &p)=0
Create the request to send to the client.
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.
ResponseResult
The processing result of receiving a response.
@ RetryNextMethod
The client failed to authenticate, but there is another method to try.
@ NotAuthenticated
All authentications for this handler have been exhausted.
@ Authenticated
The client was authenticated successfully.
Simple helper to (more easily) manage authorized keys.
Base class for handling the encryption (or decryption) of a network connection.
virtual bool Decrypt(std::span< std::uint8_t > mac, std::span< std::uint8_t > message)=0
Decrypt the given message in-place, validating against the given MAC.
virtual void Encrypt(std::span< std::uint8_t > mac, std::span< std::uint8_t > message)=0
Encrypt the given message in-place, and write the associated MAC.
virtual size_t MACSize() const =0
Get the size of the MAC (Message Authentication Code) used by the underlying encryption protocol.
EnumBitSet< NetworkAuthenticationMethod, uint16_t > NetworkAuthenticationMethodMask
The mask of authentication methods that can be used.
NetworkAuthenticationMethod
The authentication method that can be used.
@ X25519_KeyExchangeOnly
No actual authentication is taking place, just perform a x25519 key exchange. This method is not supp...
@ X25519_PAKE
Authentication using x25519 password-authenticated key agreement.
@ End
Must ALWAYS be on the end of this list!! (period).
@ X25519_AuthorizedKey
Authentication using x22519 key exchange and authorized keys.
Types used for networking.
Internal entity of a packet.
Definition packet.h:41