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