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