blob: 94702da1309232794127717c2a70fe2c9ad2a6af [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef PC_SRTPSESSION_H_
12#define PC_SRTPSESSION_H_
zstein4dde3df2017-07-07 14:26:25 -070013
14#include <vector>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/basictypes.h"
17#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
25// Class that wraps a libSRTP session.
26class 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
zstein4dde3df2017-07-07 14:26:25 -070077 private:
78 bool DoSetKey(int type, int cs, const uint8_t* key, size_t len);
79 bool SetKey(int type, int cs, const uint8_t* key, size_t len);
80 bool UpdateKey(int type, int cs, const uint8_t* key, size_t len);
81 bool SetEncryptedHeaderExtensionIds(
82 int type,
83 const std::vector<int>& encrypted_header_extension_ids);
84 // Returns send stream current packet index from srtp db.
85 bool GetSendStreamPacketIndex(void* data, int in_len, int64_t* index);
86
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -070087 // These methods are responsible for initializing libsrtp (if the usage count
88 // is incremented from 0 to 1) or deinitializing it (when decremented from 1
89 // to 0).
90 //
91 // Returns true if successful (will always be successful if already inited).
92 static bool IncrementLibsrtpUsageCountAndMaybeInit();
93 static void DecrementLibsrtpUsageCountAndMaybeDeinit();
94
zstein4dde3df2017-07-07 14:26:25 -070095 void HandleEvent(const srtp_event_data_t* ev);
96 static void HandleEventThunk(srtp_event_data_t* ev);
97
98 rtc::ThreadChecker thread_checker_;
99 srtp_ctx_t_* session_ = nullptr;
100 int rtp_auth_tag_len_ = 0;
101 int rtcp_auth_tag_len_ = 0;
Taylor Brandstetterb140b9f2017-10-12 17:24:16 -0700102 bool inited_ = false;
zstein4dde3df2017-07-07 14:26:25 -0700103 static rtc::GlobalLockPod lock_;
104 int last_send_seq_num_ = -1;
105 bool external_auth_active_ = false;
106 bool external_auth_enabled_ = false;
107 std::vector<int> encrypted_header_extension_ids_;
108 RTC_DISALLOW_COPY_AND_ASSIGN(SrtpSession);
109};
110
111} // namespace cricket
112
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200113#endif // PC_SRTPSESSION_H_