Skip to content

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

63.3 KB
Raw Blame
import { ForbiddenError, subject } from "@casl/ability";
import * as x509 from "@peculiar/x509";

import { ActionProjectType, ResourceType } from "@app/db/schemas";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
  ProjectPermissionCertificateActions,
  ProjectPermissionCertificateProfileActions,
  ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import {
  ResourcePermissionApplicationActions,
  ResourcePermissionCertificateActions,
  ResourcePermissionSub
} from "@app/ee/services/permission/resource-permission";
import { buildUrl } from "@app/ee/services/pki-acme/pki-acme-fns";
import { ScepChallengeType } from "@app/ee/services/pki-scep/challenge";
import { TScepDynamicChallengeDALFactory } from "@app/ee/services/pki-scep/pki-scep-dynamic-challenge-dal";
import { generateAndEncryptScepRaCertificate, resolveScepRaSigning } from "@app/ee/services/pki-scep/pki-scep-fns";
import { getProcessedPermissionRules } from "@app/lib/casl/permission-filter-utils";
import { extractX509CertFromChain } from "@app/lib/certificates/extract-certificate";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, ConflictError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";

import { ActorAuthMethod, ActorType } from "../auth/auth-type";
import type { TCaKeySourceServiceFactory } from "../certificate-authority/ca-key-source/ca-key-source-service";
import { TCertificateAuthorityCertDALFactory } from "../certificate-authority/certificate-authority-cert-dal";
import { TCertificateAuthorityDALFactory } from "../certificate-authority/certificate-authority-dal";
import { CaCapability, CaType } from "../certificate-authority/certificate-authority-enums";
import {
  caSupportsCapability,
  CERTIFICATE_AUTHORITIES_TYPE_MAP
} from "../certificate-authority/certificate-authority-maps";
import { TCertificateAuthoritySecretDALFactory } from "../certificate-authority/certificate-authority-secret-dal";
import { TExternalCertificateAuthorityDALFactory } from "../certificate-authority/external-certificate-authority-dal";
import {
  MAX_CUSTOM_EXTENSIONS_PER_AWS_PCA_PROFILE,
  MAX_CUSTOM_EXTENSIONS_PER_PROFILE
} from "../certificate-common/certificate-constants";
import {
  describeReservedExtensionOid,
  isReservedExtensionOid,
  resolveCustomExtensions,
  TCustomExtensionRule
} from "../certificate-common/certificate-extension-fns";
import { isSignatureAlgorithmCompatibleWithCaKey } from "../certificate-common/certificate-issuance-utils";
import { TCertificatePolicyDALFactory } from "../certificate-policy/certificate-policy-dal";
import { TCertificatePolicyServiceFactory } from "../certificate-policy/certificate-policy-service";
import { TCertificateRequest } from "../certificate-policy/certificate-policy-types";
import { TCertificateBodyDALFactory } from "../certificate/certificate-body-dal";
import { getCertificateCredentials, isCertChainValid } from "../certificate/certificate-fns";
import { TCertificateSecretDALFactory } from "../certificate/certificate-secret-dal";
import { CertStatus } from "../certificate/certificate-types";
import { TAcmeEnrollmentConfigDALFactory } from "../enrollment-config/acme-enrollment-config-dal";
import { TApiEnrollmentConfigDALFactory } from "../enrollment-config/api-enrollment-config-dal";
import {
  TAcmeConfigData,
  TApiConfigData,
  TEstConfigData,
  TScepConfigData
} from "../enrollment-config/enrollment-config-types";
import { TEstEnrollmentConfigDALFactory } from "../enrollment-config/est-enrollment-config-dal";
import { TScepEnrollmentConfigDALFactory } from "../enrollment-config/scep-enrollment-config-dal";
import { TKmsServiceFactory } from "../kms/kms-service";
import { TPkiApplicationProfileDALFactory } from "../pki-application/pki-application-profile-dal";
import { TProjectDALFactory } from "../project/project-dal";
import { getProjectKmsCertificateKeyId } from "../project/project-fns";
import { TResourceMetadataDALFactory } from "../resource-metadata/resource-metadata-dal";
import { TCertificateProfileDALFactory } from "./certificate-profile-dal";
import {
  EnrollmentType,
  IssuerType,
  TCertificateProfile,
  TCertificateProfileCertificate,
  TCertificateProfileDefaults,
  TCertificateProfileInsert,
  TCertificateProfileUpdate,
  TCertificateProfileWithConfigs
} from "./certificate-profile-types";

const validateIssuerTypeConstraints = (
  issuerType: IssuerType,
  enrollmentType: EnrollmentType,
  caId: string | null,
  existingCaId?: string | null
) => {
  if (issuerType === IssuerType.CA) {
    if (!caId && !existingCaId) {
      throw new ForbiddenRequestError({
        message: "CA issuer type requires a Certificate Authority to be selected"
      });
    }
  }

  if (issuerType === IssuerType.SELF_SIGNED) {
    if (caId) {
      throw new ForbiddenRequestError({
        message: "Self-signed issuer type cannot have a Certificate Authority"
      });
    }
    if (enrollmentType !== EnrollmentType.API) {
      throw new ForbiddenRequestError({
        message: "Self-signed issuer type only supports API enrollment"
      });
    }
  }
};

/**
 * A default the issuing CA cannot sign is worse than an invalid request: it is stored, and then every
 * request that omits a signature algorithm inherits it and fails at issuance.
 */
const validateDefaultSignatureAlgorithmAgainstCa = async (
  signatureAlgorithm: string | undefined,
  caId: string | null | undefined,
  certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findByIdWithAssociatedCa">
) => {
  if (!signatureAlgorithm || !caId) return;

  const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
  const caKeyAlgorithm = ca?.internalCa?.keyAlgorithm;
  // External CAs pick their own signing key, so there is nothing to check against here.
  if (!caKeyAlgorithm) return;

  if (!isSignatureAlgorithmCompatibleWithCaKey(signatureAlgorithm, caKeyAlgorithm)) {
    throw new BadRequestError({
      message: `Default signature algorithm ${signatureAlgorithm} is not compatible with the certificate authority's ${caKeyAlgorithm} key`
    });
  }
};

const validateTemplateByExternalCaType = (
  externalCaType: CaType | undefined,
  externalConfigs: Record<string, unknown> | null | undefined
) => {
  if (!externalCaType) return;

  switch (externalCaType) {
    case CaType.AZURE_AD_CS:
      if (!externalConfigs?.template || typeof externalConfigs.template !== "string") {
        throw new ForbiddenRequestError({
          message: "Azure ADCS Certificate Authority requires a template to be specified in external configs"
        });
      }
      break;
    case CaType.ADCS:
      if (!externalConfigs?.template || typeof externalConfigs.template !== "string") {
        throw new ForbiddenRequestError({
          message:
            "Active Directory Certificate Service Certificate Authority requires a template to be specified in external configs"
        });
      }
      break;
    default:
      break;
  }
};

const validateAcmEnrollmentType = async (
  caId: string | null | undefined,
  enrollmentType: EnrollmentType,
  externalCertificateAuthorityDAL: Pick<TExternalCertificateAuthorityDALFactory, "findOne">
) => {
  if (!caId) return;
  const externalCa = await externalCertificateAuthorityDAL.findOne({ caId });
  if (externalCa?.type === CaType.AWS_ACM_PUBLIC_CA && enrollmentType !== EnrollmentType.API) {
    throw new ForbiddenRequestError({
      message: "AWS Certificate Manager only supports API enrollment"
    });
  }
};

const validateCaProjectMatch = async (
  caId: string | null | undefined,
  projectId: string,
  certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findById">
) => {
  if (!caId) return;
  const ca = await certificateAuthorityDAL.findById(caId);
  if (!ca) {
    throw new NotFoundError({ message: "Certificate Authority not found" });
  }
  if (ca.projectId !== projectId) {
    throw new ForbiddenRequestError({
      message: "Invalid Certificate Authority"
    });
  }
};

const validateExternalConfigs = async (
  externalConfigs: Record<string, unknown> | null | undefined,
  caId: string | null,
  certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findById">,
  externalCertificateAuthorityDAL: Pick<TExternalCertificateAuthorityDALFactory, "findOne">
) => {
  if (!externalConfigs) return;

  if (!caId) {
    throw new ForbiddenRequestError({
      message: "External configs can only be specified when a Certificate Authority is selected"
    });
  }

  const ca = await certificateAuthorityDAL.findById(caId);
  if (!ca) {
    throw new NotFoundError({ message: "Certificate Authority not found" });
  }

  const externalCa = await externalCertificateAuthorityDAL.findOne({ caId });

  if (!externalCa) {
    throw new ForbiddenRequestError({
      message: "External configs can only be specified for external Certificate Authorities"
    });
  }

  validateTemplateByExternalCaType(externalCa.type as CaType, externalConfigs);
};

export const generateAndEncryptAcmeEabSecret = async (
  projectId: string,
  kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey">,
  projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction">
) => {
  try {
    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId,
      projectDAL,
      kmsService
    });

    const kmsEncryptor = await kmsService.encryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });

    const appCfg = getConfig();
    const secret = crypto.randomBytes(32).toString("hex");
    const secretHash = await crypto.hashing().createHash(secret, appCfg.SALT_ROUNDS);

    const { cipherTextBlob } = await kmsEncryptor({
      plainText: Buffer.from(secretHash)
    });

    return { encryptedEabSecret: cipherTextBlob };
  } catch (error) {
    throw new BadRequestError({ message: `Failed to generate ACME EAB secret: ${(error as Error).message}` });
  }
};

export const validateAndEncryptPemCaChain = async (
  caChain: string,
  projectId: string,
  kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey">,
  projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction">
) => {
  try {
    const certificates = extractX509CertFromChain(caChain)?.map((cert) => new x509.X509Certificate(cert));

    if (!certificates || certificates.length === 0) {
      throw new BadRequestError({ message: "Failed to parse certificate chain" });
    }

    if (!(await isCertChainValid(certificates))) {
      throw new BadRequestError({ message: "Invalid certificate chain" });
    }

    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId,
      projectDAL,
      kmsService
    });

    const kmsEncryptor = await kmsService.encryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });

    const { cipherTextBlob } = await kmsEncryptor({
      plainText: Buffer.from(caChain)
    });

    return { encryptedCaChain: cipherTextBlob };
  } catch (error) {
    throw new BadRequestError({ message: `Failed to process certificate chain: ${(error as Error).message}` });
  }
};

export const decryptCaChain = async (
  encryptedCaChain: Buffer,
  projectId: string,
  kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "decryptWithKmsKey">,
  projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "transaction">
): Promise<string> => {
  try {
    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId,
      projectDAL,
      kmsService
    });

    const kmsDecryptor = await kmsService.decryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });

    const decryptedCaChain = await kmsDecryptor({
      cipherTextBlob: encryptedCaChain
    });

    return decryptedCaChain.toString();
  } catch (error) {
    throw new BadRequestError({ message: `Failed to decrypt certificate chain: ${(error as Error).message}` });
  }
};

export type TCertificateProfileCreateData = Omit<
  TCertificateProfileInsert,
  "enrollmentType" | "estConfigId" | "apiConfigId" | "acmeConfigId" | "scepConfigId"
> & {
  enrollmentType?: EnrollmentType;
  estConfig?: TEstConfigData;
  apiConfig?: TApiConfigData;
  acmeConfig?: TAcmeConfigData;
  scepConfig?: TScepConfigData;
};

type TCertificateProfileServiceFactoryDep = {
  certificateProfileDAL: TCertificateProfileDALFactory;
  certificatePolicyDAL: TCertificatePolicyDALFactory;
  certificatePolicyService: Pick<TCertificatePolicyServiceFactory, "validateRequestAgainstPolicy">;
  apiEnrollmentConfigDAL: TApiEnrollmentConfigDALFactory;
  estEnrollmentConfigDAL: TEstEnrollmentConfigDALFactory;
  acmeEnrollmentConfigDAL: TAcmeEnrollmentConfigDALFactory;
  scepEnrollmentConfigDAL: TScepEnrollmentConfigDALFactory;
  scepDynamicChallengeDAL: Pick<TScepDynamicChallengeDALFactory, "deleteByConfigId">;
  certificateBodyDAL: Pick<TCertificateBodyDALFactory, "findOne">;
  certificateSecretDAL: Pick<TCertificateSecretDALFactory, "findOne">;
  certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findById" | "findByIdWithAssociatedCa">;
  certificateAuthoritySecretDAL: Pick<TCertificateAuthoritySecretDALFactory, "findOne">;
  certificateAuthorityCertDAL: Pick<TCertificateAuthorityCertDALFactory, "find">;
  caKeySourceService: Pick<TCaKeySourceServiceFactory, "buildSigner">;
  externalCertificateAuthorityDAL: Pick<TExternalCertificateAuthorityDALFactory, "findById" | "findOne">;
  permissionService: Pick<TPermissionServiceFactory, "getProjectPermission" | "getResourcePermission">;
  kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey" | "decryptWithKmsKey">;
  projectDAL: Pick<TProjectDALFactory, "findProjectBySlug" | "findOne" | "updateById" | "findById" | "transaction">;
  resourceMetadataDAL: Pick<TResourceMetadataDALFactory, "find">;
  pkiApplicationProfileDAL?: Pick<
    TPkiApplicationProfileDALFactory,
    "findOneByApplicationAndProfile" | "findByApplicationId" | "findAllByProfileId"
  >;
};

export type TCertificateProfileServiceFactory = ReturnType<typeof certificateProfileServiceFactory>;

const convertDalToService = (dalResult: Record<string, unknown>): TCertificateProfile => {
  let parsedExternalConfigs: Record<string, unknown> | null = null;
  if (dalResult.externalConfigs && typeof dalResult.externalConfigs === "string") {
    try {
      parsedExternalConfigs = JSON.parse(dalResult.externalConfigs) as Record<string, unknown>;
    } catch {
      parsedExternalConfigs = null;
    }
  } else if (dalResult.externalConfigs && typeof dalResult.externalConfigs === "object") {
    parsedExternalConfigs = dalResult.externalConfigs as Record<string, unknown>;
  }

  const parsedDefaults = (dalResult.defaults as TCertificateProfileDefaults) ?? null;

  return {
    ...dalResult,
    enrollmentType: dalResult.enrollmentType as EnrollmentType,
    issuerType: dalResult.issuerType as IssuerType,
    externalConfigs: parsedExternalConfigs,
    defaults: parsedDefaults
  } as TCertificateProfile;
};

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,
  certificatePolicyService,
  apiEnrollmentConfigDAL,
  estEnrollmentConfigDAL,
  acmeEnrollmentConfigDAL,
  scepEnrollmentConfigDAL,
  scepDynamicChallengeDAL,
  certificateBodyDAL,
  certificateSecretDAL,
  certificateAuthorityDAL,
  certificateAuthoritySecretDAL,
  certificateAuthorityCertDAL,
  caKeySourceService,
  externalCertificateAuthorityDAL,
  permissionService,
  kmsService,
  projectDAL,
  resourceMetadataDAL,
  pkiApplicationProfileDAL
}: TCertificateProfileServiceFactoryDep) => {
  const validateProfileCustomExtensions = async ({
    certificatePolicyId,
    caId,
    defaults
  }: {
    certificatePolicyId?: string | null;
    caId?: string | null;
    defaults?: TCertificateProfileDefaults | null;
  }) => {
    const declarations = defaults?.customExtensions;
    if (!declarations?.length) return;

    if (declarations.length > MAX_CUSTOM_EXTENSIONS_PER_PROFILE) {
      throw new BadRequestError({
        message: `A profile cannot declare more than ${MAX_CUSTOM_EXTENSIONS_PER_PROFILE} custom extensions`
      });
    }

    const seenOids = new Set<string>();
    for (const declaration of declarations) {
      if (seenOids.has(declaration.oid)) {
        throw new BadRequestError({
          message: `Duplicate custom extension for OID '${declaration.oid}'. Each OID must appear only once.`
        });
      }
      seenOids.add(declaration.oid);

      if (isReservedExtensionOid(declaration.oid)) {
        throw new BadRequestError({ message: describeReservedExtensionOid(declaration.oid) });
      }
    }

    const policy = certificatePolicyId ? await certificatePolicyDAL.findById(certificatePolicyId) : null;
    const { errors } = resolveCustomExtensions({
      declarations,
      rules: policy?.customExtensions as TCustomExtensionRule[] | null | undefined,
      skipRequired: true
    });
    if (errors.length) {
      throw new BadRequestError({ message: errors.join(" ") });
    }

    if (caId) {
      const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
      const caType = (ca?.externalCa?.type ?? CaType.INTERNAL) as CaType;
      if (!caSupportsCapability(caType, CaCapability.CUSTOM_EXTENSIONS)) {
        throw new BadRequestError({
          message: `${CERTIFICATE_AUTHORITIES_TYPE_MAP[caType] ?? caType} certificate authorities cannot carry custom extensions, so this profile cannot declare any. Use an internal certificate authority, ${CERTIFICATE_AUTHORITIES_TYPE_MAP[CaType.ADCS]}, or ${CERTIFICATE_AUTHORITIES_TYPE_MAP[CaType.AWS_PCA]} instead.`
        });
      }

      if (caType === CaType.AWS_PCA && declarations.length > MAX_CUSTOM_EXTENSIONS_PER_AWS_PCA_PROFILE) {
        throw new BadRequestError({
          message: `AWS Private CA accepts at most ${MAX_CUSTOM_EXTENSIONS_PER_AWS_PCA_PROFILE} custom extensions on a certificate, so this profile cannot declare ${declarations.length}.`
        });
      }
    }
  };

  const createProfile = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    projectId,
    data
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    projectId: string;
    data: Omit<TCertificateProfileCreateData, "projectId">;
  }): Promise<TCertificateProfile> => {
    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateProfileActions.Create,
      subject(ProjectPermissionSub.CertificateProfiles, {
        slug: data.slug
      })
    );

    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
    if (data.certificatePolicyId) {
      const policy = await certificatePolicyDAL.findById(data.certificatePolicyId);
      if (!policy) {
        throw new NotFoundError({ message: "Certificate policy not found" });
      }
      if (policy.projectId !== projectId) {
        throw new ForbiddenRequestError({
          message: "Certificate policy must belong to the same project"
        });
      }
    }

    await validateCaProjectMatch(data.caId, projectId, certificateAuthorityDAL);

    // Check for slug uniqueness within project
    const existingSlugProfile = await certificateProfileDAL.findBySlugAndProjectId(data.slug, projectId);
    if (existingSlugProfile) {
      throw new ConflictError({
        message: `A certificate profile named '${data.slug}' already exists. Choose a different name.`
      });
    }

    validateIssuerTypeConstraints(data.issuerType, enrollmentType, data.caId ?? null);

    await validateAcmEnrollmentType(data.caId, enrollmentType, externalCertificateAuthorityDAL);

    // Validate defaults against policy constraints
    if (data.defaults && data.certificatePolicyId) {
      const policy = await certificatePolicyDAL.findById(data.certificatePolicyId);
      if (policy) {
        const request: TCertificateRequest = {
          commonName: data.defaults.commonName,
          organization: data.defaults.organization,
          organizationalUnit: data.defaults.organizationalUnit,
          country: data.defaults.country,
          state: data.defaults.state,
          locality: data.defaults.locality,
          domainComponents: data.defaults.domainComponents,
          keyUsages: data.defaults.keyUsages,
          extendedKeyUsages: data.defaults.extendedKeyUsages,
          signatureAlgorithm: data.defaults.signatureAlgorithm,
          keyAlgorithm: data.defaults.keyAlgorithm,
          validity: data.defaults.ttlDays ? { ttl: `${data.defaults.ttlDays}d` } : undefined,
          basicConstraints: data.defaults.basicConstraints
        };
        const result = certificatePolicyService.validateRequestAgainstPolicy(policy, request, { skipRequired: true });
        if (!result.isValid) {
          throw new BadRequestError({ message: `Profile defaults violate policy: ${result.errors.join("; ")}` });
        }
      }
    }

    await validateProfileCustomExtensions({
      certificatePolicyId: data.certificatePolicyId,
      caId: data.caId,
      defaults: data.defaults
    });

    await validateDefaultSignatureAlgorithmAgainstCa(
      data.defaults?.signatureAlgorithm,
      data.caId,
      certificateAuthorityDAL
    );

    // Validate external configs
    await validateExternalConfigs(
      data.externalConfigs,
      data.caId ?? null,
      certificateAuthorityDAL,
      externalCertificateAuthorityDAL
    );

    if (enrollmentType === EnrollmentType.ACME && data.acmeConfig) {
      if (data.acmeConfig.skipEabBinding && data.acmeConfig.skipDnsOwnershipVerification) {
        throw new ForbiddenRequestError({
          message: "Cannot skip both External Account Binding (EAB) and DNS ownership verification at the same time."
        });
      }
    }

    // Perform crypto operations before the transaction to avoid holding DB connections
    let precomputedScepConfig:
      | {
          encryptedRaPrivateKey: Buffer;
          raCertificatePem: string;
          raCertExpiresAt: Date;
          hashedChallengePassword: string | null;
          challengeType: string;
          includeCaCertInResponse: boolean;
          allowCertBasedRenewal: boolean;
          dynamicChallengeExpiryMinutes: number | null;
          dynamicChallengeMaxPending: number | null;
          signRaWithCa: boolean;
        }
      | undefined;

    if (enrollmentType === EnrollmentType.SCEP && data.scepConfig) {
      if (data.scepConfig.challengeType === ScepChallengeType.MICROSOFT_INTUNE) {
        throw new BadRequestError({
          message:
            "Microsoft Intune SCEP validation must be configured through an application's enrollment method, not directly on a certificate profile."
        });
      }

      const { signRaWithCa } = await resolveScepRaSigning({
        caId: data.caId,
        requestedSignRaWithCa: data.scepConfig.signRaWithCa,
        certificateAuthorityDAL
      });

      const raCert = await generateAndEncryptScepRaCertificate({
        slug: data.slug,
        caId: data.caId,
        signRaWithCa,
        projectId,
        deps: {
          certificateAuthorityDAL,
          certificateAuthoritySecretDAL,
          certificateAuthorityCertDAL,
          projectDAL,
          kmsService,
          caKeySourceService
        }
      });

      const challengeType = (data.scepConfig.challengeType as ScepChallengeType) || ScepChallengeType.STATIC;
      let hashedChallengePassword: string | null = null;

      if (challengeType === ScepChallengeType.STATIC && data.scepConfig.challengePassword) {
        const appCfg = getConfig();
        hashedChallengePassword = await crypto
          .hashing()
          .createHash(data.scepConfig.challengePassword, appCfg.SALT_ROUNDS);
      }

      precomputedScepConfig = {
        encryptedRaPrivateKey: raCert.encryptedPrivateKey,
        raCertificatePem: raCert.certificatePem,
        raCertExpiresAt: raCert.expiresAt,
        hashedChallengePassword,
        challengeType,
        includeCaCertInResponse: data.scepConfig.includeCaCertInResponse ?? true,
        allowCertBasedRenewal: data.scepConfig.allowCertBasedRenewal ?? true,
        signRaWithCa,
        dynamicChallengeExpiryMinutes:
          challengeType === ScepChallengeType.DYNAMIC ? (data.scepConfig.dynamicChallengeExpiryMinutes ?? 60) : null,
        dynamicChallengeMaxPending:
          challengeType === ScepChallengeType.DYNAMIC ? (data.scepConfig.dynamicChallengeMaxPending ?? 100) : null
      };
    }

    // Create enrollment configs and profile
    const profile = await certificateProfileDAL.transaction(async (tx) => {
      let estConfigId: string | null = null;
      let apiConfigId: string | null = null;
      let acmeConfigId: string | null = null;
      let scepConfigId: string | null = null;

      if (enrollmentType === EnrollmentType.EST && data.estConfig) {
        const appCfg = getConfig();
        // Hash the passphrase
        const hashedPassphrase = await crypto.hashing().createHash(data.estConfig.passphrase, appCfg.SALT_ROUNDS);

        let encryptedCaChainBuffer: Buffer | null = null;
        if (!data.estConfig.disableBootstrapCaValidation && data.estConfig.caChain) {
          const { encryptedCaChain } = await validateAndEncryptPemCaChain(
            data.estConfig.caChain,
            projectId,
            kmsService,
            projectDAL
          );
          encryptedCaChainBuffer = encryptedCaChain;
        }

        const estConfig = await estEnrollmentConfigDAL.create(
          {
            disableBootstrapCaValidation: data.estConfig.disableBootstrapCaValidation,
            hashedPassphrase,
            encryptedCaChain: encryptedCaChainBuffer
          },
          tx
        );
        estConfigId = estConfig.id;
      } else if (enrollmentType === EnrollmentType.API && data.apiConfig) {
        const apiConfig = await apiEnrollmentConfigDAL.create(
          {
            autoRenew: data.apiConfig.autoRenew,
            renewBeforeDays: data.apiConfig.renewBeforeDays
          },
          tx
        );
        apiConfigId = apiConfig.id;
      } else if (enrollmentType === EnrollmentType.ACME && data.acmeConfig) {
        const { encryptedEabSecret } = await generateAndEncryptAcmeEabSecret(projectId, kmsService, projectDAL);
        const acmeConfig = await acmeEnrollmentConfigDAL.create(
          {
            skipDnsOwnershipVerification: data.acmeConfig.skipDnsOwnershipVerification ?? false,
            skipEabBinding: data.acmeConfig.skipEabBinding ?? false,
            encryptedEabSecret
          },
          tx
        );
        acmeConfigId = acmeConfig.id;
      } else if (precomputedScepConfig) {
        const scepConfig = await scepEnrollmentConfigDAL.create(
          {
            encryptedRaPrivateKey: precomputedScepConfig.encryptedRaPrivateKey,
            raCertificate: precomputedScepConfig.raCertificatePem,
            raCertExpiresAt: precomputedScepConfig.raCertExpiresAt,
            hashedChallengePassword: precomputedScepConfig.hashedChallengePassword,
            challengeType: precomputedScepConfig.challengeType,
            includeCaCertInResponse: precomputedScepConfig.includeCaCertInResponse,
            allowCertBasedRenewal: precomputedScepConfig.allowCertBasedRenewal,
            signRaWithCa: precomputedScepConfig.signRaWithCa,
            dynamicChallengeExpiryMinutes: precomputedScepConfig.dynamicChallengeExpiryMinutes,
            dynamicChallengeMaxPending: precomputedScepConfig.dynamicChallengeMaxPending
          },
          tx
        );
        scepConfigId = scepConfig.id;
      }

      // Create the profile with the created config IDs
      const { estConfig, apiConfig, acmeConfig, scepConfig: profileScepConfig, ...profileData } = data;
      const profileResult = await certificateProfileDAL.create(
        {
          ...profileData,
          projectId,
          enrollmentType,
          estConfigId,
          apiConfigId,
          acmeConfigId,
          scepConfigId,
          externalConfigs: data.externalConfigs
        },
        tx
      );

      return profileResult;
    });

    return convertDalToService(profile);
  };

  const updateProfile = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId,
    data
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
    data: TCertificateProfileUpdate;
  }): Promise<TCertificateProfile> => {
    const existingProfile = await certificateProfileDAL.findById(profileId);
    if (!existingProfile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: existingProfile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateProfileActions.Edit,
      subject(ProjectPermissionSub.CertificateProfiles, {
        slug: existingProfile.slug
      })
    );

    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) {
        throw new NotFoundError({ message: "Certificate policy not found" });
      }
      if (policy.projectId !== existingProfile.projectId) {
        throw new ForbiddenRequestError({
          message: "Certificate policy must belong to the same project"
        });
      }
    }

    if (data.slug && data.slug !== existingProfile.slug) {
      const conflictingProfile = await certificateProfileDAL.findBySlugAndProjectId(
        data.slug,
        existingProfile.projectId
      );
      if (conflictingProfile && conflictingProfile.id !== profileId) {
        throw new ConflictError({
          message: `A certificate profile named '${data.slug}' already exists. Choose a different name.`
        });
      }
    }

    const finalIssuerType = data.issuerType || existingProfile.issuerType;
    const finalEnrollmentType = data.enrollmentType || existingProfile.enrollmentType;
    const finalCaId = data.caId !== undefined ? data.caId : existingProfile.caId;

    if (data.caId !== undefined) {
      await validateCaProjectMatch(data.caId, existingProfile.projectId, certificateAuthorityDAL);
    }

    validateIssuerTypeConstraints(finalIssuerType, finalEnrollmentType, finalCaId ?? null, existingProfile.caId);

    await validateAcmEnrollmentType(finalCaId, finalEnrollmentType, externalCertificateAuthorityDAL);

    // Validate external configs only if they are provided in the update
    if (data.externalConfigs !== undefined) {
      await validateExternalConfigs(
        data.externalConfigs,
        finalCaId ?? null,
        certificateAuthorityDAL,
        externalCertificateAuthorityDAL
      );
    }

    if (finalEnrollmentType === EnrollmentType.ACME && data.acmeConfig && existingProfile.acmeConfigId) {
      const existingAcmeConfig = await acmeEnrollmentConfigDAL.findById(existingProfile.acmeConfigId);
      if (existingAcmeConfig) {
        const finalSkipEabBinding = data.acmeConfig.skipEabBinding ?? existingAcmeConfig.skipEabBinding;
        const finalSkipDnsOwnershipVerification =
          data.acmeConfig.skipDnsOwnershipVerification ?? existingAcmeConfig.skipDnsOwnershipVerification;

        if (finalSkipEabBinding && finalSkipDnsOwnershipVerification) {
          throw new ForbiddenRequestError({
            message: "Cannot skip both External Account Binding (EAB) and DNS ownership verification at the same time."
          });
        }
      }
    }
    // Validate defaults against policy constraints if provided
    if (data.defaults) {
      const policyId = data.certificatePolicyId || existingProfile.certificatePolicyId;
      const policy = await certificatePolicyDAL.findById(policyId);
      if (policy) {
        const request: TCertificateRequest = {
          commonName: data.defaults.commonName,
          organization: data.defaults.organization,
          organizationalUnit: data.defaults.organizationalUnit,
          country: data.defaults.country,
          state: data.defaults.state,
          locality: data.defaults.locality,
          domainComponents: data.defaults.domainComponents,
          keyUsages: data.defaults.keyUsages,
          extendedKeyUsages: data.defaults.extendedKeyUsages,
          signatureAlgorithm: data.defaults.signatureAlgorithm,
          keyAlgorithm: data.defaults.keyAlgorithm,
          validity: data.defaults.ttlDays ? { ttl: `${data.defaults.ttlDays}d` } : undefined,
          basicConstraints: data.defaults.basicConstraints
        };
        const result = certificatePolicyService.validateRequestAgainstPolicy(policy, request, { skipRequired: true });
        if (!result.isValid) {
          throw new BadRequestError({ message: `Profile defaults violate policy: ${result.errors.join("; ")}` });
        }
      }
    }

    await validateProfileCustomExtensions({
      certificatePolicyId:
        data.certificatePolicyId !== undefined ? data.certificatePolicyId : existingProfile.certificatePolicyId,
      caId: data.caId !== undefined ? data.caId : existingProfile.caId,
      defaults:
        data.defaults !== undefined ? data.defaults : (existingProfile.defaults as TCertificateProfileDefaults | null)
    });

    await validateDefaultSignatureAlgorithmAgainstCa(
      data.defaults?.signatureAlgorithm,
      finalCaId,
      certificateAuthorityDAL
    );

    const updatedData =
      finalIssuerType === IssuerType.SELF_SIGNED && existingProfile.caId ? { ...data, caId: null } : data;

    const { estConfig, apiConfig, acmeConfig, scepConfig, ...profileUpdateData } = updatedData;

    const updatedProfile = await certificateProfileDAL.transaction(async (tx) => {
      if (estConfig && existingProfile.estConfigId) {
        const updateData: {
          disableBootstrapCaValidation: boolean;
          hashedPassphrase?: string;
          encryptedCaChain?: Buffer;
        } = {
          disableBootstrapCaValidation: estConfig.disableBootstrapCaValidation ?? false
        };

        if (estConfig.passphrase) {
          updateData.hashedPassphrase = await crypto
            .hashing()
            .createHash(estConfig.passphrase, getConfig().SALT_ROUNDS);
        }

        if (estConfig.caChain) {
          const { encryptedCaChain } = await validateAndEncryptPemCaChain(
            estConfig.caChain,
            existingProfile.projectId,
            kmsService,
            projectDAL
          );
          updateData.encryptedCaChain = encryptedCaChain;
        }

        await estEnrollmentConfigDAL.updateById(existingProfile.estConfigId, updateData, tx);
      }

      if (apiConfig && existingProfile.apiConfigId) {
        await apiEnrollmentConfigDAL.updateById(
          existingProfile.apiConfigId,
          {
            autoRenew: apiConfig.autoRenew,
            renewBeforeDays: apiConfig.renewBeforeDays
          },
          tx
        );
      }

      if (acmeConfig && existingProfile.acmeConfigId) {
        const acmeUpdateData: { skipDnsOwnershipVerification?: boolean; skipEabBinding?: boolean } = {};
        if (acmeConfig.skipDnsOwnershipVerification !== undefined) {
          acmeUpdateData.skipDnsOwnershipVerification = acmeConfig.skipDnsOwnershipVerification;
        }
        if (acmeConfig.skipEabBinding !== undefined) {
          acmeUpdateData.skipEabBinding = acmeConfig.skipEabBinding;
        }
        if (Object.keys(acmeUpdateData).length > 0) {
          await acmeEnrollmentConfigDAL.updateById(existingProfile.acmeConfigId, acmeUpdateData, tx);
        }
      }

      if (scepConfig && existingProfile.scepConfigId) {
        const existingScepConfig = await scepEnrollmentConfigDAL.findById(existingProfile.scepConfigId, tx);

        const scepUpdateData: {
          hashedChallengePassword?: string | null;
          challengeType?: string;
          includeCaCertInResponse?: boolean;
          allowCertBasedRenewal?: boolean;
          dynamicChallengeExpiryMinutes?: number | null;
          dynamicChallengeMaxPending?: number | null;
        } = {};

        if (scepConfig.challengeType !== undefined) {
          scepUpdateData.challengeType = scepConfig.challengeType;
          if (scepConfig.challengeType === ScepChallengeType.DYNAMIC) {
            scepUpdateData.hashedChallengePassword = null;
            scepUpdateData.dynamicChallengeExpiryMinutes = scepConfig.dynamicChallengeExpiryMinutes ?? 60;
            scepUpdateData.dynamicChallengeMaxPending = scepConfig.dynamicChallengeMaxPending ?? 100;
          }
          if (scepConfig.challengeType === ScepChallengeType.STATIC) {
            // Require password when switching from dynamic to static
            const isSwitchingFromDynamic = existingScepConfig?.challengeType === ScepChallengeType.DYNAMIC;
            if (isSwitchingFromDynamic && !scepConfig.challengePassword) {
              throw new BadRequestError({
                message: "Switching to static challenge type requires providing a challenge password"
              });
            }
            await scepDynamicChallengeDAL.deleteByConfigId(existingProfile.scepConfigId, tx);
            scepUpdateData.dynamicChallengeExpiryMinutes = null;
            scepUpdateData.dynamicChallengeMaxPending = null;
          }
        }
        if (scepConfig.challengePassword && scepUpdateData.challengeType !== ScepChallengeType.DYNAMIC) {
          scepUpdateData.hashedChallengePassword = await crypto
            .hashing()
            .createHash(scepConfig.challengePassword, getConfig().SALT_ROUNDS);
        }
        if (scepConfig.includeCaCertInResponse !== undefined) {
          scepUpdateData.includeCaCertInResponse = scepConfig.includeCaCertInResponse;
        }
        if (scepConfig.allowCertBasedRenewal !== undefined) {
          scepUpdateData.allowCertBasedRenewal = scepConfig.allowCertBasedRenewal;
        }
        if (scepUpdateData.challengeType === undefined && scepConfig.dynamicChallengeExpiryMinutes !== undefined) {
          scepUpdateData.dynamicChallengeExpiryMinutes = scepConfig.dynamicChallengeExpiryMinutes;
        }
        if (scepUpdateData.challengeType === undefined && scepConfig.dynamicChallengeMaxPending !== undefined) {
          scepUpdateData.dynamicChallengeMaxPending = scepConfig.dynamicChallengeMaxPending;
        }
        if (Object.keys(scepUpdateData).length > 0) {
          await scepEnrollmentConfigDAL.updateById(existingProfile.scepConfigId, scepUpdateData, tx);
        }
      }

      const profileResult = await certificateProfileDAL.updateById(profileId, profileUpdateData, tx);
      return profileResult;
    });

    return convertDalToService(updatedProfile);
  };

  const $isMemberOfAnyAttachedApplication = async (
    profileId: string,
    projectId: string,
    actor: ActorType,
    actorId: string,
    actorAuthMethod: ActorAuthMethod,
    actorOrgId: string
  ): Promise<boolean> => {
    if (!pkiApplicationProfileDAL) return false;
    const attachments = await pkiApplicationProfileDAL.findAllByProfileId(profileId);
    const reads = await Promise.all(
      attachments.map(async (attachment) => {
        const { permission: resourcePermission } = await permissionService.getResourcePermission({
          actor,
          actorId,
          projectId,
          resourceType: ResourceType.CertificateApplication,
          resourceId: attachment.applicationId,
          actorAuthMethod,
          actorOrgId
        });
        return resourcePermission.can(ResourcePermissionApplicationActions.Read, ResourcePermissionSub.Application);
      })
    );
    return reads.some(Boolean);
  };

  const getProfileById = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
  }): Promise<TCertificateProfile> => {
    const profile = await certificateProfileDAL.findById(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: profile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });

    const profileSubject = subject(ProjectPermissionSub.CertificateProfiles, { slug: profile.slug });
    if (!permission.can(ProjectPermissionCertificateProfileActions.Read, profileSubject)) {
      const allowedByApplication = await $isMemberOfAnyAttachedApplication(
        profileId,
        profile.projectId,
        actor,
        actorId,
        actorAuthMethod,
        actorOrgId
      );
      if (!allowedByApplication) {
        ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionCertificateProfileActions.Read, profileSubject);
      }
    }

    const converted = convertDalToService(profile);

    return converted;
  };

  const getProfileByIdWithConfigs = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
  }): Promise<TCertificateProfileWithConfigs> => {
    const profile = await certificateProfileDAL.findByIdWithConfigs(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: profile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });

    const profileWithConfigsSubject = subject(ProjectPermissionSub.CertificateProfiles, {
      slug: profile.slug
    });
    if (!permission.can(ProjectPermissionCertificateProfileActions.Read, profileWithConfigsSubject)) {
      const allowedByApplication = await $isMemberOfAnyAttachedApplication(
        profileId,
        profile.projectId,
        actor,
        actorId,
        actorAuthMethod,
        actorOrgId
      );
      if (!allowedByApplication) {
        ForbiddenError.from(permission).throwUnlessCan(
          ProjectPermissionCertificateProfileActions.Read,
          profileWithConfigsSubject
        );
      }
    }

    if (profile.estConfig && profile.estConfig.caChain) {
      try {
        const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId!);
        if (estConfig && estConfig.encryptedCaChain) {
          const decryptedCaChain = await decryptCaChain(
            estConfig.encryptedCaChain,
            profile.projectId,
            kmsService,
            projectDAL
          );
          profile.estConfig.caChain = decryptedCaChain;
        } else {
          profile.estConfig.caChain = "";
        }
      } catch (error) {
        profile.estConfig.caChain = "";
      }
    }

    if (profile.enrollmentType === EnrollmentType.ACME && profile.acmeConfig) {
      profile.acmeConfig.directoryUrl = buildUrl(profile.id, "/directory");
      if (profile.acmeConfig.encryptedEabSecret) {
        profile.acmeConfig.encryptedEabSecret = undefined;
      }
    }

    if (profile.enrollmentType === EnrollmentType.SCEP && profile.scepConfig) {
      const appCfg = getConfig();
      const siteUrl = appCfg.SITE_URL ?? "";
      profile.scepConfig.scepEndpointUrl = `${siteUrl}/scep/${profile.id}/pkiclient.exe`;
      if (profile.scepConfig.challengeType === ScepChallengeType.DYNAMIC) {
        profile.scepConfig.challengeEndpointUrl = `${siteUrl}/scep/${profile.id}/challenge`;
      } else {
        delete profile.scepConfig.dynamicChallengeExpiryMinutes;
        delete profile.scepConfig.dynamicChallengeMaxPending;
      }
    }

    // Parse externalConfigs from JSON string to object if it exists
    let parsedExternalConfigs: Record<string, unknown> | null = null;
    if (profile.externalConfigs && typeof profile.externalConfigs === "string") {
      try {
        parsedExternalConfigs = JSON.parse(profile.externalConfigs) as Record<string, unknown>;
      } catch {
        // If parsing fails, leave as null
        parsedExternalConfigs = null;
      }
    } else if (profile.externalConfigs && typeof profile.externalConfigs === "object") {
      // Already an object, use as-is
      parsedExternalConfigs = profile.externalConfigs;
    }

    return {
      ...profile,
      enrollmentType: profile.enrollmentType as EnrollmentType,
      externalConfigs: parsedExternalConfigs
    };
  };

  const getProfileBySlug = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    projectId,
    slug
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    projectId: string;
    slug: string;
  }): Promise<TCertificateProfile> => {
    const profile = await certificateProfileDAL.findBySlugAndProjectId(slug, projectId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });

    const profileSlugSubject = subject(ProjectPermissionSub.CertificateProfiles, { slug });
    if (!permission.can(ProjectPermissionCertificateProfileActions.Read, profileSlugSubject)) {
      const allowedByApplication = await $isMemberOfAnyAttachedApplication(
        profile.id,
        projectId,
        actor,
        actorId,
        actorAuthMethod,
        actorOrgId
      );
      if (!allowedByApplication) {
        ForbiddenError.from(permission).throwUnlessCan(
          ProjectPermissionCertificateProfileActions.Read,
          profileSlugSubject
        );
      }
    }

    return convertDalToService(profile);
  };

  const listProfiles = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    projectId,
    offset = 0,
    limit = 20,
    search,
    enrollmentType,
    issuerType,
    caId,
    applicationId
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    projectId: string;
    offset?: number;
    limit?: number;
    search?: string;
    enrollmentType?: EnrollmentType;
    issuerType?: IssuerType;
    caId?: string;
    applicationId?: string;
  }): Promise<{
    profiles: TCertificateProfileWithConfigs[];
    totalCount: number;
  }> => {
    let processedRules: ReturnType<typeof getProcessedPermissionRules> | undefined;
    let attachedProfileIds: string[] | undefined;
    if (applicationId && pkiApplicationProfileDAL && (actor === ActorType.USER || actor === ActorType.IDENTITY)) {
      const { permission } = await permissionService.getResourcePermission({
        actor,
        actorId,
        projectId,
        resourceType: ResourceType.CertificateApplication,
        resourceId: applicationId,
        actorAuthMethod,
        actorOrgId
      });
      ForbiddenError.from(permission).throwUnlessCan(
        ResourcePermissionApplicationActions.Read,
        ResourcePermissionSub.Application
      );
      const attached = await pkiApplicationProfileDAL.findByApplicationId(applicationId);
      attachedProfileIds = attached.map((p) => p.profileId);
    } else {
      const { permission } = await permissionService.getProjectPermission({
        actor,
        actorId,
        projectId,
        actorAuthMethod,
        actorOrgId,
        actionProjectType: ActionProjectType.CertificateManager
      });
      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateProfileActions.Read,
        ProjectPermissionSub.CertificateProfiles
      );

      processedRules = getProcessedPermissionRules(
        permission,
        ProjectPermissionCertificateProfileActions.Read,
        ProjectPermissionSub.CertificateProfiles
      );
    }

    if (attachedProfileIds && attachedProfileIds.length === 0) {
      return { profiles: [], totalCount: 0 };
    }

    const profiles = await certificateProfileDAL.findByProjectId(
      projectId,
      {
        offset,
        limit,
        search,
        enrollmentType,
        issuerType,
        caId,
        profileIds: attachedProfileIds
      },
      processedRules
    );

    const totalCount = await certificateProfileDAL.countByProjectId(
      projectId,
      {
        search,
        enrollmentType,
        issuerType,
        caId,
        profileIds: attachedProfileIds
      },
      processedRules
    );

    const convertedProfiles = await Promise.all(
      profiles.map(async (profile) => {
        const profileWithConfigs = profile as TCertificateProfileWithConfigs;

        let decryptedEstConfig = profileWithConfigs.estConfig;
        if (decryptedEstConfig && profileWithConfigs.estConfigId) {
          try {
            const estConfig = await estEnrollmentConfigDAL.findById(profileWithConfigs.estConfigId);
            if (estConfig && estConfig.encryptedCaChain) {
              const decryptedCaChain = await decryptCaChain(
                estConfig.encryptedCaChain,
                projectId,
                kmsService,
                projectDAL
              );
              decryptedEstConfig = {
                ...decryptedEstConfig,
                caChain: decryptedCaChain
              };
            } else if (decryptedEstConfig) {
              decryptedEstConfig = {
                ...decryptedEstConfig,
                caChain: ""
              };
            }
          } catch (error) {
            if (decryptedEstConfig) {
              decryptedEstConfig = {
                ...decryptedEstConfig,
                caChain: ""
              };
            }
          }
        }

        const converted = convertDalToService(profileWithConfigs);
        const appCfg = getConfig();
        const siteUrl = appCfg.SITE_URL ?? "";
        const result: TCertificateProfileWithConfigs = {
          ...converted,
          estConfig: decryptedEstConfig,
          apiConfig: profileWithConfigs.apiConfig,
          acmeConfig: profileWithConfigs.acmeConfig
            ? { ...profileWithConfigs.acmeConfig, directoryUrl: buildUrl(profile.id, "/directory") }
            : undefined,
          scepConfig: profileWithConfigs.scepConfig
            ? {
                ...profileWithConfigs.scepConfig,
                scepEndpointUrl: `${siteUrl}/scep/${profile.id}/pkiclient.exe`,
                ...(profileWithConfigs.scepConfig.challengeType === ScepChallengeType.DYNAMIC
                  ? { challengeEndpointUrl: `${siteUrl}/scep/${profile.id}/challenge` }
                  : {
                      dynamicChallengeExpiryMinutes: undefined,
                      dynamicChallengeMaxPending: undefined
                    })
              }
            : undefined
        };

        return result;
      })
    );

    return {
      profiles: convertedProfiles,
      totalCount
    };
  };

  const deleteProfile = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
  }): Promise<TCertificateProfile> => {
    const profile = await certificateProfileDAL.findById(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: profile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateProfileActions.Delete,
      subject(ProjectPermissionSub.CertificateProfiles, {
        slug: profile.slug
      })
    );

    if (pkiApplicationProfileDAL) {
      const attachments = await pkiApplicationProfileDAL.findAllByProfileId(profileId);
      if (attachments.length > 0) {
        const uniqueNames = Array.from(new Set(attachments.map((a) => a.applicationName))).sort();
        const preview = uniqueNames.slice(0, 5).join(", ");
        const remaining = uniqueNames.length - 5;
        const list = remaining > 0 ? `${preview}, and ${remaining} more` : preview;
        throw new BadRequestError({
          message: `Cannot delete this certificate profile while it is attached to ${uniqueNames.length} application${
            uniqueNames.length === 1 ? "" : "s"
          } (${list}). Detach the profile from those applications first.`
        });
      }
    }

    const deletedProfile = await certificateProfileDAL.deleteById(profileId);
    if (!deletedProfile) {
      throw new NotFoundError({ message: "Failed to delete certificate profile" });
    }
    return convertDalToService(deletedProfile);
  };

  const getProfileCertificates = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId,
    offset = 0,
    limit = 20,
    status,
    search
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
    offset?: number;
    limit?: number;
    status?: CertStatus;
    search?: string;
  }): Promise<TCertificateProfileCertificate[]> => {
    const profile = await certificateProfileDAL.findById(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: profile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateProfileActions.Read,
      subject(ProjectPermissionSub.CertificateProfiles, {
        slug: profile.slug
      })
    );

    const certificates = await certificateProfileDAL.getCertificatesByProfile(profileId, {
      offset,
      limit,
      status,
      search
    });

    return certificates;
  };

  const getLatestActiveCertificateBundle = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
  }) => {
    const profile = await certificateProfileDAL.findById(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: profile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateProfileActions.Read,
      subject(ProjectPermissionSub.CertificateProfiles, {
        slug: profile.slug
      })
    );

    const cert = await certificateProfileDAL.getLatestActiveCertificateForProfile(profileId);

    if (!cert) {
      return null;
    }

    const metadataRows = await resourceMetadataDAL.find({ certificateId: cert.id });
    const certMetadata = metadataRows.map(({ key, value }) => ({ key, value: value || "" }));

    const certSubject = subject(ProjectPermissionSub.Certificates, {
      commonName: cert.commonName,
      altNames: cert.altNames?.split(",").map((s) => s.trim()),
      serialNumber: cert.serialNumber,
      metadata: certMetadata
    });
    ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionCertificateActions.Read, certSubject);

    if (cert.applicationId) {
      const { permission: resourcePermission } = await permissionService.getResourcePermission({
        actor,
        actorId,
        projectId: cert.projectId,
        resourceType: ResourceType.CertificateApplication,
        resourceId: cert.applicationId,
        actorAuthMethod,
        actorOrgId
      });
      const allowedByApplication = resourcePermission.can(
        ResourcePermissionCertificateActions.ReadPrivateKey,
        ResourcePermissionSub.Certificates
      );
      if (!allowedByApplication) {
        throw new ForbiddenRequestError({
          message: "You don't have permission to read this certificate's private key"
        });
      }
    } else {
      ForbiddenError.from(permission).throwUnlessCan(ProjectPermissionCertificateActions.ReadPrivateKey, certSubject);
    }

    const certBody = await certificateBodyDAL.findOne({ certId: cert.id });

    const certificateManagerKeyId = await getProjectKmsCertificateKeyId({
      projectId: cert.projectId,
      projectDAL,
      kmsService
    });

    const kmsDecryptor = await kmsService.decryptWithKmsKey({
      kmsId: certificateManagerKeyId
    });
    const decryptedCert = await kmsDecryptor({
      cipherTextBlob: certBody.encryptedCertificate
    });

    const certObj = new x509.X509Certificate(decryptedCert);
    const certificate = certObj.toString("pem");

    const certificateChain = certBody.encryptedCertificateChain
      ? (await kmsDecryptor({ cipherTextBlob: certBody.encryptedCertificateChain })).toString()
      : "";

    let privateKey = null;
    try {
      const { certPrivateKey } = await getCertificateCredentials({
        certId: cert.id,
        projectId: cert.projectId,
        certificateSecretDAL,
        projectDAL,
        kmsService
      });
      privateKey = certPrivateKey;
    } catch (error) {
      // Private key might not exist for ACME certificates or other external workflows
      // where the key is generated client-side
      if (error instanceof NotFoundError) {
        privateKey = null;
      } else {
        throw error;
      }
    }

    return {
      certificate,
      certificateChain,
      privateKey,
      profile,
      certObj: cert
    };
  };

  const getEstConfigurationByProfile = async (
    params:
      | {
          profileId: string;
          applicationId?: string;
          isInternal: true;
        }
      | {
          actor: ActorType;
          actorId: string;
          actorAuthMethod: ActorAuthMethod;
          actorOrgId: string | undefined;
          profileId: string;
          applicationId?: string;
          isInternal?: false;
        }
  ) => {
    const { profileId, applicationId, isInternal = false } = params;
    const profile = await certificateProfileDAL.findByIdWithConfigs(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    if (!isInternal) {
      const { actor, actorId, actorAuthMethod, actorOrgId } = params as {
        actor: ActorType;
        actorId: string;
        actorAuthMethod: ActorAuthMethod;
        actorOrgId: string | undefined;
      };

      const { permission } = await permissionService.getProjectPermission({
        actor,
        actorId,
        projectId: profile.projectId,
        actorAuthMethod,
        actorOrgId,
        actionProjectType: ActionProjectType.CertificateManager
      });

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateProfileActions.Read,
        subject(ProjectPermissionSub.CertificateProfiles, {
          slug: profile.slug
        })
      );
    }

    if (!applicationId && profile.enrollmentType !== EnrollmentType.EST) {
      throw new ForbiddenRequestError({
        message: "Profile is not configured for EST enrollment"
      });
    }

    if (applicationId && pkiApplicationProfileDAL) {
      const junction = await pkiApplicationProfileDAL.findOneByApplicationAndProfile(applicationId, profileId);
      if (!junction) {
        throw new NotFoundError({
          message: `Profile '${profileId}' is not attached to application '${applicationId}'.`
        });
      }
      if (!junction.estConfigId) {
        throw new NotFoundError({
          message: "EST configuration not found for this Application/Profile pair."
        });
      }
      const estConfig = await estEnrollmentConfigDAL.findById(junction.estConfigId);
      if (!estConfig) {
        throw new NotFoundError({ message: "EST configuration not found." });
      }
      return {
        orgId: profile.projectId,
        isEnabled: true,
        caChain: estConfig.encryptedCaChain ?? null,
        disableBootstrapCertValidation: estConfig.disableBootstrapCaValidation ?? false,
        hashedPassphrase: estConfig.hashedPassphrase
      };
    }

    if (!profile.estConfig) {
      throw new NotFoundError({ message: "EST configuration not found for this profile" });
    }

    return {
      orgId: profile.projectId,
      isEnabled: true,
      caChain: profile.estConfig.caChain,
      disableBootstrapCertValidation: profile.estConfig.disableBootstrapCaValidation,
      hashedPassphrase: profile.estConfig.passphrase
    };
  };

  const revealAcmeEabSecret = async ({
    actor,
    actorId,
    actorAuthMethod,
    actorOrgId,
    profileId
  }: {
    actor: ActorType;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actorOrgId: string;
    profileId: string;
  }) => {
    const profile = await certificateProfileDAL.findByIdWithConfigs(profileId);
    if (!profile) {
      throw new NotFoundError({ message: "Certificate profile not found" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: profile.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateProfileActions.RevealAcmeEabSecret,
      subject(ProjectPermissionSub.CertificateProfiles, {
        slug: profile.slug
      })
    );

    if (profile.enrollmentType !== EnrollmentType.ACME) {
      throw new ForbiddenRequestError({
        message: "Profile is not configured for ACME enrollment"
      });
    }
    if (!profile.acmeConfig) {
      throw new NotFoundError({ message: "ACME configuration not found for this profile" });
    }

    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId: profile.projectId,
      projectDAL,
      kmsService
    });

    const kmsDecryptor = await kmsService.decryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });
    const eabSecret = await kmsDecryptor({ cipherTextBlob: profile.acmeConfig.encryptedEabSecret! });
    return { eabKid: profile.id, eabSecret: eabSecret.toString("base64url") };
  };

  return {
    createProfile,
    updateProfile,
    getProfileById,
    getProfileByIdWithConfigs,
    getProfileBySlug,
    listProfiles,
    deleteProfile,
    getProfileCertificates,
    getLatestActiveCertificateBundle,
    getEstConfigurationByProfile,
    revealAcmeEabSecret
  };
};