Skip to content

Commit 80f7f65

Browse files
bnoordhuisrvagg
authored andcommitted
tls: support reading multiple cas from one input
Before this commit you had to pass multiple CA certificates as an array of strings. For convenience you can now pass them as a single string. Fixes: #4096 PR-URL: #4099 Reviewed-By: Fedor Indutny <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6d4a03d commit 80f7f65

File tree

2 files changed

+47
-29
lines changed

2 files changed

+47
-29
lines changed

src/node_crypto.cc

+12-29
Original file line numberDiff line numberDiff line change
@@ -453,26 +453,6 @@ static BIO* LoadBIO(Environment* env, Local<Value> v) {
453453
}
454454

455455

456-
// Takes a string or buffer and loads it into an X509
457-
// Caller responsible for X509_free-ing the returned object.
458-
static X509* LoadX509(Environment* env, Local<Value> v) {
459-
HandleScope scope(env->isolate());
460-
461-
BIO *bio = LoadBIO(env, v);
462-
if (!bio)
463-
return nullptr;
464-
465-
X509 * x509 = PEM_read_bio_X509(bio, nullptr, CryptoPemCallback, nullptr);
466-
if (!x509) {
467-
BIO_free_all(bio);
468-
return nullptr;
469-
}
470-
471-
BIO_free_all(bio);
472-
return x509;
473-
}
474-
475-
476456
void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
477457
Environment* env = Environment::GetCurrent(args);
478458

@@ -668,16 +648,19 @@ void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) {
668648
newCAStore = true;
669649
}
670650

671-
X509* x509 = LoadX509(env, args[0]);
672-
if (!x509)
673-
return;
674-
675-
X509_STORE_add_cert(sc->ca_store_, x509);
676-
SSL_CTX_add_client_CA(sc->ctx_, x509);
677-
678-
X509_free(x509);
651+
unsigned cert_count = 0;
652+
if (BIO* bio = LoadBIO(env, args[0])) {
653+
while (X509* x509 = // NOLINT(whitespace/if-one-line)
654+
PEM_read_bio_X509(bio, nullptr, CryptoPemCallback, nullptr)) {
655+
X509_STORE_add_cert(sc->ca_store_, x509);
656+
SSL_CTX_add_client_CA(sc->ctx_, x509);
657+
X509_free(x509);
658+
cert_count += 1;
659+
}
660+
BIO_free_all(bio);
661+
}
679662

680-
if (newCAStore) {
663+
if (cert_count > 0 && newCAStore) {
681664
SSL_CTX_set_cert_store(sc->ctx_, sc->ca_store_);
682665
}
683666
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const tls = require('tls');
5+
const fs = require('fs');
6+
7+
const ca1 =
8+
fs.readFileSync(`${common.fixturesDir}/keys/ca1-cert.pem`, `utf8`);
9+
const ca2 =
10+
fs.readFileSync(`${common.fixturesDir}/keys/ca2-cert.pem`, `utf8`);
11+
const cert =
12+
fs.readFileSync(`${common.fixturesDir}/keys/agent3-cert.pem`, `utf8`);
13+
const key =
14+
fs.readFileSync(`${common.fixturesDir}/keys/agent3-key.pem`, `utf8`);
15+
16+
function test(ca, next) {
17+
const server = tls.createServer({ ca, cert, key }, function(conn) {
18+
this.close();
19+
conn.end();
20+
});
21+
22+
server.addContext('agent3', { ca, cert, key });
23+
24+
const host = common.localhostIPv4;
25+
const port = common.PORT;
26+
server.listen(port, host, function() {
27+
tls.connect({ servername: 'agent3', host, port, ca });
28+
});
29+
30+
server.once('close', next);
31+
}
32+
33+
const array = [ca1, ca2];
34+
const string = ca1 + '\n' + ca2;
35+
test(array, () => test(string, () => {}));

0 commit comments

Comments
 (0)