OPAL  Version 3.18.8
dtls_srtp_session.h
Go to the documentation of this file.
1 /*
2  * dtls_srtp_session.h
3  *
4  * SRTP protocol session handler with DTLS key exchange
5  *
6  * OPAL Library
7  *
8  * Copyright (C) 2014 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 Sysolyatin Pavel
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef OPAL_RTP_DTLS_SRTP_SESSION_H
28 #define OPAL_RTP_DTLS_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 #include <sdp/ice.h>
40 #include <rtp/srtp_session.h>
41 
42 #if OPAL_SRTP
43 #include <ptclib/pssl.h>
44 #include <ptclib/pstun.h>
45 #include <ptclib/qchannel.h>
46 
47 
51 #define OPAL_OPT_DTLS_TIMEOUT "DTLS-Timeout"
52 
53 
54 #if OPAL_ICE
55 typedef OpalICEMediaTransport OpalDTLSMediaTransportParent;
56 #else
57 typedef OpalUDPMediaTransport OpalDTLSMediaTransportParent;
58 #endif
59 
60 class OpalDTLSMediaTransport : public OpalDTLSMediaTransportParent
61 {
62  PCLASSINFO(OpalDTLSMediaTransport, OpalDTLSMediaTransportParent);
63  public:
64  OpalDTLSMediaTransport(const PString & name, bool passiveMode, const PSSLCertificateFingerprint& fp);
65 
66  virtual bool Open(OpalMediaSession & session, PINDEX count, const PString & localInterface, const OpalTransportAddress & remoteAddress);
67  virtual bool IsEstablished() const;
68  virtual bool GetKeyInfo(OpalMediaCryptoKeyInfo * keyInfo[2]);
69 
70  void SetPassiveMode(bool passive);
71  PSSLCertificateFingerprint GetLocalFingerprint(PSSLCertificateFingerprint::HashType hashType) const;
72  bool SetRemoteFingerprint(const PSSLCertificateFingerprint& fp);
73  PSSLCertificateFingerprint GetRemoteFingerprint() const;
74 
75  protected:
76  virtual PChannel * AddWrapperChannels(SubChannels subchannel, PChannel * channel);
77 
78  class DTLSChannel : public PSSLChannelDTLS
79  {
80  PCLASSINFO(DTLSChannel, PSSLChannelDTLS);
81  public:
82  DTLSChannel(OpalDTLSMediaTransport & transport, PChannel * channel);
83  ~DTLSChannel() { Close(); }
84  virtual bool Read(void * buf, PINDEX len);
85  virtual int BioRead(char * buf, int len);
86  virtual int BioWrite(const char * buf, int len);
87  protected:
88  OpalDTLSMediaTransport & m_transport;
89  // Used to cache a ClientHello received before we're ready to handshake.
90  // Unfortunately Firefox likes to fragment this over two packets after the
91  // first 3 ignored attempts (~350ms).
92  std::list<PBYTEArray> m_lastReceivedPackets;
93  // The final handshake response packet (for retransmission in case it is lost)
94  PBYTEArray m_lastResponseData;
95  PINDEX m_lastResponseLength;
96  };
97  friend class DTLSChannel;
98 
99  bool InternalPerformHandshake(DTLSChannel * channel);
100  virtual bool PerformHandshake(DTLSChannel & channel);
101  PDECLARE_SSLVerifyNotifier(OpalDTLSMediaTransport, OnVerify);
102 
103  atomic<bool> m_passiveMode;
104  PTimeInterval m_handshakeTimeout;
105  unsigned m_MTU;
106  PSSLCertificate m_certificate;
107  PSSLPrivateKey m_privateKey;
108  PSSLCertificateFingerprint m_remoteFingerprint;
109  PAutoPtr<OpalMediaCryptoKeyInfo> m_keyInfo[2];
110 
111  friend class OpalDTLSContext;
112 
113  P_REMOVE_VIRTUAL(DTLSChannel*,CreateDTLSChannel(),NULL);
114  P_REMOVE_VIRTUAL_VOID(PerformHandshake(PChannel*));
115 };
116 
117 
118 class OpalDTLSSRTPSession : public OpalSRTPSession
119 {
120  PCLASSINFO(OpalDTLSSRTPSession, OpalSRTPSession);
121  public:
122  static const PCaselessString & RTP_DTLS_SAVP();
123  static const PCaselessString & RTP_DTLS_SAVPF();
124 
125  OpalDTLSSRTPSession(const Init & init);
126  ~OpalDTLSSRTPSession();
127 
128  virtual const PCaselessString & GetSessionType() const { return RTP_DTLS_SAVP(); }
129 
130  // New members
131  void SetPassiveMode(bool passive);
132  bool IsPassiveMode() const;
133 
134  PSSLCertificateFingerprint GetLocalFingerprint(PSSLCertificateFingerprint::HashType hashType) const;
135  void SetRemoteFingerprint(const PSSLCertificateFingerprint& fp);
136 
137  protected:
138  virtual OpalMediaTransport * CreateMediaTransport(const PString & name);
139 
140  bool m_passiveMode;
141  PSSLCertificateFingerprint m_earlyRemoteFingerprint;
142 };
143 
144 
145 #endif // OPAL_SRTP
146 
147 #endif // OPAL_RTP_DTLS_SRTP_SESSION_H
Definition: mediasession.h:647
Definition: mediasession.h:282
Definition: mediasession.h:621
Definition: transports.h:151
Definition: mediasession.h:392