PLCnext API Documentation 23.6.0.37
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
51 using ISocketService = Arp::System::Ve::ISocketService;
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
390 SocketError SetOptionBlocking(bool enable);
391
409 SocketError InitClient(const String& trustStoreName, const String& identityStoreName, const String& hostName);
410
422 SocketError InitServer(const String& identityStoreName, const String& trustStoreName = "");
423
432 void SetCipherList(String cipherList);
433
443
444
457 SocketError UpdateSessionKeys(bool requestUpdate = true);
458
472 SocketError GetPeerCertificate(Certificate& certificate);
473
474protected: // operations
475
476private: // static methods
477
478
479private: // methods
480 Ptr TcpAccept(IpAddress& ip4address, int& port, SocketError& error);
481 SocketError TlsAccept(void);
482 SocketError TcpConnect(const IpAddress& ipAddress, int port);
483 SocketError TlsConnect(void);
484 int PollRead(Milliseconds timeout, SocketError& error);
485 int PollWrite(Milliseconds timeout, SocketError& error);
486 int PollConnect(Milliseconds timeout, SocketError& error);
487 int PollAccept(Milliseconds timeout, SocketError& error);
488 SocketError HandleSslResult(int result, int* sslErrorOut = nullptr);
489 int GetFileDescriptor(void);
490 void ClearOpenSslErrors(void);
491
492 SocketError UpdateSessionKeysTls13(bool requestUpdate = true);
493 SocketError UpdateSessionKeysTls12();
494 bool IsKeyUpdateScheduled() const;
495 SocketError CreateSslConnection(void);
496
497private: // fields
498 Socket::Ptr pSocket = nullptr;
499 SSL* sslConnection = nullptr;
500 TlsContextPtr pContext;
501
502 Ptr currentAcceptSocket;
503
504 bool tlsIsConnected; //true if TLS Handshake was completed
505 bool hasSslError;
506 bool socketIsConnected; //true if tcp socket is connected / accepted (set before SSL_connect / SSL_accept)
507 bool tlsInitDone;
508 bool isInitialized; // true if InitClient resp. InitServer has already been called
509 bool tlsConnectIsPending;
510 bool pendingTlsConnectNeedsRead;
511
512 String hostNameToVerify;
513};
514
516// inline methods of class Socket
517
519{
520 return this->pSocket->GetSocketType();
521}
522
524{
525 return this->pSocket->GetSocketDomain();
526}
527
528inline bool TlsSocket::IsBlocking(void)
529{
530 return this->pSocket->IsBlocking();
531}
532
534{
535 return this->pSocket->GetRemoteIpAddress();
536}
537
539{
540 return this->pSocket->GetRemotePort();
541}
542
543inline bool TlsSocket::IsConnected(void)
544{
545 return (!this->hasSslError) && (this->tlsIsConnected || this->socketIsConnected);
546}
547
549{
550 return (!this->hasSslError) && (this->tlsIsConnected);
551}
552
553inline SocketError TlsSocket::Bind(const IpAddress& ip4Address, int port)
554{
555 return pSocket->Bind(ip4Address, port);
556}
557
558inline SocketError TlsSocket::Bind2(const IpAddress& ip4Address, int& port)
559{
560 return pSocket->Bind2(ip4Address, port);
561}
562
563
564inline SocketError TlsSocket::Listen(size_t backlog)
565{
566 return pSocket->Listen(backlog);
567}
568
569inline SocketError TlsSocket::SetSocketOption(SocketOptionName optionName, const void * optionValue, size_t optionLength)
570{
571 return this->pSocket->SetSocketOption(optionName, optionValue, optionLength);
572}
573
574inline SocketError TlsSocket::GetSocketOption(SocketOptionName optionName, void * optionValue, size_t * optionLength)
575{
576 return this->pSocket->GetSocketOption(optionName, optionValue, optionLength);
577}
578
580{
581 return this->pSocket->SetOptionReuseAddress(enabled);
582}
583
585{
586 return this->pSocket->GetOptionReuseAddress(enabled);
587}
588
590{
591 return this->pSocket->SetOptionKeepAlive(enabled);
592}
593
595{
596 return this->pSocket->GetOptionKeepAlive(enabled);
597}
598
600{
601 return this->pSocket->SetOptionBroadcast(enabled);
602}
603
605{
606 return this->pSocket->GetOptionBroadcast(enabled);
607}
608
610{
611 return this->pSocket->SetOptionNoDelay(enabled);
612}
613
615{
616 return this->pSocket->GetOptionNoDelay(enabled);
617}
618
619inline SocketError TlsSocket::SetOptionLinger(bool enable, size_t timeout)
620{
621 return this->pSocket->SetOptionLinger(enable, timeout);
622}
623
624inline SocketError TlsSocket::GetOptionLinger(bool& enable, size_t& timeout)
625{
626 return this->pSocket->GetOptionLinger(enable, timeout);
627}
628
630{
631 return this->pSocket->SetOptionBlocking(enable);
632}
633
634}}}} // 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.
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:594
SocketError SetOptionReuseAddress(bool enabled)
Enables/Disables the reuse-address option for this socket.
Definition: TlsSocket.hpp:579
SocketDomain GetSocketDomain(void)
Returns the type of the socket.
Definition: TlsSocket.hpp:523
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:609
TlsSocket & operator=(const TlsSocket &arg)=delete
Assignment operator.
SocketError SetOptionBlocking(bool enable)
Enables/disables the blocking mode of this socket..
Definition: TlsSocket.hpp:629
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:574
SocketError SetSocketOption(SocketOptionName optionName, const void *optionValue, size_t optionLength)
Sets a single option on the socket.
Definition: TlsSocket.hpp:569
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:558
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:624
SocketError Bind(const IpAddress &ip4Address, int port)
Binds the socket to a specific address and port combination.
Definition: TlsSocket.hpp:553
SocketError Shutdown(void)
Shuts down a full-duplex connection.
int Receive(void *pBuffer, size_t length, SocketError &error)
Reads data from connected socket.
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)
int GetRemotePort(void)
If this socket is connected this method returns the port of the connection.
Definition: TlsSocket.hpp:538
SocketError InitServer(const String &identityStoreName, const String &trustStoreName="")
bool IsBlocking(void)
Checks if the socket is in blocking mode.
Definition: TlsSocket.hpp:528
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.hpp:548
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:589
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:619
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:533
bool IsConnected(void)
Checks if the socket is in blocking mode.
Definition: TlsSocket.hpp:543
TlsSocket(const TlsSocket &arg)=delete
Copy contructor.
SocketError SetOptionBroadcast(bool enabled)
Enables/Disables broadcast for this socket.
Definition: TlsSocket.hpp:599
SocketError GetOptionBroadcast(bool &enabled)
Checks if broadcast is enabled.
Definition: TlsSocket.hpp:604
SocketError UpdateSessionKeys(bool requestUpdate=true)
SocketType GetSocketType(void)
Returns the type of the socket.
Definition: TlsSocket.hpp:518
void SetCipherList(String cipherList)
SocketError Listen(size_t backlog)
Marks this socket as a passive socket that accepts incoming connection requests.
Definition: TlsSocket.hpp:564
SocketError GetOptionReuseAddress(bool &enabled)
Checks if reuse-address is enabled.
Definition: TlsSocket.hpp:584
SocketError GetOptionNoDelay(bool &enabled)
Checks if no-delay is enabled.
Definition: TlsSocket.hpp:614
Class to handle x.509 certificates
Definition: Certificate.hpp:25
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