@@ -143,7 +143,9 @@ async function generateCertificate({
143
143
issuerAttrsExtra ?: Array < { [ key : string ] : Array < string > } > ;
144
144
now ?: Date ;
145
145
} ) : Promise < X509Certificate > {
146
- const subjectPublicKey = subjectKeyPair . publicKey ;
146
+ const certIdNum = parseInt ( certId )
147
+ const iss = certIdNum == 0 ? certIdNum : certIdNum - 1 ;
148
+ const sub = certIdNum ;
147
149
const subjectPublicCryptoKey = await importPublicKey (
148
150
subjectKeyPair . publicKey ,
149
151
) ;
@@ -179,14 +181,14 @@ async function generateCertificate({
179
181
// Because the OID is what is encoded into ASN.1
180
182
const subjectAttrs = [
181
183
{
182
- CN : [ 'SubjectID' ] ,
184
+ CN : [ ` ${ sub } ` ] ,
183
185
} ,
184
186
// Filter out conflicting CN attributes
185
187
...subjectAttrsExtra . filter ( ( attr ) => ! ( 'CN' in attr ) ) ,
186
188
] ;
187
189
const issuerAttrs = [
188
190
{
189
- CN : [ 'IssuerId' ] ,
191
+ CN : [ ` ${ iss } ` ] ,
190
192
} ,
191
193
// Filter out conflicting CN attributes
192
194
...issuerAttrsExtra . filter ( ( attr ) => ! ( 'CN' in attr ) ) ,
@@ -203,7 +205,11 @@ async function generateCertificate({
203
205
publicKey : subjectPublicCryptoKey ,
204
206
signingKey : subjectPrivateCryptoKey ,
205
207
extensions : [
206
- new x509 . BasicConstraintsExtension ( true ) ,
208
+ new x509 . BasicConstraintsExtension (
209
+ true ,
210
+ undefined ,
211
+ true ,
212
+ ) ,
207
213
new x509 . KeyUsagesExtension (
208
214
x509 . KeyUsageFlags . keyCertSign |
209
215
x509 . KeyUsageFlags . cRLSign |
@@ -212,6 +218,7 @@ async function generateCertificate({
212
218
x509 . KeyUsageFlags . keyAgreement |
213
219
x509 . KeyUsageFlags . keyEncipherment |
214
220
x509 . KeyUsageFlags . dataEncipherment ,
221
+ true ,
215
222
) ,
216
223
new x509 . ExtendedKeyUsageExtension ( [
217
224
extendedKeyUsageFlags . serverAuth ,
@@ -239,9 +246,10 @@ async function createTLSConfigWithChain(
239
246
) : Promise < {
240
247
certChainPem : string ;
241
248
privKeyPem : string ;
249
+ caPem : string ;
242
250
} > {
243
251
if ( keyPairs . length === 0 ) throw Error ( 'Must have at least 1 keypair' ) ;
244
- let num = 0 ;
252
+ let num = - 1 ;
245
253
const defaultNumGen = ( ) => {
246
254
num += 1 ;
247
255
return `${ num } ` ;
@@ -264,13 +272,17 @@ async function createTLSConfigWithChain(
264
272
previousKeyPair = keyPair ;
265
273
}
266
274
let certChainPEM = '' ;
275
+ let caPem : string | null = null ;
267
276
for ( const certificate of certChain ) {
268
- certChainPEM += certToPEM ( certificate ) ;
277
+ const pem = certToPEM ( certificate )
278
+ if ( caPem == null ) caPem = pem ;
279
+ certChainPEM += pem ;
269
280
}
270
281
271
282
return {
272
283
privKeyPem : privateKeyToPEM ( previousKeyPair ! . privateKey ) ,
273
284
certChainPem : certChainPEM ,
285
+ caPem : caPem ! ,
274
286
} ;
275
287
}
276
288
0 commit comments