1/*
2 * Copyright 2016-present Facebook, Inc.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#pragma once
18
19#include <folly/Memory.h>
20#include <folly/portability/OpenSSL.h>
21#include <folly/ssl/detail/SSLSessionImpl.h>
22
23namespace folly {
24namespace ssl {
25
26class SSLSession {
27 public:
28 // Holds and takes ownership of an SSL_SESSION object by incrementing refcount
29 explicit SSLSession(SSL_SESSION* session, bool takeOwnership = true)
30 : impl_(
31 std::make_unique<detail::SSLSessionImpl>(session, takeOwnership)) {}
32
33 // Deserialize from a string
34 explicit SSLSession(const std::string& serializedSession)
35 : impl_(std::make_unique<detail::SSLSessionImpl>(serializedSession)) {}
36
37 // Serialize to a string that is suitable to store in a persistent cache
38 std::string serialize() const {
39 return impl_->serialize();
40 }
41
42 // Get Session ID. Returns an empty container if session isn't set
43 std::string getSessionID() const {
44 return impl_->getSessionID();
45 }
46
47 // Get a const raw SSL_SESSION ptr without incrementing referecnce count
48 // (Warning: do not use)
49 const SSL_SESSION* getRawSSLSession() const {
50 return impl_->getRawSSLSession();
51 }
52
53 // Get raw SSL_SESSION pointer
54 // Warning: do not use unless you know what you're doing - caller needs to
55 // decrement refcount using SSL_SESSION_free or this will leak
56 SSL_SESSION* getRawSSLSessionDangerous() {
57 return impl_->getRawSSLSessionDangerous();
58 }
59
60 private:
61 std::unique_ptr<detail::SSLSessionImpl> impl_;
62};
63
64} // namespace ssl
65} // namespace folly
66