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 // Failed due to some error in the handshake process. |
| 34 QUIC_TRANSPORT_CLOSED, |
| 35 }; |
| 36 |
| 37 // QuicTransportChannel uses the QUIC protocol to establish encryption with |
| 38 // another peer, wrapping an existing TransportChannel 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 are transferred |
| 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 TransportChannel 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 // Returns true if local certificate is set and this peer supports QUIC. |
| 83 // TODO(mikescarlett): Rename method so it is not DTLS-specific. |
| 84 bool IsDtlsActive() const override { return true; } |
| 85 // Sends a RTP packet if sent with the PF_SRTP_BYPASS flag. |
| 86 int SendPacket(const char* data, |
| 87 size_t size, |
| 88 const rtc::PacketOptions& options, |
| 89 int flags) override; |
| 90 |
| 91 // TransportChannelImpl calls that we forward to the wrapped transport. |
| 92 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } |
| 93 IceRole GetIceRole() const override { return channel_->GetIceRole(); } |
| 94 int SetOption(rtc::Socket::Option opt, int value) override { |
| 95 return channel_->SetOption(opt, value); |
| 96 } |
| 97 bool GetOption(rtc::Socket::Option opt, int* value) override { |
| 98 return channel_->GetOption(opt, value); |
| 99 } |
| 100 int GetError() override { return channel_->GetError(); } |
| 101 bool GetStats(ConnectionInfos* infos) override { |
| 102 return channel_->GetStats(infos); |
| 103 } |
| 104 const std::string SessionId() const override { return channel_->SessionId(); } |
| 105 TransportChannelState GetState() const override { |
| 106 return channel_->GetState(); |
| 107 } |
| 108 void SetIceTiebreaker(uint64_t tiebreaker) override { |
| 109 channel_->SetIceTiebreaker(tiebreaker); |
| 110 } |
| 111 void SetIceCredentials(const std::string& ice_ufrag, |
| 112 const std::string& ice_pwd) override { |
| 113 channel_->SetIceCredentials(ice_ufrag, ice_pwd); |
| 114 } |
| 115 void SetRemoteIceCredentials(const std::string& ice_ufrag, |
| 116 const std::string& ice_pwd) override { |
| 117 channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd); |
| 118 } |
| 119 void SetRemoteIceMode(IceMode mode) override { |
| 120 channel_->SetRemoteIceMode(mode); |
| 121 } |
| 122 void MaybeStartGathering() override { channel_->MaybeStartGathering(); } |
| 123 IceGatheringState gathering_state() const override { |
| 124 return channel_->gathering_state(); |
| 125 } |
| 126 void AddRemoteCandidate(const Candidate& candidate) override { |
| 127 channel_->AddRemoteCandidate(candidate); |
| 128 } |
| 129 void SetIceConfig(const IceConfig& config) override { |
| 130 channel_->SetIceConfig(config); |
| 131 } |
| 132 |
| 133 // Sets up the ciphers to use for SRTP. |
| 134 // TODO(mikescarlett): Use SRTP ciphers for negotiation. |
| 135 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
| 136 return true; |
| 137 } |
| 138 // Determines which SRTP cipher was negotiated. |
| 139 // TODO(mikescarlett): Implement QUIC cipher negotiation. This currently |
| 140 // returns SRTP_AES128_CM_SHA1_80. |
| 141 bool GetSrtpCryptoSuite(int* cipher) override; |
| 142 bool SetSslRole(rtc::SSLRole role) override; |
| 143 bool GetSslRole(rtc::SSLRole* role) const override; |
| 144 // Determines which SSL cipher was negotiated. |
| 145 // TODO(mikescarlett): Implement QUIC cipher negotiation. |
| 146 bool GetSslCipherSuite(int* cipher) override { return false; } |
| 147 // Once QUIC is established (i.e., |quic_state_| is QUIC_TRANSPORT_CONNECTED), |
| 148 // this extracts the keys negotiated during the QUIC handshake, for use |
| 149 // in external encryption such as for extracting SRTP keys. |
| 150 bool ExportKeyingMaterial(const std::string& label, |
| 151 const uint8_t* context, |
| 152 size_t context_len, |
| 153 bool use_context, |
| 154 uint8_t* result, |
| 155 size_t result_len) override; |
| 156 void Connect() override; |
| 157 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { |
| 158 return false; |
| 159 } |
| 160 |
| 161 // QuicPacketWriter overrides. |
| 162 // Called from net::QuicConnection when |quic_| has packets to write. |
| 163 net::WriteResult WritePacket(const char* buffer, |
| 164 size_t buf_len, |
| 165 const net::IPAddressNumber& self_address, |
| 166 const net::IPEndPoint& peer_address) override; |
| 167 // Whether QuicTransportChannel buffers data when unable to write. If this is |
| 168 // set to false, then net::QuicConnection buffers unsent packets. |
| 169 bool IsWriteBlockedDataBuffered() const override { return false; } |
| 170 // Whether QuicTransportChannel is write blocked. If this returns true, |
| 171 // outgoing QUIC packets are queued until OnCanWrite() is called. |
| 172 bool IsWriteBlocked() const override; |
| 173 // Maximum size of the QUIC packet which can be written. |
| 174 net::QuicByteCount GetMaxPacketSize( |
| 175 const net::IPEndPoint& peer_address) const override { |
| 176 return net::kMaxPacketSize; |
| 177 } |
| 178 // This method is not used -- call set_writable(bool writable) instead. |
| 179 void SetWritable() override {} |
| 180 |
| 181 // QuicCryptoClientStream::ProofHandler overrides. |
| 182 // Called by client crypto handshake when cached proof is marked valid. |
| 183 void OnProofValid( |
| 184 const net::QuicCryptoClientConfig::CachedState& cached) override; |
| 185 // Called by client crypto handshake when proof verification details become |
| 186 // available, either because proof verification is complete, or when cached |
| 187 // details are used. |
| 188 void OnProofVerifyDetailsAvailable( |
| 189 const net::ProofVerifyDetails& verify_details) override; |
| 190 |
| 191 // Returns true if |quic_| has queued data which wasn't written due |
| 192 // to |channel_| being write blocked. |
| 193 bool HasDataToWrite() const; |
| 194 // Writes queued data for |quic_| when |channel_| is no longer write blocked. |
| 195 void OnCanWrite(); |
| 196 // Connectivity state of QuicTransportChannel. |
| 197 QuicTransportState quic_state() const { return quic_state_; } |
| 198 |
| 199 private: |
| 200 // Fingerprint of remote peer. |
| 201 struct RemoteFingerprint { |
| 202 std::string value; |
| 203 std::string algorithm; |
| 204 }; |
| 205 |
| 206 // Callbacks for |channel_|. |
| 207 void OnReadableState(TransportChannel* channel); |
| 208 void OnWritableState(TransportChannel* channel); |
| 209 void OnReadPacket(TransportChannel* channel, |
| 210 const char* data, |
| 211 size_t size, |
| 212 const rtc::PacketTime& packet_time, |
| 213 int flags); |
| 214 void OnSentPacket(TransportChannel* channel, |
| 215 const rtc::SentPacket& sent_packet); |
| 216 void OnReadyToSend(TransportChannel* channel); |
| 217 void OnReceivingState(TransportChannel* channel); |
| 218 void OnGatheringState(TransportChannelImpl* channel); |
| 219 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); |
| 220 void OnRoleConflict(TransportChannelImpl* channel); |
| 221 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); |
| 222 void OnConnectionRemoved(TransportChannelImpl* channel); |
| 223 |
| 224 // Callbacks for |quic_|. |
| 225 // Called when |quic_| has established crypto handshake. |
| 226 void OnHandshakeComplete(); |
| 227 // Called when |quic_| has connection closed. |
| 228 void OnConnectionClosed(net::QuicErrorCode error, bool from_peer); |
| 229 |
| 230 // Called by OnReadPacket() when QUIC packet is received. |
| 231 bool HandleQuicPacket(const char* data, size_t size); |
| 232 // Sets up the QUIC handshake. |
| 233 bool MaybeStartQuic(); |
| 234 // Creates the QUIC connection and |quic_|. |
| 235 bool CreateQuicSession(); |
| 236 // Creates the crypto stream and initializes handshake. |
| 237 bool StartQuicHandshake(); |
| 238 // Sets QuicTransportChannel connectivity state. |
| 239 void set_quic_state(QuicTransportState state); |
| 240 |
| 241 // Everything should occur on this thread. |
| 242 rtc::Thread* worker_thread_; |
| 243 // Underlying channel which is responsible for connecting with the remote peer |
| 244 // and sending/receiving packets across the network. |
| 245 TransportChannelImpl* const channel_; |
| 246 // Connectivity state of QuicTransportChannel. |
| 247 QuicTransportState quic_state_ = QUIC_TRANSPORT_NEW; |
| 248 // QUIC session which establishes the crypto handshake and converts data |
| 249 // to/from QUIC packets. |
| 250 rtc::scoped_ptr<QuicSession> quic_; |
| 251 // Config for |quic_|. |
| 252 net::QuicConfig config_; |
| 253 // Helper for net::QuicConnection that provides timing and |
| 254 // random number generation. |
| 255 QuicConnectionHelper helper_; |
| 256 // This peer's role in the QUIC crypto handshake. SSL_CLIENT implies this peer |
| 257 // initiates the handshake, while SSL_SERVER implies the remote peer initiates |
| 258 // the handshake. This must be set before we start QUIC. |
| 259 rtc::Optional<rtc::SSLRole> ssl_role_; |
| 260 // Config for QUIC crypto client stream, used when |ssl_role_| is SSL_CLIENT. |
| 261 rtc::scoped_ptr<net::QuicCryptoClientConfig> quic_crypto_client_config_; |
| 262 // Config for QUIC crypto server stream, used when |ssl_role_| is SSL_SERVER. |
| 263 rtc::scoped_ptr<net::QuicCryptoServerConfig> quic_crypto_server_config_; |
| 264 // This peer's certificate. |
| 265 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; |
| 266 // Fingerprint of the remote peer. This must be set before we start QUIC. |
| 267 rtc::Optional<RemoteFingerprint> remote_fingerprint_; |
| 268 |
| 269 RTC_DISALLOW_COPY_AND_ASSIGN(QuicTransportChannel); |
| 270 }; |
| 271 |
| 272 } // namespace cricket |
| 273 |
| 274 #endif // WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ |
OLD | NEW |