跳到主要内容

证书生成与配置 (C)

前置阅读

生成客户端 PFX​

C SDK 不内置 PFX 生成器, 用 OpenSSL 命令行:

OpenSSL 命令行​

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 含私钥, 给客户端用; client.der 是公钥, 拷贝到服务端.

服务端导入信任​

把客户端公钥(.der 或 .pem)拷到服务端 trusted/ 目录:

Server路径
Darra Software PLC Server%ProgramData%\Darra\PKI\trusted\certs\(配置项 trust_list_dir, 默认值即此; 复制后重启 Service 生效)
客户现有 Server由对端运维负责导入, 路径形如 <server>/pki/trusted/certs/ 或上传到管理后台

Darra 服务端默认不自动信任新客户端证书(配置项 auto_accept_untrusted = false), 没进信任目录的证书会被丢到 %ProgramData%\Darra\PKI\rejected\certs\, 连接被拒。

C 端构造时传 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 密码 */
cfg.server_cert_path = NULL; /* NULL = 不预置, 走 None→Secure 自动发现 */

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Session_Create(&cfg, &h);
DarraUa_Session_Connect(h);
路径分隔符

Windows 下用 / 或 \\ 都可, 不要用单 \(会被 C 当转义符).

服务端证书校验​

SDK 不保存服务端证书指纹, 也不做跨次连接的一致性比对.

SDK 没有"保存指纹、之后比对"的 TOFU 机制. 服务端证书有两种来源, 按要不要预置选一条:

cfg.server_cert_path = "C:/certs/server.pfx";    /* 一: 预置服务端证书 (PKCS#12, 无密码) */

二: 不传 server_cert_path — SDK 走 None→Secure 升级: 先开 None 通道用 GetEndpoints 取服务端证书 DER, 再升级到加密通道. 传了路径但 PFX 加载失败时, 也会自动退回这条路.

预置失败只 WARN 就静默降级

路一的 PFX 加载失败时, SDK 只打一条 WARN (Server cert PFX load failed, will discover via None channel), 不返回错误码、不上抛异常、也没有查询接口能确认"最终用的是预置证书还是自动发现的"。所以传成 .der 会必然失败并静默降级 —— 你以为 pin 住了服务端身份, 实际没有。务必到日志确认没有这条 WARN。

两条路都不保存指纹, 也不做跨次连接的一致性比对; 要固定"只认这一台服务端", 就用路一预置服务端证书 (PFX, 无密码).

故障排查​

StatusCode原因解决
BadCertificateUntrusted服务端没信任客户端把 client.der 拷到服务端 %ProgramData%\Darra\PKI\trusted\certs\
BadCertificateInvalid证书过期 / 未生效重新生成
BadCertificateUriInvalid证书 SAN.URI 与 ApplicationUri 不匹配SAN 只能填 URI:urn:darra:opcua:client (ApplicationUri 是写死常量)
BadCertificateHostNameInvalid证书 SAN.DNS 与连接的 host 不匹配生成时加 subjectAltName=DNS:server.example.com
BadSecurityChecksFailed通用安全校验失败看服务端日志查具体原因
BadSecurityModeRejected服务端不支持该 SecurityModeGetEndpoints 看服务端实际支持哪些
BadSecurityPolicyRejected服务端不支持 Basic256Sha256服务端配置开启该 Policy

表内是 OPC UA 规范状态码名. DarraUa_StatusName 只映射常用码, 未收录的返回 "Unknown"; 上表中 BadCertificateInvalid / BadSecurityChecksFailed 会按名打印, 其余按返回的十六进制码比对.

SAN URI 重要性​

OPC UA 校验客户端证书 SAN 中必须有 URI: 字段, 且与 ApplicationUri 一致. 这是防伪造的关键, 必须正确设置.

ApplicationUri 是写死常量, 不是可自定义的

客户端在 CreateSession 里发的 ApplicationUri 硬编码为 urn:darra:opcua:client, DarraUa_SessionConfig 没有任何字段可以改它。所以证书 SAN 的 URI: 只能填这一个值 —— 自己编一个 URI 会被服务端按 "SAN URI 与 ApplicationUri 不一致" 拒掉。

用 OpenSSL 检查:

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

输出应该有:

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

时间同步 (NTP)​

本 SDK 不做本地时间 / 时钟偏差校验

本 SDK 不会返回 UA_STATUS_BAD_CERTIFICATE_TIME_INVALID (0x80140000) / UA_STATUS_BAD_INVALID_TIMESTAMP (0x80230000) 这两个状态码, 因此不存在"偏差超过多少就被拒"的阈值。BadCertificateTimeInvalid 是服务端按它自己的系统时钟判证书有效期产生的。系统时间不准仍要修(否则服务端会判证书过期), 但不要以为 SDK 会拦:

w32tm /resync

高级: 直接调 X509 / RSA / SHA / AES API​

<opcua/opcua_security.h> 提供内置加密接口. 需要用它给业务自己的数据签名时直接调用, 一般业务无需.

下一步​