xref: /aosp_15_r20/external/webrtc/pc/srtp_session.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_SESSION_H_
12*d9f75844SAndroid Build Coastguard Worker #define PC_SRTP_SESSION_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stddef.h>
15*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
16*d9f75844SAndroid Build Coastguard Worker 
17*d9f75844SAndroid Build Coastguard Worker #include <vector>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "api/field_trials_view.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/scoped_refptr.h"
21*d9f75844SAndroid Build Coastguard Worker #include "api/sequence_checker.h"
22*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
23*d9f75844SAndroid Build Coastguard Worker 
24*d9f75844SAndroid Build Coastguard Worker // Forward declaration to avoid pulling in libsrtp headers here
25*d9f75844SAndroid Build Coastguard Worker struct srtp_event_data_t;
26*d9f75844SAndroid Build Coastguard Worker struct srtp_ctx_t_;
27*d9f75844SAndroid Build Coastguard Worker 
28*d9f75844SAndroid Build Coastguard Worker namespace cricket {
29*d9f75844SAndroid Build Coastguard Worker 
30*d9f75844SAndroid Build Coastguard Worker // Prohibits webrtc from initializing libsrtp. This can be used if libsrtp is
31*d9f75844SAndroid Build Coastguard Worker // initialized by another library or explicitly. Note that this must be called
32*d9f75844SAndroid Build Coastguard Worker // before creating an SRTP session with WebRTC.
33*d9f75844SAndroid Build Coastguard Worker void ProhibitLibsrtpInitialization();
34*d9f75844SAndroid Build Coastguard Worker 
35*d9f75844SAndroid Build Coastguard Worker // Class that wraps a libSRTP session.
36*d9f75844SAndroid Build Coastguard Worker class SrtpSession {
37*d9f75844SAndroid Build Coastguard Worker  public:
38*d9f75844SAndroid Build Coastguard Worker   SrtpSession();
39*d9f75844SAndroid Build Coastguard Worker   explicit SrtpSession(const webrtc::FieldTrialsView& field_trials);
40*d9f75844SAndroid Build Coastguard Worker   ~SrtpSession();
41*d9f75844SAndroid Build Coastguard Worker 
42*d9f75844SAndroid Build Coastguard Worker   SrtpSession(const SrtpSession&) = delete;
43*d9f75844SAndroid Build Coastguard Worker   SrtpSession& operator=(const SrtpSession&) = delete;
44*d9f75844SAndroid Build Coastguard Worker 
45*d9f75844SAndroid Build Coastguard Worker   // Configures the session for sending data using the specified
46*d9f75844SAndroid Build Coastguard Worker   // cipher-suite and key. Receiving must be done by a separate session.
47*d9f75844SAndroid Build Coastguard Worker   bool SetSend(int cs,
48*d9f75844SAndroid Build Coastguard Worker                const uint8_t* key,
49*d9f75844SAndroid Build Coastguard Worker                size_t len,
50*d9f75844SAndroid Build Coastguard Worker                const std::vector<int>& extension_ids);
51*d9f75844SAndroid Build Coastguard Worker   bool UpdateSend(int cs,
52*d9f75844SAndroid Build Coastguard Worker                   const uint8_t* key,
53*d9f75844SAndroid Build Coastguard Worker                   size_t len,
54*d9f75844SAndroid Build Coastguard Worker                   const std::vector<int>& extension_ids);
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker   // Configures the session for receiving data using the specified
57*d9f75844SAndroid Build Coastguard Worker   // cipher-suite and key. Sending must be done by a separate session.
58*d9f75844SAndroid Build Coastguard Worker   bool SetRecv(int cs,
59*d9f75844SAndroid Build Coastguard Worker                const uint8_t* key,
60*d9f75844SAndroid Build Coastguard Worker                size_t len,
61*d9f75844SAndroid Build Coastguard Worker                const std::vector<int>& extension_ids);
62*d9f75844SAndroid Build Coastguard Worker   bool UpdateRecv(int cs,
63*d9f75844SAndroid Build Coastguard Worker                   const uint8_t* key,
64*d9f75844SAndroid Build Coastguard Worker                   size_t len,
65*d9f75844SAndroid Build Coastguard Worker                   const std::vector<int>& extension_ids);
66*d9f75844SAndroid Build Coastguard Worker 
67*d9f75844SAndroid Build Coastguard Worker   // Encrypts/signs an individual RTP/RTCP packet, in-place.
68*d9f75844SAndroid Build Coastguard Worker   // If an HMAC is used, this will increase the packet size.
69*d9f75844SAndroid Build Coastguard Worker   bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
70*d9f75844SAndroid Build Coastguard Worker   // Overloaded version, outputs packet index.
71*d9f75844SAndroid Build Coastguard Worker   bool ProtectRtp(void* data,
72*d9f75844SAndroid Build Coastguard Worker                   int in_len,
73*d9f75844SAndroid Build Coastguard Worker                   int max_len,
74*d9f75844SAndroid Build Coastguard Worker                   int* out_len,
75*d9f75844SAndroid Build Coastguard Worker                   int64_t* index);
76*d9f75844SAndroid Build Coastguard Worker   bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
77*d9f75844SAndroid Build Coastguard Worker   // Decrypts/verifies an invidiual RTP/RTCP packet.
78*d9f75844SAndroid Build Coastguard Worker   // If an HMAC is used, this will decrease the packet size.
79*d9f75844SAndroid Build Coastguard Worker   bool UnprotectRtp(void* data, int in_len, int* out_len);
80*d9f75844SAndroid Build Coastguard Worker   bool UnprotectRtcp(void* data, int in_len, int* out_len);
81*d9f75844SAndroid Build Coastguard Worker 
82*d9f75844SAndroid Build Coastguard Worker   // Helper method to get authentication params.
83*d9f75844SAndroid Build Coastguard Worker   bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
84*d9f75844SAndroid Build Coastguard Worker 
85*d9f75844SAndroid Build Coastguard Worker   int GetSrtpOverhead() const;
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 SRTP session 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  private:
101*d9f75844SAndroid Build Coastguard Worker   bool DoSetKey(int type,
102*d9f75844SAndroid Build Coastguard Worker                 int cs,
103*d9f75844SAndroid Build Coastguard Worker                 const uint8_t* key,
104*d9f75844SAndroid Build Coastguard Worker                 size_t len,
105*d9f75844SAndroid Build Coastguard Worker                 const std::vector<int>& extension_ids);
106*d9f75844SAndroid Build Coastguard Worker   bool SetKey(int type,
107*d9f75844SAndroid Build Coastguard Worker               int cs,
108*d9f75844SAndroid Build Coastguard Worker               const uint8_t* key,
109*d9f75844SAndroid Build Coastguard Worker               size_t len,
110*d9f75844SAndroid Build Coastguard Worker               const std::vector<int>& extension_ids);
111*d9f75844SAndroid Build Coastguard Worker   bool UpdateKey(int type,
112*d9f75844SAndroid Build Coastguard Worker                  int cs,
113*d9f75844SAndroid Build Coastguard Worker                  const uint8_t* key,
114*d9f75844SAndroid Build Coastguard Worker                  size_t len,
115*d9f75844SAndroid Build Coastguard Worker                  const std::vector<int>& extension_ids);
116*d9f75844SAndroid Build Coastguard Worker   // Returns send stream current packet index from srtp db.
117*d9f75844SAndroid Build Coastguard Worker   bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index);
118*d9f75844SAndroid Build Coastguard Worker 
119*d9f75844SAndroid Build Coastguard Worker   // Writes unencrypted packets in text2pcap format to the log file
120*d9f75844SAndroid Build Coastguard Worker   // for debugging.
121*d9f75844SAndroid Build Coastguard Worker   void DumpPacket(const void* buf, int len, bool outbound);
122*d9f75844SAndroid Build Coastguard Worker 
123*d9f75844SAndroid Build Coastguard Worker   void HandleEvent(const srtp_event_data_t* ev);
124*d9f75844SAndroid Build Coastguard Worker   static void HandleEventThunk(srtp_event_data_t* ev);
125*d9f75844SAndroid Build Coastguard Worker 
126*d9f75844SAndroid Build Coastguard Worker   webrtc::SequenceChecker thread_checker_;
127*d9f75844SAndroid Build Coastguard Worker   srtp_ctx_t_* session_ = nullptr;
128*d9f75844SAndroid Build Coastguard Worker 
129*d9f75844SAndroid Build Coastguard Worker   // Overhead of the SRTP auth tag for RTP and RTCP in bytes.
130*d9f75844SAndroid Build Coastguard Worker   // Depends on the cipher suite used and is usually the same with the exception
131*d9f75844SAndroid Build Coastguard Worker   // of the kCsAesCm128HmacSha1_32 cipher suite. The additional four bytes
132*d9f75844SAndroid Build Coastguard Worker   // required for RTCP protection are not included.
133*d9f75844SAndroid Build Coastguard Worker   int rtp_auth_tag_len_ = 0;
134*d9f75844SAndroid Build Coastguard Worker   int rtcp_auth_tag_len_ = 0;
135*d9f75844SAndroid Build Coastguard Worker 
136*d9f75844SAndroid Build Coastguard Worker   bool inited_ = false;
137*d9f75844SAndroid Build Coastguard Worker   int last_send_seq_num_ = -1;
138*d9f75844SAndroid Build Coastguard Worker   bool external_auth_active_ = false;
139*d9f75844SAndroid Build Coastguard Worker   bool external_auth_enabled_ = false;
140*d9f75844SAndroid Build Coastguard Worker   int decryption_failure_count_ = 0;
141*d9f75844SAndroid Build Coastguard Worker   bool dump_plain_rtp_ = false;
142*d9f75844SAndroid Build Coastguard Worker };
143*d9f75844SAndroid Build Coastguard Worker 
144*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
145*d9f75844SAndroid Build Coastguard Worker 
146*d9f75844SAndroid Build Coastguard Worker #endif  // PC_SRTP_SESSION_H_
147