Skip to content

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

fix nameconstrain

Thiago Araujo da Silva92b9359f0121
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 4706fb0c82..71d7a1f9b7 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
@@ -1,6 +1,7 @@
import crypto from "node:crypto";
import * as x509 from "@peculiar/x509";
+import * as asn1js from "asn1js";
import { GeneralName, GeneralSubtree, NameConstraints } from "pkijs";
import { beforeAll, describe, expect, test, vi } from "vitest";
@@ -358,6 +359,19 @@ describe("readSubjectAltNames", () => {
});
});
+// A critical name constraints extension whose value is well-formed DER but does not match the
+// NameConstraints schema, and one whose value is not DER at all. pkijs cannot read either, so the
+// namespace the CA meant to restrict is unknown and the chain must be denied rather than accepted as
+// unconstrained.
+const schemaMismatchedNameConstraint = () =>
+ new x509.Extension(
+ "2.5.29.30",
+ true,
+ new asn1js.Sequence({ value: [new asn1js.Integer({ value: 1 })] }).toBER(false)
+ );
+
+const undecodableNameConstraint = () => new x509.Extension("2.5.29.30", true, new Uint8Array([0x99, 0x99, 0x99]));
+
describe("verifyDirectlyIssuedClientCertificate", () => {
const alg: RsaHashedKeyGenParams = {
name: "RSASSA-PKCS1-v1_5",
@@ -393,7 +407,11 @@ describe("verifyDirectlyIssuedClientCertificate", () => {
return { cert, keys };
};
- const makeLeaf = async (issuer: { cert: x509.X509Certificate; keys: CryptoKeyPair }, dnsName: string) => {
+ const makeLeaf = async (
+ issuer: { cert: x509.X509Certificate; keys: CryptoKeyPair },
+ dnsName: string,
+ extraExtensions: x509.Extension[] = []
+ ) => {
const keys = await crypto.webcrypto.subtle.generateKey(alg, true, ["sign", "verify"]);
const cert = await x509.X509CertificateGenerator.create({
serialNumber: "02",
@@ -406,7 +424,8 @@ describe("verifyDirectlyIssuedClientCertificate", () => {
signingAlgorithm: alg,
extensions: [
new x509.BasicConstraintsExtension(false),
- new x509.SubjectAlternativeNameExtension([{ type: "dns", value: dnsName }])
+ new x509.SubjectAlternativeNameExtension([{ type: "dns", value: dnsName }]),
+ ...extraExtensions
]
});
return { cert, keys };
@@ -493,6 +512,35 @@ describe("verifyDirectlyIssuedClientCertificate", () => {
await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })
).toEqual({ ok: true });
});
+
+ test("rejects when the configured CA's name constraints do not match the schema", async () => {
+ const ca = await makeCa("Malformed NC CA", { extensions: [schemaMismatchedNameConstraint()] });
+ const leaf = await makeLeaf(ca, "workload.example.com");
+
+ expect(
+ await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })
+ ).toEqual({ ok: false, reasonCode: "ca_verification_failed" });
+ });
+
+ test("rejects when the configured CA's name constraints are not decodable", async () => {
+ const ca = await makeCa("Undecodable NC CA", { extensions: [undecodableNameConstraint()] });
+ const leaf = await makeLeaf(ca, "workload.example.com");
+
+ expect(
+ await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })
+ ).toEqual({ ok: false, reasonCode: "ca_verification_failed" });
+ });
+
+ // Constraints restrict what sits below the certificate carrying them, so a leaf's own copy governs
+ // nothing and an unreadable one is not grounds to deny.
+ test("accepts a leaf whose own name constraints cannot be read", async () => {
+ const ca = await makeCa("Root CA");
+ const leaf = await makeLeaf(ca, "workload.example.com", [schemaMismatchedNameConstraint()]);
+
+ expect(
+ await verifyDirectlyIssuedClientCertificate({ leaf: toNative(leaf.cert), ca: toNative(ca.cert), now: NOW })
+ ).toEqual({ ok: true });
+ });
});
describe("verifyClientCertificateChain", () => {
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 af21052b79..31f9304b60 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
@@ -128,9 +128,31 @@ const enforcePathLength = (orderedPath: TNativeX509[]): TVerificationFailure | n
return exceeded ? { ok: false, reasonCode: "path_length_exceeded" } : null;
};
-const nameConstraintsOf = (cert: Certificate): NameConstraints | null => {
+type TNameConstraintsState =
+ | { status: "absent" }
+ | { status: "unreadable" }
+ | { status: "present"; constraints: NameConstraints };
+
+/**
+ * A certificate that carries name constraints this code cannot read has asserted a restriction that
+ * cannot be honored, which is not the same as asserting none: reading it as "no constraints" would
+ * let a CA that means to restrict its subordinates certify anything. The extension is critical per
+ * RFC 5280 4.2.1.10, so an unreadable one has to deny rather than be skipped.
+ *
+ * pkijs surfaces an unreadable extension two ways, both covered here: `parsedValue` is undefined
+ * when the extension value is not valid DER at all, and is an otherwise-empty `NameConstraints`
+ * when the value parses as ASN.1 but not against the schema. Neither carries a subtree list, and a
+ * conforming extension always carries at least one.
+ */
+const nameConstraintsOf = (cert: Certificate): TNameConstraintsState => {
const extension = cert.extensions?.find((ext) => ext.extnID === NAME_CONSTRAINTS_EXTENSION_OID);
- return extension?.parsedValue instanceof NameConstraints ? extension.parsedValue : null;
+ if (!extension) return { status: "absent" };
+
+ if (!(extension.parsedValue instanceof NameConstraints)) return { status: "unreadable" };
+
+ const constraints = extension.parsedValue;
+ if (!constraints.permittedSubtrees && !constraints.excludedSubtrees) return { status: "unreadable" };
+ return { status: "present", constraints };
};
/**
@@ -145,16 +167,28 @@ const nameConstraintsOf = (cert: Certificate): NameConstraints | null => {
* constrained sub-CA directly rather than the root above it.
*
* Only runs when some certificate on the path actually asserts constraints, so a chain that has
- * none is validated exactly as before.
+ * none is validated exactly as before. A CA asserting constraints that cannot be read denies before
+ * that point; see `nameConstraintsOf`.
*/
const enforceNameConstraints = async (orderedPath: TNativeX509[], now: Date): Promise<TVerificationFailure | null> => {
if (orderedPath.length < 2) return null;
const parsedPath = orderedPath.map((cert) => Certificate.fromBER(cert.raw));
- if (!parsedPath.some((cert) => nameConstraintsOf(cert))) return null;
+ const pathConstraints = parsedPath.map(nameConstraintsOf);
+
+ const unreadableIssuer = pathConstraints.findIndex((state, idx) => idx > 0 && state.status === "unreadable");
+ if (unreadableIssuer !== -1) {
+ logger.warn(
+ `TLS certificate auth: a CA on the chain asserts name constraints that could not be read [subject=${orderedPath[unreadableIssuer].subject}]`
+ );
+ return { ok: false, reasonCode: "ca_verification_failed" };
+ }
+
+ if (!pathConstraints.some((state) => state.status === "present")) return null;
const anchor = parsedPath[parsedPath.length - 1];
- const anchorConstraints = nameConstraintsOf(anchor);
+ const anchorState = pathConstraints[pathConstraints.length - 1];
+ const anchorConstraints = anchorState.status === "present" ? anchorState.constraints : null;
const engine = new CertificateChainValidationEngine({
trustedCerts: [anchor],