[c301152] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[7bebb42] | 3 | * Copyright 2007 Nikos Mavrogiannopoulos |
---|
[c301152] | 4 | * |
---|
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 6 | * you may not use this file except in compliance with the License. |
---|
| 7 | * You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "mod_gnutls.h" |
---|
| 20 | #include "http_vhost.h" |
---|
[84cb5b2] | 21 | #include "ap_mpm.h" |
---|
[c301152] | 22 | |
---|
| 23 | #if !USING_2_1_RECENT |
---|
| 24 | extern server_rec *ap_server_conf; |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | #if APR_HAS_THREADS |
---|
| 28 | GCRY_THREAD_OPTION_PTHREAD_IMPL; |
---|
| 29 | #endif |
---|
| 30 | |
---|
| 31 | #if MOD_GNUTLS_DEBUG |
---|
[7bebb42] | 32 | static apr_file_t *debug_log_fp; |
---|
[c301152] | 33 | #endif |
---|
| 34 | |
---|
[84cb5b2] | 35 | static int mpm_is_threaded; |
---|
[ed07ddf] | 36 | static gnutls_datum session_ticket_key = { NULL, 0 }; |
---|
[84cb5b2] | 37 | |
---|
[7bebb42] | 38 | static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt); |
---|
| 39 | /* use side==0 for server and side==1 for client */ |
---|
[e5bbda4] | 40 | static void mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, |
---|
| 41 | int side, |
---|
| 42 | int export_certificates_enabled); |
---|
| 43 | static void mgs_add_common_pgpcert_vars(request_rec * r, gnutls_openpgp_crt_t cert, |
---|
[fd73a08] | 44 | int side, |
---|
| 45 | int export_certificates_enabled); |
---|
[7bebb42] | 46 | |
---|
[c301152] | 47 | static apr_status_t mgs_cleanup_pre_config(void *data) |
---|
| 48 | { |
---|
[8e98518] | 49 | gnutls_free(session_ticket_key.data); |
---|
| 50 | session_ticket_key.data = NULL; |
---|
| 51 | session_ticket_key.size = 0; |
---|
[c301152] | 52 | gnutls_global_deinit(); |
---|
| 53 | return APR_SUCCESS; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | #if MOD_GNUTLS_DEBUG |
---|
[7bebb42] | 57 | static void gnutls_debug_log_all(int level, const char *str) |
---|
[c301152] | 58 | { |
---|
| 59 | apr_file_printf(debug_log_fp, "<%d> %s\n", level, str); |
---|
| 60 | } |
---|
[a208cd3] | 61 | #define _gnutls_log apr_file_printf |
---|
| 62 | #else |
---|
| 63 | # define _gnutls_log(...) |
---|
[c301152] | 64 | #endif |
---|
| 65 | |
---|
[7bebb42] | 66 | int |
---|
| 67 | mgs_hook_pre_config(apr_pool_t * pconf, |
---|
| 68 | apr_pool_t * plog, apr_pool_t * ptemp) |
---|
[c301152] | 69 | { |
---|
[b077bdd] | 70 | int ret; |
---|
[c301152] | 71 | |
---|
[26b08fd] | 72 | #if MOD_GNUTLS_DEBUG |
---|
| 73 | apr_file_open(&debug_log_fp, "/tmp/gnutls_debug", |
---|
| 74 | APR_APPEND | APR_WRITE | APR_CREATE, APR_OS_DEFAULT, |
---|
| 75 | pconf); |
---|
| 76 | |
---|
[a208cd3] | 77 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[26b08fd] | 78 | |
---|
| 79 | gnutls_global_set_log_level(9); |
---|
| 80 | gnutls_global_set_log_function(gnutls_debug_log_all); |
---|
[a208cd3] | 81 | _gnutls_log(debug_log_fp, "gnutls: %s\n", gnutls_check_version(NULL)); |
---|
[26b08fd] | 82 | #endif |
---|
| 83 | |
---|
[c301152] | 84 | #if APR_HAS_THREADS |
---|
[84cb5b2] | 85 | ap_mpm_query(AP_MPMQ_IS_THREADED, &mpm_is_threaded); |
---|
| 86 | if (mpm_is_threaded) { |
---|
[7bebb42] | 87 | gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread); |
---|
[84cb5b2] | 88 | } |
---|
| 89 | #else |
---|
| 90 | mpm_is_threaded = 0; |
---|
[c301152] | 91 | #endif |
---|
| 92 | |
---|
[2b3a248b] | 93 | if (gnutls_check_version(LIBGNUTLS_VERSION)==NULL) { |
---|
[a208cd3] | 94 | _gnutls_log(debug_log_fp, "gnutls_check_version() failed. Required: gnutls-%s Found: gnutls-%s\n", |
---|
[2b3a248b] | 95 | LIBGNUTLS_VERSION, gnutls_check_version(NULL)); |
---|
| 96 | return -3; |
---|
| 97 | } |
---|
| 98 | |
---|
[b077bdd] | 99 | ret = gnutls_global_init(); |
---|
[2b3a248b] | 100 | if (ret < 0) { |
---|
[a208cd3] | 101 | _gnutls_log(debug_log_fp, "gnutls_global_init: %s\n", gnutls_strerror(ret)); |
---|
[8bd529f] | 102 | return -3; |
---|
| 103 | } |
---|
[ed07ddf] | 104 | |
---|
| 105 | ret = gnutls_session_ticket_key_generate( &session_ticket_key); |
---|
| 106 | if (ret < 0) { |
---|
| 107 | _gnutls_log(debug_log_fp, "gnutls_session_ticket_key_generate: %s\n", gnutls_strerror(ret)); |
---|
| 108 | } |
---|
[e5bbda4] | 109 | |
---|
[c301152] | 110 | apr_pool_cleanup_register(pconf, NULL, mgs_cleanup_pre_config, |
---|
[7bebb42] | 111 | apr_pool_cleanup_null); |
---|
[c301152] | 112 | |
---|
| 113 | |
---|
| 114 | return OK; |
---|
| 115 | } |
---|
| 116 | |
---|
[fd73a08] | 117 | static int mgs_select_virtual_server_cb(gnutls_session_t session) |
---|
[7bebb42] | 118 | { |
---|
| 119 | mgs_handle_t *ctxt; |
---|
| 120 | mgs_srvconf_rec *tsc; |
---|
| 121 | int ret; |
---|
[2b3a248b] | 122 | int cprio[2]; |
---|
[7bebb42] | 123 | |
---|
[a208cd3] | 124 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[26b08fd] | 125 | |
---|
[7bebb42] | 126 | ctxt = gnutls_transport_get_ptr(session); |
---|
| 127 | |
---|
| 128 | /* find the virtual server */ |
---|
| 129 | tsc = mgs_find_sni_server(session); |
---|
| 130 | |
---|
| 131 | if (tsc != NULL) |
---|
| 132 | ctxt->sc = tsc; |
---|
| 133 | |
---|
| 134 | gnutls_certificate_server_set_request(session, |
---|
[1c90c18] | 135 | ctxt->sc->client_verify_mode); |
---|
[7bebb42] | 136 | |
---|
| 137 | /* set the new server credentials |
---|
| 138 | */ |
---|
| 139 | |
---|
| 140 | gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, |
---|
[fd73a08] | 141 | ctxt->sc->certs); |
---|
[7bebb42] | 142 | |
---|
[fd73a08] | 143 | gnutls_credentials_set(session, GNUTLS_CRD_ANON, ctxt->sc->anon_creds); |
---|
[7bebb42] | 144 | |
---|
[787dab7] | 145 | #ifdef ENABLE_SRP |
---|
[fd73a08] | 146 | if (ctxt->sc->srp_tpasswd_conf_file != NULL |
---|
| 147 | && ctxt->sc->srp_tpasswd_file != NULL) { |
---|
| 148 | gnutls_credentials_set(session, GNUTLS_CRD_SRP, |
---|
| 149 | ctxt->sc->srp_creds); |
---|
[7bebb42] | 150 | } |
---|
[787dab7] | 151 | #endif |
---|
[7bebb42] | 152 | |
---|
| 153 | /* update the priorities - to avoid negotiating a ciphersuite that is not |
---|
| 154 | * enabled on this virtual server. Note that here we ignore the version |
---|
| 155 | * negotiation. |
---|
[ed07ddf] | 156 | */ |
---|
[fd73a08] | 157 | ret = gnutls_priority_set(session, ctxt->sc->priorities); |
---|
[2b3a248b] | 158 | /* actually it shouldn't fail since we have checked at startup */ |
---|
| 159 | if (ret < 0) |
---|
| 160 | return ret; |
---|
[fd73a08] | 161 | |
---|
[2b3a248b] | 162 | /* If both certificate types are not present disallow them from |
---|
| 163 | * being negotiated. |
---|
| 164 | */ |
---|
| 165 | if (ctxt->sc->certs_x509[0] != NULL && ctxt->sc->cert_pgp == NULL) { |
---|
[e5bbda4] | 166 | cprio[0] = GNUTLS_CRT_X509; |
---|
| 167 | cprio[1] = 0; |
---|
| 168 | gnutls_certificate_type_set_priority( session, cprio); |
---|
[2b3a248b] | 169 | } else if (ctxt->sc->cert_pgp != NULL && ctxt->sc->certs_x509[0]==NULL) { |
---|
[e5bbda4] | 170 | cprio[0] = GNUTLS_CRT_OPENPGP; |
---|
| 171 | cprio[1] = 0; |
---|
| 172 | gnutls_certificate_type_set_priority( session, cprio); |
---|
| 173 | } |
---|
[fd73a08] | 174 | |
---|
[7bebb42] | 175 | return 0; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | static int cert_retrieve_fn(gnutls_session_t session, gnutls_retr_st * ret) |
---|
| 179 | { |
---|
| 180 | mgs_handle_t *ctxt; |
---|
| 181 | |
---|
[a208cd3] | 182 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[7bebb42] | 183 | ctxt = gnutls_transport_get_ptr(session); |
---|
| 184 | |
---|
[df34f08] | 185 | if (ctxt == NULL) |
---|
| 186 | return GNUTLS_E_INTERNAL_ERROR; |
---|
| 187 | |
---|
[e5bbda4] | 188 | if (gnutls_certificate_type_get( session) == GNUTLS_CRT_X509) { |
---|
| 189 | ret->type = GNUTLS_CRT_X509; |
---|
| 190 | ret->ncerts = ctxt->sc->certs_x509_num; |
---|
| 191 | ret->deinit_all = 0; |
---|
| 192 | |
---|
| 193 | ret->cert.x509 = ctxt->sc->certs_x509; |
---|
| 194 | ret->key.x509 = ctxt->sc->privkey_x509; |
---|
| 195 | |
---|
| 196 | return 0; |
---|
| 197 | } else if (gnutls_certificate_type_get( session) == GNUTLS_CRT_OPENPGP) { |
---|
| 198 | ret->type = GNUTLS_CRT_OPENPGP; |
---|
| 199 | ret->ncerts = 1; |
---|
| 200 | ret->deinit_all = 0; |
---|
| 201 | |
---|
| 202 | ret->cert.pgp = ctxt->sc->cert_pgp; |
---|
| 203 | ret->key.pgp = ctxt->sc->privkey_pgp; |
---|
| 204 | |
---|
| 205 | return 0; |
---|
| 206 | |
---|
| 207 | } |
---|
[7bebb42] | 208 | |
---|
[e5bbda4] | 209 | return GNUTLS_E_INTERNAL_ERROR; |
---|
[7bebb42] | 210 | } |
---|
| 211 | |
---|
[2b3a248b] | 212 | /* 2048-bit group parameters from SRP specification */ |
---|
[7bebb42] | 213 | const char static_dh_params[] = "-----BEGIN DH PARAMETERS-----\n" |
---|
[fd73a08] | 214 | "MIIBBwKCAQCsa9tBMkqam/Fm3l4TiVgvr3K2ZRmH7gf8MZKUPbVgUKNzKcu0oJnt\n" |
---|
| 215 | "gZPgdXdnoT3VIxKrSwMxDc1/SKnaBP1Q6Ag5ae23Z7DPYJUXmhY6s2YaBfvV+qro\n" |
---|
| 216 | "KRipli8Lk7hV+XmT7Jde6qgNdArb9P90c1nQQdXDPqcdKB5EaxR3O8qXtDoj+4AW\n" |
---|
| 217 | "dr0gekNsZIHx0rkHhxdGGludMuaI+HdIVEUjtSSw1X1ep3onddLs+gMs+9v1L7N4\n" |
---|
| 218 | "YWAnkATleuavh05zA85TKZzMBBx7wwjYKlaY86jQw4JxrjX46dv7tpS1yAPYn3rk\n" |
---|
| 219 | "Nd4jbVJfVHWbZeNy/NaO8g+nER+eSv9zAgEC\n" |
---|
| 220 | "-----END DH PARAMETERS-----\n"; |
---|
| 221 | |
---|
| 222 | /* Read the common name or the alternative name of the certificate. |
---|
| 223 | * We only support a single name per certificate. |
---|
| 224 | * |
---|
| 225 | * Returns negative on error. |
---|
| 226 | */ |
---|
[a5dc815] | 227 | static int read_crt_cn(server_rec * s, apr_pool_t * p, |
---|
[e5bbda4] | 228 | gnutls_x509_crt_t cert, char **cert_cn) |
---|
[fd73a08] | 229 | { |
---|
| 230 | int rv = 0, i; |
---|
| 231 | size_t data_len; |
---|
| 232 | |
---|
| 233 | |
---|
[a208cd3] | 234 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[fd73a08] | 235 | *cert_cn = NULL; |
---|
| 236 | |
---|
[e5bbda4] | 237 | data_len = 0; |
---|
[fd73a08] | 238 | rv = gnutls_x509_crt_get_dn_by_oid(cert, |
---|
| 239 | GNUTLS_OID_X520_COMMON_NAME, |
---|
| 240 | 0, 0, NULL, &data_len); |
---|
| 241 | |
---|
[41f7031] | 242 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) { |
---|
[fd73a08] | 243 | *cert_cn = apr_palloc(p, data_len); |
---|
| 244 | rv = gnutls_x509_crt_get_dn_by_oid(cert, |
---|
[a5dc815] | 245 | GNUTLS_OID_X520_COMMON_NAME, 0, |
---|
| 246 | 0, *cert_cn, &data_len); |
---|
[fd73a08] | 247 | } else { /* No CN return subject alternative name */ |
---|
[e6f648a] | 248 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, |
---|
[e5bbda4] | 249 | "No common name found in certificate for '%s:%d'. Looking for subject alternative name...", |
---|
[717206c] | 250 | s->server_hostname, s->port); |
---|
[a5dc815] | 251 | rv = 0; |
---|
[fd73a08] | 252 | /* read subject alternative name */ |
---|
| 253 | for (i = 0; !(rv < 0); i++) { |
---|
[a5dc815] | 254 | data_len = 0; |
---|
[fd73a08] | 255 | rv = gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
[a5dc815] | 256 | NULL, &data_len, |
---|
| 257 | NULL); |
---|
| 258 | |
---|
| 259 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) { |
---|
| 260 | /* FIXME: not very efficient. What if we have several alt names |
---|
| 261 | * before DNSName? |
---|
| 262 | */ |
---|
| 263 | *cert_cn = apr_palloc(p, data_len + 1); |
---|
| 264 | |
---|
| 265 | rv = gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
| 266 | *cert_cn, |
---|
| 267 | &data_len, NULL); |
---|
| 268 | (*cert_cn)[data_len] = 0; |
---|
| 269 | |
---|
| 270 | if (rv == GNUTLS_SAN_DNSNAME) |
---|
| 271 | break; |
---|
| 272 | } |
---|
[fd73a08] | 273 | } |
---|
| 274 | } |
---|
[a5dc815] | 275 | |
---|
[fd73a08] | 276 | return rv; |
---|
[e5bbda4] | 277 | } |
---|
| 278 | |
---|
| 279 | static int read_pgpcrt_cn(server_rec * s, apr_pool_t * p, |
---|
| 280 | gnutls_openpgp_crt_t cert, char **cert_cn) |
---|
| 281 | { |
---|
| 282 | int rv = 0; |
---|
| 283 | size_t data_len; |
---|
| 284 | |
---|
| 285 | |
---|
[a208cd3] | 286 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[e5bbda4] | 287 | *cert_cn = NULL; |
---|
| 288 | |
---|
| 289 | data_len = 0; |
---|
| 290 | rv = gnutls_openpgp_crt_get_name(cert, 0, NULL, &data_len); |
---|
| 291 | |
---|
| 292 | if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && data_len > 1) { |
---|
| 293 | *cert_cn = apr_palloc(p, data_len); |
---|
| 294 | rv = gnutls_openpgp_crt_get_name(cert, 0, *cert_cn, &data_len); |
---|
| 295 | } else { /* No CN return subject alternative name */ |
---|
| 296 | ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, |
---|
| 297 | "No name found in PGP certificate for '%s:%d'.", |
---|
| 298 | s->server_hostname, s->port); |
---|
| 299 | } |
---|
[fd73a08] | 300 | |
---|
[e5bbda4] | 301 | return rv; |
---|
[fd73a08] | 302 | } |
---|
[7bebb42] | 303 | |
---|
[e5bbda4] | 304 | |
---|
[7bebb42] | 305 | int |
---|
| 306 | mgs_hook_post_config(apr_pool_t * p, apr_pool_t * plog, |
---|
| 307 | apr_pool_t * ptemp, server_rec * base_server) |
---|
[c301152] | 308 | { |
---|
| 309 | int rv; |
---|
| 310 | server_rec *s; |
---|
[e239d1a] | 311 | gnutls_dh_params_t dh_params = NULL; |
---|
| 312 | gnutls_rsa_params_t rsa_params = NULL; |
---|
[c301152] | 313 | mgs_srvconf_rec *sc; |
---|
| 314 | mgs_srvconf_rec *sc_base; |
---|
| 315 | void *data = NULL; |
---|
| 316 | int first_run = 0; |
---|
| 317 | const char *userdata_key = "mgs_init"; |
---|
[7bebb42] | 318 | |
---|
[a208cd3] | 319 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 320 | apr_pool_userdata_get(&data, userdata_key, base_server->process->pool); |
---|
| 321 | if (data == NULL) { |
---|
[7bebb42] | 322 | first_run = 1; |
---|
| 323 | apr_pool_userdata_set((const void *) 1, userdata_key, |
---|
| 324 | apr_pool_cleanup_null, |
---|
| 325 | base_server->process->pool); |
---|
[c301152] | 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | { |
---|
[7bebb42] | 330 | s = base_server; |
---|
| 331 | sc_base = |
---|
| 332 | (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
| 333 | &gnutls_module); |
---|
| 334 | |
---|
| 335 | gnutls_dh_params_init(&dh_params); |
---|
| 336 | |
---|
[a3c97d1] | 337 | if (sc_base->dh_params == NULL) { |
---|
| 338 | gnutls_datum pdata = { (void *) static_dh_params, sizeof(static_dh_params) }; |
---|
| 339 | /* loading defaults */ |
---|
| 340 | rv = gnutls_dh_params_import_pkcs3(dh_params, &pdata, |
---|
[7bebb42] | 341 | GNUTLS_X509_FMT_PEM); |
---|
| 342 | |
---|
[a3c97d1] | 343 | if (rv < 0) { |
---|
| 344 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
| 345 | "GnuTLS: Unable to load DH Params: (%d) %s", |
---|
| 346 | rv, gnutls_strerror(rv)); |
---|
| 347 | exit(rv); |
---|
| 348 | } |
---|
| 349 | } else dh_params = sc_base->dh_params; |
---|
| 350 | |
---|
| 351 | if (sc_base->rsa_params != NULL) |
---|
| 352 | rsa_params = sc_base->rsa_params; |
---|
| 353 | |
---|
| 354 | /* else not an error but RSA-EXPORT ciphersuites are not available |
---|
[fd73a08] | 355 | */ |
---|
[7bebb42] | 356 | |
---|
| 357 | rv = mgs_cache_post_config(p, s, sc_base); |
---|
| 358 | if (rv != 0) { |
---|
| 359 | ap_log_error(APLOG_MARK, APLOG_STARTUP, rv, s, |
---|
| 360 | "GnuTLS: Post Config for GnuTLSCache Failed." |
---|
| 361 | " Shutting Down."); |
---|
| 362 | exit(-1); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | for (s = base_server; s; s = s->next) { |
---|
[a3c97d1] | 366 | void *load = NULL; |
---|
[7bebb42] | 367 | sc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
| 368 | &gnutls_module); |
---|
| 369 | sc->cache_type = sc_base->cache_type; |
---|
| 370 | sc->cache_config = sc_base->cache_config; |
---|
| 371 | |
---|
[e239d1a] | 372 | /* Check if the priorities have been set */ |
---|
[2226711] | 373 | if (sc->priorities == NULL && sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
[e239d1a] | 374 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
| 375 | "GnuTLS: Host '%s:%d' is missing the GnuTLSPriorities directive!", |
---|
| 376 | s->server_hostname, s->port); |
---|
| 377 | exit(-1); |
---|
| 378 | } |
---|
| 379 | |
---|
[a3c97d1] | 380 | /* Check if DH or RSA params have been set per host */ |
---|
| 381 | if (sc->rsa_params != NULL) |
---|
| 382 | load = sc->rsa_params; |
---|
| 383 | else if (rsa_params) load = rsa_params; |
---|
| 384 | |
---|
| 385 | if (load != NULL) |
---|
| 386 | gnutls_certificate_set_rsa_export_params(sc->certs, load); |
---|
[e239d1a] | 387 | |
---|
[7bebb42] | 388 | |
---|
[a3c97d1] | 389 | load = NULL; |
---|
| 390 | if (sc->dh_params != NULL) |
---|
| 391 | load = sc->dh_params; |
---|
| 392 | else if (dh_params) load = dh_params; |
---|
| 393 | |
---|
| 394 | if (load != NULL) { /* not needed but anyway */ |
---|
| 395 | gnutls_certificate_set_dh_params(sc->certs, load); |
---|
| 396 | gnutls_anon_set_server_dh_params(sc->anon_creds, load); |
---|
| 397 | } |
---|
[fd73a08] | 398 | |
---|
| 399 | gnutls_certificate_server_set_retrieve_function(sc->certs, |
---|
| 400 | cert_retrieve_fn); |
---|
| 401 | |
---|
[787dab7] | 402 | #ifdef ENABLE_SRP |
---|
[fd73a08] | 403 | if (sc->srp_tpasswd_conf_file != NULL |
---|
| 404 | && sc->srp_tpasswd_file != NULL) { |
---|
[41f7031] | 405 | rv = gnutls_srp_set_server_credentials_file(sc->srp_creds, |
---|
[a5dc815] | 406 | sc-> |
---|
| 407 | srp_tpasswd_file, |
---|
| 408 | sc-> |
---|
| 409 | srp_tpasswd_conf_file); |
---|
| 410 | |
---|
| 411 | if (rv < 0 && sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
[1a53be1] | 412 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
[a5dc815] | 413 | "[GnuTLS] - Host '%s:%d' is missing a " |
---|
| 414 | "SRP password or conf File!", |
---|
| 415 | s->server_hostname, s->port); |
---|
| 416 | exit(-1); |
---|
| 417 | } |
---|
[fd73a08] | 418 | } |
---|
[787dab7] | 419 | #endif |
---|
[7bebb42] | 420 | |
---|
[52149ee] | 421 | if (sc->certs_x509[0] == NULL && |
---|
| 422 | sc->cert_pgp == NULL && |
---|
| 423 | sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
[1a53be1] | 424 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
[7bebb42] | 425 | "[GnuTLS] - Host '%s:%d' is missing a " |
---|
| 426 | "Certificate File!", s->server_hostname, |
---|
| 427 | s->port); |
---|
| 428 | exit(-1); |
---|
| 429 | } |
---|
| 430 | |
---|
[52149ee] | 431 | if (sc->enabled == GNUTLS_ENABLED_TRUE && |
---|
| 432 | ((sc->certs_x509[0] != NULL && sc->privkey_x509 == NULL) || |
---|
| 433 | (sc->cert_pgp != NULL && sc->privkey_pgp == NULL))) { |
---|
[1a53be1] | 434 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
[7bebb42] | 435 | "[GnuTLS] - Host '%s:%d' is missing a " |
---|
| 436 | "Private Key File!", |
---|
| 437 | s->server_hostname, s->port); |
---|
| 438 | exit(-1); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | if (sc->enabled == GNUTLS_ENABLED_TRUE) { |
---|
[5e81262] | 442 | rv = read_crt_cn(s, p, sc->certs_x509[0], &sc->cert_cn); |
---|
[2b3a248b] | 443 | if (rv < 0 && sc->cert_pgp != NULL) /* try openpgp certificate */ |
---|
[e5bbda4] | 444 | rv = read_pgpcrt_cn(s, p, sc->cert_pgp, &sc->cert_cn); |
---|
| 445 | |
---|
[fd73a08] | 446 | if (rv < 0) { |
---|
[1a53be1] | 447 | ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, |
---|
[a5dc815] | 448 | "[GnuTLS] - Cannot find a certificate for host '%s:%d'!", |
---|
| 449 | s->server_hostname, s->port); |
---|
[fd73a08] | 450 | sc->cert_cn = NULL; |
---|
| 451 | continue; |
---|
| 452 | } |
---|
[7bebb42] | 453 | } |
---|
| 454 | } |
---|
[c301152] | 455 | } |
---|
| 456 | |
---|
| 457 | ap_add_version_component(p, "mod_gnutls/" MOD_GNUTLS_VERSION); |
---|
| 458 | |
---|
| 459 | return OK; |
---|
| 460 | } |
---|
| 461 | |
---|
[7bebb42] | 462 | void mgs_hook_child_init(apr_pool_t * p, server_rec * s) |
---|
[c301152] | 463 | { |
---|
| 464 | apr_status_t rv = APR_SUCCESS; |
---|
| 465 | mgs_srvconf_rec *sc = ap_get_module_config(s->module_config, |
---|
[7bebb42] | 466 | &gnutls_module); |
---|
[c301152] | 467 | |
---|
[a208cd3] | 468 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 469 | if (sc->cache_type != mgs_cache_none) { |
---|
[7bebb42] | 470 | rv = mgs_cache_child_init(p, s, sc); |
---|
| 471 | if (rv != APR_SUCCESS) { |
---|
| 472 | ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, |
---|
| 473 | "[GnuTLS] - Failed to run Cache Init"); |
---|
| 474 | } |
---|
| 475 | } else { |
---|
| 476 | ap_log_error(APLOG_MARK, APLOG_CRIT, 0, s, |
---|
| 477 | "[GnuTLS] - No Cache Configured. Hint: GnuTLSCache"); |
---|
[c301152] | 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | const char *mgs_hook_http_scheme(const request_rec * r) |
---|
| 482 | { |
---|
| 483 | mgs_srvconf_rec *sc = |
---|
[7bebb42] | 484 | (mgs_srvconf_rec *) ap_get_module_config(r->server->module_config, |
---|
| 485 | &gnutls_module); |
---|
[c301152] | 486 | |
---|
[a208cd3] | 487 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 488 | if (sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
[7bebb42] | 489 | return NULL; |
---|
[c301152] | 490 | } |
---|
| 491 | |
---|
| 492 | return "https"; |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | apr_port_t mgs_hook_default_port(const request_rec * r) |
---|
| 496 | { |
---|
| 497 | mgs_srvconf_rec *sc = |
---|
[7bebb42] | 498 | (mgs_srvconf_rec *) ap_get_module_config(r->server->module_config, |
---|
| 499 | &gnutls_module); |
---|
[c301152] | 500 | |
---|
[a208cd3] | 501 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 502 | if (sc->enabled == GNUTLS_ENABLED_FALSE) { |
---|
[7bebb42] | 503 | return 0; |
---|
[c301152] | 504 | } |
---|
| 505 | |
---|
| 506 | return 443; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | #define MAX_HOST_LEN 255 |
---|
| 510 | |
---|
| 511 | #if USING_2_1_RECENT |
---|
[7bebb42] | 512 | typedef struct { |
---|
[c301152] | 513 | mgs_handle_t *ctxt; |
---|
[836417f] | 514 | mgs_srvconf_rec *sc; |
---|
[7bebb42] | 515 | const char *sni_name; |
---|
[c301152] | 516 | } vhost_cb_rec; |
---|
| 517 | |
---|
[7bebb42] | 518 | static int vhost_cb(void *baton, conn_rec * conn, server_rec * s) |
---|
[c301152] | 519 | { |
---|
| 520 | mgs_srvconf_rec *tsc; |
---|
[7bebb42] | 521 | vhost_cb_rec *x = baton; |
---|
[c301152] | 522 | |
---|
[a208cd3] | 523 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 524 | tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
[7bebb42] | 525 | &gnutls_module); |
---|
| 526 | |
---|
[c301152] | 527 | if (tsc->enabled != GNUTLS_ENABLED_TRUE || tsc->cert_cn == NULL) { |
---|
[7bebb42] | 528 | return 0; |
---|
[c301152] | 529 | } |
---|
[7bebb42] | 530 | |
---|
[c301152] | 531 | /* The CN can contain a * -- this will match those too. */ |
---|
| 532 | if (ap_strcasecmp_match(x->sni_name, tsc->cert_cn) == 0) { |
---|
[7bebb42] | 533 | /* found a match */ |
---|
[c301152] | 534 | #if MOD_GNUTLS_DEBUG |
---|
[7bebb42] | 535 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, |
---|
| 536 | x->ctxt->c->base_server, |
---|
| 537 | "GnuTLS: Virtual Host CB: " |
---|
| 538 | "'%s' == '%s'", tsc->cert_cn, x->sni_name); |
---|
[c301152] | 539 | #endif |
---|
[7bebb42] | 540 | /* Because we actually change the server used here, we need to reset |
---|
| 541 | * things like ClientVerify. |
---|
| 542 | */ |
---|
| 543 | x->sc = tsc; |
---|
| 544 | /* Shit. Crap. Dammit. We *really* should rehandshake here, as our |
---|
| 545 | * certificate structure *should* change when the server changes. |
---|
| 546 | * acccckkkkkk. |
---|
| 547 | */ |
---|
| 548 | return 1; |
---|
[717206c] | 549 | } else { |
---|
| 550 | #if MOD_GNUTLS_DEBUG |
---|
| 551 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, |
---|
| 552 | x->ctxt->c->base_server, |
---|
| 553 | "GnuTLS: Virtual Host CB: " |
---|
| 554 | "'%s' != '%s'", tsc->cert_cn, x->sni_name); |
---|
| 555 | #endif |
---|
[a5dc815] | 556 | |
---|
[c301152] | 557 | } |
---|
| 558 | return 0; |
---|
| 559 | } |
---|
| 560 | #endif |
---|
| 561 | |
---|
[7bebb42] | 562 | mgs_srvconf_rec *mgs_find_sni_server(gnutls_session_t session) |
---|
[c301152] | 563 | { |
---|
| 564 | int rv; |
---|
[7bebb42] | 565 | unsigned int sni_type; |
---|
| 566 | size_t data_len = MAX_HOST_LEN; |
---|
[c301152] | 567 | char sni_name[MAX_HOST_LEN]; |
---|
| 568 | mgs_handle_t *ctxt; |
---|
| 569 | #if USING_2_1_RECENT |
---|
| 570 | vhost_cb_rec cbx; |
---|
| 571 | #else |
---|
[7bebb42] | 572 | server_rec *s; |
---|
| 573 | mgs_srvconf_rec *tsc; |
---|
[c301152] | 574 | #endif |
---|
[7bebb42] | 575 | |
---|
[a208cd3] | 576 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 577 | ctxt = gnutls_transport_get_ptr(session); |
---|
[7bebb42] | 578 | |
---|
| 579 | rv = gnutls_server_name_get(ctxt->session, sni_name, |
---|
| 580 | &data_len, &sni_type, 0); |
---|
| 581 | |
---|
[c301152] | 582 | if (rv != 0) { |
---|
[7bebb42] | 583 | return NULL; |
---|
[c301152] | 584 | } |
---|
[7bebb42] | 585 | |
---|
[c301152] | 586 | if (sni_type != GNUTLS_NAME_DNS) { |
---|
[7bebb42] | 587 | ap_log_error(APLOG_MARK, APLOG_CRIT, 0, |
---|
| 588 | ctxt->c->base_server, |
---|
| 589 | "GnuTLS: Unknown type '%d' for SNI: " |
---|
| 590 | "'%s'", sni_type, sni_name); |
---|
| 591 | return NULL; |
---|
[c301152] | 592 | } |
---|
[7bebb42] | 593 | |
---|
[c301152] | 594 | /** |
---|
| 595 | * Code in the Core already sets up the c->base_server as the base |
---|
| 596 | * for this IP/Port combo. Trust that the core did the 'right' thing. |
---|
| 597 | */ |
---|
| 598 | #if USING_2_1_RECENT |
---|
| 599 | cbx.ctxt = ctxt; |
---|
[836417f] | 600 | cbx.sc = NULL; |
---|
[c301152] | 601 | cbx.sni_name = sni_name; |
---|
[7bebb42] | 602 | |
---|
[c301152] | 603 | rv = ap_vhost_iterate_given_conn(ctxt->c, vhost_cb, &cbx); |
---|
| 604 | if (rv == 1) { |
---|
[7bebb42] | 605 | return cbx.sc; |
---|
[c301152] | 606 | } |
---|
| 607 | #else |
---|
| 608 | for (s = ap_server_conf; s; s = s->next) { |
---|
[7bebb42] | 609 | |
---|
| 610 | tsc = (mgs_srvconf_rec *) ap_get_module_config(s->module_config, |
---|
| 611 | &gnutls_module); |
---|
| 612 | if (tsc->enabled != GNUTLS_ENABLED_TRUE) { |
---|
| 613 | continue; |
---|
| 614 | } |
---|
[c301152] | 615 | #if MOD_GNUTLS_DEBUG |
---|
[7bebb42] | 616 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, |
---|
| 617 | ctxt->c->base_server, |
---|
| 618 | "GnuTLS: sni-x509 cn: %s/%d pk: %s s: 0x%08X s->n: 0x%08X sc: 0x%08X", |
---|
| 619 | tsc->cert_cn, rv, |
---|
| 620 | gnutls_pk_algorithm_get_name |
---|
| 621 | (gnutls_x509_privkey_get_pk_algorithm |
---|
| 622 | (ctxt->sc->privkey_x509)), (unsigned int) s, |
---|
| 623 | (unsigned int) s->next, (unsigned int) tsc); |
---|
| 624 | #endif |
---|
| 625 | /* The CN can contain a * -- this will match those too. */ |
---|
| 626 | if (ap_strcasecmp_match(sni_name, tsc->cert_cn) == 0) { |
---|
[c301152] | 627 | #if MOD_GNUTLS_DEBUG |
---|
[7bebb42] | 628 | ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, |
---|
| 629 | ctxt->c->base_server, |
---|
| 630 | "GnuTLS: Virtual Host: " |
---|
| 631 | "'%s' == '%s'", tsc->cert_cn, sni_name); |
---|
[c301152] | 632 | #endif |
---|
[7bebb42] | 633 | return tsc; |
---|
| 634 | } |
---|
[c301152] | 635 | } |
---|
| 636 | #endif |
---|
[836417f] | 637 | return NULL; |
---|
| 638 | } |
---|
| 639 | |
---|
| 640 | |
---|
[7bebb42] | 641 | static const int protocol_priority[] = { |
---|
[fd73a08] | 642 | GNUTLS_TLS1_1, GNUTLS_TLS1_0, GNUTLS_SSL3, 0 |
---|
| 643 | }; |
---|
| 644 | |
---|
[836417f] | 645 | |
---|
[7bebb42] | 646 | static mgs_handle_t *create_gnutls_handle(apr_pool_t * pool, conn_rec * c) |
---|
[c301152] | 647 | { |
---|
| 648 | mgs_handle_t *ctxt; |
---|
| 649 | mgs_srvconf_rec *sc = |
---|
[7bebb42] | 650 | (mgs_srvconf_rec *) ap_get_module_config(c->base_server-> |
---|
| 651 | module_config, |
---|
| 652 | &gnutls_module); |
---|
[c301152] | 653 | |
---|
[a208cd3] | 654 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 655 | ctxt = apr_pcalloc(pool, sizeof(*ctxt)); |
---|
| 656 | ctxt->c = c; |
---|
| 657 | ctxt->sc = sc; |
---|
| 658 | ctxt->status = 0; |
---|
| 659 | |
---|
| 660 | ctxt->input_rc = APR_SUCCESS; |
---|
| 661 | ctxt->input_bb = apr_brigade_create(c->pool, c->bucket_alloc); |
---|
| 662 | ctxt->input_cbuf.length = 0; |
---|
| 663 | |
---|
| 664 | ctxt->output_rc = APR_SUCCESS; |
---|
| 665 | ctxt->output_bb = apr_brigade_create(c->pool, c->bucket_alloc); |
---|
| 666 | ctxt->output_blen = 0; |
---|
| 667 | ctxt->output_length = 0; |
---|
| 668 | |
---|
| 669 | gnutls_init(&ctxt->session, GNUTLS_SERVER); |
---|
[ed07ddf] | 670 | if (session_ticket_key.data != NULL) |
---|
| 671 | gnutls_session_ticket_enable_server(ctxt->session, &session_ticket_key); |
---|
[fd73a08] | 672 | |
---|
[7bebb42] | 673 | /* because we don't set any default priorities here (we set later at |
---|
| 674 | * the user hello callback) we need to at least set this in order for |
---|
| 675 | * gnutls to be able to read packets. |
---|
| 676 | */ |
---|
[fd73a08] | 677 | gnutls_protocol_set_priority(ctxt->session, protocol_priority); |
---|
[7bebb42] | 678 | |
---|
[fd73a08] | 679 | gnutls_handshake_set_post_client_hello_function(ctxt->session, |
---|
| 680 | mgs_select_virtual_server_cb); |
---|
[c301152] | 681 | |
---|
[ae4a2b0] | 682 | mgs_cache_session_init(ctxt); |
---|
| 683 | |
---|
[c301152] | 684 | return ctxt; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | int mgs_hook_pre_connection(conn_rec * c, void *csd) |
---|
| 688 | { |
---|
| 689 | mgs_handle_t *ctxt; |
---|
| 690 | mgs_srvconf_rec *sc = |
---|
[7bebb42] | 691 | (mgs_srvconf_rec *) ap_get_module_config(c->base_server-> |
---|
| 692 | module_config, |
---|
| 693 | &gnutls_module); |
---|
[c301152] | 694 | |
---|
[a208cd3] | 695 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 696 | if (!(sc && (sc->enabled == GNUTLS_ENABLED_TRUE))) { |
---|
[7bebb42] | 697 | return DECLINED; |
---|
[c301152] | 698 | } |
---|
| 699 | |
---|
[56f3628] | 700 | if(c->remote_addr->hostname) |
---|
| 701 | /* Connection initiated by Apache (mod_proxy) => ignore */ |
---|
| 702 | return OK; |
---|
| 703 | |
---|
[c301152] | 704 | ctxt = create_gnutls_handle(c->pool, c); |
---|
| 705 | |
---|
| 706 | ap_set_module_config(c->conn_config, &gnutls_module, ctxt); |
---|
| 707 | |
---|
[7bebb42] | 708 | gnutls_transport_set_pull_function(ctxt->session, mgs_transport_read); |
---|
| 709 | gnutls_transport_set_push_function(ctxt->session, mgs_transport_write); |
---|
[c301152] | 710 | gnutls_transport_set_ptr(ctxt->session, ctxt); |
---|
[7bebb42] | 711 | |
---|
| 712 | ctxt->input_filter = |
---|
| 713 | ap_add_input_filter(GNUTLS_INPUT_FILTER_NAME, ctxt, NULL, c); |
---|
| 714 | ctxt->output_filter = |
---|
| 715 | ap_add_output_filter(GNUTLS_OUTPUT_FILTER_NAME, ctxt, NULL, c); |
---|
[c301152] | 716 | |
---|
| 717 | return OK; |
---|
| 718 | } |
---|
| 719 | |
---|
[7bebb42] | 720 | int mgs_hook_fixups(request_rec * r) |
---|
[c301152] | 721 | { |
---|
| 722 | unsigned char sbuf[GNUTLS_MAX_SESSION_ID]; |
---|
[7ba803b] | 723 | char buf[AP_IOBUFSIZE]; |
---|
[7bebb42] | 724 | const char *tmp; |
---|
| 725 | size_t len; |
---|
[c301152] | 726 | mgs_handle_t *ctxt; |
---|
[2ad3683] | 727 | int rv = OK; |
---|
[7bebb42] | 728 | |
---|
[a208cd3] | 729 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[c301152] | 730 | apr_table_t *env = r->subprocess_env; |
---|
| 731 | |
---|
[7bebb42] | 732 | ctxt = |
---|
| 733 | ap_get_module_config(r->connection->conn_config, &gnutls_module); |
---|
[c301152] | 734 | |
---|
[7bebb42] | 735 | if (!ctxt) { |
---|
| 736 | return DECLINED; |
---|
[c301152] | 737 | } |
---|
| 738 | |
---|
| 739 | apr_table_setn(env, "HTTPS", "on"); |
---|
| 740 | |
---|
[fd73a08] | 741 | apr_table_setn(env, "SSL_VERSION_LIBRARY", |
---|
| 742 | "GnuTLS/" LIBGNUTLS_VERSION); |
---|
| 743 | apr_table_setn(env, "SSL_VERSION_INTERFACE", |
---|
| 744 | "mod_gnutls/" MOD_GNUTLS_VERSION); |
---|
[c301152] | 745 | |
---|
| 746 | apr_table_setn(env, "SSL_PROTOCOL", |
---|
[7bebb42] | 747 | gnutls_protocol_get_name(gnutls_protocol_get_version |
---|
| 748 | (ctxt->session))); |
---|
[c301152] | 749 | |
---|
[fd73a08] | 750 | /* should have been called SSL_CIPHERSUITE instead */ |
---|
[c301152] | 751 | apr_table_setn(env, "SSL_CIPHER", |
---|
[fd73a08] | 752 | gnutls_cipher_suite_get_name(gnutls_kx_get |
---|
| 753 | (ctxt->session), |
---|
| 754 | gnutls_cipher_get(ctxt-> |
---|
| 755 | session), |
---|
| 756 | gnutls_mac_get(ctxt-> |
---|
| 757 | session))); |
---|
[7bebb42] | 758 | |
---|
| 759 | apr_table_setn(env, "SSL_COMPRESS_METHOD", |
---|
| 760 | gnutls_compression_get_name(gnutls_compression_get |
---|
[fd73a08] | 761 | (ctxt->session))); |
---|
[7bebb42] | 762 | |
---|
[787dab7] | 763 | #ifdef ENABLE_SRP |
---|
[377a900] | 764 | tmp = gnutls_srp_server_get_username(ctxt->session); |
---|
| 765 | apr_table_setn(env, "SSL_SRP_USER", (tmp!=NULL)?tmp:""); |
---|
[787dab7] | 766 | #endif |
---|
[c301152] | 767 | |
---|
[7bebb42] | 768 | if (apr_table_get(env, "SSL_CLIENT_VERIFY") == NULL) |
---|
| 769 | apr_table_setn(env, "SSL_CLIENT_VERIFY", "NONE"); |
---|
[c301152] | 770 | |
---|
[7bebb42] | 771 | unsigned int key_size = |
---|
| 772 | 8 * gnutls_cipher_get_key_size(gnutls_cipher_get(ctxt->session)); |
---|
| 773 | tmp = apr_psprintf(r->pool, "%u", key_size); |
---|
[c301152] | 774 | |
---|
| 775 | apr_table_setn(env, "SSL_CIPHER_USEKEYSIZE", tmp); |
---|
| 776 | |
---|
| 777 | apr_table_setn(env, "SSL_CIPHER_ALGKEYSIZE", tmp); |
---|
| 778 | |
---|
[7bebb42] | 779 | apr_table_setn(env, "SSL_CIPHER_EXPORT", |
---|
| 780 | (key_size <= 40) ? "true" : "false"); |
---|
| 781 | |
---|
[c301152] | 782 | len = sizeof(sbuf); |
---|
| 783 | gnutls_session_get_id(ctxt->session, sbuf, &len); |
---|
| 784 | tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf)); |
---|
[38435cc] | 785 | apr_table_setn(env, "SSL_SESSION_ID", apr_pstrdup(r->pool, tmp)); |
---|
[7ba803b] | 786 | |
---|
[e5bbda4] | 787 | if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) |
---|
| 788 | mgs_add_common_cert_vars(r, ctxt->sc->certs_x509[0], 0, |
---|
| 789 | ctxt->sc->export_certificates_enabled); |
---|
| 790 | else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP) |
---|
| 791 | mgs_add_common_pgpcert_vars(r, ctxt->sc->cert_pgp, 0, |
---|
[fd73a08] | 792 | ctxt->sc->export_certificates_enabled); |
---|
[7bebb42] | 793 | |
---|
[2ad3683] | 794 | return rv; |
---|
[c301152] | 795 | } |
---|
| 796 | |
---|
[7bebb42] | 797 | int mgs_hook_authz(request_rec * r) |
---|
[c301152] | 798 | { |
---|
| 799 | int rv; |
---|
| 800 | mgs_handle_t *ctxt; |
---|
| 801 | mgs_dirconf_rec *dc = ap_get_module_config(r->per_dir_config, |
---|
[7bebb42] | 802 | &gnutls_module); |
---|
| 803 | |
---|
[a208cd3] | 804 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[7bebb42] | 805 | ctxt = |
---|
| 806 | ap_get_module_config(r->connection->conn_config, &gnutls_module); |
---|
| 807 | |
---|
[c301152] | 808 | if (!ctxt) { |
---|
[7bebb42] | 809 | return DECLINED; |
---|
[c301152] | 810 | } |
---|
[836c2f9] | 811 | |
---|
[c301152] | 812 | if (dc->client_verify_mode == GNUTLS_CERT_IGNORE) { |
---|
[7bebb42] | 813 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
| 814 | "GnuTLS: Directory set to Ignore Client Certificate!"); |
---|
| 815 | } else { |
---|
| 816 | if (ctxt->sc->client_verify_mode < dc->client_verify_mode) { |
---|
| 817 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
| 818 | "GnuTLS: Attempting to rehandshake with peer. %d %d", |
---|
| 819 | ctxt->sc->client_verify_mode, |
---|
| 820 | dc->client_verify_mode); |
---|
| 821 | |
---|
[c38a4e9] | 822 | /* If we already have a client certificate, there's no point in |
---|
| 823 | * re-handshaking... */ |
---|
| 824 | rv = mgs_cert_verify(r, ctxt); |
---|
| 825 | if (rv != DECLINED && rv != HTTP_FORBIDDEN) |
---|
| 826 | return rv; |
---|
| 827 | |
---|
[7bebb42] | 828 | gnutls_certificate_server_set_request(ctxt->session, |
---|
| 829 | dc->client_verify_mode); |
---|
| 830 | |
---|
| 831 | if (mgs_rehandshake(ctxt) != 0) { |
---|
| 832 | return HTTP_FORBIDDEN; |
---|
| 833 | } |
---|
| 834 | } else if (ctxt->sc->client_verify_mode == GNUTLS_CERT_IGNORE) { |
---|
[c301152] | 835 | #if MOD_GNUTLS_DEBUG |
---|
[7bebb42] | 836 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 837 | "GnuTLS: Peer is set to IGNORE"); |
---|
[c301152] | 838 | #endif |
---|
[1c90c18] | 839 | return DECLINED; |
---|
| 840 | } |
---|
| 841 | rv = mgs_cert_verify(r, ctxt); |
---|
| 842 | if (rv != DECLINED && |
---|
| 843 | (rv != HTTP_FORBIDDEN || |
---|
| 844 | dc->client_verify_mode == GNUTLS_CERT_REQUIRE)) { |
---|
| 845 | return rv; |
---|
[7bebb42] | 846 | } |
---|
| 847 | } |
---|
[836c2f9] | 848 | |
---|
[7bebb42] | 849 | return DECLINED; |
---|
| 850 | } |
---|
| 851 | |
---|
| 852 | /* variables that are not sent by default: |
---|
| 853 | * |
---|
| 854 | * SSL_CLIENT_CERT string PEM-encoded client certificate |
---|
| 855 | * SSL_SERVER_CERT string PEM-encoded client certificate |
---|
| 856 | */ |
---|
[836c2f9] | 857 | |
---|
[7bebb42] | 858 | /* side is either 0 for SERVER or 1 for CLIENT |
---|
| 859 | */ |
---|
| 860 | #define MGS_SIDE ((side==0)?"SSL_SERVER":"SSL_CLIENT") |
---|
| 861 | static void |
---|
[e5bbda4] | 862 | mgs_add_common_cert_vars(request_rec * r, gnutls_x509_crt_t cert, int side, |
---|
[fd73a08] | 863 | int export_certificates_enabled) |
---|
[836c2f9] | 864 | { |
---|
[7bebb42] | 865 | unsigned char sbuf[64]; /* buffer to hold serials */ |
---|
| 866 | char buf[AP_IOBUFSIZE]; |
---|
| 867 | const char *tmp; |
---|
[a5dc815] | 868 | char *tmp2; |
---|
[7bebb42] | 869 | size_t len; |
---|
[a5dc815] | 870 | int ret, i; |
---|
[7bebb42] | 871 | |
---|
| 872 | apr_table_t *env = r->subprocess_env; |
---|
[836c2f9] | 873 | |
---|
[a208cd3] | 874 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[7bebb42] | 875 | if (export_certificates_enabled != 0) { |
---|
[fd73a08] | 876 | char cert_buf[10 * 1024]; |
---|
| 877 | len = sizeof(cert_buf); |
---|
| 878 | |
---|
| 879 | if (gnutls_x509_crt_export |
---|
| 880 | (cert, GNUTLS_X509_FMT_PEM, cert_buf, &len) >= 0) |
---|
| 881 | apr_table_setn(env, |
---|
| 882 | apr_pstrcat(r->pool, MGS_SIDE, "_CERT", NULL), |
---|
| 883 | apr_pstrmemdup(r->pool, cert_buf, len)); |
---|
[7bebb42] | 884 | |
---|
| 885 | } |
---|
[836c2f9] | 886 | |
---|
[7bebb42] | 887 | len = sizeof(buf); |
---|
| 888 | gnutls_x509_crt_get_dn(cert, buf, &len); |
---|
| 889 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_S_DN", NULL), |
---|
| 890 | apr_pstrmemdup(r->pool, buf, len)); |
---|
| 891 | |
---|
| 892 | len = sizeof(buf); |
---|
| 893 | gnutls_x509_crt_get_issuer_dn(cert, buf, &len); |
---|
| 894 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_I_DN", NULL), |
---|
| 895 | apr_pstrmemdup(r->pool, buf, len)); |
---|
| 896 | |
---|
| 897 | len = sizeof(sbuf); |
---|
| 898 | gnutls_x509_crt_get_serial(cert, sbuf, &len); |
---|
| 899 | tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf)); |
---|
| 900 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_M_SERIAL", NULL), |
---|
| 901 | apr_pstrdup(r->pool, tmp)); |
---|
| 902 | |
---|
[a5dc815] | 903 | ret = gnutls_x509_crt_get_version(cert); |
---|
| 904 | if (ret > 0) |
---|
| 905 | apr_table_setn(env, |
---|
| 906 | apr_pstrcat(r->pool, MGS_SIDE, "_M_VERSION", NULL), |
---|
| 907 | apr_psprintf(r->pool, "%u", ret)); |
---|
[ee65fcb] | 908 | |
---|
[36736d4] | 909 | apr_table_setn(env, |
---|
[25dad18] | 910 | apr_pstrcat(r->pool, MGS_SIDE, "_CERT_TYPE", NULL), "X.509"); |
---|
[36736d4] | 911 | |
---|
[7bebb42] | 912 | tmp = |
---|
| 913 | mgs_time2sz(gnutls_x509_crt_get_expiration_time |
---|
| 914 | (cert), buf, sizeof(buf)); |
---|
| 915 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_END", NULL), |
---|
| 916 | apr_pstrdup(r->pool, tmp)); |
---|
| 917 | |
---|
| 918 | tmp = |
---|
| 919 | mgs_time2sz(gnutls_x509_crt_get_activation_time |
---|
| 920 | (cert), buf, sizeof(buf)); |
---|
| 921 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_START", NULL), |
---|
| 922 | apr_pstrdup(r->pool, tmp)); |
---|
| 923 | |
---|
[a5dc815] | 924 | ret = gnutls_x509_crt_get_signature_algorithm(cert); |
---|
| 925 | if (ret >= 0) { |
---|
[fd73a08] | 926 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_A_SIG", NULL), |
---|
[a5dc815] | 927 | gnutls_sign_algorithm_get_name(ret)); |
---|
[7bebb42] | 928 | } |
---|
| 929 | |
---|
[a5dc815] | 930 | ret = gnutls_x509_crt_get_pk_algorithm(cert, NULL); |
---|
| 931 | if (ret >= 0) { |
---|
[fd73a08] | 932 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_A_KEY", NULL), |
---|
[a5dc815] | 933 | gnutls_pk_algorithm_get_name(ret)); |
---|
| 934 | } |
---|
| 935 | |
---|
| 936 | /* export all the alternative names (DNS, RFC822 and URI) */ |
---|
| 937 | for (i = 0; !(ret < 0); i++) { |
---|
| 938 | len = 0; |
---|
| 939 | ret = gnutls_x509_crt_get_subject_alt_name(cert, i, |
---|
| 940 | NULL, &len, NULL); |
---|
| 941 | |
---|
| 942 | if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER && len > 1) { |
---|
| 943 | tmp2 = apr_palloc(r->pool, len + 1); |
---|
| 944 | |
---|
| 945 | ret = |
---|
| 946 | gnutls_x509_crt_get_subject_alt_name(cert, i, tmp2, &len, |
---|
| 947 | NULL); |
---|
| 948 | tmp2[len] = 0; |
---|
| 949 | |
---|
| 950 | if (ret == GNUTLS_SAN_DNSNAME) { |
---|
| 951 | apr_table_setn(env, |
---|
[25dad18] | 952 | apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), |
---|
[a5dc815] | 953 | apr_psprintf(r->pool, "DNSNAME:%s", tmp2)); |
---|
| 954 | } else if (ret == GNUTLS_SAN_RFC822NAME) { |
---|
| 955 | apr_table_setn(env, |
---|
[25dad18] | 956 | apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), |
---|
[a5dc815] | 957 | apr_psprintf(r->pool, "RFC822NAME:%s", tmp2)); |
---|
| 958 | } else if (ret == GNUTLS_SAN_URI) { |
---|
| 959 | apr_table_setn(env, |
---|
[25dad18] | 960 | apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), |
---|
[a5dc815] | 961 | apr_psprintf(r->pool, "URI:%s", tmp2)); |
---|
| 962 | } else { |
---|
| 963 | apr_table_setn(env, |
---|
[25dad18] | 964 | apr_psprintf(r->pool, "%s_S_AN%u", MGS_SIDE, i), |
---|
[a5dc815] | 965 | "UNSUPPORTED"); |
---|
| 966 | } |
---|
| 967 | } |
---|
[7bebb42] | 968 | } |
---|
[e5bbda4] | 969 | } |
---|
[7bebb42] | 970 | |
---|
[e5bbda4] | 971 | static void |
---|
| 972 | mgs_add_common_pgpcert_vars(request_rec * r, gnutls_openpgp_crt_t cert, int side, |
---|
| 973 | int export_certificates_enabled) |
---|
| 974 | { |
---|
| 975 | unsigned char sbuf[64]; /* buffer to hold serials */ |
---|
| 976 | char buf[AP_IOBUFSIZE]; |
---|
| 977 | const char *tmp; |
---|
| 978 | size_t len; |
---|
| 979 | int ret; |
---|
[7bebb42] | 980 | |
---|
[a208cd3] | 981 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[e5bbda4] | 982 | apr_table_t *env = r->subprocess_env; |
---|
| 983 | |
---|
| 984 | if (export_certificates_enabled != 0) { |
---|
| 985 | char cert_buf[10 * 1024]; |
---|
| 986 | len = sizeof(cert_buf); |
---|
| 987 | |
---|
| 988 | if (gnutls_openpgp_crt_export |
---|
| 989 | (cert, GNUTLS_OPENPGP_FMT_BASE64, cert_buf, &len) >= 0) |
---|
| 990 | apr_table_setn(env, |
---|
| 991 | apr_pstrcat(r->pool, MGS_SIDE, "_CERT", NULL), |
---|
| 992 | apr_pstrmemdup(r->pool, cert_buf, len)); |
---|
| 993 | |
---|
| 994 | } |
---|
| 995 | |
---|
| 996 | len = sizeof(buf); |
---|
| 997 | gnutls_openpgp_crt_get_name(cert, 0, buf, &len); |
---|
| 998 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_NAME", NULL), |
---|
| 999 | apr_pstrmemdup(r->pool, buf, len)); |
---|
| 1000 | |
---|
| 1001 | len = sizeof(sbuf); |
---|
| 1002 | gnutls_openpgp_crt_get_fingerprint(cert, sbuf, &len); |
---|
| 1003 | tmp = mgs_session_id2sz(sbuf, len, buf, sizeof(buf)); |
---|
| 1004 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_FINGERPRINT", NULL), |
---|
| 1005 | apr_pstrdup(r->pool, tmp)); |
---|
| 1006 | |
---|
| 1007 | ret = gnutls_openpgp_crt_get_version(cert); |
---|
| 1008 | if (ret > 0) |
---|
| 1009 | apr_table_setn(env, |
---|
| 1010 | apr_pstrcat(r->pool, MGS_SIDE, "_M_VERSION", NULL), |
---|
| 1011 | apr_psprintf(r->pool, "%u", ret)); |
---|
| 1012 | |
---|
| 1013 | apr_table_setn(env, |
---|
| 1014 | apr_pstrcat(r->pool, MGS_SIDE, "_CERT_TYPE", NULL), "OPENPGP"); |
---|
| 1015 | |
---|
| 1016 | tmp = |
---|
| 1017 | mgs_time2sz(gnutls_openpgp_crt_get_expiration_time |
---|
| 1018 | (cert), buf, sizeof(buf)); |
---|
| 1019 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_END", NULL), |
---|
| 1020 | apr_pstrdup(r->pool, tmp)); |
---|
[7bebb42] | 1021 | |
---|
[e5bbda4] | 1022 | tmp = |
---|
| 1023 | mgs_time2sz(gnutls_openpgp_crt_get_creation_time |
---|
| 1024 | (cert), buf, sizeof(buf)); |
---|
| 1025 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_V_START", NULL), |
---|
| 1026 | apr_pstrdup(r->pool, tmp)); |
---|
[7bebb42] | 1027 | |
---|
[e5bbda4] | 1028 | ret = gnutls_openpgp_crt_get_pk_algorithm(cert, NULL); |
---|
| 1029 | if (ret >= 0) { |
---|
| 1030 | apr_table_setn(env, apr_pstrcat(r->pool, MGS_SIDE, "_A_KEY", NULL), |
---|
| 1031 | gnutls_pk_algorithm_get_name(ret)); |
---|
| 1032 | } |
---|
| 1033 | |
---|
| 1034 | } |
---|
| 1035 | |
---|
[2b3a248b] | 1036 | /* TODO: Allow client sending a X.509 certificate chain */ |
---|
[7bebb42] | 1037 | static int mgs_cert_verify(request_rec * r, mgs_handle_t * ctxt) |
---|
| 1038 | { |
---|
| 1039 | const gnutls_datum_t *cert_list; |
---|
| 1040 | unsigned int cert_list_size, status, expired; |
---|
[8e98518] | 1041 | int rv = GNUTLS_E_NO_CERTIFICATE_FOUND, ret; |
---|
[67101aa] | 1042 | unsigned int ch_size = 0; |
---|
[e5bbda4] | 1043 | union { |
---|
[67101aa] | 1044 | gnutls_x509_crt_t x509[MAX_CHAIN_SIZE]; |
---|
[e5bbda4] | 1045 | gnutls_openpgp_crt_t pgp; |
---|
| 1046 | } cert; |
---|
[7bebb42] | 1047 | apr_time_t activation_time, expiration_time, cur_time; |
---|
| 1048 | |
---|
[a208cd3] | 1049 | _gnutls_log(debug_log_fp, "%s: %d\n", __func__, __LINE__); |
---|
[7bebb42] | 1050 | cert_list = |
---|
| 1051 | gnutls_certificate_get_peers(ctxt->session, &cert_list_size); |
---|
[836c2f9] | 1052 | |
---|
| 1053 | if (cert_list == NULL || cert_list_size == 0) { |
---|
[7bebb42] | 1054 | /* It is perfectly OK for a client not to send a certificate if on REQUEST mode |
---|
| 1055 | */ |
---|
| 1056 | if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST) |
---|
| 1057 | return OK; |
---|
| 1058 | |
---|
| 1059 | /* no certificate provided by the client, but one was required. */ |
---|
| 1060 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1061 | "GnuTLS: Failed to Verify Peer: " |
---|
| 1062 | "Client did not submit a certificate"); |
---|
| 1063 | return HTTP_FORBIDDEN; |
---|
[c301152] | 1064 | } |
---|
[836c2f9] | 1065 | |
---|
[67101aa] | 1066 | if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) { |
---|
| 1067 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
| 1068 | "GnuTLS: A Chain of %d certificate(s) was provided for validation", cert_list_size); |
---|
| 1069 | |
---|
[8e98518] | 1070 | for (ch_size = 0; ch_size<cert_list_size; ch_size++) { |
---|
[67101aa] | 1071 | gnutls_x509_crt_init(&cert.x509[ch_size]); |
---|
| 1072 | rv = gnutls_x509_crt_import(cert.x509[ch_size], &cert_list[ch_size], GNUTLS_X509_FMT_DER); |
---|
| 1073 | // When failure to import, leave the loop |
---|
| 1074 | if ( rv != GNUTLS_E_SUCCESS ) { |
---|
| 1075 | if (ch_size < 1) { |
---|
| 1076 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1077 | "GnuTLS: Failed to Verify Peer: " |
---|
| 1078 | "Failed to import peer certificates."); |
---|
| 1079 | ret = HTTP_FORBIDDEN; |
---|
| 1080 | goto exit; |
---|
| 1081 | } |
---|
| 1082 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1083 | "GnuTLS: Failed to import some peer certificates. Using %d certificates", |
---|
| 1084 | ch_size); |
---|
| 1085 | rv = GNUTLS_E_SUCCESS; |
---|
| 1086 | break; |
---|
| 1087 | } |
---|
| 1088 | } |
---|
| 1089 | } else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP) { |
---|
| 1090 | if (cert_list_size > 1) { |
---|
| 1091 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
[7bebb42] | 1092 | "GnuTLS: Failed to Verify Peer: " |
---|
| 1093 | "Chained Client Certificates are not supported."); |
---|
[67101aa] | 1094 | return HTTP_FORBIDDEN; |
---|
| 1095 | } |
---|
[836c2f9] | 1096 | |
---|
[e5bbda4] | 1097 | gnutls_openpgp_crt_init(&cert.pgp); |
---|
| 1098 | rv = gnutls_openpgp_crt_import(cert.pgp, &cert_list[0], GNUTLS_OPENPGP_FMT_RAW); |
---|
[67101aa] | 1099 | |
---|
[e5bbda4] | 1100 | } else return HTTP_FORBIDDEN; |
---|
| 1101 | |
---|
[7bebb42] | 1102 | if (rv < 0) { |
---|
[e5bbda4] | 1103 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
[7bebb42] | 1104 | "GnuTLS: Failed to Verify Peer: " |
---|
| 1105 | "Failed to import peer certificates."); |
---|
[e5bbda4] | 1106 | ret = HTTP_FORBIDDEN; |
---|
| 1107 | goto exit; |
---|
[7bebb42] | 1108 | } |
---|
[836c2f9] | 1109 | |
---|
[e5bbda4] | 1110 | if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) { |
---|
| 1111 | apr_time_ansi_put(&expiration_time, |
---|
[67101aa] | 1112 | gnutls_x509_crt_get_expiration_time(cert.x509[0])); |
---|
[e5bbda4] | 1113 | apr_time_ansi_put(&activation_time, |
---|
[67101aa] | 1114 | gnutls_x509_crt_get_activation_time(cert.x509[0])); |
---|
[7bebb42] | 1115 | |
---|
[67101aa] | 1116 | ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, |
---|
| 1117 | "GnuTLS: Verifying list of %d certificate(s)", ch_size); |
---|
| 1118 | rv = gnutls_x509_crt_list_verify(cert.x509, ch_size, |
---|
| 1119 | ctxt->sc->ca_list, ctxt->sc->ca_list_size, |
---|
| 1120 | NULL, 0, 0, &status); |
---|
[e5bbda4] | 1121 | } else { |
---|
| 1122 | apr_time_ansi_put(&expiration_time, |
---|
| 1123 | gnutls_openpgp_crt_get_expiration_time(cert.pgp)); |
---|
| 1124 | apr_time_ansi_put(&activation_time, |
---|
| 1125 | gnutls_openpgp_crt_get_creation_time(cert.pgp)); |
---|
| 1126 | |
---|
| 1127 | rv = gnutls_openpgp_crt_verify_ring(cert.pgp, ctxt->sc->pgp_list, |
---|
| 1128 | 0, &status); |
---|
| 1129 | } |
---|
| 1130 | |
---|
[c301152] | 1131 | if (rv < 0) { |
---|
[7bebb42] | 1132 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1133 | "GnuTLS: Failed to Verify Peer certificate: (%d) %s", |
---|
| 1134 | rv, gnutls_strerror(rv)); |
---|
[2b3a248b] | 1135 | if (rv == GNUTLS_E_NO_CERTIFICATE_FOUND) |
---|
| 1136 | ap_log_rerror(APLOG_MARK, APLOG_EMERG, 0, r, |
---|
| 1137 | "GnuTLS: No certificate was found for verification. Did you set the GnuTLSX509CAFile or GnuTLSPGPKeyringFile directives?"); |
---|
[7bebb42] | 1138 | ret = HTTP_FORBIDDEN; |
---|
| 1139 | goto exit; |
---|
[c301152] | 1140 | } |
---|
[7bebb42] | 1141 | |
---|
[2b3a248b] | 1142 | /* TODO: X509 CRL Verification. */ |
---|
| 1143 | /* May add later if anyone needs it. |
---|
| 1144 | */ |
---|
| 1145 | /* ret = gnutls_x509_crt_check_revocation(crt, crl_list, crl_list_size); */ |
---|
| 1146 | |
---|
[7bebb42] | 1147 | expired = 0; |
---|
| 1148 | cur_time = apr_time_now(); |
---|
| 1149 | if (activation_time > cur_time) { |
---|
| 1150 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1151 | "GnuTLS: Failed to Verify Peer: " |
---|
| 1152 | "Peer Certificate is not yet activated."); |
---|
| 1153 | expired = 1; |
---|
[836c2f9] | 1154 | } |
---|
| 1155 | |
---|
[f7a7d66] | 1156 | if (gnutls_certificate_type_get( ctxt->session) != GNUTLS_CRT_OPENPGP || expiration_time != 0) { |
---|
| 1157 | if (expiration_time < cur_time) { |
---|
| 1158 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
[7bebb42] | 1159 | "GnuTLS: Failed to Verify Peer: " |
---|
| 1160 | "Peer Certificate is expired."); |
---|
[f7a7d66] | 1161 | expired = 1; |
---|
| 1162 | } |
---|
[7bebb42] | 1163 | } |
---|
[836c2f9] | 1164 | |
---|
[7bebb42] | 1165 | if (status & GNUTLS_CERT_SIGNER_NOT_FOUND) { |
---|
| 1166 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1167 | "GnuTLS: Could not find Signer for Peer Certificate"); |
---|
[c301152] | 1168 | } |
---|
[836c2f9] | 1169 | |
---|
[7bebb42] | 1170 | if (status & GNUTLS_CERT_SIGNER_NOT_CA) { |
---|
| 1171 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1172 | "GnuTLS: Peer's Certificate signer is not a CA"); |
---|
| 1173 | } |
---|
| 1174 | |
---|
| 1175 | if (status & GNUTLS_CERT_INVALID) { |
---|
| 1176 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1177 | "GnuTLS: Peer Certificate is invalid."); |
---|
| 1178 | } else if (status & GNUTLS_CERT_REVOKED) { |
---|
| 1179 | ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, |
---|
| 1180 | "GnuTLS: Peer Certificate is revoked."); |
---|
| 1181 | } |
---|
| 1182 | |
---|
[e5bbda4] | 1183 | if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) |
---|
[67101aa] | 1184 | mgs_add_common_cert_vars(r, cert.x509[0], 1, |
---|
[e5bbda4] | 1185 | ctxt->sc->export_certificates_enabled); |
---|
| 1186 | else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP) |
---|
| 1187 | mgs_add_common_pgpcert_vars(r, cert.pgp, 1, |
---|
[fd73a08] | 1188 | ctxt->sc->export_certificates_enabled); |
---|
[7bebb42] | 1189 | |
---|
| 1190 | { |
---|
[fd73a08] | 1191 | /* days remaining */ |
---|
| 1192 | unsigned long remain = |
---|
| 1193 | (apr_time_sec(expiration_time) - |
---|
| 1194 | apr_time_sec(cur_time)) / 86400; |
---|
| 1195 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_V_REMAIN", |
---|
| 1196 | apr_psprintf(r->pool, "%lu", remain)); |
---|
[7bebb42] | 1197 | } |
---|
| 1198 | |
---|
| 1199 | if (status == 0 && expired == 0) { |
---|
| 1200 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", "SUCCESS"); |
---|
| 1201 | ret = OK; |
---|
| 1202 | } else { |
---|
| 1203 | apr_table_setn(r->subprocess_env, "SSL_CLIENT_VERIFY", "FAILED"); |
---|
| 1204 | if (ctxt->sc->client_verify_mode == GNUTLS_CERT_REQUEST) |
---|
| 1205 | ret = OK; |
---|
| 1206 | else |
---|
| 1207 | ret = HTTP_FORBIDDEN; |
---|
| 1208 | } |
---|
| 1209 | |
---|
| 1210 | exit: |
---|
[67101aa] | 1211 | if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_X509) { |
---|
| 1212 | int i; |
---|
| 1213 | for (i=0; i<ch_size; i++) { |
---|
| 1214 | gnutls_x509_crt_deinit(cert.x509[i]); |
---|
| 1215 | } |
---|
| 1216 | } else if (gnutls_certificate_type_get( ctxt->session) == GNUTLS_CRT_OPENPGP) |
---|
[e5bbda4] | 1217 | gnutls_openpgp_crt_deinit(cert.pgp); |
---|
[7bebb42] | 1218 | return ret; |
---|
| 1219 | |
---|
| 1220 | |
---|
| 1221 | } |
---|