[46b85d8] | 1 | /** |
---|
| 2 | * Copyright 2004-2005 Paul Querna |
---|
[e183628] | 3 | * Copyright 2008 Nikos Mavrogiannopoulos |
---|
| 4 | * Copyright 2011 Dash Shendy |
---|
[46b85d8] | 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 | |
---|
[7bebb42] | 22 | static int load_datum_from_file(apr_pool_t * pool, |
---|
[e183628] | 23 | const char *file, gnutls_datum_t * data) { |
---|
| 24 | apr_file_t *fp; |
---|
| 25 | apr_finfo_t finfo; |
---|
| 26 | apr_status_t rv; |
---|
| 27 | apr_size_t br = 0; |
---|
[7bebb42] | 28 | |
---|
[e183628] | 29 | rv = apr_file_open(&fp, file, APR_READ | APR_BINARY, |
---|
| 30 | APR_OS_DEFAULT, pool); |
---|
| 31 | if (rv != APR_SUCCESS) { |
---|
| 32 | return rv; |
---|
| 33 | } |
---|
[7bebb42] | 34 | |
---|
[e183628] | 35 | rv = apr_file_info_get(&finfo, APR_FINFO_SIZE, fp); |
---|
[7bebb42] | 36 | |
---|
[e183628] | 37 | if (rv != APR_SUCCESS) { |
---|
| 38 | return rv; |
---|
| 39 | } |
---|
[7bebb42] | 40 | |
---|
[e183628] | 41 | data->data = apr_palloc(pool, finfo.size + 1); |
---|
| 42 | rv = apr_file_read_full(fp, data->data, finfo.size, &br); |
---|
[7bebb42] | 43 | |
---|
[e183628] | 44 | if (rv != APR_SUCCESS) { |
---|
| 45 | return rv; |
---|
| 46 | } |
---|
| 47 | apr_file_close(fp); |
---|
[7bebb42] | 48 | |
---|
[e183628] | 49 | data->data[br] = '\0'; |
---|
| 50 | data->size = br; |
---|
[7bebb42] | 51 | |
---|
[e183628] | 52 | return 0; |
---|
[46b85d8] | 53 | } |
---|
| 54 | |
---|
[7bebb42] | 55 | const char *mgs_set_dh_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 56 | const char *arg) { |
---|
| 57 | int ret; |
---|
| 58 | gnutls_datum_t data; |
---|
| 59 | const char *file; |
---|
| 60 | apr_pool_t *spool; |
---|
| 61 | mgs_srvconf_rec *sc = |
---|
| 62 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 63 | module_config, |
---|
| 64 | &gnutls_module); |
---|
| 65 | |
---|
| 66 | apr_pool_create(&spool, parms->pool); |
---|
| 67 | |
---|
| 68 | file = ap_server_root_relative(spool, arg); |
---|
| 69 | |
---|
| 70 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
| 71 | return apr_psprintf(parms->pool, "GnuTLS: Error Reading " |
---|
| 72 | "DH params '%s'", file); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | ret = gnutls_dh_params_init(&sc->dh_params); |
---|
| 76 | if (ret < 0) { |
---|
| 77 | return apr_psprintf(parms->pool, |
---|
| 78 | "GnuTLS: Failed to initialize" |
---|
| 79 | ": (%d) %s", ret, |
---|
| 80 | gnutls_strerror(ret)); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | ret = |
---|
| 84 | gnutls_dh_params_import_pkcs3(sc->dh_params, &data, |
---|
| 85 | GNUTLS_X509_FMT_PEM); |
---|
| 86 | if (ret < 0) { |
---|
| 87 | return apr_psprintf(parms->pool, |
---|
| 88 | "GnuTLS: Failed to Import " |
---|
| 89 | "DH params '%s': (%d) %s", file, ret, |
---|
| 90 | gnutls_strerror(ret)); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | apr_pool_destroy(spool); |
---|
| 94 | |
---|
| 95 | return NULL; |
---|
[7bebb42] | 96 | } |
---|
| 97 | |
---|
[3b4c0d0] | 98 | const char *mgs_set_cert_file(cmd_parms * parms, void *dummy, const char *arg) { |
---|
| 99 | |
---|
[e183628] | 100 | int ret; |
---|
| 101 | gnutls_datum_t data; |
---|
| 102 | const char *file; |
---|
| 103 | apr_pool_t *spool; |
---|
| 104 | |
---|
[3b4c0d0] | 105 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) ap_get_module_config(parms->server->module_config, &gnutls_module); |
---|
[e183628] | 106 | apr_pool_create(&spool, parms->pool); |
---|
| 107 | |
---|
| 108 | file = ap_server_root_relative(spool, arg); |
---|
| 109 | |
---|
| 110 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
[3b4c0d0] | 111 | apr_pool_destroy(spool); |
---|
| 112 | return apr_psprintf(parms->pool, "GnuTLS: Error Reading Certificate '%s'", file); |
---|
[e183628] | 113 | } |
---|
| 114 | |
---|
[3b4c0d0] | 115 | sc->certs_x509_chain_num = MAX_CHAIN_SIZE; |
---|
| 116 | ret = gnutls_x509_crt_list_import(sc->certs_x509_chain, &sc->certs_x509_chain_num, &data, GNUTLS_X509_FMT_PEM, 0); |
---|
[e183628] | 117 | if (ret < 0) { |
---|
[3b4c0d0] | 118 | apr_pool_destroy(spool); |
---|
| 119 | return apr_psprintf(parms->pool, "GnuTLS: Failed to Import Certificate '%s': (%d) %s", file, ret, gnutls_strerror(ret)); |
---|
[e183628] | 120 | } |
---|
[3b4c0d0] | 121 | |
---|
| 122 | apr_pool_destroy(spool); |
---|
[e183628] | 123 | return NULL; |
---|
| 124 | |
---|
[46b85d8] | 125 | } |
---|
| 126 | |
---|
[3b4c0d0] | 127 | const char *mgs_set_key_file(cmd_parms * parms, void *dummy, const char *arg) { |
---|
| 128 | |
---|
[e183628] | 129 | int ret; |
---|
| 130 | gnutls_datum_t data; |
---|
| 131 | const char *file; |
---|
| 132 | apr_pool_t *spool; |
---|
[929d313] | 133 | const char *out; |
---|
[3b4c0d0] | 134 | |
---|
| 135 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) ap_get_module_config(parms->server->module_config, &gnutls_module); |
---|
| 136 | |
---|
| 137 | apr_pool_create(&spool, parms->pool); |
---|
[e183628] | 138 | |
---|
| 139 | file = ap_server_root_relative(spool, arg); |
---|
| 140 | |
---|
| 141 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
[929d313] | 142 | out = apr_psprintf(parms->pool, "GnuTLS: Error Reading Private Key '%s'", file); |
---|
[3b4c0d0] | 143 | apr_pool_destroy(spool); |
---|
[929d313] | 144 | return out; |
---|
[e183628] | 145 | } |
---|
| 146 | |
---|
| 147 | ret = gnutls_x509_privkey_init(&sc->privkey_x509); |
---|
[3b4c0d0] | 148 | |
---|
[e183628] | 149 | if (ret < 0) { |
---|
[3b4c0d0] | 150 | apr_pool_destroy(spool); |
---|
| 151 | return apr_psprintf(parms->pool, "GnuTLS: Failed to initialize: (%d) %s", ret, gnutls_strerror(ret)); |
---|
[e183628] | 152 | } |
---|
| 153 | |
---|
[3b4c0d0] | 154 | ret = gnutls_x509_privkey_import(sc->privkey_x509, &data, GNUTLS_X509_FMT_PEM); |
---|
[e183628] | 155 | |
---|
[3b4c0d0] | 156 | if (ret < 0) { |
---|
| 157 | ret = gnutls_x509_privkey_import_pkcs8(sc->privkey_x509, &data, GNUTLS_X509_FMT_PEM, NULL, GNUTLS_PKCS_PLAIN); |
---|
| 158 | } |
---|
[e183628] | 159 | |
---|
| 160 | if (ret < 0) { |
---|
[929d313] | 161 | out = apr_psprintf(parms->pool, "GnuTLS: Failed to Import Private Key '%s': (%d) %s", file, ret, gnutls_strerror(ret)); |
---|
[3b4c0d0] | 162 | apr_pool_destroy(spool); |
---|
[929d313] | 163 | return out; |
---|
[e183628] | 164 | } |
---|
[3b4c0d0] | 165 | |
---|
[e183628] | 166 | apr_pool_destroy(spool); |
---|
[3b4c0d0] | 167 | |
---|
[e183628] | 168 | return NULL; |
---|
[46b85d8] | 169 | } |
---|
| 170 | |
---|
[e5bbda4] | 171 | const char *mgs_set_pgpcert_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 172 | const char *arg) { |
---|
| 173 | int ret; |
---|
| 174 | gnutls_datum_t data; |
---|
| 175 | const char *file; |
---|
| 176 | apr_pool_t *spool; |
---|
| 177 | mgs_srvconf_rec *sc = |
---|
| 178 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 179 | module_config, |
---|
| 180 | &gnutls_module); |
---|
| 181 | apr_pool_create(&spool, parms->pool); |
---|
| 182 | |
---|
| 183 | file = ap_server_root_relative(spool, arg); |
---|
| 184 | |
---|
| 185 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
| 186 | return apr_psprintf(parms->pool, "GnuTLS: Error Reading " |
---|
| 187 | "Certificate '%s'", file); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | ret = gnutls_openpgp_crt_init(&sc->cert_pgp); |
---|
| 191 | if (ret < 0) { |
---|
| 192 | return apr_psprintf(parms->pool, "GnuTLS: Failed to Init " |
---|
| 193 | "PGP Certificate: (%d) %s", ret, |
---|
| 194 | gnutls_strerror(ret)); |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | ret = |
---|
| 198 | gnutls_openpgp_crt_import(sc->cert_pgp, &data, |
---|
| 199 | GNUTLS_OPENPGP_FMT_BASE64); |
---|
| 200 | if (ret < 0) { |
---|
| 201 | return apr_psprintf(parms->pool, |
---|
| 202 | "GnuTLS: Failed to Import " |
---|
| 203 | "PGP Certificate '%s': (%d) %s", file, |
---|
| 204 | ret, gnutls_strerror(ret)); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | apr_pool_destroy(spool); |
---|
| 208 | return NULL; |
---|
[e5bbda4] | 209 | } |
---|
| 210 | |
---|
| 211 | const char *mgs_set_pgpkey_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 212 | const char *arg) { |
---|
| 213 | int ret; |
---|
| 214 | gnutls_datum_t data; |
---|
| 215 | const char *file; |
---|
| 216 | apr_pool_t *spool; |
---|
| 217 | mgs_srvconf_rec *sc = |
---|
| 218 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 219 | module_config, |
---|
| 220 | &gnutls_module); |
---|
| 221 | apr_pool_create(&spool, parms->pool); |
---|
| 222 | |
---|
| 223 | file = ap_server_root_relative(spool, arg); |
---|
| 224 | |
---|
| 225 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
| 226 | return apr_psprintf(parms->pool, "GnuTLS: Error Reading " |
---|
| 227 | "Private Key '%s'", file); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | ret = gnutls_openpgp_privkey_init(&sc->privkey_pgp); |
---|
| 231 | if (ret < 0) { |
---|
| 232 | return apr_psprintf(parms->pool, |
---|
| 233 | "GnuTLS: Failed to initialize" |
---|
| 234 | ": (%d) %s", ret, |
---|
| 235 | gnutls_strerror(ret)); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | ret = |
---|
| 239 | gnutls_openpgp_privkey_import(sc->privkey_pgp, &data, |
---|
| 240 | GNUTLS_OPENPGP_FMT_BASE64, NULL, |
---|
| 241 | 0); |
---|
| 242 | if (ret != 0) { |
---|
| 243 | return apr_psprintf(parms->pool, |
---|
| 244 | "GnuTLS: Failed to Import " |
---|
| 245 | "PGP Private Key '%s': (%d) %s", file, |
---|
| 246 | ret, gnutls_strerror(ret)); |
---|
| 247 | } |
---|
| 248 | apr_pool_destroy(spool); |
---|
| 249 | return NULL; |
---|
[e5bbda4] | 250 | } |
---|
| 251 | |
---|
[ae233c2] | 252 | const char *mgs_set_tickets(cmd_parms * parms, void *dummy, |
---|
[e183628] | 253 | const char *arg) { |
---|
| 254 | mgs_srvconf_rec *sc = |
---|
| 255 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 256 | module_config, |
---|
| 257 | &gnutls_module); |
---|
| 258 | |
---|
| 259 | sc->tickets = 0; |
---|
| 260 | if (strcasecmp("on", arg) == 0) { |
---|
| 261 | sc->tickets = 1; |
---|
| 262 | } |
---|
| 263 | |
---|
| 264 | return NULL; |
---|
[ae233c2] | 265 | } |
---|
| 266 | |
---|
[e5bbda4] | 267 | |
---|
[787dab7] | 268 | #ifdef ENABLE_SRP |
---|
| 269 | |
---|
[7bebb42] | 270 | const char *mgs_set_srp_tpasswd_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 271 | const char *arg) { |
---|
| 272 | mgs_srvconf_rec *sc = |
---|
| 273 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 274 | module_config, |
---|
| 275 | &gnutls_module); |
---|
[7bebb42] | 276 | |
---|
[e183628] | 277 | sc->srp_tpasswd_file = ap_server_root_relative(parms->pool, arg); |
---|
[7bebb42] | 278 | |
---|
[e183628] | 279 | return NULL; |
---|
[7bebb42] | 280 | } |
---|
| 281 | |
---|
| 282 | const char *mgs_set_srp_tpasswd_conf_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 283 | const char *arg) { |
---|
| 284 | mgs_srvconf_rec *sc = |
---|
| 285 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 286 | module_config, |
---|
| 287 | &gnutls_module); |
---|
[7bebb42] | 288 | |
---|
[e183628] | 289 | sc->srp_tpasswd_conf_file = |
---|
| 290 | ap_server_root_relative(parms->pool, arg); |
---|
[7bebb42] | 291 | |
---|
[e183628] | 292 | return NULL; |
---|
[7bebb42] | 293 | } |
---|
| 294 | |
---|
[787dab7] | 295 | #endif |
---|
| 296 | |
---|
[46b85d8] | 297 | const char *mgs_set_cache(cmd_parms * parms, void *dummy, |
---|
[e183628] | 298 | const char *type, const char *arg) { |
---|
| 299 | const char *err; |
---|
| 300 | mgs_srvconf_rec *sc = |
---|
| 301 | ap_get_module_config(parms->server->module_config, |
---|
| 302 | &gnutls_module); |
---|
| 303 | if ((err = ap_check_cmd_context(parms, GLOBAL_ONLY))) { |
---|
| 304 | return err; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | if (strcasecmp("none", type) == 0) { |
---|
| 308 | sc->cache_type = mgs_cache_none; |
---|
| 309 | sc->cache_config = NULL; |
---|
| 310 | return NULL; |
---|
| 311 | } else if (strcasecmp("dbm", type) == 0) { |
---|
| 312 | sc->cache_type = mgs_cache_dbm; |
---|
| 313 | } else if (strcasecmp("gdbm", type) == 0) { |
---|
| 314 | sc->cache_type = mgs_cache_gdbm; |
---|
| 315 | } |
---|
[46b85d8] | 316 | #if HAVE_APR_MEMCACHE |
---|
[e183628] | 317 | else if (strcasecmp("memcache", type) == 0) { |
---|
| 318 | sc->cache_type = mgs_cache_memcache; |
---|
| 319 | } |
---|
[46b85d8] | 320 | #endif |
---|
[e183628] | 321 | else { |
---|
| 322 | return "Invalid Type for GnuTLSCache!"; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | if (arg == NULL) |
---|
| 326 | return "Invalid argument 2 for GnuTLSCache!"; |
---|
| 327 | |
---|
| 328 | if (sc->cache_type == mgs_cache_dbm |
---|
| 329 | || sc->cache_type == mgs_cache_gdbm) { |
---|
| 330 | sc->cache_config = |
---|
| 331 | ap_server_root_relative(parms->pool, arg); |
---|
| 332 | } else { |
---|
| 333 | sc->cache_config = apr_pstrdup(parms->pool, arg); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | return NULL; |
---|
[46b85d8] | 337 | } |
---|
| 338 | |
---|
| 339 | const char *mgs_set_cache_timeout(cmd_parms * parms, void *dummy, |
---|
[e183628] | 340 | const char *arg) { |
---|
| 341 | int argint; |
---|
[480aba1] | 342 | const char *err; |
---|
[e183628] | 343 | mgs_srvconf_rec *sc = |
---|
| 344 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 345 | module_config, |
---|
| 346 | &gnutls_module); |
---|
| 347 | |
---|
[480aba1] | 348 | if ((err = ap_check_cmd_context(parms, GLOBAL_ONLY))) { |
---|
| 349 | return err; |
---|
| 350 | } |
---|
| 351 | |
---|
[e183628] | 352 | argint = atoi(arg); |
---|
| 353 | |
---|
| 354 | if (argint < 0) { |
---|
| 355 | return "GnuTLSCacheTimeout: Invalid argument"; |
---|
| 356 | } else if (argint == 0) { |
---|
| 357 | sc->cache_timeout = 0; |
---|
| 358 | } else { |
---|
| 359 | sc->cache_timeout = apr_time_from_sec(argint); |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | return NULL; |
---|
[e02dd8c] | 363 | } |
---|
| 364 | |
---|
| 365 | const char *mgs_set_client_verify(cmd_parms * parms, void *dummy, |
---|
[e183628] | 366 | const char *arg) { |
---|
| 367 | int mode; |
---|
| 368 | |
---|
| 369 | if (strcasecmp("none", arg) == 0 || strcasecmp("ignore", arg) == 0) { |
---|
| 370 | mode = GNUTLS_CERT_IGNORE; |
---|
| 371 | } else if (strcasecmp("optional", arg) == 0 |
---|
| 372 | || strcasecmp("request", arg) == 0) { |
---|
| 373 | mode = GNUTLS_CERT_REQUEST; |
---|
| 374 | } else if (strcasecmp("require", arg) == 0) { |
---|
| 375 | mode = GNUTLS_CERT_REQUIRE; |
---|
| 376 | } else { |
---|
| 377 | return "GnuTLSClientVerify: Invalid argument"; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /* This was set from a directory context */ |
---|
| 381 | if (parms->path) { |
---|
| 382 | mgs_dirconf_rec *dc = (mgs_dirconf_rec *) dummy; |
---|
| 383 | dc->client_verify_mode = mode; |
---|
| 384 | } else { |
---|
| 385 | mgs_srvconf_rec *sc = |
---|
| 386 | (mgs_srvconf_rec *) |
---|
| 387 | ap_get_module_config(parms->server->module_config, |
---|
| 388 | &gnutls_module); |
---|
| 389 | sc->client_verify_mode = mode; |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | return NULL; |
---|
[46b85d8] | 393 | } |
---|
| 394 | |
---|
[8663ace] | 395 | #define INIT_CA_SIZE 128 |
---|
[e183628] | 396 | |
---|
[46b85d8] | 397 | const char *mgs_set_client_ca_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 398 | const char *arg) { |
---|
| 399 | int rv; |
---|
| 400 | const char *file; |
---|
| 401 | apr_pool_t *spool; |
---|
| 402 | gnutls_datum_t data; |
---|
| 403 | |
---|
| 404 | mgs_srvconf_rec *sc = |
---|
| 405 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 406 | module_config, |
---|
| 407 | &gnutls_module); |
---|
| 408 | apr_pool_create(&spool, parms->pool); |
---|
| 409 | |
---|
| 410 | file = ap_server_root_relative(spool, arg); |
---|
| 411 | |
---|
| 412 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
| 413 | return apr_psprintf(parms->pool, "GnuTLS: Error Reading " |
---|
| 414 | "Client CA File '%s'", file); |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | sc->ca_list_size = INIT_CA_SIZE; |
---|
| 418 | sc->ca_list = malloc(sc->ca_list_size * sizeof (*sc->ca_list)); |
---|
| 419 | if (sc->ca_list == NULL) { |
---|
| 420 | return apr_psprintf(parms->pool, |
---|
| 421 | "mod_gnutls: Memory allocation error"); |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | rv = gnutls_x509_crt_list_import(sc->ca_list, &sc->ca_list_size, |
---|
| 425 | &data, GNUTLS_X509_FMT_PEM, |
---|
| 426 | GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED); |
---|
| 427 | if (rv < 0 && rv != GNUTLS_E_SHORT_MEMORY_BUFFER) { |
---|
| 428 | return apr_psprintf(parms->pool, "GnuTLS: Failed to load " |
---|
| 429 | "Client CA File '%s': (%d) %s", file, |
---|
| 430 | rv, gnutls_strerror(rv)); |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | if (INIT_CA_SIZE < sc->ca_list_size) { |
---|
| 434 | sc->ca_list = |
---|
| 435 | realloc(sc->ca_list, |
---|
| 436 | sc->ca_list_size * sizeof (*sc->ca_list)); |
---|
| 437 | if (sc->ca_list == NULL) { |
---|
| 438 | return apr_psprintf(parms->pool, |
---|
| 439 | "mod_gnutls: Memory allocation error"); |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | /* re-read */ |
---|
| 443 | rv = gnutls_x509_crt_list_import(sc->ca_list, |
---|
| 444 | &sc->ca_list_size, &data, |
---|
| 445 | GNUTLS_X509_FMT_PEM, 0); |
---|
| 446 | |
---|
| 447 | if (rv < 0) { |
---|
| 448 | return apr_psprintf(parms->pool, |
---|
| 449 | "GnuTLS: Failed to load " |
---|
| 450 | "Client CA File '%s': (%d) %s", |
---|
| 451 | file, rv, gnutls_strerror(rv)); |
---|
| 452 | } |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | apr_pool_destroy(spool); |
---|
| 456 | return NULL; |
---|
[46b85d8] | 457 | } |
---|
| 458 | |
---|
[e5bbda4] | 459 | const char *mgs_set_keyring_file(cmd_parms * parms, void *dummy, |
---|
[e183628] | 460 | const char *arg) { |
---|
| 461 | int rv; |
---|
| 462 | const char *file; |
---|
| 463 | apr_pool_t *spool; |
---|
| 464 | gnutls_datum_t data; |
---|
| 465 | |
---|
| 466 | mgs_srvconf_rec *sc = |
---|
| 467 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 468 | module_config, |
---|
| 469 | &gnutls_module); |
---|
| 470 | apr_pool_create(&spool, parms->pool); |
---|
| 471 | |
---|
| 472 | file = ap_server_root_relative(spool, arg); |
---|
| 473 | |
---|
| 474 | if (load_datum_from_file(spool, file, &data) != 0) { |
---|
| 475 | return apr_psprintf(parms->pool, "GnuTLS: Error Reading " |
---|
| 476 | "Keyring File '%s'", file); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | rv = gnutls_openpgp_keyring_init(&sc->pgp_list); |
---|
| 480 | if (rv < 0) { |
---|
| 481 | return apr_psprintf(parms->pool, |
---|
| 482 | "GnuTLS: Failed to initialize" |
---|
| 483 | "keyring: (%d) %s", rv, |
---|
| 484 | gnutls_strerror(rv)); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | rv = gnutls_openpgp_keyring_import(sc->pgp_list, &data, |
---|
| 488 | GNUTLS_OPENPGP_FMT_BASE64); |
---|
| 489 | if (rv < 0) { |
---|
| 490 | return apr_psprintf(parms->pool, "GnuTLS: Failed to load " |
---|
| 491 | "Keyring File '%s': (%d) %s", file, rv, |
---|
| 492 | gnutls_strerror(rv)); |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | apr_pool_destroy(spool); |
---|
| 496 | return NULL; |
---|
[e5bbda4] | 497 | } |
---|
| 498 | |
---|
[33826c5] | 499 | const char *mgs_set_proxy_engine(cmd_parms * parms, void *dummy, |
---|
| 500 | const char *arg) { |
---|
| 501 | |
---|
| 502 | mgs_srvconf_rec *sc =(mgs_srvconf_rec *) |
---|
| 503 | ap_get_module_config(parms->server->module_config, &gnutls_module); |
---|
| 504 | |
---|
| 505 | if (!strcasecmp(arg, "On")) { |
---|
| 506 | sc->proxy_enabled = GNUTLS_ENABLED_TRUE; |
---|
| 507 | } else if (!strcasecmp(arg, "Off")) { |
---|
| 508 | sc->proxy_enabled = GNUTLS_ENABLED_FALSE; |
---|
| 509 | } else { |
---|
| 510 | return "SSLProxyEngine must be set to 'On' or 'Off'"; |
---|
| 511 | } |
---|
| 512 | |
---|
| 513 | return NULL; |
---|
| 514 | } |
---|
| 515 | |
---|
[46b85d8] | 516 | const char *mgs_set_enabled(cmd_parms * parms, void *dummy, |
---|
[e183628] | 517 | const char *arg) { |
---|
| 518 | mgs_srvconf_rec *sc = |
---|
| 519 | (mgs_srvconf_rec *) ap_get_module_config(parms->server-> |
---|
| 520 | module_config, |
---|
| 521 | &gnutls_module); |
---|
| 522 | if (!strcasecmp(arg, "On")) { |
---|
| 523 | sc->enabled = GNUTLS_ENABLED_TRUE; |
---|
| 524 | } else if (!strcasecmp(arg, "Off")) { |
---|
| 525 | sc->enabled = GNUTLS_ENABLED_FALSE; |
---|
| 526 | } else { |
---|
| 527 | return "GnuTLSEnable must be set to 'On' or 'Off'"; |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | return NULL; |
---|
[7bebb42] | 531 | } |
---|
| 532 | |
---|
[7d1ab49] | 533 | const char *mgs_set_export_certificates_enabled(cmd_parms * parms, void *dummy, const char *arg) { |
---|
| 534 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) ap_get_module_config(parms->server->module_config, &gnutls_module); |
---|
| 535 | if (!strcasecmp(arg, "On")) { |
---|
| 536 | sc->export_certificates_enabled = GNUTLS_ENABLED_TRUE; |
---|
| 537 | } else if (!strcasecmp(arg, "Off")) { |
---|
| 538 | sc->export_certificates_enabled = GNUTLS_ENABLED_FALSE; |
---|
| 539 | } else { |
---|
| 540 | return |
---|
| 541 | "GnuTLSExportCertificates must be set to 'On' or 'Off'"; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | return NULL; |
---|
| 545 | } |
---|
| 546 | |
---|
[3b4c0d0] | 547 | const char *mgs_set_priorities(cmd_parms * parms, void *dummy, const char *arg) { |
---|
[e183628] | 548 | |
---|
[3b4c0d0] | 549 | int ret; |
---|
[e183628] | 550 | const char *err; |
---|
| 551 | |
---|
[3b4c0d0] | 552 | mgs_srvconf_rec *sc = (mgs_srvconf_rec *) |
---|
| 553 | ap_get_module_config(parms->server->module_config, &gnutls_module); |
---|
[e183628] | 554 | |
---|
| 555 | ret = gnutls_priority_init(&sc->priorities, arg, &err); |
---|
[3b4c0d0] | 556 | |
---|
[e183628] | 557 | if (ret < 0) { |
---|
[3b4c0d0] | 558 | if (ret == GNUTLS_E_INVALID_REQUEST) { |
---|
| 559 | return apr_psprintf(parms->pool, |
---|
| 560 | "GnuTLS: Syntax error parsing priorities string at: %s", err); |
---|
| 561 | } |
---|
[e183628] | 562 | return "Error setting priorities"; |
---|
| 563 | } |
---|
| 564 | |
---|
| 565 | return NULL; |
---|
[46b85d8] | 566 | } |
---|
| 567 | |
---|
[040387c] | 568 | static mgs_srvconf_rec *_mgs_config_server_create(apr_pool_t * p, char** err) { |
---|
[e183628] | 569 | mgs_srvconf_rec *sc = apr_pcalloc(p, sizeof (*sc)); |
---|
| 570 | int ret; |
---|
| 571 | |
---|
[040387c] | 572 | sc->enabled = GNUTLS_ENABLED_UNSET; |
---|
[e183628] | 573 | |
---|
| 574 | ret = gnutls_certificate_allocate_credentials(&sc->certs); |
---|
| 575 | if (ret < 0) { |
---|
[040387c] | 576 | *err = apr_psprintf(p, "GnuTLS: Failed to initialize" |
---|
| 577 | ": (%d) %s", ret, |
---|
| 578 | gnutls_strerror(ret)); |
---|
| 579 | return NULL; |
---|
[e183628] | 580 | } |
---|
| 581 | |
---|
| 582 | ret = gnutls_anon_allocate_server_credentials(&sc->anon_creds); |
---|
| 583 | if (ret < 0) { |
---|
[040387c] | 584 | *err = apr_psprintf(p, "GnuTLS: Failed to initialize" |
---|
| 585 | ": (%d) %s", ret, |
---|
| 586 | gnutls_strerror(ret)); |
---|
| 587 | return NULL; |
---|
[e183628] | 588 | } |
---|
[787dab7] | 589 | #ifdef ENABLE_SRP |
---|
[e183628] | 590 | ret = gnutls_srp_allocate_server_credentials(&sc->srp_creds); |
---|
| 591 | if (ret < 0) { |
---|
[040387c] | 592 | *err = apr_psprintf(p, "GnuTLS: Failed to initialize" |
---|
| 593 | ": (%d) %s", ret, |
---|
| 594 | gnutls_strerror(ret)); |
---|
| 595 | return NULL; |
---|
[e183628] | 596 | } |
---|
| 597 | |
---|
| 598 | sc->srp_tpasswd_conf_file = NULL; |
---|
| 599 | sc->srp_tpasswd_file = NULL; |
---|
[787dab7] | 600 | #endif |
---|
| 601 | |
---|
[e183628] | 602 | sc->privkey_x509 = NULL; |
---|
[3b4c0d0] | 603 | /* Initialize all Certificate Chains */ |
---|
[040387c] | 604 | /* FIXME: how do we indicate that this is unset for a merge? (that |
---|
| 605 | * is, how can a subordinate server override the chain by setting |
---|
| 606 | * an empty one? what would that even look like in the |
---|
| 607 | * configuration?) */ |
---|
[3b4c0d0] | 608 | sc->certs_x509_chain = malloc(MAX_CHAIN_SIZE * sizeof (*sc->certs_x509_chain)); |
---|
| 609 | sc->certs_x509_chain_num = 0; |
---|
[040387c] | 610 | sc->cache_timeout = -1; /* -1 means "unset" */ |
---|
| 611 | sc->cache_type = mgs_cache_unset; |
---|
[8400c2e] | 612 | sc->cache_config = NULL; |
---|
[040387c] | 613 | sc->tickets = GNUTLS_ENABLED_UNSET; |
---|
| 614 | sc->priorities = NULL; |
---|
| 615 | sc->dh_params = NULL; |
---|
| 616 | sc->proxy_enabled = GNUTLS_ENABLED_UNSET; |
---|
[7d1ab49] | 617 | sc->export_certificates_enabled = GNUTLS_ENABLED_UNSET; |
---|
[040387c] | 618 | |
---|
| 619 | /* this relies on GnuTLS never changing the gnutls_certificate_request_t enum to define -1 */ |
---|
| 620 | sc->client_verify_mode = -1; |
---|
[7bebb42] | 621 | |
---|
[040387c] | 622 | return sc; |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | void *mgs_config_server_create(apr_pool_t * p, server_rec * s) { |
---|
| 626 | char *err = NULL; |
---|
| 627 | mgs_srvconf_rec *sc = _mgs_config_server_create(p, &err); |
---|
| 628 | if (sc) return sc; else return err; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | #define gnutls_srvconf_merge(t, unset) sc->t = (add->t == unset) ? base->t : add->t |
---|
| 632 | #define gnutls_srvconf_assign(t) sc->t = add->t |
---|
| 633 | |
---|
| 634 | void *mgs_config_server_merge(apr_pool_t *p, void *BASE, void *ADD) { |
---|
| 635 | int i; |
---|
| 636 | char *err = NULL; |
---|
| 637 | mgs_srvconf_rec *base = (mgs_srvconf_rec *)BASE; |
---|
| 638 | mgs_srvconf_rec *add = (mgs_srvconf_rec *)ADD; |
---|
| 639 | mgs_srvconf_rec *sc = _mgs_config_server_create(p, &err); |
---|
| 640 | if (NULL == sc) return err; |
---|
| 641 | |
---|
| 642 | gnutls_srvconf_merge(enabled, GNUTLS_ENABLED_UNSET); |
---|
| 643 | gnutls_srvconf_merge(tickets, GNUTLS_ENABLED_UNSET); |
---|
| 644 | gnutls_srvconf_merge(proxy_enabled, GNUTLS_ENABLED_UNSET); |
---|
[7d1ab49] | 645 | gnutls_srvconf_merge(export_certificates_enabled, GNUTLS_ENABLED_UNSET); |
---|
[040387c] | 646 | gnutls_srvconf_merge(client_verify_mode, -1); |
---|
| 647 | gnutls_srvconf_merge(srp_tpasswd_file, NULL); |
---|
| 648 | gnutls_srvconf_merge(srp_tpasswd_conf_file, NULL); |
---|
| 649 | gnutls_srvconf_merge(privkey_x509, NULL); |
---|
| 650 | gnutls_srvconf_merge(priorities, NULL); |
---|
| 651 | gnutls_srvconf_merge(dh_params, NULL); |
---|
| 652 | |
---|
| 653 | /* FIXME: the following items are pre-allocated, and should be |
---|
| 654 | * properly disposed of before assigning in order to avoid leaks; |
---|
| 655 | * so at the moment, we can't actually have them in the config. |
---|
| 656 | * what happens during de-allocation? |
---|
| 657 | |
---|
| 658 | * This is probably leaky. |
---|
| 659 | */ |
---|
| 660 | gnutls_srvconf_assign(certs); |
---|
| 661 | gnutls_srvconf_assign(anon_creds); |
---|
| 662 | gnutls_srvconf_assign(srp_creds); |
---|
| 663 | gnutls_srvconf_assign(certs_x509_chain); |
---|
| 664 | gnutls_srvconf_assign(certs_x509_chain_num); |
---|
| 665 | |
---|
| 666 | /* how do these get transferred cleanly before the data from ADD |
---|
| 667 | * goes away? */ |
---|
| 668 | gnutls_srvconf_assign(cert_cn); |
---|
| 669 | for (i = 0; i < MAX_CERT_SAN; i++) |
---|
| 670 | gnutls_srvconf_assign(cert_san[i]); |
---|
| 671 | gnutls_srvconf_assign(ca_list); |
---|
| 672 | gnutls_srvconf_assign(ca_list_size); |
---|
| 673 | gnutls_srvconf_assign(cert_pgp); |
---|
| 674 | gnutls_srvconf_assign(pgp_list); |
---|
| 675 | gnutls_srvconf_assign(privkey_pgp); |
---|
[7bebb42] | 676 | |
---|
[e183628] | 677 | return sc; |
---|
[46b85d8] | 678 | } |
---|
| 679 | |
---|
[040387c] | 680 | #undef gnutls_srvconf_merge |
---|
| 681 | #undef gnutls_srvconf_assign |
---|
| 682 | |
---|
[e183628] | 683 | void *mgs_config_dir_merge(apr_pool_t * p, void *basev, void *addv) { |
---|
| 684 | mgs_dirconf_rec *new; |
---|
| 685 | /* mgs_dirconf_rec *base = (mgs_dirconf_rec *) basev; */ |
---|
| 686 | mgs_dirconf_rec *add = (mgs_dirconf_rec *) addv; |
---|
[e02dd8c] | 687 | |
---|
[e183628] | 688 | new = (mgs_dirconf_rec *) apr_pcalloc(p, sizeof (mgs_dirconf_rec)); |
---|
| 689 | new->client_verify_mode = add->client_verify_mode; |
---|
| 690 | return new; |
---|
[84cb5b2] | 691 | } |
---|
| 692 | |
---|
[e183628] | 693 | void *mgs_config_dir_create(apr_pool_t * p, char *dir) { |
---|
| 694 | mgs_dirconf_rec *dc = apr_palloc(p, sizeof (*dc)); |
---|
| 695 | dc->client_verify_mode = -1; |
---|
| 696 | return dc; |
---|
[46b85d8] | 697 | } |
---|
[040387c] | 698 | |
---|