8817b143b336750aac4eaaaf74205bae7a3c2a0b
[folly.git] / folly / wangle / ssl / SSLCacheProvider.h
1 /*
2  *  Copyright (c) 2015, Facebook, Inc.
3  *  All rights reserved.
4  *
5  *  This source code is licensed under the BSD-style license found in the
6  *  LICENSE file in the root directory of this source tree. An additional grant
7  *  of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #pragma once
11
12 #include <folly/io/async/AsyncSSLSocket.h>
13
14 namespace folly {
15
16 class SSLSessionCacheManager;
17
18 /**
19  * Interface to be implemented by providers of external session caches
20  */
21 class SSLCacheProvider {
22 public:
23   /**
24    * Context saved during an external cache request that is used to
25    * resume the waiting client.
26    */
27   typedef struct {
28     std::string sessionId;
29     SSL_SESSION* session;
30     SSLSessionCacheManager* manager;
31     AsyncSSLSocket* sslSocket;
32     std::unique_ptr<
33       folly::DelayedDestruction::DestructorGuard> guard;
34   } CacheContext;
35
36   virtual ~SSLCacheProvider() {}
37
38   /**
39    * Store a session in the external cache.
40    * @param sessionId   Identifier that can be used later to fetch the
41    *                      session with getAsync()
42    * @param value       Serialized session to store
43    * @param expiration  Relative expiration time: seconds from now
44    * @return true if the storing of the session is initiated successfully
45    *         (though not necessarily completed; the completion may
46    *         happen either before or after this method returns), or
47    *         false if the storing cannot be initiated due to an error.
48    */
49   virtual bool setAsync(const std::string& sessionId,
50                         const std::string& value,
51                         std::chrono::seconds expiration) = 0;
52
53   /**
54    * Retrieve a session from the external cache. When done, call
55    * the cache manager's onGetSuccess() or onGetFailure() callback.
56    * @param sessionId   Session ID to fetch
57    * @param context     Data to pass back to the SSLSessionCacheManager
58    *                      in the completion callback
59    * @return true if the lookup of the session is initiated successfully
60    *         (though not necessarily completed; the completion may
61    *         happen either before or after this method returns), or
62    *         false if the lookup cannot be initiated due to an error.
63    */
64   virtual bool getAsync(const std::string& sessionId,
65                         CacheContext* context) = 0;
66
67 };
68
69 }