Skip to content

Commit 33012e9

Browse files
AdamMajersam-github
authored andcommitted
crypto: ability to select cert store at runtime
PR-URL: #8334 Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent 3aa17e4 commit 33012e9

File tree

5 files changed

+94
-8
lines changed

5 files changed

+94
-8
lines changed

doc/api/cli.md

+36
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,24 @@ Load an OpenSSL configuration file on startup. Among other uses, this can be
257257
used to enable FIPS-compliant crypto if Node.js is built with
258258
`./configure --openssl-fips`.
259259

260+
### `--use-openssl-ca`, `--use-bundled-ca`
261+
<!-- YAML
262+
added: REPLACEME
263+
-->
264+
265+
Use OpenSSL's default CA store or use bundled Mozilla CA store as supplied by
266+
current NodeJS version. The default store is selectable at build-time.
267+
268+
Using OpenSSL store allows for external modifications of the store. For most
269+
Linux and BSD distributions, this store is maintained by the distribution
270+
maintainers and system administrators. OpenSSL CA store location is dependent on
271+
configuration of the OpenSSL library but this can be altered at runtime using
272+
environmental variables.
273+
274+
The bundled CA store, as supplied by NodeJS, is a snapshot of Mozilla CA store
275+
that is fixed at release time. It is identical on all supported platforms.
276+
277+
See `SSL_CERT_DIR` and `SSL_CERT_FILE`.
260278

261279
### `--icu-data-dir=file`
262280
<!-- YAML
@@ -350,6 +368,24 @@ misformatted, but any errors are otherwise ignored.
350368
Note that neither the well known nor extra certificates are used when the `ca`
351369
options property is explicitly specified for a TLS or HTTPS client or server.
352370

371+
### `SSL_CERT_DIR=dir`
372+
373+
If `--use-openssl-ca` is enabled, this overrides and sets OpenSSL's directory
374+
containing trusted certificates.
375+
376+
Note: Be aware that unless the child environment is explicitly set, this
377+
evironment variable will be inherited by any child processes, and if they use
378+
OpenSSL, it may cause them to trust the same CAs as node.
379+
380+
### `SSL_CERT_FILE=file`
381+
382+
If `--use-openssl-ca` is enabled, this overrides and sets OpenSSL's file
383+
containing trusted certificates.
384+
385+
Note: Be aware that unless the child environment is explicitly set, this
386+
evironment variable will be inherited by any child processes, and if they use
387+
OpenSSL, it may cause them to trust the same CAs as node.
388+
353389
[emit_warning]: process.html#process_process_emitwarning_warning_name_ctor
354390
[Buffer]: buffer.html#buffer_buffer
355391
[debugger]: debugger.html

doc/node.1

+25
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,22 @@ Load an OpenSSL configuration file on startup. Among other uses, this can be
180180
used to enable FIPS-compliant crypto if Node.js is built with
181181
\fB./configure \-\-openssl\-fips\fR.
182182

183+
.TP
184+
.BR \-\-use\-openssl\-ca,\-\-use\-bundled\-ca
185+
Use OpenSSL's default CA store or use bundled Mozilla CA store as supplied by
186+
current NodeJS version. The default store is selectable at build-time.
187+
188+
Using OpenSSL store allows for external modifications of the store. For most
189+
Linux and BSD distributions, this store is maintained by the distribution
190+
maintainers and system administrators. OpenSSL CA store location is dependent on
191+
configuration of the OpenSSL library but this can be altered at runtime using
192+
environmental variables.
193+
194+
The bundled CA store, as supplied by NodeJS, is a snapshot of Mozilla CA store
195+
that is fixed at release time. It is identical on all supported platforms.
196+
197+
See \fBSSL_CERT_DIR\fR and \fBSSL_CERT_FILE\fR.
198+
183199
.TP
184200
.BR \-\-icu\-data\-dir =\fIfile\fR
185201
Specify ICU data load path. (overrides \fBNODE_ICU_DATA\fR)
@@ -228,6 +244,15 @@ asynchronous when outputting to a TTY on platforms which support async stdio.
228244
Setting this will void any guarantee that stdio will not be interleaved or
229245
dropped at program exit. \fBAvoid use.\fR
230246

247+
.TP
248+
.BR SSL_CERT_DIR = \fIdir\fR
249+
If \fB\-\-use\-openssl\-ca\fR is enabled, this overrides and sets OpenSSL's directory
250+
containing trusted certificates.
251+
252+
.TP
253+
.BR SSL_CERT_FILE = \fIfile\fR
254+
If \fB\-\-use\-openssl\-ca\fR is enabled, this overrides and sets OpenSSL's
255+
file containing trusted certificates.
231256

232257
.SH BUGS
233258
Bugs are tracked in GitHub Issues:

src/node.cc

+22
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ static const char* icu_data_dir = nullptr;
163163
bool no_deprecation = false;
164164

165165
#if HAVE_OPENSSL
166+
// use OpenSSL's cert store instead of bundled certs
167+
bool ssl_openssl_cert_store =
168+
#if defined(NODE_OPENSSL_CERT_STORE)
169+
true;
170+
#else
171+
false;
172+
#endif
173+
166174
# if NODE_FIPS_MODE
167175
// used by crypto module
168176
bool enable_fips_crypto = false;
@@ -3508,6 +3516,16 @@ static void PrintHelp() {
35083516
#if HAVE_OPENSSL
35093517
" --tls-cipher-list=val use an alternative default TLS cipher "
35103518
"list\n"
3519+
" --use-bundled-ca use bundled CA store"
3520+
#if !defined(NODE_OPENSSL_CERT_STORE)
3521+
" (default)"
3522+
#endif
3523+
"\n"
3524+
" --use-openssl-ca use OpenSSL's default CA store"
3525+
#if defined(NODE_OPENSSL_CERT_STORE)
3526+
" (default)"
3527+
#endif
3528+
"\n"
35113529
#if NODE_FIPS_MODE
35123530
" --enable-fips enable FIPS crypto at startup\n"
35133531
" --force-fips force FIPS crypto (cannot be disabled)\n"
@@ -3681,6 +3699,10 @@ static void ParseArgs(int* argc,
36813699
#if HAVE_OPENSSL
36823700
} else if (strncmp(arg, "--tls-cipher-list=", 18) == 0) {
36833701
default_cipher_list = arg + 18;
3702+
} else if (strncmp(arg, "--use-openssl-ca", 16) == 0) {
3703+
ssl_openssl_cert_store = true;
3704+
} else if (strncmp(arg, "--use-bundled-ca", 16) == 0) {
3705+
ssl_openssl_cert_store = false;
36843706
#if NODE_FIPS_MODE
36853707
} else if (strcmp(arg, "--enable-fips") == 0) {
36863708
enable_fips_crypto = true;

src/node.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,12 @@ typedef intptr_t ssize_t;
180180
namespace node {
181181

182182
NODE_EXTERN extern bool no_deprecation;
183-
#if HAVE_OPENSSL && NODE_FIPS_MODE
183+
#if HAVE_OPENSSL
184+
NODE_EXTERN extern bool ssl_openssl_cert_store;
185+
# if NODE_FIPS_MODE
184186
NODE_EXTERN extern bool enable_fips_crypto;
185187
NODE_EXTERN extern bool force_fips_crypto;
188+
# endif
186189
#endif
187190

188191
NODE_EXTERN int Start(int argc, char *argv[]);

src/node_crypto.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,14 @@ static X509_STORE* NewRootCertStore() {
707707
}
708708

709709
X509_STORE* store = X509_STORE_new();
710-
#if defined(NODE_OPENSSL_CERT_STORE)
711-
X509_STORE_set_default_paths(store);
712-
#else
713-
for (X509 *cert : root_certs_vector) {
714-
X509_up_ref(cert);
715-
X509_STORE_add_cert(store, cert);
710+
if (ssl_openssl_cert_store) {
711+
X509_STORE_set_default_paths(store);
712+
} else {
713+
for (X509 *cert : root_certs_vector) {
714+
X509_up_ref(cert);
715+
X509_STORE_add_cert(store, cert);
716+
}
716717
}
717-
#endif
718718

719719
return store;
720720
}

0 commit comments

Comments
 (0)