blob: ab785affb173a3c82739b39d9cb44834a567a778 [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
Zhi Huang365381f2018-04-13 16:44:34 -070011#include "pc/jseptransport.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080012
13#include <memory>
14#include <utility> // for std::pair
15
16#include "api/candidate.h"
17#include "p2p/base/p2pconstants.h"
18#include "p2p/base/p2ptransportchannel.h"
19#include "p2p/base/port.h"
20#include "rtc_base/bind.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/ptr_util.h"
Zhi Huang365381f2018-04-13 16:44:34 -070024#include "rtc_base/strings/string_builder.h"
Zhi Huange818b6e2018-02-22 15:26:27 -080025
26using webrtc::SdpType;
27
28namespace cricket {
29
30static bool VerifyIceParams(const JsepTransportDescription& jsep_description) {
31 // For legacy protocols.
32 // TODO(zhihuang): Remove this once the legacy protocol is no longer
33 // supported.
34 if (jsep_description.transport_desc.ice_ufrag.empty() &&
35 jsep_description.transport_desc.ice_pwd.empty()) {
36 return true;
37 }
38
39 if (jsep_description.transport_desc.ice_ufrag.length() <
40 ICE_UFRAG_MIN_LENGTH ||
41 jsep_description.transport_desc.ice_ufrag.length() >
42 ICE_UFRAG_MAX_LENGTH) {
43 return false;
44 }
45 if (jsep_description.transport_desc.ice_pwd.length() < ICE_PWD_MIN_LENGTH ||
46 jsep_description.transport_desc.ice_pwd.length() > ICE_PWD_MAX_LENGTH) {
47 return false;
48 }
49 return true;
50}
51
52JsepTransportDescription::JsepTransportDescription() {}
53
54JsepTransportDescription::JsepTransportDescription(
55 bool rtcp_mux_enabled,
56 const std::vector<CryptoParams>& cryptos,
57 const std::vector<int>& encrypted_header_extension_ids,
Zhi Huange830e682018-03-30 10:48:35 -070058 int rtp_abs_sendtime_extn_id,
Zhi Huange818b6e2018-02-22 15:26:27 -080059 const TransportDescription& transport_desc)
60 : rtcp_mux_enabled(rtcp_mux_enabled),
61 cryptos(cryptos),
62 encrypted_header_extension_ids(encrypted_header_extension_ids),
Zhi Huange830e682018-03-30 10:48:35 -070063 rtp_abs_sendtime_extn_id(rtp_abs_sendtime_extn_id),
Zhi Huange818b6e2018-02-22 15:26:27 -080064 transport_desc(transport_desc) {}
65
66JsepTransportDescription::JsepTransportDescription(
67 const JsepTransportDescription& from)
68 : rtcp_mux_enabled(from.rtcp_mux_enabled),
69 cryptos(from.cryptos),
70 encrypted_header_extension_ids(from.encrypted_header_extension_ids),
Zhi Huange830e682018-03-30 10:48:35 -070071 rtp_abs_sendtime_extn_id(from.rtp_abs_sendtime_extn_id),
Zhi Huange818b6e2018-02-22 15:26:27 -080072 transport_desc(from.transport_desc) {}
73
74JsepTransportDescription::~JsepTransportDescription() = default;
75
76JsepTransportDescription& JsepTransportDescription::operator=(
77 const JsepTransportDescription& from) {
78 if (this == &from) {
79 return *this;
80 }
81 rtcp_mux_enabled = from.rtcp_mux_enabled;
82 cryptos = from.cryptos;
83 encrypted_header_extension_ids = from.encrypted_header_extension_ids;
Zhi Huange830e682018-03-30 10:48:35 -070084 rtp_abs_sendtime_extn_id = from.rtp_abs_sendtime_extn_id;
Zhi Huange818b6e2018-02-22 15:26:27 -080085 transport_desc = from.transport_desc;
86
87 return *this;
88}
89
Zhi Huang365381f2018-04-13 16:44:34 -070090JsepTransport::JsepTransport(
Zhi Huange818b6e2018-02-22 15:26:27 -080091 const std::string& mid,
92 const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate,
93 std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport,
94 std::unique_ptr<webrtc::SrtpTransport> sdes_transport,
95 std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport,
96 std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport,
97 std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport)
98 : mid_(mid),
99 local_certificate_(local_certificate),
100 rtp_dtls_transport_(std::move(rtp_dtls_transport)),
101 rtcp_dtls_transport_(std::move(rtcp_dtls_transport)) {
102 RTC_DCHECK(rtp_dtls_transport_);
103 if (unencrypted_rtp_transport) {
104 RTC_DCHECK(!sdes_transport);
105 RTC_DCHECK(!dtls_srtp_transport);
106 unencrypted_rtp_transport_ = std::move(unencrypted_rtp_transport);
107 } else if (sdes_transport) {
108 RTC_DCHECK(!unencrypted_rtp_transport);
109 RTC_DCHECK(!dtls_srtp_transport);
110 sdes_transport_ = std::move(sdes_transport);
111 } else {
112 RTC_DCHECK(dtls_srtp_transport);
113 RTC_DCHECK(!unencrypted_rtp_transport);
114 RTC_DCHECK(!sdes_transport);
115 dtls_srtp_transport_ = std::move(dtls_srtp_transport);
116 }
117}
118
Zhi Huang365381f2018-04-13 16:44:34 -0700119JsepTransport::~JsepTransport() {}
Zhi Huange818b6e2018-02-22 15:26:27 -0800120
Zhi Huang365381f2018-04-13 16:44:34 -0700121webrtc::RTCError JsepTransport::SetLocalJsepTransportDescription(
Zhi Huange818b6e2018-02-22 15:26:27 -0800122 const JsepTransportDescription& jsep_description,
123 SdpType type) {
124 webrtc::RTCError error;
125
126 if (!VerifyIceParams(jsep_description)) {
127 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
128 "Invalid ice-ufrag or ice-pwd length.");
129 }
130
131 if (!SetRtcpMux(jsep_description.rtcp_mux_enabled, type,
132 ContentSource::CS_LOCAL)) {
133 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
134 "Failed to setup RTCP mux.");
135 }
136
137 // If doing SDES, setup the SDES crypto parameters.
138 if (sdes_transport_) {
139 RTC_DCHECK(!unencrypted_rtp_transport_);
140 RTC_DCHECK(!dtls_srtp_transport_);
141 if (!SetSdes(jsep_description.cryptos,
142 jsep_description.encrypted_header_extension_ids, type,
143 ContentSource::CS_LOCAL)) {
144 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
145 "Failed to setup SDES crypto parameters.");
146 }
147 } else if (dtls_srtp_transport_) {
148 RTC_DCHECK(!unencrypted_rtp_transport_);
149 RTC_DCHECK(!sdes_transport_);
150 dtls_srtp_transport_->UpdateRecvEncryptedHeaderExtensionIds(
151 jsep_description.encrypted_header_extension_ids);
152 }
153
154 bool ice_restarting =
155 local_description_ != nullptr &&
156 IceCredentialsChanged(local_description_->transport_desc.ice_ufrag,
157 local_description_->transport_desc.ice_pwd,
158 jsep_description.transport_desc.ice_ufrag,
159 jsep_description.transport_desc.ice_pwd);
160 local_description_.reset(new JsepTransportDescription(jsep_description));
161
162 rtc::SSLFingerprint* local_fp =
163 local_description_->transport_desc.identity_fingerprint.get();
164
165 if (!local_fp) {
166 local_certificate_ = nullptr;
167 } else {
168 error = VerifyCertificateFingerprint(local_certificate_.get(), local_fp);
169 if (!error.ok()) {
170 local_description_.reset();
171 return error;
172 }
173 }
174
175 SetLocalIceParameters(rtp_dtls_transport_->ice_transport());
176
177 if (rtcp_dtls_transport_) {
178 SetLocalIceParameters(rtcp_dtls_transport_->ice_transport());
179 }
180
181 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
182 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
183 error = NegotiateAndSetDtlsParameters(type);
184 }
185 if (!error.ok()) {
186 local_description_.reset();
187 return error;
188 }
189
190 if (needs_ice_restart_ && ice_restarting) {
191 needs_ice_restart_ = false;
192 RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag cleared for transport "
193 << mid();
194 }
195
196 return webrtc::RTCError::OK();
197}
198
Zhi Huang365381f2018-04-13 16:44:34 -0700199webrtc::RTCError JsepTransport::SetRemoteJsepTransportDescription(
Zhi Huange818b6e2018-02-22 15:26:27 -0800200 const JsepTransportDescription& jsep_description,
201 webrtc::SdpType type) {
202 webrtc::RTCError error;
203
204 if (!VerifyIceParams(jsep_description)) {
205 remote_description_.reset();
206 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
207 "Invalid ice-ufrag or ice-pwd length.");
208 }
209
210 if (!SetRtcpMux(jsep_description.rtcp_mux_enabled, type,
211 ContentSource::CS_REMOTE)) {
212 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
213 "Failed to setup RTCP mux.");
214 }
215
216 // If doing SDES, setup the SDES crypto parameters.
217 if (sdes_transport_) {
218 RTC_DCHECK(!unencrypted_rtp_transport_);
219 RTC_DCHECK(!dtls_srtp_transport_);
220 if (!SetSdes(jsep_description.cryptos,
221 jsep_description.encrypted_header_extension_ids, type,
222 ContentSource::CS_REMOTE)) {
223 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
224 "Failed to setup SDES crypto parameters.");
225 }
Zhi Huange830e682018-03-30 10:48:35 -0700226 sdes_transport_->CacheRtpAbsSendTimeHeaderExtension(
227 jsep_description.rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800228 } else if (dtls_srtp_transport_) {
229 RTC_DCHECK(!unencrypted_rtp_transport_);
230 RTC_DCHECK(!sdes_transport_);
231 dtls_srtp_transport_->UpdateSendEncryptedHeaderExtensionIds(
232 jsep_description.encrypted_header_extension_ids);
Zhi Huange830e682018-03-30 10:48:35 -0700233 dtls_srtp_transport_->CacheRtpAbsSendTimeHeaderExtension(
234 jsep_description.rtp_abs_sendtime_extn_id);
Zhi Huange818b6e2018-02-22 15:26:27 -0800235 }
236
237 remote_description_.reset(new JsepTransportDescription(jsep_description));
238 SetRemoteIceParameters(rtp_dtls_transport_->ice_transport());
239
240 if (rtcp_dtls_transport_) {
241 SetRemoteIceParameters(rtcp_dtls_transport_->ice_transport());
242 }
243
244 // If PRANSWER/ANSWER is set, we should decide transport protocol type.
245 if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) {
246 error = NegotiateAndSetDtlsParameters(SdpType::kOffer);
247 }
248 if (!error.ok()) {
249 remote_description_.reset();
250 return error;
251 }
252 return webrtc::RTCError::OK();
253}
254
Zhi Huang365381f2018-04-13 16:44:34 -0700255webrtc::RTCError JsepTransport::AddRemoteCandidates(
Zhi Huange818b6e2018-02-22 15:26:27 -0800256 const Candidates& candidates) {
Henrik Boström5d8f8fa2018-04-13 15:22:50 +0000257 if (!local_description_ || !remote_description_) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800258 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_STATE,
259 mid() +
260 " is not ready to use the remote candidate "
Henrik Boström5d8f8fa2018-04-13 15:22:50 +0000261 "because the local or remote description is "
262 "not set.");
Zhi Huange818b6e2018-02-22 15:26:27 -0800263 }
264
265 for (const cricket::Candidate& candidate : candidates) {
266 auto transport =
267 candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP
268 ? rtp_dtls_transport_.get()
269 : rtcp_dtls_transport_.get();
270 if (!transport) {
271 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
272 "Candidate has an unknown component: " +
273 candidate.ToString() + " for mid " + mid());
274 }
275 transport->ice_transport()->AddRemoteCandidate(candidate);
276 }
277 return webrtc::RTCError::OK();
278}
279
Zhi Huang365381f2018-04-13 16:44:34 -0700280void JsepTransport::SetNeedsIceRestartFlag() {
Zhi Huange818b6e2018-02-22 15:26:27 -0800281 if (!needs_ice_restart_) {
282 needs_ice_restart_ = true;
283 RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag set for transport " << mid();
284 }
285}
286
Zhi Huang365381f2018-04-13 16:44:34 -0700287rtc::Optional<rtc::SSLRole> JsepTransport::GetDtlsRole() const {
Zhi Huange818b6e2018-02-22 15:26:27 -0800288 RTC_DCHECK(rtp_dtls_transport_);
289 rtc::SSLRole dtls_role;
290 if (!rtp_dtls_transport_->GetDtlsRole(&dtls_role)) {
291 return rtc::Optional<rtc::SSLRole>();
292 }
293
294 return rtc::Optional<rtc::SSLRole>(dtls_role);
295}
296
Zhi Huang365381f2018-04-13 16:44:34 -0700297bool JsepTransport::GetStats(TransportStats* stats) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800298 stats->transport_name = mid();
299 stats->channel_stats.clear();
300 bool ret = GetTransportStats(rtp_dtls_transport_.get(), stats);
301 if (rtcp_dtls_transport_) {
302 ret &= GetTransportStats(rtcp_dtls_transport_.get(), stats);
303 }
304 return ret;
305}
306
Zhi Huang365381f2018-04-13 16:44:34 -0700307webrtc::RTCError JsepTransport::VerifyCertificateFingerprint(
Zhi Huange818b6e2018-02-22 15:26:27 -0800308 const rtc::RTCCertificate* certificate,
309 const rtc::SSLFingerprint* fingerprint) const {
310 if (!fingerprint) {
311 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
312 "No fingerprint");
313 }
314 if (!certificate) {
315 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
316 "Fingerprint provided but no identity available.");
317 }
318 std::unique_ptr<rtc::SSLFingerprint> fp_tmp(rtc::SSLFingerprint::Create(
319 fingerprint->algorithm, certificate->identity()));
320 RTC_DCHECK(fp_tmp.get() != NULL);
321 if (*fp_tmp == *fingerprint) {
322 return webrtc::RTCError::OK();
323 }
Zhi Huang365381f2018-04-13 16:44:34 -0700324 char ss_buf[1024];
325 rtc::SimpleStringBuilder desc(ss_buf);
Zhi Huange818b6e2018-02-22 15:26:27 -0800326 desc << "Local fingerprint does not match identity. Expected: ";
327 desc << fp_tmp->ToString();
328 desc << " Got: " << fingerprint->ToString();
Zhi Huang365381f2018-04-13 16:44:34 -0700329 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
330 std::string(desc.str()));
Zhi Huange818b6e2018-02-22 15:26:27 -0800331}
332
Zhi Huang365381f2018-04-13 16:44:34 -0700333void JsepTransport::SetLocalIceParameters(IceTransportInternal* ice_transport) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800334 RTC_DCHECK(ice_transport);
335 RTC_DCHECK(local_description_);
336 ice_transport->SetIceParameters(
337 local_description_->transport_desc.GetIceParameters());
338}
339
Zhi Huang365381f2018-04-13 16:44:34 -0700340void JsepTransport::SetRemoteIceParameters(
Zhi Huange818b6e2018-02-22 15:26:27 -0800341 IceTransportInternal* ice_transport) {
342 RTC_DCHECK(ice_transport);
343 RTC_DCHECK(remote_description_);
344 ice_transport->SetRemoteIceParameters(
345 remote_description_->transport_desc.GetIceParameters());
346 ice_transport->SetRemoteIceMode(remote_description_->transport_desc.ice_mode);
347}
348
Zhi Huang365381f2018-04-13 16:44:34 -0700349webrtc::RTCError JsepTransport::SetNegotiatedDtlsParameters(
Zhi Huange818b6e2018-02-22 15:26:27 -0800350 DtlsTransportInternal* dtls_transport,
351 rtc::Optional<rtc::SSLRole> dtls_role,
352 rtc::SSLFingerprint* remote_fingerprint) {
353 RTC_DCHECK(dtls_transport);
354 // Set SSL role. Role must be set before fingerprint is applied, which
355 // initiates DTLS setup.
356 if (dtls_role && !dtls_transport->SetDtlsRole(*dtls_role)) {
357 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
358 "Failed to set SSL role for the transport.");
359 }
360 // Apply remote fingerprint.
361 if (!remote_fingerprint ||
362 !dtls_transport->SetRemoteFingerprint(
363 remote_fingerprint->algorithm,
364 reinterpret_cast<const uint8_t*>(remote_fingerprint->digest.data()),
365 remote_fingerprint->digest.size())) {
366 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER,
367 "Failed to apply remote fingerprint.");
368 }
369 return webrtc::RTCError::OK();
370}
371
Zhi Huang365381f2018-04-13 16:44:34 -0700372bool JsepTransport::SetRtcpMux(bool enable,
373 webrtc::SdpType type,
374 ContentSource source) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800375 bool ret = false;
376 switch (type) {
377 case SdpType::kOffer:
378 ret = rtcp_mux_negotiator_.SetOffer(enable, source);
379 break;
380 case SdpType::kPrAnswer:
381 // This may activate RTCP muxing, but we don't yet destroy the transport
382 // because the final answer may deactivate it.
383 ret = rtcp_mux_negotiator_.SetProvisionalAnswer(enable, source);
384 break;
385 case SdpType::kAnswer:
386 ret = rtcp_mux_negotiator_.SetAnswer(enable, source);
387 if (ret && rtcp_mux_negotiator_.IsActive()) {
388 ActivateRtcpMux();
389 }
390 break;
391 default:
392 RTC_NOTREACHED();
393 }
394
395 if (!ret) {
396 return false;
397 }
398
399 auto transport = rtp_transport();
400 transport->SetRtcpMuxEnabled(rtcp_mux_negotiator_.IsActive());
401 return ret;
402}
403
Zhi Huang365381f2018-04-13 16:44:34 -0700404void JsepTransport::ActivateRtcpMux() {
Zhi Huange818b6e2018-02-22 15:26:27 -0800405 if (unencrypted_rtp_transport_) {
406 RTC_DCHECK(!sdes_transport_);
407 RTC_DCHECK(!dtls_srtp_transport_);
408 unencrypted_rtp_transport_->SetRtcpPacketTransport(nullptr);
409 } else if (sdes_transport_) {
410 RTC_DCHECK(!unencrypted_rtp_transport_);
411 RTC_DCHECK(!dtls_srtp_transport_);
412 sdes_transport_->SetRtcpPacketTransport(nullptr);
413 } else {
414 RTC_DCHECK(dtls_srtp_transport_);
415 RTC_DCHECK(!unencrypted_rtp_transport_);
416 RTC_DCHECK(!sdes_transport_);
417 dtls_srtp_transport_->SetDtlsTransports(rtp_dtls_transport(),
418 /*rtcp_dtls_transport=*/nullptr);
419 }
420 rtcp_dtls_transport_.reset();
421 // Notify the JsepTransportController to update the aggregate states.
422 SignalRtcpMuxActive();
423}
424
Zhi Huang365381f2018-04-13 16:44:34 -0700425bool JsepTransport::SetSdes(const std::vector<CryptoParams>& cryptos,
426 const std::vector<int>& encrypted_extension_ids,
427 webrtc::SdpType type,
428 ContentSource source) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800429 bool ret = false;
430 ret = sdes_negotiator_.Process(cryptos, type, source);
431 if (!ret) {
432 return ret;
433 }
434
435 if (source == ContentSource::CS_LOCAL) {
436 recv_extension_ids_ = std::move(encrypted_extension_ids);
437 } else {
438 send_extension_ids_ = std::move(encrypted_extension_ids);
439 }
440
441 // If setting an SDES answer succeeded, apply the negotiated parameters
442 // to the SRTP transport.
443 if ((type == SdpType::kPrAnswer || type == SdpType::kAnswer) && ret) {
444 if (sdes_negotiator_.send_cipher_suite() &&
445 sdes_negotiator_.recv_cipher_suite()) {
446 RTC_DCHECK(send_extension_ids_);
447 RTC_DCHECK(recv_extension_ids_);
448 ret = sdes_transport_->SetRtpParams(
449 *(sdes_negotiator_.send_cipher_suite()),
450 sdes_negotiator_.send_key().data(),
451 static_cast<int>(sdes_negotiator_.send_key().size()),
452 *(send_extension_ids_), *(sdes_negotiator_.recv_cipher_suite()),
453 sdes_negotiator_.recv_key().data(),
454 static_cast<int>(sdes_negotiator_.recv_key().size()),
455 *(recv_extension_ids_));
456 } else {
457 RTC_LOG(LS_INFO) << "No crypto keys are provided for SDES.";
458 if (type == SdpType::kAnswer) {
459 // Explicitly reset the |sdes_transport_| if no crypto param is
460 // provided in the answer. No need to call |ResetParams()| for
461 // |sdes_negotiator_| because it resets the params inside |SetAnswer|.
462 sdes_transport_->ResetParams();
463 }
464 }
465 }
466 return ret;
467}
468
Zhi Huang365381f2018-04-13 16:44:34 -0700469webrtc::RTCError JsepTransport::NegotiateAndSetDtlsParameters(
Zhi Huange818b6e2018-02-22 15:26:27 -0800470 SdpType local_description_type) {
471 if (!local_description_ || !remote_description_) {
472 return webrtc::RTCError(webrtc::RTCErrorType::INVALID_STATE,
473 "Applying an answer transport description "
474 "without applying any offer.");
475 }
476 std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint;
477 rtc::Optional<rtc::SSLRole> negotiated_dtls_role;
478
479 rtc::SSLFingerprint* local_fp =
480 local_description_->transport_desc.identity_fingerprint.get();
481 rtc::SSLFingerprint* remote_fp =
482 remote_description_->transport_desc.identity_fingerprint.get();
483 if (remote_fp && local_fp) {
484 remote_fingerprint = rtc::MakeUnique<rtc::SSLFingerprint>(*remote_fp);
485 webrtc::RTCError error =
486 NegotiateDtlsRole(local_description_type,
487 local_description_->transport_desc.connection_role,
488 remote_description_->transport_desc.connection_role,
489 &negotiated_dtls_role);
490 if (!error.ok()) {
491 return error;
492 }
493 } else if (local_fp && (local_description_type == SdpType::kAnswer)) {
494 return webrtc::RTCError(
495 webrtc::RTCErrorType::INVALID_PARAMETER,
496 "Local fingerprint supplied when caller didn't offer DTLS.");
497 } else {
498 // We are not doing DTLS
499 remote_fingerprint = rtc::MakeUnique<rtc::SSLFingerprint>("", nullptr, 0);
500 }
501 // Now that we have negotiated everything, push it downward.
502 // Note that we cache the result so that if we have race conditions
503 // between future SetRemote/SetLocal invocations and new transport
504 // creation, we have the negotiation state saved until a new
505 // negotiation happens.
506 webrtc::RTCError error = SetNegotiatedDtlsParameters(
507 rtp_dtls_transport_.get(), negotiated_dtls_role,
508 remote_fingerprint.get());
509 if (!error.ok()) {
510 return error;
511 }
512
513 if (rtcp_dtls_transport_) {
514 error = SetNegotiatedDtlsParameters(rtcp_dtls_transport_.get(),
515 negotiated_dtls_role,
516 remote_fingerprint.get());
517 }
518 return error;
519}
520
Zhi Huang365381f2018-04-13 16:44:34 -0700521webrtc::RTCError JsepTransport::NegotiateDtlsRole(
Zhi Huange818b6e2018-02-22 15:26:27 -0800522 SdpType local_description_type,
523 ConnectionRole local_connection_role,
524 ConnectionRole remote_connection_role,
525 rtc::Optional<rtc::SSLRole>* negotiated_dtls_role) {
526 // From RFC 4145, section-4.1, The following are the values that the
527 // 'setup' attribute can take in an offer/answer exchange:
528 // Offer Answer
529 // ________________
530 // active passive / holdconn
531 // passive active / holdconn
532 // actpass active / passive / holdconn
533 // holdconn holdconn
534 //
535 // Set the role that is most conformant with RFC 5763, Section 5, bullet 1
536 // The endpoint MUST use the setup attribute defined in [RFC4145].
537 // The endpoint that is the offerer MUST use the setup attribute
538 // value of setup:actpass and be prepared to receive a client_hello
539 // before it receives the answer. The answerer MUST use either a
540 // setup attribute value of setup:active or setup:passive. Note that
541 // if the answerer uses setup:passive, then the DTLS handshake will
542 // not begin until the answerer is received, which adds additional
543 // latency. setup:active allows the answer and the DTLS handshake to
544 // occur in parallel. Thus, setup:active is RECOMMENDED. Whichever
545 // party is active MUST initiate a DTLS handshake by sending a
546 // ClientHello over each flow (host/port quartet).
547 // IOW - actpass and passive modes should be treated as server and
548 // active as client.
549 bool is_remote_server = false;
550 if (local_description_type == SdpType::kOffer) {
551 if (local_connection_role != CONNECTIONROLE_ACTPASS) {
552 return webrtc::RTCError(
553 webrtc::RTCErrorType::INVALID_PARAMETER,
554 "Offerer must use actpass value for setup attribute.");
555 }
556
557 if (remote_connection_role == CONNECTIONROLE_ACTIVE ||
558 remote_connection_role == CONNECTIONROLE_PASSIVE ||
559 remote_connection_role == CONNECTIONROLE_NONE) {
560 is_remote_server = (remote_connection_role == CONNECTIONROLE_PASSIVE);
561 } else {
562 return webrtc::RTCError(
563 webrtc::RTCErrorType::INVALID_PARAMETER,
564 "Answerer must use either active or passive value "
565 "for setup attribute.");
566 }
567 // If remote is NONE or ACTIVE it will act as client.
568 } else {
569 if (remote_connection_role != CONNECTIONROLE_ACTPASS &&
570 remote_connection_role != CONNECTIONROLE_NONE) {
571 // Accept a remote role attribute that's not "actpass", but matches the
572 // current negotiated role. This is allowed by dtls-sdp, though our
573 // implementation will never generate such an offer as it's not
574 // recommended.
575 //
576 // See https://datatracker.ietf.org/doc/html/draft-ietf-mmusic-dtls-sdp,
577 // section 5.5.
578 auto current_dtls_role = GetDtlsRole();
579 if (!current_dtls_role ||
580 (*current_dtls_role == rtc::SSL_CLIENT &&
581 remote_connection_role == CONNECTIONROLE_ACTIVE) ||
582 (*current_dtls_role == rtc::SSL_SERVER &&
583 remote_connection_role == CONNECTIONROLE_PASSIVE)) {
584 return webrtc::RTCError(
585 webrtc::RTCErrorType::INVALID_PARAMETER,
586 "Offerer must use actpass value or current negotiated role for "
587 "setup attribute.");
588 }
589 }
590
591 if (local_connection_role == CONNECTIONROLE_ACTIVE ||
592 local_connection_role == CONNECTIONROLE_PASSIVE) {
593 is_remote_server = (local_connection_role == CONNECTIONROLE_ACTIVE);
594 } else {
595 return webrtc::RTCError(
596 webrtc::RTCErrorType::INVALID_PARAMETER,
597 "Answerer must use either active or passive value "
598 "for setup attribute.");
599 }
600
601 // If local is passive, local will act as server.
602 }
603
604 *negotiated_dtls_role = (is_remote_server ? std::move(rtc::SSL_CLIENT)
605 : std::move(rtc::SSL_SERVER));
606 return webrtc::RTCError::OK();
607}
608
Zhi Huang365381f2018-04-13 16:44:34 -0700609bool JsepTransport::GetTransportStats(DtlsTransportInternal* dtls_transport,
610 TransportStats* stats) {
Zhi Huange818b6e2018-02-22 15:26:27 -0800611 RTC_DCHECK(dtls_transport);
612 TransportChannelStats substats;
613 substats.component = dtls_transport == rtcp_dtls_transport_.get()
614 ? ICE_CANDIDATE_COMPONENT_RTCP
615 : ICE_CANDIDATE_COMPONENT_RTP;
616 dtls_transport->GetSrtpCryptoSuite(&substats.srtp_crypto_suite);
617 dtls_transport->GetSslCipherSuite(&substats.ssl_cipher_suite);
618 substats.dtls_state = dtls_transport->dtls_state();
619 if (!dtls_transport->ice_transport()->GetStats(
620 &substats.connection_infos, &substats.candidate_stats_list)) {
621 return false;
622 }
623 stats->channel_stats.push_back(substats);
624 return true;
625}
626
627} // namespace cricket