/src/curl_fuzzer/proto_fuzzer/tls_mock_server.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Max Dymond, <cmeister2@gmail.com>, et al. |
3 | | * |
4 | | * SPDX-License-Identifier: curl |
5 | | */ |
6 | | |
7 | | /// @file |
8 | | /// @brief Reusable TLS transport for structured in-process protocol peers. |
9 | | |
10 | | #ifndef PROTO_FUZZER_TLS_MOCK_SERVER_H_ |
11 | | #define PROTO_FUZZER_TLS_MOCK_SERVER_H_ |
12 | | |
13 | | #include <cstddef> |
14 | | #include <memory> |
15 | | #include <string> |
16 | | |
17 | | #include "proto_fuzzer/mock_server.h" |
18 | | #include "proto_fuzzer/tls_session_archive.h" |
19 | | |
20 | | namespace proto_fuzzer { |
21 | | |
22 | | /// Application protocol selected by the in-process TLS peer. Keeping this a |
23 | | /// closed enum prevents a caller-controlled ALPN string from making a fixed |
24 | | /// protocol lane negotiate an unrelated transport. |
25 | | enum class TlsApplicationProtocol { |
26 | | /// Ordinary HTTPS scripts contain HTTP/1.1 response bytes. |
27 | | kHttp11, |
28 | | /// The dedicated proxy peer exchanges HTTP/2 frames after its handshake. |
29 | | kHttp2, |
30 | | }; |
31 | | |
32 | | /// Key-exchange group policy fixed when a TLS peer is constructed. Keeping |
33 | | /// this separate from mutable transfer options prevents a scenario from |
34 | | /// configuring incompatible client and server group sets. |
35 | | enum class TlsGroupPolicy { |
36 | | /// Retain the linked OpenSSL provider's preferred group ordering. |
37 | | kProviderDefault, |
38 | | /// Restrict negotiation to the inexpensive X25519 group. |
39 | | kX25519, |
40 | | }; |
41 | | |
42 | | /// TLS 1.3 early-data behavior fixed when a TLS peer is constructed. Keeping |
43 | | /// this explicit prevents unrelated secure protocol lanes from advertising |
44 | | /// 0-RTT merely because their shared carrier supports it. |
45 | | enum class TlsEarlyDataPolicy { |
46 | | /// Do not advertise early-data support in session tickets. |
47 | | kDisabled, |
48 | | /// Advertise a bounded allowance, then reject 0-RTT during SSL_accept. |
49 | | kAdvertiseAndReject, |
50 | | }; |
51 | | |
52 | | TlsEarlyDataPolicy EarlyDataPolicyForScenario(const curl::fuzzer::proto::Scenario& scenario); |
53 | | |
54 | | /// Owns the OpenSSL server context without exposing OpenSSL types through the |
55 | | /// public mock-server headers used by sanitizer builds that disable TLS. |
56 | | class TlsServerContext; |
57 | | |
58 | | /// Reusable TLS carrier for protocol-specific mock peers. It owns the |
59 | | /// per-scenario server context, creates nonblocking TLS connections, and |
60 | | /// installs the checked-in trust anchor without taking ownership of curl's |
61 | | /// socket callbacks or application-protocol drive loop. |
62 | | class TlsMockTransport { |
63 | | public: |
64 | | explicit TlsMockTransport( |
65 | | TlsApplicationProtocol protocol = TlsApplicationProtocol::kHttp11, |
66 | | curl::fuzzer::proto::TlsCertificateChainProfile certificate_chain = |
67 | | curl::fuzzer::proto::TLS_CERTIFICATE_CHAIN_DEFAULT_EC, |
68 | | TlsGroupPolicy group_policy = TlsGroupPolicy::kProviderDefault, |
69 | | curl::fuzzer::proto::TlsHandshakeProfile handshake_profile = curl::fuzzer::proto::TLS_HANDSHAKE_DEFAULT, |
70 | | TlsEarlyDataPolicy early_data_policy = TlsEarlyDataPolicy::kDisabled, |
71 | | curl::fuzzer::proto::TlsOcspStapleProfile ocsp_staple_profile = curl::fuzzer::proto::TLS_OCSP_STAPLE_NONE); |
72 | | ~TlsMockTransport(); |
73 | | |
74 | | TlsMockTransport(const TlsMockTransport&) = delete; |
75 | | TlsMockTransport& operator=(const TlsMockTransport&) = delete; |
76 | | |
77 | | void Install(CURL* easy) const; |
78 | | |
79 | | std::unique_ptr<MockConnection> CreateConnection(); |
80 | | |
81 | | std::unique_ptr<MockConnection> CreateAcceptedConnection(int accepted_fd); |
82 | | |
83 | | int negotiated_tls_version() const; |
84 | | std::size_t completed_handshake_count() const; |
85 | | std::size_t reused_session_count() const; |
86 | | std::size_t write_retry_count() const; |
87 | | std::string negotiated_alpn() const; |
88 | | TlsGroupPolicy group_policy() const; |
89 | | std::string negotiated_group() const; |
90 | | int ech_status() const; |
91 | | std::string ech_inner_name() const; |
92 | | std::string ech_outer_name() const; |
93 | | |
94 | | private: |
95 | | const TlsGroupPolicy group_policy_; |
96 | | const bool openssl_close_notify_; |
97 | | const bool openssl_hello_request_; |
98 | | std::unique_ptr<TlsServerContext> context_; |
99 | | }; |
100 | | |
101 | | /// Runs MockServer's existing bounded HTTP scripts through a real TLS peer. |
102 | | /// The TLS connection itself remains nonblocking and is advanced by the same |
103 | | /// deterministic outer loop that services plaintext socketpairs. |
104 | | class TlsMockServer : public MockServer { |
105 | | public: |
106 | | explicit TlsMockServer(const curl::fuzzer::proto::Scenario& scenario); |
107 | | ~TlsMockServer() override; |
108 | | |
109 | | TlsMockServer(const TlsMockServer&) = delete; |
110 | | TlsMockServer& operator=(const TlsMockServer&) = delete; |
111 | | |
112 | | void Install(CURL* easy) override; |
113 | | |
114 | | bool saw_live_tls_session() const; |
115 | | |
116 | | std::size_t exported_session_count() const; |
117 | | |
118 | | std::size_t imported_session_count() const; |
119 | | |
120 | | CURLcode ImportArchivedSession(CURL* easy); |
121 | | |
122 | | const TlsSessionArchive& session_archive() const; |
123 | | |
124 | | int negotiated_tls_version() const; |
125 | | |
126 | | std::size_t completed_handshake_count() const; |
127 | | |
128 | | std::size_t reused_session_count() const; |
129 | | |
130 | | std::size_t write_retry_count() const; |
131 | | |
132 | | std::string negotiated_alpn() const; |
133 | | |
134 | | std::string negotiated_group() const; |
135 | | |
136 | | int ech_status() const; |
137 | | |
138 | | std::string ech_inner_name() const; |
139 | | |
140 | | std::string ech_outer_name() const; |
141 | | |
142 | | protected: |
143 | | /// Fixed transport role selected by a derived peer before it initializes |
144 | | /// from the complete Scenario. |
145 | | enum class PeerRole { |
146 | | /// Direct HTTP/1.1 or Gopher-over-TLS peer. |
147 | | kHttp11, |
148 | | /// Direct HTTP/2 origin peer. |
149 | | kHttp2Origin, |
150 | | /// HTTPS proxy using the Scenario's fixed proxy protocol. |
151 | | kProxy, |
152 | | }; |
153 | | |
154 | | /// Defer transport setup for a derived peer that will call Init in its |
155 | | /// constructor body. |
156 | 31.6k | TlsMockServer() = default; |
157 | | |
158 | | void Init(const curl::fuzzer::proto::Scenario& scenario, PeerRole role); |
159 | | |
160 | | TlsGroupPolicy group_policy() const; |
161 | | |
162 | | std::unique_ptr<MockConnection> CreateConnection() override; |
163 | | |
164 | | void ObserveActiveTransfer(CURL* easy) override; |
165 | | |
166 | | private: |
167 | | std::unique_ptr<TlsMockTransport> transport_; |
168 | | bool saw_live_tls_session_ = false; |
169 | | std::size_t session_export_attempt_count_ = 0; |
170 | | std::size_t exported_session_count_ = 0; |
171 | | std::size_t imported_session_count_ = 0; |
172 | | curl::fuzzer::proto::TlsSessionCacheProfile session_cache_profile_ = curl::fuzzer::proto::TLS_SESSION_CACHE_DEFAULT; |
173 | | TlsSessionArchive session_archive_; |
174 | | }; |
175 | | |
176 | | } // namespace proto_fuzzer |
177 | | |
178 | | #endif // PROTO_FUZZER_TLS_MOCK_SERVER_H_ |