source: mod_gnutls/src/mod_gnutls.c @ 4cdd4fd

asynciodebian/masterdebian/stretch-backportsproxy-ticketupstream
Last change on this file since 4cdd4fd was 4cdd4fd, checked in by Fiona Klute <fiona.klute@…>, 3 years ago

Implement ssl_var_lookup function (subset of mod_ssl implementation)

mod_http2 uses the ssl_var_lookup function to check if the TLS
connection is acceptable. The implementation added here provides the
subset used there, not all varibles provided by mod_ssl.

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