Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 1 | // Copyright (c) 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef QUICHE_QUIC_CORE_TLS_SERVER_HANDSHAKER_H_ |
| 6 | #define QUICHE_QUIC_CORE_TLS_SERVER_HANDSHAKER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "absl/strings/string_view.h" |
vasilvv | daa2fda | 2022-04-11 14:08:36 -0700 | [diff] [blame] | 11 | #include "openssl/pool.h" |
| 12 | #include "openssl/ssl.h" |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 13 | #include "quiche/quic/core/crypto/quic_crypto_server_config.h" |
| 14 | #include "quiche/quic/core/crypto/tls_server_connection.h" |
| 15 | #include "quiche/quic/core/proto/cached_network_parameters_proto.h" |
| 16 | #include "quiche/quic/core/quic_crypto_server_stream_base.h" |
| 17 | #include "quiche/quic/core/quic_crypto_stream.h" |
| 18 | #include "quiche/quic/core/quic_time_accumulator.h" |
| 19 | #include "quiche/quic/core/quic_types.h" |
| 20 | #include "quiche/quic/core/tls_handshaker.h" |
| 21 | #include "quiche/quic/platform/api/quic_export.h" |
| 22 | #include "quiche/quic/platform/api/quic_flag_utils.h" |
| 23 | #include "quiche/quic/platform/api/quic_flags.h" |
| 24 | |
| 25 | namespace quic { |
| 26 | |
| 27 | // An implementation of QuicCryptoServerStreamBase which uses |
| 28 | // TLS 1.3 for the crypto handshake protocol. |
| 29 | class QUIC_EXPORT_PRIVATE TlsServerHandshaker |
| 30 | : public TlsHandshaker, |
| 31 | public TlsServerConnection::Delegate, |
| 32 | public ProofSourceHandleCallback, |
| 33 | public QuicCryptoServerStreamBase { |
| 34 | public: |
| 35 | // |crypto_config| must outlive TlsServerHandshaker. |
| 36 | TlsServerHandshaker(QuicSession* session, |
| 37 | const QuicCryptoServerConfig* crypto_config); |
| 38 | TlsServerHandshaker(const TlsServerHandshaker&) = delete; |
| 39 | TlsServerHandshaker& operator=(const TlsServerHandshaker&) = delete; |
| 40 | |
| 41 | ~TlsServerHandshaker() override; |
| 42 | |
| 43 | // From QuicCryptoServerStreamBase |
| 44 | void CancelOutstandingCallbacks() override; |
| 45 | bool GetBase64SHA256ClientChannelID(std::string* output) const override; |
| 46 | void SendServerConfigUpdate( |
| 47 | const CachedNetworkParameters* cached_network_params) override; |
| 48 | bool DisableResumption() override; |
| 49 | bool IsZeroRtt() const override; |
| 50 | bool IsResumption() const override; |
| 51 | bool ResumptionAttempted() const override; |
| 52 | // Must be called after EarlySelectCertCallback is started. |
| 53 | bool EarlyDataAttempted() const override; |
| 54 | int NumServerConfigUpdateMessagesSent() const override; |
| 55 | const CachedNetworkParameters* PreviousCachedNetworkParams() const override; |
| 56 | void SetPreviousCachedNetworkParams( |
| 57 | CachedNetworkParameters cached_network_params) override; |
| 58 | void OnPacketDecrypted(EncryptionLevel level) override; |
| 59 | void OnOneRttPacketAcknowledged() override {} |
| 60 | void OnHandshakePacketSent() override {} |
| 61 | void OnConnectionClosed(QuicErrorCode error, |
| 62 | ConnectionCloseSource source) override; |
| 63 | void OnHandshakeDoneReceived() override; |
| 64 | std::string GetAddressToken( |
| 65 | const CachedNetworkParameters* cached_network_params) const override; |
| 66 | bool ValidateAddressToken(absl::string_view token) const override; |
| 67 | void OnNewTokenReceived(absl::string_view token) override; |
| 68 | bool ShouldSendExpectCTHeader() const override; |
| 69 | bool DidCertMatchSni() const override; |
| 70 | const ProofSource::Details* ProofSourceDetails() const override; |
| 71 | bool ExportKeyingMaterial(absl::string_view label, absl::string_view context, |
| 72 | size_t result_len, std::string* result) override; |
| 73 | SSL* GetSsl() const override; |
| 74 | |
| 75 | // From QuicCryptoServerStreamBase and TlsHandshaker |
| 76 | ssl_early_data_reason_t EarlyDataReason() const override; |
| 77 | bool encryption_established() const override; |
| 78 | bool one_rtt_keys_available() const override; |
| 79 | const QuicCryptoNegotiatedParameters& crypto_negotiated_params() |
| 80 | const override; |
| 81 | CryptoMessageParser* crypto_message_parser() override; |
| 82 | HandshakeState GetHandshakeState() const override; |
| 83 | void SetServerApplicationStateForResumption( |
| 84 | std::unique_ptr<ApplicationState> state) override; |
| 85 | size_t BufferSizeLimitForLevel(EncryptionLevel level) const override; |
| 86 | std::unique_ptr<QuicDecrypter> AdvanceKeysAndCreateCurrentOneRttDecrypter() |
| 87 | override; |
| 88 | std::unique_ptr<QuicEncrypter> CreateCurrentOneRttEncrypter() override; |
| 89 | void SetWriteSecret(EncryptionLevel level, |
| 90 | const SSL_CIPHER* cipher, |
| 91 | const std::vector<uint8_t>& write_secret) override; |
| 92 | |
| 93 | // Called with normalized SNI hostname as |hostname|. Return value will be |
| 94 | // sent in an ACCEPT_CH frame in the TLS ALPS extension, unless empty. |
| 95 | virtual std::string GetAcceptChValueForHostname( |
| 96 | const std::string& hostname) const; |
| 97 | |
| 98 | // Get the ClientCertMode that is currently in effect on this handshaker. |
| 99 | ClientCertMode client_cert_mode() const { |
| 100 | return tls_connection_.ssl_config().client_cert_mode; |
| 101 | } |
| 102 | |
| 103 | protected: |
| 104 | // Override for tracing. |
| 105 | void InfoCallback(int type, int value) override; |
| 106 | |
| 107 | // Creates a proof source handle for selecting cert and computing signature. |
| 108 | virtual std::unique_ptr<ProofSourceHandle> MaybeCreateProofSourceHandle(); |
| 109 | |
| 110 | // Hook to allow the server to override parts of the QuicConfig based on SNI |
| 111 | // before we generate transport parameters. |
| 112 | virtual void OverrideQuicConfigDefaults(QuicConfig* config); |
| 113 | |
| 114 | virtual bool ValidateHostname(const std::string& hostname) const; |
| 115 | |
| 116 | const TlsConnection* tls_connection() const override { |
| 117 | return &tls_connection_; |
| 118 | } |
| 119 | |
| 120 | virtual void ProcessAdditionalTransportParameters( |
| 121 | const TransportParameters& /*params*/) {} |
| 122 | |
| 123 | // Called when a potentially async operation is done and the done callback |
| 124 | // needs to advance the handshake. |
| 125 | void AdvanceHandshakeFromCallback(); |
| 126 | |
| 127 | // TlsHandshaker implementation: |
| 128 | void FinishHandshake() override; |
| 129 | void ProcessPostHandshakeMessage() override {} |
| 130 | QuicAsyncStatus VerifyCertChain( |
| 131 | const std::vector<std::string>& certs, |
| 132 | std::string* error_details, |
| 133 | std::unique_ptr<ProofVerifyDetails>* details, |
| 134 | uint8_t* out_alert, |
| 135 | std::unique_ptr<ProofVerifierCallback> callback) override; |
| 136 | void OnProofVerifyDetailsAvailable( |
| 137 | const ProofVerifyDetails& verify_details) override; |
| 138 | |
| 139 | // TlsServerConnection::Delegate implementation: |
| 140 | // Used to select certificates and process transport parameters. |
| 141 | ssl_select_cert_result_t EarlySelectCertCallback( |
| 142 | const SSL_CLIENT_HELLO* client_hello) override; |
| 143 | int TlsExtServernameCallback(int* out_alert) override; |
| 144 | int SelectAlpn(const uint8_t** out, |
| 145 | uint8_t* out_len, |
| 146 | const uint8_t* in, |
| 147 | unsigned in_len) override; |
| 148 | ssl_private_key_result_t PrivateKeySign(uint8_t* out, |
| 149 | size_t* out_len, |
| 150 | size_t max_out, |
| 151 | uint16_t sig_alg, |
| 152 | absl::string_view in) override; |
| 153 | ssl_private_key_result_t PrivateKeyComplete(uint8_t* out, |
| 154 | size_t* out_len, |
| 155 | size_t max_out) override; |
| 156 | size_t SessionTicketMaxOverhead() override; |
| 157 | int SessionTicketSeal(uint8_t* out, |
| 158 | size_t* out_len, |
| 159 | size_t max_out_len, |
| 160 | absl::string_view in) override; |
| 161 | ssl_ticket_aead_result_t SessionTicketOpen(uint8_t* out, |
| 162 | size_t* out_len, |
| 163 | size_t max_out_len, |
| 164 | absl::string_view in) override; |
| 165 | // Called when ticket_decryption_callback_ is done to determine a final |
| 166 | // decryption result. |
| 167 | ssl_ticket_aead_result_t FinalizeSessionTicketOpen(uint8_t* out, |
| 168 | size_t* out_len, |
| 169 | size_t max_out_len); |
| 170 | TlsConnection::Delegate* ConnectionDelegate() override { return this; } |
| 171 | |
| 172 | // The status of cert selection. nullopt means it hasn't started. |
| 173 | const absl::optional<QuicAsyncStatus>& select_cert_status() const { |
| 174 | return select_cert_status_; |
| 175 | } |
| 176 | // Whether |cert_verify_sig_| contains a valid signature. |
| 177 | // NOTE: BoringSSL queries the result of a async signature operation using |
| 178 | // PrivateKeyComplete(), a successful PrivateKeyComplete() will clear the |
| 179 | // content of |cert_verify_sig_|, this function should not be called after |
| 180 | // that. |
| 181 | bool HasValidSignature(size_t max_signature_size) const; |
| 182 | |
| 183 | // ProofSourceHandleCallback implementation: |
| 184 | void OnSelectCertificateDone( |
| 185 | bool ok, bool is_sync, const ProofSource::Chain* chain, |
| 186 | absl::string_view handshake_hints, |
| 187 | absl::string_view ticket_encryption_key, bool cert_matched_sni, |
| 188 | QuicDelayedSSLConfig delayed_ssl_config) override; |
| 189 | |
| 190 | void OnComputeSignatureDone( |
| 191 | bool ok, |
| 192 | bool is_sync, |
| 193 | std::string signature, |
| 194 | std::unique_ptr<ProofSource::Details> details) override; |
| 195 | |
| 196 | void set_encryption_established(bool encryption_established) { |
| 197 | encryption_established_ = encryption_established; |
| 198 | } |
| 199 | |
| 200 | bool WillNotCallComputeSignature() const override; |
| 201 | |
| 202 | void SetIgnoreTicketOpen(bool value) { ignore_ticket_open_ = value; } |
| 203 | |
| 204 | private: |
| 205 | class QUIC_EXPORT_PRIVATE DecryptCallback |
| 206 | : public ProofSource::DecryptCallback { |
| 207 | public: |
| 208 | explicit DecryptCallback(TlsServerHandshaker* handshaker); |
| 209 | void Run(std::vector<uint8_t> plaintext) override; |
| 210 | |
| 211 | // If called, Cancel causes the pending callback to be a no-op. |
| 212 | void Cancel(); |
| 213 | |
| 214 | // Return true if either |
| 215 | // - Cancel() has been called. |
| 216 | // - Run() has been called, or is in the middle of it. |
| 217 | bool IsDone() const { return handshaker_ == nullptr; } |
| 218 | |
| 219 | private: |
| 220 | TlsServerHandshaker* handshaker_; |
| 221 | }; |
| 222 | |
| 223 | // DefaultProofSourceHandle delegates all operations to the shared proof |
| 224 | // source. |
| 225 | class QUIC_EXPORT_PRIVATE DefaultProofSourceHandle |
| 226 | : public ProofSourceHandle { |
| 227 | public: |
| 228 | DefaultProofSourceHandle(TlsServerHandshaker* handshaker, |
| 229 | ProofSource* proof_source); |
| 230 | |
| 231 | ~DefaultProofSourceHandle() override; |
| 232 | |
| 233 | // Close the handle. Cancel the pending signature operation, if any. |
| 234 | void CloseHandle() override; |
| 235 | |
| 236 | // Delegates to proof_source_->GetCertChain. |
| 237 | // Returns QUIC_SUCCESS or QUIC_FAILURE. Never returns QUIC_PENDING. |
| 238 | QuicAsyncStatus SelectCertificate( |
| 239 | const QuicSocketAddress& server_address, |
| 240 | const QuicSocketAddress& client_address, |
| 241 | absl::string_view ssl_capabilities, |
| 242 | const std::string& hostname, |
| 243 | absl::string_view client_hello, |
| 244 | const std::string& alpn, |
| 245 | absl::optional<std::string> alps, |
| 246 | const std::vector<uint8_t>& quic_transport_params, |
| 247 | const absl::optional<std::vector<uint8_t>>& early_data_context, |
| 248 | const QuicSSLConfig& ssl_config) override; |
| 249 | |
| 250 | // Delegates to proof_source_->ComputeTlsSignature. |
| 251 | // Returns QUIC_SUCCESS, QUIC_FAILURE or QUIC_PENDING. |
| 252 | QuicAsyncStatus ComputeSignature(const QuicSocketAddress& server_address, |
| 253 | const QuicSocketAddress& client_address, |
| 254 | const std::string& hostname, |
| 255 | uint16_t signature_algorithm, |
| 256 | absl::string_view in, |
| 257 | size_t max_signature_size) override; |
| 258 | |
| 259 | protected: |
| 260 | ProofSourceHandleCallback* callback() override { return handshaker_; } |
| 261 | |
| 262 | private: |
| 263 | class QUIC_EXPORT_PRIVATE DefaultSignatureCallback |
| 264 | : public ProofSource::SignatureCallback { |
| 265 | public: |
| 266 | explicit DefaultSignatureCallback(DefaultProofSourceHandle* handle) |
| 267 | : handle_(handle) {} |
| 268 | |
| 269 | void Run(bool ok, |
| 270 | std::string signature, |
| 271 | std::unique_ptr<ProofSource::Details> details) override { |
| 272 | if (handle_ == nullptr) { |
| 273 | // Operation has been canceled, or Run has been called. |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | DefaultProofSourceHandle* handle = handle_; |
| 278 | handle_ = nullptr; |
| 279 | |
| 280 | handle->signature_callback_ = nullptr; |
| 281 | if (handle->handshaker_ != nullptr) { |
| 282 | handle->handshaker_->OnComputeSignatureDone( |
| 283 | ok, is_sync_, std::move(signature), std::move(details)); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | // If called, Cancel causes the pending callback to be a no-op. |
| 288 | void Cancel() { handle_ = nullptr; } |
| 289 | |
| 290 | void set_is_sync(bool is_sync) { is_sync_ = is_sync; } |
| 291 | |
| 292 | private: |
| 293 | DefaultProofSourceHandle* handle_; |
| 294 | // Set to false if handle_->ComputeSignature returns QUIC_PENDING. |
| 295 | bool is_sync_ = true; |
| 296 | }; |
| 297 | |
| 298 | // Not nullptr on construction. Set to nullptr when cancelled. |
| 299 | TlsServerHandshaker* handshaker_; // Not owned. |
| 300 | ProofSource* proof_source_; // Not owned. |
| 301 | DefaultSignatureCallback* signature_callback_ = nullptr; |
| 302 | }; |
| 303 | |
| 304 | struct QUIC_NO_EXPORT SetTransportParametersResult { |
| 305 | bool success = false; |
| 306 | // Empty vector if QUIC transport params are not set successfully. |
| 307 | std::vector<uint8_t> quic_transport_params; |
| 308 | // absl::nullopt if there is no application state to begin with. |
| 309 | // Empty vector if application state is not set successfully. |
| 310 | absl::optional<std::vector<uint8_t>> early_data_context; |
| 311 | }; |
| 312 | |
| 313 | SetTransportParametersResult SetTransportParameters(); |
| 314 | bool ProcessTransportParameters(const SSL_CLIENT_HELLO* client_hello, |
| 315 | std::string* error_details); |
| 316 | |
| 317 | struct QUIC_NO_EXPORT SetApplicationSettingsResult { |
| 318 | bool success = false; |
| 319 | std::unique_ptr<char[]> alps_buffer; |
| 320 | size_t alps_length = 0; |
| 321 | }; |
| 322 | SetApplicationSettingsResult SetApplicationSettings(absl::string_view alpn); |
| 323 | |
| 324 | QuicConnectionStats& connection_stats() { |
| 325 | return session()->connection()->mutable_stats(); |
| 326 | } |
| 327 | QuicTime now() const { return session()->GetClock()->Now(); } |
| 328 | |
| 329 | QuicConnectionContext* connection_context() { |
| 330 | return session()->connection()->context(); |
| 331 | } |
| 332 | |
| 333 | std::unique_ptr<ProofSourceHandle> proof_source_handle_; |
| 334 | ProofSource* proof_source_; |
| 335 | |
| 336 | // State to handle potentially asynchronous session ticket decryption. |
| 337 | // |ticket_decryption_callback_| points to the non-owned callback that was |
| 338 | // passed to ProofSource::TicketCrypter::Decrypt but hasn't finished running |
| 339 | // yet. |
| 340 | DecryptCallback* ticket_decryption_callback_ = nullptr; |
| 341 | // |decrypted_session_ticket_| contains the decrypted session ticket after the |
| 342 | // callback has run but before it is passed to BoringSSL. |
| 343 | std::vector<uint8_t> decrypted_session_ticket_; |
| 344 | // |ticket_received_| tracks whether we received a resumption ticket from the |
| 345 | // client. It does not matter whether we were able to decrypt said ticket or |
| 346 | // if we actually resumed a session with it - the presence of this ticket |
| 347 | // indicates that the client attempted a resumption. |
| 348 | bool ticket_received_ = false; |
| 349 | |
| 350 | // True if the "early_data" extension is in the client hello. |
| 351 | bool early_data_attempted_ = false; |
| 352 | |
| 353 | // Force SessionTicketOpen to return ssl_ticket_aead_ignore_ticket if called. |
| 354 | bool ignore_ticket_open_ = false; |
| 355 | |
| 356 | // nullopt means select cert hasn't started. |
| 357 | absl::optional<QuicAsyncStatus> select_cert_status_; |
| 358 | |
| 359 | std::string cert_verify_sig_; |
| 360 | std::unique_ptr<ProofSource::Details> proof_source_details_; |
| 361 | |
| 362 | // Count the duration of the current async operation, if any. |
| 363 | absl::optional<QuicTimeAccumulator> async_op_timer_; |
| 364 | |
| 365 | std::unique_ptr<ApplicationState> application_state_; |
| 366 | |
| 367 | // Pre-shared key used during the handshake. |
| 368 | std::string pre_shared_key_; |
| 369 | |
| 370 | // (optional) Key to use for encrypting TLS resumption tickets. |
| 371 | std::string ticket_encryption_key_; |
| 372 | |
| 373 | HandshakeState state_ = HANDSHAKE_START; |
| 374 | bool encryption_established_ = false; |
| 375 | bool valid_alpn_received_ = false; |
| 376 | bool can_disable_resumption_ = true; |
| 377 | quiche::QuicheReferenceCountedPointer<QuicCryptoNegotiatedParameters> |
| 378 | crypto_negotiated_params_; |
| 379 | TlsServerConnection tls_connection_; |
| 380 | const QuicCryptoServerConfig* crypto_config_; // Unowned. |
| 381 | // The last received CachedNetworkParameters from a validated address token. |
| 382 | mutable std::unique_ptr<CachedNetworkParameters> |
| 383 | last_received_cached_network_params_; |
| 384 | |
| 385 | bool cert_matched_sni_ = false; |
Bence Béky | bac0405 | 2022-04-07 15:44:29 -0400 | [diff] [blame] | 386 | }; |
| 387 | |
| 388 | } // namespace quic |
| 389 | |
| 390 | #endif // QUICHE_QUIC_CORE_TLS_SERVER_HANDSHAKER_H_ |