1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 // Original author: ekr@rtfm.com
6 
7 #ifndef srtpflow_h__
8 #define srtpflow_h__
9 
10 #include "mozilla/RefPtr.h"
11 #include "nsISupportsImpl.h"
12 #include "srtp.h"
13 
14 namespace mozilla {
15 
16 #define SRTP_ICM_MASTER_KEY_LENGTH 16
17 #define SRTP_ICM_MASTER_SALT_LENGTH 14
18 #define SRTP_ICM_MAX_MASTER_LENGTH \
19   (SRTP_ICM_MASTER_KEY_LENGTH + SRTP_ICM_MASTER_SALT_LENGTH)
20 
21 #define SRTP_GCM_MASTER_KEY_MIN_LENGTH 16
22 #define SRTP_GCM_MASTER_KEY_MAX_LENGTH 32
23 #define SRTP_GCM_MASTER_SALT_LENGTH 12
24 
25 #define SRTP_GCM_MIN_MASTER_LENGTH \
26   (SRTP_GCM_MASTER_KEY_MIN_LENGTH + SRTP_GCM_MASTER_SALT_LENGTH)
27 #define SRTP_GCM_MAX_MASTER_LENGTH \
28   (SRTP_GCM_MASTER_KEY_MAX_LENGTH + SRTP_GCM_MASTER_SALT_LENGTH)
29 
30 #define SRTP_MIN_KEY_LENGTH SRTP_GCM_MIN_MASTER_LENGTH
31 #define SRTP_MAX_KEY_LENGTH SRTP_GCM_MAX_MASTER_LENGTH
32 
33 // SRTCP requires an auth tag *plus* a 4-byte index-plus-'E'-bit value (see
34 // RFC 3711)
35 #define SRTP_MAX_EXPANSION (SRTP_MAX_TRAILER_LEN + 4)
36 
37 class SrtpFlow {
38   ~SrtpFlow();
39 
40  public:
41   static unsigned int KeySize(int cipher_suite);
42   static unsigned int SaltSize(int cipher_suite);
43 
44   static RefPtr<SrtpFlow> Create(int cipher_suite, bool inbound,
45                                  const void* key, size_t key_len);
46 
47   nsresult ProtectRtp(void* in, int in_len, int max_len, int* out_len);
48   nsresult UnprotectRtp(void* in, int in_len, int max_len, int* out_len);
49   nsresult ProtectRtcp(void* in, int in_len, int max_len, int* out_len);
50   nsresult UnprotectRtcp(void* in, int in_len, int max_len, int* out_len);
51 
52   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SrtpFlow)
53 
54   static void srtp_event_handler(srtp_event_data_t* data);
55 
56  private:
SrtpFlow()57   SrtpFlow() : session_(nullptr) {}
58 
59   nsresult CheckInputs(bool protect, void* in, int in_len, int max_len,
60                        int* out_len);
61 
62   static nsresult Init();
63   static bool initialized;  // Was libsrtp initialized? Only happens once.
64 
65   srtp_t session_;
66 };
67 
68 }  // namespace mozilla
69 #endif
70