Skip to content

fix: handle certificate chains returned by OPC UA servers#853

Open
diericd wants to merge 2 commits intogopcua:mainfrom
diericd:feature/support-multiple-certificates
Open

fix: handle certificate chains returned by OPC UA servers#853
diericd wants to merge 2 commits intogopcua:mainfrom
diericd:feature/support-multiple-certificates

Conversation

@diericd
Copy link
Copy Markdown
Contributor

@diericd diericd commented Mar 18, 2026

Summary

Some OPC UA servers — notably Siemens WinCC OA — return a full certificate chain (leaf + intermediate/root CA certificates concatenated as DER) rather than a single DER-encoded certificate. This caused several failures:

  • x509.ParseCertificate fails with "trailing data" when the response contains multiple concatenated certificates
  • Thumbprint hashed the entire chain instead of just the leaf certificate, producing a thumbprint the server didn't recognize
  • A SecurityMode override to SignAndEncrypt in readChunk broke Sign mode by encrypting MSG messages that should only be signed

Changes

  • Add uapolicy.ParseCertificate — a helper that uses x509.ParseCertificates (plural) and returns the first (leaf) certificate. All call sites in uasc/secure_channel.go and uasc/secure_channel_crypto.go now use this instead of x509.ParseCertificate.
  • Fix uapolicy.Thumbprint — parse the DER bytes first and hash only the leaf certificate's raw bytes, with a fallback to hashing the full input if parsing fails.
  • Remove SecurityMode override in readChunk — the isAsymmetric flag already ensures OPN messages use asymmetric encryption regardless of the configured security mode, making the override redundant.

Tested with

  • Siemens WinCC OA server returning certificate chains (leaf + intermediate CA)

diericd added 2 commits March 28, 2026 13:02
Replace x509.ParseCertificate (which fails with "trailing data" when
the server sends a certificate chain) with uapolicy.ParseCertificate,
a new helper that uses x509.ParseCertificates and returns the first
(leaf) certificate.
Remove the SecurityMode override to SignAndEncrypt in readChunk during
OPN response processing. This was likely a workaround from before the
isAsymmetric flag was added to signAndEncrypt/verifyAndDecrypt. The
isAsymmetric check already ensures OPN messages are always encrypted
regardless of the configured security mode, making the override
redundant. It was harmless in SignAndEncrypt mode (a no-op) but broke
Sign mode by causing MSG messages to be encrypted instead of just
signed, resulting in BadSecurityChecksFailed from the server.
@diericd diericd force-pushed the feature/support-multiple-certificates branch from 9151e35 to 9280a6b Compare March 28, 2026 13:34
@diericd diericd changed the title fix: handle certificate chains returned by server fix: handle certificate chains returned by OPC UA servers Mar 28, 2026
@diericd diericd marked this pull request as ready for review March 28, 2026 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant