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