Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_crypto_server_stream.h
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 #ifndef NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_
6 #define NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_
7
8 #include <string>
9
10 #include "net/quic/crypto/crypto_handshake.h"
11 #include "net/quic/crypto/quic_crypto_server_config.h"
12 #include "net/quic/quic_config.h"
13 #include "net/quic/quic_crypto_stream.h"
14
15 namespace net {
16
17 class CryptoHandshakeMessage;
18 class QuicCryptoServerConfig;
19 class QuicCryptoServerStream;
20 class QuicSession;
21
22 namespace test {
23 class CryptoTestUtils;
24 }  // namespace test
25
26 // Receives a notification when the server hello (SHLO) has been ACKed by the
27 // peer. At this point we disable HANDSHAKE_MODE in the sent packet manager.
28 class NET_EXPORT_PRIVATE ServerHelloNotifier : public
29     QuicAckNotifier::DelegateInterface {
30  public:
31   explicit ServerHelloNotifier(QuicCryptoServerStream* stream)
32       : server_stream_(stream) {}
33
34   // QuicAckNotifier::DelegateInterface implementation
35   virtual void OnAckNotification(
36       int num_original_packets,
37       int num_original_bytes,
38       int num_retransmitted_packets,
39       int num_retransmitted_bytes,
40       QuicTime::Delta delta_largest_observed) OVERRIDE;
41
42  private:
43   virtual ~ServerHelloNotifier() {}
44
45   QuicCryptoServerStream* server_stream_;
46
47   DISALLOW_COPY_AND_ASSIGN(ServerHelloNotifier);
48 };
49
50 class NET_EXPORT_PRIVATE QuicCryptoServerStream : public QuicCryptoStream {
51  public:
52   QuicCryptoServerStream(const QuicCryptoServerConfig& crypto_config,
53                          QuicSession* session);
54   explicit QuicCryptoServerStream(QuicSession* session);
55   virtual ~QuicCryptoServerStream();
56
57   // Cancel any outstanding callbacks, such as asynchronous validation of client
58   // hello.
59   void CancelOutstandingCallbacks();
60
61   // CryptoFramerVisitorInterface implementation
62   virtual void OnHandshakeMessage(
63       const CryptoHandshakeMessage& message) OVERRIDE;
64
65   // GetBase64SHA256ClientChannelID sets |*output| to the base64 encoded,
66   // SHA-256 hash of the client's ChannelID key and returns true, if the client
67   // presented a ChannelID. Otherwise it returns false.
68   bool GetBase64SHA256ClientChannelID(std::string* output) const;
69
70   uint8 num_handshake_messages() const { return num_handshake_messages_; }
71
72   int num_server_config_update_messages_sent() const {
73     return num_server_config_update_messages_sent_;
74   }
75
76   // Sends the latest server config and source-address token to the client.
77   void SendServerConfigUpdate();
78
79   // Called by the ServerHello AckNotifier once the SHLO has been ACKed by the
80   // client.
81   void OnServerHelloAcked();
82
83  protected:
84   virtual QuicErrorCode ProcessClientHello(
85       const CryptoHandshakeMessage& message,
86       const ValidateClientHelloResultCallback::Result& result,
87       CryptoHandshakeMessage* reply,
88       std::string* error_details);
89
90   // Hook that allows the server to set QuicConfig defaults just
91   // before going through the parameter negotiation step.
92   virtual void OverrideQuicConfigDefaults(QuicConfig* config);
93
94  private:
95   friend class test::CryptoTestUtils;
96
97   class ValidateCallback : public ValidateClientHelloResultCallback {
98    public:
99     explicit ValidateCallback(QuicCryptoServerStream* parent);
100     // To allow the parent to detach itself from the callback before deletion.
101     void Cancel();
102
103     // From ValidateClientHelloResultCallback
104     virtual void RunImpl(const CryptoHandshakeMessage& client_hello,
105                          const Result& result) OVERRIDE;
106
107    private:
108     QuicCryptoServerStream* parent_;
109
110     DISALLOW_COPY_AND_ASSIGN(ValidateCallback);
111   };
112
113   // Invoked by ValidateCallback::RunImpl once initial validation of
114   // the client hello is complete.  Finishes processing of the client
115   // hello message and handles handshake success/failure.
116   void FinishProcessingHandshakeMessage(
117       const CryptoHandshakeMessage& message,
118       const ValidateClientHelloResultCallback::Result& result);
119
120   // crypto_config_ contains crypto parameters for the handshake.
121   const QuicCryptoServerConfig& crypto_config_;
122
123   // Pointer to the active callback that will receive the result of
124   // the client hello validation request and forward it to
125   // FinishProcessingHandshakeMessage for processing.  NULL if no
126   // handshake message is being validated.
127   ValidateCallback* validate_client_hello_cb_;
128
129   // Number of handshake messages received by this stream.
130   uint8 num_handshake_messages_;
131
132   // Number of server config update (SCUP) messages sent by this stream.
133   int num_server_config_update_messages_sent_;
134
135   DISALLOW_COPY_AND_ASSIGN(QuicCryptoServerStream);
136 };
137
138 }  // namespace net
139
140 #endif  // NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_