blob: 24c682ff0e133741db2517871253c1ca8ede5b2f [file] [log] [blame]
zhihuange50658d2017-01-03 19:34:121/*
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 Anton10542f22019-01-11 17:11:0011#ifndef P2P_BASE_DTLS_TRANSPORT_INTERNAL_H_
12#define P2P_BASE_DTLS_TRANSPORT_INTERNAL_H_
zhihuange50658d2017-01-03 19:34:1213
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
15#include <stdint.h>
Anton Sukhanov316f3ac2019-05-23 22:50:3816
zhihuange50658d2017-01-03 19:34:1217#include <memory>
18#include <string>
Lahiru Ginnaliya Gamathige16ab60c2021-02-02 15:27:0919#include <utility>
zhihuange50658d2017-01-03 19:34:1220
Tommi653bab62021-04-03 15:53:5421#include "absl/base/attributes.h"
Steve Anton10542f22019-01-11 17:11:0022#include "api/crypto/crypto_options.h"
Anton Sukhanov316f3ac2019-05-23 22:50:3823#include "api/dtls_transport_interface.h"
Mirko Bonadeid9708072019-01-25 19:26:4824#include "api/scoped_refptr.h"
Steve Anton10542f22019-01-11 17:11:0025#include "p2p/base/ice_transport_internal.h"
26#include "p2p/base/packet_transport_internal.h"
Lahiru Ginnaliya Gamathige16ab60c2021-02-02 15:27:0927#include "rtc_base/callback_list.h"
Steve Anton10542f22019-01-11 17:11:0028#include "rtc_base/ssl_certificate.h"
29#include "rtc_base/ssl_fingerprint.h"
30#include "rtc_base/ssl_stream_adapter.h"
zhihuange50658d2017-01-03 19:34:1231
32namespace cricket {
33
zhihuangb2cdd932017-01-20 00:54:2534enum 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
deadbeef7914b8c2017-04-21 10:23:3340// DtlsTransportInternal is an internal interface that does DTLS, also
41// negotiating SRTP crypto suites so that it may be used for DTLS-SRTP.
42//
zhihuange50658d2017-01-03 19:34:1243// Once the public interface is supported,
44// (https://www.w3.org/TR/webrtc/#rtcdtlstransport-interface)
45// the DtlsTransportInterface will be split from this class.
deadbeef5bd5ca32017-02-10 19:31:5046class DtlsTransportInternal : public rtc::PacketTransportInternal {
zhihuange50658d2017-01-03 19:34:1247 public:
Steve Anton33f69db2017-10-30 17:01:1548 ~DtlsTransportInternal() override;
zhihuange50658d2017-01-03 19:34:1249
Byoungchan Leec065e732022-01-18 00:35:4850 DtlsTransportInternal(const DtlsTransportInternal&) = delete;
51 DtlsTransportInternal& operator=(const DtlsTransportInternal&) = delete;
52
Mirko Bonadei9f6808b2021-05-21 18:46:0953 virtual webrtc::DtlsTransportState dtls_state() const = 0;
zhihuange50658d2017-01-03 19:34:1254
zhihuange50658d2017-01-03 19:34:1255 virtual int component() const = 0;
56
57 virtual bool IsDtlsActive() const = 0;
58
Zhi Huange818b6e2018-02-22 23:26:2759 virtual bool GetDtlsRole(rtc::SSLRole* role) const = 0;
zhihuange50658d2017-01-03 19:34:1260
Zhi Huange818b6e2018-02-22 23:26:2761 virtual bool SetDtlsRole(rtc::SSLRole role) = 0;
zhihuange50658d2017-01-03 19:34:1262
Harald Alvestrand5cb78072019-10-28 08:51:1763 // Finds out which TLS/DTLS version is running.
64 virtual bool GetSslVersionBytes(int* version) const = 0;
zhihuange50658d2017-01-03 19:34:1265 // 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 Huang70b820f2018-01-27 22:16:1580 // Gets a copy of the remote side's SSL certificate chain.
81 virtual std::unique_ptr<rtc::SSLCertChain> GetRemoteSSLCertChain() const = 0;
82
zhihuange50658d2017-01-03 19:34:1283 // 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
Tommi653bab62021-04-03 15:53:5496 ABSL_DEPRECATED("Set the max version via construction.")
97 bool SetSslMaxProtocolVersion(rtc::SSLProtocolVersion version) {
98 return true;
99 }
Zhi Huange818b6e2018-02-22 23:26:27100
zhihuange50658d2017-01-03 19:34:12101 // Expose the underneath IceTransport.
102 virtual IceTransportInternal* ice_transport() = 0;
103
Mirko Bonadeiedc347c2021-05-13 14:50:45104 // 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 Gamathige60c0b442021-02-16 15:29:08120 void SendDtlsState(DtlsTransportInternal* transport,
Mirko Bonadei9f6808b2021-05-21 18:46:09121 webrtc::DtlsTransportState state) {
122 dtls_transport_state_callback_list_.Send(transport, state);
Lahiru Ginnaliya Gamathige60c0b442021-02-16 15:29:08123 }
zhihuange50658d2017-01-03 19:34:12124
125 // Emitted whenever the Dtls handshake failed on some transport channel.
Lahiru Ginnaliya Gamathige16ab60c2021-02-02 15:27:09126 // 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 }
zhihuange50658d2017-01-03 19:34:12135
zhihuangb2cdd932017-01-20 00:54:25136 protected:
Steve Anton33f69db2017-10-30 17:01:15137 DtlsTransportInternal();
zhihuangb2cdd932017-01-20 00:54:25138
zhihuange50658d2017-01-03 19:34:12139 private:
Lahiru Ginnaliya Gamathige16ab60c2021-02-02 15:27:09140 webrtc::CallbackList<const rtc::SSLHandshakeError>
141 dtls_handshake_error_callback_list_;
Mirko Bonadeiedc347c2021-05-13 14:50:45142 webrtc::CallbackList<DtlsTransportInternal*, const webrtc::DtlsTransportState>
143 dtls_transport_state_callback_list_;
zhihuange50658d2017-01-03 19:34:12144};
145
146} // namespace cricket
147
Steve Anton10542f22019-01-11 17:11:00148#endif // P2P_BASE_DTLS_TRANSPORT_INTERNAL_H_