1
- import crypto from 'node:crypto'
2
1
import fs from 'node:fs'
3
2
import os from 'node:os'
4
3
import path from 'node:path'
@@ -55,7 +54,7 @@ const DEFAULT_O = 'Tlsx Stacks RootCA'
55
54
// Generate a new Root CA Certificate
56
55
export async function CreateRootCA ( ) {
57
56
// 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 )
59
58
60
59
// Define the attributes for the new Root CA
61
60
const attributes = [
@@ -90,31 +89,31 @@ export async function CreateRootCA() {
90
89
]
91
90
92
91
// Create an empty Certificate
93
- const cert = forge . pki . createCertificate ( )
92
+ const CAcert = pki . createCertificate ( )
94
93
95
94
// 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 )
104
103
105
104
// Self-sign the Certificate
106
- cert . sign ( privateKey , forge . md . sha512 . create ( ) )
105
+ CAcert . sign ( privateKey , forge . md . sha512 . create ( ) )
107
106
108
107
// 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 )
111
110
112
111
// Return the PEM encoded cert and private key
113
112
return {
114
113
certificate : pemCert ,
115
114
privateKey : pemKey ,
116
- notBefore : cert . validity . notBefore ,
117
- notAfter : cert . validity . notAfter ,
115
+ notBefore : CAcert . validity . notBefore ,
116
+ notAfter : CAcert . validity . notAfter ,
118
117
}
119
118
}
120
119
@@ -188,12 +187,12 @@ export async function generateCert(
188
187
} ,
189
188
{
190
189
name : 'subjectAltName' ,
191
- altNames : { type : 2 , value : domain } ,
190
+ altNames : [ { type : 2 , value : domain } ] ,
192
191
} ,
193
192
]
194
193
195
194
// Create an empty Certificate
196
- const newHostCert = forge . pki . createCertificate ( )
195
+ const newHostCert = pki . createCertificate ( )
197
196
newHostCert . publicKey = hostKeys . publicKey
198
197
199
198
// Set the attributes for the new Host Certificate
@@ -222,7 +221,11 @@ export interface AddCertOptions {
222
221
customCertPath ?: string
223
222
}
224
223
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
+ ) {
226
229
const certPath = storeCert ( cert , options )
227
230
const CAcertPath = storeCACert ( CAcert , options )
228
231
@@ -253,30 +256,37 @@ export async function addCertToSystemTrustStoreAndSaveCerts(cert: string, CAcert
253
256
return certPath
254
257
}
255
258
256
- export function storeCert ( cert : string , options ?: AddCertOptions ) {
259
+ export function storeCert ( cert : { certificate : string ; privateKey : string } , options ?: AddCertOptions ) {
257
260
// Construct the path using os.homedir() and path.join()
258
261
const certPath = options ?. customCertPath || path . join ( os . homedir ( ) , '.stacks' , 'ssl' , `stacks.localhost.crt` )
259
262
263
+ const certKeyPath = options ?. customCertPath || path . join ( os . homedir ( ) , '.stacks' , 'ssl' , `stacks.localhost.crt.key` )
264
+
260
265
// Ensure the directory exists before writing the file
261
266
const certDir = path . dirname ( certPath )
262
267
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 } )
263
273
264
- fs . writeFileSync ( certPath , cert )
274
+ fs . writeFileSync ( certKeyPath , cert . privateKey )
265
275
266
276
return certPath
267
277
}
268
278
269
279
export function storeCACert ( CAcert : string , options ?: AddCertOptions ) {
270
280
// 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` )
272
282
273
283
// 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 } )
276
286
277
- fs . writeFileSync ( certPath , CAcert )
287
+ fs . writeFileSync ( CAcertPath , CAcert )
278
288
279
- return certPath
289
+ return CAcertPath
280
290
}
281
291
282
- export { tls , pki , forge }
292
+ export { tls , pki , forge }
0 commit comments