mirror of
https://github.com/kmc7468/arkvault.git
synced 2025-12-14 22:08:45 +00:00
암호 키 생성 및 등록시 최초 MEK도 함께 생성 및 등록하도록 구현
This commit is contained in:
@@ -6,6 +6,7 @@ interface KeyExportState {
|
|||||||
redirectPath: string;
|
redirectPath: string;
|
||||||
pubKeyBase64: string;
|
pubKeyBase64: string;
|
||||||
privKeyBase64: string;
|
privKeyBase64: string;
|
||||||
|
mekDraft: ArrayBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
const useAutoNull = <T>(value: T | null) => {
|
const useAutoNull = <T>(value: T | null) => {
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
export type RSAKeyType = "public" | "private";
|
export type RSAKeyType = "public" | "private";
|
||||||
|
|
||||||
|
export const encodeToBase64 = (data: ArrayBuffer) => {
|
||||||
|
return btoa(String.fromCharCode(...new Uint8Array(data)));
|
||||||
|
};
|
||||||
|
|
||||||
|
export const decodeFromBase64 = (data: string) => {
|
||||||
|
return Uint8Array.from(atob(data), (c) => c.charCodeAt(0)).buffer;
|
||||||
|
};
|
||||||
|
|
||||||
export const generateRSAKeyPair = async () => {
|
export const generateRSAKeyPair = async () => {
|
||||||
const keyPair = await window.crypto.subtle.generateKey(
|
const keyPair = await window.crypto.subtle.generateKey(
|
||||||
{
|
{
|
||||||
@@ -15,36 +23,71 @@ export const generateRSAKeyPair = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const makeRSAKeyNonextractable = async (key: CryptoKey, type: RSAKeyType) => {
|
export const makeRSAKeyNonextractable = async (key: CryptoKey, type: RSAKeyType) => {
|
||||||
const format = type === "public" ? "spki" : "pkcs8";
|
const { format, key: exportedKey } = await exportRSAKey(key, type);
|
||||||
const keyUsage = type === "public" ? "encrypt" : "decrypt";
|
|
||||||
return await window.crypto.subtle.importKey(
|
return await window.crypto.subtle.importKey(
|
||||||
format,
|
format,
|
||||||
await window.crypto.subtle.exportKey(format, key),
|
exportedKey,
|
||||||
{
|
{
|
||||||
name: "RSA-OAEP",
|
name: "RSA-OAEP",
|
||||||
hash: "SHA-256",
|
hash: "SHA-256",
|
||||||
} satisfies RsaHashedImportParams,
|
} satisfies RsaHashedImportParams,
|
||||||
false,
|
false,
|
||||||
[keyUsage],
|
[type === "public" ? "encrypt" : "decrypt"],
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const exportRSAKeyToBase64 = async (key: CryptoKey, type: RSAKeyType) => {
|
export const exportRSAKey = async (key: CryptoKey, type: RSAKeyType) => {
|
||||||
const exportedKey = await window.crypto.subtle.exportKey(
|
const format = type === "public" ? ("spki" as const) : ("pkcs8" as const);
|
||||||
type === "public" ? "spki" : "pkcs8",
|
return {
|
||||||
key,
|
format,
|
||||||
);
|
key: await window.crypto.subtle.exportKey(format, key),
|
||||||
return btoa(String.fromCharCode(...new Uint8Array(exportedKey)));
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const decryptRSACiphertext = async (ciphertext: string, privateKey: CryptoKey) => {
|
export const encryptRSAPlaintext = async (plaintext: ArrayBuffer, publicKey: CryptoKey) => {
|
||||||
const ciphertextBuffer = Uint8Array.from(atob(ciphertext), (c) => c.charCodeAt(0));
|
return await window.crypto.subtle.encrypt(
|
||||||
const plaintext = await window.crypto.subtle.decrypt(
|
{
|
||||||
|
name: "RSA-OAEP",
|
||||||
|
} satisfies RsaOaepParams,
|
||||||
|
publicKey,
|
||||||
|
plaintext,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const decryptRSACiphertext = async (ciphertext: ArrayBuffer, privateKey: CryptoKey) => {
|
||||||
|
return await window.crypto.subtle.decrypt(
|
||||||
{
|
{
|
||||||
name: "RSA-OAEP",
|
name: "RSA-OAEP",
|
||||||
} satisfies RsaOaepParams,
|
} satisfies RsaOaepParams,
|
||||||
privateKey,
|
privateKey,
|
||||||
ciphertextBuffer,
|
ciphertext,
|
||||||
);
|
);
|
||||||
return btoa(String.fromCharCode(...new Uint8Array(plaintext)));
|
};
|
||||||
|
|
||||||
|
export const generateAESKey = async () => {
|
||||||
|
return await window.crypto.subtle.generateKey(
|
||||||
|
{
|
||||||
|
name: "AES-GCM",
|
||||||
|
length: 256,
|
||||||
|
} satisfies AesKeyGenParams,
|
||||||
|
true,
|
||||||
|
["encrypt", "decrypt"],
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const makeAESKeyNonextractable = async (key: CryptoKey) => {
|
||||||
|
return await window.crypto.subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
await exportAESKey(key),
|
||||||
|
{
|
||||||
|
name: "AES-GCM",
|
||||||
|
length: 256,
|
||||||
|
} satisfies AesKeyAlgorithm,
|
||||||
|
false,
|
||||||
|
["encrypt", "decrypt"],
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const exportAESKey = async (key: CryptoKey) => {
|
||||||
|
return await window.crypto.subtle.exportKey("raw", key);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { and, eq, gt, lte, count } from "drizzle-orm";
|
import { and, eq, gt, lte } from "drizzle-orm";
|
||||||
import db from "./drizzle";
|
import db from "./drizzle";
|
||||||
import { client, userClient, userClientChallenge } from "./schema";
|
import { client, userClient, userClientChallenge } from "./schema";
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
|
|
||||||
export const keyPairStore = writable<CryptoKeyPair | null>(null);
|
export const keyPairStore = writable<CryptoKeyPair | null>(null);
|
||||||
|
export const mekStore = writable<Map<number, CryptoKey>>(new Map());
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { exportRSAKeyToBase64 } from "$lib/modules/crypto";
|
import { encodeToBase64, exportRSAKey } from "$lib/modules/crypto";
|
||||||
import { requestPubKeyRegistration } from "../../key/export/service";
|
import { requestPubKeyRegistration } from "../../key/export/service";
|
||||||
|
|
||||||
const callLoginAPI = async (email: string, password: string, pubKeyBase64?: string) => {
|
const callLoginAPI = async (email: string, password: string, pubKeyBase64?: string) => {
|
||||||
@@ -22,7 +22,7 @@ export const requestLogin = async (
|
|||||||
registerPubKey = true,
|
registerPubKey = true,
|
||||||
): Promise<boolean> => {
|
): Promise<boolean> => {
|
||||||
const pubKeyBase64 = keyPair
|
const pubKeyBase64 = keyPair
|
||||||
? await exportRSAKeyToBase64(keyPair.publicKey, "public")
|
? encodeToBase64((await exportRSAKey(keyPair.publicKey, "public")).key)
|
||||||
: undefined;
|
: undefined;
|
||||||
let loginRes = await callLoginAPI(email, password, pubKeyBase64);
|
let loginRes = await callLoginAPI(email, password, pubKeyBase64);
|
||||||
if (loginRes.ok) {
|
if (loginRes.ok) {
|
||||||
|
|||||||
@@ -9,8 +9,9 @@
|
|||||||
import {
|
import {
|
||||||
createBlobFromKeyPairBase64,
|
createBlobFromKeyPairBase64,
|
||||||
requestPubKeyRegistration,
|
requestPubKeyRegistration,
|
||||||
requestTokenUpgrade,
|
|
||||||
storeKeyPairPersistently,
|
storeKeyPairPersistently,
|
||||||
|
requestTokenUpgrade,
|
||||||
|
requestInitialMekRegistration,
|
||||||
} from "./service";
|
} from "./service";
|
||||||
|
|
||||||
import IconKey from "~icons/material-symbols/key";
|
import IconKey from "~icons/material-symbols/key";
|
||||||
@@ -39,16 +40,22 @@
|
|||||||
isBeforeContinueModalOpen = false;
|
isBeforeContinueModalOpen = false;
|
||||||
isBeforeContinueBottomSheetOpen = false;
|
isBeforeContinueBottomSheetOpen = false;
|
||||||
|
|
||||||
if (await requestPubKeyRegistration(data.pubKeyBase64, $keyPairStore.privateKey)) {
|
try {
|
||||||
|
if (!(await requestPubKeyRegistration(data.pubKeyBase64, $keyPairStore.privateKey)))
|
||||||
|
throw new Error("Failed to register public key");
|
||||||
|
|
||||||
await storeKeyPairPersistently($keyPairStore);
|
await storeKeyPairPersistently($keyPairStore);
|
||||||
|
|
||||||
if (await requestTokenUpgrade(data.pubKeyBase64)) {
|
if (!(await requestTokenUpgrade(data.pubKeyBase64)))
|
||||||
await goto(data.redirectPath);
|
throw new Error("Failed to upgrade token");
|
||||||
} else {
|
|
||||||
// TODO: Error handling
|
if (!(await requestInitialMekRegistration(data.mekDraft, $keyPairStore.publicKey)))
|
||||||
}
|
throw new Error("Failed to register initial MEK");
|
||||||
} else {
|
|
||||||
|
await goto(data.redirectPath);
|
||||||
|
} catch (e) {
|
||||||
// TODO: Error handling
|
// TODO: Error handling
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
import { callAPI } from "$lib/hooks";
|
import { callAPI } from "$lib/hooks";
|
||||||
import { storeKeyPairIntoIndexedDB } from "$lib/indexedDB";
|
import { storeKeyPairIntoIndexedDB } from "$lib/indexedDB";
|
||||||
import { decryptRSACiphertext } from "$lib/modules/crypto";
|
import {
|
||||||
|
encodeToBase64,
|
||||||
|
decodeFromBase64,
|
||||||
|
encryptRSAPlaintext,
|
||||||
|
decryptRSACiphertext,
|
||||||
|
} from "$lib/modules/crypto";
|
||||||
|
|
||||||
export const createBlobFromKeyPairBase64 = (pubKeyBase64: string, privKeyBase64: string) => {
|
export const createBlobFromKeyPairBase64 = (pubKeyBase64: string, privKeyBase64: string) => {
|
||||||
const pubKeyFormatted = pubKeyBase64.match(/.{1,64}/g)?.join("\n");
|
const pubKeyFormatted = pubKeyBase64.match(/.{1,64}/g)?.join("\n");
|
||||||
@@ -26,18 +31,22 @@ export const requestPubKeyRegistration = async (pubKeyBase64: string, privateKey
|
|||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
const challenge = data.challenge as string;
|
const challenge = data.challenge as string;
|
||||||
const answer = await decryptRSACiphertext(challenge, privateKey);
|
const answer = await decryptRSACiphertext(decodeFromBase64(challenge), privateKey);
|
||||||
|
|
||||||
res = await callAPI("/api/client/verify", {
|
res = await callAPI("/api/client/verify", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ answer }),
|
body: JSON.stringify({ answer: encodeToBase64(answer) }),
|
||||||
});
|
});
|
||||||
return res.ok;
|
return res.ok;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const storeKeyPairPersistently = async (keyPair: CryptoKeyPair) => {
|
||||||
|
await storeKeyPairIntoIndexedDB(keyPair.publicKey, keyPair.privateKey);
|
||||||
|
};
|
||||||
|
|
||||||
export const requestTokenUpgrade = async (pubKeyBase64: string) => {
|
export const requestTokenUpgrade = async (pubKeyBase64: string) => {
|
||||||
const res = await fetch("/api/auth/upgradeToken", {
|
const res = await fetch("/api/auth/upgradeToken", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
@@ -49,6 +58,17 @@ export const requestTokenUpgrade = async (pubKeyBase64: string) => {
|
|||||||
return res.ok;
|
return res.ok;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const storeKeyPairPersistently = async (keyPair: CryptoKeyPair) => {
|
export const requestInitialMekRegistration = async (
|
||||||
await storeKeyPairIntoIndexedDB(keyPair.publicKey, keyPair.privateKey);
|
mekDraft: ArrayBuffer,
|
||||||
|
publicKey: CryptoKey,
|
||||||
|
) => {
|
||||||
|
const mekDraftEncrypted = await encryptRSAPlaintext(mekDraft, publicKey);
|
||||||
|
const res = await callAPI("/api/mek/register/initial", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ mek: encodeToBase64(mekDraftEncrypted) }),
|
||||||
|
});
|
||||||
|
return res.ok || res.status === 403;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import { gotoStateful } from "$lib/hooks";
|
import { gotoStateful } from "$lib/hooks";
|
||||||
import { keyPairStore } from "$lib/stores";
|
import { keyPairStore } from "$lib/stores";
|
||||||
import Order from "./Order.svelte";
|
import Order from "./Order.svelte";
|
||||||
import { generateKeyPair } from "./service";
|
import { generateKeyPair, generateMekDraft } from "./service";
|
||||||
|
|
||||||
import IconKey from "~icons/material-symbols/key";
|
import IconKey from "~icons/material-symbols/key";
|
||||||
|
|
||||||
@@ -33,11 +33,14 @@
|
|||||||
const generate = async () => {
|
const generate = async () => {
|
||||||
// TODO: Loading indicator
|
// TODO: Loading indicator
|
||||||
|
|
||||||
const keyPair = await generateKeyPair();
|
const { pubKeyBase64, privKeyBase64 } = await generateKeyPair();
|
||||||
|
const { mekDraft } = await generateMekDraft();
|
||||||
|
|
||||||
await gotoStateful("/key/export", {
|
await gotoStateful("/key/export", {
|
||||||
redirectPath: data.redirectPath,
|
redirectPath: data.redirectPath,
|
||||||
pubKeyBase64: keyPair.pubKeyBase64,
|
pubKeyBase64,
|
||||||
privKeyBase64: keyPair.privKeyBase64,
|
privKeyBase64,
|
||||||
|
mekDraft,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,13 @@
|
|||||||
import {
|
import {
|
||||||
|
encodeToBase64,
|
||||||
generateRSAKeyPair,
|
generateRSAKeyPair,
|
||||||
makeRSAKeyNonextractable,
|
makeRSAKeyNonextractable,
|
||||||
exportRSAKeyToBase64,
|
exportRSAKey,
|
||||||
|
generateAESKey,
|
||||||
|
makeAESKeyNonextractable,
|
||||||
|
exportAESKey,
|
||||||
} from "$lib/modules/crypto";
|
} from "$lib/modules/crypto";
|
||||||
import { keyPairStore } from "$lib/stores";
|
import { keyPairStore, mekStore } from "$lib/stores";
|
||||||
|
|
||||||
export const generateKeyPair = async () => {
|
export const generateKeyPair = async () => {
|
||||||
const keyPair = await generateRSAKeyPair();
|
const keyPair = await generateRSAKeyPair();
|
||||||
@@ -15,7 +19,21 @@ export const generateKeyPair = async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pubKeyBase64: await exportRSAKeyToBase64(keyPair.publicKey, "public"),
|
pubKeyBase64: encodeToBase64((await exportRSAKey(keyPair.publicKey, "public")).key),
|
||||||
privKeyBase64: await exportRSAKeyToBase64(keyPair.privateKey, "private"),
|
privKeyBase64: encodeToBase64((await exportRSAKey(keyPair.privateKey, "private")).key),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const generateMekDraft = async () => {
|
||||||
|
const mek = await generateAESKey();
|
||||||
|
const mekSecured = await makeAESKeyNonextractable(mek);
|
||||||
|
|
||||||
|
mekStore.update((meks) => {
|
||||||
|
meks.set(meks.size, mekSecured);
|
||||||
|
return meks;
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
mekDraft: await exportAESKey(mek),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { error, json } from "@sveltejs/kit";
|
import { json } from "@sveltejs/kit";
|
||||||
import { authorize } from "$lib/server/modules/auth";
|
import { authorize } from "$lib/server/modules/auth";
|
||||||
import { getClientMekList } from "$lib/server/services/mek";
|
import { getClientMekList } from "$lib/server/services/mek";
|
||||||
import type { RequestHandler } from "@sveltejs/kit";
|
import type { RequestHandler } from "@sveltejs/kit";
|
||||||
|
|||||||
Reference in New Issue
Block a user