Skip to content

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

fix(pki): record key and signature algorithm on imported certificates

Saif1f6e42a8eb4c
diff --git a/backend/src/services/certificate/certificate-fns.ts b/backend/src/services/certificate/certificate-fns.ts
index a31d721f41..68a572e1e8 100644
--- a/backend/src/services/certificate/certificate-fns.ts
+++ b/backend/src/services/certificate/certificate-fns.ts
@@ -12,6 +12,7 @@ import {
CertExtendedKeyUsageOIDToName,
CertKeyAlgorithm,
CertKeyUsage,
+ CertSignatureAlgorithm,
CrlReason,
TCertificateFingerprints,
TCertificateSubject,
@@ -306,6 +307,68 @@ export const parseCertificateBody = (decryptedCertificate: Buffer): TParsedCerti
}
};
+const EC_CURVE_KEY_ALGORITHMS: Record<string, CertKeyAlgorithm> = {
+ "P-256": CertKeyAlgorithm.ECDSA_P256,
+ "P-384": CertKeyAlgorithm.ECDSA_P384,
+ "P-521": CertKeyAlgorithm.ECDSA_P521
+};
+
+const RSA_KEY_ALGORITHMS: Record<number, CertKeyAlgorithm> = {
+ 2048: CertKeyAlgorithm.RSA_2048,
+ 3072: CertKeyAlgorithm.RSA_3072,
+ 4096: CertKeyAlgorithm.RSA_4096
+};
+
+const RSA_SIGNATURE_ALGORITHMS: Record<string, CertSignatureAlgorithm> = {
+ "SHA-256": CertSignatureAlgorithm.RSA_SHA256,
+ "SHA-384": CertSignatureAlgorithm.RSA_SHA384,
+ "SHA-512": CertSignatureAlgorithm.RSA_SHA512
+};
+
+const ECDSA_SIGNATURE_ALGORITHMS: Record<string, CertSignatureAlgorithm> = {
+ "SHA-256": CertSignatureAlgorithm.ECDSA_SHA256,
+ "SHA-384": CertSignatureAlgorithm.ECDSA_SHA384,
+ "SHA-512": CertSignatureAlgorithm.ECDSA_SHA512
+};
+
+// Import accepts certificates the issuance enums do not cover, such as Ed25519 and secp256k1, so an
+// unrecognised algorithm is recorded under its own name rather than rejected. The UI falls back to
+// the raw value when it is not one it has a label for.
+export const extractCertificateAlgorithms = (decryptedCertificate: Buffer) => {
+ let certObj: x509.X509Certificate;
+ try {
+ certObj = new x509.X509Certificate(decryptedCertificate);
+ } catch {
+ return {};
+ }
+
+ const publicKeyAlgorithm = certObj.publicKey.algorithm as {
+ name: string;
+ modulusLength?: number;
+ namedCurve?: string;
+ };
+ const signature = certObj.signatureAlgorithm as unknown as { name: string; hash?: { name: string } };
+ const hashName = signature.hash?.name ?? "";
+
+ let keyAlgorithm: string = publicKeyAlgorithm.name;
+ if (publicKeyAlgorithm.name.startsWith("RSA")) {
+ const bits = publicKeyAlgorithm.modulusLength;
+ keyAlgorithm = (bits && RSA_KEY_ALGORITHMS[bits]) || (bits ? `RSA_${bits}` : "RSA");
+ } else if (publicKeyAlgorithm.name === "ECDSA") {
+ const curve = publicKeyAlgorithm.namedCurve ?? "";
+ keyAlgorithm = EC_CURVE_KEY_ALGORITHMS[curve] ?? (curve ? `EC_${curve}` : "EC");
+ }
+
+ let signatureAlgorithm: string = signature.name;
+ if (signature.name.startsWith("RSA")) {
+ signatureAlgorithm = RSA_SIGNATURE_ALGORITHMS[hashName] ?? `RSA-${hashName || "UNKNOWN"}`;
+ } else if (signature.name === "ECDSA") {
+ signatureAlgorithm = ECDSA_SIGNATURE_ALGORITHMS[hashName] ?? `ECDSA-${hashName || "UNKNOWN"}`;
+ }
+
+ return { keyAlgorithm, signatureAlgorithm };
+};
+
/**
* Extract certificate fields including subject attributes, fingerprints, and basic constraints.
* Returns all parsed fields as separate properties.
diff --git a/backend/src/services/certificate/certificate-service.ts b/backend/src/services/certificate/certificate-service.ts
index 36c89343d5..f6a79fda5e 100644
--- a/backend/src/services/certificate/certificate-service.ts
+++ b/backend/src/services/certificate/certificate-service.ts
@@ -46,6 +46,7 @@ import { expandInternalCa, getCaCertChain, rebuildCaCrl } from "../certificate-a
import { validatePqcLicense } from "../certificate-common/certificate-utils";
import {
CertificateThumbprintAlgorithm,
+ extractCertificateAlgorithms,
extractCertificateFields,
generatePkcs12FromCertificate,
getCertificateCredentials,
@@ -899,7 +900,8 @@ export const certificateServiceFactory = ({
const cert = await certificateDAL.transaction(async (tx) => {
try {
// Extract certificate fields for storage
- const parsedFields = extractCertificateFields(Buffer.from(certificatePem));
+ const certificateBuffer = Buffer.from(certificatePem);
+ const parsedFields = extractCertificateFields(certificateBuffer);
const txCert = await certificateDAL.create(
{
@@ -914,7 +916,10 @@ export const certificateServiceFactory = ({
applicationId: applicationId ?? null,
keyUsages,
extendedKeyUsages,
- ...parsedFields
+ ...parsedFields,
+ // Issuance records these from what it was asked to produce. An imported certificate has
+ // no such request, so they come from the certificate itself.
+ ...extractCertificateAlgorithms(certificateBuffer)
},
tx
);