Skip to content

Commit 6705fc9

Browse files
committed
Change generateCACertificate subject options to allow arbitrary attrs
This is a breaking change to this API, by moving into the options into a 'subject' option, allowing additional custom fields to be added if required.
1 parent 45f0cc1 commit 6705fc9

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/util/tls.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,27 @@ export type GeneratedCertificate = {
6060
* as HTTPS options to a Mockttp server.
6161
*/
6262
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+
},
6669
bits?: number,
6770
nameConstraints?: {
6871
permitted?: string[]
6972
}
7073
} = {}) {
7174
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:
7281
commonName: 'Mockttp Testing CA - DO NOT TRUST - TESTING ONLY',
7382
organizationName: 'Mockttp',
7483
countryName: 'XX', // ISO-3166-1 alpha-2 'unknown country' code
75-
bits: 2048,
7684
});
7785

7886
const keyPair = await new Promise<forge.pki.rsa.KeyPair>((resolve, reject) => {
@@ -94,12 +102,10 @@ export async function generateCACertificate(options: {
94102
// Valid for the next year by default.
95103
cert.validity.notAfter.setFullYear(cert.validity.notAfter.getFullYear() + 1);
96104

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+
})));
103109

104110
const extensions: any[] = [
105111
{ name: 'basicConstraints', cA: true, critical: true },

test/ca.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ nodeOnly(() => {
5757
});
5858

5959
it("can generate a valid certificate for a domain included in a constrained CA", async () => {
60-
6160
const { cert, key } = constrainedCA.generateCertificate("hello.example.com");
6261

6362
server = https.createServer({ cert, key }, (req: any, res: any) => {
@@ -79,8 +78,7 @@ nodeOnly(() => {
7978
reject(err);
8079
});
8180
req.end();
82-
});
83-
81+
});
8482
});
8583

8684
it("can not generate a valid certificate for a domain not included in a constrained CA", async () => {

0 commit comments

Comments
 (0)