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 | #include "pc/srtp_transport.h" |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 12 | |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 13 | #include <string.h> |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 14 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 15 | #include <string> |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 16 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 16:57:42 | [diff] [blame] | 17 | #include <vector> |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 18 | |
Mirko Bonadei | 57cabed | 2020-04-01 10:03:11 | [diff] [blame] | 19 | #include "absl/strings/match.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 20 | #include "media/base/rtp_utils.h" |
Danil Chapovalov | 99a71f4 | 2021-07-19 13:20:46 | [diff] [blame] | 21 | #include "modules/rtp_rtcp/source/rtp_util.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 22 | #include "pc/rtp_transport.h" |
| 23 | #include "pc/srtp_session.h" |
| 24 | #include "rtc_base/async_packet_socket.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 26 | #include "rtc_base/copy_on_write_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 27 | #include "rtc_base/logging.h" |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 28 | #include "rtc_base/numerics/safe_conversions.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 29 | #include "rtc_base/ssl_stream_adapter.h" |
Artem Titov | a76af0c | 2018-07-23 15:38:12 | [diff] [blame] | 30 | #include "rtc_base/third_party/base64/base64.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 31 | #include "rtc_base/trace_event.h" |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 32 | #include "rtc_base/zero_memory.h" |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 33 | |
| 34 | namespace webrtc { |
| 35 | |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 36 | SrtpTransport::SrtpTransport(bool rtcp_mux_enabled, |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 37 | const FieldTrialsView& field_trials) |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 38 | : RtpTransport(rtcp_mux_enabled), field_trials_(field_trials) {} |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 39 | |
Harald Alvestrand | 0d01841 | 2021-11-04 13:52:31 | [diff] [blame] | 40 | RTCError SrtpTransport::SetSrtpSendKey(const cricket::CryptoParams& params) { |
| 41 | if (send_params_) { |
| 42 | LOG_AND_RETURN_ERROR( |
| 43 | webrtc::RTCErrorType::UNSUPPORTED_OPERATION, |
| 44 | "Setting the SRTP send key twice is currently unsupported."); |
| 45 | } |
| 46 | if (recv_params_ && recv_params_->cipher_suite != params.cipher_suite) { |
| 47 | LOG_AND_RETURN_ERROR( |
| 48 | webrtc::RTCErrorType::UNSUPPORTED_OPERATION, |
| 49 | "The send key and receive key must have the same cipher suite."); |
| 50 | } |
| 51 | |
| 52 | send_cipher_suite_ = rtc::SrtpCryptoSuiteFromName(params.cipher_suite); |
| 53 | if (*send_cipher_suite_ == rtc::kSrtpInvalidCryptoSuite) { |
| 54 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 55 | "Invalid SRTP crypto suite"); |
| 56 | } |
| 57 | |
| 58 | int send_key_len, send_salt_len; |
| 59 | if (!rtc::GetSrtpKeyAndSaltLengths(*send_cipher_suite_, &send_key_len, |
| 60 | &send_salt_len)) { |
| 61 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 62 | "Could not get lengths for crypto suite(s):" |
| 63 | " send cipher_suite "); |
| 64 | } |
| 65 | |
| 66 | send_key_ = rtc::ZeroOnFreeBuffer<uint8_t>(send_key_len + send_salt_len); |
| 67 | if (!ParseKeyParams(params.key_params, send_key_.data(), send_key_.size())) { |
| 68 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 69 | "Failed to parse the crypto key params"); |
| 70 | } |
| 71 | |
| 72 | if (!MaybeSetKeyParams()) { |
| 73 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 74 | "Failed to set the crypto key params"); |
| 75 | } |
| 76 | send_params_ = params; |
| 77 | return RTCError::OK(); |
| 78 | } |
| 79 | |
| 80 | RTCError SrtpTransport::SetSrtpReceiveKey(const cricket::CryptoParams& params) { |
| 81 | if (recv_params_) { |
| 82 | LOG_AND_RETURN_ERROR( |
| 83 | webrtc::RTCErrorType::UNSUPPORTED_OPERATION, |
| 84 | "Setting the SRTP send key twice is currently unsupported."); |
| 85 | } |
| 86 | if (send_params_ && send_params_->cipher_suite != params.cipher_suite) { |
| 87 | LOG_AND_RETURN_ERROR( |
| 88 | webrtc::RTCErrorType::UNSUPPORTED_OPERATION, |
| 89 | "The send key and receive key must have the same cipher suite."); |
| 90 | } |
| 91 | |
| 92 | recv_cipher_suite_ = rtc::SrtpCryptoSuiteFromName(params.cipher_suite); |
| 93 | if (*recv_cipher_suite_ == rtc::kSrtpInvalidCryptoSuite) { |
| 94 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 95 | "Invalid SRTP crypto suite"); |
| 96 | } |
| 97 | |
| 98 | int recv_key_len, recv_salt_len; |
| 99 | if (!rtc::GetSrtpKeyAndSaltLengths(*recv_cipher_suite_, &recv_key_len, |
| 100 | &recv_salt_len)) { |
| 101 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 102 | "Could not get lengths for crypto suite(s):" |
| 103 | " recv cipher_suite "); |
| 104 | } |
| 105 | |
| 106 | recv_key_ = rtc::ZeroOnFreeBuffer<uint8_t>(recv_key_len + recv_salt_len); |
| 107 | if (!ParseKeyParams(params.key_params, recv_key_.data(), recv_key_.size())) { |
| 108 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 109 | "Failed to parse the crypto key params"); |
| 110 | } |
| 111 | |
| 112 | if (!MaybeSetKeyParams()) { |
| 113 | return RTCError(RTCErrorType::INVALID_PARAMETER, |
| 114 | "Failed to set the crypto key params"); |
| 115 | } |
| 116 | recv_params_ = params; |
| 117 | return RTCError::OK(); |
| 118 | } |
| 119 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 120 | bool SrtpTransport::SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 121 | const rtc::PacketOptions& options, |
| 122 | int flags) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 123 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 124 | RTC_LOG(LS_ERROR) |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 125 | << "Failed to send the packet because SRTP transport is inactive."; |
| 126 | return false; |
| 127 | } |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 128 | rtc::PacketOptions updated_options = options; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 129 | TRACE_EVENT0("webrtc", "SRTP Encode"); |
| 130 | bool res; |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 131 | uint8_t* data = packet->MutableData(); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 132 | int len = rtc::checked_cast<int>(packet->size()); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 133 | // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done |
| 134 | // inside libsrtp for a RTP packet. A external HMAC module will be writing |
| 135 | // a fake HMAC value. This is ONLY done for a RTP packet. |
| 136 | // Socket layer will update rtp sendtime extension header if present in |
| 137 | // packet with current time before updating the HMAC. |
| 138 | #if !defined(ENABLE_EXTERNAL_AUTH) |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 139 | res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len); |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 | [diff] [blame] | 140 | #else |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 141 | if (!IsExternalAuthActive()) { |
| 142 | res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len); |
| 143 | } else { |
| 144 | updated_options.packet_time_params.rtp_sendtime_extension_id = |
| 145 | rtp_abs_sendtime_extn_id_; |
| 146 | res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len, |
| 147 | &updated_options.packet_time_params.srtp_packet_index); |
| 148 | // If protection succeeds, let's get auth params from srtp. |
| 149 | if (res) { |
| 150 | uint8_t* auth_key = nullptr; |
| 151 | int key_len = 0; |
| 152 | res = GetRtpAuthParams( |
| 153 | &auth_key, &key_len, |
| 154 | &updated_options.packet_time_params.srtp_auth_tag_len); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 155 | if (res) { |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 156 | updated_options.packet_time_params.srtp_auth_key.resize(key_len); |
| 157 | updated_options.packet_time_params.srtp_auth_key.assign( |
| 158 | auth_key, auth_key + key_len); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 159 | } |
| 160 | } |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 161 | } |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 162 | #endif |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 163 | if (!res) { |
Danil Chapovalov | 99a71f4 | 2021-07-19 13:20:46 | [diff] [blame] | 164 | uint16_t seq_num = ParseRtpSequenceNumber(*packet); |
| 165 | uint32_t ssrc = ParseRtpSsrc(*packet); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 166 | RTC_LOG(LS_ERROR) << "Failed to protect RTP packet: size=" << len |
| 167 | << ", seqnum=" << seq_num << ", SSRC=" << ssrc; |
| 168 | return false; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // Update the length of the packet now that we've added the auth tag. |
| 172 | packet->SetSize(len); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 173 | return SendPacket(/*rtcp=*/false, packet, updated_options, flags); |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 174 | } |
| 175 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 176 | bool SrtpTransport::SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 177 | const rtc::PacketOptions& options, |
| 178 | int flags) { |
| 179 | if (!IsSrtpActive()) { |
| 180 | RTC_LOG(LS_ERROR) |
| 181 | << "Failed to send the packet because SRTP transport is inactive."; |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | TRACE_EVENT0("webrtc", "SRTP Encode"); |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 186 | uint8_t* data = packet->MutableData(); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 187 | int len = rtc::checked_cast<int>(packet->size()); |
| 188 | if (!ProtectRtcp(data, len, static_cast<int>(packet->capacity()), &len)) { |
| 189 | int type = -1; |
| 190 | cricket::GetRtcpType(data, len, &type); |
| 191 | RTC_LOG(LS_ERROR) << "Failed to protect RTCP packet: size=" << len |
| 192 | << ", type=" << type; |
| 193 | return false; |
| 194 | } |
| 195 | // Update the length of the packet now that we've added the auth tag. |
| 196 | packet->SetSize(len); |
| 197 | |
| 198 | return SendPacket(/*rtcp=*/true, packet, options, flags); |
| 199 | } |
| 200 | |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 201 | void SrtpTransport::OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 202 | int64_t packet_time_us) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 203 | TRACE_EVENT0("webrtc", "SrtpTransport::OnRtpPacketReceived"); |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 204 | if (!IsSrtpActive()) { |
Zhi Huang | 27f3bf5 | 2018-03-27 04:37:23 | [diff] [blame] | 205 | RTC_LOG(LS_WARNING) |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 206 | << "Inactive SRTP transport received an RTP packet. Drop it."; |
Zhi Huang | 27f3bf5 | 2018-03-27 04:37:23 | [diff] [blame] | 207 | return; |
| 208 | } |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 209 | char* data = packet.MutableData<char>(); |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 210 | int len = rtc::checked_cast<int>(packet.size()); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 211 | if (!UnprotectRtp(data, len, &len)) { |
erikvarga@webrtc.org | d76a0fc | 2018-10-09 10:31:28 | [diff] [blame] | 212 | // Limit the error logging to avoid excessive logs when there are lots of |
| 213 | // bad packets. |
| 214 | const int kFailureLogThrottleCount = 100; |
| 215 | if (decryption_failure_count_ % kFailureLogThrottleCount == 0) { |
| 216 | RTC_LOG(LS_ERROR) << "Failed to unprotect RTP packet: size=" << len |
Danil Chapovalov | 99a71f4 | 2021-07-19 13:20:46 | [diff] [blame] | 217 | << ", seqnum=" << ParseRtpSequenceNumber(packet) |
| 218 | << ", SSRC=" << ParseRtpSsrc(packet) |
erikvarga@webrtc.org | d76a0fc | 2018-10-09 10:31:28 | [diff] [blame] | 219 | << ", previous failure count: " |
| 220 | << decryption_failure_count_; |
| 221 | } |
| 222 | ++decryption_failure_count_; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 223 | return; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 224 | } |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 225 | packet.SetSize(len); |
| 226 | DemuxPacket(std::move(packet), packet_time_us); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 227 | } |
| 228 | |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 229 | void SrtpTransport::OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet, |
Niels Möller | e693381 | 2018-11-05 12:01:41 | [diff] [blame] | 230 | int64_t packet_time_us) { |
Markus Handell | 518669d | 2021-06-07 11:30:46 | [diff] [blame] | 231 | TRACE_EVENT0("webrtc", "SrtpTransport::OnRtcpPacketReceived"); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 232 | if (!IsSrtpActive()) { |
| 233 | RTC_LOG(LS_WARNING) |
| 234 | << "Inactive SRTP transport received an RTCP packet. Drop it."; |
| 235 | return; |
| 236 | } |
Danil Chapovalov | e15dc58 | 2021-01-07 14:24:05 | [diff] [blame] | 237 | char* data = packet.MutableData<char>(); |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 238 | int len = rtc::checked_cast<int>(packet.size()); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 239 | if (!UnprotectRtcp(data, len, &len)) { |
| 240 | int type = -1; |
| 241 | cricket::GetRtcpType(data, len, &type); |
| 242 | RTC_LOG(LS_ERROR) << "Failed to unprotect RTCP packet: size=" << len |
| 243 | << ", type=" << type; |
| 244 | return; |
| 245 | } |
Amit Hilbuch | edd2054 | 2019-03-18 19:33:43 | [diff] [blame] | 246 | packet.SetSize(len); |
| 247 | SignalRtcpPacketReceived(&packet, packet_time_us); |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 248 | } |
| 249 | |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 250 | void SrtpTransport::OnNetworkRouteChanged( |
Danil Chapovalov | 66cadcc | 2018-06-19 14:47:43 | [diff] [blame] | 251 | absl::optional<rtc::NetworkRoute> network_route) { |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 252 | // Only append the SRTP overhead when there is a selected network route. |
| 253 | if (network_route) { |
| 254 | int srtp_overhead = 0; |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 255 | if (IsSrtpActive()) { |
Zhi Huang | 942bc2e | 2017-11-13 21:26:07 | [diff] [blame] | 256 | GetSrtpOverhead(&srtp_overhead); |
| 257 | } |
| 258 | network_route->packet_overhead += srtp_overhead; |
| 259 | } |
| 260 | SignalNetworkRouteChanged(network_route); |
| 261 | } |
| 262 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 263 | void SrtpTransport::OnWritableState( |
| 264 | rtc::PacketTransportInternal* packet_transport) { |
Harald Alvestrand | c9a0985 | 2020-11-04 20:53:49 | [diff] [blame] | 265 | SignalWritableState(IsWritable(/*rtcp=*/false) && IsWritable(/*rtcp=*/true)); |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 266 | } |
| 267 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 268 | bool SrtpTransport::SetRtpParams(int send_cs, |
| 269 | const uint8_t* send_key, |
| 270 | int send_key_len, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 271 | const std::vector<int>& send_extension_ids, |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 272 | int recv_cs, |
| 273 | const uint8_t* recv_key, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 274 | int recv_key_len, |
| 275 | const std::vector<int>& recv_extension_ids) { |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 276 | // If parameters are being set for the first time, we should create new SRTP |
| 277 | // sessions and call "SetSend/SetRecv". Otherwise we should call |
| 278 | // "UpdateSend"/"UpdateRecv" on the existing sessions, which will internally |
| 279 | // call "srtp_update". |
| 280 | bool new_sessions = false; |
| 281 | if (!send_session_) { |
| 282 | RTC_DCHECK(!recv_session_); |
| 283 | CreateSrtpSessions(); |
| 284 | new_sessions = true; |
| 285 | } |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 286 | bool ret = new_sessions |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 287 | ? send_session_->SetSend(send_cs, send_key, send_key_len, |
| 288 | send_extension_ids) |
| 289 | : send_session_->UpdateSend(send_cs, send_key, send_key_len, |
| 290 | send_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 291 | if (!ret) { |
| 292 | ResetParams(); |
| 293 | return false; |
| 294 | } |
| 295 | |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 296 | ret = new_sessions ? recv_session_->SetRecv(recv_cs, recv_key, recv_key_len, |
| 297 | recv_extension_ids) |
| 298 | : recv_session_->UpdateRecv( |
| 299 | recv_cs, recv_key, recv_key_len, recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 300 | if (!ret) { |
| 301 | ResetParams(); |
| 302 | return false; |
| 303 | } |
| 304 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 305 | RTC_LOG(LS_INFO) << "SRTP " << (new_sessions ? "activated" : "updated") |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 306 | << " with negotiated parameters: send cipher_suite " |
| 307 | << send_cs << " recv cipher_suite " << recv_cs; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 308 | MaybeUpdateWritableState(); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
| 312 | bool SrtpTransport::SetRtcpParams(int send_cs, |
| 313 | const uint8_t* send_key, |
| 314 | int send_key_len, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 315 | const std::vector<int>& send_extension_ids, |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 316 | int recv_cs, |
| 317 | const uint8_t* recv_key, |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 318 | int recv_key_len, |
| 319 | const std::vector<int>& recv_extension_ids) { |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 320 | // This can only be called once, but can be safely called after |
| 321 | // SetRtpParams |
| 322 | if (send_rtcp_session_ || recv_rtcp_session_) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 323 | RTC_LOG(LS_ERROR) << "Tried to set SRTCP Params when filter already active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 327 | send_rtcp_session_.reset(new cricket::SrtpSession(field_trials_)); |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 328 | if (!send_rtcp_session_->SetSend(send_cs, send_key, send_key_len, |
| 329 | send_extension_ids)) { |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 333 | recv_rtcp_session_.reset(new cricket::SrtpSession(field_trials_)); |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 334 | if (!recv_rtcp_session_->SetRecv(recv_cs, recv_key, recv_key_len, |
| 335 | recv_extension_ids)) { |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 336 | return false; |
| 337 | } |
| 338 | |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 339 | RTC_LOG(LS_INFO) << "SRTCP activated with negotiated parameters:" |
Jonas Olsson | 45cc890 | 2018-02-13 09:37:07 | [diff] [blame] | 340 | " send cipher_suite " |
| 341 | << send_cs << " recv cipher_suite " << recv_cs; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 342 | MaybeUpdateWritableState(); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 343 | return true; |
| 344 | } |
| 345 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 346 | bool SrtpTransport::IsSrtpActive() const { |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 347 | return send_session_ && recv_session_; |
| 348 | } |
| 349 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 350 | bool SrtpTransport::IsWritable(bool rtcp) const { |
| 351 | return IsSrtpActive() && RtpTransport::IsWritable(rtcp); |
| 352 | } |
| 353 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 354 | void SrtpTransport::ResetParams() { |
| 355 | send_session_ = nullptr; |
| 356 | recv_session_ = nullptr; |
| 357 | send_rtcp_session_ = nullptr; |
| 358 | recv_rtcp_session_ = nullptr; |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 359 | MaybeUpdateWritableState(); |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 360 | RTC_LOG(LS_INFO) << "The params in SRTP transport are reset."; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 361 | } |
| 362 | |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 363 | void SrtpTransport::CreateSrtpSessions() { |
Jonas Oreland | ed99dae | 2022-03-09 08:28:10 | [diff] [blame] | 364 | send_session_.reset(new cricket::SrtpSession(field_trials_)); |
| 365 | recv_session_.reset(new cricket::SrtpSession(field_trials_)); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 366 | if (external_auth_enabled_) { |
| 367 | send_session_->EnableExternalAuth(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | bool SrtpTransport::ProtectRtp(void* p, int in_len, int max_len, int* out_len) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 372 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 373 | RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 374 | return false; |
| 375 | } |
| 376 | RTC_CHECK(send_session_); |
| 377 | return send_session_->ProtectRtp(p, in_len, max_len, out_len); |
| 378 | } |
| 379 | |
| 380 | bool SrtpTransport::ProtectRtp(void* p, |
| 381 | int in_len, |
| 382 | int max_len, |
| 383 | int* out_len, |
| 384 | int64_t* index) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 385 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 386 | RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | RTC_CHECK(send_session_); |
| 390 | return send_session_->ProtectRtp(p, in_len, max_len, out_len, index); |
| 391 | } |
| 392 | |
| 393 | bool SrtpTransport::ProtectRtcp(void* p, |
| 394 | int in_len, |
| 395 | int max_len, |
| 396 | int* out_len) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 397 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 398 | RTC_LOG(LS_WARNING) << "Failed to ProtectRtcp: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | if (send_rtcp_session_) { |
| 402 | return send_rtcp_session_->ProtectRtcp(p, in_len, max_len, out_len); |
| 403 | } else { |
| 404 | RTC_CHECK(send_session_); |
| 405 | return send_session_->ProtectRtcp(p, in_len, max_len, out_len); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | bool SrtpTransport::UnprotectRtp(void* p, int in_len, int* out_len) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 410 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 411 | RTC_LOG(LS_WARNING) << "Failed to UnprotectRtp: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 412 | return false; |
| 413 | } |
| 414 | RTC_CHECK(recv_session_); |
| 415 | return recv_session_->UnprotectRtp(p, in_len, out_len); |
| 416 | } |
| 417 | |
| 418 | bool SrtpTransport::UnprotectRtcp(void* p, int in_len, int* out_len) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 419 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 420 | RTC_LOG(LS_WARNING) << "Failed to UnprotectRtcp: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | if (recv_rtcp_session_) { |
| 424 | return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len); |
| 425 | } else { |
| 426 | RTC_CHECK(recv_session_); |
| 427 | return recv_session_->UnprotectRtcp(p, in_len, out_len); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | bool SrtpTransport::GetRtpAuthParams(uint8_t** key, |
| 432 | int* key_len, |
| 433 | int* tag_len) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 434 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 435 | RTC_LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 436 | return false; |
| 437 | } |
| 438 | |
| 439 | RTC_CHECK(send_session_); |
| 440 | return send_session_->GetRtpAuthParams(key, key_len, tag_len); |
| 441 | } |
| 442 | |
| 443 | bool SrtpTransport::GetSrtpOverhead(int* srtp_overhead) const { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 444 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 445 | RTC_LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | |
| 449 | RTC_CHECK(send_session_); |
| 450 | *srtp_overhead = send_session_->GetSrtpOverhead(); |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | void SrtpTransport::EnableExternalAuth() { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 455 | RTC_DCHECK(!IsSrtpActive()); |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 456 | external_auth_enabled_ = true; |
| 457 | } |
| 458 | |
| 459 | bool SrtpTransport::IsExternalAuthEnabled() const { |
| 460 | return external_auth_enabled_; |
| 461 | } |
| 462 | |
| 463 | bool SrtpTransport::IsExternalAuthActive() const { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 464 | if (!IsSrtpActive()) { |
Mirko Bonadei | 675513b | 2017-11-09 10:09:25 | [diff] [blame] | 465 | RTC_LOG(LS_WARNING) |
| 466 | << "Failed to check IsExternalAuthActive: SRTP not active"; |
Zhi Huang | cf990f5 | 2017-09-22 19:12:30 | [diff] [blame] | 467 | return false; |
| 468 | } |
| 469 | |
| 470 | RTC_CHECK(send_session_); |
| 471 | return send_session_->IsExternalAuthActive(); |
| 472 | } |
| 473 | |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 474 | bool SrtpTransport::MaybeSetKeyParams() { |
| 475 | if (!send_cipher_suite_ || !recv_cipher_suite_) { |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | return SetRtpParams(*send_cipher_suite_, send_key_.data(), |
| 480 | static_cast<int>(send_key_.size()), std::vector<int>(), |
| 481 | *recv_cipher_suite_, recv_key_.data(), |
| 482 | static_cast<int>(recv_key_.size()), std::vector<int>()); |
| 483 | } |
| 484 | |
| 485 | bool SrtpTransport::ParseKeyParams(const std::string& key_params, |
| 486 | uint8_t* key, |
| 487 | size_t len) { |
| 488 | // example key_params: "inline:YUJDZGVmZ2hpSktMbW9QUXJzVHVWd3l6MTIzNDU2" |
| 489 | |
| 490 | // Fail if key-method is wrong. |
Mirko Bonadei | 57cabed | 2020-04-01 10:03:11 | [diff] [blame] | 491 | if (!absl::StartsWith(key_params, "inline:")) { |
Zhi Huang | e830e68 | 2018-03-30 17:48:35 | [diff] [blame] | 492 | return false; |
| 493 | } |
| 494 | |
| 495 | // Fail if base64 decode fails, or the key is the wrong size. |
| 496 | std::string key_b64(key_params.substr(7)), key_str; |
| 497 | if (!rtc::Base64::Decode(key_b64, rtc::Base64::DO_STRICT, &key_str, |
| 498 | nullptr) || |
| 499 | key_str.size() != len) { |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | memcpy(key, key_str.c_str(), len); |
| 504 | // TODO(bugs.webrtc.org/8905): Switch to ZeroOnFreeBuffer for storing |
| 505 | // sensitive data. |
| 506 | rtc::ExplicitZeroMemory(&key_str[0], key_str.size()); |
| 507 | return true; |
| 508 | } |
| 509 | |
Zhi Huang | 365381f | 2018-04-13 23:44:34 | [diff] [blame] | 510 | void SrtpTransport::MaybeUpdateWritableState() { |
| 511 | bool writable = IsWritable(/*rtcp=*/true) && IsWritable(/*rtcp=*/false); |
| 512 | // Only fire the signal if the writable state changes. |
| 513 | if (writable_ != writable) { |
| 514 | writable_ = writable; |
| 515 | SignalWritableState(writable_); |
| 516 | } |
Steve Anton | db67ba1 | 2018-03-20 00:41:42 | [diff] [blame] | 517 | } |
| 518 | |
zstein | 398c3fd | 2017-07-19 20:38:02 | [diff] [blame] | 519 | } // namespace webrtc |