source: mod_gnutls/src/mod_gnutls.c @ 8ac7c0d

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

Register "ssl_is_https" function for compatibility with mod_rewrite

mod_rewrite calls this function to fill its %{HTTPS} special variable,
and not providing it meant that conditions like

RewriteCond? "%{HTTPS}" "off"

would match HTTPS connections using mod_gnutls. When used to redirect
clients from HTTP to HTTPS connections, this could lead to redirection
loops as reported in Debian bug #514005 [1]. In addition to
registering the function this commit also adds a test chase that
checks if an HTTP to HTTPS redirection works.

[1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=514005

  • Property mode set to 100644
File size: 9.3 KB
Line 
1/**
2 *  Copyright 2004-2005 Paul Querna
3 *  Copyright 2008, 2014 Nikos Mavrogiannopoulos
4 *  Copyright 2011 Dash Shendy
5 *  Copyright 2015 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
21#include "mod_gnutls.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#if USING_2_1_RECENT
35    ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
36#else
37    ap_hook_http_method(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
38#endif
39    /* Default Port Hook */
40    ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE);
41    /* Pre-Connect Hook */
42    ap_hook_pre_connection(mgs_hook_pre_connection, NULL, NULL,
43                           APR_HOOK_MIDDLE);
44    /* Pre-Config Hook */
45    ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL,
46                       APR_HOOK_MIDDLE);
47    /* Child-Init Hook */
48    ap_hook_child_init(mgs_hook_child_init, NULL, NULL,
49                       APR_HOOK_MIDDLE);
50    /* Authentication Hook */
51    ap_hook_access_checker(mgs_hook_authz, NULL, NULL,
52                           APR_HOOK_REALLY_FIRST);
53    /* Fixups Hook */
54    ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST);
55
56    /* TODO: HTTP Upgrade Filter */
57    /* ap_register_output_filter ("UPGRADE_FILTER",
58     *          ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5);
59     */
60
61    /* Input Filter */
62    ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, mgs_filter_input,
63                             NULL, AP_FTYPE_CONNECTION + 5);
64    /* Output Filter */
65    ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, mgs_filter_output,
66                              NULL, AP_FTYPE_CONNECTION + 5);
67
68    /* mod_proxy calls these functions */
69    APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
70    APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
71
72    /* mod_rewrite calls this function to detect HTTPS */
73    APR_REGISTER_OPTIONAL_FN(ssl_is_https);
74}
75
76
77
78/*
79 * mod_rewrite calls this function to fill %{HTTPS}. A non-zero return
80 * value means that HTTPS is in use.
81 */
82int ssl_is_https(conn_rec *c)
83{
84    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
85        ap_get_module_config(c->base_server->module_config, &gnutls_module);
86    mgs_handle_t *ctxt = (mgs_handle_t *)
87        ap_get_module_config(c->conn_config, &gnutls_module);
88
89    if(sc->enabled == GNUTLS_ENABLED_FALSE
90       || ctxt == NULL
91       || ctxt->enabled == GNUTLS_ENABLED_FALSE)
92    {
93        /* SSL/TLS Disabled or Plain HTTP Connection Detected */
94        return 0;
95    }
96    /* Connection is Using SSL/TLS */
97    return 1;
98}
99
100
101
102int ssl_engine_disable(conn_rec *c)
103{
104    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
105        ap_get_module_config(c->base_server->module_config, &gnutls_module);
106    if(sc->enabled == GNUTLS_ENABLED_FALSE) {
107        return 1;
108    }
109
110    /* disable TLS for this connection */
111    mgs_handle_t *ctxt = (mgs_handle_t *)
112        ap_get_module_config(c->conn_config, &gnutls_module);
113    if (ctxt == NULL)
114    {
115        ctxt = apr_pcalloc(c->pool, sizeof (*ctxt));
116        ap_set_module_config(c->conn_config, &gnutls_module, ctxt);
117    }
118    ctxt->enabled = GNUTLS_ENABLED_FALSE;
119    ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
120
121    if (c->input_filters)
122        ap_remove_input_filter(c->input_filters);
123    if (c->output_filters)
124        ap_remove_output_filter(c->output_filters);
125
126    return 1;
127}
128
129int ssl_proxy_enable(conn_rec *c)
130{
131    /* check if TLS proxy support is enabled */
132    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
133        ap_get_module_config(c->base_server->module_config, &gnutls_module);
134    if (sc->proxy_enabled != GNUTLS_ENABLED_TRUE)
135    {
136        ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c,
137                      "%s: mod_proxy requested TLS proxy, but not enabled "
138                      "for %s", __func__, sc->cert_cn);
139        return 0;
140    }
141
142    /* enable TLS for this connection */
143    mgs_handle_t *ctxt = (mgs_handle_t *)
144        ap_get_module_config(c->conn_config, &gnutls_module);
145    if (ctxt == NULL)
146    {
147        ctxt = apr_pcalloc(c->pool, sizeof (*ctxt));
148        ap_set_module_config(c->conn_config, &gnutls_module, ctxt);
149    }
150    ctxt->enabled = GNUTLS_ENABLED_TRUE;
151    ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
152    return 1;
153}
154
155static const command_rec mgs_config_cmds[] = {
156    AP_INIT_FLAG("GnuTLSProxyEngine", mgs_set_proxy_engine,
157    NULL,
158    RSRC_CONF | OR_AUTHCFG,
159    "Enable TLS Proxy Engine"),
160    AP_INIT_TAKE1("GnuTLSP11Module", mgs_set_p11_module,
161    NULL,
162    RSRC_CONF,
163    "Load this specific PKCS #11 provider library"),
164    AP_INIT_RAW_ARGS("GnuTLSPIN", mgs_set_pin,
165    NULL,
166    RSRC_CONF,
167    "The PIN to use in case of encrypted keys or PKCS #11 tokens."),
168    AP_INIT_RAW_ARGS("GnuTLSSRKPIN", mgs_set_srk_pin,
169    NULL,
170    RSRC_CONF,
171    "The SRK PIN to use in case of TPM keys."),
172    AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify,
173    NULL,
174    RSRC_CONF | OR_AUTHCFG,
175    "Set Verification Requirements of the Client Certificate"),
176    AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method,
177    NULL,
178    RSRC_CONF,
179    "Set Verification Method of the Client Certificate"),
180    AP_INIT_TAKE1("GnuTLSClientCAFile", mgs_set_client_ca_file,
181    NULL,
182    RSRC_CONF,
183    "Set the CA File to verify Client Certificates"),
184    AP_INIT_TAKE1("GnuTLSX509CAFile", mgs_set_client_ca_file,
185    NULL,
186    RSRC_CONF,
187    "Set the CA File to verify Client Certificates"),
188    AP_INIT_TAKE1("GnuTLSPGPKeyringFile", mgs_set_keyring_file,
189    NULL,
190    RSRC_CONF,
191    "Set the Keyring File to verify Client Certificates"),
192    AP_INIT_TAKE1("GnuTLSDHFile", mgs_set_dh_file,
193    NULL,
194    RSRC_CONF,
195    "Set the file to read Diffie Hellman parameters from"),
196    AP_INIT_TAKE1("GnuTLSCertificateFile", mgs_set_cert_file,
197    NULL,
198    RSRC_CONF,
199    "TLS Server X509 Certificate file"),
200    AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file,
201    NULL,
202    RSRC_CONF,
203    "TLS Server X509 Private Key file"),
204    AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file,
205    NULL,
206    RSRC_CONF,
207    "TLS Server X509 Certificate file"),
208    AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file,
209    NULL,
210    RSRC_CONF,
211    "TLS Server X509 Private Key file"),
212    AP_INIT_TAKE1("GnuTLSPGPCertificateFile", mgs_set_pgpcert_file,
213    NULL,
214    RSRC_CONF,
215    "TLS Server PGP Certificate file"),
216    AP_INIT_TAKE1("GnuTLSPGPKeyFile", mgs_set_pgpkey_file,
217    NULL,
218    RSRC_CONF,
219    "TLS Server PGP Private key file"),
220#ifdef ENABLE_SRP
221    AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file,
222    NULL,
223    RSRC_CONF,
224    "TLS Server SRP Password Conf file"),
225    AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile",
226    mgs_set_srp_tpasswd_conf_file,
227    NULL,
228    RSRC_CONF,
229    "TLS Server SRP Parameters file"),
230#endif
231    AP_INIT_TAKE1("GnuTLSCacheTimeout", mgs_set_cache_timeout,
232    NULL,
233    RSRC_CONF,
234    "Cache Timeout"),
235    AP_INIT_TAKE12("GnuTLSCache", mgs_set_cache,
236    NULL,
237    RSRC_CONF,
238    "Cache Configuration"),
239    AP_INIT_FLAG("GnuTLSSessionTickets", mgs_set_tickets,
240    NULL,
241    RSRC_CONF,
242    "Session Tickets Configuration"),
243    AP_INIT_RAW_ARGS("GnuTLSPriorities", mgs_set_priorities,
244    NULL,
245    RSRC_CONF,
246    "The priorities to enable (ciphers, Key exchange, macs, compression)."),
247    AP_INIT_FLAG("GnuTLSEnable", mgs_set_enabled,
248    NULL,
249    RSRC_CONF,
250    "Whether this server has GnuTLS Enabled. Default: Off"),
251    AP_INIT_TAKE1("GnuTLSExportCertificates",
252    mgs_set_export_certificates_size,
253    NULL,
254    RSRC_CONF,
255    "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"),
256    AP_INIT_TAKE1("GnuTLSProxyKeyFile", mgs_store_cred_path,
257    NULL,
258    RSRC_CONF,
259    "X509 client private file for proxy connections"),
260    AP_INIT_TAKE1("GnuTLSProxyCertificateFile", mgs_store_cred_path,
261    NULL,
262    RSRC_CONF,
263    "X509 client certificate file for proxy connections"),
264    AP_INIT_TAKE1("GnuTLSProxyCAFile", mgs_store_cred_path,
265    NULL,
266    RSRC_CONF,
267    "X509 trusted CA file for proxy connections"),
268    AP_INIT_TAKE1("GnuTLSProxyCRLFile", mgs_store_cred_path,
269    NULL,
270    RSRC_CONF,
271    "X509 CRL file for proxy connections"),
272    AP_INIT_RAW_ARGS("GnuTLSProxyPriorities", mgs_set_priorities,
273    NULL,
274    RSRC_CONF,
275    "The priorities to enable for proxy connections (ciphers, key exchange, "
276    "MACs, compression)."),
277    { NULL },
278};
279
280module AP_MODULE_DECLARE_DATA gnutls_module = {
281    STANDARD20_MODULE_STUFF,
282    .create_dir_config = mgs_config_dir_create,
283    .merge_dir_config = mgs_config_dir_merge,
284    .create_server_config = mgs_config_server_create,
285    .merge_server_config = mgs_config_server_merge,
286    .cmds = mgs_config_cmds,
287    .register_hooks = gnutls_hooks
288};
Note: See TracBrowser for help on using the repository browser.