reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Security.hxx
Go to the documentation of this file.
1#if !defined(RESIP_SECURITY_HXX)
2#define RESIP_SECURITY_HXX
3
4#include <map>
5#include <vector>
6#include <list>
7
8#if defined(HAVE_CONFIG_H)
9 #include "config.h"
10#endif
11
12
13#include "rutil/Socket.hxx"
17
18// If USE_SSL is not defined, Security will not be built, and this header will
19// not be installed. If you are including this file from a source tree, and are
20// getting link errors, the source tree was probably built without USE_SSL.
21//#if defined(USE_SSL)
22//#else
24//typedef void BIO;
25//typedef void SSL;
26//typedef void X509;
27//typedef void X509_STORE;
28//typedef void SSL_CTX;
29//typedef void EVP_PKEY;
30//#endif
31
32#include <openssl/ssl.h>
33
34namespace resip
35{
36
37class Contents;
38class Pkcs7Contents;
39class Security;
40class MultipartSignedContents;
41class SipMessage;
42
44{
45 public:
46 static int getResipConnectionExDataIdx();
47
48 class Exception final : public BaseException
49 {
50 public:
51 Exception(const Data& msg, const Data& file, int line);
52 const char* name() const noexcept override { return "SecurityException"; }
53 };
54
56 {
57 public:
59 CipherList(const Data& cipherList) : mCipherList(cipherList) {}
60 Data cipherList() const { return mCipherList; }
61 private:
62 Data mCipherList;
63 };
64
65 public:
66
67 typedef enum
68 {
72
73 struct PeerName
74 {
77 PeerName(NameType type, Data name): mType(type), mName(name){}
78 };
79
82
93
94 BaseSecurity(const CipherList& cipherSuite = StrongestSuite,
95 const Data& defaultPrivateKeyPassPhrase = Data::Empty,
96 const Data& dHParamsFilename = Data::Empty);
97 virtual ~BaseSecurity();
98
99 // used to initialize the openssl library
100 static void initialize();
101
110
111 virtual void preload()=0;
112
113 // name refers to the domainname or username which could be converted to a
114 // filename by convention
115 virtual void onReadPEM(const Data& name, PEMType type, Data& buffer) const =0;
116 virtual void onWritePEM(const Data& name, PEMType type, const Data& buffer) const =0;
117 virtual void onRemovePEM(const Data& name, PEMType type) const =0;
118
126
127 typedef std::vector<CertificateInfo> CertificateInfoContainer;
129
130 // All of these guys can throw SecurityException
131
132 void addRootCertPEM(const Data& x509PEMEncodedRootCerts);
133
134 void addDomainCertPEM(const Data& domainName, const Data& certPEM);
135 void addDomainCertDER(const Data& domainName, const Data& certDER);
136 bool hasDomainCert(const Data& domainName, bool logErrors = true) const;
137 void removeDomainCert(const Data& domainName);
138 Data getDomainCertDER(const Data& domainName) const;
139
140 void addDomainPrivateKeyPEM(const Data& domainName, const Data& privateKeyPEM, const Data& privateKeyPassPhrase = Data::Empty);
141 bool hasDomainPrivateKey(const Data& domainName) const;
142 void removeDomainPrivateKey(const Data& domainName);
143 Data getDomainPrivateKeyPEM(const Data& domainName) const;
144
145 void addUserCertPEM(const Data& aor, const Data& certPEM);
146 void addUserCertDER(const Data& aor, const Data& certDER);
147 bool hasUserCert(const Data& aor) const;
148 void removeUserCert(const Data& aor);
149 Data getUserCertDER(const Data& aor) const;
150
151 void setUserPassPhrase(const Data& aor, const Data& passPhrase);
152 bool hasUserPassPhrase(const Data& aor) const;
153 void removeUserPassPhrase(const Data& aor);
154 Data getUserPassPhrase(const Data& aor) const;
155
156 void addUserPrivateKeyPEM(const Data& aor, const Data& certPEM, const Data& privateKeyPassPhrase = Data::Empty);
157 void addUserPrivateKeyDER(const Data& aor, const Data& certDER, const Data& privateKeyPassPhrase = Data::Empty);
158 bool hasUserPrivateKey(const Data& aor) const;
159 void removeUserPrivateKey(const Data& aor);
160 Data getUserPrivateKeyPEM(const Data& aor) const;
161 Data getUserPrivateKeyDER(const Data& aor) const;
162
163 // Generates a self-signed certificate (SHA-256) and RSA key of keyLen bits for aor, and adds
164 // them as its user certificate and private key - throws if the key can't be generated
165 void generateUserCert(const Data& aor, int expireDays=365, int keyLen=2048);
166
167 // Produces a detached signature
168 MultipartSignedContents* sign(const Data& senderAor, Contents* );
169 Pkcs7Contents* encrypt(Contents* , const Data& recipCertName );
170 MultipartSignedContents* signAndEncrypt( const Data& senderAor, Contents* , const Data& recipCertName );
171
172 Data computeIdentity( const Data& signerDomain, const Data& in ) const;
173 bool checkIdentity( const Data& signerDomain, const Data& in, const Data& sig, X509* cert=NULL ) const;
174
175 void checkAndSetIdentity(SipMessage& msg, const Data& derCert=Data::Empty ) const;
176
177 // returns NULL if it fails
178 Contents* decrypt( const Data& decryptorAor, const Pkcs7Contents* );
179
180 // returns NULL if fails. returns the data that was originally signed
182 Data* signedBy, SignatureStatus* sigStat );
183 // returns the first SubjectAltName or commonName, if subjectAltName does not exist
184 static Data getCertName(X509 *cert);
185
186 // retrieves a list of all certificate names (subjectAltNAme's and CommonName)
187 static void getCertNames(X509 *cert, std::list<PeerName> &peerNames, bool useEmailAsSIP = false);
188
189 static bool isSelfSigned(const X509* cert);
190
191 static int matchHostName(const Data& certificateName, const Data& domainName);
192
193 // allow particular classes to acces the functions below
194 // friend class TlsConnection;
195
196 // Allow overriding of RFC 5922 rules on certificate matching.
197 static void setAllowWildcardCertificates(bool bEnable) { mAllowWildcardCertificates = bEnable; }
199
200 static SecurityTypes::SSLType parseSSLType(const Data& typeName);
201 static long parseOpenSSLCTXOption(const Data& optionName);
202
203 public:
204 SSL_CTX* getTlsCtx ();
205 SSL_CTX* getSslCtx ();
206
207 X509* getDomainCert( const Data& domain );
208 EVP_PKEY* getDomainKey( const Data& domain );
209 X509* getUserCert(const Data& aor);
210 EVP_PKEY* getUserPrivateKey(const Data& aor);
211
212 // map of name to certificates
213 typedef std::map<Data,X509*> X509Map;
214 typedef std::list<X509*> X509List;
215 typedef std::map<Data,EVP_PKEY*> PrivateKeyMap;
216 typedef std::map<Data,Data> PassPhraseMap;
217
218 protected:
229 SSL_CTX* mTlsCtx; // Context locked to TLSv1 (1.0 only)
230 SSL_CTX* mSslCtx; // Context that supports Dynamic TLS version negotiation
231 static void dumpAsn(const char*, Data);
232
236
237 // root cert list
239 X509_STORE* mRootTlsCerts;
240 X509_STORE* mRootSslCerts;
241
244
248
249 void addCertPEM (PEMType type, const Data& name, const Data& certPEM, bool write);
250 void addCertDER (PEMType type, const Data& name, const Data& certDER, bool write);
251 bool hasCert (PEMType type, const Data& name, bool logErrors = true) const;
252 void removeCert (PEMType type, const Data& name);
253 Data getCertDER (PEMType type, const Data& name) const;
254 void addCertX509(PEMType type, const Data& name, X509* cert, bool write);
255
256 void addPrivateKeyPEM (PEMType type, const Data& name, const Data& privateKeyPEM, bool write, const Data& privateKeyPassPhrase = Data::Empty);
257 void addPrivateKeyDER (PEMType type, const Data& name, const Data& privateKeyDER, bool write, const Data& privateKeyPassPhrase = Data::Empty);
258 bool hasPrivateKey (PEMType type, const Data& name) const;
259 void removePrivateKey (PEMType type, const Data& name);
260 Data getPrivateKeyPEM (PEMType type, const Data& name) const;
261 Data getPrivateKeyDER (PEMType type, const Data& name) const;
262 void addPrivateKeyPKEY(PEMType type, const Data& name, EVP_PKEY* pKey, bool write);
263
264 // match with wildcards
265 static int matchHostNameWithWildcards(const Data& certificateName, const Data& domainName);
267
268 void setDHParams(SSL_CTX* ctx);
269};
270
271class Security : public BaseSecurity
272{
273 public:
274 Security(const Data& pathToCerts, const CipherList& = StrongestSuite, const Data& defaultPrivateKeyPassPhrase = Data::Empty, const Data& dHParamsFilename = Data::Empty);
275 Security(const CipherList& = StrongestSuite, const Data& defaultPrivateKeyPassPhrase = Data::Empty, const Data& dHParamsFilename = Data::Empty);
276
277 void addCADirectory(const Data& caDirectory);
278 void addCAFile(const Data& caFile);
279
280 void loadCADirectory(const Data& directoryName);
281 void loadCAFile(const Data& fileName);
282 virtual void preload();
285 virtual SSL_CTX* createDomainCtx(const SSL_METHOD* method, const Data& domain, const Data& certificateFilename,
286 const Data& privateKeyFilename, const Data& privateKeyPassPhrase);
291 virtual void updateDomainCtx(SSL_CTX* ctx, const Data& domain, const Data& certificateFilename, const Data& privateKeyFilename, const Data& privateKeyPassPhrase);
292
293 virtual void onReadPEM(const Data& name, PEMType type, Data& buffer) const;
294 virtual void onWritePEM(const Data& name, PEMType type, const Data& buffer) const;
295 virtual void onRemovePEM(const Data& name, PEMType type) const;
296
297 private:
298 Data mPath;
299 std::list<Data> mCADirectories;
300 std::list<Data> mCAFiles;
301};
302
303}
304
305#endif
306
307/* ====================================================================
308 * The Vovida Software License, Version 1.0
309 *
310 * Copyright (c) 2026 SIP Spectrum, Inc. https://www.sipspectrum.com
311 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
312 *
313 * Redistribution and use in source and binary forms, with or without
314 * modification, are permitted provided that the following conditions
315 * are met:
316 *
317 * 1. Redistributions of source code must retain the above copyright
318 * notice, this list of conditions and the following disclaimer.
319 *
320 * 2. Redistributions in binary form must reproduce the above copyright
321 * notice, this list of conditions and the following disclaimer in
322 * the documentation and/or other materials provided with the
323 * distribution.
324 *
325 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
326 * and "Vovida Open Communication Application Library (VOCAL)" must
327 * not be used to endorse or promote products derived from this
328 * software without prior written permission. For written
329 * permission, please contact vocal@vovida.org.
330 *
331 * 4. Products derived from this software may not be called "VOCAL", nor
332 * may "VOCAL" appear in their name, without prior written
333 * permission of Vovida Networks, Inc.
334 *
335 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
336 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
337 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
338 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
339 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
340 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
341 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
342 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
343 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
344 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
345 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
346 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
347 * DAMAGE.
348 *
349 * ====================================================================
350 *
351 * This software consists of voluntary contributions made by Vovida
352 * Networks, Inc. and many individuals on behalf of Vovida Networks,
353 * Inc. For more information on Vovida Networks, Inc., please see
354 * <http://www.vovida.org/>.
355 *
356 */
struct ssl_ctx_st SSL_CTX
struct evp_pkey_st EVP_PKEY
struct x509_st X509
Handles cross-platform sockets compatibility.
The abstract base-class for all exceptions thrown by resip code.
CipherList(const Data &cipherList)
Definition Security.hxx:59
Exception(const Data &msg, const Data &file, int line)
const char * name() const noexcept override
Definition Security.hxx:52
Data mDefaultPrivateKeyPassPhrase
Definition Security.hxx:234
void addCertDER(PEMType type, const Data &name, const Data &certDER, bool write)
Definition Security.cxx:550
EVP_PKEY * getUserPrivateKey(const Data &aor)
static int getResipConnectionExDataIdx()
Definition Security.cxx:101
virtual void onReadPEM(const Data &name, PEMType type, Data &buffer) const =0
Data getUserPassPhrase(const Data &aor) const
static SecurityTypes::SSLType parseSSLType(const Data &typeName)
Converts a string containing an SSL type name to the corresponding enum value.
Data getUserPrivateKeyDER(const Data &aor) const
static void dumpAsn(const char *, Data)
void generateUserCert(const Data &aor, int expireDays=365, int keyLen=2048)
void addUserCertPEM(const Data &aor, const Data &certPEM)
static bool allowWildcardCertificates()
Definition Security.hxx:198
static long OpenSSLCTXClearOptions
Definition Security.hxx:92
MultipartSignedContents * signAndEncrypt(const Data &senderAor, Contents *, const Data &recipCertName)
bool hasUserCert(const Data &aor) const
virtual void preload()=0
void addPrivateKeyPEM(PEMType type, const Data &name, const Data &privateKeyPEM, bool write, const Data &privateKeyPassPhrase=Data::Empty)
Definition Security.cxx:940
void addCertPEM(PEMType type, const Data &name, const Data &certPEM, bool write)
Definition Security.cxx:574
void removeUserPrivateKey(const Data &aor)
PrivateKeyMap mUserPrivateKeys
Definition Security.hxx:247
Data computeIdentity(const Data &signerDomain, const Data &in) const
virtual ~BaseSecurity()
virtual void onWritePEM(const Data &name, PEMType type, const Data &buffer) const =0
void addRootCertPEM(const Data &x509PEMEncodedRootCerts)
void addUserCertDER(const Data &aor, const Data &certDER)
X509 * getUserCert(const Data &aor)
static Data getCertName(X509 *cert)
virtual void onRemovePEM(const Data &name, PEMType type) const =0
SSL_CTX * mTlsCtx
Note:
Definition Security.hxx:229
void addUserPrivateKeyDER(const Data &aor, const Data &certDER, const Data &privateKeyPassPhrase=Data::Empty)
static void setAllowWildcardCertificates(bool bEnable)
Definition Security.hxx:197
Contents * checkSignature(MultipartSignedContents *, Data *signedBy, SignatureStatus *sigStat)
void setDHParams(SSL_CTX *ctx)
void addPrivateKeyDER(PEMType type, const Data &name, const Data &privateKeyDER, bool write, const Data &privateKeyPassPhrase=Data::Empty)
Definition Security.cxx:874
Data getPrivateKeyPEM(PEMType type, const Data &name) const
void setUserPassPhrase(const Data &aor, const Data &passPhrase)
void addCertX509(PEMType type, const Data &name, X509 *cert, bool write)
Definition Security.cxx:613
Data getDomainCertDER(const Data &domainName) const
EVP_PKEY * getDomainKey(const Data &domain)
SSL_CTX * getSslCtx()
static int matchHostNameWithWildcards(const Data &certificateName, const Data &domainName)
Matches a certificate name, which may hold a wildcard, against the domain name we connected to,...
static CipherList StrongestSuite
Definition Security.hxx:81
void removeDomainPrivateKey(const Data &domainName)
static void getCertNames(X509 *cert, std::list< PeerName > &peerNames, bool useEmailAsSIP=false)
static int matchHostName(const Data &certificateName, const Data &domainName)
Applies the certificate and domain name matching rules.
std::map< Data, EVP_PKEY * > PrivateKeyMap
Definition Security.hxx:215
Contents * decrypt(const Data &decryptorAor, const Pkcs7Contents *)
void removeDomainCert(const Data &domainName)
SSL_CTX * getTlsCtx()
CipherList mCipherList
Definition Security.hxx:233
bool checkIdentity(const Data &signerDomain, const Data &in, const Data &sig, X509 *cert=NULL) const
void removeCert(PEMType type, const Data &name)
Definition Security.cxx:734
void checkAndSetIdentity(SipMessage &msg, const Data &derCert=Data::Empty) const
std::list< X509 * > X509List
Definition Security.hxx:214
static bool mAllowWildcardCertificates
Definition Security.hxx:266
void addPrivateKeyPKEY(PEMType type, const Data &name, EVP_PKEY *pKey, bool write)
Definition Security.cxx:791
bool hasUserPassPhrase(const Data &aor) const
static CipherList ExportableSuite
Definition Security.hxx:80
Data getDomainPrivateKeyPEM(const Data &domainName) const
void addDomainPrivateKeyPEM(const Data &domainName, const Data &privateKeyPEM, const Data &privateKeyPassPhrase=Data::Empty)
std::map< Data, Data > PassPhraseMap
Definition Security.hxx:216
static bool isSelfSigned(const X509 *cert)
static void initialize()
bool hasCert(PEMType type, const Data &name, bool logErrors=true) const
Definition Security.cxx:690
void addUserPrivateKeyPEM(const Data &aor, const Data &certPEM, const Data &privateKeyPassPhrase=Data::Empty)
std::map< Data, X509 * > X509Map
Definition Security.hxx:213
Data getUserCertDER(const Data &aor) const
X509_STORE * mRootTlsCerts
Definition Security.hxx:239
PrivateKeyMap mDomainPrivateKeys
Definition Security.hxx:243
void removeUserCert(const Data &aor)
void addDomainCertDER(const Data &domainName, const Data &certDER)
Data getPrivateKeyDER(PEMType type, const Data &name) const
static long parseOpenSSLCTXOption(const Data &optionName)
Converts a string containing an OpenSSL option name (for SSL_CTX_set_options) to the numeric value fr...
X509_STORE * mRootSslCerts
Definition Security.hxx:240
bool hasUserPrivateKey(const Data &aor) const
void removePrivateKey(PEMType type, const Data &name)
void removeUserPassPhrase(const Data &aor)
static long OpenSSLCTXSetOptions
Note:
Definition Security.hxx:91
bool hasPrivateKey(PEMType type, const Data &name) const
PassPhraseMap mUserPassPhrases
Definition Security.hxx:246
X509 * getDomainCert(const Data &domain)
bool hasDomainPrivateKey(const Data &domainName) const
std::vector< CertificateInfo > CertificateInfoContainer
Definition Security.hxx:127
Data getCertDER(PEMType type, const Data &name) const
Definition Security.cxx:752
CertificateInfoContainer getRootCertDescriptions() const
void addDomainCertPEM(const Data &domainName, const Data &certPEM)
MultipartSignedContents * sign(const Data &senderAor, Contents *)
Data getUserPrivateKeyPEM(const Data &aor) const
Pkcs7Contents * encrypt(Contents *, const Data &recipCertName)
bool hasDomainCert(const Data &domainName, bool logErrors=true) const
Base class for all SIP body types.
Definition Contents.hxx:51
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
static const Data Empty
Constant that represents a zero-length data.
Definition Data.hxx:882
SIP body type for holding Multipart-Signed body contents (MIME content-type multipart/signed).
SIP body type for holding PKCS7 contents (MIME content-type application/pkcs7-mime).
void addCADirectory(const Data &caDirectory)
Definition Security.cxx:215
virtual void preload()
Definition Security.cxx:277
virtual SSL_CTX * createDomainCtx(const SSL_METHOD *method, const Data &domain, const Data &certificateFilename, const Data &privateKeyFilename, const Data &privateKeyPassPhrase)
Throws BaseSecurity::Exception if the certificate or private key can't be loaded; nothing is left all...
Definition Security.cxx:407
virtual void onWritePEM(const Data &name, PEMType type, const Data &buffer) const
Definition Security.cxx:521
virtual void onReadPEM(const Data &name, PEMType type, Data &buffer) const
Definition Security.cxx:511
virtual void updateDomainCtx(SSL_CTX *ctx, const Data &domain, const Data &certificateFilename, const Data &privateKeyFilename, const Data &privateKeyPassPhrase)
Loads the domain's certificate chain and private key into ctx.
Definition Security.cxx:454
virtual void onRemovePEM(const Data &name, PEMType type) const
Definition Security.cxx:543
void loadCAFile(const Data &fileName)
Definition Security.cxx:259
void addCAFile(const Data &caFile)
Definition Security.cxx:226
void loadCADirectory(const Data &directoryName)
Definition Security.cxx:232
Represents a SIP message.
SSLType
Note: Before adding to the SSLType enum, please see the comments about mTlsCtx in resip/stack/ssl/Sec...
dcm! – add UnusedChecking(_enum) below;
PeerName(NameType type, Data name)
Definition Security.hxx:77