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 "sbuf/SBuf.h"
18 #include "security/forward.h"
19 #include "ssl/gadgets.h"
21 #if HAVE_OPENSSL_X509V3_H
22 #include <openssl/x509v3.h>
24 #if HAVE_OPENSSL_ERR_H
25 #include <openssl/err.h>
27 #if HAVE_OPENSSL_ENGINE_H
28 #include <openssl/engine.h>
33 \defgroup ServerProtocolSSLAPI Server-Side SSL API
34 \ingroup ServerProtocol
37 // Custom SSL errors; assumes all official errors are positive
38 #define SQUID_X509_V_ERR_INFINITE_VALIDATION -4
39 #define SQUID_X509_V_ERR_CERT_CHANGE -3
40 #define SQUID_ERR_SSL_HANDSHAKE -2
41 #define SQUID_X509_V_ERR_DOMAIN_MISMATCH -1
42 // All SSL errors range: from smallest (negative) custom to largest SSL error
43 #define SQUID_SSL_ERROR_MIN SQUID_X509_V_ERR_CERT_CHANGE
44 #define SQUID_SSL_ERROR_MAX INT_MAX
46 // Maximum certificate validation callbacks. OpenSSL versions exceeding this
47 // limit are deemed stuck in an infinite validation loop (OpenSSL bug #3090)
48 // and will trigger the SQUID_X509_V_ERR_INFINITE_VALIDATION error.
49 // Can be set to a number up to UINT32_MAX
50 #ifndef SQUID_CERT_VALIDATION_ITERATION_MAX
51 #define SQUID_CERT_VALIDATION_ITERATION_MAX 16384
66 /// initialize the SSL library global state.
67 /// call before generating any SSL context
70 /// Squid defined error code (<0), an error code returned by SSL X509 api, or SSL_ERROR_NONE
71 typedef int ssl_error_t
;
73 typedef CbDataList
<Ssl::ssl_error_t
> Errors
;
76 class CertValidationResponse
;
77 typedef RefCount
<CertValidationResponse
> CertValidationResponsePointer
;
79 /// Creates SSL Client connection structure and initializes SSL I/O (Comm and BIO).
80 /// On errors, emits DBG_IMPORTANT with details and returns NULL.
81 SSL
*CreateClient(Security::ContextPtr sslContext
, const int fd
, const char *squidCtx
);
83 /// Creates SSL Server connection structure and initializes SSL I/O (Comm and BIO).
84 /// On errors, emits DBG_IMPORTANT with details and returns NULL.
85 SSL
*CreateServer(Security::ContextPtr sslContext
, const int fd
, const char *squidCtx
);
87 /// An SSL certificate-related error.
88 /// Pairs an error code with the certificate experiencing the error.
92 ssl_error_t code
; ///< certificate error code
93 Security::CertPointer cert
; ///< certificate with the above error code
95 * Absolute cert position in the final certificate chain that may include
96 * intermediate certificates. Chain positions start with zero and increase
97 * towards the root certificate. Negative if unknown.
100 CertError(ssl_error_t anErr
, X509
*aCert
, int depth
= -1);
101 CertError(CertError
const &err
);
102 CertError
& operator = (const CertError
&old
);
103 bool operator == (const CertError
&ce
) const;
104 bool operator != (const CertError
&ce
) const;
107 /// Holds a list of certificate SSL errors
108 typedef CbDataList
<Ssl::CertError
> CertErrors
;
110 void SetSessionCallbacks(Security::ContextPtr
);
111 extern Ipc::MemMap
*SessionCache
;
112 extern const char *SessionCacheName
;
116 /// \ingroup ServerProtocolSSLAPI
117 Security::ContextPtr
sslCreateServerContext(AnyP::PortCfg
&port
);
119 /// \ingroup ServerProtocolSSLAPI
120 Security::ContextPtr
sslCreateClientContext(Security::PeerOptions
&, long options
, long flags
);
122 /// \ingroup ServerProtocolSSLAPI
123 int ssl_read_method(int, char *, int);
125 /// \ingroup ServerProtocolSSLAPI
126 int ssl_write_method(int, const char *, int);
128 /// \ingroup ServerProtocolSSLAPI
129 void ssl_shutdown_method(SSL
*ssl
);
131 /// \ingroup ServerProtocolSSLAPI
132 const char *sslGetUserEmail(SSL
*ssl
);
134 /// \ingroup ServerProtocolSSLAPI
135 const char *sslGetUserAttribute(SSL
*ssl
, const char *attribute_name
);
137 /// \ingroup ServerProtocolSSLAPI
138 const char *sslGetCAAttribute(SSL
*ssl
, const char *attribute_name
);
140 /// \ingroup ServerProtocolSSLAPI
141 const char *sslGetUserCertificatePEM(SSL
*ssl
);
143 /// \ingroup ServerProtocolSSLAPI
144 const char *sslGetUserCertificateChainPEM(SSL
*ssl
);
148 /// \ingroup ServerProtocolSSLAPI
149 typedef char const *GETX509ATTRIBUTE(X509
*, const char *);
151 /// \ingroup ServerProtocolSSLAPI
152 GETX509ATTRIBUTE GetX509UserAttribute
;
154 /// \ingroup ServerProtocolSSLAPI
155 GETX509ATTRIBUTE GetX509CAAttribute
;
157 /// \ingroup ServerProtocolSSLAPI
158 GETX509ATTRIBUTE GetX509Fingerprint
;
160 extern const EVP_MD
*DefaultSignHash
;
163 \ingroup ServerProtocolSSLAPI
164 * Supported ssl-bump modes
166 enum BumpMode
{bumpNone
= 0, bumpClientFirst
, bumpServerFirst
, bumpPeek
, bumpStare
, bumpBump
, bumpSplice
, bumpTerminate
, /*bumpErr,*/ bumpEnd
};
168 enum BumpStep
{bumpStep1
, bumpStep2
, bumpStep3
};
171 \ingroup ServerProtocolSSLAPI
172 * Short names for ssl-bump modes
174 extern const char *BumpModeStr
[];
177 \ingroup ServerProtocolSSLAPI
178 * Return the short name of the ssl-bump mode "bm"
180 inline const char *bumpMode(int bm
)
182 return (0 <= bm
&& bm
< Ssl::bumpEnd
) ? Ssl::BumpModeStr
[bm
] : NULL
;
186 \ingroup ServerProtocolSSLAPI
187 * Generate a certificate to be used as untrusted signing certificate, based on a trusted CA
189 bool generateUntrustedCert(Security::CertPointer
& untrustedCert
, EVP_PKEY_Pointer
& untrustedPkey
, Security::CertPointer
const & cert
, EVP_PKEY_Pointer
const & pkey
);
191 /// certificates indexed by issuer name
192 typedef std::multimap
<SBuf
, X509
*> CertsIndexedList
;
195 \ingroup ServerProtocolSSLAPI
196 * Load PEM-encoded certificates from the given file.
198 bool loadCerts(const char *certsFile
, Ssl::CertsIndexedList
&list
);
201 \ingroup ServerProtocolSSLAPI
202 * Load PEM-encoded certificates to the squid untrusteds certificates
203 * internal DB from the given file.
205 bool loadSquidUntrusted(const char *path
);
208 \ingroup ServerProtocolSSLAPI
209 * Removes all certificates from squid untrusteds certificates
210 * internal DB and frees all memory
212 void unloadSquidUntrusted();
215 \ingroup ServerProtocolSSLAPI
216 * Decide on the kind of certificate and generate a CA- or self-signed one
218 Security::ContextPtr
generateSslContext(CertificateProperties
const &properties
, AnyP::PortCfg
&port
);
221 \ingroup ServerProtocolSSLAPI
222 * Check if the certificate of the given context is still valid
223 \param sslContext The context to check
224 \param properties Check if the context certificate matches the given properties
225 \return true if the contexts certificate is valid, false otherwise
227 bool verifySslCertificate(Security::ContextPtr sslContext
, CertificateProperties
const &properties
);
230 \ingroup ServerProtocolSSLAPI
231 * Read private key and certificate from memory and generate SSL context
234 Security::ContextPtr
generateSslContextUsingPkeyAndCertFromMemory(const char * data
, AnyP::PortCfg
&port
);
237 \ingroup ServerProtocolSSLAPI
238 * Create an SSL context using the provided certificate and key
240 Security::ContextPtr
createSSLContext(Security::CertPointer
& x509
, Ssl::EVP_PKEY_Pointer
& pkey
, AnyP::PortCfg
&port
);
243 \ingroup ServerProtocolSSLAPI
244 * Chain signing certificate and chained certificates to an SSL Context
246 void chainCertificatesToSSLContext(SSL_CTX
*sslContext
, AnyP::PortCfg
&port
);
249 \ingroup ServerProtocolSSLAPI
250 * Configure a previously unconfigured SSL context object.
252 void configureUnconfiguredSslContext(SSL_CTX
*sslContext
, Ssl::CertSignAlgorithm signAlgorithm
,AnyP::PortCfg
&port
);
255 \ingroup ServerProtocolSSLAPI
256 * Generates a certificate and a private key using provided properies and set it
259 bool configureSSL(SSL
*ssl
, CertificateProperties
const &properties
, AnyP::PortCfg
&port
);
262 \ingroup ServerProtocolSSLAPI
263 * Read private key and certificate from memory and set it to SSL object
266 bool configureSSLUsingPkeyAndCertFromMemory(SSL
*ssl
, const char *data
, AnyP::PortCfg
&port
);
269 \ingroup ServerProtocolSSLAPI
270 * Adds the certificates in certList to the certificate chain of the SSL context
272 void addChainToSslContext(Security::ContextPtr sslContext
, STACK_OF(X509
) *certList
);
275 \ingroup ServerProtocolSSLAPI
276 * Configures sslContext to use squid untrusted certificates internal list
277 * to complete certificate chains when verifies SSL servers certificates.
279 void useSquidUntrusted(SSL_CTX
*sslContext
);
282 \ingroup ServerProtocolSSLAPI
283 * Read certificate, private key and any certificates which must be chained from files.
284 * See also: Ssl::readCertAndPrivateKeyFromFiles function, defined in gadgets.h
285 * \param certFilename name of file with certificate and certificates which must be chainned.
286 * \param keyFilename name of file with private key.
288 void readCertChainAndPrivateKeyFromFiles(Security::CertPointer
& cert
, EVP_PKEY_Pointer
& pkey
, X509_STACK_Pointer
& chain
, char const * certFilename
, char const * keyFilename
);
291 \ingroup ServerProtocolSSLAPI
292 * Iterates over the X509 common and alternate names and to see if matches with given data
293 * using the check_func.
294 \param peer_cert The X509 cert to check
295 \param check_data The data with which the X509 CNs compared
296 \param check_func The function used to match X509 CNs. The CN data passed as ASN1_STRING data
297 \return 1 if any of the certificate CN matches, 0 if none matches.
299 int matchX509CommonNames(X509
*peer_cert
, void *check_data
, int (*check_func
)(void *check_data
, ASN1_STRING
*cn_data
));
302 \ingroup ServerProtocolSSLAPI
303 * Check if the certificate is valid for a server
304 \param cert The X509 cert to check.
305 \param server The server name.
306 \return true if the certificate is valid for the server or false otherwise.
308 bool checkX509ServerValidity(X509
*cert
, const char *server
);
311 \ingroup ServerProtocolSSLAPI
312 * Convert a given ASN1_TIME to a string form.
313 \param tm the time in ASN1_TIME form
314 \param buf the buffer to write the output
315 \param len write at most len bytes
316 \return The number of bytes written
318 int asn1timeToString(ASN1_TIME
*tm
, char *buf
, int len
);
321 \ingroup ServerProtocolSSLAPI
322 * Sets the hostname for the Server Name Indication (SNI) TLS extension
323 * if supported by the used openssl toolkit.
324 \return true if SNI set false otherwise
326 bool setClientSNI(SSL
*ssl
, const char *fqdn
);
332 #if defined(__cplusplus)
334 /** \cond AUTODOCS-IGNORE */
339 /// \ingroup ServerProtocolSSLAPI
341 int SSL_set_fd(SSL
*ssl
, int fd
)
343 return ::SSL_set_fd(ssl
, _get_osfhandle(fd
));
346 /// \ingroup ServerProtocolSSLAPI
347 #define SSL_set_fd(ssl,fd) Squid::SSL_set_fd(ssl,fd)
349 } /* namespace Squid */
353 /// \ingroup ServerProtocolSSLAPI
354 #define SSL_set_fd(s,f) (SSL_set_fd(s, _get_osfhandle(f)))
356 #endif /* __cplusplus */
358 #endif /* _SQUID_WINDOWS_ */
360 #endif /* USE_OPENSSL */
361 #endif /* SQUID_SSL_SUPPORT_H */