blob: 995809ed4b9604fa842c8bab2c5a3beb6512e202 [file] [log] [blame]
Zhi Huangf2d7beb2017-11-20 22:35:111/*
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
Steve Anton10542f22019-01-11 17:11:0011#ifndef PC_DTLS_SRTP_TRANSPORT_H_
12#define PC_DTLS_SRTP_TRANSPORT_H_
Zhi Huangf2d7beb2017-11-20 22:35:1113
Harald Alvestrandc24a2182022-02-23 13:44:5914#include <functional>
Harald Alvestrand5761e7b2021-01-29 14:45:0815#include <string>
Zhi Huangf2d7beb2017-11-20 22:35:1116#include <vector>
17
Yves Gerey3e707812018-11-28 15:47:4918#include "absl/types/optional.h"
Harald Alvestrand0d018412021-11-04 13:52:3119#include "api/crypto_params.h"
Mirko Bonadei9f6808b2021-05-21 18:46:0920#include "api/dtls_transport_interface.h"
Steve Anton10542f22019-01-11 17:11:0021#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"
Joachim Bauch5b32f232018-03-07 19:02:2625#include "rtc_base/buffer.h"
Zhi Huangf2d7beb2017-11-20 22:35:1126
27namespace webrtc {
28
Zhi Huang365381f2018-04-13 23:44:3429// The subclass of SrtpTransport is used for DTLS-SRTP. When the DTLS handshake
30// is finished, it extracts the keying materials from DtlsTransport and
31// configures the SrtpSessions in the base class.
32class DtlsSrtpTransport : public SrtpTransport {
Zhi Huangf2d7beb2017-11-20 22:35:1133 public:
Jonas Orelande62c2f22022-03-29 09:04:4834 DtlsSrtpTransport(bool rtcp_mux_enabled, const FieldTrialsView& field_trials);
Zhi Huangf2d7beb2017-11-20 22:35:1135
36 // Set P2P layer RTP/RTCP DtlsTransports. When using RTCP-muxing,
Artem Titov880fa812021-07-30 20:30:2337 // `rtcp_dtls_transport` is null.
Zhi Huangf2d7beb2017-11-20 22:35:1138 void SetDtlsTransports(cricket::DtlsTransportInternal* rtp_dtls_transport,
39 cricket::DtlsTransportInternal* rtcp_dtls_transport);
40
41 void SetRtcpMuxEnabled(bool enable) override;
42
43 // Set the header extension ids that should be encrypted.
Zhi Huangcd3fc5d2017-11-29 18:41:5744 void UpdateSendEncryptedHeaderExtensionIds(
Zhi Huangf2d7beb2017-11-20 22:35:1145 const std::vector<int>& send_extension_ids);
46
Zhi Huangcd3fc5d2017-11-29 18:41:5747 void UpdateRecvEncryptedHeaderExtensionIds(
Zhi Huangf2d7beb2017-11-20 22:35:1148 const std::vector<int>& recv_extension_ids);
49
Lahiru Ginnaliya Gamathigec32f00e2021-02-17 08:43:2150 void SetOnDtlsStateChange(std::function<void(void)> callback);
Zhi Huangf2d7beb2017-11-20 22:35:1151
Artem Titov880fa812021-07-30 20:30:2352 // If `active_reset_srtp_params_` is set to be true, the SRTP parameters will
Zhi Huangb57e1692018-06-12 18:41:1153 // be reset whenever the DtlsTransports are reset.
54 void SetActiveResetSrtpParams(bool active_reset_srtp_params) {
55 active_reset_srtp_params_ = active_reset_srtp_params;
56 }
57
Zhi Huangf2d7beb2017-11-20 22:35:1158 private:
59 bool IsDtlsActive();
60 bool IsDtlsConnected();
61 bool IsDtlsWritable();
62 bool DtlsHandshakeCompleted();
63 void MaybeSetupDtlsSrtp();
64 void SetupRtpDtlsSrtp();
65 void SetupRtcpDtlsSrtp();
66 bool ExtractParams(cricket::DtlsTransportInternal* dtls_transport,
67 int* selected_crypto_suite,
Joachim Bauch5b32f232018-03-07 19:02:2668 rtc::ZeroOnFreeBuffer<unsigned char>* send_key,
69 rtc::ZeroOnFreeBuffer<unsigned char>* recv_key);
Zhi Huangf2d7beb2017-11-20 22:35:1170 void SetDtlsTransport(cricket::DtlsTransportInternal* new_dtls_transport,
71 cricket::DtlsTransportInternal** old_dtls_transport);
72 void SetRtpDtlsTransport(cricket::DtlsTransportInternal* rtp_dtls_transport);
73 void SetRtcpDtlsTransport(
74 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huangf2d7beb2017-11-20 22:35:1175
76 void OnDtlsState(cricket::DtlsTransportInternal* dtls_transport,
Mirko Bonadei9f6808b2021-05-21 18:46:0977 DtlsTransportState state);
Zhi Huangf2d7beb2017-11-20 22:35:1178
Zhi Huang365381f2018-04-13 23:44:3479 // Override the SrtpTransport::OnWritableState.
80 void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
81
Zhi Huangf2d7beb2017-11-20 22:35:1182 // Owned by the TransportController.
83 cricket::DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
84 cricket::DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
85
86 // The encrypted header extension IDs.
Danil Chapovalov66cadcc2018-06-19 14:47:4387 absl::optional<std::vector<int>> send_extension_ids_;
88 absl::optional<std::vector<int>> recv_extension_ids_;
Zhi Huangb57e1692018-06-12 18:41:1189
90 bool active_reset_srtp_params_ = false;
Lahiru Ginnaliya Gamathigec32f00e2021-02-17 08:43:2191 std::function<void(void)> on_dtls_state_change_;
Zhi Huangf2d7beb2017-11-20 22:35:1192};
93
94} // namespace webrtc
95
Steve Anton10542f22019-01-11 17:11:0096#endif // PC_DTLS_SRTP_TRANSPORT_H_