OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2017 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_BASE_FAKEDTLSTRANSPORT_H_ |
| 12 #define WEBRTC_P2P_BASE_FAKEDTLSTRANSPORT_H_ |
| 13 |
| 14 #include <memory> |
| 15 #include <string> |
| 16 #include <vector> |
| 17 |
| 18 #include "webrtc/base/fakesslidentity.h" |
| 19 #include "webrtc/p2p/base/dtlstransportinternal.h" |
| 20 #include "webrtc/p2p/base/fakeicetransport.h" |
| 21 |
| 22 namespace cricket { |
| 23 |
| 24 // Fake DTLS transport which is implemented by wrapping a fake ICE transport. |
| 25 // Doesn't interact directly with fake ICE transport for anything other than |
| 26 // sending packets. |
| 27 class FakeDtlsTransport : public DtlsTransportInternal { |
| 28 public: |
| 29 explicit FakeDtlsTransport(FakeIceTransport* ice_transport) |
| 30 : ice_transport_(ice_transport), |
| 31 transport_name_(ice_transport->transport_name()), |
| 32 component_(ice_transport->component()), |
| 33 dtls_fingerprint_("", nullptr, 0) { |
| 34 ice_transport_->SignalReadPacket.connect( |
| 35 this, &FakeDtlsTransport::OnIceTransportReadPacket); |
| 36 } |
| 37 |
| 38 // If this constructor is called, a new fake ICE transport will be created, |
| 39 // and this FakeDtlsTransport will take the ownership. |
| 40 explicit FakeDtlsTransport(const std::string& name, int component) |
| 41 : owned_ice_transport_(new FakeIceTransport(name, component)), |
| 42 transport_name_(owned_ice_transport_->transport_name()), |
| 43 component_(owned_ice_transport_->component()), |
| 44 dtls_fingerprint_("", nullptr, 0) { |
| 45 ice_transport_ = owned_ice_transport_.get(); |
| 46 ice_transport_->SignalReadPacket.connect( |
| 47 this, &FakeDtlsTransport::OnIceTransportReadPacket); |
| 48 } |
| 49 |
| 50 ~FakeDtlsTransport() override { |
| 51 if (dest_ && dest_->dest_ == this) { |
| 52 dest_->dest_ = nullptr; |
| 53 } |
| 54 } |
| 55 |
| 56 // Get inner fake ICE transport. |
| 57 FakeIceTransport* fake_ice_transport() { return ice_transport_; } |
| 58 |
| 59 // If async, will send packets by "Post"-ing to message queue instead of |
| 60 // synchronously "Send"-ing. |
| 61 void SetAsync(bool async) { ice_transport_->SetAsync(async); } |
| 62 void SetAsyncDelay(int delay_ms) { ice_transport_->SetAsyncDelay(delay_ms); } |
| 63 |
| 64 // SetWritable, SetReceiving and SetDestination are the main methods that can |
| 65 // be used for testing, to simulate connectivity or lack thereof. |
| 66 void SetWritable(bool writable) { |
| 67 ice_transport_->SetWritable(writable); |
| 68 set_writable(writable); |
| 69 } |
| 70 void SetReceiving(bool receiving) { |
| 71 ice_transport_->SetReceiving(receiving); |
| 72 set_receiving(receiving); |
| 73 } |
| 74 |
| 75 // Simulates the two DTLS transports connecting to each other. |
| 76 // If |asymmetric| is true this method only affects this FakeDtlsTransport. |
| 77 // If false, it affects |dest| as well. |
| 78 void SetDestination(FakeDtlsTransport* dest, bool asymmetric = false) { |
| 79 if (dest == dest_) { |
| 80 return; |
| 81 } |
| 82 RTC_DCHECK(!dest || !dest_) |
| 83 << "Changing fake destination from one to another is not supported."; |
| 84 if (dest && !dest_) { |
| 85 // This simulates the DTLS handshake. |
| 86 dest_ = dest; |
| 87 if (local_cert_ && dest_->local_cert_) { |
| 88 do_dtls_ = true; |
| 89 NegotiateSrtpCiphers(); |
| 90 } |
| 91 SetWritable(true); |
| 92 if (!asymmetric) { |
| 93 dest->SetDestination(this, true); |
| 94 } |
| 95 ice_transport_->SetDestination( |
| 96 static_cast<FakeIceTransport*>(dest->ice_transport()), asymmetric); |
| 97 } else { |
| 98 // Simulates loss of connectivity, by asymmetrically forgetting dest_. |
| 99 dest_ = nullptr; |
| 100 SetWritable(false); |
| 101 ice_transport_->SetDestination(nullptr, asymmetric); |
| 102 } |
| 103 } |
| 104 |
| 105 // Fake DtlsTransportInternal implementation. |
| 106 DtlsTransportState dtls_state() const override { return dtls_state_; } |
| 107 const std::string& transport_name() const override { return transport_name_; } |
| 108 int component() const override { return component_; } |
| 109 const rtc::SSLFingerprint& dtls_fingerprint() const { |
| 110 return dtls_fingerprint_; |
| 111 } |
| 112 bool SetRemoteFingerprint(const std::string& alg, |
| 113 const uint8_t* digest, |
| 114 size_t digest_len) override { |
| 115 dtls_fingerprint_ = rtc::SSLFingerprint(alg, digest, digest_len); |
| 116 return true; |
| 117 } |
| 118 bool SetSslRole(rtc::SSLRole role) override { |
| 119 ssl_role_ = role; |
| 120 return true; |
| 121 } |
| 122 bool GetSslRole(rtc::SSLRole* role) const override { |
| 123 *role = ssl_role_; |
| 124 return true; |
| 125 } |
| 126 bool SetLocalCertificate( |
| 127 const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override { |
| 128 local_cert_ = certificate; |
| 129 return true; |
| 130 } |
| 131 void SetRemoteSSLCertificate(rtc::FakeSSLCertificate* cert) { |
| 132 remote_cert_ = cert; |
| 133 } |
| 134 bool IsDtlsActive() const override { return do_dtls_; } |
| 135 bool SetSrtpCryptoSuites(const std::vector<int>& ciphers) override { |
| 136 srtp_ciphers_ = ciphers; |
| 137 return true; |
| 138 } |
| 139 bool GetSrtpCryptoSuite(int* crypto_suite) override { |
| 140 if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 141 *crypto_suite = chosen_crypto_suite_; |
| 142 return true; |
| 143 } |
| 144 return false; |
| 145 } |
| 146 bool GetSslCipherSuite(int* cipher_suite) override { return false; } |
| 147 rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() const override { |
| 148 return local_cert_; |
| 149 } |
| 150 std::unique_ptr<rtc::SSLCertificate> GetRemoteSSLCertificate() |
| 151 const override { |
| 152 return remote_cert_ ? std::unique_ptr<rtc::SSLCertificate>( |
| 153 remote_cert_->GetReference()) |
| 154 : nullptr; |
| 155 } |
| 156 bool ExportKeyingMaterial(const std::string& label, |
| 157 const uint8_t* context, |
| 158 size_t context_len, |
| 159 bool use_context, |
| 160 uint8_t* result, |
| 161 size_t result_len) override { |
| 162 if (chosen_crypto_suite_ != rtc::SRTP_INVALID_CRYPTO_SUITE) { |
| 163 memset(result, 0xff, result_len); |
| 164 return true; |
| 165 } |
| 166 |
| 167 return false; |
| 168 } |
| 169 void set_ssl_max_protocol_version(rtc::SSLProtocolVersion version) { |
| 170 ssl_max_version_ = version; |
| 171 } |
| 172 rtc::SSLProtocolVersion ssl_max_protocol_version() const { |
| 173 return ssl_max_version_; |
| 174 } |
| 175 bool SetSrtpCiphers(const std::vector<std::string>& ciphers) override { |
| 176 std::vector<int> crypto_suites; |
| 177 for (const auto cipher : ciphers) { |
| 178 crypto_suites.push_back(rtc::SrtpCryptoSuiteFromName(cipher)); |
| 179 } |
| 180 return SetSrtpCryptoSuites(crypto_suites); |
| 181 } |
| 182 |
| 183 IceTransportInternal* ice_transport() override { return ice_transport_; } |
| 184 |
| 185 // PacketTransportInterface implementation, which passes through to fake ICE |
| 186 // transport for sending actual packets. |
| 187 bool writable() const override { return writable_; } |
| 188 bool receiving() const override { return receiving_; } |
| 189 int SendPacket(const char* data, |
| 190 size_t len, |
| 191 const rtc::PacketOptions& options, |
| 192 int flags) override { |
| 193 // We expect only SRTP packets to be sent through this interface. |
| 194 if (flags != PF_SRTP_BYPASS && flags != 0) { |
| 195 return -1; |
| 196 } |
| 197 return ice_transport_->SendPacket(data, len, options, flags); |
| 198 } |
| 199 int SetOption(rtc::Socket::Option opt, int value) override { |
| 200 return ice_transport_->SetOption(opt, value); |
| 201 } |
| 202 bool GetOption(rtc::Socket::Option opt, int* value) override { |
| 203 return ice_transport_->GetOption(opt, value); |
| 204 } |
| 205 int GetError() override { return ice_transport_->GetError(); } |
| 206 |
| 207 private: |
| 208 void OnIceTransportReadPacket(PacketTransportInterface* ice_, |
| 209 const char* data, |
| 210 size_t len, |
| 211 const rtc::PacketTime& time, |
| 212 int flags) { |
| 213 SignalReadPacket(this, data, len, time, flags); |
| 214 } |
| 215 |
| 216 void NegotiateSrtpCiphers() { |
| 217 for (std::vector<int>::const_iterator it1 = srtp_ciphers_.begin(); |
| 218 it1 != srtp_ciphers_.end(); ++it1) { |
| 219 for (std::vector<int>::const_iterator it2 = dest_->srtp_ciphers_.begin(); |
| 220 it2 != dest_->srtp_ciphers_.end(); ++it2) { |
| 221 if (*it1 == *it2) { |
| 222 chosen_crypto_suite_ = *it1; |
| 223 return; |
| 224 } |
| 225 } |
| 226 } |
| 227 } |
| 228 |
| 229 void set_receiving(bool receiving) { |
| 230 if (receiving_ == receiving) { |
| 231 return; |
| 232 } |
| 233 receiving_ = receiving; |
| 234 SignalReceivingState(this); |
| 235 } |
| 236 |
| 237 void set_writable(bool writable) { |
| 238 if (writable_ == writable) { |
| 239 return; |
| 240 } |
| 241 writable_ = writable; |
| 242 if (writable_) { |
| 243 SignalReadyToSend(this); |
| 244 } |
| 245 SignalWritableState(this); |
| 246 } |
| 247 |
| 248 FakeIceTransport* ice_transport_; |
| 249 std::unique_ptr<FakeIceTransport> owned_ice_transport_; |
| 250 std::string transport_name_; |
| 251 int component_; |
| 252 FakeDtlsTransport* dest_ = nullptr; |
| 253 rtc::scoped_refptr<rtc::RTCCertificate> local_cert_; |
| 254 rtc::FakeSSLCertificate* remote_cert_ = nullptr; |
| 255 bool do_dtls_ = false; |
| 256 std::vector<int> srtp_ciphers_; |
| 257 int chosen_crypto_suite_ = rtc::SRTP_INVALID_CRYPTO_SUITE; |
| 258 rtc::SSLProtocolVersion ssl_max_version_ = rtc::SSL_PROTOCOL_DTLS_12; |
| 259 rtc::SSLFingerprint dtls_fingerprint_; |
| 260 rtc::SSLRole ssl_role_ = rtc::SSL_CLIENT; |
| 261 |
| 262 DtlsTransportState dtls_state_ = DTLS_TRANSPORT_NEW; |
| 263 |
| 264 bool receiving_ = false; |
| 265 bool writable_ = false; |
| 266 }; |
| 267 |
| 268 } // namespace cricket |
| 269 |
| 270 #endif // WEBRTC_P2P_BASE_FAKEDTLSTRANSPORT_H_ |
OLD | NEW |