Add self-sign certs support

This commit is contained in:
Carlos Monastyrski
2025-11-22 00:33:30 -03:00
parent 1233ff8a5f
commit 933830e17e
20 changed files with 1085 additions and 135 deletions
@@ -0,0 +1,31 @@
import { Knex } from "knex";
import { TableName } from "../schemas";
export async function up(knex: Knex): Promise<void> {
const hasIssuerTypeColumn = await knex.schema.hasColumn(TableName.PkiCertificateProfile, "issuerType");
if (!hasIssuerTypeColumn) {
await knex.schema.alterTable(TableName.PkiCertificateProfile, (t) => {
t.string("issuerType").notNullable().defaultTo("ca");
});
}
await knex.schema.alterTable(TableName.PkiCertificateProfile, (t) => {
t.uuid("caId").nullable().alter();
});
}
export async function down(knex: Knex): Promise<void> {
const hasIssuerTypeColumn = await knex.schema.hasColumn(TableName.PkiCertificateProfile, "issuerType");
if (hasIssuerTypeColumn) {
await knex.schema.alterTable(TableName.PkiCertificateProfile, (t) => {
t.dropColumn("issuerType");
});
}
await knex.schema.alterTable(TableName.PkiCertificateProfile, (t) => {
t.uuid("caId").notNullable().alter();
});
}
@@ -10,7 +10,7 @@ import { TImmutableDBKeys } from "./models";
export const PkiCertificateProfilesSchema = z.object({ export const PkiCertificateProfilesSchema = z.object({
id: z.string().uuid(), id: z.string().uuid(),
projectId: z.string(), projectId: z.string(),
caId: z.string().uuid(), caId: z.string().uuid().nullable().optional(),
certificateTemplateId: z.string().uuid(), certificateTemplateId: z.string().uuid(),
slug: z.string(), slug: z.string(),
description: z.string().nullable().optional(), description: z.string().nullable().optional(),
@@ -19,7 +19,8 @@ export const PkiCertificateProfilesSchema = z.object({
apiConfigId: z.string().uuid().nullable().optional(), apiConfigId: z.string().uuid().nullable().optional(),
createdAt: z.date(), createdAt: z.date(),
updatedAt: z.date(), updatedAt: z.date(),
acmeConfigId: z.string().uuid().nullable().optional() acmeConfigId: z.string().uuid().nullable().optional(),
issuerType: z.string().default("ca")
}); });
export type TPkiCertificateProfiles = z.infer<typeof PkiCertificateProfilesSchema>; export type TPkiCertificateProfiles = z.infer<typeof PkiCertificateProfilesSchema>;
@@ -2787,6 +2787,7 @@ interface CreateCertificateProfile {
name: string; name: string;
projectId: string; projectId: string;
enrollmentType: string; enrollmentType: string;
issuerType: string;
}; };
} }
@@ -683,6 +683,13 @@ export const pkiAcmeServiceFactory = ({
payload: TFinalizeAcmeOrderPayload; payload: TFinalizeAcmeOrderPayload;
}): Promise<TAcmeResponse<TAcmeOrderResource>> => { }): Promise<TAcmeResponse<TAcmeOrderResource>> => {
const profile = (await certificateProfileDAL.findByIdWithConfigs(profileId))!; const profile = (await certificateProfileDAL.findByIdWithConfigs(profileId))!;
if (!profile.caId) {
throw new BadRequestError({
message: "Self-signed certificates are not supported for ACME enrollment"
});
}
let order = await acmeOrderDAL.findByAccountAndOrderIdWithAuthorizations(accountId, orderId); let order = await acmeOrderDAL.findByAccountAndOrderIdWithAuthorizations(accountId, orderId);
if (!order) { if (!order) {
throw new NotFoundError({ message: "ACME order not found" }); throw new NotFoundError({ message: "ACME order not found" });
@@ -732,6 +739,10 @@ export const pkiAcmeServiceFactory = ({
throw new AcmeBadCSRError({ message: "Invalid CSR: Common name + SANs mismatch with order identifiers" }); throw new AcmeBadCSRError({ message: "Invalid CSR: Common name + SANs mismatch with order identifiers" });
} }
if (!profile.caId) {
throw new NotFoundError({ message: "Self-signed certificates are not supported for ACME enrollment" });
}
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId); const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId);
if (!ca) { if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" }); throw new NotFoundError({ message: "Certificate Authority not found" });
+4 -1
View File
@@ -2219,7 +2219,10 @@ export const registerRoutes = async (
permissionService, permissionService,
certificateSyncDAL, certificateSyncDAL,
pkiSyncDAL, pkiSyncDAL,
pkiSyncQueue pkiSyncQueue,
kmsService,
projectDAL,
certificateBodyDAL
}); });
const certificateV3Queue = certificateV3QueueServiceFactory({ const certificateV3Queue = certificateV3QueueServiceFactory({
@@ -8,7 +8,7 @@ import { readLimit, writeLimit } from "@app/server/config/rateLimiter";
import { verifyAuth } from "@app/server/plugins/auth/verify-auth"; import { verifyAuth } from "@app/server/plugins/auth/verify-auth";
import { AuthMode } from "@app/services/auth/auth-type"; import { AuthMode } from "@app/services/auth/auth-type";
import { CertStatus } from "@app/services/certificate/certificate-types"; import { CertStatus } from "@app/services/certificate/certificate-types";
import { EnrollmentType } from "@app/services/certificate-profile/certificate-profile-types"; import { EnrollmentType, IssuerType } from "@app/services/certificate-profile/certificate-profile-types";
export const registerCertificateProfilesRouter = async (server: FastifyZodProvider) => { export const registerCertificateProfilesRouter = async (server: FastifyZodProvider) => {
server.route({ server.route({
@@ -23,7 +23,7 @@ export const registerCertificateProfilesRouter = async (server: FastifyZodProvid
body: z body: z
.object({ .object({
projectId: z.string().min(1), projectId: z.string().min(1),
caId: z.string().uuid(), caId: z.string().uuid().optional(),
certificateTemplateId: z.string().uuid(), certificateTemplateId: z.string().uuid(),
slug: z slug: z
.string() .string()
@@ -32,6 +32,7 @@ export const registerCertificateProfilesRouter = async (server: FastifyZodProvid
.regex(new RE2("^[a-z0-9-]+$"), "Slug must contain only lowercase letters, numbers, and hyphens"), .regex(new RE2("^[a-z0-9-]+$"), "Slug must contain only lowercase letters, numbers, and hyphens"),
description: z.string().max(1000).optional(), description: z.string().max(1000).optional(),
enrollmentType: z.nativeEnum(EnrollmentType), enrollmentType: z.nativeEnum(EnrollmentType),
issuerType: z.nativeEnum(IssuerType).default(IssuerType.CA),
estConfig: z estConfig: z
.object({ .object({
disableBootstrapCaValidation: z.boolean().default(false), disableBootstrapCaValidation: z.boolean().default(false),
@@ -82,11 +83,26 @@ export const registerCertificateProfilesRouter = async (server: FastifyZodProvid
return false; return false;
} }
} }
if (data.issuerType === IssuerType.CA) {
if (!data.caId) {
return false;
}
}
if (data.issuerType === IssuerType.SELF_SIGNED) {
if (data.caId) {
return false;
}
if (data.enrollmentType !== EnrollmentType.API) {
return false;
}
}
return true; return true;
}, },
{ {
message: message:
"EST enrollment type requires EST configuration and cannot have API or ACME configuration. API enrollment type requires API configuration and cannot have EST or ACME configuration. ACME enrollment type requires ACME configuration and cannot have EST or API configuration." "EST enrollment type requires EST configuration and cannot have API or ACME configuration. API enrollment type requires API configuration and cannot have EST or ACME configuration. ACME enrollment type requires ACME configuration and cannot have EST or API configuration. CA issuer type requires a CA ID. Self-signed issuer type cannot have a CA ID and only supports API enrollment."
} }
), ),
response: { response: {
@@ -115,7 +131,8 @@ export const registerCertificateProfilesRouter = async (server: FastifyZodProvid
certificateProfileId: certificateProfile.id, certificateProfileId: certificateProfile.id,
name: certificateProfile.slug, name: certificateProfile.slug,
projectId: certificateProfile.projectId, projectId: certificateProfile.projectId,
enrollmentType: certificateProfile.enrollmentType enrollmentType: certificateProfile.enrollmentType,
issuerType: certificateProfile.issuerType
} }
} }
}); });
@@ -139,6 +156,7 @@ export const registerCertificateProfilesRouter = async (server: FastifyZodProvid
limit: z.coerce.number().min(1).max(100).default(20), limit: z.coerce.number().min(1).max(100).default(20),
search: z.string().optional(), search: z.string().optional(),
enrollmentType: z.nativeEnum(EnrollmentType).optional(), enrollmentType: z.nativeEnum(EnrollmentType).optional(),
issuerType: z.nativeEnum(IssuerType).optional(),
caId: z.string().uuid().optional() caId: z.string().uuid().optional()
}), }),
response: { response: {
@@ -339,6 +357,7 @@ export const registerCertificateProfilesRouter = async (server: FastifyZodProvid
.optional(), .optional(),
description: z.string().max(1000).optional(), description: z.string().max(1000).optional(),
enrollmentType: z.nativeEnum(EnrollmentType).optional(), enrollmentType: z.nativeEnum(EnrollmentType).optional(),
issuerType: z.nativeEnum(IssuerType).optional(),
estConfig: z estConfig: z
.object({ .object({
disableBootstrapCaValidation: z.boolean().default(false), disableBootstrapCaValidation: z.boolean().default(false),
@@ -67,6 +67,12 @@ export const certificateEstV3ServiceFactory = ({
throw new BadRequestError({ message: "EST enrollment not configured for this profile" }); throw new BadRequestError({ message: "EST enrollment not configured for this profile" });
} }
if (!profile.caId) {
throw new BadRequestError({
message: "Self-signed certificates are not supported for EST enrollment"
});
}
const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId); const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId);
if (!estConfig) { if (!estConfig) {
throw new NotFoundError({ message: "EST configuration not found" }); throw new NotFoundError({ message: "EST configuration not found" });
@@ -169,6 +175,12 @@ export const certificateEstV3ServiceFactory = ({
throw new BadRequestError({ message: "EST enrollment not configured for this profile" }); throw new BadRequestError({ message: "EST enrollment not configured for this profile" });
} }
if (!profile.caId) {
throw new BadRequestError({
message: "Self-signed certificates are not supported for EST enrollment"
});
}
const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId); const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId);
if (!estConfig) { if (!estConfig) {
throw new NotFoundError({ message: "EST configuration not found" }); throw new NotFoundError({ message: "EST configuration not found" });
@@ -281,6 +293,12 @@ export const certificateEstV3ServiceFactory = ({
throw new BadRequestError({ message: "EST enrollment not configured for this profile" }); throw new BadRequestError({ message: "EST enrollment not configured for this profile" });
} }
if (!profile.caId) {
throw new BadRequestError({
message: "Self-signed certificates are not supported for EST enrollment"
});
}
const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId); const estConfig = await estEnrollmentConfigDAL.findById(profile.estConfigId);
if (!estConfig) { if (!estConfig) {
throw new NotFoundError({ message: "EST configuration not found" }); throw new NotFoundError({ message: "EST configuration not found" });
@@ -7,6 +7,7 @@ import { ormify, selectAllTableCols } from "@app/lib/knex";
import { import {
EnrollmentType, EnrollmentType,
IssuerType,
TCertificateProfile, TCertificateProfile,
TCertificateProfileCertificate, TCertificateProfileCertificate,
TCertificateProfileInsert, TCertificateProfileInsert,
@@ -198,6 +199,7 @@ export const certificateProfileDALFactory = (db: TDbClient) => {
slug: result.slug, slug: result.slug,
description: result.description, description: result.description,
enrollmentType: result.enrollmentType as EnrollmentType, enrollmentType: result.enrollmentType as EnrollmentType,
issuerType: result.issuerType as IssuerType,
estConfigId: result.estConfigId, estConfigId: result.estConfigId,
apiConfigId: result.apiConfigId, apiConfigId: result.apiConfigId,
acmeConfigId: result.acmeConfigId, acmeConfigId: result.acmeConfigId,
@@ -239,12 +241,13 @@ export const certificateProfileDALFactory = (db: TDbClient) => {
limit?: number; limit?: number;
search?: string; search?: string;
enrollmentType?: EnrollmentType; enrollmentType?: EnrollmentType;
issuerType?: IssuerType;
caId?: string; caId?: string;
} = {}, } = {},
tx?: Knex tx?: Knex
): Promise<TCertificateProfile[] | TCertificateProfileWithConfigs[]> => { ): Promise<TCertificateProfile[] | TCertificateProfileWithConfigs[]> => {
try { try {
const { offset = 0, limit = 20, search, enrollmentType, caId } = options; const { offset = 0, limit = 20, search, enrollmentType, issuerType, caId } = options;
let baseQuery = (tx || db)(TableName.PkiCertificateProfile).where( let baseQuery = (tx || db)(TableName.PkiCertificateProfile).where(
`${TableName.PkiCertificateProfile}.projectId`, `${TableName.PkiCertificateProfile}.projectId`,
@@ -269,6 +272,10 @@ export const certificateProfileDALFactory = (db: TDbClient) => {
baseQuery = baseQuery.where(`${TableName.PkiCertificateProfile}.caId`, caId); baseQuery = baseQuery.where(`${TableName.PkiCertificateProfile}.caId`, caId);
} }
if (issuerType) {
baseQuery = baseQuery.where(`${TableName.PkiCertificateProfile}.issuerType`, issuerType);
}
const query = baseQuery const query = baseQuery
.leftJoin( .leftJoin(
TableName.PkiEstEnrollmentConfig, TableName.PkiEstEnrollmentConfig,
@@ -338,8 +345,10 @@ export const certificateProfileDALFactory = (db: TDbClient) => {
slug: result.slug, slug: result.slug,
description: result.description, description: result.description,
enrollmentType: result.enrollmentType as EnrollmentType, enrollmentType: result.enrollmentType as EnrollmentType,
issuerType: result.issuerType as IssuerType,
estConfigId: result.estConfigId, estConfigId: result.estConfigId,
apiConfigId: result.apiConfigId, apiConfigId: result.apiConfigId,
acmeConfigId: result.acmeConfigId,
createdAt: result.createdAt, createdAt: result.createdAt,
updatedAt: result.updatedAt, updatedAt: result.updatedAt,
estConfig, estConfig,
@@ -359,12 +368,13 @@ export const certificateProfileDALFactory = (db: TDbClient) => {
options: { options: {
search?: string; search?: string;
enrollmentType?: EnrollmentType; enrollmentType?: EnrollmentType;
issuerType?: IssuerType;
caId?: string; caId?: string;
} = {}, } = {},
tx?: Knex tx?: Knex
): Promise<number> => { ): Promise<number> => {
try { try {
const { search, enrollmentType, caId } = options; const { search, enrollmentType, issuerType, caId } = options;
let query = (tx || db)(TableName.PkiCertificateProfile).where({ projectId }); let query = (tx || db)(TableName.PkiCertificateProfile).where({ projectId });
@@ -384,6 +394,10 @@ export const certificateProfileDALFactory = (db: TDbClient) => {
query = query.where({ caId }); query = query.where({ caId });
} }
if (issuerType) {
query = query.where({ issuerType });
}
const result = await query.count("*").first(); const result = await query.count("*").first();
return parseInt((result as unknown as { count: string }).count || "0", 10); return parseInt((result as unknown as { count: string }).count || "0", 10);
} catch (error) { } catch (error) {
@@ -1,12 +1,17 @@
import RE2 from "re2"; import RE2 from "re2";
import { z } from "zod"; import { z } from "zod";
import { EnrollmentType } from "./certificate-profile-types"; import { CertStatus } from "../certificate/certificate-types";
import { EnrollmentType, IssuerType } from "./certificate-profile-types";
export const createCertificateProfileSchema = z export const createCertificateProfileSchema = z
.object({ .object({
projectId: z.string().uuid("Project ID must be valid"), projectId: z.string().uuid("Project ID must be valid"),
caId: z.string().uuid(), caId: z
.union([z.string().uuid(), z.literal("")])
.optional()
.nullable()
.transform((val) => (val === "" ? null : val)),
certificateTemplateId: z.string().uuid(), certificateTemplateId: z.string().uuid(),
slug: z slug: z
.string() .string()
@@ -15,6 +20,7 @@ export const createCertificateProfileSchema = z
.regex(new RE2("^[a-z0-9-]+$"), "Slug must contain only lowercase letters, numbers, and hyphens"), .regex(new RE2("^[a-z0-9-]+$"), "Slug must contain only lowercase letters, numbers, and hyphens"),
description: z.string().max(1000).optional(), description: z.string().max(1000).optional(),
enrollmentType: z.nativeEnum(EnrollmentType), enrollmentType: z.nativeEnum(EnrollmentType),
issuerType: z.nativeEnum(IssuerType).default(IssuerType.CA),
estConfig: z estConfig: z
.object({ .object({
disableBootstrapCaValidation: z.boolean().default(false), disableBootstrapCaValidation: z.boolean().default(false),
@@ -32,6 +38,7 @@ export const createCertificateProfileSchema = z
}) })
.refine( .refine(
(data) => { (data) => {
// Validate enrollment type configurations
if (data.enrollmentType === EnrollmentType.EST) { if (data.enrollmentType === EnrollmentType.EST) {
if (!data.estConfig) { if (!data.estConfig) {
return false; return false;
@@ -65,11 +72,26 @@ export const createCertificateProfileSchema = z
return false; return false;
} }
} }
if (data.issuerType === IssuerType.CA) {
if (!data.caId) {
return false;
}
}
if (data.issuerType === IssuerType.SELF_SIGNED) {
if (data.caId) {
return false;
}
if (data.enrollmentType !== EnrollmentType.API) {
return false;
}
}
return true; return true;
}, },
{ {
message: message:
"EST enrollment type requires EST configuration and cannot have API configuration. API enrollment type requires API configuration and cannot have EST configuration." "EST enrollment type requires EST configuration and cannot have API configuration. API enrollment type requires API configuration and cannot have EST configuration. CA issuer type requires a CA ID. Self-signed issuer type cannot have a CA ID and only supports API enrollment."
} }
); );
@@ -83,6 +105,7 @@ export const updateCertificateProfileSchema = z
.optional(), .optional(),
description: z.string().max(1000).optional(), description: z.string().max(1000).optional(),
enrollmentType: z.nativeEnum(EnrollmentType).optional(), enrollmentType: z.nativeEnum(EnrollmentType).optional(),
issuerType: z.nativeEnum(IssuerType).optional(),
estConfig: z estConfig: z
.object({ .object({
disableBootstrapCaValidation: z.boolean().default(false), disableBootstrapCaValidation: z.boolean().default(false),
@@ -109,10 +132,18 @@ export const updateCertificateProfileSchema = z
return false; return false;
} }
} }
if (data.issuerType === IssuerType.SELF_SIGNED) {
if (data.enrollmentType && data.enrollmentType !== EnrollmentType.API) {
return false;
}
}
return true; return true;
}, },
{ {
message: "Cannot have EST config with API enrollment type or API config with EST enrollment type." message:
"Cannot have EST config with API enrollment type or API config with EST enrollment type. Self-signed issuer type only supports API enrollment."
} }
); );
@@ -131,6 +162,7 @@ export const listCertificateProfilesSchema = z.object({
limit: z.coerce.number().min(1).max(100).default(20), limit: z.coerce.number().min(1).max(100).default(20),
search: z.string().optional(), search: z.string().optional(),
enrollmentType: z.nativeEnum(EnrollmentType).optional(), enrollmentType: z.nativeEnum(EnrollmentType).optional(),
issuerType: z.nativeEnum(IssuerType).optional(),
caId: z.string().uuid().optional() caId: z.string().uuid().optional()
}); });
@@ -142,6 +174,6 @@ export const listCertificatesByProfileSchema = z.object({
profileId: z.string().uuid(), profileId: z.string().uuid(),
offset: z.coerce.number().min(0).default(0), offset: z.coerce.number().min(0).default(0),
limit: z.coerce.number().min(1).max(100).default(20), limit: z.coerce.number().min(1).max(100).default(20),
status: z.enum(["active", "expired", "revoked"]).optional(), status: z.nativeEnum(CertStatus).optional(),
search: z.string().optional() search: z.string().optional()
}); });
@@ -22,7 +22,12 @@ import type { TKmsServiceFactory } from "../kms/kms-service";
import type { TProjectDALFactory } from "../project/project-dal"; import type { TProjectDALFactory } from "../project/project-dal";
import type { TCertificateProfileDALFactory } from "./certificate-profile-dal"; import type { TCertificateProfileDALFactory } from "./certificate-profile-dal";
import { certificateProfileServiceFactory, TCertificateProfileServiceFactory } from "./certificate-profile-service"; import { certificateProfileServiceFactory, TCertificateProfileServiceFactory } from "./certificate-profile-service";
import { EnrollmentType, TCertificateProfile, TCertificateProfileWithConfigs } from "./certificate-profile-types"; import {
EnrollmentType,
IssuerType,
TCertificateProfile,
TCertificateProfileWithConfigs
} from "./certificate-profile-types";
vi.mock("@app/lib/crypto/cryptography", () => ({ vi.mock("@app/lib/crypto/cryptography", () => ({
crypto: { crypto: {
@@ -90,6 +95,7 @@ describe("CertificateProfileService", () => {
description: "Test certificate profile", description: "Test certificate profile",
slug: "test-profile", slug: "test-profile",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfigId: "api-config-123", apiConfigId: "api-config-123",
@@ -272,6 +278,7 @@ describe("CertificateProfileService", () => {
slug: "new-profile", slug: "new-profile",
description: "New test profile", description: "New test profile",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -312,6 +319,7 @@ describe("CertificateProfileService", () => {
slug: "new-profile", slug: "new-profile",
description: "New test profile", description: "New test profile",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfigId: "api-config-123", apiConfigId: "api-config-123",
@@ -383,6 +391,7 @@ describe("CertificateProfileService", () => {
slug: "invalid-profile", slug: "invalid-profile",
description: "Invalid test profile", description: "Invalid test profile",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123" certificateTemplateId: "template-123"
}; };
@@ -401,6 +410,7 @@ describe("CertificateProfileService", () => {
slug: "api-profile", slug: "api-profile",
description: "Profile with API enrollment", description: "Profile with API enrollment",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -726,6 +736,7 @@ describe("CertificateProfileService", () => {
slug: "est-profile", slug: "est-profile",
description: "Profile with EST enrollment", description: "Profile with EST enrollment",
enrollmentType: EnrollmentType.EST, enrollmentType: EnrollmentType.EST,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
estConfig: { estConfig: {
@@ -776,6 +787,7 @@ describe("CertificateProfileService", () => {
slug: "different-profile-name", slug: "different-profile-name",
description: "Profile with duplicate slug", description: "Profile with duplicate slug",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -801,6 +813,7 @@ describe("CertificateProfileService", () => {
slug: "auto-renew-profile", slug: "auto-renew-profile",
description: "Profile with auto-renewal", description: "Profile with auto-renewal",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -965,6 +978,7 @@ describe("CertificateProfileService", () => {
slug: "invalid-template-profile", slug: "invalid-template-profile",
description: "Profile with invalid template", description: "Profile with invalid template",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "nonexistent-template", certificateTemplateId: "nonexistent-template",
apiConfig: { apiConfig: {
@@ -990,6 +1004,7 @@ describe("CertificateProfileService", () => {
slug: "concurrent-profile", slug: "concurrent-profile",
description: "Profile created concurrently", description: "Profile created concurrently",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -1018,6 +1033,7 @@ describe("CertificateProfileService", () => {
slug: "cross-project-profile", slug: "cross-project-profile",
description: "Profile using template from different project", description: "Profile using template from different project",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-456", certificateTemplateId: "template-456",
apiConfig: { apiConfig: {
@@ -1047,6 +1063,7 @@ describe("CertificateProfileService", () => {
slug: "invalid-slug-profile", slug: "invalid-slug-profile",
description: "Profile with invalid slug format", description: "Profile with invalid slug format",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -32,6 +32,7 @@ import { getProjectKmsCertificateKeyId } from "../project/project-fns";
import { TCertificateProfileDALFactory } from "./certificate-profile-dal"; import { TCertificateProfileDALFactory } from "./certificate-profile-dal";
import { import {
EnrollmentType, EnrollmentType,
IssuerType,
TCertificateProfile, TCertificateProfile,
TCertificateProfileCertificate, TCertificateProfileCertificate,
TCertificateProfileInsert, TCertificateProfileInsert,
@@ -39,6 +40,34 @@ import {
TCertificateProfileWithConfigs TCertificateProfileWithConfigs
} from "./certificate-profile-types"; } from "./certificate-profile-types";
const validateIssuerTypeConstraints = (
issuerType: IssuerType,
enrollmentType: EnrollmentType,
caId: string | null,
existingCaId?: string | null
) => {
if (issuerType === IssuerType.CA) {
if (!caId && !existingCaId) {
throw new ForbiddenRequestError({
message: "CA issuer type requires a Certificate Authority to be selected"
});
}
}
if (issuerType === IssuerType.SELF_SIGNED) {
if (caId) {
throw new ForbiddenRequestError({
message: "Self-signed issuer type cannot have a Certificate Authority"
});
}
if (enrollmentType !== EnrollmentType.API) {
throw new ForbiddenRequestError({
message: "Self-signed issuer type only supports API enrollment"
});
}
}
};
const generateAndEncryptAcmeEabSecret = async ( const generateAndEncryptAcmeEabSecret = async (
projectId: string, projectId: string,
kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey">, kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey">,
@@ -163,7 +192,8 @@ export type TCertificateProfileServiceFactory = ReturnType<typeof certificatePro
const convertDalToService = (dalResult: Record<string, unknown>): TCertificateProfile => { const convertDalToService = (dalResult: Record<string, unknown>): TCertificateProfile => {
return { return {
...dalResult, ...dalResult,
enrollmentType: dalResult.enrollmentType as EnrollmentType enrollmentType: dalResult.enrollmentType as EnrollmentType,
issuerType: dalResult.issuerType as IssuerType
} as TCertificateProfile; } as TCertificateProfile;
}; };
@@ -240,6 +270,8 @@ export const certificateProfileServiceFactory = ({
}); });
} }
validateIssuerTypeConstraints(data.issuerType, data.enrollmentType, data.caId ?? null);
// Validate enrollment configuration requirements // Validate enrollment configuration requirements
if (data.enrollmentType === EnrollmentType.EST && !data.estConfig) { if (data.enrollmentType === EnrollmentType.EST && !data.estConfig) {
throw new ForbiddenRequestError({ throw new ForbiddenRequestError({
@@ -376,7 +408,18 @@ export const certificateProfileServiceFactory = ({
} }
} }
const { estConfig, apiConfig, ...profileUpdateData } = data; const finalIssuerType = data.issuerType || existingProfile.issuerType;
const finalEnrollmentType = data.enrollmentType || existingProfile.enrollmentType;
const finalCaId = data.caId !== undefined ? data.caId : existingProfile.caId;
validateIssuerTypeConstraints(finalIssuerType, finalEnrollmentType, finalCaId ?? null, existingProfile.caId);
const updatedData =
finalIssuerType === IssuerType.SELF_SIGNED && existingProfile.caId && data.issuerType === IssuerType.SELF_SIGNED
? { ...data, caId: null }
: data;
const { estConfig, apiConfig, ...profileUpdateData } = updatedData;
const updatedProfile = await certificateProfileDAL.transaction(async (tx) => { const updatedProfile = await certificateProfileDAL.transaction(async (tx) => {
if (estConfig && existingProfile.estConfigId) { if (estConfig && existingProfile.estConfigId) {
@@ -569,6 +612,7 @@ export const certificateProfileServiceFactory = ({
limit = 20, limit = 20,
search, search,
enrollmentType, enrollmentType,
issuerType,
caId caId
}: { }: {
actor: ActorType; actor: ActorType;
@@ -580,6 +624,7 @@ export const certificateProfileServiceFactory = ({
limit?: number; limit?: number;
search?: string; search?: string;
enrollmentType?: EnrollmentType; enrollmentType?: EnrollmentType;
issuerType?: IssuerType;
caId?: string; caId?: string;
}): Promise<{ }): Promise<{
profiles: TCertificateProfileWithConfigs[]; profiles: TCertificateProfileWithConfigs[];
@@ -603,12 +648,14 @@ export const certificateProfileServiceFactory = ({
limit, limit,
search, search,
enrollmentType, enrollmentType,
issuerType,
caId caId
}); });
const totalCount = await certificateProfileDAL.countByProjectId(projectId, { const totalCount = await certificateProfileDAL.countByProjectId(projectId, {
search, search,
enrollmentType, enrollmentType,
issuerType,
caId caId
}); });
@@ -10,16 +10,24 @@ export enum EnrollmentType {
ACME = "acme" ACME = "acme"
} }
export type TCertificateProfile = Omit<TPkiCertificateProfiles, "enrollmentType"> & { export enum IssuerType {
CA = "ca",
SELF_SIGNED = "self-signed"
}
export type TCertificateProfile = Omit<TPkiCertificateProfiles, "enrollmentType" | "issuerType"> & {
enrollmentType: EnrollmentType; enrollmentType: EnrollmentType;
issuerType: IssuerType;
}; };
export type TCertificateProfileInsert = Omit<TPkiCertificateProfilesInsert, "enrollmentType"> & { export type TCertificateProfileInsert = Omit<TPkiCertificateProfilesInsert, "enrollmentType" | "issuerType"> & {
enrollmentType: EnrollmentType; enrollmentType: EnrollmentType;
issuerType: IssuerType;
}; };
export type TCertificateProfileUpdate = Omit<TPkiCertificateProfilesUpdate, "enrollmentType"> & { export type TCertificateProfileUpdate = Omit<TPkiCertificateProfilesUpdate, "enrollmentType" | "issuerType"> & {
enrollmentType?: EnrollmentType; enrollmentType?: EnrollmentType;
issuerType?: IssuerType;
estConfig?: { estConfig?: {
disableBootstrapCaValidation?: boolean; disableBootstrapCaValidation?: boolean;
passphrase?: string; passphrase?: string;
@@ -22,7 +22,7 @@ import {
CertSubjectAttributeType CertSubjectAttributeType
} from "@app/services/certificate-common/certificate-constants"; } from "@app/services/certificate-common/certificate-constants";
import { TCertificateProfileDALFactory } from "@app/services/certificate-profile/certificate-profile-dal"; import { TCertificateProfileDALFactory } from "@app/services/certificate-profile/certificate-profile-dal";
import { EnrollmentType } from "@app/services/certificate-profile/certificate-profile-types"; import { EnrollmentType, IssuerType } from "@app/services/certificate-profile/certificate-profile-types";
import { TCertificateTemplateV2ServiceFactory } from "@app/services/certificate-template-v2/certificate-template-v2-service"; import { TCertificateTemplateV2ServiceFactory } from "@app/services/certificate-template-v2/certificate-template-v2-service";
import { ActorType, AuthMethod } from "../auth/auth-type"; import { ActorType, AuthMethod } from "../auth/auth-type";
@@ -40,18 +40,29 @@ vi.mock("../certificate-common/certificate-csr-utils", () => ({
describe("CertificateV3Service", () => { describe("CertificateV3Service", () => {
let service: TCertificateV3ServiceFactory; let service: TCertificateV3ServiceFactory;
const mockCertificateDAL: Pick<TCertificateDALFactory, "findOne" | "findById" | "updateById" | "transaction"> = { const mockCertificateDAL: Pick<
TCertificateDALFactory,
"findOne" | "findById" | "updateById" | "transaction" | "create"
> = {
findOne: vi.fn(), findOne: vi.fn(),
findById: vi.fn(), findById: vi.fn(),
updateById: vi.fn(), updateById: vi.fn(),
create: vi.fn().mockResolvedValue({
id: "new-cert-id",
serialNumber: "123456789",
friendlyName: "Test Certificate",
commonName: "test.example.com",
status: "ACTIVE"
}),
transaction: vi.fn().mockImplementation(async (callback: (tx: any) => Promise<unknown>) => { transaction: vi.fn().mockImplementation(async (callback: (tx: any) => Promise<unknown>) => {
const mockTx = {}; const mockTx = {};
return callback(mockTx); return callback(mockTx);
}) })
}; };
const mockCertificateSecretDAL: Pick<TCertificateSecretDALFactory, "findOne"> = { const mockCertificateSecretDAL: Pick<TCertificateSecretDALFactory, "findOne" | "create"> = {
findOne: vi.fn() findOne: vi.fn(),
create: vi.fn()
}; };
const mockCertificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findByIdWithAssociatedCa"> = { const mockCertificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findByIdWithAssociatedCa"> = {
@@ -150,7 +161,24 @@ describe("CertificateV3Service", () => {
}, },
pkiSyncQueue: { pkiSyncQueue: {
queuePkiSyncSyncCertificatesById: vi.fn().mockResolvedValue(undefined) queuePkiSyncSyncCertificatesById: vi.fn().mockResolvedValue(undefined)
} },
certificateBodyDAL: {
create: vi.fn().mockResolvedValue({ id: "body-123" })
},
kmsService: {
generateKmsKey: vi.fn().mockResolvedValue("kms-key-123"),
encryptWithKmsKey: vi.fn().mockResolvedValue(vi.fn().mockResolvedValue(Buffer.from("encrypted"))),
decryptWithKmsKey: vi.fn().mockResolvedValue(vi.fn().mockResolvedValue(Buffer.from("decrypted")))
},
projectDAL: {
findOne: vi.fn().mockResolvedValue({ id: "project-123" }),
findById: vi.fn().mockResolvedValue({ id: "project-123" }),
updateById: vi.fn().mockResolvedValue({ id: "project-123" }),
transaction: vi.fn().mockImplementation(async (callback: (tx: any) => Promise<unknown>) => {
const mockTx = {};
return callback(mockTx);
})
} as any
}); });
}); });
@@ -175,6 +203,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -319,6 +348,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -508,6 +538,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.EST, // Wrong enrollment type enrollmentType: EnrollmentType.EST, // Wrong enrollment type
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -561,6 +592,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -721,6 +753,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.EST, // Wrong enrollment type enrollmentType: EnrollmentType.EST, // Wrong enrollment type
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -772,6 +805,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -933,6 +967,7 @@ describe("CertificateV3Service", () => {
id: profileId, id: profileId,
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.EST, // Wrong enrollment type enrollmentType: EnrollmentType.EST, // Wrong enrollment type
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
createdAt: new Date(), createdAt: new Date(),
@@ -971,6 +1006,7 @@ describe("CertificateV3Service", () => {
caId: "ca-1", caId: "ca-1",
certificateTemplateId: "template-1", certificateTemplateId: "template-1",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
description: "Test profile for algorithm compatibility", description: "Test profile for algorithm compatibility",
@@ -1552,6 +1588,7 @@ describe("CertificateV3Service", () => {
id: "profile-123", id: "profile-123",
projectId: "project-123", projectId: "project-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
caId: "ca-123", caId: "ca-123",
certificateTemplateId: "template-123", certificateTemplateId: "template-123",
apiConfig: { apiConfig: {
@@ -1733,9 +1770,9 @@ describe("CertificateV3Service", () => {
}); });
}); });
it("should reject renewal if certificate is not from a profile", async () => { it("should reject renewal if certificate has no profile and no CA", async () => {
const certWithoutProfile = { ...mockOriginalCert, profileId: null }; const certWithoutProfileAndCA = { ...mockOriginalCert, profileId: null, caId: null };
vi.mocked(mockCertificateDAL.findById).mockResolvedValue(certWithoutProfile); vi.mocked(mockCertificateDAL.findById).mockResolvedValue(certWithoutProfileAndCA);
// Set up transaction mock to properly handle errors // Set up transaction mock to properly handle errors
vi.mocked(mockCertificateDAL.transaction).mockImplementation(async (callback: (tx: any) => Promise<unknown>) => { vi.mocked(mockCertificateDAL.transaction).mockImplementation(async (callback: (tx: any) => Promise<unknown>) => {
@@ -1755,7 +1792,7 @@ describe("CertificateV3Service", () => {
certificateId: "cert-123", certificateId: "cert-123",
...mockActor ...mockActor
}) })
).rejects.toThrow("Only certificates issued from a profile can be renewed"); ).rejects.toThrow("Only certificates issued from a profile or self-signed certificates can be renewed");
}); });
it("should reject renewal if certificate was issued from CSR (external private key)", async () => { it("should reject renewal if certificate was issued from CSR (external private key)", async () => {
@@ -1989,6 +2026,44 @@ describe("CertificateV3Service", () => {
expect(result).toHaveProperty("certificate", "renewed-cert"); expect(result).toHaveProperty("certificate", "renewed-cert");
}); });
it("should successfully renew self-signed certificate", async () => {
// Self-signed certificate has no caId and no profileId
const selfSignedCert = {
...mockOriginalCert,
profileId: null,
certificateTemplateId: null,
caId: null
};
vi.mocked(mockCertificateDAL.findById).mockResolvedValue(selfSignedCert);
vi.mocked(mockCertificateAuthorityDAL.findByIdWithAssociatedCa).mockResolvedValue(mockCA);
vi.mocked(mockCertificateSecretDAL.findOne).mockResolvedValue({ id: "secret-123", certId: "cert-123" } as any);
vi.mocked(mockCertificateTemplateV2Service.getTemplateV2ById).mockResolvedValue(mockTemplate);
const newCert = {
...selfSignedCert,
id: "cert-456",
serialNumber: "self-signed-789012"
};
vi.mocked(mockCertificateDAL.create).mockResolvedValue(newCert);
vi.mocked(mockCertificateDAL.findOne).mockResolvedValue(newCert);
vi.mocked(mockCertificateDAL.updateById).mockResolvedValue(newCert);
// Set up transaction mock
vi.mocked(mockCertificateDAL.transaction).mockImplementation(async (callback: (tx: any) => Promise<unknown>) => {
const mockTx = {};
return callback(mockTx);
});
const result = await service.renewCertificate({
certificateId: "cert-123",
...mockActor
});
expect(result).toHaveProperty("certificate");
expect(result).toHaveProperty("serialNumber");
});
}); });
describe("updateRenewalConfig", () => { describe("updateRenewalConfig", () => {
@@ -2008,6 +2083,7 @@ describe("CertificateV3Service", () => {
const mockProfile = { const mockProfile = {
id: "profile-123", id: "profile-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
projectId: "project-123" projectId: "project-123"
}; };
@@ -2084,6 +2160,7 @@ describe("CertificateV3Service", () => {
const mockProfile = { const mockProfile = {
id: "profile-123", id: "profile-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
projectId: "project-123" projectId: "project-123"
}; };
@@ -2129,6 +2206,7 @@ describe("CertificateV3Service", () => {
const mockProfile = { const mockProfile = {
id: "profile-123", id: "profile-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
projectId: "project-123" projectId: "project-123"
}; };
@@ -2172,6 +2250,7 @@ describe("CertificateV3Service", () => {
const mockProfile = { const mockProfile = {
id: "profile-123", id: "profile-123",
enrollmentType: EnrollmentType.API, enrollmentType: EnrollmentType.API,
issuerType: IssuerType.CA,
projectId: "project-123" projectId: "project-123"
}; };
@@ -1,8 +1,9 @@
import { ForbiddenError } from "@casl/ability"; import { ForbiddenError } from "@casl/ability";
import * as x509 from "@peculiar/x509";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
import RE2 from "re2"; import RE2 from "re2";
import { ActionProjectType } from "@app/db/schemas"; import { ActionProjectType, TCertificates } from "@app/db/schemas";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types"; import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
import { import {
ProjectPermissionCertificateActions, ProjectPermissionCertificateActions,
@@ -10,8 +11,11 @@ import {
ProjectPermissionSub ProjectPermissionSub
} from "@app/ee/services/permission/project-permission"; } from "@app/ee/services/permission/project-permission";
import { TPkiAcmeAccountDALFactory } from "@app/ee/services/pki-acme/pki-acme-account-dal"; import { TPkiAcmeAccountDALFactory } from "@app/ee/services/pki-acme/pki-acme-account-dal";
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors"; import { BadRequestError, ForbiddenRequestError, NotFoundError } from "@app/lib/errors";
import { ms } from "@app/lib/ms";
import { ActorAuthMethod, ActorType } from "@app/services/auth/auth-type"; import { ActorAuthMethod, ActorType } from "@app/services/auth/auth-type";
import { TCertificateBodyDALFactory } from "@app/services/certificate/certificate-body-dal";
import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal"; import { TCertificateDALFactory } from "@app/services/certificate/certificate-dal";
import { TCertificateSecretDALFactory } from "@app/services/certificate/certificate-secret-dal"; import { TCertificateSecretDALFactory } from "@app/services/certificate/certificate-secret-dal";
import { import {
@@ -28,12 +32,25 @@ import {
TCertificateAuthorityWithAssociatedCa TCertificateAuthorityWithAssociatedCa
} from "@app/services/certificate-authority/certificate-authority-dal"; } from "@app/services/certificate-authority/certificate-authority-dal";
import { CaStatus, CaType } from "@app/services/certificate-authority/certificate-authority-enums"; import { CaStatus, CaType } from "@app/services/certificate-authority/certificate-authority-enums";
import {
createDistinguishedName,
createSerialNumber,
keyAlgorithmToAlgCfg,
signatureAlgorithmToAlgCfg
} from "@app/services/certificate-authority/certificate-authority-fns";
import { TInternalCertificateAuthorityServiceFactory } from "@app/services/certificate-authority/internal/internal-certificate-authority-service"; import { TInternalCertificateAuthorityServiceFactory } from "@app/services/certificate-authority/internal/internal-certificate-authority-service";
import { TCertificateProfileDALFactory } from "@app/services/certificate-profile/certificate-profile-dal"; import { TCertificateProfileDALFactory } from "@app/services/certificate-profile/certificate-profile-dal";
import { EnrollmentType } from "@app/services/certificate-profile/certificate-profile-types"; import { EnrollmentType, IssuerType } from "@app/services/certificate-profile/certificate-profile-types";
import { TCertificateTemplateV2ServiceFactory } from "@app/services/certificate-template-v2/certificate-template-v2-service"; import { TCertificateTemplateV2ServiceFactory } from "@app/services/certificate-template-v2/certificate-template-v2-service";
import { TKmsServiceFactory } from "@app/services/kms/kms-service";
import { TProjectDALFactory } from "@app/services/project/project-dal";
import { getProjectKmsCertificateKeyId } from "@app/services/project/project-fns";
import { CertSubjectAlternativeNameType } from "../certificate-common/certificate-constants"; import {
CertExtendedKeyUsageType,
CertKeyUsageType,
CertSubjectAlternativeNameType
} from "../certificate-common/certificate-constants";
import { import {
extractAlgorithmsFromCSR, extractAlgorithmsFromCSR,
extractCertificateRequestFromCSR extractCertificateRequestFromCSR
@@ -68,8 +85,9 @@ import {
} from "./certificate-v3-types"; } from "./certificate-v3-types";
type TCertificateV3ServiceFactoryDep = { type TCertificateV3ServiceFactoryDep = {
certificateDAL: Pick<TCertificateDALFactory, "findOne" | "findById" | "updateById" | "transaction">; certificateDAL: Pick<TCertificateDALFactory, "findOne" | "findById" | "updateById" | "transaction" | "create">;
certificateSecretDAL: Pick<TCertificateSecretDALFactory, "findOne">; certificateBodyDAL: Pick<TCertificateBodyDALFactory, "create">;
certificateSecretDAL: Pick<TCertificateSecretDALFactory, "findOne" | "create">;
certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findByIdWithAssociatedCa">; certificateAuthorityDAL: Pick<TCertificateAuthorityDALFactory, "findByIdWithAssociatedCa">;
certificateProfileDAL: Pick<TCertificateProfileDALFactory, "findByIdWithConfigs">; certificateProfileDAL: Pick<TCertificateProfileDALFactory, "findByIdWithConfigs">;
acmeAccountDAL: Pick<TPkiAcmeAccountDALFactory, "findById">; acmeAccountDAL: Pick<TPkiAcmeAccountDALFactory, "findById">;
@@ -85,6 +103,8 @@ type TCertificateV3ServiceFactoryDep = {
>; >;
pkiSyncDAL: Pick<TPkiSyncDALFactory, "find">; pkiSyncDAL: Pick<TPkiSyncDALFactory, "find">;
pkiSyncQueue: Pick<TPkiSyncQueueFactory, "queuePkiSyncSyncCertificatesById">; pkiSyncQueue: Pick<TPkiSyncQueueFactory, "queuePkiSyncSyncCertificatesById">;
kmsService: Pick<TKmsServiceFactory, "generateKmsKey" | "encryptWithKmsKey" | "decryptWithKmsKey">;
projectDAL: TProjectDALFactory;
}; };
export type TCertificateV3ServiceFactory = ReturnType<typeof certificateV3ServiceFactory>; export type TCertificateV3ServiceFactory = ReturnType<typeof certificateV3ServiceFactory>;
@@ -329,6 +349,141 @@ const parseTtlToDays = (ttl: string): number => {
} }
}; };
const generateSelfSignedCertificate = async ({
certificateRequest,
template,
effectiveSignatureAlgorithm,
effectiveKeyAlgorithm
}: {
certificateRequest: {
commonName?: string;
keyUsages?: CertKeyUsageType[];
extendedKeyUsages?: CertExtendedKeyUsageType[];
altNames?: Array<{
type: CertSubjectAlternativeNameType;
value: string;
}>;
validity: { ttl: string };
notBefore?: Date;
notAfter?: Date;
};
template?: {
subject?: Array<{
type: string;
allowed?: string[];
required?: string[];
denied?: string[];
}>;
sans?: Array<{
type: string;
allowed?: string[];
required?: string[];
denied?: string[];
}>;
} | null;
effectiveSignatureAlgorithm: CertSignatureAlgorithm;
effectiveKeyAlgorithm: CertKeyAlgorithm;
}): Promise<{
certificate: Buffer;
privateKey: Buffer;
serialNumber: string;
notBefore: Date;
notAfter: Date;
certificateSubject: Record<string, unknown>;
subjectAlternativeNames: Array<{
type: CertSubjectAlternativeNameType;
value: string;
}>;
}> => {
const certificateSubject = buildCertificateSubjectFromTemplate(certificateRequest, template?.subject);
const subjectAlternativeNames = buildSubjectAlternativeNamesFromTemplate(
{ subjectAlternativeNames: certificateRequest.altNames },
template?.sans
);
const keyGenAlg = keyAlgorithmToAlgCfg(effectiveKeyAlgorithm);
const keyPair = await crypto.nativeCrypto.subtle.generateKey(keyGenAlg, true, ["sign", "verify"]);
const signatureAlgorithmConfig = signatureAlgorithmToAlgCfg(effectiveSignatureAlgorithm, effectiveKeyAlgorithm);
const notBeforeDate = certificateRequest.notBefore ? new Date(certificateRequest.notBefore) : new Date();
let notAfterDate = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
if (certificateRequest.notAfter) {
notAfterDate = new Date(certificateRequest.notAfter);
} else if (certificateRequest.validity.ttl) {
notAfterDate = new Date(new Date().getTime() + ms(certificateRequest.validity.ttl));
}
const serialNumber = createSerialNumber();
const dn = createDistinguishedName({
commonName: certificateSubject.common_name,
organization: certificateSubject.organization,
ou: certificateSubject.organizational_unit,
country: certificateSubject.country,
province: certificateSubject.state_or_province_name,
locality: certificateSubject.locality_name
});
const cert = await x509.X509CertificateGenerator.createSelfSigned({
name: dn,
serialNumber,
notBefore: notBeforeDate,
notAfter: notAfterDate,
signingAlgorithm: signatureAlgorithmConfig,
keys: keyPair,
extensions: [
new x509.BasicConstraintsExtension(false, undefined, false),
...(certificateRequest.keyUsages?.length
? [
new x509.KeyUsagesExtension(
(convertKeyUsageArrayToLegacy(certificateRequest.keyUsages) || []).reduce(
// eslint-disable-next-line no-bitwise
(acc: number, usage) => acc | x509.KeyUsageFlags[usage],
0
),
false
)
]
: []),
...(certificateRequest.extendedKeyUsages?.length
? [
new x509.ExtendedKeyUsageExtension(
(convertExtendedKeyUsageArrayToLegacy(certificateRequest.extendedKeyUsages) || []).map(
(eku) => x509.ExtendedKeyUsage[eku]
),
false
)
]
: []),
...(subjectAlternativeNames
? [
new x509.SubjectAlternativeNameExtension(
certificateRequest.altNames?.map((san) => ({
type: san.type === CertSubjectAlternativeNameType.DNS_NAME ? "dns" : "ip",
value: san.value
})) || [],
false
)
]
: [])
]
});
const certificatePem = cert.toString("pem");
const privateKeyObj = crypto.nativeCrypto.KeyObject.from(keyPair.privateKey);
const privateKeyPem = privateKeyObj.export({ format: "pem", type: "pkcs8" }) as string;
return {
certificate: Buffer.from(certificatePem),
privateKey: Buffer.from(privateKeyPem),
serialNumber,
notBefore: notBeforeDate,
notAfter: notAfterDate,
certificateSubject,
subjectAlternativeNames: certificateRequest.altNames || []
};
};
const calculateFinalRenewBeforeDays = ( const calculateFinalRenewBeforeDays = (
profile: { apiConfig?: { autoRenew?: boolean; renewBeforeDays?: number } }, profile: { apiConfig?: { autoRenew?: boolean; renewBeforeDays?: number } },
ttl: string, ttl: string,
@@ -348,8 +503,248 @@ const calculateFinalRenewBeforeDays = (
return isValidRenewalTiming(renewBeforeDays, certificateExpiryDate) ? renewBeforeDays : undefined; return isValidRenewalTiming(renewBeforeDays, certificateExpiryDate) ? renewBeforeDays : undefined;
}; };
const getEffectiveAlgorithms = (
requestSignatureAlgorithm?: CertSignatureAlgorithm,
requestKeyAlgorithm?: CertKeyAlgorithm,
originalSignatureAlgorithm?: CertSignatureAlgorithm,
originalKeyAlgorithm?: CertKeyAlgorithm
) => {
return {
signatureAlgorithm: requestSignatureAlgorithm || originalSignatureAlgorithm || CertSignatureAlgorithm.RSA_SHA256,
keyAlgorithm: requestKeyAlgorithm || originalKeyAlgorithm || CertKeyAlgorithm.RSA_2048
};
};
const createSelfSignedCertificateRecord = async ({
selfSignedResult,
certificateRequest,
profile,
originalCert,
certificateDAL,
tx,
isRenewal = false
}: {
selfSignedResult: Awaited<ReturnType<typeof generateSelfSignedCertificate>>;
certificateRequest: {
commonName?: string;
keyUsages?: CertKeyUsageType[];
extendedKeyUsages?: CertExtendedKeyUsageType[];
};
profile?: { id: string; projectId: string } | null;
originalCert?: {
id: string;
friendlyName?: string | null;
commonName?: string | null;
projectId: string;
};
certificateDAL: Pick<TCertificateDALFactory, "create" | "updateById">;
tx: Parameters<TCertificateDALFactory["create"]>[1];
isRenewal?: boolean;
}) => {
const subjectCommonName =
(selfSignedResult.certificateSubject.common_name as string) ||
certificateRequest.commonName ||
originalCert?.commonName ||
(isRenewal ? "Renewed Self-signed Certificate" : "Self-signed Certificate");
const altNamesList = selfSignedResult.subjectAlternativeNames.map((san) => san.value).join(",");
const projectId = originalCert?.projectId || profile?.projectId;
if (!projectId) {
throw new BadRequestError({ message: "Project ID is required for certificate creation" });
}
const baseRecord = {
serialNumber: selfSignedResult.serialNumber,
friendlyName: originalCert?.friendlyName || subjectCommonName,
commonName: subjectCommonName,
altNames: altNamesList,
status: CertStatus.ACTIVE,
notBefore: selfSignedResult.notBefore,
notAfter: selfSignedResult.notAfter,
projectId,
keyUsages: convertKeyUsageArrayToLegacy(certificateRequest.keyUsages) || [],
extendedKeyUsages: convertExtendedKeyUsageArrayToLegacy(certificateRequest.extendedKeyUsages) || [],
profileId: profile?.id || null
};
const renewalRecord =
isRenewal && originalCert
? {
renewedFromCertificateId: originalCert.id
}
: {};
return certificateDAL.create(
{
...baseRecord,
...renewalRecord
},
tx
);
};
const createEncryptedCertificateData = async ({
certificateId,
certificate,
privateKey,
projectId,
certificateBodyDAL,
certificateSecretDAL,
kmsService,
projectDAL,
tx
}: {
certificateId: string;
certificate: Buffer;
privateKey: Buffer;
projectId: string;
certificateBodyDAL: Pick<TCertificateBodyDALFactory, "create">;
certificateSecretDAL: Pick<TCertificateSecretDALFactory, "create">;
kmsService: Pick<TKmsServiceFactory, "encryptWithKmsKey" | "generateKmsKey">;
projectDAL: TProjectDALFactory;
tx: Parameters<TCertificateBodyDALFactory["create"]>[1];
}) => {
const certificateManagerKeyId = await getProjectKmsCertificateKeyId({
projectId,
projectDAL,
kmsService
});
const kmsEncryptor = await kmsService.encryptWithKmsKey({ kmsId: certificateManagerKeyId });
const encryptedCertificate = await kmsEncryptor({
plainText: certificate
});
await certificateBodyDAL.create(
{
certId: certificateId,
encryptedCertificate: encryptedCertificate.cipherTextBlob
},
tx
);
const encryptedPrivateKey = await kmsEncryptor({
plainText: privateKey
});
await certificateSecretDAL.create(
{
certId: certificateId,
encryptedPrivateKey: encryptedPrivateKey.cipherTextBlob
},
tx
);
};
const processSelfSignedCertificate = async ({
certificateRequest,
template,
profile,
originalCert,
effectiveAlgorithms,
certificateDAL,
certificateBodyDAL,
certificateSecretDAL,
kmsService,
projectDAL,
tx,
isRenewal = false
}: {
certificateRequest: {
commonName?: string;
keyUsages?: CertKeyUsageType[];
extendedKeyUsages?: CertExtendedKeyUsageType[];
validity: { ttl: string };
notBefore?: Date;
notAfter?: Date;
};
template?: {
subject?: Array<{
type: string;
allowed?: string[];
required?: string[];
denied?: string[];
}>;
sans?: Array<{
type: string;
allowed?: string[];
required?: string[];
denied?: string[];
}>;
} | null;
profile?: { id: string; projectId: string } | null;
originalCert?: {
id: string;
friendlyName?: string | null;
commonName?: string | null;
projectId: string;
};
effectiveAlgorithms: {
signatureAlgorithm: CertSignatureAlgorithm;
keyAlgorithm: CertKeyAlgorithm;
};
certificateDAL: Pick<TCertificateDALFactory, "create" | "updateById">;
certificateBodyDAL: Pick<TCertificateBodyDALFactory, "create">;
certificateSecretDAL: Pick<TCertificateSecretDALFactory, "create">;
kmsService: Pick<TKmsServiceFactory, "encryptWithKmsKey" | "generateKmsKey">;
projectDAL: TProjectDALFactory;
tx: Parameters<TCertificateDALFactory["create"]>[1];
isRenewal?: boolean;
}) => {
const projectId = originalCert?.projectId || profile?.projectId;
if (!projectId) {
throw new BadRequestError({ message: "Project ID is required for certificate creation" });
}
const selfSignedResult = await generateSelfSignedCertificate({
certificateRequest,
template,
effectiveSignatureAlgorithm: effectiveAlgorithms.signatureAlgorithm,
effectiveKeyAlgorithm: effectiveAlgorithms.keyAlgorithm
});
const certificateData = await createSelfSignedCertificateRecord({
selfSignedResult,
certificateRequest,
profile,
originalCert,
certificateDAL,
tx,
isRenewal
});
await certificateDAL.updateById(
certificateData.id,
{
signatureAlgorithm: effectiveAlgorithms.signatureAlgorithm,
keyAlgorithm: effectiveAlgorithms.keyAlgorithm
},
tx
);
await createEncryptedCertificateData({
certificateId: certificateData.id,
certificate: Buffer.from(selfSignedResult.certificate),
privateKey: Buffer.from(selfSignedResult.privateKey),
projectId,
certificateBodyDAL,
certificateSecretDAL,
kmsService,
projectDAL,
tx
});
return {
selfSignedResult,
certificateData
};
};
export const certificateV3ServiceFactory = ({ export const certificateV3ServiceFactory = ({
certificateDAL, certificateDAL,
certificateBodyDAL,
certificateSecretDAL, certificateSecretDAL,
certificateAuthorityDAL, certificateAuthorityDAL,
certificateProfileDAL, certificateProfileDAL,
@@ -359,7 +754,9 @@ export const certificateV3ServiceFactory = ({
permissionService, permissionService,
certificateSyncDAL, certificateSyncDAL,
pkiSyncDAL, pkiSyncDAL,
pkiSyncQueue pkiSyncQueue,
kmsService,
projectDAL
}: TCertificateV3ServiceFactoryDep) => { }: TCertificateV3ServiceFactoryDep) => {
const issueCertificateFromProfile = async ({ const issueCertificateFromProfile = async ({
profileId, profileId,
@@ -416,15 +813,6 @@ export const certificateV3ServiceFactory = ({
}); });
} }
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId);
if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" });
}
validateCaSupport(ca, "direct certificate issuance");
validateAlgorithmCompatibility(ca, template);
const effectiveSignatureAlgorithm = certificateRequest.signatureAlgorithm as CertSignatureAlgorithm | undefined; const effectiveSignatureAlgorithm = certificateRequest.signatureAlgorithm as CertSignatureAlgorithm | undefined;
const effectiveKeyAlgorithm = certificateRequest.keyAlgorithm as CertKeyAlgorithm | undefined; const effectiveKeyAlgorithm = certificateRequest.keyAlgorithm as CertKeyAlgorithm | undefined;
@@ -440,12 +828,76 @@ export const certificateV3ServiceFactory = ({
}); });
} }
const certificateSubject = buildCertificateSubjectFromTemplate(certificateRequest, template.subject); const certificateSubject = buildCertificateSubjectFromTemplate(certificateRequest, template?.subject);
const subjectAlternativeNames = buildSubjectAlternativeNamesFromTemplate( const subjectAlternativeNames = buildSubjectAlternativeNamesFromTemplate(
{ subjectAlternativeNames: certificateRequest.altNames }, { subjectAlternativeNames: certificateRequest.altNames },
template.sans template?.sans
); );
const issuerType = profile?.issuerType || (profile?.caId ? IssuerType.CA : IssuerType.SELF_SIGNED);
if (issuerType === IssuerType.SELF_SIGNED) {
const result = await certificateDAL.transaction(async (tx) => {
const effectiveAlgorithms = getEffectiveAlgorithms(effectiveSignatureAlgorithm, effectiveKeyAlgorithm);
return processSelfSignedCertificate({
certificateRequest,
template,
profile,
effectiveAlgorithms,
certificateDAL,
certificateBodyDAL,
certificateSecretDAL,
kmsService,
projectDAL,
tx
});
});
const { selfSignedResult, certificateData } = result;
const subjectCommonName =
(selfSignedResult.certificateSubject.common_name as string) ||
certificateRequest.commonName ||
"Self-signed Certificate";
const finalRenewBeforeDays = calculateFinalRenewBeforeDays(
profile,
certificateRequest.validity.ttl,
selfSignedResult.notAfter
);
if (finalRenewBeforeDays !== undefined) {
await certificateDAL.updateById(certificateData.id, {
renewBeforeDays: finalRenewBeforeDays
});
}
return {
certificate: selfSignedResult.certificate.toString("utf8"),
issuingCaCertificate: "",
certificateChain: selfSignedResult.certificate.toString("utf8"),
privateKey: selfSignedResult.privateKey.toString("utf8"),
serialNumber: selfSignedResult.serialNumber,
certificateId: certificateData.id,
projectId: profile.projectId,
profileName: profile.slug,
commonName: subjectCommonName
};
}
if (!profile.caId) {
throw new NotFoundError({ message: "Certificate Authority ID not found" });
}
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId);
if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" });
}
validateCaSupport(ca, "direct certificate issuance");
validateAlgorithmCompatibility(ca, template);
const { certificate, certificateChain, issuingCaCertificate, privateKey, serialNumber } = const { certificate, certificateChain, issuingCaCertificate, privateKey, serialNumber } =
await internalCaService.issueCertFromCa({ await internalCaService.issueCertFromCa({
caId: ca.id, caId: ca.id,
@@ -477,10 +929,11 @@ export const certificateV3ServiceFactory = ({
new Date(cert.notAfter) new Date(cert.notAfter)
); );
await certificateDAL.updateById(cert.id, { const updateData: { profileId: string; renewBeforeDays?: number } = { profileId };
profileId, if (finalRenewBeforeDays !== undefined) {
renewBeforeDays: finalRenewBeforeDays updateData.renewBeforeDays = finalRenewBeforeDays;
}); }
await certificateDAL.updateById(cert.id, updateData);
let finalCertificateChain = bufferToString(certificateChain); let finalCertificateChain = bufferToString(certificateChain);
if (removeRootsFromChain) { if (removeRootsFromChain) {
@@ -525,6 +978,12 @@ export const certificateV3ServiceFactory = ({
enrollmentType enrollmentType
); );
if (!profile.caId) {
throw new BadRequestError({
message: "Self-signed certificates are not supported for CSR signing"
});
}
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId); const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId);
if (!ca) { if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" }); throw new NotFoundError({ message: "Certificate Authority not found" });
@@ -592,10 +1051,11 @@ export const certificateV3ServiceFactory = ({
const finalRenewBeforeDays = calculateFinalRenewBeforeDays(profile, validity.ttl, new Date(cert.notAfter)); const finalRenewBeforeDays = calculateFinalRenewBeforeDays(profile, validity.ttl, new Date(cert.notAfter));
await certificateDAL.updateById(cert.id, { const updateData2: { profileId: string; renewBeforeDays?: number } = { profileId };
profileId, if (finalRenewBeforeDays !== undefined) {
renewBeforeDays: finalRenewBeforeDays updateData2.renewBeforeDays = finalRenewBeforeDays;
}); }
await certificateDAL.updateById(cert.id, updateData2);
const certificateString = extractCertificateFromBuffer(certificate as unknown as Buffer); const certificateString = extractCertificateFromBuffer(certificate as unknown as Buffer);
let certificateChainString = extractCertificateFromBuffer(certificateChain as unknown as Buffer); let certificateChainString = extractCertificateFromBuffer(certificateChain as unknown as Buffer);
@@ -663,6 +1123,12 @@ export const certificateV3ServiceFactory = ({
}); });
} }
if (!profile.caId) {
throw new BadRequestError({
message: "Self-signed certificates are not supported for certificate ordering"
});
}
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId); const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId);
if (!ca) { if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" }); throw new NotFoundError({ message: "Certificate Authority not found" });
@@ -725,9 +1191,10 @@ export const certificateV3ServiceFactory = ({
throw new NotFoundError({ message: "Certificate not found" }); throw new NotFoundError({ message: "Certificate not found" });
} }
if (!originalCert.profileId) { const isSelfSigned = !originalCert.profileId && !originalCert.caId && originalCert.certificateTemplateId === null;
if (!originalCert.profileId && !originalCert.caId && !isSelfSigned) {
throw new ForbiddenRequestError({ throw new ForbiddenRequestError({
message: "Only certificates issued from a profile can be renewed" message: "Only certificates issued from a profile or self-signed certificates can be renewed"
}); });
} }
@@ -741,15 +1208,18 @@ export const certificateV3ServiceFactory = ({
}); });
} }
const profile = await certificateProfileDAL.findByIdWithConfigs(originalCert.profileId); let profile = null;
if (!profile) { if (originalCert.profileId) {
throw new NotFoundError({ message: "Certificate profile not found" }); profile = await certificateProfileDAL.findByIdWithConfigs(originalCert.profileId);
} if (!profile) {
throw new NotFoundError({ message: "Certificate profile not found" });
}
if (profile.enrollmentType !== EnrollmentType.API) { if (profile.enrollmentType !== EnrollmentType.API) {
throw new ForbiddenRequestError({ throw new ForbiddenRequestError({
message: "Certificate is not eligible for renewal: EST certificates cannot be renewed through this endpoint" message: "Certificate is not eligible for renewal: EST certificates cannot be renewed through this endpoint"
}); });
}
} }
const certificateSecret = await certificateSecretDAL.findOne({ certId: originalCert.id }, tx); const certificateSecret = await certificateSecretDAL.findOne({ certId: originalCert.id }, tx);
@@ -761,10 +1231,11 @@ export const certificateV3ServiceFactory = ({
} }
if (!internal) { if (!internal) {
const projectId = profile?.projectId || originalCert.projectId;
const { permission } = await permissionService.getProjectPermission({ const { permission } = await permissionService.getProjectPermission({
actor, actor,
actorId, actorId,
projectId: profile.projectId, projectId,
actorAuthMethod, actorAuthMethod,
actorOrgId, actorOrgId,
actionProjectType: ActionProjectType.CertificateManager actionProjectType: ActionProjectType.CertificateManager
@@ -776,33 +1247,46 @@ export const certificateV3ServiceFactory = ({
); );
} }
const ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(profile.caId); const issuerType = profile?.issuerType || (originalCert.caId ? IssuerType.CA : IssuerType.SELF_SIGNED);
if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" }); let ca;
if (issuerType === IssuerType.CA) {
const caId = profile?.caId || originalCert.caId;
if (!caId) {
throw new NotFoundError({ message: "Certificate Authority ID not found" });
}
ca = await certificateAuthorityDAL.findByIdWithAssociatedCa(caId);
if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found" });
}
const eligibilityCheck = validateRenewalEligibility(originalCert, ca);
if (!eligibilityCheck.isEligible) {
await certificateDAL.updateById(originalCert.id, {
renewalError: `Certificate is not eligible for renewal: ${eligibilityCheck.errors.join(", ")}`
});
throw new BadRequestError({
message: `Certificate is not eligible for renewal: ${eligibilityCheck.errors.join(", ")}`
});
}
validateCaSupport(ca, "direct certificate issuance");
} }
const eligibilityCheck = validateRenewalEligibility(originalCert, ca); const templateId = profile?.certificateTemplateId || originalCert.certificateTemplateId;
if (!eligibilityCheck.isEligible) { const template = templateId
await certificateDAL.updateById(originalCert.id, { ? await certificateTemplateV2Service.getTemplateV2ById({
renewalError: `Certificate is not eligible for renewal: ${eligibilityCheck.errors.join(", ")}` actor,
}); actorId,
throw new BadRequestError({ actorAuthMethod,
message: `Certificate is not eligible for renewal: ${eligibilityCheck.errors.join(", ")}` actorOrgId,
}); templateId,
} internal
})
: null;
validateCaSupport(ca, "direct certificate issuance"); if (!template && profile) {
const template = await certificateTemplateV2Service.getTemplateV2ById({
actor,
actorId,
actorAuthMethod,
actorOrgId,
templateId: profile.certificateTemplateId,
internal
});
if (!template) {
throw new NotFoundError({ message: "Certificate template not found for this profile" }); throw new NotFoundError({ message: "Certificate template not found for this profile" });
} }
@@ -857,10 +1341,13 @@ export const certificateV3ServiceFactory = ({
keyAlgorithm: originalCert.keyAlgorithm || undefined keyAlgorithm: originalCert.keyAlgorithm || undefined
}; };
const validationResult = await certificateTemplateV2Service.validateCertificateRequest( let validationResult: { isValid: boolean; errors: string[] } = { isValid: true, errors: [] };
profile.certificateTemplateId, if (profile?.certificateTemplateId) {
certificateRequest validationResult = await certificateTemplateV2Service.validateCertificateRequest(
); profile.certificateTemplateId,
certificateRequest
);
}
if (!validationResult.isValid) { if (!validationResult.isValid) {
await certificateDAL.updateById(originalCert.id, { await certificateDAL.updateById(originalCert.id, {
@@ -872,14 +1359,28 @@ export const certificateV3ServiceFactory = ({
}); });
} }
validateAlgorithmCompatibility(ca, template);
const notBefore = new Date(); const notBefore = new Date();
const notAfter = new Date(Date.now() + parseTtlToDays(ttl) * 24 * 60 * 60 * 1000); const notAfter = new Date(Date.now() + parseTtlToDays(ttl) * 24 * 60 * 60 * 1000);
const finalRenewBeforeDays = calculateFinalRenewBeforeDays(profile, ttl, notAfter); const finalRenewBeforeDays = profile ? calculateFinalRenewBeforeDays(profile, ttl, notAfter) : undefined;
const { certificate, certificateChain, issuingCaCertificate, serialNumber } = let certificate: string;
await internalCaService.issueCertFromCa({ let certificateChain: string;
let issuingCaCertificate: string;
let serialNumber: string;
let newCert: TCertificates;
if (issuerType === IssuerType.CA) {
// CA-signed certificate renewal
if (!ca) {
throw new NotFoundError({ message: "Certificate Authority not found for CA-signed certificate renewal" });
}
validateAlgorithmCompatibility(ca, {
algorithms: template?.algorithms
} as { algorithms?: { signature?: string[] } });
const caResult = await internalCaService.issueCertFromCa({
caId: ca.id, caId: ca.id,
friendlyName: originalCert.friendlyName || originalCert.commonName || "Renewed Certificate", friendlyName: originalCert.friendlyName || originalCert.commonName || "Renewed Certificate",
commonName: originalCert.commonName || "", commonName: originalCert.commonName || "",
@@ -900,20 +1401,72 @@ export const certificateV3ServiceFactory = ({
tx tx
}); });
const newCert = await certificateDAL.findOne({ serialNumber, caId: ca.id }, tx); certificate = caResult.certificate;
certificateChain = caResult.certificateChain;
issuingCaCertificate = caResult.issuingCaCertificate;
serialNumber = caResult.serialNumber;
const foundCert = await certificateDAL.findOne({ serialNumber, caId: ca.id }, tx);
if (!foundCert) {
throw new NotFoundError({ message: "Certificate was signed but could not be found in database" });
}
newCert = foundCert;
} else {
// Self-signed certificate renewal
const effectiveAlgorithms = getEffectiveAlgorithms(
undefined,
undefined,
originalSignatureAlgorithm,
originalKeyAlgorithm
);
const selfSignedRenewalResult = await processSelfSignedCertificate({
certificateRequest,
template,
profile,
originalCert,
effectiveAlgorithms,
certificateDAL,
certificateBodyDAL,
certificateSecretDAL,
kmsService,
projectDAL,
tx,
isRenewal: true
});
certificate = selfSignedRenewalResult.selfSignedResult.certificate.toString("utf8");
certificateChain = selfSignedRenewalResult.selfSignedResult.certificate.toString("utf8"); // Self-signed has no chain
issuingCaCertificate = ""; // No issuing CA for self-signed
serialNumber = selfSignedRenewalResult.selfSignedResult.serialNumber;
newCert = selfSignedRenewalResult.certificateData;
}
if (!newCert) { if (!newCert) {
throw new NotFoundError({ message: "Certificate was signed but could not be found in database" }); throw new NotFoundError({ message: "Certificate was signed but could not be found in database" });
} }
await certificateDAL.updateById( // For self-signed certificates, we already set the renewal data during creation
newCert.id, // For CA-signed certificates, we need to set it now
{ if (issuerType === IssuerType.CA) {
profileId: originalCert.profileId, const renewalUpdateData: {
renewBeforeDays: finalRenewBeforeDays, profileId: string | null;
renewedFromCertificateId: string;
renewBeforeDays?: number;
} = {
profileId: originalCert.profileId || null,
renewedFromCertificateId: originalCert.id renewedFromCertificateId: originalCert.id
}, };
tx
); if (finalRenewBeforeDays !== undefined) {
renewalUpdateData.renewBeforeDays = finalRenewBeforeDays;
}
await certificateDAL.updateById(newCert.id, renewalUpdateData, tx);
} else if (finalRenewBeforeDays !== undefined) {
// For self-signed certificates, just update the renewBeforeDays if needed
await certificateDAL.updateById(newCert.id, { renewBeforeDays: finalRenewBeforeDays }, tx);
}
await certificateDAL.updateById( await certificateDAL.updateById(
originalCert.id, originalCert.id,
@@ -953,8 +1506,8 @@ export const certificateV3ServiceFactory = ({
certificateChain: finalCertificateChain, certificateChain: finalCertificateChain,
serialNumber: renewalResult.serialNumber, serialNumber: renewalResult.serialNumber,
certificateId: renewalResult.newCert.id, certificateId: renewalResult.newCert.id,
projectId: renewalResult.profile.projectId, projectId: renewalResult.originalCert.projectId,
profileName: renewalResult.profile.slug, profileName: renewalResult.profile?.slug || "Self-signed Certificate",
commonName: renewalResult.originalCert.commonName || "" commonName: renewalResult.originalCert.commonName || ""
}; };
}; };
@@ -309,6 +309,14 @@ export const certificateServiceFactory = ({
const certBody = await certificateBodyDAL.findOne({ certId: cert.id }); const certBody = await certificateBodyDAL.findOne({ certId: cert.id });
if (!certBody) {
throw new NotFoundError({ message: "Certificate body not found" });
}
if (!certBody.encryptedCertificate) {
throw new BadRequestError({ message: "Certificate data not available" });
}
const certificateManagerKeyId = await getProjectKmsCertificateKeyId({ const certificateManagerKeyId = await getProjectKmsCertificateKeyId({
projectId: cert.projectId, projectId: cert.projectId,
projectDAL, projectDAL,
@@ -599,6 +607,14 @@ export const certificateServiceFactory = ({
const certBody = await certificateBodyDAL.findOne({ certId: cert.id }); const certBody = await certificateBodyDAL.findOne({ certId: cert.id });
if (!certBody) {
throw new NotFoundError({ message: "Certificate body not found" });
}
if (!certBody.encryptedCertificate) {
throw new BadRequestError({ message: "Certificate data not available" });
}
const certificateManagerKeyId = await getProjectKmsCertificateKeyId({ const certificateManagerKeyId = await getProjectKmsCertificateKeyId({
projectId: cert.projectId, projectId: cert.projectId,
projectDAL, projectDAL,
@@ -1,11 +1,12 @@
export type TCertificateProfile = { export type TCertificateProfile = {
id: string; id: string;
projectId: string; projectId: string;
caId: string; caId: string | null;
certificateTemplateId: string; certificateTemplateId: string;
slug: string; slug: string;
description?: string; description?: string;
enrollmentType: "api" | "est" | "acme"; enrollmentType: "api" | "est" | "acme";
issuerType: "ca" | "self-signed";
estConfigId?: string; estConfigId?: string;
apiConfigId?: string; apiConfigId?: string;
createdAt: string; createdAt: string;
@@ -44,11 +45,12 @@ export type TCertificateProfileWithDetails = TCertificateProfile & {
export type TCreateCertificateProfileDTO = { export type TCreateCertificateProfileDTO = {
projectId: string; projectId: string;
caId: string; caId?: string;
certificateTemplateId: string; certificateTemplateId: string;
slug: string; slug: string;
description?: string; description?: string;
enrollmentType: "api" | "est" | "acme"; enrollmentType: "api" | "est" | "acme";
issuerType: "ca" | "self-signed";
estConfig?: { estConfig?: {
disableBootstrapCaValidation?: boolean; disableBootstrapCaValidation?: boolean;
passphrase: string; passphrase: string;
@@ -65,6 +67,8 @@ export type TUpdateCertificateProfileDTO = {
profileId: string; profileId: string;
slug?: string; slug?: string;
description?: string; description?: string;
enrollmentType?: "api" | "est" | "acme";
issuerType?: "ca" | "self-signed";
estConfig?: { estConfig?: {
disableBootstrapCaValidation?: boolean; disableBootstrapCaValidation?: boolean;
passphrase?: string; passphrase?: string;
@@ -88,6 +92,8 @@ export type TListCertificateProfilesDTO = {
search?: string; search?: string;
includeConfigs?: boolean; includeConfigs?: boolean;
enrollmentType?: "api" | "est" | "acme"; enrollmentType?: "api" | "est" | "acme";
issuerType?: "ca" | "self-signed";
caId?: string;
}; };
export type TGetCertificateProfileByIdDTO = { export type TGetCertificateProfileByIdDTO = {
@@ -416,7 +416,7 @@ export const CertificatesTable = ({ handlePopUpOpen }: Props) => {
{/* Manual renewal action for profile-issued certificates that are not revoked/expired (including failed ones) */} {/* Manual renewal action for profile-issued certificates that are not revoked/expired (including failed ones) */}
{(() => { {(() => {
const canRenew = const canRenew =
certificate.profileId && (certificate.profileId || certificate.caId) &&
certificate.hasPrivateKey !== false && certificate.hasPrivateKey !== false &&
!certificate.renewedByCertificateId && !certificate.renewedByCertificateId &&
!isRevoked && !isRevoked &&
@@ -47,7 +47,8 @@ const createSchema = z
.max(1000, "Description must be less than 1000 characters") .max(1000, "Description must be less than 1000 characters")
.optional(), .optional(),
enrollmentType: z.enum(["api", "est", "acme"]), enrollmentType: z.enum(["api", "est", "acme"]),
certificateAuthorityId: z.string().min(1, "Certificate Authority is required"), issuerType: z.enum(["ca", "self-signed"]),
certificateAuthorityId: z.string().nullable().optional(),
certificateTemplateId: z.string().min(1, "Certificate Template is required"), certificateTemplateId: z.string().min(1, "Certificate Template is required"),
estConfig: z estConfig: z
.object({ .object({
@@ -87,10 +88,21 @@ const createSchema = z
if (data.enrollmentType === "acme" && !data.acmeConfig) { if (data.enrollmentType === "acme" && !data.acmeConfig) {
return false; return false;
} }
if (data.issuerType === "ca" && !data.certificateAuthorityId) {
return false;
}
if (data.issuerType === "self-signed" && data.certificateAuthorityId) {
return false;
}
if (data.issuerType === "self-signed" && data.enrollmentType !== "api") {
return false;
}
return true; return true;
}, },
{ {
message: "Configuration is required for selected enrollment type" message: "Configuration is required for selected enrollment type and issuer type"
} }
); );
@@ -111,7 +123,8 @@ const editSchema = z
.max(1000, "Description must be less than 1000 characters") .max(1000, "Description must be less than 1000 characters")
.optional(), .optional(),
enrollmentType: z.enum(["api", "est", "acme"]), enrollmentType: z.enum(["api", "est", "acme"]),
certificateAuthorityId: z.string().optional(), issuerType: z.enum(["ca", "self-signed"]),
certificateAuthorityId: z.string().nullable().optional(),
certificateTemplateId: z.string().optional(), certificateTemplateId: z.string().optional(),
estConfig: z estConfig: z
.object({ .object({
@@ -139,10 +152,22 @@ const editSchema = z
if (data.enrollmentType === "acme" && !data.acmeConfig) { if (data.enrollmentType === "acme" && !data.acmeConfig) {
return false; return false;
} }
if (data.issuerType === "ca" && !data.certificateAuthorityId) {
return false;
}
if (data.issuerType === "self-signed" && data.certificateAuthorityId) {
return false;
}
if (data.issuerType === "self-signed" && data.enrollmentType !== "api") {
return false;
}
return true; return true;
}, },
{ {
message: "Configuration is required for selected enrollment type" message:
"Configuration is required for selected enrollment type and issuer type. CA issuer requires a certificate authority. Self-signed issuer cannot have a certificate authority and only supports API enrollment."
} }
); );
@@ -193,7 +218,8 @@ export const CreateProfileModal = ({
slug: profile.slug, slug: profile.slug,
description: profile.description || "", description: profile.description || "",
enrollmentType: profile.enrollmentType, enrollmentType: profile.enrollmentType,
certificateAuthorityId: profile.caId, issuerType: profile.issuerType,
certificateAuthorityId: profile.caId || undefined,
certificateTemplateId: profile.certificateTemplateId, certificateTemplateId: profile.certificateTemplateId,
estConfig: estConfig:
profile.enrollmentType === "est" profile.enrollmentType === "est"
@@ -217,6 +243,7 @@ export const CreateProfileModal = ({
slug: "", slug: "",
description: "", description: "",
enrollmentType: "api", enrollmentType: "api",
issuerType: "ca",
certificateAuthorityId: "", certificateAuthorityId: "",
certificateTemplateId: "", certificateTemplateId: "",
apiConfig: { apiConfig: {
@@ -228,6 +255,7 @@ export const CreateProfileModal = ({
}); });
const watchedEnrollmentType = watch("enrollmentType"); const watchedEnrollmentType = watch("enrollmentType");
const watchedIssuerType = watch("issuerType");
const watchedDisableBootstrapValidation = watch("estConfig.disableBootstrapCaValidation"); const watchedDisableBootstrapValidation = watch("estConfig.disableBootstrapCaValidation");
const watchedAutoRenew = watch("apiConfig.autoRenew"); const watchedAutoRenew = watch("apiConfig.autoRenew");
@@ -237,7 +265,8 @@ export const CreateProfileModal = ({
slug: profile.slug, slug: profile.slug,
description: profile.description || "", description: profile.description || "",
enrollmentType: profile.enrollmentType, enrollmentType: profile.enrollmentType,
certificateAuthorityId: profile.caId, issuerType: profile.issuerType,
certificateAuthorityId: profile.caId || undefined,
certificateTemplateId: profile.certificateTemplateId, certificateTemplateId: profile.certificateTemplateId,
estConfig: estConfig:
profile.enrollmentType === "est" profile.enrollmentType === "est"
@@ -276,7 +305,8 @@ export const CreateProfileModal = ({
const updateData: TUpdateCertificateProfileDTO = { const updateData: TUpdateCertificateProfileDTO = {
profileId: profile.id, profileId: profile.id,
slug: data.slug, slug: data.slug,
description: data.description description: data.description,
issuerType: data.issuerType
}; };
if (data.enrollmentType === "est" && data.estConfig) { if (data.enrollmentType === "est" && data.estConfig) {
@@ -298,7 +328,9 @@ export const CreateProfileModal = ({
slug: data.slug, slug: data.slug,
description: data.description, description: data.description,
enrollmentType: data.enrollmentType, enrollmentType: data.enrollmentType,
caId: data.certificateAuthorityId, issuerType: data.issuerType,
caId:
data.issuerType === "self-signed" ? undefined : data.certificateAuthorityId || undefined,
certificateTemplateId: data.certificateTemplateId certificateTemplateId: data.certificateTemplateId
}; };
@@ -372,34 +404,73 @@ export const CreateProfileModal = ({
<Controller <Controller
control={control} control={control}
name="certificateAuthorityId" name="issuerType"
render={({ field: { onChange, ...field }, fieldState: { error } }) => ( render={({ field: { onChange, ...field }, fieldState: { error } }) => (
<FormControl <FormControl
label="Issuing CA" label="Issuer Type"
isRequired isRequired
isError={Boolean(error)} isError={Boolean(error)}
errorText={error?.message} errorText={error?.message}
> >
<Select <Select
{...field} {...field}
onValueChange={onChange} onValueChange={(value) => {
placeholder="Select a certificate authority" if (value === "self-signed") {
setValue("certificateAuthorityId", "");
setValue("enrollmentType", "api");
setValue("apiConfig", {
autoRenew: false,
renewBeforeDays: 30
});
setValue("estConfig", undefined);
setValue("acmeConfig", undefined);
}
onChange(value);
}}
className="w-full" className="w-full"
position="popper" position="popper"
isDisabled={Boolean(isEdit)} isDisabled={Boolean(isEdit)}
> >
{certificateAuthorities.map((ca) => ( <SelectItem value="ca">Certificate Authority</SelectItem>
<SelectItem key={ca.id} value={ca.id}> <SelectItem value="self-signed">Self-signed</SelectItem>
{ca.type === "internal" && ca.configuration.friendlyName
? ca.configuration.friendlyName
: ca.name}
</SelectItem>
))}
</Select> </Select>
</FormControl> </FormControl>
)} )}
/> />
{watchedIssuerType === "ca" && (
<Controller
control={control}
name="certificateAuthorityId"
render={({ field: { onChange, value, ...field }, fieldState: { error } }) => (
<FormControl
label="Issuing CA"
isRequired
isError={Boolean(error)}
errorText={error?.message}
>
<Select
{...field}
value={value || undefined}
onValueChange={onChange}
placeholder="Select a certificate authority"
className="w-full"
position="popper"
isDisabled={Boolean(isEdit)}
>
{certificateAuthorities.map((ca) => (
<SelectItem key={ca.id} value={ca.id}>
{ca.type === "internal" && ca.configuration.friendlyName
? ca.configuration.friendlyName
: ca.name}
</SelectItem>
))}
</Select>
</FormControl>
)}
/>
)}
<Controller <Controller
control={control} control={control}
name="certificateTemplateId" name="certificateTemplateId"
@@ -488,8 +559,12 @@ export const CreateProfileModal = ({
isDisabled={Boolean(isEdit)} isDisabled={Boolean(isEdit)}
> >
<SelectItem value="api">API</SelectItem> <SelectItem value="api">API</SelectItem>
<SelectItem value="est">EST</SelectItem> <SelectItem value="est" isDisabled={watchedIssuerType === "self-signed"}>
<SelectItem value="acme">ACME</SelectItem> EST
</SelectItem>
<SelectItem value="acme" isDisabled={watchedIssuerType === "self-signed"}>
ACME
</SelectItem>
</Select> </Select>
</FormControl> </FormControl>
)} )}
@@ -47,6 +47,7 @@ export const ProfileList = ({
<Tr> <Tr>
<Th>Name</Th> <Th>Name</Th>
<Th>Enrollment Method</Th> <Th>Enrollment Method</Th>
<Th>Issuer Type</Th>
<Th>Issuing CA</Th> <Th>Issuing CA</Th>
<Th>Certificate Template</Th> <Th>Certificate Template</Th>
<Th className="w-5" /> <Th className="w-5" />
@@ -54,7 +55,7 @@ export const ProfileList = ({
</THead> </THead>
<TBody> <TBody>
<Tr> <Tr>
<Td colSpan={6}> <Td colSpan={7}>
<EmptyState title="No Project Selected" /> <EmptyState title="No Project Selected" />
</Td> </Td>
</Tr> </Tr>
@@ -71,6 +72,7 @@ export const ProfileList = ({
<Tr> <Tr>
<Th>Name</Th> <Th>Name</Th>
<Th>Enrollment Method</Th> <Th>Enrollment Method</Th>
<Th>Issuer Type</Th>
<Th>Issuing CA</Th> <Th>Issuing CA</Th>
<Th>Certificate Template</Th> <Th>Certificate Template</Th>
<Th className="w-5" /> <Th className="w-5" />
@@ -49,7 +49,7 @@ export const ProfileRow = ({
}: Props) => { }: Props) => {
const { permission } = useProjectPermission(); const { permission } = useProjectPermission();
const { data: caData } = useGetCaById(profile.caId); const { data: caData } = useGetCaById(profile.caId || "");
const { popUp, handlePopUpToggle } = usePopUp(["issueCertificate"] as const); const { popUp, handlePopUpToggle } = usePopUp(["issueCertificate"] as const);
@@ -106,6 +106,20 @@ export const ProfileRow = ({
return <Badge variant={variant}>{label}</Badge>; return <Badge variant={variant}>{label}</Badge>;
}; };
const getIssuerTypeBadge = (issuerType: string) => {
const config = {
ca: { variant: "success" as const, label: "CA" },
"self-signed": { variant: "info" as const, label: "Self-Signed" }
} as const;
const configKey = Object.keys(config).includes(issuerType)
? (issuerType as keyof typeof config)
: "ca";
const { variant, label } = config[configKey];
return <Badge variant={variant}>{label}</Badge>;
};
return ( return (
<Tr key={profile.id} className="h-10 transition-colors duration-100 hover:bg-mineshaft-700"> <Tr key={profile.id} className="h-10 transition-colors duration-100 hover:bg-mineshaft-700">
<Td> <Td>
@@ -119,9 +133,12 @@ export const ProfileRow = ({
</div> </div>
</Td> </Td>
<Td className="text-start">{getEnrollmentTypeBadge(profile.enrollmentType)}</Td> <Td className="text-start">{getEnrollmentTypeBadge(profile.enrollmentType)}</Td>
<Td className="text-start">{getIssuerTypeBadge(profile.issuerType)}</Td>
<Td className="text-start"> <Td className="text-start">
<span className="text-sm text-mineshaft-300"> <span className="text-sm text-mineshaft-300">
{caData?.friendlyName || caData?.commonName || profile.caId} {profile.issuerType === "self-signed"
? "—"
: caData?.friendlyName || caData?.commonName || profile.caId}
</span> </span>
</Td> </Td>
<Td> <Td>