You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ma...@apache.org on 2022/01/14 06:51:20 UTC

[trafficserver] branch master updated: Add TLSEarlyDataSupport (#8582)

This is an automated email from the ASF dual-hosted git repository.

maskit pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 1141121  Add TLSEarlyDataSupport (#8582)
1141121 is described below

commit 11411212c5d98f91495ae13a0ae63ff3db18457b
Author: Masakazu Kitajo <ma...@apache.org>
AuthorDate: Fri Jan 14 15:51:08 2022 +0900

    Add TLSEarlyDataSupport (#8582)
---
 iocore/net/Makefile.am            |  1 +
 iocore/net/P_SSLNetVConnection.h  | 15 ++++----
 iocore/net/SSLNetVConnection.cc   | 42 ++++++++++++----------
 iocore/net/SSLUtils.cc            |  1 +
 iocore/net/TLSEarlyDataSupport.cc | 74 +++++++++++++++++++++++++++++++++++++++
 iocore/net/TLSEarlyDataSupport.h  | 50 ++++++++++++++++++++++++++
 proxy/http/Http1ClientSession.cc  | 11 +++---
 proxy/http2/Http2ClientSession.cc |  8 ++---
 8 files changed, 168 insertions(+), 34 deletions(-)

diff --git a/iocore/net/Makefile.am b/iocore/net/Makefile.am
index dfeda52..88981e3 100644
--- a/iocore/net/Makefile.am
+++ b/iocore/net/Makefile.am
@@ -191,6 +191,7 @@ libinknet_a_SOURCES = \
 	SSLUtils.cc \
 	OCSPStapling.cc \
 	TLSBasicSupport.cc \
+	TLSEarlyDataSupport.cc \
 	TLSSessionResumptionSupport.cc \
 	TLSSNISupport.cc \
 	UDPIOEvent.cc \
diff --git a/iocore/net/P_SSLNetVConnection.h b/iocore/net/P_SSLNetVConnection.h
index 8e109a9..3bb46a3 100644
--- a/iocore/net/P_SSLNetVConnection.h
+++ b/iocore/net/P_SSLNetVConnection.h
@@ -49,6 +49,7 @@
 #include "P_ALPNSupport.h"
 #include "TLSSessionResumptionSupport.h"
 #include "TLSSNISupport.h"
+#include "TLSEarlyDataSupport.h"
 #include "TLSBasicSupport.h"
 #include "P_SSLUtils.h"
 #include "P_SSLConfig.h"
@@ -99,6 +100,7 @@ class SSLNetVConnection : public UnixNetVConnection,
                           public ALPNSupport,
                           public TLSSessionResumptionSupport,
                           public TLSSNISupport,
+                          public TLSEarlyDataSupport,
                           public TLSBasicSupport
 {
   typedef UnixNetVConnection super; ///< Parent type.
@@ -359,12 +361,6 @@ public:
   bool protocol_mask_set = false;
   unsigned long protocol_mask;
 
-  // early data related stuff
-  bool early_data_finish            = false;
-  MIOBuffer *early_data_buf         = nullptr;
-  IOBufferReader *early_data_reader = nullptr;
-  int64_t read_from_early_data      = 0;
-
   // Only applies during the VERIFY certificate hooks (client and server side)
   // Means to give the plugin access to the data structure passed in during the underlying
   // openssl callback so the plugin can make more detailed decisions about the
@@ -503,6 +499,13 @@ private:
 
   EventIO async_ep{};
 
+  // early data related stuff
+#if TS_HAS_TLS_EARLY_DATA
+  bool _early_data_finish            = false;
+  MIOBuffer *_early_data_buf         = nullptr;
+  IOBufferReader *_early_data_reader = nullptr;
+#endif
+
 private:
   void _make_ssl_connection(SSL_CTX *ctx);
   void _bindSSLObject();
diff --git a/iocore/net/SSLNetVConnection.cc b/iocore/net/SSLNetVConnection.cc
index 282bbe5..edd3580 100644
--- a/iocore/net/SSLNetVConnection.cc
+++ b/iocore/net/SSLNetVConnection.cc
@@ -218,6 +218,7 @@ SSLNetVConnection::_bindSSLObject()
   ALPNSupport::bind(this->ssl, this);
   TLSSessionResumptionSupport::bind(this->ssl, this);
   TLSSNISupport::bind(this->ssl, this);
+  TLSEarlyDataSupport::bind(this->ssl, this);
 }
 
 void
@@ -228,6 +229,7 @@ SSLNetVConnection::_unbindSSLObject()
   ALPNSupport::unbind(this->ssl);
   TLSSessionResumptionSupport::unbind(this->ssl);
   TLSSNISupport::unbind(this->ssl);
+  TLSEarlyDataSupport::unbind(this->ssl);
 }
 
 static void
@@ -991,16 +993,18 @@ SSLNetVConnection::free(EThread *t)
 
   ats_free(tunnel_host);
 
-  if (early_data_reader != nullptr) {
-    early_data_reader->dealloc();
+#if TS_HAS_TLS_EARLY_DATA
+  if (_early_data_reader != nullptr) {
+    _early_data_reader->dealloc();
   }
 
-  if (early_data_buf != nullptr) {
-    free_MIOBuffer(early_data_buf);
+  if (_early_data_buf != nullptr) {
+    free_MIOBuffer(_early_data_buf);
   }
 
-  early_data_reader = nullptr;
-  early_data_buf    = nullptr;
+  _early_data_reader = nullptr;
+  _early_data_buf    = nullptr;
+#endif
 
   clear();
   SET_CONTINUATION_HANDLER(this, (SSLNetVConnHandler)&SSLNetVConnection::startEvent);
@@ -2009,7 +2013,7 @@ SSLNetVConnection::_ssl_accept()
   int ssl_error = SSL_ERROR_NONE;
 
 #if TS_HAS_TLS_EARLY_DATA
-  if (SSLConfigParams::server_max_early_data > 0 && !this->early_data_finish) {
+  if (SSLConfigParams::server_max_early_data > 0 && !this->_early_data_finish) {
     size_t nread;
 
     while (true) {
@@ -2023,12 +2027,12 @@ SSLNetVConnection::_ssl_accept()
         break;
       } else {
         if (nread > 0) {
-          if (this->early_data_buf == nullptr) {
-            this->early_data_buf    = new_MIOBuffer(BUFFER_SIZE_INDEX_16K);
-            this->early_data_reader = this->early_data_buf->alloc_reader();
+          if (this->_early_data_buf == nullptr) {
+            this->_early_data_buf    = new_MIOBuffer(BUFFER_SIZE_INDEX_16K);
+            this->_early_data_reader = this->_early_data_buf->alloc_reader();
           }
           block->fill(nread);
-          this->early_data_buf->append_block(block);
+          this->_early_data_buf->append_block(block);
           SSL_INCREMENT_DYN_STAT(ssl_early_data_received_count);
 
           if (is_debug_tag_set("ssl_early_data_show_received")) {
@@ -2040,10 +2044,10 @@ SSLNetVConnection::_ssl_accept()
         }
 
         if (ret == SSL_READ_EARLY_DATA_FINISH) {
-          this->early_data_finish = true;
+          this->_early_data_finish = true;
           Debug("ssl_early_data", "SSL_READ_EARLY_DATA_FINISH: size = %lu", nread);
 
-          if (this->early_data_reader == nullptr || this->early_data_reader->read_avail() == 0) {
+          if (this->_early_data_reader == nullptr || this->_early_data_reader->read_avail() == 0) {
             Debug("ssl_early_data", "no data in early data buffer");
             ERR_clear_error();
             ret = SSL_accept(ssl);
@@ -2183,13 +2187,13 @@ SSLNetVConnection::_ssl_read_buffer(void *buf, int64_t nbytes, int64_t &nread)
 #if TS_HAS_TLS_EARLY_DATA
   if (SSL_version(ssl) >= TLS1_3_VERSION) {
     int64_t early_data_len = 0;
-    if (this->early_data_reader != nullptr) {
-      early_data_len = this->early_data_reader->read_avail();
+    if (this->_early_data_reader != nullptr) {
+      early_data_len = this->_early_data_reader->read_avail();
     }
 
     if (early_data_len > 0) {
       Debug("ssl_early_data", "Reading from early data buffer.");
-      this->read_from_early_data += this->early_data_reader->read(buf, nbytes < early_data_len ? nbytes : early_data_len);
+      this->_increment_early_data_len(this->_early_data_reader->read(buf, nbytes < early_data_len ? nbytes : early_data_len));
 
       if (nbytes < early_data_len) {
         nread = nbytes;
@@ -2200,7 +2204,7 @@ SSLNetVConnection::_ssl_read_buffer(void *buf, int64_t nbytes, int64_t &nread)
       return SSL_ERROR_NONE;
     }
 
-    if (SSLConfigParams::server_max_early_data > 0 && !this->early_data_finish) {
+    if (SSLConfigParams::server_max_early_data > 0 && !this->_early_data_finish) {
       Debug("ssl_early_data", "More early data to read.");
       ssl_error_t ssl_error = SSL_ERROR_NONE;
       size_t read_bytes     = 0;
@@ -2213,7 +2217,7 @@ SSLNetVConnection::_ssl_read_buffer(void *buf, int64_t nbytes, int64_t &nread)
         Debug("ssl_early_data", "Error reading early data: %s", ERR_error_string(ERR_get_error(), nullptr));
       } else {
         if ((nread = read_bytes) > 0) {
-          this->read_from_early_data += read_bytes;
+          this->_increment_early_data_len(read_bytes);
           SSL_INCREMENT_DYN_STAT(ssl_early_data_received_count);
           if (is_debug_tag_set("ssl_early_data_show_received")) {
             std::string early_data_str(reinterpret_cast<char *>(buf), nread);
@@ -2222,7 +2226,7 @@ SSLNetVConnection::_ssl_read_buffer(void *buf, int64_t nbytes, int64_t &nread)
         }
 
         if (ret == SSL_READ_EARLY_DATA_FINISH) {
-          this->early_data_finish = true;
+          this->_early_data_finish = true;
           Debug("ssl_early_data", "SSL_READ_EARLY_DATA_FINISH: size = %" PRId64, nread);
         } else {
           Debug("ssl_early_data", "SSL_READ_EARLY_DATA_SUCCESS: size = %" PRId64, nread);
diff --git a/iocore/net/SSLUtils.cc b/iocore/net/SSLUtils.cc
index a8448ad..26cb731 100644
--- a/iocore/net/SSLUtils.cc
+++ b/iocore/net/SSLUtils.cc
@@ -1066,6 +1066,7 @@ SSLInitializeLibrary()
   ALPNSupport::initialize();
   TLSSessionResumptionSupport::initialize();
   TLSSNISupport::initialize();
+  TLSEarlyDataSupport::initialize();
 
   open_ssl_initialized = true;
 }
diff --git a/iocore/net/TLSEarlyDataSupport.cc b/iocore/net/TLSEarlyDataSupport.cc
new file mode 100644
index 0000000..1cce292
--- /dev/null
+++ b/iocore/net/TLSEarlyDataSupport.cc
@@ -0,0 +1,74 @@
+/** @file
+
+  TLSSEarlyDataSupport.cc provides implmentations for
+  TLSEarlyDataSupport methods
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#include <openssl/ssl.h>
+#include "TLSEarlyDataSupport.h"
+#include "tscore/ink_assert.h"
+
+int TLSEarlyDataSupport::_ex_data_index = -1;
+
+void
+TLSEarlyDataSupport::initialize()
+{
+  ink_assert(_ex_data_index == -1);
+  if (_ex_data_index == -1) {
+    _ex_data_index = SSL_get_ex_new_index(0, (void *)"TLSEarlyDataSupport index", nullptr, nullptr, nullptr);
+  }
+}
+
+TLSEarlyDataSupport *
+TLSEarlyDataSupport::getInstance(SSL *ssl)
+{
+  return static_cast<TLSEarlyDataSupport *>(SSL_get_ex_data(ssl, _ex_data_index));
+}
+
+void
+TLSEarlyDataSupport::bind(SSL *ssl, TLSEarlyDataSupport *srs)
+{
+  SSL_set_ex_data(ssl, _ex_data_index, srs);
+}
+
+void
+TLSEarlyDataSupport::unbind(SSL *ssl)
+{
+  SSL_set_ex_data(ssl, _ex_data_index, nullptr);
+}
+
+void
+TLSEarlyDataSupport::clear()
+{
+  this->_early_data_len = 0;
+}
+
+size_t
+TLSEarlyDataSupport::get_early_data_len() const
+{
+  return this->_early_data_len;
+}
+
+void
+TLSEarlyDataSupport::_increment_early_data_len(size_t amount)
+{
+  this->_early_data_len += amount;
+}
diff --git a/iocore/net/TLSEarlyDataSupport.h b/iocore/net/TLSEarlyDataSupport.h
new file mode 100644
index 0000000..f18bbbf
--- /dev/null
+++ b/iocore/net/TLSEarlyDataSupport.h
@@ -0,0 +1,50 @@
+/** @file
+
+  TLSEarlyDataSupport implements common methods and members to
+  support TLS Early Data
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+ */
+
+#pragma once
+
+#include <openssl/ssl.h>
+
+class TLSEarlyDataSupport
+{
+public:
+  virtual ~TLSEarlyDataSupport() = default;
+
+  static void initialize();
+  static TLSEarlyDataSupport *getInstance(SSL *ssl);
+  static void bind(SSL *ssl, TLSEarlyDataSupport *srs);
+  static void unbind(SSL *ssl);
+
+  size_t get_early_data_len() const;
+
+protected:
+  void clear();
+
+  void _increment_early_data_len(size_t amount);
+
+private:
+  static int _ex_data_index;
+
+  size_t _early_data_len = 0;
+};
diff --git a/proxy/http/Http1ClientSession.cc b/proxy/http/Http1ClientSession.cc
index 6ba1588..23f7696 100644
--- a/proxy/http/Http1ClientSession.cc
+++ b/proxy/http/Http1ClientSession.cc
@@ -38,7 +38,8 @@
 #include "Plugin.h"
 #include "PoolableSession.h"
 
-#include "P_SSLNetVConnection.h"
+#include "TLSBasicSupport.h"
+#include "TLSEarlyDataSupport.h"
 
 #define HttpSsnDebug(fmt, ...) SsnDebug(this, "http_cs", fmt, __VA_ARGS__)
 
@@ -140,9 +141,9 @@ Http1ClientSession::new_connection(NetVConnection *new_vc, MIOBuffer *iobuf, IOB
   trans.mutex = mutex; // Share this mutex with the transaction
   in_destroy  = false;
 
-  SSLNetVConnection *ssl_vc = dynamic_cast<SSLNetVConnection *>(new_vc);
-  if (ssl_vc != nullptr) {
-    read_from_early_data = ssl_vc->read_from_early_data;
+  TLSEarlyDataSupport *eds = dynamic_cast<TLSEarlyDataSupport *>(new_vc);
+  if (eds != nullptr) {
+    read_from_early_data = eds->get_early_data_len();
     Debug("ssl_early_data", "read_from_early_data = %" PRId64, read_from_early_data);
   }
 
@@ -533,7 +534,7 @@ bool
 Http1ClientSession::allow_half_open() const
 {
   // Only allow half open connections if the not over TLS
-  return (_vc && dynamic_cast<SSLNetVConnection *>(_vc) == nullptr);
+  return (_vc && dynamic_cast<TLSBasicSupport *>(_vc) == nullptr);
 }
 
 void
diff --git a/proxy/http2/Http2ClientSession.cc b/proxy/http2/Http2ClientSession.cc
index ea0c963..f3d6e4f 100644
--- a/proxy/http2/Http2ClientSession.cc
+++ b/proxy/http2/Http2ClientSession.cc
@@ -25,7 +25,7 @@
 #include "HttpDebugNames.h"
 #include "tscore/ink_base64.h"
 #include "Http2CommonSessionInternal.h"
-#include "P_SSLNetVConnection.h"
+#include "TLSEarlyDataSupport.h"
 
 ClassAllocator<Http2ClientSession, true> http2ClientSessionAllocator("http2ClientSessionAllocator");
 
@@ -101,9 +101,9 @@ Http2ClientSession::new_connection(NetVConnection *new_vc, MIOBuffer *iobuf, IOB
 
   this->connection_state.mutex = this->mutex;
 
-  SSLNetVConnection *ssl_vc = dynamic_cast<SSLNetVConnection *>(new_vc);
-  if (ssl_vc != nullptr) {
-    this->read_from_early_data = ssl_vc->read_from_early_data;
+  TLSEarlyDataSupport *eds = dynamic_cast<TLSEarlyDataSupport *>(new_vc);
+  if (eds != nullptr) {
+    this->read_from_early_data = eds->get_early_data_len();
     Debug("ssl_early_data", "read_from_early_data = %" PRId64, this->read_from_early_data);
   }