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