1 // 2 // 3 // Copyright 2018 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_H 20 #define GRPC_SRC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_H 21 22 #include <grpc/slice.h> 23 #include <grpc/support/port_platform.h> 24 #include <openssl/ssl.h> 25 26 #include <memory> 27 28 #include "src/core/util/ref_counted.h" 29 30 // The main purpose of code here is to provide means to cache SSL sessions 31 // in a way that they can be shared between connections. 32 // 33 // SSL_SESSION stands for single instance of session and is not generally safe 34 // to share between SSL contexts with different lifetimes. It happens because 35 // not all SSL implementations guarantee immutability of SSL_SESSION object. 36 // See SSL_SESSION documentation in BoringSSL and OpenSSL for more details. 37 38 namespace tsi { 39 40 struct SslSessionDeleter { operatorSslSessionDeleter41 void operator()(SSL_SESSION* session) { SSL_SESSION_free(session); } 42 }; 43 44 typedef std::unique_ptr<SSL_SESSION, SslSessionDeleter> SslSessionPtr; 45 46 /// SslCachedSession is an immutable thread-safe storage for single session 47 /// representation. It provides means to share SSL session data (e.g. TLS 48 /// ticket) between encrypted connections regardless of SSL context lifetime. 49 class SslCachedSession { 50 public: 51 // Not copyable nor movable. 52 SslCachedSession(const SslCachedSession&) = delete; 53 SslCachedSession& operator=(const SslCachedSession&) = delete; 54 55 /// Create single cached instance of \a session. 56 static std::unique_ptr<SslCachedSession> Create(SslSessionPtr session); 57 58 virtual ~SslCachedSession() = default; 59 60 /// Returns a copy of previously cached session. 61 virtual SslSessionPtr CopySession() const = 0; 62 63 protected: 64 SslCachedSession() = default; 65 }; 66 67 } // namespace tsi 68 69 #endif // GRPC_SRC_CORE_TSI_SSL_SESSION_CACHE_SSL_SESSION_H 70