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 | |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 19 | #include "p2p/base/icetransportinternal.h" |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 20 | #include "pc/rtptransportinternaladapter.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "pc/srtpfilter.h" |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 22 | #include "pc/srtpsession.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "rtc_base/checks.h" |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 24 | |
| 25 | namespace webrtc { |
| 26 | |
| 27 | // This class will eventually be a wrapper around RtpTransportInternal |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 28 | // that protects and unprotects sent and received RTP packets. |
Zhi Huang | f2d7beb | 2017-11-20 14:35:11 -0800 | [diff] [blame] | 29 | class SrtpTransport : public RtpTransportInternalAdapter { |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 30 | public: |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 31 | explicit SrtpTransport(bool rtcp_mux_enabled); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 32 | |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 33 | explicit SrtpTransport(std::unique_ptr<RtpTransportInternal> rtp_transport); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 34 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 35 | bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 36 | const rtc::PacketOptions& options, |
| 37 | int flags) override; |
| 38 | |
| 39 | bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 40 | const rtc::PacketOptions& options, |
| 41 | int flags) override; |
| 42 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 43 | // The transport becomes active if the send_session_ and recv_session_ are |
| 44 | // created. |
| 45 | bool IsActive() const; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 46 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 47 | // TODO(zstein): Remove this when we remove RtpTransportAdapter. |
| 48 | RtpTransportAdapter* GetInternal() override { return nullptr; } |
| 49 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 50 | // Create new send/recv sessions and set the negotiated crypto keys for RTP |
| 51 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 52 | // handshake. |
| 53 | bool SetRtpParams(int send_cs, |
| 54 | const uint8_t* send_key, |
| 55 | int send_key_len, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 56 | const std::vector<int>& send_extension_ids, |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 57 | int recv_cs, |
| 58 | const uint8_t* recv_key, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 59 | int recv_key_len, |
| 60 | const std::vector<int>& recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 61 | |
| 62 | // Create new send/recv sessions and set the negotiated crypto keys for RTCP |
| 63 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 64 | // handshake. |
| 65 | bool SetRtcpParams(int send_cs, |
| 66 | const uint8_t* send_key, |
| 67 | int send_key_len, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 68 | const std::vector<int>& send_extension_ids, |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 69 | int recv_cs, |
| 70 | const uint8_t* recv_key, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 71 | int recv_key_len, |
| 72 | const std::vector<int>& recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 73 | |
| 74 | void ResetParams(); |
| 75 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 76 | // If external auth is enabled, SRTP will write a dummy auth tag that then |
| 77 | // later must get replaced before the packet is sent out. Only supported for |
| 78 | // non-GCM cipher suites and can be checked through "IsExternalAuthActive" |
| 79 | // if it is actually used. This method is only valid before the RTP params |
| 80 | // have been set. |
| 81 | void EnableExternalAuth(); |
| 82 | bool IsExternalAuthEnabled() const; |
| 83 | |
| 84 | // A SrtpTransport supports external creation of the auth tag if a non-GCM |
| 85 | // cipher is used. This method is only valid after the RTP params have |
| 86 | // been set. |
| 87 | bool IsExternalAuthActive() const; |
| 88 | |
| 89 | // Returns srtp overhead for rtp packets. |
| 90 | bool GetSrtpOverhead(int* srtp_overhead) const; |
| 91 | |
| 92 | // Returns rtp auth params from srtp context. |
| 93 | bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 94 | |
Zhi Huang | 2a4d70c | 2017-11-29 15:41:59 -0800 | [diff] [blame] | 95 | // Cache RTP Absoulute SendTime extension header ID. This is only used when |
| 96 | // external authentication is enabled. |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 97 | void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) { |
| 98 | rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id; |
| 99 | } |
| 100 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 101 | private: |
| 102 | void ConnectToRtpTransport(); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 103 | void CreateSrtpSessions(); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 104 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 105 | bool SendPacket(bool rtcp, |
| 106 | rtc::CopyOnWriteBuffer* packet, |
| 107 | const rtc::PacketOptions& options, |
| 108 | int flags); |
| 109 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 110 | void OnPacketReceived(bool rtcp, |
| 111 | rtc::CopyOnWriteBuffer* packet, |
| 112 | const rtc::PacketTime& packet_time); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 113 | void OnReadyToSend(bool ready) { SignalReadyToSend(ready); } |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 114 | void OnNetworkRouteChanged(rtc::Optional<rtc::NetworkRoute> network_route); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 115 | |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 116 | void OnWritableState(bool writable) { SignalWritableState(writable); } |
| 117 | |
| 118 | void OnSentPacket(const rtc::SentPacket& sent_packet) { |
| 119 | SignalSentPacket(sent_packet); |
| 120 | } |
| 121 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 122 | bool ProtectRtp(void* data, int in_len, int max_len, int* out_len); |
zhihuang | eb23e17 | 2017-09-19 01:12:52 -0700 | [diff] [blame] | 123 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 124 | // Overloaded version, outputs packet index. |
| 125 | bool ProtectRtp(void* data, |
| 126 | int in_len, |
| 127 | int max_len, |
| 128 | int* out_len, |
| 129 | int64_t* index); |
| 130 | bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len); |
| 131 | |
| 132 | // Decrypts/verifies an invidiual RTP/RTCP packet. |
| 133 | // If an HMAC is used, this will decrease the packet size. |
| 134 | bool UnprotectRtp(void* data, int in_len, int* out_len); |
| 135 | |
| 136 | bool UnprotectRtcp(void* data, int in_len, int* out_len); |
| 137 | |
| 138 | const std::string content_name_; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 139 | std::unique_ptr<RtpTransportInternal> rtp_transport_; |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 140 | |
| 141 | std::unique_ptr<cricket::SrtpSession> send_session_; |
| 142 | std::unique_ptr<cricket::SrtpSession> recv_session_; |
| 143 | std::unique_ptr<cricket::SrtpSession> send_rtcp_session_; |
| 144 | std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_; |
| 145 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 146 | bool external_auth_enabled_ = false; |
| 147 | |
| 148 | int rtp_abs_sendtime_extn_id_ = -1; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | } // namespace webrtc |
| 152 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 153 | #endif // PC_SRTPTRANSPORT_H_ |