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