PLCnext API Documentation 24.6.0.58
TlsSocket.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
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
52
53public: // construction/destruction
54
61
68 TlsSocket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode, TlsOptions options);
69
71 TlsSocket(const TlsSocket& arg) = delete;
72
74 TlsSocket& operator=(const TlsSocket& arg) = delete;
75
80
81private: // construction
82 TlsSocket(Socket::Ptr pSocket, TlsContextPtr pContext, bool isInitialized);
83
84public: // operators
85
86public: // static operations
87
88public: // setter/getter operations
89
94
99
103 bool IsBlocking(void);
104
108 bool IsConnected(void);
109
113 bool IsTlsConnected(void);
114
119
125 int GetRemotePort(void);
126
127public: // operations
128
146 Ptr Accept(IpAddress& ip4address, int& port, SocketError& error);
147
159 SocketError Bind(const IpAddress& ip4Address, int port);
160
175 SocketError Bind2(const IpAddress& ip4Address, int& port);
176
188 SocketError Listen(size_t backlog);
189
199 SocketError Connect(const IpAddress& ip4Address, int port);
200
205
211
218
226 int Send(const void* pBuffer, size_t length, SocketError& error);
227
235 int Receive(void* pBuffer, size_t length, SocketError& error);
236
245 bool Select(SelectMode mode, Microseconds timeout, SocketError& error);
246
260 int Poll(PollMode mode, Milliseconds timeout, SocketError& error);
261
269 SocketError SetSocketOption(SocketOptionName optionName, const void* optionValue, size_t optionLength);
270
279 SocketError GetSocketOption(SocketOptionName optionName, void* optionValue, size_t *optionLength);
280
292
300 SocketError GetOptionReuseAddress(bool& enabled);
301
312 SocketError SetOptionKeepAlive(bool enabled);
313
321 SocketError GetOptionKeepAlive(bool& enabled);
322
331 SocketError SetOptionBroadcast(bool enabled);
332
340 SocketError GetOptionBroadcast(bool& enabled);
341
351 SocketError SetOptionNoDelay(bool enabled);
352
360 SocketError GetOptionNoDelay(bool& enabled);
361
377 SocketError SetOptionLinger(bool enable, size_t timeout);
378
385 SocketError GetOptionLinger(bool& enable, size_t& timeout);
386
406 SocketError SetOptionUserTimeout(size_t timeout_ms);
407
416 SocketError GetOptionUserTimeout(size_t& timeout_ms);
417
426
436
445
455
464
474
478 SocketError SetOptionBlocking(bool enable);
479
497 SocketError InitClient(const String& trustStoreName, const String& identityStoreName, const String& hostName);
498
510 SocketError InitServer(const String& identityStoreName, const String& trustStoreName = "");
511
520 void SetCipherList(String cipherList);
521
531
532
545 SocketError UpdateSessionKeys(bool requestUpdate = true);
546
560 SocketError GetPeerCertificate(Certificate& certificate);
561
562protected: // operations
563
564private: // static methods
565
566
567private: // methods
568 Ptr TcpAccept(IpAddress& ip4address, int& port, SocketError& error);
569 SocketError TlsAccept(void);
570 SocketError TcpConnect(const IpAddress& ipAddress, int port);
571 SocketError TlsConnect(void);
572 int PollRead(Milliseconds timeout, SocketError& error);
573 int PollWrite(Milliseconds timeout, SocketError& error);
574 int PollConnect(Milliseconds timeout, SocketError& error);
575 int PollAccept(Milliseconds timeout, SocketError& error);
576 SocketError HandleSslResult(int result, int* sslErrorOut = nullptr);
577 int GetFileDescriptor(void);
578 void ClearOpenSslErrors(void);
579
580 SocketError UpdateSessionKeysTls13(bool requestUpdate = true);
581 SocketError UpdateSessionKeysTls12();
582 bool IsKeyUpdateScheduled() const;
583 SocketError CreateSslConnection(void);
584
585private: // fields
586 Socket::Ptr pSocket = nullptr;
587 SSL* sslConnection = nullptr;
588 TlsContextPtr pContext;
589
590 Ptr currentAcceptSocket;
591
592 bool tlsIsConnected; //true if TLS Handshake was completed
593 bool hasSslError;
594 bool socketIsConnected; //true if tcp socket is connected / accepted (set before SSL_connect / SSL_accept)
595 bool tlsInitDone;
596 bool isInitialized; // true if InitClient resp. InitServer has already been called
597 bool tlsConnectIsPending;
598 bool pendingTlsConnectNeedsRead;
599
600 String hostNameToVerify;
601};
602
604// inline methods of class Socket
605
607{
608 return this->pSocket->GetSocketType();
609}
610
612{
613 return this->pSocket->GetSocketDomain();
614}
615
616inline bool TlsSocket::IsBlocking(void)
617{
618 return this->pSocket->IsBlocking();
619}
620
622{
623 return this->pSocket->GetRemoteIpAddress();
624}
625
627{
628 return this->pSocket->GetRemotePort();
629}
630
631inline bool TlsSocket::IsConnected(void)
632{
633 return (!this->hasSslError) && (this->tlsIsConnected || this->socketIsConnected);
634}
635
637{
638 return (!this->hasSslError) && (this->tlsIsConnected);
639}
640
641inline SocketError TlsSocket::Bind(const IpAddress& ip4Address, int port)
642{
643 return pSocket->Bind(ip4Address, port);
644}
645
646inline SocketError TlsSocket::Bind2(const IpAddress& ip4Address, int& port)
647{
648 return pSocket->Bind2(ip4Address, port);
649}
650
651
652inline SocketError TlsSocket::Listen(size_t backlog)
653{
654 return pSocket->Listen(backlog);
655}
656
657inline SocketError TlsSocket::SetSocketOption(SocketOptionName optionName, const void * optionValue, size_t optionLength)
658{
659 return this->pSocket->SetSocketOption(optionName, optionValue, optionLength);
660}
661
662inline SocketError TlsSocket::GetSocketOption(SocketOptionName optionName, void * optionValue, size_t * optionLength)
663{
664 return this->pSocket->GetSocketOption(optionName, optionValue, optionLength);
665}
666
668{
669 return this->pSocket->SetOptionReuseAddress(enabled);
670}
671
673{
674 return this->pSocket->GetOptionReuseAddress(enabled);
675}
676
678{
679 return this->pSocket->SetOptionKeepAlive(enabled);
680}
681
683{
684 return this->pSocket->GetOptionKeepAlive(enabled);
685}
686
688{
689 return this->pSocket->SetOptionBroadcast(enabled);
690}
691
693{
694 return this->pSocket->GetOptionBroadcast(enabled);
695}
696
698{
699 return this->pSocket->SetOptionNoDelay(enabled);
700}
701
703{
704 return this->pSocket->GetOptionNoDelay(enabled);
705}
706
707inline SocketError TlsSocket::SetOptionLinger(bool enable, size_t timeout)
708{
709 return this->pSocket->SetOptionLinger(enable, timeout);
710}
711
712inline SocketError TlsSocket::GetOptionLinger(bool& enable, size_t& timeout)
713{
714 return this->pSocket->GetOptionLinger(enable, timeout);
715}
716
718{
719 return this->pSocket->SetOptionUserTimeout(timeout_ms);
720}
721
723{
724 return this->pSocket->GetOptionUserTimeout(timeout_ms);
725}
726
728{
729 return this->pSocket->SetOptionKeepAliveIdleTime(seconds);
730}
731
733{
734 return this->pSocket->GetOptionKeepAliveIdleTime(seconds);
735}
736
738{
739 return this->pSocket->SetOptionKeepAliveProbeInterval(seconds);
740}
741
743{
744 return this->pSocket->GetOptionKeepAliveProbeInterval(seconds);
745}
746
748{
749 return this->pSocket->SetOptionKeepAliveProbeCount(probeCount);
750}
751
753{
754 return this->pSocket->GetOptionKeepAliveProbeCount(probeCount);
755}
756
758{
759 return this->pSocket->SetOptionBlocking(enable);
760}
761
762}}}} // end of namespace Arp::System::Commons::Net
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.
SocketError SetOptionUserTimeout(size_t timeout_ms)
Sets the retransmission timeout of a socket. This only works on Linux.
Definition: TlsSocket.hpp:717
Ptr Accept(IpAddress &ip4address, int &port, SocketError &error)
Accepts a pending connection request.
SocketError GetOptionKeepAlive(bool &enabled)
Checks if keep-alive is enabled.
Definition: TlsSocket.hpp:682
SocketError SetOptionReuseAddress(bool enabled)
Enables/Disables the reuse-address option for this socket.
Definition: TlsSocket.hpp:667
SocketDomain GetSocketDomain(void)
Returns the type of the socket.
Definition: TlsSocket.hpp:611
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.hpp:697
TlsSocket & operator=(const TlsSocket &arg)=delete
Assignment operator.
SocketError SetOptionBlocking(bool enable)
Enables/disables the blocking mode of this socket..
Definition: TlsSocket.hpp:757
bool Select(SelectMode mode, Microseconds timeout, SocketError &error)
Checks if an i/o operation can be performed without blocking the calling thread.
SocketError GetSocketOption(SocketOptionName optionName, void *optionValue, size_t *optionLength)
Returns current value of queried socket option.
Definition: TlsSocket.hpp:662
SocketError SetSocketOption(SocketOptionName optionName, const void *optionValue, size_t optionLength)
Sets a single option on the socket.
Definition: TlsSocket.hpp:657
TlsSocket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode)
Constructs an TlsSocket instance.
int Send(const void *pBuffer, size_t length, SocketError &error)
Transmit data over the socket that is in a connected state.
SocketError Bind2(const IpAddress &ip4Address, int &port)
Binds the socket to a specific address and port combination.
Definition: TlsSocket.hpp:646
SocketError SetOptionKeepAliveProbeCount(int probeCount)
Sets the amount of probes to be sent, if the KeepAlive SocketOption is set to true.
Definition: TlsSocket.hpp:747
TlsSocket(SocketType type, SocketDomain domain, SocketBlockingMode blockingMode, TlsOptions options)
Constructs an TlsSocket instance.
SocketError GetOptionLinger(bool &enable, size_t &timeout)
Returns the current linger options. For more information see Arp::System::Commons::Net::Socket::SetOp...
Definition: TlsSocket.hpp:712
SocketError Bind(const IpAddress &ip4Address, int port)
Binds the socket to a specific address and port combination.
Definition: TlsSocket.hpp:641
SocketError Shutdown(void)
Shuts down a full-duplex connection.
int Receive(void *pBuffer, size_t length, SocketError &error)
Reads data from connected socket.
SocketError GetOptionKeepAliveProbeCount(int &probeCount)
Gets the amount of probes to be sent, if the KeepAlive SocketOption is set to true....
Definition: TlsSocket.hpp:752
SocketError GetOptionKeepAliveProbeInterval(int &seconds)
Gets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
Definition: TlsSocket.hpp:742
SocketError Shutdown(ShutdownMode mode)
Shuts down a full-duplex connection.
~TlsSocket(void)
Destructs this instance and frees all resouces.
SocketError InitClient(const String &trustStoreName, const String &identityStoreName, const String &hostName)
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.hpp:722
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.hpp:727
int GetRemotePort(void)
If this socket is connected this method returns the port of the connection.
Definition: TlsSocket.hpp:626
SocketError InitServer(const String &identityStoreName, const String &trustStoreName="")
bool IsBlocking(void)
Checks if the socket is in blocking mode.
Definition: TlsSocket.hpp:616
bool IsTlsConnected(void)
Checks if a TLS connection is established with a remote peer.
Definition: TlsSocket.hpp:636
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.hpp:732
int Poll(PollMode mode, Milliseconds timeout, SocketError &error)
Checks if an i/o operation can be processed without blocking.
SocketError SetOptionKeepAlive(bool enabled)
Enables/Disables the keep-alive option for this socket.
Definition: TlsSocket.hpp:677
SocketError SetOptionLinger(bool enable, size_t timeout)
Sets the amount of time a socket resides in TIME_WAIT state after active close.
Definition: TlsSocket.hpp:707
SocketError Connect(const IpAddress &ip4Address, int port)
Tries to connect with a remote socket.
IpAddress GetRemoteIpAddress(void)
Checks if this socket is connected with a remote peer.
Definition: TlsSocket.hpp:621
bool IsConnected(void)
Checks if the socket is in blocking mode.
Definition: TlsSocket.hpp:631
TlsSocket(const TlsSocket &arg)=delete
Copy contructor.
SocketError SetOptionBroadcast(bool enabled)
Enables/Disables broadcast for this socket.
Definition: TlsSocket.hpp:687
SocketError GetOptionBroadcast(bool &enabled)
Checks if broadcast is enabled.
Definition: TlsSocket.hpp:692
SocketError UpdateSessionKeys(bool requestUpdate=true)
SocketError SetOptionKeepAliveProbeInterval(int seconds)
Sets the interval time in between each Keep Alive probe, if the KeepAlive SocketOption is set to true...
Definition: TlsSocket.hpp:737
SocketType GetSocketType(void)
Returns the type of the socket.
Definition: TlsSocket.hpp:606
void SetCipherList(String cipherList)
SocketError Listen(size_t backlog)
Marks this socket as a passive socket that accepts incoming connection requests.
Definition: TlsSocket.hpp:652
SocketError GetOptionReuseAddress(bool &enabled)
Checks if reuse-address is enabled.
Definition: TlsSocket.hpp:672
SocketError GetOptionNoDelay(bool &enabled)
Checks if no-delay is enabled.
Definition: TlsSocket.hpp:702
Class to handle x.509 certificates
Definition: Certificate.hpp:25
Definition: ISocketService.hpp:13
std::chrono::milliseconds Milliseconds
The Arp Milliseconds unit class.
Definition: TypeSystem.h:52
std::chrono::microseconds Microseconds
The Arp Microseconds unit class.
Definition: TypeSystem.h:49
@ System
System components used by the System, Device, Plc or Io domains.
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
Definition: Asn1Time.hpp:18
Root namespace for the PLCnext API