xref: /aosp_15_r20/external/webrtc/pc/srtp_transport.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef PC_SRTP_TRANSPORT_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_SRTP_TRANSPORT_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include <cstdint>
17*d9f75844SAndroid Build Coastguard Worker #include <memory>
18*d9f75844SAndroid Build Coastguard Worker #include <string>
19*d9f75844SAndroid Build Coastguard Worker #include <vector>
20*d9f75844SAndroid Build Coastguard Worker 
21*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
22*d9f75844SAndroid Build Coastguard Worker #include "api/crypto_params.h"
23*d9f75844SAndroid Build Coastguard Worker #include "api/field_trials_view.h"
24*d9f75844SAndroid Build Coastguard Worker #include "api/rtc_error.h"
25*d9f75844SAndroid Build Coastguard Worker #include "p2p/base/packet_transport_internal.h"
26*d9f75844SAndroid Build Coastguard Worker #include "pc/rtp_transport.h"
27*d9f75844SAndroid Build Coastguard Worker #include "pc/srtp_session.h"
28*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_packet_socket.h"
29*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/buffer.h"
30*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/copy_on_write_buffer.h"
31*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/network_route.h"
32*d9f75844SAndroid Build Coastguard Worker 
33*d9f75844SAndroid Build Coastguard Worker namespace webrtc {
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker // This subclass of the RtpTransport is used for SRTP which is reponsible for
36*d9f75844SAndroid Build Coastguard Worker // protecting/unprotecting the packets. It provides interfaces to set the crypto
37*d9f75844SAndroid Build Coastguard Worker // parameters for the SrtpSession underneath.
38*d9f75844SAndroid Build Coastguard Worker class SrtpTransport : public RtpTransport {
39*d9f75844SAndroid Build Coastguard Worker  public:
40*d9f75844SAndroid Build Coastguard Worker   SrtpTransport(bool rtcp_mux_enabled, const FieldTrialsView& field_trials);
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   virtual ~SrtpTransport() = default;
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker   virtual RTCError SetSrtpSendKey(const cricket::CryptoParams& params);
45*d9f75844SAndroid Build Coastguard Worker   virtual RTCError SetSrtpReceiveKey(const cricket::CryptoParams& params);
46*d9f75844SAndroid Build Coastguard Worker 
47*d9f75844SAndroid Build Coastguard Worker   bool SendRtpPacket(rtc::CopyOnWriteBuffer* packet,
48*d9f75844SAndroid Build Coastguard Worker                      const rtc::PacketOptions& options,
49*d9f75844SAndroid Build Coastguard Worker                      int flags) override;
50*d9f75844SAndroid Build Coastguard Worker 
51*d9f75844SAndroid Build Coastguard Worker   bool SendRtcpPacket(rtc::CopyOnWriteBuffer* packet,
52*d9f75844SAndroid Build Coastguard Worker                       const rtc::PacketOptions& options,
53*d9f75844SAndroid Build Coastguard Worker                       int flags) override;
54*d9f75844SAndroid Build Coastguard Worker 
55*d9f75844SAndroid Build Coastguard Worker   // The transport becomes active if the send_session_ and recv_session_ are
56*d9f75844SAndroid Build Coastguard Worker   // created.
57*d9f75844SAndroid Build Coastguard Worker   bool IsSrtpActive() const override;
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   bool IsWritable(bool rtcp) const override;
60*d9f75844SAndroid Build Coastguard Worker 
61*d9f75844SAndroid Build Coastguard Worker   // Create new send/recv sessions and set the negotiated crypto keys for RTP
62*d9f75844SAndroid Build Coastguard Worker   // packet encryption. The keys can either come from SDES negotiation or DTLS
63*d9f75844SAndroid Build Coastguard Worker   // handshake.
64*d9f75844SAndroid Build Coastguard Worker   bool SetRtpParams(int send_cs,
65*d9f75844SAndroid Build Coastguard Worker                     const uint8_t* send_key,
66*d9f75844SAndroid Build Coastguard Worker                     int send_key_len,
67*d9f75844SAndroid Build Coastguard Worker                     const std::vector<int>& send_extension_ids,
68*d9f75844SAndroid Build Coastguard Worker                     int recv_cs,
69*d9f75844SAndroid Build Coastguard Worker                     const uint8_t* recv_key,
70*d9f75844SAndroid Build Coastguard Worker                     int recv_key_len,
71*d9f75844SAndroid Build Coastguard Worker                     const std::vector<int>& recv_extension_ids);
72*d9f75844SAndroid Build Coastguard Worker 
73*d9f75844SAndroid Build Coastguard Worker   // Create new send/recv sessions and set the negotiated crypto keys for RTCP
74*d9f75844SAndroid Build Coastguard Worker   // packet encryption. The keys can either come from SDES negotiation or DTLS
75*d9f75844SAndroid Build Coastguard Worker   // handshake.
76*d9f75844SAndroid Build Coastguard Worker   bool SetRtcpParams(int send_cs,
77*d9f75844SAndroid Build Coastguard Worker                      const uint8_t* send_key,
78*d9f75844SAndroid Build Coastguard Worker                      int send_key_len,
79*d9f75844SAndroid Build Coastguard Worker                      const std::vector<int>& send_extension_ids,
80*d9f75844SAndroid Build Coastguard Worker                      int recv_cs,
81*d9f75844SAndroid Build Coastguard Worker                      const uint8_t* recv_key,
82*d9f75844SAndroid Build Coastguard Worker                      int recv_key_len,
83*d9f75844SAndroid Build Coastguard Worker                      const std::vector<int>& recv_extension_ids);
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   void ResetParams();
86*d9f75844SAndroid Build Coastguard Worker 
87*d9f75844SAndroid Build Coastguard Worker   // If external auth is enabled, SRTP will write a dummy auth tag that then
88*d9f75844SAndroid Build Coastguard Worker   // later must get replaced before the packet is sent out. Only supported for
89*d9f75844SAndroid Build Coastguard Worker   // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
90*d9f75844SAndroid Build Coastguard Worker   // if it is actually used. This method is only valid before the RTP params
91*d9f75844SAndroid Build Coastguard Worker   // have been set.
92*d9f75844SAndroid Build Coastguard Worker   void EnableExternalAuth();
93*d9f75844SAndroid Build Coastguard Worker   bool IsExternalAuthEnabled() const;
94*d9f75844SAndroid Build Coastguard Worker 
95*d9f75844SAndroid Build Coastguard Worker   // A SrtpTransport supports external creation of the auth tag if a non-GCM
96*d9f75844SAndroid Build Coastguard Worker   // cipher is used. This method is only valid after the RTP params have
97*d9f75844SAndroid Build Coastguard Worker   // been set.
98*d9f75844SAndroid Build Coastguard Worker   bool IsExternalAuthActive() const;
99*d9f75844SAndroid Build Coastguard Worker 
100*d9f75844SAndroid Build Coastguard Worker   // Returns srtp overhead for rtp packets.
101*d9f75844SAndroid Build Coastguard Worker   bool GetSrtpOverhead(int* srtp_overhead) const;
102*d9f75844SAndroid Build Coastguard Worker 
103*d9f75844SAndroid Build Coastguard Worker   // Returns rtp auth params from srtp context.
104*d9f75844SAndroid Build Coastguard Worker   bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
105*d9f75844SAndroid Build Coastguard Worker 
106*d9f75844SAndroid Build Coastguard Worker   // Cache RTP Absoulute SendTime extension header ID. This is only used when
107*d9f75844SAndroid Build Coastguard Worker   // external authentication is enabled.
CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id)108*d9f75844SAndroid Build Coastguard Worker   void CacheRtpAbsSendTimeHeaderExtension(int rtp_abs_sendtime_extn_id) {
109*d9f75844SAndroid Build Coastguard Worker     rtp_abs_sendtime_extn_id_ = rtp_abs_sendtime_extn_id;
110*d9f75844SAndroid Build Coastguard Worker   }
111*d9f75844SAndroid Build Coastguard Worker 
112*d9f75844SAndroid Build Coastguard Worker  protected:
113*d9f75844SAndroid Build Coastguard Worker   // If the writable state changed, fire the SignalWritableState.
114*d9f75844SAndroid Build Coastguard Worker   void MaybeUpdateWritableState();
115*d9f75844SAndroid Build Coastguard Worker 
116*d9f75844SAndroid Build Coastguard Worker  private:
117*d9f75844SAndroid Build Coastguard Worker   void ConnectToRtpTransport();
118*d9f75844SAndroid Build Coastguard Worker   void CreateSrtpSessions();
119*d9f75844SAndroid Build Coastguard Worker 
120*d9f75844SAndroid Build Coastguard Worker   void OnRtpPacketReceived(rtc::CopyOnWriteBuffer packet,
121*d9f75844SAndroid Build Coastguard Worker                            int64_t packet_time_us) override;
122*d9f75844SAndroid Build Coastguard Worker   void OnRtcpPacketReceived(rtc::CopyOnWriteBuffer packet,
123*d9f75844SAndroid Build Coastguard Worker                             int64_t packet_time_us) override;
124*d9f75844SAndroid Build Coastguard Worker   void OnNetworkRouteChanged(
125*d9f75844SAndroid Build Coastguard Worker       absl::optional<rtc::NetworkRoute> network_route) override;
126*d9f75844SAndroid Build Coastguard Worker 
127*d9f75844SAndroid Build Coastguard Worker   // Override the RtpTransport::OnWritableState.
128*d9f75844SAndroid Build Coastguard Worker   void OnWritableState(rtc::PacketTransportInternal* packet_transport) override;
129*d9f75844SAndroid Build Coastguard Worker 
130*d9f75844SAndroid Build Coastguard Worker   bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
131*d9f75844SAndroid Build Coastguard Worker 
132*d9f75844SAndroid Build Coastguard Worker   // Overloaded version, outputs packet index.
133*d9f75844SAndroid Build Coastguard Worker   bool ProtectRtp(void* data,
134*d9f75844SAndroid Build Coastguard Worker                   int in_len,
135*d9f75844SAndroid Build Coastguard Worker                   int max_len,
136*d9f75844SAndroid Build Coastguard Worker                   int* out_len,
137*d9f75844SAndroid Build Coastguard Worker                   int64_t* index);
138*d9f75844SAndroid Build Coastguard Worker   bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
139*d9f75844SAndroid Build Coastguard Worker 
140*d9f75844SAndroid Build Coastguard Worker   // Decrypts/verifies an invidiual RTP/RTCP packet.
141*d9f75844SAndroid Build Coastguard Worker   // If an HMAC is used, this will decrease the packet size.
142*d9f75844SAndroid Build Coastguard Worker   bool UnprotectRtp(void* data, int in_len, int* out_len);
143*d9f75844SAndroid Build Coastguard Worker 
144*d9f75844SAndroid Build Coastguard Worker   bool UnprotectRtcp(void* data, int in_len, int* out_len);
145*d9f75844SAndroid Build Coastguard Worker 
146*d9f75844SAndroid Build Coastguard Worker   bool MaybeSetKeyParams();
147*d9f75844SAndroid Build Coastguard Worker   bool ParseKeyParams(const std::string& key_params, uint8_t* key, size_t len);
148*d9f75844SAndroid Build Coastguard Worker 
149*d9f75844SAndroid Build Coastguard Worker   const std::string content_name_;
150*d9f75844SAndroid Build Coastguard Worker 
151*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<cricket::SrtpSession> send_session_;
152*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<cricket::SrtpSession> recv_session_;
153*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<cricket::SrtpSession> send_rtcp_session_;
154*d9f75844SAndroid Build Coastguard Worker   std::unique_ptr<cricket::SrtpSession> recv_rtcp_session_;
155*d9f75844SAndroid Build Coastguard Worker 
156*d9f75844SAndroid Build Coastguard Worker   absl::optional<cricket::CryptoParams> send_params_;
157*d9f75844SAndroid Build Coastguard Worker   absl::optional<cricket::CryptoParams> recv_params_;
158*d9f75844SAndroid Build Coastguard Worker   absl::optional<int> send_cipher_suite_;
159*d9f75844SAndroid Build Coastguard Worker   absl::optional<int> recv_cipher_suite_;
160*d9f75844SAndroid Build Coastguard Worker   rtc::ZeroOnFreeBuffer<uint8_t> send_key_;
161*d9f75844SAndroid Build Coastguard Worker   rtc::ZeroOnFreeBuffer<uint8_t> recv_key_;
162*d9f75844SAndroid Build Coastguard Worker 
163*d9f75844SAndroid Build Coastguard Worker   bool writable_ = false;
164*d9f75844SAndroid Build Coastguard Worker 
165*d9f75844SAndroid Build Coastguard Worker   bool external_auth_enabled_ = false;
166*d9f75844SAndroid Build Coastguard Worker 
167*d9f75844SAndroid Build Coastguard Worker   int rtp_abs_sendtime_extn_id_ = -1;
168*d9f75844SAndroid Build Coastguard Worker 
169*d9f75844SAndroid Build Coastguard Worker   int decryption_failure_count_ = 0;
170*d9f75844SAndroid Build Coastguard Worker 
171*d9f75844SAndroid Build Coastguard Worker   const FieldTrialsView& field_trials_;
172*d9f75844SAndroid Build Coastguard Worker };
173*d9f75844SAndroid Build Coastguard Worker 
174*d9f75844SAndroid Build Coastguard Worker }  // namespace webrtc
175*d9f75844SAndroid Build Coastguard Worker 
176*d9f75844SAndroid Build Coastguard Worker #endif  // PC_SRTP_TRANSPORT_H_
177