blob: 7b0151249c2ba5ed73cca8d5b8f9914da7563aa8 [file] [log] [blame]
Harald Alvestrandad88c882018-11-28 16:47:46 +01001/*
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 09:11:00 -080011#ifndef API_DTLS_TRANSPORT_INTERFACE_H_
12#define API_DTLS_TRANSPORT_INTERFACE_H_
Harald Alvestrandad88c882018-11-28 16:47:46 +010013
Harald Alvestrand7061e512019-04-10 17:20:42 +020014#include <memory>
15#include <utility>
16
Harald Alvestrand114871b2019-04-11 13:37:41 +020017#include "absl/types/optional.h"
Harald Alvestrand98462622019-01-30 14:57:03 +010018#include "api/ice_transport_interface.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#include "api/rtc_error.h"
Harald Alvestrand98462622019-01-30 14:57:03 +010020#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/ref_count.h"
Harald Alvestrand7061e512019-04-10 17:20:42 +020022#include "rtc_base/ssl_certificate.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020023#include "rtc_base/system/rtc_export.h"
Harald Alvestrandad88c882018-11-28 16:47:46 +010024
25namespace webrtc {
26
Harald Alvestrandd02541e2019-01-03 12:43:28 +010027// States of a DTLS transport, corresponding to the JS API specification.
28// http://w3c.github.io/webrtc-pc/#dom-rtcdtlstransportstate
29enum class DtlsTransportState {
30 kNew, // Has not started negotiating yet.
31 kConnecting, // In the process of negotiating a secure connection.
32 kConnected, // Completed negotiation and verified fingerprints.
33 kClosed, // Intentionally closed.
Harald Alvestrand4a7b3ac2019-01-17 10:39:40 +010034 kFailed, // Failure due to an error or failing to verify a remote
35 // fingerprint.
36 kNumValues
Harald Alvestrandd02541e2019-01-03 12:43:28 +010037};
38
Harald Alvestrand316ab122022-02-10 08:23:47 +000039enum class DtlsTransportTlsRole {
40 kServer, // Other end sends CLIENT_HELLO
41 kClient // This end sends CLIENT_HELLO
42};
43
Harald Alvestrandd02541e2019-01-03 12:43:28 +010044// This object gives snapshot information about the changeable state of a
45// DTLSTransport.
Mirko Bonadei35214fc2019-09-23 14:54:28 +020046class RTC_EXPORT DtlsTransportInformation {
Harald Alvestrandd02541e2019-01-03 12:43:28 +010047 public:
Harald Alvestrand7061e512019-04-10 17:20:42 +020048 DtlsTransportInformation();
49 explicit DtlsTransportInformation(DtlsTransportState state);
50 DtlsTransportInformation(
51 DtlsTransportState state,
Harald Alvestrand316ab122022-02-10 08:23:47 +000052 absl::optional<DtlsTransportTlsRole> role,
Harald Alvestrandc6c3f862019-10-29 12:19:31 +010053 absl::optional<int> tls_version,
Harald Alvestrand114871b2019-04-11 13:37:41 +020054 absl::optional<int> ssl_cipher_suite,
Harald Alvestrandc6c3f862019-10-29 12:19:31 +010055 absl::optional<int> srtp_cipher_suite,
Harald Alvestrand7061e512019-04-10 17:20:42 +020056 std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates);
Harald Alvestrand316ab122022-02-10 08:23:47 +000057 ABSL_DEPRECATED("Use version with role parameter")
58 DtlsTransportInformation(
59 DtlsTransportState state,
60 absl::optional<int> tls_version,
61 absl::optional<int> ssl_cipher_suite,
62 absl::optional<int> srtp_cipher_suite,
63 std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates);
64
Harald Alvestrand7061e512019-04-10 17:20:42 +020065 // Copy and assign
66 DtlsTransportInformation(const DtlsTransportInformation& c);
67 DtlsTransportInformation& operator=(const DtlsTransportInformation& c);
68 // Move
69 DtlsTransportInformation(DtlsTransportInformation&& other) = default;
70 DtlsTransportInformation& operator=(DtlsTransportInformation&& other) =
71 default;
72
Harald Alvestrandd02541e2019-01-03 12:43:28 +010073 DtlsTransportState state() const { return state_; }
Harald Alvestrand316ab122022-02-10 08:23:47 +000074 absl::optional<DtlsTransportTlsRole> role() const { return role_; }
Harald Alvestrandc6c3f862019-10-29 12:19:31 +010075 absl::optional<int> tls_version() const { return tls_version_; }
Harald Alvestrand114871b2019-04-11 13:37:41 +020076 absl::optional<int> ssl_cipher_suite() const { return ssl_cipher_suite_; }
Harald Alvestrandc6c3f862019-10-29 12:19:31 +010077 absl::optional<int> srtp_cipher_suite() const { return srtp_cipher_suite_; }
Harald Alvestrand7061e512019-04-10 17:20:42 +020078 // The accessor returns a temporary pointer, it does not release ownership.
79 const rtc::SSLCertChain* remote_ssl_certificates() const {
80 return remote_ssl_certificates_.get();
81 }
82
Harald Alvestrandd02541e2019-01-03 12:43:28 +010083 private:
84 DtlsTransportState state_;
Harald Alvestrand316ab122022-02-10 08:23:47 +000085 absl::optional<DtlsTransportTlsRole> role_;
Harald Alvestrandc6c3f862019-10-29 12:19:31 +010086 absl::optional<int> tls_version_;
Harald Alvestrand114871b2019-04-11 13:37:41 +020087 absl::optional<int> ssl_cipher_suite_;
Harald Alvestrandc6c3f862019-10-29 12:19:31 +010088 absl::optional<int> srtp_cipher_suite_;
Harald Alvestrand7061e512019-04-10 17:20:42 +020089 std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates_;
Harald Alvestrandd02541e2019-01-03 12:43:28 +010090};
91
92class DtlsTransportObserverInterface {
93 public:
94 // This callback carries information about the state of the transport.
95 // The argument is a pass-by-value snapshot of the state.
96 virtual void OnStateChange(DtlsTransportInformation info) = 0;
97 // This callback is called when an error occurs, causing the transport
98 // to go to the kFailed state.
99 virtual void OnError(RTCError error) = 0;
100
101 protected:
102 virtual ~DtlsTransportObserverInterface() = default;
103};
104
Harald Alvestrandad88c882018-11-28 16:47:46 +0100105// A DTLS transport, as represented to the outside world.
Harald Alvestrand69fb6c82019-02-13 19:40:11 +0100106// This object is created on the network thread, and can only be
107// accessed on that thread, except for functions explicitly marked otherwise.
Harald Alvestrandd02541e2019-01-03 12:43:28 +0100108// References can be held by other threads, and destruction can therefore
109// be initiated by other threads.
Harald Alvestrandad88c882018-11-28 16:47:46 +0100110class DtlsTransportInterface : public rtc::RefCountInterface {
111 public:
Harald Alvestrand98462622019-01-30 14:57:03 +0100112 // Returns a pointer to the ICE transport that is owned by the DTLS transport.
113 virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0;
Harald Alvestrand69fb6c82019-02-13 19:40:11 +0100114 // Returns information on the state of the DtlsTransport.
115 // This function can be called from other threads.
Harald Alvestrandd02541e2019-01-03 12:43:28 +0100116 virtual DtlsTransportInformation Information() = 0;
117 // Observer management.
118 virtual void RegisterObserver(DtlsTransportObserverInterface* observer) = 0;
119 virtual void UnregisterObserver() = 0;
Harald Alvestrandad88c882018-11-28 16:47:46 +0100120};
121
122} // namespace webrtc
123
Steve Anton10542f22019-01-11 09:11:00 -0800124#endif // API_DTLS_TRANSPORT_INTERFACE_H_