mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 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 | #include "webrtc/p2p/quic/quictransportchannel.h" |
| 12 | |
| 13 | #include <utility> |
| 14 | |
| 15 | #include "net/quic/crypto/proof_source.h" |
| 16 | #include "net/quic/crypto/proof_verifier.h" |
| 17 | #include "net/quic/crypto/quic_crypto_client_config.h" |
| 18 | #include "net/quic/crypto/quic_crypto_server_config.h" |
| 19 | #include "net/quic/quic_connection.h" |
| 20 | #include "net/quic/quic_crypto_client_stream.h" |
| 21 | #include "net/quic/quic_crypto_server_stream.h" |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 22 | #include "net/quic/quic_packet_writer.h" |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 23 | #include "net/quic/quic_protocol.h" |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 24 | #include "webrtc/p2p/base/common.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 25 | #include "webrtc/rtc_base/checks.h" |
| 26 | #include "webrtc/rtc_base/helpers.h" |
| 27 | #include "webrtc/rtc_base/logging.h" |
| 28 | #include "webrtc/rtc_base/socket.h" |
| 29 | #include "webrtc/rtc_base/thread.h" |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // QUIC public header constants for net::QuicConnection. These are arbitrary |
| 34 | // given that |channel_| only receives packets specific to this channel, |
| 35 | // in which case we already know the QUIC packets have the correct destination. |
| 36 | const net::QuicConnectionId kConnectionId = 0; |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 37 | const net::IPAddress kConnectionIpAddress(0, 0, 0, 0); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 38 | const net::IPEndPoint kConnectionIpEndpoint(kConnectionIpAddress, 0); |
| 39 | |
| 40 | // Arbitrary server port number for net::QuicCryptoClientConfig. |
| 41 | const int kQuicServerPort = 0; |
| 42 | |
| 43 | // QUIC connection timeout. This is large so that |channel_| can |
| 44 | // be responsible for connection timeout. |
| 45 | const int kIdleConnectionStateLifetime = 1000; // seconds |
| 46 | |
| 47 | // Length of HKDF input keying material, equal to its number of bytes. |
| 48 | // https://tools.ietf.org/html/rfc5869#section-2.2. |
| 49 | // TODO(mikescarlett): Verify that input keying material length is correct. |
| 50 | const size_t kInputKeyingMaterialLength = 32; |
| 51 | |
| 52 | // We don't pull the RTP constants from rtputils.h, to avoid a layer violation. |
| 53 | const size_t kMinRtpPacketLen = 12; |
| 54 | |
| 55 | bool IsRtpPacket(const char* data, size_t len) { |
| 56 | const uint8_t* u = reinterpret_cast<const uint8_t*>(data); |
| 57 | return (len >= kMinRtpPacketLen && (u[0] & 0xC0) == 0x80); |
| 58 | } |
| 59 | |
| 60 | // Function for detecting QUIC packets based off |
| 61 | // https://tools.ietf.org/html/draft-tsvwg-quic-protocol-02#section-6. |
| 62 | const size_t kMinQuicPacketLen = 2; |
| 63 | |
| 64 | bool IsQuicPacket(const char* data, size_t len) { |
| 65 | const uint8_t* u = reinterpret_cast<const uint8_t*>(data); |
| 66 | return (len >= kMinQuicPacketLen && (u[0] & 0x80) == 0); |
| 67 | } |
| 68 | |
| 69 | // Used by QuicCryptoServerConfig to provide dummy proof credentials. |
| 70 | // TODO(mikescarlett): Remove when secure P2P QUIC handshake is possible. |
| 71 | class DummyProofSource : public net::ProofSource { |
| 72 | public: |
| 73 | DummyProofSource() {} |
| 74 | ~DummyProofSource() override {} |
| 75 | |
| 76 | // ProofSource override. |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 77 | bool GetProof(const net::IPAddress& server_ip, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 78 | const std::string& hostname, |
| 79 | const std::string& server_config, |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 80 | net::QuicVersion quic_version, |
| 81 | base::StringPiece chlo_hash, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 82 | bool ecdsa_ok, |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 83 | scoped_refptr<net::ProofSource::Chain>* out_chain, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 84 | std::string* out_signature, |
| 85 | std::string* out_leaf_cert_sct) override { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 86 | LOG(LS_INFO) << "GetProof() providing dummy credentials for insecure QUIC"; |
| 87 | std::vector<std::string> certs; |
| 88 | certs.push_back("Dummy cert"); |
| 89 | *out_chain = new ProofSource::Chain(certs); |
| 90 | *out_signature = "Dummy signature"; |
| 91 | *out_leaf_cert_sct = "Dummy timestamp"; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | // Used by QuicCryptoClientConfig to ignore the peer's credentials |
| 97 | // and establish an insecure QUIC connection. |
| 98 | // TODO(mikescarlett): Remove when secure P2P QUIC handshake is possible. |
| 99 | class InsecureProofVerifier : public net::ProofVerifier { |
| 100 | public: |
| 101 | InsecureProofVerifier() {} |
| 102 | ~InsecureProofVerifier() override {} |
| 103 | |
| 104 | // ProofVerifier override. |
| 105 | net::QuicAsyncStatus VerifyProof( |
| 106 | const std::string& hostname, |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame] | 107 | const uint16_t port, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 108 | const std::string& server_config, |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame] | 109 | net::QuicVersion quic_version, |
| 110 | base::StringPiece chlo_hash, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 111 | const std::vector<std::string>& certs, |
| 112 | const std::string& cert_sct, |
| 113 | const std::string& signature, |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame] | 114 | const net::ProofVerifyContext* context, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 115 | std::string* error_details, |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 116 | std::unique_ptr<net::ProofVerifyDetails>* verify_details, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 117 | net::ProofVerifierCallback* callback) override { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 118 | LOG(LS_INFO) << "VerifyProof() ignoring credentials and returning success"; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 119 | return net::QUIC_SUCCESS; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | } // namespace |
| 124 | |
| 125 | namespace cricket { |
| 126 | |
| 127 | QuicTransportChannel::QuicTransportChannel(TransportChannelImpl* channel) |
| 128 | : TransportChannelImpl(channel->transport_name(), channel->component()), |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 129 | network_thread_(rtc::Thread::Current()), |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 130 | channel_(channel), |
johan | 27c3d5b | 2016-10-17 00:54:57 -0700 | [diff] [blame] | 131 | helper_(network_thread_) { |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 132 | channel_->SignalWritableState.connect(this, |
| 133 | &QuicTransportChannel::OnWritableState); |
| 134 | channel_->SignalReadPacket.connect(this, &QuicTransportChannel::OnReadPacket); |
| 135 | channel_->SignalSentPacket.connect(this, &QuicTransportChannel::OnSentPacket); |
| 136 | channel_->SignalReadyToSend.connect(this, |
| 137 | &QuicTransportChannel::OnReadyToSend); |
| 138 | channel_->SignalGatheringState.connect( |
| 139 | this, &QuicTransportChannel::OnGatheringState); |
| 140 | channel_->SignalCandidateGathered.connect( |
| 141 | this, &QuicTransportChannel::OnCandidateGathered); |
| 142 | channel_->SignalRoleConflict.connect(this, |
| 143 | &QuicTransportChannel::OnRoleConflict); |
| 144 | channel_->SignalRouteChange.connect(this, |
| 145 | &QuicTransportChannel::OnRouteChange); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 146 | channel_->SignalSelectedCandidatePairChanged.connect( |
| 147 | this, &QuicTransportChannel::OnSelectedCandidatePairChanged); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 148 | channel_->SignalStateChanged.connect( |
| 149 | this, &QuicTransportChannel::OnChannelStateChanged); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 150 | channel_->SignalReceivingState.connect( |
| 151 | this, &QuicTransportChannel::OnReceivingState); |
| 152 | |
| 153 | // Set the QUIC connection timeout. |
| 154 | config_.SetIdleConnectionStateLifetime( |
| 155 | net::QuicTime::Delta::FromSeconds(kIdleConnectionStateLifetime), |
| 156 | net::QuicTime::Delta::FromSeconds(kIdleConnectionStateLifetime)); |
| 157 | // Set the bytes reserved for the QUIC connection ID to zero. |
| 158 | config_.SetBytesForConnectionIdToSend(0); |
| 159 | } |
| 160 | |
| 161 | QuicTransportChannel::~QuicTransportChannel() {} |
| 162 | |
| 163 | bool QuicTransportChannel::SetLocalCertificate( |
| 164 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) { |
| 165 | if (!certificate) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 166 | LOG_J(LS_ERROR, this) |
| 167 | << "No local certificate was supplied. Not doing QUIC."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | if (!local_certificate_) { |
| 171 | local_certificate_ = certificate; |
| 172 | return true; |
| 173 | } |
| 174 | if (certificate == local_certificate_) { |
| 175 | // This may happen during renegotiation. |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 176 | LOG_J(LS_INFO, this) << "Ignoring identical certificate"; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 177 | return true; |
| 178 | } |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 179 | LOG_J(LS_ERROR, this) |
| 180 | << "Local certificate of the QUIC connection already set. " |
| 181 | "Can't change the local certificate once it's active."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | |
| 185 | rtc::scoped_refptr<rtc::RTCCertificate> |
| 186 | QuicTransportChannel::GetLocalCertificate() const { |
| 187 | return local_certificate_; |
| 188 | } |
| 189 | |
| 190 | bool QuicTransportChannel::SetSslRole(rtc::SSLRole role) { |
| 191 | if (ssl_role_ && *ssl_role_ == role) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 192 | LOG_J(LS_WARNING, this) << "Ignoring SSL Role identical to current role."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | if (quic_state_ != QUIC_TRANSPORT_CONNECTED) { |
| 196 | ssl_role_ = rtc::Optional<rtc::SSLRole>(role); |
| 197 | return true; |
| 198 | } |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 199 | LOG_J(LS_ERROR, this) |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 200 | << "SSL Role can't be reversed after the session is setup."; |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | bool QuicTransportChannel::GetSslRole(rtc::SSLRole* role) const { |
| 205 | if (!ssl_role_) { |
| 206 | return false; |
| 207 | } |
| 208 | *role = *ssl_role_; |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool QuicTransportChannel::SetRemoteFingerprint(const std::string& digest_alg, |
| 213 | const uint8_t* digest, |
| 214 | size_t digest_len) { |
| 215 | if (digest_alg.empty()) { |
| 216 | RTC_DCHECK(!digest_len); |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 217 | LOG_J(LS_ERROR, this) << "Remote peer doesn't support digest algorithm."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | std::string remote_fingerprint_value(reinterpret_cast<const char*>(digest), |
| 221 | digest_len); |
| 222 | // Once we have the local certificate, the same remote fingerprint can be set |
| 223 | // multiple times. This may happen during renegotiation. |
| 224 | if (remote_fingerprint_ && |
| 225 | remote_fingerprint_->value == remote_fingerprint_value && |
| 226 | remote_fingerprint_->algorithm == digest_alg) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 227 | LOG_J(LS_INFO, this) |
| 228 | << "Ignoring identical remote fingerprint and algorithm"; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | remote_fingerprint_ = rtc::Optional<RemoteFingerprint>(RemoteFingerprint()); |
| 232 | remote_fingerprint_->value = remote_fingerprint_value; |
| 233 | remote_fingerprint_->algorithm = digest_alg; |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | bool QuicTransportChannel::ExportKeyingMaterial(const std::string& label, |
| 238 | const uint8_t* context, |
| 239 | size_t context_len, |
| 240 | bool use_context, |
| 241 | uint8_t* result, |
| 242 | size_t result_len) { |
| 243 | std::string quic_context(reinterpret_cast<const char*>(context), context_len); |
| 244 | std::string quic_result; |
| 245 | if (!quic_->ExportKeyingMaterial(label, quic_context, result_len, |
| 246 | &quic_result)) { |
| 247 | return false; |
| 248 | } |
| 249 | quic_result.copy(reinterpret_cast<char*>(result), result_len); |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool QuicTransportChannel::GetSrtpCryptoSuite(int* cipher) { |
| 254 | *cipher = rtc::SRTP_AES128_CM_SHA1_80; |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | // Called from upper layers to send a media packet. |
| 259 | int QuicTransportChannel::SendPacket(const char* data, |
| 260 | size_t size, |
| 261 | const rtc::PacketOptions& options, |
| 262 | int flags) { |
| 263 | if ((flags & PF_SRTP_BYPASS) && IsRtpPacket(data, size)) { |
| 264 | return channel_->SendPacket(data, size, options); |
| 265 | } |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 266 | LOG(LS_ERROR) << "Failed to send an invalid SRTP bypass packet using QUIC."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | // The state transition logic here is as follows: |
| 271 | // - Before the QUIC handshake is complete, the QUIC channel is unwritable. |
| 272 | // - When |channel_| goes writable we start the QUIC handshake. |
| 273 | // - Once the QUIC handshake completes, the state is that of the |
| 274 | // |channel_| again. |
| 275 | void QuicTransportChannel::OnWritableState(TransportChannel* channel) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 276 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 277 | RTC_DCHECK(channel == channel_.get()); |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 278 | LOG_J(LS_VERBOSE, this) |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 279 | << "QuicTransportChannel: channel writable state changed to " |
| 280 | << channel_->writable(); |
| 281 | switch (quic_state_) { |
| 282 | case QUIC_TRANSPORT_NEW: |
| 283 | // Start the QUIC handshake when |channel_| is writable. |
| 284 | // This will fail if the SSL role or remote fingerprint are not set. |
| 285 | // Otherwise failure could result from network or QUIC errors. |
| 286 | MaybeStartQuic(); |
| 287 | break; |
| 288 | case QUIC_TRANSPORT_CONNECTED: |
| 289 | // Note: SignalWritableState fired by set_writable. |
| 290 | set_writable(channel_->writable()); |
| 291 | if (HasDataToWrite()) { |
| 292 | OnCanWrite(); |
| 293 | } |
| 294 | break; |
| 295 | case QUIC_TRANSPORT_CONNECTING: |
| 296 | // This channel is not writable until the QUIC handshake finishes. It |
| 297 | // might have been write blocked. |
| 298 | if (HasDataToWrite()) { |
| 299 | OnCanWrite(); |
| 300 | } |
| 301 | break; |
| 302 | case QUIC_TRANSPORT_CLOSED: |
| 303 | // TODO(mikescarlett): Allow the QUIC connection to be reset if it drops |
| 304 | // due to a non-failure. |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void QuicTransportChannel::OnReceivingState(TransportChannel* channel) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 310 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 311 | RTC_DCHECK(channel == channel_.get()); |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 312 | LOG_J(LS_VERBOSE, this) |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 313 | << "QuicTransportChannel: channel receiving state changed to " |
| 314 | << channel_->receiving(); |
| 315 | if (quic_state_ == QUIC_TRANSPORT_CONNECTED) { |
| 316 | // Note: SignalReceivingState fired by set_receiving. |
| 317 | set_receiving(channel_->receiving()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void QuicTransportChannel::OnReadPacket(TransportChannel* channel, |
| 322 | const char* data, |
| 323 | size_t size, |
| 324 | const rtc::PacketTime& packet_time, |
| 325 | int flags) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 326 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
| 327 | RTC_DCHECK(channel == channel_.get()); |
| 328 | RTC_DCHECK(flags == 0); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 329 | |
| 330 | switch (quic_state_) { |
| 331 | case QUIC_TRANSPORT_NEW: |
| 332 | // This would occur if other peer is ready to start QUIC but this peer |
| 333 | // hasn't started QUIC. |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 334 | LOG_J(LS_INFO, this) << "Dropping packet received before QUIC started."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 335 | break; |
| 336 | case QUIC_TRANSPORT_CONNECTING: |
| 337 | case QUIC_TRANSPORT_CONNECTED: |
| 338 | // We should only get QUIC or SRTP packets; STUN's already been demuxed. |
| 339 | // Is this potentially a QUIC packet? |
| 340 | if (IsQuicPacket(data, size)) { |
| 341 | if (!HandleQuicPacket(data, size)) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 342 | LOG_J(LS_ERROR, this) << "Failed to handle QUIC packet."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | } else { |
| 346 | // If this is an RTP packet, signal upwards as a bypass packet. |
| 347 | if (!IsRtpPacket(data, size)) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 348 | LOG_J(LS_ERROR, this) |
| 349 | << "Received unexpected non-QUIC, non-RTP packet."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | SignalReadPacket(this, data, size, packet_time, PF_SRTP_BYPASS); |
| 353 | } |
| 354 | break; |
| 355 | case QUIC_TRANSPORT_CLOSED: |
| 356 | // This shouldn't be happening. Drop the packet. |
| 357 | break; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | void QuicTransportChannel::OnSentPacket(TransportChannel* channel, |
| 362 | const rtc::SentPacket& sent_packet) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 363 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 364 | SignalSentPacket(this, sent_packet); |
| 365 | } |
| 366 | |
| 367 | void QuicTransportChannel::OnReadyToSend(TransportChannel* channel) { |
| 368 | if (writable()) { |
| 369 | SignalReadyToSend(this); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | void QuicTransportChannel::OnGatheringState(TransportChannelImpl* channel) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 374 | RTC_DCHECK(channel == channel_.get()); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 375 | SignalGatheringState(this); |
| 376 | } |
| 377 | |
| 378 | void QuicTransportChannel::OnCandidateGathered(TransportChannelImpl* channel, |
| 379 | const Candidate& c) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 380 | RTC_DCHECK(channel == channel_.get()); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 381 | SignalCandidateGathered(this, c); |
| 382 | } |
| 383 | |
| 384 | void QuicTransportChannel::OnRoleConflict(TransportChannelImpl* channel) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 385 | RTC_DCHECK(channel == channel_.get()); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 386 | SignalRoleConflict(this); |
| 387 | } |
| 388 | |
| 389 | void QuicTransportChannel::OnRouteChange(TransportChannel* channel, |
| 390 | const Candidate& candidate) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 391 | RTC_DCHECK(channel == channel_.get()); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 392 | SignalRouteChange(this, candidate); |
| 393 | } |
| 394 | |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 395 | void QuicTransportChannel::OnSelectedCandidatePairChanged( |
| 396 | TransportChannel* channel, |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 397 | CandidatePairInterface* selected_candidate_pair, |
zhihuang | f2c2f8f | 2016-07-13 14:13:49 -0700 | [diff] [blame] | 398 | int last_sent_packet_id, |
| 399 | bool ready_to_send) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 400 | RTC_DCHECK(channel == channel_.get()); |
Honghai Zhang | 52dce73 | 2016-03-31 12:37:31 -0700 | [diff] [blame] | 401 | SignalSelectedCandidatePairChanged(this, selected_candidate_pair, |
Taylor Brandstetter | 6bb1ef2 | 2016-06-27 18:09:03 -0700 | [diff] [blame] | 402 | last_sent_packet_id, ready_to_send); |
Honghai Zhang | cc411c0 | 2016-03-29 17:27:21 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 405 | void QuicTransportChannel::OnChannelStateChanged( |
| 406 | TransportChannelImpl* channel) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 407 | RTC_DCHECK(channel == channel_.get()); |
Honghai Zhang | 1590c39 | 2016-05-24 13:15:02 -0700 | [diff] [blame] | 408 | SignalStateChanged(this); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | bool QuicTransportChannel::MaybeStartQuic() { |
| 412 | if (!channel_->writable()) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 413 | LOG_J(LS_ERROR, this) << "Couldn't start QUIC handshake."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 414 | return false; |
| 415 | } |
| 416 | if (!CreateQuicSession() || !StartQuicHandshake()) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 417 | LOG_J(LS_WARNING, this) |
| 418 | << "Underlying channel is writable but cannot start " |
| 419 | "the QUIC handshake."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | // Verify connection is not closed due to QUIC bug or network failure. |
| 423 | // A closed connection should not happen since |channel_| is writable. |
| 424 | if (!quic_->connection()->connected()) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 425 | LOG_J(LS_ERROR, this) |
| 426 | << "QUIC connection should not be closed if underlying " |
| 427 | "channel is writable."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 428 | return false; |
| 429 | } |
| 430 | // Indicate that |quic_| is ready to receive QUIC packets. |
| 431 | set_quic_state(QUIC_TRANSPORT_CONNECTING); |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | bool QuicTransportChannel::CreateQuicSession() { |
| 436 | if (!ssl_role_ || !remote_fingerprint_) { |
| 437 | return false; |
| 438 | } |
| 439 | net::Perspective perspective = (*ssl_role_ == rtc::SSL_CLIENT) |
| 440 | ? net::Perspective::IS_CLIENT |
| 441 | : net::Perspective::IS_SERVER; |
| 442 | bool owns_writer = false; |
kwiberg | 3ec4679 | 2016-04-27 07:22:53 -0700 | [diff] [blame] | 443 | std::unique_ptr<net::QuicConnection> connection(new net::QuicConnection( |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 444 | kConnectionId, kConnectionIpEndpoint, &helper_, this, owns_writer, |
| 445 | perspective, net::QuicSupportedVersions())); |
| 446 | quic_.reset(new QuicSession(std::move(connection), config_)); |
| 447 | quic_->SignalHandshakeComplete.connect( |
| 448 | this, &QuicTransportChannel::OnHandshakeComplete); |
| 449 | quic_->SignalConnectionClosed.connect( |
| 450 | this, &QuicTransportChannel::OnConnectionClosed); |
mikescarlett | 18b67a5 | 2016-04-11 16:56:23 -0700 | [diff] [blame] | 451 | quic_->SignalIncomingStream.connect(this, |
| 452 | &QuicTransportChannel::OnIncomingStream); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | |
| 456 | bool QuicTransportChannel::StartQuicHandshake() { |
| 457 | if (*ssl_role_ == rtc::SSL_CLIENT) { |
| 458 | // Unique identifier for remote peer. |
| 459 | net::QuicServerId server_id(remote_fingerprint_->value, kQuicServerPort); |
| 460 | // Perform authentication of remote peer; owned by QuicCryptoClientConfig. |
| 461 | // TODO(mikescarlett): Actually verify proof. |
| 462 | net::ProofVerifier* proof_verifier = new InsecureProofVerifier(); |
| 463 | quic_crypto_client_config_.reset( |
| 464 | new net::QuicCryptoClientConfig(proof_verifier)); |
| 465 | net::QuicCryptoClientStream* crypto_stream = |
| 466 | new net::QuicCryptoClientStream(server_id, quic_.get(), |
| 467 | new net::ProofVerifyContext(), |
| 468 | quic_crypto_client_config_.get(), this); |
| 469 | quic_->StartClientHandshake(crypto_stream); |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 470 | LOG_J(LS_INFO, this) << "QuicTransportChannel: Started client handshake."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 471 | } else { |
| 472 | RTC_DCHECK_EQ(*ssl_role_, rtc::SSL_SERVER); |
| 473 | // Provide credentials to remote peer; owned by QuicCryptoServerConfig. |
| 474 | // TODO(mikescarlett): Actually provide credentials. |
| 475 | net::ProofSource* proof_source = new DummyProofSource(); |
| 476 | // Input keying material to HKDF, per http://tools.ietf.org/html/rfc5869. |
| 477 | // This is pseudorandom so that HKDF-Extract outputs a pseudorandom key, |
| 478 | // since QuicCryptoServerConfig does not use a salt value. |
| 479 | std::string source_address_token_secret; |
| 480 | if (!rtc::CreateRandomString(kInputKeyingMaterialLength, |
| 481 | &source_address_token_secret)) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 482 | LOG_J(LS_ERROR, this) |
| 483 | << "Error generating input keying material for HKDF."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 484 | return false; |
| 485 | } |
| 486 | quic_crypto_server_config_.reset(new net::QuicCryptoServerConfig( |
| 487 | source_address_token_secret, helper_.GetRandomGenerator(), |
| 488 | proof_source)); |
| 489 | // Provide server with serialized config string to prove ownership. |
| 490 | net::QuicCryptoServerConfig::ConfigOptions options; |
| 491 | quic_crypto_server_config_->AddDefaultConfig(helper_.GetRandomGenerator(), |
| 492 | helper_.GetClock(), options); |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame] | 493 | quic_compressed_certs_cache_.reset(new net::QuicCompressedCertsCache( |
| 494 | net::QuicCompressedCertsCache::kQuicCompressedCertsCacheSize)); |
| 495 | // TODO(mikescarlett): Add support for stateless rejects. |
| 496 | bool use_stateless_rejects_if_peer_supported = false; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 497 | net::QuicCryptoServerStream* crypto_stream = |
| 498 | new net::QuicCryptoServerStream(quic_crypto_server_config_.get(), |
mikescarlett | 8d37d29 | 2016-04-29 15:35:00 -0700 | [diff] [blame] | 499 | quic_compressed_certs_cache_.get(), |
| 500 | use_stateless_rejects_if_peer_supported, |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 501 | quic_.get()); |
| 502 | quic_->StartServerHandshake(crypto_stream); |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 503 | LOG_J(LS_INFO, this) << "QuicTransportChannel: Started server handshake."; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | bool QuicTransportChannel::HandleQuicPacket(const char* data, size_t size) { |
nisse | ede5da4 | 2017-01-12 05:15:36 -0800 | [diff] [blame] | 509 | RTC_DCHECK(rtc::Thread::Current() == network_thread_); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 510 | return quic_->OnReadPacket(data, size); |
| 511 | } |
| 512 | |
| 513 | net::WriteResult QuicTransportChannel::WritePacket( |
| 514 | const char* buffer, |
| 515 | size_t buf_len, |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 516 | const net::IPAddress& self_address, |
| 517 | const net::IPEndPoint& peer_address, |
| 518 | net::PerPacketOptions* options) { |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 519 | // QUIC should never call this if IsWriteBlocked, but just in case... |
| 520 | if (IsWriteBlocked()) { |
| 521 | return net::WriteResult(net::WRITE_STATUS_BLOCKED, EWOULDBLOCK); |
| 522 | } |
| 523 | // TODO(mikescarlett): Figure out how to tell QUIC "I dropped your packet, but |
| 524 | // don't block" without the QUIC connection tearing itself down. |
| 525 | int sent = channel_->SendPacket(buffer, buf_len, rtc::PacketOptions()); |
| 526 | int bytes_written = sent > 0 ? sent : 0; |
| 527 | return net::WriteResult(net::WRITE_STATUS_OK, bytes_written); |
| 528 | } |
| 529 | |
| 530 | // TODO(mikescarlett): Implement check for whether |channel_| is currently |
| 531 | // write blocked so that |quic_| does not try to write packet. This is |
| 532 | // necessary because |channel_| can be writable yet write blocked and |
| 533 | // channel_->GetError() is not flushed when there is no error. |
| 534 | bool QuicTransportChannel::IsWriteBlocked() const { |
| 535 | return !channel_->writable(); |
| 536 | } |
| 537 | |
| 538 | void QuicTransportChannel::OnHandshakeComplete() { |
| 539 | set_quic_state(QUIC_TRANSPORT_CONNECTED); |
| 540 | set_writable(true); |
| 541 | // OnReceivingState might have been called before the QUIC channel was |
| 542 | // connected, in which case the QUIC channel is now receiving. |
| 543 | if (channel_->receiving()) { |
| 544 | set_receiving(true); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void QuicTransportChannel::OnConnectionClosed(net::QuicErrorCode error, |
| 549 | bool from_peer) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 550 | LOG_J(LS_INFO, this) << "Connection closed by " |
| 551 | << (from_peer ? "other" : "this") << " peer " |
| 552 | << "with QUIC error " << error; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 553 | // TODO(mikescarlett): Allow the QUIC session to be reset when the connection |
| 554 | // does not close due to failure. |
| 555 | set_quic_state(QUIC_TRANSPORT_CLOSED); |
| 556 | set_writable(false); |
mikescarlett | 18b67a5 | 2016-04-11 16:56:23 -0700 | [diff] [blame] | 557 | SignalClosed(); |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | void QuicTransportChannel::OnProofValid( |
| 561 | const net::QuicCryptoClientConfig::CachedState& cached) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 562 | LOG_J(LS_INFO, this) << "Cached proof marked valid"; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void QuicTransportChannel::OnProofVerifyDetailsAvailable( |
| 566 | const net::ProofVerifyDetails& verify_details) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 567 | LOG_J(LS_INFO, this) << "Proof verify details available from" |
| 568 | << " QuicCryptoClientStream"; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | bool QuicTransportChannel::HasDataToWrite() const { |
| 572 | return quic_ && quic_->HasDataToWrite(); |
| 573 | } |
| 574 | |
| 575 | void QuicTransportChannel::OnCanWrite() { |
| 576 | RTC_DCHECK(quic_ != nullptr); |
| 577 | quic_->connection()->OnCanWrite(); |
| 578 | } |
| 579 | |
| 580 | void QuicTransportChannel::set_quic_state(QuicTransportState state) { |
mikescarlett | f537768 | 2016-03-29 12:14:55 -0700 | [diff] [blame] | 581 | LOG_J(LS_VERBOSE, this) << "set_quic_state from:" << quic_state_ << " to " |
| 582 | << state; |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 583 | quic_state_ = state; |
| 584 | } |
| 585 | |
mikescarlett | 18b67a5 | 2016-04-11 16:56:23 -0700 | [diff] [blame] | 586 | ReliableQuicStream* QuicTransportChannel::CreateQuicStream() { |
| 587 | if (quic_) { |
mikescarlett | 70035ca | 2016-04-29 18:14:37 -0700 | [diff] [blame] | 588 | net::SpdyPriority priority = 0; // Priority of the QUIC stream |
mikescarlett | 18b67a5 | 2016-04-11 16:56:23 -0700 | [diff] [blame] | 589 | return quic_->CreateOutgoingDynamicStream(priority); |
| 590 | } |
| 591 | return nullptr; |
| 592 | } |
| 593 | |
| 594 | void QuicTransportChannel::OnIncomingStream(ReliableQuicStream* stream) { |
| 595 | SignalIncomingStream(stream); |
| 596 | } |
| 597 | |
mikescarlett | 6459f84 | 2016-03-04 09:55:02 -0800 | [diff] [blame] | 598 | } // namespace cricket |