Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
Add support for revoking certificates using thumbprints
Carlos Monastyrski86b69d26b9a5
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.tsindex 8492e44a37..31e8a46d09 100644--- a/backend/src/lib/api-docs/constants.ts+++ b/backend/src/lib/api-docs/constants.ts@@ -2215,6 +2215,13 @@ export const CERTIFICATES = {revokedAt: "The date and time when the certificate was revoked.",serialNumberRes: "The serial number of the revoked certificate."},+ REVOKE_BY_THUMBPRINT: {+ thumbprint:+ "The SHA-1 or SHA-256 thumbprint (fingerprint) of the certificate to revoke. Colons and casing are ignored.",+ revocationReason: "The reason for revoking the certificate.",+ revokedAt: "The date and time when the certificate was revoked.",+ serialNumberRes: "The serial number of the revoked certificate."+ },DELETE: {id: "The ID of the certificate to delete.",serialNumber: "The serial number of the certificate to delete."diff --git a/backend/src/server/routes/v1/certificate-router.ts b/backend/src/server/routes/v1/certificate-router.tsindex 8b8eefab35..213b4d6e65 100644--- a/backend/src/server/routes/v1/certificate-router.ts+++ b/backend/src/server/routes/v1/certificate-router.ts@@ -1688,6 +1688,70 @@ export const registerCertificateRouter = async (server: FastifyZodProvider) => {}});+ server.route({+ method: "POST",+ url: "/revoke-by-thumbprint",+ config: {+ rateLimit: writeLimit+ },+ onRequest: verifyAuth([AuthMode.JWT, AuthMode.IDENTITY_ACCESS_TOKEN]),+ schema: {+ hide: false,+ operationId: "revokeCertificateByThumbprint",+ tags: [ApiDocsTags.PkiCertificates],+ description: "Revoke a certificate by its SHA-1 or SHA-256 thumbprint",+ body: z.object({+ thumbprint: z.string().trim().min(1).describe(CERTIFICATES.REVOKE_BY_THUMBPRINT.thumbprint),+ revocationReason: z.nativeEnum(CrlReason).describe(CERTIFICATES.REVOKE_BY_THUMBPRINT.revocationReason)+ }),+ response: {+ 200: z.object({+ message: z.string().trim(),+ serialNumber: z.string().trim().describe(CERTIFICATES.REVOKE_BY_THUMBPRINT.serialNumberRes),+ revokedAt: z.date().describe(CERTIFICATES.REVOKE_BY_THUMBPRINT.revokedAt)+ })+ }+ },+ handler: async (req) => {+ const { revokedAt, cert, ca } = await server.services.certificate.revokeCert({+ thumbprint: req.body.thumbprint,+ revocationReason: req.body.revocationReason,+ actor: req.permission.type,+ actorId: req.permission.id,+ actorAuthMethod: req.permission.authMethod,+ actorOrgId: req.permission.orgId+ });++ await server.services.auditLog.createAuditLog({+ ...req.auditLogInfo,+ projectId: ca.projectId,+ event: {+ type: EventType.REVOKE_CERT,+ metadata: {+ certId: cert.id,+ cn: cert.commonName,+ serialNumber: cert.serialNumber+ }+ }+ });++ await server.services.telemetry.sendPostHogEvents({+ event: PostHogEventTypes.CertificateRevoked,+ distinctId: getTelemetryDistinctId(req),+ organizationId: req.permission.orgId,+ properties: {+ orgId: req.permission.orgId+ }+ });++ return {+ message: "Successfully revoked certificate",+ serialNumber: cert.serialNumber,+ revokedAt+ };+ }+ });+server.route({method: "DELETE",url: "/:id",diff --git a/backend/src/services/certificate/certificate-fns.test.ts b/backend/src/services/certificate/certificate-fns.test.tsnew file mode 100644index 0000000000..f4a8013c38--- /dev/null+++ b/backend/src/services/certificate/certificate-fns.test.ts@@ -0,0 +1,33 @@+import { BadRequestError } from "@app/lib/errors";++import { CertificateThumbprintAlgorithm, normalizeThumbprint } from "./certificate-fns";++describe("normalizeThumbprint", () => {+ const sha1Hex = "a".repeat(40);+ const sha256Hex = "b".repeat(64);++ test("detects SHA-1 thumbprints and formats with colons", () => {+ const { algorithm, fingerprint } = normalizeThumbprint(sha1Hex);+ expect(algorithm).toBe(CertificateThumbprintAlgorithm.SHA1);+ expect(fingerprint).toBe(`${"AA:".repeat(19)}AA`);+ });++ test("detects SHA-256 thumbprints", () => {+ const { algorithm, fingerprint } = normalizeThumbprint(sha256Hex);+ expect(algorithm).toBe(CertificateThumbprintAlgorithm.SHA256);+ expect(fingerprint.replace(/:/g, "")).toBe("B".repeat(64));+ });++ test("ignores colons, whitespace, and casing", () => {+ const colonDelimited = sha1Hex.toUpperCase().match(/.{2}/g)!.join(":");+ const messyInput = ` ${colonDelimited.toLowerCase()} `;++ const { algorithm, fingerprint } = normalizeThumbprint(messyInput);+ expect(algorithm).toBe(CertificateThumbprintAlgorithm.SHA1);+ expect(fingerprint).toBe(colonDelimited);+ });++ test("throws on invalid digest length", () => {+ expect(() => normalizeThumbprint("abc123")).toThrow(BadRequestError);+ });+});diff --git a/backend/src/services/certificate/certificate-fns.ts b/backend/src/services/certificate/certificate-fns.tsindex 29ce481575..0e30e6f716 100644--- a/backend/src/services/certificate/certificate-fns.ts+++ b/backend/src/services/certificate/certificate-fns.ts@@ -187,6 +187,40 @@ export const generatePkcs12FromCertificate = async ({}};+/**+ * Format a raw hex digest as an upper-cased, colon-delimited fingerprint (e.g., "1A:2F:73:...").+ */+export const formatFingerprint = (hash: string) =>+ new RE2(".{2}", "g").match(hash.toUpperCase())?.join(":") ?? hash.toUpperCase();++export enum CertificateThumbprintAlgorithm {+ SHA1 = "sha1",+ SHA256 = "sha256"+}++/**+ * Normalize a user-supplied certificate thumbprint into the colon-delimited fingerprint format+ * stored on the certificate (e.g., "1A:2F:..."). Accepts thumbprints with or without colons,+ * whitespace, or "0x" prefixes, in any case. Returns the matching algorithm based on digest length+ * (40 hex chars => SHA-1, 64 hex chars => SHA-256).+ */+export const normalizeThumbprint = (thumbprint: string) => {+ const hex = thumbprint.replace(new RE2("[^a-fA-F0-9]", "g"), "");++ let algorithm: CertificateThumbprintAlgorithm;+ if (hex.length === 40) {+ algorithm = CertificateThumbprintAlgorithm.SHA1;+ } else if (hex.length === 64) {+ algorithm = CertificateThumbprintAlgorithm.SHA256;+ } else {+ throw new BadRequestError({+ message: "Invalid thumbprint. Expected a SHA-1 (40 hex characters) or SHA-256 (64 hex characters) digest"+ });+ }++ return { algorithm, fingerprint: formatFingerprint(hex) };+};+/*** Parse and extract subject, fingerprints, and basicConstraints from a decrypted certificate.* Returns empty object on failure (graceful degradation).@@ -208,8 +242,6 @@ export const parseCertificateBody = (decryptedCertificate: Buffer): TParsedCerti// Calculate fingerprints and format with colons (e.g., "1A:2F:73:...")const rawData = Buffer.from(certObj.rawData);- const formatFingerprint = (hash: string) =>- new RE2(".{2}", "g").match(hash.toUpperCase())?.join(":") ?? hash.toUpperCase();const fingerprints: TCertificateFingerprints = {sha256: formatFingerprint(crypto.nativeCrypto.createHash("sha256").update(rawData).digest("hex")),sha1: formatFingerprint(crypto.nativeCrypto.createHash("sha1").update(rawData).digest("hex"))diff --git a/backend/src/services/certificate/certificate-service.ts b/backend/src/services/certificate/certificate-service.tsindex cf4f4ba6d6..049a8a0f86 100644--- a/backend/src/services/certificate/certificate-service.ts+++ b/backend/src/services/certificate/certificate-service.ts@@ -42,9 +42,11 @@ import { TResourceMetadataDALFactory } from "@app/services/resource-metadata/resimport { expandInternalCa, getCaCertChain, rebuildCaCrl } from "../certificate-authority/certificate-authority-fns";import { validatePqcLicense } from "../certificate-common/certificate-utils";import {+ CertificateThumbprintAlgorithm,extractCertificateFields,generatePkcs12FromCertificate,getCertificateCredentials,+ normalizeThumbprint,parseCertificateBody,revocationReasonToCrlCode,splitPemChain@@ -415,13 +417,42 @@ export const certificateServiceFactory = ({const revokeCert = async ({id,serialNumber,+ thumbprint,revocationReason,actorId,actorAuthMethod,actor,actorOrgId}: TRevokeCertDTO) => {- const cert = id ? await certificateDAL.findById(id) : await certificateDAL.findOne({ serialNumber });+ let cert: Awaited<ReturnType<typeof certificateDAL.findById>> | undefined;+ if (id) {+ cert = await certificateDAL.findById(id);+ } else if (serialNumber) {+ cert = await certificateDAL.findOne({ serialNumber });+ } else if (thumbprint) {+ const { algorithm, fingerprint } = normalizeThumbprint(thumbprint);+ const filter =+ algorithm === CertificateThumbprintAlgorithm.SHA1+ ? { fingerprintSha1: fingerprint }+ : { fingerprintSha256: fingerprint };++ const matches = await certificateDAL.find(filter, { limit: 2 });+ if (matches.length > 1) {+ throw new BadRequestError({+ message:+ "Multiple certificates match the provided thumbprint. Revoke by certificate ID or serial number instead"+ });+ }+ [cert] = matches;+ } else {+ throw new BadRequestError({+ message: "Must provide a certificate ID, serial number, or thumbprint to revoke"+ });+ }++ if (!cert) {+ throw new NotFoundError({ message: "Certificate not found" });+ }if (!cert.caId) {throw new BadRequestError({diff --git a/backend/src/services/certificate/certificate-types.ts b/backend/src/services/certificate/certificate-types.tsindex e99e2e6b73..d256f196d7 100644--- a/backend/src/services/certificate/certificate-types.ts+++ b/backend/src/services/certificate/certificate-types.ts@@ -130,6 +130,7 @@ export type TDeleteCertDTO = {export type TRevokeCertDTO = {id?: string;serialNumber?: string;+ thumbprint?: string;revocationReason: CrlReason;} & Omit<TProjectPermission, "projectId">;diff --git a/docs/api-reference/endpoints/certificates/revoke-by-thumbprint.mdx b/docs/api-reference/endpoints/certificates/revoke-by-thumbprint.mdxnew file mode 100644index 0000000000..4afefe1c77--- /dev/null+++ b/docs/api-reference/endpoints/certificates/revoke-by-thumbprint.mdx@@ -0,0 +1,4 @@+---+title: "Revoke by Thumbprint"+openapi: "POST /api/v1/cert-manager/certificates/revoke-by-thumbprint"+---diff --git a/docs/docs.json b/docs/docs.jsonindex 8b541335a3..b7381cf168 100644--- a/docs/docs.json+++ b/docs/docs.json@@ -3103,6 +3103,7 @@"api-reference/endpoints/certificates/renew","api-reference/endpoints/certificates/update-config","api-reference/endpoints/certificates/revoke",+ "api-reference/endpoints/certificates/revoke-by-thumbprint","api-reference/endpoints/certificates/delete","api-reference/endpoints/certificates/cert-body","api-reference/endpoints/certificates/bundle",