crypto: add tls.setDefaultCACertificates() · nodejs/node@eeeb40e · GitHub
Skip to content

Commit eeeb40e

Browse files
joyeecheungaduh95
authored andcommitted
crypto: add tls.setDefaultCACertificates()
This API allows dynamically configuring CA certificates that will be used by the Node.js TLS clients by default. Once called, the provided certificates will become the default CA certificate list returned by `tls.getCACertificates('default')` and used by TLS connections that don't specify their own CA certificates. This function only affects the current Node.js thread. PR-URL: #58822 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
1 parent b1a318d commit eeeb40e

21 files changed

Lines changed: 1128 additions & 14 deletions

doc/api/tls.md

Lines changed: 48 additions & 0 deletions

lib/tls.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
ERR_TLS_CERT_ALTNAME_INVALID,
3838
ERR_OUT_OF_RANGE,
3939
ERR_INVALID_ARG_VALUE,
40+
ERR_INVALID_ARG_TYPE,
4041
} = require('internal/errors').codes;
4142
const internalUtil = require('internal/util');
4243
internalUtil.assertCrypto();
@@ -51,6 +52,8 @@ const {
5152
getBundledRootCertificates,
5253
getExtraCACertificates,
5354
getSystemCACertificates,
55+
resetRootCertStore,
56+
getUserRootCertificates,
5457
getSSLCiphers,
5558
} = internalBinding('crypto');
5659
const { Buffer } = require('buffer');
@@ -122,8 +125,17 @@ function cacheSystemCACertificates() {
122125
}
123126

124127
let defaultCACertificates;
128+
let hasResetDefaultCACertificates = false;
129+
125130
function cacheDefaultCACertificates() {
126131
if (defaultCACertificates) { return defaultCACertificates; }
132+
133+
if (hasResetDefaultCACertificates) {
134+
defaultCACertificates = getUserRootCertificates();
135+
ObjectFreeze(defaultCACertificates);
136+
return defaultCACertificates;
137+
}
138+
127139
defaultCACertificates = [];
128140

129141
if (!getOptionValue('--use-openssl-ca')) {
@@ -171,6 +183,26 @@ function getCACertificates(type = 'default') {
171183
}
172184
exports.getCACertificates = getCACertificates;
173185

186+
function setDefaultCACertificates(certs) {
187+
if (!ArrayIsArray(certs)) {
188+
throw new ERR_INVALID_ARG_TYPE('certs', 'Array', certs);
189+
}
190+
191+
// Verify that all elements in the array are strings
192+
for (let i = 0; i < certs.length; i++) {
193+
if (typeof certs[i] !== 'string' && !isArrayBufferView(certs[i])) {
194+
throw new ERR_INVALID_ARG_TYPE(
195+
`certs[${i}]`, ['string', 'ArrayBufferView'], certs[i]);
196+
}
197+
}
198+
199+
resetRootCertStore(certs);
200+
defaultCACertificates = undefined; // Reset the cached default certificates
201+
hasResetDefaultCACertificates = true;
202+
}
203+
204+
exports.setDefaultCACertificates = setDefaultCACertificates;
205+
174206
// Convert protocols array into valid OpenSSL protocols list
175207
// ("\x06spdy/2\x08http/1.1\x08http/1.0")
176208
function convertProtocols(protocols) {

src/crypto/crypto_context.cc

Lines changed: 182 additions & 14 deletions

0 commit comments

Comments
 (0)