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