Compare commits

..

2 Commits

Author SHA1 Message Date
HoloPanio 05bab2c90f convert PKCS#1 keys to PKCS#8 at load time 2026-02-25 21:56:09 -06:00
HoloPanio 29b5c986cd convert PKCS#1 keys to PKCS#8 at load time 2026-02-25 21:55:09 -06:00
3 changed files with 44 additions and 26 deletions
+41 -15
View File
@@ -1,4 +1,5 @@
import { readFileSync } from "fs";
import crypto from "crypto";
import { PrismaPg } from "@prisma/adapter-pg";
import { Prisma, PrismaClient } from "../generated/prisma/client";
import * as msal from "@azure/msal-node";
@@ -28,21 +29,46 @@ const isProduction = process.env.NODE_ENV === "production";
const readKeyFile = (path: string) => readFileSync(path).toString();
export const accessTokenPrivateKey = isProduction
? process.env.ACCESS_TOKEN_PRIVATE_KEY!
: readKeyFile(`.accessToken.key`);
export const refreshTokenPrivateKey = isProduction
? process.env.REFRESH_TOKEN_PRIVATE_KEY!
: readKeyFile(`.refreshToken.key`);
export const permissionsPrivateKey = isProduction
? process.env.PERMISSIONS_PRIVATE_KEY!
: readKeyFile(`.permissions.key`);
export const secureValuesPrivateKey = isProduction
? process.env.SECURE_VALUES_PRIVATE_KEY!
: readKeyFile(`.secureValues.key`);
export const secureValuesPublicKey = isProduction
? process.env.SECURE_VALUES_PUBLIC_KEY!
: readKeyFile(`public-keys/.secureValues.pub`);
/**
* Convert a PKCS#1 PEM key to PKCS#8 PEM format.
* The compiled Bun binary on Ubuntu uses an OpenSSL that doesn't auto-detect PKCS#1 format,
* so we normalize all keys to PKCS#8 at load time.
*/
const toPkcs8Private = (pem: string) =>
crypto
.createPrivateKey({ key: pem, format: "pem", type: "pkcs1" })
.export({ type: "pkcs8", format: "pem" }) as string;
const toPkcs8Public = (pem: string) =>
crypto
.createPublicKey({ key: pem, format: "pem", type: "pkcs1" })
.export({ type: "spki", format: "pem" }) as string;
export const accessTokenPrivateKey = toPkcs8Private(
isProduction
? process.env.ACCESS_TOKEN_PRIVATE_KEY!
: readKeyFile(`.accessToken.key`),
);
export const refreshTokenPrivateKey = toPkcs8Private(
isProduction
? process.env.REFRESH_TOKEN_PRIVATE_KEY!
: readKeyFile(`.refreshToken.key`),
);
export const permissionsPrivateKey = toPkcs8Private(
isProduction
? process.env.PERMISSIONS_PRIVATE_KEY!
: readKeyFile(`.permissions.key`),
);
export const secureValuesPrivateKey = toPkcs8Private(
isProduction
? process.env.SECURE_VALUES_PRIVATE_KEY!
: readKeyFile(`.secureValues.key`),
);
export const secureValuesPublicKey = toPkcs8Public(
isProduction
? process.env.SECURE_VALUES_PUBLIC_KEY!
: readKeyFile(`public-keys/.secureValues.pub`),
);
// Microsoft Auth Constants
const msalConfig: msal.Configuration = {
@@ -6,12 +6,8 @@ export const generateSecureValue = (content: string) => {
// Generate a hash of the content
const hash = Password.hash(content);
// Parse the PKCS#1 PEM key into a proper KeyObject
const publicKey = crypto.createPublicKey({
key: secureValuesPublicKey,
format: "pem",
type: "pkcs1",
});
// Parse the PEM key into a proper KeyObject
const publicKey = crypto.createPublicKey(secureValuesPublicKey);
// Encrypt the content using the .secureValues.pub public key
const encrypted = crypto.publicEncrypt(
+1 -5
View File
@@ -3,11 +3,7 @@ import crypto from "crypto";
import { secureValuesPrivateKey } from "../../constants";
import GenericError from "../../Errors/GenericError";
const privateKey = crypto.createPrivateKey({
key: secureValuesPrivateKey,
format: "pem",
type: "pkcs1",
});
const privateKey = crypto.createPrivateKey(secureValuesPrivateKey);
export const readSecureValue = (
encryptedContent: string,