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