Skip to content

Commit 667db28

Browse files
committed
fix: generated Polykey certs can act as a CA cert now
* Related #9 [ci skip]
1 parent dd90d33 commit 667db28

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

tests/tlsUtils.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ async function generateCertificate({
143143
issuerAttrsExtra?: Array<{ [key: string]: Array<string> }>;
144144
now?: Date;
145145
}): Promise<X509Certificate> {
146-
const subjectPublicKey = subjectKeyPair.publicKey;
146+
const certIdNum = parseInt(certId)
147+
const iss = certIdNum == 0 ? certIdNum : certIdNum - 1;
148+
const sub = certIdNum;
147149
const subjectPublicCryptoKey = await importPublicKey(
148150
subjectKeyPair.publicKey,
149151
);
@@ -179,14 +181,14 @@ async function generateCertificate({
179181
// Because the OID is what is encoded into ASN.1
180182
const subjectAttrs = [
181183
{
182-
CN: ['SubjectID'],
184+
CN: [`${sub}`],
183185
},
184186
// Filter out conflicting CN attributes
185187
...subjectAttrsExtra.filter((attr) => !('CN' in attr)),
186188
];
187189
const issuerAttrs = [
188190
{
189-
CN: ['IssuerId'],
191+
CN: [`${iss}`],
190192
},
191193
// Filter out conflicting CN attributes
192194
...issuerAttrsExtra.filter((attr) => !('CN' in attr)),
@@ -203,7 +205,11 @@ async function generateCertificate({
203205
publicKey: subjectPublicCryptoKey,
204206
signingKey: subjectPrivateCryptoKey,
205207
extensions: [
206-
new x509.BasicConstraintsExtension(true),
208+
new x509.BasicConstraintsExtension(
209+
true,
210+
undefined,
211+
true,
212+
),
207213
new x509.KeyUsagesExtension(
208214
x509.KeyUsageFlags.keyCertSign |
209215
x509.KeyUsageFlags.cRLSign |
@@ -212,6 +218,7 @@ async function generateCertificate({
212218
x509.KeyUsageFlags.keyAgreement |
213219
x509.KeyUsageFlags.keyEncipherment |
214220
x509.KeyUsageFlags.dataEncipherment,
221+
true,
215222
),
216223
new x509.ExtendedKeyUsageExtension([
217224
extendedKeyUsageFlags.serverAuth,
@@ -239,9 +246,10 @@ async function createTLSConfigWithChain(
239246
): Promise<{
240247
certChainPem: string;
241248
privKeyPem: string;
249+
caPem: string;
242250
}> {
243251
if (keyPairs.length === 0) throw Error('Must have at least 1 keypair');
244-
let num = 0;
252+
let num = -1;
245253
const defaultNumGen = () => {
246254
num+=1;
247255
return `${num}`;
@@ -264,13 +272,17 @@ async function createTLSConfigWithChain(
264272
previousKeyPair = keyPair;
265273
}
266274
let certChainPEM = '';
275+
let caPem: string | null = null;
267276
for (const certificate of certChain) {
268-
certChainPEM += certToPEM(certificate);
277+
const pem = certToPEM(certificate)
278+
if (caPem == null) caPem = pem;
279+
certChainPEM += pem;
269280
}
270281

271282
return {
272283
privKeyPem: privateKeyToPEM(previousKeyPair!.privateKey),
273284
certChainPem: certChainPEM,
285+
caPem: caPem!,
274286
};
275287
}
276288

0 commit comments

Comments
 (0)