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