blob: 048e6656444e283f77d19862ec2afc5fb88e45c1 [file] [log] [blame]
zstein4dde3df2017-07-07 21:26:251/*
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 17:11:0011#ifndef PC_SRTP_SESSION_H_
12#define PC_SRTP_SESSION_H_
zstein4dde3df2017-07-07 21:26:2513
Harald Alvestrandc24a2182022-02-23 13:44:5914#include <stddef.h>
15#include <stdint.h>
16
zstein4dde3df2017-07-07 21:26:2517#include <vector>
18
Jonas Orelande62c2f22022-03-29 09:04:4819#include "api/field_trials_view.h"
Mirko Bonadeid9708072019-01-25 19:26:4820#include "api/scoped_refptr.h"
Artem Titovd15a5752021-02-10 13:31:2421#include "api/sequence_checker.h"
Markus Handell4c7bb272020-07-15 11:23:3022#include "rtc_base/synchronization/mutex.h"
zstein4dde3df2017-07-07 21:26:2523
24// Forward declaration to avoid pulling in libsrtp headers here
25struct srtp_event_data_t;
26struct srtp_ctx_t_;
27
28namespace cricket {
29
Sebastian Jansson22619b32019-12-12 12:15:5430// Prohibits webrtc from initializing libsrtp. This can be used if libsrtp is
31// initialized by another library or explicitly. Note that this must be called
32// before creating an SRTP session with WebRTC.
33void ProhibitLibsrtpInitialization();
34
zstein4dde3df2017-07-07 21:26:2535// Class that wraps a libSRTP session.
36class SrtpSession {
37 public:
38 SrtpSession();
Jonas Orelande62c2f22022-03-29 09:04:4839 explicit SrtpSession(const webrtc::FieldTrialsView& field_trials);
zstein4dde3df2017-07-07 21:26:2540 ~SrtpSession();
41
Byoungchan Leec065e732022-01-18 00:35:4842 SrtpSession(const SrtpSession&) = delete;
43 SrtpSession& operator=(const SrtpSession&) = delete;
44
zstein4dde3df2017-07-07 21:26:2545 // Configures the session for sending data using the specified
46 // cipher-suite and key. Receiving must be done by a separate session.
Zhi Huangc99b6c72017-11-11 00:44:4647 bool SetSend(int cs,
48 const uint8_t* key,
49 size_t len,
50 const std::vector<int>& extension_ids);
51 bool UpdateSend(int cs,
52 const uint8_t* key,
53 size_t len,
54 const std::vector<int>& extension_ids);
zstein4dde3df2017-07-07 21:26:2555
56 // Configures the session for receiving data using the specified
57 // cipher-suite and key. Sending must be done by a separate session.
Zhi Huangc99b6c72017-11-11 00:44:4658 bool SetRecv(int cs,
59 const uint8_t* key,
60 size_t len,
61 const std::vector<int>& extension_ids);
62 bool UpdateRecv(int cs,
63 const uint8_t* key,
64 size_t len,
65 const std::vector<int>& extension_ids);
zstein4dde3df2017-07-07 21:26:2566
67 // Encrypts/signs an individual RTP/RTCP packet, in-place.
68 // If an HMAC is used, this will increase the packet size.
69 bool ProtectRtp(void* data, int in_len, int max_len, int* out_len);
70 // Overloaded version, outputs packet index.
71 bool ProtectRtp(void* data,
72 int in_len,
73 int max_len,
74 int* out_len,
75 int64_t* index);
76 bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len);
77 // Decrypts/verifies an invidiual RTP/RTCP packet.
78 // If an HMAC is used, this will decrease the packet size.
79 bool UnprotectRtp(void* data, int in_len, int* out_len);
80 bool UnprotectRtcp(void* data, int in_len, int* out_len);
81
82 // Helper method to get authentication params.
83 bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len);
84
85 int GetSrtpOverhead() const;
86
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 SRTP session 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
zstein4dde3df2017-07-07 21:26:25100 private:
Zhi Huangc99b6c72017-11-11 00:44:46101 bool DoSetKey(int type,
102 int cs,
103 const uint8_t* key,
104 size_t len,
105 const std::vector<int>& extension_ids);
106 bool SetKey(int type,
107 int cs,
108 const uint8_t* key,
109 size_t len,
110 const std::vector<int>& extension_ids);
111 bool UpdateKey(int type,
112 int cs,
113 const uint8_t* key,
114 size_t len,
115 const std::vector<int>& extension_ids);
zstein4dde3df2017-07-07 21:26:25116 // Returns send stream current packet index from srtp db.
117 bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index);
118
Philipp Hancke397c40e2020-12-04 11:11:27119 // Writes unencrypted packets in text2pcap format to the log file
120 // for debugging.
121 void DumpPacket(const void* buf, int len, bool outbound);
122
zstein4dde3df2017-07-07 21:26:25123 void HandleEvent(const srtp_event_data_t* ev);
124 static void HandleEventThunk(srtp_event_data_t* ev);
125
Artem Titovc8421c42021-02-02 09:57:19126 webrtc::SequenceChecker thread_checker_;
zstein4dde3df2017-07-07 21:26:25127 srtp_ctx_t_* session_ = nullptr;
Philipp Hanckebe66d952021-03-01 13:56:22128
129 // Overhead of the SRTP auth tag for RTP and RTCP in bytes.
130 // Depends on the cipher suite used and is usually the same with the exception
Mirko Bonadei7750d802021-07-26 15:27:42131 // of the kCsAesCm128HmacSha1_32 cipher suite. The additional four bytes
Philipp Hanckebe66d952021-03-01 13:56:22132 // required for RTCP protection are not included.
zstein4dde3df2017-07-07 21:26:25133 int rtp_auth_tag_len_ = 0;
134 int rtcp_auth_tag_len_ = 0;
Philipp Hanckebe66d952021-03-01 13:56:22135
Taylor Brandstetterb140b9f2017-10-13 00:24:16136 bool inited_ = false;
zstein4dde3df2017-07-07 21:26:25137 int last_send_seq_num_ = -1;
138 bool external_auth_active_ = false;
139 bool external_auth_enabled_ = false;
erikvarga@webrtc.orgd76a0fc2018-10-09 10:31:28140 int decryption_failure_count_ = 0;
Philipp Hancke397c40e2020-12-04 11:11:27141 bool dump_plain_rtp_ = false;
zstein4dde3df2017-07-07 21:26:25142};
143
144} // namespace cricket
145
Steve Anton10542f22019-01-11 17:11:00146#endif // PC_SRTP_SESSION_H_