zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 P2P_BASE_DTLS_TRANSPORT_INTERNAL_H_ |
| 12 | #define P2P_BASE_DTLS_TRANSPORT_INTERNAL_H_ |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 16 | |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 17 | #include <memory> |
| 18 | #include <string> |
Lahiru Ginnaliya Gamathige | 16ab60c | 2021-02-02 15:27:09 | [diff] [blame] | 19 | #include <utility> |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 20 | |
Tommi | 653bab6 | 2021-04-03 15:53:54 | [diff] [blame] | 21 | #include "absl/base/attributes.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "api/crypto/crypto_options.h" |
Anton Sukhanov | 316f3ac | 2019-05-23 22:50:38 | [diff] [blame] | 23 | #include "api/dtls_transport_interface.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 24 | #include "api/scoped_refptr.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 25 | #include "p2p/base/ice_transport_internal.h" |
| 26 | #include "p2p/base/packet_transport_internal.h" |
Lahiru Ginnaliya Gamathige | 16ab60c | 2021-02-02 15:27:09 | [diff] [blame] | 27 | #include "rtc_base/callback_list.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 28 | #include "rtc_base/ssl_certificate.h" |
| 29 | #include "rtc_base/ssl_fingerprint.h" |
| 30 | #include "rtc_base/ssl_stream_adapter.h" |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 31 | |
| 32 | namespace cricket { |
| 33 | |
zhihuang | b2cdd93 | 2017-01-20 00:54:25 | [diff] [blame] | 34 | enum PacketFlags { |
| 35 | PF_NORMAL = 0x00, // A normal packet. |
| 36 | PF_SRTP_BYPASS = 0x01, // An encrypted SRTP packet; bypass any additional |
| 37 | // crypto provided by the transport (e.g. DTLS) |
| 38 | }; |
| 39 | |
deadbeef | 7914b8c | 2017-04-21 10:23:33 | [diff] [blame] | 40 | // DtlsTransportInternal is an internal interface that does DTLS, also |
| 41 | // negotiating SRTP crypto suites so that it may be used for DTLS-SRTP. |
| 42 | // |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 43 | // Once the public interface is supported, |
| 44 | // (https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface) |
| 45 | // the DtlsTransportInterface will be split from this class. |
deadbeef | 5bd5ca3 | 2017-02-10 19:31:50 | [diff] [blame] | 46 | class DtlsTransportInternal : public rtc::PacketTransportInternal { |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 47 | public: |
Steve Anton | 33f69db | 2017-10-30 17:01:15 | [diff] [blame] | 48 | ~DtlsTransportInternal() override; |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 49 | |
Byoungchan Lee | c065e73 | 2022-01-18 00:35:48 | [diff] [blame] | 50 | DtlsTransportInternal(const DtlsTransportInternal&) = delete; |
| 51 | DtlsTransportInternal& operator=(const DtlsTransportInternal&) = delete; |
| 52 | |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 53 | virtual webrtc::DtlsTransportState dtls_state() const = 0; |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 54 | |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 55 | virtual int component() const = 0; |
| 56 | |
| 57 | virtual bool IsDtlsActive() const = 0; |
| 58 | |
Zhi Huang | e818b6e | 2018-02-22 23:26:27 | [diff] [blame] | 59 | virtual bool GetDtlsRole(rtc::SSLRole* role) const = 0; |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 60 | |
Zhi Huang | e818b6e | 2018-02-22 23:26:27 | [diff] [blame] | 61 | virtual bool SetDtlsRole(rtc::SSLRole role) = 0; |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 62 | |
Harald Alvestrand | 5cb7807 | 2019-10-28 08:51:17 | [diff] [blame] | 63 | // Finds out which TLS/DTLS version is running. |
| 64 | virtual bool GetSslVersionBytes(int* version) const = 0; |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 65 | // Finds out which DTLS-SRTP cipher was negotiated. |
| 66 | // TODO(zhihuang): Remove this once all dependencies implement this. |
| 67 | virtual bool GetSrtpCryptoSuite(int* cipher) = 0; |
| 68 | |
| 69 | // Finds out which DTLS cipher was negotiated. |
| 70 | // TODO(zhihuang): Remove this once all dependencies implement this. |
| 71 | virtual bool GetSslCipherSuite(int* cipher) = 0; |
| 72 | |
| 73 | // Gets the local RTCCertificate used for DTLS. |
| 74 | virtual rtc::scoped_refptr<rtc::RTCCertificate> GetLocalCertificate() |
| 75 | const = 0; |
| 76 | |
| 77 | virtual bool SetLocalCertificate( |
| 78 | const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) = 0; |
| 79 | |
Zhi Huang | 70b820f | 2018-01-27 22:16:15 | [diff] [blame] | 80 | // Gets a copy of the remote side's SSL certificate chain. |
| 81 | virtual std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain() const = 0; |
| 82 | |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 83 | // Allows key material to be extracted for external encryption. |
| 84 | virtual bool ExportKeyingMaterial(const std::string& label, |
| 85 | const uint8_t* context, |
| 86 | size_t context_len, |
| 87 | bool use_context, |
| 88 | uint8_t* result, |
| 89 | size_t result_len) = 0; |
| 90 | |
| 91 | // Set DTLS remote fingerprint. Must be after local identity set. |
| 92 | virtual bool SetRemoteFingerprint(const std::string& digest_alg, |
| 93 | const uint8_t* digest, |
| 94 | size_t digest_len) = 0; |
| 95 | |
Tommi | 653bab6 | 2021-04-03 15:53:54 | [diff] [blame] | 96 | ABSL_DEPRECATED("Set the max version via construction.") |
| 97 | bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) { |
| 98 | return true; |
| 99 | } |
Zhi Huang | e818b6e | 2018-02-22 23:26:27 | [diff] [blame] | 100 | |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 101 | // Expose the underneath IceTransport. |
| 102 | virtual IceTransportInternal* ice_transport() = 0; |
| 103 | |
Mirko Bonadei | edc347c | 2021-05-13 14:50:45 | [diff] [blame] | 104 | // F: void(DtlsTransportInternal*, const webrtc::DtlsTransportState) |
| 105 | template <typename F> |
| 106 | void SubscribeDtlsTransportState(F&& callback) { |
| 107 | dtls_transport_state_callback_list_.AddReceiver(std::forward<F>(callback)); |
| 108 | } |
| 109 | |
| 110 | template <typename F> |
| 111 | void SubscribeDtlsTransportState(const void* id, F&& callback) { |
| 112 | dtls_transport_state_callback_list_.AddReceiver(id, |
| 113 | std::forward<F>(callback)); |
| 114 | } |
| 115 | // Unsubscribe the subscription with given id. |
| 116 | void UnsubscribeDtlsTransportState(const void* id) { |
| 117 | dtls_transport_state_callback_list_.RemoveReceivers(id); |
| 118 | } |
| 119 | |
Lahiru Ginnaliya Gamathige | 60c0b44 | 2021-02-16 15:29:08 | [diff] [blame] | 120 | void SendDtlsState(DtlsTransportInternal* transport, |
Mirko Bonadei | 9f6808b | 2021-05-21 18:46:09 | [diff] [blame] | 121 | webrtc::DtlsTransportState state) { |
| 122 | dtls_transport_state_callback_list_.Send(transport, state); |
Lahiru Ginnaliya Gamathige | 60c0b44 | 2021-02-16 15:29:08 | [diff] [blame] | 123 | } |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 124 | |
| 125 | // Emitted whenever the Dtls handshake failed on some transport channel. |
Lahiru Ginnaliya Gamathige | 16ab60c | 2021-02-02 15:27:09 | [diff] [blame] | 126 | // F: void(rtc::SSLHandshakeError) |
| 127 | template <typename F> |
| 128 | void SubscribeDtlsHandshakeError(F&& callback) { |
| 129 | dtls_handshake_error_callback_list_.AddReceiver(std::forward<F>(callback)); |
| 130 | } |
| 131 | |
| 132 | void SendDtlsHandshakeError(rtc::SSLHandshakeError error) { |
| 133 | dtls_handshake_error_callback_list_.Send(error); |
| 134 | } |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 135 | |
zhihuang | b2cdd93 | 2017-01-20 00:54:25 | [diff] [blame] | 136 | protected: |
Steve Anton | 33f69db | 2017-10-30 17:01:15 | [diff] [blame] | 137 | DtlsTransportInternal(); |
zhihuang | b2cdd93 | 2017-01-20 00:54:25 | [diff] [blame] | 138 | |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 139 | private: |
Lahiru Ginnaliya Gamathige | 16ab60c | 2021-02-02 15:27:09 | [diff] [blame] | 140 | webrtc::CallbackList<const rtc::SSLHandshakeError> |
| 141 | dtls_handshake_error_callback_list_; |
Mirko Bonadei | edc347c | 2021-05-13 14:50:45 | [diff] [blame] | 142 | webrtc::CallbackList<DtlsTransportInternal*, const webrtc::DtlsTransportState> |
| 143 | dtls_transport_state_callback_list_; |
zhihuang | e50658d | 2017-01-03 19:34:12 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | } // namespace cricket |
| 147 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 148 | #endif // P2P_BASE_DTLS_TRANSPORT_INTERNAL_H_ |