convert PKCS#1 keys to PKCS#8 at load time

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