1 /* Copyright 2001-2005 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * 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 * The SSL engine kernel
26 /* ``It took me fifteen years to discover
27 I had no talent for programming, but
28 I couldn't give it up because by that
29 time I was too famous.''
31 #include "ssl_private.h"
33 static void ssl_configure_env(request_rec *r, SSLConnRec *sslconn);
36 * Post Read Request Handler
38 int ssl_hook_ReadReq(request_rec *r)
40 SSLConnRec *sslconn = myConnConfig(r->connection);
47 if (sslconn->non_ssl_request) {
51 int port = ap_get_server_port(r);
53 if (!ap_is_default_port(port, r)) {
54 thisport = apr_psprintf(r->pool, ":%u", port);
57 thisurl = ap_escape_html(r->pool,
58 apr_psprintf(r->pool, "https://%s%s/",
59 ap_get_server_name(r),
62 errmsg = apr_psprintf(r->pool,
63 "Reason: You're speaking plain HTTP "
64 "to an SSL-enabled server port.<br />\n"
65 "Instead use the HTTPS scheme to access "
66 "this URL, please.<br />\n"
68 "<a href=\"%s\"><b>%s</b></a></blockquote>",
71 apr_table_setn(r->notes, "error-notes", errmsg);
73 /* Now that we have caught this error, forget it. we are done
74 * with using SSL on this request.
76 sslconn->non_ssl_request = 0;
79 return HTTP_BAD_REQUEST;
83 * Get the SSL connection structure and perform the
84 * delayed interlinking from SSL back to request_rec
90 SSL_set_app_data2(ssl, r);
93 * Log information about incoming HTTPS requests
95 if (r->server->loglevel >= APLOG_INFO && ap_is_initial_req(r)) {
96 ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
97 "%s HTTPS request received for child %ld (server %s)",
98 (r->connection->keepalives <= 0 ?
100 apr_psprintf(r->pool, "Subsequent (No.%d)",
101 r->connection->keepalives+1)),
103 ssl_util_vhostid(r->pool, r->server));
106 /* SetEnvIf ssl-*-shutdown flags can only be per-server,
107 * so they won't change across keepalive requests
109 if (sslconn->shutdown_type == SSL_SHUTDOWN_TYPE_UNSET) {
110 ssl_configure_env(r, sslconn);
117 * Move SetEnvIf information from request_rec to conn_rec/BUFF
118 * to allow the close connection handler to use them.
121 static void ssl_configure_env(request_rec *r, SSLConnRec *sslconn)
124 const apr_array_header_t *arr = apr_table_elts(r->subprocess_env);
125 const apr_table_entry_t *elts = (const apr_table_entry_t *)arr->elts;
127 sslconn->shutdown_type = SSL_SHUTDOWN_TYPE_STANDARD;
129 for (i = 0; i < arr->nelts; i++) {
130 const char *key = elts[i].key;
134 /* being case-sensitive here.
135 * and not checking for the -shutdown since these are the only
136 * SetEnvIf "flags" we support
138 if (!strncmp(key+1, "sl-", 3)) {
140 if (!strncmp(key, "unclean", 7)) {
141 sslconn->shutdown_type = SSL_SHUTDOWN_TYPE_UNCLEAN;
143 else if (!strncmp(key, "accurate", 8)) {
144 sslconn->shutdown_type = SSL_SHUTDOWN_TYPE_ACCURATE;
146 return; /* should only ever be one ssl-*-shutdown */
156 int ssl_hook_Access(request_rec *r)
158 SSLDirConfigRec *dc = myDirConfig(r);
159 SSLSrvConfigRec *sc = mySrvConfig(r->server);
160 SSLConnRec *sslconn = myConnConfig(r->connection);
161 SSL *ssl = sslconn ? sslconn->ssl : NULL;
163 apr_array_header_t *requires;
164 ssl_require_t *ssl_requires;
167 BOOL renegotiate = FALSE, renegotiate_quick = FALSE;
170 X509_STORE *cert_store = NULL;
171 X509_STORE_CTX cert_store_ctx;
172 STACK_OF(SSL_CIPHER) *cipher_list_old = NULL, *cipher_list = NULL;
173 SSL_CIPHER *cipher = NULL;
174 int depth, verify_old, verify, n;
177 ctx = SSL_get_SSL_CTX(ssl);
181 * Support for SSLRequireSSL directive
183 if (dc->bSSLRequired && !ssl) {
184 if (sc->enabled == SSL_ENABLED_OPTIONAL) {
185 /* This vhost was configured for optional SSL, just tell the
186 * client that we need to upgrade.
188 apr_table_setn(r->err_headers_out, "Upgrade", "TLS/1.0, HTTP/1.1");
189 apr_table_setn(r->err_headers_out, "Connection", "Upgrade");
191 return HTTP_UPGRADE_REQUIRED;
194 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
195 "access to %s failed, reason: %s",
196 r->filename, "SSL connection required");
198 /* remember forbidden access for strict require option */
199 apr_table_setn(r->notes, "ssl-access-forbidden", "1");
201 return HTTP_FORBIDDEN;
205 * Check to see whether SSL is in use; if it's not, then no
206 * further access control checks are relevant. (the test for
207 * sc->enabled is probably strictly unnecessary)
209 if (sc->enabled == SSL_ENABLED_FALSE || !ssl) {
214 * Support for per-directory reconfigured SSL connection parameters.
216 * This is implemented by forcing an SSL renegotiation with the
217 * reconfigured parameter suite. But Apache's internal API processing
218 * makes our life very hard here, because when internal sub-requests occur
219 * we nevertheless should avoid multiple unnecessary SSL handshakes (they
220 * require extra network I/O and especially time to perform).
222 * But the optimization for filtering out the unnecessary handshakes isn't
223 * obvious and trivial. Especially because while Apache is in its
224 * sub-request processing the client could force additional handshakes,
225 * too. And these take place perhaps without our notice. So the only
226 * possibility is to explicitly _ask_ OpenSSL whether the renegotiation
227 * has to be performed or not. It has to performed when some parameters
228 * which were previously known (by us) are not those we've now
229 * reconfigured (as known by OpenSSL) or (in optimized way) at least when
230 * the reconfigured parameter suite is stronger (more restrictions) than
231 * the currently active one.
235 * Override of SSLCipherSuite
237 * We provide two options here:
239 * o The paranoid and default approach where we force a renegotiation when
240 * the cipher suite changed in _any_ way (which is straight-forward but
241 * often forces renegotiations too often and is perhaps not what the
242 * user actually wanted).
244 * o The optimized and still secure way where we force a renegotiation
245 * only if the currently active cipher is no longer contained in the
246 * reconfigured/new cipher suite. Any other changes are not important
247 * because it's the servers choice to select a cipher from the ones the
248 * client supports. So as long as the current cipher is still in the new
249 * cipher suite we're happy. Because we can assume we would have
250 * selected it again even when other (better) ciphers exists now in the
251 * new cipher suite. This approach is fine because the user explicitly
252 * has to enable this via ``SSLOptions +OptRenegotiate''. So we do no
253 * implicit optimizations.
255 if (dc->szCipherSuite) {
256 /* remember old state */
258 if (dc->nOptions & SSL_OPT_OPTRENEGOTIATE) {
259 cipher = SSL_get_current_cipher(ssl);
262 cipher_list_old = (STACK_OF(SSL_CIPHER) *)SSL_get_ciphers(ssl);
264 if (cipher_list_old) {
265 cipher_list_old = sk_SSL_CIPHER_dup(cipher_list_old);
269 /* configure new state */
270 if (!modssl_set_cipher_list(ssl, dc->szCipherSuite)) {
271 ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
273 "Unable to reconfigure (per-directory) "
274 "permitted SSL ciphers");
275 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, r->server);
277 if (cipher_list_old) {
278 sk_SSL_CIPHER_free(cipher_list_old);
281 return HTTP_FORBIDDEN;
284 /* determine whether a renegotiation has to be forced */
285 cipher_list = (STACK_OF(SSL_CIPHER) *)SSL_get_ciphers(ssl);
287 if (dc->nOptions & SSL_OPT_OPTRENEGOTIATE) {
289 if ((!cipher && cipher_list) ||
290 (cipher && !cipher_list))
294 else if (cipher && cipher_list &&
295 (sk_SSL_CIPHER_find(cipher_list, cipher) < 0))
302 if ((!cipher_list_old && cipher_list) ||
303 (cipher_list_old && !cipher_list))
307 else if (cipher_list_old && cipher_list) {
309 !renegotiate && (n < sk_SSL_CIPHER_num(cipher_list));
312 SSL_CIPHER *value = sk_SSL_CIPHER_value(cipher_list, n);
314 if (sk_SSL_CIPHER_find(cipher_list_old, value) < 0) {
320 !renegotiate && (n < sk_SSL_CIPHER_num(cipher_list_old));
323 SSL_CIPHER *value = sk_SSL_CIPHER_value(cipher_list_old, n);
325 if (sk_SSL_CIPHER_find(cipher_list, value) < 0) {
333 if (cipher_list_old) {
334 sk_SSL_CIPHER_free(cipher_list_old);
339 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
340 "Reconfigured cipher suite will force renegotiation");
345 * override of SSLVerifyDepth
347 * The depth checks are handled by us manually inside the verify callback
348 * function and not by OpenSSL internally (and our function is aware of
349 * both the per-server and per-directory contexts). So we cannot ask
350 * OpenSSL about the currently verify depth. Instead we remember it in our
351 * ap_ctx attached to the SSL* of OpenSSL. We've to force the
352 * renegotiation if the reconfigured/new verify depth is less than the
353 * currently active/remembered verify depth (because this means more
354 * restriction on the certificate chain).
356 if (dc->nVerifyDepth != UNSET) {
357 /* XXX: doesnt look like sslconn->verify_depth is actually used */
358 if (!(n = sslconn->verify_depth)) {
359 sslconn->verify_depth = n = sc->server->auth.verify_depth;
362 /* determine whether a renegotiation has to be forced */
363 if (dc->nVerifyDepth < n) {
365 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
366 "Reduced client verification depth will force "
372 * override of SSLVerifyClient
374 * We force a renegotiation if the reconfigured/new verify type is
375 * stronger than the currently active verify type.
377 * The order is: none << optional_no_ca << optional << require
379 * Additionally the following optimization is possible here: When the
380 * currently active verify type is "none" but a client certificate is
381 * already known/present, it's enough to manually force a client
382 * verification but at least skip the I/O-intensive renegotation
385 if (dc->nVerifyClient != SSL_CVERIFY_UNSET) {
386 /* remember old state */
387 verify_old = SSL_get_verify_mode(ssl);
388 /* configure new state */
389 verify = SSL_VERIFY_NONE;
391 if (dc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
392 verify |= SSL_VERIFY_PEER_STRICT;
395 if ((dc->nVerifyClient == SSL_CVERIFY_OPTIONAL) ||
396 (dc->nVerifyClient == SSL_CVERIFY_OPTIONAL_NO_CA))
398 verify |= SSL_VERIFY_PEER;
401 modssl_set_verify(ssl, verify, ssl_callback_SSLVerify);
402 SSL_set_verify_result(ssl, X509_V_OK);
404 /* determine whether we've to force a renegotiation */
405 if (!renegotiate && verify != verify_old) {
406 if (((verify_old == SSL_VERIFY_NONE) &&
407 (verify != SSL_VERIFY_NONE)) ||
409 (!(verify_old & SSL_VERIFY_PEER) &&
410 (verify & SSL_VERIFY_PEER)) ||
412 (!(verify_old & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) &&
413 (verify & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
418 if ((dc->nOptions & SSL_OPT_OPTRENEGOTIATE) &&
419 (verify_old == SSL_VERIFY_NONE) &&
420 ((peercert = SSL_get_peer_certificate(ssl)) != NULL))
422 renegotiate_quick = TRUE;
426 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
428 "Changed client verification type will force "
430 renegotiate_quick ? "quick " : "");
436 * override SSLCACertificateFile & SSLCACertificatePath
437 * This is only enabled if the SSL_set_cert_store() function
438 * is available in the ssl library. the 1.x based mod_ssl
439 * used SSL_CTX_set_cert_store which is not thread safe.
442 #ifdef HAVE_SSL_SET_CERT_STORE
444 * check if per-dir and per-server config field are not the same.
445 * if f is defined in per-dir and not defined in per-server
446 * or f is defined in both but not the equal ...
448 #define MODSSL_CFG_NE(f) \
449 (dc->f && (!sc->f || (sc->f && strNE(dc->f, sc->f))))
451 #define MODSSL_CFG_CA(f) \
452 (dc->f ? dc->f : sc->f)
454 if (MODSSL_CFG_NE(szCACertificateFile) ||
455 MODSSL_CFG_NE(szCACertificatePath))
457 STACK_OF(X509_NAME) *ca_list;
458 const char *ca_file = MODSSL_CFG_CA(szCACertificateFile);
459 const char *ca_path = MODSSL_CFG_CA(szCACertificatePath);
461 cert_store = X509_STORE_new();
463 if (!X509_STORE_load_locations(cert_store, ca_file, ca_path)) {
464 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
465 "Unable to reconfigure verify locations "
466 "for client authentication");
467 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, r->server);
469 X509_STORE_free(cert_store);
471 return HTTP_FORBIDDEN;
474 /* SSL_free will free cert_store */
475 SSL_set_cert_store(ssl, cert_store);
477 if (!(ca_list = ssl_init_FindCAList(r->server, r->pool,
480 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
481 "Unable to determine list of available "
482 "CA certificates for client authentication");
484 return HTTP_FORBIDDEN;
487 SSL_set_client_CA_list(ssl, ca_list);
490 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
491 "Changed client verification locations will force "
494 #endif /* HAVE_SSL_SET_CERT_STORE */
496 /* If a renegotiation is now required for this location, and the
497 * request includes a message body (and the client has not
498 * requested a "100 Continue" response), then the client will be
499 * streaming the request body over the wire already. In that
500 * case, it is not possible to stop and perform a new SSL
501 * handshake immediately; once the SSL library moves to the
502 * "accept" state, it will reject the SSL packets which the client
503 * is sending for the request body.
505 * To allow authentication to complete in this auth hook, the
506 * solution used here is to fill a (bounded) buffer with the
507 * request body, and then to reinject that request body later.
509 if (renegotiate && !renegotiate_quick
510 && (apr_table_get(r->headers_in, "transfer-encoding")
511 || (apr_table_get(r->headers_in, "content-length")
512 && strcmp(apr_table_get(r->headers_in, "content-length"), "0")))
513 && !r->expecting_100) {
516 /* Fill the I/O buffer with the request body if possible. */
517 rv = ssl_io_buffer_fill(r);
520 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
521 "could not buffer message body to allow "
522 "SSL renegotiation to proceed");
528 * now do the renegotiation if anything was actually reconfigured
532 * Now we force the SSL renegotation by sending the Hello Request
533 * message to the client. Here we have to do a workaround: Actually
534 * OpenSSL returns immediately after sending the Hello Request (the
535 * intent AFAIK is because the SSL/TLS protocol says it's not a must
536 * that the client replies to a Hello Request). But because we insist
537 * on a reply (anything else is an error for us) we have to go to the
538 * ACCEPT state manually. Using SSL_set_accept_state() doesn't work
539 * here because it resets too much of the connection. So we set the
540 * state explicitly and continue the handshake manually.
542 ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
543 "Requesting connection re-negotiation");
545 if (renegotiate_quick) {
546 STACK_OF(X509) *cert_stack;
548 /* perform just a manual re-verification of the peer */
549 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
550 "Performing quick renegotiation: "
551 "just re-verifying the peer");
553 cert_stack = (STACK_OF(X509) *)SSL_get_peer_cert_chain(ssl);
555 cert = SSL_get_peer_certificate(ssl);
557 if (!cert_stack && cert) {
558 /* client cert is in the session cache, but there is
559 * no chain, since ssl3_get_client_certificate()
560 * sk_X509_shift-ed the peer cert out of the chain.
561 * we put it back here for the purpose of quick_renegotiation.
563 cert_stack = sk_new_null();
564 sk_X509_push(cert_stack, MODSSL_PCHAR_CAST cert);
567 if (!cert_stack || (sk_X509_num(cert_stack) == 0)) {
568 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
569 "Cannot find peer certificate chain");
571 return HTTP_FORBIDDEN;
575 (cert_store = SSL_CTX_get_cert_store(ctx))))
577 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
578 "Cannot find certificate storage");
580 return HTTP_FORBIDDEN;
584 cert = sk_X509_value(cert_stack, 0);
587 X509_STORE_CTX_init(&cert_store_ctx, cert_store, cert, cert_stack);
588 depth = SSL_get_verify_depth(ssl);
591 X509_STORE_CTX_set_depth(&cert_store_ctx, depth);
594 X509_STORE_CTX_set_ex_data(&cert_store_ctx,
595 SSL_get_ex_data_X509_STORE_CTX_idx(),
598 if (!modssl_X509_verify_cert(&cert_store_ctx)) {
599 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
600 "Re-negotiation verification step failed");
601 ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, r->server);
604 SSL_set_verify_result(ssl, cert_store_ctx.error);
605 X509_STORE_CTX_cleanup(&cert_store_ctx);
607 if (cert_stack != SSL_get_peer_cert_chain(ssl)) {
608 /* we created this ourselves, so free it */
609 sk_X509_pop_free(cert_stack, X509_free);
613 request_rec *id = r->main ? r->main : r;
615 /* do a full renegotiation */
616 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
617 "Performing full renegotiation: "
618 "complete handshake protocol");
620 SSL_set_session_id_context(ssl,
621 (unsigned char *)&id,
624 SSL_renegotiate(ssl);
625 SSL_do_handshake(ssl);
627 if (SSL_get_state(ssl) != SSL_ST_OK) {
628 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
629 "Re-negotiation request failed");
631 r->connection->aborted = 1;
632 return HTTP_FORBIDDEN;
635 ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
636 "Awaiting re-negotiation handshake");
638 /* XXX: Should replace SSL_set_state with SSL_renegotiate(ssl);
639 * However, this causes failures in perl-framework currently,
640 * perhaps pre-test if we have already negotiated?
642 SSL_set_state(ssl, SSL_ST_ACCEPT);
643 SSL_do_handshake(ssl);
645 if (SSL_get_state(ssl) != SSL_ST_OK) {
646 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
647 "Re-negotiation handshake failed: "
648 "Not accepted by client!?");
650 r->connection->aborted = 1;
651 return HTTP_FORBIDDEN;
656 * Remember the peer certificate's DN
658 if ((cert = SSL_get_peer_certificate(ssl))) {
659 if (sslconn->client_cert) {
660 X509_free(sslconn->client_cert);
662 sslconn->client_cert = cert;
663 sslconn->client_dn = NULL;
667 * Finally check for acceptable renegotiation results
669 if (dc->nVerifyClient != SSL_CVERIFY_NONE) {
670 BOOL do_verify = (dc->nVerifyClient == SSL_CVERIFY_REQUIRE);
672 if (do_verify && (SSL_get_verify_result(ssl) != X509_V_OK)) {
673 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
674 "Re-negotiation handshake failed: "
675 "Client verification failed");
677 return HTTP_FORBIDDEN;
681 if ((peercert = SSL_get_peer_certificate(ssl)) == NULL) {
682 ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
683 "Re-negotiation handshake failed: "
684 "Client certificate missing");
686 return HTTP_FORBIDDEN;
694 * Also check that SSLCipherSuite has been enforced as expected.
697 cipher = SSL_get_current_cipher(ssl);
698 if (sk_SSL_CIPHER_find(cipher_list, cipher) < 0) {
699 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
700 "SSL cipher suite not renegotiated: "
701 "access to %s denied using cipher %s",
703 SSL_CIPHER_get_name(cipher));
704 return HTTP_FORBIDDEN;
709 /* If we're trying to have the user name set from a client
710 * certificate then we need to set it here. This should be safe as
711 * the user name probably isn't important from an auth checking point
712 * of view as the certificate supplied acts in that capacity.
713 * However, if FakeAuth is being used then this isn't the case so
714 * we need to postpone setting the username until later.
716 if ((dc->nOptions & SSL_OPT_FAKEBASICAUTH) == 0 && dc->szUserName) {
717 char *val = ssl_var_lookup(r->pool, r->server, r->connection,
718 r, (char *)dc->szUserName);
722 ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r,
723 "Failed to set r->user to '%s'", dc->szUserName);
727 * Check SSLRequire boolean expressions
729 requires = dc->aRequirement;
730 ssl_requires = (ssl_require_t *)requires->elts;
732 for (i = 0; i < requires->nelts; i++) {
733 ssl_require_t *req = &ssl_requires[i];
734 ok = ssl_expr_exec(r, req->mpExpr);
737 cp = apr_psprintf(r->pool,
739 "SSL requirement expression: %s",
740 ssl_expr_get_error());
742 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
743 "access to %s failed, reason: %s",
746 /* remember forbidden access for strict require option */
747 apr_table_setn(r->notes, "ssl-access-forbidden", "1");
749 return HTTP_FORBIDDEN;
753 ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
754 "Access to %s denied for %s "
755 "(requirement expression not fulfilled)",
756 r->filename, r->connection->remote_ip);
758 ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
759 "Failed expression: %s", req->cpExpr);
761 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
762 "access to %s failed, reason: %s",
764 "SSL requirement expression not fulfilled "
765 "(see SSL logfile for more details)");
767 /* remember forbidden access for strict require option */
768 apr_table_setn(r->notes, "ssl-access-forbidden", "1");
770 return HTTP_FORBIDDEN;
775 * Else access is granted from our point of view (except vendor
776 * handlers override). But we have to return DECLINED here instead
777 * of OK, because mod_auth and other modules still might want to
785 * Authentication Handler:
786 * Fake a Basic authentication from the X509 client certificate.
788 * This must be run fairly early on to prevent a real authentication from
789 * occuring, in particular it must be run before anything else that
790 * authenticates a user. This means that the Module statement for this
791 * module should be LAST in the Configuration file.
793 int ssl_hook_UserCheck(request_rec *r)
795 SSLConnRec *sslconn = myConnConfig(r->connection);
796 SSLSrvConfigRec *sc = mySrvConfig(r->server);
797 SSLDirConfigRec *dc = myDirConfig(r);
799 const char *auth_line, *username, *password;
802 * Additionally forbid access (again)
803 * when strict require option is used.
805 if ((dc->nOptions & SSL_OPT_STRICTREQUIRE) &&
806 (apr_table_get(r->notes, "ssl-access-forbidden")))
808 return HTTP_FORBIDDEN;
812 * We decline when we are in a subrequest. The Authorization header
813 * would already be present if it was added in the main request.
815 if (!ap_is_initial_req(r)) {
820 * Make sure the user is not able to fake the client certificate
821 * based authentication by just entering an X.509 Subject DN
822 * ("/XX=YYY/XX=YYY/..") as the username and "password" as the
825 if ((auth_line = apr_table_get(r->headers_in, "Authorization"))) {
826 if (strcEQ(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
827 while ((*auth_line == ' ') || (*auth_line == '\t')) {
831 auth_line = ap_pbase64decode(r->pool, auth_line);
832 username = ap_getword_nulls(r->pool, &auth_line, ':');
833 password = auth_line;
835 if ((username[0] == '/') && strEQ(password, "password")) {
836 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
837 "Encountered FakeBasicAuth spoof: %s", username);
838 return HTTP_FORBIDDEN;
844 * We decline operation in various situations...
845 * - SSLOptions +FakeBasicAuth not configured
846 * - r->user already authenticated
848 * - client did not present a certificate
850 if (!((sc->enabled == SSL_ENABLED_TRUE || sc->enabled == SSL_ENABLED_OPTIONAL)
851 && sslconn && sslconn->ssl && sslconn->client_cert) ||
852 !(dc->nOptions & SSL_OPT_FAKEBASICAUTH) || r->user)
857 if (!sslconn->client_dn) {
858 X509_NAME *name = X509_get_subject_name(sslconn->client_cert);
859 char *cp = X509_NAME_oneline(name, NULL, 0);
860 sslconn->client_dn = apr_pstrdup(r->connection->pool, cp);
864 clientdn = (char *)sslconn->client_dn;
867 * Fake a password - which one would be immaterial, as, it seems, an empty
868 * password in the users file would match ALL incoming passwords, if only
869 * we were using the standard crypt library routine. Unfortunately, OpenSSL
870 * "fixes" a "bug" in crypt and thus prevents blank passwords from
871 * working. (IMHO what they really fix is a bug in the users of the code
872 * - failing to program correctly for shadow passwords). We need,
873 * therefore, to provide a password. This password can be matched by
874 * adding the string "xxj31ZMTZzkVA" as the password in the user file.
875 * This is just the crypted variant of the word "password" ;-)
877 auth_line = apr_pstrcat(r->pool, "Basic ",
878 ap_pbase64encode(r->pool,
879 apr_pstrcat(r->pool, clientdn,
882 apr_table_set(r->headers_in, "Authorization", auth_line);
884 ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
885 "Faking HTTP Basic Auth header: \"Authorization: %s\"",
891 /* authorization phase */
892 int ssl_hook_Auth(request_rec *r)
894 SSLDirConfigRec *dc = myDirConfig(r);
897 * Additionally forbid access (again)
898 * when strict require option is used.
900 if ((dc->nOptions & SSL_OPT_STRICTREQUIRE) &&
901 (apr_table_get(r->notes, "ssl-access-forbidden")))
903 return HTTP_FORBIDDEN;
913 static const char *ssl_hook_Fixup_vars[] = {
914 "SSL_VERSION_INTERFACE",
915 "SSL_VERSION_LIBRARY",
917 "SSL_COMPRESS_METHOD",
920 "SSL_CIPHER_USEKEYSIZE",
921 "SSL_CIPHER_ALGKEYSIZE",
923 "SSL_CLIENT_M_VERSION",
924 "SSL_CLIENT_M_SERIAL",
925 "SSL_CLIENT_V_START",
927 "SSL_CLIENT_V_REMAIN",
930 "SSL_CLIENT_S_DN_ST",
933 "SSL_CLIENT_S_DN_OU",
934 "SSL_CLIENT_S_DN_CN",
940 "SSL_CLIENT_S_DN_UID",
941 "SSL_CLIENT_S_DN_Email",
944 "SSL_CLIENT_I_DN_ST",
947 "SSL_CLIENT_I_DN_OU",
948 "SSL_CLIENT_I_DN_CN",
954 "SSL_CLIENT_I_DN_UID",
955 "SSL_CLIENT_I_DN_Email",
958 "SSL_SERVER_M_VERSION",
959 "SSL_SERVER_M_SERIAL",
960 "SSL_SERVER_V_START",
964 "SSL_SERVER_S_DN_ST",
967 "SSL_SERVER_S_DN_OU",
968 "SSL_SERVER_S_DN_CN",
974 "SSL_SERVER_S_DN_UID",
975 "SSL_SERVER_S_DN_Email",
978 "SSL_SERVER_I_DN_ST",
981 "SSL_SERVER_I_DN_OU",
982 "SSL_SERVER_I_DN_CN",
988 "SSL_SERVER_I_DN_UID",
989 "SSL_SERVER_I_DN_Email",
996 int ssl_hook_Fixup(request_rec *r)
998 SSLConnRec *sslconn = myConnConfig(r->connection);
999 SSLSrvConfigRec *sc = mySrvConfig(r->server);
1000 SSLDirConfigRec *dc = myDirConfig(r);
1001 apr_table_t *env = r->subprocess_env;
1002 char *var, *val = "";
1003 STACK_OF(X509) *peer_certs;
1007 if (sc->enabled == SSL_ENABLED_OPTIONAL && !(sslconn && sslconn->ssl)) {
1008 apr_table_setn(r->headers_out, "Upgrade", "TLS/1.0, HTTP/1.1");
1012 * Check to see if SSL is on
1014 if (!(((sc->enabled == SSL_ENABLED_TRUE) || (sc->enabled == SSL_ENABLED_OPTIONAL)) && sslconn && (ssl = sslconn->ssl))) {
1019 * Annotate the SSI/CGI environment with standard SSL information
1021 /* the always present HTTPS (=HTTP over SSL) flag! */
1022 apr_table_setn(env, "HTTPS", "on");
1024 /* standard SSL environment variables */
1025 if (dc->nOptions & SSL_OPT_STDENVVARS) {
1026 for (i = 0; ssl_hook_Fixup_vars[i]; i++) {
1027 var = (char *)ssl_hook_Fixup_vars[i];
1028 val = ssl_var_lookup(r->pool, r->server, r->connection, r, var);
1029 if (!strIsEmpty(val)) {
1030 apr_table_setn(env, var, val);
1036 * On-demand bloat up the SSI/CGI environment with certificate data
1038 if (dc->nOptions & SSL_OPT_EXPORTCERTDATA) {
1039 val = ssl_var_lookup(r->pool, r->server, r->connection,
1040 r, "SSL_SERVER_CERT");
1042 apr_table_setn(env, "SSL_SERVER_CERT", val);
1044 val = ssl_var_lookup(r->pool, r->server, r->connection,
1045 r, "SSL_CLIENT_CERT");
1047 apr_table_setn(env, "SSL_CLIENT_CERT", val);
1049 if ((peer_certs = (STACK_OF(X509) *)SSL_get_peer_cert_chain(ssl))) {
1050 for (i = 0; i < sk_X509_num(peer_certs); i++) {
1051 var = apr_psprintf(r->pool, "SSL_CLIENT_CERT_CHAIN_%d", i);
1052 val = ssl_var_lookup(r->pool, r->server, r->connection,
1055 apr_table_setn(env, var, val);
1064 /* _________________________________________________________________
1066 ** OpenSSL Callback Functions
1067 ** _________________________________________________________________
1071 * Handle out temporary RSA private keys on demand
1073 * The background of this as the TLSv1 standard explains it:
1075 * | D.1. Temporary RSA keys
1077 * | US Export restrictions limit RSA keys used for encryption to 512
1078 * | bits, but do not place any limit on lengths of RSA keys used for
1079 * | signing operations. Certificates often need to be larger than 512
1080 * | bits, since 512-bit RSA keys are not secure enough for high-value
1081 * | transactions or for applications requiring long-term security. Some
1082 * | certificates are also designated signing-only, in which case they
1083 * | cannot be used for key exchange.
1085 * | When the public key in the certificate cannot be used for encryption,
1086 * | the server signs a temporary RSA key, which is then exchanged. In
1087 * | exportable applications, the temporary RSA key should be the maximum
1088 * | allowable length (i.e., 512 bits). Because 512-bit RSA keys are
1089 * | relatively insecure, they should be changed often. For typical
1090 * | electronic commerce applications, it is suggested that keys be
1091 * | changed daily or every 500 transactions, and more often if possible.
1092 * | Note that while it is acceptable to use the same temporary key for
1093 * | multiple transactions, it must be signed each time it is used.
1095 * | RSA key generation is a time-consuming process. In many cases, a
1096 * | low-priority process can be assigned the task of key generation.
1097 * | Whenever a new key is completed, the existing temporary key can be
1098 * | replaced with the new one.
1100 * XXX: base on comment above, if thread support is enabled,
1101 * we should spawn a low-priority thread to generate new keys
1104 * So we generated 512 and 1024 bit temporary keys on startup
1105 * which we now just hand out on demand....
1108 RSA *ssl_callback_TmpRSA(SSL *ssl, int export, int keylen)
1110 conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
1111 SSLModConfigRec *mc = myModConfig(c->base_server);
1114 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c,
1115 "handing out temporary %d bit RSA key", keylen);
1117 /* doesn't matter if export flag is on,
1118 * we won't be asked for keylen > 512 in that case.
1119 * if we are asked for a keylen > 1024, it is too expensive
1120 * to generate on the fly.
1121 * XXX: any reason not to generate 2048 bit keys at startup?
1126 idx = SSL_TMP_KEY_RSA_512;
1131 idx = SSL_TMP_KEY_RSA_1024;
1134 return (RSA *)mc->pTmpKeys[idx];
1138 * Hand out the already generated DH parameters...
1140 DH *ssl_callback_TmpDH(SSL *ssl, int export, int keylen)
1142 conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
1143 SSLModConfigRec *mc = myModConfig(c->base_server);
1146 ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c,
1147 "handing out temporary %d bit DH key", keylen);
1151 idx = SSL_TMP_KEY_DH_512;
1156 idx = SSL_TMP_KEY_DH_1024;
1159 return (DH *)mc->pTmpKeys[idx];
1163 * This OpenSSL callback function is called when OpenSSL
1164 * does client authentication and verifies the certificate chain.
1166 int ssl_callback_SSLVerify(int ok, X509_STORE_CTX *ctx)
1168 /* Get Apache context back through OpenSSL context */
1169 SSL *ssl = X509_STORE_CTX_get_ex_data(ctx,
1170 SSL_get_ex_data_X509_STORE_CTX_idx());
1171 conn_rec *conn = (conn_rec *)SSL_get_app_data(ssl);
1172 server_rec *s = conn->base_server;
1173 request_rec *r = (request_rec *)SSL_get_app_data2(ssl);
1175 SSLSrvConfigRec *sc = mySrvConfig(s);
1176 SSLDirConfigRec *dc = r ? myDirConfig(r) : NULL;
1177 SSLConnRec *sslconn = myConnConfig(conn);
1178 modssl_ctx_t *mctx = myCtxConfig(sslconn, sc);
1180 /* Get verify ingredients */
1181 int errnum = X509_STORE_CTX_get_error(ctx);
1182 int errdepth = X509_STORE_CTX_get_error_depth(ctx);
1186 * Log verification information
1188 if (s->loglevel >= APLOG_DEBUG) {
1189 X509 *cert = X509_STORE_CTX_get_current_cert(ctx);
1190 char *sname = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
1191 char *iname = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
1193 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1194 "Certificate Verification: "
1195 "depth: %d, subject: %s, issuer: %s",
1197 sname ? sname : "-unknown-",
1198 iname ? iname : "-unknown-");
1210 * Check for optionally acceptable non-verifiable issuer situation
1212 if (dc && (dc->nVerifyClient != SSL_CVERIFY_UNSET)) {
1213 verify = dc->nVerifyClient;
1216 verify = mctx->auth.verify_mode;
1219 if (verify == SSL_CVERIFY_NONE) {
1221 * SSLProxyVerify is either not configured or set to "none".
1222 * (this callback doesn't happen in the server context if SSLVerify
1223 * is not configured or set to "none")
1228 if (ssl_verify_error_is_optional(errnum) &&
1229 (verify == SSL_CVERIFY_OPTIONAL_NO_CA))
1231 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1232 "Certificate Verification: Verifiable Issuer is "
1233 "configured as optional, therefore we're accepting "
1236 sslconn->verify_info = "GENEROUS";
1241 * Additionally perform CRL-based revocation checks
1244 if (!(ok = ssl_callback_SSLVerify_CRL(ok, ctx, conn))) {
1245 errnum = X509_STORE_CTX_get_error(ctx);
1250 * If we already know it's not ok, log the real reason
1253 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
1254 "Certificate Verification: Error (%d): %s",
1255 errnum, X509_verify_cert_error_string(errnum));
1257 if (sslconn->client_cert) {
1258 X509_free(sslconn->client_cert);
1259 sslconn->client_cert = NULL;
1261 sslconn->client_dn = NULL;
1262 sslconn->verify_error = X509_verify_cert_error_string(errnum);
1266 * Finally check the depth of the certificate verification
1268 if (dc && (dc->nVerifyDepth != UNSET)) {
1269 depth = dc->nVerifyDepth;
1272 depth = mctx->auth.verify_depth;
1275 if (errdepth > depth) {
1276 ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
1277 "Certificate Verification: Certificate Chain too long "
1278 "(chain has %d certificates, but maximum allowed are "
1282 errnum = X509_V_ERR_CERT_CHAIN_TOO_LONG;
1283 sslconn->verify_error = X509_verify_cert_error_string(errnum);
1289 * And finally signal OpenSSL the (perhaps changed) state
1294 int ssl_callback_SSLVerify_CRL(int ok, X509_STORE_CTX *ctx, conn_rec *c)
1296 server_rec *s = c->base_server;
1297 SSLSrvConfigRec *sc = mySrvConfig(s);
1298 SSLConnRec *sslconn = myConnConfig(c);
1299 modssl_ctx_t *mctx = myCtxConfig(sslconn, sc);
1301 X509_NAME *subject, *issuer;
1308 * Unless a revocation store for CRLs was created we
1309 * cannot do any CRL-based verification, of course.
1316 * Determine certificate ingredients in advance
1318 cert = X509_STORE_CTX_get_current_cert(ctx);
1319 subject = X509_get_subject_name(cert);
1320 issuer = X509_get_issuer_name(cert);
1323 * OpenSSL provides the general mechanism to deal with CRLs but does not
1324 * use them automatically when verifying certificates, so we do it
1325 * explicitly here. We will check the CRL for the currently checked
1326 * certificate, if there is such a CRL in the store.
1328 * We come through this procedure for each certificate in the certificate
1329 * chain, starting with the root-CA's certificate. At each step we've to
1330 * both verify the signature on the CRL (to make sure it's a valid CRL)
1331 * and it's revocation list (to make sure the current certificate isn't
1332 * revoked). But because to check the signature on the CRL we need the
1333 * public key of the issuing CA certificate (which was already processed
1334 * one round before), we've a little problem. But we can both solve it and
1335 * at the same time optimize the processing by using the following
1336 * verification scheme (idea and code snippets borrowed from the GLOBUS
1339 * 1. We'll check the signature of a CRL in each step when we find a CRL
1340 * through the _subject_ name of the current certificate. This CRL
1341 * itself will be needed the first time in the next round, of course.
1342 * But we do the signature processing one round before this where the
1343 * public key of the CA is available.
1345 * 2. We'll check the revocation list of a CRL in each step when
1346 * we find a CRL through the _issuer_ name of the current certificate.
1347 * This CRLs signature was then already verified one round before.
1349 * This verification scheme allows a CA to revoke its own certificate as
1354 * Try to retrieve a CRL corresponding to the _subject_ of
1355 * the current certificate in order to verify it's integrity.
1357 memset((char *)&obj, 0, sizeof(obj));
1358 rc = SSL_X509_STORE_lookup(mctx->crl,
1359 X509_LU_CRL, subject, &obj);
1362 if ((rc > 0) && crl) {
1364 * Log information about CRL
1365 * (A little bit complicated because of ASN.1 and BIOs...)
1367 if (s->loglevel >= APLOG_DEBUG) {
1368 char buff[512]; /* should be plenty */
1369 BIO *bio = BIO_new(BIO_s_mem());
1371 BIO_printf(bio, "CA CRL: Issuer: ");
1372 X509_NAME_print(bio, issuer, 0);
1374 BIO_printf(bio, ", lastUpdate: ");
1375 ASN1_UTCTIME_print(bio, X509_CRL_get_lastUpdate(crl));
1377 BIO_printf(bio, ", nextUpdate: ");
1378 ASN1_UTCTIME_print(bio, X509_CRL_get_nextUpdate(crl));
1380 n = BIO_read(bio, buff, sizeof(buff) - 1);
1385 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "%s", buff);
1389 * Verify the signature on this CRL
1391 pubkey = X509_get_pubkey(cert);
1392 rc = X509_CRL_verify(crl, pubkey);
1393 #ifdef OPENSSL_VERSION_NUMBER
1394 /* Only refcounted in OpenSSL */
1396 EVP_PKEY_free(pubkey);
1399 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
1400 "Invalid signature on CRL");
1402 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
1403 X509_OBJECT_free_contents(&obj);
1408 * Check date of CRL to make sure it's not expired
1410 i = X509_cmp_current_time(X509_CRL_get_nextUpdate(crl));
1413 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
1414 "Found CRL has invalid nextUpdate field");
1416 X509_STORE_CTX_set_error(ctx,
1417 X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
1418 X509_OBJECT_free_contents(&obj);
1424 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
1425 "Found CRL is expired - "
1426 "revoking all certificates until you get updated CRL");
1428 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_HAS_EXPIRED);
1429 X509_OBJECT_free_contents(&obj);
1434 X509_OBJECT_free_contents(&obj);
1438 * Try to retrieve a CRL corresponding to the _issuer_ of
1439 * the current certificate in order to check for revocation.
1441 memset((char *)&obj, 0, sizeof(obj));
1442 rc = SSL_X509_STORE_lookup(mctx->crl,
1443 X509_LU_CRL, issuer, &obj);
1446 if ((rc > 0) && crl) {
1448 * Check if the current certificate is revoked by this CRL
1450 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
1452 for (i = 0; i < n; i++) {
1453 X509_REVOKED *revoked =
1454 sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
1456 ASN1_INTEGER *sn = X509_REVOKED_get_serialNumber(revoked);
1458 if (!ASN1_INTEGER_cmp(sn, X509_get_serialNumber(cert))) {
1459 if (s->loglevel >= APLOG_DEBUG) {
1460 char *cp = X509_NAME_oneline(issuer, NULL, 0);
1461 long serial = ASN1_INTEGER_get(sn);
1463 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
1464 "Certificate with serial %ld (0x%lX) "
1465 "revoked per CRL from issuer %s",
1466 serial, serial, cp);
1470 X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REVOKED);
1471 X509_OBJECT_free_contents(&obj);
1477 X509_OBJECT_free_contents(&obj);
1483 #define SSLPROXY_CERT_CB_LOG_FMT \
1484 "Proxy client certificate callback: (%s) "
1486 static void modssl_proxy_info_log(server_rec *s,
1490 SSLSrvConfigRec *sc = mySrvConfig(s);
1495 if (s->loglevel < APLOG_DEBUG) {
1499 name = X509_get_subject_name(info->x509);
1500 dn = X509_NAME_oneline(name, name_buf, sizeof(name_buf));
1502 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1503 SSLPROXY_CERT_CB_LOG_FMT "%s, sending %s",
1504 sc->vhost_id, msg, dn ? dn : "-uknown-");
1508 * caller will decrement the cert and key reference
1509 * so we need to increment here to prevent them from
1512 #define modssl_set_cert_info(info, cert, pkey) \
1513 *cert = info->x509; \
1514 X509_reference_inc(*cert); \
1515 *pkey = info->x_pkey->dec_pkey; \
1516 EVP_PKEY_reference_inc(*pkey)
1518 int ssl_callback_proxy_cert(SSL *ssl, MODSSL_CLIENT_CERT_CB_ARG_TYPE **x509, EVP_PKEY **pkey)
1520 conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
1521 server_rec *s = c->base_server;
1522 SSLSrvConfigRec *sc = mySrvConfig(s);
1523 X509_NAME *ca_name, *issuer;
1525 STACK_OF(X509_NAME) *ca_list;
1526 STACK_OF(X509_INFO) *certs = sc->proxy->pkp->certs;
1529 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1530 SSLPROXY_CERT_CB_LOG_FMT "entered",
1533 if (!certs || (sk_X509_INFO_num(certs) <= 0)) {
1534 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
1535 SSLPROXY_CERT_CB_LOG_FMT
1536 "downstream server wanted client certificate "
1537 "but none are configured", sc->vhost_id);
1541 ca_list = SSL_get_client_CA_list(ssl);
1543 if (!ca_list || (sk_X509_NAME_num(ca_list) <= 0)) {
1545 * downstream server didn't send us a list of acceptable CA certs,
1546 * so we send the first client cert in the list.
1548 info = sk_X509_INFO_value(certs, 0);
1550 modssl_proxy_info_log(s, info, "no acceptable CA list");
1552 modssl_set_cert_info(info, x509, pkey);
1557 for (i = 0; i < sk_X509_NAME_num(ca_list); i++) {
1558 ca_name = sk_X509_NAME_value(ca_list, i);
1560 for (j = 0; j < sk_X509_INFO_num(certs); j++) {
1561 info = sk_X509_INFO_value(certs, j);
1562 issuer = X509_get_issuer_name(info->x509);
1564 if (X509_NAME_cmp(issuer, ca_name) == 0) {
1565 modssl_proxy_info_log(s, info, "found acceptable cert");
1567 modssl_set_cert_info(info, x509, pkey);
1574 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1575 SSLPROXY_CERT_CB_LOG_FMT
1576 "no client certificate found!?", sc->vhost_id);
1581 static void ssl_session_log(server_rec *s,
1582 const char *request,
1589 char buf[SSL_SESSION_ID_STRING_LEN];
1590 char timeout_str[56] = {'\0'};
1592 if (s->loglevel < APLOG_DEBUG) {
1597 apr_snprintf(timeout_str, sizeof(timeout_str),
1598 "timeout=%lds ", (timeout - time(NULL)));
1601 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1602 "Inter-Process Session Cache: "
1603 "request=%s status=%s id=%s %s(session %s)",
1605 SSL_SESSION_id2sz(id, idlen, buf, sizeof(buf)),
1606 timeout_str, result);
1610 * This callback function is executed by OpenSSL whenever a new SSL_SESSION is
1611 * added to the internal OpenSSL session cache. We use this hook to spread the
1612 * SSL_SESSION also to the inter-process disk-cache to make share it with our
1613 * other Apache pre-forked server processes.
1615 int ssl_callback_NewSessionCacheEntry(SSL *ssl, SSL_SESSION *session)
1617 /* Get Apache context back through OpenSSL context */
1618 conn_rec *conn = (conn_rec *)SSL_get_app_data(ssl);
1619 server_rec *s = conn->base_server;
1620 SSLSrvConfigRec *sc = mySrvConfig(s);
1621 long timeout = sc->session_cache_timeout;
1627 * Set the timeout also for the internal OpenSSL cache, because this way
1628 * our inter-process cache is consulted only when it's really necessary.
1630 SSL_set_timeout(session, timeout);
1633 * Store the SSL_SESSION in the inter-process cache with the
1634 * same expire time, so it expires automatically there, too.
1636 id = SSL_SESSION_get_session_id(session);
1637 idlen = SSL_SESSION_get_session_id_length(session);
1639 timeout += modssl_session_get_time(session);
1641 rc = ssl_scache_store(s, id, idlen, timeout, session);
1643 ssl_session_log(s, "SET", id, idlen,
1644 rc == TRUE ? "OK" : "BAD",
1645 "caching", timeout);
1648 * return 0 which means to OpenSSL that the session is still
1649 * valid and was not freed by us with SSL_SESSION_free().
1655 * This callback function is executed by OpenSSL whenever a
1656 * SSL_SESSION is looked up in the internal OpenSSL cache and it
1657 * was not found. We use this to lookup the SSL_SESSION in the
1658 * inter-process disk-cache where it was perhaps stored by one
1659 * of our other Apache pre-forked server processes.
1661 SSL_SESSION *ssl_callback_GetSessionCacheEntry(SSL *ssl,
1663 int idlen, int *do_copy)
1665 /* Get Apache context back through OpenSSL context */
1666 conn_rec *conn = (conn_rec *)SSL_get_app_data(ssl);
1667 server_rec *s = conn->base_server;
1668 SSL_SESSION *session;
1671 * Try to retrieve the SSL_SESSION from the inter-process cache
1673 session = ssl_scache_retrieve(s, id, idlen);
1675 ssl_session_log(s, "GET", id, idlen,
1676 session ? "FOUND" : "MISSED",
1677 session ? "reuse" : "renewal", 0);
1680 * Return NULL or the retrieved SSL_SESSION. But indicate (by
1681 * setting do_copy to 0) that the reference count on the
1682 * SSL_SESSION should not be incremented by the SSL library,
1683 * because we will no longer hold a reference to it ourself.
1691 * This callback function is executed by OpenSSL whenever a
1692 * SSL_SESSION is removed from the the internal OpenSSL cache.
1693 * We use this to remove the SSL_SESSION in the inter-process
1696 void ssl_callback_DelSessionCacheEntry(SSL_CTX *ctx,
1697 SSL_SESSION *session)
1700 SSLSrvConfigRec *sc;
1705 * Get Apache context back through OpenSSL context
1707 if (!(s = (server_rec *)SSL_CTX_get_app_data(ctx))) {
1708 return; /* on server shutdown Apache is already gone */
1711 sc = mySrvConfig(s);
1714 * Remove the SSL_SESSION from the inter-process cache
1716 id = SSL_SESSION_get_session_id(session);
1717 idlen = SSL_SESSION_get_session_id_length(session);
1719 ssl_scache_remove(s, id, idlen);
1721 ssl_session_log(s, "REM", id, idlen,
1728 * This callback function is executed while OpenSSL processes the
1729 * SSL handshake and does SSL record layer stuff. We use it to
1730 * trace OpenSSL's processing in out SSL logfile.
1732 void ssl_callback_LogTracingState(MODSSL_INFO_CB_ARG_TYPE ssl, int where, int rc)
1736 SSLSrvConfigRec *sc;
1739 * find corresponding server
1741 if (!(c = (conn_rec *)SSL_get_app_data((SSL *)ssl))) {
1746 if (!(sc = mySrvConfig(s))) {
1751 * create the various trace messages
1753 if (s->loglevel >= APLOG_DEBUG) {
1754 if (where & SSL_CB_HANDSHAKE_START) {
1755 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1756 "%s: Handshake: start", SSL_LIBRARY_NAME);
1758 else if (where & SSL_CB_HANDSHAKE_DONE) {
1759 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1760 "%s: Handshake: done", SSL_LIBRARY_NAME);
1762 else if (where & SSL_CB_LOOP) {
1763 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1765 SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
1767 else if (where & SSL_CB_READ) {
1768 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1770 SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
1772 else if (where & SSL_CB_WRITE) {
1773 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1775 SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
1777 else if (where & SSL_CB_ALERT) {
1778 char *str = (where & SSL_CB_READ) ? "read" : "write";
1779 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1780 "%s: Alert: %s:%s:%s",
1781 SSL_LIBRARY_NAME, str,
1782 SSL_alert_type_string_long(rc),
1783 SSL_alert_desc_string_long(rc));
1785 else if (where & SSL_CB_EXIT) {
1787 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1788 "%s: Exit: failed in %s",
1789 SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
1792 ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
1793 "%s: Exit: error in %s",
1794 SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
1800 * Because SSL renegotations can happen at any time (not only after
1801 * SSL_accept()), the best way to log the current connection details is
1802 * right after a finished handshake.
1804 if (where & SSL_CB_HANDSHAKE_DONE) {
1805 ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
1806 "Connection: Client IP: %s, Protocol: %s, "
1807 "Cipher: %s (%s/%s bits)",
1808 ssl_var_lookup(NULL, s, c, NULL, "REMOTE_ADDR"),
1809 ssl_var_lookup(NULL, s, c, NULL, "SSL_PROTOCOL"),
1810 ssl_var_lookup(NULL, s, c, NULL, "SSL_CIPHER"),
1811 ssl_var_lookup(NULL, s, c, NULL, "SSL_CIPHER_USEKEYSIZE"),
1812 ssl_var_lookup(NULL, s, c, NULL, "SSL_CIPHER_ALGKEYSIZE"));