]> granicus.if.org Git - apache/blob - modules/ssl/mod_ssl.c
mod_ssl: work around leaks on (graceful) restart.
[apache] / modules / ssl / mod_ssl.c
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  *  mod_ssl.c
24  *  Apache API interface structures
25  */
26
27 #include "ssl_private.h"
28 #include "mod_ssl.h"
29 #include "mod_ssl_openssl.h"
30 #include "util_md5.h"
31 #include "util_mutex.h"
32 #include "ap_provider.h"
33 #include "http_config.h"
34
35 #include "mod_proxy.h" /* for proxy_hook_section_post_config() */
36
37 #include <assert.h>
38
39 #if HAVE_VALGRIND
40 #include <valgrind.h>
41 int ssl_running_on_valgrind = 0;
42 #endif
43 static int modssl_running_statically = 0;
44
45 APR_IMPLEMENT_OPTIONAL_HOOK_RUN_ALL(ssl, SSL, int, pre_handshake,
46                                     (conn_rec *c,SSL *ssl,int is_proxy),
47                                     (c,ssl,is_proxy), OK, DECLINED);
48
49 /*
50  *  the table of configuration directives we provide
51  */
52
53 #define SSL_CMD_ALL(name, args, desc) \
54         AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
55                        NULL, RSRC_CONF|OR_AUTHCFG, desc),
56
57 #define SSL_CMD_SRV(name, args, desc) \
58         AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
59                        NULL, RSRC_CONF, desc),
60
61 #define SSL_CMD_PXY(name, args, desc) \
62         AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
63                        NULL, RSRC_CONF|PROXY_CONF, desc),
64
65 #define SSL_CMD_DIR(name, type, args, desc) \
66         AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
67                        NULL, OR_##type, desc),
68
69 #define AP_END_CMD { NULL }
70
71 static const command_rec ssl_config_cmds[] = {
72     /*
73      * Global (main-server) context configuration directives
74      */
75     SSL_CMD_SRV(PassPhraseDialog, TAKE1,
76                 "SSL dialog mechanism for the pass phrase query "
77                 "('builtin', '|/path/to/pipe_program', "
78                 "or 'exec:/path/to/cgi_program')")
79     SSL_CMD_SRV(SessionCache, TAKE1,
80                 "SSL Session Cache storage "
81                 "('none', 'nonenotnull', 'dbm:/path/to/file')")
82 #if defined(HAVE_OPENSSL_ENGINE_H) && defined(HAVE_ENGINE_INIT)
83     SSL_CMD_SRV(CryptoDevice, TAKE1,
84                 "SSL external Crypto Device usage "
85                 "('builtin', '...')")
86 #endif
87     SSL_CMD_SRV(RandomSeed, TAKE23,
88                 "SSL Pseudo Random Number Generator (PRNG) seeding source "
89                 "('startup|connect builtin|file:/path|exec:/path [bytes]')")
90
91     /*
92      * Per-server context configuration directives
93      */
94     SSL_CMD_SRV(Engine, TAKE1,
95                 "SSL switch for the protocol engine "
96                 "('on', 'off')")
97     SSL_CMD_SRV(FIPS, FLAG,
98                 "Enable FIPS-140 mode "
99                 "(`on', `off')")
100     SSL_CMD_ALL(CipherSuite, TAKE1,
101                 "Colon-delimited list of permitted SSL Ciphers "
102                 "('XXX:...:XXX' - see manual)")
103     SSL_CMD_SRV(CertificateFile, TAKE1,
104                 "SSL Server Certificate file "
105                 "('/path/to/file' - PEM or DER encoded)")
106     SSL_CMD_SRV(CertificateKeyFile, TAKE1,
107                 "SSL Server Private Key file "
108                 "('/path/to/file' - PEM or DER encoded)")
109     SSL_CMD_SRV(CertificateChainFile, TAKE1,
110                 "SSL Server CA Certificate Chain file "
111                 "('/path/to/file' - PEM encoded)")
112 #ifdef HAVE_TLS_SESSION_TICKETS
113     SSL_CMD_SRV(SessionTicketKeyFile, TAKE1,
114                 "TLS session ticket encryption/decryption key file (RFC 5077) "
115                 "('/path/to/file' - file with 48 bytes of random data)")
116 #endif
117     SSL_CMD_ALL(CACertificatePath, TAKE1,
118                 "SSL CA Certificate path "
119                 "('/path/to/dir' - contains PEM encoded files)")
120     SSL_CMD_ALL(CACertificateFile, TAKE1,
121                 "SSL CA Certificate file "
122                 "('/path/to/file' - PEM encoded)")
123     SSL_CMD_SRV(CADNRequestPath, TAKE1,
124                 "SSL CA Distinguished Name path "
125                 "('/path/to/dir' - symlink hashes to PEM of acceptable CA names to request)")
126     SSL_CMD_SRV(CADNRequestFile, TAKE1,
127                 "SSL CA Distinguished Name file "
128                 "('/path/to/file' - PEM encoded to derive acceptable CA names to request)")
129     SSL_CMD_SRV(CARevocationPath, TAKE1,
130                 "SSL CA Certificate Revocation List (CRL) path "
131                 "('/path/to/dir' - contains PEM encoded files)")
132     SSL_CMD_SRV(CARevocationFile, TAKE1,
133                 "SSL CA Certificate Revocation List (CRL) file "
134                 "('/path/to/file' - PEM encoded)")
135     SSL_CMD_SRV(CARevocationCheck, RAW_ARGS,
136                 "SSL CA Certificate Revocation List (CRL) checking mode")
137     SSL_CMD_ALL(VerifyClient, TAKE1,
138                 "SSL Client verify type "
139                 "('none', 'optional', 'require', 'optional_no_ca')")
140     SSL_CMD_ALL(VerifyDepth, TAKE1,
141                 "SSL Client verify depth "
142                 "('N' - number of intermediate certificates)")
143     SSL_CMD_SRV(SessionCacheTimeout, TAKE1,
144                 "SSL Session Cache object lifetime "
145                 "('N' - number of seconds)")
146 #ifdef OPENSSL_NO_SSL3
147 #define SSLv3_PROTO_PREFIX ""
148 #else
149 #define SSLv3_PROTO_PREFIX "SSLv3|"
150 #endif
151 #ifdef HAVE_TLSV1_X
152 #define SSL_PROTOCOLS SSLv3_PROTO_PREFIX "TLSv1|TLSv1.1|TLSv1.2"
153 #else
154 #define SSL_PROTOCOLS SSLv3_PROTO_PREFIX "TLSv1"
155 #endif
156     SSL_CMD_SRV(Protocol, RAW_ARGS,
157                 "Enable or disable various SSL protocols "
158                 "('[+-][" SSL_PROTOCOLS "] ...' - see manual)")
159     SSL_CMD_SRV(HonorCipherOrder, FLAG,
160                 "Use the server's cipher ordering preference")
161     SSL_CMD_SRV(Compression, FLAG,
162                 "Enable SSL level compression "
163                 "(`on', `off')")
164     SSL_CMD_SRV(SessionTickets, FLAG,
165                 "Enable or disable TLS session tickets"
166                 "(`on', `off')")
167     SSL_CMD_SRV(InsecureRenegotiation, FLAG,
168                 "Enable support for insecure renegotiation")
169     SSL_CMD_ALL(UserName, TAKE1,
170                 "Set user name to SSL variable value")
171     SSL_CMD_SRV(StrictSNIVHostCheck, FLAG,
172                 "Strict SNI virtual host checking")
173
174 #ifdef HAVE_SRP
175     SSL_CMD_SRV(SRPVerifierFile, TAKE1,
176                 "SRP verifier file "
177                 "('/path/to/file' - created by srptool)")
178     SSL_CMD_SRV(SRPUnknownUserSeed, TAKE1,
179                 "SRP seed for unknown users (to avoid leaking a user's existence) "
180                 "('some secret text')")
181 #endif
182
183     /*
184      * Proxy configuration for remote SSL connections
185      */
186     SSL_CMD_PXY(ProxyEngine, FLAG,
187                 "SSL switch for the proxy protocol engine "
188                 "('on', 'off')")
189     SSL_CMD_PXY(ProxyProtocol, RAW_ARGS,
190                "SSL Proxy: enable or disable SSL protocol flavors "
191                 "('[+-][" SSL_PROTOCOLS "] ...' - see manual)")
192     SSL_CMD_PXY(ProxyCipherSuite, TAKE1,
193                "SSL Proxy: colon-delimited list of permitted SSL ciphers "
194                "('XXX:...:XXX' - see manual)")
195     SSL_CMD_PXY(ProxyVerify, TAKE1,
196                "SSL Proxy: whether to verify the remote certificate "
197                "('on' or 'off')")
198     SSL_CMD_PXY(ProxyVerifyDepth, TAKE1,
199                "SSL Proxy: maximum certificate verification depth "
200                "('N' - number of intermediate certificates)")
201     SSL_CMD_PXY(ProxyCACertificateFile, TAKE1,
202                "SSL Proxy: file containing server certificates "
203                "('/path/to/file' - PEM encoded certificates)")
204     SSL_CMD_PXY(ProxyCACertificatePath, TAKE1,
205                "SSL Proxy: directory containing server certificates "
206                "('/path/to/dir' - contains PEM encoded certificates)")
207     SSL_CMD_PXY(ProxyCARevocationPath, TAKE1,
208                 "SSL Proxy: CA Certificate Revocation List (CRL) path "
209                 "('/path/to/dir' - contains PEM encoded files)")
210     SSL_CMD_PXY(ProxyCARevocationFile, TAKE1,
211                 "SSL Proxy: CA Certificate Revocation List (CRL) file "
212                 "('/path/to/file' - PEM encoded)")
213     SSL_CMD_PXY(ProxyCARevocationCheck, RAW_ARGS,
214                 "SSL Proxy: CA Certificate Revocation List (CRL) checking mode")
215     SSL_CMD_PXY(ProxyMachineCertificateFile, TAKE1,
216                "SSL Proxy: file containing client certificates "
217                "('/path/to/file' - PEM encoded certificates)")
218     SSL_CMD_PXY(ProxyMachineCertificatePath, TAKE1,
219                "SSL Proxy: directory containing client certificates "
220                "('/path/to/dir' - contains PEM encoded certificates)")
221     SSL_CMD_PXY(ProxyMachineCertificateChainFile, TAKE1,
222                "SSL Proxy: file containing issuing certificates "
223                "of the client certificate "
224                "(`/path/to/file' - PEM encoded certificates)")
225     SSL_CMD_PXY(ProxyCheckPeerExpire, FLAG,
226                 "SSL Proxy: check the peer certificate's expiration date")
227     SSL_CMD_PXY(ProxyCheckPeerCN, FLAG,
228                 "SSL Proxy: check the peer certificate's CN")
229     SSL_CMD_PXY(ProxyCheckPeerName, FLAG,
230                 "SSL Proxy: check the peer certificate's name "
231                 "(must be present in subjectAltName extension or CN")
232
233     /*
234      * Per-directory context configuration directives
235      */
236     SSL_CMD_DIR(Options, OPTIONS, RAW_ARGS,
237                "Set one or more options to configure the SSL engine"
238                "('[+-]option[=value] ...' - see manual)")
239     SSL_CMD_DIR(RequireSSL, AUTHCFG, NO_ARGS,
240                "Require the SSL protocol for the per-directory context "
241                "(no arguments)")
242     SSL_CMD_DIR(Require, AUTHCFG, RAW_ARGS,
243                "Require a boolean expression to evaluate to true for granting access"
244                "(arbitrary complex boolean expression - see manual)")
245     SSL_CMD_DIR(RenegBufferSize, AUTHCFG, TAKE1,
246                 "Configure the amount of memory that will be used for buffering the "
247                 "request body if a per-location SSL renegotiation is required due to "
248                 "changed access control requirements")
249
250     SSL_CMD_SRV(OCSPEnable, FLAG,
251                "Enable use of OCSP to verify certificate revocation ('on', 'off')")
252     SSL_CMD_SRV(OCSPDefaultResponder, TAKE1,
253                "URL of the default OCSP Responder")
254     SSL_CMD_SRV(OCSPOverrideResponder, FLAG,
255                "Force use of the default responder URL ('on', 'off')")
256     SSL_CMD_SRV(OCSPResponseTimeSkew, TAKE1,
257                 "Maximum time difference in OCSP responses")
258     SSL_CMD_SRV(OCSPResponseMaxAge, TAKE1,
259                 "Maximum age of OCSP responses")
260     SSL_CMD_SRV(OCSPResponderTimeout, TAKE1,
261                 "OCSP responder query timeout")
262     SSL_CMD_SRV(OCSPUseRequestNonce, FLAG,
263                 "Whether OCSP queries use a nonce or not ('on', 'off')")
264     SSL_CMD_SRV(OCSPProxyURL, TAKE1,
265                 "Proxy URL to use for OCSP requests")
266
267 #ifdef HAVE_OCSP_STAPLING
268     /*
269      * OCSP Stapling options
270      */
271     SSL_CMD_SRV(StaplingCache, TAKE1,
272                 "SSL Stapling Response Cache storage "
273                 "(`dbm:/path/to/file')")
274     SSL_CMD_SRV(UseStapling, FLAG,
275                 "SSL switch for the OCSP Stapling protocol " "(`on', `off')")
276     SSL_CMD_SRV(StaplingResponseTimeSkew, TAKE1,
277                 "SSL stapling option for maximum time difference in OCSP responses")
278     SSL_CMD_SRV(StaplingResponderTimeout, TAKE1,
279                 "SSL stapling option for OCSP responder timeout")
280     SSL_CMD_SRV(StaplingResponseMaxAge, TAKE1,
281                 "SSL stapling option for maximum age of OCSP responses")
282     SSL_CMD_SRV(StaplingStandardCacheTimeout, TAKE1,
283                 "SSL stapling option for normal OCSP Response Cache Lifetime")
284     SSL_CMD_SRV(StaplingReturnResponderErrors, FLAG,
285                 "SSL stapling switch to return Status Errors Back to Client"
286                 "(`on', `off')")
287     SSL_CMD_SRV(StaplingFakeTryLater, FLAG,
288                 "SSL stapling switch to send tryLater response to client on error "
289                 "(`on', `off')")
290     SSL_CMD_SRV(StaplingErrorCacheTimeout, TAKE1,
291                 "SSL stapling option for OCSP Response Error Cache Lifetime")
292     SSL_CMD_SRV(StaplingForceURL, TAKE1,
293                 "SSL stapling option to Force the OCSP Stapling URL")
294 #endif
295
296 #ifdef HAVE_SSL_CONF_CMD
297     SSL_CMD_SRV(OpenSSLConfCmd, TAKE2,
298                 "OpenSSL configuration command")
299 #endif
300
301     /* Deprecated directives. */
302     AP_INIT_RAW_ARGS("SSLLog", ap_set_deprecated, NULL, OR_ALL,
303       "SSLLog directive is no longer supported - use ErrorLog."),
304     AP_INIT_RAW_ARGS("SSLLogLevel", ap_set_deprecated, NULL, OR_ALL,
305       "SSLLogLevel directive is no longer supported - use LogLevel."),
306
307     AP_END_CMD
308 };
309
310 /*
311  *  the various processing hooks
312  */
313 static int modssl_is_prelinked(void)
314 {
315     apr_size_t i = 0;
316     const module *mod;
317     while ((mod = ap_prelinked_modules[i++])) {
318         if (strcmp(mod->name, "mod_ssl.c") == 0) {
319             return 1;
320         }
321     }
322     return 0;
323 }
324
325 static apr_status_t ssl_cleanup_pre_config(void *data)
326 {
327     /*
328      * Try to kill the internals of the SSL library.
329      */
330 #ifdef HAVE_FIPS
331     FIPS_mode_set(0);
332 #endif
333     /* Corresponds to OBJ_create()s */
334     OBJ_cleanup();
335     /* Corresponds to OPENSSL_load_builtin_modules() */
336     CONF_modules_free();
337     /* Corresponds to SSL_library_init: */
338     EVP_cleanup();
339 #if HAVE_ENGINE_LOAD_BUILTIN_ENGINES
340     ENGINE_cleanup();
341 #endif
342 #if OPENSSL_VERSION_NUMBER >= 0x1000200fL
343     SSL_COMP_free_compression_methods();
344 #endif
345
346     /* Usually needed per thread, but this parent process is single-threaded */
347 #if OPENSSL_VERSION_NUMBER < 0x10100000L
348 #if OPENSSL_VERSION_NUMBER >= 0x1000000fL
349     ERR_remove_thread_state(NULL);
350 #else
351     ERR_remove_state(0);
352 #endif
353 #endif
354
355     /* Don't call ERR_free_strings in earlier versions, ERR_load_*_strings only
356      * actually loaded the error strings once per process due to static
357      * variable abuse in OpenSSL. */
358 #if (OPENSSL_VERSION_NUMBER >= 0x00090805f)
359     ERR_free_strings();
360 #endif
361
362     /* Also don't call CRYPTO_cleanup_all_ex_data when linked statically here;
363      * any registered ex_data indices may have been cached in static variables
364      * in OpenSSL; removing them may cause havoc.  Notably, with OpenSSL
365      * versions >= 0.9.8f, COMP_CTX cleanups would not be run, which
366      * could result in a per-connection memory leak (!). */
367     if (!modssl_running_statically) {
368         CRYPTO_cleanup_all_ex_data();
369     }
370
371     /*
372      * TODO: determine somewhere we can safely shove out diagnostics
373      *       (when enabled) at this late stage in the game:
374      * CRYPTO_mem_leaks_fp(stderr);
375      */
376     return APR_SUCCESS;
377 }
378
379 static int ssl_hook_pre_config(apr_pool_t *pconf,
380                                apr_pool_t *plog,
381                                apr_pool_t *ptemp)
382 {
383 #if HAVE_VALGRIND
384     ssl_running_on_valgrind = RUNNING_ON_VALGRIND;
385 #endif
386     modssl_running_statically = modssl_is_prelinked();
387
388     /* Some OpenSSL internals are allocated per-thread, make sure they
389      * are associated to the/our same thread-id until cleaned up.
390      */
391     ssl_util_thread_id_setup(pconf);
392
393     /* We must register the library in full, to ensure our configuration
394      * code can successfully test the SSL environment.
395      */
396 #if OPENSSL_VERSION_NUMBER < 0x10100000L
397     CRYPTO_malloc_init();
398 #else
399     OPENSSL_malloc_init();
400 #endif
401     ERR_load_crypto_strings();
402     SSL_load_error_strings();
403     SSL_library_init();
404 #if HAVE_ENGINE_LOAD_BUILTIN_ENGINES
405     ENGINE_load_builtin_engines();
406 #endif
407     OpenSSL_add_all_algorithms();
408     OPENSSL_load_builtin_modules();
409
410     if (OBJ_txt2nid("id-on-dnsSRV") == NID_undef) {
411         (void)OBJ_create("1.3.6.1.5.5.7.8.7", "id-on-dnsSRV",
412                          "SRVName otherName form");
413     }
414
415     /* Start w/o errors (e.g. OBJ_txt2nid() above) */
416     ERR_clear_error();
417
418     /*
419      * Let us cleanup the ssl library when the module is unloaded
420      */
421     apr_pool_cleanup_register(pconf, NULL, ssl_cleanup_pre_config,
422                                            apr_pool_cleanup_null);
423
424     /* Register us to handle mod_log_config %c/%x variables */
425     ssl_var_log_config_register(pconf);
426
427     /* Register to handle mod_status status page generation */
428     ssl_scache_status_register(pconf);
429
430     /* Register mutex type names so they can be configured with Mutex */
431     ap_mutex_register(pconf, SSL_CACHE_MUTEX_TYPE, NULL, APR_LOCK_DEFAULT, 0);
432 #ifdef HAVE_OCSP_STAPLING
433     ap_mutex_register(pconf, SSL_STAPLING_CACHE_MUTEX_TYPE, NULL,
434                       APR_LOCK_DEFAULT, 0);
435     ap_mutex_register(pconf, SSL_STAPLING_REFRESH_MUTEX_TYPE, NULL,
436                       APR_LOCK_DEFAULT, 0);
437 #endif
438
439     return OK;
440 }
441
442 static SSLConnRec *ssl_init_connection_ctx(conn_rec *c,
443                                            ap_conf_vector_t *per_dir_config)
444 {
445     SSLConnRec *sslconn = myConnConfig(c);
446     SSLSrvConfigRec *sc;
447
448     if (sslconn) {
449         return sslconn;
450     }
451
452     sslconn = apr_pcalloc(c->pool, sizeof(*sslconn));
453
454     if (per_dir_config) {
455         sslconn->dc = ap_get_module_config(per_dir_config, &ssl_module);
456     }
457     else {
458         sslconn->dc = ap_get_module_config(c->base_server->lookup_defaults,
459                                            &ssl_module);
460     }
461
462     sslconn->server = c->base_server;
463     sslconn->verify_depth = UNSET;
464     sc = mySrvConfig(c->base_server);
465     sslconn->cipher_suite = sc->server->auth.cipher_suite;
466
467     myConnConfigSet(c, sslconn);
468
469     return sslconn;
470 }
471
472 static int ssl_engine_status(conn_rec *c, SSLConnRec *sslconn)
473 {
474     if (c->master) {
475         return DECLINED;
476     }
477     if (sslconn) {
478         if (sslconn->disabled) {
479             return SUSPENDED;
480         }
481         if (sslconn->is_proxy) {
482             if (!sslconn->dc->proxy_enabled) {
483                 return DECLINED;
484             }
485         }
486         else {
487             if (mySrvConfig(sslconn->server)->enabled != SSL_ENABLED_TRUE) {
488                 return DECLINED;
489             }
490         }
491     }
492     else {
493         if (mySrvConfig(c->base_server)->enabled != SSL_ENABLED_TRUE) {
494             return DECLINED;
495         }
496     }
497     return OK;
498 }
499
500 static int ssl_engine_set(conn_rec *c,
501                           ap_conf_vector_t *per_dir_config,
502                           int proxy, int enable)
503 {
504     SSLConnRec *sslconn;
505     int status;
506     
507     if (proxy) {
508         sslconn = ssl_init_connection_ctx(c, per_dir_config);
509         sslconn->is_proxy = 1;
510     }
511     else {
512         sslconn = myConnConfig(c);
513     }
514
515     status = ssl_engine_status(c, sslconn);
516
517     if (proxy && status == DECLINED) {
518         if (enable) {
519             SSLSrvConfigRec *sc = mySrvConfig(sslconn->server);
520             ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(01961)
521                           "SSL Proxy requested for %s but not enabled "
522                           "[Hint: SSLProxyEngine]", sc->vhost_id);
523         }
524         sslconn->disabled = 1;
525     }
526     else if (sslconn) {
527         sslconn->disabled = !enable;
528     }
529
530     return status != DECLINED;
531 }
532
533 static int ssl_proxy_enable(conn_rec *c)
534 {
535     return ssl_engine_set(c, NULL, 1, 1);
536 }
537
538 static int ssl_engine_disable(conn_rec *c)
539 {
540     return ssl_engine_set(c, NULL, 0, 0);
541 }
542
543 int ssl_init_ssl_connection(conn_rec *c, request_rec *r)
544 {
545     SSLSrvConfigRec *sc;
546     SSL *ssl;
547     SSLConnRec *sslconn;
548     char *vhost_md5;
549     int rc;
550     modssl_ctx_t *mctx;
551     server_rec *server;
552
553     /*
554      * Create or retrieve SSL context
555      */
556     sslconn = ssl_init_connection_ctx(c, r ? r->per_dir_config : NULL);
557     server = sslconn->server;
558     sc = mySrvConfig(server);
559
560     /*
561      * Seed the Pseudo Random Number Generator (PRNG)
562      */
563     ssl_rand_seed(server, c->pool, SSL_RSCTX_CONNECT,
564                   sslconn->is_proxy ? "Proxy: " : "Server: ");
565
566     mctx = myCtxConfig(sslconn, sc);
567
568     /*
569      * Create a new SSL connection with the configured server SSL context and
570      * attach this to the socket. Additionally we register this attachment
571      * so we can detach later.
572      */
573     if (!(sslconn->ssl = ssl = SSL_new(mctx->ssl_ctx))) {
574         ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(01962)
575                       "Unable to create a new SSL connection from the SSL "
576                       "context");
577         ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, server);
578
579         c->aborted = 1;
580
581         return DECLINED; /* XXX */
582     }
583
584     rc = ssl_run_pre_handshake(c, ssl, sslconn->is_proxy ? 1 : 0);
585     if (rc != OK && rc != DECLINED) {
586         return rc;
587     }
588
589     vhost_md5 = ap_md5_binary(c->pool, (unsigned char *)sc->vhost_id,
590                               sc->vhost_id_len);
591
592     if (!SSL_set_session_id_context(ssl, (unsigned char *)vhost_md5,
593                                     APR_MD5_DIGESTSIZE*2))
594     {
595         ap_log_cerror(APLOG_MARK, APLOG_ERR, 0, c, APLOGNO(01963)
596                       "Unable to set session id context to '%s'", vhost_md5);
597         ssl_log_ssl_error(SSLLOG_MARK, APLOG_ERR, server);
598
599         c->aborted = 1;
600
601         return DECLINED; /* XXX */
602     }
603
604     SSL_set_app_data(ssl, c);
605     modssl_set_app_data2(ssl, NULL); /* will be request_rec */
606
607     SSL_set_verify_result(ssl, X509_V_OK);
608
609     ssl_io_filter_init(c, r, ssl);
610
611     return APR_SUCCESS;
612 }
613
614 static const char *ssl_hook_http_scheme(const request_rec *r)
615 {
616     SSLSrvConfigRec *sc = mySrvConfig(r->server);
617
618     if (sc->enabled == SSL_ENABLED_FALSE || sc->enabled == SSL_ENABLED_OPTIONAL) {
619         return NULL;
620     }
621
622     return "https";
623 }
624
625 static apr_port_t ssl_hook_default_port(const request_rec *r)
626 {
627     SSLSrvConfigRec *sc = mySrvConfig(r->server);
628
629     if (sc->enabled == SSL_ENABLED_FALSE || sc->enabled == SSL_ENABLED_OPTIONAL) {
630         return 0;
631     }
632
633     return 443;
634 }
635
636 static int ssl_hook_pre_connection(conn_rec *c, void *csd)
637 {
638     SSLSrvConfigRec *sc;
639     SSLConnRec *sslconn = myConnConfig(c);
640
641     /*
642      * Immediately stop processing if SSL is disabled for this connection
643      */
644     if (ssl_engine_status(c, sslconn) != OK) {
645         return DECLINED;
646     }
647
648     if (sslconn) {
649         sc = mySrvConfig(sslconn->server);
650     }
651     else {
652         sc = mySrvConfig(c->base_server);
653     }
654
655     /*
656      * Remember the connection information for
657      * later access inside callback functions
658      */
659
660     ap_log_cerror(APLOG_MARK, APLOG_INFO, 0, c, APLOGNO(01964)
661                   "Connection to child %ld established "
662                   "(server %s)", c->id, sc->vhost_id);
663
664     return ssl_init_ssl_connection(c, NULL);
665 }
666
667 static int ssl_hook_process_connection(conn_rec* c)
668 {
669     SSLConnRec *sslconn = myConnConfig(c);
670
671     if (sslconn && !sslconn->disabled) {
672         /* On an active SSL connection, let the input filters initialize
673          * themselves which triggers the handshake, which again triggers
674          * all kinds of useful things such as SNI and ALPN.
675          */
676         apr_bucket_brigade* temp;
677
678         temp = apr_brigade_create(c->pool, c->bucket_alloc);
679         ap_get_brigade(c->input_filters, temp,
680                        AP_MODE_INIT, APR_BLOCK_READ, 0);
681         apr_brigade_destroy(temp);
682     }
683     
684     return DECLINED;
685 }
686
687 /*
688  *  the module registration phase
689  */
690
691 static void ssl_register_hooks(apr_pool_t *p)
692 {
693     /* ssl_hook_ReadReq needs to use the BrowserMatch settings so must
694      * run after mod_setenvif's post_read_request hook. */
695     static const char *pre_prr[] = { "mod_setenvif.c", NULL };
696     /* The ssl_init_Module post_config hook should run before mod_proxy's
697      * for the ssl proxy main configs to be merged with vhosts' before being
698      * themselves merged with mod_proxy's in proxy_hook_section_post_config.
699      */
700     static const char *b_pc[] = { "mod_proxy.c", NULL};
701
702
703     ssl_io_filter_register(p);
704
705     ap_hook_pre_connection(ssl_hook_pre_connection,NULL,NULL, APR_HOOK_MIDDLE);
706     ap_hook_process_connection(ssl_hook_process_connection, 
707                                                    NULL, NULL, APR_HOOK_MIDDLE);
708     ap_hook_test_config   (ssl_hook_ConfigTest,    NULL,NULL, APR_HOOK_MIDDLE);
709     ap_hook_post_config   (ssl_init_Module,        NULL,b_pc, APR_HOOK_MIDDLE);
710     ap_hook_http_scheme   (ssl_hook_http_scheme,   NULL,NULL, APR_HOOK_MIDDLE);
711     ap_hook_default_port  (ssl_hook_default_port,  NULL,NULL, APR_HOOK_MIDDLE);
712     ap_hook_pre_config    (ssl_hook_pre_config,    NULL,NULL, APR_HOOK_MIDDLE);
713     ap_hook_child_init    (ssl_init_Child,         NULL,NULL, APR_HOOK_MIDDLE);
714     ap_hook_post_read_request(ssl_hook_ReadReq, pre_prr,NULL, APR_HOOK_MIDDLE);
715     ap_hook_check_access  (ssl_hook_Access,        NULL,NULL, APR_HOOK_MIDDLE,
716                            AP_AUTH_INTERNAL_PER_CONF);
717     ap_hook_check_authn   (ssl_hook_UserCheck,     NULL,NULL, APR_HOOK_FIRST,
718                            AP_AUTH_INTERNAL_PER_CONF);
719     ap_hook_check_authz   (ssl_hook_Auth,          NULL,NULL, APR_HOOK_MIDDLE,
720                            AP_AUTH_INTERNAL_PER_CONF);
721     ap_hook_fixups        (ssl_hook_Fixup,         NULL,NULL, APR_HOOK_MIDDLE);
722
723     APR_OPTIONAL_HOOK(proxy, section_post_config,
724                       ssl_proxy_section_post_config, NULL, NULL,
725                       APR_HOOK_MIDDLE);
726
727     ssl_var_register(p);
728
729     APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
730     APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
731     APR_REGISTER_OPTIONAL_FN(ssl_engine_set);
732
733     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ssl",
734                               AUTHZ_PROVIDER_VERSION,
735                               &ssl_authz_provider_require_ssl,
736                               AP_AUTH_INTERNAL_PER_CONF);
737
738     ap_register_auth_provider(p, AUTHZ_PROVIDER_GROUP, "ssl-verify-client",
739                               AUTHZ_PROVIDER_VERSION,
740                               &ssl_authz_provider_verify_client,
741                               AP_AUTH_INTERNAL_PER_CONF);
742 }
743
744 module AP_MODULE_DECLARE_DATA ssl_module = {
745     STANDARD20_MODULE_STUFF,
746     ssl_config_perdir_create,   /* create per-dir    config structures */
747     ssl_config_perdir_merge,    /* merge  per-dir    config structures */
748     ssl_config_server_create,   /* create per-server config structures */
749     ssl_config_server_merge,    /* merge  per-server config structures */
750     ssl_config_cmds,            /* table of configuration directives   */
751     ssl_register_hooks          /* register hooks */
752 };