xref: /aosp_15_r20/external/webrtc/pc/dtls_srtp_transport.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2017 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 
11 #ifndef PC_DTLS_SRTP_TRANSPORT_H_
12 #define PC_DTLS_SRTP_TRANSPORT_H_
13 
14 #include <functional>
15 #include <string>
16 #include <vector>
17 
18 #include "absl/types/optional.h"
19 #include "api/crypto_params.h"
20 #include "api/dtls_transport_interface.h"
21 #include "api/rtc_error.h"
22 #include "p2p/base/dtls_transport_internal.h"
23 #include "p2p/base/packet_transport_internal.h"
24 #include "pc/srtp_transport.h"
25 #include "rtc_base/buffer.h"
26 #include "rtc_base/third_party/sigslot/sigslot.h"
27 
28 namespace webrtc {
29 
30 // The subclass of SrtpTransport is used for DTLS-SRTP. When the DTLS handshake
31 // is finished, it extracts the keying materials from DtlsTransport and
32 // configures the SrtpSessions in the base class.
33 class DtlsSrtpTransport : public SrtpTransport {
34  public:
35   DtlsSrtpTransport(bool rtcp_mux_enabled, const FieldTrialsView& field_trials);
36 
37   // Set P2P layer RTP/RTCP DtlsTransports. When using RTCP-muxing,
38   // `rtcp_dtls_transport` is null.
39   void SetDtlsTransports(cricket::DtlsTransportInternal* rtp_dtls_transport,
40                          cricket::DtlsTransportInternal* rtcp_dtls_transport);
41 
42   void SetRtcpMuxEnabled(bool enable) override;
43 
44   // Set the header extension ids that should be encrypted.
45   void UpdateSendEncryptedHeaderExtensionIds(
46       const std::vector<int>& send_extension_ids);
47 
48   void UpdateRecvEncryptedHeaderExtensionIds(
49       const std::vector<int>& recv_extension_ids);
50 
51   void SetOnDtlsStateChange(std::function<void(void)> callback);
52 
SetSrtpSendKey(const cricket::CryptoParams & params)53   RTCError SetSrtpSendKey(const cricket::CryptoParams& params) override {
54     return RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
55                     "Set SRTP keys for DTLS-SRTP is not supported.");
56   }
SetSrtpReceiveKey(const cricket::CryptoParams & params)57   RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params) override {
58     return RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
59                     "Set SRTP keys for DTLS-SRTP is not supported.");
60   }
61 
62   // If `active_reset_srtp_params_` is set to be true, the SRTP parameters will
63   // be reset whenever the DtlsTransports are reset.
SetActiveResetSrtpParams(bool active_reset_srtp_params)64   void SetActiveResetSrtpParams(bool active_reset_srtp_params) {
65     active_reset_srtp_params_ = active_reset_srtp_params;
66   }
67 
68  private:
69   bool IsDtlsActive();
70   bool IsDtlsConnected();
71   bool IsDtlsWritable();
72   bool DtlsHandshakeCompleted();
73   void MaybeSetupDtlsSrtp();
74   void SetupRtpDtlsSrtp();
75   void SetupRtcpDtlsSrtp();
76   bool ExtractParams(cricket::DtlsTransportInternal* dtls_transport,
77                      int* selected_crypto_suite,
78                      rtc::ZeroOnFreeBuffer<unsigned char>* send_key,
79                      rtc::ZeroOnFreeBuffer<unsigned char>* recv_key);
80   void SetDtlsTransport(cricket::DtlsTransportInternal* new_dtls_transport,
81                         cricket::DtlsTransportInternal** old_dtls_transport);
82   void SetRtpDtlsTransport(cricket::DtlsTransportInternal* rtp_dtls_transport);
83   void SetRtcpDtlsTransport(
84       cricket::DtlsTransportInternal* rtcp_dtls_transport);
85 
86   void OnDtlsState(cricket::DtlsTransportInternal* dtls_transport,
87                    DtlsTransportState state);
88 
89   // Override the SrtpTransport::OnWritableState.
90   void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
91 
92   // Owned by the TransportController.
93   cricket::DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
94   cricket::DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
95 
96   // The encrypted header extension IDs.
97   absl::optional<std::vector<int>> send_extension_ids_;
98   absl::optional<std::vector<int>> recv_extension_ids_;
99 
100   bool active_reset_srtp_params_ = false;
101   std::function<void(void)> on_dtls_state_change_;
102 };
103 
104 }  // namespace webrtc
105 
106 #endif  // PC_DTLS_SRTP_TRANSPORT_H_
107