This source file includes following definitions.
- ProofVerifierCallbackImpl
- Run
- Cancel
- verify_context_
- OnHandshakeMessage
- CryptoConnect
- num_sent_client_hellos
- DoHandshakeLoop
- SetCachedProofValid
- client_session
#include "net/quic/quic_crypto_client_stream.h"
#include "net/quic/crypto/crypto_protocol.h"
#include "net/quic/crypto/crypto_utils.h"
#include "net/quic/crypto/null_encrypter.h"
#include "net/quic/crypto/proof_verifier.h"
#include "net/quic/quic_client_session_base.h"
#include "net/quic/quic_protocol.h"
#include "net/quic/quic_session.h"
namespace net {
QuicCryptoClientStream::ProofVerifierCallbackImpl::ProofVerifierCallbackImpl(
QuicCryptoClientStream* stream)
: stream_(stream) {}
QuicCryptoClientStream::ProofVerifierCallbackImpl::
~ProofVerifierCallbackImpl() {}
void QuicCryptoClientStream::ProofVerifierCallbackImpl::Run(
bool ok,
const string& error_details,
scoped_ptr<ProofVerifyDetails>* details) {
if (stream_ == NULL) {
return;
}
stream_->verify_ok_ = ok;
stream_->verify_error_details_ = error_details;
stream_->verify_details_.reset(details->release());
stream_->proof_verify_callback_ = NULL;
stream_->DoHandshakeLoop(NULL);
}
void QuicCryptoClientStream::ProofVerifierCallbackImpl::Cancel() {
stream_ = NULL;
}
QuicCryptoClientStream::QuicCryptoClientStream(
const QuicServerId& server_id,
QuicClientSessionBase* session,
ProofVerifyContext* verify_context,
QuicCryptoClientConfig* crypto_config)
: QuicCryptoStream(session),
next_state_(STATE_IDLE),
num_client_hellos_(0),
crypto_config_(crypto_config),
server_id_(server_id),
generation_counter_(0),
proof_verify_callback_(NULL),
verify_context_(verify_context) {
}
QuicCryptoClientStream::~QuicCryptoClientStream() {
if (proof_verify_callback_) {
proof_verify_callback_->Cancel();
}
}
void QuicCryptoClientStream::OnHandshakeMessage(
const CryptoHandshakeMessage& message) {
QuicCryptoStream::OnHandshakeMessage(message);
DoHandshakeLoop(&message);
}
bool QuicCryptoClientStream::CryptoConnect() {
next_state_ = STATE_INITIALIZE;
DoHandshakeLoop(NULL);
return true;
}
int QuicCryptoClientStream::num_sent_client_hellos() const {
return num_client_hellos_;
}
static const int kMaxClientHellos = 3;
void QuicCryptoClientStream::DoHandshakeLoop(
const CryptoHandshakeMessage* in) {
CryptoHandshakeMessage out;
QuicErrorCode error;
string error_details;
QuicCryptoClientConfig::CachedState* cached =
crypto_config_->LookupOrCreate(server_id_);
if (in != NULL) {
DVLOG(1) << "Client: Received " << in->DebugString();
}
for (;;) {
const State state = next_state_;
next_state_ = STATE_IDLE;
switch (state) {
case STATE_INITIALIZE: {
if (!cached->IsEmpty() && !cached->signature().empty() &&
server_id_.is_https()) {
DCHECK(crypto_config_->proof_verifier());
next_state_ = STATE_VERIFY_PROOF;
} else {
next_state_ = STATE_SEND_CHLO;
}
break;
}
case STATE_SEND_CHLO: {
session()->connection()->SetDefaultEncryptionLevel(ENCRYPTION_NONE);
if (num_client_hellos_ > kMaxClientHellos) {
CloseConnection(QUIC_CRYPTO_TOO_MANY_REJECTS);
return;
}
num_client_hellos_++;
if (!cached->IsComplete(session()->connection()->clock()->WallNow())) {
crypto_config_->FillInchoateClientHello(
server_id_,
session()->connection()->supported_versions().front(),
cached, &crypto_negotiated_params_, &out);
const size_t kFramingOverhead = 50;
const size_t max_packet_size =
session()->connection()->options()->max_packet_length;
if (max_packet_size <= kFramingOverhead) {
DLOG(DFATAL) << "max_packet_length (" << max_packet_size
<< ") has no room for framing overhead.";
CloseConnection(QUIC_INTERNAL_ERROR);
return;
}
if (kClientHelloMinimumSize > max_packet_size - kFramingOverhead) {
DLOG(DFATAL) << "Client hello won't fit in a single packet.";
CloseConnection(QUIC_INTERNAL_ERROR);
return;
}
out.set_minimum_size(max_packet_size - kFramingOverhead);
next_state_ = STATE_RECV_REJ;
DVLOG(1) << "Client: Sending " << out.DebugString();
SendHandshakeMessage(out);
return;
}
session()->config()->ToHandshakeMessage(&out);
error = crypto_config_->FillClientHello(
server_id_,
session()->connection()->connection_id(),
session()->connection()->supported_versions().front(),
session()->connection()->max_flow_control_receive_window_bytes(),
cached,
session()->connection()->clock()->WallNow(),
session()->connection()->random_generator(),
&crypto_negotiated_params_,
&out,
&error_details);
if (error != QUIC_NO_ERROR) {
cached->InvalidateServerConfig();
CloseConnectionWithDetails(error, error_details);
return;
}
if (cached->proof_verify_details()) {
client_session()->OnProofVerifyDetailsAvailable(
*cached->proof_verify_details());
}
next_state_ = STATE_RECV_SHLO;
DVLOG(1) << "Client: Sending " << out.DebugString();
SendHandshakeMessage(out);
session()->connection()->SetAlternativeDecrypter(
crypto_negotiated_params_.initial_crypters.decrypter.release(),
true );
session()->connection()->SetEncrypter(
ENCRYPTION_INITIAL,
crypto_negotiated_params_.initial_crypters.encrypter.release());
session()->connection()->SetDefaultEncryptionLevel(
ENCRYPTION_INITIAL);
if (!encryption_established_) {
encryption_established_ = true;
session()->OnCryptoHandshakeEvent(
QuicSession::ENCRYPTION_FIRST_ESTABLISHED);
} else {
session()->OnCryptoHandshakeEvent(
QuicSession::ENCRYPTION_REESTABLISHED);
}
return;
}
case STATE_RECV_REJ:
if (in->tag() != kREJ) {
CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
"Expected REJ");
return;
}
error = crypto_config_->ProcessRejection(
*in, session()->connection()->clock()->WallNow(), cached,
&crypto_negotiated_params_, &error_details);
if (error != QUIC_NO_ERROR) {
CloseConnectionWithDetails(error, error_details);
return;
}
if (!cached->proof_valid()) {
if (!server_id_.is_https()) {
SetCachedProofValid(cached);
} else if (!cached->signature().empty()) {
next_state_ = STATE_VERIFY_PROOF;
break;
}
}
next_state_ = STATE_SEND_CHLO;
break;
case STATE_VERIFY_PROOF: {
ProofVerifier* verifier = crypto_config_->proof_verifier();
DCHECK(verifier);
next_state_ = STATE_VERIFY_PROOF_COMPLETE;
generation_counter_ = cached->generation_counter();
ProofVerifierCallbackImpl* proof_verify_callback =
new ProofVerifierCallbackImpl(this);
verify_ok_ = false;
ProofVerifier::Status status = verifier->VerifyProof(
server_id_.host(),
cached->server_config(),
cached->certs(),
cached->signature(),
verify_context_.get(),
&verify_error_details_,
&verify_details_,
proof_verify_callback);
switch (status) {
case ProofVerifier::PENDING:
proof_verify_callback_ = proof_verify_callback;
DVLOG(1) << "Doing VerifyProof";
return;
case ProofVerifier::FAILURE:
delete proof_verify_callback;
break;
case ProofVerifier::SUCCESS:
delete proof_verify_callback;
verify_ok_ = true;
break;
}
break;
}
case STATE_VERIFY_PROOF_COMPLETE:
if (!verify_ok_) {
client_session()->OnProofVerifyDetailsAvailable(*verify_details_);
CloseConnectionWithDetails(
QUIC_PROOF_INVALID, "Proof invalid: " + verify_error_details_);
return;
}
if (generation_counter_ != cached->generation_counter()) {
next_state_ = STATE_VERIFY_PROOF;
} else {
SetCachedProofValid(cached);
cached->SetProofVerifyDetails(verify_details_.release());
next_state_ = STATE_SEND_CHLO;
}
break;
case STATE_RECV_SHLO: {
if (in->tag() == kREJ) {
if (session()->connection()->alternative_decrypter() == NULL) {
CloseConnectionWithDetails(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT,
"encrypted REJ message");
return;
}
next_state_ = STATE_RECV_REJ;
break;
}
if (in->tag() != kSHLO) {
CloseConnectionWithDetails(QUIC_INVALID_CRYPTO_MESSAGE_TYPE,
"Expected SHLO or REJ");
return;
}
if (session()->connection()->alternative_decrypter() != NULL) {
CloseConnectionWithDetails(QUIC_CRYPTO_ENCRYPTION_LEVEL_INCORRECT,
"unencrypted SHLO message");
return;
}
error = crypto_config_->ProcessServerHello(
*in, session()->connection()->connection_id(),
session()->connection()->server_supported_versions(),
cached, &crypto_negotiated_params_, &error_details);
if (error != QUIC_NO_ERROR) {
CloseConnectionWithDetails(
error, "Server hello invalid: " + error_details);
return;
}
error = session()->config()->ProcessServerHello(*in, &error_details);
if (error != QUIC_NO_ERROR) {
CloseConnectionWithDetails(
error, "Server hello invalid: " + error_details);
return;
}
session()->OnConfigNegotiated();
CrypterPair* crypters =
&crypto_negotiated_params_.forward_secure_crypters;
session()->connection()->SetAlternativeDecrypter(
crypters->decrypter.release(), false );
session()->connection()->SetEncrypter(
ENCRYPTION_FORWARD_SECURE, crypters->encrypter.release());
session()->connection()->SetDefaultEncryptionLevel(
ENCRYPTION_FORWARD_SECURE);
handshake_confirmed_ = true;
session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
return;
}
case STATE_IDLE:
CloseConnection(QUIC_INVALID_CRYPTO_MESSAGE_TYPE);
return;
}
}
}
void QuicCryptoClientStream::SetCachedProofValid(
QuicCryptoClientConfig::CachedState* cached) {
cached->SetProofValid();
client_session()->OnProofValid(*cached);
}
QuicClientSessionBase* QuicCryptoClientStream::client_session() {
return reinterpret_cast<QuicClientSessionBase*>(session());
}
}