source: mod_gnutls/src/mod_gnutls.c @ 235e109

debian/masterdebian/stretch-backportsupstream
Last change on this file since 235e109 was 235e109, checked in by Fiona Klute <fiona.klute@…>, 20 months ago

Unify initialization of mod_gnutls connection context

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