A caller presents a client certificate → Azure Application Gateway (WAF_v2, passthrough) forwards it → API Management performs the real certificate validation. The gateway never validates the cert — APIM does. Here is each link, proven with real command output.
Does App Gateway forward the client certificate to APIM, and does APIM validate it against Key Vault?
Yes. APIM reconstructs the forwarded certificate and validates it under the selected model before authorizing per client - a pinned-thumbprint allow list, or a real cryptographic chain-of-trust signature check. A certificate from an untrusted issuer is rejected even though the gateway forwarded it unvalidated: APIM is the trust anchor, and Key Vault holds the trust material.
There are two legitimate ways to decide whether a forwarded certificate is trustworthy, and this scenario
ships both. Choose one with the certValidationMode
parameter (pinned or chain). Neither is
universally “correct” — they trade explicit control against operational overhead. Both were run live; see the evidence below.
Accept only certificates on a Key Vault allow list.
Accept anything the Root CA cryptographically signed.
Chain mode performs a genuine RSA signature verification with the Key Vault Root CA’s public key. The spoofed certificate proves it: its issuer Distinguished Name is byte-for-byte identical to the real Root CA (CN=mTLS POC Root CA), yet it is rejected with NOT_CA_SIGNED and chainOk=false — because the Root CA’s key never signed it. A string comparison would have been fooled; RSA.VerifyData is not.
Sandbox note: the APIM policy validator blocks both X509Chain and System.Func (both confirmed at deploy time), so chain mode parses the leaf DER inline to recover the TBSCertificate bytes and signature, then verifies with GetRSAPublicKey().VerifyData(...).
The gateway forwards the certificate; APIM is the trust anchor and validates it against Key Vault material.
flowchart LR
Caller(["Caller
presents client cert"])
subgraph AGW["Application Gateway • WAF_v2 • PASSTHROUGH"]
direction TB
L["HTTPS listener :443
requests client cert,
does NOT validate"]
RW["Header rewrite (overwrite)
X-Client-Cert = {var_client_certificate}"]
L --> RW
end
subgraph VNET["Internal virtual network (locked down)"]
APIM["API Management
validates cert in policy"]
end
KV["Key Vault
Root CA + pinned allow-list"]
Caller -- "mTLS (client cert)" --> AGW
AGW -- "HTTPS + forwarded cert header" --> APIM
KV -. "named values (managed identity)" .-> APIM
APIM -- "200 ALLOW / 403 DENY" --> Caller
Trust is decided in APIM against Key Vault, not at the gateway.
sequenceDiagram
autonumber
participant C as Caller
participant AG as App Gateway
participant A as APIM
participant KV as Key Vault
C->>AG: TLS handshake plus client certificate
Note over AG: Passthrough - requests the cert but does not validate it
AG->>AG: Rewrite - overwrite X-Client-Cert with the TLS cert
AG->>A: HTTPS plus X-Client-Cert header, forwarded PEM
A->>KV: Resolve Root CA and allow-list via named values
KV-->>A: trusted-root-ca-der-b64 and client-cert-allowlist
A->>A: Parse cert, check validity, issuer vs Root CA, thumbprint vs allow-list
alt Trusted, allow-listed and authorized
A-->>C: 200 ALLOW
else Untrusted or not allow-listed or wrong path
A-->>C: 403 DENY
end
az network application-gateway rewrite-rule list --rule-set-name forward-client-cert
// App Gateway forwards the TLS client cert to APIM. Every header is SET
// (overwrite) from a mutual-auth server variable, so a client can never
// inject its own X-Client-Cert value.
rewriteRuleSets: [
{
name: 'forward-client-cert'
properties: {
rewriteRules: [
{
name: 'set-client-cert-headers'
actionSet: {
requestHeaderConfigurations: [
{ headerName: 'X-Client-Cert' headerValue: '{var_client_certificate}' }
{ headerName: 'X-Client-Cert-Issuer' headerValue: '{var_client_certificate_issuer}' }
{ headerName: 'X-Client-Cert-Verify' headerValue: '{var_client_certificate_verification}' }
// + subject / fingerprint / serial / start / end
]
}
}
]
}
}
]
az keyvault secret list + APIM namedValues keyVault.secretIdentifier
// The Root CA and the pinned allow-list live in Key Vault and are surfaced
// to the APIM policy as named values via the service's managed identity.
resource nvRoot 'Microsoft.ApiManagement/service/namedValues@2023-05-01-preview' = {
parent: apim
name: 'trusted-root-ca-der-b64'
properties: {
secret: true
keyVault: { secretIdentifier: rootSecretIdentifier } // -> KV secret
}
}
resource nvAllow 'Microsoft.ApiManagement/service/namedValues@2023-05-01-preview' = {
parent: apim
name: 'client-cert-allowlist'
properties: {
secret: true
keyVault: { secretIdentifier: allowlistSecretIdentifier }
}
}
[PINNED] GET /poc/whoami with X-Client-Cert = <client1 PEM>
// Rebuild the forwarded cert, then decide trust under the active model.
string pem = System.Uri.UnescapeDataString(fwdCertHeader); // X-Client-Cert
var cert = new X509Certificate2(Convert.FromBase64String(pemBody));
var root = new X509Certificate2(Convert.FromBase64String("{{trusted-root-ca-der-b64}}"));
// MODEL A (pinned): thumbprint must be on the KV allow-list AND issuer match.
bool issuerDnMatch = string.Equals(cert.Issuer, root.Subject,
StringComparison.OrdinalIgnoreCase);
bool pinnedMatch = false;
foreach (var e in "{{client-cert-allowlist}}".Split('|')) {
var kv = e.Split(':'); // "client1:THUMBPRINT"
if (kv.Length == 2 &&
string.Equals(kv[1].Trim(), cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
pinnedMatch = true;
}
bool pinnedOk = timeOk && issuerDnMatch && pinnedMatch; // -> 200 when true
[PINNED] GET /poc/whoami with X-Client-Cert = <client2 PEM>
// Rebuild the forwarded cert, then decide trust under the active model.
string pem = System.Uri.UnescapeDataString(fwdCertHeader); // X-Client-Cert
var cert = new X509Certificate2(Convert.FromBase64String(pemBody));
var root = new X509Certificate2(Convert.FromBase64String("{{trusted-root-ca-der-b64}}"));
// MODEL A (pinned): thumbprint must be on the KV allow-list AND issuer match.
bool issuerDnMatch = string.Equals(cert.Issuer, root.Subject,
StringComparison.OrdinalIgnoreCase);
bool pinnedMatch = false;
foreach (var e in "{{client-cert-allowlist}}".Split('|')) {
var kv = e.Split(':'); // "client1:THUMBPRINT"
if (kv.Length == 2 &&
string.Equals(kv[1].Trim(), cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
pinnedMatch = true;
}
bool pinnedOk = timeOk && issuerDnMatch && pinnedMatch; // -> 200 when true
[PINNED] GET /poc/whoami with X-Client-Cert = <client3 PEM>
// Rebuild the forwarded cert, then decide trust under the active model.
string pem = System.Uri.UnescapeDataString(fwdCertHeader); // X-Client-Cert
var cert = new X509Certificate2(Convert.FromBase64String(pemBody));
var root = new X509Certificate2(Convert.FromBase64String("{{trusted-root-ca-der-b64}}"));
// MODEL A (pinned): thumbprint must be on the KV allow-list AND issuer match.
bool issuerDnMatch = string.Equals(cert.Issuer, root.Subject,
StringComparison.OrdinalIgnoreCase);
bool pinnedMatch = false;
foreach (var e in "{{client-cert-allowlist}}".Split('|')) {
var kv = e.Split(':'); // "client1:THUMBPRINT"
if (kv.Length == 2 &&
string.Equals(kv[1].Trim(), cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
pinnedMatch = true;
}
bool pinnedOk = timeOk && issuerDnMatch && pinnedMatch; // -> 200 when true
[PINNED] GET /poc/whoami with X-Client-Cert = <spoofed PEM>
// Rebuild the forwarded cert, then decide trust under the active model.
string pem = System.Uri.UnescapeDataString(fwdCertHeader); // X-Client-Cert
var cert = new X509Certificate2(Convert.FromBase64String(pemBody));
var root = new X509Certificate2(Convert.FromBase64String("{{trusted-root-ca-der-b64}}"));
// MODEL A (pinned): thumbprint must be on the KV allow-list AND issuer match.
bool issuerDnMatch = string.Equals(cert.Issuer, root.Subject,
StringComparison.OrdinalIgnoreCase);
bool pinnedMatch = false;
foreach (var e in "{{client-cert-allowlist}}".Split('|')) {
var kv = e.Split(':'); // "client1:THUMBPRINT"
if (kv.Length == 2 &&
string.Equals(kv[1].Trim(), cert.Thumbprint, StringComparison.OrdinalIgnoreCase))
pinnedMatch = true;
}
bool pinnedOk = timeOk && issuerDnMatch && pinnedMatch; // -> 200 when true
[PINNED] GET /poc/whoami with X-Client-Cert = <rogue PEM>
// Untrusted issuer -> rejected. The gateway forwarded the cert unvalidated;
// APIM is the trust anchor. In pinned mode the issuer DN must match the KV
// Root CA; in chain mode the Root CA key must have signed the cert.
r["reason"] = ok ? (mode == "chain" ? "CHAIN_OK" : "PINNED_OK")
: (mode == "chain"
? "NOT_CA_SIGNED" // signature check failed
: (!issuerDnMatch ? "UNTRUSTED_ISSUER"
: "NOT_IN_ALLOWLIST")); // -> 403
[PINNED] GET /poc/whoami with X-Client-Cert = (none)
// No certificate forwarded -> rejected before any trust check.
if (cert == null) {
r["ok"] = false;
r["reason"] = "NO_CERT_FORWARDED";
return r.ToString(); // -> 403 DENY
}
[PINNED] GET /poc/client1 with X-Client-Cert = <client1 PEM>
// Per-client authorization: only the client that owns cert1 may use path A.
// (client2 has the mirror policy for path B.)
<choose>
<when condition='@((string)((JObject)context.Variables["auth"])["client"] != "client1")'>
<return-response>
<set-status code="403" reason="Not authorised for this resource" />
</return-response>
</when>
</choose>
[PINNED] GET /poc/client2 with X-Client-Cert = <client1 PEM>
// Per-client authorization: only the client that owns cert1 may use path A.
// (client2 has the mirror policy for path B.)
<choose>
<when condition='@((string)((JObject)context.Variables["auth"])["client"] != "client1")'>
<return-response>
<set-status code="403" reason="Not authorised for this resource" />
</return-response>
</when>
</choose>
[CHAIN] GET /poc/whoami with X-Client-Cert = <client1 PEM>
// MODEL B (chain): a REAL RSA signature check - not a name compare.
// The sandbox blocks X509Chain and System.Func, so the leaf DER is parsed
// inline to pull out the TBSCertificate bytes and the signature, then the
// Key-Vault Root CA public key must verify that signature.
byte[] der = cert.RawData;
byte[] tbs = /* inline DER walk: SEQUENCE -> tbsCertificate bytes */;
byte[] sig = /* inline DER walk: signatureValue BIT STRING */;
var rsa = root.GetRSAPublicKey();
bool chainOk = rsa.VerifyData(tbs, sig,
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
bool chainModeOk = timeOk && chainOk; // accepts ANY cert the Root CA signed
// A cert that merely copies the Root CA's issuer NAME but is signed by another
// key fails here (chainOk=false) -> 403 NOT_CA_SIGNED. Cryptography, not string.
[CHAIN] GET /poc/whoami with X-Client-Cert = <client2 PEM>
// MODEL B (chain): a REAL RSA signature check - not a name compare.
// The sandbox blocks X509Chain and System.Func, so the leaf DER is parsed
// inline to pull out the TBSCertificate bytes and the signature, then the
// Key-Vault Root CA public key must verify that signature.
byte[] der = cert.RawData;
byte[] tbs = /* inline DER walk: SEQUENCE -> tbsCertificate bytes */;
byte[] sig = /* inline DER walk: signatureValue BIT STRING */;
var rsa = root.GetRSAPublicKey();
bool chainOk = rsa.VerifyData(tbs, sig,
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
bool chainModeOk = timeOk && chainOk; // accepts ANY cert the Root CA signed
// A cert that merely copies the Root CA's issuer NAME but is signed by another
// key fails here (chainOk=false) -> 403 NOT_CA_SIGNED. Cryptography, not string.
[CHAIN] GET /poc/whoami with X-Client-Cert = <client3 PEM>
// MODEL B (chain): a REAL RSA signature check - not a name compare.
// The sandbox blocks X509Chain and System.Func, so the leaf DER is parsed
// inline to pull out the TBSCertificate bytes and the signature, then the
// Key-Vault Root CA public key must verify that signature.
byte[] der = cert.RawData;
byte[] tbs = /* inline DER walk: SEQUENCE -> tbsCertificate bytes */;
byte[] sig = /* inline DER walk: signatureValue BIT STRING */;
var rsa = root.GetRSAPublicKey();
bool chainOk = rsa.VerifyData(tbs, sig,
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
bool chainModeOk = timeOk && chainOk; // accepts ANY cert the Root CA signed
// A cert that merely copies the Root CA's issuer NAME but is signed by another
// key fails here (chainOk=false) -> 403 NOT_CA_SIGNED. Cryptography, not string.
[CHAIN] GET /poc/whoami with X-Client-Cert = <spoofed PEM>
// MODEL B (chain): a REAL RSA signature check - not a name compare.
// The sandbox blocks X509Chain and System.Func, so the leaf DER is parsed
// inline to pull out the TBSCertificate bytes and the signature, then the
// Key-Vault Root CA public key must verify that signature.
byte[] der = cert.RawData;
byte[] tbs = /* inline DER walk: SEQUENCE -> tbsCertificate bytes */;
byte[] sig = /* inline DER walk: signatureValue BIT STRING */;
var rsa = root.GetRSAPublicKey();
bool chainOk = rsa.VerifyData(tbs, sig,
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
bool chainModeOk = timeOk && chainOk; // accepts ANY cert the Root CA signed
// A cert that merely copies the Root CA's issuer NAME but is signed by another
// key fails here (chainOk=false) -> 403 NOT_CA_SIGNED. Cryptography, not string.
[CHAIN] GET /poc/whoami with X-Client-Cert = <rogue PEM>
// MODEL B (chain): a REAL RSA signature check - not a name compare.
// The sandbox blocks X509Chain and System.Func, so the leaf DER is parsed
// inline to pull out the TBSCertificate bytes and the signature, then the
// Key-Vault Root CA public key must verify that signature.
byte[] der = cert.RawData;
byte[] tbs = /* inline DER walk: SEQUENCE -> tbsCertificate bytes */;
byte[] sig = /* inline DER walk: signatureValue BIT STRING */;
var rsa = root.GetRSAPublicKey();
bool chainOk = rsa.VerifyData(tbs, sig,
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
bool chainModeOk = timeOk && chainOk; // accepts ANY cert the Root CA signed
// A cert that merely copies the Root CA's issuer NAME but is signed by another
// key fails here (chainOk=false) -> 403 NOT_CA_SIGNED. Cryptography, not string.
[CHAIN] GET /poc/whoami with X-Client-Cert = (none)
// No certificate forwarded -> rejected before any trust check.
if (cert == null) {
r["ok"] = false;
r["reason"] = "NO_CERT_FORWARDED";
return r.ToString(); // -> 403 DENY
}
The API Management v2 tiers carry a documented limitation: context.Request.Certificate and TLS renegotiation are not supported. That would matter if trust were established at APIM’s own TLS layer — but this scenario never does that. Trust is decided entirely from the forwarded X-Client-Cert header, so the limitation does not apply.
To prove it, the identical dual-model policy, named values, and certificates were deployed to a standalone API Management v2 instance (public gateway) and the same matrix was run directly against its gateway. 11/11 checks passed — behaviour is identical to the v1 deployment, including the real RSA chain-of-trust signature check that rejects the forged-issuer spoofed certificate. The certificate-validation pattern is tier-agnostic: it works the same on API Management v1 and v2.
| Model | Check | Observed on API Management v2 | Result |
|---|---|---|---|
| PINNED | Allow-listed client1 -> 200 | HTTP 200 decision=ALLOW chainOk=True pinnedMatch=True | PASS |
| PINNED | CA-signed but NOT allow-listed client3 -> 403 | HTTP 403 decision=DENY reason=NOT_IN_ALLOWLIST chainOk=True pinnedMatch=False | PASS |
| PINNED | Forged-signature cert (same issuer DN) -> 403 | HTTP 403 decision=DENY reason=NOT_IN_ALLOWLIST chainOk=False pinnedMatch=False | PASS |
| PINNED | Untrusted issuer -> 403 | HTTP 403 decision=DENY reason=UNTRUSTED_ISSUER chainOk=False pinnedMatch=False | PASS |
| PINNED | No certificate -> 403 | HTTP 403 decision=DENY reason=NO_CERT_FORWARDED chainOk=False pinnedMatch=False | PASS |
| PINNED | client1 -> client2 path B -> 403 | HTTP 403 decision=DENY_AUTHZ reason=Authenticated client is not authorised for path B (client2) | PASS |
| CHAIN | CA-signed client1 -> 200 | HTTP 200 decision=ALLOW chainOk=True pinnedMatch=True | PASS |
| CHAIN | CA-signed client3 (no allow list needed) -> 200 | HTTP 200 decision=ALLOW chainOk=True pinnedMatch=False | PASS |
| CHAIN | Forged signature, identical issuer DN -> 403 | HTTP 403 decision=DENY reason=NOT_CA_SIGNED chainOk=False pinnedMatch=False | PASS |
| CHAIN | Untrusted issuer -> 403 | HTTP 403 decision=DENY reason=NOT_CA_SIGNED chainOk=False pinnedMatch=False | PASS |
| CHAIN | No certificate -> 403 | HTTP 403 decision=DENY reason=NO_CERT_FORWARDED chainOk=False pinnedMatch=False | PASS |
API Management v2 run · 2026-07-09 03:01 UTC
Behind a Layer-7 gateway, APIM's context.Request.Certificate is not the client cert (the gateway opens a separate TLS session to APIM). The client certificate is forwarded as a percent-encoded PEM in X-Client-Cert, set from the gateway's mutual-authentication server variables.
The APIM policy URL-decodes the header, strips the PEM armor, base64-decodes to DER, and builds an X509Certificate2. It then checks the validity window and applies the active model: a Key Vault-sourced thumbprint allow list (pinned) or a real Root CA signature verification (chain).
Sandbox limitations found at deploy time: the APIM policy-expression validator rejects DateTime.ToUniversalTime(), X509Chain, and System.Func. Validity uses DateTime.Now against the certificate’s local NotBefore/NotAfter; chain mode verifies trust by parsing the leaf DER inline (no X509Chain, no lambdas) and calling GetRSAPublicKey().VerifyData(...) with the Key Vault Root CA.
spoofed certificate (identical issuer name, forged signature) with chainOk=false. Trust is proven by the CA’s key, not by a name.X-Client-Cert* header from the real TLS connection, and APIM is reachable only from the gateway (internal VNet), so a forged header can’t be injected or bypass the gateway.
One thing to keep in mind: the gateway itself performs no certificate validation in passthrough, so
treat its client_certificate_verification value as informational only —
the header overwrite and the internal-VNet lockdown are what make the forwarded certificate trustworthy for APIM to validate.