From c301152dbbdf13c4bf212c7982b948c21149468d Mon Sep 17 00:00:00 2001 From: Paul Querna Date: Sun, 24 Apr 2005 22:21:50 +0000 Subject: - move hooks to gnutls_hooks.c - use 'mgs_' as the prefix for all symbols, instead of mixed prefixes. --- src/Makefile.am | 2 +- src/gnutls_cache.c | 40 ++-- src/gnutls_config.c | 42 ++-- src/gnutls_hooks.c | 654 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/gnutls_io.c | 32 +-- src/mod_gnutls.c | 665 ++-------------------------------------------------- 6 files changed, 729 insertions(+), 706 deletions(-) create mode 100644 src/gnutls_hooks.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index a15fc57..c6d42c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ CLEANFILES = .libs/libmod_gnutls *~ -libmod_gnutls_la_SOURCES = mod_gnutls.c gnutls_io.c gnutls_cache.c gnutls_config.c +libmod_gnutls_la_SOURCES = mod_gnutls.c gnutls_io.c gnutls_cache.c gnutls_config.c gnutls_hooks.c libmod_gnutls_la_CFLAGS = -Wall ${MODULE_CFLAGS} libmod_gnutls_la_LDFLAGS = -rpath ${AP_LIBEXECDIR} -module -avoid-version ${MODULE_LIBS} diff --git a/src/gnutls_cache.c b/src/gnutls_cache.c index 8499b84..4c094fa 100644 --- a/src/gnutls_cache.c +++ b/src/gnutls_cache.c @@ -53,7 +53,7 @@ static char *gnutls_session_id2sz(unsigned char *id, int idlen, return str; } -char *mod_gnutls_session_id2sz(unsigned char *id, int idlen, +char *mgs_session_id2sz(unsigned char *id, int idlen, char *str, int strsize) { char *cp; @@ -80,7 +80,7 @@ char *mod_gnutls_session_id2sz(unsigned char *id, int idlen, static apr_memcache_t* mc; static int mc_cache_child_init(apr_pool_t *p, server_rec *s, - mod_gnutls_srvconf_rec *sc) + mgs_srvconf_rec *sc) { apr_status_t rv = APR_SUCCESS; int thread_limit = 0; @@ -166,7 +166,7 @@ static int mc_cache_store(void* baton, gnutls_datum_t key, gnutls_datum_t data) { apr_status_t rv = APR_SUCCESS; - mod_gnutls_handle_t *ctxt = baton; + mgs_handle_t *ctxt = baton; char buf[STR_SESSION_LEN]; char* strkey = NULL; apr_uint32_t timeout; @@ -193,7 +193,7 @@ static int mc_cache_store(void* baton, gnutls_datum_t key, static gnutls_datum_t mc_cache_fetch(void* baton, gnutls_datum_t key) { apr_status_t rv = APR_SUCCESS; - mod_gnutls_handle_t *ctxt = baton; + mgs_handle_t *ctxt = baton; char buf[STR_SESSION_LEN]; char* strkey = NULL; char* value; @@ -234,7 +234,7 @@ static gnutls_datum_t mc_cache_fetch(void* baton, gnutls_datum_t key) static int mc_cache_delete(void* baton, gnutls_datum_t key) { apr_status_t rv = APR_SUCCESS; - mod_gnutls_handle_t *ctxt = baton; + mgs_handle_t *ctxt = baton; char buf[STR_SESSION_LEN]; char* strkey = NULL; @@ -259,7 +259,7 @@ static int mc_cache_delete(void* baton, gnutls_datum_t key) #define SSL_DBM_FILE_MODE ( APR_UREAD | APR_UWRITE | APR_GREAD | APR_WREAD ) -static int dbm_cache_expire(mod_gnutls_handle_t *ctxt) +static int dbm_cache_expire(mgs_handle_t *ctxt) { apr_status_t rv; apr_dbm_t *dbm; @@ -346,7 +346,7 @@ static gnutls_datum_t dbm_cache_fetch(void* baton, gnutls_datum_t key) apr_dbm_t *dbm; apr_datum_t dbmkey; apr_datum_t dbmval; - mod_gnutls_handle_t *ctxt = baton; + mgs_handle_t *ctxt = baton; apr_status_t rv; dbmkey.dptr = key.data; @@ -395,7 +395,7 @@ static int dbm_cache_store(void* baton, gnutls_datum_t key, apr_dbm_t *dbm; apr_datum_t dbmkey; apr_datum_t dbmval; - mod_gnutls_handle_t *ctxt = baton; + mgs_handle_t *ctxt = baton; apr_status_t rv; apr_time_t expiry; @@ -448,7 +448,7 @@ static int dbm_cache_delete(void* baton, gnutls_datum_t key) { apr_dbm_t *dbm; apr_datum_t dbmkey; - mod_gnutls_handle_t *ctxt = baton; + mgs_handle_t *ctxt = baton; apr_status_t rv; dbmkey.dptr = (char *)key.data; @@ -483,7 +483,7 @@ static int dbm_cache_delete(void* baton, gnutls_datum_t key) } static int dbm_cache_post_config(apr_pool_t *p, server_rec *s, - mod_gnutls_srvconf_rec *sc) + mgs_srvconf_rec *sc) { apr_status_t rv; apr_dbm_t *dbm; @@ -518,23 +518,23 @@ static int dbm_cache_post_config(apr_pool_t *p, server_rec *s, return rv; } -int mod_gnutls_cache_post_config(apr_pool_t *p, server_rec *s, - mod_gnutls_srvconf_rec *sc) +int mgs_cache_post_config(apr_pool_t *p, server_rec *s, + mgs_srvconf_rec *sc) { - if (sc->cache_type == mod_gnutls_cache_dbm) { + if (sc->cache_type == mgs_cache_dbm) { return dbm_cache_post_config(p, s, sc); } return 0; } -int mod_gnutls_cache_child_init(apr_pool_t *p, server_rec *s, - mod_gnutls_srvconf_rec *sc) +int mgs_cache_child_init(apr_pool_t *p, server_rec *s, + mgs_srvconf_rec *sc) { - if (sc->cache_type == mod_gnutls_cache_dbm) { + if (sc->cache_type == mgs_cache_dbm) { return 0; } #if HAVE_APR_MEMCACHE - else if (sc->cache_type == mod_gnutls_cache_memcache) { + else if (sc->cache_type == mgs_cache_memcache) { return mc_cache_child_init(p, s, sc); } #endif @@ -543,16 +543,16 @@ int mod_gnutls_cache_child_init(apr_pool_t *p, server_rec *s, #include -int mod_gnutls_cache_session_init(mod_gnutls_handle_t *ctxt) +int mgs_cache_session_init(mgs_handle_t *ctxt) { - if (ctxt->sc->cache_type == mod_gnutls_cache_dbm) { + if (ctxt->sc->cache_type == mgs_cache_dbm) { gnutls_db_set_retrieve_function(ctxt->session, dbm_cache_fetch); gnutls_db_set_remove_function(ctxt->session, dbm_cache_delete); gnutls_db_set_store_function(ctxt->session, dbm_cache_store); gnutls_db_set_ptr(ctxt->session, ctxt); } #if HAVE_APR_MEMCACHE - else if (ctxt->sc->cache_type == mod_gnutls_cache_memcache) { + else if (ctxt->sc->cache_type == mgs_cache_memcache) { gnutls_db_set_retrieve_function(ctxt->session, mc_cache_fetch); gnutls_db_set_remove_function(ctxt->session, mc_cache_delete); gnutls_db_set_store_function(ctxt->session, mc_cache_store); diff --git a/src/gnutls_config.c b/src/gnutls_config.c index 2c29ccb..f9a315c 100644 --- a/src/gnutls_config.c +++ b/src/gnutls_config.c @@ -59,8 +59,8 @@ const char *mgs_set_cert_file(cmd_parms * parms, void *dummy, gnutls_datum_t data; const char* file; apr_pool_t* spool; - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(parms->server-> module_config, &gnutls_module); apr_pool_create(&spool, parms->pool); @@ -91,8 +91,8 @@ const char *mgs_set_key_file(cmd_parms * parms, void *dummy, gnutls_datum_t data; const char* file; apr_pool_t* spool; - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(parms->server-> module_config, &gnutls_module); apr_pool_create(&spool, parms->pool); @@ -119,7 +119,7 @@ const char *mgs_set_cache(cmd_parms * parms, void *dummy, const char *type, const char* arg) { const char* err; - mod_gnutls_srvconf_rec *sc = ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = ap_get_module_config(parms->server-> module_config, &gnutls_module); if ((err = ap_check_cmd_context(parms, GLOBAL_ONLY))) { @@ -127,21 +127,21 @@ const char *mgs_set_cache(cmd_parms * parms, void *dummy, } if (strcasecmp("none", type) == 0) { - sc->cache_type = mod_gnutls_cache_none; + sc->cache_type = mgs_cache_none; } else if (strcasecmp("dbm", type) == 0) { - sc->cache_type = mod_gnutls_cache_dbm; + sc->cache_type = mgs_cache_dbm; } #if HAVE_APR_MEMCACHE else if (strcasecmp("memcache", type) == 0) { - sc->cache_type = mod_gnutls_cache_memcache; + sc->cache_type = mgs_cache_memcache; } #endif else { return "Invalid Type for GnuTLSCache!"; } - if (sc->cache_type == mod_gnutls_cache_dbm) { + if (sc->cache_type == mgs_cache_dbm) { sc->cache_config = ap_server_root_relative(parms->pool, arg); } else { @@ -155,8 +155,8 @@ const char *mgs_set_cache_timeout(cmd_parms * parms, void *dummy, const char *arg) { int argint; - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(parms->server-> module_config, &gnutls_module); @@ -195,12 +195,12 @@ const char *mgs_set_client_verify(cmd_parms * parms, void *dummy, /* This was set from a directory context */ if (parms->path) { - mod_gnutls_dirconf_rec *dc = (mod_gnutls_dirconf_rec *)dummy; + mgs_dirconf_rec *dc = (mgs_dirconf_rec *)dummy; dc->client_verify_mode = mode; } else { - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(parms->server-> module_config, &gnutls_module); sc->client_verify_mode = mode; @@ -214,8 +214,8 @@ const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy, { int rv; const char* file; - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(parms->server-> module_config, &gnutls_module); file = ap_server_root_relative(parms->pool, arg); @@ -233,8 +233,8 @@ const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy, const char *mgs_set_enabled(cmd_parms * parms, void *dummy, const char *arg) { - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(parms->server-> + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(parms->server-> module_config, &gnutls_module); if (!strcasecmp(arg, "On")) { @@ -253,7 +253,7 @@ const char *mgs_set_enabled(cmd_parms * parms, void *dummy, void *mgs_config_server_create(apr_pool_t * p, server_rec * s) { int i; - mod_gnutls_srvconf_rec *sc = apr_pcalloc(p, sizeof(*sc)); + mgs_srvconf_rec *sc = apr_pcalloc(p, sizeof(*sc)); sc->enabled = GNUTLS_ENABLED_FALSE; @@ -261,7 +261,7 @@ void *mgs_config_server_create(apr_pool_t * p, server_rec * s) sc->privkey_x509 = NULL; sc->cert_x509 = NULL; sc->cache_timeout = apr_time_from_sec(300); - sc->cache_type = mod_gnutls_cache_dbm; + sc->cache_type = mgs_cache_dbm; sc->cache_config = ap_server_root_relative(p, "conf/gnutls_cache"); /* TODO: Make this Configurable. But it isn't configurable in mod_ssl? */ @@ -319,7 +319,7 @@ void *mgs_config_server_create(apr_pool_t * p, server_rec * s) void *mgs_config_dir_create(apr_pool_t *p, char *dir) { - mod_gnutls_dirconf_rec *dc = apr_palloc(p, sizeof(*dc)); + mgs_dirconf_rec *dc = apr_palloc(p, sizeof(*dc)); dc->client_verify_mode = -1; diff --git a/src/gnutls_hooks.c b/src/gnutls_hooks.c new file mode 100644 index 0000000..5429d66 --- /dev/null +++ b/src/gnutls_hooks.c @@ -0,0 +1,654 @@ +/** + * Copyright 2004-2005 Paul Querna + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "mod_gnutls.h" +#include "http_vhost.h" + +#if !USING_2_1_RECENT +extern server_rec *ap_server_conf; +#endif + +#if APR_HAS_THREADS +GCRY_THREAD_OPTION_PTHREAD_IMPL; +#endif + +#if MOD_GNUTLS_DEBUG +static apr_file_t* debug_log_fp; +#endif + +static apr_status_t mgs_cleanup_pre_config(void *data) +{ + gnutls_global_deinit(); + return APR_SUCCESS; +} + +#if MOD_GNUTLS_DEBUG +static void gnutls_debug_log_all( int level, const char* str) +{ + apr_file_printf(debug_log_fp, "<%d> %s\n", level, str); +} +#endif + +int mgs_hook_pre_config(apr_pool_t * pconf, + apr_pool_t * plog, apr_pool_t * ptemp) +{ + +#if APR_HAS_THREADS + /* TODO: Check MPM Type here */ + gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); +#endif + + gnutls_global_init(); + + apr_pool_cleanup_register(pconf, NULL, mgs_cleanup_pre_config, + apr_pool_cleanup_null); + +#if MOD_GNUTLS_DEBUG + apr_file_open(&debug_log_fp, "/tmp/gnutls_debug", + APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, pconf); + + gnutls_global_set_log_level(9); + gnutls_global_set_log_function(gnutls_debug_log_all); +#endif + + return OK; +} + + +static gnutls_datum load_params(const char* file, server_rec* s, + apr_pool_t* pool) +{ + gnutls_datum ret = { NULL, 0 }; + apr_file_t* fp; + apr_finfo_t finfo; + apr_status_t rv; + apr_size_t br = 0; + + rv = apr_file_open(&fp, file, APR_READ|APR_BINARY, APR_OS_DEFAULT, + pool); + if (rv != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, + "GnuTLS failed to load params file at: %s", file); + return ret; + } + + rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, fp); + + if (rv != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, + "GnuTLS failed to stat params file at: %s", file); + return ret; + } + + ret.data = apr_palloc(pool, finfo.size+1); + rv = apr_file_read_full(fp, ret.data, finfo.size, &br); + + if (rv != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, + "GnuTLS failed to read params file at: %s", file); + return ret; + } + apr_file_close(fp); + ret.data[br] = '\0'; + ret.size = br; + + return ret; +} + +int mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog, + apr_pool_t * ptemp, + server_rec * base_server) +{ + int rv; + int data_len; + server_rec *s; + gnutls_dh_params_t dh_params; + gnutls_rsa_params_t rsa_params; + mgs_srvconf_rec *sc; + mgs_srvconf_rec *sc_base; + void *data = NULL; + int first_run = 0; + const char *userdata_key = "mgs_init"; + + apr_pool_userdata_get(&data, userdata_key, base_server->process->pool); + if (data == NULL) { + first_run = 1; + apr_pool_userdata_set((const void *)1, userdata_key, + apr_pool_cleanup_null, + base_server->process->pool); + } + + + { + gnutls_datum pdata; + apr_pool_t* tpool; + s = base_server; + sc_base = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, + &gnutls_module); + + apr_pool_create(&tpool, p); + + gnutls_dh_params_init(&dh_params); + + pdata = load_params(sc_base->dh_params_file, s, tpool); + + if (pdata.size != 0) { + rv = gnutls_dh_params_import_pkcs3(dh_params, &pdata, + GNUTLS_X509_FMT_PEM); + if (rv != 0) { + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, + "GnuTLS: Unable to load DH Params: (%d) %s", + rv, gnutls_strerror(rv)); + exit(rv); + } + } + else { + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, + "GnuTLS: Unable to load DH Params." + " Shutting Down."); + exit(-1); + } + apr_pool_clear(tpool); + + gnutls_rsa_params_init(&rsa_params); + + pdata = load_params(sc_base->rsa_params_file, s, tpool); + + if (pdata.size != 0) { + rv = gnutls_rsa_params_import_pkcs1(rsa_params, &pdata, + GNUTLS_X509_FMT_PEM); + if (rv != 0) { + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, + "GnuTLS: Unable to load RSA Params: (%d) %s", + rv, gnutls_strerror(rv)); + exit(rv); + } + } + else { + ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, + "GnuTLS: Unable to load RSA Params." + " Shutting Down."); + exit(-1); + } + + apr_pool_destroy(tpool); + rv = mgs_cache_post_config(p, s, sc_base); + if (rv != 0) { + ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, + "GnuTLS: Post Config for GnuTLSCache Failed." + " Shutting Down."); + exit(-1); + } + + for (s = base_server; s; s = s->next) { + sc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, + &gnutls_module); + sc->cache_type = sc_base->cache_type; + sc->cache_config = sc_base->cache_config; + + gnutls_certificate_set_rsa_export_params(sc->certs, + rsa_params); + gnutls_certificate_set_dh_params(sc->certs, dh_params); + + if (sc->cert_x509 == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { + ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, + "[GnuTLS] - Host '%s:%d' is missing a " + "Certificate File!", + s->server_hostname, s->port); + exit(-1); + } + + if (sc->privkey_x509 == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { + ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, + "[GnuTLS] - Host '%s:%d' is missing a " + "Private Key File!", + s->server_hostname, s->port); + exit(-1); + } + + rv = gnutls_x509_crt_get_dn_by_oid(sc->cert_x509, + GNUTLS_OID_X520_COMMON_NAME, 0, 0, + NULL, &data_len); + + if (data_len < 1) { + sc->enabled = GNUTLS_ENABLED_FALSE; + sc->cert_cn = NULL; + continue; + } + + sc->cert_cn = apr_palloc(p, data_len); + rv = gnutls_x509_crt_get_dn_by_oid(sc->cert_x509, + GNUTLS_OID_X520_COMMON_NAME, 0, 0, + sc->cert_cn, &data_len); + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, + s, + "GnuTLS: sni-x509 cn: %s/%d pk: %s s: 0x%08X sc: 0x%08X", sc->cert_cn, rv, + gnutls_pk_algorithm_get_name(gnutls_x509_privkey_get_pk_algorithm(sc->privkey_x509)), + (unsigned int)s, (unsigned int)sc); + } + } + + ap_add_version_component(p, "mod_gnutls/" MOD_GNUTLS_VERSION); + + return OK; +} + +void mgs_hook_child_init(apr_pool_t *p, server_rec *s) +{ + apr_status_t rv = APR_SUCCESS; + mgs_srvconf_rec *sc = ap_get_module_config(s->module_config, + &gnutls_module); + + if (sc->cache_type != mgs_cache_none) { + rv = mgs_cache_child_init(p, s, sc); + if(rv != APR_SUCCESS) { + ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, + "[GnuTLS] - Failed to run Cache Init"); + } + } + else { + ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, + "[GnuTLS] - No Cache Configured. Hint: GnuTLSCache"); + } +} + +const char *mgs_hook_http_scheme(const request_rec * r) +{ + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(r->server-> + module_config, + &gnutls_module); + + if (sc->enabled == GNUTLS_ENABLED_FALSE) { + return NULL; + } + + return "https"; +} + +apr_port_t mgs_hook_default_port(const request_rec * r) +{ + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(r->server-> + module_config, + &gnutls_module); + + if (sc->enabled == GNUTLS_ENABLED_FALSE) { + return 0; + } + + return 443; +} + +#define MAX_HOST_LEN 255 + +#if USING_2_1_RECENT +typedef struct +{ + mgs_handle_t *ctxt; + gnutls_retr_st* ret; + const char* sni_name; +} vhost_cb_rec; + +static int vhost_cb (void* baton, conn_rec* conn, server_rec* s) +{ + mgs_srvconf_rec *tsc; + vhost_cb_rec* x = baton; + + tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, + &gnutls_module); + + if (tsc->enabled != GNUTLS_ENABLED_TRUE || tsc->cert_cn == NULL) { + return 0; + } + + /* The CN can contain a * -- this will match those too. */ + if (ap_strcasecmp_match(x->sni_name, tsc->cert_cn) == 0) { + /* found a match */ + x->ret->cert.x509 = &tsc->cert_x509; + x->ret->key.x509 = tsc->privkey_x509; +#if MOD_GNUTLS_DEBUG + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, + x->ctxt->c->base_server, + "GnuTLS: Virtual Host CB: " + "'%s' == '%s'", tsc->cert_cn, x->sni_name); +#endif + /* Because we actually change the server used here, we need to reset + * things like ClientVerify. + */ + x->ctxt->sc = tsc; + /* Shit. Crap. Dammit. We *really* should rehandshake here, as our + * certificate structure *should* change when the server changes. + * acccckkkkkk. + */ + gnutls_certificate_server_set_request(x->ctxt->session, x->ctxt->sc->client_verify_mode); + return 1; + } + return 0; +} +#endif + +static int cert_retrieve_fn(gnutls_session_t session, gnutls_retr_st* ret) +{ + int rv; + int sni_type; + int data_len = MAX_HOST_LEN; + char sni_name[MAX_HOST_LEN]; + mgs_handle_t *ctxt; +#if USING_2_1_RECENT + vhost_cb_rec cbx; +#else + server_rec* s; + mgs_srvconf_rec *tsc; +#endif + + ctxt = gnutls_transport_get_ptr(session); + + sni_type = gnutls_certificate_type_get(session); + if (sni_type != GNUTLS_CRT_X509) { + /* In theory, we could support OpenPGP Certificates. Theory != code. */ + ap_log_error(APLOG_MARK, APLOG_CRIT, 0, + ctxt->c->base_server, + "GnuTLS: Only x509 Certificates are currently supported."); + return -1; + } + + ret->type = GNUTLS_CRT_X509; + ret->ncerts = 1; + ret->deinit_all = 0; + + rv = gnutls_server_name_get(ctxt->session, sni_name, + &data_len, &sni_type, 0); + + if (rv != 0) { + goto use_default_crt; + } + + if (sni_type != GNUTLS_NAME_DNS) { + ap_log_error(APLOG_MARK, APLOG_CRIT, 0, + ctxt->c->base_server, + "GnuTLS: Unknown type '%d' for SNI: " + "'%s'", sni_type, sni_name); + goto use_default_crt; + } + + /** + * Code in the Core already sets up the c->base_server as the base + * for this IP/Port combo. Trust that the core did the 'right' thing. + */ +#if USING_2_1_RECENT + cbx.ctxt = ctxt; + cbx.ret = ret; + cbx.sni_name = sni_name; + + rv = ap_vhost_iterate_given_conn(ctxt->c, vhost_cb, &cbx); + if (rv == 1) { + return 0; + } +#else + for (s = ap_server_conf; s; s = s->next) { + + tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, + &gnutls_module); + if (tsc->enabled != GNUTLS_ENABLED_TRUE) { + continue; + } +#if MOD_GNUTLS_DEBUG + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, + ctxt->c->base_server, + "GnuTLS: sni-x509 cn: %s/%d pk: %s s: 0x%08X s->n: 0x%08X sc: 0x%08X", tsc->cert_cn, rv, + gnutls_pk_algorithm_get_name(gnutls_x509_privkey_get_pk_algorithm(ctxt->sc->privkey_x509)), + (unsigned int)s, (unsigned int)s->next, (unsigned int)tsc); +#endif + /* The CN can contain a * -- this will match those too. */ + if (ap_strcasecmp_match(sni_name, tsc->cert_cn) == 0) { + /* found a match */ + ret->cert.x509 = &tsc->cert_x509; + ret->key.x509 = tsc->privkey_x509; +#if MOD_GNUTLS_DEBUG + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, + ctxt->c->base_server, + "GnuTLS: Virtual Host: " + "'%s' == '%s'", tsc->cert_cn, sni_name); +#endif + ctxt->sc = tsc; + gnutls_certificate_server_set_request(ctxt->session, ctxt->sc->client_verify_mode); + return 0; + } + } +#endif + + /** + * If the client does not support the Server Name Indication, give the default + * certificate for this server. + */ +use_default_crt: + ret->cert.x509 = &ctxt->sc->cert_x509; + ret->key.x509 = ctxt->sc->privkey_x509; +#if MOD_GNUTLS_DEBUG + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, + ctxt->c->base_server, + "GnuTLS: Using Default Certificate."); +#endif + return 0; +} + +static mgs_handle_t* create_gnutls_handle(apr_pool_t* pool, conn_rec * c) +{ + mgs_handle_t *ctxt; + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(c->base_server-> + module_config, + &gnutls_module); + + ctxt = apr_pcalloc(pool, sizeof(*ctxt)); + ctxt->c = c; + ctxt->sc = sc; + ctxt->status = 0; + + ctxt->input_rc = APR_SUCCESS; + ctxt->input_bb = apr_brigade_create(c->pool, c->bucket_alloc); + ctxt->input_cbuf.length = 0; + + ctxt->output_rc = APR_SUCCESS; + ctxt->output_bb = apr_brigade_create(c->pool, c->bucket_alloc); + ctxt->output_blen = 0; + ctxt->output_length = 0; + + gnutls_init(&ctxt->session, GNUTLS_SERVER); + + gnutls_protocol_set_priority(ctxt->session, sc->protocol); + gnutls_cipher_set_priority(ctxt->session, sc->ciphers); + gnutls_compression_set_priority(ctxt->session, sc->compression); + gnutls_kx_set_priority(ctxt->session, sc->key_exchange); + gnutls_mac_set_priority(ctxt->session, sc->macs); + gnutls_certificate_type_set_priority(ctxt->session, sc->cert_types); + + mgs_cache_session_init(ctxt); + + gnutls_credentials_set(ctxt->session, GNUTLS_CRD_CERTIFICATE, ctxt->sc->certs); + + gnutls_certificate_server_set_retrieve_function(sc->certs, cert_retrieve_fn); + gnutls_certificate_server_set_request(ctxt->session, ctxt->sc->client_verify_mode); + return ctxt; +} + +int mgs_hook_pre_connection(conn_rec * c, void *csd) +{ + mgs_handle_t *ctxt; + mgs_srvconf_rec *sc = + (mgs_srvconf_rec *) ap_get_module_config(c->base_server-> + module_config, + &gnutls_module); + + if (!(sc && (sc->enabled == GNUTLS_ENABLED_TRUE))) { + return DECLINED; + } + + ctxt = create_gnutls_handle(c->pool, c); + + ap_set_module_config(c->conn_config, &gnutls_module, ctxt); + + gnutls_transport_set_pull_function(ctxt->session, + mgs_transport_read); + gnutls_transport_set_push_function(ctxt->session, + mgs_transport_write); + gnutls_transport_set_ptr(ctxt->session, ctxt); + + ctxt->input_filter = ap_add_input_filter(GNUTLS_INPUT_FILTER_NAME, ctxt, + NULL, c); + ctxt->output_filter = ap_add_output_filter(GNUTLS_OUTPUT_FILTER_NAME, ctxt, + NULL, c); + + return OK; +} + +int mgs_hook_fixups(request_rec *r) +{ + unsigned char sbuf[GNUTLS_MAX_SESSION_ID]; + char buf[GNUTLS_SESSION_ID_STRING_LEN]; + const char* tmp; + int len; + mgs_handle_t *ctxt; + apr_table_t *env = r->subprocess_env; + + ctxt = ap_get_module_config(r->connection->conn_config, &gnutls_module); + + if(!ctxt) { + return DECLINED; + } + + apr_table_setn(env, "HTTPS", "on"); + + apr_table_setn(env, "GNUTLS_VERSION_INTERFACE", MOD_GNUTLS_VERSION); + apr_table_setn(env, "GNUTLS_VERSION_LIBRARY", LIBGNUTLS_VERSION); + + apr_table_setn(env, "SSL_PROTOCOL", + gnutls_protocol_get_name(gnutls_protocol_get_version(ctxt->session))); + + apr_table_setn(env, "SSL_CIPHER", + gnutls_cipher_get_name(gnutls_cipher_get(ctxt->session))); + + apr_table_setn(env, "SSL_CLIENT_VERIFY", "NONE"); + + tmp = apr_psprintf(r->pool, "%d", + 8 * gnutls_cipher_get_key_size(gnutls_cipher_get(ctxt->session))); + + apr_table_setn(env, "SSL_CIPHER_USEKEYSIZE", tmp); + + apr_table_setn(env, "SSL_CIPHER_ALGKEYSIZE", tmp); + + len = sizeof(sbuf); + gnutls_session_get_id(ctxt->session, sbuf, &len); + tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf)); + apr_table_setn(env, "SSL_SESSION_ID", tmp); + + return OK; +} + +int mgs_hook_authz(request_rec *r) +{ + int rv; + int status; + mgs_handle_t *ctxt; + mgs_dirconf_rec *dc = ap_get_module_config(r->per_dir_config, + &gnutls_module); + + ctxt = ap_get_module_config(r->connection->conn_config, &gnutls_module); + + if (!ctxt) { + return DECLINED; + } + + if (!dc) { + dc = mgs_config_dir_create(r->pool, NULL); + } + + if (dc->client_verify_mode == GNUTLS_CERT_IGNORE) { + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, + "GnuTLS: Directory set to Ignore Client Certificate!"); + return DECLINED; + } + + if (ctxt->sc->client_verify_mode < dc->client_verify_mode) { + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, + "GnuTLS: Attempting to rehandshake with peer. %d %d", + ctxt->sc->client_verify_mode, dc->client_verify_mode); + + gnutls_certificate_server_set_request(ctxt->session, + dc->client_verify_mode); + + if (mgs_rehandshake(ctxt) != 0) { + return HTTP_FORBIDDEN; + } + } + else if (ctxt->sc->client_verify_mode == GNUTLS_CERT_IGNORE) { +#if MOD_GNUTLS_DEBUG + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Peer is set to IGNORE"); +#endif + return DECLINED; + } + + rv = gnutls_certificate_verify_peers2(ctxt->session, &status); + + if (rv < 0) { + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Failed to Verify Peer: (%d) %s", + rv, gnutls_strerror(rv)); + return HTTP_FORBIDDEN; + } + + if (status < 0) { + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Peer Status is invalid."); + return HTTP_FORBIDDEN; + } + + if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) { + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Could not find Signer for Peer Certificate"); + } + + if (status & GNUTLS_CERT_SIGNER_NOT_CA) { + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Could not find CA for Peer Certificate"); + } + + if (status & GNUTLS_CERT_INVALID) { + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Peer Certificate is invalid."); + return HTTP_FORBIDDEN; + } + else if (status & GNUTLS_CERT_REVOKED) { + ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, + "GnuTLS: Peer Certificate is revoked."); + return HTTP_FORBIDDEN; + } + + /* TODO: OpenPGP Certificates */ + if (gnutls_certificate_type_get(ctxt->session) != GNUTLS_CRT_X509) { + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, + "GnuTLS: Only x509 is supported for client certificates"); + return HTTP_FORBIDDEN; + } + /* TODO: Further Verification. */ + ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, + "GnuTLS: Verified Peer."); + return OK; +} + diff --git a/src/gnutls_io.c b/src/gnutls_io.c index dee2c4f..acb6095 100644 --- a/src/gnutls_io.c +++ b/src/gnutls_io.c @@ -35,7 +35,7 @@ static apr_status_t gnutls_io_filter_error(ap_filter_t * f, apr_bucket_brigade * bb, apr_status_t status) { - mod_gnutls_handle_t *ctxt = (mod_gnutls_handle_t *) f->ctx; + mgs_handle_t *ctxt = (mgs_handle_t *) f->ctx; apr_bucket *bucket; switch (status) { @@ -63,7 +63,7 @@ static apr_status_t gnutls_io_filter_error(ap_filter_t * f, return APR_SUCCESS; } -static int char_buffer_read(mod_gnutls_char_buffer_t * buffer, char *in, +static int char_buffer_read(mgs_char_buffer_t * buffer, char *in, int inl) { if (!buffer->length) { @@ -87,7 +87,7 @@ static int char_buffer_read(mod_gnutls_char_buffer_t * buffer, char *in, return inl; } -static int char_buffer_write(mod_gnutls_char_buffer_t * buffer, char *in, +static int char_buffer_write(mgs_char_buffer_t * buffer, char *in, int inl) { buffer->value = in; @@ -181,7 +181,7 @@ static apr_status_t brigade_consume(apr_bucket_brigade * bb, } -static apr_status_t gnutls_io_input_read(mod_gnutls_handle_t * ctxt, +static apr_status_t gnutls_io_input_read(mgs_handle_t * ctxt, char *buf, apr_size_t * len) { apr_size_t wanted = *len; @@ -310,7 +310,7 @@ static apr_status_t gnutls_io_input_read(mod_gnutls_handle_t * ctxt, return ctxt->input_rc; } -static apr_status_t gnutls_io_input_getline(mod_gnutls_handle_t * ctxt, +static apr_status_t gnutls_io_input_getline(mgs_handle_t * ctxt, char *buf, apr_size_t * len) { const char *pos = NULL; @@ -353,7 +353,7 @@ static apr_status_t gnutls_io_input_getline(mod_gnutls_handle_t * ctxt, return APR_SUCCESS; } -static int gnutls_do_handshake(mod_gnutls_handle_t * ctxt) +static int gnutls_do_handshake(mgs_handle_t * ctxt) { int ret; int errcode; @@ -403,7 +403,7 @@ tryagain: } } -int mod_gnutls_rehandshake(mod_gnutls_handle_t * ctxt) +int mgs_rehandshake(mgs_handle_t * ctxt) { int rv; @@ -424,14 +424,14 @@ int mod_gnutls_rehandshake(mod_gnutls_handle_t * ctxt) } -apr_status_t mod_gnutls_filter_input(ap_filter_t* f, +apr_status_t mgs_filter_input(ap_filter_t* f, apr_bucket_brigade * bb, ap_input_mode_t mode, apr_read_type_e block, apr_off_t readbytes) { apr_status_t status = APR_SUCCESS; - mod_gnutls_handle_t *ctxt = (mod_gnutls_handle_t *) f->ctx; + mgs_handle_t *ctxt = (mgs_handle_t *) f->ctx; apr_size_t len = sizeof(ctxt->input_buffer); if (f->c->aborted) { @@ -488,12 +488,12 @@ apr_status_t mod_gnutls_filter_input(ap_filter_t* f, return status; } -apr_status_t mod_gnutls_filter_output(ap_filter_t * f, +apr_status_t mgs_filter_output(ap_filter_t * f, apr_bucket_brigade * bb) { apr_size_t ret; apr_bucket* e; - mod_gnutls_handle_t *ctxt = (mod_gnutls_handle_t *) f->ctx; + mgs_handle_t *ctxt = (mgs_handle_t *) f->ctx; apr_status_t status = APR_SUCCESS; apr_read_type_e rblock = APR_NONBLOCK_READ; @@ -592,10 +592,10 @@ apr_status_t mod_gnutls_filter_output(ap_filter_t * f, return status; } -ssize_t mod_gnutls_transport_read(gnutls_transport_ptr_t ptr, +ssize_t mgs_transport_read(gnutls_transport_ptr_t ptr, void *buffer, size_t len) { - mod_gnutls_handle_t *ctxt = ptr; + mgs_handle_t *ctxt = ptr; apr_status_t rc; apr_size_t in = len; apr_read_type_e block = ctxt->input_block; @@ -659,7 +659,7 @@ ssize_t mod_gnutls_transport_read(gnutls_transport_ptr_t ptr, } -static ssize_t write_flush(mod_gnutls_handle_t * ctxt) +static ssize_t write_flush(mgs_handle_t * ctxt) { apr_bucket *e; @@ -691,10 +691,10 @@ static ssize_t write_flush(mod_gnutls_handle_t * ctxt) return (ctxt->output_rc == APR_SUCCESS) ? 1 : -1; } -ssize_t mod_gnutls_transport_write(gnutls_transport_ptr_t ptr, +ssize_t mgs_transport_write(gnutls_transport_ptr_t ptr, const void *buffer, size_t len) { - mod_gnutls_handle_t *ctxt = ptr; + mgs_handle_t *ctxt = ptr; /* pass along the encrypted data * need to flush since we're using SSL's malloc-ed buffer diff --git a/src/mod_gnutls.c b/src/mod_gnutls.c index fbcbc52..a2c36ee 100644 --- a/src/mod_gnutls.c +++ b/src/mod_gnutls.c @@ -16,676 +16,45 @@ */ #include "mod_gnutls.h" -#include "http_vhost.h" -extern server_rec *ap_server_conf; - -#if APR_HAS_THREADS -GCRY_THREAD_OPTION_PTHREAD_IMPL; -#endif - -#if MOD_GNUTLS_DEBUG -static apr_file_t* debug_log_fp; -#endif - -static apr_status_t mod_gnutls_cleanup_pre_config(void *data) -{ - gnutls_global_deinit(); - return APR_SUCCESS; -} - -#if MOD_GNUTLS_DEBUG -static void gnutls_debug_log_all( int level, const char* str) -{ - apr_file_printf(debug_log_fp, "<%d> %s\n", level, str); -} -#endif - -static int mod_gnutls_hook_pre_config(apr_pool_t * pconf, - apr_pool_t * plog, apr_pool_t * ptemp) -{ - -#if APR_HAS_THREADS - /* TODO: Check MPM Type here */ - gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); -#endif - - gnutls_global_init(); - - apr_pool_cleanup_register(pconf, NULL, mod_gnutls_cleanup_pre_config, - apr_pool_cleanup_null); - -#if MOD_GNUTLS_DEBUG - apr_file_open(&debug_log_fp, "/tmp/gnutls_debug", - APR_APPEND|APR_WRITE|APR_CREATE, APR_OS_DEFAULT, pconf); - - gnutls_global_set_log_level(9); - gnutls_global_set_log_function(gnutls_debug_log_all); -#endif - - return OK; -} - - -static gnutls_datum load_params(const char* file, server_rec* s, - apr_pool_t* pool) -{ - gnutls_datum ret = { NULL, 0 }; - apr_file_t* fp; - apr_finfo_t finfo; - apr_status_t rv; - apr_size_t br = 0; - - rv = apr_file_open(&fp, file, APR_READ|APR_BINARY, APR_OS_DEFAULT, - pool); - if (rv != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, - "GnuTLS failed to load params file at: %s", file); - return ret; - } - - rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, fp); - - if (rv != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, - "GnuTLS failed to stat params file at: %s", file); - return ret; - } - - ret.data = apr_palloc(pool, finfo.size+1); - rv = apr_file_read_full(fp, ret.data, finfo.size, &br); - - if (rv != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, - "GnuTLS failed to read params file at: %s", file); - return ret; - } - apr_file_close(fp); - ret.data[br] = '\0'; - ret.size = br; - - return ret; -} - -static int mod_gnutls_hook_post_config(apr_pool_t * p, apr_pool_t * plog, - apr_pool_t * ptemp, - server_rec * base_server) -{ - int rv; - int data_len; - server_rec *s; - gnutls_dh_params_t dh_params; - gnutls_rsa_params_t rsa_params; - mod_gnutls_srvconf_rec *sc; - mod_gnutls_srvconf_rec *sc_base; - void *data = NULL; - int first_run = 0; - const char *userdata_key = "mod_gnutls_init"; - - apr_pool_userdata_get(&data, userdata_key, base_server->process->pool); - if (data == NULL) { - first_run = 1; - apr_pool_userdata_set((const void *)1, userdata_key, - apr_pool_cleanup_null, - base_server->process->pool); - } - - - { - gnutls_datum pdata; - apr_pool_t* tpool; - s = base_server; - sc_base = (mod_gnutls_srvconf_rec *) ap_get_module_config(s->module_config, - &gnutls_module); - - apr_pool_create(&tpool, p); - - gnutls_dh_params_init(&dh_params); - - pdata = load_params(sc_base->dh_params_file, s, tpool); - - if (pdata.size != 0) { - rv = gnutls_dh_params_import_pkcs3(dh_params, &pdata, - GNUTLS_X509_FMT_PEM); - if (rv != 0) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, - "GnuTLS: Unable to load DH Params: (%d) %s", - rv, gnutls_strerror(rv)); - exit(rv); - } - } - else { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, - "GnuTLS: Unable to load DH Params." - " Shutting Down."); - exit(-1); - } - apr_pool_clear(tpool); - - gnutls_rsa_params_init(&rsa_params); - - pdata = load_params(sc_base->rsa_params_file, s, tpool); - - if (pdata.size != 0) { - rv = gnutls_rsa_params_import_pkcs1(rsa_params, &pdata, - GNUTLS_X509_FMT_PEM); - if (rv != 0) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, - "GnuTLS: Unable to load RSA Params: (%d) %s", - rv, gnutls_strerror(rv)); - exit(rv); - } - } - else { - ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, - "GnuTLS: Unable to load RSA Params." - " Shutting Down."); - exit(-1); - } - - apr_pool_destroy(tpool); - rv = mod_gnutls_cache_post_config(p, s, sc_base); - if (rv != 0) { - ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, - "GnuTLS: Post Config for GnuTLSCache Failed." - " Shutting Down."); - exit(-1); - } - - for (s = base_server; s; s = s->next) { - sc = (mod_gnutls_srvconf_rec *) ap_get_module_config(s->module_config, - &gnutls_module); - sc->cache_type = sc_base->cache_type; - sc->cache_config = sc_base->cache_config; - - gnutls_certificate_set_rsa_export_params(sc->certs, - rsa_params); - gnutls_certificate_set_dh_params(sc->certs, dh_params); - - if (sc->cert_x509 == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { - ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, - "[GnuTLS] - Host '%s:%d' is missing a " - "Certificate File!", - s->server_hostname, s->port); - exit(-1); - } - - if (sc->privkey_x509 == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { - ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, - "[GnuTLS] - Host '%s:%d' is missing a " - "Private Key File!", - s->server_hostname, s->port); - exit(-1); - } - - rv = gnutls_x509_crt_get_dn_by_oid(sc->cert_x509, - GNUTLS_OID_X520_COMMON_NAME, 0, 0, - NULL, &data_len); - - if (data_len < 1) { - sc->enabled = GNUTLS_ENABLED_FALSE; - sc->cert_cn = NULL; - continue; - } - - sc->cert_cn = apr_palloc(p, data_len); - rv = gnutls_x509_crt_get_dn_by_oid(sc->cert_x509, - GNUTLS_OID_X520_COMMON_NAME, 0, 0, - sc->cert_cn, &data_len); - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - s, - "GnuTLS: sni-x509 cn: %s/%d pk: %s s: 0x%08X sc: 0x%08X", sc->cert_cn, rv, - gnutls_pk_algorithm_get_name(gnutls_x509_privkey_get_pk_algorithm(sc->privkey_x509)), - (unsigned int)s, (unsigned int)sc); - } - } - - ap_add_version_component(p, "mod_gnutls/" MOD_GNUTLS_VERSION); - - return OK; -} - -static void mod_gnutls_hook_child_init(apr_pool_t *p, server_rec *s) -{ - apr_status_t rv = APR_SUCCESS; - mod_gnutls_srvconf_rec *sc = ap_get_module_config(s->module_config, - &gnutls_module); - - if (sc->cache_type != mod_gnutls_cache_none) { - rv = mod_gnutls_cache_child_init(p, s, sc); - if(rv != APR_SUCCESS) { - ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, - "[GnuTLS] - Failed to run Cache Init"); - } - } - else { - ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, - "[GnuTLS] - No Cache Configured. Hint: GnuTLSCache"); - } -} - -static const char *mod_gnutls_hook_http_scheme(const request_rec * r) -{ - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(r->server-> - module_config, - &gnutls_module); - - if (sc->enabled == GNUTLS_ENABLED_FALSE) { - return NULL; - } - - return "https"; -} - -static apr_port_t mod_gnutls_hook_default_port(const request_rec * r) -{ - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(r->server-> - module_config, - &gnutls_module); - - if (sc->enabled == GNUTLS_ENABLED_FALSE) { - return 0; - } - - return 443; -} - -#define MAX_HOST_LEN 255 - -#if USING_2_1_RECENT -typedef struct -{ - mod_gnutls_handle_t *ctxt; - gnutls_retr_st* ret; - const char* sni_name; -} vhost_cb_rec; - -int vhost_cb (void* baton, conn_rec* conn, server_rec* s) -{ - mod_gnutls_srvconf_rec *tsc; - vhost_cb_rec* x = baton; - - tsc = (mod_gnutls_srvconf_rec *) ap_get_module_config(s->module_config, - &gnutls_module); - - if (tsc->enabled != GNUTLS_ENABLED_TRUE || tsc->cert_cn == NULL) { - return 0; - } - - /* The CN can contain a * -- this will match those too. */ - if (ap_strcasecmp_match(x->sni_name, tsc->cert_cn) == 0) { - /* found a match */ - x->ret->cert.x509 = &tsc->cert_x509; - x->ret->key.x509 = tsc->privkey_x509; -#if MOD_GNUTLS_DEBUG - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - x->ctxt->c->base_server, - "GnuTLS: Virtual Host CB: " - "'%s' == '%s'", tsc->cert_cn, x->sni_name); -#endif - /* Because we actually change the server used here, we need to reset - * things like ClientVerify. - */ - x->ctxt->sc = tsc; - /* Shit. Crap. Dammit. We *really* should rehandshake here, as our - * certificate structure *should* change when the server changes. - * acccckkkkkk. - */ - gnutls_certificate_server_set_request(x->ctxt->session, x->ctxt->sc->client_verify_mode); - return 1; - } - return 0; -} -#endif - -static int cert_retrieve_fn(gnutls_session_t session, gnutls_retr_st* ret) -{ - int rv; - int sni_type; - int data_len = MAX_HOST_LEN; - char sni_name[MAX_HOST_LEN]; - mod_gnutls_handle_t *ctxt; -#if USING_2_1_RECENT - vhost_cb_rec cbx; -#else - server_rec* s; - mod_gnutls_srvconf_rec *tsc; -#endif - - ctxt = gnutls_transport_get_ptr(session); - - sni_type = gnutls_certificate_type_get(session); - if (sni_type != GNUTLS_CRT_X509) { - /* In theory, we could support OpenPGP Certificates. Theory != code. */ - ap_log_error(APLOG_MARK, APLOG_CRIT, 0, - ctxt->c->base_server, - "GnuTLS: Only x509 Certificates are currently supported."); - return -1; - } - - ret->type = GNUTLS_CRT_X509; - ret->ncerts = 1; - ret->deinit_all = 0; - - rv = gnutls_server_name_get(ctxt->session, sni_name, - &data_len, &sni_type, 0); - - if (rv != 0) { - goto use_default_crt; - } - - if (sni_type != GNUTLS_NAME_DNS) { - ap_log_error(APLOG_MARK, APLOG_CRIT, 0, - ctxt->c->base_server, - "GnuTLS: Unknown type '%d' for SNI: " - "'%s'", sni_type, sni_name); - goto use_default_crt; - } - - /** - * Code in the Core already sets up the c->base_server as the base - * for this IP/Port combo. Trust that the core did the 'right' thing. - */ -#if USING_2_1_RECENT - cbx.ctxt = ctxt; - cbx.ret = ret; - cbx.sni_name = sni_name; - - rv = ap_vhost_iterate_given_conn(ctxt->c, vhost_cb, &cbx); - if (rv == 1) { - return 0; - } -#else - for (s = ap_server_conf; s; s = s->next) { - - tsc = (mod_gnutls_srvconf_rec *) ap_get_module_config(s->module_config, - &gnutls_module); - if (tsc->enabled != GNUTLS_ENABLED_TRUE) { - continue; - } -#if MOD_GNUTLS_DEBUG - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - ctxt->c->base_server, - "GnuTLS: sni-x509 cn: %s/%d pk: %s s: 0x%08X s->n: 0x%08X sc: 0x%08X", tsc->cert_cn, rv, - gnutls_pk_algorithm_get_name(gnutls_x509_privkey_get_pk_algorithm(ctxt->sc->privkey_x509)), - (unsigned int)s, (unsigned int)s->next, (unsigned int)tsc); -#endif - /* The CN can contain a * -- this will match those too. */ - if (ap_strcasecmp_match(sni_name, tsc->cert_cn) == 0) { - /* found a match */ - ret->cert.x509 = &tsc->cert_x509; - ret->key.x509 = tsc->privkey_x509; -#if MOD_GNUTLS_DEBUG - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - ctxt->c->base_server, - "GnuTLS: Virtual Host: " - "'%s' == '%s'", tsc->cert_cn, sni_name); -#endif - ctxt->sc = tsc; - gnutls_certificate_server_set_request(ctxt->session, ctxt->sc->client_verify_mode); - return 0; - } - } -#endif - - /** - * If the client does not support the Server Name Indication, give the default - * certificate for this server. - */ -use_default_crt: - ret->cert.x509 = &ctxt->sc->cert_x509; - ret->key.x509 = ctxt->sc->privkey_x509; -#if MOD_GNUTLS_DEBUG - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, - ctxt->c->base_server, - "GnuTLS: Using Default Certificate."); -#endif - return 0; -} - -static mod_gnutls_handle_t* create_gnutls_handle(apr_pool_t* pool, conn_rec * c) -{ - mod_gnutls_handle_t *ctxt; - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(c->base_server-> - module_config, - &gnutls_module); - - ctxt = apr_pcalloc(pool, sizeof(*ctxt)); - ctxt->c = c; - ctxt->sc = sc; - ctxt->status = 0; - - ctxt->input_rc = APR_SUCCESS; - ctxt->input_bb = apr_brigade_create(c->pool, c->bucket_alloc); - ctxt->input_cbuf.length = 0; - - ctxt->output_rc = APR_SUCCESS; - ctxt->output_bb = apr_brigade_create(c->pool, c->bucket_alloc); - ctxt->output_blen = 0; - ctxt->output_length = 0; - - gnutls_init(&ctxt->session, GNUTLS_SERVER); - - gnutls_protocol_set_priority(ctxt->session, sc->protocol); - gnutls_cipher_set_priority(ctxt->session, sc->ciphers); - gnutls_compression_set_priority(ctxt->session, sc->compression); - gnutls_kx_set_priority(ctxt->session, sc->key_exchange); - gnutls_mac_set_priority(ctxt->session, sc->macs); - gnutls_certificate_type_set_priority(ctxt->session, sc->cert_types); - - mod_gnutls_cache_session_init(ctxt); - - gnutls_credentials_set(ctxt->session, GNUTLS_CRD_CERTIFICATE, ctxt->sc->certs); - - gnutls_certificate_server_set_retrieve_function(sc->certs, cert_retrieve_fn); - gnutls_certificate_server_set_request(ctxt->session, ctxt->sc->client_verify_mode); - return ctxt; -} - -static int mod_gnutls_hook_pre_connection(conn_rec * c, void *csd) -{ - mod_gnutls_handle_t *ctxt; - mod_gnutls_srvconf_rec *sc = - (mod_gnutls_srvconf_rec *) ap_get_module_config(c->base_server-> - module_config, - &gnutls_module); - - if (!(sc && (sc->enabled == GNUTLS_ENABLED_TRUE))) { - return DECLINED; - } - - ctxt = create_gnutls_handle(c->pool, c); - - ap_set_module_config(c->conn_config, &gnutls_module, ctxt); - - gnutls_transport_set_pull_function(ctxt->session, - mod_gnutls_transport_read); - gnutls_transport_set_push_function(ctxt->session, - mod_gnutls_transport_write); - gnutls_transport_set_ptr(ctxt->session, ctxt); - - ctxt->input_filter = ap_add_input_filter(GNUTLS_INPUT_FILTER_NAME, ctxt, - NULL, c); - ctxt->output_filter = ap_add_output_filter(GNUTLS_OUTPUT_FILTER_NAME, ctxt, - NULL, c); - - return OK; -} - -static int mod_gnutls_hook_fixups(request_rec *r) -{ - unsigned char sbuf[GNUTLS_MAX_SESSION_ID]; - char buf[GNUTLS_SESSION_ID_STRING_LEN]; - const char* tmp; - int len; - mod_gnutls_handle_t *ctxt; - apr_table_t *env = r->subprocess_env; - - ctxt = ap_get_module_config(r->connection->conn_config, &gnutls_module); - - if(!ctxt) { - return DECLINED; - } - - apr_table_setn(env, "HTTPS", "on"); - - apr_table_setn(env, "GNUTLS_VERSION_INTERFACE", MOD_GNUTLS_VERSION); - apr_table_setn(env, "GNUTLS_VERSION_LIBRARY", LIBGNUTLS_VERSION); - - apr_table_setn(env, "SSL_PROTOCOL", - gnutls_protocol_get_name(gnutls_protocol_get_version(ctxt->session))); - - apr_table_setn(env, "SSL_CIPHER", - gnutls_cipher_get_name(gnutls_cipher_get(ctxt->session))); - - apr_table_setn(env, "SSL_CLIENT_VERIFY", "NONE"); - - tmp = apr_psprintf(r->pool, "%d", - 8 * gnutls_cipher_get_key_size(gnutls_cipher_get(ctxt->session))); - - apr_table_setn(env, "SSL_CIPHER_USEKEYSIZE", tmp); - - apr_table_setn(env, "SSL_CIPHER_ALGKEYSIZE", tmp); - - len = sizeof(sbuf); - gnutls_session_get_id(ctxt->session, sbuf, &len); - tmp = mod_gnutls_session_id2sz(sbuf, len, buf, sizeof(buf)); - apr_table_setn(env, "SSL_SESSION_ID", tmp); - - return OK; -} - -int mod_gnutls_hook_authz(request_rec *r) -{ - int rv; - int status; - mod_gnutls_handle_t *ctxt; - mod_gnutls_dirconf_rec *dc = ap_get_module_config(r->per_dir_config, - &gnutls_module); - - ctxt = ap_get_module_config(r->connection->conn_config, &gnutls_module); - - if (!ctxt) { - return DECLINED; - } - - if (!dc) { - dc = mgs_config_dir_create(r->pool, NULL); - } - - if (dc->client_verify_mode == GNUTLS_CERT_IGNORE) { - ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, - "GnuTLS: Directory set to Ignore Client Certificate!"); - return DECLINED; - } - - if (ctxt->sc->client_verify_mode < dc->client_verify_mode) { - ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, - "GnuTLS: Attempting to rehandshake with peer. %d %d", - ctxt->sc->client_verify_mode, dc->client_verify_mode); - - gnutls_certificate_server_set_request(ctxt->session, - dc->client_verify_mode); - - if (mod_gnutls_rehandshake(ctxt) != 0) { - return HTTP_FORBIDDEN; - } - } - else if (ctxt->sc->client_verify_mode == GNUTLS_CERT_IGNORE) { -#if MOD_GNUTLS_DEBUG - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Peer is set to IGNORE"); -#endif - return DECLINED; - } - - rv = gnutls_certificate_verify_peers2(ctxt->session, &status); - - if (rv < 0) { - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Failed to Verify Peer: (%d) %s", - rv, gnutls_strerror(rv)); - return HTTP_FORBIDDEN; - } - - if (status < 0) { - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Peer Status is invalid."); - return HTTP_FORBIDDEN; - } - - if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) { - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Could not find Signer for Peer Certificate"); - } - - if (status & GNUTLS_CERT_SIGNER_NOT_CA) { - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Could not find CA for Peer Certificate"); - } - - if (status & GNUTLS_CERT_INVALID) { - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Peer Certificate is invalid."); - return HTTP_FORBIDDEN; - } - else if (status & GNUTLS_CERT_REVOKED) { - ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, - "GnuTLS: Peer Certificate is revoked."); - return HTTP_FORBIDDEN; - } - - /* TODO: OpenPGP Certificates */ - if (gnutls_certificate_type_get(ctxt->session) != GNUTLS_CRT_X509) { - ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, - "GnuTLS: Only x509 is supported for client certificates"); - return HTTP_FORBIDDEN; - } - /* TODO: Further Verification. */ - ap_log_rerror(APLOG_MARK, APLOG_CRIT, 0, r, - "GnuTLS: Verified Peer."); - return OK; -} static void gnutls_hooks(apr_pool_t * p) { - ap_hook_pre_connection(mod_gnutls_hook_pre_connection, NULL, NULL, + ap_hook_pre_connection(mgs_hook_pre_connection, NULL, NULL, APR_HOOK_MIDDLE); - ap_hook_post_config(mod_gnutls_hook_post_config, NULL, NULL, - APR_HOOK_MIDDLE); - ap_hook_child_init(mod_gnutls_hook_child_init, NULL, NULL, + ap_hook_post_config(mgs_hook_post_config, NULL, NULL, APR_HOOK_MIDDLE); + ap_hook_child_init(mgs_hook_child_init, NULL, NULL, + APR_HOOK_MIDDLE); #if USING_2_1_RECENT - ap_hook_http_scheme(mod_gnutls_hook_http_scheme, NULL, NULL, + ap_hook_http_scheme(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE); #else - ap_hook_http_method(mod_gnutls_hook_http_scheme, NULL, NULL, + ap_hook_http_method(mgs_hook_http_scheme, NULL, NULL, APR_HOOK_MIDDLE); #endif - ap_hook_default_port(mod_gnutls_hook_default_port, NULL, NULL, + ap_hook_default_port(mgs_hook_default_port, NULL, NULL, APR_HOOK_MIDDLE); - ap_hook_pre_config(mod_gnutls_hook_pre_config, NULL, NULL, + ap_hook_pre_config(mgs_hook_pre_config, NULL, NULL, APR_HOOK_MIDDLE); - ap_hook_access_checker(mod_gnutls_hook_authz, NULL, NULL, APR_HOOK_REALLY_FIRST); - - ap_hook_fixups(mod_gnutls_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST); - + ap_hook_access_checker(mgs_hook_authz, NULL, NULL, APR_HOOK_REALLY_FIRST); + + ap_hook_fixups(mgs_hook_fixups, NULL, NULL, APR_HOOK_REALLY_FIRST); + /* TODO: HTTP Upgrade Filter */ /* ap_register_output_filter ("UPGRADE_FILTER", - * ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5); - */ + * ssl_io_filter_Upgrade, NULL, AP_FTYPE_PROTOCOL + 5); +*/ ap_register_input_filter(GNUTLS_INPUT_FILTER_NAME, - mod_gnutls_filter_input, NULL, + mgs_filter_input, NULL, AP_FTYPE_CONNECTION + 5); ap_register_output_filter(GNUTLS_OUTPUT_FILTER_NAME, - mod_gnutls_filter_output, NULL, + mgs_filter_output, NULL, AP_FTYPE_CONNECTION + 5); } + static const command_rec mgs_config_cmds[] = { AP_INIT_TAKE1("GnuTLSClientVerify", mgs_set_client_verify, NULL, -- cgit