zstein | 398c3fd | 2017-07-19 20:38:02 | [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 17:11:00 | [diff] [blame] | 11 | #ifndef PC_SRTP_TRANSPORT_H_ |
| 12 | #define PC_SRTP_TRANSPORT_H_ |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <stddef.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 15 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 16 | #include <cstdint> |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 17 | #include <memory> |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 18 | #include <optional> |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 19 | #include <string> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 20 | #include <vector> |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 21 | |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 22 | #include "api/field_trials_view.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 23 | #include "api/rtc_error.h" |
| 24 | #include "p2p/base/packet_transport_internal.h" |
| 25 | #include "pc/rtp_transport.h" |
| 26 | #include "pc/srtp_session.h" |
| 27 | #include "rtc_base/async_packet_socket.h" |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 28 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 29 | #include "rtc_base/copy_on_write_buffer.h" |
| 30 | #include "rtc_base/network_route.h" |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 31 | |
| 32 | namespace webrtc { |
| 33 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 34 | // This subclass of the RtpTransport is used for SRTP which is reponsible for |
| 35 | // protecting/unprotecting the packets. It provides interfaces to set the crypto |
| 36 | // parameters for the SrtpSession underneath. |
| 37 | class SrtpTransport : public RtpTransport { |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 38 | public: |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 39 | SrtpTransport(bool rtcp_mux_enabled, const FieldTrialsView& field_trials); |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 40 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 41 | virtual ~SrtpTransport() = default; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 42 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 43 | bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 44 | const rtc::PacketOptions& options, |
| 45 | int flags) override; |
| 46 | |
| 47 | bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 48 | const rtc::PacketOptions& options, |
| 49 | int flags) override; |
| 50 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 51 | // The transport becomes active if the send_session_ and recv_session_ are |
| 52 | // created. |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 53 | bool IsSrtpActive() const override; |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 54 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 55 | bool IsWritable(bool rtcp) const override; |
| 56 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 57 | // Create new send/recv sessions and set the negotiated crypto keys for RTP |
| 58 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 59 | // handshake. |
Philipp Hancke | 55b89a8 | 2023-08-01 09:34:05 | [diff] [blame] | 60 | bool SetRtpParams(int send_crypto_suite, |
Philipp Hancke | 9a65339 | 2024-09-14 03:30:12 | [diff] [blame] | 61 | const rtc::ZeroOnFreeBuffer<uint8_t>& send_key, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 62 | const std::vector<int>& send_extension_ids, |
Philipp Hancke | 55b89a8 | 2023-08-01 09:34:05 | [diff] [blame] | 63 | int recv_crypto_suite, |
Philipp Hancke | 9a65339 | 2024-09-14 03:30:12 | [diff] [blame] | 64 | const rtc::ZeroOnFreeBuffer<uint8_t>& recv_key, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 65 | const std::vector<int>& recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 66 | |
| 67 | // Create new send/recv sessions and set the negotiated crypto keys for RTCP |
| 68 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 69 | // handshake. |
Philipp Hancke | 55b89a8 | 2023-08-01 09:34:05 | [diff] [blame] | 70 | bool SetRtcpParams(int send_crypto_suite, |
Philipp Hancke | 9a65339 | 2024-09-14 03:30:12 | [diff] [blame] | 71 | const rtc::ZeroOnFreeBuffer<uint8_t>& send_key, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 72 | const std::vector<int>& send_extension_ids, |
Philipp Hancke | 55b89a8 | 2023-08-01 09:34:05 | [diff] [blame] | 73 | int recv_crypto_suite, |
Philipp Hancke | 9a65339 | 2024-09-14 03:30:12 | [diff] [blame] | 74 | const rtc::ZeroOnFreeBuffer<uint8_t>& recv_key, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 75 | const std::vector<int>& recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 76 | |
| 77 | void ResetParams(); |
| 78 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 79 | // If external auth is enabled, SRTP will write a dummy auth tag that then |
| 80 | // later must get replaced before the packet is sent out. Only supported for |
Philipp Hancke | 55b89a8 | 2023-08-01 09:34:05 | [diff] [blame] | 81 | // non-GCM crypto suites and can be checked through "IsExternalAuthActive" |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 82 | // if it is actually used. This method is only valid before the RTP params |
| 83 | // have been set. |
| 84 | void EnableExternalAuth(); |
| 85 | bool IsExternalAuthEnabled() const; |
| 86 | |
| 87 | // A SrtpTransport supports external creation of the auth tag if a non-GCM |
| 88 | // cipher is used. This method is only valid after the RTP params have |
| 89 | // been set. |
| 90 | bool IsExternalAuthActive() const; |
| 91 | |
| 92 | // Returns srtp overhead for rtp packets. |
| 93 | bool GetSrtpOverhead(int* srtp_overhead) const; |
| 94 | |
| 95 | // Returns rtp auth params from srtp context. |
| 96 | bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 97 | |
Zhi Huang | 2a4d70c | 2017-11-29 23:41:59 | [diff] [blame] | 98 | // Cache RTP Absoulute SendTime extension header ID. This is only used when |
| 99 | // external authentication is enabled. |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 100 | void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) { |
| 101 | rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id; |
| 102 | } |
| 103 | |
Philipp Hancke | 977b56c | 2023-11-08 09:20:51 | [diff] [blame] | 104 | // In addition to unregistering the sink, the SRTP transport |
| 105 | // disassociates all SSRCs of the sink from libSRTP. |
| 106 | bool UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) override; |
| 107 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 108 | protected: |
| 109 | // If the writable state changed, fire the SignalWritableState. |
| 110 | void MaybeUpdateWritableState(); |
Steve Anton | db67ba1 | 2018-03-20 00:41:42 | [diff] [blame] | 111 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 112 | private: |
| 113 | void ConnectToRtpTransport(); |
Zhi Huang | cd3fc5d | 2017-11-29 18:41:57 | [diff] [blame] | 114 | void CreateSrtpSessions(); |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 115 | |
Per K | 9e0bf9b | 2024-02-27 08:25:11 | [diff] [blame] | 116 | void OnRtpPacketReceived(const rtc::ReceivedPacket& packet) override; |
| 117 | void OnRtcpPacketReceived(const rtc::ReceivedPacket& packet) override; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 118 | void OnNetworkRouteChanged( |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 119 | std::optional<rtc::NetworkRoute> network_route) override; |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 | [diff] [blame] | 120 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 121 | // Override the RtpTransport::OnWritableState. |
| 122 | void OnWritableState(rtc::PacketTransportInternal* packet_transport) override; |
Zhi Huang | cd3fc5d | 2017-11-29 18:41:57 | [diff] [blame] | 123 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 124 | bool ProtectRtp(void* data, int in_len, int max_len, int* out_len); |
zhihuang | eb23e17 | 2017-09-19 08:12:52 | [diff] [blame] | 125 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 126 | // Overloaded version, outputs packet index. |
| 127 | bool ProtectRtp(void* data, |
| 128 | int in_len, |
| 129 | int max_len, |
| 130 | int* out_len, |
| 131 | int64_t* index); |
| 132 | bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len); |
| 133 | |
| 134 | // Decrypts/verifies an invidiual RTP/RTCP packet. |
| 135 | // If an HMAC is used, this will decrease the packet size. |
| 136 | bool UnprotectRtp(void* data, int in_len, int* out_len); |
| 137 | |
| 138 | bool UnprotectRtcp(void* data, int in_len, int* out_len); |
| 139 | |
| 140 | const std::string content_name_; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 141 | |
| 142 | std::unique_ptr<cricket::SrtpSession> send_session_; |
| 143 | std::unique_ptr<cricket::SrtpSession> recv_session_; |
| 144 | std::unique_ptr<cricket::SrtpSession> send_rtcp_session_; |
| 145 | std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_; |
| 146 | |
Florent Castelli | 8037fc6 | 2024-08-29 13:00:40 | [diff] [blame] | 147 | std::optional<int> send_crypto_suite_; |
| 148 | std::optional<int> recv_crypto_suite_; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 149 | rtc::ZeroOnFreeBuffer<uint8_t> send_key_; |
| 150 | rtc::ZeroOnFreeBuffer<uint8_t> recv_key_; |
| 151 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 152 | bool writable_ = false; |
| 153 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 154 | bool external_auth_enabled_ = false; |
| 155 | |
| 156 | int rtp_abs_sendtime_extn_id_ = -1; |
erikvarga@webrtc.org | d76a0fc | 2018-10-09 10:31:28 | [diff] [blame] | 157 | |
| 158 | int decryption_failure_count_ = 0; |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 159 | |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 160 | const FieldTrialsView& field_trials_; |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | } // namespace webrtc |
| 164 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 165 | #endif // PC_SRTP_TRANSPORT_H_ |