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