Chromium Code Reviews| 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/scoped_ptr.h" | |
| 19 #include "webrtc/p2p/base/transportchannelimpl.h" | |
| 20 #include "webrtc/p2p/quic/quicconnectionhelper.h" | |
| 21 #include "webrtc/p2p/quic/quicsession.h" | |
| 22 | |
| 23 namespace cricket { | |
| 24 | |
| 25 enum QuicTransportState { | |
| 26 // Haven't started QUIC handshake. | |
| 27 QUIC_TRANSPORT_NEW = 0, | |
| 28 // Started QUIC handshake. | |
| 29 QUIC_TRANSPORT_CONNECTING, | |
| 30 // Negotiated, and has an ecrypted connection. | |
|
pthatcher1
2016/02/24 23:31:13
encrypted
mikescarlett
2016/02/26 00:54:14
Done.
| |
| 31 QUIC_TRANSPORT_CONNECTED, | |
| 32 // Failed due to some error in the handshake process. | |
| 33 QUIC_TRANSPORT_FAILED, | |
| 34 }; | |
| 35 | |
| 36 // QuicTransportChannel uses the QUIC protocol to establish encryption with | |
| 37 // another peer, wrapping an existing TransportChannel instance | |
| 38 // (e.g a P2PTransportChannel) responsible for connecting peers. | |
| 39 // Once the wrapped transport channel is connected, QuicTransportChannel | |
| 40 // negotiates the crypto handshake and establishes SRTP keying material. | |
| 41 // | |
| 42 // How it works: | |
| 43 // | |
| 44 // QuicTransportChannel { | |
| 45 // QuicSession* quic_; | |
| 46 // TransportChannelImpl* channel_; | |
| 47 // } | |
| 48 // | |
| 49 // - Data written to SendPacket() is passed directly to |channel_| if the QUIC | |
| 50 // handshake has not begun or if an SRTP packet is sent with the | |
| 51 // PF_SRTP_BYPASS flag after the QUIC handshake. | |
|
pthatcher1
2016/02/24 23:31:13
I understand why we pass SRTP packets directly, bu
mikescarlett
2016/02/26 00:54:14
I am removing this behavior. I only did that becau
| |
| 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); | |
|
pthatcher1
2016/02/24 23:31:13
Why does this need a TransportChannelImpl instead
mikescarlett
2016/02/26 00:54:14
QuicTransportChannel uses |channel| to implement I
pthatcher1
2016/02/27 01:14:19
OK, that seems fine.
| |
| 71 ~QuicTransportChannel() override; | |
| 72 | |
| 73 // TransportChannel overrides. | |
| 74 // TODO(mikescarlett): Certificate authentication is not implemented yet. | |
| 75 bool SetLocalCertificate( | |
| 76 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override; | |
| 77 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override; | |
| 78 // TODO(mikescarlett): Fingerprint authentication is not implemented yet. | |
| 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): Method name is DTLS-specific. | |
|
pthatcher1
2016/02/24 23:31:13
Can you change the TODOs to be action to take, suc
mikescarlett
2016/02/26 00:54:14
Done.
| |
| 84 bool IsDtlsActive() const override { return quic_active_; } | |
|
pthatcher1
2016/02/25 08:24:25
Can we just make this return true? Unlike DTLS, I
mikescarlett
2016/02/26 00:54:14
Done.
| |
| 85 // Sends a non-QUIC packet. Packets are sent only if the QUIC handshake has | |
| 86 // not started, or an RTP packet is sent with the PF_SRTP_BYPASS after the | |
| 87 // QUIC handshake is established. | |
| 88 int SendPacket(const char* data, | |
| 89 size_t size, | |
| 90 const rtc::PacketOptions& options, | |
| 91 int flags) override; | |
| 92 | |
| 93 // TransportChannel calls that we forward to the wrapped transport. | |
| 94 void SetIceRole(IceRole role) override { channel_->SetIceRole(role); } | |
| 95 IceRole GetIceRole() const override { return channel_->GetIceRole(); } | |
| 96 int SetOption(rtc::Socket::Option opt, int value) override { | |
| 97 return channel_->SetOption(opt, value); | |
| 98 } | |
| 99 bool GetOption(rtc::Socket::Option opt, int* value) override { | |
| 100 return channel_->GetOption(opt, value); | |
| 101 } | |
| 102 int GetError() override { return channel_->GetError(); } | |
| 103 bool GetStats(ConnectionInfos* infos) override { | |
| 104 return channel_->GetStats(infos); | |
| 105 } | |
| 106 const std::string SessionId() const override { return channel_->SessionId(); } | |
| 107 TransportChannelState GetState() const override { | |
| 108 return channel_->GetState(); | |
| 109 } | |
| 110 void SetIceTiebreaker(uint64_t tiebreaker) override { | |
| 111 channel_->SetIceTiebreaker(tiebreaker); | |
| 112 } | |
| 113 void SetIceCredentials(const std::string& ice_ufrag, | |
| 114 const std::string& ice_pwd) override { | |
| 115 channel_->SetIceCredentials(ice_ufrag, ice_pwd); | |
| 116 } | |
| 117 void SetRemoteIceCredentials(const std::string& ice_ufrag, | |
| 118 const std::string& ice_pwd) override { | |
| 119 channel_->SetRemoteIceCredentials(ice_ufrag, ice_pwd); | |
| 120 } | |
| 121 void SetRemoteIceMode(IceMode mode) override { | |
| 122 channel_->SetRemoteIceMode(mode); | |
| 123 } | |
| 124 void MaybeStartGathering() override { channel_->MaybeStartGathering(); } | |
| 125 IceGatheringState gathering_state() const override { | |
| 126 return channel_->gathering_state(); | |
| 127 } | |
| 128 | |
| 129 void AddRemoteCandidate(const Candidate& candidate) override { | |
| 130 channel_->AddRemoteCandidate(candidate); | |
| 131 } | |
| 132 | |
| 133 void SetIceConfig(const IceConfig& config) override { | |
| 134 channel_->SetIceConfig(config); | |
| 135 } | |
| 136 | |
| 137 // Sets up the ciphers to use for SRTP. | |
| 138 // TODO(mikescarlett): SRTP ciphers are currently ignored by QuicSession. | |
| 139 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { | |
| 140 return true; | |
| 141 } | |
| 142 // Determines which SRTP cipher was negotiated. | |
| 143 // TODO(mikescarlett): Cipher negotiation is not implemented by QUIC. This | |
| 144 // always uses SRTP_AES128_CM_SHA1_80. | |
| 145 bool GetSrtpCryptoSuite(int* cipher) override; | |
|
pthatcher1
2016/02/24 23:31:13
In voice-only calls, we tend to use SRTP_AES128_CM
mikescarlett
2016/02/26 00:54:14
Agreed. A hacky solution (which isn't exactly nego
| |
| 146 bool SetSslRole(rtc::SSLRole role) override; | |
| 147 bool GetSslRole(rtc::SSLRole* role) const override; | |
| 148 // Determines which SSL cipher was negotiated. | |
| 149 // TODO(mikescarlett): Cipher negotiation is not implemented by QUIC. | |
| 150 bool GetSslCipherSuite(int* cipher) override { return false; } | |
| 151 // Once QUIC has been established, retrieves the certificate in | |
| 152 // use by the remote peer, for use in external identity verification. | |
|
pthatcher1
2016/02/24 23:31:13
This is really just used for stats, so we should f
mikescarlett
2016/02/26 00:54:14
Ok. In that case I'll ignore this method and move
| |
| 153 // TODO(mikescarlett): Not implemented unless QUIC connections are secure. | |
| 154 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { | |
| 155 return false; | |
| 156 } | |
| 157 // Once QUIC is established (i.e., |quic_state_| is QUIC_TRANSPORT_CONNECTED), | |
| 158 // this extracts the keys negotiated during the QUIC handshake, for use | |
| 159 // in external encryption such as for extracting SRTP keys. | |
| 160 bool ExportKeyingMaterial(const std::string& label, | |
| 161 const uint8_t* context, | |
| 162 size_t context_len, | |
| 163 bool use_context, | |
| 164 uint8_t* result, | |
| 165 size_t result_len) override; | |
| 166 void Connect() override; | |
| 167 | |
| 168 // QuicPacketWriter overrides. | |
| 169 // Called from net::QuicConnection when |quic_| has packets to write. | |
| 170 net::WriteResult WritePacket(const char* buffer, | |
| 171 size_t buf_len, | |
| 172 const net::IPAddressNumber& self_address, | |
| 173 const net::IPEndPoint& peer_address) override; | |
| 174 // Whether QuicTransportChannel buffers data when unable to write. If this is | |
| 175 // set to false, then net::QuicConnection buffers unsent packets. | |
| 176 bool IsWriteBlockedDataBuffered() const override { return false; } | |
| 177 // Whether QuicTransportChannel is write blocked. If this returns true, | |
| 178 // outgoing QUIC packets are queued until OnCanWrite() is called. | |
| 179 bool IsWriteBlocked() const override; | |
| 180 // Maximum size of the QUIC packet which can be written. | |
| 181 net::QuicByteCount GetMaxPacketSize( | |
| 182 const net::IPEndPoint& peer_address) const override { | |
| 183 return net::kMaxPacketSize; | |
| 184 } | |
| 185 | |
| 186 // QuicCryptoClientStream::ProofHandler overrides. | |
| 187 // Called by client crypto handshake when cached proof is marked valid. | |
| 188 void OnProofValid( | |
| 189 const net::QuicCryptoClientConfig::CachedState& cached) override; | |
| 190 // Called by client crypto handshake when proof verification details become | |
| 191 // available, either because proof verification is complete, or when cached | |
| 192 // details are used. | |
| 193 void OnProofVerifyDetailsAvailable( | |
| 194 const net::ProofVerifyDetails& verify_details) override; | |
| 195 | |
| 196 // Returns true if |quic_| has queued data which wasn't written due | |
| 197 // to |channel_| being write blocked. | |
| 198 bool HasDataToWrite() const; | |
| 199 // Writes queued data for |quic_| when |channel_| is no longer write blocked. | |
| 200 void OnCanWrite(); | |
| 201 // Connectivity state of QuicTransportChannel. | |
| 202 QuicTransportState quic_state() const { return quic_state_; } | |
| 203 | |
| 204 private: | |
| 205 // QuicPacketWriter override. | |
| 206 // This method is not used -- call set_writable(bool writable) instead. | |
| 207 void SetWritable() override {} | |
| 208 | |
| 209 // Callbacks for |channel_|. | |
| 210 void OnReadableState(TransportChannel* channel); | |
| 211 void OnWritableState(TransportChannel* channel); | |
| 212 void OnReadPacket(TransportChannel* channel, | |
| 213 const char* data, | |
| 214 size_t size, | |
| 215 const rtc::PacketTime& packet_time, | |
| 216 int flags); | |
| 217 void OnSentPacket(TransportChannel* channel, | |
| 218 const rtc::SentPacket& sent_packet); | |
| 219 void OnReadyToSend(TransportChannel* channel); | |
| 220 void OnReceivingState(TransportChannel* channel); | |
| 221 void OnGatheringState(TransportChannelImpl* channel); | |
| 222 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); | |
| 223 void OnRoleConflict(TransportChannelImpl* channel); | |
| 224 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); | |
| 225 void OnConnectionRemoved(TransportChannelImpl* channel); | |
| 226 | |
| 227 // Callbacks for |quic_|. | |
| 228 // Called when |quic_| has established crypto handshake. | |
| 229 void OnHandshakeComplete(); | |
| 230 // Called when |quic_| has connection closed. | |
| 231 void OnConnectionClosed(net::QuicErrorCode error, bool from_peer); | |
| 232 | |
| 233 // Called by OnReadPacket() when QUIC packet is received. | |
| 234 bool HandleQuicPacket(const char* data, size_t size); | |
| 235 // Sets up the QUIC handshake. | |
| 236 bool MaybeStartQuic(); | |
| 237 // Creates the QUIC connection and |quic_|. | |
| 238 bool CreateQuicSession(); | |
| 239 // Creates the crypto stream and initializes handshake. | |
| 240 bool StartQuicHandshake(); | |
| 241 // Sets QuicTransportChannel connectivity state. | |
| 242 void set_quic_state(QuicTransportState state); | |
| 243 | |
| 244 // Everything should occur on this thread. | |
| 245 rtc::Thread* worker_thread_; | |
| 246 // Underlying channel which is responsible for connecting with the remote peer | |
| 247 // and sending/receiving packets across the network. | |
| 248 TransportChannelImpl* const channel_; | |
| 249 // Connectivity state of QuicTransportChannel. | |
| 250 QuicTransportState quic_state_ = QUIC_TRANSPORT_NEW; | |
| 251 // QUIC session which establishes the crypto handshake and converts data | |
| 252 // to/from QUIC packets. | |
| 253 rtc::scoped_ptr<QuicSession> quic_; | |
| 254 // Config for |quic_|. | |
| 255 net::QuicConfig config_; | |
| 256 // Helper for net::QuicConnection that provides timing and | |
| 257 // random number generation. | |
| 258 QuicConnectionHelper helper_; | |
| 259 // This peer's role in the QUIC crypto handshake. SSL_CLIENT implies this peer | |
| 260 // initiates the handshake, while SSL_SERVER implies the remote peer initiates | |
| 261 // the handshake. | |
| 262 rtc::SSLRole ssl_role_; | |
| 263 // Config for QUIC crypto client stream, used when |ssl_role_| is SSL_CLIENT. | |
| 264 rtc::scoped_ptr<net::QuicCryptoClientConfig> quic_crypto_client_config_; | |
| 265 // Config for QUIC crypto server stream, used when |ssl_role_| is SSL_SERVER. | |
| 266 rtc::scoped_ptr<net::QuicCryptoServerConfig> quic_crypto_server_config_; | |
| 267 // Remote fingerprint parameters that are established before we start QUIC. | |
| 268 std::string remote_fingerprint_value_; | |
| 269 std::string remote_fingerprint_algorithm_; | |
| 270 // This peer's certificate. | |
| 271 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; | |
| 272 // True if local certificate is set and this peer supports QUIC. | |
| 273 bool quic_active_ = false; | |
|
pthatcher1
2016/02/25 08:24:25
Instead of having a separate bool, I'd suggest usi
mikescarlett
2016/02/26 00:54:14
Done.
| |
| 274 // True if remote fingerprint is set. | |
| 275 bool set_remote_fingerprint_ = false; | |
|
pthatcher1
2016/02/25 08:24:25
I'd suggest making a RemoteFingerprint struct that
mikescarlett
2016/02/26 00:54:14
Done.
| |
| 276 // True if SetSslRole() has been called. | |
| 277 bool set_ssl_role_ = false; | |
|
pthatcher1
2016/02/25 08:24:25
Same here: Optional<rtc::SSLRole> instead of two s
mikescarlett
2016/02/26 00:54:14
Done.
| |
| 278 | |
| 279 RTC_DISALLOW_COPY_AND_ASSIGN(QuicTransportChannel); | |
| 280 }; | |
| 281 | |
| 282 } // namespace cricket | |
| 283 | |
| 284 #endif // WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ | |
| OLD | NEW |