8e7a7d351dedac91ecbc69b08e80d5b4191aa016
[folly.git] / folly / ssl / SSLSession.h
1 /*
2  * Copyright 2017 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
23 namespace folly {
24 namespace ssl {
25
26 class 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_(folly::make_unique<detail::SSLSessionImpl>(
31             session,
32             takeOwnership)) {}
33
34   // Deserialize from a string
35   explicit SSLSession(const std::string& serializedSession)
36       : impl_(folly::make_unique<detail::SSLSessionImpl>(serializedSession)) {}
37
38   // Serialize to a string that is suitable to store in a persistent cache
39   std::string serialize() const {
40     return impl_->serialize();
41   }
42
43   // Get Session ID. Returns an empty container if session isn't set
44   std::string getSessionID() const {
45     return impl_->getSessionID();
46   }
47
48   // Get a const raw SSL_SESSION ptr without incrementing referecnce count
49   // (Warning: do not use)
50   const SSL_SESSION* getRawSSLSession() const {
51     return impl_->getRawSSLSession();
52   }
53
54   // Get raw SSL_SESSION pointer
55   // Warning: do not use unless you know what you're doing - caller needs to
56   // decrement refcount using SSL_SESSION_free or this will leak
57   SSL_SESSION* getRawSSLSessionDangerous() {
58     return impl_->getRawSSLSessionDangerous();
59   }
60
61  private:
62   std::unique_ptr<detail::SSLSessionImpl> impl_;
63 };
64
65 } // namespace ssl
66 } // namespace folly