Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 11 | #ifndef API_DTLS_TRANSPORT_INTERFACE_H_ |
| 12 | #define API_DTLS_TRANSPORT_INTERFACE_H_ |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 13 | |
Harald Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
Harald Alvestrand | 114871b | 2019-04-11 11:37:41 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Harald Alvestrand | 9846262 | 2019-01-30 13:57:03 | [diff] [blame] | 18 | #include "api/ice_transport_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 19 | #include "api/rtc_error.h" |
Harald Alvestrand | 9846262 | 2019-01-30 13:57:03 | [diff] [blame] | 20 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 21 | #include "rtc_base/ref_count.h" |
Harald Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 22 | #include "rtc_base/ssl_certificate.h" |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 23 | #include "rtc_base/system/rtc_export.h" |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 27 | // States of a DTLS transport, corresponding to the JS API specification. |
| 28 | // http://w3c.github.io/webrtc-pc/#dom-rtcdtlstransportstate |
| 29 | enum 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 Alvestrand | 4a7b3ac | 2019-01-17 09:39:40 | [diff] [blame] | 34 | kFailed, // Failure due to an error or failing to verify a remote |
| 35 | // fingerprint. |
| 36 | kNumValues |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 37 | }; |
| 38 | |
Harald Alvestrand | 316ab12 | 2022-02-10 08:23:47 | [diff] [blame] | 39 | enum class DtlsTransportTlsRole { |
| 40 | kServer, // Other end sends CLIENT_HELLO |
| 41 | kClient // This end sends CLIENT_HELLO |
| 42 | }; |
| 43 | |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 44 | // This object gives snapshot information about the changeable state of a |
| 45 | // DTLSTransport. |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 46 | class RTC_EXPORT DtlsTransportInformation { |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 47 | public: |
Harald Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 48 | DtlsTransportInformation(); |
| 49 | explicit DtlsTransportInformation(DtlsTransportState state); |
| 50 | DtlsTransportInformation( |
| 51 | DtlsTransportState state, |
Harald Alvestrand | 316ab12 | 2022-02-10 08:23:47 | [diff] [blame] | 52 | absl::optional<DtlsTransportTlsRole> role, |
Harald Alvestrand | c6c3f86 | 2019-10-29 11:19:31 | [diff] [blame] | 53 | absl::optional<int> tls_version, |
Harald Alvestrand | 114871b | 2019-04-11 11:37:41 | [diff] [blame] | 54 | absl::optional<int> ssl_cipher_suite, |
Harald Alvestrand | c6c3f86 | 2019-10-29 11:19:31 | [diff] [blame] | 55 | absl::optional<int> srtp_cipher_suite, |
Harald Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 56 | std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates); |
Harald Alvestrand | 316ab12 | 2022-02-10 08:23:47 | [diff] [blame] | 57 | 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 Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 65 | // 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 Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 73 | DtlsTransportState state() const { return state_; } |
Harald Alvestrand | 316ab12 | 2022-02-10 08:23:47 | [diff] [blame] | 74 | absl::optional<DtlsTransportTlsRole> role() const { return role_; } |
Harald Alvestrand | c6c3f86 | 2019-10-29 11:19:31 | [diff] [blame] | 75 | absl::optional<int> tls_version() const { return tls_version_; } |
Harald Alvestrand | 114871b | 2019-04-11 11:37:41 | [diff] [blame] | 76 | absl::optional<int> ssl_cipher_suite() const { return ssl_cipher_suite_; } |
Harald Alvestrand | c6c3f86 | 2019-10-29 11:19:31 | [diff] [blame] | 77 | absl::optional<int> srtp_cipher_suite() const { return srtp_cipher_suite_; } |
Harald Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 78 | // 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 Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 83 | private: |
| 84 | DtlsTransportState state_; |
Harald Alvestrand | 316ab12 | 2022-02-10 08:23:47 | [diff] [blame] | 85 | absl::optional<DtlsTransportTlsRole> role_; |
Harald Alvestrand | c6c3f86 | 2019-10-29 11:19:31 | [diff] [blame] | 86 | absl::optional<int> tls_version_; |
Harald Alvestrand | 114871b | 2019-04-11 11:37:41 | [diff] [blame] | 87 | absl::optional<int> ssl_cipher_suite_; |
Harald Alvestrand | c6c3f86 | 2019-10-29 11:19:31 | [diff] [blame] | 88 | absl::optional<int> srtp_cipher_suite_; |
Harald Alvestrand | 7061e51 | 2019-04-10 15:20:42 | [diff] [blame] | 89 | std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates_; |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class 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 Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 105 | // A DTLS transport, as represented to the outside world. |
Harald Alvestrand | 69fb6c8 | 2019-02-13 18:40:11 | [diff] [blame] | 106 | // This object is created on the network thread, and can only be |
| 107 | // accessed on that thread, except for functions explicitly marked otherwise. |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 108 | // References can be held by other threads, and destruction can therefore |
| 109 | // be initiated by other threads. |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 110 | class DtlsTransportInterface : public rtc::RefCountInterface { |
| 111 | public: |
Harald Alvestrand | 9846262 | 2019-01-30 13:57:03 | [diff] [blame] | 112 | // Returns a pointer to the ICE transport that is owned by the DTLS transport. |
| 113 | virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0; |
Harald Alvestrand | 69fb6c8 | 2019-02-13 18:40:11 | [diff] [blame] | 114 | // Returns information on the state of the DtlsTransport. |
| 115 | // This function can be called from other threads. |
Harald Alvestrand | d02541e | 2019-01-03 11:43:28 | [diff] [blame] | 116 | virtual DtlsTransportInformation Information() = 0; |
| 117 | // Observer management. |
| 118 | virtual void RegisterObserver(DtlsTransportObserverInterface* observer) = 0; |
| 119 | virtual void UnregisterObserver() = 0; |
Harald Alvestrand | ad88c88 | 2018-11-28 15:47:46 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace webrtc |
| 123 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 124 | #endif // API_DTLS_TRANSPORT_INTERFACE_H_ |