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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * _ __ ___ ___ __| | ___ ___| | mod_ssl
19 * | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
20 * | | | | | | (_) | (_| | \__ \__ \ |
21 * |_| |_| |_|\___/ \__,_|___|___/___/_|
24 * Apache API interface structures
32 * the table of configuration directives we provide
35 #define SSL_CMD_ALL(name, args, desc) \
36 AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
37 NULL, RSRC_CONF|OR_AUTHCFG, desc),
39 #define SSL_CMD_SRV(name, args, desc) \
40 AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
41 NULL, RSRC_CONF, desc),
43 #define SSL_CMD_DIR(name, type, args, desc) \
44 AP_INIT_##args("SSL"#name, ssl_cmd_SSL##name, \
45 NULL, OR_##type, desc),
47 #define AP_END_CMD { NULL }
49 const char ssl_valid_ssl_mutex_string[] =
50 "Valid SSLMutex mechanisms are: `none', `default'"
51 #if APR_HAS_FLOCK_SERIALIZE
52 ", `flock:/path/to/file'"
54 #if APR_HAS_FCNTL_SERIALIZE
55 ", `fcntl:/path/to/file'"
57 #if APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)
60 #if APR_HAS_POSIXSEM_SERIALIZE
63 #if APR_HAS_PROC_PTHREAD_SERIALIZE
66 #if APR_HAS_FLOCK_SERIALIZE || APR_HAS_FCNTL_SERIALIZE
67 ", `file:/path/to/file'"
69 #if (APR_HAS_SYSVSEM_SERIALIZE && !defined(PERCHILD_MPM)) || APR_HAS_POSIXSEM_SERIALIZE
74 static const command_rec ssl_config_cmds[] = {
76 * Global (main-server) context configuration directives
78 SSL_CMD_SRV(Mutex, TAKE1, ssl_valid_ssl_mutex_string)
79 SSL_CMD_SRV(PassPhraseDialog, TAKE1,
80 "SSL dialog mechanism for the pass phrase query "
81 "(`builtin', `|/path/to/pipe_program`, "
82 "or `exec:/path/to/cgi_program')")
83 SSL_CMD_SRV(SessionCache, TAKE1,
84 "SSL Session Cache storage "
85 "(`none', `dbm:/path/to/file')")
86 #ifdef SSL_EXPERIMENTAL_ENGINE
87 SSL_CMD_SRV(CryptoDevice, TAKE1,
88 "SSL external Crypto Device usage "
91 SSL_CMD_SRV(RandomSeed, TAKE23,
92 "SSL Pseudo Random Number Generator (PRNG) seeding source "
93 "(`startup|connect builtin|file:/path|exec:/path [bytes]')")
96 * Per-server context configuration directives
98 SSL_CMD_SRV(Engine, FLAG,
99 "SSL switch for the protocol engine "
101 SSL_CMD_ALL(CipherSuite, TAKE1,
102 "Colon-delimited list of permitted SSL Ciphers "
103 "(`XXX:...:XXX' - see manual)")
104 SSL_CMD_SRV(CertificateFile, TAKE1,
105 "SSL Server Certificate file "
106 "(`/path/to/file' - PEM or DER encoded)")
107 SSL_CMD_SRV(CertificateKeyFile, TAKE1,
108 "SSL Server Private Key file "
109 "(`/path/to/file' - PEM or DER encoded)")
110 SSL_CMD_SRV(CertificateChainFile, TAKE1,
111 "SSL Server CA Certificate Chain file "
112 "(`/path/to/file' - PEM encoded)")
113 SSL_CMD_ALL(CACertificatePath, TAKE1,
114 "SSL CA Certificate path "
115 "(`/path/to/dir' - contains PEM encoded files)")
116 SSL_CMD_ALL(CACertificateFile, TAKE1,
117 "SSL CA Certificate file "
118 "(`/path/to/file' - PEM encoded)")
119 SSL_CMD_SRV(CARevocationPath, TAKE1,
120 "SSL CA Certificate Revocation List (CRL) path "
121 "(`/path/to/dir' - contains PEM encoded files)")
122 SSL_CMD_SRV(CARevocationFile, TAKE1,
123 "SSL CA Certificate Revocation List (CRL) file "
124 "(`/path/to/file' - PEM encoded)")
125 SSL_CMD_ALL(VerifyClient, TAKE1,
126 "SSL Client verify type "
127 "(`none', `optional', `require', `optional_no_ca')")
128 SSL_CMD_ALL(VerifyDepth, TAKE1,
129 "SSL Client verify depth "
130 "(`N' - number of intermediate certificates)")
131 SSL_CMD_SRV(SessionCacheTimeout, TAKE1,
132 "SSL Session Cache object lifetime "
133 "(`N' - number of seconds)")
134 SSL_CMD_SRV(Protocol, RAW_ARGS,
135 "Enable or disable various SSL protocols"
136 "(`[+-][SSLv2|SSLv3|TLSv1] ...' - see manual)")
137 SSL_CMD_ALL(UserName, TAKE1,
138 "Set user name to SSL variable value")
139 SSL_CMD_SRV(InsecureRenegotiation, FLAG,
140 "Enable support for insecure renegotiation")
143 * Proxy configuration for remote SSL connections
145 SSL_CMD_SRV(ProxyEngine, FLAG,
146 "SSL switch for the proxy protocol engine "
148 SSL_CMD_SRV(ProxyProtocol, RAW_ARGS,
149 "SSL Proxy: enable or disable SSL protocol flavors "
150 "(`[+-][SSLv2|SSLv3|TLSv1] ...' - see manual)")
151 SSL_CMD_SRV(ProxyCipherSuite, TAKE1,
152 "SSL Proxy: colon-delimited list of permitted SSL ciphers "
153 "(`XXX:...:XXX' - see manual)")
154 SSL_CMD_SRV(ProxyVerify, TAKE1,
155 "SSL Proxy: whether to verify the remote certificate "
157 SSL_CMD_SRV(ProxyVerifyDepth, TAKE1,
158 "SSL Proxy: maximum certificate verification depth "
159 "(`N' - number of intermediate certificates)")
160 SSL_CMD_SRV(ProxyCACertificateFile, TAKE1,
161 "SSL Proxy: file containing server certificates "
162 "(`/path/to/file' - PEM encoded certificates)")
163 SSL_CMD_SRV(ProxyCACertificatePath, TAKE1,
164 "SSL Proxy: directory containing server certificates "
165 "(`/path/to/dir' - contains PEM encoded certificates)")
166 SSL_CMD_SRV(ProxyCARevocationPath, TAKE1,
167 "SSL Proxy: CA Certificate Revocation List (CRL) path "
168 "(`/path/to/dir' - contains PEM encoded files)")
169 SSL_CMD_SRV(ProxyCARevocationFile, TAKE1,
170 "SSL Proxy: CA Certificate Revocation List (CRL) file "
171 "(`/path/to/file' - PEM encoded)")
172 SSL_CMD_SRV(ProxyMachineCertificateFile, TAKE1,
173 "SSL Proxy: file containing client certificates "
174 "(`/path/to/file' - PEM encoded certificates)")
175 SSL_CMD_SRV(ProxyMachineCertificatePath, TAKE1,
176 "SSL Proxy: directory containing client certificates "
177 "(`/path/to/dir' - contains PEM encoded certificates)")
180 * Per-directory context configuration directives
182 SSL_CMD_DIR(Options, OPTIONS, RAW_ARGS,
183 "Set one or more options to configure the SSL engine"
184 "(`[+-]option[=value] ...' - see manual)")
185 SSL_CMD_DIR(RequireSSL, AUTHCFG, NO_ARGS,
186 "Require the SSL protocol for the per-directory context "
188 SSL_CMD_DIR(Require, AUTHCFG, RAW_ARGS,
189 "Require a boolean expression to evaluate to true for granting access"
190 "(arbitrary complex boolean expression - see manual)")
192 /* Deprecated directives. */
193 AP_INIT_RAW_ARGS("SSLLog", ap_set_deprecated, NULL, OR_ALL,
194 "SSLLog directive is no longer supported - use ErrorLog."),
195 AP_INIT_RAW_ARGS("SSLLogLevel", ap_set_deprecated, NULL, OR_ALL,
196 "SSLLogLevel directive is no longer supported - use LogLevel."),
202 * the various processing hooks
205 static int ssl_hook_pre_config(apr_pool_t *pconf,
209 /* Preregister the malloc callbacks so cmds can make library calls */
210 CRYPTO_malloc_init();
212 /* Register us to handle mod_log_config %c/%x variables */
213 ssl_var_log_config_register(pconf);
215 /* XXX: Register us to handle mod_status extensions that don't exist yet */
216 ssl_scache_status_register(pconf);
222 static SSLConnRec *ssl_init_connection_ctx(conn_rec *c)
224 SSLConnRec *sslconn = myConnConfig(c);
230 sslconn = apr_pcalloc(c->pool, sizeof(*sslconn));
232 myConnConfigSet(c, sslconn);
237 int ssl_proxy_enable(conn_rec *c)
239 SSLSrvConfigRec *sc = mySrvConfig(c->base_server);
241 SSLConnRec *sslconn = ssl_init_connection_ctx(c);
243 if (!sc->proxy_enabled) {
244 ap_log_error(APLOG_MARK, APLOG_ERR, 0, c->base_server,
245 "SSL Proxy requested for %s but not enabled "
246 "[Hint: SSLProxyEngine]", sc->vhost_id);
251 sslconn->is_proxy = 1;
252 sslconn->disabled = 0;
257 int ssl_engine_disable(conn_rec *c)
259 SSLSrvConfigRec *sc = mySrvConfig(c->base_server);
267 sslconn = ssl_init_connection_ctx(c);
269 sslconn->disabled = 1;
274 static int ssl_hook_pre_connection(conn_rec *c, void *csd)
276 SSLSrvConfigRec *sc = mySrvConfig(c->base_server);
278 SSLConnRec *sslconn = myConnConfig(c);
283 * Immediately stop processing if SSL is disabled for this connection
285 if (!(sc && (sc->enabled ||
286 (sslconn && sslconn->is_proxy))))
295 sslconn = ssl_init_connection_ctx(c);
298 if (sslconn->disabled) {
303 * Remember the connection information for
304 * later access inside callback functions
307 ap_log_error(APLOG_MARK, APLOG_INFO, 0, c->base_server,
308 "Connection to child %ld established "
309 "(server %s, client %s)", c->id, sc->vhost_id,
310 c->remote_ip ? c->remote_ip : "unknown");
313 * Seed the Pseudo Random Number Generator (PRNG)
315 ssl_rand_seed(c->base_server, c->pool, SSL_RSCTX_CONNECT, "");
317 mctx = sslconn->is_proxy ? sc->proxy : sc->server;
320 * Create a new SSL connection with the configured server SSL context and
321 * attach this to the socket. Additionally we register this attachment
322 * so we can detach later.
324 if (!(ssl = SSL_new(mctx->ssl_ctx))) {
325 ap_log_error(APLOG_MARK, APLOG_ERR, 0, c->base_server,
326 "Unable to create a new SSL connection from the SSL "
328 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, c->base_server);
332 return DECLINED; /* XXX */
335 vhost_md5 = ap_md5_binary(c->pool, (unsigned char *)sc->vhost_id,
338 if (!SSL_set_session_id_context(ssl, (unsigned char *)vhost_md5,
341 ap_log_error(APLOG_MARK, APLOG_ERR, 0, c->base_server,
342 "Unable to set session id context to `%s'", vhost_md5);
343 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, c->base_server);
347 return DECLINED; /* XXX */
350 SSL_set_app_data(ssl, c);
351 SSL_set_app_data2(ssl, NULL); /* will be request_rec */
356 * Configure callbacks for SSL connection
358 SSL_set_tmp_rsa_callback(ssl, ssl_callback_TmpRSA);
359 SSL_set_tmp_dh_callback(ssl, ssl_callback_TmpDH);
361 SSL_set_verify_result(ssl, X509_V_OK);
363 ssl_io_filter_init(c, ssl);
368 static const char *ssl_hook_http_method(const request_rec *r)
370 SSLSrvConfigRec *sc = mySrvConfig(r->server);
372 if (sc->enabled == FALSE) {
379 static apr_port_t ssl_hook_default_port(const request_rec *r)
381 SSLSrvConfigRec *sc = mySrvConfig(r->server);
383 if (sc->enabled == FALSE) {
391 * the module registration phase
394 static void ssl_register_hooks(apr_pool_t *p)
396 /* ssl_hook_ReadReq needs to use the BrowserMatch settings so must
397 * run after mod_setenvif's post_read_request hook. */
398 static const char *pre_prr[] = { "mod_setenvif.c", NULL };
400 ssl_io_filter_register(p);
402 ap_hook_pre_connection(ssl_hook_pre_connection,NULL,NULL, APR_HOOK_MIDDLE);
403 ap_hook_post_config (ssl_init_Module, NULL,NULL, APR_HOOK_MIDDLE);
404 ap_hook_http_method (ssl_hook_http_method, NULL,NULL, APR_HOOK_MIDDLE);
405 ap_hook_default_port (ssl_hook_default_port, NULL,NULL, APR_HOOK_MIDDLE);
406 ap_hook_pre_config (ssl_hook_pre_config, NULL,NULL, APR_HOOK_MIDDLE);
407 ap_hook_child_init (ssl_init_Child, NULL,NULL, APR_HOOK_MIDDLE);
408 ap_hook_check_user_id (ssl_hook_UserCheck, NULL,NULL, APR_HOOK_FIRST);
409 ap_hook_fixups (ssl_hook_Fixup, NULL,NULL, APR_HOOK_MIDDLE);
410 ap_hook_access_checker(ssl_hook_Access, NULL,NULL, APR_HOOK_MIDDLE);
411 ap_hook_auth_checker (ssl_hook_Auth, NULL,NULL, APR_HOOK_MIDDLE);
412 ap_hook_post_read_request(ssl_hook_ReadReq, pre_prr,NULL, APR_HOOK_MIDDLE);
416 APR_REGISTER_OPTIONAL_FN(ssl_proxy_enable);
417 APR_REGISTER_OPTIONAL_FN(ssl_engine_disable);
420 module AP_MODULE_DECLARE_DATA ssl_module = {
421 STANDARD20_MODULE_STUFF,
422 ssl_config_perdir_create, /* create per-dir config structures */
423 ssl_config_perdir_merge, /* merge per-dir config structures */
424 ssl_config_server_create, /* create per-server config structures */
425 ssl_config_server_merge, /* merge per-server config structures */
426 ssl_config_cmds, /* table of configuration directives */
427 ssl_register_hooks /* register hooks */