Skip to content

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

105 KB
Raw Blame
/* eslint-disable no-nested-ternary */
/* eslint-disable no-bitwise */
import { ForbiddenError, subject } from "@casl/ability";
import * as x509 from "@peculiar/x509";
import slugify from "@sindresorhus/slugify";
import { Knex } from "knex";

import { ActionProjectType, TableName, TCertificateAuthorities, TCertificateTemplates } from "@app/db/schemas";
import { TLicenseServiceFactory } from "@app/ee/services/license/license-service";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import {
  ProjectPermissionCertificateActions,
  ProjectPermissionCertificateAuthorityActions,
  ProjectPermissionCertificateProfileActions,
  ProjectPermissionPkiTemplateActions,
  ProjectPermissionSub
} from "@app/ee/services/permission/project-permission";
import { extractX509CertFromChain } from "@app/lib/certificates/extract-certificate";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
import {
  exportPqcKeyToDer,
  exportPqcKeyToPem,
  getPqcCrypto,
  isPqcAlgorithm,
  isPqcCryptoKey
} from "@app/lib/crypto/pqc";
import { DatabaseErrorCode } from "@app/lib/error-codes";
import { BadRequestError, DatabaseError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { ms } from "@app/lib/ms";
import { alphaNumericNanoId } from "@app/lib/nanoid";
import { ActorAuthMethod, ActorType } from "@app/services/auth/auth-type";
import { CERT_SUBJECT_ALTERNATIVE_NAMES } from "@app/services/certificate-common/certificate-constants";
import { TCertificateBodyDALFactory } from "@app/services/certificate/certificate-body-dal";
import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { ActiveCerts, WildcardCerts } from "@app/services/license-client";
import { TUsageMeteringServiceFactory } from "@app/services/license-client/usage";
import { TPkiCollectionDALFactory } from "@app/services/pki-collection/pki-collection-dal";
import { TPkiCollectionItemDALFactory } from "@app/services/pki-collection/pki-collection-item-dal";
import { TProjectDALFactory } from "@app/services/project/project-dal";
import { getProjectKmsCertificateKeyId } from "@app/services/project/project-fns";
import { CertKeySource } from "@app/services/signer/signer-enums";

import { TCertificateAuthorityCrlDALFactory } from "../../../ee/services/certificate-authority-crl/certificate-authority-crl-dal";
import {
  DEFAULT_CRL_VALIDITY_DAYS,
  GENERAL_NAME_TYPES_WITH_OTHER_NAME,
  PKI_TEXT_COLUMN_MAX_LENGTH,
  SUPPORTED_GENERAL_NAME_TYPES
} from "../../certificate-common/certificate-constants";
import { appendCustomExtensions } from "../../certificate-common/certificate-extension-fns";
import { validatePqcLicense } from "../../certificate-common/certificate-utils";
import { TCertificateProfileDALFactory } from "../../certificate-profile/certificate-profile-dal";
import { TCertificateTemplateDALFactory } from "../../certificate-template/certificate-template-dal";
import { validateCertificateDetailsAgainstTemplate } from "../../certificate-template/certificate-template-fns";
import { extractCertificateFields } from "../../certificate/certificate-fns";
import { TCertificateSecretDALFactory } from "../../certificate/certificate-secret-dal";
import {
  CertExtendedKeyUsage,
  CertExtendedKeyUsageNameToOID,
  CertExtendedKeyUsageOIDToName,
  CertKeyAlgorithm,
  CertKeyUsage,
  CertStatus,
  TAltNameMapping,
  TAltNameType
} from "../../certificate/certificate-types";
import { isExternalCaKeySource } from "../ca-key-source/ca-key-source-fns";
import type { TCaKeySourceServiceFactory } from "../ca-key-source/ca-key-source-service";
import { TExternalCaKeyReference } from "../ca-key-source/ca-key-source-types";
import { buildLocalCaSigner, TCaSigner } from "../ca-signer";
import { TCaSigningConfigDALFactory } from "../ca-signing-config/ca-signing-config-dal";
import { CaSigningConfigType } from "../ca-signing-config/ca-signing-config-enums";
import { TCertificateAuthorityCertDALFactory } from "../certificate-authority-cert-dal";
import { TCertificateAuthorityDALFactory, TCertificateAuthorityWithAssociatedCa } from "../certificate-authority-dal";
import { CaStatus, InternalCaType } from "../certificate-authority-enums";
import {
  assertNoCertificateProfilesUsingCa,
  buildAuthorityInfoAccessExtension,
  buildCrlDistributionPointUrls,
  buildOcspResponderUrl,
  createDistinguishedName,
  createSerialNumber,
  deleteCaAndReleaseExternalKey,
  expandInternalCa,
  extractDnParts,
  getCaCertChain, // TODO: consider rename
  getCaCertChains,
  getCaSigner,
  keyAlgorithmToAlgCfg,
  signatureAlgorithmToAlgCfg,
  validateImportedCertificate
} from "../certificate-authority-fns";
import { TCertificateAuthorityQueueFactory } from "../certificate-authority-queue";
import { assertCertificateAuthorityQuota } from "../certificate-authority-quota-fns";
import { TCertificateAuthoritySecretDALFactory } from "../certificate-authority-secret-dal";
import { validateAndMapAltNameType } from "../certificate-authority-validators";
import { TInternalCertificateAuthorityDALFactory } from "./internal-certificate-authority-dal";
import {
  TCreateCaDTO,
  TDeleteCaDTO,
  TGenerateRootCaCertificateDTO,
  TGetCaCertByIdDTO,
  TGetCaCertDTO,
  TGetCaCertificateTemplatesDTO,
  TGetCaCertsDTO,
  TGetCaCsrDTO,
  TGetCaDTO,
  TImportCertToCaDTO,
  TIssueCertFromCaDTO,
  TIssueCertFromCaResponse,
  TRenewCaCertDTO,
  TSignCertFromCaDTO,
  TSignIntermediateDTO,
  TUpdateCaDTO
} from "./internal-certificate-authority-types";

type TInternalCertificateAuthorityServiceFactoryDep = {
  certificateAuthorityDAL: Pick<
    TCertificateAuthorityDALFactory,
    | "transaction"
    | "create"
    | "findById"
    | "updateById"
    | "deleteById"
    | "findOne"
    | "findByIdWithAssociatedCa"
    | "findWithAssociatedCa"
    | "countCasByOrgId"
    | "countInternalCasByOrgId"
  >;
  internalCertificateAuthorityDAL: Pick<
    TInternalCertificateAuthorityDALFactory,
    "transaction" | "create" | "findById" | "updateById" | "deleteById" | "findOne" | "update"
  >;
  certificateAuthorityCertDAL: Pick<
    TCertificateAuthorityCertDALFactory,
    "create" | "findOne" | "transaction" | "find" | "findById"
  >;
  certificateAuthoritySecretDAL: Pick<TCertificateAuthoritySecretDALFactory, "create" | "findOne" | "primaryNode">;
  certificateAuthorityCrlDAL: Pick<TCertificateAuthorityCrlDALFactory, "create" | "findOne" | "update">;
  certificateTemplateDAL: Pick<TCertificateTemplateDALFactory, "getById" | "find">;
  certificateProfileDAL: Pick<TCertificateProfileDALFactory, "findByCaId">;
  certificateAuthorityQueue: TCertificateAuthorityQueueFactory; // TODO: Pick
  certificateDAL: Pick<TCertificateDALFactory, "transaction" | "create" | "find">;
  certificateSecretDAL: Pick<TCertificateSecretDALFactory, "create">;
  certificateBodyDAL: Pick<TCertificateBodyDALFactory, "create">;
  pkiCollectionDAL: Pick<TPkiCollectionDALFactory, "findById">;
  pkiCollectionItemDAL: Pick<TPkiCollectionItemDALFactory, "create">;
  projectDAL: Pick<TProjectDALFactory, "findOne" | "updateById" | "findById" | "transaction" | "findProjectBySlug">;
  kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey" | "decryptWithKmsKey">;
  permissionService: Pick<TPermissionServiceFactory, "getProjectPermission">;
  licenseService: Pick<TLicenseServiceFactory, "getPlan" | "getInstanceType">;
  caSigningConfigDAL: Pick<TCaSigningConfigDALFactory, "findByCaId" | "create" | "deleteById" | "transaction">;
  usageMeteringService: Pick<TUsageMeteringServiceFactory, "emitForProject">;
  caKeySourceService: Pick<
    TCaKeySourceServiceFactory,
    "createKey" | "buildSigner" | "describeKey" | "prepareKeyReleases"
  >;
};

export type TInternalCertificateAuthorityServiceFactory = ReturnType<typeof internalCertificateAuthorityServiceFactory>;

export const internalCertificateAuthorityServiceFactory = ({
  certificateAuthorityDAL,
  certificateAuthorityCertDAL,
  certificateAuthoritySecretDAL,
  certificateAuthorityCrlDAL,
  certificateTemplateDAL,
  certificateProfileDAL,
  certificateDAL,
  certificateBodyDAL,
  certificateSecretDAL,
  pkiCollectionDAL,
  pkiCollectionItemDAL,
  internalCertificateAuthorityDAL,
  projectDAL,
  kmsService,
  permissionService,
  licenseService,
  caSigningConfigDAL,
  usageMeteringService,
  caKeySourceService
}: TInternalCertificateAuthorityServiceFactoryDep) => {
  const $validatePqcLicense = (keyAlgorithm: string, projectId: string) =>
    validatePqcLicense({ keyAlgorithm, projectId, projectDAL, licenseService });

  // Gates only Infisical's own distribution point. Custom URLs are gated where the CA is configured.
  const $isManagedCrlDistributionAllowed = async (projectId: string) => {
    const project = await projectDAL.findById(projectId);
    if (!project) throw new NotFoundError({ message: `Project with ID '${projectId}' not found` });

    const plan = await licenseService.getPlan(project.orgId);
    return plan.caCrl;
  };

  const $isOcspAllowed = async (projectId: string) => {
    const project = await projectDAL.findById(projectId);
    if (!project) throw new NotFoundError({ message: `Project with ID '${projectId}' not found` });

    const plan = await licenseService.getPlan(project.orgId);
    return plan.pkiOcsp;
  };

  // Root CAs: only keyCertSign + cRLSign (they don't perform end-entity operations)
  const ROOT_CA_KEY_USAGES = x509.KeyUsageFlags.keyCertSign | x509.KeyUsageFlags.cRLSign;
  // PQC root CAs also need digitalSignature per FIPS 204/205
  const PQC_ROOT_CA_KEY_USAGES = ROOT_CA_KEY_USAGES | x509.KeyUsageFlags.digitalSignature;
  // Intermediate CAs: add digitalSignature + keyEncipherment for leaf cert operations
  const PQC_CA_KEY_USAGES = ROOT_CA_KEY_USAGES | x509.KeyUsageFlags.digitalSignature;
  const CLASSICAL_CA_KEY_USAGES = PQC_CA_KEY_USAGES | x509.KeyUsageFlags.keyEncipherment;

  const $getSignatureKeyFamily = (sigAlg: string): string => {
    if (sigAlg.startsWith("ML-DSA")) return "ML-DSA";
    if (sigAlg.startsWith("SLH-DSA")) return "SLH-DSA";
    return sigAlg.split("-")[0];
  };

  const $checkSignature = (caKeyAlg: string, requestedKeyType: string, signatureAlgorithm?: string) => {
    const isRsaCa = caKeyAlg.startsWith("RSA");
    const isEcdsaCa = caKeyAlg.startsWith("EC") || caKeyAlg.startsWith("ECDSA");
    const isMlDsaCa = caKeyAlg.startsWith("ML-DSA");
    const isSlhDsaCa = caKeyAlg.startsWith("SLH-DSA");

    let caSupports = "unknown";
    if (isRsaCa) caSupports = "RSA";
    else if (isEcdsaCa) caSupports = "ECDSA";
    else if (isMlDsaCa) caSupports = "ML-DSA";
    else if (isSlhDsaCa) caSupports = "SLH-DSA";

    // PQC: exact variant match required (ML-DSA-44 CA can only sign with ML-DSA-44)
    if ((isMlDsaCa || isSlhDsaCa) && signatureAlgorithm) {
      if (signatureAlgorithm !== caKeyAlg) {
        throw new BadRequestError({
          message: `Requested signature algorithm ${signatureAlgorithm} does not match CA key algorithm ${caKeyAlg}. PQC CAs require an exact algorithm match.`
        });
      }
      return;
    }

    const isRequestValid = (requestedKeyType === "RSA" && isRsaCa) || (requestedKeyType === "ECDSA" && isEcdsaCa);

    if (!isRequestValid) {
      throw new BadRequestError({
        message: `Requested signature algorithm ${signatureAlgorithm} is not compatible with CA key algorithm ${caKeyAlg}. CA can only sign with ${caSupports}-based signature algorithms.`
      });
    }
  };

  /**
   * Validates CA path length constraints and creates the BasicConstraints extension.
   * This handles both policy-level validation and issuing CA hierarchy validation.
   */
  const $createBasicConstraintsExtension = ({
    basicConstraints,
    pathLength,
    caCertObj
  }: {
    basicConstraints?: { isCA: boolean; pathLength?: number } | null;
    pathLength?: number | null;
    caCertObj: x509.X509Certificate;
  }): x509.BasicConstraintsExtension => {
    // Decide from the value, never from presence. An explicit { isCA: false } reaches here from stored
    // certificate requests and CSR-derived constraints, and every upstream policy gate keys on
    // isCA === true. Treating a present-but-false object as a CA request would stamp a critical
    // CA:TRUE extension onto a request that asked for a leaf and that no gate had cause to reject.
    const shouldIssueCaCertificate = basicConstraints?.isCA === true;

    if (!shouldIssueCaCertificate) {
      return new x509.BasicConstraintsExtension(false);
    }

    // Validate against policy's maxPathLength
    const policyMaxPathLength = basicConstraints.pathLength;
    if (policyMaxPathLength !== undefined && policyMaxPathLength !== null && policyMaxPathLength !== -1) {
      if (pathLength === undefined || pathLength === null) {
        throw new BadRequestError({
          message: `Path length is required when issuing CA certificates because the policy only allows a maximum path length of ${policyMaxPathLength}.`
        });
      }
      if (pathLength > policyMaxPathLength) {
        throw new BadRequestError({
          message: `Path length (${pathLength}) exceeds policy's maximum allowed path length (${policyMaxPathLength})`
        });
      }
    }

    // Validate against issuing CA's BasicConstraints
    // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
    const issuingCaBasicConstraints = caCertObj.getExtension("2.5.29.19") as x509.BasicConstraintsExtension | null;

    if (issuingCaBasicConstraints) {
      const issuingCaPathLength = issuingCaBasicConstraints.pathLength;

      if (issuingCaPathLength === 0) {
        throw new BadRequestError({
          message: "Issuing CA cannot issue subordinate CA certificates (path length is 0)"
        });
      }

      if (issuingCaPathLength !== undefined && pathLength !== undefined && pathLength !== null) {
        if (pathLength >= issuingCaPathLength) {
          throw new BadRequestError({
            message: `Path length (${pathLength}) must be less than issuing CA's path length (${issuingCaPathLength})`
          });
        }
      }

      const effectivePathLength =
        pathLength !== undefined && pathLength !== null
          ? pathLength
          : issuingCaPathLength !== undefined
            ? issuingCaPathLength - 1
            : undefined;

      return new x509.BasicConstraintsExtension(true, effectivePathLength, true);
    }

    const effectivePathLength = pathLength !== undefined && pathLength !== null ? pathLength : undefined;
    return new x509.BasicConstraintsExtension(true, effectivePathLength, true);
  };

  const createCa = async ({
    type,
    friendlyName,
    commonName,
    organization,
    ou,
    country,
    province,
    locality,
    notBefore,
    notAfter,
    maxPathLength,
    keyAlgorithm,
    name,
    crlDistributionPointUrls,
    disableManagedCrlDistributionPointUrl,
    isOcspEnabled,
    ...dto
  }: TCreateCaDTO) => {
    let projectId: string;
    if (dto.isInternal) {
      projectId = dto.projectId;
    } else if (dto.projectId) {
      projectId = dto.projectId;
    } else if (dto.projectSlug) {
      const project = await projectDAL.findProjectBySlug(dto.projectSlug, dto.actorOrgId);
      if (!project) throw new NotFoundError({ message: `Project with slug '${dto.projectSlug}' not found` });
      projectId = project.id;
    } else {
      throw new BadRequestError({ message: "Either projectId or projectSlug is required" });
    }
    if (!dto.isInternal) {
      const { permission } = await permissionService.getProjectPermission({
        actor: dto.actor,
        actorId: dto.actorId,
        projectId,
        actorAuthMethod: dto.actorAuthMethod,
        actorOrgId: dto.actorOrgId,
        actionProjectType: ActionProjectType.CertificateManager
      });

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateAuthorityActions.Create,
        subject(ProjectPermissionSub.CertificateAuthorities, { name: commonName })
      );
    }

    if (keyAlgorithm.startsWith("SLH-DSA")) {
      throw new BadRequestError({
        message: "SLH-DSA algorithms are not currently supported for CA creation. Use ML-DSA instead."
      });
    }

    await $validatePqcLicense(keyAlgorithm, projectId);

    // Enforced here rather than only in certificateAuthorityService.createCertificateAuthority: the
    // POST /cert-manager/ca route calls this method directly, so a check that only sat there was
    // bypassable.
    await assertCertificateAuthorityQuota({
      projectId,
      isInternal: true,
      deps: { projectDAL, licenseService, certificateAuthorityDAL }
    });

    // Refused rather than silently dropped, since the caller asked for it explicitly.
    if (crlDistributionPointUrls?.length && !(await $isManagedCrlDistributionAllowed(projectId))) {
      throw new BadRequestError({
        message:
          "Failed to create certificate authority with CRL distribution points due to plan restriction. Upgrade plan to use certificate revocation lists."
      });
    }

    const isOcspAllowed = await $isOcspAllowed(projectId);
    if (isOcspEnabled && !isOcspAllowed) {
      throw new BadRequestError({
        message:
          "Failed to create certificate authority with OCSP enabled due to plan restriction. Upgrade plan to use OCSP."
      });
    }

    const resolvedIsOcspEnabled = isOcspEnabled ?? isOcspAllowed;

    const dn = createDistinguishedName({
      commonName,
      organization,
      ou,
      country,
      province,
      locality
    });

    // The composed DN is persisted in a varchar(255) column, so bounding each subject attribute on its
    // own is not enough — the escaped RFC 4514 string has to fit too.
    if (dn.length > PKI_TEXT_COLUMN_MAX_LENGTH) {
      throw new BadRequestError({
        message: `The combined subject (${dn.length} characters) cannot exceed ${PKI_TEXT_COLUMN_MAX_LENGTH} characters. Shorten the commonName, organization, ou, country, province, or locality fields.`
      });
    }

    const alg = keyAlgorithmToAlgCfg(keyAlgorithm);

    const resolvedCaName = name || slugify(`${(friendlyName || dn).slice(0, 16)}-${alphaNumericNanoId(8)}`);

    const keySource = dto.isInternal ? (dto.keySource ?? CertKeySource.Infisical) : CertKeySource.Infisical;

    let caSigner: TCaSigner;
    let localKeys: CryptoKeyPair | undefined;
    let externalKeyReference: Partial<TExternalCaKeyReference> | undefined;

    try {
      if (dto.isInternal && isExternalCaKeySource(keySource)) {
        // A duplicate name only fails the (name, projectId) unique constraint after the external key
        // is created. HSM keys cannot be removed again and AWS KMS keys can only be scheduled for
        // deletion, so reject a taken name before creating any key.
        if (name) {
          const existingCa = await certificateAuthorityDAL.findOne({ projectId, name });
          if (existingCa) {
            throw new BadRequestError({
              message: `A certificate authority named "${name}" already exists in this project.`
            });
          }
        }

        externalKeyReference = await caKeySourceService.createKey({
          keySource,
          projectId,
          caName: resolvedCaName,
          keyAlgorithm,
          input: dto
        });
        caSigner = await caKeySourceService.buildSigner({
          keySource,
          projectId,
          keyAlgorithm,
          reference: externalKeyReference
        });
      } else {
        const cryptoEngine = isPqcAlgorithm(keyAlgorithm) ? getPqcCrypto() : crypto.nativeCrypto;
        localKeys = await cryptoEngine.subtle.generateKey(alg as RsaHashedKeyGenParams, true, ["sign", "verify"]);
        caSigner = buildLocalCaSigner({
          privateKey: localKeys.privateKey,
          publicKey: localKeys.publicKey,
          signingAlgorithm: alg
        });
      }

      let notBeforeDate: Date | undefined;
      let notAfterDate: Date | undefined;
      if (notAfter) {
        notBeforeDate = notBefore ? new Date(notBefore) : new Date();
        // if undefined, set [notAfterDate] to 10 years from now
        notAfterDate = new Date(notAfter);
      }
      const serialNumber = createSerialNumber();

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

      let encryptedPrivateKey: Buffer | undefined;
      if (localKeys) {
        const localPrivateKey = localKeys.privateKey;
        // https://nodejs.org/api/crypto.html#static-method-keyobjectfromkey
        let privateKeyDer: Buffer;
        if (isPqcCryptoKey(localPrivateKey)) {
          privateKeyDer = await exportPqcKeyToDer(localPrivateKey);
        } else {
          const skObj = crypto.nativeCrypto.KeyObject.from(localPrivateKey);
          privateKeyDer = skObj.export({ type: "pkcs8", format: "der" });
        }
        encryptedPrivateKey = (await kmsEncryptor({ plainText: privateKeyDer })).cipherTextBlob;
      }

      let rootCertMaterial: { encryptedCertificate: Buffer; encryptedCertificateChain: Buffer } | undefined;
      if (type === InternalCaType.ROOT && notAfter) {
        const cert = await caSigner.createCertificate({
          subject: dn,
          issuer: dn,
          serialNumber,
          notBefore: notBeforeDate,
          notAfter: notAfterDate,
          publicKey: caSigner.caPublicKey,
          extensions: [
            new x509.BasicConstraintsExtension(
              true,
              maxPathLength === -1 || maxPathLength === null ? undefined : maxPathLength,
              true
            ),
            new x509.KeyUsagesExtension(
              isPqcAlgorithm(keyAlgorithm) ? PQC_ROOT_CA_KEY_USAGES : ROOT_CA_KEY_USAGES,
              true
            ),
            await x509.SubjectKeyIdentifierExtension.create(caSigner.caPublicKey)
          ]
        });

        rootCertMaterial = {
          encryptedCertificate: (await kmsEncryptor({ plainText: Buffer.from(new Uint8Array(cert.rawData)) }))
            .cipherTextBlob,
          encryptedCertificateChain: (await kmsEncryptor({ plainText: Buffer.alloc(0) })).cipherTextBlob
        };
      }

      const thisUpdate = new Date();
      const nextUpdate = new Date(thisUpdate);
      nextUpdate.setDate(nextUpdate.getDate() + DEFAULT_CRL_VALIDITY_DAYS);
      const crl = await caSigner.createCrl({
        issuer: dn,
        thisUpdate,
        nextUpdate,
        entries: []
      });
      const { cipherTextBlob: encryptedCrl } = await kmsEncryptor({
        plainText: Buffer.from(new Uint8Array(crl.rawData))
      });

      const newCa = await certificateAuthorityDAL.transaction(async (tx) => {
        const ca = await certificateAuthorityDAL
          .create(
            {
              projectId,
              name: resolvedCaName,
              status: notAfter && type === InternalCaType.ROOT ? CaStatus.ACTIVE : CaStatus.PENDING_CERTIFICATE,
              enableDirectIssuance: false
            },
            tx
          )
          .catch((error) => {
            // unique_violation: same CA name in the same project
            if (
              error instanceof DatabaseError &&
              (error.error as { code?: string })?.code === DatabaseErrorCode.UniqueViolation
            ) {
              throw new BadRequestError({
                message: `A certificate authority named "${resolvedCaName}" already exists.`
              });
            }
            throw error;
          });

        const internalCa = await internalCertificateAuthorityDAL.create(
          {
            caId: ca.id,
            type,
            organization,
            ou,
            country,
            province,
            locality,
            friendlyName: friendlyName || dn,
            commonName,
            dn,
            keyAlgorithm,
            crlDistributionPointUrls: crlDistributionPointUrls ?? [],
            disableManagedCrlDistributionPointUrl: disableManagedCrlDistributionPointUrl ?? false,
            isOcspEnabled: resolvedIsOcspEnabled,
            ...(type === InternalCaType.ROOT && {
              maxPathLength,
              ...(notAfter && {
                notBefore: notBeforeDate,
                notAfter: notAfterDate,
                serialNumber
              })
            })
          },
          tx
        );

        const caSecret = await certificateAuthoritySecretDAL.create(
          { caId: ca.id, keySource, encryptedPrivateKey, ...externalKeyReference },
          tx
        );

        if (rootCertMaterial) {
          const caCert = await certificateAuthorityCertDAL.create(
            {
              caId: ca.id,
              encryptedCertificate: rootCertMaterial.encryptedCertificate,
              encryptedCertificateChain: rootCertMaterial.encryptedCertificateChain,
              version: 1,
              caSecretId: caSecret.id
            },
            tx
          );

          await internalCertificateAuthorityDAL.updateById(internalCa.id, { activeCaCertId: caCert.id }, tx);
        }

        await certificateAuthorityCrlDAL.create(
          {
            caId: ca.id,
            encryptedCrl,
            caSecretId: caSecret.id
          },
          tx
        );

        return certificateAuthorityDAL.findByIdWithAssociatedCa(ca.id, tx);
      });

      return expandInternalCa(newCa);
    } catch (error) {
      if (externalKeyReference) {
        const releaseKey = await caKeySourceService.prepareKeyReleases([{ keySource, ...externalKeyReference }]);
        await releaseKey();
      }
      throw error;
    }
  };

  /**
   * Return CA with id [caId]
   */
  const getCaById = async ({ caId, actorId, actorAuthMethod, actor, actorOrgId }: TGetCaDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: ca.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.Read,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
    );

    return expandInternalCa(ca);
  };

  /**
   * Update CA with id [caId].
   * Note: Used to enable/disable CA and edit CRL distribution point URLs
   */
  const updateCaById = async ({
    caId,
    status,
    name,
    crlDistributionPointUrls,
    disableManagedCrlDistributionPointUrl,
    isOcspEnabled,
    ...dto
  }: TUpdateCaDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

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

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateAuthorityActions.Edit,
        subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
      );
    }

    // Gating creation alone would let an org create a CA with no distribution points and patch them in.
    // Keyed on newly added URLs, so keeping, reordering, or clearing the existing set stays open, as does
    // toggling the managed distribution point.
    const storedCrlUrls = new Set(ca.internalCa.crlDistributionPointUrls ?? []);
    const addedCrlUrl = (crlDistributionPointUrls ?? []).find((url) => !storedCrlUrls.has(url));
    if (addedCrlUrl && !(await $isManagedCrlDistributionAllowed(ca.projectId))) {
      throw new BadRequestError({
        message: `Failed to add the CRL distribution point '${addedCrlUrl}' due to plan restriction. Upgrade plan to use certificate revocation lists.`
      });
    }

    if (isOcspEnabled && !ca.internalCa.isOcspEnabled) {
      if (!(await $isOcspAllowed(ca.projectId))) {
        throw new BadRequestError({
          message: "Failed to enable OCSP due to plan restriction. Upgrade plan to use OCSP."
        });
      }
    }

    const updatedCa = await certificateAuthorityDAL.transaction(async (tx) => {
      if (status !== undefined || name !== undefined) {
        await certificateAuthorityDAL.updateById(ca.id, { status, name }, tx);
      }

      if (
        crlDistributionPointUrls !== undefined ||
        disableManagedCrlDistributionPointUrl !== undefined ||
        isOcspEnabled !== undefined
      ) {
        await internalCertificateAuthorityDAL.update(
          { caId: ca.id },
          {
            ...(crlDistributionPointUrls !== undefined && { crlDistributionPointUrls }),
            ...(disableManagedCrlDistributionPointUrl !== undefined && { disableManagedCrlDistributionPointUrl }),
            ...(isOcspEnabled !== undefined && { isOcspEnabled })
          },
          tx
        );
      }

      return certificateAuthorityDAL.findByIdWithAssociatedCa(caId, tx);
    });

    return expandInternalCa(updatedCa);
  };

  /**
   * Delete CA with id [caId]
   */
  const deleteCaById = async ({ caId, actorId, actorAuthMethod, actor, actorOrgId }: TDeleteCaDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

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

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.Delete,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
    );

    await assertNoCertificateProfilesUsingCa(certificateProfileDAL, ca.id, ca.name);

    const keyDetails = await deleteCaAndReleaseExternalKey({
      caId: ca.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      caKeySourceService
    });

    return { ca: expandInternalCa(ca), keyDetails };
  };

  /**
   * Return certificate signing request (CSR) made with CA with id [caId]
   */
  const getCaCsr = async (params: TGetCaCsrDTO) => {
    const { caId, maxPathLength } = params;
    const isInternal = "isInternal" in params && params.isInternal === true;
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

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

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateAuthorityActions.Create,
        subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
      );
    }

    if (ca.internalCa.type === InternalCaType.ROOT)
      throw new BadRequestError({ message: "Root CA cannot generate CSR" });

    await $validatePqcLicense(ca.internalCa.keyAlgorithm, ca.projectId);

    const { signer } = await getCaSigner({
      caId,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService
    });

    const effectivePathLength =
      maxPathLength !== undefined && maxPathLength >= 0 ? maxPathLength : (ca.internalCa.maxPathLength ?? -1);
    const resolvedPathLength =
      effectivePathLength === -1 || effectivePathLength === null ? undefined : effectivePathLength;

    const csrObj = await signer.createCsr({
      name: ca.internalCa.dn,
      extensions: [
        new x509.BasicConstraintsExtension(true, resolvedPathLength, true),
        new x509.KeyUsagesExtension(
          isPqcAlgorithm(ca.internalCa.keyAlgorithm) ? PQC_CA_KEY_USAGES : CLASSICAL_CA_KEY_USAGES,
          true
        ),
        await x509.SubjectKeyIdentifierExtension.create(signer.caPublicKey)
      ],
      attributes: [new x509.ChallengePasswordAttribute("password")]
    });

    return {
      csr: csrObj.toString("pem"),
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Renew certificate for CA with id [caId]
   * Note 1: This CA renewal method is only applicable to CAs with internal parent CAs
   * Note 2: Currently implements CA renewal with same key-pair only
   */
  const renewCaCert = async ({
    caId,
    notAfter,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId,
    ...rest
  }: TRenewCaCertDTO) => {
    const isInternal = "isInternal" in rest && rest.isInternal === true;

    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

    if (!ca.internalCa.activeCaCertId)
      throw new BadRequestError({ message: "CA does not have a certificate installed" });

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

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateAuthorityActions.IssueCACertificate,
        subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
      );
    }

    if (ca.status === CaStatus.DISABLED) throw new BadRequestError({ message: "CA is disabled" });

    await $validatePqcLicense(ca.internalCa.keyAlgorithm, ca.projectId);

    // get latest CA certificate
    const caCert = await certificateAuthorityCertDAL.findById(ca.internalCa.activeCaCertId);

    const serialNumber = createSerialNumber();

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

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

    const { caSecret, signer } = await getCaSigner({
      caId: ca.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService
    });

    const kmsDecryptor = await kmsService.decryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });
    const decryptedCaCert = await kmsDecryptor({
      cipherTextBlob: caCert.encryptedCertificate
    });

    const caCertObj = new x509.X509Certificate(decryptedCaCert);

    let certificate = "";
    let certificateChain = "";
    let newCertId = "";

    switch (ca.internalCa.type) {
      case InternalCaType.ROOT: {
        if (new Date(notAfter) <= new Date(caCertObj.notAfter)) {
          throw new BadRequestError({
            message:
              "New Root CA certificate must have notAfter date that is greater than the current certificate notAfter date"
          });
        }

        const notBeforeDate = new Date();
        const cert = await signer.createCertificate({
          subject: ca.internalCa.dn,
          issuer: ca.internalCa.dn,
          serialNumber,
          notBefore: notBeforeDate,
          notAfter: new Date(notAfter),
          publicKey: signer.caPublicKey,
          extensions: [
            new x509.BasicConstraintsExtension(
              true,
              ca.internalCa.maxPathLength === -1 || !ca.internalCa.maxPathLength
                ? undefined
                : ca.internalCa.maxPathLength,
              true
            ),
            new x509.KeyUsagesExtension(
              isPqcAlgorithm(ca.internalCa.keyAlgorithm) ? PQC_ROOT_CA_KEY_USAGES : ROOT_CA_KEY_USAGES,
              true
            ),
            await x509.SubjectKeyIdentifierExtension.create(signer.caPublicKey)
          ]
        });

        const { cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
          plainText: Buffer.from(new Uint8Array(cert.rawData))
        });

        const { cipherTextBlob: encryptedCertificateChain } = await kmsEncryptor({
          plainText: Buffer.alloc(0)
        });

        await internalCertificateAuthorityDAL.transaction(async (tx) => {
          const newCaCert = await certificateAuthorityCertDAL.create(
            {
              caId: ca.id,
              encryptedCertificate,
              encryptedCertificateChain,
              version: caCert.version + 1,
              caSecretId: caSecret.id
            },
            tx
          );

          newCertId = newCaCert.id;

          await internalCertificateAuthorityDAL.update(
            {
              caId: ca.id
            },
            {
              activeCaCertId: newCaCert.id,
              notBefore: notBeforeDate,
              notAfter: new Date(notAfter)
            },
            tx
          );
        });

        certificate = cert.toString("pem");
        break;
      }
      case InternalCaType.INTERMEDIATE: {
        if (!ca.internalCa.parentCaId) {
          // TODO: look into optimal way to support renewal of intermediate CA with external parent CA
          throw new BadRequestError({
            message: "Failed to renew intermediate CA certificate with external parent CA"
          });
        }

        const parentCa = await certificateAuthorityDAL.findByIdWithAssociatedCa(ca.internalCa.parentCaId);
        const { signer: parentSigner } = await getCaSigner({
          caId: parentCa.id,
          certificateAuthorityDAL,
          certificateAuthoritySecretDAL,
          projectDAL,
          kmsService,
          caKeySourceService
        });

        if (!parentCa.internalCa) {
          throw new BadRequestError({ message: "Parent CA not found" });
        }

        // get latest parent CA certificate
        if (!parentCa.internalCa.activeCaCertId)
          throw new BadRequestError({ message: "Parent CA does not have a certificate installed" });

        const parentCaCert = await certificateAuthorityCertDAL.findById(parentCa.internalCa.activeCaCertId);

        const decryptedParentCaCert = await kmsDecryptor({
          cipherTextBlob: parentCaCert.encryptedCertificate
        });

        const parentCaCertObj = new x509.X509Certificate(decryptedParentCaCert);

        if (new Date(notAfter) <= new Date(caCertObj.notAfter)) {
          throw new BadRequestError({
            message:
              "New Intermediate CA certificate must have notAfter date that is greater than the current certificate notAfter date"
          });
        }

        if (new Date(notAfter) > new Date(parentCaCertObj.notAfter)) {
          throw new BadRequestError({
            message:
              "New Intermediate CA certificate must have notAfter date that is equal to or smaller than the notAfter date of the parent CA certificate current certificate notAfter date"
          });
        }

        const csrObj = await signer.createCsr({
          name: ca.internalCa.dn,
          extensions: [
            new x509.KeyUsagesExtension(
              isPqcAlgorithm(ca.internalCa.keyAlgorithm) ? PQC_CA_KEY_USAGES : CLASSICAL_CA_KEY_USAGES
            )
          ],
          attributes: [new x509.ChallengePasswordAttribute("password")]
        });

        const notBeforeDate = new Date();
        const intermediateCert = await parentSigner.createCertificate({
          serialNumber,
          subject: csrObj.subject,
          issuer: parentCaCertObj.subject,
          notBefore: notBeforeDate,
          notAfter: new Date(notAfter),
          publicKey: csrObj.publicKey,
          extensions: [
            new x509.KeyUsagesExtension(
              isPqcAlgorithm(ca.internalCa.keyAlgorithm) ? PQC_CA_KEY_USAGES : CLASSICAL_CA_KEY_USAGES,
              true
            ),
            new x509.BasicConstraintsExtension(
              true,
              ca.internalCa.maxPathLength === -1 || !ca.internalCa.maxPathLength
                ? undefined
                : ca.internalCa.maxPathLength,
              true
            ),
            await x509.AuthorityKeyIdentifierExtension.create(parentCaCertObj, false),
            await x509.SubjectKeyIdentifierExtension.create(csrObj.publicKey)
          ]
        });

        const { cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
          plainText: Buffer.from(new Uint8Array(intermediateCert.rawData))
        });

        const { caCert: parentCaCertificate, caCertChain: parentCaCertChain } = await getCaCertChain({
          caCertId: parentCa.internalCa.activeCaCertId,
          certificateAuthorityDAL,
          certificateAuthorityCertDAL,
          projectDAL,
          kmsService
        });

        certificateChain = `${parentCaCertificate}\n${parentCaCertChain}`.trim();

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

        await internalCertificateAuthorityDAL.transaction(async (tx) => {
          const newCaCert = await certificateAuthorityCertDAL.create(
            {
              caId: ca.id,
              encryptedCertificate,
              encryptedCertificateChain,
              version: caCert.version + 1,
              caSecretId: caSecret.id
            },
            tx
          );

          newCertId = newCaCert.id;

          await internalCertificateAuthorityDAL.update(
            {
              caId: ca.id
            },
            {
              activeCaCertId: newCaCert.id,
              notBefore: notBeforeDate,
              notAfter: new Date(notAfter)
            },
            tx
          );
        });

        certificate = intermediateCert.toString("pem");
        break;
      }
      default: {
        throw new BadRequestError({
          message: "Unrecognized CA type"
        });
      }
    }

    return {
      certificate,
      certificateChain,
      serialNumber,
      certId: newCertId,
      ca: {
        ...ca,
        ...ca.internalCa
      }
    };
  };

  const getCaCerts = async ({ caId, actorId, actorAuthMethod, actor, actorOrgId }: TGetCaCertsDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

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

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.Read,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
    );

    const caCertChains = await getCaCertChains({
      caId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    return {
      ca: expandInternalCa(ca),
      caCerts: caCertChains
    };
  };

  /**
   * Return list of past and current CA certificates for a CA. CA certificates are public trust
   * material (no private keys)
   */
  const getCaCertsPublic = async ({ caId }: { caId: string }) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

    const caCertChains = await getCaCertChains({
      caId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    return {
      ca: expandInternalCa(ca),
      caCerts: caCertChains
    };
  };

  /**
   * Return current certificate and certificate chain for CA
   */
  const getCaCert = async ({ caId, actorId, actorAuthMethod, actor, actorOrgId }: TGetCaCertDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });
    if (!ca.internalCa.activeCaCertId)
      throw new BadRequestError({ message: "CA does not have a certificate installed" });

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

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.Read,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
    );

    const { caCert, caCertChain, serialNumber } = await getCaCertChain({
      caCertId: ca.internalCa.activeCaCertId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    return {
      certificate: caCert,
      certificateChain: caCertChain,
      serialNumber,
      certId: ca.internalCa.activeCaCertId,
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Return current certificate and certificate chain for CA.
   * CA certificates are public trust material (no private keys).
   */
  const getCaCertPublic = async ({ caId }: { caId: string }) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });
    if (!ca.internalCa.activeCaCertId)
      throw new BadRequestError({ message: "CA does not have a certificate installed" });

    const { caCert, caCertChain, serialNumber } = await getCaCertChain({
      caCertId: ca.internalCa.activeCaCertId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    return {
      certificate: caCert,
      certificateChain: caCertChain,
      serialNumber,
      certId: ca.internalCa.activeCaCertId,
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Return CA certificate object by ID
   */
  const getCaCertById = async ({ caId, caCertId }: { caId: string; caCertId: string }) => {
    const caCert = await certificateAuthorityCertDAL.findOne({
      caId,
      id: caCertId
    });

    if (!caCert) {
      throw new NotFoundError({ message: `Ca certificate with ID '${caCertId}' not found for CA with ID '${caId}'` });
    }

    const ca = await certificateAuthorityDAL.findById(caId);
    const keyId = await getProjectKmsCertificateKeyId({
      projectId: ca.projectId,
      projectDAL,
      kmsService
    });

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

    const decryptedCaCert = await kmsDecryptor({
      cipherTextBlob: caCert.encryptedCertificate
    });

    const caCertObj = new x509.X509Certificate(decryptedCaCert);

    return caCertObj;
  };

  const getCaCertByIdWithAuth = async ({
    caId,
    certId,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId
  }: TGetCaCertByIdDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

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

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.Read,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
    );

    const caCert = await certificateAuthorityCertDAL.findOne({
      caId,
      id: certId
    });

    if (!caCert) {
      throw new NotFoundError({ message: `Certificate with ID '${certId}' not found for CA with ID '${caId}'` });
    }

    const {
      caCert: certificate,
      caCertChain: certificateChain,
      serialNumber
    } = await getCaCertChain({
      caCertId: certId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    let notBefore: Date | undefined;
    let notAfter: Date | undefined;
    let maxPathLength: number | undefined;
    try {
      const certObj = new x509.X509Certificate(certificate);
      notBefore = certObj.notBefore;
      notAfter = certObj.notAfter;
      const basicConstraintsExt = certObj.getExtension(x509.BasicConstraintsExtension);
      if (basicConstraintsExt && basicConstraintsExt.ca) {
        maxPathLength = basicConstraintsExt.pathLength ?? -1;
      }
    } catch {
      // ignore parse errors and return undefined values
    }

    return {
      certificate,
      certificateChain,
      serialNumber,
      certId,
      notBefore,
      notAfter,
      maxPathLength,
      parentCaId: ca.internalCa.parentCaId ?? undefined,
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Return a specific CA certificate and chain by ID.
   * CA certificates are public trust material (no private keys).
   */
  const getCaCertByIdPublic = async ({ caId, certId }: { caId: string; certId: string }) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

    const caCert = await certificateAuthorityCertDAL.findOne({
      caId,
      id: certId
    });

    if (!caCert) {
      throw new NotFoundError({ message: `Certificate with ID '${certId}' not found for CA with ID '${caId}'` });
    }

    const {
      caCert: certificate,
      caCertChain: certificateChain,
      serialNumber
    } = await getCaCertChain({
      caCertId: certId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    let notBefore: Date | undefined;
    let notAfter: Date | undefined;
    let maxPathLength: number | undefined;
    try {
      const certObj = new x509.X509Certificate(certificate);
      notBefore = certObj.notBefore;
      notAfter = certObj.notAfter;
      const basicConstraintsExt = certObj.getExtension(x509.BasicConstraintsExtension);
      if (basicConstraintsExt && basicConstraintsExt.ca) {
        maxPathLength = basicConstraintsExt.pathLength ?? -1;
      }
    } catch {
      // ignore parse errors and return undefined values
    }

    return {
      certificate,
      certificateChain,
      serialNumber,
      certId,
      notBefore,
      notAfter,
      maxPathLength,
      parentCaId: ca.internalCa.parentCaId ?? undefined,
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Issue certificate to be imported back in for intermediate CA
   */
  const signIntermediate = async (params: TSignIntermediateDTO) => {
    const { caId, csr, notBefore, notAfter, maxPathLength } = params;
    const isInternal = "isInternal" in params && params.isInternal === true;
    const appCfg = getConfig();
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: "CA not found" });

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

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateAuthorityActions.SignIntermediate,
        subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
      );
    }

    if (ca.status === CaStatus.DISABLED) throw new BadRequestError({ message: "CA is disabled" });
    if (!ca.internalCa.activeCaCertId)
      throw new BadRequestError({ message: "CA does not have a certificate installed" });

    await $validatePqcLicense(ca.internalCa.keyAlgorithm, ca.projectId);

    const caCert = await certificateAuthorityCertDAL.findById(ca.internalCa.activeCaCertId);

    if (ca.internalCa.notAfter && new Date() > new Date(ca.internalCa.notAfter)) {
      throw new BadRequestError({ message: "CA is expired" });
    }

    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId: ca.projectId,
      projectDAL,
      kmsService
    });
    const kmsDecryptor = await kmsService.decryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });

    const decryptedCaCert = await kmsDecryptor({
      cipherTextBlob: caCert.encryptedCertificate
    });

    const caCertObj = new x509.X509Certificate(decryptedCaCert);
    const csrObj = new x509.Pkcs10CertificateRequest(csr);

    // check path length constraint
    const caPathLength = caCertObj.getExtension(x509.BasicConstraintsExtension)?.pathLength;
    if (caPathLength !== undefined) {
      if (caPathLength === 0)
        throw new BadRequestError({
          message: "Failed to issue intermediate certificate due to CA path length constraint"
        });
      if (maxPathLength >= caPathLength || (maxPathLength === -1 && caPathLength !== -1))
        throw new BadRequestError({
          message: "The requested path length constraint exceeds the CA's allowed path length"
        });
    }

    const notBeforeDate = notBefore ? new Date(notBefore) : new Date();
    const notAfterDate = new Date(notAfter);

    const caCertNotBeforeDate = new Date(caCertObj.notBefore);
    const caCertNotAfterDate = new Date(caCertObj.notAfter);

    // check not before constraint
    if (notBeforeDate < caCertNotBeforeDate) {
      throw new BadRequestError({ message: "notBefore date is before CA certificate's notBefore date" });
    }

    if (notBeforeDate > notAfterDate) throw new BadRequestError({ message: "notBefore date is after notAfter date" });

    // check not after constraint
    if (notAfterDate > caCertNotAfterDate) {
      throw new BadRequestError({ message: "notAfter date is after CA certificate's notAfter date" });
    }

    const { caSecret, signer } = await getCaSigner({
      caId: ca.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService
    });

    const serialNumber = createSerialNumber();

    const caCrl = await certificateAuthorityCrlDAL.findOne({ caSecretId: caSecret.id });
    const managedCdpUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/crl/${caCrl.id}/der`;
    const caIssuerUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/ca/internal/${ca.id}/certificates/${caCert.id}/der`;
    const cdpUrls = buildCrlDistributionPointUrls(
      managedCdpUrl,
      ca.internalCa.crlDistributionPointUrls,
      ca.internalCa.disableManagedCrlDistributionPointUrl
    );

    const intermediateCert = await signer.createCertificate({
      serialNumber,
      subject: csrObj.subject,
      issuer: caCertObj.subject,
      notBefore: notBeforeDate,
      notAfter: notAfterDate,
      publicKey: csrObj.publicKey,
      extensions: [
        new x509.KeyUsagesExtension(
          isPqcAlgorithm(csrObj.publicKey.algorithm.name) ? PQC_CA_KEY_USAGES : CLASSICAL_CA_KEY_USAGES,
          true
        ),
        new x509.BasicConstraintsExtension(true, maxPathLength === -1 ? undefined : maxPathLength, true),
        await x509.AuthorityKeyIdentifierExtension.create(caCertObj, false),
        await x509.SubjectKeyIdentifierExtension.create(csrObj.publicKey),
        ...(cdpUrls.length > 0 ? [new x509.CRLDistributionPointsExtension(cdpUrls)] : []),
        buildAuthorityInfoAccessExtension({ caIssuerUrl })
      ]
    });

    const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({
      caCertId: ca.internalCa.activeCaCertId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    return {
      certificate: intermediateCert.toString("pem"),
      issuingCaCertificate,
      certificateChain: `${issuingCaCertificate}\n${caCertChain}`.trim(),
      serialNumber: intermediateCert.serialNumber,
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Import certificate for CA with id [caId].
   * Note: Can be used to import an external certificate and certificate chain
   * to be into an installed or uninstalled CA.
   */
  const importCertToCa = async (params: TImportCertToCaDTO) => {
    const { caId, certificate, certificateChain } = params;
    const parentCaId = "parentCaId" in params ? params.parentCaId : undefined;
    const isInternal = "isInternal" in params && params.isInternal === true;
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

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

      ForbiddenError.from(permission).throwUnlessCan(
        ProjectPermissionCertificateAuthorityActions.Create,
        subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
      );
    }

    if (ca.internalCa.parentCaId && (!parentCaId || parentCaId !== ca.internalCa.parentCaId)) {
      /**
       * re-evaluate in the future if we should allow users to import a new CA certificate for an intermediate
       * CA chained to an internal parent CA. Doing so would allow users to re-chain the CA to a different
       * internal CA.
       */
      throw new BadRequestError({
        message: "Cannot import certificate to intermediate CA chained to internal parent CA"
      });
    }

    // No internal parent means the certificate was signed outside Infisical. createCa cannot tell the
    // two apart, because parentCaId is written here rather than at creation.
    if (!isInternal && ca.internalCa.type === InternalCaType.INTERMEDIATE && !parentCaId) {
      const caProject = await projectDAL.findById(ca.projectId);
      if (!caProject) throw new NotFoundError({ message: `Project with ID '${ca.projectId}' not found` });

      const plan = await licenseService.getPlan(caProject.orgId);
      if (!plan.pkiExternalIntermediateCa) {
        throw new BadRequestError({
          message:
            "Failed to import an externally signed certificate for this intermediate CA due to plan restriction. Upgrade plan to use externally signed intermediate CAs."
        });
      }
    }

    const caCert = ca.internalCa.activeCaCertId
      ? await certificateAuthorityCertDAL.findById(ca.internalCa.activeCaCertId)
      : undefined;

    const certObj = new x509.X509Certificate(certificate);
    const maxPathLength = certObj.getExtension(x509.BasicConstraintsExtension)?.pathLength;

    validateImportedCertificate(certObj, {
      commonName: ca.internalCa.commonName,
      organization: ca.internalCa.organization,
      ou: ca.internalCa.ou,
      country: ca.internalCa.country,
      province: ca.internalCa.province,
      locality: ca.internalCa.locality,
      maxPathLength: ca.internalCa.maxPathLength ?? null
    });

    // validate imported certificate and certificate chain
    const certificates = extractX509CertFromChain(certificateChain)?.map((cert) => new x509.X509Certificate(cert));

    if (!certificates) throw new BadRequestError({ message: "Failed to parse certificate chain" });

    const chain = new x509.X509ChainBuilder({
      certificates
    });

    const chainItems = await chain.build(certObj);

    // chain.build() implicitly verifies the chain
    if (chainItems.length !== certificates.length + 1)
      throw new BadRequestError({ message: "Invalid certificate chain" });

    const parentCertObj = chainItems[1];
    const parentSerialNumber = parentCertObj.serialNumber;

    const [parentCa] = await certificateAuthorityDAL.findWithAssociatedCa({
      [`${TableName.CertificateAuthority}.projectId` as "projectId"]: ca.projectId,
      [`${TableName.InternalCertificateAuthority}.serialNumber` as "serialNumber"]: parentSerialNumber
    });

    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId: ca.projectId,
      projectDAL,
      kmsService
    });
    const kmsEncryptor = await kmsService.encryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });

    const { cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
      plainText: Buffer.from(new Uint8Array(certObj.rawData))
    });

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

    // TODO: validate that latest key-pair of CA is used to sign the certificate
    // once renewal with new key pair is supported
    const { caSecret, signer } = await getCaSigner({
      caId: ca.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService
    });
    const { caPublicKey } = signer;

    const caPublicKeySpki = isPqcCryptoKey(caPublicKey)
      ? await exportPqcKeyToDer(caPublicKey)
      : Buffer.from(await crypto.nativeCrypto.subtle.exportKey("spki", caPublicKey));
    const isCaAndCertPublicKeySame = caPublicKeySpki.equals(Buffer.from(certObj.publicKey.rawData));

    if (!isCaAndCertPublicKeySame) {
      throw new BadRequestError({ message: "CA and certificate public key do not match" });
    }

    await certificateAuthorityCertDAL.transaction(async (tx) => {
      const newCaCert = await certificateAuthorityCertDAL.create(
        {
          caId: ca.id,
          encryptedCertificate,
          encryptedCertificateChain,
          version: caCert ? caCert.version + 1 : 1,
          caSecretId: caSecret.id
        },
        tx
      );

      await certificateAuthorityDAL.updateById(ca.id, {
        status: CaStatus.ACTIVE
      });

      await internalCertificateAuthorityDAL.update(
        {
          caId: ca.id
        },
        {
          maxPathLength: maxPathLength === undefined ? -1 : maxPathLength,
          notBefore: new Date(certObj.notBefore),
          notAfter: new Date(certObj.notAfter),
          serialNumber: certObj.serialNumber,
          parentCaId: parentCa?.id,
          activeCaCertId: newCaCert.id
        },
        tx
      );
    });

    if (!isInternal && ca.internalCa.type === InternalCaType.INTERMEDIATE) {
      const existingConfig = await caSigningConfigDAL.findByCaId(ca.internalCa.id);
      if (!existingConfig) {
        await caSigningConfigDAL.create({
          caId: ca.internalCa.id,
          type: CaSigningConfigType.Manual
        });
      }
    }

    return { ca: expandInternalCa(ca) };
  };

  const generateIntermediateCaCertificate = async ({
    caId,
    parentCaId,
    notBefore,
    notAfter,
    maxPathLength,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId,
    tx
  }: {
    caId: string;
    parentCaId: string;
    notBefore: string;
    notAfter: string;
    maxPathLength?: number;
    actorId: string;
    actorAuthMethod: ActorAuthMethod;
    actor: ActorType;
    actorOrgId: string;
    tx?: Knex.Transaction;
  }) => {
    const intermediateCa = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId, tx);
    if (!intermediateCa.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });

    const parentCa = await certificateAuthorityDAL.findByIdWithAssociatedCa(parentCaId, tx);
    if (!parentCa.internalCa) throw new NotFoundError({ message: `Parent CA with ID '${parentCaId}' not found` });

    if (intermediateCa.projectId !== parentCa.projectId) {
      throw new ForbiddenRequestError({ message: "Intermediate and Parent CA must belong to the same project" });
    }

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

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.IssueCACertificate,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: intermediateCa.name })
    );

    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.SignIntermediate,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: parentCa.name })
    );

    await $validatePqcLicense(intermediateCa.internalCa.keyAlgorithm, intermediateCa.projectId);
    await $validatePqcLicense(parentCa.internalCa.keyAlgorithm, parentCa.projectId);

    const { signer } = await getCaSigner({
      caId: intermediateCa.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService
    });

    const caKeyAlg = intermediateCa.internalCa.keyAlgorithm as CertKeyAlgorithm;

    const csrObj = await signer.createCsr({
      name: intermediateCa.internalCa.dn,
      extensions: [
        new x509.BasicConstraintsExtension(true, maxPathLength === -1 ? undefined : maxPathLength, true),
        new x509.KeyUsagesExtension(isPqcAlgorithm(caKeyAlg) ? PQC_CA_KEY_USAGES : CLASSICAL_CA_KEY_USAGES, true),
        await x509.SubjectKeyIdentifierExtension.create(signer.caPublicKey)
      ]
    });

    const csrPem = csrObj.toString("pem");

    const signedResult = await signIntermediate({
      isInternal: true as const,
      caId: parentCaId,
      csr: csrPem,
      notBefore,
      notAfter,
      maxPathLength: maxPathLength ?? -1
    });

    await importCertToCa({
      isInternal: true as const,
      caId,
      certificate: signedResult.certificate,
      certificateChain: signedResult.certificateChain,
      parentCaId
    });

    const internalCaId = intermediateCa.internalCa.id;
    const existingSigningConfig = await caSigningConfigDAL.findByCaId(internalCaId);
    if (!existingSigningConfig) {
      await caSigningConfigDAL.create({
        caId: internalCaId,
        type: CaSigningConfigType.Internal,
        parentCaId
      });
    } else if (
      existingSigningConfig.type !== CaSigningConfigType.Internal ||
      existingSigningConfig.parentCaId !== parentCaId
    ) {
      await caSigningConfigDAL.transaction(async (signingTx) => {
        await caSigningConfigDAL.deleteById(existingSigningConfig.id, signingTx);
        await caSigningConfigDAL.create(
          {
            caId: internalCaId,
            type: CaSigningConfigType.Internal,
            parentCaId
          },
          signingTx
        );
      });
    }

    const updatedCa = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId, tx);
    if (!updatedCa.internalCa?.activeCaCertId) {
      throw new Error("Failed to get certificate ID after import");
    }

    return {
      certificate: signedResult.certificate,
      certificateChain: signedResult.certificateChain,
      serialNumber: signedResult.serialNumber,
      certId: updatedCa.internalCa.activeCaCertId
    };
  };

  /**
   * Return new leaf certificate issued by CA with id [caId] and private key.
   * Note: private key and CSR are generated within Infisical.
   */
  const issueCertFromCa = async ({
    caId,
    certificateTemplateId,
    pkiCollectionId,
    friendlyName,
    commonName,
    altNames,
    altNameEntries,
    ttl,
    notBefore,
    notAfter,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId,
    keyUsages,
    extendedKeyUsages,
    signatureAlgorithm,
    keyAlgorithm,
    isFromProfile,
    internal = false,
    basicConstraints,
    customExtensions,
    pathLength,
    organization,
    country,
    state,
    locality,
    ou,
    domainComponents,
    tx,
    onPersisted
  }: TIssueCertFromCaDTO): Promise<TIssueCertFromCaResponse> => {
    let ca: TCertificateAuthorityWithAssociatedCa | undefined;
    let certificateTemplate: TCertificateTemplates | undefined;
    let collectionId = pkiCollectionId;

    if (caId) {
      ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    } else if (certificateTemplateId) {
      certificateTemplate = await certificateTemplateDAL.getById(certificateTemplateId);
      if (!certificateTemplate) {
        throw new NotFoundError({
          message: `Certificate template with ID '${certificateTemplateId}' not found`
        });
      }

      if (!collectionId) {
        collectionId = certificateTemplate.pkiCollectionId as string;
      }
      ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(certificateTemplate.caId);
    }

    if (!ca) {
      throw new NotFoundError({ message: `Internal CA with ID '${caId}' not found` });
    }

    if (!ca?.internalCa?.id) {
      throw new NotFoundError({ message: `Internal CA with ID '${caId}' not found` });
    }

    if (!internal) {
      if (!actor || !actorId || !actorOrgId) {
        throw new BadRequestError({ message: "Actor is required" });
      }
      const { permission } = await permissionService.getProjectPermission({
        actor,
        actorId,
        projectId: ca.projectId,
        actorAuthMethod: actorAuthMethod || null,
        actorOrgId,
        actionProjectType: ActionProjectType.CertificateManager
      });

      if (isFromProfile) {
        ForbiddenError.from(permission).throwUnlessCan(
          ProjectPermissionCertificateProfileActions.IssueCert,
          ProjectPermissionSub.CertificateProfiles
        );
      } else {
        ForbiddenError.from(permission).throwUnlessCan(
          ProjectPermissionCertificateActions.Create,
          ProjectPermissionSub.Certificates
        );
      }
    }

    if (ca.status !== CaStatus.ACTIVE) throw new BadRequestError({ message: "CA is not active" });
    if (!ca.internalCa.activeCaCertId)
      throw new BadRequestError({ message: "CA does not have a certificate installed" });
    if (!isFromProfile && !ca.enableDirectIssuance && !certificateTemplate) {
      throw new BadRequestError({ message: "Certificate template or subscriber is required for issuance" });
    }

    const caCert = await certificateAuthorityCertDAL.findById(ca.internalCa.activeCaCertId);

    if (ca.internalCa.notAfter && new Date() > new Date(ca.internalCa.notAfter)) {
      throw new BadRequestError({ message: "CA is expired" });
    }

    // check PKI collection
    if (collectionId) {
      const pkiCollection = await pkiCollectionDAL.findById(collectionId);
      if (!pkiCollection) throw new NotFoundError({ message: "PKI collection not found" });
      if (pkiCollection.projectId !== ca.projectId) throw new BadRequestError({ message: "Invalid PKI collection" });
    }

    const certificateManagerKmsId = await getProjectKmsCertificateKeyId({
      projectId: ca.projectId,
      projectDAL,
      kmsService
    });
    const kmsDecryptor = await kmsService.decryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });

    const decryptedCaCert = await kmsDecryptor({
      cipherTextBlob: caCert.encryptedCertificate
    });

    const caCertObj = new x509.X509Certificate(decryptedCaCert);

    const notBeforeDate = notBefore ? new Date(notBefore) : new Date();

    let notAfterDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
    if (notAfter) {
      notAfterDate = new Date(notAfter);
    } else if (ttl) {
      notAfterDate = new Date(new Date().getTime() + ms(ttl));
    }

    const caCertNotBeforeDate = new Date(caCertObj.notBefore);
    const caCertNotAfterDate = new Date(caCertObj.notAfter);

    // check not before constraint
    if (notBeforeDate < caCertNotBeforeDate) {
      throw new BadRequestError({ message: "notBefore date is before CA certificate's notBefore date" });
    }

    if (notBeforeDate > notAfterDate) throw new BadRequestError({ message: "notBefore date is after notAfter date" });

    // check not after constraint
    if (notAfterDate > caCertNotAfterDate) {
      throw new BadRequestError({ message: "notAfter date is after CA certificate's notAfter date" });
    }

    const effectiveKeyAlgorithm =
      (keyAlgorithm as CertKeyAlgorithm) || (ca.internalCa.keyAlgorithm as CertKeyAlgorithm);

    await $validatePqcLicense(effectiveKeyAlgorithm, ca.projectId);

    const keyGenAlg = keyAlgorithmToAlgCfg(effectiveKeyAlgorithm);
    const leafCryptoEngine = isPqcAlgorithm(effectiveKeyAlgorithm) ? getPqcCrypto() : crypto.nativeCrypto;
    const leafKeys = await leafCryptoEngine.subtle.generateKey(keyGenAlg as RsaHashedKeyGenParams, true, [
      "sign",
      "verify"
    ]);

    if (signatureAlgorithm && ca.internalCa.keyAlgorithm) {
      $checkSignature(ca.internalCa.keyAlgorithm, $getSignatureKeyFamily(signatureAlgorithm), signatureAlgorithm);
    }

    // For PQC CAs, the signing algorithm is always the CA's own key algorithm
    const signingAlg =
      isPqcAlgorithm(ca.internalCa.keyAlgorithm) || !signatureAlgorithm
        ? keyAlgorithmToAlgCfg(ca.internalCa.keyAlgorithm as CertKeyAlgorithm)
        : signatureAlgorithmToAlgCfg(signatureAlgorithm, ca.internalCa.keyAlgorithm as CertKeyAlgorithm);

    const leafDn = createDistinguishedName({
      commonName,
      organization,
      ou,
      country,
      province: state,
      locality,
      domainComponents
    });

    // eslint-disable-next-line no-bitwise
    const csrKeyUsageFlags = isPqcAlgorithm(effectiveKeyAlgorithm)
      ? x509.KeyUsageFlags.digitalSignature
      : x509.KeyUsageFlags.digitalSignature | x509.KeyUsageFlags.keyEncipherment;

    const csrObj = await x509.Pkcs10CertificateRequestGenerator.create({
      name: leafDn,
      keys: leafKeys,
      signingAlgorithm: keyGenAlg,
      extensions: [new x509.KeyUsagesExtension(csrKeyUsageFlags)],
      attributes: [new x509.ChallengePasswordAttribute("password")]
    });

    const { caSecret, signer } = await getCaSigner({
      caId: ca.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService,
      signatureAlgorithm: signingAlg
    });

    const caCrl = await certificateAuthorityCrlDAL.findOne({ caSecretId: caSecret.id });
    const appCfg = getConfig();

    const managedCdpUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/crl/${caCrl.id}/der`;
    const caIssuerUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/ca/internal/${ca.id}/certificates/${caCert.id}/der`;
    const cdpUrls = buildCrlDistributionPointUrls(
      managedCdpUrl,
      ca.internalCa.crlDistributionPointUrls,
      ca.internalCa.disableManagedCrlDistributionPointUrl || !(await $isManagedCrlDistributionAllowed(ca.projectId))
    );

    const ocspResponderUrl =
      ca.internalCa.isOcspEnabled && appCfg.SITE_URL ? buildOcspResponderUrl(appCfg.SITE_URL, ca.id) : null;

    const basicConstraintsExtension = $createBasicConstraintsExtension({
      basicConstraints,
      pathLength,
      caCertObj
    });

    const extensions: x509.Extension[] = [
      basicConstraintsExtension,
      ...(cdpUrls.length > 0 ? [new x509.CRLDistributionPointsExtension(cdpUrls)] : []),
      await x509.AuthorityKeyIdentifierExtension.create(caCertObj, false),
      await x509.SubjectKeyIdentifierExtension.create(csrObj.publicKey),
      buildAuthorityInfoAccessExtension({ caIssuerUrl, ocspResponderUrl }),
      new x509.CertificatePolicyExtension(["2.5.29.32.0"]) // anyPolicy
    ];

    // handle key usages
    let selectedKeyUsages: CertKeyUsage[] = keyUsages ?? [];
    if (keyUsages === undefined && !certificateTemplate) {
      if (isFromProfile) {
        selectedKeyUsages = [];
      } else if (isPqcAlgorithm(effectiveKeyAlgorithm)) {
        selectedKeyUsages = [CertKeyUsage.DIGITAL_SIGNATURE];
      } else {
        selectedKeyUsages = [CertKeyUsage.DIGITAL_SIGNATURE, CertKeyUsage.KEY_ENCIPHERMENT];
      }
    }

    if (keyUsages === undefined && certificateTemplate) {
      selectedKeyUsages = (certificateTemplate.keyUsages ?? []) as CertKeyUsage[];
    }

    if (keyUsages?.length && certificateTemplate) {
      const validKeyUsages = certificateTemplate.keyUsages || [];
      if (keyUsages.some((keyUsage) => !validKeyUsages.includes(keyUsage))) {
        throw new BadRequestError({
          message: "Invalid key usage value based on template policy"
        });
      }
      selectedKeyUsages = keyUsages;
    }

    if (isPqcAlgorithm(effectiveKeyAlgorithm)) {
      const invalidForPqc = [CertKeyUsage.KEY_ENCIPHERMENT, CertKeyUsage.KEY_AGREEMENT, CertKeyUsage.DATA_ENCIPHERMENT];
      const requested = selectedKeyUsages.filter((ku) => invalidForPqc.includes(ku));
      if (requested.length > 0) {
        throw new BadRequestError({
          message: `Key usages ${requested.join(", ")} are not valid for PQC signature-only algorithms`
        });
      }
    }

    const keyUsagesBitValue = selectedKeyUsages.reduce((accum, keyUsage) => accum | x509.KeyUsageFlags[keyUsage], 0);
    if (keyUsagesBitValue) {
      extensions.push(new x509.KeyUsagesExtension(keyUsagesBitValue, true));
    }

    // handle extended key usages
    let selectedExtendedKeyUsages: CertExtendedKeyUsage[] = extendedKeyUsages ?? [];
    if (extendedKeyUsages === undefined && certificateTemplate) {
      selectedExtendedKeyUsages = (certificateTemplate.extendedKeyUsages ?? []) as CertExtendedKeyUsage[];
    }

    if (extendedKeyUsages?.length && certificateTemplate) {
      const validExtendedKeyUsages = certificateTemplate.extendedKeyUsages || [];
      if (extendedKeyUsages.some((eku) => !validExtendedKeyUsages.includes(eku))) {
        throw new BadRequestError({
          message: "Invalid extended key usage value based on template policy"
        });
      }
      selectedExtendedKeyUsages = extendedKeyUsages;
    }

    if (selectedExtendedKeyUsages.length) {
      extensions.push(
        new x509.ExtendedKeyUsageExtension(
          selectedExtendedKeyUsages.map((eku) => CertExtendedKeyUsageNameToOID[eku]),
          true
        )
      );
    }

    let altNamesArray: TAltNameMapping[] = [];

    if (altNameEntries?.length) {
      altNamesArray = altNameEntries.map(({ type, value }) => ({
        type: CERT_SUBJECT_ALTERNATIVE_NAMES[type].generalNameType,
        value
      }));
    } else if (altNames) {
      altNamesArray = altNames
        .split(",")
        .map((name) => name.trim())
        .map((altName): TAltNameMapping => {
          const altNameType = validateAndMapAltNameType(altName);
          if (!altNameType) {
            throw new Error(`Invalid altName: ${altName}`);
          }
          return altNameType;
        });
    }

    if (altNamesArray.length) {
      // RFC 5280 4.1.2.6: subjectAltName must be marked critical when the subject is an empty sequence
      const altNamesExtension = new x509.SubjectAlternativeNameExtension(altNamesArray, leafDn.trim().length === 0);
      extensions.push(altNamesExtension);
    }

    appendCustomExtensions(extensions, customExtensions);

    if (certificateTemplate) {
      validateCertificateDetailsAgainstTemplate(
        {
          commonName,
          notBeforeDate,
          notAfterDate,
          altNames: altNamesArray.map((entry) => entry.value)
        },
        certificateTemplate
      );
    }

    const serialNumber = createSerialNumber();
    const leafCert = await signer.createCertificate({
      serialNumber,
      subject: csrObj.subject,
      issuer: caCertObj.subject,
      notBefore: notBeforeDate,
      notAfter: notAfterDate,
      publicKey: csrObj.publicKey,
      extensions
    });

    let skLeaf: string;
    if (isPqcCryptoKey(leafKeys.privateKey)) {
      skLeaf = await exportPqcKeyToPem(leafKeys.privateKey);
    } else {
      const skLeafObj = crypto.nativeCrypto.KeyObject.from(leafKeys.privateKey);
      skLeaf = skLeafObj.export({ format: "pem", type: "pkcs8" }) as string;
    }

    const kmsEncryptor = await kmsService.encryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });
    const { cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
      plainText: Buffer.from(new Uint8Array(leafCert.rawData))
    });
    const { cipherTextBlob: encryptedPrivateKey } = await kmsEncryptor({
      plainText: Buffer.from(skLeaf)
    });

    const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({
      caCertId: caCert.id,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    const certificateChainPem = `${issuingCaCertificate}\n${caCertChain}`.trim();

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

    const executeIssueCertOperations = async (transaction: Knex) => {
      // Extract certificate fields for storage
      const certificatePem = leafCert.toString("pem");
      const parsedFields = extractCertificateFields(Buffer.from(certificatePem), customExtensions);

      const cert = await certificateDAL.create(
        {
          caId: (ca as TCertificateAuthorities).id,
          caCertId: caCert.id,
          certificateTemplateId: certificateTemplate?.id,
          status: CertStatus.ACTIVE,
          friendlyName: friendlyName || commonName,
          commonName,
          altNames,
          serialNumber,
          notBefore: notBeforeDate,
          notAfter: notAfterDate,
          keyUsages: selectedKeyUsages,
          extendedKeyUsages: selectedExtendedKeyUsages,
          projectId: ca!.projectId,
          keyAlgorithm: effectiveKeyAlgorithm,
          signatureAlgorithm: signatureAlgorithm || ca!.internalCa!.keyAlgorithm,
          ...parsedFields
        },
        transaction
      );

      await certificateBodyDAL.create(
        {
          certId: cert.id,
          encryptedCertificate,
          encryptedCertificateChain
        },
        transaction
      );

      await certificateSecretDAL.create(
        {
          certId: cert.id,
          encryptedPrivateKey
        },
        transaction
      );

      if (collectionId) {
        await pkiCollectionItemDAL.create(
          {
            pkiCollectionId: collectionId,
            certId: cert.id
          },
          transaction
        );
      }

      if (onPersisted) {
        await onPersisted(cert, transaction);
      }

      return cert;
    };

    let cert;
    if (tx) {
      cert = await executeIssueCertOperations(tx);
    } else {
      cert = await certificateDAL.transaction(executeIssueCertOperations);
    }

    usageMeteringService.emitForProject(ca.projectId, ActiveCerts.key);
    usageMeteringService.emitForProject(ca.projectId, WildcardCerts.key);

    return {
      certificate: leafCert.toString("pem"),
      certificateChain: certificateChainPem,
      issuingCaCertificate,
      privateKey: skLeaf,
      serialNumber,
      certificateId: cert.id,
      commonName,
      ca: expandInternalCa(ca)
    };
  };

  /**
   * Return new leaf certificate issued by CA with id [caId].
   * Note: CSR is generated externally and submitted to Infisical.
   */
  const signCertFromCa = async (dto: TSignCertFromCaDTO) => {
    const appCfg = getConfig();
    let ca: TCertificateAuthorityWithAssociatedCa | undefined;
    let certificateTemplate: TCertificateTemplates | undefined;

    const {
      caId,
      certificateTemplateId,
      csr,
      pkiCollectionId,
      friendlyName,
      commonName,
      altNames,
      ttl,
      notBefore,
      notAfter,
      keyUsages,
      extendedKeyUsages,
      signatureAlgorithm,
      keyAlgorithm,
      basicConstraints,
      pathLength,
      subjectOverride,
      customExtensions,
      tx
    } = dto;

    let collectionId = pkiCollectionId;

    if (caId) {
      ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId, tx);
    } else if (certificateTemplateId) {
      certificateTemplate = await certificateTemplateDAL.getById(certificateTemplateId, tx);
      if (!certificateTemplate) {
        throw new NotFoundError({
          message: `Certificate template with ID '${certificateTemplateId}' not found`
        });
      }

      collectionId = certificateTemplate.pkiCollectionId as string;
      ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(certificateTemplate.caId, tx);
    }

    if (!ca) {
      throw new NotFoundError({ message: `Internal CA with ID '${caId}' not found` });
    }

    if (!ca?.internalCa?.id) {
      throw new NotFoundError({ message: `Internal CA with ID '${caId}' not found` });
    }

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

      if (dto.isFromProfile && dto.profileId) {
        ForbiddenError.from(permission).throwUnlessCan(
          ProjectPermissionCertificateProfileActions.IssueCert,
          ProjectPermissionSub.CertificateProfiles
        );
      } else {
        ForbiddenError.from(permission).throwUnlessCan(
          ProjectPermissionCertificateActions.Create,
          ProjectPermissionSub.Certificates
        );
      }
    }

    if (ca.status !== CaStatus.ACTIVE) throw new BadRequestError({ message: "CA is not active" });
    if (!ca.internalCa.activeCaCertId)
      throw new BadRequestError({ message: "CA does not have a certificate installed" });
    if (!dto.isFromProfile && !ca.enableDirectIssuance && !certificateTemplate) {
      throw new BadRequestError({ message: "Certificate template or subscriber is required for issuance" });
    }

    const caCert = await certificateAuthorityCertDAL.findById(ca.internalCa.activeCaCertId);

    if (ca.internalCa.notAfter && new Date() > new Date(ca.internalCa.notAfter)) {
      throw new BadRequestError({ message: "CA is expired" });
    }

    // check PKI collection
    if (pkiCollectionId) {
      const pkiCollection = await pkiCollectionDAL.findById(pkiCollectionId, tx);
      if (!pkiCollection) throw new NotFoundError({ message: `PKI collection with ID '${pkiCollectionId}' not found` });
      if (pkiCollection.projectId !== ca.projectId) throw new BadRequestError({ message: "Invalid PKI collection" });
    }

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

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

    const decryptedCaCert = await kmsDecryptor({
      cipherTextBlob: caCert.encryptedCertificate
    });

    const caCertObj = new x509.X509Certificate(decryptedCaCert);

    const notBeforeDate = notBefore ? new Date(notBefore) : new Date();

    let notAfterDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
    if (notAfter) {
      notAfterDate = new Date(notAfter);
    } else if (ttl) {
      notAfterDate = new Date(new Date().getTime() + ms(ttl));
    } else if (certificateTemplate?.ttl) {
      notAfterDate = new Date(new Date().getTime() + ms(certificateTemplate.ttl));
    }

    const caCertNotBeforeDate = new Date(caCertObj.notBefore);
    const caCertNotAfterDate = new Date(caCertObj.notAfter);

    // check not before constraint
    if (notBeforeDate < caCertNotBeforeDate) {
      throw new BadRequestError({ message: "notBefore date is before CA certificate's notBefore date" });
    }

    if (notBeforeDate > notAfterDate) throw new BadRequestError({ message: "notBefore date is after notAfter date" });

    // check not after constraint
    if (notAfterDate > caCertNotAfterDate) {
      throw new BadRequestError({ message: "notAfter date is after CA certificate's notAfter date" });
    }

    await $validatePqcLicense(ca.internalCa.keyAlgorithm, ca.projectId);

    if (signatureAlgorithm && ca.internalCa.keyAlgorithm) {
      $checkSignature(ca.internalCa.keyAlgorithm, $getSignatureKeyFamily(signatureAlgorithm), signatureAlgorithm);
    }

    const caKeyAlgorithm = ca.internalCa.keyAlgorithm as CertKeyAlgorithm;

    const alg =
      isPqcAlgorithm(ca.internalCa.keyAlgorithm) || !signatureAlgorithm
        ? keyAlgorithmToAlgCfg(caKeyAlgorithm)
        : signatureAlgorithmToAlgCfg(signatureAlgorithm, caKeyAlgorithm);

    const csrObj = new x509.Pkcs10CertificateRequest(csr);

    const dn = extractDnParts(csrObj.subjectName);
    const cn = (commonName || dn.commonName) ?? "";

    const { caSecret, signer } = await getCaSigner({
      caId: ca.id,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService,
      signatureAlgorithm: alg
    });

    const caCrl = await certificateAuthorityCrlDAL.findOne({ caSecretId: caSecret.id });
    const managedCdpUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/crl/${caCrl.id}/der`;
    const caIssuerUrl = `${appCfg.SITE_URL}/api/v1/cert-manager/ca/internal/${ca.id}/certificates/${caCert.id}/der`;
    const cdpUrls = buildCrlDistributionPointUrls(
      managedCdpUrl,
      ca.internalCa.crlDistributionPointUrls,
      ca.internalCa.disableManagedCrlDistributionPointUrl || !(await $isManagedCrlDistributionAllowed(ca.projectId))
    );

    const ocspResponderUrl =
      ca.internalCa.isOcspEnabled && appCfg.SITE_URL ? buildOcspResponderUrl(appCfg.SITE_URL, ca.id) : null;

    const basicConstraintsExtension = $createBasicConstraintsExtension({
      basicConstraints,
      pathLength,
      caCertObj
    });

    const extensions: x509.Extension[] = [
      basicConstraintsExtension,
      await x509.AuthorityKeyIdentifierExtension.create(caCertObj, false),
      await x509.SubjectKeyIdentifierExtension.create(csrObj.publicKey),
      ...(cdpUrls.length > 0 ? [new x509.CRLDistributionPointsExtension(cdpUrls)] : []),
      buildAuthorityInfoAccessExtension({ caIssuerUrl, ocspResponderUrl }),
      new x509.CertificatePolicyExtension(["2.5.29.32.0"]) // anyPolicy
    ];

    // handle key usages
    const csrKeyUsageExtension = csrObj.getExtension("2.5.29.15") as x509.KeyUsagesExtension;
    let csrKeyUsages: CertKeyUsage[] = [];
    if (csrKeyUsageExtension) {
      csrKeyUsages = Object.values(CertKeyUsage).filter(
        (keyUsage) => (x509.KeyUsageFlags[keyUsage] & csrKeyUsageExtension.usages) !== 0
      );
    }

    let selectedKeyUsages: CertKeyUsage[] = keyUsages ?? [];
    if (keyUsages === undefined && !certificateTemplate) {
      if (csrKeyUsageExtension) {
        selectedKeyUsages = csrKeyUsages;
      } else if (dto.isFromProfile) {
        selectedKeyUsages = [];
      } else if (isPqcAlgorithm(csrObj.publicKey.algorithm.name)) {
        selectedKeyUsages = [CertKeyUsage.DIGITAL_SIGNATURE];
      } else {
        selectedKeyUsages = [CertKeyUsage.DIGITAL_SIGNATURE, CertKeyUsage.KEY_ENCIPHERMENT];
      }
    }

    if (keyUsages === undefined && certificateTemplate) {
      if (csrKeyUsageExtension) {
        const validKeyUsages = certificateTemplate.keyUsages || [];
        if (csrKeyUsages.some((keyUsage) => !validKeyUsages.includes(keyUsage))) {
          throw new BadRequestError({
            message: "Invalid key usage value based on template policy"
          });
        }
        selectedKeyUsages = csrKeyUsages;
      } else {
        selectedKeyUsages = (certificateTemplate.keyUsages ?? []) as CertKeyUsage[];
      }
    }

    if (keyUsages?.length && certificateTemplate) {
      const validKeyUsages = certificateTemplate.keyUsages || [];
      if (keyUsages.some((keyUsage) => !validKeyUsages.includes(keyUsage))) {
        throw new BadRequestError({
          message: "Invalid key usage value based on template policy"
        });
      }
      selectedKeyUsages = keyUsages;
    }

    // Use the CSR's actual public key algorithm (not effectiveKeyAlgorithm which defaults to the CA's algo)
    if (isPqcAlgorithm(csrObj.publicKey.algorithm.name)) {
      const invalidForPqc = [CertKeyUsage.KEY_ENCIPHERMENT, CertKeyUsage.KEY_AGREEMENT, CertKeyUsage.DATA_ENCIPHERMENT];
      const requested = selectedKeyUsages.filter((ku) => invalidForPqc.includes(ku));
      if (requested.length > 0) {
        throw new BadRequestError({
          message: `Key usages ${requested.join(", ")} are not valid for PQC signature-only algorithms`
        });
      }
    }

    const keyUsagesBitValue = selectedKeyUsages.reduce((accum, keyUsage) => accum | x509.KeyUsageFlags[keyUsage], 0);
    if (keyUsagesBitValue) {
      extensions.push(new x509.KeyUsagesExtension(keyUsagesBitValue, true));
    }

    // handle extended key usages
    const csrExtendedKeyUsageExtension = csrObj.getExtension("2.5.29.37") as x509.ExtendedKeyUsageExtension;
    let csrExtendedKeyUsages: CertExtendedKeyUsage[] = [];
    if (csrExtendedKeyUsageExtension) {
      csrExtendedKeyUsages = csrExtendedKeyUsageExtension.usages.map(
        (ekuOid) => CertExtendedKeyUsageOIDToName[ekuOid as string]
      );
    }

    let selectedExtendedKeyUsages: CertExtendedKeyUsage[] = extendedKeyUsages ?? [];
    if (extendedKeyUsages === undefined && !certificateTemplate && csrExtendedKeyUsageExtension) {
      selectedExtendedKeyUsages = csrExtendedKeyUsages;
    }

    if (extendedKeyUsages === undefined && certificateTemplate) {
      if (csrExtendedKeyUsageExtension) {
        const validExtendedKeyUsages = certificateTemplate.extendedKeyUsages || [];
        if (csrExtendedKeyUsages.some((eku) => !validExtendedKeyUsages.includes(eku))) {
          throw new BadRequestError({
            message: "Invalid extended key usage value based on template policy"
          });
        }
        selectedExtendedKeyUsages = csrExtendedKeyUsages;
      } else {
        selectedExtendedKeyUsages = (certificateTemplate.extendedKeyUsages ?? []) as CertExtendedKeyUsage[];
      }
    }

    if (extendedKeyUsages?.length && certificateTemplate) {
      const validExtendedKeyUsages = certificateTemplate.extendedKeyUsages || [];
      if (extendedKeyUsages.some((keyUsage) => !validExtendedKeyUsages.includes(keyUsage))) {
        throw new BadRequestError({
          message: "Invalid extended key usage value based on template policy"
        });
      }
      selectedExtendedKeyUsages = extendedKeyUsages;
    }

    if (selectedExtendedKeyUsages.length) {
      extensions.push(
        new x509.ExtendedKeyUsageExtension(
          selectedExtendedKeyUsages.map((eku) => CertExtendedKeyUsageNameToOID[eku]),
          true
        )
      );
    }

    let altNamesFromCsr: string = "";
    let altNamesArray: TAltNameMapping[] = [];

    if (altNames) {
      altNamesArray = altNames
        .split(",")
        .map((name) => name.trim())
        .map((altName): TAltNameMapping => {
          const altNameType = validateAndMapAltNameType(altName);
          if (!altNameType) {
            throw new Error(`Invalid altName: ${altName}`);
          }
          return altNameType;
        });
    } else {
      // attempt to read from CSR if altNames is not explicitly provided
      const sanExtension = csrObj.extensions.find((ext) => ext.type === "2.5.29.17");
      if (sanExtension) {
        const sanNames = new x509.GeneralNames(sanExtension.value);

        altNamesArray = sanNames.items
          .filter((value) => SUPPORTED_GENERAL_NAME_TYPES.has(value.type))
          .map((name): TAltNameMapping => {
            if (GENERAL_NAME_TYPES_WITH_OTHER_NAME.has(name.type)) {
              return { type: name.type as TAltNameType, value: name.value };
            }

            const altNameType = validateAndMapAltNameType(name.value);
            if (!altNameType) {
              throw new Error(`Invalid altName from CSR: ${name.value}`);
            }
            return altNameType;
          });

        altNamesFromCsr = sanNames.items.map((item) => item.value).join(",");
      }
    }

    const finalSubject = subjectOverride ?? csrObj.subject;

    if (finalSubject.trim().length === 0) {
      if (basicConstraintsExtension.ca) {
        throw new BadRequestError({
          message:
            "A CA certificate must have a subject. Add a subject attribute to the CSR (common name, organization, organizational unit, country, state, locality or domain component)."
        });
      }
      if (altNamesArray.length === 0) {
        throw new BadRequestError({
          message:
            "Certificate must have a subject or at least one subject alternative name. Add a subject attribute to the CSR (common name, organization, organizational unit, country, state, locality or domain component), or request a subject alternative name."
        });
      }
    }

    if (altNamesArray.length) {
      // RFC 5280 4.1.2.6: subjectAltName must be marked critical when the subject is an empty sequence.
      const altNamesExtension = new x509.SubjectAlternativeNameExtension(
        altNamesArray,
        finalSubject.trim().length === 0
      );
      extensions.push(altNamesExtension);
    }

    appendCustomExtensions(extensions, customExtensions);

    if (certificateTemplate) {
      validateCertificateDetailsAgainstTemplate(
        {
          commonName: cn,
          notBeforeDate,
          notAfterDate,
          altNames: altNamesArray.map((entry) => entry.value)
        },
        certificateTemplate
      );
    }

    const serialNumber = createSerialNumber();
    const leafCert = await signer.createCertificate({
      serialNumber,
      subject: finalSubject,
      issuer: caCertObj.subject,
      notBefore: notBeforeDate,
      notAfter: notAfterDate,
      publicKey: csrObj.publicKey,
      extensions
    });

    const kmsEncryptor = await kmsService.encryptWithKmsKey({
      kmsId: certificateManagerKmsId
    });
    const { cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
      plainText: Buffer.from(new Uint8Array(leafCert.rawData))
    });

    const { caCert: issuingCaCertificate, caCertChain } = await getCaCertChain({
      caCertId: ca.internalCa.activeCaCertId,
      certificateAuthorityDAL,
      certificateAuthorityCertDAL,
      projectDAL,
      kmsService
    });

    const certificateChainPem = `${issuingCaCertificate}\n${caCertChain}`.trim();

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

    const createSignedCert = async (transaction: Knex) => {
      // Extract certificate fields for storage
      const certificatePem = leafCert.toString("pem");
      const parsedFields = extractCertificateFields(Buffer.from(certificatePem), customExtensions);

      const newCert = await certificateDAL.create(
        {
          caId: (ca as TCertificateAuthorities).id,
          caCertId: caCert.id,
          certificateTemplateId: certificateTemplate?.id,
          status: CertStatus.ACTIVE,
          friendlyName: friendlyName || csrObj.subject,
          commonName: cn,
          altNames: altNamesFromCsr || altNames,
          serialNumber,
          notBefore: notBeforeDate,
          notAfter: notAfterDate,
          keyUsages: selectedKeyUsages,
          extendedKeyUsages: selectedExtendedKeyUsages,
          projectId: ca!.projectId,
          keyAlgorithm: keyAlgorithm || ca!.internalCa!.keyAlgorithm,
          signatureAlgorithm: signatureAlgorithm || ca!.internalCa!.keyAlgorithm,
          ...parsedFields
        },
        transaction
      );

      await certificateBodyDAL.create(
        {
          certId: newCert.id,
          encryptedCertificate,
          encryptedCertificateChain
        },
        transaction
      );

      if (collectionId) {
        await pkiCollectionItemDAL.create(
          {
            pkiCollectionId: collectionId,
            certId: newCert.id
          },
          transaction
        );
      }

      return newCert;
    };

    // Everything above this point is reads, CA key access and signing, and deliberately runs with no
    // transaction open. Only the writes below take a connection, so a caller can get atomic issuance
    // without pinning one across the KMS/HSM round trips.
    const persist = async (transaction: Knex) => {
      const newCert = await createSignedCert(transaction);
      if (dto.onPersisted) {
        await dto.onPersisted(newCert, transaction);
      }
      return newCert;
    };

    let cert;
    if (tx) {
      cert = await persist(tx);
    } else {
      cert = await certificateDAL.transaction(persist);
    }

    usageMeteringService.emitForProject(ca.projectId, ActiveCerts.key);
    usageMeteringService.emitForProject(ca.projectId, WildcardCerts.key);

    return {
      certificate: leafCert,
      certificateChain: certificateChainPem,
      issuingCaCertificate,
      serialNumber,
      certificateId: cert.id,
      ca: expandInternalCa(ca),
      commonName: cn
    };
  };

  /**
   * Return list of certificate templates for CA with id [caId].
   */
  const getCaCertificateTemplates = async ({
    caId,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId
  }: TGetCaCertificateTemplatesDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca?.internalCa?.id) throw new NotFoundError({ message: `Internal CA with ID '${caId}' not found` });

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

    const certificateTemplates = await certificateTemplateDAL.find({ caId });

    return {
      certificateTemplates: certificateTemplates.filter((el) =>
        permission.can(
          ProjectPermissionPkiTemplateActions.Read,
          subject(ProjectPermissionSub.CertificateTemplates, { name: el.name })
        )
      ),
      ca: expandInternalCa(ca)
    };
  };

  const generateRootCaCertificate = async ({
    caId,
    notBefore,
    notAfter,
    maxPathLength,
    actorId,
    actorAuthMethod,
    actor,
    actorOrgId
  }: TGenerateRootCaCertificateDTO) => {
    const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
    if (!ca?.internalCa) throw new NotFoundError({ message: `CA with ID '${caId}' not found` });
    if (ca.internalCa.type !== InternalCaType.ROOT) {
      throw new BadRequestError({ message: "Certificate generation is only available for root CAs" });
    }

    const { permission } = await permissionService.getProjectPermission({
      actor,
      actorId,
      projectId: ca.projectId,
      actorAuthMethod,
      actorOrgId,
      actionProjectType: ActionProjectType.CertificateManager
    });
    ForbiddenError.from(permission).throwUnlessCan(
      ProjectPermissionCertificateAuthorityActions.IssueCACertificate,
      subject(ProjectPermissionSub.CertificateAuthorities, { name: ca.name })
    );

    await $validatePqcLicense(ca.internalCa.keyAlgorithm, ca.projectId);

    const notBeforeDate = new Date(notBefore);
    const notAfterDate = new Date(notAfter);
    const serialNumber = createSerialNumber();

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

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

    if (!ca.internalCa) throw new BadRequestError({ message: "CA internal configuration not found" });

    const { caSecret, signer } = await getCaSigner({
      caId,
      certificateAuthorityDAL,
      certificateAuthoritySecretDAL,
      projectDAL,
      kmsService,
      caKeySourceService
    });

    const rootKeyAlg = ca.internalCa.keyAlgorithm as CertKeyAlgorithm;

    const cert = await signer.createCertificate({
      subject: ca.internalCa.dn,
      issuer: ca.internalCa.dn,
      serialNumber,
      notBefore: notBeforeDate,
      notAfter: notAfterDate,
      publicKey: signer.caPublicKey,
      extensions: [
        new x509.BasicConstraintsExtension(
          true,
          maxPathLength === -1 || maxPathLength === null || maxPathLength === undefined
            ? undefined
            : Number(maxPathLength),
          true
        ),
        new x509.KeyUsagesExtension(isPqcAlgorithm(rootKeyAlg) ? PQC_ROOT_CA_KEY_USAGES : ROOT_CA_KEY_USAGES, true),
        await x509.SubjectKeyIdentifierExtension.create(signer.caPublicKey)
      ]
    });

    const { cipherTextBlob: encryptedCertificate } = await kmsEncryptor({
      plainText: Buffer.from(new Uint8Array(cert.rawData))
    });

    const { cipherTextBlob: encryptedCertificateChain } = await kmsEncryptor({
      plainText: Buffer.alloc(0)
    });

    return certificateAuthorityDAL.transaction(async (tx) => {
      await internalCertificateAuthorityDAL.updateById(
        ca.internalCa!.id,
        {
          notBefore: notBeforeDate,
          notAfter: notAfterDate,
          serialNumber,
          ...(maxPathLength !== undefined && { maxPathLength })
        },
        tx
      );

      const existingCaCert = ca.internalCa!.activeCaCertId
        ? await certificateAuthorityCertDAL.findById(ca.internalCa!.activeCaCertId, tx)
        : undefined;

      const caCert = await certificateAuthorityCertDAL.create(
        {
          caId: ca.id,
          encryptedCertificate,
          encryptedCertificateChain,
          version: existingCaCert ? existingCaCert.version + 1 : 1,
          caSecretId: caSecret.id
        },
        tx
      );

      await internalCertificateAuthorityDAL.updateById(
        ca.internalCa!.id,
        {
          activeCaCertId: caCert.id
        },
        tx
      );

      await certificateAuthorityDAL.updateById(
        ca.id,
        {
          status: CaStatus.ACTIVE
        },
        tx
      );

      const updatedCa = await certificateAuthorityDAL.findByIdWithAssociatedCa(ca.id, tx);

      return {
        certificate: cert.toString("pem"),
        certificateChain: "",
        serialNumber,
        certId: caCert.id,
        ca: expandInternalCa(updatedCa)
      };
    });
  };

  return {
    createCa,
    getCaById,
    updateCaById,
    deleteCaById,
    getCaCsr,
    renewCaCert,
    getCaCerts,
    getCaCertsPublic,
    getCaCert,
    getCaCertPublic,
    getCaCertById,
    getCaCertByIdWithAuth,
    getCaCertByIdPublic,
    signIntermediate,
    importCertToCa,
    generateIntermediateCaCertificate,
    issueCertFromCa,
    signCertFromCa,
    getCaCertificateTemplates,
    generateRootCaCertificate
  };
};