5 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from the
9 * Internet community. Development is led by Duane Wessels of the
10 * National Laboratory for Applied Network Research and funded by the
11 * National Science Foundation. Squid is Copyrighted (C) 1998 by
12 * Duane Wessels and the University of California San Diego. Please
13 * see the COPYRIGHT file for full details. Squid incorporates
14 * software developed and/or copyrighted by other sources. Please see
15 * the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 #ifndef SQUID_SSL_SUPPORT_H
34 #define SQUID_SSL_SUPPORT_H
36 #include "CbDataList.h"
37 #include "ssl/gadgets.h"
39 #if HAVE_OPENSSL_SSL_H
40 #include <openssl/ssl.h>
42 #if HAVE_OPENSSL_X509V3_H
43 #include <openssl/x509v3.h>
45 #if HAVE_OPENSSL_ERR_H
46 #include <openssl/err.h>
48 #if HAVE_OPENSSL_ENGINE_H
49 #include <openssl/engine.h>
53 \defgroup ServerProtocolSSLAPI Server-Side SSL API
54 \ingroup ServerProtocol
57 // Custom SSL errors; assumes all official errors are positive
58 #define SQUID_X509_V_ERR_CERT_CHANGE -3
59 #define SQUID_ERR_SSL_HANDSHAKE -2
60 #define SQUID_X509_V_ERR_DOMAIN_MISMATCH -1
61 // All SSL errors range: from smallest (negative) custom to largest SSL error
62 #define SQUID_SSL_ERROR_MIN SQUID_X509_V_ERR_CERT_CHANGE
63 #define SQUID_SSL_ERROR_MAX INT_MAX
72 /// Squid defined error code (<0), an error code returned by SSL X509 api, or SSL_ERROR_NONE
73 typedef int ssl_error_t
;
75 typedef CbDataList
<Ssl::ssl_error_t
> Errors
;
79 /// \ingroup ServerProtocolSSLAPI
80 SSL_CTX
*sslCreateServerContext(AnyP::PortCfg
&port
);
82 /// \ingroup ServerProtocolSSLAPI
83 SSL_CTX
*sslCreateClientContext(const char *certfile
, const char *keyfile
, int version
, const char *cipher
, const char *options
, const char *flags
, const char *CAfile
, const char *CApath
, const char *CRLfile
);
85 /// \ingroup ServerProtocolSSLAPI
86 int ssl_read_method(int, char *, int);
88 /// \ingroup ServerProtocolSSLAPI
89 int ssl_write_method(int, const char *, int);
91 /// \ingroup ServerProtocolSSLAPI
92 void ssl_shutdown_method(SSL
*ssl
);
94 /// \ingroup ServerProtocolSSLAPI
95 const char *sslGetUserEmail(SSL
*ssl
);
97 /// \ingroup ServerProtocolSSLAPI
98 typedef char const *SSLGETATTRIBUTE(SSL
*, const char *);
100 /// \ingroup ServerProtocolSSLAPI
101 SSLGETATTRIBUTE sslGetUserAttribute
;
103 /// \ingroup ServerProtocolSSLAPI
104 SSLGETATTRIBUTE sslGetCAAttribute
;
106 /// \ingroup ServerProtocolSSLAPI
107 const char *sslGetUserCertificatePEM(SSL
*ssl
);
109 /// \ingroup ServerProtocolSSLAPI
110 const char *sslGetUserCertificateChainPEM(SSL
*ssl
);
115 \ingroup ServerProtocolSSLAPI
116 * Supported ssl-bump modes
118 enum BumpMode
{bumpNone
= 0, bumpClientFirst
, bumpServerFirst
, bumpEnd
};
121 \ingroup ServerProtocolSSLAPI
122 * Short names for ssl-bump modes
124 extern const char *BumpModeStr
[];
127 \ingroup ServerProtocolSSLAPI
128 * Return the short name of the ssl-bump mode "bm"
130 inline const char *bumpMode(int bm
)
132 return (0 <= bm
&& bm
< Ssl::bumpEnd
) ? Ssl::BumpModeStr
[bm
] : NULL
;
136 \ingroup ServerProtocolSSLAPI
137 * Parses the SSL flags.
139 long parse_flags(const char *flags
);
142 \ingroup ServerProtocolSSLAPI
143 * Parses the SSL options.
145 long parse_options(const char *options
);
148 \ingroup ServerProtocolSSLAPI
149 * Load a CRLs list stored in a file
151 STACK_OF(X509_CRL
) *loadCrl(const char *CRLFile
, long &flags
);
154 \ingroup ServerProtocolSSLAPI
155 * Load DH params from file
157 DH
*readDHParams(const char *dhfile
);
160 \ingroup ServerProtocolSSLAPI
161 * Compute the Ssl::ContextMethod (SSL_METHOD) from SSL version
163 ContextMethod
contextMethod(int version
);
166 \ingroup ServerProtocolSSLAPI
167 * Generate a certificate to be used as untrusted signing certificate, based on a trusted CA
169 bool generateUntrustedCert(X509_Pointer
& untrustedCert
, EVP_PKEY_Pointer
& untrustedPkey
, X509_Pointer
const & cert
, EVP_PKEY_Pointer
const & pkey
);
172 \ingroup ServerProtocolSSLAPI
173 * Decide on the kind of certificate and generate a CA- or self-signed one
175 SSL_CTX
* generateSslContext(CertificateProperties
const &properties
, AnyP::PortCfg
&port
);
178 \ingroup ServerProtocolSSLAPI
179 * Check if the certificate of the given context is still valid
180 \param sslContext The context to check
181 \param properties Check if the context certificate matches the given properties
182 \return true if the contexts certificate is valid, false otherwise
184 bool verifySslCertificate(SSL_CTX
* sslContext
, CertificateProperties
const &properties
);
187 \ingroup ServerProtocolSSLAPI
188 * Read private key and certificate from memory and generate SSL context
191 SSL_CTX
* generateSslContextUsingPkeyAndCertFromMemory(const char * data
, AnyP::PortCfg
&port
);
194 \ingroup ServerProtocolSSLAPI
195 * Adds the certificates in certList to the certificate chain of the SSL context
197 void addChainToSslContext(SSL_CTX
*sslContext
, STACK_OF(X509
) *certList
);
200 \ingroup ServerProtocolSSLAPI
201 * Read certificate, private key and any certificates which must be chained from files.
202 * See also: Ssl::readCertAndPrivateKeyFromFiles function, defined in gadgets.h
203 * \param certFilename name of file with certificate and certificates which must be chainned.
204 * \param keyFilename name of file with private key.
206 void readCertChainAndPrivateKeyFromFiles(X509_Pointer
& cert
, EVP_PKEY_Pointer
& pkey
, X509_STACK_Pointer
& chain
, char const * certFilename
, char const * keyFilename
);
209 \ingroup ServerProtocolSSLAPI
210 * Iterates over the X509 common and alternate names and to see if matches with given data
211 * using the check_func.
212 \param peer_cert The X509 cert to check
213 \param check_data The data with which the X509 CNs compared
214 \param check_func The function used to match X509 CNs. The CN data passed as ASN1_STRING data
215 \return 1 if any of the certificate CN matches, 0 if none matches.
217 int matchX509CommonNames(X509
*peer_cert
, void *check_data
, int (*check_func
)(void *check_data
, ASN1_STRING
*cn_data
));
220 \ingroup ServerProtocolSSLAPI
221 * Check if the certificate is valid for a server
222 \param cert The X509 cert to check.
223 \param server The server name.
224 \return true if the certificate is valid for the server or false otherwise.
226 bool checkX509ServerValidity(X509
*cert
, const char *server
);
229 \ingroup ServerProtocolSSLAPI
230 * Convert a given ASN1_TIME to a string form.
231 \param tm the time in ASN1_TIME form
232 \param buf the buffer to write the output
233 \param len write at most len bytes
234 \return The number of bytes written
236 int asn1timeToString(ASN1_TIME
*tm
, char *buf
, int len
);
239 \ingroup ServerProtocolSSLAPI
240 * Sets the hostname for the Server Name Indication (SNI) TLS extension
241 * if supported by the used openssl toolkit.
242 \return true if SNI set false otherwise
244 bool setClientSNI(SSL
*ssl
, const char *fqdn
);
249 #if defined(__cplusplus)
251 /** \cond AUTODOCS-IGNORE */
256 /// \ingroup ServerProtocolSSLAPI
258 int SSL_set_fd(SSL
*ssl
, int fd
)
260 return ::SSL_set_fd(ssl
, _get_osfhandle(fd
));
263 /// \ingroup ServerProtocolSSLAPI
264 #define SSL_set_fd(ssl,fd) Squid::SSL_set_fd(ssl,fd)
266 } /* namespace Squid */
270 /// \ingroup ServerProtocolSSLAPI
271 #define SSL_set_fd(s,f) (SSL_set_fd(s, _get_osfhandle(f)))
273 #endif /* __cplusplus */
275 #endif /* _SQUID_MSWIN_ */
277 #endif /* SQUID_SSL_SUPPORT_H */