GeezerVison is showing github.com — tls: apply the CA of SNI contexts without a certificate · nodejs/node@67da864
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_context.cc +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1612,6 +1612,7 @@ inline void SecureContext::Reset() { | 1612 | 1612 | ctx_.reset(); | 1613 | 1613 | cert_.reset(); | 1614 | 1614 | issuer_.reset(); | | 1615 | + has_own_ca_ = false; | 1615 | 1616 | } | 1616 | 1617 |
| 1617 | 1618 | SecureContext::~SecureContext() { | @@ -1949,6 +1950,7 @@ void SecureContext::SetAllowPartialTrustChain( | 1949 | 1950 | void SecureContext::SetCACert(const BIOPointer& bio) { | 1950 | 1951 | ClearErrorOnReturn clear_error_on_return; | 1951 | 1952 | if (!bio) return; | | 1953 | + has_own_ca_ = true; | 1952 | 1954 | while (X509Pointer x509 = X509Pointer(PEM_read_bio_X509_AUX( | 1953 | 1955 | bio.get(), nullptr, NoPasswordCallback, nullptr))) { | 1954 | 1956 | CHECK_EQ(1, | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_context.h +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -48,6 +48,9 @@ class SecureContext final : public BaseObject { | 48 | 48 | static SecureContext* Create(Environment* env); | 49 | 49 |
| 50 | 50 | const ncrypto::SSLCtxPointer& ctx() const { return ctx_; } | | 51 | + // True if CA certificates were added with addCACert(), as opposed to the | | 52 | + // context relying on the default root store. | | 53 | + bool has_own_ca() const { return has_own_ca_; } | 51 | 54 |
| 52 | 55 | // Non-const ctx() that allows for non-default initialization of | 53 | 56 | // the SecureContext. | @@ -164,6 +167,7 @@ class SecureContext final : public BaseObject { | 164 | 167 | ncrypto::X509Pointer issuer_; | 165 | 168 | // Non-owning cache for SSL_CTX_get_cert_store(ctx_.get()) | 166 | 169 | X509_STORE* own_cert_store_cache_ = nullptr; | | 170 | + bool has_own_ca_ = false; | 167 | 171 | #ifndef OPENSSL_NO_ENGINE | 168 | 172 | bool client_cert_engine_provided_ = false; | 169 | 173 | ncrypto::EnginePointer private_key_engine_; | |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_tls.cc +13-5Lines changed: 13 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1615,11 +1615,19 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) { | 1615 | 1615 | // Store the SNI context for later use. | 1616 | 1616 | w->sni_context_ = BaseObjectPtr<SecureContext>(sc); | 1617 | 1617 |
| 1618 | | - if (UseSNIContext(w->ssl_, w->sni_context_) && !w->SetCACerts(sc)) { | 1619 | | - // Not clear why sometimes we throw error, and sometimes we call | 1620 | | - // onerror(). Both cause .destroy(), but onerror does a bit more. | 1621 | | - unsigned long err = ERR_get_error(); // NOLINT(runtime/int) | 1622 | | - return ThrowCryptoError(env, err, "CertCbDone"); | | 1618 | + // An SNI context without a certificate keeps the default context's | | 1619 | + // certificate, but a `ca` it configured must still replace the default | | 1620 | + // trust store for client certificates. A context with neither keeps the | | 1621 | + // default context's trust store. | | 1622 | + const bool has_cert = SSL_CTX_get0_certificate(sc->ctx().get()) != nullptr; | | 1623 | + if (has_cert || sc->has_own_ca()) { | | 1624 | + if ((has_cert && !UseSNIContext(w->ssl_, w->sni_context_)) || | | 1625 | + !w->SetCACerts(sc)) { | | 1626 | + // Not clear why sometimes we throw error, and sometimes we call | | 1627 | + // onerror(). Both cause .destroy(), but onerror does a bit more. | | 1628 | + unsigned long err = ERR_get_error(); // NOLINT(runtime/int) | | 1629 | + return ThrowCryptoError(env, err, "CertCbDone"); | | 1630 | + } | 1623 | 1631 | } | 1624 | 1632 | } else if (ctx->IsObject()) { | 1625 | 1633 | // Failure: incorrect SNI context object | |
Collapse file Copy file name to clipboard +88Lines changed: 88 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +// An SNI context that sets `ca` but no certificate must still replace the | | 8 | +// default trust store for client certificates on that host. A context that | | 9 | +// sets neither keeps the default trust store. | | 10 | + | | 11 | +const assert = require('assert'); | | 12 | +const tls = require('tls'); | | 13 | +const fixtures = require('../common/fixtures'); | | 14 | + | | 15 | +const serverCreds = { | | 16 | + key: fixtures.readKey('agent2-key.pem'), | | 17 | + cert: fixtures.readKey('agent2-cert.pem'), | | 18 | +}; | | 19 | +const ca1 = fixtures.readKey('ca1-cert.pem'); | | 20 | +const ca2 = fixtures.readKey('ca2-cert.pem'); | | 21 | +const clients = { | | 22 | + agent1: { // Issued by CA1. | | 23 | + key: fixtures.readKey('agent1-key.pem'), | | 24 | + cert: fixtures.readKey('agent1-cert.pem'), | | 25 | + }, | | 26 | + agent3: { // Issued by CA2. | | 27 | + key: fixtures.readKey('agent3-key.pem'), | | 28 | + cert: fixtures.readKey('agent3-cert.pem'), | | 29 | + }, | | 30 | +}; | | 31 | + | | 32 | +const cases = [ | | 33 | + { name: 'ca only', context: { ca: ca2 }, accepted: 'agent3' }, | | 34 | + { name: 'cert, key and ca', context: { ...serverCreds, ca: ca2 }, | | 35 | + accepted: 'agent3' }, | | 36 | + { name: 'no ca or cert', context: { ciphers: 'DEFAULT' }, | | 37 | + accepted: 'agent1' }, | | 38 | +]; | | 39 | + | | 40 | +function connect(port, client, version) { | | 41 | + return new Promise((resolve) => { | | 42 | + const socket = tls.connect({ | | 43 | + port, | | 44 | + servername: 'protected.example', | | 45 | + ...clients[client], | | 46 | + rejectUnauthorized: false, | | 47 | + minVersion: version, | | 48 | + maxVersion: version, | | 49 | + }); | | 50 | + let data = ''; | | 51 | + socket.setEncoding('utf8'); | | 52 | + socket.on('data', (chunk) => data += chunk); | | 53 | + socket.on('error', () => {}); | | 54 | + socket.on('close', () => resolve(data)); | | 55 | + }); | | 56 | +} | | 57 | + | | 58 | +async function test(version, { name, context, accepted }) { | | 59 | + const server = tls.createServer({ | | 60 | + ...serverCreds, | | 61 | + ca: ca1, | | 62 | + requestCert: true, | | 63 | + rejectUnauthorized: true, | | 64 | + minVersion: version, | | 65 | + maxVersion: version, | | 66 | + }, common.mustCall((socket) => { | | 67 | + assert.strictEqual(socket.servername, 'protected.example'); | | 68 | + socket.end(socket.getPeerCertificate().subject.CN); | | 69 | + })); | | 70 | + server.on('tlsClientError', () => {}); | | 71 | + server.addContext('protected.example', context); | | 72 | + await new Promise((resolve) => server.listen(0, resolve)); | | 73 | + | | 74 | + const { port } = server.address(); | | 75 | + for (const client of Object.keys(clients)) { | | 76 | + const expected = client === accepted ? client : ''; | | 77 | + assert.strictEqual(await connect(port, client, version), expected, | | 78 | + `${version}, ${name}: ${client}`); | | 79 | + } | | 80 | + server.close(); | | 81 | +} | | 82 | + | | 83 | +(async () => { | | 84 | + for (const version of ['TLSv1.2', 'TLSv1.3']) { | | 85 | + for (const c of cases) | | 86 | + await test(version, c); | | 87 | + } | | 88 | +})().then(common.mustCall()); |
|