1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  *
7  */
8 
9 #pragma once
10 
11 #include <quic/client/handshake/ClientHandshakeFactory.h>
12 
13 #include <quic/fizz/client/handshake/QuicPskCache.h>
14 #include <quic/fizz/handshake/FizzCryptoFactory.h>
15 
16 #include <fizz/client/FizzClientContext.h>
17 #include <fizz/protocol/DefaultCertificateVerifier.h>
18 
19 namespace quic {
20 
21 class FizzClientHandshake;
22 
23 class FizzClientQuicHandshakeContext
24     : public ClientHandshakeFactory,
25       public std::enable_shared_from_this<FizzClientQuicHandshakeContext> {
26  public:
27   std::unique_ptr<ClientHandshake>
28       makeClientHandshake(QuicClientConnectionState* conn) && override;
29 
getContext()30   const std::shared_ptr<const fizz::client::FizzClientContext>& getContext()
31       const {
32     return context_;
33   }
34 
35   const std::shared_ptr<const fizz::CertificateVerifier>&
getCertificateVerifier()36   getCertificateVerifier() const {
37     return verifier_;
38   }
39 
40   folly::Optional<QuicCachedPsk> getPsk(
41       const folly::Optional<std::string>& hostname);
42   void putPsk(
43       const folly::Optional<std::string>& hostname,
44       QuicCachedPsk quicCachedPsk);
45   void removePsk(const folly::Optional<std::string>& hostname);
46 
47  private:
48   /**
49    * We make the constructor private so that users have to use the Builder
50    * facility. This ensures that
51    *   - This will ALWAYS be managed by a shared_ptr, which the implementation
52    * expects.
53    *   - We can enforce that the internal state of FizzClientContext is always
54    * sane.
55    */
56   FizzClientQuicHandshakeContext(
57       std::shared_ptr<const fizz::client::FizzClientContext> context,
58       std::shared_ptr<const fizz::CertificateVerifier> verifier,
59       std::shared_ptr<QuicPskCache> pskCache);
60 
61   FizzClientQuicHandshakeContext(
62       std::shared_ptr<const fizz::client::FizzClientContext> context,
63       std::shared_ptr<const fizz::CertificateVerifier> verifier,
64       std::shared_ptr<QuicPskCache> pskCache,
65       std::unique_ptr<FizzCryptoFactory> cryptoFactory);
66 
67   std::shared_ptr<const fizz::client::FizzClientContext> context_;
68   std::shared_ptr<const fizz::CertificateVerifier> verifier_;
69   std::shared_ptr<QuicPskCache> pskCache_;
70   std::unique_ptr<FizzCryptoFactory> cryptoFactory_;
71 
72  public:
73   class Builder {
74    public:
setFizzClientContext(std::shared_ptr<const fizz::client::FizzClientContext> context)75     Builder&& setFizzClientContext(
76         std::shared_ptr<const fizz::client::FizzClientContext> context) && {
77       context_ = std::move(context);
78       return std::move(*this);
79     }
80 
setCertificateVerifier(std::shared_ptr<const fizz::CertificateVerifier> verifier)81     Builder&& setCertificateVerifier(
82         std::shared_ptr<const fizz::CertificateVerifier> verifier) && {
83       verifier_ = std::move(verifier);
84       return std::move(*this);
85     }
86 
setPskCache(std::shared_ptr<QuicPskCache> pskCache)87     Builder&& setPskCache(std::shared_ptr<QuicPskCache> pskCache) && {
88       pskCache_ = std::move(pskCache);
89       return std::move(*this);
90     }
91 
setCryptoFactory(std::unique_ptr<FizzCryptoFactory> factory)92     Builder&& setCryptoFactory(std::unique_ptr<FizzCryptoFactory> factory) && {
93       cryptoFactory_ = std::move(factory);
94       return std::move(*this);
95     }
96 
97     std::shared_ptr<FizzClientQuicHandshakeContext> build() &&;
98 
99    private:
100     std::shared_ptr<const fizz::client::FizzClientContext> context_;
101     std::shared_ptr<const fizz::CertificateVerifier> verifier_;
102     std::shared_ptr<QuicPskCache> pskCache_;
103     std::unique_ptr<FizzCryptoFactory> cryptoFactory_;
104   };
105 };
106 
107 } // namespace quic
108