Skip to content
Crypto Write-up Advanced

The CN That Wasn't a CN

Apigee's Common Name check parsed the certificate subject as comma-separated text, so a false CN marker inside an OU could bypass the configured check.

Nikita Markevich Follow

Published on bugs.exposed

Bounty$11,250 View original

Comment

Nikita Markevich, on bugs.exposed

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. 7 min read.

Context

Apigee is Google Cloud's API management proxy. A proxy accepts an inbound request and then opens an outbound connection to a backend service. When that backend is reached over HTTPS, trusting the certificate chain is not enough: a certificate can be signed by a trusted internal CA and still belong to the wrong service. To close that gap, the proxy author can configure a name check on an HTTPTargetConnection:

XML
<SSLInfo>
    <Enabled>true</Enabled>
    <CommonName>safe.host</CommonName>
</SSLInfo>

Apigee documents <CommonName> as a comparison against the peer certificate's Common Name or its DNS Subject Alternative Names. If the certificate names attacker.com while the proxy expects safe.host, the connection should fail. The researcher came to this feature while following Google's recommendations for an earlier Apigee TLS issue, and expected it to be a fairly uninteresting final line of defence: the certificate already has a structured subject, Java knows how to read it, and Apigee only needs to compare one name.

Setup

The work was done against Google Cloud Apigee runtime v1-15-0-apigee-1. The issue has since been patched. The researcher deployed a proxy whose <CommonName> was set to safe.host and pointed it at an HTTPS test server under his control. Certificates were generated by a single script so that only the subject shape differed between tests. No Apigee Edge deployment was available, so the Edge consequence described below rests on static analysis rather than a live reproduction.

Finding it

The outer verification method walks the certificates from the TLS session and passes each X.509 certificate to verifyCommonNameOnCertificate(). That helper assembles the names Apigee is willing to accept:

Java
boolean verifyCommonNameOnCertificate(
        X509Certificate certificate, String expectedCN, boolean wildcard)
        throws CertificateParsingException {
    List<String> validNames = new ArrayList<>();
    validNames.addAll(
        CertUtil.getFields(certificate, CertUtil.FIELD_COMMON_NAME));
    validNames.addAll(CertUtil.getDomainNamesFromSan(certificate));

    for (String name : validNames) {
        boolean matched = wildcard
            ? wildcardMatch(expectedCN, name)
            : expectedCN.equals(name);
        if (matched) {
            return true;
        }
    }
    return false;
}

The final comparison is exactly what one would expect: subject Common Names are combined with DNS SANs, and the certificate is accepted if one entry matches. The interesting question therefore moves one layer down, into what CertUtil.getFields() puts into validNames.

An X.509 subject is an X.500 distinguished name. Java exposes it as an X500Principal, preserving attributes such as CN and OU. Apigee did not read the Common Name from that structure. It flattened the principal with toString(), split the result on commas, and searched every fragment for the characters CN=:

Java
public static List<String> getFields(
        X509Certificate certificate, String fieldWithEqual) {
    List<String> names = new LinkedList<>();
    String subject = certificate.getSubjectX500Principal().toString();
    StringTokenizer tokens = new StringTokenizer(subject, ",");

    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken();
        int offset = token.indexOf(fieldWithEqual);
        if (offset != -1) {
            names.add(token.substring(
                offset + fieldWithEqual.length()).trim());
        }
    }
    return names;
}

Two mistakes compound here. The code assumes every comma separates two attributes, but X.500 strings also allow commas inside quoted or escaped attribute values, and StringTokenizer does not know the difference. And the substring search does not ask whether a fragment is a Common Name attribute; indexOf("CN=") accepts any fragment that happens to contain those three characters.

Exploitation

Step 1: build a certificate with a fake CN inside the OU

The researcher gave a certificate the real Common Name attacker.com and placed another name inside its Organizational Unit:

Text
CN=attacker.com, OU="CN=safe.host,"

Java renders that subject as:

Text
OU="CN=safe.host,", CN=attacker.com

The first comma is part of the quoted OU value. Apigee treats it as an attribute boundary and produces a fragment containing CN=safe.host. The substring search then extracts safe.host and adds it to the same list as the certificate's real identity. The certificate still says CN=attacker.com; Apigee has invented a second Common Name from text stored under a different attribute.

Step 2: confirm the parser disagreement offline

The same certificate was run through Apigee's logic and through an X.500-aware parser based on LdapName:

Text
Raw subject: OU="CN=safe.host,", CN=attacker.com
Extracted CNs (Apigee logic): [safe.host, attacker.com]
Extracted CNs (structured parser): [attacker.com]

Apigee-style match against "safe.host": true
Structured match against "safe.host": false

This proves the parser disagreement but not yet that the value influences an outbound connection.

Step 3: drive the decision through a live proxy

With the proxy configured for <CommonName>safe.host</CommonName> and pointed at the test server, two certificates generated by the same script were tried:

  1. A plain certificate with the wrong Common Name was rejected with COMMON_NAME_MISMATCH.
  2. The same wrong Common Name, with CN=safe.host, added inside OU, was accepted, and the request reached the test server.

The first request matters as much as the second: it shows the protection was enabled and capable of stopping the connection. Changing the shape of one subject attribute flips the decision without changing the certificate's real Common Name.

Step 4: account for the earlier runtime check

On Google Cloud runtime v1-15-0-apigee-1, the runtime performs an earlier Java-level identity check before reaching Apigee's own <CommonName> verifier. To get past it, the test certificate also carried the server's IP address in its SAN extension. That satisfied the earlier check honestly and let the request continue to the code under test.

The result therefore has a precise boundary: the researcher demonstrated that Apigee extracted a false certificate identity and used it to approve the configured Common Name check. The forged OU did not independently bypass every TLS check in the Google Cloud runtime.

Root cause

CertUtil.getFields() parses a structured X.500 distinguished name by string operations: toString(), a comma split with StringTokenizer, and a substring search for CN=. Because commas can legally appear inside quoted or escaped attribute values, the split does not follow attribute boundaries, and because indexOf() only looks for the characters anywhere in a fragment, a value stored under OU can masquerade as a Common Name. The final equality check in verifyCommonNameOnCertificate() is correct; it is fed a list that already contains an attacker-chosen entry.

On Google Cloud, the earlier Java-level identity check limited the effect to the <CommonName> verifier. The vulnerable parser was present in the Edge-relevant code path the researcher reviewed, and Edge appeared to rely on the configured check without that preliminary validation, so a certificate carrying the injected subject could reach the layer meant to provide the missing identity protection. That stronger Edge consequence comes from static analysis of the shared path and the different runtime protection, not from a live Edge test.

Impact

A certificate whose subject hides CN=<expected host>, inside a quoted attribute such as OU can make the proxy's <CommonName> check accept a hostname the certificate does not actually name. The check is the control that is supposed to stop a trusted-but-wrong certificate from being accepted for an outbound backend. On Google Cloud the demonstrated effect is limited to that check, because the earlier runtime identity check still applies. On Apigee Edge, where the configured check appears to be the protection relied upon, the consequence is stronger, but it was not reproduced live. The report does not describe any other precondition, such as control over the backend or the CA that issued the certificate.

Fix

Google patched the issue; the affected version listed is v1-15-0-apigee-1. The report does not describe the patch itself.

Variants to test

  1. Other attribute types. Put CN=<expected host>, inside O, L, ST or emailAddress instead of OU, and see whether the proxy accepts a certificate whose real CN differs. A positive result is a connection that succeeds where the plain wrong-CN certificate returns COMMON_NAME_MISMATCH.
  2. Escaped commas. Use an escaped comma (\,) rather than a quoted one inside an attribute value, and check whether the same fragment is produced. A positive result is the expected host appearing in the extracted name list.
  3. Multiple injected fragments. Place several CN= strings across different attributes and observe which one the parser picks up, to map how the split behaves with more than one comma.
  4. Wildcard mode. Repeat the tests with a wildcard <CommonName> configuration, since the matching branch differs (wildcardMatch versus equals), and check whether an injected fragment can satisfy a wildcard pattern.
  5. SAN parsing. Apply the same idea to getDomainNamesFromSan(): if SAN entries are also handled as text, try unusual encodings or separators there and see whether an unexpected name is accepted.
  6. Other runtimes. Test the same certificate against Apigee Edge, where the report says the preliminary runtime check appeared to be absent. A positive result is acceptance of the injected name without needing the SAN workaround used on Google Cloud.

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. 14 min read.

What happened, in plain words

A company called Google runs a service called Apigee. Apigee is a kind of middleman for computer programs. Many apps do not talk to each other directly; instead they send their messages through a middleman that can check them, log them, and pass them on. That middleman is called an API proxy, and Apigee is Google's product for running these proxies. "API" stands for Application Programming Interface, which is just the agreed way two programs ask each other for things. Think of a restaurant: you (one program) do not walk into the kitchen (the other program); you tell the waiter (the proxy), and the waiter carries your order and brings back the food.

Sometimes the proxy has to talk to another computer, called a backend, over a secure connection. "HTTPS" is the secure version of the web's normal language: the two computers agree on a secret code, and everything they say is scrambled so nobody in between can read or change it. Part of setting up that secure connection is checking an identity document, called a certificate. A certificate is like a passport for a computer: it says "this computer really is the one you asked for".

There is a catch. A certificate can be perfectly genuine, signed by a trusted authority, and still belong to the wrong computer. Imagine a passport that is real but belongs to someone else. To close that gap, Apigee lets the person who sets up the proxy add an extra name check. The proxy is told: only connect if the certificate names this exact host. If the certificate says attacker.com but the proxy expects safe.host, the connection should be refused.

Nikita Markevich, the researcher who wrote this up, came to this feature while reading Google's advice about an earlier Apigee security problem. The advice was simple: set a <CommonName> on the outbound connection, and Apigee will reject a backend certificate issued for the wrong host. He expected this to be a boring last line of defence. The certificate already stores its names in a neat structured form, the programming language Java knows how to read that structure, and Apigee only needs to compare one name. Then he opened the code.

Apigee was not reading the Common Name from the certificate's structure at all. It turned the whole subject into one long piece of text, chopped that text at every comma, and then looked through every resulting piece for the three characters CN=. At that point the researcher stopped thinking about certificates and started thinking about commas.

The protection came down to a list of strings

Let us slow down and explain the pieces, because the whole bug lives in them.

A certificate contains a section called the subject. The subject describes who the certificate belongs to. It is built from labelled pieces called attributes. The most important label here is CN, short for Common Name, which holds the hostname the certificate is for, for example safe.host. Another common label is OU, short for Organizational Unit, which is meant to hold a department name inside an organisation. There is also O for organisation, L for city, ST for state, and so on.

The formal name for this structure is an X.500 distinguished name. "Distinguished" here just means "fully identified": the labels together describe one unique entity. Java, the programming language Apigee is written in, has a class called X500Principal that holds this structure and knows which part is the label and which part is the value. That is the key point: the structure is not a blob of text, it is a list of label-and-value pairs, and a correct program reads it as such.

Now the proxy. When an Apigee proxy accepts an incoming request and later opens a connection to a backend over HTTPS, trusting the certificate chain is not enough. The certificate chain is the series of signatures that proves a certificate was issued by someone trusted: your certificate, then the certificate of the authority that signed it, and so on up to a root everyone trusts. A certificate can be signed by a trusted internal authority and still belong to the wrong service. So the proxy author can add a name check to an HTTPTargetConnection, which is the part of the proxy configuration that describes where the backend is:

XML
<SSLInfo>
    <Enabled>true</Enabled>
    <CommonName>safe.host</CommonName>
</SSLInfo>

Line by line:

  • <SSLInfo> opens a block of settings about the secure connection. SSL and its successor TLS are the protocols that make HTTPS secure; people often say SSL when they mean TLS.
  • <Enabled>true</Enabled> switches that secure connection on.
  • <CommonName>safe.host</CommonName> is the extra name check. It tells Apigee: only accept a backend certificate that names safe.host.
  • </SSLInfo> closes the block.

Apigee's documentation says <CommonName> is compared against the peer certificate's Common Name or its DNS Subject Alternative Names. Subject Alternative Names, or SANs, are extra names listed in a certificate, so one certificate can cover several hostnames. DNS is the system that turns names like safe.host into the numeric addresses computers use. If the certificate names attacker.com while the proxy expects safe.host, the connection should fail.

The outer verification method walks the certificates from the TLS session and passes each X.509 certificate to verifyCommonNameOnCertificate(). An X.509 certificate is simply the standard format for these identity documents. A method is a named block of code that does one job; a helper is a small method that a bigger one calls to get part of the work done. Here is the helper:

Java
boolean verifyCommonNameOnCertificate(
        X509Certificate certificate, String expectedCN, boolean wildcard)
        throws CertificateParsingException {
    List<String> validNames = new ArrayList<>();
    validNames.addAll(
        CertUtil.getFields(certificate, CertUtil.FIELD_COMMON_NAME));
    validNames.addAll(CertUtil.getDomainNamesFromSan(certificate));

    for (String name : validNames) {
        boolean matched = wildcard
            ? wildcardMatch(expectedCN, name)
            : expectedCN.equals(name);
        if (matched) {
            return true;
        }
    }
    return false;
}

Line by line:

  • boolean verifyCommonNameOnCertificate( starts a method that will answer yes or no. boolean is a type that holds only true or false.
  • X509Certificate certificate, String expectedCN, boolean wildcard) lists what the method receives: the certificate to inspect, the name the proxy expects (expectedCN), and a flag saying whether wildcard matching is allowed. A wildcard is a pattern with a stand-in character, like *.example.com, meaning any single name in front of the dot.
  • throws CertificateParsingException { says the method may fail with an error if the certificate cannot be read. Parsing means taking raw data and working out its structure.
  • List<String> validNames = new ArrayList<>(); creates an empty list of text strings to hold every name Apigee is willing to accept.
  • validNames.addAll( and the next line add to that list everything CertUtil.getFields(...) returns for the Common Name field. CertUtil is a utility class, a toolbox of certificate-related helpers. FIELD_COMMON_NAME is a constant naming which field to look for.
  • validNames.addAll(CertUtil.getDomainNamesFromSan(certificate)); adds the DNS names taken from the certificate's SAN extension.
  • for (String name : validNames) { starts a loop over every name in the list, one at a time.
  • boolean matched = wildcard begins the comparison. If wildcard mode is on, the next line uses wildcardMatch(expectedCN, name); otherwise it uses expectedCN.equals(name), an exact text comparison.
  • if (matched) { return true; } means: as soon as one name matches, answer yes and stop.
  • return false; means: if the loop finishes with no match, answer no.

The final comparison is exactly what you would expect. Subject Common Names are combined with DNS SANs, and the certificate is accepted if one entry matches. So the interesting question moves one layer down: what does CertUtil.getFields() actually put into validNames?

Then I saw the comma split

An X.509 subject is an X.500 distinguished name, as we said. Java exposes it as an X500Principal, preserving attributes such as CN and OU. Apigee did not read the Common Name from that structure. It flattened the principal with toString(), split the result on commas, and searched every fragment for the characters CN=:

Java
public static List<String> getFields(
        X509Certificate certificate, String fieldWithEqual) {
    List<String> names = new LinkedList<>();
    String subject = certificate.getSubjectX500Principal().toString();
    StringTokenizer tokens = new StringTokenizer(subject, ",");

    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken();
        int offset = token.indexOf(fieldWithEqual);
        if (offset != -1) {
            names.add(token.substring(
                offset + fieldWithEqual.length()).trim());
        }
    }
    return names;
}

Line by line:

  • public static List<String> getFields( starts a method that returns a list of text strings. public means other code can call it; static means it belongs to the class itself rather than to one particular object.
  • X509Certificate certificate, String fieldWithEqual) lists the inputs: the certificate, and the label to look for, here CN=.
  • List<String> names = new LinkedList<>(); creates an empty list to collect the results.
  • String subject = certificate.getSubjectX500Principal().toString(); takes the structured subject and turns it into one long piece of text with toString(). This is the moment the structure is thrown away.
  • StringTokenizer tokens = new StringTokenizer(subject, ","); creates a tool that chops that text into pieces at every comma. StringTokenizer is a very old Java class that knows nothing about certificates; it just splits text.
  • while (tokens.hasMoreTokens()) { keeps going while there are pieces left.
  • String token = tokens.nextToken(); takes the next piece.
  • int offset = token.indexOf(fieldWithEqual); asks: at which position in this piece do the characters CN= first appear? indexOf returns -1 if they do not appear at all.
  • if (offset != -1) { means: if they were found somewhere in the piece.
  • names.add(token.substring( and the next line take everything after CN= and trim the spaces around it, then add it to the list. substring cuts out part of a string; trim removes spaces at the ends.
  • return names; hands back the collected list.

Two mistakes compound here. The code assumes every comma separates two attributes, but X.500 strings also allow commas inside quoted or escaped attribute values, and StringTokenizer does not know the difference. A quoted value is one wrapped in double quotes, where a comma is just a character, not a separator. An escaped character is one marked with a backslash so it loses its special meaning, like \,. And the substring search does not ask whether a fragment is a Common Name attribute; indexOf("CN=") accepts any fragment that happens to contain those three characters anywhere.

A fake CN survived the real connection

Step 1: build a certificate with a fake CN inside the OU

The researcher gave a certificate the real Common Name attacker.com and placed another name inside its Organizational Unit:

Text
CN=attacker.com, OU="CN=safe.host,"

This is the subject written out as text. Read it as: the Common Name is attacker.com, and the Organizational Unit is the quoted text CN=safe.host,. The comma inside the quotes is part of the OU value, not a separator between attributes.

Java renders that subject as:

Text
OU="CN=safe.host,", CN=attacker.com

Java has reordered the attributes, but the meaning is the same. The first comma is part of the quoted OU value. Apigee treats it as an attribute boundary and produces a fragment containing CN=safe.host. The substring search then extracts safe.host and adds it to the same list as the certificate's real identity. The certificate still says CN=attacker.com; Apigee has invented a second Common Name from text stored under a different attribute.

Step 2: confirm the parser disagreement offline

The same certificate was run through Apigee's logic and through an X.500-aware parser based on LdapName. LDAP is a directory protocol, and LdapName is a Java class that understands distinguished names properly, including quoted and escaped commas. Running both side by side, without any network involved, isolates the parsing question:

Text
Raw subject: OU="CN=safe.host,", CN=attacker.com
Extracted CNs (Apigee logic): [safe.host, attacker.com]
Extracted CNs (structured parser): [attacker.com]

Apigee-style match against "safe.host": true
Structured match against "safe.host": false

Line by line:

  • Raw subject: shows the subject text both parsers were given.
  • Extracted CNs (Apigee logic): [safe.host, attacker.com] shows Apigee's comma-splitting code found two names: the fake one and the real one.
  • Extracted CNs (structured parser): [attacker.com] shows the correct parser found only the real Common Name.
  • Apigee-style match against "safe.host": true shows Apigee's logic would accept the certificate for safe.host.
  • Structured match against "safe.host": false shows a correct parser would refuse it.

This proves the parser disagreement but not yet that the value influences an outbound connection. A parser test is a laboratory result; the researcher wanted to see the wrong answer change a real connection decision.

Step 3: drive the decision through a live proxy

With the proxy configured for <CommonName>safe.host</CommonName> and pointed at the test server, two certificates generated by the same script were tried. Using one script matters: both certificates came from the same generator, so the subject shape was the variable under test.

  1. A plain certificate with the wrong Common Name was rejected with COMMON_NAME_MISMATCH.
  2. The same wrong Common Name, with CN=safe.host, added inside OU, was accepted, and the request reached the test server.

The first request matters as much as the second: it shows the protection was enabled and capable of stopping the connection. Without that control test, a successful second request could just mean the check was switched off. Changing the shape of one subject attribute flips the decision without changing the certificate's real Common Name.

Step 4: account for the earlier runtime check

On Google Cloud runtime v1-15-0-apigee-1, the runtime performs an earlier Java-level identity check before reaching Apigee's own <CommonName> verifier. A runtime is the environment where the software actually executes. To get past that earlier check, the test certificate also carried the server's IP address in its SAN extension. An IP address is the numeric label of a computer on a network. That satisfied the earlier check honestly and let the request continue to the code under test.

The result therefore has a precise boundary: the researcher demonstrated that Apigee extracted a false certificate identity and used it to approve the configured Common Name check. The forged OU did not independently bypass every TLS check in the Google Cloud runtime. In other words, the forged OU did not independently bypass every TLS check in the Google Cloud runtime.

Edge was the reason I cared about this check

The researcher had started here because of Apigee security bulletin GCP-2024-006. A security bulletin is an official notice about a security problem and what to do about it. The bulletin addressed Apigee Edge installations where outbound hostname verification could be disabled by default. Apigee Edge is an older, separately deployed version of Apigee. Hostname verification is the general check that the certificate names the host you meant to reach. One recommended protection was the same <CommonName> setting the researcher had just bypassed.

The vulnerable parser was present in the Edge-relevant code path he reviewed. A code path is the route the program takes through its code for a given situation. Edge also appeared to rely on the configured check without the preliminary validation that complicated the Google Cloud test. A certificate carrying the injected subject could therefore reach the layer that was supposed to provide the missing identity protection.

He did not have an Apigee Edge deployment, so he could not perform the second live reproduction there. The Google Cloud experiment established the parser bug and its effect on Apigee's connection decision. The stronger Edge consequence came from static analysis of the shared path and the different runtime protection. Static analysis means reading the code and reasoning about it, rather than running it.

By the way, this was his thirtieth Apigee report. He expected that milestone to come from some tangled proxy chain. Instead, it came from StringTokenizer, indexOf(), and one comma in quotes.

Glossary

  • API: the agreed way two programs ask each other for things.
  • API proxy: a middleman program that accepts a request and forwards it to a backend service.
  • Apigee: Google Cloud's product for running API proxies.
  • Apigee Edge: an older, separately deployed version of Apigee.
  • Attribute: one labelled piece of a certificate subject, such as CN or OU.
  • Backend: the computer or service a proxy forwards requests to.
  • Boolean: a value that is either true or false.
  • Certificate: a computer's identity document, signed by an authority, saying which host it belongs to.
  • Certificate chain: the series of signatures linking a certificate up to a trusted root.
  • Code path: the route a program takes through its code for a given situation.
  • Common Name (CN): the subject attribute holding the hostname a certificate is for.
  • DNS: the system that turns names like safe.host into numeric addresses.
  • DNS Subject Alternative Names (SANs): extra hostnames listed in a certificate.
  • Escaped character: a character marked with a backslash so it loses its special meaning.
  • Helper: a small method called by a bigger one to do part of the work.
  • Hostname verification: the check that a certificate names the host you meant to reach.
  • HTTPS: the secure version of the web's normal language, with traffic scrambled between the two computers.
  • HTTPTargetConnection: the part of an Apigee proxy configuration describing where the backend is.
  • IP address: the numeric label of a computer on a network.
  • Java: the programming language Apigee is written in.
  • LDAP: a directory protocol; LdapName is a Java class that understands distinguished names properly.
  • Method: a named block of code that does one job.
  • Organizational Unit (OU): a subject attribute meant to hold a department name inside an organisation.
  • Parsing: taking raw data and working out its structure.
  • Quoted value: a value wrapped in double quotes, where a comma is just a character.
  • Runtime: the environment where software actually executes.
  • Security bulletin: an official notice about a security problem and what to do about it.
  • SSL/TLS: the protocols that make HTTPS secure.
  • Static analysis: reading code and reasoning about it, rather than running it.
  • String: a piece of text in programming.
  • StringTokenizer: an old Java class that splits text at given characters, knowing nothing about certificates.
  • Subject: the section of a certificate describing who it belongs to.
  • Substring: a part cut out of a longer string.
  • Wildcard: a pattern with a stand-in character, like *.example.com.
  • X.500 distinguished name: the structured, fully identifying form of a certificate subject.
  • X.509 certificate: the standard format for these identity documents.
  • X500Principal: the Java class that holds a structured subject and knows its labels and values.

More by Nikita Markevich

View all

11 more disclosed reports, best first.

More from Google

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.