zstein | 4dde3df | 2017-07-07 21:26:25 | [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 17:11:00 | [diff] [blame] | 11 | #ifndef PC_SRTP_SESSION_H_ |
| 12 | #define PC_SRTP_SESSION_H_ |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 13 | |
Harald Alvestrand | c24a218 | 2022-02-23 13:44:59 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 | |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 19 | #include "api/field_trials_view.h" |
Mirko Bonadei | d970807 | 2019-01-25 19:26:48 | [diff] [blame] | 20 | #include "api/scoped_refptr.h" |
Artem Titov | d15a575 | 2021-02-10 13:31:24 | [diff] [blame] | 21 | #include "api/sequence_checker.h" |
Markus Handell | 4c7bb27 | 2020-07-15 11:23:30 | [diff] [blame] | 22 | #include "rtc_base/synchronization/mutex.h" |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 23 | |
| 24 | // Forward declaration to avoid pulling in libsrtp headers here |
| 25 | struct srtp_event_data_t; |
| 26 | struct srtp_ctx_t_; |
| 27 | |
| 28 | namespace cricket { |
| 29 | |
Sebastian Jansson | 22619b3 | 2019-12-12 12:15:54 | [diff] [blame] | 30 | // 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. |
| 33 | void ProhibitLibsrtpInitialization(); |
| 34 | |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 35 | // Class that wraps a libSRTP session. |
| 36 | class SrtpSession { |
| 37 | public: |
| 38 | SrtpSession(); |
Jonas Oreland | e62c2f2 | 2022-03-29 09:04:48 | [diff] [blame] | 39 | explicit SrtpSession(const webrtc::FieldTrialsView& field_trials); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 40 | ~SrtpSession(); |
| 41 | |
Byoungchan Lee | c065e73 | 2022-01-18 00:35:48 | [diff] [blame] | 42 | SrtpSession(const SrtpSession&) = delete; |
| 43 | SrtpSession& operator=(const SrtpSession&) = delete; |
| 44 | |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 45 | // Configures the session for sending data using the specified |
| 46 | // cipher-suite and key. Receiving must be done by a separate session. |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 47 | 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); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 55 | |
| 56 | // Configures the session for receiving data using the specified |
| 57 | // cipher-suite and key. Sending must be done by a separate session. |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 58 | 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); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 66 | |
| 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 | |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 100 | private: |
Zhi Huang | c99b6c7 | 2017-11-11 00:44:46 | [diff] [blame] | 101 | 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); |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 116 | // Returns send stream current packet index from srtp db. |
| 117 | bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index); |
| 118 | |
Philipp Hancke | 397c40e | 2020-12-04 11:11:27 | [diff] [blame] | 119 | // Writes unencrypted packets in text2pcap format to the log file |
| 120 | // for debugging. |
| 121 | void DumpPacket(const void* buf, int len, bool outbound); |
| 122 | |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 123 | void HandleEvent(const srtp_event_data_t* ev); |
| 124 | static void HandleEventThunk(srtp_event_data_t* ev); |
| 125 | |
Artem Titov | c8421c4 | 2021-02-02 09:57:19 | [diff] [blame] | 126 | webrtc::SequenceChecker thread_checker_; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 127 | srtp_ctx_t_* session_ = nullptr; |
Philipp Hancke | be66d95 | 2021-03-01 13:56:22 | [diff] [blame] | 128 | |
| 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 Bonadei | 7750d80 | 2021-07-26 15:27:42 | [diff] [blame] | 131 | // of the kCsAesCm128HmacSha1_32 cipher suite. The additional four bytes |
Philipp Hancke | be66d95 | 2021-03-01 13:56:22 | [diff] [blame] | 132 | // required for RTCP protection are not included. |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 133 | int rtp_auth_tag_len_ = 0; |
| 134 | int rtcp_auth_tag_len_ = 0; |
Philipp Hancke | be66d95 | 2021-03-01 13:56:22 | [diff] [blame] | 135 | |
Taylor Brandstetter | b140b9f | 2017-10-13 00:24:16 | [diff] [blame] | 136 | bool inited_ = false; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 137 | int last_send_seq_num_ = -1; |
| 138 | bool external_auth_active_ = false; |
| 139 | bool external_auth_enabled_ = false; |
erikvarga@webrtc.org | d76a0fc | 2018-10-09 10:31:28 | [diff] [blame] | 140 | int decryption_failure_count_ = 0; |
Philipp Hancke | 397c40e | 2020-12-04 11:11:27 | [diff] [blame] | 141 | bool dump_plain_rtp_ = false; |
zstein | 4dde3df | 2017-07-07 21:26:25 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | } // namespace cricket |
| 145 | |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 146 | #endif // PC_SRTP_SESSION_H_ |