Skip to content

Commit 319841f

Browse files
committed
tls: Improve getCACertificates() caching and test
1 parent a335306 commit 319841f

File tree

4 files changed

+50
-23
lines changed

4 files changed

+50
-23
lines changed

β€Žlib/tls.jsβ€Ž

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ function cacheDefaultCACertificates() {
168168
return defaultCACertificates;
169169
}
170170

171+
const certificateCache = { __proto__: null };
172+
171173
function getCACertificates(options = {}) {
172174
if (typeof options === 'string') {
173175
options = { type: options };
@@ -177,11 +179,37 @@ function getCACertificates(options = {}) {
177179

178180
const {
179181
type = 'default',
180-
format = 'string',
182+
format = 'pem',
181183
} = options;
182184

183185
validateString(type, 'type');
184-
validateOneOf(format, 'format', ['string', 'buffer', 'x509']);
186+
validateOneOf(format, 'format', ['pem', 'der', 'x509', 'string', 'buffer']);
187+
188+
let effectiveFormat = format;
189+
if (format === 'string') {
190+
effectiveFormat = 'pem';
191+
} else if (format === 'buffer') {
192+
effectiveFormat = 'der';
193+
}
194+
195+
if (certificateCache[type]) {
196+
const cachedCerts = certificateCache[type];
197+
198+
if (effectiveFormat === 'pem') {
199+
return cachedCerts;
200+
}
201+
202+
const buffers = cachedCerts.map((cert) => {
203+
const base64 = cert.replace(/(?:\s|-----BEGIN CERTIFICATE-----|-----END CERTIFICATE-----)+/g, '');
204+
return Buffer.from(base64, 'base64');
205+
});
206+
207+
if (effectiveFormat === 'der') {
208+
return buffers;
209+
}
210+
211+
return buffers.map((buf) => new X509Certificate(buf));
212+
}
185213

186214
let certs;
187215
switch (type) {
@@ -192,32 +220,28 @@ function getCACertificates(options = {}) {
192220
default: throw new ERR_INVALID_ARG_VALUE('type', type);
193221
}
194222

195-
if (format === 'string') {
196-
// Return PEM strings directly
197-
return certs.map((cert) => {
198-
if (typeof cert === 'string') return cert;
199-
if (Buffer.isBuffer(cert)) return cert.toString('ascii');
200-
throw new ERR_INVALID_ARG_VALUE('cert', cert);
201-
});
202-
}
203-
204-
const buffers = certs.map((cert) => {
205-
if (Buffer.isBuffer(cert)) return cert;
223+
const pemCerts = certs.map((cert) => {
206224
if (typeof cert === 'string') {
207-
const base64 = cert
208-
.replace(/-----BEGIN CERTIFICATE-----/g, '')
209-
.replace(/-----END CERTIFICATE-----/g, '')
210-
.replace(/\s+/g, '');
211-
return Buffer.from(base64, 'base64');
225+
return cert;
212226
}
213-
throw new ERR_INVALID_ARG_VALUE('cert', cert);
227+
return `-----BEGIN CERTIFICATE-----\n${cert.toString('base64').match(/.{1,64}/g).join('\n')}\n-----END CERTIFICATE-----`;
228+
});
229+
certificateCache[type] = pemCerts;
230+
231+
if (effectiveFormat === 'pem') {
232+
return pemCerts;
233+
}
234+
235+
const derBuffers = pemCerts.map((cert) => {
236+
const base64 = cert.replace(/(?:\s|-----BEGIN CERTIFICATE-----|-----END CERTIFICATE-----)+/g, '');
237+
return Buffer.from(base64, 'base64');
214238
});
215239

216-
if (format === 'buffer') {
217-
return buffers;
240+
if (effectiveFormat === 'der') {
241+
return derBuffers;
218242
}
219243

220-
return buffers.map((buf) => new X509Certificate(buf));
244+
return derBuffers.map((buf) => new X509Certificate(buf));
221245
}
222246

223247
exports.getCACertificates = getCACertificates;

β€Žtest/parallel/test-tls-get-ca-certificates-bundled.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ const certs2 = tls.getCACertificates('bundled');
2020
assertIsCAArray(certs2);
2121

2222
assert.deepStrictEqual(certs2, tls.rootCertificates);
23+
assert.strictEqual(certs, tls.getCACertificates({ type: 'bundled', format: 'string' }));

β€Žtest/parallel/test-tls-get-ca-certificates-default.jsβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ assert.deepStrictEqual(certs, certs2);
1616

1717
assert.deepStrictEqual(certs, tls.getCACertificates({ type: 'default', format: 'string' }));
1818

19-
const certs3 = tls.getCACertificates('bundled');
19+
const certs3 = tls.getCACertificates('default');
2020
assertIsCAArray(certs3);
2121

2222
assert.deepStrictEqual(certs3, tls.rootCertificates);
23+
assert.strictEqual(certs2, tls.getCACertificates({ type: 'default', format: 'string' }));

β€Žtest/parallel/test-tls-get-ca-certificates-system.jsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ const certs = tls.getCACertificates('bundled');
3131
assertIsCAArray(certs);
3232

3333
assert.deepStrictEqual(certs, tls.rootCertificates);
34+
assert.strictEqual(systemCerts, tls.getCACertificates({ type: 'system', format: 'string' }));

0 commit comments

Comments
Β (0)