Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

check EKU

Thiago Araujo da Silva7d8a7e493513
diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts
index 4d58a1193d..211b24e2fe 100644
--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts
+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.test.ts
@@ -10,6 +10,7 @@ import {
normalizeAllowedSubjectAltName,
parseCertificateSubjectAltNames,
parseSubjectDetails,
+ permitsClientAuth,
TCertificateSanItem,
verifyClientCertificateChain
} from "./identity-tls-cert-auth-fns";
@@ -240,6 +241,57 @@ describe("isSubjectAltNameAllowed", () => {
});
});
+describe("permitsClientAuth", () => {
+ const alg: RsaHashedKeyGenParams = {
+ name: "RSASSA-PKCS1-v1_5",
+ hash: "SHA-256",
+ publicExponent: new Uint8Array([1, 0, 1]),
+ modulusLength: 2048
+ };
+
+ x509.cryptoProvider.set(crypto.webcrypto as Crypto);
+
+ const makeLeafWithUsages = async (usages?: string[]) => {
+ const keys = await crypto.webcrypto.subtle.generateKey(alg, true, ["sign", "verify"]);
+ const cert = await x509.X509CertificateGenerator.createSelfSigned({
+ serialNumber: "04",
+ name: "CN=workload",
+ notBefore: new Date("2026-06-01T00:00:00Z"),
+ notAfter: new Date("2030-01-01T00:00:00Z"),
+ keys,
+ extensions: [
+ new x509.BasicConstraintsExtension(false),
+ ...(usages ? [new x509.ExtendedKeyUsageExtension(usages, true)] : [])
+ ]
+ });
+ return new crypto.X509Certificate(Buffer.from(cert.rawData));
+ };
+
+ test("accepts a certificate that asserts no extended key usage", async () => {
+ expect(permitsClientAuth(await makeLeafWithUsages())).toBe(true);
+ });
+
+ test("accepts a certificate that asserts clientAuth", async () => {
+ expect(permitsClientAuth(await makeLeafWithUsages(["1.3.6.1.5.5.7.3.2"]))).toBe(true);
+ });
+
+ test("accepts a certificate that asserts clientAuth alongside other usages", async () => {
+ expect(permitsClientAuth(await makeLeafWithUsages(["1.3.6.1.5.5.7.3.1", "1.3.6.1.5.5.7.3.2"]))).toBe(true);
+ });
+
+ test("accepts a certificate that asserts anyExtendedKeyUsage", async () => {
+ expect(permitsClientAuth(await makeLeafWithUsages(["2.5.29.37.0"]))).toBe(true);
+ });
+
+ test("rejects a serverAuth-only certificate", async () => {
+ expect(permitsClientAuth(await makeLeafWithUsages(["1.3.6.1.5.5.7.3.1"]))).toBe(false);
+ });
+
+ test("rejects a codeSigning-only certificate", async () => {
+ expect(permitsClientAuth(await makeLeafWithUsages(["1.3.6.1.5.5.7.3.3"]))).toBe(false);
+ });
+});
+
describe("verifyClientCertificateChain", () => {
const alg: RsaHashedKeyGenParams = {
name: "RSASSA-PKCS1-v1_5",
diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts
index 561a35bad5..8182fc3d8f 100644
--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts
+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-fns.ts
@@ -4,6 +4,10 @@ import * as x509 from "@peculiar/x509";
import { Certificate, CertificateChainValidationEngine, CryptoEngine, NameConstraints } from "pkijs";
import { crypto } from "@app/lib/crypto/cryptography";
+import {
+ CERT_EXTENDED_KEY_USAGES,
+ CertExtendedKeyUsageType
+} from "@app/services/certificate-common/certificate-constants";
type TNativeX509 = InstanceType<typeof crypto.nativeCrypto.X509Certificate>;
@@ -23,6 +27,9 @@ type TVerificationFailure = Extract<TVerifyClientCertificateChainResult, { ok: f
const NAME_CONSTRAINTS_EXTENSION_OID = "2.5.29.30";
+const CLIENT_AUTH_EKU_OID = CERT_EXTENDED_KEY_USAGES[CertExtendedKeyUsageType.CLIENT_AUTH].oid;
+const ANY_PURPOSE_EKU_OID = CERT_EXTENDED_KEY_USAGES[CertExtendedKeyUsageType.ANY_PURPOSE].oid;
+
const PERMITTED_SUBTREE_VIOLATION_CODE = 41;
const EXCLUDED_SUBTREE_VIOLATION_CODE = 42;
@@ -248,6 +255,22 @@ export const verifyClientCertificateChain = async ({
}
};
+/**
+ * RFC 5280 4.2.1.12: an Extended Key Usage extension is the exhaustive list of purposes its
+ * certificate may be used for. Without this check a leaf issued for serverAuth or codeSigning
+ * authenticates an identity, so anyone holding a server certificate under the configured CA can
+ * impersonate the machine identity pinned to it.
+ *
+ * A certificate that omits the extension is unconstrained and stays accepted, which is how OpenSSL
+ * and Go read a missing EKU and what keeps existing leaves that carry no EKU working. Only a
+ * certificate that explicitly enumerates purposes and leaves client authentication out is rejected.
+ */
+export const permitsClientAuth = (cert: TNativeX509): boolean => {
+ const usages = new x509.X509Certificate(cert.raw).getExtension(x509.ExtendedKeyUsageExtension)?.usages;
+ if (!usages) return true;
+ return usages.some((oid) => oid === CLIENT_AUTH_EKU_OID || oid === ANY_PURPOSE_EKU_OID);
+};
+
export const parseSubjectDetails = (data?: string | null) => {
const values: Record<string, string> = {};
if (!data) return values;
diff --git a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
index ea0dc760e1..02c39b5526 100644
--- a/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
+++ b/backend/src/services/identity-tls-cert-auth/identity-tls-cert-auth-service.ts
@@ -48,6 +48,7 @@ import {
isSubjectAltNameAllowed,
parseAllowedSubjectAltNames,
parseSubjectDetails,
+ permitsClientAuth,
serializeAllowedSubjectAltNames,
TVerifyClientCertificateChainResult,
verifyClientCertificateChain
@@ -205,6 +206,18 @@ export const identityTlsCertAuthServiceFactory = ({
});
}
+ if (!permitsClientAuth(clientCertificateX509)) {
+ throw new UnauthorizedError({
+ message: "Access denied: the client certificate is not valid for client authentication.",
+ detail: {
+ reasonCode: "client_auth_usage_not_allowed",
+ identityId: identity.id,
+ orgId: identity.orgId,
+ identityName: identity.name
+ }
+ });
+ }
+
if (new Date(clientCertificateX509.validTo) < new Date()) {
throw new UnauthorizedError({
message: "Access denied: Certificate has expired.",