proxygen
AsyncFizzClient.h
Go to the documentation of this file.
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 
14 #include <fizz/client/FizzClient.h>
17 #include <fizz/protocol/Exporter.h>
18 
19 namespace fizz {
20 namespace client {
21 
22 template <typename SM>
25  public:
27  public:
28  virtual ~HandshakeCallback() = default;
29 
30  virtual void fizzHandshakeSuccess(AsyncFizzClientT* transport) noexcept = 0;
31 
32  virtual void fizzHandshakeError(
33  AsyncFizzClientT* transport,
34  folly::exception_wrapper ex) noexcept = 0;
35  };
36 
37  using UniquePtr =
38  std::unique_ptr<AsyncFizzClientT, folly::DelayedDestruction::Destructor>;
39 
46  std::shared_ptr<const FizzClientContext> fizzContext,
47  const std::shared_ptr<ClientExtensions>& extensions = nullptr);
48 
55  folly::EventBase* eventBase,
56  std::shared_ptr<const FizzClientContext> fizzContext,
57  const std::shared_ptr<ClientExtensions>& extensions = nullptr);
58 
62  virtual void connect(
63  HandshakeCallback* callback,
64  std::shared_ptr<const CertificateVerifier> verifier,
66  folly::Optional<std::string> pskIdentity,
67  std::chrono::milliseconds = std::chrono::milliseconds(0));
68 
72  virtual void connect(
73  const folly::SocketAddress& connectAddr,
75  std::shared_ptr<const CertificateVerifier> verifier,
77  folly::Optional<std::string> pskIdentity,
78  std::chrono::milliseconds totalTimeout = std::chrono::milliseconds(0),
79  std::chrono::milliseconds socketTimeout = std::chrono::milliseconds(0),
80  const folly::AsyncSocket::OptionMap& options =
83 
88  virtual void connect(
89  HandshakeCallback* callback,
91  std::chrono::milliseconds = std::chrono::milliseconds(0));
92 
93  bool good() const override;
94  bool readable() const override;
95  bool connecting() const override;
96  bool error() const override;
97 
98  folly::ssl::X509UniquePtr getPeerCert() const override;
99  const X509* getSelfCert() const override;
100 
101  const Cert* getPeerCertificate() const override;
102  const Cert* getSelfCertificate() const override;
103 
104  bool isReplaySafe() const override;
108 
109  void close() override;
110  void closeWithReset() override;
111  void closeNow() override;
112 
120  CHECK(!earlyDataState_);
121  earlyDataRejectionPolicy_ = policy;
122  }
123 
127  const State& getState() const {
128  return state_;
129  }
130 
131  folly::Optional<CipherSuite> getCipher() const override;
132 
133  std::vector<SignatureScheme> getSupportedSigSchemes() const override;
134 
136  const override;
137 
138  Buf getEarlyEkm(folly::StringPiece label, const Buf& context, uint16_t length)
139  const;
140 
141  bool pskResumed() const;
142 
143  protected:
144  void writeAppData(
146  std::unique_ptr<folly::IOBuf>&& buf,
148 
149  void transportError(const folly::AsyncSocketException& ex) override;
150 
151  void transportDataAvailable() override;
152 
153  private:
154  void deliverAllErrors(
155  const folly::AsyncSocketException& ex,
156  bool closeTransport = true);
158 
159  void connectErr(const folly::AsyncSocketException& ex) noexcept override;
160  void connectSuccess() noexcept override;
161 
163 
164  class ActionMoveVisitor : public boost::static_visitor<> {
165  public:
167  : client_(client) {}
168 
169  void operator()(DeliverAppData&);
170  void operator()(WriteToSocket&);
171  void operator()(ReportEarlyHandshakeSuccess&);
172  void operator()(ReportHandshakeSuccess&);
173  void operator()(ReportEarlyWriteFailed&);
174  void operator()(ReportError&);
175  void operator()(WaitForData&);
176  void operator()(MutateState&);
177  void operator()(NewCachedPsk&);
178 
179  private:
181  };
182 
184  boost::variant<HandshakeCallback*, folly::AsyncSocket::ConnectCallback*>>
186 
187  std::shared_ptr<const FizzClientContext> fizzContext_;
188 
189  std::shared_ptr<ClientExtensions> extensions_;
190 
192 
194 
196 
197  ActionMoveVisitor visitor_;
198 
200 
201  struct EarlyDataState {
202  // How much data is remaining in max early data size.
203  uint32_t remainingEarlyData{0};
204 
205  // Early data that has been written so far. Only used with AutomaticResend
206  // rejection policy.
208 
209  // Writes that we haven't written yet due to exceeding the max early data
210  // size.
211  std::deque<AppWrite> pendingAppWrites;
212  };
213 
214  // Only set if we are currently in early data state.
216 
219 
221 
222  // Set when using socket connect() API to later pass into the state machine
223  std::shared_ptr<const CertificateVerifier> verifier_;
224 };
225 
227 } // namespace client
228 } // namespace fizz
229 
ActionMoveVisitor(AsyncFizzClientT< SM > &client)
std::shared_ptr< const FizzClientContext > fizzContext_
flags
Definition: http_parser.h:127
Buf getEarlyEkm(folly::StringPiece label, const Buf &context, uint16_t length) const
EarlyDataRejectionPolicy earlyDataRejectionPolicy_
folly::Optional< CipherSuite > getCipher() const override
void setEarlyDataRejectionPolicy(EarlyDataRejectionPolicy policy)
std::unique_ptr< X509, X509Deleter > X509UniquePtr
void connectSuccess() noexceptoverride
void connectErr(const folly::AsyncSocketException &ex) noexceptoverride
context
Definition: CMakeCache.txt:563
virtual void fizzHandshakeError(AsyncFizzClientT *transport, folly::exception_wrapper ex) noexcept=0
bool isReplaySafe() const override
void transportError(const folly::AsyncSocketException &ex) override
folly::AsyncTransport::ReplaySafetyCallback * replaySafetyCallback_
requires E e noexcept(noexcept(s.error(std::move(e))))
std::unique_ptr< AsyncFizzClientT, folly::DelayedDestruction::Destructor > UniquePtr
folly::Optional< folly::AsyncSocketException > handleEarlyReject()
std::map< OptionKey, int > OptionMap
Definition: AsyncSocket.h:376
std::unique_ptr< AsyncTransportWrapper, Destructor > UniquePtr
void writeAppData(folly::AsyncTransportWrapper::WriteCallback *callback, std::unique_ptr< folly::IOBuf > &&buf, folly::WriteFlags flags=folly::WriteFlags::NONE) override
void setReplaySafetyCallback(folly::AsyncTransport::ReplaySafetyCallback *callback) override
static Options cacheChainLength()
Definition: IOBufQueue.h:83
FizzClient< ActionMoveVisitor, SM > fizzClient_
static const folly::SocketAddress & anyAddress()
folly::Optional< boost::variant< HandshakeCallback *, folly::AsyncSocket::ConnectCallback * > > callback_
std::shared_ptr< ClientExtensions > extensions_
const Cert * getSelfCertificate() const override
void deliverAllErrors(const folly::AsyncSocketException &ex, bool closeTransport=true)
Definition: Actions.h:16
static const OptionMap emptyOptionMap
Definition: AsyncSocket.h:378
virtual void fizzHandshakeSuccess(AsyncFizzClientT *transport) noexcept=0
NetworkSocket socket(int af, int type, int protocol)
Definition: NetOps.cpp:412
folly::Optional< EarlyDataState > earlyDataState_
std::shared_ptr< const CertificateVerifier > verifier_
void deliverHandshakeError(folly::exception_wrapper ex)
std::vector< SignatureScheme > getSupportedSigSchemes() const override
StringPiece sni
const char * string
Definition: Conv.cpp:212
Buf getEkm(folly::StringPiece label, const Buf &context, uint16_t length) const override
std::unique_ptr< folly::IOBuf > Buf
Definition: Types.h:22
const X509 * getSelfCert() const override
const
Definition: upload.py:398
folly::ssl::X509UniquePtr getPeerCert() const override
const Cert * getPeerCertificate() const override
folly::Optional< std::string > pskIdentity_
folly::Optional< std::string > sni_
virtual void connect(HandshakeCallback *callback, std::shared_ptr< const CertificateVerifier > verifier, folly::Optional< std::string > sni, folly::Optional< std::string > pskIdentity, std::chrono::milliseconds=std::chrono::milliseconds(0))
const State & getState() const
std::string getApplicationProtocol() const noexceptoverride
AsyncFizzClientT(folly::AsyncTransportWrapper::UniquePtr socket, std::shared_ptr< const FizzClientContext > fizzContext, const std::shared_ptr< ClientExtensions > &extensions=nullptr)
StringPiece label