2020-08-14 16:58:22 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 The WebRTC project authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pc/dtls_transport.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
2021-06-25 00:43:10 +00:00
|
|
|
#include "absl/types/optional.h"
|
2022-03-11 16:49:54 +00:00
|
|
|
#include "api/dtls_transport_interface.h"
|
2021-06-25 00:43:10 +00:00
|
|
|
#include "api/sequence_checker.h"
|
2020-08-14 16:58:22 +00:00
|
|
|
#include "pc/ice_transport.h"
|
2021-06-25 00:43:10 +00:00
|
|
|
#include "rtc_base/checks.h"
|
|
|
|
#include "rtc_base/logging.h"
|
|
|
|
#include "rtc_base/ref_counted_object.h"
|
2022-03-11 16:49:54 +00:00
|
|
|
#include "rtc_base/ssl_stream_adapter.h"
|
2020-08-14 16:58:22 +00:00
|
|
|
|
|
|
|
namespace webrtc {
|
|
|
|
|
|
|
|
// Implementation of DtlsTransportInterface
|
|
|
|
DtlsTransport::DtlsTransport(
|
|
|
|
std::unique_ptr<cricket::DtlsTransportInternal> internal)
|
|
|
|
: owner_thread_(rtc::Thread::Current()),
|
|
|
|
info_(DtlsTransportState::kNew),
|
|
|
|
internal_dtls_transport_(std::move(internal)),
|
2021-06-25 00:43:10 +00:00
|
|
|
ice_transport_(rtc::make_ref_counted<IceTransportWithPointer>(
|
2020-08-14 16:58:22 +00:00
|
|
|
internal_dtls_transport_->ice_transport())) {
|
|
|
|
RTC_DCHECK(internal_dtls_transport_.get());
|
2022-03-11 16:49:54 +00:00
|
|
|
internal_dtls_transport_->SubscribeDtlsTransportState(
|
2021-06-25 00:43:10 +00:00
|
|
|
[this](cricket::DtlsTransportInternal* transport,
|
2022-03-11 16:49:54 +00:00
|
|
|
DtlsTransportState state) {
|
2021-06-25 00:43:10 +00:00
|
|
|
OnInternalDtlsState(transport, state);
|
|
|
|
});
|
2020-08-14 16:58:22 +00:00
|
|
|
UpdateInformation();
|
|
|
|
}
|
|
|
|
|
|
|
|
DtlsTransport::~DtlsTransport() {
|
|
|
|
// We depend on the signaling thread to call Clear() before dropping
|
|
|
|
// its last reference to this object.
|
|
|
|
RTC_DCHECK(owner_thread_->IsCurrent() || !internal_dtls_transport_);
|
|
|
|
}
|
|
|
|
|
|
|
|
DtlsTransportInformation DtlsTransport::Information() {
|
|
|
|
MutexLock lock(&lock_);
|
|
|
|
return info_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DtlsTransport::RegisterObserver(DtlsTransportObserverInterface* observer) {
|
|
|
|
RTC_DCHECK_RUN_ON(owner_thread_);
|
|
|
|
RTC_DCHECK(observer);
|
|
|
|
observer_ = observer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DtlsTransport::UnregisterObserver() {
|
|
|
|
RTC_DCHECK_RUN_ON(owner_thread_);
|
|
|
|
observer_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtc::scoped_refptr<IceTransportInterface> DtlsTransport::ice_transport() {
|
|
|
|
return ice_transport_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal functions
|
|
|
|
void DtlsTransport::Clear() {
|
|
|
|
RTC_DCHECK_RUN_ON(owner_thread_);
|
|
|
|
RTC_DCHECK(internal());
|
|
|
|
bool must_send_event =
|
2022-03-11 16:49:54 +00:00
|
|
|
(internal()->dtls_state() != DtlsTransportState::kClosed);
|
2020-08-14 16:58:22 +00:00
|
|
|
// The destructor of cricket::DtlsTransportInternal calls back
|
|
|
|
// into DtlsTransport, so we can't hold the lock while releasing.
|
|
|
|
std::unique_ptr<cricket::DtlsTransportInternal> transport_to_release;
|
|
|
|
{
|
|
|
|
MutexLock lock(&lock_);
|
|
|
|
transport_to_release = std::move(internal_dtls_transport_);
|
|
|
|
ice_transport_->Clear();
|
|
|
|
}
|
|
|
|
UpdateInformation();
|
|
|
|
if (observer_ && must_send_event) {
|
|
|
|
observer_->OnStateChange(Information());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DtlsTransport::OnInternalDtlsState(
|
|
|
|
cricket::DtlsTransportInternal* transport,
|
2022-03-11 16:49:54 +00:00
|
|
|
DtlsTransportState state) {
|
2020-08-14 16:58:22 +00:00
|
|
|
RTC_DCHECK_RUN_ON(owner_thread_);
|
|
|
|
RTC_DCHECK(transport == internal());
|
|
|
|
RTC_DCHECK(state == internal()->dtls_state());
|
|
|
|
UpdateInformation();
|
|
|
|
if (observer_) {
|
|
|
|
observer_->OnStateChange(Information());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DtlsTransport::UpdateInformation() {
|
|
|
|
RTC_DCHECK_RUN_ON(owner_thread_);
|
|
|
|
MutexLock lock(&lock_);
|
|
|
|
if (internal_dtls_transport_) {
|
|
|
|
if (internal_dtls_transport_->dtls_state() ==
|
2022-03-11 16:49:54 +00:00
|
|
|
DtlsTransportState::kConnected) {
|
2020-08-14 16:58:22 +00:00
|
|
|
bool success = true;
|
2022-03-11 16:49:54 +00:00
|
|
|
rtc::SSLRole internal_role;
|
|
|
|
absl::optional<DtlsTransportTlsRole> role;
|
2020-08-14 16:58:22 +00:00
|
|
|
int ssl_cipher_suite;
|
|
|
|
int tls_version;
|
|
|
|
int srtp_cipher;
|
2022-03-11 16:49:54 +00:00
|
|
|
success &= internal_dtls_transport_->GetDtlsRole(&internal_role);
|
|
|
|
if (success) {
|
|
|
|
switch (internal_role) {
|
|
|
|
case rtc::SSL_CLIENT:
|
|
|
|
role = DtlsTransportTlsRole::kClient;
|
|
|
|
break;
|
|
|
|
case rtc::SSL_SERVER:
|
|
|
|
role = DtlsTransportTlsRole::kServer;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-08-14 16:58:22 +00:00
|
|
|
success &= internal_dtls_transport_->GetSslVersionBytes(&tls_version);
|
|
|
|
success &= internal_dtls_transport_->GetSslCipherSuite(&ssl_cipher_suite);
|
|
|
|
success &= internal_dtls_transport_->GetSrtpCryptoSuite(&srtp_cipher);
|
|
|
|
if (success) {
|
|
|
|
info_ = DtlsTransportInformation(
|
2022-03-11 16:49:54 +00:00
|
|
|
internal_dtls_transport_->dtls_state(), role, tls_version,
|
2020-08-14 16:58:22 +00:00
|
|
|
ssl_cipher_suite, srtp_cipher,
|
|
|
|
internal_dtls_transport_->GetRemoteSSLCertChain());
|
|
|
|
} else {
|
|
|
|
RTC_LOG(LS_ERROR) << "DtlsTransport in connected state has incomplete "
|
|
|
|
"TLS information";
|
|
|
|
info_ = DtlsTransportInformation(
|
2022-03-11 16:49:54 +00:00
|
|
|
internal_dtls_transport_->dtls_state(), role, absl::nullopt,
|
|
|
|
absl::nullopt, absl::nullopt,
|
2020-08-14 16:58:22 +00:00
|
|
|
internal_dtls_transport_->GetRemoteSSLCertChain());
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-11 16:49:54 +00:00
|
|
|
info_ = DtlsTransportInformation(internal_dtls_transport_->dtls_state());
|
2020-08-14 16:58:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
info_ = DtlsTransportInformation(DtlsTransportState::kClosed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace webrtc
|