OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/pc/srtptransport.h" | 11 #include "webrtc/pc/srtptransport.h" |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "webrtc/media/base/rtputils.h" | 15 #include "webrtc/media/base/rtputils.h" |
16 #include "webrtc/pc/rtptransport.h" | 16 #include "webrtc/pc/rtptransport.h" |
17 #include "webrtc/pc/srtpsession.h" | 17 #include "webrtc/pc/srtpsession.h" |
18 #include "webrtc/rtc_base/asyncpacketsocket.h" | 18 #include "webrtc/rtc_base/asyncpacketsocket.h" |
| 19 #include "webrtc/rtc_base/base64.h" |
19 #include "webrtc/rtc_base/copyonwritebuffer.h" | 20 #include "webrtc/rtc_base/copyonwritebuffer.h" |
20 #include "webrtc/rtc_base/ptr_util.h" | 21 #include "webrtc/rtc_base/ptr_util.h" |
| 22 #include "webrtc/rtc_base/thread.h" |
21 #include "webrtc/rtc_base/trace_event.h" | 23 #include "webrtc/rtc_base/trace_event.h" |
22 | 24 |
23 namespace webrtc { | 25 namespace webrtc { |
24 | 26 |
25 SrtpTransport::SrtpTransport(bool rtcp_mux_enabled, | 27 SrtpTransport::SrtpTransport(bool rtcp_mux_enabled, |
26 const std::string& content_name) | 28 const std::string& content_name) |
27 : content_name_(content_name), | 29 : content_name_(content_name), |
28 rtp_transport_(rtc::MakeUnique<RtpTransport>(rtcp_mux_enabled)) { | 30 rtp_transport_(rtc::MakeUnique<RtpTransport>(rtcp_mux_enabled)) { |
29 ConnectToRtpTransport(); | 31 ConnectToRtpTransport(); |
30 } | 32 } |
31 | 33 |
32 SrtpTransport::SrtpTransport(std::unique_ptr<RtpTransportInternal> transport, | 34 SrtpTransport::SrtpTransport(std::unique_ptr<RtpTransportInternal> transport, |
33 const std::string& content_name) | 35 const std::string& content_name) |
34 : content_name_(content_name), rtp_transport_(std::move(transport)) { | 36 : content_name_(content_name), rtp_transport_(std::move(transport)) { |
35 ConnectToRtpTransport(); | 37 ConnectToRtpTransport(); |
36 } | 38 } |
37 | 39 |
38 void SrtpTransport::ConnectToRtpTransport() { | 40 void SrtpTransport::ConnectToRtpTransport() { |
39 rtp_transport_->SignalPacketReceived.connect( | 41 rtp_transport_->SignalPacketReceived.connect( |
40 this, &SrtpTransport::OnPacketReceived); | 42 this, &SrtpTransport::OnPacketReceived); |
41 rtp_transport_->SignalReadyToSend.connect(this, | 43 rtp_transport_->SignalReadyToSend.connect(this, |
42 &SrtpTransport::OnReadyToSend); | 44 &SrtpTransport::OnReadyToSend); |
43 } | 45 } |
44 | 46 |
45 bool SrtpTransport::SendPacket(bool rtcp, | 47 bool SrtpTransport::SendPacket(bool rtcp, |
46 rtc::CopyOnWriteBuffer* packet, | 48 rtc::CopyOnWriteBuffer* packet, |
47 const rtc::PacketOptions& options, | 49 const rtc::PacketOptions& options, |
48 int flags) { | 50 int flags) { |
49 // TODO(zstein): Protect packet. | 51 rtc::CopyOnWriteBuffer cp = *packet; |
50 | 52 if (IsActive()) { |
| 53 TRACE_EVENT0("webrtc", "SRTP Encode"); |
| 54 bool res; |
| 55 uint8_t* data = packet->data(); |
| 56 int len = static_cast<int>(packet->size()); |
| 57 if (!rtcp) { |
| 58 // If ENABLE_EXTERNAL_AUTH flag is on then packet authentication is not done |
| 59 // inside libsrtp for a RTP packet. A external HMAC module will be writing |
| 60 // a fake HMAC value. This is ONLY done for a RTP packet. |
| 61 // Socket layer will update rtp sendtime extension header if present in |
| 62 // packet with current time before updating the HMAC. |
| 63 #if !defined(ENABLE_EXTERNAL_AUTH) |
| 64 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len); |
| 65 #else |
| 66 if (!IsExternalAuthActive()) { |
| 67 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len); |
| 68 } else { |
| 69 updated_options.packet_time_params.rtp_sendtime_extension_id = |
| 70 rtp_abs_sendtime_extn_id_; |
| 71 res = ProtectRtp(data, len, static_cast<int>(packet->capacity()), &len, |
| 72 &updated_options.packet_time_params.srtp_packet_index); |
| 73 // If protection succeeds, let's get auth params from srtp. |
| 74 if (res) { |
| 75 uint8_t* auth_key = NULL; |
| 76 int key_len; |
| 77 res = GetRtpAuthParams( |
| 78 &auth_key, &key_len, |
| 79 &updated_options.packet_time_params.srtp_auth_tag_len); |
| 80 if (res) { |
| 81 updated_options.packet_time_params.srtp_auth_key.resize(key_len); |
| 82 updated_options.packet_time_params.srtp_auth_key.assign( |
| 83 auth_key, auth_key + key_len); |
| 84 } |
| 85 } |
| 86 } |
| 87 #endif |
| 88 if (!res) { |
| 89 int seq_num = -1; |
| 90 uint32_t ssrc = 0; |
| 91 cricket::GetRtpSeqNum(data, len, &seq_num); |
| 92 cricket::GetRtpSsrc(data, len, &ssrc); |
| 93 LOG(LS_ERROR) << "Failed to protect " << content_name_ |
| 94 << " RTP packet: size=" << len << ", seqnum=" << seq_num |
| 95 << ", SSRC=" << ssrc; |
| 96 return false; |
| 97 } |
| 98 } else { |
| 99 res = ProtectRtcp(data, len, static_cast<int>(packet->capacity()), &len); |
| 100 if (!res) { |
| 101 int type = -1; |
| 102 cricket::GetRtcpType(data, len, &type); |
| 103 LOG(LS_ERROR) << "Failed to protect " << content_name_ |
| 104 << " RTCP packet: size=" << len << ", type=" << type; |
| 105 return false; |
| 106 } |
| 107 } |
| 108 |
| 109 // Update the length of the packet now that we've added the auth tag. |
| 110 packet->SetSize(len); |
| 111 } |
51 return rtp_transport_->SendPacket(rtcp, packet, options, flags); | 112 return rtp_transport_->SendPacket(rtcp, packet, options, flags); |
52 } | 113 } |
53 | 114 |
54 void SrtpTransport::OnPacketReceived(bool rtcp, | 115 void SrtpTransport::OnPacketReceived(bool rtcp, |
55 rtc::CopyOnWriteBuffer* packet, | 116 rtc::CopyOnWriteBuffer* packet, |
56 const rtc::PacketTime& packet_time) { | 117 const rtc::PacketTime& packet_time) { |
57 // TODO(zstein): Unprotect packet. | 118 if (IsActive()) { |
58 | 119 TRACE_EVENT0("webrtc", "SRTP Decode"); |
| 120 char* data = packet->data<char>(); |
| 121 int len = static_cast<int>(packet->size()); |
| 122 bool res; |
| 123 if (!rtcp) { |
| 124 res = UnprotectRtp(data, len, &len); |
| 125 if (!res) { |
| 126 int seq_num = -1; |
| 127 uint32_t ssrc = 0; |
| 128 cricket::GetRtpSeqNum(data, len, &seq_num); |
| 129 cricket::GetRtpSsrc(data, len, &ssrc); |
| 130 LOG(LS_ERROR) << "Failed to unprotect " << content_name_ |
| 131 << " RTP packet: size=" << len << ", seqnum=" << seq_num |
| 132 << ", SSRC=" << ssrc; |
| 133 return; |
| 134 } |
| 135 } else { |
| 136 res = UnprotectRtcp(data, len, &len); |
| 137 if (!res) { |
| 138 int type = -1; |
| 139 cricket::GetRtcpType(data, len, &type); |
| 140 LOG(LS_ERROR) << "Failed to unprotect " << content_name_ |
| 141 << " RTCP packet: size=" << len << ", type=" << type; |
| 142 return; |
| 143 } |
| 144 } |
| 145 |
| 146 packet->SetSize(len); |
| 147 } |
59 SignalPacketReceived(rtcp, packet, packet_time); | 148 SignalPacketReceived(rtcp, packet, packet_time); |
60 } | 149 } |
61 | 150 |
| 151 bool SrtpTransport::SetRtpParams(int send_cs, |
| 152 const uint8_t* send_key, |
| 153 int send_key_len, |
| 154 int recv_cs, |
| 155 const uint8_t* recv_key, |
| 156 int recv_key_len) { |
| 157 CreateSrtpSessions(); |
| 158 send_session_->SetEncryptedHeaderExtensionIds( |
| 159 send_encrypted_header_extension_ids_); |
| 160 if (external_auth_enabled_) { |
| 161 send_session_->EnableExternalAuth(); |
| 162 } |
| 163 if (!send_session_->SetSend(send_cs, send_key, send_key_len)) { |
| 164 send_session_ = nullptr; |
| 165 return false; |
| 166 } |
| 167 |
| 168 recv_session_->SetEncryptedHeaderExtensionIds( |
| 169 recv_encrypted_header_extension_ids_); |
| 170 if (!recv_session_->SetRecv(recv_cs, recv_key, recv_key_len)) { |
| 171 recv_session_ = nullptr; |
| 172 return false; |
| 173 } |
| 174 |
| 175 LOG(LS_INFO) << "SRTP activated with negotiated parameters:" |
| 176 << " send cipher_suite " << send_cs << " recv cipher_suite " |
| 177 << recv_cs; |
| 178 return true; |
| 179 } |
| 180 |
| 181 bool SrtpTransport::SetRtcpParams(int send_cs, |
| 182 const uint8_t* send_key, |
| 183 int send_key_len, |
| 184 int recv_cs, |
| 185 const uint8_t* recv_key, |
| 186 int recv_key_len) { |
| 187 // This can only be called once, but can be safely called after |
| 188 // SetRtpParams |
| 189 if (send_rtcp_session_ || recv_rtcp_session_) { |
| 190 LOG(LS_ERROR) << "Tried to set SRTCP Params when filter already active"; |
| 191 return false; |
| 192 } |
| 193 |
| 194 send_rtcp_session_.reset(new cricket::SrtpSession()); |
| 195 if (!send_rtcp_session_->SetRecv(send_cs, send_key, send_key_len)) { |
| 196 return false; |
| 197 } |
| 198 |
| 199 recv_rtcp_session_.reset(new cricket::SrtpSession()); |
| 200 if (!recv_rtcp_session_->SetRecv(recv_cs, recv_key, recv_key_len)) { |
| 201 return false; |
| 202 } |
| 203 |
| 204 LOG(LS_INFO) << "SRTCP activated with negotiated parameters:" |
| 205 << " send cipher_suite " << send_cs << " recv cipher_suite " |
| 206 << recv_cs; |
| 207 |
| 208 return true; |
| 209 } |
| 210 |
| 211 bool SrtpTransport::UpdateRtpParams(int send_cs, |
| 212 const uint8_t* send_key, |
| 213 int send_key_len, |
| 214 int recv_cs, |
| 215 const uint8_t* recv_key, |
| 216 int recv_key_len) { |
| 217 RTC_DCHECK(send_session_); |
| 218 RTC_DCHECK(recv_session_); |
| 219 send_session_->SetEncryptedHeaderExtensionIds( |
| 220 send_encrypted_header_extension_ids_); |
| 221 if (!send_session_->UpdateSend(send_cs, send_key, send_key_len)) { |
| 222 return false; |
| 223 } |
| 224 |
| 225 recv_session_->SetEncryptedHeaderExtensionIds( |
| 226 recv_encrypted_header_extension_ids_); |
| 227 if (!recv_session_->UpdateRecv(recv_cs, recv_key, recv_key_len)) { |
| 228 return false; |
| 229 } |
| 230 |
| 231 LOG(LS_INFO) << "SRTP updated with negotiated parameters:" |
| 232 << " send cipher_suite " << send_cs << " recv cipher_suite " |
| 233 << recv_cs; |
| 234 return true; |
| 235 } |
| 236 |
| 237 bool SrtpTransport::IsActive() const { |
| 238 return send_session_ && recv_session_; |
| 239 } |
| 240 |
| 241 void SrtpTransport::ResetParams() { |
| 242 send_session_ = nullptr; |
| 243 recv_session_ = nullptr; |
| 244 send_rtcp_session_ = nullptr; |
| 245 recv_rtcp_session_ = nullptr; |
| 246 LOG(LS_INFO) << "The params in SRTP transport are reset."; |
| 247 } |
| 248 |
| 249 void SrtpTransport::SetEncryptedHeaderExtensionIds( |
| 250 cricket::ContentSource source, |
| 251 const std::vector<int>& extension_ids) { |
| 252 if (source == cricket::CS_LOCAL) { |
| 253 recv_encrypted_header_extension_ids_ = extension_ids; |
| 254 } else { |
| 255 send_encrypted_header_extension_ids_ = extension_ids; |
| 256 } |
| 257 } |
| 258 |
| 259 void SrtpTransport::CreateSrtpSessions() { |
| 260 send_session_.reset(new cricket::SrtpSession()); |
| 261 recv_session_.reset(new cricket::SrtpSession()); |
| 262 |
| 263 if (external_auth_enabled_) { |
| 264 send_session_->EnableExternalAuth(); |
| 265 } |
| 266 } |
| 267 |
| 268 bool SrtpTransport::ProtectRtp(void* p, int in_len, int max_len, int* out_len) { |
| 269 if (!IsActive()) { |
| 270 LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active"; |
| 271 return false; |
| 272 } |
| 273 RTC_CHECK(send_session_); |
| 274 return send_session_->ProtectRtp(p, in_len, max_len, out_len); |
| 275 } |
| 276 |
| 277 bool SrtpTransport::ProtectRtp(void* p, |
| 278 int in_len, |
| 279 int max_len, |
| 280 int* out_len, |
| 281 int64_t* index) { |
| 282 if (!IsActive()) { |
| 283 LOG(LS_WARNING) << "Failed to ProtectRtp: SRTP not active"; |
| 284 return false; |
| 285 } |
| 286 RTC_CHECK(send_session_); |
| 287 return send_session_->ProtectRtp(p, in_len, max_len, out_len, index); |
| 288 } |
| 289 |
| 290 bool SrtpTransport::ProtectRtcp(void* p, |
| 291 int in_len, |
| 292 int max_len, |
| 293 int* out_len) { |
| 294 if (!IsActive()) { |
| 295 LOG(LS_WARNING) << "Failed to ProtectRtcp: SRTP not active"; |
| 296 return false; |
| 297 } |
| 298 if (send_rtcp_session_) { |
| 299 return send_rtcp_session_->ProtectRtcp(p, in_len, max_len, out_len); |
| 300 } else { |
| 301 RTC_CHECK(send_session_); |
| 302 return send_session_->ProtectRtcp(p, in_len, max_len, out_len); |
| 303 } |
| 304 } |
| 305 |
| 306 bool SrtpTransport::UnprotectRtp(void* p, int in_len, int* out_len) { |
| 307 if (!IsActive()) { |
| 308 LOG(LS_WARNING) << "Failed to UnprotectRtp: SRTP not active"; |
| 309 return false; |
| 310 } |
| 311 RTC_CHECK(recv_session_); |
| 312 return recv_session_->UnprotectRtp(p, in_len, out_len); |
| 313 } |
| 314 |
| 315 bool SrtpTransport::UnprotectRtcp(void* p, int in_len, int* out_len) { |
| 316 if (!IsActive()) { |
| 317 LOG(LS_WARNING) << "Failed to UnprotectRtcp: SRTP not active"; |
| 318 return false; |
| 319 } |
| 320 if (recv_rtcp_session_) { |
| 321 return recv_rtcp_session_->UnprotectRtcp(p, in_len, out_len); |
| 322 } else { |
| 323 RTC_CHECK(recv_session_); |
| 324 return recv_session_->UnprotectRtcp(p, in_len, out_len); |
| 325 } |
| 326 } |
| 327 |
| 328 bool SrtpTransport::GetRtpAuthParams(uint8_t** key, |
| 329 int* key_len, |
| 330 int* tag_len) { |
| 331 if (!IsActive()) { |
| 332 LOG(LS_WARNING) << "Failed to GetRtpAuthParams: SRTP not active"; |
| 333 return false; |
| 334 } |
| 335 |
| 336 RTC_CHECK(send_session_); |
| 337 return send_session_->GetRtpAuthParams(key, key_len, tag_len); |
| 338 } |
| 339 |
| 340 bool SrtpTransport::GetSrtpOverhead(int* srtp_overhead) const { |
| 341 if (!IsActive()) { |
| 342 LOG(LS_WARNING) << "Failed to GetSrtpOverhead: SRTP not active"; |
| 343 return false; |
| 344 } |
| 345 |
| 346 RTC_CHECK(send_session_); |
| 347 *srtp_overhead = send_session_->GetSrtpOverhead(); |
| 348 return true; |
| 349 } |
| 350 |
| 351 void SrtpTransport::EnableExternalAuth() { |
| 352 RTC_DCHECK(!IsActive()); |
| 353 external_auth_enabled_ = true; |
| 354 } |
| 355 |
| 356 bool SrtpTransport::IsExternalAuthEnabled() const { |
| 357 return external_auth_enabled_; |
| 358 } |
| 359 |
| 360 bool SrtpTransport::IsExternalAuthActive() const { |
| 361 if (!IsActive()) { |
| 362 LOG(LS_WARNING) << "Failed to check IsExternalAuthActive: SRTP not active"; |
| 363 return false; |
| 364 } |
| 365 |
| 366 RTC_CHECK(send_session_); |
| 367 return send_session_->IsExternalAuthActive(); |
| 368 } |
| 369 |
62 } // namespace webrtc | 370 } // namespace webrtc |
OLD | NEW |