Repository navigation
Root certificate is prioritized over SNICallback context in HTTPS Server #54235
Description
Activity
- addedtlsIssues and PRs related to the tls subsystem.Issues and PRs related to the tls subsystem.httpsIssues and PRs related to the https subsystem.Issues and PRs related to the https subsystem.opensslIssues and PRs related to the OpenSSL dependency.Issues and PRs related to the OpenSSL dependency.
on Aug 6, 2024 Edit: this issue doesn't seem strictly related to Let's Encrypt. I was able to create a locally generated cert that is prioritized. I think this highlights that this is maybe not a bug but a poorly documented feature of OpenSSL? If anyone knows anything about certificate prioritization, more information would be greatly appreciated. I'm working on a Node.js regression test too that hopefully demonstrates it.
- changed the title
[-]Let's Encrypt based root certificate is prioritized over SNICallback context in HTTPS Server[/-][+]Root certificate is prioritized over SNICallback context in HTTPS Server[/+]on Aug 7, 2024 @nodejs/http @nodejs/net any ideas?
I did a test using
uWebSocketsand this doesn't seem reproducible there:const app = uWS.SSLApp({ key_file_name: "example.com-key.pem", cert_file_name: 'example.com.pem', }).addServerName('test.cloud.com', { key_file_name: 'test.cloud.com.key', cert_file_name: 'test.cloud.com.crt' }).get('/*', (res, req) => { res.end('Hello World!'); }).listen(port, (token) => { if (token) { console.log('Listening to port ' + port); } else { console.log('Failed to listen to port ' + port); } });
Requests to this app will get the correct certificates back regardless of the certificate content.
I think this helps point the issue back towards Node.js and its usage of OpenSSL.
I did a debug step-through using the test I wrote in #54251 here is what I found:
Starting off with the SNICallback flow:
- This is where the SNICallback is actually executed, on line 227, the context returned by the callback is assigned to the
Lines 214 to 230 in e4f61de
owner._SNICallback(servername, (err, context) => { if (once) return owner.destroy(new ERR_MULTIPLE_CALLBACK()); once = true; if (err) return owner.destroy(err); if (owner._handle === null) return owner.destroy(new ERR_SOCKET_CLOSED()); // TODO(indutny): eventually disallow raw `SecureContext` if (context) owner._handle.sni_context = context.context || context; requestOCSP(owner, info); }); sni_contextof the socket - Following that, the
requestOCSP()method is called with the updated socket + info as{servername: '[agent1.com](http://agent1.com/)', OCSPRequest: false} - Stepping into that method we hit the very first conditional () which enters into
Line 274 in e4f61de
if (!info.OCSPRequest || !socket.server) requestOCSPDone(socket) - This small function () calls into a native method
Line 325 in e4f61de
socket._handle.certCbDone(); certCbDone()defined here: https://github.com/nodejs/node/blob/main/src/crypto/crypto_tls.cc#L1553
Stepping through
TLSWrap::CertCBDone:- There are two properties on
TLSWrap::SecureContext,sni_context_andsc_and these are two separate certificates. sc_is the default certificate for that server, meanwhilesni_context_is in fact the correct certificate for the given servername (see screenshot for proof).
- The next important call is to [UseSNIContext](https://github.com/nodejs/node/blob/e4f61de14f8cfb83f1ce0ad1597b86278cd5f5f1/src/crypto/crypto_common.cc#L118-L131) called from here: https://github.com/nodejs/node/blob/e4f61de14f8cfb83f1ce0ad1597b86278cd5f5f1/src/crypto/crypto_tls.cc#L1573
- That function is what is doing the actual certificate resolution for the request. It makes calls to SSL and as far as I can tell it is up to SSL's resolution strategy for which certificate in a chain gets used. This is the unexpected behavior I'm struggling to understand as either "intended behavior" or a "bug" of `SNICallback`/`addContext` I am considering a fix to
UseSNIContext. I'm digging into the SSL calls now to understand if maybe theres a better way to call them for this.@indutny do you happen to know whats going on here? I'm still trying to determine if there is a better way to implement
UseSNIContextor not. This is my first time working with OpenSSL directly.I guess a code change may not be necessary here. As far as I can tell this is about the OpenSSL Verification configuration (https://docs.openssl.org/3.0/man1/openssl-verification-options/).
I think I could try using these options to get the certs to play nice.
I'd still appreciate someone with more OpenSSL experience to weigh in here whether this is a bug or not.
Thank you!
I know this has been stagnant for awhile, but why was
repro-existsremoved? I have a regression test case here proving my issue: #54251@Ethan-Arrowood I think the best solution would be for you to send a PR.
Yes of course, given the test case in #54251, at the time I tried some solutions - particularly trying to configure some of these options: https://docs.openssl.org/3.0/man1/openssl-verification-options/#description, but I couldn't get anything to satisfy the test case. Hence my last request for someone with more OpenSSL experience to take a look
- addedconfirmed-bugIssues and PRs for confirmed bugs.Issues and PRs for confirmed bugs.
on Jul 16, 2025 - added a commit that references this issue
on Aug 14, 2025 Hi @Ethan-Arrowood , I think I have encountered this same bug.
In my experiments, the bug seems to occur when the certificate/key type in the SNICallback context differs from the certificate/key type in the default context. (i.e. an SNICallback returning a context with EC keys is not compatible with a default context that uses RSA keys (and vice versa)). The context returned in the SNICallback will only take effect if its key type is the same as the key type used in the default context, otherwise the default is used instead. (I think this is what you mean by "prioritized".)
I have tested this with various pairings of EC and RSA certs, and the bug only (and always) occurs for me when the SNICallback returns a context using a key type different from the default.
In your test case, I can see that the certificate for ca5 uses an EC key, and agent1 uses an RSA key - so this is consistent with the behaviour exhibited in your test case too.
In the case of dual-certificate contexts (i.e. where the context is created using both EC and RSA keys/certs (for compatibility with clients that only support one or the other)) - the default context works fine, and selects the appropriate keypair according to the client's supported ciphers and signature algorithms, but the context returned from SNICallback doesn't do this correctly, exhibiting the following quirk:
- If the SNICallback context is constructed with the EC key/cert added first and the RSA key/cert added second, then:
- If the client only supports RSA ciphers, then the server will use the SNI context's RSA key/cert correctly.
- If the client only supports EC ciphers, then the server will default to using the EC key/cert of the default context.
- Likewise, if the SNICallback context is constructed in the opposite order (RSA first, EC second), then:
- If the client only supports RSA ciphers, then the server will default to using the RSA key/cert of the default context.
- If the client only supports EC ciphers, then the server will use the SNI context's EC key/cert correctly.
- If the SNICallback context is constructed with the EC key/cert added first and the RSA key/cert added second, then:
I have gone back to see when this bug was first introduced (or to see if it has always been this way).
I can confirm that this bug first appeared in nodejs v12.0.0.
It does not occur in v11.15.0.This major version also coincides with the upgrade to OpenSSL 1.1.1b, which may be relevant.
Making a bit further progress...
I have discovered that this bug is related to the version of TLS being used.
If the https server is created with the option
maxVersion: 'TLSv1.2', then the bug does not occur. (You can retry your test case with this option and you will see that it now passes).The bug only occurs when the client uses TLSv1.3.
This explains why I didn't see the bug in nodejs v11.15.0, as
tls.DEFAULT_MAX_VERSIONwas TLSv1.2 until nodejs v12.0.0 when TLSv1.3 support was added and it became the new default.
Version
22.5.1
Platform
Subsystem
tls, https
What steps will reproduce the bug?
Edit: I've added a test that reproduces the issue here: #54251
Old Example Repro
Output:
How often does it reproduce? Is there a required condition?
This bug only seems to happen when the root cert/key pair is from Let's Encrypt. We ran into this issue with a Digicert first, but were able to reproduce using a locally generated certificate too.I've further narrowed down the conditions. It seems that certain certificates (those with better CA or maybe created using an ext) will be prioritized over others without those things. It is not directly related to a certain provider. The reproduction demonstrates that the
test/fixtures/keys/ca5-cert.pemis prioritized overtest/fixtures/keys/agent1-cert.pem.What is the expected behavior? Why is that the expected behavior?
We expect the correct certificate to be used every time.
Additional information
Other issues/prs I reviewed:
I also asked Claude AI about this to see if it could help me along and it replied with some interesting points:
Maybe that'll help folks who understand SNI better 🤷♂️