| 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 "pc/jsep_transport.h" |
| 12 |
| 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
| 15 |
| 16 | #include <functional> |
| 17 | #include <memory> |
| 18 | #include <string> |
| 19 | #include <utility> |
| 20 |
| 21 | #include "api/array_view.h" |
| 22 | #include "api/candidate.h" |
| 23 | #include "p2p/base/p2p_constants.h" |
| 24 | #include "p2p/base/p2p_transport_channel.h" |
| 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/copy_on_write_buffer.h" |
| 27 | #include "rtc_base/logging.h" |
| 28 | #include "rtc_base/strings/string_builder.h" |
| 29 | #include "rtc_base/trace_event.h" |
| 30 |
| 31 | using webrtc::SdpType; |
| 32 |
| 33 | namespace cricket { |
| 34 |
| 35 | JsepTransportDescription::JsepTransportDescription() {} |
| 36 |
| 37 | JsepTransportDescription::JsepTransportDescription( |
| 38 | bool rtcp_mux_enabled, |
| 39 | const std::vector<int>& encrypted_header_extension_ids, |
| 40 | int rtp_abs_sendtime_extn_id, |
| 41 | const TransportDescription& transport_desc) |
| 42 | : rtcp_mux_enabled(rtcp_mux_enabled), |
| 43 | encrypted_header_extension_ids(encrypted_header_extension_ids), |
| 44 | rtp_abs_sendtime_extn_id(rtp_abs_sendtime_extn_id), |
| 45 | transport_desc(transport_desc) {} |
| 46 |
| 47 | JsepTransportDescription::JsepTransportDescription( |
| 48 | const JsepTransportDescription& from) |
| 49 | : rtcp_mux_enabled(from.rtcp_mux_enabled), |
| 50 | encrypted_header_extension_ids(from.encrypted_header_extension_ids), |
| 51 | rtp_abs_sendtime_extn_id(from.rtp_abs_sendtime_extn_id), |
| 52 | transport_desc(from.transport_desc) {} |
| 53 |
| 54 | JsepTransportDescription::~JsepTransportDescription() = default; |
| 55 |
| 56 | JsepTransportDescription& JsepTransportDescription::operator=( |
| 57 | const JsepTransportDescription& from) { |
| 58 | if (this == &from) { |
| 59 | return *this; |
| 60 | } |
| 61 | rtcp_mux_enabled = from.rtcp_mux_enabled; |
| 62 | encrypted_header_extension_ids = from.encrypted_header_extension_ids; |
| 63 | rtp_abs_sendtime_extn_id = from.rtp_abs_sendtime_extn_id; |
| 64 | transport_desc = from.transport_desc; |
| 65 |
| 66 | return *this; |
| 67 | } |
| 68 |
| 69 | JsepTransport::JsepTransport( |
| 70 | const std::string& mid, |
| 71 | const rtc::scoped_refptr<rtc::RTCCertificate>& local_certificate, |
| 72 | rtc::scoped_refptr<webrtc::IceTransportInterface> ice_transport, |
| 73 | rtc::scoped_refptr<webrtc::IceTransportInterface> rtcp_ice_transport, |
| 74 | std::unique_ptr<webrtc::RtpTransport> unencrypted_rtp_transport, |
| 75 | std::unique_ptr<webrtc::SrtpTransport> sdes_transport, |
| 76 | std::unique_ptr<webrtc::DtlsSrtpTransport> dtls_srtp_transport, |
| 77 | std::unique_ptr<DtlsTransportInternal> rtp_dtls_transport, |
| 78 | std::unique_ptr<DtlsTransportInternal> rtcp_dtls_transport, |
| 79 | std::unique_ptr<SctpTransportInternal> sctp_transport, |
| 80 | std::function<void()> rtcp_mux_active_callback) |
| 81 | : network_thread_(rtc::Thread::Current()), |
| 82 | mid_(mid), |
| 83 | local_certificate_(local_certificate), |
| 84 | ice_transport_(std::move(ice_transport)), |
| 85 | rtcp_ice_transport_(std::move(rtcp_ice_transport)), |
| 86 | unencrypted_rtp_transport_(std::move(unencrypted_rtp_transport)), |
| 87 | sdes_transport_(std::move(sdes_transport)), |
| 88 | dtls_srtp_transport_(std::move(dtls_srtp_transport)), |
| 89 | rtp_dtls_transport_(rtp_dtls_transport |
| 90 | ? rtc::make_ref_counted<webrtc::DtlsTransport>( |
| 91 | std::move(rtp_dtls_transport)) |
| 92 | : nullptr), |
| 93 | rtcp_dtls_transport_(rtcp_dtls_transport |
| 94 | ? rtc::make_ref_counted<webrtc::DtlsTransport>( |
| 95 | std::move(rtcp_dtls_transport)) |
| 96 | : nullptr), |
| 97 | sctp_transport_(sctp_transport |
| 98 | ? rtc::make_ref_counted<webrtc::SctpTransport>( |
| 99 | std::move(sctp_transport)) |
| 100 | : nullptr), |
| 101 | rtcp_mux_active_callback_(std::move(rtcp_mux_active_callback)) { |
| 102 | TRACE_EVENT0("webrtc", "JsepTransport::JsepTransport"); |
| 103 | RTC_DCHECK(ice_transport_); |
| 104 | RTC_DCHECK(rtp_dtls_transport_); |
| 105 | // `rtcp_ice_transport_` must be present iff `rtcp_dtls_transport_` is |
| 106 | // present. |
| 107 | RTC_DCHECK_EQ((rtcp_ice_transport_ != nullptr), |
| 108 | (rtcp_dtls_transport_ != nullptr)); |
| 109 | // Verify the "only one out of these three can be set" invariant. |
| 110 | if (unencrypted_rtp_transport_) { |
| 111 | RTC_DCHECK(!sdes_transport); |
| 112 | RTC_DCHECK(!dtls_srtp_transport); |
| 113 | } else if (sdes_transport_) { |
| 114 | RTC_DCHECK(!unencrypted_rtp_transport); |
| 115 | RTC_DCHECK(!dtls_srtp_transport); |
| 116 | } else { |
| 117 | RTC_DCHECK(dtls_srtp_transport_); |
| 118 | RTC_DCHECK(!unencrypted_rtp_transport); |
| 119 | RTC_DCHECK(!sdes_transport); |
| 120 | } |
| 121 |
| 122 | if (sctp_transport_) { |
| 123 | sctp_transport_->SetDtlsTransport(rtp_dtls_transport_); |
| 124 | } |
| 125 | } |
| 126 |
| 127 | JsepTransport::~JsepTransport() { |
| 128 | TRACE_EVENT0("webrtc", "JsepTransport::~JsepTransport"); |
| 129 | if (sctp_transport_) { |
| 130 | sctp_transport_->Clear(); |
| 131 | } |
| 132 |
| 133 | // Clear all DtlsTransports. There may be pointers to these from |
| 134 | // other places, so we can't assume they'll be deleted by the destructor. |
| 135 | rtp_dtls_transport_->Clear(); |
| 136 | if (rtcp_dtls_transport_) { |
| 137 | rtcp_dtls_transport_->Clear(); |
| 138 | } |
| 139 |
| 140 | // ICE will be the last transport to be deleted. |
| 141 | } |
| 142 |
| 143 | webrtc::RTCError JsepTransport::SetLocalJsepTransportDescription( |
| 144 | const JsepTransportDescription& jsep_description, |
| 145 | SdpType type) { |
| 146 | webrtc::RTCError error; |
| 147 | TRACE_EVENT0("webrtc", "JsepTransport::SetLocalJsepTransportDescription"); |
| 148 | RTC_DCHECK_RUN_ON(network_thread_); |
| 149 |
| 150 | IceParameters ice_parameters = |
| 151 | jsep_description.transport_desc.GetIceParameters(); |
| 152 | webrtc::RTCError ice_parameters_result = ice_parameters.Validate(); |
| 153 | if (!ice_parameters_result.ok()) { |
| 154 | rtc::StringBuilder sb; |
| 155 | sb << "Invalid ICE parameters: " << ice_parameters_result.message(); |
| 156 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 157 | sb.Release()); |
| 158 | } |
| 159 |
| 160 | if (!SetRtcpMux(jsep_description.rtcp_mux_enabled, type, |
| 161 | ContentSource::CS_LOCAL)) { |
| 162 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 163 | "Failed to setup RTCP mux."); |
| 164 | } |
| 165 |
| 166 | if (dtls_srtp_transport_) { |
| 167 | RTC_DCHECK(!unencrypted_rtp_transport_); |
| 168 | RTC_DCHECK(!sdes_transport_); |
| 169 | dtls_srtp_transport_->UpdateRecvEncryptedHeaderExtensionIds( |
| 170 | jsep_description.encrypted_header_extension_ids); |
| 171 | } |
| 172 | bool ice_restarting = |
| 173 | local_description_ != nullptr && |
| 174 | IceCredentialsChanged(local_description_->transport_desc.ice_ufrag, |
| 175 | local_description_->transport_desc.ice_pwd, |
| 176 | ice_parameters.ufrag, ice_parameters.pwd); |
| 177 | local_description_.reset(new JsepTransportDescription(jsep_description)); |
| 178 |
| 179 | rtc::SSLFingerprint* local_fp = |
| 180 | local_description_->transport_desc.identity_fingerprint.get(); |
| 181 |
| 182 | if (!local_fp) { |
| 183 | local_certificate_ = nullptr; |
| 184 | } else { |
| 185 | error = VerifyCertificateFingerprint(local_certificate_.get(), local_fp); |
| 186 | if (!error.ok()) { |
| 187 | local_description_.reset(); |
| 188 | return error; |
| 189 | } |
| 190 | } |
| 191 | RTC_DCHECK(rtp_dtls_transport_->internal()); |
| 192 | rtp_dtls_transport_->internal()->ice_transport()->SetIceParameters( |
| 193 | ice_parameters); |
| 194 |
| 195 | if (rtcp_dtls_transport_) { |
| 196 | RTC_DCHECK(rtcp_dtls_transport_->internal()); |
| 197 | rtcp_dtls_transport_->internal()->ice_transport()->SetIceParameters( |
| 198 | ice_parameters); |
| 199 | } |
| 200 | // If PRANSWER/ANSWER is set, we should decide transport protocol type. |
| 201 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 202 | error = NegotiateAndSetDtlsParameters(type); |
| 203 | } |
| 204 | if (!error.ok()) { |
| 205 | local_description_.reset(); |
| 206 | return error; |
| 207 | } |
| 208 |
| 209 | if (needs_ice_restart_ && ice_restarting) { |
| 210 | needs_ice_restart_ = false; |
| 211 | RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag cleared for transport " |
| 212 | << mid(); |
| 213 | } |
| 214 |
| 215 | return webrtc::RTCError::OK(); |
| 216 | } |
| 217 |
| 218 | webrtc::RTCError JsepTransport::SetRemoteJsepTransportDescription( |
| 219 | const JsepTransportDescription& jsep_description, |
| 220 | webrtc::SdpType type) { |
| 221 | TRACE_EVENT0("webrtc", "JsepTransport::SetLocalJsepTransportDescription"); |
| 222 | webrtc::RTCError error; |
| 223 |
| 224 | RTC_DCHECK_RUN_ON(network_thread_); |
| 225 |
| 226 | IceParameters ice_parameters = |
| 227 | jsep_description.transport_desc.GetIceParameters(); |
| 228 | webrtc::RTCError ice_parameters_result = ice_parameters.Validate(); |
| 229 | if (!ice_parameters_result.ok()) { |
| 230 | remote_description_.reset(); |
| 231 | rtc::StringBuilder sb; |
| 232 | sb << "Invalid ICE parameters: " << ice_parameters_result.message(); |
| 233 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 234 | sb.Release()); |
| 235 | } |
| 236 |
| 237 | if (!SetRtcpMux(jsep_description.rtcp_mux_enabled, type, |
| 238 | ContentSource::CS_REMOTE)) { |
| 239 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 240 | "Failed to setup RTCP mux."); |
| 241 | } |
| 242 |
| 243 | if (dtls_srtp_transport_) { |
| 244 | RTC_DCHECK(!unencrypted_rtp_transport_); |
| 245 | RTC_DCHECK(!sdes_transport_); |
| 246 | dtls_srtp_transport_->UpdateSendEncryptedHeaderExtensionIds( |
| 247 | jsep_description.encrypted_header_extension_ids); |
| 248 | dtls_srtp_transport_->CacheRtpAbsSendTimeHeaderExtension( |
| 249 | jsep_description.rtp_abs_sendtime_extn_id); |
| 250 | } |
| 251 |
| 252 | remote_description_.reset(new JsepTransportDescription(jsep_description)); |
| 253 | RTC_DCHECK(rtp_dtls_transport()); |
| 254 | SetRemoteIceParameters(ice_parameters, rtp_dtls_transport()->ice_transport()); |
| 255 |
| 256 | if (rtcp_dtls_transport()) { |
| 257 | SetRemoteIceParameters(ice_parameters, |
| 258 | rtcp_dtls_transport()->ice_transport()); |
| 259 | } |
| 260 |
| 261 | // If PRANSWER/ANSWER is set, we should decide transport protocol type. |
| 262 | if (type == SdpType::kPrAnswer || type == SdpType::kAnswer) { |
| 263 | error = NegotiateAndSetDtlsParameters(SdpType::kOffer); |
| 264 | } |
| 265 | if (!error.ok()) { |
| 266 | remote_description_.reset(); |
| 267 | return error; |
| 268 | } |
| 269 | return webrtc::RTCError::OK(); |
| 270 | } |
| 271 |
| 272 | webrtc::RTCError JsepTransport::AddRemoteCandidates( |
| 273 | const Candidates& candidates) { |
| 274 | RTC_DCHECK_RUN_ON(network_thread_); |
| 275 | if (!local_description_ || !remote_description_) { |
| 276 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_STATE, |
| 277 | mid() + |
| 278 | " is not ready to use the remote candidate " |
| 279 | "because the local or remote description is " |
| 280 | "not set."); |
| 281 | } |
| 282 |
| 283 | for (const cricket::Candidate& candidate : candidates) { |
| 284 | auto transport = |
| 285 | candidate.component() == cricket::ICE_CANDIDATE_COMPONENT_RTP |
| 286 | ? rtp_dtls_transport_ |
| 287 | : rtcp_dtls_transport_; |
| 288 | if (!transport) { |
| 289 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 290 | "Candidate has an unknown component: " + |
| 291 | candidate.ToSensitiveString() + " for mid " + |
| 292 | mid()); |
| 293 | } |
| 294 | RTC_DCHECK(transport->internal() && transport->internal()->ice_transport()); |
| 295 | transport->internal()->ice_transport()->AddRemoteCandidate(candidate); |
| 296 | } |
| 297 | return webrtc::RTCError::OK(); |
| 298 | } |
| 299 |
| 300 | void JsepTransport::SetNeedsIceRestartFlag() { |
| 301 | RTC_DCHECK_RUN_ON(network_thread_); |
| 302 | if (!needs_ice_restart_) { |
| 303 | needs_ice_restart_ = true; |
| 304 | RTC_LOG(LS_VERBOSE) << "needs-ice-restart flag set for transport " << mid(); |
| 305 | } |
| 306 | } |
| 307 |
| 308 | absl::optional<rtc::SSLRole> JsepTransport::GetDtlsRole() const { |
| 309 | RTC_DCHECK_RUN_ON(network_thread_); |
| 310 | RTC_DCHECK(rtp_dtls_transport_); |
| 311 | RTC_DCHECK(rtp_dtls_transport_->internal()); |
| 312 | rtc::SSLRole dtls_role; |
| 313 | if (!rtp_dtls_transport_->internal()->GetDtlsRole(&dtls_role)) { |
| 314 | return absl::optional<rtc::SSLRole>(); |
| 315 | } |
| 316 |
| 317 | return absl::optional<rtc::SSLRole>(dtls_role); |
| 318 | } |
| 319 |
| 320 | bool JsepTransport::GetStats(TransportStats* stats) { |
| 321 | TRACE_EVENT0("webrtc", "JsepTransport::GetStats"); |
| 322 | RTC_DCHECK_RUN_ON(network_thread_); |
| 323 | stats->transport_name = mid(); |
| 324 | stats->channel_stats.clear(); |
| 325 | RTC_DCHECK(rtp_dtls_transport_->internal()); |
| 326 | bool ret = GetTransportStats(rtp_dtls_transport_->internal(), |
| 327 | ICE_CANDIDATE_COMPONENT_RTP, stats); |
| 328 |
| 329 | if (rtcp_dtls_transport_) { |
| 330 | RTC_DCHECK(rtcp_dtls_transport_->internal()); |
| 331 | ret &= GetTransportStats(rtcp_dtls_transport_->internal(), |
| 332 | ICE_CANDIDATE_COMPONENT_RTCP, stats); |
| 333 | } |
| 334 | return ret; |
| 335 | } |
| 336 |
| 337 | webrtc::RTCError JsepTransport::VerifyCertificateFingerprint( |
| 338 | const rtc::RTCCertificate* certificate, |
| 339 | const rtc::SSLFingerprint* fingerprint) const { |
| 340 | TRACE_EVENT0("webrtc", "JsepTransport::VerifyCertificateFingerprint"); |
| 341 | RTC_DCHECK_RUN_ON(network_thread_); |
| 342 | if (!fingerprint) { |
| 343 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 344 | "No fingerprint"); |
| 345 | } |
| 346 | if (!certificate) { |
| 347 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 348 | "Fingerprint provided but no identity available."); |
| 349 | } |
| 350 | std::unique_ptr<rtc::SSLFingerprint> fp_tmp = |
| 351 | rtc::SSLFingerprint::CreateUnique(fingerprint->algorithm, |
| 352 | *certificate->identity()); |
| 353 | RTC_DCHECK(fp_tmp.get() != NULL); |
| 354 | if (*fp_tmp == *fingerprint) { |
| 355 | return webrtc::RTCError::OK(); |
| 356 | } |
| 357 | char ss_buf[1024]; |
| 358 | rtc::SimpleStringBuilder desc(ss_buf); |
| 359 | desc << "Local fingerprint does not match identity. Expected: "; |
| 360 | desc << fp_tmp->ToString(); |
| 361 | desc << " Got: " << fingerprint->ToString(); |
| 362 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_PARAMETER, |
| 363 | std::string(desc.str())); |
| 364 | } |
| 365 |
| 366 | void JsepTransport::SetActiveResetSrtpParams(bool active_reset_srtp_params) { |
| 367 | RTC_DCHECK_RUN_ON(network_thread_); |
| 368 | if (dtls_srtp_transport_) { |
| 369 | RTC_LOG(LS_INFO) |
| 370 | << "Setting active_reset_srtp_params of DtlsSrtpTransport to: " |
| 371 | << active_reset_srtp_params; |
| 372 | dtls_srtp_transport_->SetActiveResetSrtpParams(active_reset_srtp_params); |
| 373 | } |
| 374 | } |
| 375 |
| 376 | void JsepTransport::SetRemoteIceParameters( |
| 377 | const IceParameters& ice_parameters, |
| 378 | IceTransportInternal* ice_transport) { |
| 379 | TRACE_EVENT0("webrtc", "JsepTransport::SetRemoteIceParameters"); |
| 380 | RTC_DCHECK_RUN_ON(network_thread_); |
| 381 | RTC_DCHECK(ice_transport); |
| 382 | RTC_DCHECK(remote_description_); |
| 383 | ice_transport->SetRemoteIceParameters(ice_parameters); |
| 384 | ice_transport->SetRemoteIceMode(remote_description_->transport_desc.ice_mode); |
| 385 | } |
| 386 |
| 387 | webrtc::RTCError JsepTransport::SetNegotiatedDtlsParameters( |
| 388 | DtlsTransportInternal* dtls_transport, |
| 389 | absl::optional<rtc::SSLRole> dtls_role, |
| 390 | rtc::SSLFingerprint* remote_fingerprint) { |
| 391 | RTC_DCHECK(dtls_transport); |
| 392 | return dtls_transport->SetRemoteParameters( |
| 393 | remote_fingerprint->algorithm, remote_fingerprint->digest.cdata(), |
| 394 | remote_fingerprint->digest.size(), dtls_role); |
| 395 | } |
| 396 |
| 397 | bool JsepTransport::SetRtcpMux(bool enable, |
| 398 | webrtc::SdpType type, |
| 399 | ContentSource source) { |
| 400 | RTC_DCHECK_RUN_ON(network_thread_); |
| 401 | bool ret = false; |
| 402 | switch (type) { |
| 403 | case SdpType::kOffer: |
| 404 | ret = rtcp_mux_negotiator_.SetOffer(enable, source); |
| 405 | break; |
| 406 | case SdpType::kPrAnswer: |
| 407 | // This may activate RTCP muxing, but we don't yet destroy the transport |
| 408 | // because the final answer may deactivate it. |
| 409 | ret = rtcp_mux_negotiator_.SetProvisionalAnswer(enable, source); |
| 410 | break; |
| 411 | case SdpType::kAnswer: |
| 412 | ret = rtcp_mux_negotiator_.SetAnswer(enable, source); |
| 413 | if (ret && rtcp_mux_negotiator_.IsActive()) { |
| 414 | ActivateRtcpMux(); |
| 415 | } |
| 416 | break; |
| 417 | default: |
| 418 | RTC_DCHECK_NOTREACHED(); |
| 419 | } |
| 420 |
| 421 | if (!ret) { |
| 422 | return false; |
| 423 | } |
| 424 |
| 425 | auto transport = rtp_transport(); |
| 426 | transport->SetRtcpMuxEnabled(rtcp_mux_negotiator_.IsActive()); |
| 427 | return ret; |
| 428 | } |
| 429 |
| 430 | void JsepTransport::ActivateRtcpMux() { |
| 431 | if (unencrypted_rtp_transport_) { |
| 432 | RTC_DCHECK(!sdes_transport_); |
| 433 | RTC_DCHECK(!dtls_srtp_transport_); |
| 434 | unencrypted_rtp_transport_->SetRtcpPacketTransport(nullptr); |
| 435 | } else if (sdes_transport_) { |
| 436 | RTC_DCHECK(!unencrypted_rtp_transport_); |
| 437 | RTC_DCHECK(!dtls_srtp_transport_); |
| 438 | sdes_transport_->SetRtcpPacketTransport(nullptr); |
| 439 | } else if (dtls_srtp_transport_) { |
| 440 | RTC_DCHECK(dtls_srtp_transport_); |
| 441 | RTC_DCHECK(!unencrypted_rtp_transport_); |
| 442 | RTC_DCHECK(!sdes_transport_); |
| 443 | dtls_srtp_transport_->SetDtlsTransports(rtp_dtls_transport(), |
| 444 | /*rtcp_dtls_transport=*/nullptr); |
| 445 | } |
| 446 | rtcp_dtls_transport_ = nullptr; // Destroy this reference. |
| 447 | // Notify the JsepTransportController to update the aggregate states. |
| 448 | rtcp_mux_active_callback_(); |
| 449 | } |
| 450 |
| 451 | webrtc::RTCError JsepTransport::NegotiateAndSetDtlsParameters( |
| 452 | SdpType local_description_type) { |
| 453 | RTC_DCHECK_RUN_ON(network_thread_); |
| 454 | if (!local_description_ || !remote_description_) { |
| 455 | return webrtc::RTCError(webrtc::RTCErrorType::INVALID_STATE, |
| 456 | "Applying an answer transport description " |
| 457 | "without applying any offer."); |
| 458 | } |
| 459 | std::unique_ptr<rtc::SSLFingerprint> remote_fingerprint; |
| 460 | absl::optional<rtc::SSLRole> negotiated_dtls_role; |
| 461 |
| 462 | rtc::SSLFingerprint* local_fp = |
| 463 | local_description_->transport_desc.identity_fingerprint.get(); |
| 464 | rtc::SSLFingerprint* remote_fp = |
| 465 | remote_description_->transport_desc.identity_fingerprint.get(); |
| 466 | if (remote_fp && local_fp) { |
| 467 | remote_fingerprint = std::make_unique<rtc::SSLFingerprint>(*remote_fp); |
| 468 | webrtc::RTCError error = |
| 469 | NegotiateDtlsRole(local_description_type, |
| 470 | local_description_->transport_desc.connection_role, |
| 471 | remote_description_->transport_desc.connection_role, |
| 472 | &negotiated_dtls_role); |
| 473 | if (!error.ok()) { |
| 474 | return error; |
| 475 | } |
| 476 | } else if (local_fp && (local_description_type == SdpType::kAnswer)) { |
| 477 | return webrtc::RTCError( |
| 478 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 479 | "Local fingerprint supplied when caller didn't offer DTLS."); |
| 480 | } else { |
| 481 | // We are not doing DTLS |
| 482 | remote_fingerprint = std::make_unique<rtc::SSLFingerprint>( |
| 483 | "", rtc::ArrayView<const uint8_t>()); |
| 484 | } |
| 485 | // Now that we have negotiated everything, push it downward. |
| 486 | // Note that we cache the result so that if we have race conditions |
| 487 | // between future SetRemote/SetLocal invocations and new transport |
| 488 | // creation, we have the negotiation state saved until a new |
| 489 | // negotiation happens. |
| 490 | RTC_DCHECK(rtp_dtls_transport()); |
| 491 | webrtc::RTCError error = SetNegotiatedDtlsParameters( |
| 492 | rtp_dtls_transport(), negotiated_dtls_role, remote_fingerprint.get()); |
| 493 | if (!error.ok()) { |
| 494 | return error; |
| 495 | } |
| 496 |
| 497 | if (rtcp_dtls_transport()) { |
| 498 | error = SetNegotiatedDtlsParameters( |
| 499 | rtcp_dtls_transport(), negotiated_dtls_role, remote_fingerprint.get()); |
| 500 | } |
| 501 | return error; |
| 502 | } |
| 503 |
| 504 | webrtc::RTCError JsepTransport::NegotiateDtlsRole( |
| 505 | SdpType local_description_type, |
| 506 | ConnectionRole local_connection_role, |
| 507 | ConnectionRole remote_connection_role, |
| 508 | absl::optional<rtc::SSLRole>* negotiated_dtls_role) { |
| 509 | // From RFC 4145, section-4.1, The following are the values that the |
| 510 | // 'setup' attribute can take in an offer/answer exchange: |
| 511 | // Offer Answer |
| 512 | // ________________ |
| 513 | // active passive / holdconn |
| 514 | // passive active / holdconn |
| 515 | // actpass active / passive / holdconn |
| 516 | // holdconn holdconn |
| 517 | // |
| 518 | // Set the role that is most conformant with RFC 5763, Section 5, bullet 1 |
| 519 | // The endpoint MUST use the setup attribute defined in [RFC4145]. |
| 520 | // The endpoint that is the offerer MUST use the setup attribute |
| 521 | // value of setup:actpass and be prepared to receive a client_hello |
| 522 | // before it receives the answer. The answerer MUST use either a |
| 523 | // setup attribute value of setup:active or setup:passive. Note that |
| 524 | // if the answerer uses setup:passive, then the DTLS handshake will |
| 525 | // not begin until the answerer is received, which adds additional |
| 526 | // latency. setup:active allows the answer and the DTLS handshake to |
| 527 | // occur in parallel. Thus, setup:active is RECOMMENDED. Whichever |
| 528 | // party is active MUST initiate a DTLS handshake by sending a |
| 529 | // ClientHello over each flow (host/port quartet). |
| 530 | // IOW - actpass and passive modes should be treated as server and |
| 531 | // active as client. |
| 532 | // RFC 8842 section 5.3 updates this text, so that it is mandated |
| 533 | // for the responder to handle offers with "active" and "passive" |
| 534 | // as well as "actpass" |
| 535 | bool is_remote_server = false; |
| 536 | if (local_description_type == SdpType::kOffer) { |
| 537 | if (local_connection_role != CONNECTIONROLE_ACTPASS) { |
| 538 | return webrtc::RTCError( |
| 539 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 540 | "Offerer must use actpass value for setup attribute."); |
| 541 | } |
| 542 |
| 543 | if (remote_connection_role == CONNECTIONROLE_ACTIVE || |
| 544 | remote_connection_role == CONNECTIONROLE_PASSIVE || |
| 545 | remote_connection_role == CONNECTIONROLE_NONE) { |
| 546 | is_remote_server = (remote_connection_role == CONNECTIONROLE_PASSIVE); |
| 547 | } else { |
| 548 | return webrtc::RTCError( |
| 549 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 550 | "Answerer must use either active or passive value " |
| 551 | "for setup attribute."); |
| 552 | } |
| 553 | // If remote is NONE or ACTIVE it will act as client. |
| 554 | } else { |
| 555 | if (remote_connection_role != CONNECTIONROLE_ACTPASS && |
| 556 | remote_connection_role != CONNECTIONROLE_NONE) { |
| 557 | // Accept a remote role attribute that's not "actpass", but matches the |
| 558 | // current negotiated role. This is allowed by dtls-sdp, though our |
| 559 | // implementation will never generate such an offer as it's not |
| 560 | // recommended. |
| 561 | // |
| 562 | // See https://datatracker.ietf.org/doc/html/draft-ietf-mmusic-dtls-sdp, |
| 563 | // section 5.5. |
| 564 | auto current_dtls_role = GetDtlsRole(); |
| 565 | if (!current_dtls_role) { |
| 566 | // Role not assigned yet. Verify that local role fits with remote role. |
| 567 | switch (remote_connection_role) { |
| 568 | case CONNECTIONROLE_ACTIVE: |
| 569 | if (local_connection_role != CONNECTIONROLE_PASSIVE) { |
| 570 | return webrtc::RTCError( |
| 571 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 572 | "Answerer must be passive when offerer is active"); |
| 573 | } |
| 574 | break; |
| 575 | case CONNECTIONROLE_PASSIVE: |
| 576 | if (local_connection_role != CONNECTIONROLE_ACTIVE) { |
| 577 | return webrtc::RTCError( |
| 578 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 579 | "Answerer must be active when offerer is passive"); |
| 580 | } |
| 581 | break; |
| 582 | default: |
| 583 | RTC_DCHECK_NOTREACHED(); |
| 584 | break; |
| 585 | } |
| 586 | } else { |
| 587 | if ((*current_dtls_role == rtc::SSL_CLIENT && |
| 588 | remote_connection_role == CONNECTIONROLE_ACTIVE) || |
| 589 | (*current_dtls_role == rtc::SSL_SERVER && |
| 590 | remote_connection_role == CONNECTIONROLE_PASSIVE)) { |
| 591 | return webrtc::RTCError( |
| 592 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 593 | "Offerer must use current negotiated role for " |
| 594 | "setup attribute."); |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 |
| 599 | if (local_connection_role == CONNECTIONROLE_ACTIVE || |
| 600 | local_connection_role == CONNECTIONROLE_PASSIVE) { |
| 601 | is_remote_server = (local_connection_role == CONNECTIONROLE_ACTIVE); |
| 602 | } else { |
| 603 | return webrtc::RTCError( |
| 604 | webrtc::RTCErrorType::INVALID_PARAMETER, |
| 605 | "Answerer must use either active or passive value " |
| 606 | "for setup attribute."); |
| 607 | } |
| 608 |
| 609 | // If local is passive, local will act as server. |
| 610 | } |
| 611 |
| 612 | *negotiated_dtls_role = |
| 613 | (is_remote_server ? rtc::SSL_CLIENT : rtc::SSL_SERVER); |
| 614 | return webrtc::RTCError::OK(); |
| 615 | } |
| 616 |
| 617 | bool JsepTransport::GetTransportStats(DtlsTransportInternal* dtls_transport, |
| 618 | int component, |
| 619 | TransportStats* stats) { |
| 620 | RTC_DCHECK_RUN_ON(network_thread_); |
| 621 | RTC_DCHECK(dtls_transport); |
| 622 | TransportChannelStats substats; |
| 623 | substats.component = component; |
| 624 | dtls_transport->GetSslVersionBytes(&substats.ssl_version_bytes); |
| 625 | dtls_transport->GetSrtpCryptoSuite(&substats.srtp_crypto_suite); |
| 626 | dtls_transport->GetSslCipherSuite(&substats.ssl_cipher_suite); |
| 627 | substats.dtls_state = dtls_transport->dtls_state(); |
| 628 | rtc::SSLRole dtls_role; |
| 629 | if (dtls_transport->GetDtlsRole(&dtls_role)) { |
| 630 | substats.dtls_role = dtls_role; |
| 631 | } |
| 632 | if (!dtls_transport->ice_transport()->GetStats( |
| 633 | &substats.ice_transport_stats)) { |
| 634 | return false; |
| 635 | } |
| 636 | substats.ssl_peer_signature_algorithm = |
| 637 | dtls_transport->GetSslPeerSignatureAlgorithm(); |
| 638 | stats->channel_stats.push_back(substats); |
| 639 | return true; |
| 640 | } |
| 641 |
| 642 | } // namespace cricket |
| 643 |