11'use strict'
22
33const { randomBytes} = require ( 'crypto' )
4- const fs = require ( 'fs' )
5- const path = require ( 'path' )
4+ import fs = require( 'fs' )
5+ import path = require( 'path' )
66const { tmpdir} = require ( 'os' )
7- const helper = require ( './helper.js ' )
8- const openssl = require ( './openssl.js ' )
9- const pem = require ( './pem.js ' )
7+ import helper = require( './helper' )
8+ import openssl = require( './openssl' )
9+ const pem = require ( './pem' )
1010
1111const createCSR = pem . createCSR
1212
1313const DAY_MS = 24 * 60 * 60 * 1000
1414
15+ interface CAConstructorOptions {
16+ key ?: string | Buffer
17+ certificate ?: string | Buffer
18+ keyPassword ?: string
19+ password ?: string
20+ chain ?: Array < string | Buffer >
21+ defaultDays ?: number
22+ }
23+
24+ interface IssueCertificateOptions {
25+ csr ?: string
26+ clientKey ?: string
27+ clientKeyPassword ?: string
28+ serial ?: string | number | Buffer
29+ hash ?: string
30+ copyExtensions ?: boolean
31+ config ?: string
32+ extFile ?: string
33+ startDate ?: Date | number | string
34+ endDate ?: Date | number | string
35+ days ?: number
36+ keyBitsize ?: number
37+ country ?: string
38+ state ?: string
39+ locality ?: string
40+ organization ?: string | string [ ]
41+ organizationUnit ?: string | string [ ]
42+ commonName ?: string
43+ altNames ?: string [ ]
44+ emailAddress ?: string
45+ csrConfigFile ?: string
46+ password ?: string
47+ dc ?: string | string [ ]
48+ C ?: string
49+ ST ?: string
50+ L ?: string
51+ O ?: string | string [ ]
52+ OU ?: string | string [ ]
53+ CN ?: string
54+ }
55+
56+ interface IssueResult {
57+ csr : string | undefined
58+ clientKey : string
59+ certificate : string
60+ caCertificate : string
61+ caChain : string [ ]
62+ serial : string
63+ validity : { start : Date ; end : Date }
64+ }
65+
66+ type IssueCallback = ( err ?: Error | null , result ?: IssueResult ) => void
67+
1568class CertificateAuthority {
16- constructor ( options = { } ) {
69+ declare key : string
70+ declare certificate : string
71+ declare password : string | null
72+ declare chain : string [ ]
73+ declare defaultDays : number
74+
75+ constructor ( options : CAConstructorOptions = { } ) {
1776 if ( ! options . key ) {
1877 throw new Error ( 'CA private key is required' )
1978 }
@@ -27,17 +86,17 @@ class CertificateAuthority {
2786 this . chain = Array . isArray ( options . chain ) ? options . chain . map ( function ( value ) {
2887 return value . toString ( )
2988 } ) : [ ]
30- this . defaultDays = Number . isFinite ( options . defaultDays ) && options . defaultDays > 0 ? Number ( options . defaultDays ) : 7
89+ this . defaultDays = Number . isFinite ( options . defaultDays ) && ( options . defaultDays as number ) > 0 ? Number ( options . defaultDays ) : 7
3190 }
3291
33- issueCertificate ( options , callback ) {
92+ issueCertificate ( options ?: IssueCertificateOptions | IssueCallback , callback ?: IssueCallback ) {
3493 if ( ! callback && typeof options === 'function' ) {
3594 callback = options
3695 options = undefined
3796 }
3897
3998 if ( ! callback ) {
40- return new Promise ( ( resolve , reject ) => {
99+ return new Promise < IssueResult | undefined > ( ( resolve , reject ) => {
41100 this . issueCertificate ( options , function ( err , result ) {
42101 if ( err ) {
43102 return reject ( err )
@@ -47,9 +106,9 @@ class CertificateAuthority {
47106 } )
48107 }
49108
50- const issueOptions = Object . assign ( { } , options || { } )
109+ const issueOptions = Object . assign ( { } , options || { } ) as IssueCertificateOptions
51110
52- const handleResult = ( err , signResult ) => {
111+ const handleResult = ( err : Error | null , signResult : any ) => {
53112 if ( err ) {
54113 return callback ( err )
55114 }
@@ -76,7 +135,7 @@ class CertificateAuthority {
76135 return
77136 }
78137
79- createCSR ( issueOptions , ( err , csrData ) => {
138+ createCSR ( issueOptions , ( err : Error | null , csrData : { csr : string ; clientKey : string ; config : string | null } ) => {
80139 if ( err ) {
81140 return callback ( err )
82141 }
@@ -89,25 +148,25 @@ class CertificateAuthority {
89148 } )
90149 }
91150
92- _signCertificate ( options , callback ) {
93- let validity
151+ _signCertificate ( options : IssueCertificateOptions , callback : ( err : Error | null , result ?: { certificate : string ; serial : string ; validity : { start : Date ; end : Date } } ) => void ) {
152+ let validity : { asn1 : { start : string ; end : string } , dates : { start : Date ; end : Date } }
94153 try {
95154 validity = resolveValidity ( options , this . defaultDays )
96155 } catch ( err ) {
97- return callback ( err )
156+ return callback ( err as Error )
98157 }
99158
100- let serial
159+ let serial : string
101160 try {
102161 serial = formatSerial ( options . serial )
103162 } catch ( err ) {
104- return callback ( err )
163+ return callback ( err as Error )
105164 }
106165
107166 const serialValue = serial . slice ( 2 ) . toUpperCase ( ) || '1'
108- const passwordFiles = [ ]
109- let workDir
110- let certPath
167+ const passwordFiles : string [ ] = [ ]
168+ let workDir : string | undefined
169+ let certPath : string | undefined
111170
112171 try {
113172 workDir = fs . mkdtempSync ( path . join ( tmpdir ( ) , 'pem-ca-' ) )
@@ -117,7 +176,7 @@ class CertificateAuthority {
117176 certPath = path . join ( workDir , 'issued.pem' )
118177 } catch ( err ) {
119178 const cleanupErr = removeDirectory ( workDir )
120- return callback ( cleanupErr || err )
179+ return callback ( cleanupErr || ( err as Error ) )
121180 }
122181
123182 const configContent = buildCaConfig ( workDir , options . hash || 'sha256' , options . copyExtensions )
@@ -139,10 +198,10 @@ class CertificateAuthority {
139198 '-in' ,
140199 '--TMPFILE--' ,
141200 '-out' ,
142- certPath
201+ certPath as string
143202 ]
144203
145- const tmpfiles = [ configContent , this . key , this . certificate , options . csr ]
204+ const tmpfiles = [ configContent , this . key , this . certificate , options . csr as string ]
146205
147206 if ( this . password ) {
148207 helper . createPasswordFile ( {
@@ -157,14 +216,14 @@ class CertificateAuthority {
157216 params . push ( 'v3_req' )
158217 params . push ( '-extfile' )
159218 params . push ( '--TMPFILE--' )
160- tmpfiles . push ( options . config )
219+ tmpfiles . push ( options . config as string )
161220 } else if ( options . extFile ) {
162221 params . push ( '-extfile' )
163- params . push ( options . extFile )
222+ params . push ( options . extFile as string )
164223 }
165224
166- const finalize = ( err , certificate ) => {
167- const removePasswordFiles = ( removeErr ) => {
225+ const finalize = ( err : Error | null , certificate ?: any ) => {
226+ const removePasswordFiles = ( removeErr : NodeJS . ErrnoException | null ) => {
168227 const dirErr = removeDirectory ( workDir )
169228 const finalErr = err || removeErr || dirErr
170229 if ( finalErr ) {
@@ -190,16 +249,16 @@ class CertificateAuthority {
190249 }
191250 let certificate
192251 try {
193- certificate = fs . readFileSync ( certPath , 'utf-8' )
252+ certificate = fs . readFileSync ( certPath as string , 'utf-8' )
194253 } catch ( readErr ) {
195- return finalize ( readErr )
254+ return finalize ( readErr as Error )
196255 }
197256 finalize ( null , certificate )
198257 } )
199258 }
200259}
201260
202- function buildCaConfig ( workDir , digest , copyExtensions ) {
261+ function buildCaConfig ( workDir : string , digest : string , copyExtensions : boolean | undefined ) : string {
203262 const lines = [
204263 '[ ca ]' ,
205264 'default_ca = pem_ca' ,
@@ -241,7 +300,7 @@ function buildCaConfig(workDir, digest, copyExtensions) {
241300 return lines . join ( '\n' )
242301}
243302
244- function removeDirectory ( directory ) {
303+ function removeDirectory ( directory ?: string ) : NodeJS . ErrnoException | null {
245304 if ( ! directory ) {
246305 return null
247306 }
@@ -250,14 +309,14 @@ function removeDirectory(directory) {
250309 fs . rmSync ( directory , { recursive : true } )
251310 return null
252311 } catch ( err ) {
253- if ( err && err . code === 'ENOENT' ) {
312+ if ( err && ( err as NodeJS . ErrnoException ) . code === 'ENOENT' ) {
254313 return null
255314 }
256- return err
315+ return err as NodeJS . ErrnoException
257316 }
258317}
259318
260- function formatSerial ( serial ) {
319+ function formatSerial ( serial : string | Buffer | number | undefined | null ) {
261320 if ( serial === undefined || serial === null ) {
262321 return '0x' + randomBytes ( 20 ) . toString ( 'hex' )
263322 }
@@ -293,9 +352,9 @@ function formatSerial(serial) {
293352 return '0x' + hexSerial . padStart ( 40 , '0' ) . slice ( - 40 )
294353}
295354
296- function resolveValidity ( options , defaultDays ) {
355+ function resolveValidity ( options : IssueCertificateOptions , defaultDays : number ) {
297356 const start = normalizeDate ( options . startDate ) || new Date ( )
298- let end
357+ let end : Date | null
299358
300359 if ( options . endDate ) {
301360 end = normalizeDate ( options . endDate )
@@ -332,7 +391,7 @@ function resolveValidity(options, defaultDays) {
332391 }
333392}
334393
335- function normalizeDate ( value ) {
394+ function normalizeDate ( value : Date | number | string | undefined | null ) {
336395 if ( ! value ) {
337396 return null
338397 }
@@ -366,8 +425,8 @@ function normalizeDate(value) {
366425 throw new Error ( 'Unsupported date value' )
367426}
368427
369- function formatAsn1Date ( date ) {
370- const pad = ( num ) => {
428+ function formatAsn1Date ( date : Date ) {
429+ const pad = ( num : number ) => {
371430 let s = String ( num )
372431 while ( s . length < 2 ) {
373432 s = '0' + s
@@ -389,14 +448,14 @@ function formatAsn1Date(date) {
389448 return year . toString ( ) + timePortion + 'Z'
390449}
391450
392- function parseAsn1Date ( value ) {
451+ function parseAsn1Date ( value : string ) {
393452 const normalized = value . replace ( / \s + / g, '' )
394453 if ( ! / ^ \d { 12 } Z $ / . test ( normalized ) && ! / ^ \d { 14 } Z $ / . test ( normalized ) ) {
395454 throw new Error ( 'Invalid ASN.1 date value' )
396455 }
397456
398- let offset
399- let year
457+ let offset : number
458+ let year : number
400459
401460 if ( normalized . length === 13 ) { // YYMMDDHHMMSSZ
402461 const twoDigit = Number ( normalized . slice ( 0 , 2 ) )
@@ -419,4 +478,4 @@ function parseAsn1Date(value) {
419478 return date
420479}
421480
422- module . exports = CertificateAuthority
481+ export = CertificateAuthority
0 commit comments