@@ -60,19 +60,27 @@ export type GeneratedCertificate = {
60
60
* as HTTPS options to a Mockttp server.
61
61
*/
62
62
export async function generateCACertificate ( options : {
63
- commonName ?: string ,
64
- organizationName ?: string ,
65
- countryName ?: string ,
63
+ subject ?: {
64
+ commonName ?: string ,
65
+ organizationName ?: string ,
66
+ countryName ?: string ,
67
+ [ key : string ] : string | undefined // Add any other subject field you like
68
+ } ,
66
69
bits ?: number ,
67
70
nameConstraints ?: {
68
71
permitted ?: string [ ]
69
72
}
70
73
} = { } ) {
71
74
options = _ . defaults ( { } , options , {
75
+ bits : 2048 ,
76
+ } ) ;
77
+
78
+ const subjectOptions = _ . defaults ( { } , options . subject , {
79
+ // These subject fields are required for a fully valid CA cert that will be
80
+ // accepted when imported anywhere:
72
81
commonName : 'Mockttp Testing CA - DO NOT TRUST - TESTING ONLY' ,
73
82
organizationName : 'Mockttp' ,
74
83
countryName : 'XX' , // ISO-3166-1 alpha-2 'unknown country' code
75
- bits : 2048 ,
76
84
} ) ;
77
85
78
86
const keyPair = await new Promise < forge . pki . rsa . KeyPair > ( ( resolve , reject ) => {
@@ -94,12 +102,10 @@ export async function generateCACertificate(options: {
94
102
// Valid for the next year by default.
95
103
cert . validity . notAfter . setFullYear ( cert . validity . notAfter . getFullYear ( ) + 1 ) ;
96
104
97
- cert . setSubject ( [
98
- // All of these are required for a fully valid CA cert that will be accepted when imported anywhere:
99
- { name : 'commonName' , value : options . commonName } ,
100
- { name : 'countryName' , value : options . countryName } ,
101
- { name : 'organizationName' , value : options . organizationName }
102
- ] ) ;
105
+ cert . setSubject ( Object . entries ( subjectOptions ) . map ( ( [ key , value ] ) => ( {
106
+ name : key ,
107
+ value : value
108
+ } ) ) ) ;
103
109
104
110
const extensions : any [ ] = [
105
111
{ name : 'basicConstraints' , cA : true , critical : true } ,
0 commit comments