source: mod_gnutls/src/mod_gnutls.c @ 64856fd

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

Minimal OCSP stapling implementation using externally provided response

Works if the configured file contains a valid and current OCSP
response. Note that the module does not yet check those conditions,
the file is just read and forwarded to GnuTLS.

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