· 5 years ago · Sep 18, 2020, 09:54 AM
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* _ _
18 * _ __ ___ ___ __| | ___ ___| | mod_ssl
19 * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
20 * | | | | | | (_) | (_| | \__ \__ \ |
21 * |_| |_| |_|\___/ \__,_|___|___/___/_|
22 * |_____|
23 * ssl_engine_init.c
24 * Initialization of Servers
25 */
26 /* ``Recursive, adj.;
27 see Recursive.''
28 -- Unknown */
29#include "ssl_private.h"
30#include "mod_ssl.h"
31#include "mod_ssl_openssl.h"
32#include "mpm_common.h"
33#include "mod_md.h"
34
35APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, init_server,
36 (server_rec *s,apr_pool_t *p,int is_proxy,SSL_CTX *ctx),
37 (s,p,is_proxy,ctx), OK, DECLINED)
38
39APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, add_cert_files,
40 (server_rec *s, apr_pool_t *p,
41 apr_array_header_t *cert_files, apr_array_header_t *key_files),
42 (s, p, cert_files, key_files),
43 OK, DECLINED)
44
45APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, add_fallback_cert_files,
46 (server_rec *s, apr_pool_t *p,
47 apr_array_header_t *cert_files, apr_array_header_t *key_files),
48 (s, p, cert_files, key_files),
49 OK, DECLINED)
50
51APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, answer_challenge,
52 (conn_rec *c, const char *server_name,
53 X509 **pcert, EVP_PKEY **pkey),
54 (c, server_name, pcert, pkey),
55 DECLINED, DECLINED)
56
57
58/* _________________________________________________________________
59**
60** Module Initialization
61** _________________________________________________________________
62*/
63
64#ifdef HAVE_ECC
65#define KEYTYPES "RSA, DSA or ECC"
66#else
67#define KEYTYPES "RSA or DSA"
68#endif
69
70#if MODSSL_USE_OPENSSL_PRE_1_1_API
71/* OpenSSL Pre-1.1.0 compatibility */
72/* Taken from OpenSSL 1.1.0 snapshot 20160410 */
73static int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
74{
75 /* q is optional */
76 if (p == NULL || g == NULL)
77 return 0;
78 BN_free(dh->p);
79 BN_free(dh->q);
80 BN_free(dh->g);
81 dh->p = p;
82 dh->q = q;
83 dh->g = g;
84
85 if (q != NULL) {
86 dh->length = BN_num_bits(q);
87 }
88
89 return 1;
90}
91#endif
92
93/*
94 * Grab well-defined DH parameters from OpenSSL, see the BN_get_rfc*
95 * functions in <openssl/bn.h> for all available primes.
96 */
97static DH *make_dh_params(BIGNUM *(*prime)(BIGNUM *))
98{
99 DH *dh = DH_new();
100 BIGNUM *p, *g;
101
102 if (!dh) {
103 return NULL;
104 }
105 p = prime(NULL);
106 g = BN_new();
107 if (g != NULL) {
108 BN_set_word(g, 2);
109 }
110 if (!p || !g || !DH_set0_pqg(dh, p, NULL, g)) {
111 DH_free(dh);
112 BN_free(p);
113 BN_free(g);
114 return NULL;
115 }
116 return dh;
117}
118
119/* Storage and initialization for DH parameters. */
120static struct dhparam {
121 BIGNUM *(*const prime)(BIGNUM *); /* function to generate... */
122 DH *dh; /* ...this, used for keys.... */
123 const unsigned int min; /* ...of length >= this. */
124} dhparams[] = {
125 { BN_get_rfc3526_prime_8192, NULL, 6145 },
126 { BN_get_rfc3526_prime_6144, NULL, 4097 },
127 { BN_get_rfc3526_prime_4096, NULL, 3073 },
128 { BN_get_rfc3526_prime_3072, NULL, 2049 },
129 { BN_get_rfc3526_prime_2048, NULL, 1025 },
130 { BN_get_rfc2409_prime_1024, NULL, 0 }
131};
132
133static void init_dh_params(void)
134{
135 unsigned n;
136
137 for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++)
138 dhparams[n].dh = make_dh_params(dhparams[n].prime);
139}
140
141static void free_dh_params(void)
142{
143 unsigned n;
144
145 /* DH_free() is a noop for a NULL parameter, so these are harmless
146 * in the (unexpected) case where these variables are already
147 * NULL. */
148 for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++) {
149 DH_free(dhparams[n].dh);
150 dhparams[n].dh = NULL;
151 }
152}
153
154/* Hand out the same DH structure though once generated as we leak
155 * memory otherwise and freeing the structure up after use would be
156 * hard to track and in fact is not needed at all as it is safe to
157 * use the same parameters over and over again security wise (in
158 * contrast to the keys itself) and code safe as the returned structure
159 * is duplicated by OpenSSL anyway. Hence no modification happens
160 * to our copy. */
161DH *modssl_get_dh_params(unsigned keylen)
162{
163 unsigned n;
164
165 for (n = 0; n < sizeof(dhparams)/sizeof(dhparams[0]); n++)
166 if (keylen >= dhparams[n].min)
167 return dhparams[n].dh;
168
169 return NULL; /* impossible to reach. */
170}
171
172static void ssl_add_version_components(apr_pool_t *p,
173 server_rec *s)
174{
175 char *modver = ssl_var_lookup(p, s, NULL, NULL, "SSL_VERSION_INTERFACE");
176 char *libver = ssl_var_lookup(p, s, NULL, NULL, "SSL_VERSION_LIBRARY");
177 char *incver = ssl_var_lookup(p, s, NULL, NULL,
178 "SSL_VERSION_LIBRARY_INTERFACE");
179
180 ap_add_version_component(p, libver);
181
182 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01876)
183 "%s compiled against Server: %s, Library: %s",
184 modver, AP_SERVER_BASEVERSION, incver);
185}
186
187/**************************************************************************************************/
188/* Managed Domains Interface */
189
190static APR_OPTIONAL_FN_TYPE(md_is_managed) *md_is_managed;
191static APR_OPTIONAL_FN_TYPE(md_get_certificate) *md_get_certificate;
192static APR_OPTIONAL_FN_TYPE(md_is_challenge) *md_is_challenge;
193
194int ssl_is_challenge(conn_rec *c, const char *servername,
195 X509 **pcert, EVP_PKEY **pkey)
196{
197 if (APR_SUCCESS == ssl_run_answer_challenge(c, servername, pcert, pkey)) {
198 return 1;
199 }
200 *pcert = NULL;
201 *pkey = NULL;
202 return 0;
203}
204
205#ifdef HAVE_FIPS
206static apr_status_t modssl_fips_cleanup(void *data)
207{
208 FIPS_mode_set(0);
209 return APR_SUCCESS;
210}
211#endif
212
213/*
214 * Per-module initialization
215 */
216apr_status_t ssl_init_Module(apr_pool_t *p, apr_pool_t *plog,
217 apr_pool_t *ptemp,
218 server_rec *base_server)
219{
220 SSLModConfigRec *mc = myModConfig(base_server);
221 SSLSrvConfigRec *sc;
222 server_rec *s;
223 apr_status_t rv;
224 apr_array_header_t *pphrases;
225
226#ifndef __VMS
227 if (SSLeay() < MODSSL_LIBRARY_VERSION) {
228 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server, APLOGNO(01882)
229 "Init: this version of mod_ssl was compiled against "
230 "a newer library (%s, version currently loaded is %s)"
231 " - may result in undefined or erroneous behavior",
232 MODSSL_LIBRARY_TEXT, MODSSL_LIBRARY_DYNTEXT);
233 }
234#endif
235
236 /* We initialize mc->pid per-process in the child init,
237 * but it should be initialized for startup before we
238 * call ssl_rand_seed() below.
239 */
240 mc->pid = getpid();
241
242 /*
243 * Let us cleanup on restarts and exits
244 */
245 apr_pool_cleanup_register(p, base_server,
246 ssl_init_ModuleKill,
247 apr_pool_cleanup_null);
248
249 /*
250 * Any init round fixes the global config
251 */
252 ssl_config_global_create(base_server); /* just to avoid problems */
253 ssl_config_global_fix(mc);
254
255 /* Initialize our interface to mod_md, if it is loaded
256 */
257 md_is_managed = APR_RETRIEVE_OPTIONAL_FN(md_is_managed);
258 md_get_certificate = APR_RETRIEVE_OPTIONAL_FN(md_get_certificate);
259 md_is_challenge = APR_RETRIEVE_OPTIONAL_FN(md_is_challenge);
260 if (!md_is_managed || !md_get_certificate) {
261 md_is_managed = NULL;
262 md_get_certificate = NULL;
263 }
264
265 /*
266 * try to fix the configuration and open the dedicated SSL
267 * logfile as early as possible
268 */
269 for (s = base_server; s; s = s->next) {
270 sc = mySrvConfig(s);
271
272 if (sc->server) {
273 sc->server->sc = sc;
274 }
275
276 /*
277 * Create the server host:port string because we need it a lot
278 */
279 if (sc->vhost_id) {
280 /* already set. This should only happen if this config rec is
281 * shared with another server. Argh! */
282 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(10104)
283 "%s, SSLSrvConfigRec shared from %s",
284 ssl_util_vhostid(p, s), sc->vhost_id);
285 }
286 sc->vhost_id = ssl_util_vhostid(p, s);
287 sc->vhost_id_len = strlen(sc->vhost_id);
288
289 /* Default to enabled if SSLEngine is not set explicitly, and
290 * the protocol is https. */
291 if (ap_get_server_protocol(s)
292 && strcmp("https", ap_get_server_protocol(s)) == 0
293 && sc->enabled == SSL_ENABLED_UNSET
294 && (!apr_is_empty_array(sc->server->pks->cert_files))) {
295 sc->enabled = SSL_ENABLED_TRUE;
296 }
297
298 /* Fix up stuff that may not have been set. If sc->enabled is
299 * UNSET, then SSL is disabled on this vhost. */
300 if (sc->enabled == SSL_ENABLED_UNSET) {
301 sc->enabled = SSL_ENABLED_FALSE;
302 }
303
304 if (sc->session_cache_timeout == UNSET) {
305 sc->session_cache_timeout = SSL_SESSION_CACHE_TIMEOUT;
306 }
307
308 if (sc->server && sc->server->pphrase_dialog_type == SSL_PPTYPE_UNSET) {
309 sc->server->pphrase_dialog_type = SSL_PPTYPE_BUILTIN;
310 }
311
312#ifdef HAVE_FIPS
313 if (sc->fips == UNSET) {
314 sc->fips = FALSE;
315 }
316#endif
317 }
318
319#if APR_HAS_THREADS && MODSSL_USE_OPENSSL_PRE_1_1_API
320 ssl_util_thread_setup(p);
321#endif
322
323 /*
324 * SSL external crypto device ("engine") support
325 */
326#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
327 if ((rv = ssl_init_Engine(base_server, p)) != APR_SUCCESS) {
328 return rv;
329 }
330#endif
331
332 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01883)
333 "Init: Initialized %s library", MODSSL_LIBRARY_NAME);
334
335 /*
336 * Seed the Pseudo Random Number Generator (PRNG)
337 * only need ptemp here; nothing inside allocated from the pool
338 * needs to live once we return from ssl_rand_seed().
339 */
340 ssl_rand_seed(base_server, ptemp, SSL_RSCTX_STARTUP, "Init: ");
341
342#ifdef HAVE_FIPS
343 if (sc->fips) {
344 if (!FIPS_mode()) {
345 if (FIPS_mode_set(1)) {
346 ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, APLOGNO(01884)
347 "Operating in SSL FIPS mode");
348 apr_pool_cleanup_register(p, NULL, modssl_fips_cleanup,
349 apr_pool_cleanup_null);
350 }
351 else {
352 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01885) "FIPS mode failed");
353 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
354 return ssl_die(s);
355 }
356 }
357 }
358 else {
359 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01886)
360 "SSL FIPS mode disabled");
361 }
362#endif
363
364 /*
365 * initialize the mutex handling
366 */
367 if (!ssl_mutex_init(base_server, p)) {
368 return HTTP_INTERNAL_SERVER_ERROR;
369 }
370#ifdef HAVE_OCSP_STAPLING
371 ssl_stapling_certinfo_hash_init(p);
372#endif
373
374 /*
375 * initialize session caching
376 */
377 if ((rv = ssl_scache_init(base_server, p)) != APR_SUCCESS) {
378 return rv;
379 }
380
381 pphrases = apr_array_make(ptemp, 2, sizeof(char *));
382
383 /*
384 * initialize servers
385 */
386 ap_log_error(APLOG_MARK, APLOG_INFO, 0, base_server, APLOGNO(01887)
387 "Init: Initializing (virtual) servers for SSL");
388
389 for (s = base_server; s; s = s->next) {
390 sc = mySrvConfig(s);
391 /*
392 * Either now skip this server when SSL is disabled for
393 * it or give out some information about what we're
394 * configuring.
395 */
396
397 /*
398 * Read the server certificate and key
399 */
400 if ((rv = ssl_init_ConfigureServer(s, p, ptemp, sc, pphrases))
401 != APR_SUCCESS) {
402 return rv;
403 }
404 }
405
406 if (pphrases->nelts > 0) {
407 memset(pphrases->elts, 0, pphrases->elt_size * pphrases->nelts);
408 pphrases->nelts = 0;
409 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(02560)
410 "Init: Wiped out the queried pass phrases from memory");
411 }
412
413 /*
414 * Configuration consistency checks
415 */
416 if ((rv = ssl_init_CheckServers(base_server, ptemp)) != APR_SUCCESS) {
417 return rv;
418 }
419
420 for (s = base_server; s; s = s->next) {
421 SSLDirConfigRec *sdc = ap_get_module_config(s->lookup_defaults,
422 &ssl_module);
423
424 sc = mySrvConfig(s);
425 if (sc->enabled == SSL_ENABLED_TRUE || sc->enabled == SSL_ENABLED_OPTIONAL) {
426 if ((rv = ssl_run_init_server(s, p, 0, sc->server->ssl_ctx)) != APR_SUCCESS) {
427 return rv;
428 }
429 }
430
431 if (sdc->proxy_enabled) {
432 rv = ssl_run_init_server(s, p, 1, sdc->proxy->ssl_ctx);
433 if (rv != APR_SUCCESS) {
434 return rv;
435 }
436 }
437 }
438
439 /*
440 * Announce mod_ssl and SSL library in HTTP Server field
441 * as ``mod_ssl/X.X.X OpenSSL/X.X.X''
442 */
443 ssl_add_version_components(p, base_server);
444
445 modssl_init_app_data2_idx(); /* for modssl_get_app_data2() at request time */
446
447 init_dh_params();
448#if !MODSSL_USE_OPENSSL_PRE_1_1_API
449 init_bio_methods();
450#endif
451
452 return OK;
453}
454
455/*
456 * Support for external a Crypto Device ("engine"), usually
457 * a hardware accellerator card for crypto operations.
458 */
459#if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
460apr_status_t ssl_init_Engine(server_rec *s, apr_pool_t *p)
461{
462 SSLModConfigRec *mc = myModConfig(s);
463 ENGINE *e;
464
465 if (mc->szCryptoDevice) {
466 if (!(e = ENGINE_by_id(mc->szCryptoDevice))) {
467 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01888)
468 "Init: Failed to load Crypto Device API `%s'",
469 mc->szCryptoDevice);
470 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
471 return ssl_die(s);
472 }
473
474#ifdef ENGINE_CTRL_CHIL_SET_FORKCHECK
475 if (strEQ(mc->szCryptoDevice, "chil")) {
476 ENGINE_ctrl(e, ENGINE_CTRL_CHIL_SET_FORKCHECK, 1, 0, 0);
477 }
478#endif
479
480 if (!ENGINE_set_default(e, ENGINE_METHOD_ALL)) {
481 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01889)
482 "Init: Failed to enable Crypto Device API `%s'",
483 mc->szCryptoDevice);
484 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
485 return ssl_die(s);
486 }
487 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01890)
488 "Init: loaded Crypto Device API `%s'",
489 mc->szCryptoDevice);
490
491 ENGINE_free(e);
492 }
493
494 return APR_SUCCESS;
495}
496#endif
497
498#ifdef HAVE_TLSEXT
499static apr_status_t ssl_init_ctx_tls_extensions(server_rec *s,
500 apr_pool_t *p,
501 apr_pool_t *ptemp,
502 modssl_ctx_t *mctx)
503{
504 apr_status_t rv;
505
506 /*
507 * Configure TLS extensions support
508 */
509 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01893)
510 "Configuring TLS extension handling");
511
512 /*
513 * The Server Name Indication (SNI) provided by the ClientHello can be
514 * used to select the right (name-based-)vhost and its SSL configuration
515 * before the handshake takes place.
516 */
517 if (!SSL_CTX_set_tlsext_servername_callback(mctx->ssl_ctx,
518 ssl_callback_ServerNameIndication) ||
519 !SSL_CTX_set_tlsext_servername_arg(mctx->ssl_ctx, mctx)) {
520 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01894)
521 "Unable to initialize TLS servername extension "
522 "callback (incompatible OpenSSL version?)");
523 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
524 return ssl_die(s);
525 }
526
527#if OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined(LIBRESSL_VERSION_NUMBER)
528 /*
529 * The ClientHello callback also allows to retrieve the SNI, but since it
530 * runs at the earliest possible connection stage we can even set the TLS
531 * protocol version(s) according to the selected (name-based-)vhost, which
532 * is not possible at the SNI callback stage (due to OpenSSL internals).
533 */
534 SSL_CTX_set_client_hello_cb(mctx->ssl_ctx, ssl_callback_ClientHello, NULL);
535#endif
536
537#ifdef HAVE_OCSP_STAPLING
538 /*
539 * OCSP Stapling support, status_request extension
540 */
541 if ((mctx->pkp == FALSE) && (mctx->stapling_enabled == TRUE)) {
542 if ((rv = modssl_init_stapling(s, p, ptemp, mctx)) != APR_SUCCESS) {
543 return rv;
544 }
545 }
546#endif
547
548#ifdef HAVE_SRP
549 /*
550 * TLS-SRP support
551 */
552 if (mctx->srp_vfile != NULL) {
553 int err;
554 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02308)
555 "Using SRP verifier file [%s]", mctx->srp_vfile);
556
557 if (!(mctx->srp_vbase = SRP_VBASE_new(mctx->srp_unknown_user_seed))) {
558 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02309)
559 "Unable to initialize SRP verifier structure "
560 "[%s seed]",
561 mctx->srp_unknown_user_seed ? "with" : "without");
562 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
563 return ssl_die(s);
564 }
565
566 err = SRP_VBASE_init(mctx->srp_vbase, mctx->srp_vfile);
567 if (err != SRP_NO_ERROR) {
568 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02310)
569 "Unable to load SRP verifier file [error %d]", err);
570 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
571 return ssl_die(s);
572 }
573
574 SSL_CTX_set_srp_username_callback(mctx->ssl_ctx,
575 ssl_callback_SRPServerParams);
576 SSL_CTX_set_srp_cb_arg(mctx->ssl_ctx, mctx);
577 }
578#endif
579 return APR_SUCCESS;
580}
581#endif
582
583static apr_status_t ssl_init_ctx_protocol(server_rec *s,
584 apr_pool_t *p,
585 apr_pool_t *ptemp,
586 modssl_ctx_t *mctx)
587{
588 SSL_CTX *ctx = NULL;
589 MODSSL_SSL_METHOD_CONST SSL_METHOD *method = NULL;
590 char *cp;
591 int protocol = mctx->protocol;
592 SSLSrvConfigRec *sc = mySrvConfig(s);
593#if OPENSSL_VERSION_NUMBER >= 0x10100000L
594 int prot;
595#endif
596
597 /*
598 * Create the new per-server SSL context
599 */
600 if (protocol == SSL_PROTOCOL_NONE) {
601 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02231)
602 "No SSL protocols available [hint: SSLProtocol]");
603 return ssl_die(s);
604 }
605
606 cp = apr_pstrcat(p,
607#ifndef OPENSSL_NO_SSL3
608 (protocol & SSL_PROTOCOL_SSLV3 ? "SSLv3, " : ""),
609#endif
610 (protocol & SSL_PROTOCOL_TLSV1 ? "TLSv1, " : ""),
611#ifdef HAVE_TLSV1_X
612 (protocol & SSL_PROTOCOL_TLSV1_1 ? "TLSv1.1, " : ""),
613 (protocol & SSL_PROTOCOL_TLSV1_2 ? "TLSv1.2, " : ""),
614#if SSL_HAVE_PROTOCOL_TLSV1_3
615 (protocol & SSL_PROTOCOL_TLSV1_3 ? "TLSv1.3, " : ""),
616#endif
617#endif
618 NULL);
619 cp[strlen(cp)-2] = NUL;
620
621 ap_log_error(APLOG_MARK, APLOG_TRACE3, 0, s,
622 "Creating new SSL context (protocols: %s)", cp);
623
624#if OPENSSL_VERSION_NUMBER < 0x10100000L
625#ifndef OPENSSL_NO_SSL3
626 if (protocol == SSL_PROTOCOL_SSLV3) {
627 method = mctx->pkp ?
628 SSLv3_client_method() : /* proxy */
629 SSLv3_server_method(); /* server */
630 }
631 else
632#endif
633 if (protocol == SSL_PROTOCOL_TLSV1) {
634 method = mctx->pkp ?
635 TLSv1_client_method() : /* proxy */
636 TLSv1_server_method(); /* server */
637 }
638#ifdef HAVE_TLSV1_X
639 else if (protocol == SSL_PROTOCOL_TLSV1_1) {
640 method = mctx->pkp ?
641 TLSv1_1_client_method() : /* proxy */
642 TLSv1_1_server_method(); /* server */
643 }
644 else if (protocol == SSL_PROTOCOL_TLSV1_2) {
645 method = mctx->pkp ?
646 TLSv1_2_client_method() : /* proxy */
647 TLSv1_2_server_method(); /* server */
648 }
649#if SSL_HAVE_PROTOCOL_TLSV1_3
650 else if (protocol == SSL_PROTOCOL_TLSV1_3) {
651 method = mctx->pkp ?
652 TLSv1_3_client_method() : /* proxy */
653 TLSv1_3_server_method(); /* server */
654 }
655#endif
656#endif
657 else { /* For multiple protocols, we need a flexible method */
658 method = mctx->pkp ?
659 SSLv23_client_method() : /* proxy */
660 SSLv23_server_method(); /* server */
661 }
662#else
663 method = mctx->pkp ?
664 TLS_client_method() : /* proxy */
665 TLS_server_method(); /* server */
666#endif
667 ctx = SSL_CTX_new(method);
668
669 mctx->ssl_ctx = ctx;
670
671 SSL_CTX_set_options(ctx, SSL_OP_ALL);
672
673#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
674 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20800000L)
675 /* always disable SSLv2, as per RFC 6176 */
676 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2);
677
678#ifndef OPENSSL_NO_SSL3
679 if (!(protocol & SSL_PROTOCOL_SSLV3)) {
680 SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv3);
681 }
682#endif
683
684 if (!(protocol & SSL_PROTOCOL_TLSV1)) {
685 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1);
686 }
687
688#ifdef HAVE_TLSV1_X
689 if (!(protocol & SSL_PROTOCOL_TLSV1_1)) {
690 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_1);
691 }
692
693 if (!(protocol & SSL_PROTOCOL_TLSV1_2)) {
694 SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2);
695 }
696#if SSL_HAVE_PROTOCOL_TLSV1_3
697 ssl_set_ctx_protocol_option(s, ctx, SSL_OP_NO_TLSv1_3,
698 protocol & SSL_PROTOCOL_TLSV1_3, "TLSv1.3");
699#endif
700#endif
701
702#else /* #if OPENSSL_VERSION_NUMBER < 0x10100000L */
703 /* We first determine the maximum protocol version we should provide */
704#if SSL_HAVE_PROTOCOL_TLSV1_3
705 if (SSL_HAVE_PROTOCOL_TLSV1_3 && (protocol & SSL_PROTOCOL_TLSV1_3)) {
706 prot = TLS1_3_VERSION;
707 } else
708#endif
709 if (protocol & SSL_PROTOCOL_TLSV1_2) {
710 prot = TLS1_2_VERSION;
711 } else if (protocol & SSL_PROTOCOL_TLSV1_1) {
712 prot = TLS1_1_VERSION;
713 } else if (protocol & SSL_PROTOCOL_TLSV1) {
714 prot = TLS1_VERSION;
715#ifndef OPENSSL_NO_SSL3
716 } else if (protocol & SSL_PROTOCOL_SSLV3) {
717 prot = SSL3_VERSION;
718#endif
719 } else {
720 SSL_CTX_free(ctx);
721 mctx->ssl_ctx = NULL;
722 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(03378)
723 "No SSL protocols available [hint: SSLProtocol]");
724 return ssl_die(s);
725 }
726 SSL_CTX_set_max_proto_version(ctx, prot);
727
728 /* Next we scan for the minimal protocol version we should provide,
729 * but we do not allow holes between max and min */
730#if SSL_HAVE_PROTOCOL_TLSV1_3
731 if (prot == TLS1_3_VERSION && protocol & SSL_PROTOCOL_TLSV1_2) {
732 prot = TLS1_2_VERSION;
733 }
734#endif
735 if (prot == TLS1_2_VERSION && protocol & SSL_PROTOCOL_TLSV1_1) {
736 prot = TLS1_1_VERSION;
737 }
738 if (prot == TLS1_1_VERSION && protocol & SSL_PROTOCOL_TLSV1) {
739 prot = TLS1_VERSION;
740 }
741#ifndef OPENSSL_NO_SSL3
742 if (prot == TLS1_VERSION && protocol & SSL_PROTOCOL_SSLV3) {
743 prot = SSL3_VERSION;
744 }
745#endif
746 SSL_CTX_set_min_proto_version(ctx, prot);
747#endif /* if OPENSSL_VERSION_NUMBER < 0x10100000L */
748
749#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
750 if (sc->cipher_server_pref == TRUE) {
751 SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
752 }
753#endif
754
755
756#ifndef OPENSSL_NO_COMP
757 if (sc->compression != TRUE) {
758#ifdef SSL_OP_NO_COMPRESSION
759 /* OpenSSL >= 1.0 only */
760 SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);
761#else
762 sk_SSL_COMP_zero(SSL_COMP_get_compression_methods());
763#endif
764 }
765#endif
766
767#ifdef SSL_OP_NO_TICKET
768 /*
769 * Configure using RFC 5077 TLS session tickets
770 * for session resumption.
771 */
772 if (sc->session_tickets == FALSE) {
773 SSL_CTX_set_options(ctx, SSL_OP_NO_TICKET);
774 }
775#endif
776
777#ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
778 if (sc->insecure_reneg == TRUE) {
779 SSL_CTX_set_options(ctx, SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION);
780 }
781#endif
782
783 SSL_CTX_set_app_data(ctx, s);
784
785 /*
786 * Configure additional context ingredients
787 */
788 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE);
789#ifdef HAVE_ECC
790 SSL_CTX_set_options(ctx, SSL_OP_SINGLE_ECDH_USE);
791#endif
792
793#ifdef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
794 /*
795 * Disallow a session from being resumed during a renegotiation,
796 * so that an acceptable cipher suite can be negotiated.
797 */
798 SSL_CTX_set_options(ctx, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION);
799#endif
800
801#ifdef SSL_MODE_RELEASE_BUFFERS
802 /* If httpd is configured to reduce mem usage, ask openssl to do so, too */
803 if (ap_max_mem_free != APR_ALLOCATOR_MAX_FREE_UNLIMITED)
804 SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
805#endif
806
807#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
808 /* For OpenSSL >=1.1.1, disable auto-retry mode so it's possible
809 * to consume handshake records without blocking for app-data.
810 * https://github.com/openssl/openssl/issues/7178 */
811 SSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY);
812#endif
813
814 return APR_SUCCESS;
815}
816
817static void ssl_init_ctx_session_cache(server_rec *s,
818 apr_pool_t *p,
819 apr_pool_t *ptemp,
820 modssl_ctx_t *mctx)
821{
822 SSL_CTX *ctx = mctx->ssl_ctx;
823 SSLModConfigRec *mc = myModConfig(s);
824
825 SSL_CTX_set_session_cache_mode(ctx, mc->sesscache_mode);
826
827 if (mc->sesscache) {
828 SSL_CTX_sess_set_new_cb(ctx, ssl_callback_NewSessionCacheEntry);
829 SSL_CTX_sess_set_get_cb(ctx, ssl_callback_GetSessionCacheEntry);
830 SSL_CTX_sess_set_remove_cb(ctx, ssl_callback_DelSessionCacheEntry);
831 }
832}
833
834static void ssl_init_ctx_callbacks(server_rec *s,
835 apr_pool_t *p,
836 apr_pool_t *ptemp,
837 modssl_ctx_t *mctx)
838{
839 SSL_CTX *ctx = mctx->ssl_ctx;
840
841 SSL_CTX_set_tmp_dh_callback(ctx, ssl_callback_TmpDH);
842
843 SSL_CTX_set_info_callback(ctx, ssl_callback_Info);
844
845#ifdef HAVE_TLS_ALPN
846 SSL_CTX_set_alpn_select_cb(ctx, ssl_callback_alpn_select, NULL);
847#endif
848}
849
850static apr_status_t ssl_init_ctx_verify(server_rec *s,
851 apr_pool_t *p,
852 apr_pool_t *ptemp,
853 modssl_ctx_t *mctx)
854{
855 SSL_CTX *ctx = mctx->ssl_ctx;
856
857 int verify = SSL_VERIFY_NONE;
858 STACK_OF(X509_NAME) *ca_list;
859
860 if (mctx->auth.verify_mode == SSL_CVERIFY_UNSET) {
861 mctx->auth.verify_mode = SSL_CVERIFY_NONE;
862 }
863
864 if (mctx->auth.verify_depth == UNSET) {
865 mctx->auth.verify_depth = 1;
866 }
867
868 /*
869 * Configure callbacks for SSL context
870 */
871 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
872 verify |= SSL_VERIFY_PEER_STRICT;
873 }
874
875 if ((mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL) ||
876 (mctx->auth.verify_mode == SSL_CVERIFY_OPTIONAL_NO_CA))
877 {
878 verify |= SSL_VERIFY_PEER;
879 }
880
881 SSL_CTX_set_verify(ctx, verify, ssl_callback_SSLVerify);
882
883 /*
884 * Configure Client Authentication details
885 */
886 if (mctx->auth.ca_cert_file || mctx->auth.ca_cert_path) {
887 ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, s,
888 "Configuring client authentication");
889
890 if (!SSL_CTX_load_verify_locations(ctx,
891 mctx->auth.ca_cert_file,
892 mctx->auth.ca_cert_path))
893 {
894 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01895)
895 "Unable to configure verify locations "
896 "for client authentication");
897 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
898 return ssl_die(s);
899 }
900
901 if (mctx->pks && (mctx->pks->ca_name_file || mctx->pks->ca_name_path)) {
902 ca_list = ssl_init_FindCAList(s, ptemp,
903 mctx->pks->ca_name_file,
904 mctx->pks->ca_name_path);
905 } else
906 ca_list = ssl_init_FindCAList(s, ptemp,
907 mctx->auth.ca_cert_file,
908 mctx->auth.ca_cert_path);
909 if (sk_X509_NAME_num(ca_list) <= 0) {
910 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01896)
911 "Unable to determine list of acceptable "
912 "CA certificates for client authentication");
913 return ssl_die(s);
914 }
915
916 SSL_CTX_set_client_CA_list(ctx, ca_list);
917 }
918
919 /*
920 * Give a warning when no CAs were configured but client authentication
921 * should take place. This cannot work.
922 */
923 if (mctx->auth.verify_mode == SSL_CVERIFY_REQUIRE) {
924 ca_list = SSL_CTX_get_client_CA_list(ctx);
925
926 if (sk_X509_NAME_num(ca_list) == 0) {
927 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01897)
928 "Init: Oops, you want to request client "
929 "authentication, but no CAs are known for "
930 "verification!? [Hint: SSLCACertificate*]");
931 }
932 }
933
934 return APR_SUCCESS;
935}
936
937static apr_status_t ssl_init_ctx_cipher_suite(server_rec *s,
938 apr_pool_t *p,
939 apr_pool_t *ptemp,
940 modssl_ctx_t *mctx)
941{
942 SSL_CTX *ctx = mctx->ssl_ctx;
943 const char *suite;
944
945 /*
946 * Configure SSL Cipher Suite. Always disable NULL and export ciphers,
947 * see also ssl_engine_config.c:ssl_cmd_SSLCipherSuite().
948 * OpenSSL's SSL_DEFAULT_CIPHER_LIST includes !aNULL:!eNULL from 0.9.8f,
949 * and !EXP from 0.9.8zf/1.0.1m/1.0.2a, so append them while we support
950 * earlier versions.
951 */
952 suite = mctx->auth.cipher_suite ? mctx->auth.cipher_suite :
953 apr_pstrcat(ptemp, SSL_DEFAULT_CIPHER_LIST, ":!aNULL:!eNULL:!EXP",
954 NULL);
955
956 ap_log_error(APLOG_MARK, APLOG_TRACE1, 0, s,
957 "Configuring permitted SSL ciphers [%s]",
958 suite);
959
960 if (!SSL_CTX_set_cipher_list(ctx, suite)) {
961 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01898)
962 "Unable to configure permitted SSL ciphers");
963 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
964 return ssl_die(s);
965 }
966#if SSL_HAVE_PROTOCOL_TLSV1_3
967 if (mctx->auth.tls13_ciphers
968 && !SSL_CTX_set_ciphersuites(ctx, mctx->auth.tls13_ciphers)) {
969 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10127)
970 "Unable to configure permitted TLSv1.3 ciphers");
971 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
972 return ssl_die(s);
973 }
974#endif
975 return APR_SUCCESS;
976}
977
978static apr_status_t ssl_init_ctx_crl(server_rec *s,
979 apr_pool_t *p,
980 apr_pool_t *ptemp,
981 modssl_ctx_t *mctx)
982{
983 X509_STORE *store = SSL_CTX_get_cert_store(mctx->ssl_ctx);
984 unsigned long crlflags = 0;
985 char *cfgp = mctx->pkp ? "SSLProxy" : "SSL";
986 int crl_check_mode;
987
988 if (mctx->ocsp_mask == UNSET) {
989 mctx->ocsp_mask = SSL_OCSPCHECK_NONE;
990 }
991
992 if (mctx->crl_check_mask == UNSET) {
993 mctx->crl_check_mask = SSL_CRLCHECK_NONE;
994 }
995 crl_check_mode = mctx->crl_check_mask & ~SSL_CRLCHECK_FLAGS;
996
997 /*
998 * Configure Certificate Revocation List (CRL) Details
999 */
1000
1001 if (!(mctx->crl_file || mctx->crl_path)) {
1002 if (crl_check_mode == SSL_CRLCHECK_LEAF ||
1003 crl_check_mode == SSL_CRLCHECK_CHAIN) {
1004 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01899)
1005 "Host %s: CRL checking has been enabled, but "
1006 "neither %sCARevocationFile nor %sCARevocationPath "
1007 "is configured", mctx->sc->vhost_id, cfgp, cfgp);
1008 return ssl_die(s);
1009 }
1010 return APR_SUCCESS;
1011 }
1012
1013 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01900)
1014 "Configuring certificate revocation facility");
1015
1016 if (!store || !X509_STORE_load_locations(store, mctx->crl_file,
1017 mctx->crl_path)) {
1018 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01901)
1019 "Host %s: unable to configure X.509 CRL storage "
1020 "for certificate revocation", mctx->sc->vhost_id);
1021 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1022 return ssl_die(s);
1023 }
1024
1025 switch (crl_check_mode) {
1026 case SSL_CRLCHECK_LEAF:
1027 crlflags = X509_V_FLAG_CRL_CHECK;
1028 break;
1029 case SSL_CRLCHECK_CHAIN:
1030 crlflags = X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL;
1031 break;
1032 default:
1033 crlflags = 0;
1034 }
1035
1036 if (crlflags) {
1037 X509_STORE_set_flags(store, crlflags);
1038 } else {
1039 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01902)
1040 "Host %s: X.509 CRL storage locations configured, "
1041 "but CRL checking (%sCARevocationCheck) is not "
1042 "enabled", mctx->sc->vhost_id, cfgp);
1043 }
1044
1045 return APR_SUCCESS;
1046}
1047
1048/*
1049 * Read a file that optionally contains the server certificate in PEM
1050 * format, possibly followed by a sequence of CA certificates that
1051 * should be sent to the peer in the SSL Certificate message.
1052 */
1053static int use_certificate_chain(
1054 SSL_CTX *ctx, char *file, int skipfirst, pem_password_cb *cb)
1055{
1056 BIO *bio;
1057 X509 *x509;
1058 unsigned long err;
1059 int n;
1060
1061 if ((bio = BIO_new(BIO_s_file())) == NULL)
1062 return -1;
1063 if (BIO_read_filename(bio, file) <= 0) {
1064 BIO_free(bio);
1065 return -1;
1066 }
1067 /* optionally skip a leading server certificate */
1068 if (skipfirst) {
1069 if ((x509 = PEM_read_bio_X509(bio, NULL, cb, NULL)) == NULL) {
1070 BIO_free(bio);
1071 return -1;
1072 }
1073 X509_free(x509);
1074 }
1075 /* free a perhaps already configured extra chain */
1076#ifdef OPENSSL_NO_SSL_INTERN
1077 SSL_CTX_clear_extra_chain_certs(ctx);
1078#else
1079 if (ctx->extra_certs != NULL) {
1080 sk_X509_pop_free((STACK_OF(X509) *)ctx->extra_certs, X509_free);
1081 ctx->extra_certs = NULL;
1082 }
1083#endif
1084
1085 /* create new extra chain by loading the certs */
1086 n = 0;
1087 ERR_clear_error();
1088 while ((x509 = PEM_read_bio_X509(bio, NULL, cb, NULL)) != NULL) {
1089 if (!SSL_CTX_add_extra_chain_cert(ctx, x509)) {
1090 X509_free(x509);
1091 BIO_free(bio);
1092 return -1;
1093 }
1094 n++;
1095 }
1096 /* Make sure that only the error is just an EOF */
1097 if ((err = ERR_peek_error()) > 0) {
1098 if (!( ERR_GET_LIB(err) == ERR_LIB_PEM
1099 && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) {
1100 BIO_free(bio);
1101 return -1;
1102 }
1103 while (ERR_get_error() > 0) ;
1104 }
1105 BIO_free(bio);
1106 return n;
1107}
1108
1109static apr_status_t ssl_init_ctx_cert_chain(server_rec *s,
1110 apr_pool_t *p,
1111 apr_pool_t *ptemp,
1112 modssl_ctx_t *mctx)
1113{
1114 BOOL skip_first = FALSE;
1115 int i, n;
1116 const char *chain = mctx->cert_chain;
1117
1118 /*
1119 * Optionally configure extra server certificate chain certificates.
1120 * This is usually done by OpenSSL automatically when one of the
1121 * server cert issuers are found under SSLCACertificatePath or in
1122 * SSLCACertificateFile. But because these are intended for client
1123 * authentication it can conflict. For instance when you use a
1124 * Global ID server certificate you've to send out the intermediate
1125 * CA certificate, too. When you would just configure this with
1126 * SSLCACertificateFile and also use client authentication mod_ssl
1127 * would accept all clients also issued by this CA. Obviously this
1128 * isn't what we want in this situation. So this feature here exists
1129 * to allow one to explicitly configure CA certificates which are
1130 * used only for the server certificate chain.
1131 */
1132 if (!chain) {
1133 return APR_SUCCESS;
1134 }
1135
1136 for (i = 0; (i < mctx->pks->cert_files->nelts) &&
1137 APR_ARRAY_IDX(mctx->pks->cert_files, i, const char *); i++) {
1138 if (strEQ(APR_ARRAY_IDX(mctx->pks->cert_files, i, const char *), chain)) {
1139 skip_first = TRUE;
1140 break;
1141 }
1142 }
1143
1144 n = use_certificate_chain(mctx->ssl_ctx, (char *)chain, skip_first, NULL);
1145 if (n < 0) {
1146 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01903)
1147 "Failed to configure CA certificate chain!");
1148 return ssl_die(s);
1149 }
1150
1151 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(01904)
1152 "Configuring server certificate chain "
1153 "(%d CA certificate%s)",
1154 n, n == 1 ? "" : "s");
1155
1156 return APR_SUCCESS;
1157}
1158
1159static apr_status_t ssl_init_ctx(server_rec *s,
1160 apr_pool_t *p,
1161 apr_pool_t *ptemp,
1162 modssl_ctx_t *mctx)
1163{
1164 apr_status_t rv;
1165
1166 if ((rv = ssl_init_ctx_protocol(s, p, ptemp, mctx)) != APR_SUCCESS) {
1167 return rv;
1168 }
1169
1170 ssl_init_ctx_session_cache(s, p, ptemp, mctx);
1171
1172 ssl_init_ctx_callbacks(s, p, ptemp, mctx);
1173
1174 if ((rv = ssl_init_ctx_verify(s, p, ptemp, mctx)) != APR_SUCCESS) {
1175 return rv;
1176 }
1177
1178 if ((rv = ssl_init_ctx_cipher_suite(s, p, ptemp, mctx)) != APR_SUCCESS) {
1179 return rv;
1180 }
1181
1182 if ((rv = ssl_init_ctx_crl(s, p, ptemp, mctx)) != APR_SUCCESS) {
1183 return rv;
1184 }
1185
1186 if (mctx->pks) {
1187 /* XXX: proxy support? */
1188 if ((rv = ssl_init_ctx_cert_chain(s, p, ptemp, mctx)) != APR_SUCCESS) {
1189 return rv;
1190 }
1191#ifdef HAVE_TLSEXT
1192 if ((rv = ssl_init_ctx_tls_extensions(s, p, ptemp, mctx)) !=
1193 APR_SUCCESS) {
1194 return rv;
1195 }
1196#endif
1197 }
1198
1199 return APR_SUCCESS;
1200}
1201
1202static void ssl_check_public_cert(server_rec *s,
1203 apr_pool_t *ptemp,
1204 X509 *cert,
1205 const char *key_id)
1206{
1207 int is_ca, pathlen;
1208
1209 if (!cert) {
1210 return;
1211 }
1212
1213 /*
1214 * Some information about the certificate(s)
1215 */
1216
1217 if (modssl_X509_getBC(cert, &is_ca, &pathlen)) {
1218 if (is_ca) {
1219 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01906)
1220 "%s server certificate is a CA certificate "
1221 "(BasicConstraints: CA == TRUE !?)", key_id);
1222 }
1223
1224 if (pathlen > 0) {
1225 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01907)
1226 "%s server certificate is not a leaf certificate "
1227 "(BasicConstraints: pathlen == %d > 0 !?)",
1228 key_id, pathlen);
1229 }
1230 }
1231
1232 if (modssl_X509_match_name(ptemp, cert, (const char *)s->server_hostname,
1233 TRUE, s) == FALSE) {
1234 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(01909)
1235 "%s server certificate does NOT include an ID "
1236 "which matches the server name", key_id);
1237 }
1238}
1239
1240/* prevent OpenSSL from showing its "Enter PEM pass phrase:" prompt */
1241static int ssl_no_passwd_prompt_cb(char *buf, int size, int rwflag,
1242 void *userdata) {
1243 return 0;
1244}
1245
1246static apr_status_t ssl_init_server_certs(server_rec *s,
1247 apr_pool_t *p,
1248 apr_pool_t *ptemp,
1249 modssl_ctx_t *mctx,
1250 apr_array_header_t *pphrases)
1251{
1252 SSLModConfigRec *mc = myModConfig(s);
1253 const char *vhost_id = mctx->sc->vhost_id, *key_id, *certfile, *keyfile;
1254 int i;
1255 X509 *cert;
1256 DH *dhparams;
1257#ifdef HAVE_ECC
1258 EC_GROUP *ecparams = NULL;
1259 int nid;
1260 EC_KEY *eckey = NULL;
1261#endif
1262#ifndef HAVE_SSL_CONF_CMD
1263 SSL *ssl;
1264#endif
1265
1266 /* no OpenSSL default prompts for any of the SSL_CTX_use_* calls, please */
1267 SSL_CTX_set_default_passwd_cb(mctx->ssl_ctx, ssl_no_passwd_prompt_cb);
1268
1269 /* Iterate over the SSLCertificateFile array */
1270 for (i = 0; (i < mctx->pks->cert_files->nelts) &&
1271 (certfile = APR_ARRAY_IDX(mctx->pks->cert_files, i,
1272 const char *));
1273 i++) {
1274 EVP_PKEY *pkey;
1275 const char *engine_certfile = NULL;
1276
1277 key_id = apr_psprintf(ptemp, "%s:%d", vhost_id, i);
1278
1279 ERR_clear_error();
1280
1281 /* first the certificate (public key) */
1282 if (modssl_is_engine_id(certfile)) {
1283 engine_certfile = certfile;
1284 }
1285 else if (mctx->cert_chain) {
1286 if ((SSL_CTX_use_certificate_file(mctx->ssl_ctx, certfile,
1287 SSL_FILETYPE_PEM) < 1)) {
1288 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02561)
1289 "Failed to configure certificate %s, check %s",
1290 key_id, certfile);
1291 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1292 return APR_EGENERAL;
1293 }
1294 } else {
1295 if ((SSL_CTX_use_certificate_chain_file(mctx->ssl_ctx,
1296 certfile) < 1)) {
1297 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02562)
1298 "Failed to configure certificate %s (with chain),"
1299 " check %s", key_id, certfile);
1300 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1301 return APR_EGENERAL;
1302 }
1303 }
1304
1305 /* and second, the private key */
1306 if (i < mctx->pks->key_files->nelts) {
1307 keyfile = APR_ARRAY_IDX(mctx->pks->key_files, i, const char *);
1308 } else {
1309 keyfile = certfile;
1310 }
1311
1312 ERR_clear_error();
1313
1314 if (modssl_is_engine_id(keyfile)) {
1315 apr_status_t rv;
1316
1317 cert = NULL;
1318
1319 if ((rv = modssl_load_engine_keypair(s, ptemp, vhost_id,
1320 engine_certfile, keyfile,
1321 &cert, &pkey))) {
1322 return rv;
1323 }
1324
1325 if (cert) {
1326 if (SSL_CTX_use_certificate(mctx->ssl_ctx, cert) < 1) {
1327 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10137)
1328 "Failed to configure engine certificate %s, check %s",
1329 key_id, certfile);
1330 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1331 return APR_EGENERAL;
1332 }
1333
1334 /* SSL_CTX now owns the cert. */
1335 X509_free(cert);
1336 }
1337
1338 if (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) < 1) {
1339 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(10130)
1340 "Failed to configure private key %s from engine",
1341 keyfile);
1342 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1343 return APR_EGENERAL;
1344 }
1345
1346 /* SSL_CTX now owns the key */
1347 EVP_PKEY_free(pkey);
1348 }
1349 else if ((SSL_CTX_use_PrivateKey_file(mctx->ssl_ctx, keyfile,
1350 SSL_FILETYPE_PEM) < 1)
1351 && (ERR_GET_FUNC(ERR_peek_last_error())
1352 != X509_F_X509_CHECK_PRIVATE_KEY)) {
1353 ssl_asn1_t *asn1;
1354 EVP_PKEY *pkey;
1355 const unsigned char *ptr;
1356
1357 ERR_clear_error();
1358
1359 /* perhaps it's an encrypted private key, so try again */
1360 ssl_load_encrypted_pkey(s, ptemp, i, keyfile, &pphrases);
1361
1362 if (!(asn1 = ssl_asn1_table_get(mc->tPrivateKey, key_id)) ||
1363 !(ptr = asn1->cpData) ||
1364 !(pkey = d2i_AutoPrivateKey(NULL, &ptr, asn1->nData)) ||
1365 (SSL_CTX_use_PrivateKey(mctx->ssl_ctx, pkey) < 1)) {
1366 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02564)
1367 "Failed to configure encrypted (?) private key %s,"
1368 " check %s", key_id, keyfile);
1369 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1370 return APR_EGENERAL;
1371 }
1372 }
1373
1374 if (SSL_CTX_check_private_key(mctx->ssl_ctx) < 1) {
1375 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02565)
1376 "Certificate and private key %s from %s and %s "
1377 "do not match", key_id, certfile, keyfile);
1378 return APR_EGENERAL;
1379 }
1380
1381#ifdef HAVE_SSL_CONF_CMD
1382 /*
1383 * workaround for those OpenSSL versions where SSL_CTX_get0_certificate
1384 * is not yet available: create an SSL struct which we dispose of
1385 * as soon as we no longer need access to the cert. (Strictly speaking,
1386 * SSL_CTX_get0_certificate does not depend on the SSL_CONF stuff,
1387 * but there's no reliable way to check for its existence, so we
1388 * assume that if SSL_CONF is available, it's OpenSSL 1.0.2 or later,
1389 * and SSL_CTX_get0_certificate is implemented.)
1390 */
1391 if (!(cert = SSL_CTX_get0_certificate(mctx->ssl_ctx))) {
1392#else
1393 ssl = SSL_new(mctx->ssl_ctx);
1394 if (ssl) {
1395 /* Workaround bug in SSL_get_certificate in OpenSSL 0.9.8y */
1396 SSL_set_connect_state(ssl);
1397 cert = SSL_get_certificate(ssl);
1398 }
1399 if (!ssl || !cert) {
1400#endif
1401 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02566)
1402 "Unable to retrieve certificate %s", key_id);
1403#ifndef HAVE_SSL_CONF_CMD
1404 if (ssl)
1405 SSL_free(ssl);
1406#endif
1407 return APR_EGENERAL;
1408 }
1409
1410 /* warn about potential cert issues */
1411 ssl_check_public_cert(s, ptemp, cert, key_id);
1412
1413#if defined(HAVE_OCSP_STAPLING) && !defined(SSL_CTRL_SET_CURRENT_CERT)
1414 /*
1415 * OpenSSL up to 1.0.1: configure stapling as we go. In 1.0.2
1416 * and later, there's SSL_CTX_set_current_cert, which allows
1417 * iterating over all certs in an SSL_CTX (including those possibly
1418 * loaded via SSLOpenSSLConfCmd Certificate), so for 1.0.2 and
1419 * later, we defer to the code in ssl_init_server_ctx.
1420 */
1421 if ((mctx->stapling_enabled == TRUE) &&
1422 !ssl_stapling_init_cert(s, p, ptemp, mctx, cert)) {
1423 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02567)
1424 "Unable to configure certificate %s for stapling",
1425 key_id);
1426 }
1427#endif
1428
1429#ifndef HAVE_SSL_CONF_CMD
1430 SSL_free(ssl);
1431#endif
1432
1433 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(02568)
1434 "Certificate and private key %s configured from %s and %s",
1435 key_id, certfile, keyfile);
1436 }
1437
1438 /*
1439 * Try to read DH parameters from the (first) SSLCertificateFile
1440 */
1441 certfile = APR_ARRAY_IDX(mctx->pks->cert_files, 0, const char *);
1442 if (certfile && !modssl_is_engine_id(certfile)
1443 && (dhparams = ssl_dh_GetParamFromFile(certfile))) {
1444 SSL_CTX_set_tmp_dh(mctx->ssl_ctx, dhparams);
1445 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02540)
1446 "Custom DH parameters (%d bits) for %s loaded from %s",
1447 DH_bits(dhparams), vhost_id, certfile);
1448 DH_free(dhparams);
1449 }
1450
1451#ifdef HAVE_ECC
1452 /*
1453 * Similarly, try to read the ECDH curve name from SSLCertificateFile...
1454 */
1455 if (certfile && !modssl_is_engine_id(certfile)
1456 && (ecparams = ssl_ec_GetParamFromFile(certfile))
1457 && (nid = EC_GROUP_get_curve_name(ecparams))
1458 && (eckey = EC_KEY_new_by_curve_name(nid))) {
1459 SSL_CTX_set_tmp_ecdh(mctx->ssl_ctx, eckey);
1460 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02541)
1461 "ECDH curve %s for %s specified in %s",
1462 OBJ_nid2sn(nid), vhost_id, certfile);
1463 }
1464 /*
1465 * ...otherwise, enable auto curve selection (OpenSSL 1.0.2)
1466 * or configure NIST P-256 (required to enable ECDHE for earlier versions)
1467 * ECDH is always enabled in 1.1.0 unless excluded from SSLCipherList
1468 */
1469#if MODSSL_USE_OPENSSL_PRE_1_1_API
1470 else {
1471#if defined(SSL_CTX_set_ecdh_auto)
1472 SSL_CTX_set_ecdh_auto(mctx->ssl_ctx, 1);
1473#else
1474 eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
1475 SSL_CTX_set_tmp_ecdh(mctx->ssl_ctx, eckey);
1476#endif
1477 }
1478#endif
1479 /* OpenSSL assures us that _free() is NULL-safe */
1480 EC_KEY_free(eckey);
1481 EC_GROUP_free(ecparams);
1482#endif
1483
1484 return APR_SUCCESS;
1485}
1486
1487#ifdef HAVE_TLS_SESSION_TICKETS
1488static apr_status_t ssl_init_ticket_key(server_rec *s,
1489 apr_pool_t *p,
1490 apr_pool_t *ptemp,
1491 modssl_ctx_t *mctx)
1492{
1493 apr_status_t rv;
1494 apr_file_t *fp;
1495 apr_size_t len;
1496 char buf[TLSEXT_TICKET_KEY_LEN];
1497 char *path;
1498 modssl_ticket_key_t *ticket_key = mctx->ticket_key;
1499
1500 if (!ticket_key->file_path) {
1501 return APR_SUCCESS;
1502 }
1503
1504 path = ap_server_root_relative(p, ticket_key->file_path);
1505
1506 rv = apr_file_open(&fp, path, APR_READ|APR_BINARY,
1507 APR_OS_DEFAULT, ptemp);
1508
1509 if (rv != APR_SUCCESS) {
1510 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02286)
1511 "Failed to open ticket key file %s: (%d) %pm",
1512 path, rv, &rv);
1513 return ssl_die(s);
1514 }
1515
1516 rv = apr_file_read_full(fp, &buf[0], TLSEXT_TICKET_KEY_LEN, &len);
1517
1518 if (rv != APR_SUCCESS) {
1519 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02287)
1520 "Failed to read %d bytes from %s: (%d) %pm",
1521 TLSEXT_TICKET_KEY_LEN, path, rv, &rv);
1522 return ssl_die(s);
1523 }
1524
1525 memcpy(ticket_key->key_name, buf, 16);
1526 memcpy(ticket_key->hmac_secret, buf + 16, 16);
1527 memcpy(ticket_key->aes_key, buf + 32, 16);
1528
1529 if (!SSL_CTX_set_tlsext_ticket_key_cb(mctx->ssl_ctx,
1530 ssl_callback_SessionTicket)) {
1531 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(01913)
1532 "Unable to initialize TLS session ticket key callback "
1533 "(incompatible OpenSSL version?)");
1534 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1535 return ssl_die(s);
1536 }
1537
1538 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(02288)
1539 "TLS session ticket key for %s successfully loaded from %s",
1540 (mySrvConfig(s))->vhost_id, path);
1541
1542 return APR_SUCCESS;
1543}
1544#endif
1545
1546static BOOL load_x509_info(apr_pool_t *ptemp,
1547 STACK_OF(X509_INFO) *sk,
1548 const char *filename)
1549{
1550 BIO *in;
1551
1552 if (!(in = BIO_new(BIO_s_file()))) {
1553 return FALSE;
1554 }
1555
1556 if (BIO_read_filename(in, filename) <= 0) {
1557 BIO_free(in);
1558 return FALSE;
1559 }
1560
1561 ERR_clear_error();
1562
1563 PEM_X509_INFO_read_bio(in, sk, NULL, NULL);
1564
1565 BIO_free(in);
1566
1567 return TRUE;
1568}
1569
1570static apr_status_t ssl_init_proxy_certs(server_rec *s,
1571 apr_pool_t *p,
1572 apr_pool_t *ptemp,
1573 modssl_ctx_t *mctx)
1574{
1575 int n, ncerts = 0;
1576 STACK_OF(X509_INFO) *sk;
1577 modssl_pk_proxy_t *pkp = mctx->pkp;
1578 STACK_OF(X509) *chain;
1579 X509_STORE_CTX *sctx;
1580 X509_STORE *store = SSL_CTX_get_cert_store(mctx->ssl_ctx);
1581
1582#if OPENSSL_VERSION_NUMBER >= 0x1010100fL
1583 /* For OpenSSL >=1.1.1, turn on client cert support which is
1584 * otherwise turned off by default (by design).
1585 * https://github.com/openssl/openssl/issues/6933 */
1586 SSL_CTX_set_post_handshake_auth(mctx->ssl_ctx, 1);
1587#endif
1588
1589 SSL_CTX_set_client_cert_cb(mctx->ssl_ctx,
1590 ssl_callback_proxy_cert);
1591
1592 if (!(pkp->cert_file || pkp->cert_path)) {
1593 return APR_SUCCESS;
1594 }
1595
1596 sk = sk_X509_INFO_new_null();
1597
1598 if (pkp->cert_file) {
1599 load_x509_info(ptemp, sk, pkp->cert_file);
1600 }
1601
1602 if (pkp->cert_path) {
1603 apr_dir_t *dir;
1604 apr_finfo_t dirent;
1605 apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
1606
1607 if (apr_dir_open(&dir, pkp->cert_path, ptemp) == APR_SUCCESS) {
1608 while ((apr_dir_read(&dirent, finfo_flags, dir)) == APR_SUCCESS) {
1609 const char *fullname;
1610
1611 if (dirent.filetype == APR_DIR) {
1612 continue; /* don't try to load directories */
1613 }
1614
1615 fullname = apr_pstrcat(ptemp,
1616 pkp->cert_path, "/", dirent.name,
1617 NULL);
1618 load_x509_info(ptemp, sk, fullname);
1619 }
1620
1621 apr_dir_close(dir);
1622 }
1623 }
1624
1625 if ((ncerts = sk_X509_INFO_num(sk)) <= 0) {
1626 sk_X509_INFO_free(sk);
1627 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(02206)
1628 "no client certs found for SSL proxy");
1629 return APR_SUCCESS;
1630 }
1631
1632 /* Check that all client certs have got certificates and private
1633 * keys. */
1634 for (n = 0; n < ncerts; n++) {
1635 X509_INFO *inf = sk_X509_INFO_value(sk, n);
1636
1637 if (!inf->x509 || !inf->x_pkey || !inf->x_pkey->dec_pkey ||
1638 inf->enc_data) {
1639 sk_X509_INFO_free(sk);
1640 ap_log_error(APLOG_MARK, APLOG_STARTUP, 0, s, APLOGNO(02252)
1641 "incomplete client cert configured for SSL proxy "
1642 "(missing or encrypted private key?)");
1643 return ssl_die(s);
1644 }
1645
1646 if (X509_check_private_key(inf->x509, inf->x_pkey->dec_pkey) != 1) {
1647 ssl_log_xerror(SSLLOG_MARK, APLOG_STARTUP, 0, ptemp, s, inf->x509,
1648 APLOGNO(02326) "proxy client certificate and "
1649 "private key do not match");
1650 ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, s);
1651 return ssl_die(s);
1652 }
1653 }
1654
1655 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02207)
1656 "loaded %d client certs for SSL proxy",
1657 ncerts);
1658 pkp->certs = sk;
1659
1660
1661 if (!pkp->ca_cert_file || !store) {
1662 return APR_SUCCESS;
1663 }
1664
1665 /* If SSLProxyMachineCertificateChainFile is configured, load all
1666 * the CA certs and have OpenSSL attempt to construct a full chain
1667 * from each configured end-entity cert up to a root. This will
1668 * allow selection of the correct cert given a list of root CA
1669 * names in the certificate request from the server. */
1670 pkp->ca_certs = (STACK_OF(X509) **) apr_pcalloc(p, ncerts * sizeof(sk));
1671 sctx = X509_STORE_CTX_new();
1672
1673 if (!sctx) {
1674 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02208)
1675 "SSL proxy client cert initialization failed");
1676 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1677 return ssl_die(s);
1678 }
1679
1680 X509_STORE_load_locations(store, pkp->ca_cert_file, NULL);
1681
1682 for (n = 0; n < ncerts; n++) {
1683 int i;
1684
1685 X509_INFO *inf = sk_X509_INFO_value(pkp->certs, n);
1686 X509_STORE_CTX_init(sctx, store, inf->x509, NULL);
1687
1688 /* Attempt to verify the client cert */
1689 if (X509_verify_cert(sctx) != 1) {
1690 int err = X509_STORE_CTX_get_error(sctx);
1691 ssl_log_xerror(SSLLOG_MARK, APLOG_WARNING, 0, ptemp, s, inf->x509,
1692 APLOGNO(02270) "SSL proxy client cert chain "
1693 "verification failed: %s :",
1694 X509_verify_cert_error_string(err));
1695 }
1696
1697 /* Clear X509_verify_cert errors */
1698 ERR_clear_error();
1699
1700 /* Obtain a copy of the verified chain */
1701 chain = X509_STORE_CTX_get1_chain(sctx);
1702
1703 if (chain != NULL) {
1704 /* Discard end entity cert from the chain */
1705 X509_free(sk_X509_shift(chain));
1706
1707 if ((i = sk_X509_num(chain)) > 0) {
1708 /* Store the chain for later use */
1709 pkp->ca_certs[n] = chain;
1710 }
1711 else {
1712 /* Discard empty chain */
1713 sk_X509_pop_free(chain, X509_free);
1714 pkp->ca_certs[n] = NULL;
1715 }
1716
1717 ssl_log_xerror(SSLLOG_MARK, APLOG_DEBUG, 0, ptemp, s, inf->x509,
1718 APLOGNO(02271)
1719 "loaded %i intermediate CA%s for cert %i: ",
1720 i, i == 1 ? "" : "s", n);
1721 if (i > 0) {
1722 int j;
1723 for (j = 0; j < i; j++) {
1724 ssl_log_xerror(SSLLOG_MARK, APLOG_DEBUG, 0, ptemp, s,
1725 sk_X509_value(chain, j), APLOGNO(03039)
1726 "%i:", j);
1727 }
1728 }
1729 }
1730
1731 /* get ready for next X509_STORE_CTX_init */
1732 X509_STORE_CTX_cleanup(sctx);
1733 }
1734
1735 X509_STORE_CTX_free(sctx);
1736
1737 return APR_SUCCESS;
1738}
1739
1740#define MODSSL_CFG_ITEM_FREE(func, item) \
1741 if (item) { \
1742 func(item); \
1743 item = NULL; \
1744 }
1745
1746static void ssl_init_ctx_cleanup(modssl_ctx_t *mctx)
1747{
1748 MODSSL_CFG_ITEM_FREE(SSL_CTX_free, mctx->ssl_ctx);
1749
1750#ifdef HAVE_SRP
1751 if (mctx->srp_vbase != NULL) {
1752 SRP_VBASE_free(mctx->srp_vbase);
1753 mctx->srp_vbase = NULL;
1754 }
1755#endif
1756}
1757
1758static apr_status_t ssl_cleanup_proxy_ctx(void *data)
1759{
1760 modssl_ctx_t *mctx = data;
1761
1762 ssl_init_ctx_cleanup(mctx);
1763
1764 if (mctx->pkp->certs) {
1765 int i = 0;
1766 int ncerts = sk_X509_INFO_num(mctx->pkp->certs);
1767
1768 if (mctx->pkp->ca_certs) {
1769 for (i = 0; i < ncerts; i++) {
1770 if (mctx->pkp->ca_certs[i] != NULL) {
1771 sk_X509_pop_free(mctx->pkp->ca_certs[i], X509_free);
1772 }
1773 }
1774 }
1775
1776 sk_X509_INFO_pop_free(mctx->pkp->certs, X509_INFO_free);
1777 mctx->pkp->certs = NULL;
1778 }
1779
1780 return APR_SUCCESS;
1781}
1782
1783static apr_status_t ssl_init_proxy_ctx(server_rec *s,
1784 apr_pool_t *p,
1785 apr_pool_t *ptemp,
1786 modssl_ctx_t *proxy)
1787{
1788 apr_status_t rv;
1789
1790 if (proxy->ssl_ctx) {
1791 /* Merged/initialized already */
1792 return APR_SUCCESS;
1793 }
1794
1795 apr_pool_cleanup_register(p, proxy,
1796 ssl_cleanup_proxy_ctx,
1797 apr_pool_cleanup_null);
1798
1799 if ((rv = ssl_init_ctx(s, p, ptemp, proxy)) != APR_SUCCESS) {
1800 return rv;
1801 }
1802
1803 if ((rv = ssl_init_proxy_certs(s, p, ptemp, proxy)) != APR_SUCCESS) {
1804 return rv;
1805 }
1806
1807 return APR_SUCCESS;
1808}
1809
1810static apr_status_t ssl_init_server_ctx(server_rec *s,
1811 apr_pool_t *p,
1812 apr_pool_t *ptemp,
1813 SSLSrvConfigRec *sc,
1814 apr_array_header_t *pphrases)
1815{
1816 apr_status_t rv;
1817 modssl_pk_server_t *pks;
1818#ifdef HAVE_SSL_CONF_CMD
1819 ssl_ctx_param_t *param = (ssl_ctx_param_t *)sc->server->ssl_ctx_param->elts;
1820 SSL_CONF_CTX *cctx = sc->server->ssl_ctx_config;
1821 int i;
1822#endif
1823 int n;
1824
1825 /*
1826 * Check for problematic re-initializations
1827 */
1828 if (sc->server->ssl_ctx) {
1829 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02569)
1830 "Illegal attempt to re-initialise SSL for server "
1831 "(SSLEngine On should go in the VirtualHost, not in global scope.)");
1832 return APR_EGENERAL;
1833 }
1834
1835 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(10083)
1836 "Init: (%s) mod_md support is %s.", ssl_util_vhostid(p, s),
1837 md_is_managed? "available" : "unavailable");
1838 if (md_is_managed && md_is_managed(s)) {
1839 modssl_pk_server_t *const pks = sc->server->pks;
1840 if (pks->cert_files->nelts > 0 || pks->key_files->nelts > 0) {
1841 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(10084)
1842 "Init: (%s) You configured certificate/key files on this host, but "
1843 "is is covered by a Managed Domain. You need to remove these directives "
1844 "for the Managed Domain to take over.", ssl_util_vhostid(p, s));
1845 }
1846 else {
1847 const char *key_file, *cert_file, *chain_file;
1848 }
1849 }
1850
1851 if (apr_is_empty_array(pks->cert_files)) {
1852 /* does someone propose a certiciate to fall back on here? */
1853 ssl_run_add_fallback_cert_files(s, p, pks->cert_files, pks->key_files);
1854 if (n < pks->cert_files->nelts) {
1855 pks->service_unavailable = 1;
1856 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, APLOGNO(10085)
1857 "Init: %s will respond with '503 Service Unavailable' for now. There "
1858 "are no SSL certificates configured and no other module contributed any.",
1859 ssl_util_vhostid(p, s));
1860 }
1861 else {
1862 rv = APR_ENOTIMPL;
1863 }
1864 }
1865
1866 if (n < pks->cert_files->nelts) {
1867 /* additionally installed certs overrides any old chain configuration */
1868 sc->server->cert_chain = NULL;
1869 }
1870
1871 if ((rv = ssl_init_ctx(s, p, ptemp, sc->server)) != APR_SUCCESS) {
1872 return rv;
1873 }
1874
1875 if ((rv = ssl_init_server_certs(s, p, ptemp, sc->server, pphrases))
1876 != APR_SUCCESS) {
1877 return rv;
1878 }
1879
1880#ifdef HAVE_SSL_CONF_CMD
1881 SSL_CONF_CTX_set_ssl_ctx(cctx, sc->server->ssl_ctx);
1882 for (i = 0; i < sc->server->ssl_ctx_param->nelts; i++, param++) {
1883 ERR_clear_error();
1884 if (SSL_CONF_cmd(cctx, param->name, param->value) <= 0) {
1885 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02407)
1886 "\"SSLOpenSSLConfCmd %s %s\" failed for %s",
1887 param->name, param->value, sc->vhost_id);
1888 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1889 return ssl_die(s);
1890 } else {
1891 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02556)
1892 "\"SSLOpenSSLConfCmd %s %s\" applied to %s",
1893 param->name, param->value, sc->vhost_id);
1894 }
1895 }
1896
1897 if (SSL_CONF_CTX_finish(cctx) == 0) {
1898 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02547)
1899 "SSL_CONF_CTX_finish() failed");
1900 SSL_CONF_CTX_free(cctx);
1901 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1902 return ssl_die(s);
1903 }
1904#endif
1905
1906 if (SSL_CTX_check_private_key(sc->server->ssl_ctx) != 1) {
1907 ap_log_error(APLOG_MARK, APLOG_EMERG, 0, s, APLOGNO(02572)
1908 "Failed to configure at least one certificate and key "
1909 "for %s", sc->vhost_id);
1910 ssl_log_ssl_error(SSLLOG_MARK, APLOG_EMERG, s);
1911 return ssl_die(s);
1912 }
1913
1914#if defined(HAVE_OCSP_STAPLING) && defined(SSL_CTRL_SET_CURRENT_CERT)
1915 /*
1916 * OpenSSL 1.0.2 and later allows iterating over all SSL_CTX certs
1917 * by means of SSL_CTX_set_current_cert. Enabling stapling at this
1918 * (late) point makes sure that we catch both certificates loaded
1919 * via SSLCertificateFile and SSLOpenSSLConfCmd Certificate.
1920 */
1921 do {
1922 if (sc->server->stapling_enabled == TRUE) {
1923 X509 *cert;
1924 int i = 0;
1925 int ret = SSL_CTX_set_current_cert(sc->server->ssl_ctx,
1926 SSL_CERT_SET_FIRST);
1927 while (ret) {
1928 cert = SSL_CTX_get0_certificate(sc->server->ssl_ctx);
1929 if (!cert || !ssl_stapling_init_cert(s, p, ptemp, sc->server,
1930 cert)) {
1931 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02604)
1932 "Unable to configure certificate %s:%d "
1933 "for stapling", sc->vhost_id, i);
1934 }
1935 ret = SSL_CTX_set_current_cert(sc->server->ssl_ctx,
1936 SSL_CERT_SET_NEXT);
1937 i++;
1938 }
1939 }
1940 } while(0);
1941#endif
1942
1943#ifdef HAVE_TLS_SESSION_TICKETS
1944 if ((rv = ssl_init_ticket_key(s, p, ptemp, sc->server)) != APR_SUCCESS) {
1945 return rv;
1946 }
1947#endif
1948
1949 SSL_CTX_set_timeout(sc->server->ssl_ctx,
1950 sc->session_cache_timeout == UNSET ?
1951 SSL_SESSION_CACHE_TIMEOUT : sc->session_cache_timeout);
1952
1953 return APR_SUCCESS;
1954}
1955
1956/*
1957 * Configure a particular server
1958 */
1959apr_status_t ssl_init_ConfigureServer(server_rec *s,
1960 apr_pool_t *p,
1961 apr_pool_t *ptemp,
1962 SSLSrvConfigRec *sc,
1963 apr_array_header_t *pphrases)
1964{
1965 SSLDirConfigRec *sdc = ap_get_module_config(s->lookup_defaults,
1966 &ssl_module);
1967 apr_status_t rv;
1968
1969 /* Initialize the server if SSL is enabled or optional.
1970 */
1971 if ((sc->enabled == SSL_ENABLED_TRUE) || (sc->enabled == SSL_ENABLED_OPTIONAL)) {
1972 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, APLOGNO(01914)
1973 "Configuring server %s for SSL protocol", sc->vhost_id);
1974 if ((rv = ssl_init_server_ctx(s, p, ptemp, sc, pphrases))
1975 != APR_SUCCESS) {
1976 return rv;
1977 }
1978
1979 /* Initialize OCSP Responder certificate if OCSP enabled */
1980 #ifndef OPENSSL_NO_OCSP
1981 ssl_init_ocsp_certificates(s, sc->server);
1982 #endif
1983
1984 }
1985
1986 sdc->proxy->sc = sc;
1987 if (sdc->proxy_enabled == TRUE) {
1988 rv = ssl_init_proxy_ctx(s, p, ptemp, sdc->proxy);
1989 if (rv != APR_SUCCESS) {
1990 return rv;
1991 }
1992 }
1993 else {
1994 sdc->proxy_enabled = FALSE;
1995 }
1996 sdc->proxy_post_config = 1;
1997
1998 return APR_SUCCESS;
1999}
2000
2001apr_status_t ssl_init_CheckServers(server_rec *base_server, apr_pool_t *p)
2002{
2003 server_rec *s;
2004 SSLSrvConfigRec *sc;
2005#ifndef HAVE_TLSEXT
2006 server_rec *ps;
2007 apr_hash_t *table;
2008 const char *key;
2009 apr_ssize_t klen;
2010
2011 BOOL conflict = FALSE;
2012#endif
2013
2014 /*
2015 * Give out warnings when a server has HTTPS configured
2016 * for the HTTP port or vice versa
2017 */
2018 for (s = base_server; s; s = s->next) {
2019 sc = mySrvConfig(s);
2020
2021 if ((sc->enabled == SSL_ENABLED_TRUE) && (s->port == DEFAULT_HTTP_PORT)) {
2022 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
2023 base_server, APLOGNO(01915)
2024 "Init: (%s) You configured HTTPS(%d) "
2025 "on the standard HTTP(%d) port!",
2026 ssl_util_vhostid(p, s),
2027 DEFAULT_HTTPS_PORT, DEFAULT_HTTP_PORT);
2028 }
2029
2030 if ((sc->enabled == SSL_ENABLED_FALSE) && (s->port == DEFAULT_HTTPS_PORT)) {
2031 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
2032 base_server, APLOGNO(01916)
2033 "Init: (%s) You configured HTTP(%d) "
2034 "on the standard HTTPS(%d) port!",
2035 ssl_util_vhostid(p, s),
2036 DEFAULT_HTTP_PORT, DEFAULT_HTTPS_PORT);
2037 }
2038 }
2039
2040#ifndef HAVE_TLSEXT
2041 /*
2042 * Give out warnings when more than one SSL-aware virtual server uses the
2043 * same IP:port and an OpenSSL version without support for TLS extensions
2044 * (SNI in particular) is used.
2045 */
2046 table = apr_hash_make(p);
2047
2048 for (s = base_server; s; s = s->next) {
2049 char *addr;
2050
2051 sc = mySrvConfig(s);
2052
2053 if (!((sc->enabled == SSL_ENABLED_TRUE) && s->addrs)) {
2054 continue;
2055 }
2056
2057 apr_sockaddr_ip_get(&addr, s->addrs->host_addr);
2058 key = apr_psprintf(p, "%s:%u", addr, s->addrs->host_port);
2059 klen = strlen(key);
2060
2061 if ((ps = (server_rec *)apr_hash_get(table, key, klen))) {
2062 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server, APLOGNO(02662)
2063 "Init: SSL server IP/port conflict: "
2064 "%s (%s:%d) vs. %s (%s:%d)",
2065 ssl_util_vhostid(p, s),
2066 (s->defn_name ? s->defn_name : "unknown"),
2067 s->defn_line_number,
2068 ssl_util_vhostid(p, ps),
2069 (ps->defn_name ? ps->defn_name : "unknown"),
2070 ps->defn_line_number);
2071 conflict = TRUE;
2072 continue;
2073 }
2074
2075 apr_hash_set(table, key, klen, s);
2076 }
2077
2078 if (conflict) {
2079 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, base_server, APLOGNO(01917)
2080 "Init: Name-based SSL virtual hosts require "
2081 "an OpenSSL version with support for TLS extensions "
2082 "(RFC 6066 - Server Name Indication / SNI), "
2083 "but the currently used library version (%s) is "
2084 "lacking this feature", MODSSL_LIBRARY_DYNTEXT);
2085 }
2086#endif
2087
2088 return APR_SUCCESS;
2089}
2090
2091int ssl_proxy_section_post_config(apr_pool_t *p, apr_pool_t *plog,
2092 apr_pool_t *ptemp, server_rec *s,
2093 ap_conf_vector_t *section_config)
2094{
2095 SSLDirConfigRec *sdc = ap_get_module_config(s->lookup_defaults,
2096 &ssl_module);
2097 SSLDirConfigRec *pdc = ap_get_module_config(section_config,
2098 &ssl_module);
2099 if (pdc) {
2100 pdc->proxy->sc = mySrvConfig(s);
2101 ssl_config_proxy_merge(p, sdc, pdc);
2102 if (pdc->proxy_enabled) {
2103 apr_status_t rv;
2104
2105 rv = ssl_init_proxy_ctx(s, p, ptemp, pdc->proxy);
2106 if (rv != APR_SUCCESS) {
2107 return !OK;
2108 }
2109
2110 rv = ssl_run_init_server(s, p, 1, pdc->proxy->ssl_ctx);
2111 if (rv != APR_SUCCESS) {
2112 return !OK;
2113 }
2114 }
2115 pdc->proxy_post_config = 1;
2116 }
2117 return OK;
2118}
2119
2120static int ssl_init_FindCAList_X509NameCmp(const X509_NAME * const *a,
2121 const X509_NAME * const *b)
2122{
2123 return(X509_NAME_cmp(*a, *b));
2124}
2125
2126static void ssl_init_PushCAList(STACK_OF(X509_NAME) *ca_list,
2127 server_rec *s, apr_pool_t *ptemp,
2128 const char *file)
2129{
2130 int n;
2131 STACK_OF(X509_NAME) *sk;
2132
2133 sk = (STACK_OF(X509_NAME) *)
2134 SSL_load_client_CA_file(file);
2135
2136 if (!sk) {
2137 return;
2138 }
2139
2140 for (n = 0; n < sk_X509_NAME_num(sk); n++) {
2141 X509_NAME *name = sk_X509_NAME_value(sk, n);
2142
2143 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, APLOGNO(02209)
2144 "CA certificate: %s",
2145 modssl_X509_NAME_to_string(ptemp, name, 0));
2146
2147 /*
2148 * note that SSL_load_client_CA_file() checks for duplicates,
2149 * but since we call it multiple times when reading a directory
2150 * we must also check for duplicates ourselves.
2151 */
2152
2153 if (sk_X509_NAME_find(ca_list, name) < 0) {
2154 /* this will be freed when ca_list is */
2155 sk_X509_NAME_push(ca_list, name);
2156 }
2157 else {
2158 /* need to free this ourselves, else it will leak */
2159 X509_NAME_free(name);
2160 }
2161 }
2162
2163 sk_X509_NAME_free(sk);
2164}
2165
2166STACK_OF(X509_NAME) *ssl_init_FindCAList(server_rec *s,
2167 apr_pool_t *ptemp,
2168 const char *ca_file,
2169 const char *ca_path)
2170{
2171 STACK_OF(X509_NAME) *ca_list;
2172
2173 /*
2174 * Start with a empty stack/list where new
2175 * entries get added in sorted order.
2176 */
2177 ca_list = sk_X509_NAME_new(ssl_init_FindCAList_X509NameCmp);
2178
2179 /*
2180 * Process CA certificate bundle file
2181 */
2182 if (ca_file) {
2183 ssl_init_PushCAList(ca_list, s, ptemp, ca_file);
2184 /*
2185 * If ca_list is still empty after trying to load ca_file
2186 * then the file failed to load, and users should hear about that.
2187 */
2188 if (sk_X509_NAME_num(ca_list) == 0) {
2189 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s, APLOGNO(02210)
2190 "Failed to load SSLCACertificateFile: %s", ca_file);
2191 ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, s);
2192 }
2193 }
2194
2195 /*
2196 * Process CA certificate path files
2197 */
2198 if (ca_path) {
2199 apr_dir_t *dir;
2200 apr_finfo_t direntry;
2201 apr_int32_t finfo_flags = APR_FINFO_TYPE|APR_FINFO_NAME;
2202 apr_status_t rv;
2203
2204 if ((rv = apr_dir_open(&dir, ca_path, ptemp)) != APR_SUCCESS) {
2205 ap_log_error(APLOG_MARK, APLOG_EMERG, rv, s, APLOGNO(02211)
2206 "Failed to open Certificate Path `%s'",
2207 ca_path);
2208 sk_X509_NAME_pop_free(ca_list, X509_NAME_free);
2209 return NULL;
2210 }
2211
2212 while ((apr_dir_read(&direntry, finfo_flags, dir)) == APR_SUCCESS) {
2213 const char *file;
2214 if (direntry.filetype == APR_DIR) {
2215 continue; /* don't try to load directories */
2216 }
2217 file = apr_pstrcat(ptemp, ca_path, "/", direntry.name, NULL);
2218 ssl_init_PushCAList(ca_list, s, ptemp, file);
2219 }
2220
2221 apr_dir_close(dir);
2222 }
2223
2224 /*
2225 * Cleanup
2226 */
2227 (void) sk_X509_NAME_set_cmp_func(ca_list, NULL);
2228
2229 return ca_list;
2230}
2231
2232void ssl_init_Child(apr_pool_t *p, server_rec *s)
2233{
2234 SSLModConfigRec *mc = myModConfig(s);
2235 mc->pid = getpid(); /* only call getpid() once per-process */
2236
2237 /* XXX: there should be an ap_srand() function */
2238 srand((unsigned int)time(NULL));
2239
2240 /* open the mutex lockfile */
2241 ssl_mutex_reinit(s, p);
2242#ifdef HAVE_OCSP_STAPLING
2243 ssl_stapling_mutex_reinit(s, p);
2244#endif
2245}
2246
2247apr_status_t ssl_init_ModuleKill(void *data)
2248{
2249 SSLSrvConfigRec *sc;
2250 server_rec *base_server = (server_rec *)data;
2251 server_rec *s;
2252
2253 /*
2254 * Drop the session cache and mutex
2255 */
2256 ssl_scache_kill(base_server);
2257
2258 /*
2259 * Free the non-pool allocated structures
2260 * in the per-server configurations
2261 */
2262 for (s = base_server; s; s = s->next) {
2263 sc = mySrvConfig(s);
2264
2265 ssl_init_ctx_cleanup(sc->server);
2266
2267 /* Not Sure but possibly clear X509 trusted cert file */
2268 #ifndef OPENSSL_NO_OCSP
2269 sk_X509_pop_free(sc->server->ocsp_certs, X509_free);
2270 #endif
2271
2272 }
2273
2274#if !MODSSL_USE_OPENSSL_PRE_1_1_API
2275 free_bio_methods();
2276#endif
2277 free_dh_params();
2278
2279 return APR_SUCCESS;
2280}
2281