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

debian/masterdebian/stretch-backportsjessie-backportsproxy-ticketupstream
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
RevLine 
[fcb122d]1/**
2 *  Copyright 2004-2005 Paul Querna
[e021722]3 *  Copyright 2008, 2014 Nikos Mavrogiannopoulos
[e183628]4 *  Copyright 2011 Dash Shendy
[e391197]5 *  Copyright 2015 Thomas Klute
[9706fc2]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
[7e2b223]21#include "mod_gnutls.h"
[9706fc2]22
[e8acf05]23#ifdef APLOG_USE_MODULE
24APLOG_USE_MODULE(gnutls);
25#endif
[671b64f]26
[e8acf05]27static void gnutls_hooks(apr_pool_t * p __attribute__((unused)))
28{
[33826c5]29    /* Try Run Post-Config Hook After mod_proxy */
30    static const char * const aszPre[] = { "mod_proxy.c", NULL };
[accbb83]31    ap_hook_post_config(mgs_hook_post_config, aszPre, NULL,
32                        APR_HOOK_REALLY_LAST);
[33826c5]33    /* HTTP Scheme Hook */
[482f47f]34#if USING_2_1_RECENT
[33826c5]35    ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
[482f47f]36#else
[33826c5]37    ap_hook_http_method(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
[482f47f]38#endif
[33826c5]39    /* Default Port Hook */
[accbb83]40    ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE);
[33826c5]41    /* Pre-Connect Hook */
[accbb83]42    ap_hook_pre_connection(mgs_hook_pre_connection, NULL, NULL,
43                           APR_HOOK_MIDDLE);
[33826c5]44    /* Pre-Config Hook */
[e183628]45    ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL,
[accbb83]46                       APR_HOOK_MIDDLE);
[33826c5]47    /* Child-Init Hook */
48    ap_hook_child_init(mgs_hook_child_init, NULL, NULL,
[accbb83]49                       APR_HOOK_MIDDLE);
[33826c5]50    /* Authentication Hook */
[e183628]51    ap_hook_access_checker(mgs_hook_authz, NULL, NULL,
[accbb83]52                           APR_HOOK_REALLY_FIRST);
[33826c5]53    /* Fixups Hook */
[e183628]54    ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST);
[e02dd8c]55
[e183628]56    /* TODO: HTTP Upgrade Filter */
[671b64f]57    /* ap_register_output_filter ("UPGRADE_FILTER",
[e183628]58     *          ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5);
59     */
[e02dd8c]60
[33826c5]61    /* Input Filter */
[accbb83]62    ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, mgs_filter_input,
63                             NULL, AP_FTYPE_CONNECTION + 5);
[33826c5]64    /* Output Filter */
[accbb83]65    ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, mgs_filter_output,
66                              NULL, AP_FTYPE_CONNECTION + 5);
[671b64f]67
[33826c5]68    /* mod_proxy calls these functions */
69    APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
70    APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
[8ac7c0d]71
72    /* mod_rewrite calls this function to detect HTTPS */
73    APR_REGISTER_OPTIONAL_FN(ssl_is_https);
[33826c5]74}
75
[8ac7c0d]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 */
[accbb83]82int ssl_is_https(conn_rec *c)
83{
[671b64f]84    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
[accbb83]85        ap_get_module_config(c->base_server->module_config, &gnutls_module);
[8ac7c0d]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    {
[33826c5]93        /* SSL/TLS Disabled or Plain HTTP Connection Detected */
94        return 0;
95    }
96    /* Connection is Using SSL/TLS */
97    return 1;
98}
99
[8ac7c0d]100
101
[e8acf05]102int ssl_engine_disable(conn_rec *c)
103{
[671b64f]104    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
[e8acf05]105        ap_get_module_config(c->base_server->module_config, &gnutls_module);
[33826c5]106    if(sc->enabled == GNUTLS_ENABLED_FALSE) {
107        return 1;
[671b64f]108    }
[e8acf05]109
110    /* disable TLS for this connection */
[accbb83]111    mgs_handle_t *ctxt = (mgs_handle_t *)
112        ap_get_module_config(c->conn_config, &gnutls_module);
[e8acf05]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;
[c1ef069]119    ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
[e8acf05]120
[3d361b8]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);
[c782c1f]125
[33826c5]126    return 1;
127}
128
[accbb83]129int ssl_proxy_enable(conn_rec *c)
130{
[07d548d]131    /* check if TLS proxy support is enabled */
[671b64f]132    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
[accbb83]133        ap_get_module_config(c->base_server->module_config, &gnutls_module);
[07d548d]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;
[c1ef069]151    ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
[37f8282]152    return 1;
[9706fc2]153}
154
[46b85d8]155static const command_rec mgs_config_cmds[] = {
[176047e]156    AP_INIT_FLAG("GnuTLSProxyEngine", mgs_set_proxy_engine,
[33826c5]157    NULL,
158    RSRC_CONF | OR_AUTHCFG,
[176047e]159    "Enable TLS Proxy Engine"),
[87f1ed2]160    AP_INIT_TAKE1("GnuTLSP11Module", mgs_set_p11_module,
161    NULL,
162    RSRC_CONF,
[f21d2a6]163    "Load this specific PKCS #11 provider library"),
[031acac]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."),
[e183628]172    AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify,
173    NULL,
174    RSRC_CONF | OR_AUTHCFG,
175    "Set Verification Requirements of the Client Certificate"),
[cf2b905]176    AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method,
177    NULL,
178    RSRC_CONF,
179    "Set Verification Method of the Client Certificate"),
[e183628]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,
[88df24d]199    "TLS Server X509 Certificate file"),
[e183628]200    AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file,
201    NULL,
202    RSRC_CONF,
[88df24d]203    "TLS Server X509 Private Key file"),
[e183628]204    AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file,
205    NULL,
206    RSRC_CONF,
[88df24d]207    "TLS Server X509 Certificate file"),
[e183628]208    AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file,
209    NULL,
210    RSRC_CONF,
[88df24d]211    "TLS Server X509 Private Key file"),
[e183628]212    AP_INIT_TAKE1("GnuTLSPGPCertificateFile", mgs_set_pgpcert_file,
213    NULL,
214    RSRC_CONF,
[88df24d]215    "TLS Server PGP Certificate file"),
[e183628]216    AP_INIT_TAKE1("GnuTLSPGPKeyFile", mgs_set_pgpkey_file,
217    NULL,
218    RSRC_CONF,
[88df24d]219    "TLS Server PGP Private key file"),
[787dab7]220#ifdef ENABLE_SRP
[e183628]221    AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file,
222    NULL,
223    RSRC_CONF,
[88df24d]224    "TLS Server SRP Password Conf file"),
[e183628]225    AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile",
226    mgs_set_srp_tpasswd_conf_file,
227    NULL,
228    RSRC_CONF,
[88df24d]229    "TLS Server SRP Parameters file"),
[787dab7]230#endif
[e183628]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"),
[176047e]239    AP_INIT_FLAG("GnuTLSSessionTickets", mgs_set_tickets,
[e183628]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)."),
[176047e]247    AP_INIT_FLAG("GnuTLSEnable", mgs_set_enabled,
[e183628]248    NULL,
249    RSRC_CONF,
250    "Whether this server has GnuTLS Enabled. Default: Off"),
[7d1ab49]251    AP_INIT_TAKE1("GnuTLSExportCertificates",
[2aaf4f5]252    mgs_set_export_certificates_size,
[7d1ab49]253    NULL,
254    RSRC_CONF,
[2aaf4f5]255    "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"),
[0de1839]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"),
[809c422]268    AP_INIT_TAKE1("GnuTLSProxyCRLFile", mgs_store_cred_path,
269    NULL,
270    RSRC_CONF,
271    "X509 CRL file for proxy connections"),
[f030883]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)."),
[7d1ab49]277    { NULL },
[46b85d8]278};
[9706fc2]279
280module AP_MODULE_DECLARE_DATA gnutls_module = {
[e183628]281    STANDARD20_MODULE_STUFF,
[2d0f6cf]282    .create_dir_config = mgs_config_dir_create,
283    .merge_dir_config = mgs_config_dir_merge,
284    .create_server_config = mgs_config_server_create,
[040387c]285    .merge_server_config = mgs_config_server_merge,
[2d0f6cf]286    .cmds = mgs_config_cmds,
287    .register_hooks = gnutls_hooks
[9706fc2]288};
Note: See TracBrowser for help on using the repository browser.