Skip to content

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

fix(pki): read the key-derivation cost inside safe contents, not just the outer layer

Saifd85f86bdc559
diff --git a/backend/src/services/certificate/certificate-pkcs12-fixtures.ts b/backend/src/services/certificate/certificate-pkcs12-fixtures.ts
index d7d22ca16e..22732adf4b 100644
--- a/backend/src/services/certificate/certificate-pkcs12-fixtures.ts
+++ b/backend/src/services/certificate/certificate-pkcs12-fixtures.ts
@@ -762,3 +762,40 @@ export const notYetValidLeaf =
"WRGFHni4Tzd6+DFMMCMGCSqGSIb3DQEJFTEWBBTp4g5Hocxi2mOSJTcB8HGMH7wwszAlBgkqhkiG9w0BCRQxGB4WAGYAdQB0AHUA" +
"cgBlAC0AbABlAGEAZjBJMDEwDQYJYIZIAWUDBAIBBQAEILGaN9p2ShHubwc6M9PVXnVw7QLP4h06KkLA/zL+o3hbBBDypgyzDgNx" +
"YcaLqnqtT1kQAgIIAA==";
+
+// Cheap MAC and unencrypted certificates, with the cost hidden on the shrouded key inside the
+// safe contents octet string. Generated with -certpbe NONE -keypbe AES-256-CBC -iter 5000000 -nomaciter.
+export const hostileNestedIterations =
+ "MIIJngIBAzCCCVAGCSqGSIb3DQEHAaCCCUEEggk9MIIJOTCCA5kGCSqGSIb3DQEHAaCCA4oEggOGMIIDgjCCA34GCyqGSIb3DQEM" +
+ "CgEDoIIDKzCCAycGCiqGSIb3DQEJFgGgggMXBIIDEzCCAw8wggH3oAMCAQICFDBgThj4MPuBd7RdvqMVeX8RFp05MA0GCSqGSIb3" +
+ "DQEBCwUAMBcxFTATBgNVBAMMDGJ5cGFzcy1wcm9iZTAeFw0yNjA4MTgxNzU5NDNaFw0yNjA5MTcxNzU5NDNaMBcxFTATBgNVBAMM" +
+ "DGJ5cGFzcy1wcm9iZTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKtb9uVCJBZuJm7ye1+YxGFRUnmeeAZtdRouaSAj" +
+ "RzGUJrwOUbdo/p3pq2bDiC3q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FWknbH21NUouFJi7yl3/ZmuywyVvhsQc8" +
+ "L4F+gRgiJQwScCbfh15Mc/CIQdwxigPjLnIJBdOaJ0NhSCTDC+b9ekFsT/AAecx9/WK+aPzpxFnjc0uBy76LF3yR3xaS6aX0WAyp" +
+ "XSF0+pNQFnKVF90C5vErvdeFt0Dg22keAAAIF9pKTdOpbWxcMK3/ELfVrd6d62gw2puoZUqVd4wQGNmXrT74glvzVqT3hYIZzDX4" +
+ "BhKzourrB0gJ+iOi6EP5B+P6Ymnv7nZTdSWML8sEF09uRjKN3+5CCOakqagfytkmlmi4JgUNzmSlq74Oo4zNZcPFrDVd7l9wmw2L" +
+ "FSDq+mv+YGUIdo9ZC4NrHiKK1pE5rmh0QGE4mF9oxV5aYxiKKgPYK35wX0ha90ZbKbbzlFkd8pG5Ya4JOOBgVMO533R9b/11E42m" +
+ "uL7io0beIwaTH1yWVHxhd0qP4zANwuwKbwhfFqJ/ltkHAItwOK3MUc35DtnvTwGR+MLZBhZjboornsApSKplRpMBe5s1Yn1vhnAz" +
+ "6Oc5ah2b9GsLcbMA5WHzH/vIJx4kkfq7zLcNfjnTkldz4YvsPKhtWWUMCC17G8Sn3pUpabfbPHLeXFS5QwT1gWmzLN+J123bamcH" +
+ "Hyb6IDFAMBkGCSqGSIb3DQEJFDEMHgoAcAByAG8AYgBlMCMGCSqGSIb3DQEJFTEWBBRDZG5sge6Vr7P8A3fc7Ntc8duvhTBFMDEw" +
+ "DQYJYIZIAWUDBAIBBQAEIJwZb0itw8U8GBDcFxTxgMl2T9WPvwR/m97S0eOazp0SBBBdcmtDi6ReeORy2fujzZs3";
diff --git a/backend/src/services/certificate/certificate-pkcs12-fns.test.ts b/backend/src/services/certificate/certificate-pkcs12-fns.test.ts
index 66c1504458..403f23be6e 100644
--- a/backend/src/services/certificate/certificate-pkcs12-fns.test.ts
+++ b/backend/src/services/certificate/certificate-pkcs12-fns.test.ts
@@ -204,6 +204,10 @@ describe("extractPkcs12Entries failures", () => {
await expectFailure(fixtures.hostileIterations, "test", Pkcs12ErrorCode.TooExpensive);
});
+ test("refuses one that hides the cost on a key inside the safe contents", async () => {
+ await expectFailure(fixtures.hostileNestedIterations, "test", Pkcs12ErrorCode.TooExpensive);
+ });
+
test("reports a file that is not a keystore", async () => {
await expectFailure(Buffer.from("this is not a keystore").toString("base64"), "test", Pkcs12ErrorCode.NotAKeystore);
});
diff --git a/backend/src/services/certificate/certificate-pkcs12-fns.ts b/backend/src/services/certificate/certificate-pkcs12-fns.ts
index 53f5268d03..659dc577f9 100644
--- a/backend/src/services/certificate/certificate-pkcs12-fns.ts
+++ b/backend/src/services/certificate/certificate-pkcs12-fns.ts
@@ -264,7 +264,9 @@ const buildChain = async (leaf: TParsedCert, certs: TParsedCert[]) => {
// A keystore states its own key-derivation cost in the clear, so an expensive one is refused before
// any decryption happens rather than by running out of time. PBKDF2 and the PKCS#12 PBE algorithms
-// all carry the count as the first integer of their parameters.
+// all carry the count as the first integer of their parameters. The counts are summed rather than
+// compared one by one, since the cost of opening the file is the sum of every derivation it asks
+// for. Real keystores are nowhere near the ceiling: openssl writes 2048 and keytool 10000.
const ITERATION_PARAM_OIDS = new Set([
"1.2.840.113549.1.5.12",
"1.2.840.113549.1.12.1.1",
@@ -275,7 +277,8 @@ const ITERATION_PARAM_OIDS = new Set([
"1.2.840.113549.1.12.1.6"
]);
-const MAX_KDF_ITERATIONS = 1_000_000;
+const MAX_KDF_ROUNDS = 500_000;
+const MAX_NESTING_DEPTH = 16;
const asn1Integer = (node: forge.asn1.Asn1) => {
if (typeof node.value !== "string") return 0;
@@ -287,16 +290,28 @@ const asn1Integer = (node: forge.asn1.Asn1) => {
}
};
-const declaredIterations = (node: forge.asn1.Asn1): number => {
- if (!Array.isArray(node.value)) return 0;
+const declaredKdfRounds = (node: forge.asn1.Asn1, depth = 0): number => {
+ if (depth > MAX_NESTING_DEPTH) return 0;
+
+ // A PKCS#12 carries its safe contents as DER inside an octet string, and the shrouded key bags
+ // that state the largest counts live in there, so the walk has to open them.
+ if (!Array.isArray(node.value)) {
+ if (node.type !== forge.asn1.Type.OCTETSTRING || typeof node.value !== "string") return 0;
+ try {
+ return declaredKdfRounds(forge.asn1.fromDer(node.value), depth + 1);
+ } catch {
+ return 0;
+ }
+ }
+
const children = node.value;
- let max = 0;
+ let total = 0;
const [first, second, third] = children;
if (first?.type === forge.asn1.Type.OID && typeof first.value === "string" && Array.isArray(second?.value)) {
if (ITERATION_PARAM_OIDS.has(forge.asn1.derToOid(first.value))) {
const count = second.value.find((param) => param.type === forge.asn1.Type.INTEGER);
- if (count) max = Math.max(max, asn1Integer(count));
+ if (count) total += asn1Integer(count);
}
}
@@ -307,14 +322,14 @@ const declaredIterations = (node: forge.asn1.Asn1): number => {
second?.type === forge.asn1.Type.OCTETSTRING &&
third?.type === forge.asn1.Type.INTEGER
) {
- max = Math.max(max, asn1Integer(third));
+ total += asn1Integer(third);
}
children.forEach((child) => {
- max = Math.max(max, declaredIterations(child));
+ total += declaredKdfRounds(child, depth + 1);
});
- return max;
+ return total;
};
export const extractPkcs12Entries = async ({
@@ -331,7 +346,7 @@ export const extractPkcs12Entries = async ({
throw new Pkcs12ExtractionError(Pkcs12ErrorCode.NotAKeystore);
}
- if (declaredIterations(asn1) > MAX_KDF_ITERATIONS) {
+ if (declaredKdfRounds(asn1) > MAX_KDF_ROUNDS) {
throw new Pkcs12ExtractionError(Pkcs12ErrorCode.TooExpensive);
}
diff --git a/backend/src/services/certificate/certificate-pkcs12-runner.ts b/backend/src/services/certificate/certificate-pkcs12-runner.ts
index 23de368e0f..4e99fb76df 100644
--- a/backend/src/services/certificate/certificate-pkcs12-runner.ts
+++ b/backend/src/services/certificate/certificate-pkcs12-runner.ts
@@ -8,7 +8,7 @@ import { Pkcs12ErrorCode, TExtractPkcs12Result, TPkcs12ErrorCode } from "./certi
// Cost is refused up front by the extraction itself, so this is only a backstop for anything that
// check cannot see, and node-forge decrypts synchronously so it has to run off the event loop.
-const EXTRACTION_TIMEOUT_MS = 15_000;
+const EXTRACTION_TIMEOUT_MS = 5_000;
const MAX_CONCURRENT_EXTRACTIONS = 2;
let inFlight = 0;