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