feat(kms): sign & verify data

This commit is contained in:
Daniel Hougaard
2025-04-03 07:17:29 +04:00
parent ebcf4761b6
commit c6ca668db9
36 changed files with 1873 additions and 162 deletions
@@ -8,6 +8,10 @@ import {
TCmekDecryptResponse,
TCmekEncrypt,
TCmekEncryptResponse,
TCmekSign,
TCmekSignResponse,
TCmekVerify,
TCmekVerifyResponse,
TCreateCmek,
TDeleteCmek,
TUpdateCmek
@@ -74,6 +78,44 @@ export const useCmekEncrypt = () => {
});
};
export const useCmekSign = () => {
return useMutation({
mutationFn: async ({
keyId,
data,
signingAlgorithm,
isBase64Encoded
}: TCmekSign & { isBase64Encoded: boolean }) => {
const res = await apiRequest.post<TCmekSignResponse>(`/api/v1/kms/keys/${keyId}/sign`, {
data: isBase64Encoded ? data : encodeBase64(Buffer.from(data)),
signingAlgorithm
});
return res.data;
}
});
};
export const useCmekVerify = () => {
return useMutation({
mutationFn: async ({
keyId,
data,
signature,
signingAlgorithm,
isBase64Encoded
}: TCmekVerify & { isBase64Encoded: boolean }) => {
const res = await apiRequest.post<TCmekVerifyResponse>(`/api/v1/kms/keys/${keyId}/verify`, {
data: isBase64Encoded ? data : encodeBase64(Buffer.from(data)),
signature,
signingAlgorithm
});
return res.data;
}
});
};
export const useCmekDecrypt = () => {
return useMutation({
mutationFn: async ({ keyId, ciphertext }: TCmekDecrypt) => {
+59 -3
View File
@@ -1,10 +1,18 @@
import { z } from "zod";
import { OrderByDirection } from "@app/hooks/api/generic/types";
export enum KmsKeyIntent {
ENCRYPT_DECRYPT = "encrypt-decrypt",
SIGN_VERIFY = "sign-verify"
}
export type TCmek = {
id: string;
type: KmsKeyIntent;
name: string;
description?: string;
encryptionAlgorithm: EncryptionAlgorithm;
encryptionAlgorithm: AsymmetricKeySignVerify | SymmetricKeyEncryptDecrypt;
projectId: string;
isDisabled: boolean;
isReserved: boolean;
@@ -17,7 +25,8 @@ export type TCmek = {
type ProjectRef = { projectId: string };
type KeyRef = { keyId: string };
export type TCreateCmek = Pick<TCmek, "name" | "description" | "encryptionAlgorithm"> & ProjectRef;
export type TCreateCmek = Pick<TCmek, "name" | "description" | "encryptionAlgorithm" | "type"> &
ProjectRef;
export type TUpdateCmek = KeyRef &
Partial<Pick<TCmek, "name" | "description" | "isDisabled">> &
ProjectRef;
@@ -26,6 +35,13 @@ export type TDeleteCmek = KeyRef & ProjectRef;
export type TCmekEncrypt = KeyRef & { plaintext: string; isBase64Encoded?: boolean };
export type TCmekDecrypt = KeyRef & { ciphertext: string };
export type TCmekSign = KeyRef & { data: string; signingAlgorithm: SigningAlgorithm };
export type TCmekVerify = KeyRef & {
data: string;
signature: string;
signingAlgorithm: SigningAlgorithm;
};
export type TProjectCmeksList = {
keys: TCmek[];
totalCount: number;
@@ -44,6 +60,18 @@ export type TCmekEncryptResponse = {
ciphertext: string;
};
export type TCmekSignResponse = {
signature: string;
keyId: string;
signingAlgorithm: SigningAlgorithm;
};
export type TCmekVerifyResponse = {
signatureValid: boolean;
keyId: string;
signingAlgorithm: SigningAlgorithm;
};
export type TCmekDecryptResponse = {
plaintext: string;
};
@@ -52,7 +80,35 @@ export enum CmekOrderBy {
Name = "name"
}
export enum EncryptionAlgorithm {
export enum AsymmetricKeySignVerify {
RSA_4096 = "rsa-4096",
ECC_NIST_P256 = "ecc-nist-p256"
}
// Supported symmetric encrypt/decrypt algorithms
export enum SymmetricKeyEncryptDecrypt {
AES_GCM_256 = "aes-256-gcm",
AES_GCM_128 = "aes-128-gcm"
}
export const AllowedEncryptionKeyAlgorithms = z.enum([
...Object.values(SymmetricKeyEncryptDecrypt),
...Object.values(AsymmetricKeySignVerify)
] as [string, ...string[]]).options;
export enum SigningAlgorithm {
// RSA PSS algorithms
RSASSA_PSS_SHA_256 = "RSASSA_PSS_SHA_256",
RSASSA_PSS_SHA_384 = "RSASSA_PSS_SHA_384",
RSASSA_PSS_SHA_512 = "RSASSA_PSS_SHA_512",
// RSA PKCS#1 v1.5 algorithms
RSASSA_PKCS1_V1_5_SHA_256 = "RSASSA_PKCS1_V1_5_SHA_256",
RSASSA_PKCS1_V1_5_SHA_384 = "RSASSA_PKCS1_V1_5_SHA_384",
RSASSA_PKCS1_V1_5_SHA_512 = "RSASSA_PKCS1_V1_5_SHA_512",
// ECDSA algorithms
ECDSA_SHA_256 = "ECDSA_SHA_256",
ECDSA_SHA_384 = "ECDSA_SHA_384",
ECDSA_SHA_512 = "ECDSA_SHA_512"
}