2. OPC UA Security

This section assumes that you are familiar with the security configuration of OPC UA and with public key infrastructures.

By default, the client connects using the SecurityPolicy None, i.e. the communication is unencrypted. This is only acceptable in trusted networks, e.g. the internal network of a Docker or Kubernetes deployment.

voraus_ecat.configure_security() enables encryption for an asyncua client, with the PKI directory and the options in voraus_ecat.SecurityOptions. The client is then connected with voraus_ecat.EtherCAT.connection_from_client():

from pathlib import Path

from asyncua.sync import Client

from voraus_ecat import EtherCAT, SecurityOptions, configure_security

from fieldbus import Inputs, Outputs

ethercat = EtherCAT(inputs=Inputs(), outputs=Outputs())

client = Client("opc.tcp://192.168.1.10:4840")
try:
    configure_security(client, SecurityOptions(pki_dir=Path("pki")))
except:
    client.disconnect()
    raise
with ethercat.connection_from_client(client):
    ethercat.set_op_state()

voraus_ecat.configure_security() requests the endpoints of the master and selects the endpoint with the configured SecurityPolicy and security mode, by default Basic256Sha256 with SignAndEncrypt. Other combinations are selected with security_policy and security_mode, e.g. SecurityPolicyAes256Sha256RsaPss from asyncua.crypto.security_policies. If the master does not offer the configured combination, voraus_ecat.configure_security() raises asyncua.ua.UaError with the message No matching endpoints. The SecurityPolicies and security modes that the master offers depend on its configuration, see the OPC UA server documentation of the master.

Because the endpoints are requested, the master has to be running when voraus_ecat.configure_security() is called. If the master cannot be reached, voraus_ecat.configure_security() raises ConnectionError.

Call voraus_ecat.configure_security() once per client, before the client is connected. If it fails, disconnect the client, otherwise its thread keeps the Python process alive, see Connecting with an Existing Client.

2.1. PKI Directory

The PKI directory has the layout of the PKI directory of the master, limited to the folders read by the client:

<pki>/
└── ApplCerts/
    ├── own/
    │   ├── certs/
    │   └── private/
    └── trusted/
        ├── certs/
        └── crl/

ApplCerts follows the certificate store directory layout of OPC 10000-12, F.1. Missing folders are created by voraus_ecat.configure_security(). The folders of ApplCerts hold:

  • own/certs: the client certificate, exactly one .der or .pem file.

  • own/private: the private key of the client certificate, with the same base name as the certificate (.der, .pem or .key).

  • trusted/certs: the trusted server certificates and the certificates of trusted CAs (.der or .pem).

  • trusted/crl: the certificate revocation lists of the CAs, .der if DER encoded or .pem if PEM encoded. voraus_ecat.configure_security() raises voraus_ecat.exceptions.PkiError for other files.

Certificate chains with intermediate CAs are currently unsupported: the server certificate has to be self-signed, or issued directly by a CA whose certificate is in trusted/certs.

The server certificate is checked against the trust list when the session is created, after the secure channel is opened. An untrusted server certificate fails the connection with BadCertificateUntrusted, a revoked one with BadCertificateRevoked. To trust a master, copy its certificate to trusted/certs. The master has to trust the client certificate as well.

Instead of trusting each server certificate individually, the certificate of a CA can be placed in trusted/certs. Then every server certificate issued by this CA is trusted. See OPC 10000-2, 9.2 and 9.3 for the management of self-signed and CA signed certificates.

Certificate revocation lists are optional. A certificate is revoked if its serial number is listed in any revocation list in trusted/crl, regardless of the issuer of the list.

2.2. Self-Signed Certificate

Self-signed certificates are useful for tests and small-scale deployments. Since every self-signed client certificate has to be added to the trust list of the master individually, this approach does not scale well. Large deployments can use certificates issued by a CA instead.

With create_self_signed_cert, voraus_ecat.configure_security() creates a self-signed certificate, if ApplCerts/own/certs holds no certificate yet. The certificate is written to ApplCerts/own/certs/client.der, its key to ApplCerts/own/private/client.der. The subject alternative name of the certificate contains the application URI, the hostname and localhost. By default, the certificate is valid for one year, cert_validity_days sets a different validity. An existing certificate is never replaced, also not when it has expired.

The master rejects the connection until the new certificate is in its trust list. Unless the master accepts all client certificates, it is therefore easier to create the certificate before the first connection with the create-self-signed-cert command:

voraus-ecat create-self-signed-cert pki

The command writes the certificate and its key to the same files and prints the path of the certificate. Copy the certificate to ApplCerts/trusted/certs in the PKI directory of the master. --cert-validity-days sets the validity, by default one year. If the PKI directory holds a certificate already, the command fails. To get a new certificate, delete both files and add the new certificate to the trust list of the master.

The application URI of the client must match the URI in its certificate. When using a certificate issued by a CA, set application_uri to the URI in that certificate.

2.3. Insecure Options

Use this option only in trusted networks.

With accept_all_server_certs, the client accepts any server certificate and ignores the trust list. The communication is still encrypted, but the identity of the master is not verified, which allows man-in-the-middle attacks.

2.4. Generating a Script

With --pki, the generate command connects with security, and the generated script uses the same security options:

voraus-ecat generate --pki pki opc.tcp://192.168.1.10:4840 fieldbus.py

The options --create-self-signed-cert, --cert-validity-days and --accept-all-server-certs set the respective fields of voraus_ecat.SecurityOptions.

The path of --pki is written to the generated script unchanged. During the generation, a relative path is resolved against the directory in which voraus-ecat generate is called. The generated script resolves it against the directory in which the script is started. Use an absolute path to start the script from a different directory.