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

50 lines
1.3 KiB
TypeScript
Raw Normal View History

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