암호 키 생성 및 등록시 최초 MEK도 함께 생성 및 등록하도록 구현

This commit is contained in:
static
2024-12-30 01:59:09 +09:00
parent d39931c79a
commit 941e2a49bc
10 changed files with 133 additions and 40 deletions

View File

@@ -1,6 +1,11 @@
import { callAPI } from "$lib/hooks";
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) => {
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 challenge = data.challenge as string;
const answer = await decryptRSACiphertext(challenge, privateKey);
const answer = await decryptRSACiphertext(decodeFromBase64(challenge), privateKey);
res = await callAPI("/api/client/verify", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ answer }),
body: JSON.stringify({ answer: encodeToBase64(answer) }),
});
return res.ok;
};
export const storeKeyPairPersistently = async (keyPair: CryptoKeyPair) => {
await storeKeyPairIntoIndexedDB(keyPair.publicKey, keyPair.privateKey);
};
export const requestTokenUpgrade = async (pubKeyBase64: string) => {
const res = await fetch("/api/auth/upgradeToken", {
method: "POST",
@@ -49,6 +58,17 @@ export const requestTokenUpgrade = async (pubKeyBase64: string) => {
return res.ok;
};
export const storeKeyPairPersistently = async (keyPair: CryptoKeyPair) => {
await storeKeyPairIntoIndexedDB(keyPair.publicKey, keyPair.privateKey);
export const requestInitialMekRegistration = async (
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;
};