fix: working on AEA-GCM encryption.

This commit is contained in:
charlesgauthereau
2026-02-12 18:46:11 +01:00
parent c01440f9d4
commit 2426166676
5 changed files with 56 additions and 8 deletions
@@ -28,7 +28,7 @@ export default async function RoutePage(props: PageParams<{ agentId: string }>)
}
const edgeKey = await generateEdgeKey(getServerUrl(), agent.id);
return (
<Page>
<div className="justify-between gap-2 sm:flex">
+16
View File
@@ -15,3 +15,19 @@ export function getPublicServerKeyContent() {
};
}
}
/**
* Get Master server key
*/
export function getMasterServerKeyContent() {
try {
return fs.readFileSync("private/keys/master_key.bin");
} catch (error: any) {
console.error("Error :", error);
return {
success: false,
message: `An error occurred while getting master server key`,
};
}
}
+5 -5
View File
@@ -1,16 +1,16 @@
"use server"
import {getPublicServerKeyContent} from "@/features/keys/keys.action";
import {getMasterServerKeyContent} from "@/features/keys/keys.action";
export async function generateEdgeKey(serverUrl: string, agentId: string): Promise<string> {
const publicKey = getPublicServerKeyContent()
const masterKey = getMasterServerKeyContent()
console.log("Master server key: ", masterKey)
const edgeKeyData = {
serverUrl,
agentId,
publicKey
masterKeyB64: masterKey.toString('base64')
};
const edgeKeyJson = JSON.stringify(edgeKeyData);
const edgeKeyBuffer = Buffer.from(edgeKeyJson, 'utf-8');
return edgeKeyBuffer.toString('base64').replace(/=+$/, '').replace(/\+/g, '-').replace(/\//g, '_');
return Buffer.from(edgeKeyJson, 'utf-8').toString('base64');
}
function decodeEdgeKey(edgeKey: string): object {
+3 -2
View File
@@ -3,13 +3,14 @@ import {db, makeMigration} from "@/db";
import {eq} from "drizzle-orm";
import * as drizzleDb from "@/db";
import {retentionJob} from "@/lib/tasks";
import {generateRSAKeys} from "@/utils/rsa-keys";
import {generateRSAKeys, getOrCreateMasterKey} from "@/utils/rsa-keys";
import {StorageProviderKind} from "@/features/storages/types";
export async function init() {
consoleAscii();
console.log("====Init Functions====");
await getOrCreateMasterKey();
await generateRSAKeys();
await makeMigration();
await createDefaultOrganization();
@@ -82,7 +83,7 @@ async function createSettingsIfNotExist() {
if (!finalSystemSetting.defaultStorageChannelId) {
await tx
.update(drizzleDb.schemas.setting)
.set({ defaultStorageChannelId: localStorage.id })
.set({defaultStorageChannelId: localStorage.id})
.where(eq(drizzleDb.schemas.setting.id, finalSystemSetting.id));
}
});
+31
View File
@@ -2,6 +2,7 @@ import {promises as fs} from 'fs';
import path from 'path';
import {generateKeyPair} from 'crypto';
import {promisify} from 'util';
import {randomBytes} from 'crypto';
const generateKeyPairAsync = promisify(generateKeyPair);
@@ -37,3 +38,33 @@ export async function generateRSAKeys(dir = path.join(process.cwd(), 'private/ke
return {privateKeyPath, publicKeyPath};
}
/**
* Generate a 256-bit AES master key for AES-256-GCM.
* - Skips generation if the file already exists.
* - File mode 0o600 for private key.
* @param {string} [filePath] Path to store the key
* @returns {Promise<Buffer>} The master key
*/
export async function getOrCreateMasterKey(filePath = path.join(process.cwd(), 'private/keys', 'master_key.bin')) {
await fs.mkdir(path.dirname(filePath), {recursive: true});
try {
const existing = await fs.readFile(filePath);
console.log('Master key already exists. Skipping generation.');
return existing;
} catch {
// File does not exist, generate
}
const key = randomBytes(32); // 256-bit key
console.log(key)
await fs.writeFile(filePath, key, {mode: 0o600});
console.log(`Master key generated at ${filePath}`);
return key;
}