Skip to content
Crypto Write-up Advanced

Exploiting a Critical Spoofing Vulnerability in Windows CryptoAPI

Windows CryptoAPI trusted a certificate if its MD5 thumbprint matched one already in the cache, without comparing the full certificate contents. An attacker could use an MD5 chosen-prefix collision to make a malicious certificate appear identical to a cached legitimate one, spoofing the identity of the original certificate's subject.

Tomer Peled Follow

Published on akamai.com

Akamai acquires Fermyon to combine WebAssembly function-as-a-service (FaaS) with Akamai’s globally distributed platform. Learn more

Tomer Peled (@tomerpeled92), Yoni Rozenshein, on akamai.com

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

Context

Windows CryptoAPI (crypt32.dll) is the de facto API in Windows for handling anything related to cryptography. It reads, parses and validates certificates against verified certificate authorities, and browsers use it for TLS certificate validation, the process that produces the lock icon. It is not only browsers: PowerShell web authentication, curl, wget, FTP managers, EDRs and many other applications use it for TLS client verification, and code-signing certificates are verified on executables and libraries while driver-signing certificates are verified when loading drivers.

The vulnerability described here is CVE-2022-34689, a critical spoofing flaw disclosed to Microsoft by the National Security Agency (NSA) and the National Cyber Security Center (NCSC). Microsoft's description, quoted in the report, is that an attacker can "spoof their identity and perform actions such as authentication or code signing as the targeted certificate." It carries a CVSS score of 7.5, was patched in August 2022, and was publicly announced in the October 2022 Patch Tuesday. The same agency previously disclosed CurveBall (CVE-2020-0601); both result in identity spoofing, but CVE-2022-34689 has more prerequisites and therefore a more limited set of vulnerable targets.

Akamai Wave Blue
Akamai Wave Blue From the original on akamai.com

Certificates play a major role in identity verification online, which is what makes a flaw in their verification lucrative for attackers.

Certificates play a major role in identity verification online, making this vulnerability lucrative for attackers.
Certificates play a major role in identity verification online, making this vulnerability lucrative for attackers. From the original on akamai.com

Setup

The analysis started by locating the patched code: BinDiff was used to observe the code changes to CryptoAPI. To reproduce the trust decision, the researchers wrote a small application that calls CertGetCertificateChain and debugged the certificate verification flow in crypt32.dll with WinDbg. To produce colliding certificates they used HashClash, and to deliver the attack they used a Python script acting as a proxy for a machine-in-the-middle position. The vulnerable target they identified in the wild is Chrome v48 and earlier; other Chromium-based applications from that time are also vulnerable.

The key precondition on the victim side is that the application must have end-certificate caching enabled. CryptoAPI can cache received end certificates to improve performance and efficiency, but this mechanism is disabled by default. The developer enables it either by setting the flag CERT_CHAIN_CACHE_END_CERT in the dwFlags argument of CertGetCertificateChain, or by creating a custom chain engine and setting CERT_CHAIN_CACHE_END_CERT in that engine's dwFlags field. Chrome v48 is vulnerable simply because it passes CERT_CHAIN_CACHE_END_CERT to CertGetCertificateChain.

Finding it

The starting point was locating the patch. In crypt32.dll, only one function changed: CreateChainContextFromPathGraph. Inside that function there is a comparison of two certificates, one received as input and one that resides in the receiving application's certificate cache. Inspection of the changes revealed that memcmp checks were added to the function in two locations.

Inspection of the changes revealed that memcmp checks were added to the function in two locations (Figure 1).
Inspection of the changes revealed that memcmp checks were added to the function in two locations (Figure 1). From the original on akamai.com

Before the patch, the function decided whether a received certificate was already in the cache, and therefore verified, based only on its MD5 thumbprint. After the patch, the added memcmp requires that the actual contents of the two certificates match completely. That difference pointed at the assumption behind the original check: that an MD5-based cache index key is collision-free. MD5's collision resistance has been known to be broken since 2009.

The theory that followed was straightforward: if an attacker could serve a malicious certificate whose MD5 collides with one already in the victim's certificate cache, the vulnerable check would be bypassed and the malicious certificate would be trusted.

The certificate cache

To understand how the cache is implemented and used, the report looks at FindIssuerObject, the function that pulls a certificate from the cache. Broadly, it behaves as follows:

  1. It calculates the input certificate's bucket index in the cache based on the four least-significant bytes of its MD5 thumbprint.
  2. If a certificate exists in the cache, the function compares the entire MD5 thumbprint of the cached certificate with that of the input certificate.
  3. If the thumbprints match (cache hit), the input certificate is trusted and returned. From then on, the application uses the input certificate's attributes (public key, issuer and so on), not the cached certificate's.
  4. If the thumbprints do not match (cache miss), it moves to the next certificate in the bucket, compares its MD5 thumbprint, and repeats.

Microsoft inherently trusts the validity of cached certificates and performs no additional validity checks once an end certificate is found in the cache. That assumption on its own is reasonable. The code then makes a further assumption, that two certificates are identical if their MD5 thumbprints match, and that is the incorrect assumption that was exploited and that the patch addresses.

To support the hypothesis, the researchers simulated a scenario in WinDbg in which the MD5 thumbprint of their own self-signed certificate matched a legitimate certificate already in the cache. The crafted certificate was trusted.

As shown in Figure 4, our crafted certificate was trusted.
As shown in Figure 4, our crafted certificate was trusted. From the original on akamai.com

Bypassing that single check was enough to make Windows believe the malicious certificate was a legitimate one.

Exploitation

Constructing a certificate whose MD5 thumbprint matches a given MD5 value is a preimage attack, and that is computationally infeasible even today. What is feasible is generating two certificates with two chosen prefixes that end up with the same MD5 thumbprint: a chosen prefix collision. The attack therefore needs to get two certificates to the victim application. The first, the "modified target certificate", is correctly signed, verified and cached, and is generated in a way that facilitates the chosen prefix collision. The second, the "malicious certificate", contains the spoofed identity and collides with the MD5 thumbprint of the first.

Step 1: understand what a thumbprint covers

MD5 collisions date back about 14 years, to the time when MD5 collisions were first used to spoof SSL certificates. The difference from that earlier attack is what is being collided: the old scenario attacked MD5 signatures, while this vulnerability attacks MD5 thumbprints.

According to RFC 5280, section 4.1, a certificate is an ASN.1 sequence with two sections:

JavaScript
   Certificate  ::=  SEQUENCE  {
        tbsCertificate       TBSCertificate,
        signatureAlgorithm   AlgorithmIdentifier,
        signatureValue       BIT STRING  }
  • tbsCertificate ("to-be-signed") contains all identity-related details: subject, public key, serial number, EKU and so on. This is the part that is signed.
  • signatureAlgorithm and signatureValue comprise the signature of the TBS.

A certificate signature is a structure embedded inside the certificate that only signs the TBS part. A certificate thumbprint is a hash of the entire certificate, including the signature. So if any part of the certificate outside the TBS can be modified without invalidating the certificate, the thumbprint changes while the signature does not. If the parser parses the signature correctly and the TBS is unchanged, the certificate is still considered valid and signed even though the full certificate structure has changed.

If the parser parses the signature correctly and the TBS is unchanged, then the certificate would still be considered valid and signed, even though the full certificate structure has changed (Figure 7).
If the parser parses the signature correctly and the TBS is unchanged, then the certificate would still be considered valid and signed, even though the full certificate structure has changed (Figure 7). From the original on akamai.com

Step 2: make room for collision blocks

According to RFC 5280, section [redacted], the structure of signatureAlgorithm is:

JavaScript
  AlgorithmIdentifier  ::=  SEQUENCE  {
        algorithm               OBJECT IDENTIFIER,
        parameters              ANY DEFINED BY algorithm OPTIONAL  }

For the RSA algorithm, RFC 3279 says the parameters field "SHALL be the ASN.1 type NULL". RSA does not use signature parameters and takes NULL as the value. The question the researchers asked was whether CryptoAPI ignores this field for RSA signatures. To insert placeholder bytes into the field as preparation for collision blocks, they changed its ASN.1 type from NULL to BIT STRING. Tested against CryptoAPI as well as OpenSSL, it works: the certificate is still considered valid, the signature is unchanged and unbroken because the TBS was not modified. The MD5 thumbprint does change.

Step 3: the collision recipe

With that in place, the recipe for manipulating an existing, already-signed certificate so that it collides with a malicious certificate's MD5 thumbprint is:

  1. Take a legitimate RSA-signed end certificate, such as a website's TLS certificate (the "target certificate").
  2. Modify any interesting fields (subject, extensions, EKU, public key, etc.) in the TBS part to create the malicious certificate. The signature is not touched, so the malicious certificate is incorrectly signed. Modifying the public key matters here: it lets the attacker sign as the malicious certificate.
  3. Modify the parameters field of the signatureAlgorithm field of both certificates so that there is enough space to put MD5 collision blocks (C and D below) starting at the same offset in both certificates.
  4. Truncate both certificates at the position where the MD5 collision blocks are to be placed.
  5. Perform an MD5 chosen prefix collision computation and copy the result into the certificates.
  6. Concatenate the legitimate certificate's signature value (the suffix E below) to both incomplete certificates.

The collision primitive itself: given two arbitrary strings A and B of the same length, two strings C and D can be calculated efficiently such that

MD5(A || C) = MD5(B || D) |

where || indicates string concatenation. It is not only the final MD5 result that matches, but also the MD5 internal state after appending C or D, so for any suffix E:

MD5(A || C || E) = MD5(B || D || E) |

provided the same suffix E is added on both sides.

Step 4: generate the colliding certificates

Among the numerous applications checked, the vulnerable target found was Chrome v48, vulnerable because it passes CERT_CHAIN_CACHE_END_CERT to CertGetCertificateChain. The first requirement was two certificates with the same MD5 thumbprint, which the researchers produced with HashClash.

In order for us to exploit this vulnerability we first needed to create two certificates that have the same MD5 thumbprint, which we did using HashClash (Figure 8).
In order for us to exploit this vulnerability we first needed to create two certificates that have the same MD5 thumbprint, which we did using HashClash (Figure 8). From the original on akamai.com

Step 5: inject the modified certificate into the cache

The next problem was getting the modified target certificate into Chrome's cache. Serving a certificate without knowing its private key is impossible, so the injection had to work around that. In TLS 1.2 there are two relevant verification stages:

  1. The Server Key Exchange message, which can only be constructed by someone who knows the certificate's private key, since it is signed by the certificate.
  2. The Server Handshake Finished message, which includes an anti-tamper verification of all previous handshake messages.

TLS 1.3 is different and was not the focus. Using a Python script as a proxy, the researchers carried out a machine-in-the-middle attack:

  1. The malicious MITM server talks to the real server and reflects the first messages of the TLS handshake to the victim.
  2. In the Server Certificate message, the malicious MITM server modifies the real server's message and replaces the real target certificate with the modified certificate.
  3. The Server Key Exchange message can be reflected without changes.
  4. The malicious server cannot simply forward the Server Handshake Finished message, because the handshake was indeed tampered with. It therefore terminates the connection.

To verify the Server Key Exchange message, Chrome must load the modified certificate with CryptoAPI, and so the certificate is injected into the cache. Chrome does not treat the broken connection as a TLS security issue, since it could be a random network issue. Chrome tries to reconnect, and this time, instead of reflecting messages from the real website, the malicious server serves a website with the malicious certificate. Chrome skips the full verification process because it thinks the certificate is already in the cache. The result is a seamless site visit to a seemingly legitimate Microsoft website.

Malicious certificate
Malicious certificate From the original on akamai.com

The full exploitation flow is shown in the video linked from the report.

Root cause

The bug is the assumption that the certificate cache index key, which is MD5-based, is collision-free. FindIssuerObject buckets certificates by the four least-significant bytes of the MD5 thumbprint and treats a full thumbprint match as proof that the input certificate is the cached one, then trusts the input certificate's attributes. Because a thumbprint hashes the entire certificate including the signature, and because CryptoAPI (and OpenSSL) accept a changed ASN.1 type in the RSA signatureAlgorithm parameters field without invalidating the signature, an attacker can alter bytes outside the TBS, keep the signature valid, and steer the thumbprint into a chosen prefix collision with a certificate the victim already trusts. The cache lookup was the weakness: it treated a matching MD5 thumbprint as proof that two certificates were identical.

Impact

An attacker who can put a colliding certificate in front of a vulnerable application can masquerade as a legitimate entity and perform actions such as authentication or code signing as the targeted certificate. The scope is limited by the prerequisites: the application must enable end-certificate caching, and the attacker must be in a position to intercept and modify the TLS handshake, as in the MITM scenario demonstrated against Chrome v48. Applications that do not use end-certificate caching are not vulnerable. The researchers believe there are more vulnerable targets in the wild and their research is ongoing. They also found that fewer than 1% of visible devices in data centers are patched, leaving the rest unprotected. Although the vulnerability was marked critical, its CVSS score is 7.5, which the researchers attribute to the limited scope of vulnerable applications and Windows components in which the prerequisites are met.

Fix

Microsoft patched the flaw in August 2022, and the fix is the memcmp addition in CreateChainContextFromPathGraph: after the MD5 thumbprint matches, the actual contents of the two certificates must match completely before the cached certificate is treated as the same certificate. The advice is to patch Windows servers and endpoints with the latest Microsoft security patch, including discontinued versions such as Windows 7, since a lot of code uses this API. For developers, an alternative mitigation is to double-check the validity of a certificate with another WinAPI such as CertVerifyCertificateChainPolicy before using it.

For detection, the report provides an OSQuery that inspects the version of crypt32.dll. Akamai Guardicore Segmentation customers can use the Insight feature together with this query to search for vulnerable assets. An asset is vulnerable only if it has an unpatched crypt32.dll and runs a vulnerable application; to that day, only Chrome v48 had been found vulnerable.

SQL
  WITH product_version AS (
  WITH os_minor AS (
    WITH os_major AS (
      SELECT substr(product_version, 0, instr(product_version, ".")) as os_major, substr(product_version, instr(product_version, ".")+1) as no_os_major_substr
      FROM file
      WHERE path = "c:\windows\system32\crypt32.dll"
    )
    SELECT substr(no_os_major_substr, instr(no_os_major_substr, ".")+1) as no_os_minor_substr, substr(no_os_major_substr, 0, instr(no_os_major_substr, ".")) as os_minor, os_major
    FROM os_major
  )
  SELECT
    CAST(substr(no_os_minor_substr, instr(no_os_minor_substr, ".")+1) AS INTEGER) AS product_minor,
    CAST(substr(no_os_minor_substr, 0, instr(no_os_minor_substr, ".")) AS INTEGER) AS product_major,
    CAST(os_minor AS INTEGER) AS os_minor,
    CAST(os_major AS INTEGER) AS os_major
  FROM os_minor
)
SELECT
  CASE
    WHEN os_major = 6 AND os_minor = 3 THEN "not supported"
    WHEN (
        (product_major = 20348 AND product_minor >= 887)
        OR
        (product_major = 17763 AND product_minor >= 3287)
        OR
        (product_major = 14393 AND product_minor >= 5291)
        OR
        (product_major >= 19041 AND product_minor >= 1889)
    )
    THEN
      "patched"
    ELSE
      "not patched"
  END is_patched
FROM product_version

The proof of concept for the complete attack is published in the researchers' GitHub repository.

Variants to test

  • Other applications that enable end-certificate caching. Look for callers of CertGetCertificateChain that pass CERT_CHAIN_CACHE_END_CERT, or that build a custom chain engine with that flag. A positive result is a client that accepts a certificate whose MD5 thumbprint matches a cached one but whose contents differ, which the patched memcmp would reject.
  • Other TLS clients using CryptoAPI. PowerShell web authentication, curl, wget, FTP managers and EDRs are named as users of certificate verification. A positive result is the same cache-hit trust decision being made without full content comparison.
  • Other signature algorithms and parameter encodings. The report tested changing the RSA signatureAlgorithm parameters from NULL to BIT STRING against CryptoAPI and OpenSSL. Trying other encodings of that optional field, and other algorithms, is a natural extension; a positive result is a certificate that still validates while carrying attacker-controlled bytes outside the TBS.
  • TLS 1.3 handshakes. The demonstrated MITM relied on TLS 1.2 message structure and the researchers did not focus on TLS 1.3. A positive result would be a TLS 1.3 flow in which the modified certificate still reaches the cache before the connection is terminated.
  • Unpatched crypt32.dll versions. Run the OSQuery above across endpoints and data centres; a positive result is any host reporting "not patched" that also runs a vulnerable application.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 20 min read.

What happened, in plain words

Every time you visit a website with a padlock in the address bar, your computer is checking a digital certificate. A certificate is like a passport for a website: it says who the site claims to be, and it carries a stamp (a signature) from someone your computer already trusts. If the passport checks out, your browser shows the padlock and lets you continue.

In 2022, two researchers at Akamai, Tomer Peled and Yoni Rozenshein, looked at a flaw in the part of Windows that does this checking, called CryptoAPI. The flaw had been disclosed to Microsoft by the National Security Agency (NSA) and the National Cyber Security Center (NCSC), and Microsoft had already fixed it. The researchers worked out how an attacker could have abused it, and built a working demonstration against an old version of the Chrome browser.

The short version: Windows kept a small memory of certificates it had already checked, so it would not have to check them again. That memory was indexed by a short fingerprint of each certificate, and the fingerprint was made with an old hashing method called MD5. MD5 has a known weakness: it is possible to build two different things that produce the same fingerprint. If an attacker could slip a fake certificate into that memory under the same fingerprint as a real one, Windows would treat the fake as already checked and trusted.

Akamai Wave Blue
Akamai Wave Blue From the original on akamai.com

Certificates are how identity is proven online, which is exactly why a flaw in checking them is valuable to an attacker.

Certificates play a major role in identity verification online, making this vulnerability lucrative for attackers.
Certificates play a major role in identity verification online, making this vulnerability lucrative for attackers. From the original on akamai.com

Background: what CryptoAPI is and why it matters

CryptoAPI is a set of functions built into Windows for anything to do with cryptography, which here means certificates and the maths that protects them. Think of it as a shared front desk that many programs walk up to when they need to know whether a certificate is genuine. The actual code lives in a file called crypt32.dll.

Browsers use CryptoAPI to check the certificates of websites they visit, which is what produces the padlock. But browsers are not the only users. The report lists PowerShell web authentication, curl, wget, FTP managers and EDRs (endpoint detection and response tools, security software that watches a computer for suspicious activity) as other programs that use it to check certificates. Separately, Windows checks code-signing certificates on programs and libraries, and driver-signing certificates when loading drivers. A code-signing certificate is a certificate that says "this program really came from this company and has not been altered".

The vulnerability is tracked as CVE-2022-34689. A CVE is a public catalogue number given to a known security flaw so people can refer to it unambiguously. Microsoft's own description, quoted in the report, says an attacker can "spoof their identity and perform actions such as authentication or code signing as the targeted certificate." In plain terms: the attacker can pretend to be someone else and have that pretence accepted.

The flaw has a CVSS score of 7.5. CVSS is a standard 0 to 10 scale for how serious a vulnerability is; 7.5 is in the "high" band. It was patched in August 2022 and publicly announced in the October 2022 Patch Tuesday. Patch Tuesday is Microsoft's regular monthly day for releasing security fixes.

The same agency had previously reported another CryptoAPI flaw, nicknamed CurveBall (CVE-2020-0601). Both flaws let an attacker pretend to be someone else, but this one has more conditions that must be met before it can be used, so fewer programs are actually exposed to it.

Setting up the investigation

The researchers started by comparing the fixed and unfixed versions of crypt32.dll. They used a tool called BinDiff, which lines up two compiled programs and highlights what changed between them. This is a common way to find out exactly what a security fix did, and therefore what the bug was.

To watch the trust decision happen live, they wrote a small test program that calls a Windows function called CertGetCertificateChain, and stepped through it in WinDbg, a debugger (a tool that lets you pause a running program and inspect it line by line). To build the colliding certificates they used HashClash, a free tool for producing MD5 collisions. To deliver the attack they used a Python script acting as a proxy, which is a program that sits in the middle of a connection and passes messages along, optionally changing them.

The vulnerable target they found in the wild was Chrome v48 and earlier, an old version of Google's browser. Other Chromium-based applications from that era are also vulnerable. Chromium is the open-source project that Chrome is built from, and many other browsers and apps are built from it too.

The key condition on the victim's side is that the program must have end-certificate caching turned on. Caching means keeping a copy of something you have already checked so you do not have to check it again. CryptoAPI can cache end certificates (the certificate of the website itself, as opposed to the certificates of the authorities that signed it) to save time, but this is off by default. A developer turns it on either by setting a flag called CERT_CHAIN_CACHE_END_CERT in the dwFlags argument of CertGetCertificateChain, or by creating a custom chain engine and setting the same flag there. Chrome v48 is vulnerable simply because it passes CERT_CHAIN_CACHE_END_CERT to CertGetCertificateChain.

Finding the flaw

The first step was to locate the fix. In crypt32.dll, only one function had changed: CreateChainContextFromPathGraph. Inside that function, two certificates are compared: one that arrived as input, and one sitting in the receiving program's certificate cache. Looking at the changes showed that memcmp checks had been added in two places.

Inspection of the changes revealed that memcmp checks were added to the function in two locations (Figure 1).
Inspection of the changes revealed that memcmp checks were added to the function in two locations (Figure 1). From the original on akamai.com

memcmp is a standard C function that compares two blocks of memory byte by byte and reports whether they are identical. Before the patch, the function decided whether an incoming certificate was already in the cache, and therefore already verified, using only its MD5 thumbprint. A thumbprint is a short fingerprint computed from the whole certificate. After the patch, the added memcmp requires that the actual contents of the two certificates match completely.

That difference pointed straight at the assumption behind the old check: that an MD5-based cache key can never collide, meaning two different certificates can never produce the same fingerprint. MD5's collision resistance has been known to be broken since 2009, so that assumption was wrong.

The theory that followed was simple. If an attacker could serve a malicious certificate whose MD5 fingerprint matched one already in the victim's certificate cache, the vulnerable check would be bypassed and the malicious certificate would be trusted.

The certificate cache

To understand how the cache works, the report looks at FindIssuerObject, the function that pulls a certificate out of the cache. Broadly, it does this:

  1. It works out which bucket in the cache the incoming certificate belongs to, using the four least-significant bytes of its MD5 thumbprint. A bucket is just a slot in a list; using part of the fingerprint to pick a slot makes lookups fast.
  2. If a certificate exists in that bucket, it compares the entire MD5 thumbprint of the cached certificate with that of the incoming one.
  3. If the thumbprints match (a cache hit), the incoming certificate is trusted and returned. From then on, the program uses the incoming certificate's details (public key, issuer and so on), not the cached certificate's.
  4. If the thumbprints do not match (a cache miss), it moves to the next certificate in the bucket, compares its MD5 thumbprint, and repeats.

Microsoft trusts cached certificates by definition and performs no further validity checks once an end certificate is found in the cache. On its own that is a reasonable assumption. The code then makes a second assumption, that two certificates are identical if their MD5 thumbprints match, and that is the incorrect assumption that was exploited and that the patch addresses.

To test the theory, the researchers set up a scenario in WinDbg where the MD5 thumbprint of their own self-signed certificate matched a legitimate certificate already in the cache. A self-signed certificate is one that is signed by itself rather than by an authority, so normally it would not be trusted. The crafted certificate was trusted anyway.

As shown in Figure 4, our crafted certificate was trusted.
As shown in Figure 4, our crafted certificate was trusted. From the original on akamai.com

Bypassing that single check was enough to make Windows believe the malicious certificate was a legitimate one.

How the attack works

Building a certificate whose MD5 thumbprint matches a specific given value is called a preimage attack, and that is computationally infeasible even today. "Computationally infeasible" means it would take more computing power and time than anyone has.

What is feasible is generating two certificates, each with a chosen starting block, that end up with the same MD5 thumbprint. This is called a chosen prefix collision. The attack therefore needs to get two certificates in front of the victim program. The first, called the "modified target certificate", is correctly signed, gets verified and lands in the cache, and is built in a way that makes the collision possible. The second, called the "malicious certificate", carries the spoofed identity and collides with the MD5 thumbprint of the first.

Step 1: understand what a thumbprint covers

MD5 collisions go back about 14 years, to when MD5 collisions were first used to spoof SSL certificates. SSL is the older name for the encryption that protects web traffic; today it is usually called TLS. The difference from that earlier attack is what is being collided: the old scenario attacked MD5 signatures, while this vulnerability attacks MD5 thumbprints.

According to RFC 5280, section 4.1, a certificate is an ASN.1 sequence with two sections. ASN.1 is a standard way of writing down structured data so that different programs can read it the same way; think of it as a strict grammar for data.

JavaScript
   Certificate  ::=  SEQUENCE  {
        tbsCertificate       TBSCertificate,
        signatureAlgorithm   AlgorithmIdentifier,
        signatureValue       BIT STRING  }

This block is a grammar rule. Read it as: a Certificate is a SEQUENCE (an ordered list) containing three things. The first is tbsCertificate, of type TBSCertificate. The second is signatureAlgorithm, of type AlgorithmIdentifier. The third is signatureValue, of type BIT STRING (a string of bits).

  • tbsCertificate ("to-be-signed") contains all the identity details: subject, public key, serial number, EKU and so on. EKU stands for Extended Key Usage, a field that says what the certificate may be used for, such as web server authentication. This is the part that is signed.
  • signatureAlgorithm and signatureValue together make up the signature of the TBS.

A certificate signature is a structure embedded inside the certificate that only signs the TBS part. A certificate thumbprint is a hash of the entire certificate, including the signature. A hash is a one-way mathematical summary: feed in any data and you get back a fixed-length fingerprint, and changing even one byte changes the fingerprint.

So if any part of the certificate outside the TBS can be modified without invalidating the certificate, the thumbprint changes while the signature does not. If the parser reads the signature correctly and the TBS is unchanged, the certificate is still considered valid and signed, even though the full certificate structure has changed.

If the parser parses the signature correctly and the TBS is unchanged, then the certificate would still be considered valid and signed, even though the full certificate structure has changed (Figure 7).
If the parser parses the signature correctly and the TBS is unchanged, then the certificate would still be considered valid and signed, even though the full certificate structure has changed (Figure 7). From the original on akamai.com

Step 2: make room for collision blocks

According to RFC 5280, section [redacted], the structure of signatureAlgorithm is:

JavaScript
  AlgorithmIdentifier  ::=  SEQUENCE  {
        algorithm               OBJECT IDENTIFIER,
        parameters              ANY DEFINED BY algorithm OPTIONAL  }

Again this is a grammar rule. An AlgorithmIdentifier is a SEQUENCE of two things: algorithm, an OBJECT IDENTIFIER (a standard code that names a specific algorithm), and parameters, which is optional and whose type depends on the algorithm.

For the RSA algorithm, RFC 3279 says the parameters field "SHALL be the ASN.1 type NULL". RSA is one of the most common algorithms for signing certificates. NULL here means "nothing"; RSA does not use signature parameters, so the field is normally empty. The question the researchers asked was whether CryptoAPI actually ignores this field for RSA signatures. To insert placeholder bytes into the field as preparation for collision blocks, they changed its ASN.1 type from NULL to BIT STRING. Tested against CryptoAPI and against OpenSSL (a widely used open-source cryptography library), it works: the certificate is still considered valid, and the signature is unchanged and unbroken because the TBS was not modified. The MD5 thumbprint does change.

Step 3: the collision recipe

With that in place, the recipe for manipulating an existing, already-signed certificate so that it collides with a malicious certificate's MD5 thumbprint is:

  1. Take a legitimate RSA-signed end certificate, such as a website's TLS certificate (the "target certificate").
  2. Modify any interesting fields (subject, extensions, EKU, public key, etc.) in the TBS part to create the malicious certificate. The signature is not touched, so the malicious certificate is incorrectly signed. Modifying the public key matters here: it lets the attacker sign as the malicious certificate.
  3. Modify the parameters field of the signatureAlgorithm field of both certificates so that there is enough space to put MD5 collision blocks (C and D below) starting at the same offset in both certificates. An offset is a position counted from the start of the data.
  4. Truncate both certificates at the position where the MD5 collision blocks are to be placed. Truncate means cut off.
  5. Perform an MD5 chosen prefix collision computation and copy the result into the certificates.
  6. Concatenate the legitimate certificate's signature value (the suffix E below) to both incomplete certificates. Concatenate means join end to end.

The collision primitive itself: given two arbitrary strings A and B of the same length, two strings C and D can be calculated efficiently such that

MD5(A || C) = MD5(B || D) |

where || indicates string concatenation. In words: if you start with A and append C, you get the same fingerprint as starting with B and appending D. It is not only the final MD5 result that matches, but also the MD5 internal state after appending C or D, so for any suffix E:

MD5(A || C || E) = MD5(B || D || E) |

provided the same suffix E is added on both sides. That last property is what makes the attack practical: the attacker can glue the real certificate's signature onto both halves and the fingerprints still match.

Step 4: generate the colliding certificates

Among the many applications checked, the vulnerable target found was Chrome v48, vulnerable because it passes CERT_CHAIN_CACHE_END_CERT to CertGetCertificateChain. The first requirement was two certificates with the same MD5 thumbprint, which the researchers produced with HashClash.

In order for us to exploit this vulnerability we first needed to create two certificates that have the same MD5 thumbprint, which we did using HashClash (Figure 8).
In order for us to exploit this vulnerability we first needed to create two certificates that have the same MD5 thumbprint, which we did using HashClash (Figure 8). From the original on akamai.com

Step 5: inject the modified certificate into the cache

The next problem was getting the modified target certificate into Chrome's cache. Serving a certificate without knowing its private key is impossible, so the injection had to work around that. A private key is the secret half of a key pair; the matching public key is in the certificate, and only the holder of the private key can produce a signature that the public key will accept.

In TLS 1.2 there are two relevant verification stages:

  1. The Server Key Exchange message, which can only be constructed by someone who knows the certificate's private key, since it is signed by the certificate.
  2. The Server Handshake Finished message, which includes an anti-tamper verification of all previous handshake messages. A handshake is the opening conversation in which a browser and a server agree how to talk securely.

TLS 1.3 is different and was not the focus. Using a Python script as a proxy, the researchers carried out a machine-in-the-middle attack. That means placing a computer between the victim and the real website, so that traffic passes through it and can be read or altered.

  1. The malicious MITM server talks to the real server and reflects the first messages of the TLS handshake to the victim. Reflect here means passing the messages through unchanged.
  2. In the Server Certificate message, the malicious MITM server modifies the real server's message and replaces the real target certificate with the modified certificate.
  3. The Server Key Exchange message can be reflected without changes.
  4. The malicious server cannot simply forward the Server Handshake Finished message, because the handshake was indeed tampered with. It therefore terminates the connection.

To verify the Server Key Exchange message, Chrome must load the modified certificate with CryptoAPI, and so the certificate is injected into the cache. Chrome does not treat the broken connection as a TLS security issue, since it could be a random network issue. Chrome tries to reconnect, and this time, instead of reflecting messages from the real website, the malicious server serves a website with the malicious certificate. Chrome skips the full verification process because it thinks the certificate is already in the cache. The result is a seamless site visit to a seemingly legitimate Microsoft website.

Malicious certificate
Malicious certificate From the original on akamai.com

The full exploitation flow is shown in the video linked from the report.

The root cause

The bug is the assumption that the certificate cache index key, which is MD5-based, is collision-free. FindIssuerObject buckets certificates by the four least-significant bytes of the MD5 thumbprint and treats a full thumbprint match as proof that the incoming certificate is the cached one, then trusts the incoming certificate's details. Because a thumbprint hashes the entire certificate including the signature, and because CryptoAPI (and OpenSSL) accept a changed ASN.1 type in the RSA signatureAlgorithm parameters field without invalidating the signature, an attacker can alter bytes outside the TBS, keep the signature valid, and steer the thumbprint into a chosen prefix collision with a certificate the victim already trusts. The cache lookup was the weakness: it treated a matching MD5 thumbprint as proof that two certificates were identical.

What the impact is

An attacker who can put a colliding certificate in front of a vulnerable application can masquerade as a legitimate entity and perform actions such as authentication or code signing as the targeted certificate. The scope is limited by the prerequisites: the application must enable end-certificate caching, and the attacker must be in a position to intercept and modify the TLS handshake, as in the MITM scenario demonstrated against Chrome v48. Applications that do not use end-certificate caching are not vulnerable. The researchers believe there are more vulnerable targets in the wild and their research is ongoing. They also found that fewer than 1% of visible devices in data centres are patched, leaving the rest unprotected. Although the vulnerability was marked critical, its CVSS score is 7.5, which the researchers attribute to the limited scope of vulnerable applications and Windows components in which the prerequisites are met.

The fix

Microsoft patched the flaw in August 2022, and the fix is the memcmp addition in CreateChainContextFromPathGraph: after the MD5 thumbprint matches, the actual contents of the two certificates must match completely before the cached certificate is treated as the same certificate. The advice is to patch Windows servers and endpoints with the latest Microsoft security patch, including discontinued versions such as Windows 7, since a lot of code uses this API. For developers, an alternative mitigation is to double-check the validity of a certificate with another WinAPI such as CertVerifyCertificateChainPolicy before using it.

For detection, the report provides an OSQuery that inspects the version of crypt32.dll. OSQuery is a tool that asks a computer questions about itself, such as which files it has and what versions they are. Akamai Guardicore Segmentation customers can use the Insight feature together with this query to search for vulnerable assets. An asset is vulnerable only if it has an unpatched crypt32.dll and runs a vulnerable application; to that day, only Chrome v48 had been found vulnerable.

SQL
  WITH product_version AS (
  WITH os_minor AS (
    WITH os_major AS (
      SELECT substr(product_version, 0, instr(product_version, ".")) as os_major, substr(product_version, instr(product_version, ".")+1) as no_os_major_substr
      FROM file
      WHERE path = "c:\windows\system32\crypt32.dll"
    )
    SELECT substr(no_os_major_substr, instr(no_os_major_substr, ".")+1) as no_os_minor_substr, substr(no_os_major_substr, 0, instr(no_os_major_substr, ".")) as os_minor, os_major
    FROM os_major
  )
  SELECT
    CAST(substr(no_os_minor_substr, instr(no_os_minor_substr, ".")+1) AS INTEGER) AS product_minor,
    CAST(substr(no_os_minor_substr, 0, instr(no_os_minor_substr, ".")) AS INTEGER) AS product_major,
    CAST(os_minor AS INTEGER) AS os_minor,
    CAST(os_major AS INTEGER) AS os_major
  FROM os_minor
)
SELECT
  CASE
    WHEN os_major = 6 AND os_minor = 3 THEN "not supported"
    WHEN (
        (product_major = 20348 AND product_minor >= 887)
        OR
        (product_major = 17763 AND product_minor >= 3287)
        OR
        (product_major = 14393 AND product_minor >= 5291)
        OR
        (product_major >= 19041 AND product_minor >= 1889)
    )
    THEN
      "patched"
    ELSE
      "not patched"
  END is_patched
FROM product_version

The proof of concept for the complete attack is published in the researchers' GitHub repository.

Other things worth testing

  • Other applications that enable end-certificate caching. Look for callers of CertGetCertificateChain that pass CERT_CHAIN_CACHE_END_CERT, or that build a custom chain engine with that flag. A positive result is a client that accepts a certificate whose MD5 thumbprint matches a cached one but whose contents differ, which the patched memcmp would reject.
  • Other TLS clients using CryptoAPI. PowerShell web authentication, curl, wget, FTP managers and EDRs are named as users of certificate verification. A positive result is the same cache-hit trust decision being made without full content comparison.
  • Other signature algorithms and parameter encodings. The report tested changing the RSA signatureAlgorithm parameters from NULL to BIT STRING against CryptoAPI and OpenSSL. Trying other encodings of that optional field, and other algorithms, is a natural extension; a positive result is a certificate that still validates while carrying attacker-controlled bytes outside the TBS.
  • TLS 1.3 handshakes. The demonstrated MITM relied on TLS 1.2 message structure and the researchers did not focus on TLS 1.3. A positive result would be a TLS 1.3 flow in which the modified certificate still reaches the cache before the connection is terminated.
  • Unpatched crypt32.dll versions. Run the OSQuery above across endpoints and data centres; a positive result is any host reporting "not patched" that also runs a vulnerable application.

Glossary

  • API: a set of functions a program can call to make another piece of software do something, like a menu of services.
  • ASN.1: a standard grammar for writing structured data so different programs can read it the same way.
  • BinDiff: a tool that compares two compiled programs and highlights the differences.
  • BIT STRING: an ASN.1 type meaning a string of bits.
  • Bucket: a slot in a list used to make lookups fast; here, a slot in the certificate cache.
  • Cache: a store of things already checked or fetched, kept so they do not have to be checked or fetched again.
  • Certificate: a digital document that binds an identity to a public key and carries a signature from a trusted authority.
  • Chosen prefix collision: a way of building two different pieces of data, each with a chosen starting block, that produce the same hash.
  • Chromium: the open-source browser project that Chrome and many other browsers are built from.
  • Code-signing certificate: a certificate used to prove that a program really came from a particular company and has not been altered.
  • Collision: two different inputs that produce the same hash output.
  • Concatenate: join two pieces of data end to end.
  • CryptoAPI: the part of Windows, in crypt32.dll, that handles certificates and cryptography.
  • CVSS: a standard 0 to 10 scale for rating the severity of a vulnerability.
  • CVE: a public catalogue number given to a known security flaw.
  • Debugger: a tool that lets you pause a running program and inspect it step by step.
  • Driver-signing certificate: a certificate used to prove that a hardware driver really came from a particular company.
  • EDR: endpoint detection and response, security software that watches a computer for suspicious activity.
  • EKU: Extended Key Usage, a certificate field that says what the certificate may be used for.
  • End certificate: the certificate of the website or service itself, as opposed to the certificates of the authorities that signed it.
  • Flag: a setting passed to a function to switch a behaviour on or off.
  • Hash: a one-way mathematical summary that turns any data into a fixed-length fingerprint.
  • HashClash: a free tool for producing MD5 collisions.
  • Handshake: the opening conversation in which a browser and a server agree how to talk securely.
  • Machine-in-the-middle (MITM): an attack where a computer is placed between the victim and the real server so traffic can be read or altered.
  • MD5: an old hashing method whose collision resistance has been known to be broken since 2009.
  • memcmp: a standard C function that compares two blocks of memory byte by byte.
  • NULL: in ASN.1, a type meaning "nothing"; here, the empty value RSA uses for signature parameters.
  • Offset: a position counted from the start of a piece of data.
  • OpenSSL: a widely used open-source cryptography library.
  • OSQuery: a tool that asks a computer questions about itself, such as which files it has and what versions they are.
  • Parser: the part of a program that reads structured data and works out what it means.
  • Patch Tuesday: Microsoft's regular monthly day for releasing security fixes.
  • Preimage attack: building an input that produces a specific given hash value; computationally infeasible for MD5 today.
  • Private key: the secret half of a key pair; only its holder can produce a signature the matching public key will accept.
  • Proxy: a program that sits in the middle of a connection and passes messages along, optionally changing them.
  • Public key: the non-secret half of a key pair, usually published inside a certificate.
  • RSA: one of the most common algorithms for signing certificates.
  • Self-signed certificate: a certificate signed by itself rather than by an authority, so normally not trusted.
  • Signature: a cryptographic stamp that proves a piece of data came from the holder of a private key and has not been altered.
  • SQL: the language used to ask questions of a database.
  • SSL/TLS: the encryption that protects web traffic; SSL is the older name, TLS the current one.
  • Thumbprint: a short fingerprint computed from the whole certificate, including its signature.
  • Truncate: cut off.
  • TBS (to-be-signed): the part of a certificate that holds the identity details and is covered by the signature.
  • WinDbg: a debugger for Windows.
  • WinAPI: the set of functions Windows offers to programs.

More from Microsoft

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.