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