Skip to content

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

Merge remote-tracking branch 'origin/main' into feat/PKI-60

Carlos Monastyrski53d38413c10a
diff --cc backend/src/server/routes/v1/certificate-policy-router.ts
index 5685f944c4,a5c466569f..d872288774
--- a/backend/src/server/routes/v1/certificate-policy-router.ts
+++ b/backend/src/server/routes/v1/certificate-policy-router.ts
@@@ -53,32 -53,9 +54,32 @@@ const policyKeyUsagesSchema =
{
message: "Key usages must have at least one allowed, required, or denied value"
}
+ )
+ .refine(
+ (data) => {
+ if (!data.denied || data.denied.length === 0) {
+ return true;
+ }
+ const deniedSet = new Set(data.denied);
+ const allowedSet = data.allowed ? new Set(data.allowed) : new Set();
+ const requiredSet = data.required ? new Set(data.required) : new Set();
+
+ for (const deniedValue of deniedSet) {
+ if (allowedSet.has(deniedValue)) {
+ return false;
+ }
+ if (requiredSet.has(deniedValue)) {
+ return false;
+ }
+ }
+ return true;
+ },
+ {
+ message: "A key usage cannot be both denied and present in the allowed or required arrays."
+ }
);
- const templateV2ExtendedKeyUsagesSchema = z
+ const policyExtendedKeyUsagesSchema = z
.object({
allowed: z.array(z.nativeEnum(CertExtendedKeyUsageType)).optional(),
required: z.array(z.nativeEnum(CertExtendedKeyUsageType)).optional(),
@@@ -94,32 -71,9 +95,32 @@@
{
message: "Extended key usages must have at least one allowed, required, or denied value"
}
+ )
+ .refine(
+ (data) => {
+ if (!data.denied || data.denied.length === 0) {
+ return true;
+ }
+ const deniedSet = new Set(data.denied);
+ const allowedSet = data.allowed ? new Set(data.allowed) : new Set();
+ const requiredSet = data.required ? new Set(data.required) : new Set();
+
+ for (const deniedValue of deniedSet) {
+ if (allowedSet.has(deniedValue)) {
+ return false;
+ }
+ if (requiredSet.has(deniedValue)) {
+ return false;
+ }
+ }
+ return true;
+ },
+ {
+ message: "An extended key usage cannot be both denied and present in the allowed or required arrays."
+ }
);
- const templateV2SanSchema = z
+ const policySanSchema = z
.object({
type: sanTypeSchema,
allowed: z.array(z.string()).optional(),
@@@ -162,30 -116,43 +163,43 @@@ const policyAlgorithmsSchema = z.object
keyAlgorithm: z.array(z.string()).min(1, "At least one key algorithm must be provided").optional()
});
- const createCertificateTemplateV2Schema = z.object({
+ const policyBasicConstraintsSchema = z
+ .object({
+ isCA: z.nativeEnum(CertPolicyState).optional(),
+ maxPathLength: z
+ .number()
+ .int("Path length must be an integer")
+ .min(-1, "Path length must be -1 (unlimited) or a non-negative integer")
+ .optional()
+ })
+ .nullable();
+
+ const createCertificatePolicySchema = z.object({
projectId: z.string().min(1),
- name: z.string().min(1).max(255, "Name must be between 1 and 255 characters"),
+ name: slugSchema({ min: 1, max: 255, field: "Name" }),
description: z.string().max(1000).optional(),
- subject: z.array(templateV2SubjectSchema).optional(),
- sans: z.array(templateV2SanSchema).optional(),
- keyUsages: templateV2KeyUsagesSchema.optional(),
- extendedKeyUsages: templateV2ExtendedKeyUsagesSchema.optional(),
- algorithms: templateV2AlgorithmsSchema.optional(),
- validity: templateV2ValiditySchema.optional()
+ subject: z.array(policySubjectSchema).optional(),
+ sans: z.array(policySanSchema).optional(),
+ keyUsages: policyKeyUsagesSchema.optional(),
+ extendedKeyUsages: policyExtendedKeyUsagesSchema.optional(),
+ algorithms: policyAlgorithmsSchema.optional(),
+ validity: policyValiditySchema.optional(),
+ basicConstraints: policyBasicConstraintsSchema.optional()
});
- const updateCertificateTemplateV2Schema = z.object({
- name: slugSchema({ min: 1, max: 255, field: "Name" }).optional(),
+ const updateCertificatePolicySchema = z.object({
+ name: z.string().min(1).max(255, "Name must be between 1 and 255 characters").optional(),
description: z.string().max(1000).optional(),
- subject: z.array(templateV2SubjectSchema).optional(),
- sans: z.array(templateV2SanSchema).optional(),
- keyUsages: templateV2KeyUsagesSchema.optional(),
- extendedKeyUsages: templateV2ExtendedKeyUsagesSchema.optional(),
- algorithms: templateV2AlgorithmsSchema.optional(),
- validity: templateV2ValiditySchema.optional()
+ subject: z.array(policySubjectSchema).optional(),
+ sans: z.array(policySanSchema).optional(),
+ keyUsages: policyKeyUsagesSchema.optional(),
+ extendedKeyUsages: policyExtendedKeyUsagesSchema.optional(),
+ algorithms: policyAlgorithmsSchema.optional(),
+ validity: policyValiditySchema.optional(),
+ basicConstraints: policyBasicConstraintsSchema.optional()
});
- export const registerCertificateTemplateRouter = async (server: FastifyZodProvider) => {
+ export const registerCertificatePolicyRouter = async (server: FastifyZodProvider) => {
server.route({
method: "POST",
url: "/",
diff --cc backend/src/services/certificate-authority/internal/internal-certificate-authority-types.ts
index 4adb16a063,605dc10d9a..957812075f
--- a/backend/src/services/certificate-authority/internal/internal-certificate-authority-types.ts
+++ b/backend/src/services/certificate-authority/internal/internal-certificate-authority-types.ts
@@@ -114,61 -108,26 +115,66 @@@ export type TGetCaCertDTO =
caId: string;
} & Omit<TProjectPermission, "projectId">;
-export type TSignIntermediateDTO = {
+export type TGetCaCertByIdDTO = {
caId: string;
- csr: string;
- notBefore?: string;
- notAfter: string;
- maxPathLength: number;
+ certId: string;
} & Omit<TProjectPermission, "projectId">;
+export type TSignIntermediateDTO = {
+ isInternal?: boolean;
+} & (
+ | {
+ isInternal: true;
+ caId: string;
+ csr: string;
+ notBefore?: string;
+ notAfter: string;
+ maxPathLength: number;
+ actorId?: undefined;
+ actorAuthMethod?: undefined;
+ actor?: undefined;
+ actorOrgId?: undefined;
+ }
+ | ({
+ isInternal?: false;
+ caId: string;
+ csr: string;
+ notBefore?: string;
+ notAfter: string;
+ maxPathLength: number;
+ } & Omit<TProjectPermission, "projectId">)
+);
+
export type TImportCertToCaDTO = {
- caId: string;
- certificate: string;
- certificateChain: string;
-} & Omit<TProjectPermission, "projectId">;
+ isInternal?: boolean;
+ parentCaId?: string;
+} & (
+ | {
+ isInternal: true;
+ caId: string;
+ certificate: string;
+ certificateChain: string;
+ parentCaId?: string | undefined;
+ actorId?: undefined;
+ actorAuthMethod?: undefined;
+ actor?: undefined;
+ actorOrgId?: undefined;
+ }
+ | ({
+ isInternal?: false;
+ caId: string;
+ certificate: string;
+ certificateChain: string;
+ parentCaId?: string;
+ } & Omit<TProjectPermission, "projectId">)
+);
- export type TIssueCertFromCaDTO = {
+ export type TBasicConstraints = {
+ isCA: boolean;
+ pathLength?: number;
+ } | null;
+
+ type TIssueCertFromCaBaseDTO = {
caId?: string;
certificateTemplateId?: string;
pkiCollectionId?: string;
diff --cc backend/src/services/certificate-v3/certificate-v3-service.ts
index a62efa9449,d8a6204e8f..4ecbb14e25
--- a/backend/src/services/certificate-v3/certificate-v3-service.ts
+++ b/backend/src/services/certificate-v3/certificate-v3-service.ts
@@@ -1,7 -1,5 +1,6 @@@
import { ForbiddenError, subject } from "@casl/ability";
- import * as x509 from "@peculiar/x509";
import { randomUUID } from "crypto";
+import RE2 from "re2";
import { ActionProjectType, TCertificates } from "@app/db/schemas";
import { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";
@@@ -56,6 -57,17 +58,16 @@@ import
extractAlgorithmsFromCSR,
extractCertificateRequestFromCSR
} from "../certificate-common/certificate-csr-utils";
+ import {
+ calculateFinalRenewBeforeDays,
+ detectSanType,
+ extractCertificateFromBuffer,
+ generateSelfSignedCertificate,
+ getEffectiveAlgorithms,
+ isValidRenewalTiming,
- parseTtlToDays,
+ validateAlgorithmCompatibility,
+ validateCaSupport
+ } from "../certificate-common/certificate-issuance-utils";
import {
bufferToString,
buildCertificateSubjectFromTemplate,
@@@ -745,9 -467,9 +467,32 @@@ const createEncryptedCertificateData =
);
};
++const parseTtlToMs = (ttl: string): number => {
++ const regex = new RE2("^(\\d+)(s|m|h|d)$");
++ const match = regex.exec(ttl);
++
++ if (!match) throw new Error(`Invalid TTL format: ${ttl}`);
++
++ const value = parseInt(match[1], 10);
++ const unit = match[2];
++
++ switch (unit) {
++ case "s":
++ return value * 1000;
++ case "m":
++ return value * 60 * 1000;
++ case "h":
++ return value * 60 * 60 * 1000;
++ case "d":
++ return value * 24 * 60 * 60 * 1000;
++ default:
++ throw new Error(`Unknown TTL unit: ${unit}`);
++ }
++};
++
const processSelfSignedCertificate = async ({
certificateRequest,
- template,
+ policy,
profile,
originalCert,
effectiveAlgorithms,
@@@ -1608,27 -1785,14 +1808,27 @@@ export const certificateV3ServiceFactor
})
: null;
- if (!template && profile) {
- throw new NotFoundError({ message: "Certificate template not found for this profile" });
+ if (!policy && profile) {
+ throw new NotFoundError({ message: "Certificate policy not found for this profile" });
}
- const originalTtlInDays = Math.ceil(
- (new Date(originalCert.notAfter).getTime() - new Date(originalCert.notBefore).getTime()) / (1000 * 60 * 60 * 24)
- );
- const ttl = `${originalTtlInDays}d`;
+ const getSimpleTtl = (startDate: Date, endDate: Date): string => {
+ const diffMs = endDate.getTime() - startDate.getTime();
+
+ const days = Math.floor(diffMs / (1000 * 60 * 60 * 24));
+ if (days > 0) return `${days}d`;
+
+ const hours = Math.floor(diffMs / (1000 * 60 * 60));
+ if (hours > 0) return `${hours}h`;
+
+ const minutes = Math.floor(diffMs / (1000 * 60));
+ if (minutes > 0) return `${minutes}m`;
+
+ const seconds = Math.floor(diffMs / 1000);
+ return `${seconds}s`;
+ };
+
+ const ttl = getSimpleTtl(originalCert.notBefore, originalCert.notAfter);
const certificateRequest = {
commonName: originalCert.commonName || undefined,