OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #ifndef WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |
| 12 #define WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |
| 13 |
| 14 #include <string> |
| 15 #include <vector> |
| 16 |
| 17 #include "net/quic/quic_packet_writer.h" |
| 18 #include "webrtc/base/optional.h" |
| 19 #include "webrtc/base/scoped_ptr.h" |
| 20 #include "webrtc/p2p/base/transportchannelimpl.h" |
| 21 #include "webrtc/p2p/quic/quicconnectionhelper.h" |
| 22 #include "webrtc/p2p/quic/quicsession.h" |
| 23 |
| 24 namespace cricket { |
| 25 |
| 26 enum QuicTransportState { |
| 27 // Haven't started QUIC handshake. |
| 28 QUIC_TRANSPORT_NEW = 0, |
| 29 // Started QUIC handshake. |
| 30 QUIC_TRANSPORT_CONNECTING, |
| 31 // Negotiated, and has an encrypted connection. |
| 32 QUIC_TRANSPORT_CONNECTED, |
| 33 // QUIC connection closed due to handshake failure or explicit shutdown. |
| 34 QUIC_TRANSPORT_CLOSED, |
| 35 }; |
| 36 |
| 37 // QuicTransportChannel uses the QUIC protocol to establish encryption with |
| 38 // another peer, wrapping an existing TransportChannelImpl instance |
| 39 // (e.g a P2PTransportChannel) responsible for connecting peers. |
| 40 // Once the wrapped transport channel is connected, QuicTransportChannel |
| 41 // negotiates the crypto handshake and establishes SRTP keying material. |
| 42 // |
| 43 // How it works: |
| 44 // |
| 45 // QuicTransportChannel { |
| 46 // QuicSession* quic_; |
| 47 // TransportChannelImpl* channel_; |
| 48 // } |
| 49 // |
| 50 // - Data written to SendPacket() is passed directly to |channel_| if it is |
| 51 // an SRTP packet with the PF_SRTP_BYPASS flag. |
| 52 // |
| 53 // - |quic_| passes outgoing packets to WritePacket(), which transfers them |
| 54 // to |channel_| to be sent across the network. |
| 55 // |
| 56 // - Data which comes into QuicTransportChannel::OnReadPacket is checked to |
| 57 // see if it is QUIC, and if it is, passed to |quic_|. SRTP packets are |
| 58 // signaled upwards as bypass packets. |
| 59 // |
| 60 // - When the QUIC handshake is completed, quic_state() returns |
| 61 // QUIC_TRANSPORT_CONNECTED and SRTP keying material can be exported. |
| 62 // |
| 63 // TODO(mikescarlett): Implement secure QUIC handshake, 0-RTT handshakes, and |
| 64 // QUIC data streams. |
| 65 class QuicTransportChannel : public TransportChannelImpl, |
| 66 public net::QuicPacketWriter, |
| 67 public net::QuicCryptoClientStream::ProofHandler { |
| 68 public: |
| 69 // |channel| - the TransportChannelImpl we are wrapping. |
| 70 explicit QuicTransportChannel(TransportChannelImpl* channel); |
| 71 ~QuicTransportChannel() override; |
| 72 |
| 73 // TransportChannel overrides. |
| 74 // TODO(mikescarlett): Implement certificate authentication. |
| 75 bool SetLocalCertificate( |
| 76 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; |
| 77 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; |
| 78 // TODO(mikescarlett): Implement fingerprint authentication. |
| 79 bool SetRemoteFingerprint(const std::string& digest_alg, |
| 80 const uint8_t* digest, |
| 81 size_t digest_len) override; |
| 82 // TODO(mikescarlett): Remove this DTLS-specific method when TransportChannel |
| 83 // does not require defining it. |
| 84 bool IsDtlsActive() const override { return true; } |
| 85 // Sends a RTP packet if the PF_SRTP_BYPASS flag is set. |
| 86 int SendPacket(const char* data, |
| 87 size_t size, |
| 88 const rtc::PacketOptions& options, |
| 89 int flags) override; |
| 90 // Sets up the ciphers to use for SRTP. |
| 91 // TODO(mikescarlett): Use SRTP ciphers for negotiation. |
| 92 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
| 93 return true; |
| 94 } |
| 95 // Determines which SRTP cipher was negotiated. |
| 96 // TODO(mikescarlett): Implement QUIC cipher negotiation. This currently |
| 97 // returns SRTP_AES128_CM_SHA1_80. |
| 98 bool GetSrtpCryptoSuite(int* cipher) override; |
| 99 bool SetSslRole(rtc::SSLRole role) override; |
| 100 bool GetSslRole(rtc::SSLRole* role) const override; |
| 101 // Determines which SSL cipher was negotiated. |
| 102 // TODO(mikescarlett): Implement QUIC cipher negotiation. |
| 103 bool GetSslCipherSuite(int* cipher) override { return false; } |
| 104 // Once QUIC is established (i.e., |quic_state_| is QUIC_TRANSPORT_CONNECTED), |
| 105 // this extracts the keys negotiated during the QUIC handshake, for use |
| 106 // in external encryption such as for extracting SRTP keys. |
| 107 bool ExportKeyingMaterial(const std::string& label, |
| 108 const uint8_t* context, |
| 109 size_t context_len, |
| 110 bool use_context, |
| 111 uint8_t* result, |
| 112 size_t result_len) override; |
| 113 // TODO(mikescarlett): Remove this method once TransportChannel does not |
| 114 // require defining it. |
| 115 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
| 116 return false; |
| 117 } |
| 118 |
| 119 // TransportChannelImpl overrides that we forward to the wrapped transport. |
| 120 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } |
| 121 IceRole GetIceRole() const override { return channel_->GetIceRole(); } |
| 122 int SetOption(rtc::Socket::Option opt, int value) override { |
| 123 return channel_->SetOption(opt, value); |
| 124 } |
| 125 bool GetOption(rtc::Socket::Option opt, int* value) override { |
| 126 return channel_->GetOption(opt, value); |
| 127 } |
| 128 int GetError() override { return channel_->GetError(); } |
| 129 bool GetStats(ConnectionInfos* infos) override { |
| 130 return channel_->GetStats(infos); |
| 131 } |
| 132 const std::string SessionId() const override { return channel_->SessionId(); } |
| 133 TransportChannelState GetState() const override { |
| 134 return channel_->GetState(); |
| 135 } |
| 136 void SetIceTiebreaker(uint64_t tiebreaker) override { |
| 137 channel_->SetIceTiebreaker(tiebreaker); |
| 138 } |
| 139 void SetIceCredentials(const std::string& ice_ufrag, |
| 140 const std::string& ice_pwd) override { |
| 141 channel_->SetIceCredentials(ice_ufrag, ice_pwd); |
| 142 } |
| 143 void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 144 const std::string& ice_pwd) override { |
| 145 channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd); |
| 146 } |
| 147 void SetRemoteIceMode(IceMode mode) override { |
| 148 channel_->SetRemoteIceMode(mode); |
| 149 } |
| 150 void MaybeStartGathering() override { channel_->MaybeStartGathering(); } |
| 151 IceGatheringState gathering_state() const override { |
| 152 return channel_->gathering_state(); |
| 153 } |
| 154 void AddRemoteCandidate(const Candidate& candidate) override { |
| 155 channel_->AddRemoteCandidate(candidate); |
| 156 } |
| 157 void SetIceConfig(const IceConfig& config) override { |
| 158 channel_->SetIceConfig(config); |
| 159 } |
| 160 void Connect() override { |
| 161 channel_->Connect(); |
| 162 } |
| 163 |
| 164 // QuicPacketWriter overrides. |
| 165 // Called from net::QuicConnection when |quic_| has packets to write. |
| 166 net::WriteResult WritePacket(const char* buffer, |
| 167 size_t buf_len, |
| 168 const net::IPAddressNumber& self_address, |
| 169 const net::IPEndPoint& peer_address) override; |
| 170 // Whether QuicTransportChannel buffers data when unable to write. If this is |
| 171 // set to false, then net::QuicConnection buffers unsent packets. |
| 172 bool IsWriteBlockedDataBuffered() const override { return false; } |
| 173 // Whether QuicTransportChannel is write blocked. If this returns true, |
| 174 // outgoing QUIC packets are queued by net::QuicConnection until |
| 175 // QuicTransportChannel::OnCanWrite() is called. |
| 176 bool IsWriteBlocked() const override; |
| 177 // Maximum size of the QUIC packet which can be written. |
| 178 net::QuicByteCount GetMaxPacketSize( |
| 179 const net::IPEndPoint& peer_address) const override { |
| 180 return net::kMaxPacketSize; |
| 181 } |
| 182 // This method is not used -- call set_writable(bool writable) instead. |
| 183 // TODO(miekscarlett): Remove this method once QuicPacketWriter does not |
| 184 // require defining it. |
| 185 void SetWritable() override {} |
| 186 |
| 187 // QuicCryptoClientStream::ProofHandler overrides. |
| 188 // Called by client crypto handshake when cached proof is marked valid. |
| 189 void OnProofValid( |
| 190 const net::QuicCryptoClientConfig::CachedState& cached) override; |
| 191 // Called by the client crypto handshake when proof verification details |
| 192 // become available, either because proof verification is complete, or when |
| 193 // cached details are used. |
| 194 void OnProofVerifyDetailsAvailable( |
| 195 const net::ProofVerifyDetails& verify_details) override; |
| 196 |
| 197 // Returns true if |quic_| has queued data which wasn't written due |
| 198 // to |channel_| being write blocked. |
| 199 bool HasDataToWrite() const; |
| 200 // Writes queued data for |quic_| when |channel_| is no longer write blocked. |
| 201 void OnCanWrite(); |
| 202 // Connectivity state of QuicTransportChannel. |
| 203 QuicTransportState quic_state() const { return quic_state_; } |
| 204 |
| 205 private: |
| 206 // Fingerprint of remote peer. |
| 207 struct RemoteFingerprint { |
| 208 std::string value; |
| 209 std::string algorithm; |
| 210 }; |
| 211 |
| 212 // Callbacks for |channel_|. |
| 213 void OnReadableState(TransportChannel* channel); |
| 214 void OnWritableState(TransportChannel* channel); |
| 215 void OnReadPacket(TransportChannel* channel, |
| 216 const char* data, |
| 217 size_t size, |
| 218 const rtc::PacketTime& packet_time, |
| 219 int flags); |
| 220 void OnSentPacket(TransportChannel* channel, |
| 221 const rtc::SentPacket& sent_packet); |
| 222 void OnReadyToSend(TransportChannel* channel); |
| 223 void OnReceivingState(TransportChannel* channel); |
| 224 void OnGatheringState(TransportChannelImpl* channel); |
| 225 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); |
| 226 void OnRoleConflict(TransportChannelImpl* channel); |
| 227 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); |
| 228 void OnConnectionRemoved(TransportChannelImpl* channel); |
| 229 |
| 230 // Callbacks for |quic_|. |
| 231 // Called when |quic_| has established the crypto handshake. |
| 232 void OnHandshakeComplete(); |
| 233 // Called when |quic_| has closed the connection. |
| 234 void OnConnectionClosed(net::QuicErrorCode error, bool from_peer); |
| 235 |
| 236 // Called by OnReadPacket() when a QUIC packet is received. |
| 237 bool HandleQuicPacket(const char* data, size_t size); |
| 238 // Sets up the QUIC handshake. |
| 239 bool MaybeStartQuic(); |
| 240 // Creates the QUIC connection and |quic_|. |
| 241 bool CreateQuicSession(); |
| 242 // Creates the crypto stream and initializes the handshake. |
| 243 bool StartQuicHandshake(); |
| 244 // Sets the QuicTransportChannel connectivity state. |
| 245 void set_quic_state(QuicTransportState state); |
| 246 |
| 247 // Everything should occur on this thread. |
| 248 rtc::Thread* worker_thread_; |
| 249 // Underlying channel which is responsible for connecting with the remote peer |
| 250 // and sending/receiving packets across the network. |
| 251 TransportChannelImpl* const channel_; |
| 252 // Connectivity state of QuicTransportChannel. |
| 253 QuicTransportState quic_state_ = QUIC_TRANSPORT_NEW; |
| 254 // QUIC session which establishes the crypto handshake and converts data |
| 255 // to/from QUIC packets. |
| 256 rtc::scoped_ptr<QuicSession> quic_; |
| 257 // Non-crypto config for |quic_|. |
| 258 net::QuicConfig config_; |
| 259 // Helper for net::QuicConnection that provides timing and |
| 260 // random number generation. |
| 261 QuicConnectionHelper helper_; |
| 262 // This peer's role in the QUIC crypto handshake. SSL_CLIENT implies this peer |
| 263 // initiates the handshake, while SSL_SERVER implies the remote peer initiates |
| 264 // the handshake. This must be set before we start QUIC. |
| 265 rtc::Optional<rtc::SSLRole> ssl_role_; |
| 266 // Config for QUIC crypto client stream, used when |ssl_role_| is SSL_CLIENT. |
| 267 rtc::scoped_ptr<net::QuicCryptoClientConfig> quic_crypto_client_config_; |
| 268 // Config for QUIC crypto server stream, used when |ssl_role_| is SSL_SERVER. |
| 269 rtc::scoped_ptr<net::QuicCryptoServerConfig> quic_crypto_server_config_; |
| 270 // This peer's certificate. |
| 271 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; |
| 272 // Fingerprint of the remote peer. This must be set before we start QUIC. |
| 273 rtc::Optional<RemoteFingerprint> remote_fingerprint_; |
| 274 |
| 275 RTC_DISALLOW_COPY_AND_ASSIGN(QuicTransportChannel); |
| 276 }; |
| 277 |
| 278 } // namespace cricket |
| 279 |
| 280 #endif // WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |
OLD | NEW |