-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
tls: fix SNICallback certificate selection #64700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mcollina
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
mcollina:fix/snicallback-certificate-selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
|
|
||
| const assert = require('assert'); | ||
| const { X509Certificate } = require('crypto'); | ||
| const https = require('https'); | ||
| const tls = require('tls'); | ||
| const fixtures = require('../common/fixtures'); | ||
|
|
||
| const defaultCredentials = { | ||
| cert: fixtures.readKey('ca5-cert.pem'), | ||
| key: fixtures.readKey('ca5-key.pem'), | ||
| }; | ||
| const sniCredentials = { | ||
| cert: fixtures.readKey('agent1-cert.pem'), | ||
| key: fixtures.readKey('agent1-key.pem'), | ||
| }; | ||
|
|
||
| const defaultCertificate = new X509Certificate(defaultCredentials.cert); | ||
| const sniCertificate = new X509Certificate(sniCredentials.cert); | ||
| const sniContext = tls.createSecureContext(sniCredentials); | ||
|
|
||
| function request(port, servername, expectedCertificate) { | ||
| return new Promise((resolve, reject) => { | ||
| const req = https.get({ | ||
| host: '127.0.0.1', | ||
| port, | ||
| servername, | ||
| rejectUnauthorized: false, | ||
| agent: false, | ||
| }, common.mustCall((response) => { | ||
| try { | ||
| const certificate = response.socket.getPeerX509Certificate(); | ||
| assert.strictEqual(certificate.fingerprint256, | ||
| expectedCertificate.fingerprint256); | ||
| } catch (err) { | ||
| reject(err); | ||
| return; | ||
| } | ||
|
|
||
| response.resume(); | ||
| response.once('end', resolve); | ||
| response.once('error', reject); | ||
| })); | ||
| req.once('error', reject); | ||
| }); | ||
| } | ||
|
|
||
| const server = https.createServer({ | ||
| cert: defaultCredentials.cert, | ||
| key: defaultCredentials.key, | ||
| SNICallback: common.mustCall((servername, callback) => { | ||
| assert.strictEqual(servername, 'agent1.com'); | ||
| callback(null, sniContext); | ||
| }, 1), | ||
| }, (_request, response) => { | ||
| response.end('ok'); | ||
| }); | ||
|
|
||
| server.listen(0, common.mustCall(async () => { | ||
| try { | ||
| const { port } = server.address(); | ||
| await request(port, undefined, defaultCertificate); | ||
| await request(port, 'agent1.com', sniCertificate); | ||
| } finally { | ||
| server.close(common.mustCall()); | ||
| } | ||
| })); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this is a bigger change than it needs to be to fix the bug, in a way that will break things. It doesn't just replace the cert configuration: it swaps out the entire context, which has all the other TLS configuration and runtime internals attached to it.
This means it pulls in other configuration from the context returned by the SNI callback provided: e.g. it replaces at least
ciphers,sigalgs,dhparamwith the SNI result value.Given the normal patterns for this today (nothing but certs configured on the SNI result value, because all other config does nothing) in practice that means for existing code this silently resets every TLS handshake to our default configuration, dropping most server/client custom TLS options you set.
It also drops things we internally store on the context - I set claude on it, it thinks this'd break at least keylog, OCSP stapling, ALPNProtocols/ALPNCallback (on TLS 1.2) and all session resumption.
Honestly if we were designing from scratch I think this might be the right shape - it gives you a hook to set per-SNI TLS context configuration which is neat - but it would be a very high-risk breaking change to existing code today. If we want that replace-context behaviour, I think we need to make a new option name for it to make it opt-in. Without that, replacing the context silently wipes custom TLS configurations (not to mention breaking the features above).
We don't need to do this to fix the bug though. I think we can just wipe the cert config of the existing context with
SSL_certs_clear, and then add the new certs as before with no risk of old certs applying. We don't need to replace the whole context.