initial commit with imported utils from old github project
This commit is contained in:
commit
66bb463e23
39 files changed
+1944
No files matched your search
@@ -0,0 +1,43 @@
|
||||
"use server";
|
||||
import { error } from "console";
|
||||
import { subtle } from "crypto";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function storeAccessKey_Cookie(key: CryptoKey) {
|
||||
if (key.extractable) {
|
||||
try {
|
||||
const exported = await subtle.exportKey("raw", key);
|
||||
const exportedKeyBuffer = new Uint8Array(exported);
|
||||
const key_base64 = Buffer.from(exportedKeyBuffer).toString("base64");
|
||||
(await cookies()).set("user_identity", key_base64);
|
||||
} catch (err) {
|
||||
throw error.apply(err);
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeAccessKey_Cookie() {
|
||||
(await cookies()).set("user_identity", "");
|
||||
}
|
||||
|
||||
export async function retreiveAccessKey_Cookie() {
|
||||
const cookieStore = await cookies();
|
||||
try {
|
||||
const key_base64 = cookieStore.get("user_identity");
|
||||
if (key_base64 == null || key_base64.value.length < 1) {
|
||||
return null;
|
||||
}
|
||||
const key_raw = Uint8Array.from(atob(key_base64.value), (c) =>
|
||||
c.charCodeAt(0),
|
||||
);
|
||||
const key = subtle.importKey("raw", key_raw, "AES-GCM", true, [
|
||||
"encrypt",
|
||||
"decrypt",
|
||||
]);
|
||||
return key;
|
||||
} catch (err) {
|
||||
return error.apply(err);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
"use server";
|
||||
import { subtle } from "crypto";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { deriveKey } from "./encryption";
|
||||
import { retreiveAccessKey_Cookie } from "./access_storage";
|
||||
|
||||
export async function setAccessKey(plain_pw: string) {
|
||||
const supabase = await createClient();
|
||||
const { data: key_data, error } = await supabase
|
||||
.from("users")
|
||||
.select("private_key_enc, salt, iv, auth_tag")
|
||||
.eq("id", (await supabase.auth.getUser()).data.user?.id)
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
return null;
|
||||
}
|
||||
const salt_Base64 = key_data?.salt || "";
|
||||
const encryptedPrivateKey_Base64 = key_data?.private_key_enc || "";
|
||||
const authTag_base64 = key_data?.auth_tag || "";
|
||||
|
||||
const encodedPrivateKey = Uint8Array.from(
|
||||
atob(encryptedPrivateKey_Base64),
|
||||
(c) => c.charCodeAt(0),
|
||||
);
|
||||
const authTag = Uint8Array.from(atob(authTag_base64), (c) => c.charCodeAt(0));
|
||||
const salt = Uint8Array.from(atob(salt_Base64), (c) => c.charCodeAt(0));
|
||||
|
||||
const dataWithAuthTag = new Uint8Array(
|
||||
encodedPrivateKey.length + authTag.length,
|
||||
);
|
||||
dataWithAuthTag.set(encodedPrivateKey, 0);
|
||||
dataWithAuthTag.set(authTag, encodedPrivateKey.length);
|
||||
|
||||
const key = await deriveKey(plain_pw, salt);
|
||||
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
export async function decryptPrivateKey() {
|
||||
const supabase = await createClient();
|
||||
const { data: key_data, error } = await supabase
|
||||
.from("users")
|
||||
.select("private_key_enc, salt, iv, auth_tag")
|
||||
.eq("id", (await supabase.auth.getUser()).data.user?.id)
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
return null;
|
||||
}
|
||||
const iv_Base64 = key_data?.iv || "";
|
||||
const encryptedPrivateKey_Base64 = key_data?.private_key_enc || "";
|
||||
const authTag_base64 = key_data?.auth_tag || "";
|
||||
|
||||
const encodedPrivateKey = Uint8Array.from(
|
||||
atob(encryptedPrivateKey_Base64),
|
||||
(c) => c.charCodeAt(0),
|
||||
);
|
||||
const authTag = Uint8Array.from(atob(authTag_base64), (c) => c.charCodeAt(0));
|
||||
const iv = Uint8Array.from(atob(iv_Base64), (c) => c.charCodeAt(0));
|
||||
|
||||
const dataWithAuthTag = new Uint8Array(
|
||||
encodedPrivateKey.length + authTag.length,
|
||||
);
|
||||
dataWithAuthTag.set(encodedPrivateKey, 0);
|
||||
dataWithAuthTag.set(authTag, encodedPrivateKey.length);
|
||||
|
||||
const key = await retreiveAccessKey_Cookie();
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
const decryptedContent = await subtle.decrypt(
|
||||
{ name: "AES-GCM", iv, tagLength: 128 },
|
||||
key,
|
||||
dataWithAuthTag,
|
||||
);
|
||||
const decodedPrivateKey = new TextDecoder().decode(decryptedContent);
|
||||
return decodedPrivateKey;
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
"use server";
|
||||
import { generateKeyPairSync, getRandomValues, subtle } from "crypto";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { deriveKey } from "./encryption";
|
||||
|
||||
export async function generateKeyPair(plain_pw: string, user_id: string) {
|
||||
const supabase = await createClient();
|
||||
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
|
||||
modulusLength: 4096,
|
||||
publicKeyEncoding: {
|
||||
type: "spki",
|
||||
format: "pem",
|
||||
},
|
||||
privateKeyEncoding: {
|
||||
type: "pkcs8",
|
||||
format: "pem",
|
||||
cipher: "aes-256-cbc",
|
||||
passphrase: "",
|
||||
},
|
||||
});
|
||||
|
||||
const salt = getRandomValues(new Uint8Array(16));
|
||||
const iv = getRandomValues(new Uint8Array(32));
|
||||
const key = await deriveKey(plain_pw, salt);
|
||||
const encodedPrivateKey = new TextEncoder().encode(privateKey);
|
||||
const encryptedContent = await subtle.encrypt(
|
||||
{
|
||||
name: "AES-GCM",
|
||||
iv,
|
||||
tagLength: 128, // 128-bit tag length
|
||||
},
|
||||
key,
|
||||
encodedPrivateKey,
|
||||
);
|
||||
|
||||
const encryptedPrivateKey = encryptedContent.slice(
|
||||
0,
|
||||
encryptedContent.byteLength - 16,
|
||||
);
|
||||
const encryptedPrivateKey_Base64 =
|
||||
Buffer.from(encryptedPrivateKey).toString("base64");
|
||||
|
||||
const authTag = encryptedContent.slice(encryptedContent.byteLength - 16);
|
||||
const authTag_Base64 = Buffer.from(authTag).toString("base64");
|
||||
const salt_Base64 = Buffer.from(salt).toString("base64");
|
||||
const iv_Base64 = Buffer.from(iv).toString("base64");
|
||||
|
||||
const { error } = await supabase
|
||||
.from("users")
|
||||
.update({
|
||||
private_key_enc: encryptedPrivateKey_Base64,
|
||||
public_key_dec: publicKey,
|
||||
salt: salt_Base64,
|
||||
iv: iv_Base64,
|
||||
auth_tag: authTag_Base64,
|
||||
})
|
||||
.eq("id", user_id);
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
"use server";
|
||||
import { subtle } from "crypto";
|
||||
|
||||
export async function deriveKey(passphrase: string, salt: Uint8Array) {
|
||||
const encodedPassword = new TextEncoder().encode(passphrase);
|
||||
const baseKey = await subtle.importKey(
|
||||
"raw",
|
||||
encodedPassword,
|
||||
{ name: "PBKDF2" },
|
||||
false,
|
||||
["deriveKey"],
|
||||
);
|
||||
|
||||
const derivedKey = await subtle.deriveKey(
|
||||
{
|
||||
name: "PBKDF2",
|
||||
salt,
|
||||
iterations: 600000,
|
||||
hash: "SHA-256",
|
||||
},
|
||||
baseKey,
|
||||
{ name: "AES-GCM", length: 256 },
|
||||
true,
|
||||
["encrypt", "decrypt"],
|
||||
);
|
||||
|
||||
return derivedKey;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
"use server";
|
||||
|
||||
import { generateKeyPair } from "@/utils/security/encrypt_private_key";
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
|
||||
export async function validateIdentity(plain_pw: string) {
|
||||
const supabase = createClient();
|
||||
const user_id = (await (await supabase).auth.getUser()).data.user?.id;
|
||||
if (user_id == null) {
|
||||
return null;
|
||||
}
|
||||
const { data, error } = await (await supabase)
|
||||
.from("users")
|
||||
.select("has_identity")
|
||||
.eq("id", user_id)
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!data.has_identity) {
|
||||
generateKeyPair(plain_pw, user_id);
|
||||
const { error } = await (await supabase)
|
||||
.from("users")
|
||||
.update({ has_identity: true })
|
||||
.eq("id", user_id)
|
||||
.single();
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user