source: mod_gnutls/src/mod_gnutls.c @ fe21671

debian/masterdebian/stretch-backportsupstream
Last change on this file since fe21671 was fe21671, checked in by Thomas Klute <thomas2.klute@…>, 2 years ago

ssl_engine_disable(): Remove mod_gnutls filters, not first in chain

  • Property mode set to 100644
File size: 10.5 KB
Line 
1/*
2 *  Copyright 2004-2005 Paul Querna
3 *  Copyright 2008, 2014 Nikos Mavrogiannopoulos
4 *  Copyright 2011 Dash Shendy
5 *  Copyright 2015-2016 Thomas Klute
6 *
7 *  Licensed under the Apache License, Version 2.0 (the "License");
8 *  you may not use this file except in compliance with the License.
9 *  You may obtain a copy of the License at
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
13 *  Unless required by applicable law or agreed to in writing, software
14 *  distributed under the License is distributed on an "AS IS" BASIS,
15 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 *  See the License for the specific language governing permissions and
17 *  limitations under the License.
18 */
19
20#include "mod_gnutls.h"
21#include "gnutls_ocsp.h"
22
23#ifdef APLOG_USE_MODULE
24APLOG_USE_MODULE(gnutls);
25#endif
26
27static void gnutls_hooks(apr_pool_t * p __attribute__((unused)))
28{
29    /* Try Run Post-Config Hook After mod_proxy */
30    static const char * const aszPre[] = { "mod_proxy.c", NULL };
31    ap_hook_post_config(mgs_hook_post_config, aszPre, NULL,
32                        APR_HOOK_REALLY_LAST);
33    /* HTTP Scheme Hook */
34    ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
35    /* Default Port Hook */
36    ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE);
37    /* Pre-Connect Hook */
38    ap_hook_pre_connection(mgs_hook_pre_connection, NULL, NULL,
39                           APR_HOOK_MIDDLE);
40    /* Pre-Config Hook */
41    ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL,
42                       APR_HOOK_MIDDLE);
43    /* Child-Init Hook */
44    ap_hook_child_init(mgs_hook_child_init, NULL, NULL,
45                       APR_HOOK_MIDDLE);
46    /* Authentication Hook */
47    ap_hook_access_checker(mgs_hook_authz, NULL, NULL,
48                           APR_HOOK_REALLY_FIRST);
49    /* Fixups Hook */
50    ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST);
51
52    /* TODO: HTTP Upgrade Filter */
53    /* ap_register_output_filter ("UPGRADE_FILTER",
54     *          ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5);
55     */
56
57    /* Input Filter */
58    ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, mgs_filter_input,
59                             NULL, AP_FTYPE_CONNECTION + 5);
60    /* Output Filter */
61    ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, mgs_filter_output,
62                              NULL, AP_FTYPE_CONNECTION + 5);
63
64    /* mod_proxy calls these functions */
65    APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
66    APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
67
68    /* mod_rewrite calls this function to detect HTTPS */
69    APR_REGISTER_OPTIONAL_FN(ssl_is_https);
70}
71
72
73
74/**
75 * mod_rewrite calls this function to fill %{HTTPS}.
76 *
77 * @param c the connection to check
78 * @return non-zero value if HTTPS is in use, zero if not
79 */
80int ssl_is_https(conn_rec *c)
81{
82    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
83        ap_get_module_config(c->base_server->module_config, &gnutls_module);
84    mgs_handle_t *ctxt = (mgs_handle_t *)
85        ap_get_module_config(c->conn_config, &gnutls_module);
86
87    if(sc->enabled == GNUTLS_ENABLED_FALSE
88       || ctxt == NULL
89       || ctxt->enabled == GNUTLS_ENABLED_FALSE)
90    {
91        /* SSL/TLS Disabled or Plain HTTP Connection Detected */
92        return 0;
93    }
94    /* Connection is Using SSL/TLS */
95    return 1;
96}
97
98
99
100int ssl_engine_disable(conn_rec *c)
101{
102    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
103        ap_get_module_config(c->base_server->module_config, &gnutls_module);
104    if(sc->enabled == GNUTLS_ENABLED_FALSE) {
105        return 1;
106    }
107
108    /* disable TLS for this connection */
109    mgs_handle_t *ctxt = (mgs_handle_t *)
110        ap_get_module_config(c->conn_config, &gnutls_module);
111    if (ctxt == NULL)
112    {
113        ctxt = apr_pcalloc(c->pool, sizeof (*ctxt));
114        ap_set_module_config(c->conn_config, &gnutls_module, ctxt);
115    }
116    ctxt->enabled = GNUTLS_ENABLED_FALSE;
117    ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
118
119    if (ctxt->input_filter)
120        ap_remove_input_filter(ctxt->input_filter);
121    if (ctxt->output_filter)
122        ap_remove_output_filter(ctxt->output_filter);
123
124    return 1;
125}
126
127int ssl_proxy_enable(conn_rec *c)
128{
129    /* check if TLS proxy support is enabled */
130    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
131        ap_get_module_config(c->base_server->module_config, &gnutls_module);
132    if (sc->proxy_enabled != GNUTLS_ENABLED_TRUE)
133    {
134        ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c,
135                      "%s: mod_proxy requested TLS proxy, but not enabled "
136                      "for %s", __func__, sc->cert_cn);
137        return 0;
138    }
139
140    /* enable TLS for this connection */
141    mgs_handle_t *ctxt = (mgs_handle_t *)
142        ap_get_module_config(c->conn_config, &gnutls_module);
143    if (ctxt == NULL)
144    {
145        ctxt = apr_pcalloc(c->pool, sizeof (*ctxt));
146        ap_set_module_config(c->conn_config, &gnutls_module, ctxt);
147    }
148    ctxt->enabled = GNUTLS_ENABLED_TRUE;
149    ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
150    return 1;
151}
152
153static const command_rec mgs_config_cmds[] = {
154    AP_INIT_FLAG("GnuTLSProxyEngine", mgs_set_proxy_engine,
155    NULL,
156    RSRC_CONF | OR_AUTHCFG,
157    "Enable TLS Proxy Engine"),
158    AP_INIT_TAKE1("GnuTLSP11Module", mgs_set_p11_module,
159    NULL,
160    RSRC_CONF,
161    "Load this specific PKCS #11 provider library"),
162    AP_INIT_RAW_ARGS("GnuTLSPIN", mgs_set_pin,
163    NULL,
164    RSRC_CONF,
165    "The PIN to use in case of encrypted keys or PKCS #11 tokens."),
166    AP_INIT_RAW_ARGS("GnuTLSSRKPIN", mgs_set_srk_pin,
167    NULL,
168    RSRC_CONF,
169    "The SRK PIN to use in case of TPM keys."),
170    AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify,
171    NULL,
172    RSRC_CONF | OR_AUTHCFG,
173    "Set Verification Requirements of the Client Certificate"),
174    AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method,
175    NULL,
176    RSRC_CONF,
177    "Set Verification Method of the Client Certificate"),
178    AP_INIT_TAKE1("GnuTLSClientCAFile", mgs_set_client_ca_file,
179    NULL,
180    RSRC_CONF,
181    "Set the CA File to verify Client Certificates"),
182    AP_INIT_TAKE1("GnuTLSX509CAFile", mgs_set_client_ca_file,
183    NULL,
184    RSRC_CONF,
185    "Set the CA File to verify Client Certificates"),
186    AP_INIT_TAKE1("GnuTLSPGPKeyringFile", mgs_set_keyring_file,
187    NULL,
188    RSRC_CONF,
189    "Set the Keyring File to verify Client Certificates"),
190    AP_INIT_TAKE1("GnuTLSDHFile", mgs_set_dh_file,
191    NULL,
192    RSRC_CONF,
193    "Set the file to read Diffie Hellman parameters from"),
194    AP_INIT_TAKE1("GnuTLSCertificateFile", mgs_set_cert_file,
195    NULL,
196    RSRC_CONF,
197    "TLS Server X509 Certificate file"),
198    AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file,
199    NULL,
200    RSRC_CONF,
201    "TLS Server X509 Private Key file"),
202    AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file,
203    NULL,
204    RSRC_CONF,
205    "TLS Server X509 Certificate file"),
206    AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file,
207    NULL,
208    RSRC_CONF,
209    "TLS Server X509 Private Key file"),
210    AP_INIT_TAKE1("GnuTLSPGPCertificateFile", mgs_set_pgpcert_file,
211    NULL,
212    RSRC_CONF,
213    "TLS Server PGP Certificate file"),
214    AP_INIT_TAKE1("GnuTLSPGPKeyFile", mgs_set_pgpkey_file,
215    NULL,
216    RSRC_CONF,
217    "TLS Server PGP Private key file"),
218#ifdef ENABLE_SRP
219    AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file,
220    NULL,
221    RSRC_CONF,
222    "TLS Server SRP Password Conf file"),
223    AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile",
224    mgs_set_srp_tpasswd_conf_file,
225    NULL,
226    RSRC_CONF,
227    "TLS Server SRP Parameters file"),
228#endif
229    AP_INIT_TAKE1("GnuTLSCacheTimeout", mgs_set_timeout,
230    NULL,
231    RSRC_CONF,
232    "Cache Timeout"),
233    AP_INIT_TAKE12("GnuTLSCache", mgs_set_cache,
234    NULL,
235    RSRC_CONF,
236    "Cache Configuration"),
237    AP_INIT_FLAG("GnuTLSSessionTickets", mgs_set_tickets,
238    NULL,
239    RSRC_CONF,
240    "Session Tickets Configuration"),
241    AP_INIT_RAW_ARGS("GnuTLSPriorities", mgs_set_priorities,
242    NULL,
243    RSRC_CONF,
244    "The priorities to enable (ciphers, Key exchange, macs, compression)."),
245    AP_INIT_FLAG("GnuTLSEnable", mgs_set_enabled,
246    NULL,
247    RSRC_CONF,
248    "Whether this server has GnuTLS Enabled. Default: Off"),
249    AP_INIT_TAKE1("GnuTLSExportCertificates",
250    mgs_set_export_certificates_size,
251    NULL,
252    RSRC_CONF,
253    "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"),
254    AP_INIT_TAKE1("GnuTLSProxyKeyFile", mgs_store_cred_path,
255    NULL,
256    RSRC_CONF,
257    "X509 client private file for proxy connections"),
258    AP_INIT_TAKE1("GnuTLSProxyCertificateFile", mgs_store_cred_path,
259    NULL,
260    RSRC_CONF,
261    "X509 client certificate file for proxy connections"),
262    AP_INIT_TAKE1("GnuTLSProxyCAFile", mgs_store_cred_path,
263    NULL,
264    RSRC_CONF,
265    "X509 trusted CA file for proxy connections"),
266    AP_INIT_TAKE1("GnuTLSProxyCRLFile", mgs_store_cred_path,
267    NULL,
268    RSRC_CONF,
269    "X509 CRL file for proxy connections"),
270    AP_INIT_RAW_ARGS("GnuTLSProxyPriorities", mgs_set_priorities,
271    NULL,
272    RSRC_CONF,
273    "The priorities to enable for proxy connections (ciphers, key exchange, "
274    "MACs, compression)."),
275    AP_INIT_FLAG("GnuTLSOCSPStapling", mgs_ocsp_stapling_enable,
276                 NULL, RSRC_CONF,
277                 "Enable OCSP stapling"),
278    AP_INIT_FLAG("GnuTLSOCSPCheckNonce", mgs_set_ocsp_check_nonce,
279                 NULL, RSRC_CONF,
280                 "Check nonce in OCSP responses?"),
281    AP_INIT_TAKE1("GnuTLSOCSPResponseFile", mgs_store_ocsp_response_path,
282                  NULL, RSRC_CONF,
283                  "Read OCSP response for stapling from this file instead "
284                  "of sending a request over HTTP (must be updated "
285                  "externally)"),
286    AP_INIT_TAKE1("GnuTLSOCSPCacheTimeout", mgs_set_timeout,
287                  NULL, RSRC_CONF,
288                  "Cache timeout for OCSP responses"),
289    AP_INIT_TAKE1("GnuTLSOCSPFailureTimeout", mgs_set_timeout,
290                  NULL, RSRC_CONF,
291                  "Wait this many seconds before retrying a failed OCSP "
292                  "request"),
293    AP_INIT_TAKE1("GnuTLSOCSPSocketTimeout", mgs_set_timeout,
294                  NULL, RSRC_CONF,
295                  "Socket timeout for OCSP requests"),
296#ifdef __clang__
297    /* Workaround for this clang bug:
298     * https://llvm.org/bugs/show_bug.cgi?id=21689 */
299    {},
300#else
301    { NULL },
302#endif
303};
304
305module AP_MODULE_DECLARE_DATA gnutls_module = {
306    STANDARD20_MODULE_STUFF,
307    .create_dir_config = mgs_config_dir_create,
308    .merge_dir_config = mgs_config_dir_merge,
309    .create_server_config = mgs_config_server_create,
310    .merge_server_config = mgs_config_server_merge,
311    .cmds = mgs_config_cmds,
312    .register_hooks = gnutls_hooks
313};
Note: See TracBrowser for help on using the repository browser.