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