2 * Copyright 2016-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/ssl/detail/SSLSessionImpl.h>
18 #include <folly/portability/OpenSSL.h>
19 #include <folly/ssl/OpenSSLVersionFinder.h>
26 // Wrapper OpenSSL 1.0.2 (and possibly 1.0.1)
29 SSLSessionImpl::SSLSessionImpl(SSL_SESSION* session, bool takeOwnership)
31 if (session_ == nullptr) {
32 throw std::runtime_error("SSL_SESSION is null");
34 // If we're not given ownership, we need to up the refcount so the SSL_SESSION
35 // object won't be freed while SSLSessionImpl is alive
41 SSLSessionImpl::SSLSessionImpl(const std::string& serializedSession) {
43 reinterpret_cast<const unsigned char*>(serializedSession.data());
44 auto longLen = long(serializedSession.length());
45 if ((session_ = d2i_SSL_SESSION(nullptr, &sessionData, longLen)) == nullptr) {
46 throw std::runtime_error("Cannot deserialize SSLSession string");
50 SSLSessionImpl::~SSLSessionImpl() {
54 std::string SSLSessionImpl::serialize() const {
57 // Get the length first, then we know how much space to allocate.
58 auto len = i2d_SSL_SESSION(session_, nullptr);
61 std::unique_ptr<unsigned char[]> uptr(new unsigned char[size_t(len)]);
63 auto written = i2d_SSL_SESSION(session_, &p);
65 VLOG(2) << "Could not serialize SSL_SESSION!";
67 ret.assign(uptr.get(), uptr.get() + written);
73 std::string SSLSessionImpl::getSessionID() const {
76 const unsigned char* ptr = nullptr;
78 ptr = SSL_SESSION_get_id(session_, &len);
79 ret.assign(ptr, ptr + len);
84 const SSL_SESSION* SSLSessionImpl::getRawSSLSession() const {
85 return const_cast<SSL_SESSION*>(session_);
88 SSL_SESSION* SSLSessionImpl::getRawSSLSessionDangerous() {
93 void SSLSessionImpl::upRef() {
95 SSL_SESSION_up_ref(session_);
99 void SSLSessionImpl::downRef() {
101 SSL_SESSION_free(session_);
105 } // namespace detail