Skip to content

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

SCEP/EST/enrollment permission improvements

Carlos Monastyrskib09ebe770f0f
diff --git a/backend/src/ee/services/pki-scep/pki-scep-fns.ts b/backend/src/ee/services/pki-scep/pki-scep-fns.ts
index 0a528147d3..9e77d539b0 100644
--- a/backend/src/ee/services/pki-scep/pki-scep-fns.ts
+++ b/backend/src/ee/services/pki-scep/pki-scep-fns.ts
@@ -1,4 +1,5 @@
import * as x509 from "@peculiar/x509";
+import RE2 from "re2";
import { extractX509CertFromChain } from "@app/lib/certificates/extract-certificate";
import { crypto } from "@app/lib/crypto/cryptography";
@@ -128,6 +129,62 @@ export const isSignerCertIssuedByCa = async ({
}
};
+export enum ScepRenewalDenyReason {
+ InvalidSigner = "invalid-signer",
+ WrongProfile = "wrong-profile",
+ IdentityMismatch = "identity-mismatch"
+}
+
+export type TScepRenewalAuthResult = { authorized: true } | { authorized: false; reason: ScepRenewalDenyReason };
+
+const DN_WHITESPACE_RE = new RE2("\\s+", "g");
+
+export const normalizeX500Name = (name: x509.Name): string => {
+ const rdns = name.toJSON();
+
+ const canonicalRdns = rdns.map((rdn) => {
+ const pairs: string[] = [];
+ for (const [type, values] of Object.entries(rdn)) {
+ const normalizedType = type.toLowerCase();
+ for (const value of values) {
+ const normalizedValue = value.normalize("NFC").replace(DN_WHITESPACE_RE, " ").trim().toLowerCase();
+ pairs.push(`${normalizedType}=${normalizedValue}`);
+ }
+ }
+ return pairs.sort().join("+");
+ });
+
+ return canonicalRdns.sort().join(",");
+};
+
+export const evaluateScepRenewalAuthorization = ({
+ isValidSigner,
+ storedSignerCert,
+ profileId,
+ csrSubjectName,
+ signerCertSubjectName
+}: {
+ isValidSigner: boolean;
+ storedSignerCert?: { profileId?: string | null } | null;
+ profileId: string;
+ csrSubjectName: x509.Name;
+ signerCertSubjectName: x509.Name;
+}): TScepRenewalAuthResult => {
+ if (!isValidSigner) {
+ return { authorized: false, reason: ScepRenewalDenyReason.InvalidSigner };
+ }
+
+ if (!storedSignerCert || storedSignerCert.profileId !== profileId) {
+ return { authorized: false, reason: ScepRenewalDenyReason.WrongProfile };
+ }
+
+ if (normalizeX500Name(csrSubjectName) !== normalizeX500Name(signerCertSubjectName)) {
+ return { authorized: false, reason: ScepRenewalDenyReason.IdentityMismatch };
+ }
+
+ return { authorized: true };
+};
+
export const getScepCapabilities = ({ allowCertBasedRenewal }: { allowCertBasedRenewal: boolean }): string => {
const caps = ["POSTPKIOperation", "SHA-256", "SHA-1", "AES", "DES3", "SCEPStandard"];
if (allowCertBasedRenewal) {
diff --git a/backend/src/ee/services/pki-scep/pki-scep-service.ts b/backend/src/ee/services/pki-scep/pki-scep-service.ts
index 106c469de0..874e247e06 100644
--- a/backend/src/ee/services/pki-scep/pki-scep-service.ts
+++ b/backend/src/ee/services/pki-scep/pki-scep-service.ts
@@ -50,7 +50,13 @@ import { convertRawCertsToPkcs7 } from "../certificate-est/certificate-est-fns";
import { TLicenseServiceFactory } from "../license/license-service";
import { getScepChallengeValidator, ScepChallengeType } from "./challenge";
import { TScepDynamicChallengeDALFactory } from "./pki-scep-dynamic-challenge-dal";
-import { getScepCapabilities, isSignerCertIssuedByCa } from "./pki-scep-fns";
+import {
+ evaluateScepRenewalAuthorization,
+ getScepCapabilities,
+ isSignerCertIssuedByCa,
+ ScepRenewalDenyReason,
+ TScepRenewalAuthResult
+} from "./pki-scep-fns";
import { buildCertRepFailure, buildCertRepPending, buildCertRepSuccess } from "./pki-scep-message-builder";
import { parseScepMessage } from "./pki-scep-message-parser";
import { TScepTransactionDALFactory } from "./pki-scep-transaction-dal";
@@ -552,16 +558,52 @@ export const pkiScepServiceFactory = ({
const csrObj = new x509.Pkcs10CertificateRequest(parsed.csr);
- const isValidSigner = await isSignerCertIssuedByCa({
- signerCertDer: parsed.signerCertDer,
- caId: profile.caId!,
- certificateDAL,
- certificateAuthorityCertDAL,
- certificateAuthorityDAL,
- projectDAL,
- kmsService
- });
- if (!isValidSigner) {
+ let signerCertObj: x509.X509Certificate | null = null;
+ try {
+ signerCertObj = new x509.X509Certificate(parsed.signerCertDer);
+ } catch {
+ signerCertObj = null;
+ }
+
+ const isValidSigner =
+ signerCertObj !== null &&
+ (await isSignerCertIssuedByCa({
+ signerCertDer: parsed.signerCertDer,
+ caId: profile.caId!,
+ certificateDAL,
+ certificateAuthorityCertDAL,
+ certificateAuthorityDAL,
+ projectDAL,
+ kmsService
+ }));
+
+ const storedSignerCert =
+ isValidSigner && signerCertObj
+ ? await certificateDAL.findOne({ serialNumber: signerCertObj.serialNumber, caId: profile.caId! })
+ : null;
+
+ const renewalAuth: TScepRenewalAuthResult = signerCertObj
+ ? evaluateScepRenewalAuthorization({
+ isValidSigner,
+ storedSignerCert,
+ profileId: profile.id,
+ csrSubjectName: csrObj.subjectName,
+ signerCertSubjectName: signerCertObj.subjectName
+ })
+ : { authorized: false, reason: ScepRenewalDenyReason.InvalidSigner };
+
+ if (!renewalAuth.authorized) {
+ const failReasonByDenyReason: Record<ScepRenewalDenyReason, string> = {
+ [ScepRenewalDenyReason.InvalidSigner]:
+ "Signer certificate is missing, malformed, expired, revoked, or does not chain to profile CA",
+ [ScepRenewalDenyReason.WrongProfile]: "Signer certificate does not belong to this profile",
+ [ScepRenewalDenyReason.IdentityMismatch]: "Renewal CSR identity does not match the renewing certificate"
+ };
+ const failInfo =
+ renewalAuth.reason === ScepRenewalDenyReason.IdentityMismatch
+ ? ScepFailInfo.BadRequest
+ : ScepFailInfo.BadCertId;
+
void auditLogService.createAuditLog({
projectId: profile.projectId,
actor: {
@@ -576,7 +618,7 @@ export const pkiScepServiceFactory = ({
transactionId: parsed.transactionId,
csrSubject: csrObj.subject,
status: "failure" as const,
- failReason: "Signer certificate is expired or does not chain to profile CA",
+ failReason: failReasonByDenyReason[renewalAuth.reason],
clientIp
}
}
@@ -587,7 +629,7 @@ export const pkiScepServiceFactory = ({
raPrivateKeyDer,
transactionId: parsed.transactionId,
recipientNonce: parsed.senderNonce,
- failInfo: ScepFailInfo.BadCertId
+ failInfo
});
}
diff --git a/backend/src/server/routes/v1/certificate-profiles-router.ts b/backend/src/server/routes/v1/certificate-profiles-router.ts
index ad1b55843c..8cf28fa689 100644
--- a/backend/src/server/routes/v1/certificate-profiles-router.ts
+++ b/backend/src/server/routes/v1/certificate-profiles-router.ts
@@ -429,7 +429,6 @@ export const registerCertificateProfilesRouter = async (
.object({
id: z.string(),
disableBootstrapCaValidation: z.boolean(),
- passphrase: z.string(),
caChain: z.string().optional()
})
.optional(),
diff --git a/backend/src/services/certificate-profile/certificate-profile-service.ts b/backend/src/services/certificate-profile/certificate-profile-service.ts
index 1262bdf375..7f54dc1456 100644
--- a/backend/src/services/certificate-profile/certificate-profile-service.ts
+++ b/backend/src/services/certificate-profile/certificate-profile-service.ts
@@ -321,6 +321,19 @@ const convertDalToService = (dalResult: Record<string, unknown>): TCertificatePr
} as TCertificateProfile;
};
+export const requiresIssueCertForEnrollmentConfig = (data: {
+ enrollmentType?: EnrollmentType;
+ estConfig?: unknown;
+ acmeConfig?: unknown;
+ scepConfig?: unknown;
+}): boolean =>
+ data.enrollmentType === EnrollmentType.EST ||
+ data.enrollmentType === EnrollmentType.ACME ||
+ data.enrollmentType === EnrollmentType.SCEP ||
+ data.estConfig !== undefined ||
+ data.acmeConfig !== undefined ||
+ data.scepConfig !== undefined;
+
export const certificateProfileServiceFactory = ({
certificateProfileDAL,
certificatePolicyDAL,
@@ -370,6 +383,15 @@ export const certificateProfileServiceFactory = ({
})
);
+ if (requiresIssueCertForEnrollmentConfig(data)) {
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionCertificateProfileActions.IssueCert,
+ subject(ProjectPermissionSub.CertificateProfiles, {
+ slug: data.slug
+ })
+ );
+ }
+
const enrollmentType = data.enrollmentType ?? EnrollmentType.API;
// Validate that certificate policy exists and belongs to the same project
@@ -620,6 +642,15 @@ export const certificateProfileServiceFactory = ({
})
);
+ if (requiresIssueCertForEnrollmentConfig(data)) {
+ ForbiddenError.from(permission).throwUnlessCan(
+ ProjectPermissionCertificateProfileActions.IssueCert,
+ subject(ProjectPermissionSub.CertificateProfiles, {
+ slug: existingProfile.slug
+ })
+ );
+ }
+
if (data.certificatePolicyId) {
const policy = await certificatePolicyDAL.findById(data.certificatePolicyId);
if (!policy) {
@@ -964,6 +995,10 @@ export const certificateProfileServiceFactory = ({
profile.estConfig.caChain = "";
}
}
+
+ if (profile.estConfig) {
+ profile.estConfig.passphrase = "";
+ }
if (profile.enrollmentType === EnrollmentType.ACME && profile.acmeConfig) {
profile.acmeConfig.directoryUrl = buildUrl(profile.id, "/directory");
if (profile.acmeConfig.encryptedEabSecret) {
diff --git a/frontend/src/hooks/api/certificateProfiles/types.ts b/frontend/src/hooks/api/certificateProfiles/types.ts
index e585444577..2e37baf8c9 100644
--- a/frontend/src/hooks/api/certificateProfiles/types.ts
+++ b/frontend/src/hooks/api/certificateProfiles/types.ts
@@ -73,7 +73,7 @@ export type TCertificateProfileWithDetails = TCertificateProfile & {
estConfig?: {
id: string;
disableBootstrapCaValidation: boolean;
- passphrase: string;
+ passphrase?: string;
caChain: string;
};
apiConfig?: {