blob: f82cf2a11a2828533170ada1c1469a810fc55d79 [file] [log] [blame]
Zhi Huange818b6e2018-02-22 15:26:27 -08001/*
2 * Copyright 2018 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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "pc/jsep_transport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stddef.h>
14#include <stdint.h>
Zhi Huange818b6e2018-02-22 15:26:27 -080015#include <memory>
Yves Gerey3e707812018-11-28 16:47:49 +010016#include <type_traits>
Zhi Huange818b6e2018-02-22 15:26:27 -080017#include <utility> // for std::pair
18
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "absl/memory/memory.h"
20#include "api/array_view.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080021#include "api/candidate.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "p2p/base/p2p_constants.h"
23#include "p2p/base/p2p_transport_channel.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080024#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080025#include "rtc_base/copy_on_write_buffer.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080026#include "rtc_base/logging.h"
Zhi Huang365381f2018-04-13 16:44:34 -070027#include "rtc_base/strings/string_builder.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080028
29using webrtc::SdpType;
30
31namespace cricket {
32
33static bool VerifyIceParams(const JsepTransportDescription& jsep_description) {
34 // For legacy protocols.
35 // TODO(zhihuang): Remove this once the legacy protocol is no longer
36 // supported.
37 if (jsep_description.transport_desc.ice_ufrag.empty() &&
38 jsep_description.transport_desc.ice_pwd.empty()) {
39 return true;
40 }
41
42 if (jsep_description.transport_desc.ice_ufrag.length() <
43 ICE_UFRAG_MIN_LENGTH ||
44 jsep_description.transport_desc.ice_ufrag.length() >
45 ICE_UFRAG_MAX_LENGTH) {
46 return false;
47 }
48 if (jsep_description.transport_desc.ice_pwd.length() < ICE_PWD_MIN_LENGTH ||
49 jsep_description.transport_desc.ice_pwd.length() > ICE_PWD_MAX_LENGTH) {
50 return false;
51 }
52 return true;
53}
54
55JsepTransportDescription::JsepTransportDescription() {}
56
57JsepTransportDescription::JsepTransportDescription(
58 bool rtcp_mux_enabled,
59 const std::vector<CryptoParams>& cryptos,
60 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -070061 int rtp_abs_sendtime_extn_id,
Zhi Huange818b6e2018-02-22 15:26:27 -080062 const TransportDescription& transport_desc)
63 : rtcp_mux_enabled(rtcp_mux_enabled),
64 cryptos(cryptos),
65 encrypted_header_extension_ids(encrypted_header_extension_ids),
Zhi Huange830e682018-03-30 10:48:35 -070066 rtp_abs_sendtime_extn_id(rtp_abs_sendtime_extn_id),
Zhi Huange818b6e2018-02-22 15:26:27 -080067 transport_desc(transport_desc) {}
68
69JsepTransportDescription::JsepTransportDescription(
70 const JsepTransportDescription& from)
71 : rtcp_mux_enabled(from.rtcp_mux_enabled),
72 cryptos(from.cryptos),
73 encrypted_header_extension_ids(from.encrypted_header_extension_ids),
Zhi Huange830e682018-03-30 10:48:35 -070074 rtp_abs_sendtime_extn_id(from.rtp_abs_sendtime_extn_id),
Zhi Huange818b6e2018-02-22 15:26:27 -080075 transport_desc(from.transport_desc) {}
76
77JsepTransportDescription::~JsepTransportDescription() = default;
78
79JsepTransportDescription& JsepTransportDescription::operator=(
80 const JsepTransportDescription& from) {
81 if (this == &from) {
82 return *this;
83 }
84 rtcp_mux_enabled = from.rtcp_mux_enabled;
85 cryptos = from.cryptos;
86 encrypted_header_extension_ids = from.encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 10:48:35 -070087 rtp_abs_sendtime_extn_id = from.rtp_abs_sendtime_extn_id;
Zhi Huange818b6e2018-02-22 15:26:27 -080088 transport_desc = from.transport_desc;
89
90 return *this;
91}
92
Zhi Huang365381f2018-04-13 16:44:34 -070093JsepTransport::JsepTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -080094 const std::string& mid,
95 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
96 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
97 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
98 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
99 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
Anton Sukhanov7940da02018-10-10 10:34:49 -0700100 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport,
101 std::unique_ptr<webrtc::MediaTransportInterface> media_transport)
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200102 : network_thread_(rtc::Thread::Current()),
103 mid_(mid),
Zhi Huange818b6e2018-02-22 15:26:27 -0800104 local_certificate_(local_certificate),
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200105 unencrypted_rtp_transport_(std::move(unencrypted_rtp_transport)),
106 sdes_transport_(std::move(sdes_transport)),
107 dtls_srtp_transport_(std::move(dtls_srtp_transport)),
Harald Alvestrandad88c882018-11-28 16:47:46 +0100108 rtp_dtls_transport_(
109 rtp_dtls_transport ? new rtc::RefCountedObject<webrtc::DtlsTransport>(
110 std::move(rtp_dtls_transport))
111 : nullptr),
112 rtcp_dtls_transport_(
113 rtcp_dtls_transport
114 ? new rtc::RefCountedObject<webrtc::DtlsTransport>(
115 std::move(rtcp_dtls_transport))
116 : nullptr),
Anton Sukhanov7940da02018-10-10 10:34:49 -0700117 media_transport_(std::move(media_transport)) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800118 RTC_DCHECK(rtp_dtls_transport_);
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200119 // Verify the "only one out of these three can be set" invariant.
120 if (unencrypted_rtp_transport_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800121 RTC_DCHECK(!sdes_transport);
122 RTC_DCHECK(!dtls_srtp_transport);
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200123 } else if (sdes_transport_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800124 RTC_DCHECK(!unencrypted_rtp_transport);
125 RTC_DCHECK(!dtls_srtp_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800126 } else {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200127 RTC_DCHECK(dtls_srtp_transport_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800128 RTC_DCHECK(!unencrypted_rtp_transport);
129 RTC_DCHECK(!sdes_transport);
Zhi Huange818b6e2018-02-22 15:26:27 -0800130 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700131
132 if (media_transport_) {
133 media_transport_->SetMediaTransportStateCallback(this);
134 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800135}
136
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700137JsepTransport::~JsepTransport() {
138 if (media_transport_) {
139 media_transport_->SetMediaTransportStateCallback(nullptr);
Anton Sukhanovd644feb2018-12-06 09:00:39 -0800140
141 // Make sure we delete media transport before ICE.
142 media_transport_.reset();
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700143 }
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100144 // Clear all DtlsTransports. There may be pointers to these from
145 // other places, so we can't assume they'll be deleted by the destructor.
Harald Alvestrandd02541e2019-01-03 12:43:28 +0100146 rtp_dtls_transport_->Clear();
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100147 if (rtcp_dtls_transport_) {
Harald Alvestrandd02541e2019-01-03 12:43:28 +0100148 rtcp_dtls_transport_->Clear();
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100149 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700150}
Zhi Huange818b6e2018-02-22 15:26:27 -0800151
Zhi Huang365381f2018-04-13 16:44:34 -0700152webrtc::RTCError JsepTransport::SetLocalJsepTransportDescription(
Zhi Huange818b6e2018-02-22 15:26:27 -0800153 const JsepTransportDescription& jsep_description,
154 SdpType type) {
155 webrtc::RTCError error;
156
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200157 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800158 if (!VerifyIceParams(jsep_description)) {
159 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
160 "Invalid ice-ufrag or ice-pwd length.");
161 }
162
163 if (!SetRtcpMux(jsep_description.rtcp_mux_enabled, type,
164 ContentSource::CS_LOCAL)) {
165 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
166 "Failed to setup RTCP mux.");
167 }
168
169 // If doing SDES, setup the SDES crypto parameters.
170 if (sdes_transport_) {
171 RTC_DCHECK(!unencrypted_rtp_transport_);
172 RTC_DCHECK(!dtls_srtp_transport_);
173 if (!SetSdes(jsep_description.cryptos,
174 jsep_description.encrypted_header_extension_ids, type,
175 ContentSource::CS_LOCAL)) {
176 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
177 "Failed to setup SDES crypto parameters.");
178 }
179 } else if (dtls_srtp_transport_) {
180 RTC_DCHECK(!unencrypted_rtp_transport_);
181 RTC_DCHECK(!sdes_transport_);
182 dtls_srtp_transport_->UpdateRecvEncryptedHeaderExtensionIds(
183 jsep_description.encrypted_header_extension_ids);
184 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800185 bool ice_restarting =
186 local_description_ != nullptr &&
187 IceCredentialsChanged(local_description_->transport_desc.ice_ufrag,
188 local_description_->transport_desc.ice_pwd,
189 jsep_description.transport_desc.ice_ufrag,
190 jsep_description.transport_desc.ice_pwd);
191 local_description_.reset(new JsepTransportDescription(jsep_description));
192
193 rtc::SSLFingerprint* local_fp =
194 local_description_->transport_desc.identity_fingerprint.get();
195
196 if (!local_fp) {
197 local_certificate_ = nullptr;
198 } else {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200199 error = VerifyCertificateFingerprint(local_certificate_, local_fp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800200 if (!error.ok()) {
201 local_description_.reset();
202 return error;
203 }
204 }
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200205 {
206 rtc::CritScope scope(&accessor_lock_);
207 RTC_DCHECK(rtp_dtls_transport_->internal());
208 SetLocalIceParameters(rtp_dtls_transport_->internal()->ice_transport());
Zhi Huange818b6e2018-02-22 15:26:27 -0800209
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200210 if (rtcp_dtls_transport_) {
211 RTC_DCHECK(rtcp_dtls_transport_->internal());
212 SetLocalIceParameters(rtcp_dtls_transport_->internal()->ice_transport());
213 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800214 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800215 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
216 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
217 error = NegotiateAndSetDtlsParameters(type);
218 }
219 if (!error.ok()) {
220 local_description_.reset();
221 return error;
222 }
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200223 {
224 rtc::CritScope scope(&accessor_lock_);
225 if (needs_ice_restart_ && ice_restarting) {
226 needs_ice_restart_ = false;
227 RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag cleared for transport "
228 << mid();
229 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800230 }
231
232 return webrtc::RTCError::OK();
233}
234
Zhi Huang365381f2018-04-13 16:44:34 -0700235webrtc::RTCError JsepTransport::SetRemoteJsepTransportDescription(
Zhi Huange818b6e2018-02-22 15:26:27 -0800236 const JsepTransportDescription& jsep_description,
237 webrtc::SdpType type) {
238 webrtc::RTCError error;
239
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200240 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800241 if (!VerifyIceParams(jsep_description)) {
242 remote_description_.reset();
243 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
244 "Invalid ice-ufrag or ice-pwd length.");
245 }
246
247 if (!SetRtcpMux(jsep_description.rtcp_mux_enabled, type,
248 ContentSource::CS_REMOTE)) {
249 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
250 "Failed to setup RTCP mux.");
251 }
252
253 // If doing SDES, setup the SDES crypto parameters.
254 if (sdes_transport_) {
255 RTC_DCHECK(!unencrypted_rtp_transport_);
256 RTC_DCHECK(!dtls_srtp_transport_);
257 if (!SetSdes(jsep_description.cryptos,
258 jsep_description.encrypted_header_extension_ids, type,
259 ContentSource::CS_REMOTE)) {
260 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
261 "Failed to setup SDES crypto parameters.");
262 }
Zhi Huange830e682018-03-30 10:48:35 -0700263 sdes_transport_->CacheRtpAbsSendTimeHeaderExtension(
264 jsep_description.rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800265 } else if (dtls_srtp_transport_) {
266 RTC_DCHECK(!unencrypted_rtp_transport_);
267 RTC_DCHECK(!sdes_transport_);
268 dtls_srtp_transport_->UpdateSendEncryptedHeaderExtensionIds(
269 jsep_description.encrypted_header_extension_ids);
Zhi Huange830e682018-03-30 10:48:35 -0700270 dtls_srtp_transport_->CacheRtpAbsSendTimeHeaderExtension(
271 jsep_description.rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800272 }
273
274 remote_description_.reset(new JsepTransportDescription(jsep_description));
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200275 RTC_DCHECK(rtp_dtls_transport());
276 SetRemoteIceParameters(rtp_dtls_transport()->ice_transport());
Zhi Huange818b6e2018-02-22 15:26:27 -0800277
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200278 if (rtcp_dtls_transport()) {
279 SetRemoteIceParameters(rtcp_dtls_transport()->ice_transport());
Zhi Huange818b6e2018-02-22 15:26:27 -0800280 }
281
282 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
283 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
284 error = NegotiateAndSetDtlsParameters(SdpType::kOffer);
285 }
286 if (!error.ok()) {
287 remote_description_.reset();
288 return error;
289 }
290 return webrtc::RTCError::OK();
291}
292
Zhi Huang365381f2018-04-13 16:44:34 -0700293webrtc::RTCError JsepTransport::AddRemoteCandidates(
Zhi Huange818b6e2018-02-22 15:26:27 -0800294 const Candidates& candidates) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200295 RTC_DCHECK_RUN_ON(network_thread_);
Henrik Boström5d8f8fa2018-04-13 15:22:50 +0000296 if (!local_description_ || !remote_description_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800297 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_STATE,
298 mid() +
299 " is not ready to use the remote candidate "
Henrik Boström5d8f8fa2018-04-13 15:22:50 +0000300 "because the local or remote description is "
301 "not set.");
Zhi Huange818b6e2018-02-22 15:26:27 -0800302 }
303
304 for (const cricket::Candidate& candidate : candidates) {
305 auto transport =
306 candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP
Harald Alvestrandad88c882018-11-28 16:47:46 +0100307 ? rtp_dtls_transport_
308 : rtcp_dtls_transport_;
Zhi Huange818b6e2018-02-22 15:26:27 -0800309 if (!transport) {
310 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
311 "Candidate has an unknown component: " +
312 candidate.ToString() + " for mid " + mid());
313 }
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100314 RTC_DCHECK(transport->internal() && transport->internal()->ice_transport());
Harald Alvestrandad88c882018-11-28 16:47:46 +0100315 transport->internal()->ice_transport()->AddRemoteCandidate(candidate);
Zhi Huange818b6e2018-02-22 15:26:27 -0800316 }
317 return webrtc::RTCError::OK();
318}
319
Zhi Huang365381f2018-04-13 16:44:34 -0700320void JsepTransport::SetNeedsIceRestartFlag() {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200321 rtc::CritScope scope(&accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800322 if (!needs_ice_restart_) {
323 needs_ice_restart_ = true;
324 RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag set for transport " << mid();
325 }
326}
327
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200328absl::optional<rtc::SSLRole> JsepTransport::GetDtlsRole() const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200329 RTC_DCHECK_RUN_ON(network_thread_);
330 rtc::CritScope scope(&accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800331 RTC_DCHECK(rtp_dtls_transport_);
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100332 RTC_DCHECK(rtp_dtls_transport_->internal());
Zhi Huange818b6e2018-02-22 15:26:27 -0800333 rtc::SSLRole dtls_role;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100334 if (!rtp_dtls_transport_->internal()->GetDtlsRole(&dtls_role)) {
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200335 return absl::optional<rtc::SSLRole>();
Zhi Huange818b6e2018-02-22 15:26:27 -0800336 }
337
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200338 return absl::optional<rtc::SSLRole>(dtls_role);
Zhi Huange818b6e2018-02-22 15:26:27 -0800339}
340
Zhi Huang365381f2018-04-13 16:44:34 -0700341bool JsepTransport::GetStats(TransportStats* stats) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200342 RTC_DCHECK_RUN_ON(network_thread_);
343 rtc::CritScope scope(&accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800344 stats->transport_name = mid();
345 stats->channel_stats.clear();
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100346 RTC_DCHECK(rtp_dtls_transport_->internal());
Harald Alvestrandad88c882018-11-28 16:47:46 +0100347 bool ret = GetTransportStats(rtp_dtls_transport_->internal(), stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800348 if (rtcp_dtls_transport_) {
Harald Alvestrand628f37a2018-12-06 10:55:20 +0100349 RTC_DCHECK(rtcp_dtls_transport_->internal());
Harald Alvestrandad88c882018-11-28 16:47:46 +0100350 ret &= GetTransportStats(rtcp_dtls_transport_->internal(), stats);
Zhi Huange818b6e2018-02-22 15:26:27 -0800351 }
352 return ret;
353}
354
Zhi Huang365381f2018-04-13 16:44:34 -0700355webrtc::RTCError JsepTransport::VerifyCertificateFingerprint(
Zhi Huange818b6e2018-02-22 15:26:27 -0800356 const rtc::RTCCertificate* certificate,
357 const rtc::SSLFingerprint* fingerprint) const {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200358 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800359 if (!fingerprint) {
360 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
361 "No fingerprint");
362 }
363 if (!certificate) {
364 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
365 "Fingerprint provided but no identity available.");
366 }
Steve Anton4905edb2018-10-15 19:27:44 -0700367 std::unique_ptr<rtc::SSLFingerprint> fp_tmp =
368 rtc::SSLFingerprint::CreateUnique(fingerprint->algorithm,
369 *certificate->identity());
Zhi Huange818b6e2018-02-22 15:26:27 -0800370 RTC_DCHECK(fp_tmp.get() != NULL);
371 if (*fp_tmp == *fingerprint) {
372 return webrtc::RTCError::OK();
373 }
Zhi Huang365381f2018-04-13 16:44:34 -0700374 char ss_buf[1024];
375 rtc::SimpleStringBuilder desc(ss_buf);
Zhi Huange818b6e2018-02-22 15:26:27 -0800376 desc << "Local fingerprint does not match identity. Expected: ";
377 desc << fp_tmp->ToString();
378 desc << " Got: " << fingerprint->ToString();
Zhi Huang365381f2018-04-13 16:44:34 -0700379 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
380 std::string(desc.str()));
Zhi Huange818b6e2018-02-22 15:26:27 -0800381}
382
Zhi Huangb57e1692018-06-12 11:41:11 -0700383void JsepTransport::SetActiveResetSrtpParams(bool active_reset_srtp_params) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200384 RTC_DCHECK_RUN_ON(network_thread_);
385 rtc::CritScope scope(&accessor_lock_);
Zhi Huangb57e1692018-06-12 11:41:11 -0700386 if (dtls_srtp_transport_) {
387 RTC_LOG(INFO)
388 << "Setting active_reset_srtp_params of DtlsSrtpTransport to: "
389 << active_reset_srtp_params;
390 dtls_srtp_transport_->SetActiveResetSrtpParams(active_reset_srtp_params);
391 }
392}
393
Zhi Huang365381f2018-04-13 16:44:34 -0700394void JsepTransport::SetLocalIceParameters(IceTransportInternal* ice_transport) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200395 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800396 RTC_DCHECK(ice_transport);
397 RTC_DCHECK(local_description_);
398 ice_transport->SetIceParameters(
399 local_description_->transport_desc.GetIceParameters());
400}
401
Zhi Huang365381f2018-04-13 16:44:34 -0700402void JsepTransport::SetRemoteIceParameters(
Zhi Huange818b6e2018-02-22 15:26:27 -0800403 IceTransportInternal* ice_transport) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200404 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800405 RTC_DCHECK(ice_transport);
406 RTC_DCHECK(remote_description_);
407 ice_transport->SetRemoteIceParameters(
408 remote_description_->transport_desc.GetIceParameters());
409 ice_transport->SetRemoteIceMode(remote_description_->transport_desc.ice_mode);
410}
411
Zhi Huang365381f2018-04-13 16:44:34 -0700412webrtc::RTCError JsepTransport::SetNegotiatedDtlsParameters(
Zhi Huange818b6e2018-02-22 15:26:27 -0800413 DtlsTransportInternal* dtls_transport,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200414 absl::optional<rtc::SSLRole> dtls_role,
Zhi Huange818b6e2018-02-22 15:26:27 -0800415 rtc::SSLFingerprint* remote_fingerprint) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200416 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800417 RTC_DCHECK(dtls_transport);
418 // Set SSL role. Role must be set before fingerprint is applied, which
419 // initiates DTLS setup.
420 if (dtls_role && !dtls_transport->SetDtlsRole(*dtls_role)) {
421 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
422 "Failed to set SSL role for the transport.");
423 }
424 // Apply remote fingerprint.
425 if (!remote_fingerprint ||
426 !dtls_transport->SetRemoteFingerprint(
Amit Hilbuche7a5f7b2019-03-12 11:10:27 -0700427 remote_fingerprint->algorithm, remote_fingerprint->digest.cdata(),
Zhi Huange818b6e2018-02-22 15:26:27 -0800428 remote_fingerprint->digest.size())) {
429 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
430 "Failed to apply remote fingerprint.");
431 }
432 return webrtc::RTCError::OK();
433}
434
Zhi Huang365381f2018-04-13 16:44:34 -0700435bool JsepTransport::SetRtcpMux(bool enable,
436 webrtc::SdpType type,
437 ContentSource source) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200438 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800439 bool ret = false;
440 switch (type) {
441 case SdpType::kOffer:
442 ret = rtcp_mux_negotiator_.SetOffer(enable, source);
443 break;
444 case SdpType::kPrAnswer:
445 // This may activate RTCP muxing, but we don't yet destroy the transport
446 // because the final answer may deactivate it.
447 ret = rtcp_mux_negotiator_.SetProvisionalAnswer(enable, source);
448 break;
449 case SdpType::kAnswer:
450 ret = rtcp_mux_negotiator_.SetAnswer(enable, source);
451 if (ret && rtcp_mux_negotiator_.IsActive()) {
452 ActivateRtcpMux();
453 }
454 break;
455 default:
456 RTC_NOTREACHED();
457 }
458
459 if (!ret) {
460 return false;
461 }
462
463 auto transport = rtp_transport();
464 transport->SetRtcpMuxEnabled(rtcp_mux_negotiator_.IsActive());
465 return ret;
466}
467
Zhi Huang365381f2018-04-13 16:44:34 -0700468void JsepTransport::ActivateRtcpMux() {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200469 {
470 // Don't hold the network_thread_ lock while calling other functions,
471 // since they might call other functions that call RTC_DCHECK_RUN_ON.
472 // TODO(https://crbug.com/webrtc/10318): Simplify when possible.
473 RTC_DCHECK_RUN_ON(network_thread_);
474 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800475 if (unencrypted_rtp_transport_) {
476 RTC_DCHECK(!sdes_transport_);
477 RTC_DCHECK(!dtls_srtp_transport_);
478 unencrypted_rtp_transport_->SetRtcpPacketTransport(nullptr);
479 } else if (sdes_transport_) {
480 RTC_DCHECK(!unencrypted_rtp_transport_);
481 RTC_DCHECK(!dtls_srtp_transport_);
482 sdes_transport_->SetRtcpPacketTransport(nullptr);
483 } else {
484 RTC_DCHECK(dtls_srtp_transport_);
485 RTC_DCHECK(!unencrypted_rtp_transport_);
486 RTC_DCHECK(!sdes_transport_);
487 dtls_srtp_transport_->SetDtlsTransports(rtp_dtls_transport(),
488 /*rtcp_dtls_transport=*/nullptr);
489 }
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200490 {
491 rtc::CritScope scope(&accessor_lock_);
492 rtcp_dtls_transport_ = nullptr; // Destroy this reference.
493 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800494 // Notify the JsepTransportController to update the aggregate states.
495 SignalRtcpMuxActive();
496}
497
Zhi Huang365381f2018-04-13 16:44:34 -0700498bool JsepTransport::SetSdes(const std::vector<CryptoParams>& cryptos,
499 const std::vector<int>& encrypted_extension_ids,
500 webrtc::SdpType type,
501 ContentSource source) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200502 RTC_DCHECK_RUN_ON(network_thread_);
503 rtc::CritScope scope(&accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800504 bool ret = false;
505 ret = sdes_negotiator_.Process(cryptos, type, source);
506 if (!ret) {
507 return ret;
508 }
509
510 if (source == ContentSource::CS_LOCAL) {
511 recv_extension_ids_ = std::move(encrypted_extension_ids);
512 } else {
513 send_extension_ids_ = std::move(encrypted_extension_ids);
514 }
515
516 // If setting an SDES answer succeeded, apply the negotiated parameters
517 // to the SRTP transport.
518 if ((type == SdpType::kPrAnswer || type == SdpType::kAnswer) && ret) {
519 if (sdes_negotiator_.send_cipher_suite() &&
520 sdes_negotiator_.recv_cipher_suite()) {
521 RTC_DCHECK(send_extension_ids_);
522 RTC_DCHECK(recv_extension_ids_);
523 ret = sdes_transport_->SetRtpParams(
524 *(sdes_negotiator_.send_cipher_suite()),
525 sdes_negotiator_.send_key().data(),
526 static_cast<int>(sdes_negotiator_.send_key().size()),
527 *(send_extension_ids_), *(sdes_negotiator_.recv_cipher_suite()),
528 sdes_negotiator_.recv_key().data(),
529 static_cast<int>(sdes_negotiator_.recv_key().size()),
530 *(recv_extension_ids_));
531 } else {
532 RTC_LOG(LS_INFO) << "No crypto keys are provided for SDES.";
533 if (type == SdpType::kAnswer) {
534 // Explicitly reset the |sdes_transport_| if no crypto param is
535 // provided in the answer. No need to call |ResetParams()| for
536 // |sdes_negotiator_| because it resets the params inside |SetAnswer|.
537 sdes_transport_->ResetParams();
538 }
539 }
540 }
541 return ret;
542}
543
Zhi Huang365381f2018-04-13 16:44:34 -0700544webrtc::RTCError JsepTransport::NegotiateAndSetDtlsParameters(
Zhi Huange818b6e2018-02-22 15:26:27 -0800545 SdpType local_description_type) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200546 RTC_DCHECK_RUN_ON(network_thread_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800547 if (!local_description_ || !remote_description_) {
548 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_STATE,
549 "Applying an answer transport description "
550 "without applying any offer.");
551 }
552 std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint;
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200553 absl::optional<rtc::SSLRole> negotiated_dtls_role;
Zhi Huange818b6e2018-02-22 15:26:27 -0800554
555 rtc::SSLFingerprint* local_fp =
556 local_description_->transport_desc.identity_fingerprint.get();
557 rtc::SSLFingerprint* remote_fp =
558 remote_description_->transport_desc.identity_fingerprint.get();
559 if (remote_fp && local_fp) {
Karl Wiberg918f50c2018-07-05 11:40:33 +0200560 remote_fingerprint = absl::make_unique<rtc::SSLFingerprint>(*remote_fp);
Zhi Huange818b6e2018-02-22 15:26:27 -0800561 webrtc::RTCError error =
562 NegotiateDtlsRole(local_description_type,
563 local_description_->transport_desc.connection_role,
564 remote_description_->transport_desc.connection_role,
565 &negotiated_dtls_role);
566 if (!error.ok()) {
567 return error;
568 }
569 } else if (local_fp && (local_description_type == SdpType::kAnswer)) {
570 return webrtc::RTCError(
571 webrtc::RTCErrorType::INVALID_PARAMETER,
572 "Local fingerprint supplied when caller didn't offer DTLS.");
573 } else {
574 // We are not doing DTLS
Steve Anton4905edb2018-10-15 19:27:44 -0700575 remote_fingerprint = absl::make_unique<rtc::SSLFingerprint>(
576 "", rtc::ArrayView<const uint8_t>());
Zhi Huange818b6e2018-02-22 15:26:27 -0800577 }
578 // Now that we have negotiated everything, push it downward.
579 // Note that we cache the result so that if we have race conditions
580 // between future SetRemote/SetLocal invocations and new transport
581 // creation, we have the negotiation state saved until a new
582 // negotiation happens.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200583 RTC_DCHECK(rtp_dtls_transport());
Zhi Huange818b6e2018-02-22 15:26:27 -0800584 webrtc::RTCError error = SetNegotiatedDtlsParameters(
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200585 rtp_dtls_transport(), negotiated_dtls_role, remote_fingerprint.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800586 if (!error.ok()) {
587 return error;
588 }
589
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200590 if (rtcp_dtls_transport()) {
591 error = SetNegotiatedDtlsParameters(
592 rtcp_dtls_transport(), negotiated_dtls_role, remote_fingerprint.get());
Zhi Huange818b6e2018-02-22 15:26:27 -0800593 }
594 return error;
595}
596
Zhi Huang365381f2018-04-13 16:44:34 -0700597webrtc::RTCError JsepTransport::NegotiateDtlsRole(
Zhi Huange818b6e2018-02-22 15:26:27 -0800598 SdpType local_description_type,
599 ConnectionRole local_connection_role,
600 ConnectionRole remote_connection_role,
Danil Chapovalov66cadcc2018-06-19 16:47:43 +0200601 absl::optional<rtc::SSLRole>* negotiated_dtls_role) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800602 // From RFC 4145, section-4.1, The following are the values that the
603 // 'setup' attribute can take in an offer/answer exchange:
604 // Offer Answer
605 // ________________
606 // active passive / holdconn
607 // passive active / holdconn
608 // actpass active / passive / holdconn
609 // holdconn holdconn
610 //
611 // Set the role that is most conformant with RFC 5763, Section 5, bullet 1
612 // The endpoint MUST use the setup attribute defined in [RFC4145].
613 // The endpoint that is the offerer MUST use the setup attribute
614 // value of setup:actpass and be prepared to receive a client_hello
615 // before it receives the answer. The answerer MUST use either a
616 // setup attribute value of setup:active or setup:passive. Note that
617 // if the answerer uses setup:passive, then the DTLS handshake will
618 // not begin until the answerer is received, which adds additional
619 // latency. setup:active allows the answer and the DTLS handshake to
620 // occur in parallel. Thus, setup:active is RECOMMENDED. Whichever
621 // party is active MUST initiate a DTLS handshake by sending a
622 // ClientHello over each flow (host/port quartet).
623 // IOW - actpass and passive modes should be treated as server and
624 // active as client.
625 bool is_remote_server = false;
626 if (local_description_type == SdpType::kOffer) {
627 if (local_connection_role != CONNECTIONROLE_ACTPASS) {
628 return webrtc::RTCError(
629 webrtc::RTCErrorType::INVALID_PARAMETER,
630 "Offerer must use actpass value for setup attribute.");
631 }
632
633 if (remote_connection_role == CONNECTIONROLE_ACTIVE ||
634 remote_connection_role == CONNECTIONROLE_PASSIVE ||
635 remote_connection_role == CONNECTIONROLE_NONE) {
636 is_remote_server = (remote_connection_role == CONNECTIONROLE_PASSIVE);
637 } else {
638 return webrtc::RTCError(
639 webrtc::RTCErrorType::INVALID_PARAMETER,
640 "Answerer must use either active or passive value "
641 "for setup attribute.");
642 }
643 // If remote is NONE or ACTIVE it will act as client.
644 } else {
645 if (remote_connection_role != CONNECTIONROLE_ACTPASS &&
646 remote_connection_role != CONNECTIONROLE_NONE) {
647 // Accept a remote role attribute that's not "actpass", but matches the
648 // current negotiated role. This is allowed by dtls-sdp, though our
649 // implementation will never generate such an offer as it's not
650 // recommended.
651 //
652 // See https://datatracker.ietf.org/doc/html/draft-ietf-mmusic-dtls-sdp,
653 // section 5.5.
654 auto current_dtls_role = GetDtlsRole();
655 if (!current_dtls_role ||
656 (*current_dtls_role == rtc::SSL_CLIENT &&
657 remote_connection_role == CONNECTIONROLE_ACTIVE) ||
658 (*current_dtls_role == rtc::SSL_SERVER &&
659 remote_connection_role == CONNECTIONROLE_PASSIVE)) {
660 return webrtc::RTCError(
661 webrtc::RTCErrorType::INVALID_PARAMETER,
662 "Offerer must use actpass value or current negotiated role for "
663 "setup attribute.");
664 }
665 }
666
667 if (local_connection_role == CONNECTIONROLE_ACTIVE ||
668 local_connection_role == CONNECTIONROLE_PASSIVE) {
669 is_remote_server = (local_connection_role == CONNECTIONROLE_ACTIVE);
670 } else {
671 return webrtc::RTCError(
672 webrtc::RTCErrorType::INVALID_PARAMETER,
673 "Answerer must use either active or passive value "
674 "for setup attribute.");
675 }
676
677 // If local is passive, local will act as server.
678 }
679
Mirko Bonadei05cf6be2019-01-31 21:38:12 +0100680 *negotiated_dtls_role =
681 (is_remote_server ? rtc::SSL_CLIENT : rtc::SSL_SERVER);
Zhi Huange818b6e2018-02-22 15:26:27 -0800682 return webrtc::RTCError::OK();
683}
684
Zhi Huang365381f2018-04-13 16:44:34 -0700685bool JsepTransport::GetTransportStats(DtlsTransportInternal* dtls_transport,
686 TransportStats* stats) {
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200687 RTC_DCHECK_RUN_ON(network_thread_);
688 rtc::CritScope scope(&accessor_lock_);
Zhi Huange818b6e2018-02-22 15:26:27 -0800689 RTC_DCHECK(dtls_transport);
690 TransportChannelStats substats;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100691 if (rtcp_dtls_transport_) {
692 substats.component = dtls_transport == rtcp_dtls_transport_->internal()
693 ? ICE_CANDIDATE_COMPONENT_RTCP
694 : ICE_CANDIDATE_COMPONENT_RTP;
695 } else {
696 substats.component = ICE_CANDIDATE_COMPONENT_RTP;
697 }
Zhi Huange818b6e2018-02-22 15:26:27 -0800698 dtls_transport->GetSrtpCryptoSuite(&substats.srtp_crypto_suite);
699 dtls_transport->GetSslCipherSuite(&substats.ssl_cipher_suite);
700 substats.dtls_state = dtls_transport->dtls_state();
701 if (!dtls_transport->ice_transport()->GetStats(
702 &substats.connection_infos, &substats.candidate_stats_list)) {
703 return false;
704 }
705 stats->channel_stats.push_back(substats);
706 return true;
707}
708
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700709void JsepTransport::OnStateChanged(webrtc::MediaTransportState state) {
710 // TODO(bugs.webrtc.org/9719) This method currently fires on the network
711 // thread, but media transport does not make such guarantees. We need to make
712 // sure this callback is guaranteed to be executed on the network thread.
Harald Alvestrand78a5e962019-04-03 10:42:39 +0200713 RTC_DCHECK_RUN_ON(network_thread_);
714 {
715 rtc::CritScope scope(&accessor_lock_);
716 media_transport_state_ = state;
717 }
Piotr (Peter) Slatala4eb41122018-11-01 07:26:03 -0700718 SignalMediaTransportStateChanged();
719}
720
Zhi Huange818b6e2018-02-22 15:26:27 -0800721} // namespace cricket