fix: handle certificate chains returned by OPC UA servers#853
Open
diericd wants to merge 2 commits intogopcua:mainfrom
Open
fix: handle certificate chains returned by OPC UA servers#853diericd wants to merge 2 commits intogopcua:mainfrom
diericd wants to merge 2 commits intogopcua:mainfrom
Conversation
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.
9151e35 to
9280a6b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.ParseCertificatefails with "trailing data" when the response contains multiple concatenated certificatesThumbprinthashed the entire chain instead of just the leaf certificate, producing a thumbprint the server didn't recognizeSecurityModeoverride toSignAndEncryptinreadChunkbrokeSignmode by encrypting MSG messages that should only be signedChanges
uapolicy.ParseCertificate— a helper that usesx509.ParseCertificates(plural) and returns the first (leaf) certificate. All call sites inuasc/secure_channel.goanduasc/secure_channel_crypto.gonow use this instead ofx509.ParseCertificate.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.SecurityModeoverride inreadChunk— theisAsymmetricflag already ensures OPN messages use asymmetric encryption regardless of the configured security mode, making the override redundant.Tested with