[fcb122d] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[e183628] | 3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
| 4 | * Copyright 2011 Dash Shendy |
---|
[9706fc2] | 5 | * |
---|
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 7 | * you may not use this file except in compliance with the License. |
---|
| 8 | * You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | * |
---|
| 18 | */ |
---|
| 19 | |
---|
[7e2b223] | 20 | #include "mod_gnutls.h" |
---|
[9706fc2] | 21 | |
---|
[e8acf05] | 22 | #ifdef APLOG_USE_MODULE |
---|
| 23 | APLOG_USE_MODULE(gnutls); |
---|
| 24 | #endif |
---|
[671b64f] | 25 | |
---|
[e8acf05] | 26 | static void gnutls_hooks(apr_pool_t * p __attribute__((unused))) |
---|
| 27 | { |
---|
[33826c5] | 28 | /* Try Run Post-Config Hook After mod_proxy */ |
---|
| 29 | static const char * const aszPre[] = { "mod_proxy.c", NULL }; |
---|
[accbb83] | 30 | ap_hook_post_config(mgs_hook_post_config, aszPre, NULL, |
---|
| 31 | APR_HOOK_REALLY_LAST); |
---|
[33826c5] | 32 | /* HTTP Scheme Hook */ |
---|
[482f47f] | 33 | #if USING_2_1_RECENT |
---|
[33826c5] | 34 | ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE); |
---|
[482f47f] | 35 | #else |
---|
[33826c5] | 36 | ap_hook_http_method(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE); |
---|
[482f47f] | 37 | #endif |
---|
[33826c5] | 38 | /* Default Port Hook */ |
---|
[accbb83] | 39 | ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE); |
---|
[33826c5] | 40 | /* Pre-Connect Hook */ |
---|
[accbb83] | 41 | ap_hook_pre_connection(mgs_hook_pre_connection, NULL, NULL, |
---|
| 42 | APR_HOOK_MIDDLE); |
---|
[33826c5] | 43 | /* Pre-Config Hook */ |
---|
[e183628] | 44 | ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL, |
---|
[accbb83] | 45 | APR_HOOK_MIDDLE); |
---|
[33826c5] | 46 | /* Child-Init Hook */ |
---|
| 47 | ap_hook_child_init(mgs_hook_child_init, NULL, NULL, |
---|
[accbb83] | 48 | APR_HOOK_MIDDLE); |
---|
[33826c5] | 49 | /* Authentication Hook */ |
---|
[e183628] | 50 | ap_hook_access_checker(mgs_hook_authz, NULL, NULL, |
---|
[accbb83] | 51 | APR_HOOK_REALLY_FIRST); |
---|
[33826c5] | 52 | /* Fixups Hook */ |
---|
[e183628] | 53 | ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST); |
---|
[e02dd8c] | 54 | |
---|
[e183628] | 55 | /* TODO: HTTP Upgrade Filter */ |
---|
[671b64f] | 56 | /* ap_register_output_filter ("UPGRADE_FILTER", |
---|
[e183628] | 57 | * ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5); |
---|
| 58 | */ |
---|
[e02dd8c] | 59 | |
---|
[33826c5] | 60 | /* Input Filter */ |
---|
[accbb83] | 61 | ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, mgs_filter_input, |
---|
| 62 | NULL, AP_FTYPE_CONNECTION + 5); |
---|
[33826c5] | 63 | /* Output Filter */ |
---|
[accbb83] | 64 | ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, mgs_filter_output, |
---|
| 65 | NULL, AP_FTYPE_CONNECTION + 5); |
---|
[671b64f] | 66 | |
---|
[33826c5] | 67 | /* mod_proxy calls these functions */ |
---|
| 68 | APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable); |
---|
| 69 | APR_REGISTER_OPTIONAL_FN(ssl_engine_disable); |
---|
| 70 | } |
---|
| 71 | |
---|
[accbb83] | 72 | int ssl_is_https(conn_rec *c) |
---|
| 73 | { |
---|
[671b64f] | 74 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
[accbb83] | 75 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
[37f8282] | 76 | if(sc->enabled == 0 || sc->non_ssl_request == 1) { |
---|
[33826c5] | 77 | /* SSL/TLS Disabled or Plain HTTP Connection Detected */ |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
| 80 | /* Connection is Using SSL/TLS */ |
---|
| 81 | return 1; |
---|
| 82 | } |
---|
| 83 | |
---|
[e8acf05] | 84 | int ssl_engine_disable(conn_rec *c) |
---|
| 85 | { |
---|
[671b64f] | 86 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
[e8acf05] | 87 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
[33826c5] | 88 | if(sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
| 89 | return 1; |
---|
[671b64f] | 90 | } |
---|
[e8acf05] | 91 | |
---|
| 92 | /* disable TLS for this connection */ |
---|
[accbb83] | 93 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
| 94 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
[e8acf05] | 95 | if (ctxt == NULL) |
---|
| 96 | { |
---|
[accbb83] | 97 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, |
---|
| 98 | "%s: allocating connection memory", __func__); |
---|
[e8acf05] | 99 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
| 100 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
| 101 | } |
---|
| 102 | ctxt->enabled = GNUTLS_ENABLED_FALSE; |
---|
| 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 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, |
---|
| 131 | "%s: allocating connection memory", __func__); |
---|
| 132 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
| 133 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
| 134 | } |
---|
| 135 | ctxt->enabled = GNUTLS_ENABLED_TRUE; |
---|
[37f8282] | 136 | return 1; |
---|
[9706fc2] | 137 | } |
---|
| 138 | |
---|
[46b85d8] | 139 | static const command_rec mgs_config_cmds[] = { |
---|
[33826c5] | 140 | AP_INIT_TAKE1("SSLProxyEngine", mgs_set_proxy_engine, |
---|
| 141 | NULL, |
---|
| 142 | RSRC_CONF | OR_AUTHCFG, |
---|
[37f8282] | 143 | "Enable SSL Proxy Engine"), |
---|
[e183628] | 144 | AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify, |
---|
| 145 | NULL, |
---|
| 146 | RSRC_CONF | OR_AUTHCFG, |
---|
| 147 | "Set Verification Requirements of the Client Certificate"), |
---|
[cf2b905] | 148 | AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method, |
---|
| 149 | NULL, |
---|
| 150 | RSRC_CONF, |
---|
| 151 | "Set Verification Method of the Client Certificate"), |
---|
[e183628] | 152 | AP_INIT_TAKE1("GnuTLSClientCAFile", mgs_set_client_ca_file, |
---|
| 153 | NULL, |
---|
| 154 | RSRC_CONF, |
---|
| 155 | "Set the CA File to verify Client Certificates"), |
---|
| 156 | AP_INIT_TAKE1("GnuTLSX509CAFile", mgs_set_client_ca_file, |
---|
| 157 | NULL, |
---|
| 158 | RSRC_CONF, |
---|
| 159 | "Set the CA File to verify Client Certificates"), |
---|
| 160 | AP_INIT_TAKE1("GnuTLSPGPKeyringFile", mgs_set_keyring_file, |
---|
| 161 | NULL, |
---|
| 162 | RSRC_CONF, |
---|
| 163 | "Set the Keyring File to verify Client Certificates"), |
---|
| 164 | AP_INIT_TAKE1("GnuTLSDHFile", mgs_set_dh_file, |
---|
| 165 | NULL, |
---|
| 166 | RSRC_CONF, |
---|
| 167 | "Set the file to read Diffie Hellman parameters from"), |
---|
| 168 | AP_INIT_TAKE1("GnuTLSCertificateFile", mgs_set_cert_file, |
---|
| 169 | NULL, |
---|
| 170 | RSRC_CONF, |
---|
| 171 | "SSL Server X509 Certificate file"), |
---|
| 172 | AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file, |
---|
| 173 | NULL, |
---|
| 174 | RSRC_CONF, |
---|
| 175 | "SSL Server X509 Private Key file"), |
---|
| 176 | AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file, |
---|
| 177 | NULL, |
---|
| 178 | RSRC_CONF, |
---|
| 179 | "SSL Server X509 Certificate file"), |
---|
| 180 | AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file, |
---|
| 181 | NULL, |
---|
| 182 | RSRC_CONF, |
---|
| 183 | "SSL Server X509 Private Key file"), |
---|
| 184 | AP_INIT_TAKE1("GnuTLSPGPCertificateFile", mgs_set_pgpcert_file, |
---|
| 185 | NULL, |
---|
| 186 | RSRC_CONF, |
---|
| 187 | "SSL Server PGP Certificate file"), |
---|
| 188 | AP_INIT_TAKE1("GnuTLSPGPKeyFile", mgs_set_pgpkey_file, |
---|
| 189 | NULL, |
---|
| 190 | RSRC_CONF, |
---|
| 191 | "SSL Server PGP Private key file"), |
---|
[787dab7] | 192 | #ifdef ENABLE_SRP |
---|
[e183628] | 193 | AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file, |
---|
| 194 | NULL, |
---|
| 195 | RSRC_CONF, |
---|
| 196 | "SSL Server SRP Password Conf file"), |
---|
| 197 | AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile", |
---|
| 198 | mgs_set_srp_tpasswd_conf_file, |
---|
| 199 | NULL, |
---|
| 200 | RSRC_CONF, |
---|
| 201 | "SSL Server SRP Parameters file"), |
---|
[787dab7] | 202 | #endif |
---|
[e183628] | 203 | AP_INIT_TAKE1("GnuTLSCacheTimeout", mgs_set_cache_timeout, |
---|
| 204 | NULL, |
---|
| 205 | RSRC_CONF, |
---|
| 206 | "Cache Timeout"), |
---|
| 207 | AP_INIT_TAKE12("GnuTLSCache", mgs_set_cache, |
---|
| 208 | NULL, |
---|
| 209 | RSRC_CONF, |
---|
| 210 | "Cache Configuration"), |
---|
| 211 | AP_INIT_TAKE1("GnuTLSSessionTickets", mgs_set_tickets, |
---|
| 212 | NULL, |
---|
| 213 | RSRC_CONF, |
---|
| 214 | "Session Tickets Configuration"), |
---|
| 215 | AP_INIT_RAW_ARGS("GnuTLSPriorities", mgs_set_priorities, |
---|
| 216 | NULL, |
---|
| 217 | RSRC_CONF, |
---|
| 218 | "The priorities to enable (ciphers, Key exchange, macs, compression)."), |
---|
| 219 | AP_INIT_TAKE1("GnuTLSEnable", mgs_set_enabled, |
---|
| 220 | NULL, |
---|
| 221 | RSRC_CONF, |
---|
| 222 | "Whether this server has GnuTLS Enabled. Default: Off"), |
---|
[7d1ab49] | 223 | AP_INIT_TAKE1("GnuTLSExportCertificates", |
---|
[2aaf4f5] | 224 | mgs_set_export_certificates_size, |
---|
[7d1ab49] | 225 | NULL, |
---|
| 226 | RSRC_CONF, |
---|
[2aaf4f5] | 227 | "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"), |
---|
[7d1ab49] | 228 | { NULL }, |
---|
[46b85d8] | 229 | }; |
---|
[9706fc2] | 230 | |
---|
| 231 | module AP_MODULE_DECLARE_DATA gnutls_module = { |
---|
[e183628] | 232 | STANDARD20_MODULE_STUFF, |
---|
[2d0f6cf] | 233 | .create_dir_config = mgs_config_dir_create, |
---|
| 234 | .merge_dir_config = mgs_config_dir_merge, |
---|
| 235 | .create_server_config = mgs_config_server_create, |
---|
[040387c] | 236 | .merge_server_config = mgs_config_server_merge, |
---|
[2d0f6cf] | 237 | .cmds = mgs_config_cmds, |
---|
| 238 | .register_hooks = gnutls_hooks |
---|
[9706fc2] | 239 | }; |
---|