Skip to content

Commit 336468e

Browse files
committed
address PR feedback
1 parent 4d79cdc commit 336468e

File tree

1 file changed

+9
-22
lines changed

1 file changed

+9
-22
lines changed

src/util/tls.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ export async function generateCACertificate(options: {
6464
organizationName?: string,
6565
countryName?: string,
6666
bits?: number,
67-
contrainToDomains?: string[]
67+
nameConstraints?: {
68+
permitted?: string[]
69+
}
6870
} = {}) {
6971
options = _.defaults({}, options, {
7072
commonName: 'Mockttp Testing CA - DO NOT TRUST - TESTING ONLY',
@@ -104,12 +106,13 @@ export async function generateCACertificate(options: {
104106
{ name: 'keyUsage', keyCertSign: true, digitalSignature: true, nonRepudiation: true, cRLSign: true, critical: true },
105107
{ name: 'subjectKeyIdentifier' },
106108
];
107-
if(options.contrainToDomains && options.contrainToDomains.length > 0) {
109+
const permittedDomains = options.nameConstraints?.permitted || [];
110+
if(permittedDomains.length > 0) {
108111
extensions.push({
109112
critical: true,
110113
name: 'nameConstraints',
111114
value: generateNameConstraints({
112-
permitted: options.contrainToDomains,
115+
permitted: permittedDomains,
113116
}),
114117
})
115118
}
@@ -129,11 +132,6 @@ export async function generateCACertificate(options: {
129132

130133

131134
type GenerateNameConstraintsInput = {
132-
/**
133-
* Array of excluded domains
134-
*/
135-
excluded?: string[];
136-
137135
/**
138136
* Array of permitted domains
139137
*/
@@ -147,7 +145,7 @@ type GenerateNameConstraintsInput = {
147145
function generateNameConstraints(
148146
input: GenerateNameConstraintsInput
149147
): forge.asn1.Asn1 {
150-
const ipsToSequence = (ips: string[]) =>
148+
const domainsToSequence = (ips: string[]) =>
151149
ips.map((domain) => {
152150
return asn1.create(asn1.Class.UNIVERSAL, asn1.Type.SEQUENCE, true, [
153151
asn1.create(
@@ -161,24 +159,13 @@ function generateNameConstraints(
161159

162160
const permittedAndExcluded: forge.asn1.Asn1[] = [];
163161

164-
if (input.permitted !== undefined) {
162+
if (input.permitted && input.permitted.length > 0) {
165163
permittedAndExcluded.push(
166164
asn1.create(
167165
asn1.Class.CONTEXT_SPECIFIC,
168166
0,
169167
true,
170-
ipsToSequence(input.permitted)
171-
)
172-
);
173-
}
174-
175-
if (input.excluded !== undefined) {
176-
permittedAndExcluded.push(
177-
asn1.create(
178-
asn1.Class.CONTEXT_SPECIFIC,
179-
1,
180-
true,
181-
ipsToSequence(input.excluded)
168+
domainsToSequence(input.permitted)
182169
)
183170
);
184171
}

0 commit comments

Comments
 (0)