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
15namespace net {
16
17class CachedNetworkParameters;
18class CryptoHandshakeMessage;
19class QuicCryptoServerConfig;
20class QuicCryptoServerStream;
21class QuicSession;
22
23namespace test {
24class CryptoTestUtils;
25}  // namespace test
26
27// Receives a notification when the server hello (SHLO) has been ACKed by the
28// peer. At this point we disable HANDSHAKE_MODE in the sent packet manager.
29class NET_EXPORT_PRIVATE ServerHelloNotifier : public
30    QuicAckNotifier::DelegateInterface {
31 public:
32  explicit ServerHelloNotifier(QuicCryptoServerStream* stream)
33      : server_stream_(stream) {}
34
35  // QuicAckNotifier::DelegateInterface implementation
36  virtual void OnAckNotification(
37      int num_original_packets,
38      int num_original_bytes,
39      int num_retransmitted_packets,
40      int num_retransmitted_bytes,
41      QuicTime::Delta delta_largest_observed) OVERRIDE;
42
43 private:
44  virtual ~ServerHelloNotifier() {}
45
46  QuicCryptoServerStream* server_stream_;
47
48  DISALLOW_COPY_AND_ASSIGN(ServerHelloNotifier);
49};
50
51class NET_EXPORT_PRIVATE QuicCryptoServerStream : public QuicCryptoStream {
52 public:
53  QuicCryptoServerStream(const QuicCryptoServerConfig& crypto_config,
54                         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  virtual void SendServerConfigUpdate(
78      const CachedNetworkParameters* cached_network_params);
79
80  // Called by the ServerHello AckNotifier once the SHLO has been ACKed by the
81  // client.
82  void OnServerHelloAcked();
83
84 protected:
85  virtual QuicErrorCode ProcessClientHello(
86      const CryptoHandshakeMessage& message,
87      const ValidateClientHelloResultCallback::Result& result,
88      CryptoHandshakeMessage* reply,
89      std::string* error_details);
90
91  // Hook that allows the server to set QuicConfig defaults just
92  // before going through the parameter negotiation step.
93  virtual void OverrideQuicConfigDefaults(QuicConfig* config);
94
95 private:
96  friend class test::CryptoTestUtils;
97
98  class ValidateCallback : public ValidateClientHelloResultCallback {
99   public:
100    explicit ValidateCallback(QuicCryptoServerStream* parent);
101    // To allow the parent to detach itself from the callback before deletion.
102    void Cancel();
103
104    // From ValidateClientHelloResultCallback
105    virtual void RunImpl(const CryptoHandshakeMessage& client_hello,
106                         const Result& result) OVERRIDE;
107
108   private:
109    QuicCryptoServerStream* parent_;
110
111    DISALLOW_COPY_AND_ASSIGN(ValidateCallback);
112  };
113
114  // Invoked by ValidateCallback::RunImpl once initial validation of
115  // the client hello is complete.  Finishes processing of the client
116  // hello message and handles handshake success/failure.
117  void FinishProcessingHandshakeMessage(
118      const CryptoHandshakeMessage& message,
119      const ValidateClientHelloResultCallback::Result& result);
120
121  // crypto_config_ contains crypto parameters for the handshake.
122  const QuicCryptoServerConfig& crypto_config_;
123
124  // Pointer to the active callback that will receive the result of
125  // the client hello validation request and forward it to
126  // FinishProcessingHandshakeMessage for processing.  NULL if no
127  // handshake message is being validated.
128  ValidateCallback* validate_client_hello_cb_;
129
130  // Number of handshake messages received by this stream.
131  uint8 num_handshake_messages_;
132
133  // Number of server config update (SCUP) messages sent by this stream.
134  int num_server_config_update_messages_sent_;
135
136  DISALLOW_COPY_AND_ASSIGN(QuicCryptoServerStream);
137};
138
139}  // namespace net
140
141#endif  // NET_QUIC_QUIC_CRYPTO_SERVER_STREAM_H_
142