source: mod_gnutls/src/mod_gnutls.c @ 92b5f4d

asynciodebian/masterproxy-ticket
Last change on this file since 92b5f4d was d036f96, checked in by Fiona Klute <fiona.klute@…>, 3 years ago

Add configuration directive GnuTLSOCSPCache (no-op for now)

First step to a separate cache for OCSP data.

  • Property mode set to 100644
File size: 13.9 KB
RevLine 
[104e881]1/*
[fcb122d]2 *  Copyright 2004-2005 Paul Querna
[e021722]3 *  Copyright 2008, 2014 Nikos Mavrogiannopoulos
[e183628]4 *  Copyright 2011 Dash Shendy
[3c123cd]5 *  Copyright 2015-2018 Fiona 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
[7e2b223]20#include "mod_gnutls.h"
[0470e44]21#include "gnutls_config.h"
[94cb972]22#include "gnutls_ocsp.h"
[235e109]23#include "gnutls_util.h"
[9706fc2]24
[e8acf05]25#ifdef APLOG_USE_MODULE
26APLOG_USE_MODULE(gnutls);
27#endif
[671b64f]28
[23e98b3]29int ssl_engine_set(conn_rec *c,
30                   ap_conf_vector_t *dir_conf __attribute__((unused)),
31                   int proxy, int enable);
32
[4aa63a4]33#define MOD_HTTP2 "mod_http2.c"
[3c475e0]34#define MOD_WATCHDOG "mod_watchdog.c"
[e7cf823]35static const char * const mod_proxy[] = { "mod_proxy.c", NULL };
[4aa63a4]36static const char * const mod_http2[] = { MOD_HTTP2, NULL };
[3c475e0]37static const char * const mod_watchdog[] = { MOD_WATCHDOG, NULL };
[e7cf823]38
[e8acf05]39static void gnutls_hooks(apr_pool_t * p __attribute__((unused)))
40{
[4aa63a4]41    /* Watchdog callbacks must be configured before post_config of
[3c475e0]42     * mod_watchdog runs, or the watchdog won't be started. Similarly,
43     * our child_init hook must run before mod_watchdog's because our
44     * watchdog threads are started there and need some child-specific
45     * resources. */
[4aa63a4]46    static const char * const post_conf_succ[] =
[3c475e0]47        { MOD_HTTP2, MOD_WATCHDOG, NULL };
[4aa63a4]48    ap_hook_post_config(mgs_hook_post_config, mod_proxy, post_conf_succ,
[e7cf823]49                        APR_HOOK_MIDDLE);
[33826c5]50    /* HTTP Scheme Hook */
51    ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE);
52    /* Default Port Hook */
[accbb83]53    ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE);
[33826c5]54    /* Pre-Connect Hook */
[e7cf823]55    ap_hook_pre_connection(mgs_hook_pre_connection, mod_http2, NULL,
[accbb83]56                           APR_HOOK_MIDDLE);
[e7cf823]57    ap_hook_process_connection(mgs_hook_process_connection,
58                               NULL, mod_http2, APR_HOOK_MIDDLE);
[33826c5]59    /* Pre-Config Hook */
[e183628]60    ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL,
[accbb83]61                       APR_HOOK_MIDDLE);
[33826c5]62    /* Child-Init Hook */
[3c475e0]63    ap_hook_child_init(mgs_hook_child_init, NULL, mod_watchdog,
[accbb83]64                       APR_HOOK_MIDDLE);
[33826c5]65    /* Authentication Hook */
[e183628]66    ap_hook_access_checker(mgs_hook_authz, NULL, NULL,
[accbb83]67                           APR_HOOK_REALLY_FIRST);
[33826c5]68    /* Fixups Hook */
[e183628]69    ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST);
[e02dd8c]70
[e183628]71    /* TODO: HTTP Upgrade Filter */
[671b64f]72    /* ap_register_output_filter ("UPGRADE_FILTER",
[e183628]73     *          ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5);
74     */
[e02dd8c]75
[33826c5]76    /* Input Filter */
[accbb83]77    ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, mgs_filter_input,
78                             NULL, AP_FTYPE_CONNECTION + 5);
[33826c5]79    /* Output Filter */
[accbb83]80    ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, mgs_filter_output,
81                              NULL, AP_FTYPE_CONNECTION + 5);
[671b64f]82
[33826c5]83    /* mod_proxy calls these functions */
84    APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
85    APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
[23e98b3]86    APR_REGISTER_OPTIONAL_FN(ssl_engine_set);
[8ac7c0d]87
88    /* mod_rewrite calls this function to detect HTTPS */
89    APR_REGISTER_OPTIONAL_FN(ssl_is_https);
[4cdd4fd]90    /* some modules look up TLS-related variables */
91    APR_REGISTER_OPTIONAL_FN(ssl_var_lookup);
92}
93
94
95
96/**
97 * Get the connection context, resolving to a master connection if
98 * any.
99 *
100 * @param c the connection handle
101 *
102 * @return mod_gnutls session context, might be `NULL`
103 */
104mgs_handle_t* get_effective_gnutls_ctxt(conn_rec *c)
105{
106    mgs_handle_t *ctxt = (mgs_handle_t *)
107        ap_get_module_config(c->conn_config, &gnutls_module);
108    if (!(ctxt != NULL && ctxt->enabled) && (c->master != NULL))
109    {
110        ctxt = (mgs_handle_t *)
111            ap_get_module_config(c->master->conn_config, &gnutls_module);
112    }
113    return ctxt;
[33826c5]114}
115
[8ac7c0d]116
117
[104e881]118/**
119 * mod_rewrite calls this function to fill %{HTTPS}.
120 *
121 * @param c the connection to check
122 * @return non-zero value if HTTPS is in use, zero if not
[8ac7c0d]123 */
[accbb83]124int ssl_is_https(conn_rec *c)
125{
[4cdd4fd]126    mgs_handle_t *ctxt = get_effective_gnutls_ctxt(c);
[671b64f]127    mgs_srvconf_rec *sc = (mgs_srvconf_rec *)
[accbb83]128        ap_get_module_config(c->base_server->module_config, &gnutls_module);
[8ac7c0d]129
130    if(sc->enabled == GNUTLS_ENABLED_FALSE
131       || ctxt == NULL
132       || ctxt->enabled == GNUTLS_ENABLED_FALSE)
133    {
[33826c5]134        /* SSL/TLS Disabled or Plain HTTP Connection Detected */
135        return 0;
136    }
137    /* Connection is Using SSL/TLS */
138    return 1;
139}
140
[8ac7c0d]141
142
[23e98b3]143/**
[4cdd4fd]144 * Return variables describing the current TLS session (if any).
145 *
146 * mod_ssl doc for this function: "This function must remain safe to
147 * use for a non-SSL connection." mod_http2 uses it to check if an
148 * acceptable TLS session is used.
149 */
150char* ssl_var_lookup(apr_pool_t *p, server_rec *s __attribute__((unused)),
151                     conn_rec *c, request_rec *r, char *var)
152{
153    /*
154     * When no pool is given try to find one
155     */
156    if (p == NULL) {
157        if (r != NULL)
158            p = r->pool;
159        else if (c != NULL)
160            p = c->pool;
161        else
162            return NULL;
163    }
164
165    if (strcmp(var, "HTTPS") == 0)
166    {
167        if (c != NULL && ssl_is_https(c))
168            return "on";
169        else
170            return "off";
171    }
172
173    mgs_handle_t *ctxt = get_effective_gnutls_ctxt(c);
174
175    /* TLS parameters are empty if there is no session */
176    if (ctxt == NULL || ctxt->c == NULL)
177        return NULL;
178
179    if (strcmp(var, "SSL_PROTOCOL") == 0)
180        return apr_pstrdup(p, gnutls_protocol_get_name(gnutls_protocol_get_version(ctxt->session)));
181
182    if (strcmp(var, "SSL_CIPHER") == 0)
183        return apr_pstrdup(p, gnutls_cipher_suite_get_name(gnutls_kx_get(ctxt->session),
184                                                           gnutls_cipher_get(ctxt->session),
185                                                           gnutls_mac_get(ctxt->session)));
186
187    /* mod_ssl supports a LOT more variables */
188    ap_log_cerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, c,
189                  "unsupported variable requested: '%s'",
190                  var);
191
192    return NULL;
193}
194
195
196
197/**
[23e98b3]198 * In Apache versions from 2.4.33 mod_proxy uses this function to set
199 * up its client connections. Note that mod_gnutls does not (yet)
200 * implement per directory configuration for such connections.
201 *
202 * @param c the connection
203 * @param dir_conf per directory configuration, unused for now
204 * @param proxy Is this a proxy connection?
205 * @param enable Should TLS be enabled on this connection?
206 *
207 * @param `true` (1) if successful, `false` (0) otherwise
208 */
209int ssl_engine_set(conn_rec *c,
210                   ap_conf_vector_t *dir_conf __attribute__((unused)),
211                   int proxy, int enable)
[e8acf05]212{
[235e109]213    mgs_handle_t *ctxt = init_gnutls_ctxt(c);
[e8acf05]214
[23e98b3]215    /* If TLS proxy has been requested, check if support is enabled
216     * for the server */
217    if (proxy && (ctxt->sc->proxy_enabled != GNUTLS_ENABLED_TRUE))
[07d548d]218    {
219        ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c,
220                      "%s: mod_proxy requested TLS proxy, but not enabled "
[23e98b3]221                      "for %s", __func__, ctxt->sc->cert_cn);
[07d548d]222        return 0;
223    }
224
[23e98b3]225    if (proxy)
226        ctxt->is_proxy = GNUTLS_ENABLED_TRUE;
227    else
228        ctxt->is_proxy = GNUTLS_ENABLED_FALSE;
229
230    if (enable)
231        ctxt->enabled = GNUTLS_ENABLED_TRUE;
232    else
233        ctxt->enabled = GNUTLS_ENABLED_FALSE;
234
[37f8282]235    return 1;
[9706fc2]236}
237
[23e98b3]238int ssl_engine_disable(conn_rec *c)
239{
240    return ssl_engine_set(c, NULL, 0, 0);
241}
242
243int ssl_proxy_enable(conn_rec *c)
244{
245    return ssl_engine_set(c, NULL, 1, 1);
246}
247
[7921dc7]248#define OPENPGP_REMOVED "OpenPGP support has been removed."
249
[46b85d8]250static const command_rec mgs_config_cmds[] = {
[176047e]251    AP_INIT_FLAG("GnuTLSProxyEngine", mgs_set_proxy_engine,
[33826c5]252    NULL,
253    RSRC_CONF | OR_AUTHCFG,
[176047e]254    "Enable TLS Proxy Engine"),
[87f1ed2]255    AP_INIT_TAKE1("GnuTLSP11Module", mgs_set_p11_module,
256    NULL,
257    RSRC_CONF,
[f21d2a6]258    "Load this specific PKCS #11 provider library"),
[031acac]259    AP_INIT_RAW_ARGS("GnuTLSPIN", mgs_set_pin,
260    NULL,
261    RSRC_CONF,
262    "The PIN to use in case of encrypted keys or PKCS #11 tokens."),
263    AP_INIT_RAW_ARGS("GnuTLSSRKPIN", mgs_set_srk_pin,
264    NULL,
265    RSRC_CONF,
266    "The SRK PIN to use in case of TPM keys."),
[e183628]267    AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify,
268    NULL,
269    RSRC_CONF | OR_AUTHCFG,
270    "Set Verification Requirements of the Client Certificate"),
[cf2b905]271    AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method,
272    NULL,
273    RSRC_CONF,
274    "Set Verification Method of the Client Certificate"),
[e183628]275    AP_INIT_TAKE1("GnuTLSClientCAFile", mgs_set_client_ca_file,
276    NULL,
277    RSRC_CONF,
278    "Set the CA File to verify Client Certificates"),
279    AP_INIT_TAKE1("GnuTLSX509CAFile", mgs_set_client_ca_file,
280    NULL,
281    RSRC_CONF,
282    "Set the CA File to verify Client Certificates"),
283    AP_INIT_TAKE1("GnuTLSDHFile", mgs_set_dh_file,
284    NULL,
285    RSRC_CONF,
286    "Set the file to read Diffie Hellman parameters from"),
287    AP_INIT_TAKE1("GnuTLSCertificateFile", mgs_set_cert_file,
288    NULL,
289    RSRC_CONF,
[88df24d]290    "TLS Server X509 Certificate file"),
[e183628]291    AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file,
292    NULL,
293    RSRC_CONF,
[88df24d]294    "TLS Server X509 Private Key file"),
[e183628]295    AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file,
296    NULL,
297    RSRC_CONF,
[88df24d]298    "TLS Server X509 Certificate file"),
[e183628]299    AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file,
300    NULL,
301    RSRC_CONF,
[88df24d]302    "TLS Server X509 Private Key file"),
[787dab7]303#ifdef ENABLE_SRP
[e183628]304    AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file,
305    NULL,
306    RSRC_CONF,
[88df24d]307    "TLS Server SRP Password Conf file"),
[e183628]308    AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile",
309    mgs_set_srp_tpasswd_conf_file,
310    NULL,
311    RSRC_CONF,
[88df24d]312    "TLS Server SRP Parameters file"),
[787dab7]313#endif
[70a1e5a]314    AP_INIT_TAKE1("GnuTLSCacheTimeout", mgs_set_timeout,
[e183628]315    NULL,
316    RSRC_CONF,
317    "Cache Timeout"),
318    AP_INIT_TAKE12("GnuTLSCache", mgs_set_cache,
319    NULL,
320    RSRC_CONF,
[d036f96]321    "Session Cache Configuration"),
[176047e]322    AP_INIT_FLAG("GnuTLSSessionTickets", mgs_set_tickets,
[e183628]323    NULL,
324    RSRC_CONF,
325    "Session Tickets Configuration"),
326    AP_INIT_RAW_ARGS("GnuTLSPriorities", mgs_set_priorities,
327    NULL,
328    RSRC_CONF,
329    "The priorities to enable (ciphers, Key exchange, macs, compression)."),
[176047e]330    AP_INIT_FLAG("GnuTLSEnable", mgs_set_enabled,
[e183628]331    NULL,
332    RSRC_CONF,
333    "Whether this server has GnuTLS Enabled. Default: Off"),
[7d1ab49]334    AP_INIT_TAKE1("GnuTLSExportCertificates",
[2aaf4f5]335    mgs_set_export_certificates_size,
[7d1ab49]336    NULL,
337    RSRC_CONF,
[2aaf4f5]338    "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"),
[0de1839]339    AP_INIT_TAKE1("GnuTLSProxyKeyFile", mgs_store_cred_path,
340    NULL,
341    RSRC_CONF,
342    "X509 client private file for proxy connections"),
343    AP_INIT_TAKE1("GnuTLSProxyCertificateFile", mgs_store_cred_path,
344    NULL,
345    RSRC_CONF,
346    "X509 client certificate file for proxy connections"),
347    AP_INIT_TAKE1("GnuTLSProxyCAFile", mgs_store_cred_path,
348    NULL,
349    RSRC_CONF,
350    "X509 trusted CA file for proxy connections"),
[809c422]351    AP_INIT_TAKE1("GnuTLSProxyCRLFile", mgs_store_cred_path,
352    NULL,
353    RSRC_CONF,
354    "X509 CRL file for proxy connections"),
[f030883]355    AP_INIT_RAW_ARGS("GnuTLSProxyPriorities", mgs_set_priorities,
356    NULL,
357    RSRC_CONF,
358    "The priorities to enable for proxy connections (ciphers, key exchange, "
359    "MACs, compression)."),
[4d4a406]360    AP_INIT_FLAG("GnuTLSOCSPStapling", mgs_ocsp_stapling_enable,
[78b75b3]361                 NULL, RSRC_CONF,
[3475e62]362                 "Enable OCSP stapling"),
[2246a84]363    AP_INIT_FLAG("GnuTLSOCSPAutoRefresh", mgs_set_ocsp_auto_refresh,
364                 NULL, RSRC_CONF,
365                 "Regularly refresh cached OCSP response independent "
366                 "of TLS handshakes?"),
[d036f96]367    AP_INIT_TAKE12("GnuTLSOCSPCache", mgs_set_cache,
368                   NULL,
369                   RSRC_CONF,
370                  "OCSP Cache Configuration"),
[b888e8b]371    AP_INIT_FLAG("GnuTLSOCSPCheckNonce", mgs_set_ocsp_check_nonce,
372                 NULL, RSRC_CONF,
373                 "Check nonce in OCSP responses?"),
[94cb972]374    AP_INIT_TAKE1("GnuTLSOCSPResponseFile", mgs_store_ocsp_response_path,
[78b75b3]375                  NULL, RSRC_CONF,
[3475e62]376                  "Read OCSP response for stapling from this file instead "
377                  "of sending a request over HTTP (must be updated "
378                  "externally)"),
[e1c094c]379    AP_INIT_TAKE1("GnuTLSOCSPCacheTimeout", mgs_set_timeout,
[78b75b3]380                  NULL, RSRC_CONF,
[e1c094c]381                  "Cache timeout for OCSP responses"),
[c6dda6d]382    AP_INIT_TAKE1("GnuTLSOCSPFailureTimeout", mgs_set_timeout,
383                  NULL, RSRC_CONF,
[3475e62]384                  "Wait this many seconds before retrying a failed OCSP "
385                  "request"),
[2246a84]386    AP_INIT_TAKE1("GnuTLSOCSPFuzzTime", mgs_set_timeout,
387                  NULL, RSRC_CONF,
388                  "Update cached OCSP response up to this many seconds "
389                  "before it expires, if GnuTLSOCSPAutoRefresh is enabled."),
[333bbc7]390    AP_INIT_TAKE1("GnuTLSOCSPSocketTimeout", mgs_set_timeout,
391                  NULL, RSRC_CONF,
[3475e62]392                  "Socket timeout for OCSP requests"),
[7921dc7]393    AP_INIT_RAW_ARGS("GnuTLSPGPKeyringFile",
394                     ap_set_deprecated, NULL, OR_ALL, OPENPGP_REMOVED),
395    AP_INIT_RAW_ARGS("GnuTLSPGPCertificateFile",
396                     ap_set_deprecated, NULL, OR_ALL, OPENPGP_REMOVED),
397    AP_INIT_RAW_ARGS("GnuTLSPGPKeyFile",
398                     ap_set_deprecated, NULL, OR_ALL, OPENPGP_REMOVED),
[15b22cb]399#ifdef __clang__
400    /* Workaround for this clang bug:
401     * https://llvm.org/bugs/show_bug.cgi?id=21689 */
402    {},
403#else
[7d1ab49]404    { NULL },
[15b22cb]405#endif
[46b85d8]406};
[9706fc2]407
408module AP_MODULE_DECLARE_DATA gnutls_module = {
[e183628]409    STANDARD20_MODULE_STUFF,
[2d0f6cf]410    .create_dir_config = mgs_config_dir_create,
411    .merge_dir_config = mgs_config_dir_merge,
412    .create_server_config = mgs_config_server_create,
[040387c]413    .merge_server_config = mgs_config_server_merge,
[2d0f6cf]414    .cmds = mgs_config_cmds,
415    .register_hooks = gnutls_hooks
[9706fc2]416};
Note: See TracBrowser for help on using the repository browser.