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
Address greptile comments
Carlos Monastyrskib9c9a2846551
diff --git a/backend/src/services/certificate-authority/ca-signer.ts b/backend/src/services/certificate-authority/ca-signer.tsindex 8ec2ca5e79..cb15526887 100644--- a/backend/src/services/certificate-authority/ca-signer.ts+++ b/backend/src/services/certificate-authority/ca-signer.ts@@ -129,36 +129,77 @@ export const buildLocalCaSigner = ({createCrl: (params) => x509.X509CrlGenerator.create({ ...params, signingKey: privateKey, signingAlgorithm })});+const HSM_MECHANISM_BY_HASH: Record<"rsa" | "ecdsa", Record<string, string>> = {+ rsa: {+ "SHA-256": "CKM_SHA256_RSA_PKCS",+ "SHA-384": "CKM_SHA384_RSA_PKCS",+ "SHA-512": "CKM_SHA512_RSA_PKCS"+ },+ ecdsa: {+ "SHA-256": "CKM_ECDSA_SHA256",+ "SHA-384": "CKM_ECDSA_SHA384",+ "SHA-512": "CKM_ECDSA_SHA512"+ }+};++const algorithmHashName = (alg: Algorithm): string | undefined => {+ const { hash } = alg as { hash?: string | { name?: string } };+ return typeof hash === "string" ? hash : hash?.name;+};+export const buildHsmCaSigner = ({caPublicKey,keyAlgorithm,+ signingAlgorithm,sign}: {caPublicKey: CryptoKey;keyAlgorithm: CertKeyAlgorithm | string;+ signingAlgorithm?: Algorithm;sign: TCaExternalSignFn;}): TCaSigner => {const shape = caKeyAlgorithmToHsmShape(keyAlgorithm);+ let { mechanism } = shape;+ let certSigningAlgorithm = shape.signingAlgorithm;+ let { keyGenParams } = shape;+ if (signingAlgorithm) {+ const hash = algorithmHashName(signingAlgorithm);+ const mapped = HSM_MECHANISM_BY_HASH[shape.isEcdsa ? "ecdsa" : "rsa"][hash ?? ""];+ if (!mapped) {+ throw new BadRequestError({+ message: `Signature hash "${hash}" is not supported for HSM-backed certificate authorities.`+ });+ }+ mechanism = mapped;+ certSigningAlgorithm = { name: shape.signingAlgorithm.name, hash } as Algorithm;+ // @peculiar derives the RSA certificate's signature OID from the ephemeral key's hash, so it+ // must match the chosen mechanism or the OID and signature disagree. ECDSA takes the hash from+ // signingAlgorithm directly, so its key params (curve only) are left untouched.+ if (!shape.isEcdsa) {+ keyGenParams = { ...(shape.keyGenParams as RsaHashedKeyGenParams), hash } as RsaHashedKeyGenParams;+ }+ }+const signTbs = async (tbs: Buffer): Promise<ArrayBuffer> => {// isDigest=false: the HSM hashes via its *_SHA<NN>_* mechanism, so we hand it the raw TBS.- let signature = await sign(tbs, shape.mechanism, false);+ let signature = await sign(tbs, mechanism, false);if (shape.isEcdsa) signature = ecdsaRawRsToDer(signature);return bufferToArrayBuffer(signature);};const generateEphemeralKey = () =>- crypto.nativeCrypto.subtle.generateKey(shape.keyGenParams as RsaHashedKeyGenParams, true, ["sign", "verify"]);+ crypto.nativeCrypto.subtle.generateKey(keyGenParams as RsaHashedKeyGenParams, true, ["sign", "verify"]);return {caPublicKey,- signingAlgorithm: shape.signingAlgorithm,+ signingAlgorithm: certSigningAlgorithm,createCertificate: async (params) => {const ephemeral = await generateEphemeralKey();const built = await x509.X509CertificateGenerator.create({...params,signingKey: ephemeral.privateKey,- signingAlgorithm: shape.signingAlgorithm+ signingAlgorithm: certSigningAlgorithm});const cert = AsnConvert.parse(built.rawData, Certificate);cert.signatureValue = await signTbs(Buffer.from(AsnConvert.serialize(cert.tbsCertificate)));@@ -169,7 +210,7 @@ export const buildHsmCaSigner = ({const built = await x509.Pkcs10CertificateRequestGenerator.create({...params,keys: { privateKey: ephemeral.privateKey, publicKey: caPublicKey },- signingAlgorithm: shape.signingAlgorithm+ signingAlgorithm: certSigningAlgorithm});const csr = AsnConvert.parse(built.rawData, CertificationRequest);csr.signature = await signTbs(Buffer.from(AsnConvert.serialize(csr.certificationRequestInfo)));@@ -180,7 +221,7 @@ export const buildHsmCaSigner = ({const built = await x509.X509CrlGenerator.create({...params,signingKey: ephemeral.privateKey,- signingAlgorithm: shape.signingAlgorithm+ signingAlgorithm: certSigningAlgorithm});const crl = AsnConvert.parse(built.rawData, CertificateList);crl.signature = await signTbs(Buffer.from(AsnConvert.serialize(crl.tbsCertList)));diff --git a/backend/src/services/certificate-authority/certificate-authority-fns.ts b/backend/src/services/certificate-authority/certificate-authority-fns.tsindex 456f7dac69..a6488d8dbb 100644--- a/backend/src/services/certificate-authority/certificate-authority-fns.ts+++ b/backend/src/services/certificate-authority/certificate-authority-fns.ts@@ -454,7 +454,6 @@ export const getCaSigner = async ({const keyAlgorithm = ca.internalCa.keyAlgorithm as CertKeyAlgorithm;if (caSecret.keySource === CertKeySource.Hsm) {- // signatureAlgorithm is intentionally ignored: the HSM mechanism is fixed by the key algorithm.if (!caSecret.hsmConnectorId || !caSecret.hsmKeyLabel || !caSecret.hsmPublicKeySpki) {throw new BadRequestError({ message: "HSM-backed CA secret is missing its connector, key label, or public key" });}@@ -473,6 +472,7 @@ export const getCaSigner = async ({const signer = buildHsmCaSigner({caPublicKey,keyAlgorithm,+ signingAlgorithm: signatureAlgorithm,sign: (data, mechanism, isDigest) =>hsmConnectorService.sign({connectorId,diff --git a/backend/src/services/hsm-connector/hsm-connector-dal.ts b/backend/src/services/hsm-connector/hsm-connector-dal.tsindex d1b69afd75..2e6d180d01 100644--- a/backend/src/services/hsm-connector/hsm-connector-dal.ts+++ b/backend/src/services/hsm-connector/hsm-connector-dal.ts@@ -47,7 +47,14 @@ export const hsmConnectorDALFactory = (db: TDbClient) => {}};- const listLinkedResources = async (connectorId: string, { offset, limit }: { offset: number; limit: number }) => {+ const listLinkedResources = async (+ connectorId: string,+ {+ offset,+ limit,+ includeCertificateAuthorities+ }: { offset: number; limit: number; includeCertificateAuthorities: boolean }+ ) => {try {const baseQuery = db.replicaNode()(TableName.Certificate).where("hsmConnectorId", connectorId);@@ -61,32 +68,31 @@ export const hsmConnectorDALFactory = (db: TDbClient) => {baseQuery.clone().count<{ count: string }[]>("id as count").first()]);- const caRows =- offset === 0- ? await db- .replicaNode()(TableName.CertificateAuthoritySecret)- .where(`${TableName.CertificateAuthoritySecret}.hsmConnectorId`, connectorId)- .join(- TableName.CertificateAuthority,- `${TableName.CertificateAuthority}.id`,- `${TableName.CertificateAuthoritySecret}.caId`- )- .join(- TableName.InternalCertificateAuthority,- `${TableName.InternalCertificateAuthority}.caId`,- `${TableName.CertificateAuthoritySecret}.caId`- )- .select(- db.ref("id").withSchema(TableName.CertificateAuthority).as("id"),- db.ref("name").withSchema(TableName.CertificateAuthority).as("name"),- db.ref("status").withSchema(TableName.CertificateAuthority).as("status"),- db.ref("type").withSchema(TableName.InternalCertificateAuthority).as("type"),- db.ref("commonName").withSchema(TableName.InternalCertificateAuthority).as("commonName"),- db.ref("hsmKeyLabel").withSchema(TableName.CertificateAuthoritySecret).as("hsmKeyLabel"),- db.ref("createdAt").withSchema(TableName.CertificateAuthority).as("createdAt")- )- .orderBy(`${TableName.CertificateAuthority}.createdAt`, "desc")- : [];+ const caRows = includeCertificateAuthorities+ ? await db+ .replicaNode()(TableName.CertificateAuthoritySecret)+ .where(`${TableName.CertificateAuthoritySecret}.hsmConnectorId`, connectorId)+ .join(+ TableName.CertificateAuthority,+ `${TableName.CertificateAuthority}.id`,+ `${TableName.CertificateAuthoritySecret}.caId`+ )+ .join(+ TableName.InternalCertificateAuthority,+ `${TableName.InternalCertificateAuthority}.caId`,+ `${TableName.CertificateAuthoritySecret}.caId`+ )+ .select(+ db.ref("id").withSchema(TableName.CertificateAuthority).as("id"),+ db.ref("name").withSchema(TableName.CertificateAuthority).as("name"),+ db.ref("status").withSchema(TableName.CertificateAuthority).as("status"),+ db.ref("type").withSchema(TableName.InternalCertificateAuthority).as("type"),+ db.ref("commonName").withSchema(TableName.InternalCertificateAuthority).as("commonName"),+ db.ref("hsmKeyLabel").withSchema(TableName.CertificateAuthoritySecret).as("hsmKeyLabel"),+ db.ref("createdAt").withSchema(TableName.CertificateAuthority).as("createdAt")+ )+ .orderBy(`${TableName.CertificateAuthority}.createdAt`, "desc")+ : [];return {certificates: rows.map((r) => ({diff --git a/backend/src/services/hsm-connector/hsm-connector-service.ts b/backend/src/services/hsm-connector/hsm-connector-service.tsindex a60230a03b..35b6186864 100644--- a/backend/src/services/hsm-connector/hsm-connector-service.ts+++ b/backend/src/services/hsm-connector/hsm-connector-service.ts@@ -9,6 +9,7 @@ import { TLicenseServiceFactory } from "@app/ee/services/license/license-serviceimport { TPermissionServiceFactory } from "@app/ee/services/permission/permission-service-types";import {ProjectPermissionCertificateActions,+ ProjectPermissionCertificateAuthorityActions,ProjectPermissionHsmConnectorActions,ProjectPermissionSub} from "@app/ee/services/permission/project-permission";@@ -372,9 +373,15 @@ export const hsmConnectorServiceFactory = ({ProjectPermissionSub.Certificates);+ const includeCertificateAuthorities = permission.can(+ ProjectPermissionCertificateAuthorityActions.Read,+ ProjectPermissionSub.CertificateAuthorities+ );+return hsmConnectorDAL.listLinkedResources(row.id, {offset: dto.offset,- limit: dto.limit+ limit: dto.limit,+ includeCertificateAuthorities});};