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