| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2973,7 +2973,13 @@ bool SSLPointer::setSniContext(const SSLCtxPointer& ctx) const { | |||
| 2973 | 2973 | EVP_PKEY* pkey = SSL_CTX_get0_privatekey(ctx.get()); | |
| 2974 | 2974 | STACK_OF(X509) * chain; | |
| 2975 | 2975 | int err = SSL_CTX_get0_chain_certs(ctx.get(), &chain); | |
| 2976 | - if (err == 1) err = SSL_use_certificate(get(), x509); | ||
| 2976 | + if (err == 1) { | ||
| 2977 | + // SSL_use_certificate replaces only the certificate matching the key | ||
| 2978 | + // type. Clear all existing certificates so credentials from the default | ||
| 2979 | + // context cannot be selected for a different key type. | ||
| 2980 | + SSL_certs_clear(get()); | ||
| 2981 | + err = SSL_use_certificate(get(), x509); | ||
| 2982 | + } | ||
| 2977 | 2983 | if (err == 1) err = SSL_use_PrivateKey(get(), pkey); | |
| 2978 | 2984 | if (err == 1 && chain != nullptr) err = SSL_set1_chain(get(), chain); | |
| 2979 | 2985 | return err == 1; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,71 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common'); | ||
| 4 | + if (!common.hasCrypto) | ||
| 5 | + common.skip('missing crypto'); | ||
| 6 | + | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const { X509Certificate } = require('crypto'); | ||
| 9 | + const https = require('https'); | ||
| 10 | + const tls = require('tls'); | ||
| 11 | + const fixtures = require('../common/fixtures'); | ||
| 12 | + | ||
| 13 | + const defaultCredentials = { | ||
| 14 | + cert: fixtures.readKey('ca5-cert.pem'), | ||
| 15 | + key: fixtures.readKey('ca5-key.pem'), | ||
| 16 | + }; | ||
| 17 | + const sniCredentials = { | ||
| 18 | + cert: fixtures.readKey('agent1-cert.pem'), | ||
| 19 | + key: fixtures.readKey('agent1-key.pem'), | ||
| 20 | + }; | ||
| 21 | + | ||
| 22 | + const defaultCertificate = new X509Certificate(defaultCredentials.cert); | ||
| 23 | + const sniCertificate = new X509Certificate(sniCredentials.cert); | ||
| 24 | + const sniContext = tls.createSecureContext(sniCredentials); | ||
| 25 | + | ||
| 26 | + function request(port, servername, expectedCertificate) { | ||
| 27 | + return new Promise((resolve, reject) => { | ||
| 28 | + const req = https.get({ | ||
| 29 | + host: '127.0.0.1', | ||
| 30 | + port, | ||
| 31 | + servername, | ||
| 32 | + rejectUnauthorized: false, | ||
| 33 | + agent: false, | ||
| 34 | + }, common.mustCall((response) => { | ||
| 35 | + try { | ||
| 36 | + const certificate = response.socket.getPeerX509Certificate(); | ||
| 37 | + assert.strictEqual(certificate.fingerprint256, | ||
| 38 | + expectedCertificate.fingerprint256); | ||
| 39 | + } catch (err) { | ||
| 40 | + reject(err); | ||
| 41 | + return; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + response.resume(); | ||
| 45 | + response.once('end', resolve); | ||
| 46 | + response.once('error', reject); | ||
| 47 | + })); | ||
| 48 | + req.once('error', reject); | ||
| 49 | + }); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + const server = https.createServer({ | ||
| 53 | + cert: defaultCredentials.cert, | ||
| 54 | + key: defaultCredentials.key, | ||
| 55 | + SNICallback: common.mustCall((servername, callback) => { | ||
| 56 | + assert.strictEqual(servername, 'agent1.com'); | ||
| 57 | + callback(null, sniContext); | ||
| 58 | + }, 1), | ||
| 59 | + }, (_request, response) => { | ||
| 60 | + response.end('ok'); | ||
| 61 | + }); | ||
| 62 | + | ||
| 63 | + server.listen(0, common.mustCall(async () => { | ||
| 64 | + try { | ||
| 65 | + const { port } = server.address(); | ||
| 66 | + await request(port, undefined, defaultCertificate); | ||
| 67 | + await request(port, 'agent1.com', sniCertificate); | ||
| 68 | + } finally { | ||
| 69 | + server.close(common.mustCall()); | ||
| 70 | + } | ||
| 71 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments