Enable Encryption
In a production environment, it is strongly recommended to use Sign or SignAndEncrypt + Basic256Sha256. There are only three steps: generate a certificate → import it into the server’s trust store → pass it to the client during construction PFX.
- Basics — Security Model — Concept
- FAQ — Encryption Configuration — Troubleshooting Common Errors
- Required Environment — Preparations Before Launch
Generate a Client Certificate (PFX)
- SDK Autogenerated (C#)
- OpenSSL
// If it does not exist, generate it automatically and write it to disk; if it already exists, reuse it directly. Returns (pfxPath, pfxPassword)
var (pfxPath, pwd) = CertificateManager.EnsureClientCertificate();
Subject / SAN URI / The validity period is SDK hard-coded and cannot be customized — it is SAN URI always urn:darra:opcua:client.PFX The password is a random, strong password stored in a bypass DPAPI file client.pfx.pwd; it is not a fixed password that you set yourself.
# 1. Private Key + Certificate
openssl req -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes \
-subj "/CN=MyClient/O=Darra/C=CN" \
-addext "subjectAltName=URI:urn:darra:opcua:client,DNS:my-host"
# 2. Packing PFX
openssl pkcs12 -export -out client.pfx -inkey key.pem -in cert.pem -password pass:123456
OPC UA requires the certificate to contain the subjectAltName field. This SDK's ApplicationUri is the hard-coded constant urn:darra:opcua:client, with no field to change it, so the SAN URI: can only be this one value — anything else makes the server reject the connection with "SAN URI does not match ApplicationUri".
Import and Trust on the Server
Copy cert.pem (or the exported public key .der) to the server's "Trusted Client Certificates" directory:
- Darra Software PLC Server —
%ProgramData%\Darra\PKI\trusted\certs\(Configuration optiontrust_list_dir; this is the default value; copy it and restart the service for the changes to take effect) - The customer's current server — Imported by the remote operations team; the path is in the format
<server>/pki/trusted/certs/or uploaded via the "Trusted Client Certificates" page in the admin panel
By default auto_accept_untrusted = false, the Darra server does not automatically trust new client certificates; certificates not in the trusted directory are rejected %ProgramData%\Darra\PKI\rejected\certs\, and the connection is denied.
serverCertPath When not transmitted SDKDo not store server certificate fingerprints; do not perform cross-session comparisons —— There is no implementation TOFU. At this point, every connection uses a plaintext channel with None to GetEndpoints retrieve the server's self-signed certificate and then upgrade the encryption, Can't prevent man-in-the-middle attacks. To specify "only this server," you must explicitly pass serverCertPath (PKCS#12 PFX, no password); passing DER will cause the load to fail andSilent Downgrade Back to Auto-Discovery.
Pass the certificate when constructing the client
- C#
- Python
- Java
using var ua = new DarraOpcUa(
endpointUrl: "opc.tcp://server:4840",
securityMode: MessageSecurityMode.SignAndEncrypt,
clientCertPath: @"C:\certs\client.pfx",
clientKeyPath: "123456" // PFX Password
);
ua.Connect();
ua = DarraOpcUa(
"opc.tcp://server:4840",
security_mode=MessageSecurityMode.SignAndEncrypt,
client_cert_path="/etc/darra/client.pfx",
client_key_path="123456") # PFX Password
ua.connect()
import com.darra.opcua.Enums;
import com.darra.opcua.OpcUaSession;
// Full Construction — The parameter order matches that of the constructor OpcUaSession
var ua = new OpcUaSession(
"opc.tcp://server:4840",
Enums.MessageSecurityMode.SignAndEncrypt,
/* username */ null,
/* password */ null,
/* sessionTimeoutMs */ 60000,
/* requestTimeoutMs */ 5000,
/* connectTimeoutMs */ 10000,
/* keepAliveIntervalMs*/ 10000,
/* autoReconnect */ true,
/* autoPublishOnConnect */ true,
/* clientCertPath */ "/etc/darra/client.pfx",
/* clientKeyPath */ "123456", // PFX Password
/* serverCertPath */ null,
/* securityPolicyUri */ null); // Leave blank to select securityMode automatically
ua.connect();
PFX If no password is provided, you can use the quick construction directly OpcUaSession.secureSignAndEncrypt(endpointUrl, clientCertPath)(corresponding to "Sign" mode secureSign).
SecurityMode Select
| Pattern | Tamper-proof | Encryption | Performance Overhead | When to Use |
|---|---|---|---|---|
| None | × | × | 0 | Intranet, Development, Performance-Sensitive |
| Sign | ✓ | × | Low (~5%) | Prevention MITM—it doesn't matter if the traffic is visible |
| SignAndEncrypt | ✓ | ✓ | Medium (~15%) | Public Network, Factory Intranet, Default Recommendation |
SecurityPolicy
The current implementation supports two options:
http://opcfoundation.org/UA/SecurityPolicy#None— For None mode onlyhttp://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256— Sign and SignAndEncrypt Default
If no strategy is passed to the C#, Java, or Python wrapper, the strategy is automatically securityMode selected securityPolicyUri as follows: None → #None; all others → #Basic256Sha256.
When calling the protocol stack directly in C, this inference does not occur: if left blank security_policy_uri, it always falls back to the default #None; in C, you must explicitly pass a strategy URI.
UserToken (Login credentials)
An encrypted channel and a login identity are two separate things:
// Encryption + Username and Password
using var ua = new DarraOpcUa(
endpointUrl: "opc.tcp://server:4840",
securityMode: MessageSecurityMode.SignAndEncrypt,
username: "operator",
password: "secret",
clientCertPath: @"C:\certs\client.pfx",
clientKeyPath: "123456");
Troubleshooting Certificate Mutual Trust Issues
| Error | Reason | Resolve |
|---|---|---|
| BadCertificateUntrusted | The server did not add the client certificate to the trusted list. | Copy the client .der to the server %ProgramData%\Darra\PKI\trusted\certs\ |
| BadCertificateInvalid | Certificate expired / not yet valid / incorrect format | Regenerate |
| BadCertificateUriInvalid | Certificate SAN.URI Does Not ApplicationUri Match | SAN It must be URI:urn:darra:opcua:client (ApplicationUri a hard-coded constant) |
| BadSecurityChecksFailed | General security validation failed (not specific to any particular cause) | Check the server logs to determine the specific cause. |
| BadSecurityModeRejected | The server does not support this mode. | Switch to a less secure mode or check the server configuration |
For a detailed investigation, see FAQ → Encryption Configuration.