GeezerVison is showing github.com — tls: apply the CA of SNI contexts without a certificate · nodejs/node@45e3b16
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 |
|---|
@@ -1778,6 +1778,7 @@ inline void SecureContext::Reset() { | 1778 | 1778 | ctx_.reset(); | 1779 | 1779 | cert_.reset(); | 1780 | 1780 | issuer_.reset(); | | 1781 | + has_own_ca_ = false; | 1781 | 1782 | } | 1782 | 1783 |
| 1783 | 1784 | SecureContext::~SecureContext() { | @@ -2144,6 +2145,7 @@ void SecureContext::SetCACert(const BIOPointer& bio) { | 2144 | 2145 | X509_STORE_add_cert(GetCertStoreOwnedByThisSecureContext(), x509)); | 2145 | 2146 | CHECK_EQ(1, SSL_CTX_add_client_CA(ctx_.get(), x509)); | 2146 | 2147 | } | | 2148 | + has_own_ca_ = true; | 2147 | 2149 | } | 2148 | 2150 |
| 2149 | 2151 | void SecureContext::AddCACert(const FunctionCallbackInfo<Value>& args) { | |
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 |
|---|
@@ -57,6 +57,9 @@ class SecureContext final : public BaseObject { | 57 | 57 | static SecureContext* Create(Environment* env); | 58 | 58 |
| 59 | 59 | const ncrypto::SSLCtxPointer& ctx() const { return ctx_; } | | 60 | + // True if CA certificates were added with addCACert(), as opposed to the | | 61 | + // context relying on the default root store. | | 62 | + bool has_own_ca() const { return has_own_ca_; } | 60 | 63 |
| 61 | 64 | // Non-const ctx() that allows for non-default initialization of | 62 | 65 | // the SecureContext. | @@ -210,6 +213,7 @@ class SecureContext final : public BaseObject { | 210 | 213 | bool client_cert_engine_provided_ = false; | 211 | 214 | ncrypto::EnginePointer private_key_engine_; | 212 | 215 | #endif // !OPENSSL_NO_ENGINE | | 216 | + bool has_own_ca_ = false; | 213 | 217 |
| 214 | 218 | unsigned char ticket_key_name_[16]; | 215 | 219 | unsigned char ticket_key_aes_[16]; | |
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 |
|---|
@@ -1742,11 +1742,19 @@ void TLSWrap::CertCbDone(const FunctionCallbackInfo<Value>& args) { | 1742 | 1742 | // Store the SNI context for later use. | 1743 | 1743 | w->sni_context_ = BaseObjectPtr<SecureContext>(sc); | 1744 | 1744 |
| 1745 | | - if (w->ssl_.setSniContext(w->sni_context_->ctx()) && !w->SetCACerts(sc)) { | 1746 | | - // Not clear why sometimes we throw error, and sometimes we call | 1747 | | - // onerror(). Both cause .destroy(), but onerror does a bit more. | 1748 | | - unsigned long err = ERR_get_error(); // NOLINT(runtime/int) | 1749 | | - return ThrowCryptoError(env, err, "CertCbDone"); | | 1745 | + // An SNI context without a certificate keeps the default context's | | 1746 | + // certificate, but a `ca` it configured must still replace the default | | 1747 | + // trust store for client certificates. A context with neither keeps the | | 1748 | + // default context's trust store. | | 1749 | + const bool has_cert = SSL_CTX_get0_certificate(sc->ctx().get()) != nullptr; | | 1750 | + if (has_cert || sc->has_own_ca()) { | | 1751 | + if ((has_cert && !w->ssl_.setSniContext(w->sni_context_->ctx())) || | | 1752 | + !w->SetCACerts(sc)) { | | 1753 | + // Not clear why sometimes we throw error, and sometimes we call | | 1754 | + // onerror(). Both cause .destroy(), but onerror does a bit more. | | 1755 | + unsigned long err = ERR_get_error(); // NOLINT(runtime/int) | | 1756 | + return ThrowCryptoError(env, err, "CertCbDone"); | | 1757 | + } | 1750 | 1758 | } | 1751 | 1759 | // setSniContext copies the cert via SSL_use_certificate which does not | 1752 | 1760 | // carry over pre-compressed certificate data (comp_cert). If the new | |
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()); |
|