-

TLS_SOCKET_2

The TLS_SOCKET_2 function block is a newer implementation of the TLS_SOCKET FB combined with the TCP_SOCKET FB which are referred to as "older implementation" in this help topic. The difference between the newer and the older implementation is that TLS_SOCKET_2 combines the handling of TCP and TLS in one FB, i.e., its input parameters specify whether the TLS protocol is initialized and used on top of the TCP. Furthermore, it supports a TCP/TLS server which is able to communicate with several TCP/TLS clients.

Socket handles created with the TLS_SOCKET_2 FB can only be used with the function blocks TLS_SEND_2 and TLS_RECEIVE_2.

Note
This function block requires firmware version 2021.3 or newer.

Note
To update instances of the previous FB implementation by the newer FBs in an existing project, you can simply replace them as follows: Edit the declaration of the instance variable and (only in FBD) update the instance call in the code. Formal parameters with the same name will remain connected, newly added formal parameters have to be connected (if the corresponding function is required).

Type Function block
Description The function block opens and closes a TCP/TLS socket. (The TLS protocol works on top of TCP). This socket can be used either for standard TCP communication or for secure IP communication between a server and a client via Transport Layer Security (TLS).

The TCP/TLS socket is represented by the HANDLE output parameter. This handle has to be forwarded to the function blocks TLS_SEND_2 and TLS_RECEIVE_2, which require the socket handle for the (secure) data exchange with a communication partner.

When implementing a TCP/TLS server that is able to communicate with several TCP/TLS clients, one TLS_SOCKET_2 instance (with IS_SRV = TRUE) must be created for each client to be connected. In this scenario, the clients connect to the respective TLS_SOCKET_2 instance/HANDLE as specified by the DEST_IP/DEST_PORT.

With a rising edge at the ACTIVATE input, the system starts to open the TCP/TLS socket. Once the socket has been opened and the connection has been established successfully, the ACTIVE output is set to TRUE. Only while ACTIVE = TRUE, the socket handle provided at the HANDLE output can be used for subsequent calls of the TLS_SEND_2/TLS_RECEIVE_2 function blocks.

A falling edge at ACTIVATE closes the socket. The connection is terminated in the form of a three-way handshake, i.e. three telegrams are exchanged between client and server. During the termination process a new rising edge results in a function block error.

While ACTIVE = FALSE, calling the TLS_SEND_2/TLS_RECEIVE_2 function blocks with the socket handle causes an error at the function blocks. The socket is kept open as long as the ACTIVATE input is TRUE. The BUSY output is set to TRUE while ACTIVATE is TRUE and the socket is still not open. If the ACTIVATE input switches to FALSE, the socket is closed and the ACTIVE output is set to FALSE (see also the notes below). If the connection is lost, the function block tries to re-establish the connection as long as ACTIVATE = TRUE.

TLS on top of TCP

The START_TLS input defines whether the TLS_SEND_2/TLS_RECEIVE_2 function blocks send and receive data via a pure TCP connection or via a TLS connection. In the typical use case of the TLS_SOCKET_2 FB, the START_TLS input is TRUE when the ACTIVATE input becomes TRUE. In rare situations as for SMTP, the START_TLS input gets a rising edge while ACTIVATE is TRUE. If START_TLS = FALSE once the connection is established, the TLS_SEND_2/TLS_RECEIVE_2 function blocks use a TCP connection to send and receive data.
The SEND_SECURE/RECEIVE_SECURE inputs of the function blocks must be consistent to the value of START_TLS. With a rising edge at START_TLS when the connection is established, the TLS protocol is initialized, i.e., the existing TCP connection is upgraded to a TLS connection. As long as START_TLS = TRUE, data are sent and received via a TLS-secured connection.

For the initialization of the TLS protocol, the data applied at the CONNECT_INFO input when activating the function block are used. The predefined structure at the CONNECT_INFO input holds, among others, the client and server certificates contained in the trust store and identity store of the devices used for authentication (see below for a description of the CONNECT_INFO values and how to manage and link the certificates in the web-based management (WBM) of the controller).

Client or server functionality?

The IS_SRV input defines whether the function block implements server or client functionality.

  • With IS_SRV = TRUE, the function block creates a listening (server) socket. The socket can be bound to the specified local Ethernet adapter (BIND_IP input) and the port (BIND_PORT input) and waits for incoming requests from remote clients. If necessary, the accepted clients can be limited by specifying the DEST_IP/DEST_PORT inputs. With opened (server) socket, only 1:1 connections are possible, i.e., the listening socket can accept only one incoming TLS connection.

    When implementing a TCP/TLS server that is able to communicate with several TCP/TLS clients, one dedicated instance of TLS_SOCKET_2 has to be used for each expected client.

  • With IS_SRV = FALSE, a client socket is created. In case of a client socket, the DEST_IP/DEST_PORT inputs are used to specify the IP address and IP port of the server to communicate with.

WARNING
Unintended machine operation
Verify that the influence of the output data cannot result in an unintended or hazardous behavior of the entire system.

Notes
  • The function blocks for TCP/TLS communication are processed asynchronously. This means, the function blocks issue commands and wait for their processing.
  • The socket handle created by the TLS_SOCKET_2 function block is not compatible with the socket handle created by the TCP_SOCKET or TLS_SOCKET function block. The TCP/TLS socket handle can only be used with the TLS_SEND_2/TLS_RECEIVE_2 function blocks.
  • The number of sockets that are currently opened is indicated by the system variable TLS_ACTIVE_SOCKETS (contained in the Data List of the controller node and 'IEC 61131-3' node in the PLANT).
  • In case of a warm start or cold start or a controller reset, the firmware closes all sockets that have been opened with the corresponding instance of the TLS_SOCKET_2 instance.
  • For controllers with integrated firewall, make sure that the firewall does not block the ports involved in the communication. Otherwise, the connection cannot be established.
  • All IP addresses used are IPv4 addresses that consist of four numbers (0 to 255) separated by dot.
  • Function blocks have to be instantiated.
    The instance name of the function block has to be declared in the 'Variables' table of the POU where the FB is going to be used. The instance name must be unique within the POU.
Parameters Inputs

Note
The inputs specifically relevant for setting up the TLS communication on top of the TCP communication are CONNECT_INFO and START_TLS. The other inputs specify TCP functionality.

ACTIVATE

IS_SRV

BIND_IP

BIND_PORT

DEST_IP

DEST_PORT

CONNECT_INFO

START_TLS

Outputs

HANDLE

ACTIVE

BUSY

ERROR

STATUS

USED_PORT
Additional InfoManaging and linking certificates of TrustStores and IdentityStores