Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_crypto_server_stream.cc
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.
4
5 #include "net/quic/quic_crypto_server_stream.h"
6
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"
15
16 namespace net {
17
18 QuicCryptoServerStream::QuicCryptoServerStream(
19     const QuicCryptoServerConfig& crypto_config,
20     QuicSession* session)
21     : QuicCryptoStream(session),
22       crypto_config_(crypto_config),
23       validate_client_hello_cb_(NULL),
24       num_handshake_messages_(0) {
25 }
26
27 QuicCryptoServerStream::~QuicCryptoServerStream() {
28   CancelOutstandingCallbacks();
29 }
30
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();
35   }
36 }
37
38 void QuicCryptoServerStream::OnHandshakeMessage(
39     const CryptoHandshakeMessage& message) {
40   QuicCryptoStream::OnHandshakeMessage(message);
41   ++num_handshake_messages_;
42
43   // Do not process handshake messages after the handshake is confirmed.
44   if (handshake_confirmed_) {
45     CloseConnection(QUIC_CRYPTO_MESSAGE_AFTER_HANDSHAKE_COMPLETE);
46     return;
47   }
48
49   if (message.tag() != kCHLO) {
50     CloseConnection(QUIC_INVALID_CRYPTO_MESSAGE_TYPE);
51     return;
52   }
53
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);
59     return;
60   }
61
62   validate_client_hello_cb_ = new ValidateCallback(this);
63   return crypto_config_.ValidateClientHello(
64       message,
65       session()->connection()->peer_address(),
66       session()->connection()->clock(),
67       validate_client_hello_cb_);
68 }
69
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;
76
77   string error_details;
78   CryptoHandshakeMessage reply;
79   QuicErrorCode error = ProcessClientHello(
80       message, result, &reply, &error_details);
81
82   if (error != QUIC_NO_ERROR) {
83     CloseConnectionWithDetails(error, error_details);
84     return;
85   }
86
87   if (reply.tag() != kSHLO) {
88     SendHandshakeMessage(reply);
89     return;
90   }
91
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);
97     return;
98   }
99   session()->OnConfigNegotiated();
100
101   config->ToHandshakeMessage(&reply);
102
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
105   // write key.
106   //
107   // NOTE: the SHLO will be encrypted with the new server write key.
108   session()->connection()->SetEncrypter(
109       ENCRYPTION_INITIAL,
110       crypto_negotiated_params_.initial_crypters.encrypter.release());
111   session()->connection()->SetDefaultEncryptionLevel(
112       ENCRYPTION_INITIAL);
113   // Set the decrypter immediately so that we no longer accept unencrypted
114   // packets.
115   session()->connection()->SetDecrypter(
116       crypto_negotiated_params_.initial_crypters.decrypter.release(),
117       ENCRYPTION_INITIAL);
118   SendHandshakeMessage(reply);
119
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 */);
128
129   encryption_established_ = true;
130   handshake_confirmed_ = true;
131   session()->OnCryptoHandshakeEvent(QuicSession::HANDSHAKE_CONFIRMED);
132 }
133
134 bool QuicCryptoServerStream::GetBase64SHA256ClientChannelID(
135     string* output) const {
136   if (!encryption_established_ ||
137       crypto_negotiated_params_.channel_id.empty()) {
138     return false;
139   }
140
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());
145   uint8 digest[32];
146   hash->Finish(digest, sizeof(digest));
147
148   base::Base64Encode(string(
149       reinterpret_cast<const char*>(digest), sizeof(digest)), output);
150   // Remove padding.
151   size_t len = output->size();
152   if (len >= 2) {
153     if ((*output)[len - 1] == '=') {
154       len--;
155       if ((*output)[len - 1] == '=') {
156         len--;
157       }
158       output->resize(len);
159     }
160   }
161   return true;
162 }
163
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(
170       result,
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);
179 }
180
181 QuicCryptoServerStream::ValidateCallback::ValidateCallback(
182     QuicCryptoServerStream* parent) : parent_(parent) {
183 }
184
185 void QuicCryptoServerStream::ValidateCallback::Cancel() {
186   parent_ = NULL;
187 }
188
189 void QuicCryptoServerStream::ValidateCallback::RunImpl(
190     const CryptoHandshakeMessage& client_hello,
191     const Result& result) {
192   if (parent_ != NULL) {
193     parent_->FinishProcessingHandshakeMessage(client_hello, result);
194   }
195 }
196
197 }  // namespace net