Skip to content

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

Allow import of a certificate without a private key

Carlos Monastyrskif3852229f6f5
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 78e1ad9148..40a521477d 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -2198,15 +2198,16 @@ export const CERTIFICATES = {
IMPORT: {
projectSlug: "Slug of the project to import the certificate into.",
certificatePem: "The PEM-encoded leaf certificate.",
- privateKeyPem: "The PEM-encoded private key corresponding to the certificate.",
+ privateKeyPem: "Optional PEM-encoded private key associated with the certificate.",
chainPem: "The PEM-encoded chain of intermediate certificates.",
friendlyName: "A friendly name for the certificate.",
pkiCollectionId: "The ID of the PKI collection to add the certificate to.",
- certificate: "The issued certificate.",
- certificateChain: "The certificate chain of the issued certificate.",
- privateKey: "The private key of the issued certificate.",
- serialNumber: "The serial number of the issued certificate."
+ certificate: "The imported certificate.",
+ certificateChain: "The certificate chain associated with the imported certificate.",
+ privateKey:
+ "The PEM-encoded private key associated with the imported certificate. Returned only when a private key was supplied at import.",
+ serialNumber: "The serial number of the imported certificate."
}
};
diff --git a/backend/src/server/routes/v1/certificate-router.ts b/backend/src/server/routes/v1/certificate-router.ts
index 0457e13e08..2b7aacf761 100644
--- a/backend/src/server/routes/v1/certificate-router.ts
+++ b/backend/src/server/routes/v1/certificate-router.ts
@@ -1465,7 +1465,7 @@ export const registerCertificateRouter = async (server: FastifyZodProvider) => {
description: "Import certificate",
body: z.object({
certificatePem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.certificatePem),
- privateKeyPem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.privateKeyPem),
+ privateKeyPem: z.string().trim().min(1).optional().describe(CERTIFICATES.IMPORT.privateKeyPem),
chainPem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.chainPem),
friendlyName: z.string().trim().optional().describe(CERTIFICATES.IMPORT.friendlyName),
@@ -1476,7 +1476,7 @@ export const registerCertificateRouter = async (server: FastifyZodProvider) => {
200: z.object({
certificate: z.string().trim().describe(CERTIFICATES.IMPORT.certificate),
certificateChain: z.string().trim().describe(CERTIFICATES.IMPORT.certificateChain),
- privateKey: z.string().trim().describe(CERTIFICATES.IMPORT.privateKey),
+ privateKey: z.string().trim().optional().describe(CERTIFICATES.IMPORT.privateKey),
serialNumber: z.string().trim().describe(CERTIFICATES.IMPORT.serialNumber)
})
}
diff --git a/backend/src/server/routes/v1/deprecated-certificate-router.ts b/backend/src/server/routes/v1/deprecated-certificate-router.ts
index 955407e4cc..72cfba52d6 100644
--- a/backend/src/server/routes/v1/deprecated-certificate-router.ts
+++ b/backend/src/server/routes/v1/deprecated-certificate-router.ts
@@ -301,7 +301,7 @@ export const registerDeprecatedCertRouter = async (server: FastifyZodProvider) =
projectSlug: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.projectSlug),
certificatePem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.certificatePem),
- privateKeyPem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.privateKeyPem),
+ privateKeyPem: z.string().trim().min(1).optional().describe(CERTIFICATES.IMPORT.privateKeyPem),
chainPem: z.string().trim().min(1).describe(CERTIFICATES.IMPORT.chainPem),
friendlyName: z.string().trim().optional().describe(CERTIFICATES.IMPORT.friendlyName),
@@ -311,7 +311,7 @@ export const registerDeprecatedCertRouter = async (server: FastifyZodProvider) =
200: z.object({
certificate: z.string().trim().describe(CERTIFICATES.IMPORT.certificate),
certificateChain: z.string().trim().describe(CERTIFICATES.IMPORT.certificateChain),
- privateKey: z.string().trim().describe(CERTIFICATES.IMPORT.privateKey),
+ privateKey: z.string().trim().optional().describe(CERTIFICATES.IMPORT.privateKey),
serialNumber: z.string().trim().describe(CERTIFICATES.IMPORT.serialNumber)
})
}
diff --git a/backend/src/services/certificate/certificate-service.ts b/backend/src/services/certificate/certificate-service.ts
index 0b3af7e71d..89d869e3ba 100644
--- a/backend/src/services/certificate/certificate-service.ts
+++ b/backend/src/services/certificate/certificate-service.ts
@@ -764,28 +764,30 @@ export const certificateServiceFactory = ({
});
}
- // Verify private key matches the certificate
- let privateKey;
- try {
- privateKey = crypto.nativeCrypto.createPrivateKey(privateKeyPem);
- } catch (err) {
- throw new BadRequestError({ message: "Invalid private key format" });
- }
+ // Verify private key matches the certificate when one is provided
+ if (privateKeyPem) {
+ let privateKey;
+ try {
+ privateKey = crypto.nativeCrypto.createPrivateKey(privateKeyPem);
+ } catch (err) {
+ throw new BadRequestError({ message: "Invalid private key format" });
+ }
- try {
- const message = Buffer.from(Buffer.alloc(32));
- const publicKey = crypto.nativeCrypto.createPublicKey(certificatePem);
- const signature = crypto.nativeCrypto.sign(null, message, privateKey);
- const isValid = crypto.nativeCrypto.verify(null, message, publicKey, signature);
+ try {
+ const message = Buffer.from(Buffer.alloc(32));
+ const publicKey = crypto.nativeCrypto.createPublicKey(certificatePem);
+ const signature = crypto.nativeCrypto.sign(null, message, privateKey);
+ const isValid = crypto.nativeCrypto.verify(null, message, publicKey, signature);
- if (!isValid) {
- throw new BadRequestError({ message: "Private key does not match certificate" });
- }
- } catch (err) {
- if (err instanceof BadRequestError) {
- throw err;
+ if (!isValid) {
+ throw new BadRequestError({ message: "Private key does not match certificate" });
+ }
+ } catch (err) {
+ if (err instanceof BadRequestError) {
+ throw err;
+ }
+ throw new BadRequestError({ message: "Error verifying private key against certificate" });
}
- throw new BadRequestError({ message: "Error verifying private key against certificate" });
}
// Get certificate attributes
@@ -814,9 +816,9 @@ export const certificateServiceFactory = ({
plainText: Buffer.from(certificatePem)
});
- const { cipherTextBlob: encryptedPrivateKey } = await kmsEncryptor({
- plainText: Buffer.from(privateKeyPem)
- });
+ const encryptedPrivateKey = privateKeyPem
+ ? (await kmsEncryptor({ plainText: Buffer.from(privateKeyPem) })).cipherTextBlob
+ : null;
// Extract Key Usage
const keyUsagesExt = leafCert.getExtension("2.5.29.15") as x509.KeyUsagesExtension;
@@ -872,13 +874,15 @@ export const certificateServiceFactory = ({
tx
);
- await certificateSecretDAL.create(
- {
- certId: txCert.id,
- encryptedPrivateKey
- },
- tx
- );
+ if (encryptedPrivateKey) {
+ await certificateSecretDAL.create(
+ {
+ certId: txCert.id,
+ encryptedPrivateKey
+ },
+ tx
+ );
+ }
if (collectionId) {
await pkiCollectionItemDAL.create(
diff --git a/backend/src/services/certificate/certificate-types.ts b/backend/src/services/certificate/certificate-types.ts
index ecb10c9605..2628ca7d57 100644
--- a/backend/src/services/certificate/certificate-types.ts
+++ b/backend/src/services/certificate/certificate-types.ts
@@ -152,7 +152,7 @@ export type TImportCertDTO = {
pkiCollectionId?: string;
certificatePem: string;
- privateKeyPem: string;
+ privateKeyPem?: string;
chainPem: string;
} & Omit<TProjectPermission, "projectId">;
diff --git a/frontend/src/hooks/api/certificates/types.ts b/frontend/src/hooks/api/certificates/types.ts
index b150fed462..91ef46b2d9 100644
--- a/frontend/src/hooks/api/certificates/types.ts
+++ b/frontend/src/hooks/api/certificates/types.ts
@@ -81,7 +81,7 @@ export type TRevokeCertDTO = {
export type TImportCertificateDTO = {
certificatePem: string;
- privateKeyPem: string;
+ privateKeyPem?: string;
chainPem: string;
pkiCollectionId?: string;
@@ -92,7 +92,7 @@ export type TImportCertificateDTO = {
export type TImportCertificateResponse = {
certificate: string;
certificateChain: string;
- privateKey: string;
+ privateKey?: string;
serialNumber: string;
};
diff --git a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx
index 56c2530d16..89c1a3b472 100644
--- a/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx
+++ b/frontend/src/pages/cert-manager/CertificatesPage/components/CertificateImportModal.tsx
@@ -12,7 +12,7 @@ import { CertificateContent } from "./CertificateContent";
const schema = z.object({
certificatePem: z.string().trim().min(1, "Certificate PEM is required"),
- privateKeyPem: z.string().trim().min(1, "Private Key PEM is required"),
+ privateKeyPem: z.string().trim().optional(),
chainPem: z.string().trim().min(1, "Certificate Chain PEM is required")
});
@@ -31,7 +31,7 @@ type TCertificateDetails = {
serialNumber: string;
certificate: string;
certificateChain: string;
- privateKey: string;
+ privateKey?: string;
};
export const CertificateImportModal = ({ popUp, handlePopUpToggle, applicationId }: Props) => {
@@ -52,9 +52,10 @@ export const CertificateImportModal = ({ popUp, handlePopUpToggle, applicationId
});
const onFormSubmit = async ({ certificatePem, privateKeyPem, chainPem }: FormData) => {
+ const trimmedPrivateKey = privateKeyPem?.trim();
const { serialNumber, certificate, certificateChain, privateKey } = await importCertificate({
certificatePem,
- privateKeyPem,
+ ...(trimmedPrivateKey ? { privateKeyPem: trimmedPrivateKey } : {}),
chainPem,
applicationId
});
@@ -104,10 +105,10 @@ export const CertificateImportModal = ({ popUp, handlePopUpToggle, applicationId
<Controller
control={control}
defaultValue=""
- name="privateKeyPem"
+ name="chainPem"
render={({ field, fieldState: { error } }) => (
<FormControl
- label="Private Key PEM"
+ label="Certificate Chain PEM"
isError={Boolean(error)}
errorText={error?.message}
isRequired
@@ -119,13 +120,12 @@ export const CertificateImportModal = ({ popUp, handlePopUpToggle, applicationId
<Controller
control={control}
defaultValue=""
- name="chainPem"
+ name="privateKeyPem"
render={({ field, fieldState: { error } }) => (
<FormControl
- label="Certificate Chain PEM"
+ label="Private Key PEM (optional)"
isError={Boolean(error)}
errorText={error?.message}
- isRequired
>
<TextArea {...field} isDisabled={Boolean(cert)} />
</FormControl>