DnsTlsSessionCache.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  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. #ifndef _DNS_DNSTLSSESSIONCACHE_H
  17. #define _DNS_DNSTLSSESSIONCACHE_H
  18. #include <deque>
  19. #include <mutex>
  20. #include <openssl/ssl.h>
  21. #include <android-base/thread_annotations.h>
  22. namespace android {
  23. namespace net {
  24. // Cache of recently seen SSL_SESSIONs. This is used to support session tickets.
  25. // This class is thread-safe.
  26. class DnsTlsSessionCache {
  27. public:
  28. // Prepare SSL objects to use this session cache. These methods must be called
  29. // before making use of either object.
  30. void prepareSslContext(SSL_CTX* _Nonnull ssl_ctx);
  31. bool prepareSsl(SSL* _Nonnull ssl);
  32. // Get the most recently discovered session. For TLS 1.3 compatibility and
  33. // maximum privacy, each session will only be returned once, so the caller
  34. // gains ownership of the session. (Here and throughout,
  35. // bssl::UniquePtr<SSL_SESSION> is actually serving as a reference counted
  36. // pointer.)
  37. bssl::UniquePtr<SSL_SESSION> getSession() EXCLUDES(mLock);
  38. private:
  39. static constexpr size_t kMaxSize = 5;
  40. static int newSessionCallback(SSL* _Nullable ssl, SSL_SESSION* _Nullable session);
  41. std::mutex mLock;
  42. void recordSession(SSL_SESSION* _Nullable session) EXCLUDES(mLock);
  43. // Queue of sessions, from least recently added to most recently.
  44. std::deque<bssl::UniquePtr<SSL_SESSION>> mSessions GUARDED_BY(mLock);
  45. };
  46. } // end of namespace net
  47. } // end of namespace android
  48. #endif // _DNS_DNSTLSSESSIONCACHE_H