ssl_session_openssl.cc
Go to the documentation of this file.
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 
20 
21 #include <grpc/support/log.h>
22 
24 
25 #ifndef OPENSSL_IS_BORINGSSL
26 
27 #include "absl/memory/memory.h"
28 
29 // OpenSSL invalidates SSL_SESSION on SSL destruction making it pointless
30 // to cache sessions. The workaround is to serialize (relatively expensive)
31 // session into binary blob and re-create it from blob on every handshake.
32 // Note that it's safe to keep serialized session outside of SSL lifetime
33 // as openssl performs all necessary validation while attempting to use a
34 // session and creates a new one if something is wrong (e.g. server changed
35 // set of allowed codecs).
36 
37 namespace tsi {
38 namespace {
39 
40 class OpenSslCachedSession : public SslCachedSession {
41  public:
42  OpenSslCachedSession(SslSessionPtr session) {
43  int size = i2d_SSL_SESSION(session.get(), nullptr);
44  GPR_ASSERT(size > 0);
46  unsigned char* start = GRPC_SLICE_START_PTR(slice);
47  int second_size = i2d_SSL_SESSION(session.get(), &start);
48  GPR_ASSERT(size == second_size);
49  serialized_session_ = slice;
50  }
51 
52  virtual ~OpenSslCachedSession() { grpc_slice_unref(serialized_session_); }
53 
54  SslSessionPtr CopySession() const override {
55  const unsigned char* data = GRPC_SLICE_START_PTR(serialized_session_);
56  size_t length = GRPC_SLICE_LENGTH(serialized_session_);
57  SSL_SESSION* session = d2i_SSL_SESSION(nullptr, &data, length);
58  if (session == nullptr) {
59  return SslSessionPtr();
60  }
61  return SslSessionPtr(session);
62  }
63 
64  private:
65  grpc_slice serialized_session_;
66 };
67 
68 } // namespace
69 
70 std::unique_ptr<SslCachedSession> SslCachedSession::Create(
71  SslSessionPtr session) {
72  return absl::make_unique<OpenSslCachedSession>(std::move(session));
73 }
74 
75 } // namespace tsi
76 
77 #endif /* OPENSSL_IS_BORINGSSL */
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
log.h
grpc_slice_malloc
GPRAPI grpc_slice grpc_slice_malloc(size_t length)
Definition: slice/slice.cc:227
start
static uint64_t start
Definition: benchmark-pound.c:74
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
tsi::SslSessionPtr
std::unique_ptr< SSL_SESSION, SslSessionDeleter > SslSessionPtr
Definition: ssl_session.h:46
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
ssl_session_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3787
tsi
Definition: ssl_key_logging.cc:29
ssl_session.h
i2d_SSL_SESSION
#define i2d_SSL_SESSION
Definition: boringssl_prefix_symbols.h:552
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
tsi::SslCachedSession::Create
static std::unique_ptr< SslCachedSession > Create(SslSessionPtr session)
Create single cached instance of session.
Definition: ssl_session_openssl.cc:70
d2i_SSL_SESSION
OPENSSL_EXPORT SSL_SESSION * d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length)
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:21