-
Notifications
You must be signed in to change notification settings - Fork 5.4k
tls: Implement session resumption for client connections #4790
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| #include "openssl/hmac.h" | ||
| #include "openssl/rand.h" | ||
| #include "openssl/x509v3.h" | ||
| #include <openssl/ssl.h> | ||
|
|
||
| namespace Envoy { | ||
| namespace Ssl { | ||
|
|
@@ -483,17 +484,41 @@ CertificateDetailsPtr ContextImpl::certificateDetails(X509* cert, const std::str | |
|
|
||
| ClientContextImpl::ClientContextImpl(Stats::Scope& scope, const ClientContextConfig& config) | ||
| : ContextImpl(scope, config), server_name_indication_(config.serverNameIndication()), | ||
| allow_renegotiation_(config.allowRenegotiation()) { | ||
| allow_renegotiation_(config.allowRenegotiation()), allow_session_resumption_(config.allowSessionResumption()) { | ||
| if (!parsed_alpn_protocols_.empty()) { | ||
| int rc = SSL_CTX_set_alpn_protos(ctx_.get(), &parsed_alpn_protocols_[0], | ||
| parsed_alpn_protocols_.size()); | ||
| RELEASE_ASSERT(rc == 0, ""); | ||
| } | ||
|
|
||
| if (allow_session_resumption_) { | ||
| SSL_CTX_set_session_cache_mode(ctx_.get(), SSL_SESS_CACHE_CLIENT|SSL_SESS_CACHE_NO_INTERNAL); | ||
| SSL_CTX_sess_set_new_cb( | ||
| ctx_.get(), | ||
| [](SSL * ssl, SSL_SESSION *session) { | ||
| ContextImpl* context_impl = static_cast<ContextImpl*>( | ||
| SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ssl), sslContextIndex())); | ||
| ClientContextImpl* client_context_impl = dynamic_cast<ClientContextImpl*>(context_impl); | ||
| int len = i2d_SSL_SESSION(session, NULL); | ||
| if (len <= ENVOY_MAX_SESSION_SIZE) { | ||
| client_context_impl->session_.resize(len); | ||
| unsigned char* temp = client_context_impl->session_.data(); | ||
| i2d_SSL_SESSION(session, &temp); | ||
| } | ||
| return 0; | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| bssl::UniquePtr<SSL> ClientContextImpl::newSsl() const { | ||
| bssl::UniquePtr<SSL> ssl_con(ContextImpl::newSsl()); | ||
|
|
||
| if (allow_session_resumption_ && !session_.empty()) { | ||
| const unsigned char* temp = session_.data(); | ||
| SSL_SESSION *session = d2i_SSL_SESSION(NULL, &temp, session_.size()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't |
||
| SSL_set_session(ssl_con.get(), session); | ||
| } | ||
|
|
||
| if (!server_name_indication_.empty()) { | ||
| int rc = SSL_set_tlsext_host_name(ssl_con.get(), server_name_indication_.c_str()); | ||
| RELEASE_ASSERT(rc, ""); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ namespace Envoy { | |
| #error Envoy requires BoringSSL | ||
| #endif | ||
|
|
||
| #define ENVOY_MAX_SESSION_SIZE 4096 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this a |
||
|
|
||
| namespace Ssl { | ||
|
|
||
| // clang-format off | ||
|
|
@@ -146,6 +148,8 @@ class ClientContextImpl : public ContextImpl, public ClientContext { | |
| private: | ||
| const std::string server_name_indication_; | ||
| const bool allow_renegotiation_; | ||
| const bool allow_session_resumption_; | ||
| std::vector<uint8_t> session_; | ||
| }; | ||
|
|
||
| class ServerContextImpl : public ContextImpl, public ServerContext { | ||
|
|
||
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.
Do we only have one resumption per client context? What if the same context is used to speak to multiple endpoints, each of which who negotiates their own resumption ticket?