blob: c63a3ca5dd703a8a307c2dd25bfbb0ac6ea56db2 [file] [log] [blame]
Zhi Huangf2d7beb2017-11-20 14:35:11 -08001/*
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 09:11:00 -080011#ifndef PC_DTLS_SRTP_TRANSPORT_H_
12#define PC_DTLS_SRTP_TRANSPORT_H_
Zhi Huangf2d7beb2017-11-20 14:35:11 -080013
Zhi Huangf2d7beb2017-11-20 14:35:11 -080014#include <vector>
15
Yves Gerey3e707812018-11-28 16:47:49 +010016#include "absl/types/optional.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "api/crypto_params.h"
18#include "api/rtc_error.h"
19#include "p2p/base/dtls_transport_internal.h"
20#include "p2p/base/packet_transport_internal.h"
21#include "pc/srtp_transport.h"
Joachim Bauch5b32f232018-03-07 20:02:26 +010022#include "rtc_base/buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010023#include "rtc_base/third_party/sigslot/sigslot.h"
Zhi Huangf2d7beb2017-11-20 14:35:11 -080024
25namespace webrtc {
26
Zhi Huang365381f2018-04-13 16:44:34 -070027// The subclass of SrtpTransport is used for DTLS-SRTP. When the DTLS handshake
28// is finished, it extracts the keying materials from DtlsTransport and
29// configures the SrtpSessions in the base class.
30class DtlsSrtpTransport : public SrtpTransport {
Zhi Huangf2d7beb2017-11-20 14:35:11 -080031 public:
Zhi Huang365381f2018-04-13 16:44:34 -070032 explicit DtlsSrtpTransport(bool rtcp_mux_enabled);
Zhi Huangf2d7beb2017-11-20 14:35:11 -080033
34 // Set P2P layer RTP/RTCP DtlsTransports. When using RTCP-muxing,
35 // |rtcp_dtls_transport| is null.
36 void SetDtlsTransports(cricket::DtlsTransportInternal* rtp_dtls_transport,
37 cricket::DtlsTransportInternal* rtcp_dtls_transport);
38
39 void SetRtcpMuxEnabled(bool enable) override;
40
41 // Set the header extension ids that should be encrypted.
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080042 void UpdateSendEncryptedHeaderExtensionIds(
Zhi Huangf2d7beb2017-11-20 14:35:11 -080043 const std::vector<int>& send_extension_ids);
44
Zhi Huangcd3fc5d2017-11-29 10:41:57 -080045 void UpdateRecvEncryptedHeaderExtensionIds(
Zhi Huangf2d7beb2017-11-20 14:35:11 -080046 const std::vector<int>& recv_extension_ids);
47
Jonas Olsson635474e2018-10-18 15:58:17 +020048 sigslot::signal<DtlsSrtpTransport*, bool> SignalDtlsSrtpSetupFailure;
49 sigslot::signal<> SignalDtlsStateChange;
Zhi Huangf2d7beb2017-11-20 14:35:11 -080050
Zhi Huange830e682018-03-30 10:48:35 -070051 RTCError SetSrtpSendKey(const cricket::CryptoParams& params) override {
52 return RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
53 "Set SRTP keys for DTLS-SRTP is not supported.");
54 }
55 RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params) override {
56 return RTCError(RTCErrorType::UNSUPPORTED_OPERATION,
57 "Set SRTP keys for DTLS-SRTP is not supported.");
58 }
59
Zhi Huangb57e1692018-06-12 11:41:11 -070060 // If |active_reset_srtp_params_| is set to be true, the SRTP parameters will
61 // be reset whenever the DtlsTransports are reset.
62 void SetActiveResetSrtpParams(bool active_reset_srtp_params) {
63 active_reset_srtp_params_ = active_reset_srtp_params;
64 }
65
Zhi Huangf2d7beb2017-11-20 14:35:11 -080066 private:
67 bool IsDtlsActive();
68 bool IsDtlsConnected();
69 bool IsDtlsWritable();
70 bool DtlsHandshakeCompleted();
71 void MaybeSetupDtlsSrtp();
72 void SetupRtpDtlsSrtp();
73 void SetupRtcpDtlsSrtp();
74 bool ExtractParams(cricket::DtlsTransportInternal* dtls_transport,
75 int* selected_crypto_suite,
Joachim Bauch5b32f232018-03-07 20:02:26 +010076 rtc::ZeroOnFreeBuffer<unsigned char>* send_key,
77 rtc::ZeroOnFreeBuffer<unsigned char>* recv_key);
Zhi Huangf2d7beb2017-11-20 14:35:11 -080078 void SetDtlsTransport(cricket::DtlsTransportInternal* new_dtls_transport,
79 cricket::DtlsTransportInternal** old_dtls_transport);
80 void SetRtpDtlsTransport(cricket::DtlsTransportInternal* rtp_dtls_transport);
81 void SetRtcpDtlsTransport(
82 cricket::DtlsTransportInternal* rtcp_dtls_transport);
Zhi Huangf2d7beb2017-11-20 14:35:11 -080083
84 void OnDtlsState(cricket::DtlsTransportInternal* dtls_transport,
85 cricket::DtlsTransportState state);
Zhi Huangf2d7beb2017-11-20 14:35:11 -080086
Zhi Huang365381f2018-04-13 16:44:34 -070087 // Override the SrtpTransport::OnWritableState.
88 void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
89
Zhi Huangf2d7beb2017-11-20 14:35:11 -080090 // Owned by the TransportController.
91 cricket::DtlsTransportInternal* rtp_dtls_transport_ = nullptr;
92 cricket::DtlsTransportInternal* rtcp_dtls_transport_ = nullptr;
93
94 // The encrypted header extension IDs.
Danil Chapovalov66cadcc2018-06-19 16:47:43 +020095 absl::optional<std::vector<int>> send_extension_ids_;
96 absl::optional<std::vector<int>> recv_extension_ids_;
Zhi Huangb57e1692018-06-12 11:41:11 -070097
98 bool active_reset_srtp_params_ = false;
Zhi Huangf2d7beb2017-11-20 14:35:11 -080099};
100
101} // namespace webrtc
102
Steve Anton10542f22019-01-11 09:11:00 -0800103#endif // PC_DTLS_SRTP_TRANSPORT_H_