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