SSL-Sniffer/src/lib/server/utils/openssl-utils.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-07-02 14:49:23 +00:00
import { doesFileExist, loadFile, type FileHandle } from "./filesystem-utils";
import { SERVER_PRIVATE_KEY_PATH, SERVER_PUBLIC_KEY_PATH } from "./constants";
2025-07-02 14:49:23 +00:00
import { shell, type shellOutput } from "./shell-commands";
export async function openSSLInit() {
await openSSLCreatePrivateKey()
await openSSLCreatePublicKey()
}
export async function openSSLCreatePrivateKey() {
// UGLY: may be refactored to output only the private key
2025-07-02 14:49:23 +00:00
const outputPromise = shell(`openssl ecparam -genkey -name secp521r1 -noout | openssl pkcs8 -topk8 -nocrypt`)
// const outputPromise = $`openssl ecparam -genkey -name secp521r1 -noout | openssl pkcs8 -topk8 -nocrypt`.text()
const filePromise = loadFile(SERVER_PRIVATE_KEY_PATH, true)
2025-07-02 14:49:23 +00:00
const [output, file] : [shellOutput, FileHandle]= await Promise.all([
outputPromise,
filePromise
])
2025-07-02 14:49:23 +00:00
await file.write(output.stdout)
}
export async function openSSLCreatePublicKey() {
// UGLY: may be refactored to output only the private key
2025-07-02 14:49:23 +00:00
if (! await doesFileExist(SERVER_PRIVATE_KEY_PATH)) {
// UGLY: make more specific
throw new Error("You must generate the private key before attempting to generate the public one")
}
2025-07-02 14:49:23 +00:00
const outputPromise = shell(`openssl ec -in ${SERVER_PRIVATE_KEY_PATH} -pubout `)
const filePromise = loadFile(SERVER_PUBLIC_KEY_PATH, true)
const [output, file] = await Promise.all([
outputPromise,
filePromise
])
2025-07-02 14:49:23 +00:00
await file.write(output.stdout)
}