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_server_stream.h"
7 #include "base/base64.h"
8 #include "crypto/secure_hash.h"
9 #include "net/quic/crypto/crypto_protocol.h"
10 #include "net/quic/crypto/crypto_utils.h"
11 #include "net/quic/crypto/quic_crypto_server_config.h"
12 #include "net/quic/quic_config.h"
13 #include "net/quic/quic_protocol.h"
14 #include "net/quic/quic_session.h"
18 QuicCryptoServerStream::QuicCryptoServerStream(
19 const QuicCryptoServerConfig& crypto_config,
21 : QuicCryptoStream(session),
22 crypto_config_(crypto_config),
23 validate_client_hello_cb_(NULL),
24 num_handshake_messages_(0) {
27 QuicCryptoServerStream::~QuicCryptoServerStream() {
28 CancelOutstandingCallbacks();
31 void QuicCryptoServerStream::CancelOutstandingCallbacks() {
32 // Detach from the validation callback. Calling this multiple times is safe.
33 if (validate_client_hello_cb_ != NULL) {
34 validate_client_hello_cb_->Cancel();
38 void QuicCryptoServerStream::OnHandshakeMessage(
39 const CryptoHandshakeMessage& message) {
40 QuicCryptoStream::OnHandshakeMessage(message);
41 ++num_handshake_messages_;
43 // Do not process handshake messages after the handshake is confirmed.
44 if (handshake_confirmed_) {
45 CloseConnection(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE);
49 if (message.tag() != kCHLO) {
50 CloseConnection(QUIC_INVALID_CRYPTO_MESSAGE_TYPE);
54 if (validate_client_hello_cb_ != NULL) {
55 // Already processing some other handshake message. The protocol
56 // does not allow for clients to send multiple handshake messages
57 // before the server has a chance to respond.
58 CloseConnection(QUIC_CRYPTO_MESSAGE_WHILE_VALIDATING_CLIENT_HELLO);
62 validate_client_hello_cb_ = new ValidateCallback(this);
63 return crypto_config_.ValidateClientHello(
65 session()->connection()->peer_address(),
66 session()->connection()->clock(),
67 validate_client_hello_cb_);
70 void QuicCryptoServerStream::FinishProcessingHandshakeMessage(
71 const CryptoHandshakeMessage& message,
72 const ValidateClientHelloResultCallback::Result& result) {
73 // Clear the callback that got us here.
74 DCHECK(validate_client_hello_cb_ != NULL);
75 validate_client_hello_cb_ = NULL;
78 CryptoHandshakeMessage reply;
79 QuicErrorCode error = ProcessClientHello(
80 message, result, &reply, &error_details);
82 if (error != QUIC_NO_ERROR) {
83 CloseConnectionWithDetails(error, error_details);
87 if (reply.tag() != kSHLO) {
88 SendHandshakeMessage(reply);
92 // If we are returning a SHLO then we accepted the handshake.
93 QuicConfig* config = session()->config();
94 error = config->ProcessPeerHello(message, CLIENT, &error_details);
95 if (error != QUIC_NO_ERROR) {
96 CloseConnectionWithDetails(error, error_details);
99 session()->OnConfigNegotiated();
101 config->ToHandshakeMessage(&reply);
103 // Receiving a full CHLO implies the client is prepared to decrypt with
104 // the new server write key. We can start to encrypt with the new server
107 // NOTE: the SHLO will be encrypted with the new server write key.
108 session()->connection()->SetEncrypter(
110 crypto_negotiated_params_.initial_crypters.encrypter.release());
111 session()->connection()->SetDefaultEncryptionLevel(
113 // Set the decrypter immediately so that we no longer accept unencrypted
115 session()->connection()->SetDecrypter(
116 crypto_negotiated_params_.initial_crypters.decrypter.release(),
118 SendHandshakeMessage(reply);
120 session()->connection()->SetEncrypter(
121 ENCRYPTION_FORWARD_SECURE,
122 crypto_negotiated_params_.forward_secure_crypters.encrypter.release());
123 session()->connection()->SetDefaultEncryptionLevel(
124 ENCRYPTION_FORWARD_SECURE);
125 session()->connection()->SetAlternativeDecrypter(
126 crypto_negotiated_params_.forward_secure_crypters.decrypter.release(),
127 ENCRYPTION_FORWARD_SECURE, false /* don't latch */);
129 encryption_established_ = true;
130 handshake_confirmed_ = true;
131 session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
134 bool QuicCryptoServerStream::GetBase64SHA256ClientChannelID(
135 string* output) const {
136 if (!encryption_established_ ||
137 crypto_negotiated_params_.channel_id.empty()) {
141 const string& channel_id(crypto_negotiated_params_.channel_id);
142 scoped_ptr<crypto::SecureHash> hash(
143 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
144 hash->Update(channel_id.data(), channel_id.size());
146 hash->Finish(digest, sizeof(digest));
148 base::Base64Encode(string(
149 reinterpret_cast<const char*>(digest), sizeof(digest)), output);
151 size_t len = output->size();
153 if ((*output)[len - 1] == '=') {
155 if ((*output)[len - 1] == '=') {
164 QuicErrorCode QuicCryptoServerStream::ProcessClientHello(
165 const CryptoHandshakeMessage& message,
166 const ValidateClientHelloResultCallback::Result& result,
167 CryptoHandshakeMessage* reply,
168 string* error_details) {
169 return crypto_config_.ProcessClientHello(
171 session()->connection()->connection_id(),
172 session()->connection()->peer_address(),
173 session()->connection()->version(),
174 session()->connection()->supported_versions(),
175 session()->connection()->max_flow_control_receive_window_bytes(),
176 session()->connection()->clock(),
177 session()->connection()->random_generator(),
178 &crypto_negotiated_params_, reply, error_details);
181 QuicCryptoServerStream::ValidateCallback::ValidateCallback(
182 QuicCryptoServerStream* parent) : parent_(parent) {
185 void QuicCryptoServerStream::ValidateCallback::Cancel() {
189 void QuicCryptoServerStream::ValidateCallback::RunImpl(
190 const CryptoHandshakeMessage& client_hello,
191 const Result& result) {
192 if (parent_ != NULL) {
193 parent_->FinishProcessingHandshakeMessage(client_hello, result);