reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
DtlsFactory.hxx
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#ifdef USE_SSL
6
7#ifndef DtlsFactory_hxx
8#define DtlsFactory_hxx
9
10#include <memory>
11#include "DtlsTimer.hxx"
12
13typedef struct x509_st X509;
14typedef struct ssl_ctx_st SSL_CTX;
15typedef struct ssl_st SSL;
16typedef struct evp_pkey_st EVP_PKEY;
17
18namespace dtls
19{
20class DtlsListener;
21class DtlsSocket;
24
25//Not threadsafe. Timers must fire in the same thread as dtls processing.
27{
28 public:
30
31 // Creates a DTLS SSL Context and enables srtp extension, also sets the private and public key cert
32 DtlsFactory(std::unique_ptr<DtlsTimerContext> tc, X509 *cert, EVP_PKEY *privkey);
33
34 // Note: this orphans any DtlsSockets you were stupid enough
35 // not to free
37
38 // Creates a new DtlsSocket to be used as a client
39 DtlsSocket* createClient(std::unique_ptr<DtlsSocketContext> context);
40
41 // Creates a new DtlsSocket to be used as a server
42 DtlsSocket* createServer(std::unique_ptr<DtlsSocketContext> context);
43
44 // Creates a DtlsListener, to create server DtlsSockets only for peers that complete a
45 // cookie exchange (see DtlsListener)
46 std::unique_ptr<DtlsListener> createListener();
47
48 // Returns the fingerprint of the user cert that was passed into the constructor
49 void getMyCertFingerprint(char *fingerprint);
50
51 // Returns a reference to the timer context that was passed into the constructor
52 DtlsTimerContext& getTimerContext() {return *mTimerContext;}
53
54 // The default SrtpProfile used at construction time (default is: SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32)
55 static const char* DefaultSrtpProfile;
56
57 // Changes the default SRTP profiles supported (default is: SRTP_AES128_CM_SHA1_80:SRTP_AES128_CM_SHA1_32)
58 void setSrtpProfiles(const char *policyStr);
59
60 // Changes the default DTLS Cipher Suites supported
61 void setCipherSuites(const char *cipherSuites);
62
63 // The largest UDP payload DTLS sends, for sockets created from now on: OpenSSL fits its
64 // records to it, and they're sent in datagrams no larger, so a handshake flight doesn't
65 // depend on IP fragmentation. The default, 1200, leaves room for IPv6 and TURN framing.
66 static const unsigned int DefaultMtu = 1200;
67 void setMtu(unsigned int mtu) { mMtu = mtu; }
68 unsigned int getMtu() const { return mMtu; }
69
70 // Seconds a server socket's handshake may take, from its creation, before it fails - rather
71 // than when OpenSSL stops retransmitting (after several minutes), so a peer that starts a handshake
72 // and goes quiet doesn't hold the socket that long. 0 leaves it to OpenSSL. Client sockets,
73 // which we start towards the peer the SDP names, are left to OpenSSL.
74 static const unsigned int DefaultServerHandshakeTimeout = 30;
75 void setServerHandshakeTimeout(unsigned int seconds) { mServerHandshakeTimeout = seconds; }
76
77 // Examines the first few bits of a packet to determine its type: rtp, dtls, stun or unknown
78 static PacketType demuxPacket(const unsigned char *buf, size_t len);
79
80private:
81 friend class DtlsSocket;
82 friend class DtlsListener;
83
84 // DTLS cookies (RFC 6347 section 4.2.1): an HMAC of the peer's address under a random secret
85 static int generateCookie(SSL* ssl, unsigned char* cookie, unsigned int* cookieLen);
86 static int verifyCookie(SSL* ssl, const unsigned char* cookie, unsigned int cookieLen);
87 static bool computeCookie(SSL* ssl, unsigned char* cookie, unsigned int* cookieLen);
88
89 // The SSL ex_data index of the peer (const std::string*) a cookie is for
90 static int cookiePeerIndex();
91
92 SSL_CTX* mContext;
93 std::unique_ptr<DtlsTimerContext> mTimerContext;
94 X509 *mCert;
95 unsigned char mCookieSecret[32];
96 unsigned int mMtu = DefaultMtu;
97 unsigned int mServerHandshakeTimeout = DefaultServerHandshakeTimeout;
98};
99
100}
101#endif
102
103#endif
104/* ====================================================================
105
106 Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com
107 Copyright (c) 2007-2008, Eric Rescorla and Derek MacDonald
108 All rights reserved.
109
110 Redistribution and use in source and binary forms, with or without
111 modification, are permitted provided that the following conditions are
112 met:
113
114 1. Redistributions of source code must retain the above copyright
115 notice, this list of conditions and the following disclaimer.
116
117 2. Redistributions in binary form must reproduce the above copyright
118 notice, this list of conditions and the following disclaimer in the
119 documentation and/or other materials provided with the distribution.
120
121 3. None of the contributors names may be used to endorse or promote
122 products derived from this software without specific prior written
123 permission.
124
125 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
126 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
127 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
128 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
129 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
130 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
131 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
132 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
133 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
134 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
135 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
136
137 ==================================================================== */
struct ssl_ctx_st SSL_CTX
struct evp_pkey_st EVP_PKEY
struct x509_st X509
struct ssl_st SSL
static const Data cookie("z9hG4bK")
unsigned int getMtu() const
DtlsSocket * createClient(std::unique_ptr< DtlsSocketContext > context)
DtlsSocket * createServer(std::unique_ptr< DtlsSocketContext > context)
static const unsigned int DefaultServerHandshakeTimeout
void setServerHandshakeTimeout(unsigned int seconds)
static const char * DefaultSrtpProfile
void getMyCertFingerprint(char *fingerprint)
static PacketType demuxPacket(const unsigned char *buf, size_t len)
void setSrtpProfiles(const char *policyStr)
static const unsigned int DefaultMtu
void setCipherSuites(const char *cipherSuites)
void setMtu(unsigned int mtu)
DtlsTimerContext & getTimerContext()
std::unique_ptr< DtlsListener > createListener()
void list()
Definition userAdmin.cxx:66