Skip to main content

Certificate Generation and Configuration

Prerequisite Reading

Generate Client PFX​

# Generate a self-signed certificate + SAN URI
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:my-company:my-client"

# Forward PFX
openssl pkcs12 -export -out client.pfx -inkey key.pem -in cert.pem -password pass:123456

# Export DER Public Key (for importing into the server's trust)
openssl x509 -in cert.pem -outform der -out client.der

Use C# CertificateManager (reuse existing tools)​

If the project already includes a C# component, reuse its certificate manager to generate or retrieve certificates PFX; the generated PFXorDER retrieved certificates can be loaded directly Rust SDK. This manager exposes only three methods:

MethodsFunction
CertificateManager.EnsureClientCertificate() -> (string pfxPath, string password)If a certificate already exists, reuse it; otherwise, generate one.
CertificateManager.ForceRegenerate() -> (string pfxPath, string password)Force Rebirth
CertificateManager.DescribeCachedCertificate() -> stringDescribe the currently cached certificate

password After obtaining pfxPath /, enter cfg.client_cert_path / and. respectively cfg.client_key_path.

Importing Trust on the Server Side​

Copy the client's public key (.der or .pem) to the server's "Trusted Client Certificates" directory:

ServerPath
Darra Software PLC Server%ProgramData%\DarraPLC\opcua\pki\trusted\certs\ (Copy the files and restart the service for the changes to take effect)
The customer's current serverImported by the remote operations team; the path is in the format <server>/pki/trusted/certs/ or uploaded to the management console.

Passing Arguments When Constructing a Rust Client PFX​

use darra_opcua::{Session, ConnectionConfig, MessageSecurityMode};

let mut cfg = ConnectionConfig::new("opc.tcp://server:4840");
cfg.security_mode = MessageSecurityMode::SignAndEncrypt;
cfg.client_cert_path = Some("C:/certs/client.pfx".into());
cfg.client_key_path = Some("123456".into()); // PFX Password
cfg.server_cert_path = None; // None = Use the "None→Secure" automatic upgrade path; GetEndpoints retrieve the server certificate from
let mut s = Session::with_config(cfg)?;
s.connect()?;

Server Certificate (server_cert_path)​

server_cert_path This is an optional server certificate path DER:

  • Do not pass (None): Use the automatic upgrade from None to Secure; the server certificate is GetEndpoints retrieved from.
  • Explicitly specify the path DER: Use this certificate directly.
cfg.server_cert_path = Some("C:/certs/server.der".into());
caution

The Rust SDK implements no TOFU (first-trust pinning) and no certificate fingerprint storage / comparison; the source contains no such code. Do not interpret this field as "initial trust + fingerprint verification to prevent man-in-the-middle attacks."

To explicitly specify a server certificate, first obtain Discovery::get_endpoints it server_certificate: Vec<u8>, save it to disk, and then upload it:

use std::fs::write;
use darra_opcua::Discovery;

let eps = Discovery::get_endpoints("opc.tcp://server:4840", 10_000)?;
let cert_der = &eps[0].server_certificate;
write("server.der", cert_der)?;

cfg.server_cert_path = Some("server.der".into());

Troubleshooting​

StatusCodeReasonResolve
BAD_CERTIFICATE_INVALIDCertificate has expired / is not yet validRegenerate
BAD_SECURITY_CHECKS_FAILEDGeneral security validation failedCheck the server logs to determine the specific cause.
BAD_SECURITY_MODE_REJECTEDThe server does not support this SecurityModeDiscovery::get_endpoints See which features are actually supported on the server side
BAD_SECURITY_POLICY_REJECTEDThe server does not support Basic256Sha256.Enable this policy in the server configuration

Common server-side issues SDK not listed here:

  • BadCertificateUntrusted — The server does not trust the client → Copy client.der to trusted/certs/ on the server
  • BadCertificateUriInvalid — Certificate SAN.URI does not ApplicationUri match → Ensure consistency when regenerating
  • BadCertificateHostNameInvalid — Certificate does not match the connected SAN.DNS host → Add subjectAltName=DNS:server.example.com

SAN URI Importance​

In OPC UA, the "Verify Client Certificate" SAN section must contain the URI: field, and its value must match the ApplicationUri. This is key to preventing forgery and must be configured correctly.

OpenSSL Verify Certificate SAN:

openssl x509 -in cert.pem -text -noout | grep -A 3 "Subject Alternative Name"

The output should look something like this:

X509v3 Subject Alternative Name:
URI:urn:my-company:my-client

Time Synchronization (NTP)​

In encrypted mode, both parties verify the timestamps; any deviation beyond the allowed window will be rejected. The timestamp validity window is determined by your security configuration; test it based on your configuration. This must be configured in a production environment NTP, and the time deviation must be kept within seconds.

w32tm /resync

Example of a Fully Encrypted Connection​

use darra_opcua::{Session, ConnectionConfig, MessageSecurityMode, Discovery};

darra_opcua::initialize()?;

// 1. Pull server-side endpoints
let eps = Discovery::get_endpoints("opc.tcp://server:4840", 10_000)?;
let ep = eps.iter()
.find(|e| e.security_mode == MessageSecurityMode::SignAndEncrypt)
.ok_or("no SignAndEncrypt endpoint")?;

// 2. Construct using the selected endpoint
let mut cfg = ConnectionConfig::new(&ep.endpoint_url);
cfg.security_mode = ep.security_mode;
cfg.security_policy_uri = Some(ep.security_policy_uri.clone());
cfg.username = Some("operator".into());
cfg.password = Some("secret".into());
cfg.client_cert_path = Some("C:/certs/client.pfx".into());
cfg.client_key_path = Some("123456".into());

let mut s = Session::with_config(cfg)?;
s.connect()?;
println!("Connected with {:?}", ep.security_mode);

Next Step​