Security Encryption
This section covers SDK encryption-related topics API and certificate tools.
- For information on generating certificates, configuring server-to-server trust, and troubleshooting, please refer to Certificate Generation and Configuration.
- For information on how to pass security parameters when creating a Session, please refer to Constructor.
- For information on security mode detection supported by the server, please refer to GetEndpoints.
Three types SecurityMode
enum class MessageSecurityMode : int {
Invalid = 0,
None = 1,
Sign = 2,
SignAndEncrypt = 3
};
Pass the following when constructing a Session:
#include <darra/opcua/client.hpp>
ConnectionConfig cfg;
cfg.endpoint_url = "opc.tcp://server:4840";
cfg.security_mode = MessageSecurityMode::SignAndEncrypt;
cfg.client_cert_path = "C:/certs/client.pfx"; // Client PFX, containing a private key
cfg.client_key_path = "123456"; // The password PFX for this (the field name is a historical remnant; it is not the private key path)
Session s(cfg);
client_key_path Not the private key pathclient_key_path The one installed is client_cert_path The password for PFX that. The private key is already embedded in PFX; if you don't need it, you cannot provide a separate private key file.PFX If there is no password, leave the string blank.
| Mode | Data Integrity | Confidentiality | Applicable |
|---|---|---|---|
| None | × | × | Development / Internal Network / Debugging |
| Sign | ✓ | × | Tamper-proof, but not sniffer-proof |
| SignAndEncrypt | ✓ | ✓ | Production Recommendations |
Sign / SignAndEncrypt Must Provide client_cert_path, otherwise it will fail immediately Connect().
SecurityPolicyUri
The current implementation supports only two:
http://opcfoundation.org/UA/SecurityPolicy#Nonehttp://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256(Primary Use)
security_policy_uri If left blank, the system will automatically security_mode select an option (None → #None, Other → #Basic256Sha256); if you enter a value explicitly, that value will be used.
UserToken
enum class UserTokenType : int {
Anonymous = 0,
Username = 1,
Certificate = 2,
Issued = 3
};
Select ConnectionConfig:
| Token | Field | Implementation Status |
|---|---|---|
| Anonymous | No username (default) | Available |
| Username | username = "...", password = "..." | Available |
| Certificate | — | Not yet implemented |
| Issued | — | Not yet implemented |
As long as it is not empty username, the Session will be constructedMandatory When using a token Username, the user_token_type field is ignored.
Certificate Generation (OpenSSL)
The C++ SDK currently has no built-in certificate tool; we recommend generating the PFX with OpenSSL. See Certificate Generation and Configuration for details.
Simplified commands:
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
urn:darra:opcua:client reported by the client is ApplicationUriHard-Coded Constants urn:darra:opcua:client, Does not read configuration; cannot be modified. The value SAN in the certificate URI field must be entered exactly as shown; if you enter a custom value (for example urn:my-company:my-client), the server will reject it BadCertificateUriInvalid, and the encrypted connection will not be established.