blob: 15eed9e47b5136ea542251576a58e562dfeebcc9 [file] [log] [blame]
Harald Alvestrandad88c882018-11-28 15:47:461/*
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 17:11:0011#include "pc/dtls_transport.h"
Harald Alvestrandad88c882018-11-28 15:47:4612
13#include <utility>
14
Harald Alvestrand5761e7b2021-01-29 14:45:0815#include "absl/types/optional.h"
Mirko Bonadei9f6808b2021-05-21 18:46:0916#include "api/dtls_transport_interface.h"
Niels Möller105711e2022-06-14 13:48:2617#include "api/make_ref_counted.h"
Artem Titovd15a5752021-02-10 13:31:2418#include "api/sequence_checker.h"
Harald Alvestrand98462622019-01-30 13:57:0319#include "pc/ice_transport.h"
Harald Alvestrand5761e7b2021-01-29 14:45:0820#include "rtc_base/checks.h"
21#include "rtc_base/logging.h"
Harald Alvestrandc24a2182022-02-23 13:44:5922#include "rtc_base/ssl_stream_adapter.h"
Harald Alvestrand98462622019-01-30 13:57:0323
Harald Alvestrandad88c882018-11-28 15:47:4624namespace webrtc {
25
Harald Alvestrandd02541e2019-01-03 11:43:2826// Implementation of DtlsTransportInterface
Harald Alvestrandad88c882018-11-28 15:47:4627DtlsTransport::DtlsTransport(
28 std::unique_ptr<cricket::DtlsTransportInternal> internal)
Harald Alvestrand69fb6c82019-02-13 18:40:1129 : owner_thread_(rtc::Thread::Current()),
30 info_(DtlsTransportState::kNew),
Harald Alvestrand26451932019-02-21 10:27:1531 internal_dtls_transport_(std::move(internal)),
Tommi87f70902021-04-27 12:43:0832 ice_transport_(rtc::make_ref_counted<IceTransportWithPointer>(
Harald Alvestrand26451932019-02-21 10:27:1533 internal_dtls_transport_->ice_transport())) {
Harald Alvestrandad88c882018-11-28 15:47:4634 RTC_DCHECK(internal_dtls_transport_.get());
Mirko Bonadei9f6808b2021-05-21 18:46:0935 internal_dtls_transport_->SubscribeDtlsTransportState(
Lahiru Ginnaliya Gamathige60c0b442021-02-16 15:29:0836 [this](cricket::DtlsTransportInternal* transport,
Mirko Bonadei9f6808b2021-05-21 18:46:0937 DtlsTransportState state) {
Lahiru Ginnaliya Gamathige60c0b442021-02-16 15:29:0838 OnInternalDtlsState(transport, state);
39 });
Harald Alvestrand69fb6c82019-02-13 18:40:1140 UpdateInformation();
Harald Alvestrandd02541e2019-01-03 11:43:2841}
42
43DtlsTransport::~DtlsTransport() {
44 // We depend on the signaling thread to call Clear() before dropping
45 // its last reference to this object.
Harald Alvestrand69fb6c82019-02-13 18:40:1146 RTC_DCHECK(owner_thread_->IsCurrent() || !internal_dtls_transport_);
Harald Alvestrandd02541e2019-01-03 11:43:2847}
48
49DtlsTransportInformation DtlsTransport::Information() {
Markus Handell6fcd0f82020-07-07 17:08:5350 MutexLock lock(&lock_);
Harald Alvestrand69fb6c82019-02-13 18:40:1151 return info_;
Harald Alvestrandd02541e2019-01-03 11:43:2852}
53
54void DtlsTransport::RegisterObserver(DtlsTransportObserverInterface* observer) {
Harald Alvestrand69fb6c82019-02-13 18:40:1155 RTC_DCHECK_RUN_ON(owner_thread_);
Harald Alvestrandd02541e2019-01-03 11:43:2856 RTC_DCHECK(observer);
57 observer_ = observer;
58}
59
60void DtlsTransport::UnregisterObserver() {
Harald Alvestrand69fb6c82019-02-13 18:40:1161 RTC_DCHECK_RUN_ON(owner_thread_);
Harald Alvestrandd02541e2019-01-03 11:43:2862 observer_ = nullptr;
63}
64
Harald Alvestrand98462622019-01-30 13:57:0365rtc::scoped_refptr<IceTransportInterface> DtlsTransport::ice_transport() {
66 return ice_transport_;
67}
68
Harald Alvestrandd02541e2019-01-03 11:43:2869// Internal functions
70void DtlsTransport::Clear() {
Harald Alvestrand69fb6c82019-02-13 18:40:1171 RTC_DCHECK_RUN_ON(owner_thread_);
Harald Alvestrandcdc30452019-01-08 17:08:0472 RTC_DCHECK(internal());
Harald Alvestrand69fb6c82019-02-13 18:40:1173 bool must_send_event =
Mirko Bonadei9f6808b2021-05-21 18:46:0974 (internal()->dtls_state() != DtlsTransportState::kClosed);
Harald Alvestrand69fb6c82019-02-13 18:40:1175 // The destructor of cricket::DtlsTransportInternal calls back
76 // into DtlsTransport, so we can't hold the lock while releasing.
77 std::unique_ptr<cricket::DtlsTransportInternal> transport_to_release;
78 {
Markus Handell6fcd0f82020-07-07 17:08:5379 MutexLock lock(&lock_);
Harald Alvestrand69fb6c82019-02-13 18:40:1180 transport_to_release = std::move(internal_dtls_transport_);
81 ice_transport_->Clear();
Harald Alvestrandcdc30452019-01-08 17:08:0482 }
Harald Alvestrand69fb6c82019-02-13 18:40:1183 UpdateInformation();
84 if (observer_ && must_send_event) {
85 observer_->OnStateChange(Information());
86 }
Harald Alvestrandd02541e2019-01-03 11:43:2887}
88
89void DtlsTransport::OnInternalDtlsState(
90 cricket::DtlsTransportInternal* transport,
Mirko Bonadei9f6808b2021-05-21 18:46:0991 DtlsTransportState state) {
Harald Alvestrand69fb6c82019-02-13 18:40:1192 RTC_DCHECK_RUN_ON(owner_thread_);
Harald Alvestrandd02541e2019-01-03 11:43:2893 RTC_DCHECK(transport == internal());
94 RTC_DCHECK(state == internal()->dtls_state());
Harald Alvestrand69fb6c82019-02-13 18:40:1195 UpdateInformation();
Harald Alvestrandd02541e2019-01-03 11:43:2896 if (observer_) {
97 observer_->OnStateChange(Information());
98 }
Harald Alvestrandad88c882018-11-28 15:47:4699}
100
Harald Alvestrand69fb6c82019-02-13 18:40:11101void DtlsTransport::UpdateInformation() {
102 RTC_DCHECK_RUN_ON(owner_thread_);
Markus Handell6fcd0f82020-07-07 17:08:53103 MutexLock lock(&lock_);
Harald Alvestrand69fb6c82019-02-13 18:40:11104 if (internal_dtls_transport_) {
Harald Alvestrand7061e512019-04-10 15:20:42105 if (internal_dtls_transport_->dtls_state() ==
Mirko Bonadei9f6808b2021-05-21 18:46:09106 DtlsTransportState::kConnected) {
Harald Alvestrandc6c3f862019-10-29 11:19:31107 bool success = true;
Harald Alvestrand316ab122022-02-10 08:23:47108 rtc::SSLRole internal_role;
109 absl::optional<DtlsTransportTlsRole> role;
Harald Alvestrand114871b2019-04-11 11:37:41110 int ssl_cipher_suite;
Harald Alvestrandc6c3f862019-10-29 11:19:31111 int tls_version;
112 int srtp_cipher;
Harald Alvestrand316ab122022-02-10 08:23:47113 success &= internal_dtls_transport_->GetDtlsRole(&internal_role);
114 if (success) {
115 switch (internal_role) {
116 case rtc::SSL_CLIENT:
Harald Alvestrand321ec3b2022-02-10 13:42:18117 role = DtlsTransportTlsRole::kClient;
Harald Alvestrand316ab122022-02-10 08:23:47118 break;
119 case rtc::SSL_SERVER:
Harald Alvestrand321ec3b2022-02-10 13:42:18120 role = DtlsTransportTlsRole::kServer;
Harald Alvestrand316ab122022-02-10 08:23:47121 break;
122 }
123 }
Harald Alvestrandc6c3f862019-10-29 11:19:31124 success &= internal_dtls_transport_->GetSslVersionBytes(&tls_version);
125 success &= internal_dtls_transport_->GetSslCipherSuite(&ssl_cipher_suite);
126 success &= internal_dtls_transport_->GetSrtpCryptoSuite(&srtp_cipher);
127 if (success) {
Harald Alvestrand114871b2019-04-11 11:37:41128 info_ = DtlsTransportInformation(
Harald Alvestrand316ab122022-02-10 08:23:47129 internal_dtls_transport_->dtls_state(), role, tls_version,
Harald Alvestrandc6c3f862019-10-29 11:19:31130 ssl_cipher_suite, srtp_cipher,
Harald Alvestrand114871b2019-04-11 11:37:41131 internal_dtls_transport_->GetRemoteSSLCertChain());
132 } else {
Harald Alvestrandc6c3f862019-10-29 11:19:31133 RTC_LOG(LS_ERROR) << "DtlsTransport in connected state has incomplete "
134 "TLS information";
Harald Alvestrand114871b2019-04-11 11:37:41135 info_ = DtlsTransportInformation(
Harald Alvestrand316ab122022-02-10 08:23:47136 internal_dtls_transport_->dtls_state(), role, absl::nullopt,
Mirko Bonadei9f6808b2021-05-21 18:46:09137 absl::nullopt, absl::nullopt,
Harald Alvestrandc6c3f862019-10-29 11:19:31138 internal_dtls_transport_->GetRemoteSSLCertChain());
Harald Alvestrand114871b2019-04-11 11:37:41139 }
Harald Alvestrand7061e512019-04-10 15:20:42140 } else {
Mirko Bonadei9f6808b2021-05-21 18:46:09141 info_ = DtlsTransportInformation(internal_dtls_transport_->dtls_state());
Harald Alvestrand7061e512019-04-10 15:20:42142 }
Harald Alvestrand69fb6c82019-02-13 18:40:11143 } else {
144 info_ = DtlsTransportInformation(DtlsTransportState::kClosed);
145 }
146}
147
Harald Alvestrandad88c882018-11-28 15:47:46148} // namespace webrtc