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_FAILED, | |
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 is sent with the PF_SRTP_BYPASS flag. | |
honghaiz3
2016/03/02 22:47:45
is ==> if?
mikescarlett
2016/03/03 02:19:20
Correct. Fixed.
| |
86 int SendPacket(const char* data, | |
87 size_t size, | |
88 const rtc::PacketOptions& options, | |
89 int flags) override; | |
90 | |
91 // TransportChannel calls that we forward to the wrapped transport. | |
honghaiz3
2016/03/02 22:47:45
TransportChannelImpl calls
mikescarlett
2016/03/03 02:19:20
Fixed.
| |
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 | |
127 void AddRemoteCandidate(const Candidate& candidate) override { | |
128 channel_->AddRemoteCandidate(candidate); | |
129 } | |
130 | |
131 void SetIceConfig(const IceConfig& config) override { | |
132 channel_->SetIceConfig(config); | |
133 } | |
134 | |
135 // Sets up the ciphers to use for SRTP. | |
136 // TODO(mikescarlett): Use SRTP ciphers for negotiation. | |
137 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { | |
138 return true; | |
139 } | |
140 // Determines which SRTP cipher was negotiated. | |
141 // TODO(mikescarlett): Implement QUIC cipher negotiation. This currently | |
142 // returns SRTP_AES128_CM_SHA1_80. | |
143 bool GetSrtpCryptoSuite(int* cipher) override; | |
144 bool SetSslRole(rtc::SSLRole role) override; | |
145 bool GetSslRole(rtc::SSLRole* role) const override; | |
146 // Determines which SSL cipher was negotiated. | |
147 // TODO(mikescarlett): Implement QUIC cipher negotiation. | |
148 bool GetSslCipherSuite(int* cipher) override { return false; } | |
149 // Once QUIC is established (i.e., |quic_state_| is QUIC_TRANSPORT_CONNECTED), | |
150 // this extracts the keys negotiated during the QUIC handshake, for use | |
151 // in external encryption such as for extracting SRTP keys. | |
152 bool ExportKeyingMaterial(const std::string& label, | |
153 const uint8_t* context, | |
154 size_t context_len, | |
155 bool use_context, | |
156 uint8_t* result, | |
157 size_t result_len) override; | |
158 void Connect() override; | |
159 | |
160 // QuicPacketWriter overrides. | |
161 // Called from net::QuicConnection when |quic_| has packets to write. | |
162 net::WriteResult WritePacket(const char* buffer, | |
163 size_t buf_len, | |
164 const net::IPAddressNumber& self_address, | |
165 const net::IPEndPoint& peer_address) override; | |
166 // Whether QuicTransportChannel buffers data when unable to write. If this is | |
167 // set to false, then net::QuicConnection buffers unsent packets. | |
168 bool IsWriteBlockedDataBuffered() const override { return false; } | |
169 // Whether QuicTransportChannel is write blocked. If this returns true, | |
170 // outgoing QUIC packets are queued until OnCanWrite() is called. | |
171 bool IsWriteBlocked() const override; | |
172 // Maximum size of the QUIC packet which can be written. | |
173 net::QuicByteCount GetMaxPacketSize( | |
174 const net::IPEndPoint& peer_address) const override { | |
175 return net::kMaxPacketSize; | |
176 } | |
177 | |
178 // QuicCryptoClientStream::ProofHandler overrides. | |
179 // Called by client crypto handshake when cached proof is marked valid. | |
180 void OnProofValid( | |
181 const net::QuicCryptoClientConfig::CachedState& cached) override; | |
182 // Called by client crypto handshake when proof verification details become | |
183 // available, either because proof verification is complete, or when cached | |
184 // details are used. | |
185 void OnProofVerifyDetailsAvailable( | |
186 const net::ProofVerifyDetails& verify_details) override; | |
187 | |
188 // Returns true if |quic_| has queued data which wasn't written due | |
189 // to |channel_| being write blocked. | |
190 bool HasDataToWrite() const; | |
191 // Writes queued data for |quic_| when |channel_| is no longer write blocked. | |
192 void OnCanWrite(); | |
193 // Connectivity state of QuicTransportChannel. | |
194 QuicTransportState quic_state() const { return quic_state_; } | |
195 | |
196 private: | |
197 // Fingerprint of remote peer. | |
198 struct RemoteFingerprint { | |
199 std::string value; | |
200 std::string algorithm; | |
201 }; | |
202 | |
203 // QuicPacketWriter override. | |
honghaiz3
2016/03/02 22:47:45
It is strange to change the permission of a virtua
mikescarlett
2016/03/03 02:19:20
I made these public since modifying the permission
| |
204 // This method is not used -- call set_writable(bool writable) instead. | |
205 void SetWritable() override {} | |
206 // TransportChannel override. | |
207 bool GetRemoteSSLCertificate(rtc::SSLCertificate** cert) const override { | |
208 return false; | |
209 } | |
210 | |
211 // Callbacks for |channel_|. | |
212 void OnReadableState(TransportChannel* channel); | |
213 void OnWritableState(TransportChannel* channel); | |
214 void OnReadPacket(TransportChannel* channel, | |
215 const char* data, | |
216 size_t size, | |
217 const rtc::PacketTime& packet_time, | |
218 int flags); | |
219 void OnSentPacket(TransportChannel* channel, | |
220 const rtc::SentPacket& sent_packet); | |
221 void OnReadyToSend(TransportChannel* channel); | |
222 void OnReceivingState(TransportChannel* channel); | |
223 void OnGatheringState(TransportChannelImpl* channel); | |
224 void OnCandidateGathered(TransportChannelImpl* channel, const Candidate& c); | |
225 void OnRoleConflict(TransportChannelImpl* channel); | |
226 void OnRouteChange(TransportChannel* channel, const Candidate& candidate); | |
227 void OnConnectionRemoved(TransportChannelImpl* channel); | |
228 | |
229 // Callbacks for |quic_|. | |
230 // Called when |quic_| has established crypto handshake. | |
231 void OnHandshakeComplete(); | |
232 // Called when |quic_| has connection closed. | |
233 void OnConnectionClosed(net::QuicErrorCode error, bool from_peer); | |
234 | |
235 // Called by OnReadPacket() when QUIC packet is received. | |
236 bool HandleQuicPacket(const char* data, size_t size); | |
237 // Sets up the QUIC handshake. | |
238 bool MaybeStartQuic(); | |
239 // Creates the QUIC connection and |quic_|. | |
240 bool CreateQuicSession(); | |
241 // Creates the crypto stream and initializes handshake. | |
242 bool StartQuicHandshake(); | |
243 // Sets QuicTransportChannel connectivity state. | |
244 void set_quic_state(QuicTransportState state); | |
245 | |
246 // Everything should occur on this thread. | |
247 rtc::Thread* worker_thread_; | |
248 // Underlying channel which is responsible for connecting with the remote peer | |
249 // and sending/receiving packets across the network. | |
250 TransportChannelImpl* const channel_; | |
251 // Connectivity state of QuicTransportChannel. | |
252 QuicTransportState quic_state_ = QUIC_TRANSPORT_NEW; | |
253 // QUIC session which establishes the crypto handshake and converts data | |
254 // to/from QUIC packets. | |
255 rtc::scoped_ptr<QuicSession> quic_; | |
256 // Config for |quic_|. | |
257 net::QuicConfig config_; | |
258 // Helper for net::QuicConnection that provides timing and | |
259 // random number generation. | |
260 QuicConnectionHelper helper_; | |
261 // This peer's role in the QUIC crypto handshake. SSL_CLIENT implies this peer | |
262 // initiates the handshake, while SSL_SERVER implies the remote peer initiates | |
263 // the handshake. This must be set before we start QUIC. | |
264 rtc::Optional<rtc::SSLRole> ssl_role_; | |
265 // Config for QUIC crypto client stream, used when |ssl_role_| is SSL_CLIENT. | |
266 rtc::scoped_ptr<net::QuicCryptoClientConfig> quic_crypto_client_config_; | |
267 // Config for QUIC crypto server stream, used when |ssl_role_| is SSL_SERVER. | |
268 rtc::scoped_ptr<net::QuicCryptoServerConfig> quic_crypto_server_config_; | |
269 // This peer's certificate. | |
270 rtc::scoped_refptr<rtc::RTCCertificate> local_certificate_; | |
271 // Fingerprint of the remote peer. This must be set before we start QUIC. | |
272 rtc::Optional<RemoteFingerprint> remote_fingerprint_; | |
273 | |
274 RTC_DISALLOW_COPY_AND_ASSIGN(QuicTransportChannel); | |
275 }; | |
276 | |
277 } // namespace cricket | |
278 | |
279 #endif // WEBRTC_P2P_QUIC_QUICTRANSPORTCHANNEL_H_ | |
OLD | NEW |