Skip to content

ssl: reset chain in add_certificate - #1154

Merged
rhenium merged 1 commit into
ruby:masterfrom
wtn:cert
Nov 18, 2026
Merged

rhenium merged 1 commit into
ruby:masterfrom
wtn:cert

Conversation

@wtn

@wtn wtn commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to f5fbd44 (#1149).

SSLSocket#add_certificate(cert, key) keeps the extra_certs (intermediate CA certificates) that the socket copied from its SSLContext. If the new certificate has the same key type as the SSLContext certificate, the server sends the new certificate with the extra_certs of the SSLContext certificate. SSLContext#add_certificate(cert, key) also keeps the extra_certs from an earlier call for the same key type.

ctx = OpenSSL::SSL::SSLContext.new
ctx.add_certificate(default_cert, default_key, [ca1_cert]) # default_cert is issued by CA1
ctx.servername_cb = proc do |ssl, _name|
  ssl.add_certificate(sni_cert, sni_key) # sni_cert is issued by CA2, same key type as default_cert
  nil
end

# client connecting with SNI
ssl.peer_cert_chain.map { |c| c.subject.to_s }
#=> ["/CN=sni", "/CN=CA1"]

With the servername_cb example from #1150, the server sends the chain above when the SSLContext certificate has extra_certs. SSLSocket#add_certificate is not in a release yet.

Fix

SSLContext#add_certificate(cert, key) and SSLSocket#add_certificate(cert, key) now drop the extra_certs set earlier for the same key type, and the server in the example above sends only the SNI certificate.

When extra_certs is omitted, SSLContext#add_certificate and
SSLSocket#add_certificate keep the extra certificates set earlier for
the same key type. Pass a NULL chain to SSL_CTX_set0_chain() and
SSL_set0_chain() to remove the earlier extra certificates.

Co-authored-by: Claude Mythos 5.2 <noreply@anthropic.com>

@rhenium rhenium left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the catch!

The identical bug also exists in SSLContext#add_certificate. Could you fix it as well?

Comment thread ext/openssl/ossl_ssl.c Outdated
* SSLContext#add_certificate for details.
*
* The extra certificates for the key type of _certificate_ are replaced with
* _extra_certs_, or removed if _extra_certs_ is omitted.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be in the rdoc comments for SSLContext#add_certificate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the rdoc for SSLContext#add_certificate.

@wtn wtn changed the title ssl: reset chain in SSLSocket#add_certificate ssl: reset chain in add_certificate Nov 17, 2026
@wtn

wtn commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for review.

The identical bug also exists in SSLContext#add_certificate. Could you fix it as well?

I've fixed SSLContext#add_certificate and added a test.

@rhenium rhenium left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@rhenium
rhenium merged commit b577471 into ruby:master Nov 18, 2026
48 checks passed
@wtn
wtn deleted the cert branch November 18, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants