2 * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 83 SSL accelerator support */
11 #ifndef SQUID_SSL_SUPPORT_H
12 #define SQUID_SSL_SUPPORT_H
16 #include "base/CbDataList.h"
17 #include "comm/forward.h"
18 #include "sbuf/SBuf.h"
19 #include "security/forward.h"
20 #include "ssl/gadgets.h"
22 #if HAVE_OPENSSL_X509V3_H
23 #include <openssl/x509v3.h>
25 #if HAVE_OPENSSL_ERR_H
26 #include <openssl/err.h>
28 #if HAVE_OPENSSL_ENGINE_H
29 #include <openssl/engine.h>
35 \defgroup ServerProtocolSSLAPI Server-Side SSL API
36 \ingroup ServerProtocol
39 // Custom SSL errors; assumes all official errors are positive
40 #define SQUID_X509_V_ERR_INFINITE_VALIDATION -4
41 #define SQUID_X509_V_ERR_CERT_CHANGE -3
42 #define SQUID_ERR_SSL_HANDSHAKE -2
43 #define SQUID_X509_V_ERR_DOMAIN_MISMATCH -1
44 // All SSL errors range: from smallest (negative) custom to largest SSL error
45 #define SQUID_SSL_ERROR_MIN SQUID_X509_V_ERR_CERT_CHANGE
46 #define SQUID_SSL_ERROR_MAX INT_MAX
48 // Maximum certificate validation callbacks. OpenSSL versions exceeding this
49 // limit are deemed stuck in an infinite validation loop (OpenSSL bug #3090)
50 // and will trigger the SQUID_X509_V_ERR_INFINITE_VALIDATION error.
51 // Can be set to a number up to UINT32_MAX
52 #ifndef SQUID_CERT_VALIDATION_ITERATION_MAX
53 #define SQUID_CERT_VALIDATION_ITERATION_MAX 16384
68 /// initialize the SSL library global state.
69 /// call before generating any SSL context
72 typedef CbDataList
<Security::ErrorCode
> Errors
;
75 class CertValidationResponse
;
76 typedef RefCount
<CertValidationResponse
> CertValidationResponsePointer
;
78 /// Creates SSL Client connection structure and initializes SSL I/O (Comm and BIO).
79 /// On errors, emits DBG_IMPORTANT with details and returns false.
80 bool CreateClient(Security::ContextPtr sslContext
, const Comm::ConnectionPointer
&, const char *squidCtx
);
82 /// Creates SSL Server connection structure and initializes SSL I/O (Comm and BIO).
83 /// On errors, emits DBG_IMPORTANT with details and returns false.
84 bool CreateServer(Security::ContextPtr sslContext
, const Comm::ConnectionPointer
&, const char *squidCtx
);
86 /// An SSL certificate-related error.
87 /// Pairs an error code with the certificate experiencing the error.
91 Security::ErrorCode code
; ///< certificate error code
92 Security::CertPointer cert
; ///< certificate with the above error code
94 * Absolute cert position in the final certificate chain that may include
95 * intermediate certificates. Chain positions start with zero and increase
96 * towards the root certificate. Negative if unknown.
99 CertError(Security::ErrorCode anErr
, X509
*aCert
, int depth
= -1);
100 bool operator == (const CertError
&ce
) const {
101 // We expect to be used in contexts where identical certificates have
102 // identical pointers.
103 return code
== ce
.code
&& depth
== ce
.depth
&& cert
.get() == ce
.cert
.get();
105 bool operator != (const CertError
&ce
) const {
106 return !(*this == ce
);
110 /// Holds a list of certificate SSL errors
111 typedef CbDataList
<Ssl::CertError
> CertErrors
;
113 void SetSessionCallbacks(Security::ContextPtr
);
114 extern Ipc::MemMap
*SessionCache
;
115 extern const char *SessionCacheName
;
117 /// initialize a TLS server context with OpenSSL specific settings
118 bool InitServerContext(const Security::ContextPointer
&, AnyP::PortCfg
&);
120 /// initialize a TLS client context with OpenSSL specific settings
121 bool InitClientContext(Security::ContextPtr
&, Security::PeerOptions
&, long options
, long flags
);
125 /// \ingroup ServerProtocolSSLAPI
126 int ssl_read_method(int, char *, int);
128 /// \ingroup ServerProtocolSSLAPI
129 int ssl_write_method(int, const char *, int);
131 /// \ingroup ServerProtocolSSLAPI
132 void ssl_shutdown_method(SSL
*ssl
);
134 /// \ingroup ServerProtocolSSLAPI
135 const char *sslGetUserEmail(SSL
*ssl
);
137 /// \ingroup ServerProtocolSSLAPI
138 const char *sslGetUserAttribute(SSL
*ssl
, const char *attribute_name
);
140 /// \ingroup ServerProtocolSSLAPI
141 const char *sslGetCAAttribute(SSL
*ssl
, const char *attribute_name
);
143 /// \ingroup ServerProtocolSSLAPI
144 const char *sslGetUserCertificatePEM(SSL
*ssl
);
146 /// \ingroup ServerProtocolSSLAPI
147 const char *sslGetUserCertificateChainPEM(SSL
*ssl
);
151 /// \ingroup ServerProtocolSSLAPI
152 typedef char const *GETX509ATTRIBUTE(X509
*, const char *);
154 /// \ingroup ServerProtocolSSLAPI
155 GETX509ATTRIBUTE GetX509UserAttribute
;
157 /// \ingroup ServerProtocolSSLAPI
158 GETX509ATTRIBUTE GetX509CAAttribute
;
160 /// \ingroup ServerProtocolSSLAPI
161 GETX509ATTRIBUTE GetX509Fingerprint
;
163 extern const EVP_MD
*DefaultSignHash
;
166 \ingroup ServerProtocolSSLAPI
167 * Supported ssl-bump modes
169 enum BumpMode
{bumpNone
= 0, bumpClientFirst
, bumpServerFirst
, bumpPeek
, bumpStare
, bumpBump
, bumpSplice
, bumpTerminate
, /*bumpErr,*/ bumpEnd
};
171 enum BumpStep
{bumpStep1
, bumpStep2
, bumpStep3
};
174 \ingroup ServerProtocolSSLAPI
175 * Short names for ssl-bump modes
177 extern const char *BumpModeStr
[];
180 \ingroup ServerProtocolSSLAPI
181 * Return the short name of the ssl-bump mode "bm"
183 inline const char *bumpMode(int bm
)
185 return (0 <= bm
&& bm
< Ssl::bumpEnd
) ? Ssl::BumpModeStr
[bm
] : NULL
;
188 /// certificates indexed by issuer name
189 typedef std::multimap
<SBuf
, X509
*> CertsIndexedList
;
192 * Load PEM-encoded certificates from the given file.
194 bool loadCerts(const char *certsFile
, Ssl::CertsIndexedList
&list
);
197 * Load PEM-encoded certificates to the squid untrusteds certificates
198 * internal DB from the given file.
200 bool loadSquidUntrusted(const char *path
);
203 * Removes all certificates from squid untrusteds certificates
204 * internal DB and frees all memory
206 void unloadSquidUntrusted();
209 * Add the certificate cert to ssl object untrusted certificates.
210 * Squid uses an attached to SSL object list of untrusted certificates,
211 * with certificates which can be used to complete incomplete chains sent
214 void SSL_add_untrusted_cert(SSL
*ssl
, X509
*cert
);
217 * Searches in serverCertificates list for the cert issuer and if not found
218 * and Authority Info Access of cert provides a URI return it.
220 const char *uriOfIssuerIfMissing(X509
*cert
, Security::CertList
const &serverCertificates
);
223 * Fill URIs queue with the uris of missing certificates from serverCertificate chain
224 * if this information provided by Authority Info Access.
226 void missingChainCertificatesUrls(std::queue
<SBuf
> &URIs
, Security::CertList
const &serverCertificates
);
229 \ingroup ServerProtocolSSLAPI
230 * Generate a certificate to be used as untrusted signing certificate, based on a trusted CA
232 bool generateUntrustedCert(Security::CertPointer
& untrustedCert
, EVP_PKEY_Pointer
& untrustedPkey
, Security::CertPointer
const & cert
, EVP_PKEY_Pointer
const & pkey
);
234 /// certificates indexed by issuer name
235 typedef std::multimap
<SBuf
, X509
*> CertsIndexedList
;
238 \ingroup ServerProtocolSSLAPI
239 * Load PEM-encoded certificates from the given file.
241 bool loadCerts(const char *certsFile
, Ssl::CertsIndexedList
&list
);
244 \ingroup ServerProtocolSSLAPI
245 * Load PEM-encoded certificates to the squid untrusteds certificates
246 * internal DB from the given file.
248 bool loadSquidUntrusted(const char *path
);
251 \ingroup ServerProtocolSSLAPI
252 * Removes all certificates from squid untrusteds certificates
253 * internal DB and frees all memory
255 void unloadSquidUntrusted();
258 \ingroup ServerProtocolSSLAPI
259 * Decide on the kind of certificate and generate a CA- or self-signed one
261 Security::ContextPtr
generateSslContext(CertificateProperties
const &properties
, AnyP::PortCfg
&port
);
264 \ingroup ServerProtocolSSLAPI
265 * Check if the certificate of the given context is still valid
266 \param sslContext The context to check
267 \param properties Check if the context certificate matches the given properties
268 \return true if the contexts certificate is valid, false otherwise
270 bool verifySslCertificate(Security::ContextPtr sslContext
, CertificateProperties
const &properties
);
273 \ingroup ServerProtocolSSLAPI
274 * Read private key and certificate from memory and generate SSL context
277 Security::ContextPtr
generateSslContextUsingPkeyAndCertFromMemory(const char * data
, AnyP::PortCfg
&port
);
280 \ingroup ServerProtocolSSLAPI
281 * Create an SSL context using the provided certificate and key
283 Security::ContextPtr
createSSLContext(Security::CertPointer
& x509
, Ssl::EVP_PKEY_Pointer
& pkey
, AnyP::PortCfg
&port
);
286 \ingroup ServerProtocolSSLAPI
287 * Chain signing certificate and chained certificates to an SSL Context
289 void chainCertificatesToSSLContext(SSL_CTX
*sslContext
, AnyP::PortCfg
&port
);
292 \ingroup ServerProtocolSSLAPI
293 * Configure a previously unconfigured SSL context object.
295 void configureUnconfiguredSslContext(SSL_CTX
*sslContext
, Ssl::CertSignAlgorithm signAlgorithm
,AnyP::PortCfg
&port
);
298 \ingroup ServerProtocolSSLAPI
299 * Generates a certificate and a private key using provided properies and set it
302 bool configureSSL(SSL
*ssl
, CertificateProperties
const &properties
, AnyP::PortCfg
&port
);
305 \ingroup ServerProtocolSSLAPI
306 * Read private key and certificate from memory and set it to SSL object
309 bool configureSSLUsingPkeyAndCertFromMemory(SSL
*ssl
, const char *data
, AnyP::PortCfg
&port
);
312 \ingroup ServerProtocolSSLAPI
313 * Adds the certificates in certList to the certificate chain of the SSL context
315 void addChainToSslContext(Security::ContextPtr sslContext
, STACK_OF(X509
) *certList
);
318 \ingroup ServerProtocolSSLAPI
319 * Configures sslContext to use squid untrusted certificates internal list
320 * to complete certificate chains when verifies SSL servers certificates.
322 void useSquidUntrusted(SSL_CTX
*sslContext
);
325 \ingroup ServerProtocolSSLAPI
326 * Read certificate, private key and any certificates which must be chained from files.
327 * See also: Ssl::readCertAndPrivateKeyFromFiles function, defined in gadgets.h
328 * \param certFilename name of file with certificate and certificates which must be chainned.
329 * \param keyFilename name of file with private key.
331 void readCertChainAndPrivateKeyFromFiles(Security::CertPointer
& cert
, EVP_PKEY_Pointer
& pkey
, X509_STACK_Pointer
& chain
, char const * certFilename
, char const * keyFilename
);
334 \ingroup ServerProtocolSSLAPI
335 * Iterates over the X509 common and alternate names and to see if matches with given data
336 * using the check_func.
337 \param peer_cert The X509 cert to check
338 \param check_data The data with which the X509 CNs compared
339 \param check_func The function used to match X509 CNs. The CN data passed as ASN1_STRING data
340 \return 1 if any of the certificate CN matches, 0 if none matches.
342 int matchX509CommonNames(X509
*peer_cert
, void *check_data
, int (*check_func
)(void *check_data
, ASN1_STRING
*cn_data
));
345 \ingroup ServerProtocolSSLAPI
346 * Check if the certificate is valid for a server
347 \param cert The X509 cert to check.
348 \param server The server name.
349 \return true if the certificate is valid for the server or false otherwise.
351 bool checkX509ServerValidity(X509
*cert
, const char *server
);
354 \ingroup ServerProtocolSSLAPI
355 * Convert a given ASN1_TIME to a string form.
356 \param tm the time in ASN1_TIME form
357 \param buf the buffer to write the output
358 \param len write at most len bytes
359 \return The number of bytes written
361 int asn1timeToString(ASN1_TIME
*tm
, char *buf
, int len
);
364 \ingroup ServerProtocolSSLAPI
365 * Sets the hostname for the Server Name Indication (SNI) TLS extension
366 * if supported by the used openssl toolkit.
367 \return true if SNI set false otherwise
369 bool setClientSNI(SSL
*ssl
, const char *fqdn
);
375 #if defined(__cplusplus)
377 /** \cond AUTODOCS-IGNORE */
382 /// \ingroup ServerProtocolSSLAPI
384 int SSL_set_fd(SSL
*ssl
, int fd
)
386 return ::SSL_set_fd(ssl
, _get_osfhandle(fd
));
389 /// \ingroup ServerProtocolSSLAPI
390 #define SSL_set_fd(ssl,fd) Squid::SSL_set_fd(ssl,fd)
392 } /* namespace Squid */
396 /// \ingroup ServerProtocolSSLAPI
397 #define SSL_set_fd(s,f) (SSL_set_fd(s, _get_osfhandle(f)))
399 #endif /* __cplusplus */
401 #endif /* _SQUID_WINDOWS_ */
403 #endif /* USE_OPENSSL */
404 #endif /* SQUID_SSL_SUPPORT_H */