Skip to main content

Certificate Generation and Configuration (C)

Prerequisite Reading

Generate Client PFX​

The C SDK has no built-in PFX generator; use the OpenSSL command line:

OpenSSL Command Line​

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"

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

openssl x509 -in cert.pem -outform DER -out client.der

Windows PowerShell​

$cert = New-SelfSignedCertificate `
-Subject "CN=MyClient,O=Darra,C=CN" `
-TextExtension "2.5.29.17={text}URI=urn:darra:opcua:client" `
-KeyAlgorithm RSA -KeyLength 2048 -NotAfter (Get-Date).AddYears(1) `
-CertStoreLocation "Cert:\CurrentUser\My"

$pwd = ConvertTo-SecureString -String "123456" -Force -AsPlainText
Export-PfxCertificate -Cert $cert -FilePath C:\certs\client.pfx -Password $pwd
Export-Certificate -Cert $cert -FilePath C:\certs\client.der

client.pfx Contains the private key; for use by the client. client.der This is the public key; copy it to the server.

Importing Trust on the Server Side​

Copy the client's public key (.der or .pem) to the server's trusted/ directory:

ServerPath
Darra Software PLC Server%ProgramData%\Darra\PKI\trusted\certs\(Configuration option trust_list_dir; this is the default value; copy it 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.

By default, the Darra server does not automatically trust new client certificates (configuration option auto_accept_untrusted = false); certificates not in the trusted directory are discarded %ProgramData%\Darra\PKI\rejected\certs\, and connections are rejected.

Constructing Time-Transmission in the C-End PFX​

DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = "opc.tcp://server:4840";
cfg.security_mode = UA_MSG_SECURITY_MODE_SIGN_ENCRYPT;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256";
cfg.user_token_type = UA_USER_TOKEN_USERNAME;
cfg.username = "operator";
cfg.password = "secret";
cfg.client_cert_path = "C:/certs/client.pfx";
cfg.client_key_path = "123456"; /* PFX Password */
cfg.server_cert_path = NULL; /* NULL = No preconfiguration; use None→Secure auto-discovery */

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Session_Create(&cfg, &h);
DarraUa_Session_Connect(h);
Path separators

On Windows, you can use either or / \\; do not use a single \(as C interprets it as an escape character).

Server Certificate Validation​

The SDK stores no server certificate fingerprints and performs no cross-connection consistency check.

The SDK has no TOFU mechanism for "saving a fingerprint and comparing it later." Server certificates come from two sources; pick one depending on whether you want to preconfigure them:

cfg.server_cert_path = "C:/certs/server.pfx";    /* One: Pre-configure the server certificate (PKCS#12, passwordless) */

Two: Do not transmit server_cert_path — SDK Switch from None to Secure: First, open the None channel to GetEndpoints retrieve the server certificate DER, then upgrade to the encrypted channel. If a path is provided but PFX loading fails, the system will automatically revert to this method.

If the preset fails, it only issues a WARN and silently downgrades.

When the path-one PFX fails to load, the SDK logs a single WARN (Server cert PFX load failed, will discover via None channel), returns no error code, throws no exception, and offers no way to tell "did it end up using the preconfigured certificate or the auto-discovered one?". Passing a .der this way therefore always fails and silently downgrades — you think you have pinned the server's identity, but you have not. Check the logs to confirm this WARN is absent.

Neither method stores fingerprints or performs consistency checks across sessions; to ensure that the system "only recognizes this server," use Method One to pre-configure the server certificate (PFXwithout a password).

Troubleshooting​

StatusCodeReasonResolve
BadCertificateUntrustedThe server does not trust the client.Copy "client.der" to the server %ProgramData%\Darra\PKI\trusted\certs\
BadCertificateInvalidCertificate has expired / is not yet validRegenerate
BadCertificateUriInvalidCertificate SAN.URI Does Not ApplicationUri MatchSAN You can only enter URI:urn:darra:opcua:client (ApplicationUri this is a hard-coded constant)
BadCertificateHostNameInvalidThe SAN.DNS certificate does not match the connected host.When generating, add subjectAltName=DNS:server.example.com
BadSecurityChecksFailedGeneral security validation failedCheck the server logs to determine the specific cause.
BadSecurityModeRejectedThe server does not support this SecurityModeGetEndpoints Check which ones the server actually supports
BadSecurityPolicyRejectedThe server does not support Basic256Sha256.Enable this policy in the server configuration

The table lists standard OPC UA status code names. DarraUa_StatusName Only common codes are mapped; for codes not listed, the corresponding code is returned "Unknown". In the table above, "BadCertificateInvalid/" BadSecurityChecksFailed is printed by name, while the rest are matched based on the returned hexadecimal code.

SAN URI Importance​

In OPC UA, the "Verify Client Certificate" SAN section must contain the URI: field and it must match the ApplicationUri. This is the key defence against forgery and must be configured correctly.

ApplicationUri It is a hard-coded constant; it cannot be customized.

The client hard-codes ApplicationUri in CreateSession as urn:darra:opcua:client, and there are no DarraUa_SessionConfig fields in that allow it to be changed. Therefore, the certificate's SAN URI: You can only enter this one value — URI If you make one up yourself, the server will reject it, saying it "SAN URI does not ApplicationUri match."

Use to check OpenSSL:

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

The output should be:

X509v3 Subject Alternative Name:
URI:urn:darra:opcua:client

Time Synchronization (NTP)​

This does SDK not perform local time or clock offset verification.

This SDK Will not returnUA_STATUS_BAD_CERTIFICATE_TIME_INVALID (0x80140000) / UA_STATUS_BAD_INVALID_TIMESTAMP (0x80230000) These two status codes mean there is no threshold for "rejection due to a deviation exceeding a certain amount.BadCertificateTimeInvalid" isServer-side This is caused by the system clock being used to determine the certificate's validity period. You still need to correct the system time (otherwise the server will consider the certificate expired), but don't assume it will block SDK the connection:

w32tm /resync

Advanced: Directly call X509 / RSA / SHA / AES API​

<opcua/opcua_security.h> Provides a built-in encryption interface. Call it directly when you need to sign your own business data; generally, business applications do not need to use it.

Next Step​