Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef PC_DTLS_SRTP_TRANSPORT_H_ |
| 12 | #define PC_DTLS_SRTP_TRANSPORT_H_ |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 13 | |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #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 Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 22 | #include "rtc_base/buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 27 | // 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. |
| 30 | class DtlsSrtpTransport : public SrtpTransport { |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 31 | public: |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 32 | explicit DtlsSrtpTransport(bool rtcp_mux_enabled); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 33 | |
| 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 Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 42 | void UpdateSendEncryptedHeaderExtensionIds( |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 43 | const std::vector<int>& send_extension_ids); |
| 44 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 45 | void UpdateRecvEncryptedHeaderExtensionIds( |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 46 | const std::vector<int>& recv_extension_ids); |
| 47 | |
Jonas Olsson | 635474e | 2018-10-18 15:58:17 +0200 | [diff] [blame] | 48 | sigslot::signal<DtlsSrtpTransport*, bool> SignalDtlsSrtpSetupFailure; |
| 49 | sigslot::signal<> SignalDtlsStateChange; |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 50 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 51 | 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 Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 60 | // 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 Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 66 | 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 Bauch | 5b32f23 | 2018-03-07 20:02:26 +0100 | [diff] [blame] | 76 | rtc::ZeroOnFreeBuffer<unsigned char>* send_key, |
| 77 | rtc::ZeroOnFreeBuffer<unsigned char>* recv_key); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 78 | 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 Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 83 | |
| 84 | void OnDtlsState(cricket::DtlsTransportInternal* dtls_transport, |
| 85 | cricket::DtlsTransportState state); |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 86 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 87 | // Override the SrtpTransport::OnWritableState. |
| 88 | void OnWritableState(rtc::PacketTransportInternal* packet_transport) override; |
| 89 | |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 90 | // 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 Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 95 | absl::optional<std::vector<int>> send_extension_ids_; |
| 96 | absl::optional<std::vector<int>> recv_extension_ids_; |
Zhi Huang | b57e169 | 2018-06-12 11:41:11 -0700 | [diff] [blame] | 97 | |
| 98 | bool active_reset_srtp_params_ = false; |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | } // namespace webrtc |
| 102 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 103 | #endif // PC_DTLS_SRTP_TRANSPORT_H_ |