Pwn2Own Miami 2022: OPC UA .NET Standard Trusted Application Check Bypass
The OPC UA .NET Standard server built two different certificate chains without verifying they were equal, and an unusual case-insensitive name comparison allowed an attacker to craft a certificate that appeared to be signed by a trusted root. An attacker could bypass the Trusted Application Check and set up a trusted connection to an OPC UA server without a valid certificate.
This write-up is part 1 of a series of write-ups about the 5 vulnerabilities we demonstrated last April at Pwn2Own Miami. This is the write-up for the Trusted Application Check Bypass in the OPC Foundation’s OPC UA .NET Standard (CVE-2022-29865).
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
OPC UA is a communication protocol used in the ICS world. It is an open standard developed by the OPC Foundation, and because many vendors implement it, it is often the preferred protocol for connecting systems from different vendors inside an ICS network.
Security for OPC UA connections can be configured in three ways: no security at all, signing only, or signing and encryption. In the latter two cases both endpoints authenticate to each other using X.509 certificates. These are the same type of certificates used in TLS, but the encryption protocol itself is custom and not based on TLS.
At Pwn2Own Miami 2022, four OPC UA servers were in scope with three payload options:
- Denial-of-Service. Availability is everything in an ICS network, so crashing an OPC UA server can have significant impact.
- Remote code execution. Taking over the server.
- Bypass Trusted Application Check. Setting up a trusted connection to a server without having a valid certificate.
A pre-authentication RCE would of course let an attacker modify the server configuration, lower the security level and bypass the trusted application check that way, but that was not allowed for this category.
The target of this write-up is the OPC UA .NET Standard server, the reference implementation for OPC UA in C#. It is open source, so a bypass here could affect many ICS products that incorporate it as a library. The issue is CVE-2022-29865.
Setup
The researchers looked for trusted certificate bypasses in all four servers in scope and found one, in OPC UA .NET Standard. In the demonstration, the attack was run against a server that itself has a certificate issued by a trusted root certificate.
The only prerequisite for the attack is knowledge of the subject name of one of the trusted root certificates, plus either its serial number or its subject key identifier. Certificates are not secret, so these values should be easy to obtain in practice. In the demonstration, the server's own certificate, issued by the trusted root, supplies the metadata.
Finding it
The core of the issue is the function InternalValidate in CertificateValidator.cs. The logic for verifying a certificate there is quite complicated, which likely contributed to a bug like this being missed. According to the OPC Foundation, the reason the check is so complicated is that they do not want to use the built-in certificate store of Windows. Instead, application certificates are managed by placing certificate files in a specific directory on the server. The OPC UA specification is detailed enough that it even suggests how to store those certificates.
The core problem is that two different certificate chains are built without verifying that they are equal. By crafting a chain in a very specific way, it is possible to make the server accept it even though it is not signed by a trusted root.
862protected virtual async Task InternalValidate(X509Certificate2Collection certificates, ConfiguredEndpoint endpoint)
863{
864 X509Certificate2 certificate = certificates[0];
865
866 // check for previously validated certificate.
867 X509Certificate2 certificate2 = null;
868
869 if (m_validatedCertificates.TryGetValue(certificate.Thumbprint, out certificate2))
870 {
871 if (Utils.IsEqual(certificate2.RawData, certificate.RawData))
872 {
873 return;
874 }
875 }
876
877 CertificateIdentifier trustedCertificate = await GetTrustedCertificate(certificate).ConfigureAwait(false);
878
879 // get the issuers (checks the revocation lists if using directory stores).
880 List<CertificateIdentifier> issuers = new List<CertificateIdentifier>();
881 Dictionary<X509Certificate2, ServiceResultException> validationErrors = new Dictionary<X509Certificate2, ServiceResultException>();
882
883 bool isIssuerTrusted = await GetIssuersNoExceptionsOnGetIssuer(certificates, issuers, validationErrors).ConfigureAwait(false);
884
885 ServiceResult sresult = PopulateSresultWithValidationErrors(validationErrors);
886
887 // setup policy chain
888 X509ChainPolicy policy = new X509ChainPolicy();
889 policy.RevocationFlag = X509RevocationFlag.EntireChain;
890 policy.RevocationMode = X509RevocationMode.NoCheck;
891 policy.VerificationFlags = X509VerificationFlags.NoFlag;
892
893 foreach (CertificateIdentifier issuer in issuers)
894 {
895 if ((issuer.ValidationOptions & CertificateValidationOptions.SuppressRevocationStatusUnknown) != 0)
896 {
897 policy.VerificationFlags |= X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown;
898 policy.VerificationFlags |= X509VerificationFlags.IgnoreCtlSignerRevocationUnknown;
899 policy.VerificationFlags |= X509VerificationFlags.IgnoreEndRevocationUnknown;
900 policy.VerificationFlags |= X509VerificationFlags.IgnoreRootRevocationUnknown;
901 }
902
903 // we did the revocation check in the GetIssuers call. No need here.
904 policy.RevocationMode = X509RevocationMode.NoCheck;
905 policy.ExtraStore.Add(issuer.Certificate);
906 }
907
908 // build chain.
909 using (X509Chain chain = new X509Chain())
910 {
911 chain.ChainPolicy = policy;
912 chain.Build(certificate);
913
914 // check the chain results.
915 CertificateIdentifier target = trustedCertificate;
916
917 if (target == null)
918 {
919 target = new CertificateIdentifier(certificate);
920 }
921
922 for (int ii = 0; ii < chain.ChainElements.Count; ii++)
923 {
924 X509ChainElement element = chain.ChainElements[ii];
925
926 CertificateIdentifier issuer = null;
927
928 if (ii < issuers.Count)
929 {
930 issuer = issuers[ii];
931 }
932
933 // check for chain status errors.
934 if (element.ChainElementStatus.Length > 0)
935 {
936 foreach (X509ChainStatus status in element.ChainElementStatus)
937 {
938 ServiceResult result = CheckChainStatus(status, target, issuer, (ii != 0));
939 if (ServiceResult.IsBad(result))
940 {
941 sresult = new ServiceResult(result, sresult);
942 }
943 }
944 }
945
946 if (issuer != null)
947 {
948 target = issuer;
949 }
950 }
951 }
952[...]
On line 883, GetIssuersNoExceptionsOnGetIssuer constructs a certificate chain for the certificate being validated, returned in the out variable issuers. The function works in a loop. In each iteration it tries to find the issuer of the current certificate, consulting three locations:
- The list of trusted certificates stored on the server. If the issuer is found there, the function returns
true. - The list of issuer certificates stored on the server. These are not explicitly trusted, but can be used to build a chain to a trusted root.
- The list of additional certificates sent by the client. As in TLS, the OPC UA handshake can carry extra certificates.
If an issuer is found, it becomes the current certificate and the loop continues until the current certificate is self-signed or no issuer can be found.
To find the issuer, the function Match compares the issuer name of the certificate with the subject name of each potential issuer. In addition, the serial number or the subject key identifier must match. The cryptographic signature is not considered at this stage, so the match is based only on forgeable certificate metadata.
The name comparison in Match is implemented in CompareDistinguishedName, and that implementation is unusual. It decomposes the name into components and then does a case-insensitive match on each component. Most implementations do not compare X.509 names this way.
Next, on line 912, an X509Chain object is used. The intent appears to be to verify that the chain built by GetIssuersNoExceptionsOnGetIssuer is cryptographically valid. However, because it is not configured with the root certificates used by the application, it will often produce errors. So on line 938 the function CheckChainStatus is used to ignore certain types of errors. For example, an UntrustedRoot error is ignored if it occurred for the certificate at the root.
The vulnerability is that nothing verifies that the chain built by GetIssuersNoExceptionsOnGetIssuer and the one built by X509Chain.Build are equal. By abusing the unusual name comparison it is possible to construct a certificate such that both functions produce a different chain. By making sure the errors in the second chain only occur where CheckChainStatus ignores them, the certificate gets accepted by the server.
Exploitation
Step 1: learn the trusted root metadata
Suppose the server is configured to trust a certificate with the following details:
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 9891791597891487306 (0x8946b40ca084064a)
Signature Algorithm: sha1WithRSAEncryption
Issuer: CN=Root
Validity
Not Before: Feb 24 09:35:53 2022 GMT
Not After : Feb 24 09:35:53 2023 GMT
Subject: CN=Root
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Authority Key Identifier:
DirName:/CN=Root
serial:89:46:B4:0C:A0:84:06:4A
X509v3 Basic Constraints:
CA:TRUE
X509v3 Key Usage:
Certificate Sign, CRL Sign
Signature Algorithm: sha1WithRSAEncryption
[...]
And suppose the OPC server itself is configured with the following certificate, issued from this root:
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
[redacted]:[redacted]:[redacted]
Signature Algorithm: sha1WithRSAEncryption
Issuer: CN=Root
Validity
Not Before: Feb 24 09:35:53 2022 GMT
Not After : Mar 26 09:35:53 2022 GMT
Subject: CN=Quickstart Reference Server, C=US, ST=Arizona, O=OPC Foundation, DC=opcserver
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Authority Key Identifier:
DirName:/CN=Root
serial:89:46:B4:0C:A0:84:06:4A
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Key Encipherment, Data Encipherment, Key Agreement
X509v3 Subject Alternative Name:
DNS:opcserver, URI:URI:urn:opcserver
Signature Algorithm: sha1WithRSAEncryption
[...]
The attacker connects to the server, obtains this certificate, and uses the data in the Issuer and X509v3 Authority Key Identifier fields to craft two new certificates.
Step 2: create a fake root with a case-flipped name
The attacker generates a new root certificate that uses the same common name as the trusted root, but with every letter's case flipped (upper case to lower case and lower case to upper case). This certificate is self-signed and must contain the CA=TRUE basic constraint. The attacker makes it available for download as a PEM file over HTTP at http://attacker/root.pem.
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
[redacted]:[redacted]:[redacted]
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=rOOT
Validity
Not Before: Feb 17 10:40:24 2022 GMT
Not After : May 25 10:40:24 2022 GMT
Subject: CN=rOOT
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (3072 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:TRUE
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment, Data Encipherment, Key Agreement, Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
[...]
Step 3: create the leaf certificate
The attacker generates a new leaf certificate, signed with the fake root just created. The following fields are added to it:
- The issuer contains the subject name of the fake root.
- The X509v3 Authority Key Identifier extension contains a directory name of the fake root and a serial number of the real trusted root.
- The certificate contains an Authority Information Access extension with a CA Issuers field containing the URL where the fake root PEM file can be downloaded.
All other fields, such as Subject and Subject Alternative Name, can contain any data the attacker chooses. To pass the remaining checks in InternalValidate, the validity period should contain the current time and the keyUsage field should contain Data Encipherment. A Subject Alternative Name extension can be added if the domain is checked.
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
[redacted]:[redacted]:[redacted]
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=rOOT
Validity
Not Before: Feb 17 10:40:24 2022 GMT
Not After : May 25 10:40:24 2022 GMT
Subject: CN=FakeCert
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (3072 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Authority Key Identifier:
DirName:/CN=rOOT
serial:89:46:B4:0C:A0:84:06:4A
X509v3 Basic Constraints:
CA:FALSE
Authority Information Access:
CA Issuers - URI:http://attacker/root.pem
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment, Data Encipherment, Key Agreement, Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
[...]
Step 4: connect and watch the two chains diverge
When the attacker connects with the CN=FakeCert certificate, GetIssuersNoExceptionsOnGetIssuer looks in its trusted certificate store for the issuer of the certificate. It compares the Issuer name of the received certificate with the Subject name of the certificates in the store, by decomposing the distinguished name, sorting the components, and doing a case-insensitive match on each component.
So it compares the common name of the issuer from the certificate:
CN=rOOT
with the common name of the subject of the trusted certificate:
CN=Root
It also compares the serial number of the root certificate with the serial number in the authority key identifier extension, which are equal:
Serial Number: 9891791597891487306 (0x8946b40ca084064a)
X509v3 Authority Key Identifier:
DirName:/CN=rOOT
serial:89:46:B4:0C:A0:84:06:4A
The function therefore considers the CN=Root certificate a match. The signature would show that the certificate is not correctly signed, but that is not checked yet. It obtains a chain with one issuer and isIssuerTrusted is true.
Then it creates an X509Chain object and calls chain.Build(certificate). The result code of this call is ignored, and so is the global status of the result. Only the statuses of the individual chain elements are checked.
Because chain.Build does a literal comparison of the subject of the trusted root with the issuer of FakeCert, it does not consider the CN=Root certificate to be the issuer of FakeCert (it looks for CN=rOOT). The serial number from the Authority Key Identifier extension matches, but that is not sufficient for a match.
Since it cannot find the issuer certificate in its trust store, it uses the CA Issuers URL from the Authority Information Access extension to download the certificate from the webserver. The result of chain.Build() is then a chain of two certificates, where the second one carries the error UntrustedRoot. CheckChainStatus ignores this error because it incorrectly assumes the corresponding certificate was one of its trusted certificates, while it is in fact the CN=rOOT certificate.
The rest of the checks in InternalValidate now succeed, because issuedByCA is true and isIssuerTrusted is true. The key usage, endpoint domain, use of SHA1 and minimum key size checks can all be passed because the attacker has full control over the contents of FakeCert.
The exploit in action can be seen in the video below:
Root cause
Two certificate chains are built and never compared. GetIssuersNoExceptionsOnGetIssuer builds one using Match, which compares names case-insensitively and matches on forgeable metadata (issuer name plus serial number or subject key identifier) without checking the signature. X509Chain.Build builds another using the platform's literal name comparison and, when it cannot find the issuer locally, fetches it from the CA Issuers URL in the Authority Information Access extension.
The second chain is not configured with the application's root certificates, so it produces errors that CheckChainStatus is designed to ignore, including UntrustedRoot at the root. The code assumes that error refers to a certificate from its own trusted store, but here it refers to the attacker's CN=rOOT certificate. Because the two chains are never checked for equality, the attacker's certificate is accepted even though it is not signed by a trusted root.
The underlying design choice, avoiding the Windows certificate store in favour of a custom directory-based store, led to complex and non-idiomatic validation code. By abusing the unusual case-insensitive name comparison, an attacker can make the two chains diverge.
Impact
The vulnerability bypasses the Trusted Application Check against the reference server included in the OPC UA .NET Standard repository. It would also be possible to bypass the check on the client side to impersonate a server.
OPC UA also has "User Authentication", which happens after the Trusted Application Check to establish a session. One option for User Authentication is an X.509 certificate, which could be bypassed in the same way.
In most places in practice the OPC UA server would not be exposed to the public internet, so exploiting this issue requires an attacker to already have access to an internal ICS network. In the rare cases where exposing an OPC UA server to the public internet is unavoidable, enabling certificate authentication would be the most effective way to secure it, and this check could then be bypassed to gain access to the communication.
Once connected to an OPC UA server, the attacker can read and write data, which could be used to disrupt the ICS processes that use the server.
Fix
The issues were fixed in commit [redacted] and assigned CVE-2022-29865. Names are now compared the same way as in other X.509 implementations: no case-insensitive check and no resorting of name components. In addition, defensive checks were added to make sure the two certificate chains that are used are equal.
The authors note that the CustomTrustStore property and the TrustMode=CustomRootTrust setting on an X509ChainPolicy object appear to offer the required functionality without depending on the Windows certificate store, and they are unsure whether avoiding the Windows store is truly the only way to implement this in .NET.
Variants to test
- Case-flipped names against other custom validators. Look for certificate validation code that normalises or case-folds distinguished names before matching. A positive result is a certificate whose issuer differs only in letter case from a trusted subject being treated as issued by that trusted certificate.
- Authority Key Identifier serial reuse. Craft a leaf whose Authority Key Identifier carries the directory name of your own root but the serial number of a real trusted root. A positive result is the validator accepting the leaf as chained to the trusted root without verifying the signature at that stage.
- Authority Information Access fetching. Point the CA Issuers URL at a server you control and watch for an outbound request. A positive result is the validator downloading your certificate and using it to complete a chain it then accepts.
- Ignored chain statuses. Feed certificates that trigger
UntrustedRootor revocation-unknown errors at the root position and see whether they are silently ignored. A positive result is acceptance despite the root not being in the trust store. - Client-side and user-authentication paths. Repeat the same crafted certificate against the client role and against X.509-based User Authentication. A positive result is impersonation of a server or establishment of a session without a valid certificate.
What happened, in plain words
A team of security researchers found a way to trick an industrial server program into trusting a certificate that it should not trust. The server is the OPC UA .NET Standard server, a piece of software written in C# that many industrial products use as a building block. The trick let them open a "trusted" connection to the server without owning a certificate that the server had actually approved. They demonstrated it at a hacking contest called Pwn2Own Miami 2022, in April 2022, and the organisers awarded them 40,000 US dollars and 40 "Master of Pwn" points. The bug was later fixed and given the identifier CVE-2022-29865.

The image above is the contest organiser's announcement of the result.
To understand the trick you need three ideas: what OPC UA is, what a certificate is, and what a "trusted application check" does. Each is explained below as it comes up.
Context
What OPC UA is
OPC UA is a communication protocol. A protocol is simply an agreed set of rules for two programs to talk to each other, the way two people agree to speak English on the phone. OPC UA is used in the ICS world. ICS stands for Industrial Control Systems: the computers and networks that run factories, water plants, power grids and similar physical processes. In such a network, machines from many different manufacturers need to exchange data, and OPC UA is a common language they all speak.
OPC UA is an open standard developed by the OPC Foundation. "Open standard" means the rules are published and anyone may implement them, so many vendors do. That is exactly why it is popular for connecting systems from different vendors inside one ICS network.
The three security levels
Security for OPC UA connections can be configured in three ways:
- No security at all. Anyone who can reach the server can talk to it.
- Signing only. Messages carry a cryptographic signature so the receiver can tell they were not altered and came from the holder of a particular key.
- Signing and encryption. Messages are also scrambled so nobody in the middle can read them.
In the second and third cases, both endpoints authenticate to each other using X.509 certificates. A certificate is a small file that binds a public key to a name, and it is digitally signed by someone else, called a certificate authority (CA), who vouches for that binding. Think of it as a passport: it carries your photo and details, and it is stamped by an authority that others are willing to believe. The same type of certificate is used in TLS, the protocol that puts the padlock in your browser. The encryption protocol inside OPC UA, however, is custom and not based on TLS.
The contest categories
At Pwn2Own Miami 2022, four OPC UA servers were in scope, meaning they were the allowed targets. Contestants could choose one of three "payload" options, that is, three kinds of result they were allowed to demonstrate:
- Denial-of-Service. Availability is everything in an ICS network, so being able to crash an OPC UA server can have significant impact. A denial-of-service attack does not steal data; it makes the service stop working.
- Remote code execution. Being able to take over the server, that is, to run your own commands on it.
- Bypass Trusted Application Check. Setting up a trusted connection to a server without having a valid certificate. This is the category the researchers chose.
A pre-authentication RCE (remote code execution reachable before you log in) would of course let an attacker modify the server configuration, lower the security level and bypass the trusted application check that way. But that shortcut was not allowed for this category, so the bypass had to be done honestly, through the certificate check itself.
The target
The target of this write-up is the OPC UA .NET Standard server. It is the reference implementation for OPC UA in C#. "Reference implementation" means the OPC Foundation publishes it as the example of how the standard should be implemented. It is open source, so anyone can read its code. That matters because a bypass here could affect many ICS products that incorporate it as a library. A library is a bundle of code that other programs include and reuse, so a flaw in the library is inherited by every program that uses it.
Setup
The researchers looked for trusted certificate bypasses in all four servers in scope and found one, in OPC UA .NET Standard. In the demonstration, the attack was run against a server that itself has a certificate issued by a trusted root certificate, and that fact becomes useful later.
The only prerequisite for the attack is knowledge of the subject name of one of the trusted root certificates, plus either its serial number or its subject key identifier. These are all pieces of text written inside a certificate. Certificates are not secret, so these values should be easy to obtain in practice. In the demonstration, the server's own certificate, issued by the trusted root, supplies the metadata. Metadata here means descriptive data about the certificate, not the secret key itself.
Finding it
The core of the issue is the function InternalValidate in CertificateValidator.cs. A function is a named block of code that does one job; this one's job is to decide whether a certificate presented by a connecting party should be accepted. The logic for verifying a certificate there is quite complicated, which likely contributed to a bug like this being missed. According to the OPC Foundation, the reason the check is so complicated is that they do not want to use the built-in certificate store of Windows. The Windows certificate store is the central list of trusted certificates that Windows itself maintains. Instead, application certificates are managed by placing certificate files in a specific directory on the server. The OPC UA specification is detailed enough that it even suggests how to store those certificates.
The core problem is that two different certificate chains are built without verifying that they are equal. A certificate chain is a sequence of certificates: your own certificate at the bottom, then the certificate of whoever signed it, then whoever signed that, and so on up to a root certificate that is trusted by definition. If two parts of the same program each build a chain and never compare the results, an attacker may be able to make one chain look good and the other chain look harmless. By crafting a chain in a very specific way, it is possible to make the server accept it even though it is not signed by a trusted root.
862protected virtual async Task InternalValidate(X509Certificate2Collection certificates, ConfiguredEndpoint endpoint)
863{
864 X509Certificate2 certificate = certificates[0];
865
866 // check for previously validated certificate.
867 X509Certificate2 certificate2 = null;
868
869 if (m_validatedCertificates.TryGetValue(certificate.Thumbprint, out certificate2))
870 {
871 if (Utils.IsEqual(certificate2.RawData, certificate.RawData))
872 {
873 return;
874 }
875 }
876
877 CertificateIdentifier trustedCertificate = await GetTrustedCertificate(certificate).ConfigureAwait(false);
878
879 // get the issuers (checks the revocation lists if using directory stores).
880 List<CertificateIdentifier> issuers = new List<CertificateIdentifier>();
881 Dictionary<X509Certificate2, ServiceResultException> validationErrors = new Dictionary<X509Certificate2, ServiceResultException>();
882
883 bool isIssuerTrusted = await GetIssuersNoExceptionsOnGetIssuer(certificates, issuers, validationErrors).ConfigureAwait(false);
884
885 ServiceResult sresult = PopulateSresultWithValidationErrors(validationErrors);
886
887 // setup policy chain
888 X509ChainPolicy policy = new X509ChainPolicy();
889 policy.RevocationFlag = X509RevocationFlag.EntireChain;
890 policy.RevocationMode = X509RevocationMode.NoCheck;
891 policy.VerificationFlags = X509VerificationFlags.NoFlag;
892
893 foreach (CertificateIdentifier issuer in issuers)
894 {
895 if ((issuer.ValidationOptions & CertificateValidationOptions.SuppressRevocationStatusUnknown) != 0)
896 {
897 policy.VerificationFlags |= X509VerificationFlags.IgnoreCertificateAuthorityRevocationUnknown;
898 policy.VerificationFlags |= X509VerificationFlags.IgnoreCtlSignerRevocationUnknown;
899 policy.VerificationFlags |= X509VerificationFlags.IgnoreEndRevocationUnknown;
900 policy.VerificationFlags |= X509VerificationFlags.IgnoreRootRevocationUnknown;
901 }
902
903 // we did the revocation check in the GetIssuers call. No need here.
904 policy.RevocationMode = X509RevocationMode.NoCheck;
905 policy.ExtraStore.Add(issuer.Certificate);
906 }
907
908 // build chain.
909 using (X509Chain chain = new X509Chain())
910 {
911 chain.ChainPolicy = policy;
912 chain.Build(certificate);
913
914 // check the chain results.
915 CertificateIdentifier target = trustedCertificate;
916
917 if (target == null)
918 {
919 target = new CertificateIdentifier(certificate);
920 }
921
922 for (int ii = 0; ii < chain.ChainElements.Count; ii++)
923 {
924 X509ChainElement element = chain.ChainElements[ii];
925
926 CertificateIdentifier issuer = null;
927
928 if (ii < issuers.Count)
929 {
930 issuer = issuers[ii];
931 }
932
933 // check for chain status errors.
934 if (element.ChainElementStatus.Length > 0)
935 {
936 foreach (X509ChainStatus status in element.ChainElementStatus)
937 {
938 ServiceResult result = CheckChainStatus(status, target, issuer, (ii != 0));
939 if (ServiceResult.IsBad(result))
940 {
941 sresult = new ServiceResult(result, sresult);
942 }
943 }
944 }
945
946 if (issuer != null)
947 {
948 target = issuer;
949 }
950 }
951 }
952[...]
Line by line, in plain words:
- Line 862 declares the function. It receives a collection of certificates (the ones the connecting party sent) and the endpoint being connected to.
async Taskmeans it runs asynchronously, that is, it can pause while waiting for slow work such as reading files or the network. - Line 864 takes the first certificate in the collection. That is the certificate of the party connecting, the one that must be judged.
- Lines 866 to 875 are a shortcut: if this exact certificate was validated before and is byte-for-byte identical to the stored copy, return immediately.
Thumbprintis a short fingerprint of the certificate, andRawDatais the whole certificate file. - Line 877 looks up whether this certificate itself is one of the trusted certificates.
- Lines 879 to 881 prepare two empty containers:
issuerswill hold the chain of issuers found, andvalidationErrorswill hold any errors met along the way. - Line 883 is the first of the two chain builds.
GetIssuersNoExceptionsOnGetIssuerfillsissuersand returns a true or false inisIssuerTrusted, meaning "did we reach a certificate we trust?". - Line 885 collects the errors from that first step into a result object.
- Lines 887 to 891 set up the rules for the second chain build: check revocation for the whole chain, do not check revocation here, and do not ignore any verification flags by default. Revocation is the process of checking whether a certificate has been cancelled before its expiry date.
- Lines 893 to 906 loop over the issuers found in the first step and add each one to the second chain's extra store, so the second build has them available. Lines 895 to 901 add flags to ignore "revocation status unknown" errors when the issuer's settings ask for that.
- Lines 908 to 912 create the second chain object and build it for the same certificate. Note that the return value of
chain.Build(certificate)is not stored anywhere: the code ignores whether the build as a whole succeeded. - Lines 914 to 920 pick a starting point for the error check: the trusted certificate if one was found, otherwise the certificate itself.
- Lines 922 to 950 walk through every element of the chain that was just built. For each element, line 938 asks
CheckChainStatuswhether this particular error should be ignored. If the answer is that the error is bad, it is added to the overall result. Line 946 moves the "target" pointer along as the loop advances.
On line 883, GetIssuersNoExceptionsOnGetIssuer constructs a certificate chain for the certificate being validated, returned in the out variable issuers. The function works in a loop. In each iteration it tries to find the issuer of the current certificate, consulting three locations:
- The list of trusted certificates stored on the server. If the issuer is found there, the function returns
true. - The list of issuer certificates stored on the server. These are not explicitly trusted, but can be used to build a chain to a trusted root.
- The list of additional certificates sent by the client. As in TLS, the OPC UA handshake can carry extra certificates. A handshake is the opening exchange where two programs agree how to talk and prove who they are.
If an issuer is found, it becomes the current certificate and the loop continues until the current certificate is self-signed or no issuer can be found. Self-signed means the certificate is signed by its own key, which is normal for a root certificate.
To find the issuer, the function Match compares the issuer name of the certificate with the subject name of each potential issuer. In addition, the serial number or the subject key identifier must match. The cryptographic signature is not considered at this stage, so the match is based only on forgeable certificate metadata. Forgeable means an attacker can write whatever they like into those fields when creating their own certificate; nothing proves the values are true until a signature is checked.
The name comparison in Match is implemented in CompareDistinguishedName, and that implementation is unusual. It decomposes the name into components and then does a case-insensitive match on each component. Case-insensitive means that Root and rOOT are treated as the same word. Most implementations do not compare X.509 names this way. A distinguished name is the full name inside a certificate, made of components such as CN (common name) and O (organisation).
Next, on line 912, an X509Chain object is used. The intent appears to be to verify that the chain built by GetIssuersNoExceptionsOnGetIssuer is cryptographically valid. However, because it is not configured with the root certificates used by the application, it will often produce errors. So on line 938 the function CheckChainStatus is used to ignore certain types of errors. For example, an UntrustedRoot error is ignored if it occurred for the certificate at the root. UntrustedRoot means "this chain ends in a root that I do not recognise as trusted".
The vulnerability is that nothing verifies that the chain built by GetIssuersNoExceptionsOnGetIssuer and the one built by X509Chain.Build are equal. By abusing the unusual name comparison it is possible to construct a certificate such that both functions produce a different chain. By making sure the errors in the second chain only occur where CheckChainStatus ignores them, the certificate gets accepted by the server.
Exploitation
Step 1: learn the trusted root metadata
Suppose the server is configured to trust a certificate with the following details:
Certificate:
Data:
Version: 3 (0x2)
Serial Number: 9891791597891487306 (0x8946b40ca084064a)
Signature Algorithm: sha1WithRSAEncryption
Issuer: CN=Root
Validity
Not Before: Feb 24 09:35:53 2022 GMT
Not After : Feb 24 09:35:53 2023 GMT
Subject: CN=Root
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Authority Key Identifier:
DirName:/CN=Root
serial:89:46:B4:0C:A0:84:06:4A
X509v3 Basic Constraints:
CA:TRUE
X509v3 Key Usage:
Certificate Sign, CRL Sign
Signature Algorithm: sha1WithRSAEncryption
[...]
This is a certificate printed in text form. Version: 3 is the certificate format version. Serial Number is a unique number the issuer gave this certificate. Signature Algorithm says which mathematical method was used to sign it. Issuer is who signed it; here the certificate is self-signed, so issuer and subject are both CN=Root. Validity gives the dates between which the certificate is valid. Subject is who the certificate belongs to. Subject Public Key Info contains the public key, the half of a key pair that may be shared. X509v3 extensions are extra fields: Authority Key Identifier points back to the key of the issuer, Basic Constraints: CA:TRUE says this certificate is allowed to sign other certificates, and Key Usage lists what the key may be used for.
And suppose the OPC server itself is configured with the following certificate, issued from this root:
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
[redacted]:[redacted]:[redacted]
Signature Algorithm: sha1WithRSAEncryption
Issuer: CN=Root
Validity
Not Before: Feb 24 09:35:53 2022 GMT
Not After : Mar 26 09:35:53 2022 GMT
Subject: CN=Quickstart Reference Server, C=US, ST=Arizona, O=OPC Foundation, DC=opcserver
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (2048 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Authority Key Identifier:
DirName:/CN=Root
serial:89:46:B4:0C:A0:84:06:4A
X509v3 Basic Constraints:
CA:FALSE
X509v3 Key Usage:
Digital Signature, Key Encipherment, Data Encipherment, Key Agreement
X509v3 Subject Alternative Name:
DNS:opcserver, URI:URI:urn:opcserver
Signature Algorithm: sha1WithRSAEncryption
[...]
This second certificate belongs to the server itself. Its Issuer is CN=Root, so it was signed by the root above. Its Subject names the Quickstart Reference Server. Basic Constraints: CA:FALSE says it may not sign other certificates. Subject Alternative Name lists other names the certificate is valid for, here a DNS name and a URI. The Authority Key Identifier repeats the root's name and serial number, which is the metadata the attacker needs.
The attacker connects to the server, obtains this certificate, and uses the data in the Issuer and X509v3 Authority Key Identifier fields to craft two new certificates. Why? Because those fields tell the attacker exactly which root the server trusts and how that root is identified, and the attacker needs that to make a fake certificate look related to it.
Step 2: create a fake root with a case-flipped name
The attacker generates a new root certificate that uses the same common name as the trusted root, but with every letter's case flipped (upper case to lower case and lower case to upper case). This certificate is self-signed and must contain the CA=TRUE basic constraint. The attacker makes it available for download as a PEM file over HTTP at http://attacker/root.pem. PEM is just a text format for storing a certificate in a file.
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
[redacted]:[redacted]:[redacted]
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=rOOT
Validity
Not Before: Feb 17 10:40:24 2022 GMT
Not After : May 25 10:40:24 2022 GMT
Subject: CN=rOOT
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (3072 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Basic Constraints:
CA:TRUE
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment, Data Encipherment, Key Agreement, Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
[...]
Here the subject is CN=rOOT, the case-flipped version of CN=Root. It is self-signed, so issuer and subject match. CA:TRUE allows it to sign other certificates. The key is 3072 bits, larger than the 2048-bit key of the real root, and the signature algorithm is SHA-256 rather than SHA-1. The attacker controls all of this because they generated the certificate themselves.
Step 3: create the leaf certificate
The attacker generates a new leaf certificate, signed with the fake root just created. A leaf certificate is the one at the bottom of a chain, belonging to the party that actually connects. The following fields are added to it:
- The issuer contains the subject name of the fake root.
- The X509v3 Authority Key Identifier extension contains a directory name of the fake root and a serial number of the real trusted root.
- The certificate contains an Authority Information Access extension with a CA Issuers field containing the URL where the fake root PEM file can be downloaded.
All other fields, such as Subject and Subject Alternative Name, can contain any data the attacker chooses. To pass the remaining checks in InternalValidate, the validity period should contain the current time and the keyUsage field should contain Data Encipherment. A Subject Alternative Name extension can be added if the domain is checked.
Certificate:
Data:
Version: 3 (0x2)
Serial Number:
[redacted]:[redacted]:[redacted]
Signature Algorithm: sha256WithRSAEncryption
Issuer: CN=rOOT
Validity
Not Before: Feb 17 10:40:24 2022 GMT
Not After : May 25 10:40:24 2022 GMT
Subject: CN=FakeCert
Subject Public Key Info:
Public Key Algorithm: rsaEncryption
Public-Key: (3072 bit)
Modulus:
[...]
Exponent: 65537 (0x10001)
X509v3 extensions:
X509v3 Authority Key Identifier:
DirName:/CN=rOOT
serial:89:46:B4:0C:A0:84:06:4A
X509v3 Basic Constraints:
CA:FALSE
Authority Information Access:
CA Issuers - URI:http://attacker/root.pem
X509v3 Key Usage:
Digital Signature, Non Repudiation, Key Encipherment, Data Encipherment, Key Agreement, Certificate Sign, CRL Sign
Signature Algorithm: sha256WithRSAEncryption
[...]
This is the certificate the attacker will present to the server. Its Issuer is CN=rOOT, the fake root. Its Authority Key Identifier mixes two things: the directory name of the fake root (DirName:/CN=rOOT) and the serial number of the real trusted root (serial:89:46:B4:0C:A0:84:06:4A). That mixture is the heart of the trick. The Authority Information Access field tells any program that needs the issuer certificate where to download it, namely http://attacker/root.pem, a server the attacker controls. The subject is CN=FakeCert, a name of the attacker's choosing.
Step 4: connect and watch the two chains diverge
When the attacker connects with the CN=FakeCert certificate, GetIssuersNoExceptionsOnGetIssuer looks in its trusted certificate store for the issuer of the certificate. It compares the Issuer name of the received certificate with the Subject name of the certificates in the store, by decomposing the distinguished name, sorting the components, and doing a case-insensitive match on each component.
So it compares the common name of the issuer from the certificate:
CN=rOOT
with the common name of the subject of the trusted certificate:
CN=Root
Because the comparison ignores case, these two are treated as the same. It also compares the serial number of the root certificate with the serial number in the authority key identifier extension, which are equal:
Serial Number: 9891791597891487306 (0x8946b40ca084064a)
X509v3 Authority Key Identifier:
DirName:/CN=rOOT
serial:89:46:B4:0C:A0:84:06:4A
The function therefore considers the CN=Root certificate a match. The signature would show that the certificate is not correctly signed, but that is not checked yet. It obtains a chain with one issuer and isIssuerTrusted is true. In other words, the first chain build has been fooled into thinking the attacker's certificate was issued by the trusted root.
Then it creates an X509Chain object and calls chain.Build(certificate). The result code of this call is ignored, and so is the global status of the result. Only the statuses of the individual chain elements are checked.
Because chain.Build does a literal comparison of the subject of the trusted root with the issuer of FakeCert, it does not consider the CN=Root certificate to be the issuer of FakeCert (it looks for CN=rOOT). Literal here means exact, character for character, so Root and rOOT are different. The serial number from the Authority Key Identifier extension matches, but that is not sufficient for a match.
Since it cannot find the issuer certificate in its trust store, it uses the CA Issuers URL from the Authority Information Access extension to download the certificate from the webserver. The result of chain.Build() is then a chain of two certificates, where the second one carries the error UntrustedRoot. CheckChainStatus ignores this error because it incorrectly assumes the corresponding certificate was one of its trusted certificates, while it is in fact the CN=rOOT certificate.
The rest of the checks in InternalValidate now succeed, because issuedByCA is true and isIssuerTrusted is true. The key usage, endpoint domain, use of SHA1 and minimum key size checks can all be passed because the attacker has full control over the contents of FakeCert.
The exploit in action can be seen in the video below:
Root cause
Two certificate chains are built and never compared. GetIssuersNoExceptionsOnGetIssuer builds one using Match, which compares names case-insensitively and matches on forgeable metadata (issuer name plus serial number or subject key identifier) without checking the signature. X509Chain.Build builds another using the platform's literal name comparison and, when it cannot find the issuer locally, fetches it from the CA Issuers URL in the Authority Information Access extension.
The second chain is not configured with the application's root certificates, so it produces errors that CheckChainStatus is designed to ignore, including UntrustedRoot at the root. The code assumes that error refers to a certificate from its own trusted store, but here it refers to the attacker's CN=rOOT certificate. Because the two chains are never checked for equality, the attacker's certificate is accepted even though it is not signed by a trusted root.
The underlying design choice, avoiding the Windows certificate store in favour of a custom directory-based store, led to complex and non-idiomatic validation code. Re-implementing means writing your own version of something the platform already provides. By abusing the unusual case-insensitive name comparison, an attacker can make the two chains diverge.
Impact
The vulnerability bypasses the Trusted Application Check against the reference server included in the OPC UA .NET Standard repository. It would also be possible to bypass the check on the client side to impersonate a server. Impersonation means pretending to be a party you are not, so that the other side talks to you believing you are the real server.
OPC UA also has "User Authentication", which happens after the Trusted Application Check to establish a session. A session is the ongoing conversation between two programs after they have agreed to talk. One option for User Authentication is an X.509 certificate, which could be bypassed in the same way.
In most places in practice the OPC UA server would not be exposed to the public internet, so exploiting this issue requires an attacker to already have access to an internal ICS network. In the rare cases where exposing an OPC UA server to the public internet is unavoidable, enabling certificate authentication would be the most effective way to secure it, and this check could then be bypassed to gain access to the communication.
Once connected to an OPC UA server, the attacker can read and write data, which could be used to disrupt the ICS processes that use the server.
Fix
The issues were fixed in commit [redacted] and assigned CVE-2022-29865. A commit is a recorded change in a code repository. Names are now compared the same way as in other X.509 implementations: no case-insensitive check and no resorting of name components. In addition, defensive checks were added to make sure the two certificate chains that are used are equal.
The authors note that the CustomTrustStore property and the TrustMode=CustomRootTrust setting on an X509ChainPolicy object appear to offer the required functionality without depending on the Windows certificate store, and they are unsure whether avoiding the Windows store is truly the only way to implement this in .NET.
Variants to test
- Case-flipped names against other custom validators. Look for certificate validation code that normalises or case-folds distinguished names before matching. A positive result is a certificate whose issuer differs only in letter case from a trusted subject being treated as issued by that trusted certificate.
- Authority Key Identifier serial reuse. Craft a leaf whose Authority Key Identifier carries the directory name of your own root but the serial number of a real trusted root. A positive result is the validator accepting the leaf as chained to the trusted root without verifying the signature at that stage.
- Authority Information Access fetching. Point the CA Issuers URL at a server you control and watch for an outbound request. A positive result is the validator downloading your certificate and using it to complete a chain it then accepts.
- Ignored chain statuses. Feed certificates that trigger
UntrustedRootor revocation-unknown errors at the root position and see whether they are silently ignored. A positive result is acceptance despite the root not being in the trust store. - Client-side and user-authentication paths. Repeat the same crafted certificate against the client role and against X.509-based User Authentication. A positive result is impersonation of a server or establishment of a session without a valid certificate.
Glossary
- API: an agreed way for one program to ask another program to do something, like a menu of allowed requests.
- Authority Information Access: a field in a certificate that tells a program where to download related certificates, such as the issuer's.
- Authority Key Identifier: a field in a certificate that points back to the key of the certificate that signed it.
- Basic Constraints: a field in a certificate that says whether it may sign other certificates (CA:TRUE) or not (CA:FALSE).
- CA (certificate authority): an entity that signs certificates and vouches for the link between a name and a public key.
- Certificate: a small signed file that binds a public key to a name, used to prove identity.
- Certificate chain: a sequence of certificates from a leaf up through its issuers to a trusted root.
- Certificate store: a collection of certificates kept by an operating system or application, including the ones it trusts.
- CVE: a public identifier for a known security vulnerability.
- Denial-of-Service: an attack that makes a service stop working rather than stealing data.
- Distinguished name: the full name inside a certificate, made of components such as CN and O.
- DNS: the naming system that turns names like
opcserverinto network addresses. - Encryption: scrambling data so that only the intended receiver can read it.
- Handshake: the opening exchange where two programs agree how to talk and prove who they are.
- ICS: Industrial Control Systems, the computers and networks that run physical processes such as factories and power grids.
- Impersonation: pretending to be a party you are not.
- Issuer: the entity that signed a certificate.
- Key Usage: a field in a certificate that lists what its key may be used for.
- Leaf certificate: the certificate at the bottom of a chain, belonging to the party that actually connects.
- Library: a bundle of code that other programs include and reuse.
- Metadata: descriptive data about something, as opposed to secret data.
- OPC UA: an open communication protocol used in industrial control systems.
- PEM: a text format for storing certificates and keys in a file.
- Pre-authentication: reachable before a party has logged in or been authenticated.
- Protocol: an agreed set of rules for two programs to communicate.
- Public key: the half of a key pair that may be shared; the other half is kept secret.
- Pwn2Own: a hacking contest where researchers demonstrate working exploits against real products.
- RCE (remote code execution): running your own commands on someone else's machine over a network.
- Reference implementation: the published example of how a standard should be implemented.
- Revocation: cancelling a certificate before its expiry date.
- Root certificate: the certificate at the top of a chain, trusted by definition.
- Self-signed: a certificate signed by its own key, normal for a root.
- Serial number: a unique number an issuer gives a certificate.
- Session: the ongoing conversation between two programs after they have agreed to talk.
- SHA-1 / SHA-256: mathematical hash functions used inside signatures; SHA-1 is older and weaker.
- Signature: a cryptographic value that proves a message or certificate came from the holder of a particular key and was not altered.
- Subject: the entity a certificate belongs to.
- Subject Alternative Name: a field listing other names a certificate is valid for, such as DNS names and URIs.
- Subject key identifier: a short value identifying a certificate's key, used to link certificates together.
- TLS: the protocol that secures connections on the web, shown by the padlock in a browser.
- Trusted Application Check: the step in OPC UA where a server decides whether to trust the certificate of a connecting application.
- URI: a string that identifies a resource, such as a web address.
- User Authentication: the step in OPC UA, after the Trusted Application Check, where a user proves who they are to establish a session.
- X.509: the standard format for public key certificates.
- X509Chain: the .NET class that builds and checks a certificate chain.