Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [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 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef API_DTLS_TRANSPORT_INTERFACE_H_ |
| 12 | #define API_DTLS_TRANSPORT_INTERFACE_H_ |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 13 | |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame] | 14 | #include <memory> |
| 15 | #include <utility> |
| 16 | |
Harald Alvestrand | 114871b | 2019-04-11 13:37:41 +0200 | [diff] [blame] | 17 | #include "absl/types/optional.h" |
Harald Alvestrand | 9846262 | 2019-01-30 14:57:03 +0100 | [diff] [blame] | 18 | #include "api/ice_transport_interface.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "api/rtc_error.h" |
Harald Alvestrand | 9846262 | 2019-01-30 14:57:03 +0100 | [diff] [blame] | 20 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/ref_count.h" |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame] | 22 | #include "rtc_base/ssl_certificate.h" |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 26 | // States of a DTLS transport, corresponding to the JS API specification. |
| 27 | // http://w3c.github.io/webrtc-pc/#dom-rtcdtlstransportstate |
| 28 | enum class DtlsTransportState { |
| 29 | kNew, // Has not started negotiating yet. |
| 30 | kConnecting, // In the process of negotiating a secure connection. |
| 31 | kConnected, // Completed negotiation and verified fingerprints. |
| 32 | kClosed, // Intentionally closed. |
Harald Alvestrand | 4a7b3ac | 2019-01-17 10:39:40 +0100 | [diff] [blame] | 33 | kFailed, // Failure due to an error or failing to verify a remote |
| 34 | // fingerprint. |
| 35 | kNumValues |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | // This object gives snapshot information about the changeable state of a |
| 39 | // DTLSTransport. |
| 40 | class DtlsTransportInformation { |
| 41 | public: |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame] | 42 | DtlsTransportInformation(); |
| 43 | explicit DtlsTransportInformation(DtlsTransportState state); |
| 44 | DtlsTransportInformation( |
| 45 | DtlsTransportState state, |
Harald Alvestrand | 114871b | 2019-04-11 13:37:41 +0200 | [diff] [blame] | 46 | absl::optional<int> ssl_cipher_suite, |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame] | 47 | std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates); |
| 48 | // Copy and assign |
| 49 | DtlsTransportInformation(const DtlsTransportInformation& c); |
| 50 | DtlsTransportInformation& operator=(const DtlsTransportInformation& c); |
| 51 | // Move |
| 52 | DtlsTransportInformation(DtlsTransportInformation&& other) = default; |
| 53 | DtlsTransportInformation& operator=(DtlsTransportInformation&& other) = |
| 54 | default; |
| 55 | |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 56 | DtlsTransportState state() const { return state_; } |
Harald Alvestrand | 114871b | 2019-04-11 13:37:41 +0200 | [diff] [blame] | 57 | absl::optional<int> ssl_cipher_suite() const { return ssl_cipher_suite_; } |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame] | 58 | // The accessor returns a temporary pointer, it does not release ownership. |
| 59 | const rtc::SSLCertChain* remote_ssl_certificates() const { |
| 60 | return remote_ssl_certificates_.get(); |
| 61 | } |
| 62 | |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 63 | private: |
| 64 | DtlsTransportState state_; |
Harald Alvestrand | 114871b | 2019-04-11 13:37:41 +0200 | [diff] [blame] | 65 | absl::optional<int> ssl_cipher_suite_; |
Harald Alvestrand | 7061e51 | 2019-04-10 17:20:42 +0200 | [diff] [blame] | 66 | std::unique_ptr<rtc::SSLCertChain> remote_ssl_certificates_; |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | class DtlsTransportObserverInterface { |
| 70 | public: |
| 71 | // This callback carries information about the state of the transport. |
| 72 | // The argument is a pass-by-value snapshot of the state. |
| 73 | virtual void OnStateChange(DtlsTransportInformation info) = 0; |
| 74 | // This callback is called when an error occurs, causing the transport |
| 75 | // to go to the kFailed state. |
| 76 | virtual void OnError(RTCError error) = 0; |
| 77 | |
| 78 | protected: |
| 79 | virtual ~DtlsTransportObserverInterface() = default; |
| 80 | }; |
| 81 | |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 82 | // A DTLS transport, as represented to the outside world. |
Harald Alvestrand | 69fb6c8 | 2019-02-13 19:40:11 +0100 | [diff] [blame] | 83 | // This object is created on the network thread, and can only be |
| 84 | // accessed on that thread, except for functions explicitly marked otherwise. |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 85 | // References can be held by other threads, and destruction can therefore |
| 86 | // be initiated by other threads. |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 87 | class DtlsTransportInterface : public rtc::RefCountInterface { |
| 88 | public: |
Harald Alvestrand | 9846262 | 2019-01-30 14:57:03 +0100 | [diff] [blame] | 89 | // Returns a pointer to the ICE transport that is owned by the DTLS transport. |
| 90 | virtual rtc::scoped_refptr<IceTransportInterface> ice_transport() = 0; |
Harald Alvestrand | 69fb6c8 | 2019-02-13 19:40:11 +0100 | [diff] [blame] | 91 | // Returns information on the state of the DtlsTransport. |
| 92 | // This function can be called from other threads. |
Harald Alvestrand | d02541e | 2019-01-03 12:43:28 +0100 | [diff] [blame] | 93 | virtual DtlsTransportInformation Information() = 0; |
| 94 | // Observer management. |
| 95 | virtual void RegisterObserver(DtlsTransportObserverInterface* observer) = 0; |
| 96 | virtual void UnregisterObserver() = 0; |
Harald Alvestrand | ad88c88 | 2018-11-28 16:47:46 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace webrtc |
| 100 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 101 | #endif // API_DTLS_TRANSPORT_INTERFACE_H_ |