1 /* 2 * Copyright (c) 2018-present, 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. 7 */ 8 9 #pragma once 10 11 #include <fizz/protocol/Certificate.h> 12 #include <fizz/protocol/Types.h> 13 #include <fizz/record/Types.h> 14 #include <chrono> 15 #include <unordered_map> 16 17 namespace fizz { 18 namespace client { 19 20 struct CachedPsk { 21 std::string psk; 22 std::string secret; 23 PskType type; 24 25 ProtocolVersion version; 26 CipherSuite cipher; 27 folly::Optional<NamedGroup> group; 28 std::shared_ptr<const Cert> serverCert; 29 std::shared_ptr<const Cert> clientCert; 30 31 uint32_t maxEarlyDataSize{0}; 32 folly::Optional<std::string> alpn; 33 34 uint32_t ticketAgeAdd; 35 std::chrono::system_clock::time_point ticketIssueTime; 36 std::chrono::system_clock::time_point ticketExpirationTime; 37 std::chrono::system_clock::time_point ticketHandshakeTime; 38 }; 39 40 class PskCache { 41 public: 42 virtual ~PskCache() = default; 43 44 /** 45 * Retrieve a PSK for the specified identity. 46 */ 47 virtual folly::Optional<CachedPsk> getPsk(const std::string& identity) = 0; 48 49 /** 50 * Add a new PSK for identity to the cache. 51 */ 52 virtual void putPsk(const std::string& identity, CachedPsk) = 0; 53 54 /** 55 * Remove any PSKs associated with identity from the cache. 56 */ 57 virtual void removePsk(const std::string& identity) = 0; 58 }; 59 60 /** 61 * Basic PSK cache that stores PSKs in a hash map. There is no bound on the size 62 * of this cache. 63 */ 64 class BasicPskCache : public PskCache { 65 public: 66 ~BasicPskCache() override = default; 67 getPsk(const std::string & identity)68 folly::Optional<CachedPsk> getPsk(const std::string& identity) override { 69 auto result = cache_.find(identity); 70 if (result != cache_.end()) { 71 return result->second; 72 } else { 73 return folly::none; 74 } 75 } 76 putPsk(const std::string & identity,CachedPsk psk)77 void putPsk(const std::string& identity, CachedPsk psk) override { 78 cache_[identity] = std::move(psk); 79 } 80 removePsk(const std::string & identity)81 void removePsk(const std::string& identity) override { 82 cache_.erase(identity); 83 } 84 85 private: 86 std::unordered_map<std::string, CachedPsk> cache_; 87 }; 88 } // namespace client 89 } // namespace fizz 90