1 // Copyright (c) 2012 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.
5 #include "net/quic/quic_crypto_client_stream.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "base/profiler/scoped_tracker.h"
9 #include "net/quic/crypto/crypto_protocol.h"
10 #include "net/quic/crypto/crypto_utils.h"
11 #include "net/quic/crypto/null_encrypter.h"
12 #include "net/quic/quic_client_session_base.h"
13 #include "net/quic/quic_flags.h"
14 #include "net/quic/quic_protocol.h"
15 #include "net/quic/quic_session.h"
21 QuicCryptoClientStream::ChannelIDSourceCallbackImpl::
22 ChannelIDSourceCallbackImpl(QuicCryptoClientStream
* stream
)
25 QuicCryptoClientStream::ChannelIDSourceCallbackImpl::
26 ~ChannelIDSourceCallbackImpl() {}
28 void QuicCryptoClientStream::ChannelIDSourceCallbackImpl::Run(
29 scoped_ptr
<ChannelIDKey
>* channel_id_key
) {
30 if (stream_
== nullptr) {
34 stream_
->channel_id_key_
.reset(channel_id_key
->release());
35 stream_
->channel_id_source_callback_run_
= true;
36 stream_
->channel_id_source_callback_
= nullptr;
37 stream_
->DoHandshakeLoop(nullptr);
39 // The ChannelIDSource owns this object and will delete it when this method
43 void QuicCryptoClientStream::ChannelIDSourceCallbackImpl::Cancel() {
47 QuicCryptoClientStream::ProofVerifierCallbackImpl::ProofVerifierCallbackImpl(
48 QuicCryptoClientStream
* stream
)
51 QuicCryptoClientStream::ProofVerifierCallbackImpl::
52 ~ProofVerifierCallbackImpl() {}
54 void QuicCryptoClientStream::ProofVerifierCallbackImpl::Run(
56 const string
& error_details
,
57 scoped_ptr
<ProofVerifyDetails
>* details
) {
58 if (stream_
== nullptr) {
62 stream_
->verify_ok_
= ok
;
63 stream_
->verify_error_details_
= error_details
;
64 stream_
->verify_details_
.reset(details
->release());
65 stream_
->proof_verify_callback_
= nullptr;
66 stream_
->DoHandshakeLoop(nullptr);
68 // The ProofVerifier owns this object and will delete it when this method
72 void QuicCryptoClientStream::ProofVerifierCallbackImpl::Cancel() {
76 QuicCryptoClientStream::QuicCryptoClientStream(
77 const QuicServerId
& server_id
,
78 QuicClientSessionBase
* session
,
79 ProofVerifyContext
* verify_context
,
80 QuicCryptoClientConfig
* crypto_config
)
81 : QuicCryptoStream(session
),
82 next_state_(STATE_IDLE
),
83 num_client_hellos_(0),
84 crypto_config_(crypto_config
),
85 server_id_(server_id
),
86 generation_counter_(0),
87 channel_id_sent_(false),
88 channel_id_source_callback_run_(false),
89 channel_id_source_callback_(nullptr),
90 verify_context_(verify_context
),
91 proof_verify_callback_(nullptr),
92 stateless_reject_received_(false) {
93 DCHECK_EQ(Perspective::IS_CLIENT
, session
->connection()->perspective());
96 QuicCryptoClientStream::~QuicCryptoClientStream() {
97 if (channel_id_source_callback_
) {
98 channel_id_source_callback_
->Cancel();
100 if (proof_verify_callback_
) {
101 proof_verify_callback_
->Cancel();
105 void QuicCryptoClientStream::OnHandshakeMessage(
106 const CryptoHandshakeMessage
& message
) {
107 QuicCryptoStream::OnHandshakeMessage(message
);
109 if (message
.tag() == kSCUP
) {
110 if (!handshake_confirmed()) {
111 CloseConnection(QUIC_CRYPTO_UPDATE_BEFORE_HANDSHAKE_COMPLETE
);
115 // |message| is an update from the server, so we treat it differently from a
116 // handshake message.
117 HandleServerConfigUpdateMessage(message
);
121 // Do not process handshake messages after the handshake is confirmed.
122 if (handshake_confirmed()) {
123 CloseConnection(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE
);
127 DoHandshakeLoop(&message
);
130 void QuicCryptoClientStream::CryptoConnect() {
131 next_state_
= STATE_INITIALIZE
;
132 DoHandshakeLoop(nullptr);
135 int QuicCryptoClientStream::num_sent_client_hellos() const {
136 return num_client_hellos_
;
139 // Used in Chromium, but not in the server.
140 bool QuicCryptoClientStream::WasChannelIDSent() const {
141 return channel_id_sent_
;
144 bool QuicCryptoClientStream::WasChannelIDSourceCallbackRun() const {
145 return channel_id_source_callback_run_
;
148 void QuicCryptoClientStream::HandleServerConfigUpdateMessage(
149 const CryptoHandshakeMessage
& server_config_update
) {
150 DCHECK(server_config_update
.tag() == kSCUP
);
151 string error_details
;
152 QuicCryptoClientConfig::CachedState
* cached
=
153 crypto_config_
->LookupOrCreate(server_id_
);
154 QuicErrorCode error
= crypto_config_
->ProcessServerConfigUpdate(
155 server_config_update
,
156 session()->connection()->clock()->WallNow(),
158 &crypto_negotiated_params_
,
161 if (error
!= QUIC_NO_ERROR
) {
162 CloseConnectionWithDetails(
163 error
, "Server config update invalid: " + error_details
);
167 DCHECK(handshake_confirmed());
168 if (proof_verify_callback_
) {
169 proof_verify_callback_
->Cancel();
171 next_state_
= STATE_INITIALIZE_SCUP
;
172 DoHandshakeLoop(nullptr);
175 // kMaxClientHellos is the maximum number of times that we'll send a client
176 // hello. The value 3 accounts for:
177 // * One failure due to an incorrect or missing source-address token.
178 // * One failure due the server's certificate chain being unavailible and the
179 // server being unwilling to send it without a valid source-address token.
180 static const int kMaxClientHellos
= 3;
182 void QuicCryptoClientStream::DoHandshakeLoop(
183 const CryptoHandshakeMessage
* in
) {
184 QuicCryptoClientConfig::CachedState
* cached
=
185 crypto_config_
->LookupOrCreate(server_id_
);
187 QuicAsyncStatus rv
= QUIC_SUCCESS
;
189 CHECK_NE(STATE_NONE
, next_state_
);
190 const State state
= next_state_
;
191 next_state_
= STATE_IDLE
;
194 case STATE_INITIALIZE
:
195 DoInitialize(cached
);
197 case STATE_SEND_CHLO
:
198 DoSendCHLO(in
, cached
);
199 return; // return waiting to hear from server.
201 DoReceiveREJ(in
, cached
);
203 case STATE_VERIFY_PROOF
:
204 rv
= DoVerifyProof(cached
);
206 case STATE_VERIFY_PROOF_COMPLETE
:
207 DoVerifyProofComplete(cached
);
209 case STATE_GET_CHANNEL_ID
:
210 rv
= DoGetChannelID(cached
);
212 case STATE_GET_CHANNEL_ID_COMPLETE
:
213 DoGetChannelIDComplete();
215 case STATE_RECV_SHLO
:
216 DoReceiveSHLO(in
, cached
);
219 // This means that the peer sent us a message that we weren't expecting.
220 CloseConnection(QUIC_INVALID_CRYPTO_MESSAGE_TYPE
);
222 case STATE_INITIALIZE_SCUP
:
223 DoInitializeServerConfigUpdate(cached
);
227 return; // We are done.
229 } while (rv
!= QUIC_PENDING
&& next_state_
!= STATE_NONE
);
232 void QuicCryptoClientStream::DoInitialize(
233 QuicCryptoClientConfig::CachedState
* cached
) {
234 if (!cached
->IsEmpty() && !cached
->signature().empty() &&
235 server_id_
.is_https()) {
236 // Note that we verify the proof even if the cached proof is valid.
237 // This allows us to respond to CA trust changes or certificate
238 // expiration because it may have been a while since we last verified
240 DCHECK(crypto_config_
->proof_verifier());
241 // If the cached state needs to be verified, do it now.
242 next_state_
= STATE_VERIFY_PROOF
;
244 next_state_
= STATE_GET_CHANNEL_ID
;
248 void QuicCryptoClientStream::DoSendCHLO(
249 const CryptoHandshakeMessage
* in
,
250 QuicCryptoClientConfig::CachedState
* cached
) {
251 if (stateless_reject_received_
) {
252 // If we've gotten to this point, we've sent at least one hello
253 // and received a stateless reject in response. We cannot
254 // continue to send hellos because the server has abandoned state
255 // for this connection. Abandon further handshakes.
256 next_state_
= STATE_NONE
;
257 if (session()->connection()->connected()) {
258 session()->connection()->CloseConnection(
259 QUIC_CRYPTO_HANDSHAKE_STATELESS_REJECT
, false);
264 // Send the client hello in plaintext.
265 session()->connection()->SetDefaultEncryptionLevel(ENCRYPTION_NONE
);
266 if (num_client_hellos_
> kMaxClientHellos
) {
267 CloseConnection(QUIC_CRYPTO_TOO_MANY_REJECTS
);
270 num_client_hellos_
++;
272 CryptoHandshakeMessage out
;
273 DCHECK(session() != nullptr);
274 DCHECK(session()->config() != nullptr);
275 // Send all the options, regardless of whether we're sending an
276 // inchoate or subsequent hello.
277 session()->config()->ToHandshakeMessage(&out
);
278 if (!cached
->IsComplete(session()->connection()->clock()->WallNow())) {
279 crypto_config_
->FillInchoateClientHello(
281 session()->connection()->supported_versions().front(),
282 cached
, &crypto_negotiated_params_
, &out
);
283 // Pad the inchoate client hello to fill up a packet.
284 const QuicByteCount kFramingOverhead
= 50; // A rough estimate.
285 const QuicByteCount max_packet_size
=
286 session()->connection()->max_packet_length();
287 if (max_packet_size
<= kFramingOverhead
) {
288 DLOG(DFATAL
) << "max_packet_length (" << max_packet_size
289 << ") has no room for framing overhead.";
290 CloseConnection(QUIC_INTERNAL_ERROR
);
293 if (kClientHelloMinimumSize
> max_packet_size
- kFramingOverhead
) {
294 DLOG(DFATAL
) << "Client hello won't fit in a single packet.";
295 CloseConnection(QUIC_INTERNAL_ERROR
);
298 out
.set_minimum_size(
299 static_cast<size_t>(max_packet_size
- kFramingOverhead
));
300 next_state_
= STATE_RECV_REJ
;
301 SendHandshakeMessage(out
);
305 // If the server nonce is empty, copy over the server nonce from a previous
306 // SREJ, if there is one.
307 if (FLAGS_enable_quic_stateless_reject_support
&&
308 crypto_negotiated_params_
.server_nonce
.empty() &&
309 cached
->has_server_nonce()) {
310 crypto_negotiated_params_
.server_nonce
= cached
->GetNextServerNonce();
311 DCHECK(!crypto_negotiated_params_
.server_nonce
.empty());
314 string error_details
;
315 QuicErrorCode error
= crypto_config_
->FillClientHello(
317 session()->connection()->connection_id(),
318 session()->connection()->supported_versions().front(),
320 session()->connection()->clock()->WallNow(),
321 session()->connection()->random_generator(),
322 channel_id_key_
.get(),
323 &crypto_negotiated_params_
,
327 if (error
!= QUIC_NO_ERROR
) {
328 // Flush the cached config so that, if it's bad, the server has a
329 // chance to send us another in the future.
330 cached
->InvalidateServerConfig();
331 CloseConnectionWithDetails(error
, error_details
);
334 channel_id_sent_
= (channel_id_key_
.get() != nullptr);
335 if (cached
->proof_verify_details()) {
336 client_session()->OnProofVerifyDetailsAvailable(
337 *cached
->proof_verify_details());
339 next_state_
= STATE_RECV_SHLO
;
340 SendHandshakeMessage(out
);
341 // Be prepared to decrypt with the new server write key.
342 session()->connection()->SetAlternativeDecrypter(
344 crypto_negotiated_params_
.initial_crypters
.decrypter
.release(),
345 true /* latch once used */);
346 // Send subsequent packets under encryption on the assumption that the
347 // server will accept the handshake.
348 session()->connection()->SetEncrypter(
350 crypto_negotiated_params_
.initial_crypters
.encrypter
.release());
351 session()->connection()->SetDefaultEncryptionLevel(
353 if (!encryption_established_
) {
354 encryption_established_
= true;
355 session()->OnCryptoHandshakeEvent(
356 QuicSession::ENCRYPTION_FIRST_ESTABLISHED
);
358 session()->OnCryptoHandshakeEvent(
359 QuicSession::ENCRYPTION_REESTABLISHED
);
363 void QuicCryptoClientStream::DoReceiveREJ(
364 const CryptoHandshakeMessage
* in
,
365 QuicCryptoClientConfig::CachedState
* cached
) {
366 // TODO(rtenneti): Remove ScopedTracker below once crbug.com/422516 is fixed.
367 tracked_objects::ScopedTracker
tracking_profile(
368 FROM_HERE_WITH_EXPLICIT_FUNCTION(
369 "422516 QuicCryptoClientStream::DoReceiveREJ"));
371 // We sent a dummy CHLO because we didn't have enough information to
372 // perform a handshake, or we sent a full hello that the server
373 // rejected. Here we hope to have a REJ that contains the information
375 if ((in
->tag() != kREJ
) && (in
->tag() != kSREJ
)) {
376 next_state_
= STATE_NONE
;
377 CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE
,
381 stateless_reject_received_
= in
->tag() == kSREJ
;
382 string error_details
;
383 QuicErrorCode error
= crypto_config_
->ProcessRejection(
384 *in
, session()->connection()->clock()->WallNow(), cached
,
385 server_id_
.is_https(), &crypto_negotiated_params_
, &error_details
);
387 if (error
!= QUIC_NO_ERROR
) {
388 next_state_
= STATE_NONE
;
389 CloseConnectionWithDetails(error
, error_details
);
392 if (!cached
->proof_valid()) {
393 if (!server_id_
.is_https()) {
394 // We don't check the certificates for insecure QUIC connections.
395 SetCachedProofValid(cached
);
396 } else if (!cached
->signature().empty()) {
397 // Note that we only verify the proof if the cached proof is not
398 // valid. If the cached proof is valid here, someone else must have
399 // just added the server config to the cache and verified the proof,
400 // so we can assume no CA trust changes or certificate expiration
401 // has happened since then.
402 next_state_
= STATE_VERIFY_PROOF
;
406 next_state_
= STATE_GET_CHANNEL_ID
;
409 QuicAsyncStatus
QuicCryptoClientStream::DoVerifyProof(
410 QuicCryptoClientConfig::CachedState
* cached
) {
411 ProofVerifier
* verifier
= crypto_config_
->proof_verifier();
413 next_state_
= STATE_VERIFY_PROOF_COMPLETE
;
414 generation_counter_
= cached
->generation_counter();
416 ProofVerifierCallbackImpl
* proof_verify_callback
=
417 new ProofVerifierCallbackImpl(this);
421 QuicAsyncStatus status
= verifier
->VerifyProof(
422 server_id_
.host(), cached
->server_config(), cached
->certs(),
423 cached
->signature(), verify_context_
.get(), &verify_error_details_
,
424 &verify_details_
, proof_verify_callback
);
428 proof_verify_callback_
= proof_verify_callback
;
429 DVLOG(1) << "Doing VerifyProof";
432 delete proof_verify_callback
;
435 delete proof_verify_callback
;
442 void QuicCryptoClientStream::DoVerifyProofComplete(
443 QuicCryptoClientConfig::CachedState
* cached
) {
445 next_state_
= STATE_NONE
;
446 if (verify_details_
.get()) {
447 client_session()->OnProofVerifyDetailsAvailable(*verify_details_
);
449 UMA_HISTOGRAM_BOOLEAN("Net.QuicVerifyProofFailed.HandshakeConfirmed",
450 handshake_confirmed());
451 CloseConnectionWithDetails(
452 QUIC_PROOF_INVALID
, "Proof invalid: " + verify_error_details_
);
456 // Check if generation_counter has changed between STATE_VERIFY_PROOF and
457 // STATE_VERIFY_PROOF_COMPLETE state changes.
458 if (generation_counter_
!= cached
->generation_counter()) {
459 next_state_
= STATE_VERIFY_PROOF
;
461 SetCachedProofValid(cached
);
462 cached
->SetProofVerifyDetails(verify_details_
.release());
463 if (!handshake_confirmed()) {
464 next_state_
= STATE_GET_CHANNEL_ID
;
466 next_state_
= STATE_NONE
;
471 QuicAsyncStatus
QuicCryptoClientStream::DoGetChannelID(
472 QuicCryptoClientConfig::CachedState
* cached
) {
473 next_state_
= STATE_GET_CHANNEL_ID_COMPLETE
;
474 channel_id_key_
.reset();
475 if (!RequiresChannelID(cached
)) {
476 next_state_
= STATE_SEND_CHLO
;
480 ChannelIDSourceCallbackImpl
* channel_id_source_callback
=
481 new ChannelIDSourceCallbackImpl(this);
482 QuicAsyncStatus status
=
483 crypto_config_
->channel_id_source()->GetChannelIDKey(
484 server_id_
.host(), &channel_id_key_
,
485 channel_id_source_callback
);
489 channel_id_source_callback_
= channel_id_source_callback
;
490 DVLOG(1) << "Looking up channel ID";
493 next_state_
= STATE_NONE
;
494 delete channel_id_source_callback
;
495 CloseConnectionWithDetails(QUIC_INVALID_CHANNEL_ID_SIGNATURE
,
496 "Channel ID lookup failed");
499 delete channel_id_source_callback
;
505 void QuicCryptoClientStream::DoGetChannelIDComplete() {
506 if (!channel_id_key_
.get()) {
507 next_state_
= STATE_NONE
;
508 CloseConnectionWithDetails(QUIC_INVALID_CHANNEL_ID_SIGNATURE
,
509 "Channel ID lookup failed");
512 next_state_
= STATE_SEND_CHLO
;
515 void QuicCryptoClientStream::DoReceiveSHLO(
516 const CryptoHandshakeMessage
* in
,
517 QuicCryptoClientConfig::CachedState
* cached
) {
518 next_state_
= STATE_NONE
;
519 // We sent a CHLO that we expected to be accepted and now we're
520 // hoping for a SHLO from the server to confirm that. First check
521 // to see whether the response was a reject, and if so, move on to
522 // the reject-processing state.
523 if ((in
->tag() == kREJ
) || (in
->tag() == kSREJ
)) {
524 // alternative_decrypter will be nullptr if the original alternative
525 // decrypter latched and became the primary decrypter. That happens
526 // if we received a message encrypted with the INITIAL key.
527 if (session()->connection()->alternative_decrypter() == nullptr) {
528 // The rejection was sent encrypted!
529 CloseConnectionWithDetails(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
,
530 "encrypted REJ message");
533 next_state_
= STATE_RECV_REJ
;
537 if (in
->tag() != kSHLO
) {
538 CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE
,
539 "Expected SHLO or REJ");
543 // alternative_decrypter will be nullptr if the original alternative
544 // decrypter latched and became the primary decrypter. That happens
545 // if we received a message encrypted with the INITIAL key.
546 if (session()->connection()->alternative_decrypter() != nullptr) {
547 // The server hello was sent without encryption.
548 CloseConnectionWithDetails(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT
,
549 "unencrypted SHLO message");
553 string error_details
;
554 QuicErrorCode error
= crypto_config_
->ProcessServerHello(
555 *in
, session()->connection()->connection_id(),
556 session()->connection()->server_supported_versions(),
557 cached
, &crypto_negotiated_params_
, &error_details
);
559 if (error
!= QUIC_NO_ERROR
) {
560 CloseConnectionWithDetails(error
, "Server hello invalid: " + error_details
);
563 error
= session()->config()->ProcessPeerHello(*in
, SERVER
, &error_details
);
564 if (error
!= QUIC_NO_ERROR
) {
565 CloseConnectionWithDetails(error
, "Server hello invalid: " + error_details
);
568 session()->OnConfigNegotiated();
570 CrypterPair
* crypters
= &crypto_negotiated_params_
.forward_secure_crypters
;
571 // TODO(agl): we don't currently latch this decrypter because the idea
572 // has been floated that the server shouldn't send packets encrypted
573 // with the FORWARD_SECURE key until it receives a FORWARD_SECURE
574 // packet from the client.
575 session()->connection()->SetAlternativeDecrypter(
576 ENCRYPTION_FORWARD_SECURE
, crypters
->decrypter
.release(),
577 false /* don't latch */);
578 session()->connection()->SetEncrypter(
579 ENCRYPTION_FORWARD_SECURE
, crypters
->encrypter
.release());
580 session()->connection()->SetDefaultEncryptionLevel(
581 ENCRYPTION_FORWARD_SECURE
);
583 handshake_confirmed_
= true;
584 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED
);
585 session()->connection()->OnHandshakeComplete();
588 void QuicCryptoClientStream::DoInitializeServerConfigUpdate(
589 QuicCryptoClientConfig::CachedState
* cached
) {
590 bool update_ignored
= false;
591 if (!server_id_
.is_https()) {
592 // We don't check the certificates for insecure QUIC connections.
593 SetCachedProofValid(cached
);
594 next_state_
= STATE_NONE
;
595 } else if (!cached
->IsEmpty() && !cached
->signature().empty()) {
596 // Note that we verify the proof even if the cached proof is valid.
597 DCHECK(crypto_config_
->proof_verifier());
598 next_state_
= STATE_VERIFY_PROOF
;
600 update_ignored
= true;
601 next_state_
= STATE_NONE
;
603 UMA_HISTOGRAM_COUNTS("Net.QuicNumServerConfig.UpdateMessagesIgnored",
607 void QuicCryptoClientStream::SetCachedProofValid(
608 QuicCryptoClientConfig::CachedState
* cached
) {
609 cached
->SetProofValid();
610 client_session()->OnProofValid(*cached
);
613 bool QuicCryptoClientStream::RequiresChannelID(
614 QuicCryptoClientConfig::CachedState
* cached
) {
615 if (!server_id_
.is_https() ||
616 server_id_
.privacy_mode() == PRIVACY_MODE_ENABLED
||
617 !crypto_config_
->channel_id_source()) {
620 const CryptoHandshakeMessage
* scfg
= cached
->GetServerConfig();
621 if (!scfg
) { // scfg may be null then we send an inchoate CHLO.
624 const QuicTag
* their_proof_demands
;
625 size_t num_their_proof_demands
;
626 if (scfg
->GetTaglist(kPDMD
, &their_proof_demands
,
627 &num_their_proof_demands
) != QUIC_NO_ERROR
) {
630 for (size_t i
= 0; i
< num_their_proof_demands
; i
++) {
631 if (their_proof_demands
[i
] == kCHID
) {
638 QuicClientSessionBase
* QuicCryptoClientStream::client_session() {
639 return reinterpret_cast<QuicClientSessionBase
*>(session());