[fcb122d] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[e183628] | 3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
| 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 |
---|
| 24 | APLOG_USE_MODULE(gnutls); |
---|
| 25 | #endif |
---|
[671b64f] | 26 | |
---|
[e8acf05] | 27 | static 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); |
---|
| 71 | } |
---|
| 72 | |
---|
[accbb83] | 73 | int ssl_is_https(conn_rec *c) |
---|
| 74 | { |
---|
[671b64f] | 75 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
[accbb83] | 76 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
[37f8282] | 77 | if(sc->enabled == 0 || sc->non_ssl_request == 1) { |
---|
[33826c5] | 78 | /* SSL/TLS Disabled or Plain HTTP Connection Detected */ |
---|
| 79 | return 0; |
---|
| 80 | } |
---|
| 81 | /* Connection is Using SSL/TLS */ |
---|
| 82 | return 1; |
---|
| 83 | } |
---|
| 84 | |
---|
[e8acf05] | 85 | int ssl_engine_disable(conn_rec *c) |
---|
| 86 | { |
---|
[671b64f] | 87 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
[e8acf05] | 88 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
[33826c5] | 89 | if(sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
| 90 | return 1; |
---|
[671b64f] | 91 | } |
---|
[e8acf05] | 92 | |
---|
| 93 | /* disable TLS for this connection */ |
---|
[accbb83] | 94 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
| 95 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
[e8acf05] | 96 | if (ctxt == NULL) |
---|
| 97 | { |
---|
| 98 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
| 99 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
| 100 | } |
---|
| 101 | ctxt->enabled = GNUTLS_ENABLED_FALSE; |
---|
[c1ef069] | 102 | ctxt->is_proxy = GNUTLS_ENABLED_TRUE; |
---|
[e8acf05] | 103 | |
---|
[3d361b8] | 104 | if (c->input_filters) |
---|
| 105 | ap_remove_input_filter(c->input_filters); |
---|
| 106 | if (c->output_filters) |
---|
| 107 | ap_remove_output_filter(c->output_filters); |
---|
[c782c1f] | 108 | |
---|
[33826c5] | 109 | return 1; |
---|
| 110 | } |
---|
| 111 | |
---|
[accbb83] | 112 | int ssl_proxy_enable(conn_rec *c) |
---|
| 113 | { |
---|
[07d548d] | 114 | /* check if TLS proxy support is enabled */ |
---|
[671b64f] | 115 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
[accbb83] | 116 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
[07d548d] | 117 | if (sc->proxy_enabled != GNUTLS_ENABLED_TRUE) |
---|
| 118 | { |
---|
| 119 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, |
---|
| 120 | "%s: mod_proxy requested TLS proxy, but not enabled " |
---|
| 121 | "for %s", __func__, sc->cert_cn); |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /* enable TLS for this connection */ |
---|
| 126 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
| 127 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
| 128 | if (ctxt == NULL) |
---|
| 129 | { |
---|
| 130 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
| 131 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
| 132 | } |
---|
| 133 | ctxt->enabled = GNUTLS_ENABLED_TRUE; |
---|
[c1ef069] | 134 | ctxt->is_proxy = GNUTLS_ENABLED_TRUE; |
---|
[37f8282] | 135 | return 1; |
---|
[9706fc2] | 136 | } |
---|
| 137 | |
---|
[46b85d8] | 138 | static const command_rec mgs_config_cmds[] = { |
---|
[a2e3c33] | 139 | AP_INIT_TAKE1("GnuTLSProxyEngine", mgs_set_proxy_engine, |
---|
[33826c5] | 140 | NULL, |
---|
| 141 | RSRC_CONF | OR_AUTHCFG, |
---|
[37f8282] | 142 | "Enable SSL Proxy Engine"), |
---|
[87f1ed2] | 143 | AP_INIT_TAKE1("GnuTLSP11Module", mgs_set_p11_module, |
---|
| 144 | NULL, |
---|
| 145 | RSRC_CONF, |
---|
| 146 | "Load this additional PKCS #11 provider library"), |
---|
[031acac] | 147 | AP_INIT_RAW_ARGS("GnuTLSPIN", mgs_set_pin, |
---|
| 148 | NULL, |
---|
| 149 | RSRC_CONF, |
---|
| 150 | "The PIN to use in case of encrypted keys or PKCS #11 tokens."), |
---|
| 151 | AP_INIT_RAW_ARGS("GnuTLSSRKPIN", mgs_set_srk_pin, |
---|
| 152 | NULL, |
---|
| 153 | RSRC_CONF, |
---|
| 154 | "The SRK PIN to use in case of TPM keys."), |
---|
[e183628] | 155 | AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify, |
---|
| 156 | NULL, |
---|
| 157 | RSRC_CONF | OR_AUTHCFG, |
---|
| 158 | "Set Verification Requirements of the Client Certificate"), |
---|
[cf2b905] | 159 | AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method, |
---|
| 160 | NULL, |
---|
| 161 | RSRC_CONF, |
---|
| 162 | "Set Verification Method of the Client Certificate"), |
---|
[e183628] | 163 | AP_INIT_TAKE1("GnuTLSClientCAFile", mgs_set_client_ca_file, |
---|
| 164 | NULL, |
---|
| 165 | RSRC_CONF, |
---|
| 166 | "Set the CA File to verify Client Certificates"), |
---|
| 167 | AP_INIT_TAKE1("GnuTLSX509CAFile", mgs_set_client_ca_file, |
---|
| 168 | NULL, |
---|
| 169 | RSRC_CONF, |
---|
| 170 | "Set the CA File to verify Client Certificates"), |
---|
| 171 | AP_INIT_TAKE1("GnuTLSPGPKeyringFile", mgs_set_keyring_file, |
---|
| 172 | NULL, |
---|
| 173 | RSRC_CONF, |
---|
| 174 | "Set the Keyring File to verify Client Certificates"), |
---|
| 175 | AP_INIT_TAKE1("GnuTLSDHFile", mgs_set_dh_file, |
---|
| 176 | NULL, |
---|
| 177 | RSRC_CONF, |
---|
| 178 | "Set the file to read Diffie Hellman parameters from"), |
---|
| 179 | AP_INIT_TAKE1("GnuTLSCertificateFile", mgs_set_cert_file, |
---|
| 180 | NULL, |
---|
| 181 | RSRC_CONF, |
---|
| 182 | "SSL Server X509 Certificate file"), |
---|
| 183 | AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file, |
---|
| 184 | NULL, |
---|
| 185 | RSRC_CONF, |
---|
| 186 | "SSL Server X509 Private Key file"), |
---|
| 187 | AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file, |
---|
| 188 | NULL, |
---|
| 189 | RSRC_CONF, |
---|
| 190 | "SSL Server X509 Certificate file"), |
---|
| 191 | AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file, |
---|
| 192 | NULL, |
---|
| 193 | RSRC_CONF, |
---|
| 194 | "SSL Server X509 Private Key file"), |
---|
| 195 | AP_INIT_TAKE1("GnuTLSPGPCertificateFile", mgs_set_pgpcert_file, |
---|
| 196 | NULL, |
---|
| 197 | RSRC_CONF, |
---|
| 198 | "SSL Server PGP Certificate file"), |
---|
| 199 | AP_INIT_TAKE1("GnuTLSPGPKeyFile", mgs_set_pgpkey_file, |
---|
| 200 | NULL, |
---|
| 201 | RSRC_CONF, |
---|
| 202 | "SSL Server PGP Private key file"), |
---|
[787dab7] | 203 | #ifdef ENABLE_SRP |
---|
[e183628] | 204 | AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file, |
---|
| 205 | NULL, |
---|
| 206 | RSRC_CONF, |
---|
| 207 | "SSL Server SRP Password Conf file"), |
---|
| 208 | AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile", |
---|
| 209 | mgs_set_srp_tpasswd_conf_file, |
---|
| 210 | NULL, |
---|
| 211 | RSRC_CONF, |
---|
| 212 | "SSL Server SRP Parameters file"), |
---|
[787dab7] | 213 | #endif |
---|
[e183628] | 214 | AP_INIT_TAKE1("GnuTLSCacheTimeout", mgs_set_cache_timeout, |
---|
| 215 | NULL, |
---|
| 216 | RSRC_CONF, |
---|
| 217 | "Cache Timeout"), |
---|
| 218 | AP_INIT_TAKE12("GnuTLSCache", mgs_set_cache, |
---|
| 219 | NULL, |
---|
| 220 | RSRC_CONF, |
---|
| 221 | "Cache Configuration"), |
---|
| 222 | AP_INIT_TAKE1("GnuTLSSessionTickets", mgs_set_tickets, |
---|
| 223 | NULL, |
---|
| 224 | RSRC_CONF, |
---|
| 225 | "Session Tickets Configuration"), |
---|
| 226 | AP_INIT_RAW_ARGS("GnuTLSPriorities", mgs_set_priorities, |
---|
| 227 | NULL, |
---|
| 228 | RSRC_CONF, |
---|
| 229 | "The priorities to enable (ciphers, Key exchange, macs, compression)."), |
---|
| 230 | AP_INIT_TAKE1("GnuTLSEnable", mgs_set_enabled, |
---|
| 231 | NULL, |
---|
| 232 | RSRC_CONF, |
---|
| 233 | "Whether this server has GnuTLS Enabled. Default: Off"), |
---|
[7d1ab49] | 234 | AP_INIT_TAKE1("GnuTLSExportCertificates", |
---|
[2aaf4f5] | 235 | mgs_set_export_certificates_size, |
---|
[7d1ab49] | 236 | NULL, |
---|
| 237 | RSRC_CONF, |
---|
[2aaf4f5] | 238 | "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"), |
---|
[0de1839] | 239 | AP_INIT_TAKE1("GnuTLSProxyKeyFile", mgs_store_cred_path, |
---|
| 240 | NULL, |
---|
| 241 | RSRC_CONF, |
---|
| 242 | "X509 client private file for proxy connections"), |
---|
| 243 | AP_INIT_TAKE1("GnuTLSProxyCertificateFile", mgs_store_cred_path, |
---|
| 244 | NULL, |
---|
| 245 | RSRC_CONF, |
---|
| 246 | "X509 client certificate file for proxy connections"), |
---|
| 247 | AP_INIT_TAKE1("GnuTLSProxyCAFile", mgs_store_cred_path, |
---|
| 248 | NULL, |
---|
| 249 | RSRC_CONF, |
---|
| 250 | "X509 trusted CA file for proxy connections"), |
---|
[809c422] | 251 | AP_INIT_TAKE1("GnuTLSProxyCRLFile", mgs_store_cred_path, |
---|
| 252 | NULL, |
---|
| 253 | RSRC_CONF, |
---|
| 254 | "X509 CRL file for proxy connections"), |
---|
[f030883] | 255 | AP_INIT_RAW_ARGS("GnuTLSProxyPriorities", mgs_set_priorities, |
---|
| 256 | NULL, |
---|
| 257 | RSRC_CONF, |
---|
| 258 | "The priorities to enable for proxy connections (ciphers, key exchange, " |
---|
| 259 | "MACs, compression)."), |
---|
[7d1ab49] | 260 | { NULL }, |
---|
[46b85d8] | 261 | }; |
---|
[9706fc2] | 262 | |
---|
| 263 | module AP_MODULE_DECLARE_DATA gnutls_module = { |
---|
[e183628] | 264 | STANDARD20_MODULE_STUFF, |
---|
[2d0f6cf] | 265 | .create_dir_config = mgs_config_dir_create, |
---|
| 266 | .merge_dir_config = mgs_config_dir_merge, |
---|
| 267 | .create_server_config = mgs_config_server_create, |
---|
[040387c] | 268 | .merge_server_config = mgs_config_server_merge, |
---|
[2d0f6cf] | 269 | .cmds = mgs_config_cmds, |
---|
| 270 | .register_hooks = gnutls_hooks |
---|
[9706fc2] | 271 | }; |
---|