| 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 |
| 11 | #include "pc/srtp_transport.h" |
| 12 |
| 13 | #include <string.h> |
| 14 |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 | #include <vector> |
| 18 |
| 19 | #include "absl/strings/match.h" |
| 20 | #include "media/base/rtp_utils.h" |
| 21 | #include "modules/rtp_rtcp/source/rtp_util.h" |
| 22 | #include "pc/rtp_transport.h" |
| 23 | #include "pc/srtp_session.h" |
| 24 | #include "rtc_base/async_packet_socket.h" |
| 25 | #include "rtc_base/checks.h" |
| 26 | #include "rtc_base/copy_on_write_buffer.h" |
| 27 | #include "rtc_base/logging.h" |
| 28 | #include "rtc_base/numerics/safe_conversions.h" |
| 29 | #include "rtc_base/ssl_stream_adapter.h" |
| 30 | #include "rtc_base/third_party/base64/base64.h" |
| 31 | #include "rtc_base/trace_event.h" |
| 32 | #include "rtc_base/zero_memory.h" |
| 33 |
| 34 | namespace webrtc { |
| 35 |
| 36 | SrtpTransport::SrtpTransport(bool rtcp_mux_enabled, |
| 37 | const FieldTrialsView& field_trials) |
| 38 | : RtpTransport(rtcp_mux_enabled), field_trials_(field_trials) {} |
| 39 |
| 40 | bool SrtpTransport::SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 41 | const rtc::PacketOptions& options, |
| 42 | int flags) { |
| 43 | if (!IsSrtpActive()) { |
| 44 | RTC_LOG(LS_ERROR) |
| 45 | << "Failed to send the packet because SRTP transport is inactive."; |
| 46 | return false; |
| 47 | } |
| 48 | rtc::PacketOptions updated_options = options; |
| 49 | TRACE_EVENT0("webrtc", "SRTP Encode"); |
| 50 | bool res; |
| 51 | uint8_t* data = packet->MutableData(); |
| 52 | int len = rtc::checked_cast<int>(packet->size()); |
| 53 | // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done |
| 54 | // inside libsrtp for a RTP packet. A external HMAC module will be writing |
| 55 | // a fake HMAC value. This is ONLY done for a RTP packet. |
| 56 | // Socket layer will update rtp sendtime extension header if present in |
| 57 | // packet with current time before updating the HMAC. |
| 58 | #if !defined(ENABLE_EXTERNAL_AUTH) |
| 59 | res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len); |
| 60 | #else |
| 61 | if (!IsExternalAuthActive()) { |
| 62 | res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len); |
| 63 | } else { |
| 64 | updated_options.packet_time_params.rtp_sendtime_extension_id = |
| 65 | rtp_abs_sendtime_extn_id_; |
| 66 | res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len, |
| 67 | &updated_options.packet_time_params.srtp_packet_index); |
| 68 | // If protection succeeds, let's get auth params from srtp. |
| 69 | if (res) { |
| 70 | uint8_t* auth_key = nullptr; |
| 71 | int key_len = 0; |
| 72 | res = GetRtpAuthParams( |
| 73 | &auth_key, &key_len, |
| 74 | &updated_options.packet_time_params.srtp_auth_tag_len); |
| 75 | if (res) { |
| 76 | updated_options.packet_time_params.srtp_auth_key.resize(key_len); |
| 77 | updated_options.packet_time_params.srtp_auth_key.assign( |
| 78 | auth_key, auth_key + key_len); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | #endif |
| 83 | if (!res) { |
| 84 | uint16_t seq_num = ParseRtpSequenceNumber(*packet); |
| 85 | uint32_t ssrc = ParseRtpSsrc(*packet); |
| 86 | RTC_LOG(LS_ERROR) << "Failed to protect RTP packet: size=" << len |
| 87 | << ", seqnum=" << seq_num << ", SSRC=" << ssrc; |
| 88 | return false; |
| 89 | } |
| 90 |
| 91 | // Update the length of the packet now that we've added the auth tag. |
| 92 | packet->SetSize(len); |
| 93 | return SendPacket(/*rtcp=*/false, packet, updated_options, flags); |
| 94 | } |
| 95 |
| 96 | bool SrtpTransport::SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 97 | const rtc::PacketOptions& options, |
| 98 | int flags) { |
| 99 | if (!IsSrtpActive()) { |
| 100 | RTC_LOG(LS_ERROR) |
| 101 | << "Failed to send the packet because SRTP transport is inactive."; |
| 102 | return false; |
| 103 | } |
| 104 |
| 105 | TRACE_EVENT0("webrtc", "SRTP Encode"); |
| 106 | uint8_t* data = packet->MutableData(); |
| 107 | int len = rtc::checked_cast<int>(packet->size()); |
| 108 | if (!ProtectRtcp(data, len, static_cast<int>(packet->capacity()), &len)) { |
| 109 | int type = -1; |
| 110 | cricket::GetRtcpType(data, len, &type); |
| 111 | RTC_LOG(LS_ERROR) << "Failed to protect RTCP packet: size=" << len |
| 112 | << ", type=" << type; |
| 113 | return false; |
| 114 | } |
| 115 | // Update the length of the packet now that we've added the auth tag. |
| 116 | packet->SetSize(len); |
| 117 |
| 118 | return SendPacket(/*rtcp=*/true, packet, options, flags); |
| 119 | } |
| 120 |
| 121 | void SrtpTransport::OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 122 | int64_t packet_time_us) { |
| 123 | TRACE_EVENT0("webrtc", "SrtpTransport::OnRtpPacketReceived"); |
| 124 | if (!IsSrtpActive()) { |
| 125 | RTC_LOG(LS_WARNING) |
| 126 | << "Inactive SRTP transport received an RTP packet. Drop it."; |
| 127 | return; |
| 128 | } |
| 129 | char* data = packet.MutableData<char>(); |
| 130 | int len = rtc::checked_cast<int>(packet.size()); |
| 131 | if (!UnprotectRtp(data, len, &len)) { |
| 132 | // Limit the error logging to avoid excessive logs when there are lots of |
| 133 | // bad packets. |
| 134 | const int kFailureLogThrottleCount = 100; |
| 135 | if (decryption_failure_count_ % kFailureLogThrottleCount == 0) { |
| 136 | RTC_LOG(LS_ERROR) << "Failed to unprotect RTP packet: size=" << len |
| 137 | << ", seqnum=" << ParseRtpSequenceNumber(packet) |
| 138 | << ", SSRC=" << ParseRtpSsrc(packet) |
| 139 | << ", previous failure count: " |
| 140 | << decryption_failure_count_; |
| 141 | } |
| 142 | ++decryption_failure_count_; |
| 143 | return; |
| 144 | } |
| 145 | packet.SetSize(len); |
| 146 | DemuxPacket(std::move(packet), packet_time_us); |
| 147 | } |
| 148 |
| 149 | void SrtpTransport::OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 150 | int64_t packet_time_us) { |
| 151 | TRACE_EVENT0("webrtc", "SrtpTransport::OnRtcpPacketReceived"); |
| 152 | if (!IsSrtpActive()) { |
| 153 | RTC_LOG(LS_WARNING) |
| 154 | << "Inactive SRTP transport received an RTCP packet. Drop it."; |
| 155 | return; |
| 156 | } |
| 157 | char* data = packet.MutableData<char>(); |
| 158 | int len = rtc::checked_cast<int>(packet.size()); |
| 159 | if (!UnprotectRtcp(data, len, &len)) { |
| 160 | int type = -1; |
| 161 | cricket::GetRtcpType(data, len, &type); |
| 162 | RTC_LOG(LS_ERROR) << "Failed to unprotect RTCP packet: size=" << len |
| 163 | << ", type=" << type; |
| 164 | return; |
| 165 | } |
| 166 | packet.SetSize(len); |
| 167 | SendRtcpPacketReceived(&packet, packet_time_us); |
| 168 | } |
| 169 |
| 170 | void SrtpTransport::OnNetworkRouteChanged( |
| 171 | absl::optional<rtc::NetworkRoute> network_route) { |
| 172 | // Only append the SRTP overhead when there is a selected network route. |
| 173 | if (network_route) { |
| 174 | int srtp_overhead = 0; |
| 175 | if (IsSrtpActive()) { |
| 176 | GetSrtpOverhead(&srtp_overhead); |
| 177 | } |
| 178 | network_route->packet_overhead += srtp_overhead; |
| 179 | } |
| 180 | SendNetworkRouteChanged(network_route); |
| 181 | } |
| 182 |
| 183 | void SrtpTransport::OnWritableState( |
| 184 | rtc::PacketTransportInternal* packet_transport) { |
| 185 | SendWritableState(IsWritable(/*rtcp=*/false) && IsWritable(/*rtcp=*/true)); |
| 186 | } |
| 187 |
| 188 | bool SrtpTransport::SetRtpParams(int send_crypto_suite, |
| 189 | const uint8_t* send_key, |
| 190 | int send_key_len, |
| 191 | const std::vector<int>& send_extension_ids, |
| 192 | int recv_crypto_suite, |
| 193 | const uint8_t* recv_key, |
| 194 | int recv_key_len, |
| 195 | const std::vector<int>& recv_extension_ids) { |
| 196 | // If parameters are being set for the first time, we should create new SRTP |
| 197 | // sessions and call "SetSend/SetRecv". Otherwise we should call |
| 198 | // "UpdateSend"/"UpdateRecv" on the existing sessions, which will internally |
| 199 | // call "srtp_update". |
| 200 | bool new_sessions = false; |
| 201 | if (!send_session_) { |
| 202 | RTC_DCHECK(!recv_session_); |
| 203 | CreateSrtpSessions(); |
| 204 | new_sessions = true; |
| 205 | } |
| 206 | bool ret = new_sessions |
| 207 | ? send_session_->SetSend(send_crypto_suite, send_key, |
| 208 | send_key_len, send_extension_ids) |
| 209 | : send_session_->UpdateSend(send_crypto_suite, send_key, |
| 210 | send_key_len, send_extension_ids); |
| 211 | if (!ret) { |
| 212 | ResetParams(); |
| 213 | return false; |
| 214 | } |
| 215 |
| 216 | ret = new_sessions |
| 217 | ? recv_session_->SetRecv(recv_crypto_suite, recv_key, recv_key_len, |
| 218 | recv_extension_ids) |
| 219 | : recv_session_->UpdateRecv(recv_crypto_suite, recv_key, |
| 220 | recv_key_len, recv_extension_ids); |
| 221 | if (!ret) { |
| 222 | ResetParams(); |
| 223 | return false; |
| 224 | } |
| 225 |
| 226 | RTC_LOG(LS_INFO) << "SRTP " << (new_sessions ? "activated" : "updated") |
| 227 | << " with negotiated parameters: send crypto_suite " |
| 228 | << send_crypto_suite << " recv crypto_suite " |
| 229 | << recv_crypto_suite; |
| 230 | MaybeUpdateWritableState(); |
| 231 | return true; |
| 232 | } |
| 233 |
| 234 | bool SrtpTransport::SetRtcpParams(int send_crypto_suite, |
| 235 | const uint8_t* send_key, |
| 236 | int send_key_len, |
| 237 | const std::vector<int>& send_extension_ids, |
| 238 | int recv_crypto_suite, |
| 239 | const uint8_t* recv_key, |
| 240 | int recv_key_len, |
| 241 | const std::vector<int>& recv_extension_ids) { |
| 242 | // This can only be called once, but can be safely called after |
| 243 | // SetRtpParams |
| 244 | if (send_rtcp_session_ || recv_rtcp_session_) { |
| 245 | RTC_LOG(LS_ERROR) << "Tried to set SRTCP Params when filter already active"; |
| 246 | return false; |
| 247 | } |
| 248 |
| 249 | send_rtcp_session_.reset(new cricket::SrtpSession(field_trials_)); |
| 250 | if (!send_rtcp_session_->SetSend(send_crypto_suite, send_key, send_key_len, |
| 251 | send_extension_ids)) { |
| 252 | return false; |
| 253 | } |
| 254 |
| 255 | recv_rtcp_session_.reset(new cricket::SrtpSession(field_trials_)); |
| 256 | if (!recv_rtcp_session_->SetRecv(recv_crypto_suite, recv_key, recv_key_len, |
| 257 | recv_extension_ids)) { |
| 258 | return false; |
| 259 | } |
| 260 |
| 261 | RTC_LOG(LS_INFO) << "SRTCP activated with negotiated parameters:" |
| 262 | " send crypto_suite " |
| 263 | << send_crypto_suite << " recv crypto_suite " |
| 264 | << recv_crypto_suite; |
| 265 | MaybeUpdateWritableState(); |
| 266 | return true; |
| 267 | } |
| 268 |
| 269 | bool SrtpTransport::IsSrtpActive() const { |
| 270 | return send_session_ && recv_session_; |
| 271 | } |
| 272 |
| 273 | bool SrtpTransport::IsWritable(bool rtcp) const { |
| 274 | return IsSrtpActive() && RtpTransport::IsWritable(rtcp); |
| 275 | } |
| 276 |
| 277 | void SrtpTransport::ResetParams() { |
| 278 | send_session_ = nullptr; |
| 279 | recv_session_ = nullptr; |
| 280 | send_rtcp_session_ = nullptr; |
| 281 | recv_rtcp_session_ = nullptr; |
| 282 | MaybeUpdateWritableState(); |
| 283 | RTC_LOG(LS_INFO) << "The params in SRTP transport are reset."; |
| 284 | } |
| 285 |
| 286 | void SrtpTransport::CreateSrtpSessions() { |
| 287 | send_session_.reset(new cricket::SrtpSession(field_trials_)); |
| 288 | recv_session_.reset(new cricket::SrtpSession(field_trials_)); |
| 289 | if (external_auth_enabled_) { |
| 290 | send_session_->EnableExternalAuth(); |
| 291 | } |
| 292 | } |
| 293 |
| 294 | bool SrtpTransport::ProtectRtp(void* p, int in_len, int max_len, int* out_len) { |
| 295 | if (!IsSrtpActive()) { |
| 296 | RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active"; |
| 297 | return false; |
| 298 | } |
| 299 | RTC_CHECK(send_session_); |
| 300 | return send_session_->ProtectRtp(p, in_len, max_len, out_len); |
| 301 | } |
| 302 |
| 303 | bool SrtpTransport::ProtectRtp(void* p, |
| 304 | int in_len, |
| 305 | int max_len, |
| 306 | int* out_len, |
| 307 | int64_t* index) { |
| 308 | if (!IsSrtpActive()) { |
| 309 | RTC_LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active"; |
| 310 | return false; |
| 311 | } |
| 312 | RTC_CHECK(send_session_); |
| 313 | return send_session_->ProtectRtp(p, in_len, max_len, out_len, index); |
| 314 | } |
| 315 |
| 316 | bool SrtpTransport::ProtectRtcp(void* p, |
| 317 | int in_len, |
| 318 | int max_len, |
| 319 | int* out_len) { |
| 320 | if (!IsSrtpActive()) { |
| 321 | RTC_LOG(LS_WARNING) << "Failed to ProtectRtcp: SRTP not active"; |
| 322 | return false; |
| 323 | } |
| 324 | if (send_rtcp_session_) { |
| 325 | return send_rtcp_session_->ProtectRtcp(p, in_len, max_len, out_len); |
| 326 | } else { |
| 327 | RTC_CHECK(send_session_); |
| 328 | return send_session_->ProtectRtcp(p, in_len, max_len, out_len); |
| 329 | } |
| 330 | } |
| 331 |
| 332 | bool SrtpTransport::UnprotectRtp(void* p, int in_len, int* out_len) { |
| 333 | if (!IsSrtpActive()) { |
| 334 | RTC_LOG(LS_WARNING) << "Failed to UnprotectRtp: SRTP not active"; |
| 335 | return false; |
| 336 | } |
| 337 | RTC_CHECK(recv_session_); |
| 338 | return recv_session_->UnprotectRtp(p, in_len, out_len); |
| 339 | } |
| 340 |
| 341 | bool SrtpTransport::UnprotectRtcp(void* p, int in_len, int* out_len) { |
| 342 | if (!IsSrtpActive()) { |
| 343 | RTC_LOG(LS_WARNING) << "Failed to UnprotectRtcp: SRTP not active"; |
| 344 | return false; |
| 345 | } |
| 346 | if (recv_rtcp_session_) { |
| 347 | return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len); |
| 348 | } else { |
| 349 | RTC_CHECK(recv_session_); |
| 350 | return recv_session_->UnprotectRtcp(p, in_len, out_len); |
| 351 | } |
| 352 | } |
| 353 |
| 354 | bool SrtpTransport::GetRtpAuthParams(uint8_t** key, |
| 355 | int* key_len, |
| 356 | int* tag_len) { |
| 357 | if (!IsSrtpActive()) { |
| 358 | RTC_LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active"; |
| 359 | return false; |
| 360 | } |
| 361 |
| 362 | RTC_CHECK(send_session_); |
| 363 | return send_session_->GetRtpAuthParams(key, key_len, tag_len); |
| 364 | } |
| 365 |
| 366 | bool SrtpTransport::GetSrtpOverhead(int* srtp_overhead) const { |
| 367 | if (!IsSrtpActive()) { |
| 368 | RTC_LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; |
| 369 | return false; |
| 370 | } |
| 371 |
| 372 | RTC_CHECK(send_session_); |
| 373 | *srtp_overhead = send_session_->GetSrtpOverhead(); |
| 374 | return true; |
| 375 | } |
| 376 |
| 377 | void SrtpTransport::EnableExternalAuth() { |
| 378 | RTC_DCHECK(!IsSrtpActive()); |
| 379 | external_auth_enabled_ = true; |
| 380 | } |
| 381 |
| 382 | bool SrtpTransport::IsExternalAuthEnabled() const { |
| 383 | return external_auth_enabled_; |
| 384 | } |
| 385 |
| 386 | bool SrtpTransport::IsExternalAuthActive() const { |
| 387 | if (!IsSrtpActive()) { |
| 388 | RTC_LOG(LS_WARNING) |
| 389 | << "Failed to check IsExternalAuthActive: SRTP not active"; |
| 390 | return false; |
| 391 | } |
| 392 |
| 393 | RTC_CHECK(send_session_); |
| 394 | return send_session_->IsExternalAuthActive(); |
| 395 | } |
| 396 |
| 397 | bool SrtpTransport::MaybeSetKeyParams() { |
| 398 | if (!send_crypto_suite_ || !recv_crypto_suite_) { |
| 399 | return true; |
| 400 | } |
| 401 |
| 402 | return SetRtpParams(*send_crypto_suite_, send_key_.data(), |
| 403 | static_cast<int>(send_key_.size()), std::vector<int>(), |
| 404 | *recv_crypto_suite_, recv_key_.data(), |
| 405 | static_cast<int>(recv_key_.size()), std::vector<int>()); |
| 406 | } |
| 407 |
| 408 | bool SrtpTransport::ParseKeyParams(const std::string& key_params, |
| 409 | uint8_t* key, |
| 410 | size_t len) { |
| 411 | // example key_params: "inline:YUJDZGVmZ2hpSktMbW9QUXJzVHVWd3l6MTIzNDU2" |
| 412 |
| 413 | // Fail if key-method is wrong. |
| 414 | if (!absl::StartsWith(key_params, "inline:")) { |
| 415 | return false; |
| 416 | } |
| 417 |
| 418 | // Fail if base64 decode fails, or the key is the wrong size. |
| 419 | std::string key_b64(key_params.substr(7)), key_str; |
| 420 | if (!rtc::Base64::Decode(key_b64, rtc::Base64::DO_STRICT, &key_str, |
| 421 | nullptr) || |
| 422 | key_str.size() != len) { |
| 423 | return false; |
| 424 | } |
| 425 |
| 426 | memcpy(key, key_str.c_str(), len); |
| 427 | // TODO(bugs.webrtc.org/8905): Switch to ZeroOnFreeBuffer for storing |
| 428 | // sensitive data. |
| 429 | rtc::ExplicitZeroMemory(&key_str[0], key_str.size()); |
| 430 | return true; |
| 431 | } |
| 432 |
| 433 | void SrtpTransport::MaybeUpdateWritableState() { |
| 434 | bool writable = IsWritable(/*rtcp=*/true) && IsWritable(/*rtcp=*/false); |
| 435 | // Only fire the signal if the writable state changes. |
| 436 | if (writable_ != writable) { |
| 437 | writable_ = writable; |
| 438 | SendWritableState(writable_); |
| 439 | } |
| 440 | } |
| 441 |
| 442 | bool SrtpTransport::UnregisterRtpDemuxerSink(RtpPacketSinkInterface* sink) { |
| 443 | if (recv_session_ && |
| 444 | field_trials_.IsEnabled("WebRTC-SrtpRemoveReceiveStream")) { |
| 445 | // Remove the SSRCs explicitly registered with the demuxer |
| 446 | // (via SDP negotiation) from the SRTP session. |
| 447 | for (const auto ssrc : GetSsrcsForSink(sink)) { |
| 448 | if (!recv_session_->RemoveSsrcFromSession(ssrc)) { |
| 449 | RTC_LOG(LS_WARNING) |
| 450 | << "Could not remove SSRC " << ssrc << " from SRTP session."; |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | return RtpTransport::UnregisterRtpDemuxerSink(sink); |
| 455 | } |
| 456 |
| 457 | } // namespace webrtc |
| 458 |