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