| 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/dtls_transport.h" |
| 12 |
| 13 | #include <utility> |
| 14 |
| 15 | #include "absl/types/optional.h" |
| 16 | #include "api/dtls_transport_interface.h" |
| 17 | #include "api/make_ref_counted.h" |
| 18 | #include "api/sequence_checker.h" |
| 19 | #include "pc/ice_transport.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
| 22 | #include "rtc_base/ssl_stream_adapter.h" |
| 23 |
| 24 | namespace webrtc { |
| 25 |
| 26 | // Implementation of DtlsTransportInterface |
| 27 | DtlsTransport::DtlsTransport( |
| 28 | std::unique_ptr<cricket::DtlsTransportInternal> internal) |
| 29 | : owner_thread_(rtc::Thread::Current()), |
| 30 | info_(DtlsTransportState::kNew), |
| 31 | internal_dtls_transport_(std::move(internal)), |
| 32 | ice_transport_(rtc::make_ref_counted<IceTransportWithPointer>( |
| 33 | internal_dtls_transport_->ice_transport())) { |
| 34 | RTC_DCHECK(internal_dtls_transport_.get()); |
| 35 | internal_dtls_transport_->SubscribeDtlsTransportState( |
| 36 | [this](cricket::DtlsTransportInternal* transport, |
| 37 | DtlsTransportState state) { |
| 38 | OnInternalDtlsState(transport, state); |
| 39 | }); |
| 40 | UpdateInformation(); |
| 41 | } |
| 42 |
| 43 | DtlsTransport::~DtlsTransport() { |
| 44 | // TODO(tommi): Due to a reference being held by the RtpSenderBase |
| 45 | // implementation, the last reference to the `DtlsTransport` instance can |
| 46 | // be released on the signaling thread. |
| 47 | // RTC_DCHECK_RUN_ON(owner_thread_); |
| 48 |
| 49 | // We depend on the signaling thread to call Clear() before dropping |
| 50 | // its last reference to this object. |
| 51 |
| 52 | // If there are non `owner_thread_` references outstanding, and those |
| 53 | // references are the last ones released, we depend on Clear() having been |
| 54 | // called from the owner_thread before the last reference is deleted. |
| 55 | // `Clear()` is currently called from `JsepTransport::~JsepTransport`. |
| 56 | RTC_DCHECK(owner_thread_->IsCurrent() || !internal_dtls_transport_); |
| 57 | } |
| 58 |
| 59 | DtlsTransportInformation DtlsTransport::Information() { |
| 60 | MutexLock lock(&lock_); |
| 61 | return info_; |
| 62 | } |
| 63 |
| 64 | void DtlsTransport::RegisterObserver(DtlsTransportObserverInterface* observer) { |
| 65 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 66 | RTC_DCHECK(observer); |
| 67 | observer_ = observer; |
| 68 | } |
| 69 |
| 70 | void DtlsTransport::UnregisterObserver() { |
| 71 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 72 | observer_ = nullptr; |
| 73 | } |
| 74 |
| 75 | rtc::scoped_refptr<IceTransportInterface> DtlsTransport::ice_transport() { |
| 76 | return ice_transport_; |
| 77 | } |
| 78 |
| 79 | // Internal functions |
| 80 | void DtlsTransport::Clear() { |
| 81 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 82 | RTC_DCHECK(internal()); |
| 83 | bool must_send_event = |
| 84 | (internal()->dtls_state() != DtlsTransportState::kClosed); |
| 85 | internal_dtls_transport_.reset(); |
| 86 | ice_transport_->Clear(); |
| 87 | UpdateInformation(); |
| 88 | if (observer_ && must_send_event) { |
| 89 | observer_->OnStateChange(Information()); |
| 90 | } |
| 91 | } |
| 92 |
| 93 | void DtlsTransport::OnInternalDtlsState( |
| 94 | cricket::DtlsTransportInternal* transport, |
| 95 | DtlsTransportState state) { |
| 96 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 97 | RTC_DCHECK(transport == internal()); |
| 98 | RTC_DCHECK(state == internal()->dtls_state()); |
| 99 | UpdateInformation(); |
| 100 | if (observer_) { |
| 101 | observer_->OnStateChange(Information()); |
| 102 | } |
| 103 | } |
| 104 |
| 105 | void DtlsTransport::UpdateInformation() { |
| 106 | RTC_DCHECK_RUN_ON(owner_thread_); |
| 107 | if (internal_dtls_transport_) { |
| 108 | if (internal_dtls_transport_->dtls_state() == |
| 109 | DtlsTransportState::kConnected) { |
| 110 | bool success = true; |
| 111 | rtc::SSLRole internal_role; |
| 112 | absl::optional<DtlsTransportTlsRole> role; |
| 113 | int ssl_cipher_suite; |
| 114 | int tls_version; |
| 115 | int srtp_cipher; |
| 116 | success &= internal_dtls_transport_->GetDtlsRole(&internal_role); |
| 117 | if (success) { |
| 118 | switch (internal_role) { |
| 119 | case rtc::SSL_CLIENT: |
| 120 | role = DtlsTransportTlsRole::kClient; |
| 121 | break; |
| 122 | case rtc::SSL_SERVER: |
| 123 | role = DtlsTransportTlsRole::kServer; |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | success &= internal_dtls_transport_->GetSslVersionBytes(&tls_version); |
| 128 | success &= internal_dtls_transport_->GetSslCipherSuite(&ssl_cipher_suite); |
| 129 | success &= internal_dtls_transport_->GetSrtpCryptoSuite(&srtp_cipher); |
| 130 | if (success) { |
| 131 | set_info(DtlsTransportInformation( |
| 132 | internal_dtls_transport_->dtls_state(), role, tls_version, |
| 133 | ssl_cipher_suite, srtp_cipher, |
| 134 | internal_dtls_transport_->GetRemoteSSLCertChain())); |
| 135 | } else { |
| 136 | RTC_LOG(LS_ERROR) << "DtlsTransport in connected state has incomplete " |
| 137 | "TLS information"; |
| 138 | set_info(DtlsTransportInformation( |
| 139 | internal_dtls_transport_->dtls_state(), role, absl::nullopt, |
| 140 | absl::nullopt, absl::nullopt, |
| 141 | internal_dtls_transport_->GetRemoteSSLCertChain())); |
| 142 | } |
| 143 | } else { |
| 144 | set_info( |
| 145 | DtlsTransportInformation(internal_dtls_transport_->dtls_state())); |
| 146 | } |
| 147 | } else { |
| 148 | set_info(DtlsTransportInformation(DtlsTransportState::kClosed)); |
| 149 | } |
| 150 | } |
| 151 |
| 152 | } // namespace webrtc |
| 153 |