OPAL  Version 3.18.8
srtp_session.h
Go to the documentation of this file.
1 /*
2  * srtp_session.h
3  *
4  * SRTP protocol session handler
5  *
6  * OPAL Library
7  *
8  * Copyright (C) 2012 Vox Lucida Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is OPAL Library.
21  *
22  * The Initial Developer of the Original Code is Vox Lucida
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef OPAL_RTP_SRTP_SESSION_H
28 #define OPAL_RTP_SRTP_SESSION_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #ifndef _PTLIB_H
35 #include <ptlib.h>
36 #endif
37 
38 #include <opal_config.h>
39 
40 #include <rtp/rtp.h>
41 #include <rtp/rtpconn.h>
42 
43 #if OPAL_SRTP
44 
45 class OpalSRTPCryptoSuite;
46 typedef struct srtp_ctx_t_ srtp_ctx_t;
47 
48 
55 #define OPAL_OPT_SRTP_RTCP_ANY_SSRC "SRTP-RTCP-Any-SSRC"
56 
57 
59 //
60 // this class holds the parameters required for an SRTP session
61 //
62 // Crypto modes are identified by key strings that are contained in PFactory<OpalSRTPParms>
63 // The following strings should be implemented:
64 //
65 // AES_CM_128_HMAC_SHA1_80,
66 // AES_CM_128_HMAC_SHA1_32,
67 // AES_CM_128_NULL_AUTH,
68 // NULL_CIPHER_HMAC_SHA1_80
69 // STRONGHOLD
70 //
71 
72 class OpalSRTPKeyInfo : public OpalMediaCryptoKeyInfo
73 {
74  PCLASSINFO(OpalSRTPKeyInfo, OpalMediaCryptoKeyInfo);
75  public:
76  OpalSRTPKeyInfo(const OpalSRTPCryptoSuite & cryptoSuite);
77 
78  PObject * Clone() const;
79  virtual Comparison Compare(const PObject & other) const;
80 
81  virtual bool IsValid() const;
82  virtual void Randomise();
83  virtual bool FromString(const PString & str);
84  virtual PString ToString() const;
85  virtual bool SetCipherKey(const PBYTEArray & key);
86  virtual bool SetAuthSalt(const PBYTEArray & key);
87  virtual PBYTEArray GetCipherKey() const;
88  virtual PBYTEArray GetAuthSalt() const;
89 
90  const OpalSRTPCryptoSuite & GetCryptoSuite() const { return m_cryptoSuite; }
91 
92  protected:
93  const OpalSRTPCryptoSuite & m_cryptoSuite;
94  PBYTEArray m_key;
95  PBYTEArray m_salt;
96  PBYTEArray m_key_salt;
97 
98  friend class OpalSRTPSession;
99 };
100 
101 
102 class OpalSRTPCryptoSuite : public OpalMediaCryptoSuite
103 {
104  PCLASSINFO(OpalSRTPCryptoSuite, OpalMediaCryptoSuite);
105  protected:
106  OpalSRTPCryptoSuite() { }
107 
108  public:
109 #if OPAL_H235_8
110  virtual H235SecurityCapability * CreateCapability(const H323Capability & mediaCapability) const;
111 #endif
112  virtual bool Supports(const PCaselessString & proto) const;
113  virtual bool ChangeSessionType(PCaselessString & mediaSession, KeyExchangeModes modes) const;
114 
115  virtual PINDEX GetAuthSaltBits() const;
116  virtual OpalMediaCryptoKeyInfo * CreateKeyInfo() const;
117 
118  virtual void SetCryptoPolicy(struct srtp_crypto_policy_t & policy) const = 0;
119 };
120 
121 
124 class OpalSRTPSession : public OpalRTPSession
125 {
126  PCLASSINFO(OpalSRTPSession, OpalRTPSession);
127  public:
128  static const PCaselessString & RTP_SAVP();
129  static const PCaselessString & RTP_SAVPF();
130 
131  OpalSRTPSession(const Init & init);
132  ~OpalSRTPSession();
133 
134  virtual const PCaselessString & GetSessionType() const { return RTP_SAVP(); }
136  virtual bool ApplyCryptoKey(OpalMediaCryptoKeyList & keys, bool rx);
137  virtual OpalMediaCryptoKeyInfo * IsCryptoSecured(bool rx) const;
138 
139  virtual bool Open(const PString & localInterface, const OpalTransportAddress & remoteAddress);
140  virtual RTP_SyncSourceId AddSyncSource(RTP_SyncSourceId id, Direction dir, const char * cname = NULL);
141 
142  virtual SendReceiveStatus OnSendData(RewriteMode & rewrite, RTP_DataFrame & frame, const PTime & now);
143  virtual SendReceiveStatus OnSendControl(RTP_ControlFrame & frame, const PTime & now);
144  virtual SendReceiveStatus OnReceiveData(RTP_DataFrame & frame, ReceiveType rxType, const PTime & now);
145  virtual SendReceiveStatus OnReceiveControl(RTP_ControlFrame & frame, const PTime & now);
146  virtual bool IsEncrypted() const { return true; }
147 
148  virtual SendReceiveStatus OnReceiveDecodedControl(RTP_ControlFrame & frame, const PTime & now);
149 
150  protected:
151  virtual bool ResequenceOutOfOrderPackets(SyncSource & ssrc) const;
152  virtual bool ApplyKeysToSRTP(OpalMediaTransport & transport);
153  virtual bool ApplyKeyToSRTP(const OpalMediaCryptoKeyInfo & keyInfo, Direction dir);
154  virtual bool AddStreamToSRTP(RTP_SyncSourceId ssrc, Direction dir);
155  virtual void OnRxDataPacket(OpalMediaTransport & transport, PBYTEArray data);
156  virtual void OnRxControlPacket(OpalMediaTransport & transport, PBYTEArray data);
157 
158  bool m_anyRTCP_SSRC;
159  srtp_ctx_t * m_context;
160  std::set<RTP_SyncSourceId> m_addedStream;
161  OpalSRTPKeyInfo * m_keyInfo[2]; // rx & tx
162  unsigned m_consecutiveErrors[2][2];
163  SendReceiveStatus CheckConsecutiveErrors(bool ok, Direction dir, SubChannels subchannel);
164 
165 #if PTRACING
166  map<uint64_t, PTrace::ThrottleBase> m_throttle;
167  PTrace::ThrottleBase & GetThrottle(unsigned level, Direction dir, SubChannels subchannel, RTP_SyncSourceId ssrc, int item);
168 #endif
169 };
170 
171 
172 #endif // OPAL_SRTP
173 
174 #endif // OPAL_RTP_SRTP_SESSION_H
virtual bool Supports(const PCaselessString &proto) const =0
virtual bool ChangeSessionType(PCaselessString &mediaSession, KeyExchangeModes modes) const =0
virtual SendReceiveStatus OnSendControl(RTP_ControlFrame &frame, const PTime &now)
const OpalMediaCryptoSuite & m_cryptoSuite
Definition: mediasession.h:308
virtual SendReceiveStatus OnReceiveControl(RTP_ControlFrame &frame, const PTime &now)
Definition: h323caps.h:95
virtual bool SetAuthSalt(const PBYTEArray &key)=0
Definition: mediasession.h:321
virtual bool ResequenceOutOfOrderPackets(SyncSource &ssrc) const
Definition: mediasession.h:282
virtual PBYTEArray GetCipherKey() const =0
Definition: rtp.h:540
Definition: mediasession.h:312
virtual OpalMediaCryptoKeyInfo * IsCryptoSecured(bool rx) const
virtual OpalMediaCryptoKeyInfo * CreateKeyInfo() const =0
virtual const PCaselessString & GetSessionType() const
Definition: rtp_session.h:111
Definition: rtp_session.h:90
virtual OpalMediaCryptoKeyList & GetOfferedCryptoKeys()
virtual bool FromString(const PString &str)=0
virtual SendReceiveStatus OnSendData(RewriteMode &rewrite, RTP_DataFrame &frame, const PTime &now)
virtual PString ToString() const =0
virtual SendReceiveStatus OnReceiveData(RTP_DataFrame &frame, ReceiveType rxType, const PTime &now)
virtual void Randomise()=0
virtual bool ApplyCryptoKey(OpalMediaCryptoKeyList &keys, bool rx)
virtual bool IsEncrypted() const
Definition: rtp_session.h:273
virtual bool IsValid() const =0
virtual RTP_SyncSourceId AddSyncSource(RTP_SyncSourceId id, Direction dir, const char *cname=NULL)
Definition: rtp.h:101
Definition: transports.h:151
uint32_t RTP_SyncSourceId
Definition: rtp.h:46
Definition: mediasession.h:392
virtual bool Open(const PString &localInterface, const OpalTransportAddress &remoteAddress)
const OpalMediaCryptoSuite & GetCryptoSuite() const
Definition: mediasession.h:302
virtual PBYTEArray GetAuthSalt() const =0
virtual PINDEX GetAuthSaltBits() const =0
virtual bool SetCipherKey(const PBYTEArray &key)=0