| 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 |
| 11 | #ifndef PC_SRTP_SESSION_H_ |
| 12 | #define PC_SRTP_SESSION_H_ |
| 13 |
| 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include <vector> |
| 18 |
| 19 | #include "api/field_trials_view.h" |
| 20 | #include "api/scoped_refptr.h" |
| 21 | #include "api/sequence_checker.h" |
| 22 | #include "rtc_base/synchronization/mutex.h" |
| 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 |
| 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 |
| 35 | // Class that wraps a libSRTP session. |
| 36 | class SrtpSession { |
| 37 | public: |
| 38 | SrtpSession(); |
| 39 | explicit SrtpSession(const webrtc::FieldTrialsView& field_trials); |
| 40 | ~SrtpSession(); |
| 41 |
| 42 | SrtpSession(const SrtpSession&) = delete; |
| 43 | SrtpSession& operator=(const SrtpSession&) = delete; |
| 44 |
| 45 | // Configures the session for sending data using the specified |
| 46 | // crypto suite and key. Receiving must be done by a separate session. |
| 47 | bool SetSend(int crypto_suite, |
| 48 | const uint8_t* key, |
| 49 | size_t len, |
| 50 | const std::vector<int>& extension_ids); |
| 51 | bool UpdateSend(int crypto_suite, |
| 52 | const uint8_t* key, |
| 53 | size_t len, |
| 54 | const std::vector<int>& extension_ids); |
| 55 |
| 56 | // Configures the session for receiving data using the specified |
| 57 | // crypto suite and key. Sending must be done by a separate session. |
| 58 | bool SetRecv(int crypto_suite, |
| 59 | const uint8_t* key, |
| 60 | size_t len, |
| 61 | const std::vector<int>& extension_ids); |
| 62 | bool UpdateRecv(int crypto_suite, |
| 63 | const uint8_t* key, |
| 64 | size_t len, |
| 65 | const std::vector<int>& extension_ids); |
| 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 |
| 100 | // Removes a SSRC from the underlying libSRTP session. |
| 101 | // Note: this should only be done for SSRCs that are received. |
| 102 | // Removing SSRCs that were sent and then reusing them leads to |
| 103 | // cryptographic weaknesses described in |
| 104 | // https://www.rfc-editor.org/rfc/rfc3711#section-8 |
| 105 | // https://www.rfc-editor.org/rfc/rfc7714#section-8.4 |
| 106 | bool RemoveSsrcFromSession(uint32_t ssrc); |
| 107 |
| 108 | private: |
| 109 | bool DoSetKey(int type, |
| 110 | int crypto_suite, |
| 111 | const uint8_t* key, |
| 112 | size_t len, |
| 113 | const std::vector<int>& extension_ids); |
| 114 | bool SetKey(int type, |
| 115 | int crypto_suite, |
| 116 | const uint8_t* key, |
| 117 | size_t len, |
| 118 | const std::vector<int>& extension_ids); |
| 119 | bool UpdateKey(int type, |
| 120 | int crypto_suite, |
| 121 | const uint8_t* key, |
| 122 | size_t len, |
| 123 | const std::vector<int>& extension_ids); |
| 124 | // Returns send stream current packet index from srtp db. |
| 125 | bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index); |
| 126 |
| 127 | // Writes unencrypted packets in text2pcap format to the log file |
| 128 | // for debugging. |
| 129 | void DumpPacket(const void* buf, int len, bool outbound); |
| 130 |
| 131 | void HandleEvent(const srtp_event_data_t* ev); |
| 132 | static void HandleEventThunk(srtp_event_data_t* ev); |
| 133 |
| 134 | webrtc::SequenceChecker thread_checker_; |
| 135 | srtp_ctx_t_* session_ = nullptr; |
| 136 |
| 137 | // Overhead of the SRTP auth tag for RTP and RTCP in bytes. |
| 138 | // Depends on the cipher suite used and is usually the same with the exception |
| 139 | // of the kCsAesCm128HmacSha1_32 cipher suite. The additional four bytes |
| 140 | // required for RTCP protection are not included. |
| 141 | int rtp_auth_tag_len_ = 0; |
| 142 | int rtcp_auth_tag_len_ = 0; |
| 143 |
| 144 | bool inited_ = false; |
| 145 | int last_send_seq_num_ = -1; |
| 146 | bool external_auth_active_ = false; |
| 147 | bool external_auth_enabled_ = false; |
| 148 | int decryption_failure_count_ = 0; |
| 149 | bool dump_plain_rtp_ = false; |
| 150 | }; |
| 151 |
| 152 | } // namespace cricket |
| 153 |
| 154 | #endif // PC_SRTP_SESSION_H_ |
| 155 |