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 | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 19 | #include "api/ortc/srtptransportinterface.h" |
| 20 | #include "p2p/base/dtlstransportinternal.h" |
Zhi Huang | 942bc2e | 2017-11-13 13:26:07 -0800 | [diff] [blame] | 21 | #include "p2p/base/icetransportinternal.h" |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 22 | #include "pc/rtptransport.h" |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 23 | #include "pc/srtpsession.h" |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 24 | #include "rtc_base/buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 25 | #include "rtc_base/checks.h" |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 29 | // This subclass of the RtpTransport is used for SRTP which is reponsible for |
| 30 | // protecting/unprotecting the packets. It provides interfaces to set the crypto |
| 31 | // parameters for the SrtpSession underneath. |
| 32 | class SrtpTransport : public RtpTransport { |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 33 | public: |
Zhi Huang | 2dfc42d | 2017-12-04 13:38:48 -0800 | [diff] [blame] | 34 | explicit SrtpTransport(bool rtcp_mux_enabled); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 35 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 36 | virtual ~SrtpTransport() = default; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 37 | |
| 38 | // SrtpTransportInterface specific implementation. |
| 39 | RTCError SetSrtpSendKey(const cricket::CryptoParams& params) override; |
| 40 | RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params) override; |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 41 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 42 | bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet, |
| 43 | const rtc::PacketOptions& options, |
| 44 | int flags) override; |
| 45 | |
| 46 | bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet, |
| 47 | const rtc::PacketOptions& options, |
| 48 | int flags) override; |
| 49 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 50 | // The transport becomes active if the send_session_ and recv_session_ are |
| 51 | // created. |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 52 | bool IsSrtpActive() const override; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 53 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 54 | bool IsWritable(bool rtcp) const override; |
| 55 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 56 | // Create new send/recv sessions and set the negotiated crypto keys for RTP |
| 57 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 58 | // handshake. |
| 59 | bool SetRtpParams(int send_cs, |
| 60 | const uint8_t* send_key, |
| 61 | int send_key_len, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 62 | const std::vector<int>& send_extension_ids, |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 63 | int recv_cs, |
| 64 | const uint8_t* recv_key, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 65 | int recv_key_len, |
| 66 | const std::vector<int>& recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 67 | |
| 68 | // Create new send/recv sessions and set the negotiated crypto keys for RTCP |
| 69 | // packet encryption. The keys can either come from SDES negotiation or DTLS |
| 70 | // handshake. |
| 71 | bool SetRtcpParams(int send_cs, |
| 72 | const uint8_t* send_key, |
| 73 | int send_key_len, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 74 | const std::vector<int>& send_extension_ids, |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 75 | int recv_cs, |
| 76 | const uint8_t* recv_key, |
Zhi Huang | c99b6c7 | 2017-11-10 16:44:46 -0800 | [diff] [blame] | 77 | int recv_key_len, |
| 78 | const std::vector<int>& recv_extension_ids); |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 79 | |
| 80 | void ResetParams(); |
| 81 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 82 | // If external auth is enabled, SRTP will write a dummy auth tag that then |
| 83 | // later must get replaced before the packet is sent out. Only supported for |
| 84 | // non-GCM cipher suites and can be checked through "IsExternalAuthActive" |
| 85 | // if it is actually used. This method is only valid before the RTP params |
| 86 | // have been set. |
| 87 | void EnableExternalAuth(); |
| 88 | bool IsExternalAuthEnabled() const; |
| 89 | |
| 90 | // A SrtpTransport supports external creation of the auth tag if a non-GCM |
| 91 | // cipher is used. This method is only valid after the RTP params have |
| 92 | // been set. |
| 93 | bool IsExternalAuthActive() const; |
| 94 | |
| 95 | // Returns srtp overhead for rtp packets. |
| 96 | bool GetSrtpOverhead(int* srtp_overhead) const; |
| 97 | |
| 98 | // Returns rtp auth params from srtp context. |
| 99 | bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 100 | |
Zhi Huang | 2a4d70c | 2017-11-29 15:41:59 -0800 | [diff] [blame] | 101 | // Cache RTP Absoulute SendTime extension header ID. This is only used when |
| 102 | // external authentication is enabled. |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 103 | void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) { |
| 104 | rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id; |
| 105 | } |
| 106 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 107 | protected: |
| 108 | // If the writable state changed, fire the SignalWritableState. |
| 109 | void MaybeUpdateWritableState(); |
Steve Anton | db67ba1 | 2018-03-19 17:41:42 -0700 | [diff] [blame] | 110 | |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 111 | private: |
| 112 | void ConnectToRtpTransport(); |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 113 | void CreateSrtpSessions(); |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 114 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 115 | void OnRtpPacketReceived(rtc::CopyOnWriteBuffer* packet, |
| 116 | const rtc::PacketTime& packet_time) override; |
| 117 | void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer* packet, |
| 118 | const rtc::PacketTime& packet_time) override; |
| 119 | void OnNetworkRouteChanged( |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 120 | absl::optional<rtc::NetworkRoute> network_route) override; |
Zhi Huang | 95e7dbb | 2018-03-29 00:08:03 +0000 | [diff] [blame] | 121 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 122 | // Override the RtpTransport::OnWritableState. |
| 123 | void OnWritableState(rtc::PacketTransportInternal* packet_transport) override; |
Zhi Huang | cd3fc5d | 2017-11-29 10:41:57 -0800 | [diff] [blame] | 124 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 125 | bool ProtectRtp(void* data, int in_len, int max_len, int* out_len); |
zhihuang | eb23e17 | 2017-09-19 01:12:52 -0700 | [diff] [blame] | 126 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 127 | // Overloaded version, outputs packet index. |
| 128 | bool ProtectRtp(void* data, |
| 129 | int in_len, |
| 130 | int max_len, |
| 131 | int* out_len, |
| 132 | int64_t* index); |
| 133 | bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len); |
| 134 | |
| 135 | // Decrypts/verifies an invidiual RTP/RTCP packet. |
| 136 | // If an HMAC is used, this will decrease the packet size. |
| 137 | bool UnprotectRtp(void* data, int in_len, int* out_len); |
| 138 | |
| 139 | bool UnprotectRtcp(void* data, int in_len, int* out_len); |
| 140 | |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 141 | bool MaybeSetKeyParams(); |
| 142 | bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len); |
| 143 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 144 | const std::string content_name_; |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 145 | |
| 146 | std::unique_ptr<cricket::SrtpSession> send_session_; |
| 147 | std::unique_ptr<cricket::SrtpSession> recv_session_; |
| 148 | std::unique_ptr<cricket::SrtpSession> send_rtcp_session_; |
| 149 | std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_; |
| 150 | |
Danil Chapovalov | 66cadcc | 2018-06-19 16:47:43 +0200 | [diff] [blame] | 151 | absl::optional<cricket::CryptoParams> send_params_; |
| 152 | absl::optional<cricket::CryptoParams> recv_params_; |
| 153 | absl::optional<int> send_cipher_suite_; |
| 154 | absl::optional<int> recv_cipher_suite_; |
Zhi Huang | e830e68 | 2018-03-30 10:48:35 -0700 | [diff] [blame] | 155 | rtc::ZeroOnFreeBuffer<uint8_t> send_key_; |
| 156 | rtc::ZeroOnFreeBuffer<uint8_t> recv_key_; |
| 157 | |
Zhi Huang | 365381f | 2018-04-13 16:44:34 -0700 | [diff] [blame] | 158 | bool writable_ = false; |
| 159 | |
Zhi Huang | cf990f5 | 2017-09-22 12:12:30 -0700 | [diff] [blame] | 160 | bool external_auth_enabled_ = false; |
| 161 | |
| 162 | int rtp_abs_sendtime_extn_id_ = -1; |
erikvarga@webrtc.org | d76a0fc | 2018-10-09 12:31:28 +0200 | [diff] [blame^] | 163 | |
| 164 | int decryption_failure_count_ = 0; |
zstein | 398c3fd | 2017-07-19 13:38:02 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // namespace webrtc |
| 168 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 169 | #endif // PC_SRTPTRANSPORT_H_ |