zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef PC_SRTPTRANSPORT_H_ |
| 12 | #define PC_SRTPTRANSPORT_H_ |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <utility> |
Steve Anton | 36b29d1 | 2017-10-30 09:57:42 -0700 | [diff] [blame^] | 17 | #include <vector> |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "pc/rtptransportinternal.h" |
| 20 | #include "pc/srtpfilter.h" |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 21 | #include "pc/srtpsession.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | // This class will eventually be a wrapper around RtpTransportInternal |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 27 | // that protects and unprotects sent and received RTP packets. |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 28 | class SrtpTransport : public RtpTransportInternal { |
| 29 | public: |
| 30 | SrtpTransport(bool rtcp_mux_enabled, const std::string& content_name); |
| 31 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 32 | SrtpTransport(std::unique_ptr<RtpTransportInternal> transport, |
| 33 | const std::string& content_name); |
| 34 | |
| 35 | void SetRtcpMuxEnabled(bool enable) override { |
| 36 | rtp_transport_->SetRtcpMuxEnabled(enable); |
| 37 | } |
| 38 | |
| 39 | rtc::PacketTransportInternal* rtp_packet_transport() const override { |
| 40 | return rtp_transport_->rtp_packet_transport(); |
| 41 | } |
| 42 | |
| 43 | void SetRtpPacketTransport(rtc::PacketTransportInternal* rtp) override { |
| 44 | rtp_transport_->SetRtpPacketTransport(rtp); |
| 45 | } |
| 46 | |
| 47 | PacketTransportInterface* GetRtpPacketTransport() const override { |
| 48 | return rtp_transport_->GetRtpPacketTransport(); |
| 49 | } |
| 50 | |
| 51 | rtc::PacketTransportInternal* rtcp_packet_transport() const override { |
| 52 | return rtp_transport_->rtcp_packet_transport(); |
| 53 | } |
| 54 | void SetRtcpPacketTransport(rtc::PacketTransportInternal* rtcp) override { |
| 55 | rtp_transport_->SetRtcpPacketTransport(rtcp); |
| 56 | } |
| 57 | |
| 58 | PacketTransportInterface* GetRtcpPacketTransport() const override { |
| 59 | return rtp_transport_->GetRtcpPacketTransport(); |
| 60 | } |
| 61 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 62 | bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 63 | const rtc::PacketOptions& options, |
| 64 | int flags) override; |
| 65 | |
| 66 | bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 67 | const rtc::PacketOptions& options, |
| 68 | int flags) override; |
| 69 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 70 | bool IsWritable(bool rtcp) const override { |
| 71 | return rtp_transport_->IsWritable(rtcp); |
| 72 | } |
| 73 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 74 | // The transport becomes active if the send_session_ and recv_session_ are |
| 75 | // created. |
| 76 | bool IsActive() const; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 77 | |
| 78 | bool HandlesPayloadType(int payload_type) const override { |
| 79 | return rtp_transport_->HandlesPayloadType(payload_type); |
| 80 | } |
| 81 | |
| 82 | void AddHandledPayloadType(int payload_type) override { |
| 83 | rtp_transport_->AddHandledPayloadType(payload_type); |
| 84 | } |
| 85 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 86 | RTCError SetParameters(const RtpTransportParameters& parameters) override { |
| 87 | return rtp_transport_->SetParameters(parameters); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 88 | } |
| 89 | |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 90 | RtpTransportParameters GetParameters() const override { |
| 91 | return rtp_transport_->GetParameters(); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // TODO(zstein): Remove this when we remove RtpTransportAdapter. |
| 95 | RtpTransportAdapter* GetInternal() override { return nullptr; } |
| 96 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 97 | // Create new send/recv sessions and set the negotiated crypto keys for RTP |
| 98 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 99 | // handshake. |
| 100 | bool SetRtpParams(int send_cs, |
| 101 | const uint8_t* send_key, |
| 102 | int send_key_len, |
| 103 | int recv_cs, |
| 104 | const uint8_t* recv_key, |
| 105 | int recv_key_len); |
| 106 | |
| 107 | // Create new send/recv sessions and set the negotiated crypto keys for RTCP |
| 108 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 109 | // handshake. |
| 110 | bool SetRtcpParams(int send_cs, |
| 111 | const uint8_t* send_key, |
| 112 | int send_key_len, |
| 113 | int recv_cs, |
| 114 | const uint8_t* recv_key, |
| 115 | int recv_key_len); |
| 116 | |
| 117 | void ResetParams(); |
| 118 | |
| 119 | // Set the header extension ids that should be encrypted for the given source. |
| 120 | // This method doesn't immediately update the SRTP session with the new IDs, |
| 121 | // and you need to call SetRtpParams for that to happen. |
| 122 | void SetEncryptedHeaderExtensionIds(cricket::ContentSource source, |
| 123 | const std::vector<int>& extension_ids); |
| 124 | |
| 125 | // If external auth is enabled, SRTP will write a dummy auth tag that then |
| 126 | // later must get replaced before the packet is sent out. Only supported for |
| 127 | // non-GCM cipher suites and can be checked through "IsExternalAuthActive" |
| 128 | // if it is actually used. This method is only valid before the RTP params |
| 129 | // have been set. |
| 130 | void EnableExternalAuth(); |
| 131 | bool IsExternalAuthEnabled() const; |
| 132 | |
| 133 | // A SrtpTransport supports external creation of the auth tag if a non-GCM |
| 134 | // cipher is used. This method is only valid after the RTP params have |
| 135 | // been set. |
| 136 | bool IsExternalAuthActive() const; |
| 137 | |
| 138 | // Returns srtp overhead for rtp packets. |
| 139 | bool GetSrtpOverhead(int* srtp_overhead) const; |
| 140 | |
| 141 | // Returns rtp auth params from srtp context. |
| 142 | bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 143 | |
| 144 | // Helper method to get RTP Absoulute SendTime extension header id if |
| 145 | // present in remote supported extensions list. |
| 146 | void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) { |
| 147 | rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id; |
| 148 | } |
| 149 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 150 | private: |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 151 | void CreateSrtpSessions(); |
| 152 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 153 | void ConnectToRtpTransport(); |
| 154 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 155 | bool SendPacket(bool rtcp, |
| 156 | rtc::CopyOnWriteBuffer* packet, |
| 157 | const rtc::PacketOptions& options, |
| 158 | int flags); |
| 159 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 160 | void OnPacketReceived(bool rtcp, |
| 161 | rtc::CopyOnWriteBuffer* packet, |
| 162 | const rtc::PacketTime& packet_time); |
| 163 | |
| 164 | void OnReadyToSend(bool ready) { SignalReadyToSend(ready); } |
| 165 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 166 | bool ProtectRtp(void* data, int in_len, int max_len, int* out_len); |
zhihuang | eb23e17 | 2017-09-19 01:12:52 -0700 | [diff] [blame] | 167 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 168 | // Overloaded version, outputs packet index. |
| 169 | bool ProtectRtp(void* data, |
| 170 | int in_len, |
| 171 | int max_len, |
| 172 | int* out_len, |
| 173 | int64_t* index); |
| 174 | bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len); |
| 175 | |
| 176 | // Decrypts/verifies an invidiual RTP/RTCP packet. |
| 177 | // If an HMAC is used, this will decrease the packet size. |
| 178 | bool UnprotectRtp(void* data, int in_len, int* out_len); |
| 179 | |
| 180 | bool UnprotectRtcp(void* data, int in_len, int* out_len); |
| 181 | |
| 182 | const std::string content_name_; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 183 | std::unique_ptr<RtpTransportInternal> rtp_transport_; |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 184 | |
| 185 | std::unique_ptr<cricket::SrtpSession> send_session_; |
| 186 | std::unique_ptr<cricket::SrtpSession> recv_session_; |
| 187 | std::unique_ptr<cricket::SrtpSession> send_rtcp_session_; |
| 188 | std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_; |
| 189 | |
| 190 | std::vector<int> send_encrypted_header_extension_ids_; |
| 191 | std::vector<int> recv_encrypted_header_extension_ids_; |
| 192 | bool external_auth_enabled_ = false; |
| 193 | |
| 194 | int rtp_abs_sendtime_extn_id_ = -1; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace webrtc |
| 198 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 199 | #endif // PC_SRTPTRANSPORT_H_ |