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/client/ClientExtensions.h>
12 #include <fizz/client/PskCache.h>
13 #include <fizz/crypto/aead/Aead.h>
14 #include <fizz/protocol/Events.h>
15 #include <fizz/protocol/ech/Types.h>
16 #include <fizz/record/Types.h>
17 #include <fizz/util/Variant.h>
18 #include <folly/io/IOBuf.h>
19 #include <folly/io/async/AsyncTransport.h>
20 
21 namespace fizz {
22 
23 class CertificateVerifier;
24 class ServerExtensions;
25 
26 namespace server {
27 class FizzServerContext;
28 }
29 
30 namespace client {
31 class FizzClientContext;
32 }
33 
34 struct Accept : EventType<Event::Accept> {
35   folly::Executor* executor;
36   std::shared_ptr<const server::FizzServerContext> context;
37   std::shared_ptr<ServerExtensions> extensions;
38 };
39 
40 struct Connect : EventType<Event::Connect> {
41   std::shared_ptr<const client::FizzClientContext> context;
42   std::shared_ptr<const CertificateVerifier> verifier;
43   folly::Optional<std::string> sni;
44   folly::Optional<client::CachedPsk> cachedPsk;
45   std::shared_ptr<ClientExtensions> extensions;
46   folly::Optional<std::vector<ech::ECHConfig>> echConfigs;
47 };
48 
49 struct EarlyAppWrite : EventType<Event::EarlyAppWrite> {
50   folly::AsyncTransportWrapper::WriteCallback* callback{nullptr};
51   std::unique_ptr<folly::IOBuf> data;
52   folly::WriteFlags flags;
53   Aead::AeadOptions aeadOptions;
54 };
55 
56 struct AppWrite : EventType<Event::AppWrite> {
57   folly::AsyncTransportWrapper::WriteCallback* callback{nullptr};
58   std::unique_ptr<folly::IOBuf> data;
59   folly::WriteFlags flags;
60   Aead::AeadOptions aeadOptions;
61 };
62 
63 struct AppData : EventType<Event::AppData> {
64   std::unique_ptr<folly::IOBuf> data;
65 
AppDataAppData66   explicit AppData(std::unique_ptr<folly::IOBuf> buf) : data(std::move(buf)) {}
67 };
68 
69 struct WriteNewSessionTicket : EventType<Event::WriteNewSessionTicket> {
70   Buf appToken;
71 };
72 
73 /**
74  * Parameters for each event that will be processed by the state machine.
75  */
76 #define FIZZ_PARAM(F, ...)              \
77   F(ClientHello, __VA_ARGS__)           \
78   F(ServerHello, __VA_ARGS__)           \
79   F(EndOfEarlyData, __VA_ARGS__)        \
80   F(HelloRetryRequest, __VA_ARGS__)     \
81   F(EncryptedExtensions, __VA_ARGS__)   \
82   F(CertificateRequest, __VA_ARGS__)    \
83   F(CompressedCertificate, __VA_ARGS__) \
84   F(CertificateMsg, __VA_ARGS__)        \
85   F(CertificateVerify, __VA_ARGS__)     \
86   F(Finished, __VA_ARGS__)              \
87   F(NewSessionTicket, __VA_ARGS__)      \
88   F(KeyUpdate, __VA_ARGS__)             \
89   F(Alert, __VA_ARGS__)                 \
90   F(CloseNotify, __VA_ARGS__)           \
91   F(Accept, __VA_ARGS__)                \
92   F(Connect, __VA_ARGS__)               \
93   F(AppData, __VA_ARGS__)               \
94   F(AppWrite, __VA_ARGS__)              \
95   F(EarlyAppWrite, __VA_ARGS__)         \
96   F(WriteNewSessionTicket, __VA_ARGS__)
97 
FIZZ_DECLARE_VARIANT_TYPE(Param,FIZZ_PARAM)98 FIZZ_DECLARE_VARIANT_TYPE(Param, FIZZ_PARAM)
99 
100 // Given a Param variant, return the corresponding Event
101 class EventVisitor {
102  public:
103   Event operator()(const Param& param) const;
104 };
105 
106 // App closes bypass the state machine so aren't in the Param variant.
107 struct AppClose {
108   enum ClosePolicy { IMMEDIATE, WAIT };
109 
AppCloseAppClose110   /*implicit */ constexpr AppClose(ClosePolicy pol) : policy(pol) {}
111 
112   ClosePolicy policy;
113 };
114 
115 } // namespace fizz
116