zstein | 4dde3df | 2017-07-07 14:26:25 -0700 | [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 | |
| 11 | #ifndef WEBRTC_PC_SRTPSESSION_H_ |
| 12 | #define WEBRTC_PC_SRTPSESSION_H_ |
| 13 | |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "webrtc/rtc_base/basictypes.h" |
| 17 | #include "webrtc/rtc_base/thread_checker.h" |
| 18 | |
| 19 | // Forward declaration to avoid pulling in libsrtp headers here |
| 20 | struct srtp_event_data_t; |
| 21 | struct srtp_ctx_t_; |
| 22 | |
| 23 | namespace cricket { |
| 24 | |
| 25 | // Class that wraps a libSRTP session. |
| 26 | class SrtpSession { |
| 27 | public: |
| 28 | SrtpSession(); |
| 29 | ~SrtpSession(); |
| 30 | |
| 31 | // Configures the session for sending data using the specified |
| 32 | // cipher-suite and key. Receiving must be done by a separate session. |
| 33 | bool SetSend(int cs, const uint8_t* key, size_t len); |
| 34 | bool UpdateSend(int cs, const uint8_t* key, size_t len); |
| 35 | |
| 36 | // Configures the session for receiving data using the specified |
| 37 | // cipher-suite and key. Sending must be done by a separate session. |
| 38 | bool SetRecv(int cs, const uint8_t* key, size_t len); |
| 39 | bool UpdateRecv(int cs, const uint8_t* key, size_t len); |
| 40 | |
| 41 | void SetEncryptedHeaderExtensionIds( |
| 42 | const std::vector<int>& encrypted_header_extension_ids); |
| 43 | |
| 44 | // Encrypts/signs an individual RTP/RTCP packet, in-place. |
| 45 | // If an HMAC is used, this will increase the packet size. |
| 46 | bool ProtectRtp(void* data, int in_len, int max_len, int* out_len); |
| 47 | // Overloaded version, outputs packet index. |
| 48 | bool ProtectRtp(void* data, |
| 49 | int in_len, |
| 50 | int max_len, |
| 51 | int* out_len, |
| 52 | int64_t* index); |
| 53 | bool ProtectRtcp(void* data, int in_len, int max_len, int* out_len); |
| 54 | // Decrypts/verifies an invidiual RTP/RTCP packet. |
| 55 | // If an HMAC is used, this will decrease the packet size. |
| 56 | bool UnprotectRtp(void* data, int in_len, int* out_len); |
| 57 | bool UnprotectRtcp(void* data, int in_len, int* out_len); |
| 58 | |
| 59 | // Helper method to get authentication params. |
| 60 | bool GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len); |
| 61 | |
| 62 | int GetSrtpOverhead() const; |
| 63 | |
| 64 | // If external auth is enabled, SRTP will write a dummy auth tag that then |
| 65 | // later must get replaced before the packet is sent out. Only supported for |
| 66 | // non-GCM cipher suites and can be checked through "IsExternalAuthActive" |
| 67 | // if it is actually used. This method is only valid before the RTP params |
| 68 | // have been set. |
| 69 | void EnableExternalAuth(); |
| 70 | bool IsExternalAuthEnabled() const; |
| 71 | |
| 72 | // A SRTP session supports external creation of the auth tag if a non-GCM |
| 73 | // cipher is used. This method is only valid after the RTP params have |
| 74 | // been set. |
| 75 | bool IsExternalAuthActive() const; |
| 76 | |
| 77 | // Calls srtp_shutdown if it's initialized. |
| 78 | static void Terminate(); |
| 79 | |
| 80 | private: |
| 81 | bool DoSetKey(int type, int cs, const uint8_t* key, size_t len); |
| 82 | bool SetKey(int type, int cs, const uint8_t* key, size_t len); |
| 83 | bool UpdateKey(int type, int cs, const uint8_t* key, size_t len); |
| 84 | bool SetEncryptedHeaderExtensionIds( |
| 85 | int type, |
| 86 | const std::vector<int>& encrypted_header_extension_ids); |
| 87 | // Returns send stream current packet index from srtp db. |
| 88 | bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index); |
| 89 | |
| 90 | static bool Init(); |
| 91 | void HandleEvent(const srtp_event_data_t* ev); |
| 92 | static void HandleEventThunk(srtp_event_data_t* ev); |
| 93 | |
| 94 | rtc::ThreadChecker thread_checker_; |
| 95 | srtp_ctx_t_* session_ = nullptr; |
| 96 | int rtp_auth_tag_len_ = 0; |
| 97 | int rtcp_auth_tag_len_ = 0; |
| 98 | static bool inited_; |
| 99 | static rtc::GlobalLockPod lock_; |
| 100 | int last_send_seq_num_ = -1; |
| 101 | bool external_auth_active_ = false; |
| 102 | bool external_auth_enabled_ = false; |
| 103 | std::vector<int> encrypted_header_extension_ids_; |
| 104 | RTC_DISALLOW_COPY_AND_ASSIGN(SrtpSession); |
| 105 | }; |
| 106 | |
| 107 | } // namespace cricket |
| 108 | |
| 109 | #endif // WEBRTC_PC_SRTPSESSION_H_ |