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 #include <grpc/support/port_platform.h> 20 21 #include "src/core/tsi/ssl/session_cache/ssl_session.h" 22 23 #include <grpc/support/log.h> 24 25 #ifndef OPENSSL_IS_BORINGSSL 26 27 // OpenSSL invalidates SSL_SESSION on SSL destruction making it pointless 28 // to cache sessions. The workaround is to serialize (relatively expensive) 29 // session into binary blob and re-create it from blob on every handshake. 30 // Note that it's safe to keep serialized session outside of SSL lifetime 31 // as openssl performs all necessary validation while attempting to use a 32 // session and creates a new one if something is wrong (e.g. server changed 33 // set of allowed codecs). 34 35 namespace tsi { 36 namespace { 37 38 class OpenSslCachedSession : public SslCachedSession { 39 public: OpenSslCachedSession(SslSessionPtr session)40 OpenSslCachedSession(SslSessionPtr session) { 41 int size = i2d_SSL_SESSION(session.get(), nullptr); 42 GPR_ASSERT(size > 0); 43 grpc_slice slice = grpc_slice_malloc(size_t(size)); 44 unsigned char* start = GRPC_SLICE_START_PTR(slice); 45 int second_size = i2d_SSL_SESSION(session.get(), &start); 46 GPR_ASSERT(size == second_size); 47 serialized_session_ = slice; 48 } 49 ~OpenSslCachedSession()50 virtual ~OpenSslCachedSession() { grpc_slice_unref(serialized_session_); } 51 CopySession() const52 SslSessionPtr CopySession() const override { 53 const unsigned char* data = GRPC_SLICE_START_PTR(serialized_session_); 54 size_t length = GRPC_SLICE_LENGTH(serialized_session_); 55 SSL_SESSION* session = d2i_SSL_SESSION(nullptr, &data, length); 56 if (session == nullptr) { 57 return SslSessionPtr(); 58 } 59 return SslSessionPtr(session); 60 } 61 62 private: 63 grpc_slice serialized_session_; 64 }; 65 66 } // namespace 67 Create(SslSessionPtr session)68grpc_core::UniquePtr<SslCachedSession> SslCachedSession::Create( 69 SslSessionPtr session) { 70 return grpc_core::UniquePtr<SslCachedSession>( 71 grpc_core::New<OpenSslCachedSession>(std::move(session))); 72 } 73 74 } // namespace tsi 75 76 #endif /* OPENSSL_IS_BORINGSSL */ 77