PLCnext API Documentation 25.0.2.69
TlsSocket.hpp
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include "Arp/System/Commons/Net/IpAddress.hpp"
9#include "Arp/System/Commons/Net/Socket.hpp"
10#include "Arp/System/Commons/Logging.h"
11#include "Arp/System/Commons/Exceptions/Exceptions.h"
12#include "Arp/System/Commons/Security/IdentityStore.hpp"
13#include "Arp/System/Core/PimplPtr.hxx"
14#include "Arp/System/Commons/Net/TlsOptions.hpp"
15
16using namespace Arp::System::Commons::Security;
17
18// forwards
19typedef struct ssl_st SSL;
20typedef struct ssl_ctx_st SSL_CTX;
21
22
23namespace Arp { namespace System { namespace Commons { namespace Net
24{
25
33
34
35class TlsContext;
36using TlsContextPtr = std::shared_ptr<TlsContext>;
37
38
39class TlsSocket : private Loggable<TlsSocket>
40{
41
42private:
43 friend class TlsContext;
44
45public: // typedefs/usings
46
48 typedef std::shared_ptr<TlsSocket> Ptr;
49
51 using ISocketService = Arp::System::Ve::ISocketService;
52
53public: // construction/destruction
54
55 TlsSocket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode);
56 TlsSocket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode, TlsOptions options);
57
59 TlsSocket(const TlsSocket& arg) = delete;
60
62 TlsSocket& operator=(const TlsSocket& arg) = delete;
63
64 ~TlsSocket(void);
65
66private: // construction
67 TlsSocket(Socket::Ptr pSocket, TlsContextPtr pContext, bool isInitialized);
68
69public: // operators
70
71public: // static operations
72
73public: // setter/getter operations
76 bool IsBlocking(void);
77 bool IsConnected(void);
78 bool IsTlsConnected(void);
80 int GetRemotePort(void);
81
82public: // operations
83 Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
84 SocketError Bind(const IpAddress& ip4Address, int port);
85 SocketError Bind2(const IpAddress& ip4Address, int& port);
86 SocketError Listen(size_t backlog);
87 SocketError Connect(const IpAddress& ip4Address, int port);
90 SocketError Close(void);
91 int Send(const void* pBuffer, size_t length, SocketError& error);
92 int Receive(void* pBuffer, size_t length, SocketError& error);
93 bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
94 int Poll(PollMode mode, Milliseconds timeout, SocketError& error);
95
96 SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
97 SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength);
100 SocketError SetOptionKeepAlive(bool enabled);
101 SocketError GetOptionKeepAlive(bool& enabled);
102 SocketError SetOptionBroadcast(bool enabled);
103 SocketError GetOptionBroadcast(bool& enabled);
104 SocketError SetOptionNoDelay(bool enabled);
105 SocketError GetOptionNoDelay(bool& enabled);
106 SocketError SetOptionLinger(bool enable, size_t timeout);
107 SocketError GetOptionLinger(bool& enable, size_t& timeout);
108 SocketError SetOptionUserTimeout(size_t timeout_ms);
109 SocketError GetOptionUserTimeout(size_t& timeout_ms);
116 SocketError SetOptionBlocking(bool enable);
117
118 SocketError InitClient(const String& trustStoreName, const String& identityStoreName, const String& hostName);
119 SocketError InitServer(const String& identityStoreName, const String& trustStoreName = "");
120 void SetCipherList(String cipherList);
122 SocketError UpdateSessionKeys(bool requestUpdate = true);
124
125protected: // operations
126
127private: // static methods
128
129
130private: // methods
131 Ptr TcpAccept(IpAddress& ip4address, int& port, SocketError& error);
132 SocketError TlsAccept(void);
133 SocketError TcpConnect(const IpAddress& ipAddress, int port);
134 SocketError TlsConnect(void);
135 int PollRead(Milliseconds timeout, SocketError& error);
136 int PollWrite(Milliseconds timeout, SocketError& error);
137 int PollConnect(Milliseconds timeout, SocketError& error);
138 int PollAccept(Milliseconds timeout, SocketError& error);
139 SocketError HandleSslResult(int result, int* sslErrorOut = nullptr);
140 int GetFileDescriptor(void);
141 void ClearOpenSslErrors(void);
142
143 SocketError UpdateSessionKeysTls13(bool requestUpdate = true);
144 SocketError UpdateSessionKeysTls12();
145 bool IsKeyUpdateScheduled() const;
146 SocketError CreateSslConnection(void);
147
148private: // fields
149 Socket::Ptr pSocket = nullptr;
150 SSL* sslConnection = nullptr;
151 TlsContextPtr pContext;
152
153 Ptr currentAcceptSocket;
154
155 bool tlsIsConnected; //true if TLS Handshake was completed
156 bool hasSslError;
157 bool socketIsConnected; //true if tcp socket is connected / accepted (set before SSL_connect / SSL_accept)
158 bool tlsInitDone;
159 bool isInitialized; // true if InitClient resp. InitServer has already been called
160 bool tlsConnectIsPending;
161 bool pendingTlsConnectNeedsRead;
162
163 String hostNameToVerify;
164};
165
166}}}} // end of namespace Arp::System::Commons::Net
This class represents the Arp String. The implementation is based on std::string.
Definition: String.hpp:39
Derive from this class to inherit logging functionality.
Definition: Loggable.hxx:28
Unified representation for ip address schemes.
Definition: IpAddress.hpp:14
std::shared_ptr< Socket > Ptr
Contextual definition of pointer type.
Definition: Socket.hpp:122
Definition: TlsSocket.hpp:40
SocketError Close(void)
Closes the socket. This ends all communication on the socket.
Definition: TlsSocket.cpp:849
SocketError SetOptionUserTimeout(size_t timeout_ms)
Sets the retransmission timeout of a socket. This only works on Linux.
Definition: TlsSocket.cpp:398
SocketError GetOptionKeepAlive(bool &enabled)
Checks if keep-alive is enabled.
Definition: TlsSocket.cpp:292
SocketError SetOptionReuseAddress(bool enabled)
Enables/Disables the reuse-address option for this socket.
Definition: TlsSocket.cpp:253
SocketDomain GetSocketDomain(void)
Returns the type of the socket.
Definition: TlsSocket.cpp:120
std::shared_ptr< TlsSocket > Ptr
Contextual definition of pointer type.
Definition: TlsSocket.hpp:48
SocketError SetOptionNoDelay(bool enabled)
Enables/Disables no-delay for this socket.
Definition: TlsSocket.cpp:331
TlsSocket & operator=(const TlsSocket &arg)=delete
Assignment operator.
SocketError SetOptionBlocking(bool enable)
Enables/disables the blocking mode of this socket..
Definition: TlsSocket.cpp:494
bool Select(SelectMode mode, Microseconds timeout, SocketError &error)
Checks if an i/o operation can be performed without blocking the calling thread.
Definition: TlsSocket.cpp:947
SocketError GetSocketOption(SocketOptionName optionName, void *optionValue, size_t *optionLength)
Returns current value of queried socket option.
Definition: TlsSocket.cpp:238
SocketError SetSocketOption(SocketOptionName optionName, const void *optionValue, size_t optionLength)
Sets a single option on the socket.
Definition: TlsSocket.cpp:225
TlsSocket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode)
Constructs an TlsSocket instance.
Definition: TlsSocket.cpp:57
int Send(const void *pBuffer, size_t length, SocketError &error)
Transmit data over the socket that is in a connected state.
Definition: TlsSocket.cpp:874
SocketError Bind2(const IpAddress &ip4Address, int &port)
Binds the socket to a specific address and port combination.
Definition: TlsSocket.cpp:197
SocketError SetOptionKeepAliveProbeCount(int probeCount)
Sets the amount of probes to be sent, if the KeepAlive SocketOption is set to true.
Definition: TlsSocket.cpp:473
SocketError GetOptionLinger(bool &enable, size_t &timeout)
Returns the current linger options. For more information see Arp::System::Commons::Net::Socket::SetOp...
Definition: TlsSocket.cpp:374
SocketError Bind(const IpAddress &ip4Address, int port)
Binds the socket to a specific address and port combination.
Definition: TlsSocket.cpp:178
SocketError Shutdown(void)
Shuts down a full-duplex connection.
Definition: TlsSocket.cpp:801
int Receive(void *pBuffer, size_t length, SocketError &error)
Reads data from connected socket.
Definition: TlsSocket.cpp:910
SocketError GetOptionKeepAliveProbeCount(int &probeCount)
Gets the amount of probes to be sent, if the KeepAlive SocketOption is set to true....
Definition: TlsSocket.cpp:486
SocketError GetOptionKeepAliveProbeInterval(int &seconds)
Gets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
Definition: TlsSocket.cpp:461
~TlsSocket(void)
Destructs this instance and frees all resouces.
Definition: TlsSocket.cpp:91
SocketError InitClient(const String &trustStoreName, const String &identityStoreName, const String &hostName)
Initializes this TLS socket in client mode. This method must be called before connecting to a server
Definition: TlsSocket.cpp:517
SocketError GetOptionUserTimeout(size_t &timeout_ms)
Gets the retransmission timeout of a socket. This only works on Linux. For more information see Arp::...
Definition: TlsSocket.cpp:411
SocketError SetOptionKeepAliveIdleTime(int seconds)
Sets the time that the socket needs to be idle for, before the Keep Alive mechanism can start if the ...
Definition: TlsSocket.cpp:423
int GetRemotePort(void)
If this socket is connected this method returns the port of the connection.
Definition: TlsSocket.cpp:146
SocketError RenegotiateSession()
Triggeres a complete renegotion of the esstablished TLS session
Definition: TlsSocket.cpp:1198
SocketError InitServer(const String &identityStoreName, const String &trustStoreName="")
Initializes this TLS socket in server mode. This method must be called before accepting connections w...
Definition: TlsSocket.cpp:553
bool IsBlocking(void)
Checks if the socket is in blocking mode.
Definition: TlsSocket.cpp:128
Arp::System::Ve::ISocketService ISocketService
Injection of SocketService-Interface in class context.
Definition: TlsSocket.hpp:51
bool IsTlsConnected(void)
Checks if a TLS connection is established with a remote peer.
Definition: TlsSocket.cpp:162
SocketError GetOptionKeepAliveIdleTime(int &seconds)
Gets the time that the socket needs to be idle for, before the Keep Alive mechanism can start if the ...
Definition: TlsSocket.cpp:436
int Poll(PollMode mode, Milliseconds timeout, SocketError &error)
Checks if an i/o operation can be processed without blocking.
Definition: TlsSocket.cpp:1025
SocketError SetOptionKeepAlive(bool enabled)
Enables/Disables the keep-alive option for this socket.
Definition: TlsSocket.cpp:280
SocketError SetOptionLinger(bool enable, size_t timeout)
Sets the amount of time a socket resides in TIME_WAIT state after active close.
Definition: TlsSocket.cpp:363
SocketError Connect(const IpAddress &ip4Address, int port)
Tries to connect with a remote socket.
Definition: TlsSocket.cpp:701
IpAddress GetRemoteIpAddress(void)
Checks if this socket is connected with a remote peer.
Definition: TlsSocket.cpp:136
bool IsConnected(void)
Checks if the socket is in blocking mode.
Definition: TlsSocket.cpp:154
Ptr Accept(IpAddress &ip4address, int &port, SocketError &error)
Accepts a pending connection request.
Definition: TlsSocket.cpp:594
TlsSocket(const TlsSocket &arg)=delete
Copy contructor.
SocketError SetOptionBroadcast(bool enabled)
Enables/Disables broadcast for this socket.
Definition: TlsSocket.cpp:305
SocketError GetOptionBroadcast(bool &enabled)
Checks if broadcast is enabled.
Definition: TlsSocket.cpp:317
SocketError UpdateSessionKeys(bool requestUpdate=true)
Update the keys for the current established TLS session
Definition: TlsSocket.cpp:1240
SocketError SetOptionKeepAliveProbeInterval(int seconds)
Sets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
Definition: TlsSocket.cpp:448
SocketType GetSocketType(void)
Returns the type of the socket.
Definition: TlsSocket.cpp:112
void SetCipherList(String cipherList)
Sets the supported ciphers of this socket which are used during connection negotiation
Definition: TlsSocket.cpp:1177
SocketError Listen(size_t backlog)
Marks this socket as a passive socket that accepts incoming connection requests.
Definition: TlsSocket.cpp:213
SocketError GetPeerCertificate(Certificate &certificate)
Retrieves the certificate of the TLS peer
Definition: TlsSocket.cpp:1320
SocketError GetOptionReuseAddress(bool &enabled)
Checks if reuse-address is enabled.
Definition: TlsSocket.cpp:265
SocketError GetOptionNoDelay(bool &enabled)
Checks if no-delay is enabled.
Definition: TlsSocket.cpp:343
Class to handle x.509 certificates
Definition: Certificate.hpp:25
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: ChronoTypes.hpp:40
std::chrono::milliseconds Milliseconds
The Arp Milliseconds unit class.
Definition: ChronoTypes.hpp:43
PollMode
This enum is used to specifiy the poll mode of the <cref name="Socket::Poll" > operation.
Definition: PollMode.hpp:15
SelectMode
Modes for Select call to check different data channels.
Definition: SelectMode.hpp:14
SocketDomain
Supported communication domains, selecting the protocol for communication.
Definition: SocketDomain.hpp:14
SocketType
Enumeration of supported socket types.
Definition: SocketType.hpp:14
ShutdownMode
This enum is used to specifiy the shutdown mode of the <cref name="Socket::Shutdown(ShutdownMode)" > ...
Definition: ShutdownMode.hpp:15
SocketError
Possible error codes for socket operation results.
Definition: SocketError.hpp:15
TlsOptions
This enum is used to select different options for the TlsSocket class
Definition: TlsOptions.hpp:25
SocketOptionName
Specifies socket options to be set by the application. Copied from Eclr Socket Adaption
Definition: SocketOptionName.hpp:17
SocketBlockingMode
Supported blocking modes.
Definition: Socket.hpp:28
Namespace for classes dealing with certificates
Root namespace for the PLCnext API