blob: 0a26c02c9f6b1cf0f7cbc7ee3236a12dac842539 [file] [log] [blame]
zstein4dde3df2017-07-07 14:26:25 -07001/*
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 Anton10542f22019-01-11 09:11:00 -080011#ifndef PC_SRTP_SESSION_H_
12#define PC_SRTP_SESSION_H_
zstein4dde3df2017-07-07 14:26:25 -070013
14#include <vector>
15
Mirko Bonadeid9708072019-01-25 20:26:48 +010016#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/thread_checker.h"
zstein4dde3df2017-07-07 14:26:25 -070018
19// Forward declaration to avoid pulling in libsrtp headers here
20struct srtp_event_data_t;
21struct srtp_ctx_t_;
22
23namespace cricket {
24
Sebastian Jansson22619b32019-12-12 13:15:54 +010025// Prohibits webrtc from initializing libsrtp. This can be used if libsrtp is
26// initialized by another library or explicitly. Note that this must be called
27// before creating an SRTP session with WebRTC.
28void ProhibitLibsrtpInitialization();
29
zstein4dde3df2017-07-07 14:26:25 -070030// Class that wraps a libSRTP session.
31class SrtpSession {
32 public:
33 SrtpSession();
34 ~SrtpSession();
35
36 // Configures the session for sending data using the specified
37 // cipher-suite and key. Receiving must be done by a separate session.
Zhi Huangc99b6c72017-11-10 16:44:46 -080038 bool SetSend(int cs,
39 const uint8_t* key,
40 size_t len,
41 const std::vector<int>& extension_ids);
42 bool UpdateSend(int cs,
43 const uint8_t* key,
44 size_t len,
45 const std::vector<int>& extension_ids);
zstein4dde3df2017-07-07 14:26:25 -070046
47 // Configures the session for receiving data using the specified
48 // cipher-suite and key. Sending must be done by a separate session.
Zhi Huangc99b6c72017-11-10 16:44:46 -080049 bool SetRecv(int cs,
50 const uint8_t* key,
51 size_t len,
52 const std::vector<int>& extension_ids);
53 bool UpdateRecv(int cs,
54 const uint8_t* key,
55 size_t len,
56 const std::vector<int>& extension_ids);
zstein4dde3df2017-07-07 14:26:25 -070057
58 // Encrypts/signs an individual RTP/RTCP packet, in-place.
59 // If an HMAC is used, this will increase the packet size.
60 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
61 // Overloaded version, outputs packet index.
62 bool ProtectRtp(void* data,
63 int in_len,
64 int max_len,
65 int* out_len,
66 int64_t* index);
67 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
68 // Decrypts/verifies an invidiual RTP/RTCP packet.
69 // If an HMAC is used, this will decrease the packet size.
70 bool UnprotectRtp(void* data, int in_len, int* out_len);
71 bool UnprotectRtcp(void* data, int in_len, int* out_len);
72
73 // Helper method to get authentication params.
74 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
75
76 int GetSrtpOverhead() const;
77
78 // If external auth is enabled, SRTP will write a dummy auth tag that then
79 // later must get replaced before the packet is sent out. Only supported for
80 // non-GCM cipher suites and can be checked through "IsExternalAuthActive"
81 // if it is actually used. This method is only valid before the RTP params
82 // have been set.
83 void EnableExternalAuth();
84 bool IsExternalAuthEnabled() const;
85
86 // A SRTP session supports external creation of the auth tag if a non-GCM
87 // cipher is used. This method is only valid after the RTP params have
88 // been set.
89 bool IsExternalAuthActive() const;
90
zstein4dde3df2017-07-07 14:26:25 -070091 private:
Zhi Huangc99b6c72017-11-10 16:44:46 -080092 bool DoSetKey(int type,
93 int cs,
94 const uint8_t* key,
95 size_t len,
96 const std::vector<int>& extension_ids);
97 bool SetKey(int type,
98 int cs,
99 const uint8_t* key,
100 size_t len,
101 const std::vector<int>& extension_ids);
102 bool UpdateKey(int type,
103 int cs,
104 const uint8_t* key,
105 size_t len,
106 const std::vector<int>& extension_ids);
zstein4dde3df2017-07-07 14:26:25 -0700107 // Returns send stream current packet index from srtp db.
108 bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index);
109
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700110 // These methods are responsible for initializing libsrtp (if the usage count
111 // is incremented from 0 to 1) or deinitializing it (when decremented from 1
112 // to 0).
113 //
114 // Returns true if successful (will always be successful if already inited).
115 static bool IncrementLibsrtpUsageCountAndMaybeInit();
116 static void DecrementLibsrtpUsageCountAndMaybeDeinit();
117
zstein4dde3df2017-07-07 14:26:25 -0700118 void HandleEvent(const srtp_event_data_t* ev);
119 static void HandleEventThunk(srtp_event_data_t* ev);
120
121 rtc::ThreadChecker thread_checker_;
122 srtp_ctx_t_* session_ = nullptr;
123 int rtp_auth_tag_len_ = 0;
124 int rtcp_auth_tag_len_ = 0;
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700125 bool inited_ = false;
Danil Chapovalov5740f3e2019-10-10 11:12:15 +0200126 static rtc::GlobalLock lock_;
zstein4dde3df2017-07-07 14:26:25 -0700127 int last_send_seq_num_ = -1;
128 bool external_auth_active_ = false;
129 bool external_auth_enabled_ = false;
erikvarga@webrtc.orgd76a0fc2018-10-09 12:31:28 +0200130 int decryption_failure_count_ = 0;
zstein4dde3df2017-07-07 14:26:25 -0700131 RTC_DISALLOW_COPY_AND_ASSIGN(SrtpSession);
132};
133
134} // namespace cricket
135
Steve Anton10542f22019-01-11 09:11:00 -0800136#endif // PC_SRTP_SESSION_H_