1 | /* |
---|
2 | * Copyright 2004-2005 Paul Querna |
---|
3 | * Copyright 2008, 2014 Nikos Mavrogiannopoulos |
---|
4 | * Copyright 2011 Dash Shendy |
---|
5 | * Copyright 2013-2014 Daniel Kahn Gillmor |
---|
6 | * Copyright 2015-2018 Fiona Klute |
---|
7 | * |
---|
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
9 | * you may not use this file except in compliance with the License. |
---|
10 | * You may obtain a copy of the License at |
---|
11 | * |
---|
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
13 | * |
---|
14 | * Unless required by applicable law or agreed to in writing, software |
---|
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
17 | * See the License for the specific language governing permissions and |
---|
18 | * limitations under the License. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "mod_gnutls.h" |
---|
22 | #include "gnutls_cache.h" |
---|
23 | #include "gnutls_config.h" |
---|
24 | #include "gnutls_ocsp.h" |
---|
25 | #include "gnutls_util.h" |
---|
26 | #include "gnutls_watchdog.h" |
---|
27 | |
---|
28 | #include "http_vhost.h" |
---|
29 | #include "ap_mpm.h" |
---|
30 | #include <mod_status.h> |
---|
31 | #include <util_mutex.h> |
---|
32 | #include <apr_escape.h> |
---|
33 | /* This provides strcmp and related functions */ |
---|
34 | #define APR_WANT_STRFUNC |
---|
35 | #include <apr_want.h> |
---|
36 | |
---|
37 | #ifdef ENABLE_MSVA |
---|
38 | #include <msv/msv.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifdef APLOG_USE_MODULE |
---|
42 | APLOG_USE_MODULE(gnutls); |
---|
43 | #endif |
---|
44 | |
---|
45 | #if MOD_GNUTLS_DEBUG |
---|
46 | static apr_file_t *debug_log_fp; |
---|
47 | #endif |
---|
48 | |
---|
49 | #define IS_PROXY_STR(c) \ |
---|
50 | ((c->is_proxy == GNUTLS_ENABLED_TRUE) ? "proxy " : "") |
---|
51 | |
---|
52 | /** Key to encrypt session tickets. Must be kept secret. This key is |
---|
53 | * generated in the `pre_config` hook and thus constant across |
---|
54 | * forks. The problem with this approach is that it does not support |
---|
55 | * regular key rotation. */ |
---|
56 | static gnutls_datum_t session_ticket_key = {NULL, 0}; |
---|
57 | |
---|
58 | |
---|
59 | /** Default GnuTLS priority string for mod_gnutls */ |
---|
60 | #define MGS_DEFAULT_PRIORITY "NORMAL" |
---|
61 | /** Compiled version of MGS_DEFAULT_PRIORITY (initialized in the |
---|
62 | * pre_config hook) */ |
---|
63 | static gnutls_priority_t default_prio; |
---|
64 | |
---|
65 | |
---|
66 | static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt); |
---|
67 | /* use side==0 for server and side==1 for client */ |
---|
68 | static void mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, int side, size_t export_cert_size); |
---|
69 | static int mgs_status_hook(request_rec *r, int flags); |
---|
70 | #ifdef ENABLE_MSVA |
---|
71 | static const char* mgs_x509_construct_uid(request_rec * pool, gnutls_x509_crt_t cert); |
---|
72 | #endif |
---|
73 | static int load_proxy_x509_credentials(apr_pool_t *pconf, apr_pool_t *ptemp, server_rec *s) |
---|
74 | __attribute__((nonnull)); |
---|
75 | |
---|
76 | /* Pool Cleanup Function */ |
---|
77 | apr_status_t mgs_cleanup_pre_config(void *data __attribute__((unused))) |
---|
78 | { |
---|
79 | /* Free session ticket master key */ |
---|
80 | #if GNUTLS_VERSION_NUMBER >= 0x030400 |
---|
81 | gnutls_memset(session_ticket_key.data, 0, session_ticket_key.size); |
---|
82 | #endif |
---|
83 | gnutls_free(session_ticket_key.data); |
---|
84 | session_ticket_key.data = NULL; |
---|
85 | session_ticket_key.size = 0; |
---|
86 | |
---|
87 | /* Deinit default priority setting */ |
---|
88 | gnutls_priority_deinit(default_prio); |
---|
89 | return APR_SUCCESS; |
---|
90 | } |
---|
91 | |
---|
92 | /* Logging Function for Maintainers */ |
---|
93 | #if MOD_GNUTLS_DEBUG |
---|
94 | static void gnutls_debug_log_all(int level, const char *str) { |
---|
95 | apr_file_printf(debug_log_fp, "<%d> %s", level, str); |
---|
96 | } |
---|
97 | #define _gnutls_log apr_file_printf |
---|
98 | #else |
---|
99 | #define _gnutls_log(...) |
---|
100 | #endif |
---|
101 | |
---|
102 | static const char* mgs_readable_cvm(mgs_client_verification_method_e m) { |
---|
103 | switch(m) { |
---|
104 | case mgs_cvm_unset: |
---|
105 | return "unset"; |
---|
106 | case mgs_cvm_cartel: |
---|
107 | return "cartel"; |
---|
108 | case mgs_cvm_msva: |
---|
109 | return "msva"; |
---|
110 | } |
---|
111 | return "unknown"; |
---|
112 | } |
---|
113 | |
---|
114 | /* Pre-Configuration HOOK: Runs First */ |
---|
115 | int mgs_hook_pre_config(apr_pool_t * pconf, apr_pool_t * plog, apr_pool_t * ptemp __attribute__((unused))) { |
---|
116 | |
---|
117 | /* Maintainer Logging */ |
---|
118 | #if MOD_GNUTLS_DEBUG |
---|
119 | apr_file_open(&debug_log_fp, "/tmp/gnutls_debug", APR_APPEND | APR_WRITE | APR_CREATE, APR_OS_DEFAULT, pconf); |
---|
120 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
121 | gnutls_global_set_log_level(9); |
---|
122 | gnutls_global_set_log_function(gnutls_debug_log_all); |
---|
123 | _gnutls_log(debug_log_fp, "gnutls: %s\n", gnutls_check_version(NULL)); |
---|
124 | #endif |
---|
125 | |
---|
126 | int ret; |
---|
127 | |
---|
128 | /* Check for required GnuTLS Library Version */ |
---|
129 | if (gnutls_check_version(LIBGNUTLS_VERSION) == NULL) { |
---|
130 | ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "gnutls_check_version() failed. Required: " |
---|
131 | "gnutls-%s Found: gnutls-%s", LIBGNUTLS_VERSION, gnutls_check_version(NULL)); |
---|
132 | return DONE; |
---|
133 | } |
---|
134 | |
---|
135 | /* Generate a Session Key */ |
---|
136 | ret = gnutls_session_ticket_key_generate(&session_ticket_key); |
---|
137 | if (ret < 0) { |
---|
138 | ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, "gnutls_session_ticket_key_generate: %s", gnutls_strerror(ret)); |
---|
139 | return DONE; |
---|
140 | } |
---|
141 | |
---|
142 | /* Initialize default priority setting */ |
---|
143 | ret = gnutls_priority_init(&default_prio, MGS_DEFAULT_PRIORITY, NULL); |
---|
144 | if (ret < 0) |
---|
145 | { |
---|
146 | ap_log_perror(APLOG_MARK, APLOG_EMERG, 0, plog, |
---|
147 | "gnutls_priority_init failed for default '%s': %s (%d)", |
---|
148 | MGS_DEFAULT_PRIORITY, gnutls_strerror(ret), ret); |
---|
149 | return DONE; |
---|
150 | } |
---|
151 | |
---|
152 | AP_OPTIONAL_HOOK(status_hook, mgs_status_hook, NULL, NULL, APR_HOOK_MIDDLE); |
---|
153 | |
---|
154 | ap_mutex_register(pconf, MGS_CACHE_MUTEX_NAME, NULL, APR_LOCK_DEFAULT, 0); |
---|
155 | ap_mutex_register(pconf, MGS_OCSP_MUTEX_NAME, NULL, APR_LOCK_DEFAULT, 0); |
---|
156 | ap_mutex_register(pconf, MGS_OCSP_CACHE_MUTEX_NAME, NULL, |
---|
157 | APR_LOCK_DEFAULT, 0); |
---|
158 | |
---|
159 | /* Register a pool clean-up function */ |
---|
160 | apr_pool_cleanup_register(pconf, NULL, mgs_cleanup_pre_config, apr_pool_cleanup_null); |
---|
161 | |
---|
162 | return OK; |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | /** |
---|
168 | * Get the list of available protocols for this connection and add it |
---|
169 | * to the GnuTLS session. Must run before the client hello function. |
---|
170 | */ |
---|
171 | static void prepare_alpn_proposals(mgs_handle_t *ctxt) |
---|
172 | { |
---|
173 | /* Check if any protocol upgrades are available |
---|
174 | * |
---|
175 | * The "report_all" parameter to ap_get_protocol_upgrades() is 0 |
---|
176 | * (report only more preferable protocols) because setting it to 1 |
---|
177 | * doesn't actually report ALL protocols, but only all except the |
---|
178 | * current one. This way we can at least list the current one as |
---|
179 | * available by appending it without potentially negotiating a |
---|
180 | * less preferred protocol. */ |
---|
181 | const apr_array_header_t *pupgrades = NULL; |
---|
182 | apr_status_t ret = |
---|
183 | ap_get_protocol_upgrades(ctxt->c, NULL, ctxt->c->base_server, |
---|
184 | /*report_all*/ 0, &pupgrades); |
---|
185 | if (ret != APR_SUCCESS) |
---|
186 | { |
---|
187 | ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, ctxt->c, |
---|
188 | "%s: ap_get_protocol_upgrades() failed, " |
---|
189 | "cannot configure ALPN!", __func__); |
---|
190 | return; |
---|
191 | } |
---|
192 | |
---|
193 | if (pupgrades == NULL || pupgrades->nelts == 0) |
---|
194 | { |
---|
195 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, ctxt->c, |
---|
196 | "%s: No protocol upgrades available.", __func__); |
---|
197 | return; |
---|
198 | } |
---|
199 | |
---|
200 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, ctxt->c, |
---|
201 | "%s: Found %d protocol upgrade(s) for ALPN: %s", |
---|
202 | __func__, pupgrades->nelts, |
---|
203 | apr_array_pstrcat(ctxt->c->pool, pupgrades, ',')); |
---|
204 | gnutls_datum_t *alpn_protos = |
---|
205 | apr_palloc(ctxt->c->pool, |
---|
206 | (pupgrades->nelts + 1) * sizeof(gnutls_datum_t)); |
---|
207 | for (int i = 0; i < pupgrades->nelts; i++) |
---|
208 | { |
---|
209 | alpn_protos[i].data = (void *) APR_ARRAY_IDX(pupgrades, i, char *); |
---|
210 | alpn_protos[i].size = |
---|
211 | strnlen(APR_ARRAY_IDX(pupgrades, i, char *), |
---|
212 | pupgrades->elt_size); |
---|
213 | } |
---|
214 | |
---|
215 | /* Add the current (default) protocol at the end of the list */ |
---|
216 | alpn_protos[pupgrades->nelts].data = |
---|
217 | (void*) apr_pstrdup(ctxt->c->pool, ap_get_protocol(ctxt->c)); |
---|
218 | alpn_protos[pupgrades->nelts].size = |
---|
219 | strlen((char*) alpn_protos[pupgrades->nelts].data); |
---|
220 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, ctxt->c, |
---|
221 | "%s: Adding current protocol %s to ALPN set.", |
---|
222 | __func__, alpn_protos[pupgrades->nelts].data); |
---|
223 | |
---|
224 | gnutls_alpn_set_protocols(ctxt->session, |
---|
225 | alpn_protos, |
---|
226 | pupgrades->nelts, |
---|
227 | GNUTLS_ALPN_SERVER_PRECEDENCE); |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | /** |
---|
233 | * Check if ALPN selected any protocol upgrade, try to switch if so. |
---|
234 | */ |
---|
235 | static int process_alpn_result(mgs_handle_t *ctxt) |
---|
236 | { |
---|
237 | int ret = 0; |
---|
238 | gnutls_datum_t alpn_proto; |
---|
239 | ret = gnutls_alpn_get_selected_protocol(ctxt->session, &alpn_proto); |
---|
240 | if (ret != GNUTLS_E_SUCCESS) |
---|
241 | { |
---|
242 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, APR_SUCCESS, ctxt->c, |
---|
243 | "%s: No ALPN result: %s (%d)", |
---|
244 | __func__, gnutls_strerror(ret), ret); |
---|
245 | return GNUTLS_E_SUCCESS; |
---|
246 | } |
---|
247 | |
---|
248 | apr_array_header_t *client_protos = |
---|
249 | apr_array_make(ctxt->c->pool, 1, sizeof(char *)); |
---|
250 | /* apr_pstrndup to ensure that the protocol is null terminated */ |
---|
251 | APR_ARRAY_PUSH(client_protos, char *) = |
---|
252 | apr_pstrndup(ctxt->c->pool, (char*) alpn_proto.data, alpn_proto.size); |
---|
253 | const char *selected = |
---|
254 | ap_select_protocol(ctxt->c, NULL, ctxt->c->base_server, |
---|
255 | client_protos); |
---|
256 | |
---|
257 | /* ap_select_protocol() will return NULL if none of the ALPN |
---|
258 | * proposals matched. GnuTLS negotiated alpn_proto based on the |
---|
259 | * list provided by the server, but the vhost might have changed |
---|
260 | * based on SNI. Apache seems to adjust the proposal list to avoid |
---|
261 | * such issues though. |
---|
262 | * |
---|
263 | * GnuTLS will return a fatal "no_application_protocol" alert as |
---|
264 | * required by RFC 7301 if the post client hello function returns |
---|
265 | * GNUTLS_E_NO_APPLICATION_PROTOCOL. */ |
---|
266 | if (!selected) |
---|
267 | { |
---|
268 | ap_log_cerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, ctxt->c, |
---|
269 | "%s: ap_select_protocol() returned NULL! Please " |
---|
270 | "make sure any overlapping vhosts have the same " |
---|
271 | "protocols available.", |
---|
272 | __func__); |
---|
273 | return GNUTLS_E_NO_APPLICATION_PROTOCOL; |
---|
274 | } |
---|
275 | |
---|
276 | if (strcmp(selected, ap_get_protocol(ctxt->c)) == 0) |
---|
277 | { |
---|
278 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, APR_SUCCESS, ctxt->c, |
---|
279 | "%s: Already using protocol '%s', nothing to do.", |
---|
280 | __func__, selected); |
---|
281 | return GNUTLS_E_SUCCESS; |
---|
282 | } |
---|
283 | |
---|
284 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, ctxt->c, |
---|
285 | "%s: Switching protocol to '%s' based on ALPN.", |
---|
286 | __func__, selected); |
---|
287 | apr_status_t status = ap_switch_protocol(ctxt->c, NULL, |
---|
288 | ctxt->c->base_server, |
---|
289 | selected); |
---|
290 | if (status != APR_SUCCESS) |
---|
291 | { |
---|
292 | ap_log_cerror(APLOG_MARK, APLOG_ERR, status, ctxt->c, |
---|
293 | "%s: Protocol switch to '%s' failed!", |
---|
294 | __func__, selected); |
---|
295 | return GNUTLS_E_NO_APPLICATION_PROTOCOL; |
---|
296 | } |
---|
297 | /* ALPN done! */ |
---|
298 | return GNUTLS_E_SUCCESS; |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | /** |
---|
304 | * Post client hello function for GnuTLS, used to configure the TLS |
---|
305 | * server based on virtual host configuration. Uses SNI to select the |
---|
306 | * virtual host if available. |
---|
307 | * |
---|
308 | * @param session the TLS session |
---|
309 | * |
---|
310 | * @return zero or a GnuTLS error code, as required by GnuTLS hook |
---|
311 | * definition |
---|
312 | */ |
---|
313 | static int mgs_select_virtual_server_cb(gnutls_session_t session) |
---|
314 | { |
---|
315 | int ret = 0; |
---|
316 | mgs_handle_t *ctxt = gnutls_session_get_ptr(session); |
---|
317 | |
---|
318 | /* try to find a virtual host */ |
---|
319 | mgs_srvconf_rec *tsc = mgs_find_sni_server(session); |
---|
320 | if (tsc != NULL) |
---|
321 | { |
---|
322 | /* Found a TLS vhost based on the SNI, configure the |
---|
323 | * connection context. */ |
---|
324 | ctxt->sc = tsc; |
---|
325 | } |
---|
326 | |
---|
327 | gnutls_certificate_server_set_request(session, ctxt->sc->client_verify_mode); |
---|
328 | |
---|
329 | /* Set x509 credentials */ |
---|
330 | gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, ctxt->sc->certs); |
---|
331 | /* Set Anon credentials */ |
---|
332 | gnutls_credentials_set(session, GNUTLS_CRD_ANON, ctxt->sc->anon_creds); |
---|
333 | |
---|
334 | #ifdef ENABLE_SRP |
---|
335 | /* Set SRP credentials */ |
---|
336 | if (ctxt->sc->srp_tpasswd_conf_file != NULL && ctxt->sc->srp_tpasswd_file != NULL) { |
---|
337 | gnutls_credentials_set(session, GNUTLS_CRD_SRP, ctxt->sc->srp_creds); |
---|
338 | } |
---|
339 | #endif |
---|
340 | |
---|
341 | /* Enable session tickets */ |
---|
342 | if (session_ticket_key.data != NULL && |
---|
343 | ctxt->sc->tickets == GNUTLS_ENABLED_TRUE) |
---|
344 | { |
---|
345 | ret = gnutls_session_ticket_enable_server(ctxt->session, &session_ticket_key); |
---|
346 | if (ret != GNUTLS_E_SUCCESS) |
---|
347 | ap_log_cerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, ctxt->c, |
---|
348 | "gnutls_session_ticket_enable_server failed: %s (%d)", |
---|
349 | gnutls_strerror(ret), ret); |
---|
350 | } |
---|
351 | |
---|
352 | ret = process_alpn_result(ctxt); |
---|
353 | if (ret != GNUTLS_E_SUCCESS) |
---|
354 | return ret; |
---|
355 | |
---|
356 | /* Update the priorities - to avoid negotiating a ciphersuite that is not |
---|
357 | * enabled on this virtual server. Note that here we ignore the version |
---|
358 | * negotiation. */ |
---|
359 | ret = gnutls_priority_set(session, ctxt->sc->priorities); |
---|
360 | |
---|
361 | /* actually it shouldn't fail since we have checked at startup */ |
---|
362 | return ret; |
---|
363 | } |
---|
364 | |
---|
365 | static int cert_retrieve_fn(gnutls_session_t session, |
---|
366 | const gnutls_datum_t * req_ca_rdn __attribute__((unused)), |
---|
367 | int nreqs __attribute__((unused)), |
---|
368 | const gnutls_pk_algorithm_t * pk_algos __attribute__((unused)), |
---|
369 | int pk_algos_length __attribute__((unused)), |
---|
370 | gnutls_pcert_st **pcerts, |
---|
371 | unsigned int *pcert_length, |
---|
372 | gnutls_privkey_t *privkey) |
---|
373 | { |
---|
374 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
375 | |
---|
376 | mgs_handle_t *ctxt; |
---|
377 | |
---|
378 | if (session == NULL) { |
---|
379 | // ERROR INVALID SESSION |
---|
380 | return -1; |
---|
381 | } |
---|
382 | |
---|
383 | ctxt = gnutls_transport_get_ptr(session); |
---|
384 | |
---|
385 | if (gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) { |
---|
386 | // X509 CERTIFICATE |
---|
387 | *pcerts = ctxt->sc->certs_x509_chain; |
---|
388 | *pcert_length = ctxt->sc->certs_x509_chain_num; |
---|
389 | *privkey = ctxt->sc->privkey_x509; |
---|
390 | return 0; |
---|
391 | } else { |
---|
392 | // UNKNOWN CERTIFICATE |
---|
393 | return -1; |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | |
---|
398 | |
---|
399 | #if GNUTLS_VERSION_NUMBER >= 0x030506 |
---|
400 | #define HAVE_KNOWN_DH_GROUPS 1 |
---|
401 | #endif |
---|
402 | #ifdef HAVE_KNOWN_DH_GROUPS |
---|
403 | /** |
---|
404 | * Try to estimate a GnuTLS security parameter based on the given |
---|
405 | * private key. Any errors are logged. |
---|
406 | * |
---|
407 | * @param s The `server_rec` to use for logging |
---|
408 | * |
---|
409 | * @param key The private key to use |
---|
410 | * |
---|
411 | * @return `gnutls_sec_param_t` as returned by |
---|
412 | * `gnutls_pk_bits_to_sec_param` for the key properties, or |
---|
413 | * GNUTLS_SEC_PARAM_UNKNOWN in case of error |
---|
414 | */ |
---|
415 | static gnutls_sec_param_t sec_param_from_privkey(server_rec *server, |
---|
416 | gnutls_privkey_t key) |
---|
417 | { |
---|
418 | unsigned int bits = 0; |
---|
419 | int pk_algo = gnutls_privkey_get_pk_algorithm(key, &bits); |
---|
420 | if (pk_algo < 0) |
---|
421 | { |
---|
422 | ap_log_error(APLOG_MARK, APLOG_ERR, APR_EGENERAL, server, |
---|
423 | "%s: Could not get private key parameters: %s (%d)", |
---|
424 | __func__, gnutls_strerror(pk_algo), pk_algo); |
---|
425 | return GNUTLS_SEC_PARAM_UNKNOWN; |
---|
426 | } |
---|
427 | return gnutls_pk_bits_to_sec_param(pk_algo, bits); |
---|
428 | } |
---|
429 | #else |
---|
430 | /** ffdhe2048 DH group as defined in RFC 7919, Appendix A.1. This is |
---|
431 | * the default DH group if mod_gnutls is compiled agains a GnuTLS |
---|
432 | * version that does not provide known DH groups based on security |
---|
433 | * parameters (before 3.5.6). */ |
---|
434 | static const char FFDHE2048_PKCS3[] = |
---|
435 | "-----BEGIN DH PARAMETERS-----\n" |
---|
436 | "MIIBDAKCAQEA//////////+t+FRYortKmq/cViAnPTzx2LnFg84tNpWp4TZBFGQz\n" |
---|
437 | "+8yTnc4kmz75fS/jY2MMddj2gbICrsRhetPfHtXV/WVhJDP1H18GbtCFY2VVPe0a\n" |
---|
438 | "87VXE15/V8k1mE8McODmi3fipona8+/och3xWKE2rec1MKzKT0g6eXq8CrGCsyT7\n" |
---|
439 | "YdEIqUuyyOP7uWrat2DX9GgdT0Kj3jlN9K5W7edjcrsZCwenyO4KbXCeAvzhzffi\n" |
---|
440 | "7MA0BM0oNC9hkXL+nOmFg/+OTxIy7vKBg8P+OxtMb61zO7X8vC7CIAXFjvGDfRaD\n" |
---|
441 | "ssbzSibBsu/6iGtCOGEoXJf//////////wIBAgICAQA=\n" |
---|
442 | "-----END DH PARAMETERS-----\n"; |
---|
443 | const gnutls_datum_t default_dh_params = { |
---|
444 | (void *) FFDHE2048_PKCS3, |
---|
445 | sizeof(FFDHE2048_PKCS3) |
---|
446 | }; |
---|
447 | #endif |
---|
448 | |
---|
449 | |
---|
450 | |
---|
451 | /** |
---|
452 | * Configure the default DH groups to use for the given server. When |
---|
453 | * compiled against GnuTLS version 3.5.6 or newer the known DH group |
---|
454 | * matching the GnuTLS security parameter estimated from the private |
---|
455 | * key is used. Otherwise the ffdhe2048 DH group as defined in RFC |
---|
456 | * 7919, Appendix A.1 is the default. |
---|
457 | * |
---|
458 | * @param server the host to configure |
---|
459 | * |
---|
460 | * @return `OK` on success, `HTTP_UNAUTHORIZED` otherwise |
---|
461 | */ |
---|
462 | static int set_default_dh_param(server_rec *server) |
---|
463 | { |
---|
464 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
465 | ap_get_module_config(server->module_config, &gnutls_module); |
---|
466 | |
---|
467 | #ifdef HAVE_KNOWN_DH_GROUPS |
---|
468 | gnutls_sec_param_t seclevel = GNUTLS_SEC_PARAM_UNKNOWN; |
---|
469 | if (sc->privkey_x509) |
---|
470 | { |
---|
471 | seclevel = sec_param_from_privkey(server, sc->privkey_x509); |
---|
472 | ap_log_error(APLOG_MARK, APLOG_TRACE1, APR_SUCCESS, server, |
---|
473 | "%s: GnuTLS security param estimated based on " |
---|
474 | "private key '%s': %s", |
---|
475 | __func__, sc->x509_key_file, |
---|
476 | gnutls_sec_param_get_name(seclevel)); |
---|
477 | } |
---|
478 | |
---|
479 | if (seclevel == GNUTLS_SEC_PARAM_UNKNOWN) |
---|
480 | seclevel = GNUTLS_SEC_PARAM_MEDIUM; |
---|
481 | ap_log_error(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, server, |
---|
482 | "%s: Setting DH params for security level '%s'.", |
---|
483 | __func__, gnutls_sec_param_get_name(seclevel)); |
---|
484 | |
---|
485 | int ret = gnutls_certificate_set_known_dh_params(sc->certs, seclevel); |
---|
486 | if (ret < 0) |
---|
487 | { |
---|
488 | ap_log_error(APLOG_MARK, APLOG_EMERG, APR_EGENERAL, server, |
---|
489 | "%s: setting known DH params failed: %s (%d)", |
---|
490 | __func__, gnutls_strerror(ret), ret); |
---|
491 | return HTTP_UNAUTHORIZED; |
---|
492 | } |
---|
493 | ret = gnutls_anon_set_server_known_dh_params(sc->anon_creds, seclevel); |
---|
494 | if (ret < 0) |
---|
495 | { |
---|
496 | ap_log_error(APLOG_MARK, APLOG_EMERG, APR_EGENERAL, server, |
---|
497 | "%s: setting known DH params failed: %s (%d)", |
---|
498 | __func__, gnutls_strerror(ret), ret); |
---|
499 | return HTTP_UNAUTHORIZED; |
---|
500 | } |
---|
501 | #else |
---|
502 | int ret = gnutls_dh_params_init(&sc->dh_params); |
---|
503 | if (ret < 0) |
---|
504 | { |
---|
505 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, server, |
---|
506 | "%s: Failed to initialize DH params structure: " |
---|
507 | "%s (%d)", __func__, gnutls_strerror(ret), ret); |
---|
508 | return HTTP_UNAUTHORIZED; |
---|
509 | } |
---|
510 | ret = gnutls_dh_params_import_pkcs3(sc->dh_params, &default_dh_params, |
---|
511 | GNUTLS_X509_FMT_PEM); |
---|
512 | if (ret < 0) |
---|
513 | { |
---|
514 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, server, |
---|
515 | "%s: Failed to import default DH params: %s (%d)", |
---|
516 | __func__, gnutls_strerror(ret), ret); |
---|
517 | return HTTP_UNAUTHORIZED; |
---|
518 | } |
---|
519 | |
---|
520 | gnutls_certificate_set_dh_params(sc->certs, sc->dh_params); |
---|
521 | gnutls_anon_set_server_dh_params(sc->anon_creds, sc->dh_params); |
---|
522 | #endif |
---|
523 | |
---|
524 | return OK; |
---|
525 | } |
---|
526 | |
---|
527 | |
---|
528 | |
---|
529 | /** |
---|
530 | * Post config hook. |
---|
531 | * |
---|
532 | * Must return OK or DECLINED on success, something else on |
---|
533 | * error. These codes are defined in Apache httpd.h along with the |
---|
534 | * HTTP status codes, so I'm going to use HTTP error codes both for |
---|
535 | * fun (and to avoid conflicts). |
---|
536 | */ |
---|
537 | int mgs_hook_post_config(apr_pool_t *pconf, |
---|
538 | apr_pool_t *plog __attribute__((unused)), |
---|
539 | apr_pool_t *ptemp, |
---|
540 | server_rec *base_server) |
---|
541 | { |
---|
542 | int rv; |
---|
543 | server_rec *s; |
---|
544 | mgs_srvconf_rec *sc; |
---|
545 | mgs_srvconf_rec *sc_base; |
---|
546 | void *data = NULL; |
---|
547 | const char *userdata_key = "mgs_init"; |
---|
548 | |
---|
549 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
550 | |
---|
551 | apr_pool_userdata_get(&data, userdata_key, base_server->process->pool); |
---|
552 | if (data == NULL) { |
---|
553 | apr_pool_userdata_set((const void *) 1, userdata_key, apr_pool_cleanup_null, base_server->process->pool); |
---|
554 | } |
---|
555 | |
---|
556 | s = base_server; |
---|
557 | sc_base = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, &gnutls_module); |
---|
558 | |
---|
559 | |
---|
560 | rv = mgs_cache_post_config(pconf, ptemp, s, sc_base); |
---|
561 | if (rv != APR_SUCCESS) |
---|
562 | { |
---|
563 | ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, |
---|
564 | "Post config for cache failed."); |
---|
565 | return HTTP_INSUFFICIENT_STORAGE; |
---|
566 | } |
---|
567 | |
---|
568 | if (sc_base->ocsp_mutex == NULL) |
---|
569 | { |
---|
570 | rv = ap_global_mutex_create(&sc_base->ocsp_mutex, NULL, |
---|
571 | MGS_OCSP_MUTEX_NAME, NULL, |
---|
572 | base_server, pconf, 0); |
---|
573 | if (rv != APR_SUCCESS) |
---|
574 | return rv; |
---|
575 | } |
---|
576 | |
---|
577 | /* If GnuTLSP11Module is set, load the listed PKCS #11 |
---|
578 | * modules. Otherwise system defaults will be used. */ |
---|
579 | if (sc_base->p11_modules != NULL) |
---|
580 | { |
---|
581 | rv = gnutls_pkcs11_init(GNUTLS_PKCS11_FLAG_MANUAL, NULL); |
---|
582 | if (rv < 0) |
---|
583 | { |
---|
584 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
585 | "GnuTLS: Initializing PKCS #11 " |
---|
586 | "failed: %s (%d).", |
---|
587 | gnutls_strerror(rv), rv); |
---|
588 | } |
---|
589 | else |
---|
590 | { |
---|
591 | for (int i = 0; i < sc_base->p11_modules->nelts; i++) |
---|
592 | { |
---|
593 | char *p11_module = |
---|
594 | APR_ARRAY_IDX(sc_base->p11_modules, i, char *); |
---|
595 | rv = gnutls_pkcs11_add_provider(p11_module, NULL); |
---|
596 | if (rv != GNUTLS_E_SUCCESS) |
---|
597 | ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EGENERAL, s, |
---|
598 | "GnuTLS: Loading PKCS #11 provider module %s " |
---|
599 | "failed: %s (%d).", |
---|
600 | p11_module, gnutls_strerror(rv), rv); |
---|
601 | else |
---|
602 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, |
---|
603 | "%s: PKCS #11 provider module %s loaded.", |
---|
604 | __func__, p11_module); |
---|
605 | } |
---|
606 | } |
---|
607 | } |
---|
608 | |
---|
609 | sc_base->singleton_wd = |
---|
610 | mgs_new_singleton_watchdog(base_server, MGS_SINGLETON_WATCHDOG, pconf); |
---|
611 | |
---|
612 | for (s = base_server; s; s = s->next) |
---|
613 | { |
---|
614 | sc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, &gnutls_module); |
---|
615 | sc->cache_enable = sc_base->cache_enable; |
---|
616 | sc->cache = sc_base->cache; |
---|
617 | if (sc->cache_timeout == MGS_TIMEOUT_UNSET) |
---|
618 | sc->cache_timeout = sc_base->cache_timeout; |
---|
619 | sc->ocsp_cache = sc_base->ocsp_cache; |
---|
620 | |
---|
621 | sc->singleton_wd = sc_base->singleton_wd; |
---|
622 | |
---|
623 | /* defaults for unset values: */ |
---|
624 | if (sc->enabled == GNUTLS_ENABLED_UNSET) |
---|
625 | sc->enabled = GNUTLS_ENABLED_FALSE; |
---|
626 | if (sc->tickets == GNUTLS_ENABLED_UNSET) |
---|
627 | { |
---|
628 | /* GnuTLS 3.6.4 introduced automatic master key rotation */ |
---|
629 | if (gnutls_check_version_numeric(3, 6, 4)) |
---|
630 | sc->tickets = GNUTLS_ENABLED_TRUE; |
---|
631 | else |
---|
632 | sc->tickets = GNUTLS_ENABLED_FALSE; |
---|
633 | } |
---|
634 | if (sc->export_certificates_size < 0) |
---|
635 | sc->export_certificates_size = 0; |
---|
636 | if (sc->client_verify_mode == -1) |
---|
637 | sc->client_verify_mode = GNUTLS_CERT_IGNORE; |
---|
638 | if (sc->client_verify_method == mgs_cvm_unset) |
---|
639 | sc->client_verify_method = mgs_cvm_cartel; |
---|
640 | |
---|
641 | // TODO: None of the stuff below needs to be done if |
---|
642 | // sc->enabled == GNUTLS_ENABLED_FALSE, we could just continue |
---|
643 | // to the next host. |
---|
644 | |
---|
645 | /* Load certificates and stuff (includes parsing priority) */ |
---|
646 | rv = mgs_load_files(pconf, ptemp, s); |
---|
647 | if (rv != 0) { |
---|
648 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
649 | "%s: Loading credentials failed!", __func__); |
---|
650 | return HTTP_NOT_FOUND; |
---|
651 | } |
---|
652 | |
---|
653 | sc->ocsp_mutex = sc_base->ocsp_mutex; |
---|
654 | /* init OCSP configuration unless explicitly disabled */ |
---|
655 | if (sc->enabled && sc->ocsp_staple != GNUTLS_ENABLED_FALSE) |
---|
656 | { |
---|
657 | const char *err = mgs_ocsp_configure_stapling(pconf, ptemp, s); |
---|
658 | if (err != NULL) |
---|
659 | { |
---|
660 | /* If OCSP stapling is enabled only by default ignore |
---|
661 | * error and disable stapling */ |
---|
662 | if (sc->ocsp_staple == GNUTLS_ENABLED_UNSET) |
---|
663 | { |
---|
664 | ap_log_error(APLOG_MARK, APLOG_INFO, APR_SUCCESS, s, |
---|
665 | "Cannnot enable OCSP stapling for " |
---|
666 | "host '%s:%d': %s", |
---|
667 | s->server_hostname, s->addrs->host_port, err); |
---|
668 | sc->ocsp_staple = GNUTLS_ENABLED_FALSE; |
---|
669 | } |
---|
670 | /* If OCSP stapling is explicitly enabled this is a |
---|
671 | * critical error. */ |
---|
672 | else |
---|
673 | { |
---|
674 | ap_log_error(APLOG_MARK, APLOG_STARTUP, APR_EINVAL, s, |
---|
675 | "OCSP stapling configuration failed for " |
---|
676 | "host '%s:%d': %s", |
---|
677 | s->server_hostname, s->addrs->host_port, err); |
---|
678 | return HTTP_INTERNAL_SERVER_ERROR; |
---|
679 | } |
---|
680 | } |
---|
681 | else |
---|
682 | { |
---|
683 | /* Might already be set */ |
---|
684 | sc->ocsp_staple = GNUTLS_ENABLED_TRUE; |
---|
685 | /* Set up stapling */ |
---|
686 | rv = mgs_ocsp_enable_stapling(pconf, ptemp, s); |
---|
687 | if (rv != OK && rv != DECLINED) |
---|
688 | return rv; |
---|
689 | } |
---|
690 | } |
---|
691 | |
---|
692 | /* Check if the priorities have been set */ |
---|
693 | if (sc->priorities == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
694 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, |
---|
695 | "No GnuTLSPriorities directive for host '%s:%d', " |
---|
696 | "using default '%s'.", |
---|
697 | s->server_hostname, s->addrs->host_port, |
---|
698 | MGS_DEFAULT_PRIORITY); |
---|
699 | sc->priorities = default_prio; |
---|
700 | } |
---|
701 | |
---|
702 | /* Set host DH params from user configuration or defaults */ |
---|
703 | if (sc->dh_params != NULL) { |
---|
704 | gnutls_certificate_set_dh_params(sc->certs, sc->dh_params); |
---|
705 | gnutls_anon_set_server_dh_params(sc->anon_creds, sc->dh_params); |
---|
706 | } else { |
---|
707 | rv = set_default_dh_param(s); |
---|
708 | if (rv != OK) |
---|
709 | return rv; |
---|
710 | } |
---|
711 | |
---|
712 | /* The call after this comment is a workaround for bug in |
---|
713 | * gnutls_certificate_set_retrieve_function2 that ignores |
---|
714 | * supported certificate types. Should be fixed in GnuTLS |
---|
715 | * 3.3.12. |
---|
716 | * |
---|
717 | * Details: |
---|
718 | * https://lists.gnupg.org/pipermail/gnutls-devel/2015-January/007377.html |
---|
719 | * Workaround from: |
---|
720 | * https://github.com/vanrein/tlspool/commit/4938102d3d1b086491d147e6c8e4e2a02825fc12 */ |
---|
721 | #if GNUTLS_VERSION_NUMBER < 0x030312 |
---|
722 | gnutls_certificate_set_retrieve_function(sc->certs, (void *) exit); |
---|
723 | #endif |
---|
724 | |
---|
725 | gnutls_certificate_set_retrieve_function2(sc->certs, cert_retrieve_fn); |
---|
726 | |
---|
727 | if ((sc->certs_x509_chain == NULL || sc->certs_x509_chain_num < 1) && |
---|
728 | sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
729 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
730 | "GnuTLS: Host '%s:%d' is missing a Certificate File!", |
---|
731 | s->server_hostname, s->addrs->host_port); |
---|
732 | return HTTP_UNAUTHORIZED; |
---|
733 | } |
---|
734 | if (sc->enabled == GNUTLS_ENABLED_TRUE && |
---|
735 | (sc->certs_x509_chain_num > 0 && sc->privkey_x509 == NULL)) |
---|
736 | { |
---|
737 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
738 | "GnuTLS: Host '%s:%d' is missing a Private Key File!", |
---|
739 | s->server_hostname, s->addrs->host_port); |
---|
740 | return HTTP_UNAUTHORIZED; |
---|
741 | } |
---|
742 | |
---|
743 | if (sc->enabled == GNUTLS_ENABLED_TRUE |
---|
744 | && sc->proxy_enabled == GNUTLS_ENABLED_TRUE |
---|
745 | && load_proxy_x509_credentials(pconf, ptemp, s) != APR_SUCCESS) |
---|
746 | { |
---|
747 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
748 | "%s: loading proxy credentials for host " |
---|
749 | "'%s:%d' failed, exiting!", |
---|
750 | __func__, s->server_hostname, s->addrs->host_port); |
---|
751 | return HTTP_PROXY_AUTHENTICATION_REQUIRED; |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | |
---|
756 | ap_add_version_component(pconf, "mod_gnutls/" MOD_GNUTLS_VERSION); |
---|
757 | |
---|
758 | { |
---|
759 | const char* libvers = gnutls_check_version(NULL); |
---|
760 | char* gnutls_version = NULL; |
---|
761 | if(libvers && (gnutls_version = apr_psprintf(pconf, "GnuTLS/%s", libvers))) { |
---|
762 | ap_add_version_component(pconf, gnutls_version); |
---|
763 | } else { |
---|
764 | // In case we could not create the above string go for the static version instead |
---|
765 | ap_add_version_component(pconf, "GnuTLS/" GNUTLS_VERSION "-static"); |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | return OK; |
---|
770 | } |
---|
771 | |
---|
772 | void mgs_hook_child_init(apr_pool_t *p, server_rec *s) |
---|
773 | { |
---|
774 | apr_status_t rv = APR_SUCCESS; |
---|
775 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
776 | ap_get_module_config(s->module_config, &gnutls_module); |
---|
777 | |
---|
778 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
779 | |
---|
780 | /* if we use PKCS #11 reinitialize it */ |
---|
781 | if (mgs_pkcs11_reinit(s) < 0) { |
---|
782 | ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, |
---|
783 | "GnuTLS: Failed to reinitialize PKCS #11"); |
---|
784 | exit(-1); |
---|
785 | } |
---|
786 | |
---|
787 | if (sc->cache_enable == GNUTLS_ENABLED_TRUE) |
---|
788 | { |
---|
789 | rv = mgs_cache_child_init(p, s, sc->cache, MGS_CACHE_MUTEX_NAME); |
---|
790 | if (rv != APR_SUCCESS) |
---|
791 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
792 | "Child init for session cache failed!"); |
---|
793 | } |
---|
794 | |
---|
795 | if (sc->ocsp_cache != NULL) |
---|
796 | { |
---|
797 | rv = mgs_cache_child_init(p, s, sc->ocsp_cache, |
---|
798 | MGS_OCSP_CACHE_MUTEX_NAME); |
---|
799 | if (rv != APR_SUCCESS) |
---|
800 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
801 | "Child init for OCSP cache failed!"); |
---|
802 | } |
---|
803 | |
---|
804 | /* reinit OCSP request mutex */ |
---|
805 | const char *lockfile = apr_global_mutex_lockfile(sc->ocsp_mutex); |
---|
806 | rv = apr_global_mutex_child_init(&sc->ocsp_mutex, lockfile, p); |
---|
807 | if (rv != APR_SUCCESS) |
---|
808 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
809 | "Failed to reinit mutex '" MGS_OCSP_MUTEX_NAME "'."); |
---|
810 | |
---|
811 | /* Block SIGPIPE Signals */ |
---|
812 | rv = apr_signal_block(SIGPIPE); |
---|
813 | if(rv != APR_SUCCESS) { |
---|
814 | /* error sending output */ |
---|
815 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
816 | "GnuTLS: Error Blocking SIGPIPE Signal!"); |
---|
817 | } |
---|
818 | } |
---|
819 | |
---|
820 | const char *mgs_hook_http_scheme(const request_rec * r) { |
---|
821 | mgs_srvconf_rec *sc; |
---|
822 | |
---|
823 | if (r == NULL) |
---|
824 | return NULL; |
---|
825 | |
---|
826 | sc = (mgs_srvconf_rec *) ap_get_module_config(r-> |
---|
827 | server->module_config, |
---|
828 | &gnutls_module); |
---|
829 | |
---|
830 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
831 | if (sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
832 | return NULL; |
---|
833 | } |
---|
834 | |
---|
835 | return "https"; |
---|
836 | } |
---|
837 | |
---|
838 | apr_port_t mgs_hook_default_port(const request_rec * r) { |
---|
839 | mgs_srvconf_rec *sc; |
---|
840 | |
---|
841 | if (r == NULL) |
---|
842 | return 0; |
---|
843 | |
---|
844 | sc = (mgs_srvconf_rec *) ap_get_module_config(r-> |
---|
845 | server->module_config, |
---|
846 | &gnutls_module); |
---|
847 | |
---|
848 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
849 | if (sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
850 | return 0; |
---|
851 | } |
---|
852 | |
---|
853 | return 443; |
---|
854 | } |
---|
855 | |
---|
856 | /** |
---|
857 | * Default buffer size for SNI data, including the terminating NULL |
---|
858 | * byte. The size matches what gnutls-cli uses initially. |
---|
859 | */ |
---|
860 | #define DEFAULT_SNI_HOST_LEN 256 |
---|
861 | |
---|
862 | typedef struct { |
---|
863 | mgs_handle_t *ctxt; |
---|
864 | mgs_srvconf_rec *sc; |
---|
865 | const char *sni_name; |
---|
866 | } vhost_cb_rec; |
---|
867 | |
---|
868 | /** |
---|
869 | * Matches the current vhost's ServerAlias directives |
---|
870 | * |
---|
871 | * @param x vhost callback record |
---|
872 | * @param s server record |
---|
873 | * @param tsc mod_gnutls server data for `s` |
---|
874 | * |
---|
875 | * @return true if a match, false otherwise |
---|
876 | * |
---|
877 | */ |
---|
878 | int check_server_aliases(vhost_cb_rec *x, server_rec * s, mgs_srvconf_rec *tsc) |
---|
879 | { |
---|
880 | apr_array_header_t *names; |
---|
881 | int rv = 0; |
---|
882 | char ** name; |
---|
883 | |
---|
884 | /* Check ServerName First! */ |
---|
885 | if (strcasecmp(x->sni_name, s->server_hostname) == 0) { |
---|
886 | // We have a match, save this server configuration |
---|
887 | x->sc = tsc; |
---|
888 | rv = 1; |
---|
889 | /* Check any ServerAlias directives */ |
---|
890 | } else if(s->names->nelts) { |
---|
891 | names = s->names; |
---|
892 | name = (char **) names->elts; |
---|
893 | for (int i = 0; i < names->nelts; ++i) |
---|
894 | { |
---|
895 | if (!name[i]) |
---|
896 | continue; |
---|
897 | if (strcasecmp(x->sni_name, name[i]) == 0) |
---|
898 | { |
---|
899 | // We have a match, save this server configuration |
---|
900 | x->sc = tsc; |
---|
901 | rv = 1; |
---|
902 | } |
---|
903 | } |
---|
904 | /* ServerAlias directives may contain wildcards, check those last. */ |
---|
905 | } else if(s->wild_names->nelts) { |
---|
906 | names = s->wild_names; |
---|
907 | name = (char **) names->elts; |
---|
908 | for (int i = 0; i < names->nelts; ++i) |
---|
909 | { |
---|
910 | if (!name[i]) |
---|
911 | continue; |
---|
912 | if (ap_strcasecmp_match(x->sni_name, name[i]) == 0) |
---|
913 | { |
---|
914 | x->sc = tsc; |
---|
915 | rv = 1; |
---|
916 | } |
---|
917 | } |
---|
918 | } |
---|
919 | return rv; |
---|
920 | } |
---|
921 | |
---|
922 | static int vhost_cb(void *baton, conn_rec *conn, server_rec * s) |
---|
923 | { |
---|
924 | mgs_srvconf_rec *tsc; |
---|
925 | vhost_cb_rec *x = baton; |
---|
926 | int ret; |
---|
927 | |
---|
928 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
929 | tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
930 | &gnutls_module); |
---|
931 | |
---|
932 | if (tsc->enabled != GNUTLS_ENABLED_TRUE) { |
---|
933 | return 0; |
---|
934 | } |
---|
935 | |
---|
936 | if (tsc->certs_x509_chain_num > 0) { |
---|
937 | /* this check is there to warn administrator of any missing hostname |
---|
938 | * in the certificate. */ |
---|
939 | ret = gnutls_x509_crt_check_hostname(tsc->certs_x509_crt_chain[0], s->server_hostname); |
---|
940 | if (0 == ret) |
---|
941 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, conn, |
---|
942 | "GnuTLS: the certificate doesn't match requested " |
---|
943 | "hostname '%s'", s->server_hostname); |
---|
944 | } else { |
---|
945 | ap_log_cerror(APLOG_MARK, APLOG_INFO, 0, conn, |
---|
946 | "GnuTLS: SNI request for '%s' but no X.509 certs " |
---|
947 | "available at all", |
---|
948 | s->server_hostname); |
---|
949 | } |
---|
950 | return check_server_aliases(x, s, tsc); |
---|
951 | } |
---|
952 | |
---|
953 | /** |
---|
954 | * Get SNI data from GnuTLS (if any) and search for a matching virtual |
---|
955 | * host configuration. This method is called from the post client |
---|
956 | * hello function. |
---|
957 | * |
---|
958 | * @param session the GnuTLS session |
---|
959 | * |
---|
960 | * @return either the matching mod_gnutls server config, or `NULL` |
---|
961 | */ |
---|
962 | mgs_srvconf_rec *mgs_find_sni_server(gnutls_session_t session) |
---|
963 | { |
---|
964 | mgs_handle_t *ctxt = gnutls_session_get_ptr(session); |
---|
965 | |
---|
966 | char *sni_name = apr_palloc(ctxt->c->pool, DEFAULT_SNI_HOST_LEN); |
---|
967 | size_t sni_len = DEFAULT_SNI_HOST_LEN; |
---|
968 | unsigned int sni_type; |
---|
969 | |
---|
970 | /* Search for a DNS SNI element. Note that RFC 6066 prohibits more |
---|
971 | * than one server name per type. */ |
---|
972 | int sni_index = -1; |
---|
973 | int rv = 0; |
---|
974 | do { |
---|
975 | /* The sni_index is incremented before each use, so if the |
---|
976 | * loop terminates with a type match we will have the right |
---|
977 | * one stored. */ |
---|
978 | rv = gnutls_server_name_get(session, sni_name, |
---|
979 | &sni_len, &sni_type, ++sni_index); |
---|
980 | if (rv == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) |
---|
981 | { |
---|
982 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, APR_EGENERAL, ctxt->c, |
---|
983 | "%s: no DNS SNI found (last index: %d).", |
---|
984 | __func__, sni_index); |
---|
985 | return NULL; |
---|
986 | } |
---|
987 | } while (sni_type != GNUTLS_NAME_DNS); |
---|
988 | /* The (rv == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) path inside |
---|
989 | * the loop above returns, so if we reach this point we have a DNS |
---|
990 | * SNI at the current index. */ |
---|
991 | |
---|
992 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER) |
---|
993 | { |
---|
994 | /* Allocate a new buffer of the right size and retry */ |
---|
995 | sni_name = apr_palloc(ctxt->c->pool, sni_len); |
---|
996 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, APR_SUCCESS, ctxt->c, |
---|
997 | "%s: reallocated SNI data buffer for %" APR_SIZE_T_FMT |
---|
998 | " bytes.", __func__, sni_len); |
---|
999 | rv = gnutls_server_name_get(session, sni_name, |
---|
1000 | &sni_len, &sni_type, sni_index); |
---|
1001 | } |
---|
1002 | |
---|
1003 | /* Unless there's a bug in the GnuTLS API only GNUTLS_E_IDNA_ERROR |
---|
1004 | * can occur here, but a catch all is safer and no more |
---|
1005 | * complicated. */ |
---|
1006 | if (rv != GNUTLS_E_SUCCESS) |
---|
1007 | { |
---|
1008 | ap_log_cerror(APLOG_MARK, APLOG_INFO, APR_EGENERAL, ctxt->c, |
---|
1009 | "%s: error while getting SNI DNS data: '%s' (%d).", |
---|
1010 | __func__, gnutls_strerror(rv), rv); |
---|
1011 | return NULL; |
---|
1012 | } |
---|
1013 | |
---|
1014 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, APR_SUCCESS, ctxt->c, |
---|
1015 | "%s: client requested server '%s'.", |
---|
1016 | __func__, sni_name); |
---|
1017 | ctxt->sni_name = sni_name; |
---|
1018 | |
---|
1019 | /* Search for vhosts matching connection parameters and the |
---|
1020 | * SNI. If a match is found, cbx.sc will contain the mod_gnutls |
---|
1021 | * server config for the vhost. */ |
---|
1022 | vhost_cb_rec cbx = { |
---|
1023 | .ctxt = ctxt, |
---|
1024 | .sc = NULL, |
---|
1025 | .sni_name = sni_name |
---|
1026 | }; |
---|
1027 | rv = ap_vhost_iterate_given_conn(ctxt->c, vhost_cb, &cbx); |
---|
1028 | if (rv == 1) { |
---|
1029 | return cbx.sc; |
---|
1030 | } |
---|
1031 | return NULL; |
---|
1032 | } |
---|
1033 | |
---|
1034 | /** |
---|
1035 | * This function is intended as a cleanup handler for connections |
---|
1036 | * using GnuTLS. If attached to the connection pool, it ensures that |
---|
1037 | * session resources are released with the connection pool even if the |
---|
1038 | * session wasn't terminated properly. |
---|
1039 | * |
---|
1040 | * @param data must point to the mgs_handle_t associated with the |
---|
1041 | * connection |
---|
1042 | */ |
---|
1043 | static apr_status_t cleanup_gnutls_session(void *data) |
---|
1044 | { |
---|
1045 | /* nothing to do */ |
---|
1046 | if (data == NULL) |
---|
1047 | return APR_SUCCESS; |
---|
1048 | |
---|
1049 | /* check if session needs closing */ |
---|
1050 | mgs_handle_t *ctxt = (mgs_handle_t *) data; |
---|
1051 | if (ctxt->session != NULL) |
---|
1052 | { |
---|
1053 | ap_log_cerror(APLOG_MARK, APLOG_WARNING, APR_ECONNABORTED, ctxt->c, |
---|
1054 | "%s: connection pool cleanup in progress but %sTLS " |
---|
1055 | "session hasn't been terminated, trying to close", |
---|
1056 | __func__, IS_PROXY_STR(ctxt)); |
---|
1057 | int ret; |
---|
1058 | /* Try A Clean Shutdown */ |
---|
1059 | do |
---|
1060 | ret = gnutls_bye(ctxt->session, GNUTLS_SHUT_WR); |
---|
1061 | while (ret == GNUTLS_E_INTERRUPTED || ret == GNUTLS_E_AGAIN); |
---|
1062 | if (ret != GNUTLS_E_SUCCESS) |
---|
1063 | ap_log_cerror(APLOG_MARK, APLOG_INFO, APR_EGENERAL, ctxt->c, |
---|
1064 | "%s: error while closing TLS %sconnection: %s (%d)", |
---|
1065 | __func__, IS_PROXY_STR(ctxt), |
---|
1066 | gnutls_strerror(ret), ret); |
---|
1067 | else |
---|
1068 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, APR_SUCCESS, ctxt->c, |
---|
1069 | "%s: TLS %sconnection closed.", |
---|
1070 | __func__, IS_PROXY_STR(ctxt)); |
---|
1071 | /* De-Initialize Session */ |
---|
1072 | gnutls_deinit(ctxt->session); |
---|
1073 | ctxt->session = NULL; |
---|
1074 | } |
---|
1075 | return APR_SUCCESS; |
---|
1076 | } |
---|
1077 | |
---|
1078 | static void create_gnutls_handle(conn_rec * c) |
---|
1079 | { |
---|
1080 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1081 | |
---|
1082 | /* Get connection specific configuration */ |
---|
1083 | mgs_handle_t *ctxt = init_gnutls_ctxt(c); |
---|
1084 | ctxt->enabled = GNUTLS_ENABLED_TRUE; |
---|
1085 | ctxt->status = 0; |
---|
1086 | ctxt->input_rc = APR_SUCCESS; |
---|
1087 | ctxt->input_bb = apr_brigade_create(c->pool, c->bucket_alloc); |
---|
1088 | ctxt->input_cbuf.length = 0; |
---|
1089 | ctxt->output_rc = APR_SUCCESS; |
---|
1090 | ctxt->output_bb = apr_brigade_create(c->pool, c->bucket_alloc); |
---|
1091 | ctxt->output_blen = 0; |
---|
1092 | ctxt->output_length = 0; |
---|
1093 | |
---|
1094 | /* Initialize GnuTLS Library */ |
---|
1095 | int err = 0; |
---|
1096 | if (ctxt->is_proxy == GNUTLS_ENABLED_TRUE) |
---|
1097 | { |
---|
1098 | /* this is an outgoing proxy connection, client mode */ |
---|
1099 | err = gnutls_init(&ctxt->session, GNUTLS_CLIENT); |
---|
1100 | if (err != GNUTLS_E_SUCCESS) |
---|
1101 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, |
---|
1102 | "gnutls_init for proxy connection failed: %s (%d)", |
---|
1103 | gnutls_strerror(err), err); |
---|
1104 | } |
---|
1105 | else |
---|
1106 | { |
---|
1107 | /* incoming connection, server mode */ |
---|
1108 | err = gnutls_init(&ctxt->session, GNUTLS_SERVER); |
---|
1109 | if (err != GNUTLS_E_SUCCESS) |
---|
1110 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, |
---|
1111 | "gnutls_init for server side failed: %s (%d)", |
---|
1112 | gnutls_strerror(err), err); |
---|
1113 | } |
---|
1114 | |
---|
1115 | /* Ensure TLS session resources are released when the connection |
---|
1116 | * pool is cleared, if the filters haven't done that already. */ |
---|
1117 | apr_pool_pre_cleanup_register(c->pool, ctxt, cleanup_gnutls_session); |
---|
1118 | |
---|
1119 | /* Set Default Priority */ |
---|
1120 | err = gnutls_priority_set(ctxt->session, default_prio); |
---|
1121 | if (err != GNUTLS_E_SUCCESS) |
---|
1122 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, |
---|
1123 | "gnutls_priority_set failed!"); |
---|
1124 | /* Set Handshake function */ |
---|
1125 | gnutls_handshake_set_post_client_hello_function(ctxt->session, |
---|
1126 | mgs_select_virtual_server_cb); |
---|
1127 | |
---|
1128 | /* Set GnuTLS user pointer, so we can access the module session |
---|
1129 | * context in GnuTLS callbacks */ |
---|
1130 | gnutls_session_set_ptr(ctxt->session, ctxt); |
---|
1131 | |
---|
1132 | /* If mod_gnutls is the TLS server, mgs_select_virtual_server_cb |
---|
1133 | * will load appropriate credentials during handshake. However, |
---|
1134 | * when handling a proxy backend connection, mod_gnutls acts as |
---|
1135 | * TLS client and credentials must be loaded here. */ |
---|
1136 | if (ctxt->is_proxy == GNUTLS_ENABLED_TRUE) |
---|
1137 | { |
---|
1138 | /* Set anonymous client credentials for proxy connections */ |
---|
1139 | gnutls_credentials_set(ctxt->session, GNUTLS_CRD_ANON, |
---|
1140 | ctxt->sc->anon_client_creds); |
---|
1141 | /* Set x509 credentials */ |
---|
1142 | gnutls_credentials_set(ctxt->session, GNUTLS_CRD_CERTIFICATE, |
---|
1143 | ctxt->sc->proxy_x509_creds); |
---|
1144 | /* Load priorities from the server configuration */ |
---|
1145 | err = gnutls_priority_set(ctxt->session, ctxt->sc->proxy_priorities); |
---|
1146 | if (err != GNUTLS_E_SUCCESS) |
---|
1147 | ap_log_cerror(APLOG_MARK, APLOG_ERR, err, c, |
---|
1148 | "%s: setting priorities for proxy connection " |
---|
1149 | "failed: %s (%d)", |
---|
1150 | __func__, gnutls_strerror(err), err); |
---|
1151 | } |
---|
1152 | |
---|
1153 | prepare_alpn_proposals(ctxt); |
---|
1154 | |
---|
1155 | /* Initialize Session Cache */ |
---|
1156 | mgs_cache_session_init(ctxt); |
---|
1157 | |
---|
1158 | /* Set pull, push & ptr functions */ |
---|
1159 | gnutls_transport_set_pull_function(ctxt->session, |
---|
1160 | mgs_transport_read); |
---|
1161 | gnutls_transport_set_push_function(ctxt->session, |
---|
1162 | mgs_transport_write); |
---|
1163 | gnutls_transport_set_ptr(ctxt->session, ctxt); |
---|
1164 | /* Add IO filters */ |
---|
1165 | ctxt->input_filter = ap_add_input_filter(GNUTLS_INPUT_FILTER_NAME, |
---|
1166 | ctxt, NULL, c); |
---|
1167 | ctxt->output_filter = ap_add_output_filter(GNUTLS_OUTPUT_FILTER_NAME, |
---|
1168 | ctxt, NULL, c); |
---|
1169 | } |
---|
1170 | |
---|
1171 | int mgs_hook_pre_connection(conn_rec * c, void *csd __attribute__((unused))) |
---|
1172 | { |
---|
1173 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1174 | |
---|
1175 | if (c->master) |
---|
1176 | { |
---|
1177 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, c, |
---|
1178 | "%s declined secondary connection", __func__); |
---|
1179 | return DECLINED; |
---|
1180 | } |
---|
1181 | |
---|
1182 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
1183 | ap_get_module_config(c->base_server->module_config, &gnutls_module); |
---|
1184 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
1185 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
1186 | |
---|
1187 | if ((sc && (!sc->enabled)) |
---|
1188 | || (ctxt && ctxt->enabled == GNUTLS_ENABLED_FALSE)) |
---|
1189 | { |
---|
1190 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c, "%s declined connection", |
---|
1191 | __func__); |
---|
1192 | return DECLINED; |
---|
1193 | } |
---|
1194 | |
---|
1195 | create_gnutls_handle(c); |
---|
1196 | return OK; |
---|
1197 | } |
---|
1198 | |
---|
1199 | |
---|
1200 | |
---|
1201 | /** |
---|
1202 | * process_connection hook: Do a zero byte read to trigger the |
---|
1203 | * handshake. Doesn't change anything for traditional protocols that |
---|
1204 | * just do reads, but HTTP/2 needs the TLS handshake and ALPN to |
---|
1205 | * happen before its process_connection hook runs. |
---|
1206 | */ |
---|
1207 | int mgs_hook_process_connection(conn_rec* c) |
---|
1208 | { |
---|
1209 | mgs_handle_t *ctxt = (mgs_handle_t *) |
---|
1210 | ap_get_module_config(c->conn_config, &gnutls_module); |
---|
1211 | |
---|
1212 | if ((ctxt != NULL) && (ctxt->enabled == GNUTLS_ENABLED_TRUE)) |
---|
1213 | { |
---|
1214 | /* This connection is supposed to use TLS. Give the filters a |
---|
1215 | * kick with a zero byte read to trigger the handshake. */ |
---|
1216 | apr_bucket_brigade* temp = |
---|
1217 | apr_brigade_create(c->pool, c->bucket_alloc); |
---|
1218 | ap_get_brigade(c->input_filters, temp, |
---|
1219 | AP_MODE_INIT, APR_BLOCK_READ, 0); |
---|
1220 | apr_brigade_destroy(temp); |
---|
1221 | } |
---|
1222 | return DECLINED; |
---|
1223 | } |
---|
1224 | |
---|
1225 | |
---|
1226 | |
---|
1227 | /* Post request hook, checks if TLS connection and vhost match */ |
---|
1228 | int mgs_req_vhost_check(request_rec *r) |
---|
1229 | { |
---|
1230 | /* mod_gnutls server record for the request vhost */ |
---|
1231 | mgs_srvconf_rec *r_sc = (mgs_srvconf_rec *) |
---|
1232 | ap_get_module_config(r->server->module_config, &gnutls_module); |
---|
1233 | mgs_handle_t *ctxt = get_effective_gnutls_ctxt(r->connection); |
---|
1234 | |
---|
1235 | /* Nothing to check for non-TLS and outgoing proxy connections */ |
---|
1236 | if (ctxt == NULL || !ctxt->enabled || ctxt->is_proxy) |
---|
1237 | return DECLINED; |
---|
1238 | |
---|
1239 | if (ctxt->sc != r_sc) |
---|
1240 | { |
---|
1241 | ap_log_cerror(APLOG_MARK, APLOG_ERR, APR_SUCCESS, ctxt->c, |
---|
1242 | "%s: Mismatch between handshake and request servers!", |
---|
1243 | __func__); |
---|
1244 | return HTTP_MISDIRECTED_REQUEST; |
---|
1245 | } |
---|
1246 | |
---|
1247 | if (!ctxt->sni_name) |
---|
1248 | return DECLINED; |
---|
1249 | |
---|
1250 | /* Got an SNI name, so verify it matches. */ |
---|
1251 | ap_log_cerror(APLOG_MARK, APLOG_TRACE1, APR_SUCCESS, ctxt->c, |
---|
1252 | "%s: Checking request hostname against SNI name '%s'.", |
---|
1253 | __func__, ctxt->sni_name); |
---|
1254 | |
---|
1255 | if (!r->hostname) |
---|
1256 | { |
---|
1257 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, r->connection, |
---|
1258 | "Client requested '%s' via SNI, but provided " |
---|
1259 | "no hostname in HTTP request!", ctxt->sni_name); |
---|
1260 | return HTTP_MISDIRECTED_REQUEST; |
---|
1261 | } |
---|
1262 | |
---|
1263 | if (strcasecmp(r->hostname, ctxt->sni_name) != 0) |
---|
1264 | { |
---|
1265 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, r->connection, |
---|
1266 | "Client requested '%s' via SNI, but '%s' in " |
---|
1267 | "the HTTP request!", ctxt->sni_name, r->hostname); |
---|
1268 | return HTTP_MISDIRECTED_REQUEST; |
---|
1269 | } |
---|
1270 | |
---|
1271 | return DECLINED; |
---|
1272 | } |
---|
1273 | |
---|
1274 | |
---|
1275 | |
---|
1276 | int mgs_hook_fixups(request_rec * r) { |
---|
1277 | unsigned char sbuf[GNUTLS_MAX_SESSION_ID]; |
---|
1278 | const char *tmp; |
---|
1279 | size_t len; |
---|
1280 | mgs_handle_t *ctxt; |
---|
1281 | int rv = OK; |
---|
1282 | |
---|
1283 | if (r == NULL) |
---|
1284 | return DECLINED; |
---|
1285 | |
---|
1286 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1287 | apr_table_t *env = r->subprocess_env; |
---|
1288 | |
---|
1289 | ctxt = get_effective_gnutls_ctxt(r->connection); |
---|
1290 | |
---|
1291 | if (!ctxt || ctxt->enabled != GNUTLS_ENABLED_TRUE || ctxt->session == NULL) |
---|
1292 | { |
---|
1293 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, "request declined in %s", __func__); |
---|
1294 | return DECLINED; |
---|
1295 | } |
---|
1296 | |
---|
1297 | apr_table_setn(env, "HTTPS", "on"); |
---|
1298 | |
---|
1299 | apr_table_setn(env, "SSL_VERSION_LIBRARY", |
---|
1300 | "GnuTLS/" LIBGNUTLS_VERSION); |
---|
1301 | apr_table_setn(env, "SSL_VERSION_INTERFACE", |
---|
1302 | "mod_gnutls/" MOD_GNUTLS_VERSION); |
---|
1303 | |
---|
1304 | apr_table_setn(env, "SSL_PROTOCOL", |
---|
1305 | gnutls_protocol_get_name(gnutls_protocol_get_version(ctxt->session))); |
---|
1306 | |
---|
1307 | /* should have been called SSL_CIPHERSUITE instead */ |
---|
1308 | apr_table_setn(env, "SSL_CIPHER", |
---|
1309 | gnutls_cipher_suite_get_name(gnutls_kx_get(ctxt->session), |
---|
1310 | gnutls_cipher_get(ctxt->session), |
---|
1311 | gnutls_mac_get(ctxt->session))); |
---|
1312 | |
---|
1313 | #if GNUTLS_VERSION_NUMBER >= 0x030600 |
---|
1314 | /* Compression support has been removed since GnuTLS 3.6.0 */ |
---|
1315 | apr_table_setn(env, "SSL_COMPRESS_METHOD", "NULL"); |
---|
1316 | #else |
---|
1317 | apr_table_setn(env, "SSL_COMPRESS_METHOD", |
---|
1318 | gnutls_compression_get_name(gnutls_compression_get(ctxt->session))); |
---|
1319 | #endif |
---|
1320 | |
---|
1321 | #ifdef ENABLE_SRP |
---|
1322 | if (ctxt->sc->srp_tpasswd_conf_file != NULL && ctxt->sc->srp_tpasswd_file != NULL) { |
---|
1323 | tmp = gnutls_srp_server_get_username(ctxt->session); |
---|
1324 | apr_table_setn(env, "SSL_SRP_USER", (tmp != NULL) ? tmp : ""); |
---|
1325 | } else { |
---|
1326 | apr_table_unset(env, "SSL_SRP_USER"); |
---|
1327 | } |
---|
1328 | #endif |
---|
1329 | |
---|
1330 | if (apr_table_get(env, "SSL_CLIENT_VERIFY") == NULL) |
---|
1331 | apr_table_setn(env, "SSL_CLIENT_VERIFY", "NONE"); |
---|
1332 | |
---|
1333 | unsigned int key_size = 8 * gnutls_cipher_get_key_size(gnutls_cipher_get(ctxt->session)); |
---|
1334 | tmp = apr_psprintf(r->pool, "%u", key_size); |
---|
1335 | |
---|
1336 | apr_table_setn(env, "SSL_CIPHER_USEKEYSIZE", tmp); |
---|
1337 | |
---|
1338 | apr_table_setn(env, "SSL_CIPHER_ALGKEYSIZE", tmp); |
---|
1339 | |
---|
1340 | apr_table_setn(env, "SSL_CIPHER_EXPORT", |
---|
1341 | (key_size <= 40) ? "true" : "false"); |
---|
1342 | |
---|
1343 | int dhsize = gnutls_dh_get_prime_bits(ctxt->session); |
---|
1344 | if (dhsize > 0) { |
---|
1345 | tmp = apr_psprintf(r->pool, "%d", dhsize); |
---|
1346 | apr_table_setn(env, "SSL_DH_PRIME_BITS", tmp); |
---|
1347 | } |
---|
1348 | |
---|
1349 | len = sizeof (sbuf); |
---|
1350 | gnutls_session_get_id(ctxt->session, sbuf, &len); |
---|
1351 | apr_table_setn(env, "SSL_SESSION_ID", |
---|
1352 | apr_pescape_hex(r->pool, sbuf, len, 0)); |
---|
1353 | |
---|
1354 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
1355 | mgs_add_common_cert_vars(r, ctxt->sc->certs_x509_crt_chain[0], 0, |
---|
1356 | ctxt->sc->export_certificates_size); |
---|
1357 | } |
---|
1358 | |
---|
1359 | return rv; |
---|
1360 | } |
---|
1361 | |
---|
1362 | int mgs_hook_authz(request_rec * r) { |
---|
1363 | int rv; |
---|
1364 | mgs_handle_t *ctxt; |
---|
1365 | mgs_dirconf_rec *dc; |
---|
1366 | |
---|
1367 | if (r == NULL) |
---|
1368 | return DECLINED; |
---|
1369 | |
---|
1370 | dc = ap_get_module_config(r->per_dir_config, &gnutls_module); |
---|
1371 | |
---|
1372 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1373 | ctxt = get_effective_gnutls_ctxt(r->connection); |
---|
1374 | |
---|
1375 | if (!ctxt || ctxt->session == NULL) { |
---|
1376 | return DECLINED; |
---|
1377 | } |
---|
1378 | |
---|
1379 | if (dc->client_verify_mode == GNUTLS_CERT_IGNORE) { |
---|
1380 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1381 | "GnuTLS: Directory set to Ignore Client Certificate!"); |
---|
1382 | } else { |
---|
1383 | if (ctxt->sc->client_verify_mode < dc->client_verify_mode) { |
---|
1384 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1385 | "GnuTLS: Attempting to rehandshake with peer. %d %d", |
---|
1386 | ctxt->sc->client_verify_mode, |
---|
1387 | dc->client_verify_mode); |
---|
1388 | |
---|
1389 | /* If we already have a client certificate, there's no point in |
---|
1390 | * re-handshaking... */ |
---|
1391 | rv = mgs_cert_verify(r, ctxt); |
---|
1392 | if (rv != DECLINED && rv != HTTP_FORBIDDEN) |
---|
1393 | return rv; |
---|
1394 | |
---|
1395 | gnutls_certificate_server_set_request |
---|
1396 | (ctxt->session, dc->client_verify_mode); |
---|
1397 | |
---|
1398 | if (mgs_rehandshake(ctxt) != 0) { |
---|
1399 | return HTTP_FORBIDDEN; |
---|
1400 | } |
---|
1401 | } else if (ctxt->sc->client_verify_mode == |
---|
1402 | GNUTLS_CERT_IGNORE) { |
---|
1403 | #if MOD_GNUTLS_DEBUG |
---|
1404 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1405 | "GnuTLS: Peer is set to IGNORE"); |
---|
1406 | #endif |
---|
1407 | return DECLINED; |
---|
1408 | } |
---|
1409 | rv = mgs_cert_verify(r, ctxt); |
---|
1410 | if (rv != DECLINED |
---|
1411 | && (rv != HTTP_FORBIDDEN |
---|
1412 | || dc->client_verify_mode == GNUTLS_CERT_REQUIRE |
---|
1413 | || (dc->client_verify_mode == -1 |
---|
1414 | && ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUIRE))) |
---|
1415 | { |
---|
1416 | return rv; |
---|
1417 | } |
---|
1418 | } |
---|
1419 | |
---|
1420 | return DECLINED; |
---|
1421 | } |
---|
1422 | |
---|
1423 | /* variables that are not sent by default: |
---|
1424 | * |
---|
1425 | * SSL_CLIENT_CERT string PEM-encoded client certificate |
---|
1426 | * SSL_SERVER_CERT string PEM-encoded client certificate |
---|
1427 | */ |
---|
1428 | |
---|
1429 | /* @param side is either 0 for SERVER or 1 for CLIENT |
---|
1430 | * |
---|
1431 | * @param export_cert_size (int) maximum size for environment variable |
---|
1432 | * to use for the PEM-encoded certificate (0 means do not export) |
---|
1433 | */ |
---|
1434 | #define MGS_SIDE(suffix) ((side==0) ? "SSL_SERVER" suffix : "SSL_CLIENT" suffix) |
---|
1435 | |
---|
1436 | static void mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, int side, size_t export_cert_size) { |
---|
1437 | unsigned char sbuf[64]; /* buffer to hold serials */ |
---|
1438 | char buf[AP_IOBUFSIZE]; |
---|
1439 | const char *tmp; |
---|
1440 | char *tmp2; |
---|
1441 | size_t len; |
---|
1442 | int ret; |
---|
1443 | |
---|
1444 | if (r == NULL) |
---|
1445 | return; |
---|
1446 | |
---|
1447 | apr_table_t *env = r->subprocess_env; |
---|
1448 | |
---|
1449 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1450 | if (export_cert_size > 0) { |
---|
1451 | len = 0; |
---|
1452 | ret = gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, NULL, &len); |
---|
1453 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER) { |
---|
1454 | if (len >= export_cert_size) { |
---|
1455 | apr_table_setn(env, MGS_SIDE("_CERT"), "GNUTLS_CERTIFICATE_SIZE_LIMIT_EXCEEDED"); |
---|
1456 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1457 | "GnuTLS: Failed to export too-large X.509 certificate to environment"); |
---|
1458 | } else { |
---|
1459 | char* cert_buf = apr_palloc(r->pool, len + 1); |
---|
1460 | if (cert_buf != NULL && gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_PEM, cert_buf, &len) >= 0) { |
---|
1461 | cert_buf[len] = 0; |
---|
1462 | apr_table_setn(env, MGS_SIDE("_CERT"), cert_buf); |
---|
1463 | } else { |
---|
1464 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, |
---|
1465 | "GnuTLS: failed to export X.509 certificate"); |
---|
1466 | } |
---|
1467 | } |
---|
1468 | } else { |
---|
1469 | ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, |
---|
1470 | "GnuTLS: dazed and confused about X.509 certificate size"); |
---|
1471 | } |
---|
1472 | } |
---|
1473 | |
---|
1474 | len = sizeof (buf); |
---|
1475 | gnutls_x509_crt_get_dn(cert, buf, &len); |
---|
1476 | apr_table_setn(env, MGS_SIDE("_S_DN"), apr_pstrmemdup(r->pool, buf, len)); |
---|
1477 | |
---|
1478 | len = sizeof (buf); |
---|
1479 | gnutls_x509_crt_get_issuer_dn(cert, buf, &len); |
---|
1480 | apr_table_setn(env, MGS_SIDE("_I_DN"), apr_pstrmemdup(r->pool, buf, len)); |
---|
1481 | |
---|
1482 | len = sizeof (sbuf); |
---|
1483 | gnutls_x509_crt_get_serial(cert, sbuf, &len); |
---|
1484 | apr_table_setn(env, MGS_SIDE("_M_SERIAL"), |
---|
1485 | apr_pescape_hex(r->pool, sbuf, len, 0)); |
---|
1486 | |
---|
1487 | ret = gnutls_x509_crt_get_version(cert); |
---|
1488 | if (ret > 0) |
---|
1489 | apr_table_setn(env, MGS_SIDE("_M_VERSION"), |
---|
1490 | apr_psprintf(r->pool, "%u", ret)); |
---|
1491 | |
---|
1492 | apr_table_setn(env, MGS_SIDE("_CERT_TYPE"), "X.509"); |
---|
1493 | |
---|
1494 | tmp = |
---|
1495 | mgs_time2sz(gnutls_x509_crt_get_expiration_time |
---|
1496 | (cert), buf, sizeof (buf)); |
---|
1497 | apr_table_setn(env, MGS_SIDE("_V_END"), apr_pstrdup(r->pool, tmp)); |
---|
1498 | |
---|
1499 | tmp = |
---|
1500 | mgs_time2sz(gnutls_x509_crt_get_activation_time |
---|
1501 | (cert), buf, sizeof (buf)); |
---|
1502 | apr_table_setn(env, MGS_SIDE("_V_START"), apr_pstrdup(r->pool, tmp)); |
---|
1503 | |
---|
1504 | ret = gnutls_x509_crt_get_signature_algorithm(cert); |
---|
1505 | if (ret >= 0) { |
---|
1506 | apr_table_setn(env, MGS_SIDE("_A_SIG"), |
---|
1507 | gnutls_sign_algorithm_get_name(ret)); |
---|
1508 | } |
---|
1509 | |
---|
1510 | ret = gnutls_x509_crt_get_pk_algorithm(cert, NULL); |
---|
1511 | if (ret >= 0) { |
---|
1512 | apr_table_setn(env, MGS_SIDE("_A_KEY"), |
---|
1513 | gnutls_pk_algorithm_get_name(ret)); |
---|
1514 | } |
---|
1515 | |
---|
1516 | /* export all the alternative names (DNS, RFC822 and URI) */ |
---|
1517 | for (int i = 0; !(ret < 0); i++) |
---|
1518 | { |
---|
1519 | const char *san, *sanlabel; |
---|
1520 | len = 0; |
---|
1521 | ret = gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
1522 | NULL, &len, |
---|
1523 | NULL); |
---|
1524 | |
---|
1525 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER && len > 1) { |
---|
1526 | tmp2 = apr_palloc(r->pool, len + 1); |
---|
1527 | |
---|
1528 | ret = |
---|
1529 | gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
1530 | tmp2, |
---|
1531 | &len, |
---|
1532 | NULL); |
---|
1533 | tmp2[len] = 0; |
---|
1534 | |
---|
1535 | sanlabel = apr_psprintf(r->pool, "%s%u", MGS_SIDE("_S_AN"), i); |
---|
1536 | if (ret == GNUTLS_SAN_DNSNAME) { |
---|
1537 | san = apr_psprintf(r->pool, "DNSNAME:%s", tmp2); |
---|
1538 | } else if (ret == GNUTLS_SAN_RFC822NAME) { |
---|
1539 | san = apr_psprintf(r->pool, "RFC822NAME:%s", tmp2); |
---|
1540 | } else if (ret == GNUTLS_SAN_URI) { |
---|
1541 | san = apr_psprintf(r->pool, "URI:%s", tmp2); |
---|
1542 | } else { |
---|
1543 | san = "UNSUPPORTED"; |
---|
1544 | } |
---|
1545 | apr_table_setn(env, sanlabel, san); |
---|
1546 | } |
---|
1547 | } |
---|
1548 | } |
---|
1549 | |
---|
1550 | |
---|
1551 | |
---|
1552 | /* TODO: Allow client sending a X.509 certificate chain */ |
---|
1553 | static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt) { |
---|
1554 | const gnutls_datum_t *cert_list; |
---|
1555 | unsigned int cert_list_size; |
---|
1556 | /* assume the certificate is invalid unless explicitly set |
---|
1557 | * otherwise */ |
---|
1558 | unsigned int status = GNUTLS_CERT_INVALID; |
---|
1559 | int rv = GNUTLS_E_NO_CERTIFICATE_FOUND, ret; |
---|
1560 | unsigned int ch_size = 0; |
---|
1561 | |
---|
1562 | // TODO: union no longer needed here after removing its "pgp" component. |
---|
1563 | union { |
---|
1564 | gnutls_x509_crt_t x509[MAX_CHAIN_SIZE]; |
---|
1565 | } cert; |
---|
1566 | apr_time_t expiration_time, cur_time; |
---|
1567 | |
---|
1568 | if (r == NULL || ctxt == NULL || ctxt->session == NULL) |
---|
1569 | return HTTP_FORBIDDEN; |
---|
1570 | |
---|
1571 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1572 | cert_list = |
---|
1573 | gnutls_certificate_get_peers(ctxt->session, &cert_list_size); |
---|
1574 | |
---|
1575 | if (cert_list == NULL || cert_list_size == 0) { |
---|
1576 | /* It is perfectly OK for a client not to send a certificate if on REQUEST mode |
---|
1577 | */ |
---|
1578 | if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST) |
---|
1579 | return OK; |
---|
1580 | |
---|
1581 | /* no certificate provided by the client, but one was required. */ |
---|
1582 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1583 | "GnuTLS: Failed to Verify Peer: " |
---|
1584 | "Client did not submit a certificate"); |
---|
1585 | return HTTP_FORBIDDEN; |
---|
1586 | } |
---|
1587 | |
---|
1588 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
1589 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1590 | "GnuTLS: A Chain of %d certificate(s) was provided for validation", |
---|
1591 | cert_list_size); |
---|
1592 | |
---|
1593 | for (ch_size = 0; ch_size < cert_list_size; ch_size++) { |
---|
1594 | gnutls_x509_crt_init(&cert.x509[ch_size]); |
---|
1595 | rv = gnutls_x509_crt_import(cert.x509[ch_size], |
---|
1596 | &cert_list[ch_size], |
---|
1597 | GNUTLS_X509_FMT_DER); |
---|
1598 | // When failure to import, leave the loop |
---|
1599 | if (rv != GNUTLS_E_SUCCESS) { |
---|
1600 | if (ch_size < 1) { |
---|
1601 | ap_log_rerror(APLOG_MARK, |
---|
1602 | APLOG_INFO, 0, r, |
---|
1603 | "GnuTLS: Failed to Verify Peer: " |
---|
1604 | "Failed to import peer certificates."); |
---|
1605 | ret = HTTP_FORBIDDEN; |
---|
1606 | goto exit; |
---|
1607 | } |
---|
1608 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1609 | "GnuTLS: Failed to import some peer certificates. Using %d certificates", |
---|
1610 | ch_size); |
---|
1611 | rv = GNUTLS_E_SUCCESS; |
---|
1612 | break; |
---|
1613 | } |
---|
1614 | } |
---|
1615 | } else |
---|
1616 | return HTTP_FORBIDDEN; |
---|
1617 | |
---|
1618 | if (rv < 0) { |
---|
1619 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1620 | "GnuTLS: Failed to Verify Peer: " |
---|
1621 | "Failed to import peer certificates."); |
---|
1622 | ret = HTTP_FORBIDDEN; |
---|
1623 | goto exit; |
---|
1624 | } |
---|
1625 | |
---|
1626 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) { |
---|
1627 | apr_time_ansi_put(&expiration_time, |
---|
1628 | gnutls_x509_crt_get_expiration_time |
---|
1629 | (cert.x509[0])); |
---|
1630 | |
---|
1631 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1632 | "GnuTLS: Verifying list of %d certificate(s) via method '%s'", |
---|
1633 | ch_size, mgs_readable_cvm(ctxt->sc->client_verify_method)); |
---|
1634 | switch(ctxt->sc->client_verify_method) { |
---|
1635 | case mgs_cvm_cartel: |
---|
1636 | rv = gnutls_x509_crt_list_verify(cert.x509, ch_size, |
---|
1637 | ctxt->sc->ca_list, |
---|
1638 | ctxt->sc->ca_list_size, |
---|
1639 | NULL, 0, 0, &status); |
---|
1640 | break; |
---|
1641 | #ifdef ENABLE_MSVA |
---|
1642 | case mgs_cvm_msva: |
---|
1643 | { |
---|
1644 | struct msv_response* resp = NULL; |
---|
1645 | struct msv_query q = { .context="https", .peertype="client", .pkctype="x509pem" }; |
---|
1646 | msv_ctxt_t ctx = msv_ctxt_init(NULL); |
---|
1647 | char cert_pem_buf[10 * 1024]; |
---|
1648 | size_t len = sizeof (cert_pem_buf); |
---|
1649 | |
---|
1650 | rv = 0; |
---|
1651 | if (gnutls_x509_crt_export(cert.x509[0], GNUTLS_X509_FMT_PEM, cert_pem_buf, &len) >= 0) { |
---|
1652 | /* FIXME : put together a name from the cert we received, instead of hard-coding this value: */ |
---|
1653 | q.peername = mgs_x509_construct_uid(r, cert.x509[0]); |
---|
1654 | q.pkcdata = cert_pem_buf; |
---|
1655 | rv = msv_query_agent(ctx, q, &resp); |
---|
1656 | if (rv == LIBMSV_ERROR_SUCCESS) { |
---|
1657 | status = 0; |
---|
1658 | } else if (rv == LIBMSV_ERROR_INVALID) { |
---|
1659 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1660 | "GnuTLS: Monkeysphere validation failed: (message: %s)", resp->message); |
---|
1661 | status = GNUTLS_CERT_INVALID; |
---|
1662 | } else { |
---|
1663 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
1664 | "GnuTLS: Error communicating with the Monkeysphere Validation Agent: (%d) %s", rv, msv_strerror(ctx, rv)); |
---|
1665 | status = GNUTLS_CERT_INVALID; |
---|
1666 | rv = -1; |
---|
1667 | } |
---|
1668 | } else { |
---|
1669 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1670 | "GnuTLS: Could not convert the client certificate to PEM format"); |
---|
1671 | status = GNUTLS_CERT_INVALID; |
---|
1672 | rv = GNUTLS_E_ASN1_ELEMENT_NOT_FOUND; |
---|
1673 | } |
---|
1674 | msv_response_destroy(resp); |
---|
1675 | msv_ctxt_destroy(ctx); |
---|
1676 | } |
---|
1677 | break; |
---|
1678 | #endif |
---|
1679 | default: |
---|
1680 | /* If this block is reached, that indicates a |
---|
1681 | * configuration error or bug in mod_gnutls (invalid value |
---|
1682 | * of ctxt->sc->client_verify_method). */ |
---|
1683 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1684 | "GnuTLS: Failed to Verify X.509 Peer: method '%s' is not supported", |
---|
1685 | mgs_readable_cvm(ctxt->sc->client_verify_method)); |
---|
1686 | rv = GNUTLS_E_UNIMPLEMENTED_FEATURE; |
---|
1687 | } |
---|
1688 | |
---|
1689 | } else { |
---|
1690 | /* Unknown certificate type */ |
---|
1691 | rv = GNUTLS_E_UNIMPLEMENTED_FEATURE; |
---|
1692 | } |
---|
1693 | |
---|
1694 | /* "goto exit" at the end of this block skips evaluation of the |
---|
1695 | * "status" variable */ |
---|
1696 | if (rv < 0) { |
---|
1697 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1698 | "GnuTLS: Failed to Verify Peer certificate: (%d) %s", |
---|
1699 | rv, gnutls_strerror(rv)); |
---|
1700 | if (rv == GNUTLS_E_NO_CERTIFICATE_FOUND) |
---|
1701 | ap_log_rerror(APLOG_MARK, APLOG_EMERG, 0, r, |
---|
1702 | "GnuTLS: No certificate was found for verification. Did you set the GnuTLSClientCAFile directive?"); |
---|
1703 | ret = HTTP_FORBIDDEN; |
---|
1704 | goto exit; |
---|
1705 | } |
---|
1706 | |
---|
1707 | /* TODO: X509 CRL Verification. */ |
---|
1708 | /* May add later if anyone needs it. |
---|
1709 | */ |
---|
1710 | /* ret = gnutls_x509_crt_check_revocation(crt, crl_list, crl_list_size); */ |
---|
1711 | |
---|
1712 | cur_time = apr_time_now(); |
---|
1713 | |
---|
1714 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) { |
---|
1715 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1716 | "GnuTLS: Could not find Signer for Peer Certificate"); |
---|
1717 | } |
---|
1718 | |
---|
1719 | if (status & GNUTLS_CERT_SIGNER_NOT_CA) { |
---|
1720 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1721 | "GnuTLS: Peer's Certificate signer is not a CA"); |
---|
1722 | } |
---|
1723 | |
---|
1724 | if (status & GNUTLS_CERT_INSECURE_ALGORITHM) { |
---|
1725 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1726 | "GnuTLS: Peer's Certificate is using insecure algorithms"); |
---|
1727 | } |
---|
1728 | |
---|
1729 | if (status & GNUTLS_CERT_EXPIRED |
---|
1730 | || status & GNUTLS_CERT_NOT_ACTIVATED) { |
---|
1731 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1732 | "GnuTLS: Peer's Certificate signer is expired or not yet activated"); |
---|
1733 | } |
---|
1734 | |
---|
1735 | if (status & GNUTLS_CERT_INVALID) { |
---|
1736 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1737 | "GnuTLS: Peer Certificate is invalid."); |
---|
1738 | } else if (status & GNUTLS_CERT_REVOKED) { |
---|
1739 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1740 | "GnuTLS: Peer Certificate is revoked."); |
---|
1741 | } |
---|
1742 | |
---|
1743 | mgs_add_common_cert_vars(r, cert.x509[0], 1, ctxt->sc->export_certificates_size); |
---|
1744 | |
---|
1745 | { |
---|
1746 | /* days remaining */ |
---|
1747 | unsigned long remain = |
---|
1748 | (apr_time_sec(expiration_time) - |
---|
1749 | apr_time_sec(cur_time)) / 86400; |
---|
1750 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_V_REMAIN", |
---|
1751 | apr_psprintf(r->pool, "%lu", remain)); |
---|
1752 | } |
---|
1753 | |
---|
1754 | if (status == 0) { |
---|
1755 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", |
---|
1756 | "SUCCESS"); |
---|
1757 | ret = OK; |
---|
1758 | } else { |
---|
1759 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", |
---|
1760 | "FAILED"); |
---|
1761 | if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST) |
---|
1762 | ret = OK; |
---|
1763 | else |
---|
1764 | ret = HTTP_FORBIDDEN; |
---|
1765 | } |
---|
1766 | |
---|
1767 | exit: |
---|
1768 | if (gnutls_certificate_type_get(ctxt->session) == GNUTLS_CRT_X509) |
---|
1769 | for (unsigned int i = 0; i < ch_size; i++) |
---|
1770 | gnutls_x509_crt_deinit(cert.x509[i]); |
---|
1771 | |
---|
1772 | return ret; |
---|
1773 | } |
---|
1774 | |
---|
1775 | |
---|
1776 | |
---|
1777 | #ifdef ENABLE_MSVA |
---|
1778 | /* this section of code is used only when trying to talk to the MSVA */ |
---|
1779 | static const char* mgs_x509_leaf_oid_from_dn(apr_pool_t *pool, const char* oid, gnutls_x509_crt_t cert) { |
---|
1780 | int rv=GNUTLS_E_SUCCESS, i; |
---|
1781 | size_t sz=0, lastsz=0; |
---|
1782 | char* data=NULL; |
---|
1783 | |
---|
1784 | i = -1; |
---|
1785 | while(rv != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { |
---|
1786 | i++; |
---|
1787 | lastsz=sz; |
---|
1788 | sz=0; |
---|
1789 | rv = gnutls_x509_crt_get_dn_by_oid (cert, oid, i, 0, NULL, &sz); |
---|
1790 | } |
---|
1791 | if (i > 0) { |
---|
1792 | data = apr_palloc(pool, lastsz); |
---|
1793 | sz=lastsz; |
---|
1794 | rv = gnutls_x509_crt_get_dn_by_oid (cert, oid, i-1, 0, data, &sz); |
---|
1795 | if (rv == GNUTLS_E_SUCCESS) |
---|
1796 | return data; |
---|
1797 | } |
---|
1798 | return NULL; |
---|
1799 | } |
---|
1800 | |
---|
1801 | static const char* mgs_x509_first_type_from_san(apr_pool_t *pool, gnutls_x509_subject_alt_name_t target, gnutls_x509_crt_t cert) { |
---|
1802 | int rv=GNUTLS_E_SUCCESS; |
---|
1803 | size_t sz; |
---|
1804 | char* data=NULL; |
---|
1805 | unsigned int i; |
---|
1806 | gnutls_x509_subject_alt_name_t thistype; |
---|
1807 | |
---|
1808 | i = 0; |
---|
1809 | while(rv != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) { |
---|
1810 | sz = 0; |
---|
1811 | rv = gnutls_x509_crt_get_subject_alt_name2(cert, i, NULL, &sz, &thistype, NULL); |
---|
1812 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && thistype == target) { |
---|
1813 | data = apr_palloc(pool, sz); |
---|
1814 | rv = gnutls_x509_crt_get_subject_alt_name2(cert, i, data, &sz, &thistype, NULL); |
---|
1815 | if (rv >=0 && (thistype == target)) |
---|
1816 | return data; |
---|
1817 | } |
---|
1818 | i++; |
---|
1819 | } |
---|
1820 | return NULL; |
---|
1821 | } |
---|
1822 | |
---|
1823 | |
---|
1824 | /* Create a string representing a candidate User ID from an X.509 |
---|
1825 | * certificate |
---|
1826 | |
---|
1827 | * We need this for client certification because a client gives us a |
---|
1828 | * certificate, but doesn't tell us (in any other way) who they are |
---|
1829 | * trying to authenticate as. |
---|
1830 | |
---|
1831 | * one complaint might be "but the user wanted to be another identity, |
---|
1832 | * which is also in the certificate (e.g. in a SubjectAltName)" |
---|
1833 | * However, given that any user can regenerate their own X.509 |
---|
1834 | * certificate with their own public key content, they should just do |
---|
1835 | * so, and not expect us to guess at their identity :) |
---|
1836 | |
---|
1837 | * This function allocates it's response from the pool given it. When |
---|
1838 | * that pool is reclaimed, the response will also be deallocated. |
---|
1839 | |
---|
1840 | * FIXME: what about extracting a server-style cert |
---|
1841 | * (e.g. https://imposter.example) from the DN or any sAN? |
---|
1842 | |
---|
1843 | * FIXME: what if we want to call this outside the context of a |
---|
1844 | * request? That complicates the logging. |
---|
1845 | */ |
---|
1846 | static const char* mgs_x509_construct_uid(request_rec *r, gnutls_x509_crt_t cert) { |
---|
1847 | /* basic strategy, assuming humans are the users: we are going to |
---|
1848 | * try to reconstruct a "conventional" User ID by pulling in a |
---|
1849 | * name, comment, and e-mail address. |
---|
1850 | */ |
---|
1851 | apr_pool_t *pool = r->pool; |
---|
1852 | const char *name=NULL, *comment=NULL, *email=NULL; |
---|
1853 | const char *ret=NULL; |
---|
1854 | /* subpool for temporary allocation: */ |
---|
1855 | apr_pool_t *sp=NULL; |
---|
1856 | |
---|
1857 | if (APR_SUCCESS != apr_pool_create(&sp, pool)) |
---|
1858 | return NULL; /* i'm assuming that libapr would log this kind |
---|
1859 | * of error on its own */ |
---|
1860 | |
---|
1861 | /* Name |
---|
1862 | |
---|
1863 | the name comes from the leaf commonName of the cert's Subject. |
---|
1864 | |
---|
1865 | (MAYBE: should we look at trying to assemble a candidate from |
---|
1866 | givenName, surName, suffix, etc? the "name" field |
---|
1867 | appears to be case-insensitive, which seems problematic |
---|
1868 | from what we expect; see: |
---|
1869 | http://www.itu.int/rec/T-REC-X.520-200102-s/e ) |
---|
1870 | |
---|
1871 | (MAYBE: should we try pulling a commonName or otherName or |
---|
1872 | something from subjectAltName? see: |
---|
1873 | https://tools.ietf.org/html/rfc5280#section-4.2.1.6 |
---|
1874 | GnuTLS does not support looking for Common Names in the |
---|
1875 | SAN yet) |
---|
1876 | */ |
---|
1877 | name = mgs_x509_leaf_oid_from_dn(sp, GNUTLS_OID_X520_COMMON_NAME, cert); |
---|
1878 | |
---|
1879 | /* Comment |
---|
1880 | |
---|
1881 | I am inclined to punt on this for now, as Comment has been so |
---|
1882 | atrociously misused in OpenPGP. Perhaps if there is a |
---|
1883 | pseudonym (OID 2.5.4.65, aka GNUTLS_OID_X520_PSEUDONYM) field |
---|
1884 | in the subject or sAN? |
---|
1885 | */ |
---|
1886 | comment = mgs_x509_leaf_oid_from_dn(sp, GNUTLS_OID_X520_PSEUDONYM, cert); |
---|
1887 | |
---|
1888 | /* E-mail |
---|
1889 | |
---|
1890 | This should be the the first rfc822Name from the sAN. |
---|
1891 | |
---|
1892 | failing that, we'll take the leaf email in the certificate's |
---|
1893 | subject; this is a deprecated use though. |
---|
1894 | */ |
---|
1895 | email = mgs_x509_first_type_from_san(sp, GNUTLS_SAN_RFC822NAME, cert); |
---|
1896 | if (email == NULL) |
---|
1897 | email = mgs_x509_leaf_oid_from_dn(sp, GNUTLS_OID_PKCS9_EMAIL, cert); |
---|
1898 | |
---|
1899 | /* assemble all the parts: */ |
---|
1900 | |
---|
1901 | /* must have at least a name or an e-mail. */ |
---|
1902 | if (name == NULL && email == NULL) { |
---|
1903 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
1904 | "GnuTLS: Need either a name or an e-mail address to get a User ID from an X.509 certificate."); |
---|
1905 | goto end; |
---|
1906 | } |
---|
1907 | if (name) { |
---|
1908 | if (comment) { |
---|
1909 | if (email) { |
---|
1910 | ret = apr_psprintf(pool, "%s (%s) <%s>", name, comment, email); |
---|
1911 | } else { |
---|
1912 | ret = apr_psprintf(pool, "%s (%s)", name, comment); |
---|
1913 | } |
---|
1914 | } else { |
---|
1915 | if (email) { |
---|
1916 | ret = apr_psprintf(pool, "%s <%s>", name, email); |
---|
1917 | } else { |
---|
1918 | ret = apr_pstrdup(pool, name); |
---|
1919 | } |
---|
1920 | } |
---|
1921 | } else { |
---|
1922 | if (comment) { |
---|
1923 | ret = apr_psprintf(pool, "(%s) <%s>", comment, email); |
---|
1924 | } else { |
---|
1925 | ret = apr_psprintf(pool, "<%s>", email); |
---|
1926 | } |
---|
1927 | } |
---|
1928 | |
---|
1929 | end: |
---|
1930 | apr_pool_destroy(sp); |
---|
1931 | return ret; |
---|
1932 | } |
---|
1933 | #endif /* ENABLE_MSVA */ |
---|
1934 | |
---|
1935 | |
---|
1936 | |
---|
1937 | /* |
---|
1938 | * This hook writes the mod_gnutls status message for a mod_status |
---|
1939 | * report. According to the comments in mod_status.h, the "flags" |
---|
1940 | * parameter is a bitwise OR of the AP_STATUS_ flags. |
---|
1941 | * |
---|
1942 | * Note that this implementation gives flags explicitly requesting a |
---|
1943 | * simple response priority, e.g. if AP_STATUS_SHORT is set, flags |
---|
1944 | * requesting an HTML report will be ignored. As of Apache 2.4.10, the |
---|
1945 | * following flags were defined in mod_status.h: |
---|
1946 | * |
---|
1947 | * AP_STATUS_SHORT (short, non-HTML report requested) |
---|
1948 | * AP_STATUS_NOTABLE (HTML report without tables) |
---|
1949 | * AP_STATUS_EXTENDED (detailed report) |
---|
1950 | */ |
---|
1951 | static int mgs_status_hook(request_rec *r, int flags) |
---|
1952 | { |
---|
1953 | if (r == NULL) |
---|
1954 | return OK; |
---|
1955 | |
---|
1956 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
1957 | ap_get_module_config(r->server->module_config, &gnutls_module); |
---|
1958 | |
---|
1959 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
1960 | |
---|
1961 | if (flags & AP_STATUS_SHORT) |
---|
1962 | { |
---|
1963 | ap_rprintf(r, "Using GnuTLS version: %s\n", gnutls_check_version(NULL)); |
---|
1964 | ap_rputs("Built against GnuTLS version: " GNUTLS_VERSION "\n", r); |
---|
1965 | } |
---|
1966 | else |
---|
1967 | { |
---|
1968 | ap_rputs("<hr>\n", r); |
---|
1969 | ap_rputs("<h2>GnuTLS Information:</h2>\n<dl>\n", r); |
---|
1970 | |
---|
1971 | ap_rprintf(r, "<dt>Using GnuTLS version:</dt><dd>%s</dd>\n", |
---|
1972 | gnutls_check_version(NULL)); |
---|
1973 | ap_rputs("<dt>Built against GnuTLS version:</dt><dd>" |
---|
1974 | GNUTLS_VERSION "</dd>\n", r); |
---|
1975 | ap_rprintf(r, "<dt>Using TLS:</dt><dd>%s</dd>\n", |
---|
1976 | (sc->enabled == GNUTLS_ENABLED_FALSE ? "no" : "yes")); |
---|
1977 | } |
---|
1978 | |
---|
1979 | if (sc->enabled != GNUTLS_ENABLED_FALSE) |
---|
1980 | { |
---|
1981 | mgs_handle_t* ctxt = get_effective_gnutls_ctxt(r->connection); |
---|
1982 | if (ctxt && ctxt->session != NULL) |
---|
1983 | { |
---|
1984 | char* s_info = gnutls_session_get_desc(ctxt->session); |
---|
1985 | if (s_info) |
---|
1986 | { |
---|
1987 | if (flags & AP_STATUS_SHORT) |
---|
1988 | ap_rprintf(r, "Current TLS session: %s\n", s_info); |
---|
1989 | else |
---|
1990 | ap_rprintf(r, "<dt>Current TLS session:</dt><dd>%s</dd>\n", |
---|
1991 | s_info); |
---|
1992 | gnutls_free(s_info); |
---|
1993 | } |
---|
1994 | } |
---|
1995 | } |
---|
1996 | |
---|
1997 | if (!(flags & AP_STATUS_SHORT)) |
---|
1998 | ap_rputs("</dl>\n", r); |
---|
1999 | |
---|
2000 | if (sc->ocsp_cache) |
---|
2001 | mgs_cache_status(sc->ocsp_cache, "GnuTLS OCSP Cache", r, flags); |
---|
2002 | if (sc->cache_enable) |
---|
2003 | mgs_cache_status(sc->cache, "GnuTLS Session Cache", r, flags); |
---|
2004 | |
---|
2005 | return OK; |
---|
2006 | } |
---|
2007 | |
---|
2008 | |
---|
2009 | |
---|
2010 | /* |
---|
2011 | * Callback to check the server certificate for proxy HTTPS |
---|
2012 | * connections, to be used with |
---|
2013 | * gnutls_certificate_set_verify_function. |
---|
2014 | |
---|
2015 | * Returns: 0 if certificate check was successful (certificate |
---|
2016 | * trusted), non-zero otherwise (error during check or untrusted |
---|
2017 | * certificate). |
---|
2018 | */ |
---|
2019 | static int gtls_check_server_cert(gnutls_session_t session) |
---|
2020 | { |
---|
2021 | mgs_handle_t *ctxt = (mgs_handle_t *) gnutls_session_get_ptr(session); |
---|
2022 | unsigned int status; |
---|
2023 | |
---|
2024 | /* Get peer hostname from a note left by mod_proxy */ |
---|
2025 | const char *peer_hostname = |
---|
2026 | apr_table_get(ctxt->c->notes, PROXY_SNI_NOTE); |
---|
2027 | if (peer_hostname == NULL) |
---|
2028 | ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, ctxt->c, |
---|
2029 | "%s: " PROXY_SNI_NOTE " NULL, cannot check " |
---|
2030 | "peer's hostname", __func__); |
---|
2031 | |
---|
2032 | /* Verify certificate, including hostname match. Should |
---|
2033 | * peer_hostname be NULL for some reason, the name is not |
---|
2034 | * checked. */ |
---|
2035 | int err = gnutls_certificate_verify_peers3(session, peer_hostname, |
---|
2036 | &status); |
---|
2037 | if (err != GNUTLS_E_SUCCESS) |
---|
2038 | { |
---|
2039 | ap_log_cerror(APLOG_MARK, APLOG_INFO, 0, ctxt->c, |
---|
2040 | "%s: server certificate check failed: %s (%d)", |
---|
2041 | __func__, gnutls_strerror(err), err); |
---|
2042 | return err; |
---|
2043 | } |
---|
2044 | |
---|
2045 | if (status == 0) |
---|
2046 | ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, ctxt->c, |
---|
2047 | "%s: server certificate is trusted.", |
---|
2048 | __func__); |
---|
2049 | else |
---|
2050 | { |
---|
2051 | gnutls_datum_t out; |
---|
2052 | /* GNUTLS_CRT_X509: ATM, only X509 is supported for proxy |
---|
2053 | * certs 0: according to function API, the last argument |
---|
2054 | * should be 0 */ |
---|
2055 | err = gnutls_certificate_verification_status_print(status, |
---|
2056 | GNUTLS_CRT_X509, |
---|
2057 | &out, 0); |
---|
2058 | if (err != GNUTLS_E_SUCCESS) |
---|
2059 | ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, ctxt->c, |
---|
2060 | "%s: server verify print failed: %s (%d)", |
---|
2061 | __func__, gnutls_strerror(err), err); |
---|
2062 | else |
---|
2063 | ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, ctxt->c, |
---|
2064 | "%s: %s", |
---|
2065 | __func__, out.data); |
---|
2066 | gnutls_free(out.data); |
---|
2067 | } |
---|
2068 | |
---|
2069 | return status; |
---|
2070 | } |
---|
2071 | |
---|
2072 | |
---|
2073 | |
---|
2074 | static apr_status_t cleanup_proxy_x509_credentials(void *arg) |
---|
2075 | { |
---|
2076 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) arg; |
---|
2077 | |
---|
2078 | if (sc->proxy_x509_creds) |
---|
2079 | { |
---|
2080 | /* This implicitly releases the associated trust list |
---|
2081 | * sc->proxy_x509_tl, too. */ |
---|
2082 | gnutls_certificate_free_credentials(sc->proxy_x509_creds); |
---|
2083 | sc->proxy_x509_creds = NULL; |
---|
2084 | sc->proxy_x509_tl = NULL; |
---|
2085 | } |
---|
2086 | |
---|
2087 | if (sc->anon_client_creds) |
---|
2088 | { |
---|
2089 | gnutls_anon_free_client_credentials(sc->anon_client_creds); |
---|
2090 | sc->anon_client_creds = NULL; |
---|
2091 | } |
---|
2092 | |
---|
2093 | /* Deinit proxy priorities only if set from |
---|
2094 | * sc->proxy_priorities_str. Otherwise the server is using the |
---|
2095 | * default global priority cache, which must not be deinitialized |
---|
2096 | * here. */ |
---|
2097 | if (sc->proxy_priorities_str && sc->proxy_priorities) |
---|
2098 | { |
---|
2099 | gnutls_priority_deinit(sc->proxy_priorities); |
---|
2100 | sc->proxy_priorities = NULL; |
---|
2101 | } |
---|
2102 | |
---|
2103 | return APR_SUCCESS; |
---|
2104 | } |
---|
2105 | |
---|
2106 | |
---|
2107 | |
---|
2108 | static apr_status_t load_proxy_x509_credentials(apr_pool_t *pconf, |
---|
2109 | apr_pool_t *ptemp, |
---|
2110 | server_rec *s) |
---|
2111 | { |
---|
2112 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
2113 | ap_get_module_config(s->module_config, &gnutls_module); |
---|
2114 | |
---|
2115 | if (sc == NULL) |
---|
2116 | return APR_EGENERAL; |
---|
2117 | |
---|
2118 | apr_status_t ret = APR_EINIT; |
---|
2119 | int err = GNUTLS_E_SUCCESS; |
---|
2120 | |
---|
2121 | /* Cleanup function for the GnuTLS structures allocated below */ |
---|
2122 | apr_pool_cleanup_register(pconf, sc, cleanup_proxy_x509_credentials, |
---|
2123 | apr_pool_cleanup_null); |
---|
2124 | |
---|
2125 | /* Function pool, gets destroyed before exit. */ |
---|
2126 | apr_pool_t *pool; |
---|
2127 | ret = apr_pool_create(&pool, ptemp); |
---|
2128 | if (ret != APR_SUCCESS) |
---|
2129 | { |
---|
2130 | ap_log_error(APLOG_MARK, APLOG_ERR, ret, s, |
---|
2131 | "%s: failed to allocate function memory pool.", __func__); |
---|
2132 | return ret; |
---|
2133 | } |
---|
2134 | |
---|
2135 | /* allocate credentials structures */ |
---|
2136 | err = gnutls_certificate_allocate_credentials(&sc->proxy_x509_creds); |
---|
2137 | if (err != GNUTLS_E_SUCCESS) |
---|
2138 | { |
---|
2139 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2140 | "%s: Failed to initialize proxy credentials: (%d) %s", |
---|
2141 | __func__, err, gnutls_strerror(err)); |
---|
2142 | return APR_EGENERAL; |
---|
2143 | } |
---|
2144 | err = gnutls_anon_allocate_client_credentials(&sc->anon_client_creds); |
---|
2145 | if (err != GNUTLS_E_SUCCESS) |
---|
2146 | { |
---|
2147 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2148 | "%s: Failed to initialize anon credentials for proxy: " |
---|
2149 | "(%d) %s", __func__, err, gnutls_strerror(err)); |
---|
2150 | return APR_EGENERAL; |
---|
2151 | } |
---|
2152 | |
---|
2153 | /* Check if the proxy priorities have been set, fail immediately |
---|
2154 | * if not */ |
---|
2155 | if (sc->proxy_priorities_str == NULL) |
---|
2156 | { |
---|
2157 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, |
---|
2158 | "No GnuTLSProxyPriorities directive for host '%s:%d', " |
---|
2159 | "using default '%s'.", |
---|
2160 | s->server_hostname, s->addrs->host_port, |
---|
2161 | MGS_DEFAULT_PRIORITY); |
---|
2162 | sc->proxy_priorities = default_prio; |
---|
2163 | } |
---|
2164 | else |
---|
2165 | { |
---|
2166 | /* parse proxy priorities */ |
---|
2167 | const char *err_pos = NULL; |
---|
2168 | err = gnutls_priority_init(&sc->proxy_priorities, |
---|
2169 | sc->proxy_priorities_str, &err_pos); |
---|
2170 | if (err != GNUTLS_E_SUCCESS) |
---|
2171 | { |
---|
2172 | if (ret == GNUTLS_E_INVALID_REQUEST) |
---|
2173 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2174 | "%s: Syntax error parsing proxy priorities " |
---|
2175 | "string at: %s", |
---|
2176 | __func__, err_pos); |
---|
2177 | else |
---|
2178 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2179 | "Error setting proxy priorities: %s (%d)", |
---|
2180 | gnutls_strerror(err), err); |
---|
2181 | ret = APR_EGENERAL; |
---|
2182 | } |
---|
2183 | } |
---|
2184 | |
---|
2185 | /* load certificate and key for client auth, if configured */ |
---|
2186 | if (sc->proxy_x509_key_file && sc->proxy_x509_cert_file) |
---|
2187 | { |
---|
2188 | char* cert_file = ap_server_root_relative(pool, |
---|
2189 | sc->proxy_x509_cert_file); |
---|
2190 | char* key_file = ap_server_root_relative(pool, |
---|
2191 | sc->proxy_x509_key_file); |
---|
2192 | err = gnutls_certificate_set_x509_key_file(sc->proxy_x509_creds, |
---|
2193 | cert_file, |
---|
2194 | key_file, |
---|
2195 | GNUTLS_X509_FMT_PEM); |
---|
2196 | if (err != GNUTLS_E_SUCCESS) |
---|
2197 | { |
---|
2198 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2199 | "%s: loading proxy client credentials failed: %s (%d)", |
---|
2200 | __func__, gnutls_strerror(err), err); |
---|
2201 | ret = APR_EGENERAL; |
---|
2202 | } |
---|
2203 | } |
---|
2204 | else if (!sc->proxy_x509_key_file && sc->proxy_x509_cert_file) |
---|
2205 | { |
---|
2206 | ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, |
---|
2207 | "%s: proxy key file not set!", __func__); |
---|
2208 | ret = APR_EGENERAL; |
---|
2209 | } |
---|
2210 | else if (!sc->proxy_x509_cert_file && sc->proxy_x509_key_file) |
---|
2211 | { |
---|
2212 | ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, |
---|
2213 | "%s: proxy certificate file not set!", __func__); |
---|
2214 | ret = APR_EGENERAL; |
---|
2215 | } |
---|
2216 | else |
---|
2217 | /* if both key and cert are NULL, client auth is not used */ |
---|
2218 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, |
---|
2219 | "%s: no client credentials for proxy", __func__); |
---|
2220 | |
---|
2221 | /* must be set if the server certificate is to be checked */ |
---|
2222 | if (sc->proxy_x509_ca_file) |
---|
2223 | { |
---|
2224 | /* initialize the trust list */ |
---|
2225 | err = gnutls_x509_trust_list_init(&sc->proxy_x509_tl, 0); |
---|
2226 | if (err != GNUTLS_E_SUCCESS) |
---|
2227 | { |
---|
2228 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2229 | "%s: gnutls_x509_trust_list_init failed: %s (%d)", |
---|
2230 | __func__, gnutls_strerror(err), err); |
---|
2231 | ret = APR_EGENERAL; |
---|
2232 | } |
---|
2233 | |
---|
2234 | char* ca_file = ap_server_root_relative(pool, |
---|
2235 | sc->proxy_x509_ca_file); |
---|
2236 | /* if no CRL is used, sc->proxy_x509_crl_file is NULL */ |
---|
2237 | char* crl_file = NULL; |
---|
2238 | if (sc->proxy_x509_crl_file) |
---|
2239 | crl_file = ap_server_root_relative(pool, |
---|
2240 | sc->proxy_x509_crl_file); |
---|
2241 | |
---|
2242 | /* returns number of loaded elements */ |
---|
2243 | err = gnutls_x509_trust_list_add_trust_file(sc->proxy_x509_tl, |
---|
2244 | ca_file, |
---|
2245 | crl_file, |
---|
2246 | GNUTLS_X509_FMT_PEM, |
---|
2247 | 0 /* tl_flags */, |
---|
2248 | 0 /* tl_vflags */); |
---|
2249 | if (err > 0) |
---|
2250 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, |
---|
2251 | "%s: proxy CA trust list: %d structures loaded", |
---|
2252 | __func__, err); |
---|
2253 | else if (err == 0) |
---|
2254 | ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, |
---|
2255 | "%s: proxy CA trust list is empty (%d)", |
---|
2256 | __func__, err); |
---|
2257 | else /* err < 0 */ |
---|
2258 | { |
---|
2259 | ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, |
---|
2260 | "%s: error loading proxy CA trust list: %s (%d)", |
---|
2261 | __func__, gnutls_strerror(err), err); |
---|
2262 | ret = APR_EGENERAL; |
---|
2263 | } |
---|
2264 | |
---|
2265 | /* attach trust list to credentials */ |
---|
2266 | gnutls_certificate_set_trust_list(sc->proxy_x509_creds, |
---|
2267 | sc->proxy_x509_tl, 0); |
---|
2268 | } |
---|
2269 | else |
---|
2270 | ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, |
---|
2271 | "%s: no CA trust list for proxy connections, " |
---|
2272 | "TLS connections will fail!", __func__); |
---|
2273 | |
---|
2274 | gnutls_certificate_set_verify_function(sc->proxy_x509_creds, |
---|
2275 | gtls_check_server_cert); |
---|
2276 | apr_pool_destroy(pool); |
---|
2277 | return ret; |
---|
2278 | } |
---|