1 | /** |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * Copyright 2008, 2014 Nikos Mavrogiannopoulos |
---|
4 | * Copyright 2011 Dash Shendy |
---|
5 | * Copyright 2015 Thomas Klute |
---|
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 | |
---|
21 | #include "mod_gnutls.h" |
---|
22 | #include "gnutls_ocsp.h" |
---|
23 | |
---|
24 | #ifdef APLOG_USE_MODULE |
---|
25 | APLOG_USE_MODULE(gnutls); |
---|
26 | #endif |
---|
27 | |
---|
28 | static void gnutls_hooks(apr_pool_t * p __attribute__((unused))) |
---|
29 | { |
---|
30 | /* Try Run Post-Config Hook After mod_proxy */ |
---|
31 | static const char * const aszPre[] = { "mod_proxy.c", NULL }; |
---|
32 | ap_hook_post_config(mgs_hook_post_config, aszPre, NULL, |
---|
33 | APR_HOOK_REALLY_LAST); |
---|
34 | /* HTTP Scheme Hook */ |
---|
35 | #if USING_2_1_RECENT |
---|
36 | ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE); |
---|
37 | #else |
---|
38 | ap_hook_http_method(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE); |
---|
39 | #endif |
---|
40 | /* Default Port Hook */ |
---|
41 | ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE); |
---|
42 | /* Pre-Connect Hook */ |
---|
43 | ap_hook_pre_connection(mgs_hook_pre_connection, NULL, NULL, |
---|
44 | APR_HOOK_MIDDLE); |
---|
45 | /* Pre-Config Hook */ |
---|
46 | ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL, |
---|
47 | APR_HOOK_MIDDLE); |
---|
48 | /* Child-Init Hook */ |
---|
49 | ap_hook_child_init(mgs_hook_child_init, NULL, NULL, |
---|
50 | APR_HOOK_MIDDLE); |
---|
51 | /* Authentication Hook */ |
---|
52 | ap_hook_access_checker(mgs_hook_authz, NULL, NULL, |
---|
53 | APR_HOOK_REALLY_FIRST); |
---|
54 | /* Fixups Hook */ |
---|
55 | ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST); |
---|
56 | |
---|
57 | /* TODO: HTTP Upgrade Filter */ |
---|
58 | /* ap_register_output_filter ("UPGRADE_FILTER", |
---|
59 | * ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5); |
---|
60 | */ |
---|
61 | |
---|
62 | /* Input Filter */ |
---|
63 | ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, mgs_filter_input, |
---|
64 | NULL, AP_FTYPE_CONNECTION + 5); |
---|
65 | /* Output Filter */ |
---|
66 | ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, mgs_filter_output, |
---|
67 | NULL, AP_FTYPE_CONNECTION + 5); |
---|
68 | |
---|
69 | /* mod_proxy calls these functions */ |
---|
70 | APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable); |
---|
71 | APR_REGISTER_OPTIONAL_FN(ssl_engine_disable); |
---|
72 | |
---|
73 | /* mod_rewrite calls this function to detect HTTPS */ |
---|
74 | APR_REGISTER_OPTIONAL_FN(ssl_is_https); |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | |
---|
79 | /* |
---|
80 | * mod_rewrite calls this function to fill %{HTTPS}. A non-zero return |
---|
81 | * value means that HTTPS is in use. |
---|
82 | */ |
---|
83 | int ssl_is_https(conn_rec *c) |
---|
84 | { |
---|
85 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
86 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
87 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
88 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
89 | |
---|
90 | if(sc->enabled == GNUTLS_ENABLED_FALSE |
---|
91 | || ctxt == NULL |
---|
92 | || ctxt->enabled == GNUTLS_ENABLED_FALSE) |
---|
93 | { |
---|
94 | /* SSL/TLS Disabled or Plain HTTP Connection Detected */ |
---|
95 | return 0; |
---|
96 | } |
---|
97 | /* Connection is Using SSL/TLS */ |
---|
98 | return 1; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | int ssl_engine_disable(conn_rec *c) |
---|
104 | { |
---|
105 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
106 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
107 | if(sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
108 | return 1; |
---|
109 | } |
---|
110 | |
---|
111 | /* disable TLS for this connection */ |
---|
112 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
113 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
114 | if (ctxt == NULL) |
---|
115 | { |
---|
116 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
117 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
118 | } |
---|
119 | ctxt->enabled = GNUTLS_ENABLED_FALSE; |
---|
120 | ctxt->is_proxy = GNUTLS_ENABLED_TRUE; |
---|
121 | |
---|
122 | if (c->input_filters) |
---|
123 | ap_remove_input_filter(c->input_filters); |
---|
124 | if (c->output_filters) |
---|
125 | ap_remove_output_filter(c->output_filters); |
---|
126 | |
---|
127 | return 1; |
---|
128 | } |
---|
129 | |
---|
130 | int ssl_proxy_enable(conn_rec *c) |
---|
131 | { |
---|
132 | /* check if TLS proxy support is enabled */ |
---|
133 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
134 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
135 | if (sc->proxy_enabled != GNUTLS_ENABLED_TRUE) |
---|
136 | { |
---|
137 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, |
---|
138 | "%s: mod_proxy requested TLS proxy, but not enabled " |
---|
139 | "for %s", __func__, sc->cert_cn); |
---|
140 | return 0; |
---|
141 | } |
---|
142 | |
---|
143 | /* enable TLS for this connection */ |
---|
144 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
145 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
146 | if (ctxt == NULL) |
---|
147 | { |
---|
148 | ctxt = apr_pcalloc(c->pool, sizeof (*ctxt)); |
---|
149 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
150 | } |
---|
151 | ctxt->enabled = GNUTLS_ENABLED_TRUE; |
---|
152 | ctxt->is_proxy = GNUTLS_ENABLED_TRUE; |
---|
153 | return 1; |
---|
154 | } |
---|
155 | |
---|
156 | static const command_rec mgs_config_cmds[] = { |
---|
157 | AP_INIT_FLAG("GnuTLSProxyEngine", mgs_set_proxy_engine, |
---|
158 | NULL, |
---|
159 | RSRC_CONF | OR_AUTHCFG, |
---|
160 | "Enable TLS Proxy Engine"), |
---|
161 | AP_INIT_TAKE1("GnuTLSP11Module", mgs_set_p11_module, |
---|
162 | NULL, |
---|
163 | RSRC_CONF, |
---|
164 | "Load this specific PKCS #11 provider library"), |
---|
165 | AP_INIT_RAW_ARGS("GnuTLSPIN", mgs_set_pin, |
---|
166 | NULL, |
---|
167 | RSRC_CONF, |
---|
168 | "The PIN to use in case of encrypted keys or PKCS #11 tokens."), |
---|
169 | AP_INIT_RAW_ARGS("GnuTLSSRKPIN", mgs_set_srk_pin, |
---|
170 | NULL, |
---|
171 | RSRC_CONF, |
---|
172 | "The SRK PIN to use in case of TPM keys."), |
---|
173 | AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify, |
---|
174 | NULL, |
---|
175 | RSRC_CONF | OR_AUTHCFG, |
---|
176 | "Set Verification Requirements of the Client Certificate"), |
---|
177 | AP_INIT_TAKE1("GnuTLSClientVerifyMethod", mgs_set_client_verify_method, |
---|
178 | NULL, |
---|
179 | RSRC_CONF, |
---|
180 | "Set Verification Method of the Client Certificate"), |
---|
181 | AP_INIT_TAKE1("GnuTLSClientCAFile", mgs_set_client_ca_file, |
---|
182 | NULL, |
---|
183 | RSRC_CONF, |
---|
184 | "Set the CA File to verify Client Certificates"), |
---|
185 | AP_INIT_TAKE1("GnuTLSX509CAFile", mgs_set_client_ca_file, |
---|
186 | NULL, |
---|
187 | RSRC_CONF, |
---|
188 | "Set the CA File to verify Client Certificates"), |
---|
189 | AP_INIT_TAKE1("GnuTLSPGPKeyringFile", mgs_set_keyring_file, |
---|
190 | NULL, |
---|
191 | RSRC_CONF, |
---|
192 | "Set the Keyring File to verify Client Certificates"), |
---|
193 | AP_INIT_TAKE1("GnuTLSDHFile", mgs_set_dh_file, |
---|
194 | NULL, |
---|
195 | RSRC_CONF, |
---|
196 | "Set the file to read Diffie Hellman parameters from"), |
---|
197 | AP_INIT_TAKE1("GnuTLSCertificateFile", mgs_set_cert_file, |
---|
198 | NULL, |
---|
199 | RSRC_CONF, |
---|
200 | "TLS Server X509 Certificate file"), |
---|
201 | AP_INIT_TAKE1("GnuTLSKeyFile", mgs_set_key_file, |
---|
202 | NULL, |
---|
203 | RSRC_CONF, |
---|
204 | "TLS Server X509 Private Key file"), |
---|
205 | AP_INIT_TAKE1("GnuTLSX509CertificateFile", mgs_set_cert_file, |
---|
206 | NULL, |
---|
207 | RSRC_CONF, |
---|
208 | "TLS Server X509 Certificate file"), |
---|
209 | AP_INIT_TAKE1("GnuTLSX509KeyFile", mgs_set_key_file, |
---|
210 | NULL, |
---|
211 | RSRC_CONF, |
---|
212 | "TLS Server X509 Private Key file"), |
---|
213 | AP_INIT_TAKE1("GnuTLSPGPCertificateFile", mgs_set_pgpcert_file, |
---|
214 | NULL, |
---|
215 | RSRC_CONF, |
---|
216 | "TLS Server PGP Certificate file"), |
---|
217 | AP_INIT_TAKE1("GnuTLSPGPKeyFile", mgs_set_pgpkey_file, |
---|
218 | NULL, |
---|
219 | RSRC_CONF, |
---|
220 | "TLS Server PGP Private key file"), |
---|
221 | #ifdef ENABLE_SRP |
---|
222 | AP_INIT_TAKE1("GnuTLSSRPPasswdFile", mgs_set_srp_tpasswd_file, |
---|
223 | NULL, |
---|
224 | RSRC_CONF, |
---|
225 | "TLS Server SRP Password Conf file"), |
---|
226 | AP_INIT_TAKE1("GnuTLSSRPPasswdConfFile", |
---|
227 | mgs_set_srp_tpasswd_conf_file, |
---|
228 | NULL, |
---|
229 | RSRC_CONF, |
---|
230 | "TLS Server SRP Parameters file"), |
---|
231 | #endif |
---|
232 | AP_INIT_TAKE1("GnuTLSCacheTimeout", mgs_set_cache_timeout, |
---|
233 | NULL, |
---|
234 | RSRC_CONF, |
---|
235 | "Cache Timeout"), |
---|
236 | AP_INIT_TAKE12("GnuTLSCache", mgs_set_cache, |
---|
237 | NULL, |
---|
238 | RSRC_CONF, |
---|
239 | "Cache Configuration"), |
---|
240 | AP_INIT_FLAG("GnuTLSSessionTickets", mgs_set_tickets, |
---|
241 | NULL, |
---|
242 | RSRC_CONF, |
---|
243 | "Session Tickets Configuration"), |
---|
244 | AP_INIT_RAW_ARGS("GnuTLSPriorities", mgs_set_priorities, |
---|
245 | NULL, |
---|
246 | RSRC_CONF, |
---|
247 | "The priorities to enable (ciphers, Key exchange, macs, compression)."), |
---|
248 | AP_INIT_FLAG("GnuTLSEnable", mgs_set_enabled, |
---|
249 | NULL, |
---|
250 | RSRC_CONF, |
---|
251 | "Whether this server has GnuTLS Enabled. Default: Off"), |
---|
252 | AP_INIT_TAKE1("GnuTLSExportCertificates", |
---|
253 | mgs_set_export_certificates_size, |
---|
254 | NULL, |
---|
255 | RSRC_CONF, |
---|
256 | "Max size to export PEM encoded certificates to CGIs (or off to disable). Default: off"), |
---|
257 | AP_INIT_TAKE1("GnuTLSProxyKeyFile", mgs_store_cred_path, |
---|
258 | NULL, |
---|
259 | RSRC_CONF, |
---|
260 | "X509 client private file for proxy connections"), |
---|
261 | AP_INIT_TAKE1("GnuTLSProxyCertificateFile", mgs_store_cred_path, |
---|
262 | NULL, |
---|
263 | RSRC_CONF, |
---|
264 | "X509 client certificate file for proxy connections"), |
---|
265 | AP_INIT_TAKE1("GnuTLSProxyCAFile", mgs_store_cred_path, |
---|
266 | NULL, |
---|
267 | RSRC_CONF, |
---|
268 | "X509 trusted CA file for proxy connections"), |
---|
269 | AP_INIT_TAKE1("GnuTLSProxyCRLFile", mgs_store_cred_path, |
---|
270 | NULL, |
---|
271 | RSRC_CONF, |
---|
272 | "X509 CRL file for proxy connections"), |
---|
273 | AP_INIT_RAW_ARGS("GnuTLSProxyPriorities", mgs_set_priorities, |
---|
274 | NULL, |
---|
275 | RSRC_CONF, |
---|
276 | "The priorities to enable for proxy connections (ciphers, key exchange, " |
---|
277 | "MACs, compression)."), |
---|
278 | AP_INIT_TAKE1("GnuTLSOCSPResponseFile", mgs_store_ocsp_response_path, |
---|
279 | NULL, |
---|
280 | RSRC_CONF, |
---|
281 | "EXPERIMENTAL: OCSP response for stapling (must be updated externally)"), |
---|
282 | { NULL }, |
---|
283 | }; |
---|
284 | |
---|
285 | module AP_MODULE_DECLARE_DATA gnutls_module = { |
---|
286 | STANDARD20_MODULE_STUFF, |
---|
287 | .create_dir_config = mgs_config_dir_create, |
---|
288 | .merge_dir_config = mgs_config_dir_merge, |
---|
289 | .create_server_config = mgs_config_server_create, |
---|
290 | .merge_server_config = mgs_config_server_merge, |
---|
291 | .cmds = mgs_config_cmds, |
---|
292 | .register_hooks = gnutls_hooks |
---|
293 | }; |
---|