1- import crypto from 'node:crypto'
21import fs from 'node:fs'
32import os from 'node:os'
43import path from 'node:path'
@@ -55,7 +54,7 @@ const DEFAULT_O = 'Tlsx Stacks RootCA'
5554// Generate a new Root CA Certificate
5655export async function CreateRootCA ( ) {
5756 // Create a new Keypair for the Root CA
58- const { privateKey, publicKey } = forge . pki . rsa . generateKeyPair ( 2048 )
57+ const { privateKey, publicKey } = pki . rsa . generateKeyPair ( 2048 )
5958
6059 // Define the attributes for the new Root CA
6160 const attributes = [
@@ -90,31 +89,31 @@ export async function CreateRootCA() {
9089 ]
9190
9291 // Create an empty Certificate
93- const cert = forge . pki . createCertificate ( )
92+ const CAcert = pki . createCertificate ( )
9493
9594 // Set the Certificate attributes for the new Root CA
96- cert . publicKey = publicKey
97- cert . privateKey = privateKey
98- cert . serialNumber = randomSerialNumber ( )
99- cert . validity . notBefore = getCertNotBefore ( )
100- cert . validity . notAfter = getCANotAfter ( cert . validity . notBefore )
101- cert . setSubject ( attributes )
102- cert . setIssuer ( attributes )
103- cert . setExtensions ( extensions )
95+ CAcert . publicKey = publicKey
96+ CAcert . privateKey = privateKey
97+ CAcert . serialNumber = randomSerialNumber ( )
98+ CAcert . validity . notBefore = getCertNotBefore ( )
99+ CAcert . validity . notAfter = getCANotAfter ( CAcert . validity . notBefore )
100+ CAcert . setSubject ( attributes )
101+ CAcert . setIssuer ( attributes )
102+ CAcert . setExtensions ( extensions )
104103
105104 // Self-sign the Certificate
106- cert . sign ( privateKey , forge . md . sha512 . create ( ) )
105+ CAcert . sign ( privateKey , forge . md . sha512 . create ( ) )
107106
108107 // Convert to PEM format
109- const pemCert = forge . pki . certificateToPem ( cert )
110- const pemKey = forge . pki . privateKeyToPem ( privateKey )
108+ const pemCert = pki . certificateToPem ( CAcert )
109+ const pemKey = pki . privateKeyToPem ( privateKey )
111110
112111 // Return the PEM encoded cert and private key
113112 return {
114113 certificate : pemCert ,
115114 privateKey : pemKey ,
116- notBefore : cert . validity . notBefore ,
117- notAfter : cert . validity . notAfter ,
115+ notBefore : CAcert . validity . notBefore ,
116+ notAfter : CAcert . validity . notAfter ,
118117 }
119118}
120119
@@ -188,12 +187,12 @@ export async function generateCert(
188187 } ,
189188 {
190189 name : 'subjectAltName' ,
191- altNames : { type : 2 , value : domain } ,
190+ altNames : [ { type : 2 , value : domain } ] ,
192191 } ,
193192 ]
194193
195194 // Create an empty Certificate
196- const newHostCert = forge . pki . createCertificate ( )
195+ const newHostCert = pki . createCertificate ( )
197196 newHostCert . publicKey = hostKeys . publicKey
198197
199198 // Set the attributes for the new Host Certificate
@@ -222,7 +221,11 @@ export interface AddCertOptions {
222221 customCertPath ?: string
223222}
224223
225- export async function addCertToSystemTrustStoreAndSaveCerts ( cert : string , CAcert : string , options ?: AddCertOptions ) {
224+ export async function addCertToSystemTrustStoreAndSaveCerts (
225+ cert : { certificate : string ; privateKey : string } ,
226+ CAcert : string ,
227+ options ?: AddCertOptions ,
228+ ) {
226229 const certPath = storeCert ( cert , options )
227230 const CAcertPath = storeCACert ( CAcert , options )
228231
@@ -253,30 +256,37 @@ export async function addCertToSystemTrustStoreAndSaveCerts(cert: string, CAcert
253256 return certPath
254257}
255258
256- export function storeCert ( cert : string , options ?: AddCertOptions ) {
259+ export function storeCert ( cert : { certificate : string ; privateKey : string } , options ?: AddCertOptions ) {
257260 // Construct the path using os.homedir() and path.join()
258261 const certPath = options ?. customCertPath || path . join ( os . homedir ( ) , '.stacks' , 'ssl' , `stacks.localhost.crt` )
259262
263+ const certKeyPath = options ?. customCertPath || path . join ( os . homedir ( ) , '.stacks' , 'ssl' , `stacks.localhost.crt.key` )
264+
260265 // Ensure the directory exists before writing the file
261266 const certDir = path . dirname ( certPath )
262267 if ( ! fs . existsSync ( certDir ) ) fs . mkdirSync ( certDir , { recursive : true } )
268+ fs . writeFileSync ( certPath , cert . certificate )
269+
270+ // Ensure the directory exists before writing the file
271+ const certKeyDir = path . dirname ( certKeyPath )
272+ if ( ! fs . existsSync ( certKeyDir ) ) fs . mkdirSync ( certKeyDir , { recursive : true } )
263273
264- fs . writeFileSync ( certPath , cert )
274+ fs . writeFileSync ( certKeyPath , cert . privateKey )
265275
266276 return certPath
267277}
268278
269279export function storeCACert ( CAcert : string , options ?: AddCertOptions ) {
270280 // Construct the path using os.homedir() and path.join()
271- const certPath = options ?. customCertPath || path . join ( os . homedir ( ) , '.stacks' , 'ssl' , `stacks.localhost.ca.crt` )
281+ const CAcertPath = options ?. customCertPath || path . join ( os . homedir ( ) , '.stacks' , 'ssl' , `stacks.localhost.ca.crt` )
272282
273283 // Ensure the directory exists before writing the file
274- const certDir = path . dirname ( certPath )
275- if ( ! fs . existsSync ( certDir ) ) fs . mkdirSync ( certDir , { recursive : true } )
284+ const CacertDir = path . dirname ( CAcertPath )
285+ if ( ! fs . existsSync ( CacertDir ) ) fs . mkdirSync ( CacertDir , { recursive : true } )
276286
277- fs . writeFileSync ( certPath , CAcert )
287+ fs . writeFileSync ( CAcertPath , CAcert )
278288
279- return certPath
289+ return CAcertPath
280290}
281291
282- export { tls , pki , forge }
292+ export { tls , pki , forge }
0 commit comments