~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to modules/ssl/ssl_engine_kernel.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_kernel.c
 
24
 *  The SSL engine kernel
 
25
 */
 
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.''
 
30
                                            -- Unknown                */
 
31
#include "ssl_private.h"
 
32
 
 
33
static void ssl_configure_env(request_rec *r, SSLConnRec *sslconn);
 
34
 
 
35
/*
 
36
 *  Post Read Request Handler
 
37
 */
 
38
int ssl_hook_ReadReq(request_rec *r)
 
39
{
 
40
    SSLConnRec *sslconn = myConnConfig(r->connection);
 
41
    SSL *ssl;
 
42
 
 
43
    if (!sslconn) {
 
44
        return DECLINED;
 
45
    }
 
46
 
 
47
    if (sslconn->non_ssl_request) {
 
48
        const char *errmsg;
 
49
        char *thisurl;
 
50
        char *thisport = "";
 
51
        int port = ap_get_server_port(r);
 
52
 
 
53
        if (!ap_is_default_port(port, r)) {
 
54
            thisport = apr_psprintf(r->pool, ":%u", port);
 
55
        }
 
56
 
 
57
        thisurl = ap_escape_html(r->pool,
 
58
                                 apr_psprintf(r->pool, "https://%s%s/",
 
59
                                              ap_get_server_name(r),
 
60
                                              thisport));
 
61
 
 
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"
 
67
                              "<blockquote>Hint: "
 
68
                              "<a href=\"%s\"><b>%s</b></a></blockquote>",
 
69
                              thisurl, thisurl);
 
70
 
 
71
        apr_table_setn(r->notes, "error-notes", errmsg);
 
72
 
 
73
        /* Now that we have caught this error, forget it. we are done
 
74
         * with using SSL on this request.
 
75
         */
 
76
        sslconn->non_ssl_request = 0;
 
77
 
 
78
 
 
79
        return HTTP_BAD_REQUEST;
 
80
    }
 
81
 
 
82
    /*
 
83
     * Get the SSL connection structure and perform the
 
84
     * delayed interlinking from SSL back to request_rec
 
85
     */
 
86
    ssl = sslconn->ssl;
 
87
    if (!ssl) {
 
88
        return DECLINED;
 
89
    }
 
90
    SSL_set_app_data2(ssl, r);
 
91
 
 
92
    /*
 
93
     * Log information about incoming HTTPS requests
 
94
     */
 
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 ?
 
99
                     "Initial (No.1)" :
 
100
                     apr_psprintf(r->pool, "Subsequent (No.%d)",
 
101
                                  r->connection->keepalives+1)),
 
102
                     r->connection->id,
 
103
                     ssl_util_vhostid(r->pool, r->server));
 
104
    }
 
105
 
 
106
    /* SetEnvIf ssl-*-shutdown flags can only be per-server,
 
107
     * so they won't change across keepalive requests
 
108
     */
 
109
    if (sslconn->shutdown_type == SSL_SHUTDOWN_TYPE_UNSET) {
 
110
        ssl_configure_env(r, sslconn);
 
111
    }
 
112
 
 
113
    return DECLINED;
 
114
}
 
115
 
 
116
/*
 
117
 * Move SetEnvIf information from request_rec to conn_rec/BUFF
 
118
 * to allow the close connection handler to use them.
 
119
 */
 
120
 
 
121
static void ssl_configure_env(request_rec *r, SSLConnRec *sslconn)
 
122
{
 
123
    int i;
 
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;
 
126
 
 
127
    sslconn->shutdown_type = SSL_SHUTDOWN_TYPE_STANDARD;
 
128
 
 
129
    for (i = 0; i < arr->nelts; i++) {
 
130
        const char *key = elts[i].key;
 
131
 
 
132
        switch (*key) {
 
133
          case 's':
 
134
            /* being case-sensitive here.
 
135
             * and not checking for the -shutdown since these are the only
 
136
             * SetEnvIf "flags" we support
 
137
             */
 
138
            if (!strncmp(key+1, "sl-", 3)) {
 
139
                key += 4;
 
140
                if (!strncmp(key, "unclean", 7)) {
 
141
                    sslconn->shutdown_type = SSL_SHUTDOWN_TYPE_UNCLEAN;
 
142
                }
 
143
                else if (!strncmp(key, "accurate", 8)) {
 
144
                    sslconn->shutdown_type = SSL_SHUTDOWN_TYPE_ACCURATE;
 
145
                }
 
146
                return; /* should only ever be one ssl-*-shutdown */
 
147
            }
 
148
            break;
 
149
        }
 
150
    }
 
151
}
 
152
 
 
153
/*
 
154
 *  Access Handler
 
155
 */
 
156
int ssl_hook_Access(request_rec *r)
 
157
{
 
158
    SSLDirConfigRec *dc = myDirConfig(r);
 
159
    SSLSrvConfigRec *sc = mySrvConfig(r->server);
 
160
    SSLConnRec *sslconn = myConnConfig(r->connection);
 
161
    SSL *ssl            = sslconn ? sslconn->ssl : NULL;
 
162
    SSL_CTX *ctx = NULL;
 
163
    apr_array_header_t *requires;
 
164
    ssl_require_t *ssl_requires;
 
165
    char *cp;
 
166
    int ok, i;
 
167
    BOOL renegotiate = FALSE, renegotiate_quick = FALSE;
 
168
    X509 *cert;
 
169
    X509 *peercert;
 
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;
 
175
 
 
176
    if (ssl) {
 
177
        ctx = SSL_get_SSL_CTX(ssl);
 
178
    }
 
179
 
 
180
    /*
 
181
     * Support for SSLRequireSSL directive
 
182
     */
 
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.
 
187
             */
 
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");
 
190
 
 
191
            return HTTP_UPGRADE_REQUIRED;
 
192
        }
 
193
 
 
194
        ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
195
                      "access to %s failed, reason: %s",
 
196
                      r->filename, "SSL connection required");
 
197
 
 
198
        /* remember forbidden access for strict require option */
 
199
        apr_table_setn(r->notes, "ssl-access-forbidden", "1");
 
200
 
 
201
        return HTTP_FORBIDDEN;
 
202
    }
 
203
 
 
204
    /*
 
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)
 
208
     */
 
209
    if (sc->enabled == SSL_ENABLED_FALSE || !ssl) {
 
210
        return DECLINED;
 
211
    }
 
212
 
 
213
    /*
 
214
     * Support for per-directory reconfigured SSL connection parameters.
 
215
     *
 
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).
 
221
     *
 
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.
 
232
     */
 
233
 
 
234
    /*
 
235
     * Override of SSLCipherSuite
 
236
     *
 
237
     * We provide two options here:
 
238
     *
 
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).
 
243
     *
 
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.
 
254
     */
 
255
    if (dc->szCipherSuite) {
 
256
        /* remember old state */
 
257
 
 
258
        if (dc->nOptions & SSL_OPT_OPTRENEGOTIATE) {
 
259
            cipher = SSL_get_current_cipher(ssl);
 
260
        }
 
261
        else {
 
262
            cipher_list_old = (STACK_OF(SSL_CIPHER) *)SSL_get_ciphers(ssl);
 
263
 
 
264
            if (cipher_list_old) {
 
265
                cipher_list_old = sk_SSL_CIPHER_dup(cipher_list_old);
 
266
            }
 
267
        }
 
268
 
 
269
        /* configure new state */
 
270
        if (!modssl_set_cipher_list(ssl, dc->szCipherSuite)) {
 
271
            ap_log_error(APLOG_MARK, APLOG_WARNING, 0,
 
272
                         r->server,
 
273
                         "Unable to reconfigure (per-directory) "
 
274
                         "permitted SSL ciphers");
 
275
            ssl_log_ssl_error(APLOG_MARK, APLOG_ERR, r->server);
 
276
 
 
277
            if (cipher_list_old) {
 
278
                sk_SSL_CIPHER_free(cipher_list_old);
 
279
            }
 
280
 
 
281
            return HTTP_FORBIDDEN;
 
282
        }
 
283
 
 
284
        /* determine whether a renegotiation has to be forced */
 
285
        cipher_list = (STACK_OF(SSL_CIPHER) *)SSL_get_ciphers(ssl);
 
286
 
 
287
        if (dc->nOptions & SSL_OPT_OPTRENEGOTIATE) {
 
288
            /* optimized way */
 
289
            if ((!cipher && cipher_list) ||
 
290
                (cipher && !cipher_list))
 
291
            {
 
292
                renegotiate = TRUE;
 
293
            }
 
294
            else if (cipher && cipher_list &&
 
295
                     (sk_SSL_CIPHER_find(cipher_list, cipher) < 0))
 
296
            {
 
297
                renegotiate = TRUE;
 
298
            }
 
299
        }
 
300
        else {
 
301
            /* paranoid way */
 
302
            if ((!cipher_list_old && cipher_list) ||
 
303
                (cipher_list_old && !cipher_list))
 
304
            {
 
305
                renegotiate = TRUE;
 
306
            }
 
307
            else if (cipher_list_old && cipher_list) {
 
308
                for (n = 0;
 
309
                     !renegotiate && (n < sk_SSL_CIPHER_num(cipher_list));
 
310
                     n++)
 
311
                {
 
312
                    SSL_CIPHER *value = sk_SSL_CIPHER_value(cipher_list, n);
 
313
 
 
314
                    if (sk_SSL_CIPHER_find(cipher_list_old, value) < 0) {
 
315
                        renegotiate = TRUE;
 
316
                    }
 
317
                }
 
318
 
 
319
                for (n = 0;
 
320
                     !renegotiate && (n < sk_SSL_CIPHER_num(cipher_list_old));
 
321
                     n++)
 
322
                {
 
323
                    SSL_CIPHER *value = sk_SSL_CIPHER_value(cipher_list_old, n);
 
324
 
 
325
                    if (sk_SSL_CIPHER_find(cipher_list, value) < 0) {
 
326
                        renegotiate = TRUE;
 
327
                    }
 
328
                }
 
329
            }
 
330
        }
 
331
 
 
332
        /* cleanup */
 
333
        if (cipher_list_old) {
 
334
            sk_SSL_CIPHER_free(cipher_list_old);
 
335
        }
 
336
 
 
337
        /* tracing */
 
338
        if (renegotiate) {
 
339
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
 
340
                         "Reconfigured cipher suite will force renegotiation");
 
341
        }
 
342
    }
 
343
 
 
344
    /*
 
345
     * override of SSLVerifyDepth
 
346
     *
 
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).
 
355
     */
 
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;
 
360
        }
 
361
 
 
362
        /* determine whether a renegotiation has to be forced */
 
363
        if (dc->nVerifyDepth < n) {
 
364
            renegotiate = TRUE;
 
365
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
 
366
                         "Reduced client verification depth will force "
 
367
                         "renegotiation");
 
368
        }
 
369
    }
 
370
 
 
371
    /*
 
372
     * override of SSLVerifyClient
 
373
     *
 
374
     * We force a renegotiation if the reconfigured/new verify type is
 
375
     * stronger than the currently active verify type.
 
376
     *
 
377
     * The order is: none << optional_no_ca << optional << require
 
378
     *
 
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
 
383
     * handshake.
 
384
     */
 
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;
 
390
 
 
391
        if (dc->nVerifyClient == SSL_CVERIFY_REQUIRE) {
 
392
            verify |= SSL_VERIFY_PEER_STRICT;
 
393
        }
 
394
 
 
395
        if ((dc->nVerifyClient == SSL_CVERIFY_OPTIONAL) ||
 
396
            (dc->nVerifyClient == SSL_CVERIFY_OPTIONAL_NO_CA))
 
397
        {
 
398
            verify |= SSL_VERIFY_PEER;
 
399
        }
 
400
 
 
401
        modssl_set_verify(ssl, verify, ssl_callback_SSLVerify);
 
402
        SSL_set_verify_result(ssl, X509_V_OK);
 
403
 
 
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)) ||
 
408
 
 
409
                (!(verify_old & SSL_VERIFY_PEER) &&
 
410
                  (verify     & SSL_VERIFY_PEER)) ||
 
411
 
 
412
                (!(verify_old & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) &&
 
413
                  (verify     & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
 
414
            {
 
415
                renegotiate = TRUE;
 
416
                /* optimization */
 
417
 
 
418
                if ((dc->nOptions & SSL_OPT_OPTRENEGOTIATE) &&
 
419
                    (verify_old == SSL_VERIFY_NONE) &&
 
420
                    ((peercert = SSL_get_peer_certificate(ssl)) != NULL))
 
421
                {
 
422
                    renegotiate_quick = TRUE;
 
423
                    X509_free(peercert);
 
424
                }
 
425
 
 
426
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0,
 
427
                             r->server,
 
428
                             "Changed client verification type will force "
 
429
                             "%srenegotiation",
 
430
                             renegotiate_quick ? "quick " : "");
 
431
             }
 
432
        }
 
433
    }
 
434
 
 
435
    /*
 
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.
 
440
     */
 
441
 
 
442
#ifdef HAVE_SSL_SET_CERT_STORE
 
443
    /*
 
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 ...
 
447
     */
 
448
#define MODSSL_CFG_NE(f) \
 
449
     (dc->f && (!sc->f || (sc->f && strNE(dc->f, sc->f))))
 
450
 
 
451
#define MODSSL_CFG_CA(f) \
 
452
     (dc->f ? dc->f : sc->f)
 
453
 
 
454
    if (MODSSL_CFG_NE(szCACertificateFile) ||
 
455
        MODSSL_CFG_NE(szCACertificatePath))
 
456
    {
 
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);
 
460
 
 
461
        cert_store = X509_STORE_new();
 
462
 
 
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);
 
468
 
 
469
            X509_STORE_free(cert_store);
 
470
 
 
471
            return HTTP_FORBIDDEN;
 
472
        }
 
473
 
 
474
        /* SSL_free will free cert_store */
 
475
        SSL_set_cert_store(ssl, cert_store);
 
476
 
 
477
        if (!(ca_list = ssl_init_FindCAList(r->server, r->pool,
 
478
                                            ca_file, ca_path)))
 
479
        {
 
480
            ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
 
481
                         "Unable to determine list of available "
 
482
                         "CA certificates for client authentication");
 
483
 
 
484
            return HTTP_FORBIDDEN;
 
485
        }
 
486
 
 
487
        SSL_set_client_CA_list(ssl, ca_list);
 
488
        renegotiate = TRUE;
 
489
 
 
490
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
 
491
                     "Changed client verification locations will force "
 
492
                     "renegotiation");
 
493
    }
 
494
#endif /* HAVE_SSL_SET_CERT_STORE */
 
495
 
 
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.
 
504
     *
 
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.
 
508
     */
 
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) {
 
514
        int rv;
 
515
 
 
516
        /* Fill the I/O buffer with the request body if possible. */
 
517
        rv = ssl_io_buffer_fill(r);
 
518
 
 
519
        if (rv) {
 
520
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
521
                          "could not buffer message body to allow "
 
522
                          "SSL renegotiation to proceed");
 
523
            return rv;
 
524
        }
 
525
    }
 
526
 
 
527
    /*
 
528
     * now do the renegotiation if anything was actually reconfigured
 
529
     */
 
530
    if (renegotiate) {
 
531
        /*
 
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.
 
541
         */
 
542
        ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
 
543
                     "Requesting connection re-negotiation");
 
544
 
 
545
        if (renegotiate_quick) {
 
546
            STACK_OF(X509) *cert_stack;
 
547
 
 
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");
 
552
 
 
553
            cert_stack = (STACK_OF(X509) *)SSL_get_peer_cert_chain(ssl);
 
554
 
 
555
            cert = SSL_get_peer_certificate(ssl);
 
556
 
 
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.
 
562
                 */
 
563
                cert_stack = sk_new_null();
 
564
                sk_X509_push(cert_stack, MODSSL_PCHAR_CAST cert);
 
565
            }
 
566
 
 
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");
 
570
 
 
571
                return HTTP_FORBIDDEN;
 
572
            }
 
573
 
 
574
            if (!(cert_store ||
 
575
                  (cert_store = SSL_CTX_get_cert_store(ctx))))
 
576
            {
 
577
                ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server,
 
578
                             "Cannot find certificate storage");
 
579
 
 
580
                return HTTP_FORBIDDEN;
 
581
            }
 
582
 
 
583
            if (!cert) {
 
584
                cert = sk_X509_value(cert_stack, 0);
 
585
            }
 
586
 
 
587
            X509_STORE_CTX_init(&cert_store_ctx, cert_store, cert, cert_stack);
 
588
            depth = SSL_get_verify_depth(ssl);
 
589
 
 
590
            if (depth >= 0) {
 
591
                X509_STORE_CTX_set_depth(&cert_store_ctx, depth);
 
592
            }
 
593
 
 
594
            X509_STORE_CTX_set_ex_data(&cert_store_ctx,
 
595
                                       SSL_get_ex_data_X509_STORE_CTX_idx(),
 
596
                                       (char *)ssl);
 
597
 
 
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);
 
602
            }
 
603
 
 
604
            SSL_set_verify_result(ssl, cert_store_ctx.error);
 
605
            X509_STORE_CTX_cleanup(&cert_store_ctx);
 
606
 
 
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);
 
610
            }
 
611
        }
 
612
        else {
 
613
            request_rec *id = r->main ? r->main : r;
 
614
 
 
615
            /* do a full renegotiation */
 
616
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, r->server,
 
617
                         "Performing full renegotiation: "
 
618
                         "complete handshake protocol");
 
619
 
 
620
            SSL_set_session_id_context(ssl,
 
621
                                       (unsigned char *)&id,
 
622
                                       sizeof(id));
 
623
 
 
624
            SSL_renegotiate(ssl);
 
625
            SSL_do_handshake(ssl);
 
626
 
 
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");
 
630
 
 
631
                r->connection->aborted = 1;
 
632
                return HTTP_FORBIDDEN;
 
633
            }
 
634
 
 
635
            ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
 
636
                         "Awaiting re-negotiation handshake");
 
637
 
 
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?
 
641
             */
 
642
            SSL_set_state(ssl, SSL_ST_ACCEPT);
 
643
            SSL_do_handshake(ssl);
 
644
 
 
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!?");
 
649
 
 
650
                r->connection->aborted = 1;
 
651
                return HTTP_FORBIDDEN;
 
652
            }
 
653
        }
 
654
 
 
655
        /*
 
656
         * Remember the peer certificate's DN
 
657
         */
 
658
        if ((cert = SSL_get_peer_certificate(ssl))) {
 
659
            if (sslconn->client_cert) {
 
660
                X509_free(sslconn->client_cert);
 
661
            }
 
662
            sslconn->client_cert = cert;
 
663
            sslconn->client_dn = NULL;
 
664
        }
 
665
 
 
666
        /*
 
667
         * Finally check for acceptable renegotiation results
 
668
         */
 
669
        if (dc->nVerifyClient != SSL_CVERIFY_NONE) {
 
670
            BOOL do_verify = (dc->nVerifyClient == SSL_CVERIFY_REQUIRE);
 
671
 
 
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");
 
676
 
 
677
                return HTTP_FORBIDDEN;
 
678
            }
 
679
 
 
680
            if (do_verify) {
 
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");
 
685
 
 
686
                    return HTTP_FORBIDDEN;
 
687
                }
 
688
 
 
689
                X509_free(peercert);
 
690
            }
 
691
        }
 
692
 
 
693
        /*
 
694
         * Also check that SSLCipherSuite has been enforced as expected.
 
695
         */
 
696
        if (cipher_list) {
 
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",
 
702
                              r->filename,
 
703
                              SSL_CIPHER_get_name(cipher));
 
704
                return HTTP_FORBIDDEN;
 
705
            }
 
706
        }
 
707
    }
 
708
 
 
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.
 
715
     */
 
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);
 
719
        if (val && val[0])
 
720
            r->user = val;
 
721
    }
 
722
 
 
723
    /*
 
724
     * Check SSLRequire boolean expressions
 
725
     */
 
726
    requires = dc->aRequirement;
 
727
    ssl_requires = (ssl_require_t *)requires->elts;
 
728
 
 
729
    for (i = 0; i < requires->nelts; i++) {
 
730
        ssl_require_t *req = &ssl_requires[i];
 
731
        ok = ssl_expr_exec(r, req->mpExpr);
 
732
 
 
733
        if (ok < 0) {
 
734
            cp = apr_psprintf(r->pool,
 
735
                              "Failed to execute "
 
736
                              "SSL requirement expression: %s",
 
737
                              ssl_expr_get_error());
 
738
 
 
739
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
740
                          "access to %s failed, reason: %s",
 
741
                          r->filename, cp);
 
742
 
 
743
            /* remember forbidden access for strict require option */
 
744
            apr_table_setn(r->notes, "ssl-access-forbidden", "1");
 
745
 
 
746
            return HTTP_FORBIDDEN;
 
747
        }
 
748
 
 
749
        if (ok != 1) {
 
750
            ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
 
751
                         "Access to %s denied for %s "
 
752
                         "(requirement expression not fulfilled)",
 
753
                         r->filename, r->connection->remote_ip);
 
754
 
 
755
            ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
 
756
                         "Failed expression: %s", req->cpExpr);
 
757
 
 
758
            ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
759
                          "access to %s failed, reason: %s",
 
760
                          r->filename,
 
761
                          "SSL requirement expression not fulfilled "
 
762
                          "(see SSL logfile for more details)");
 
763
 
 
764
            /* remember forbidden access for strict require option */
 
765
            apr_table_setn(r->notes, "ssl-access-forbidden", "1");
 
766
 
 
767
            return HTTP_FORBIDDEN;
 
768
        }
 
769
    }
 
770
 
 
771
    /*
 
772
     * Else access is granted from our point of view (except vendor
 
773
     * handlers override). But we have to return DECLINED here instead
 
774
     * of OK, because mod_auth and other modules still might want to
 
775
     * deny access.
 
776
     */
 
777
 
 
778
    return DECLINED;
 
779
}
 
780
 
 
781
/*
 
782
 *  Authentication Handler:
 
783
 *  Fake a Basic authentication from the X509 client certificate.
 
784
 *
 
785
 *  This must be run fairly early on to prevent a real authentication from
 
786
 *  occuring, in particular it must be run before anything else that
 
787
 *  authenticates a user.  This means that the Module statement for this
 
788
 *  module should be LAST in the Configuration file.
 
789
 */
 
790
int ssl_hook_UserCheck(request_rec *r)
 
791
{
 
792
    SSLConnRec *sslconn = myConnConfig(r->connection);
 
793
    SSLSrvConfigRec *sc = mySrvConfig(r->server);
 
794
    SSLDirConfigRec *dc = myDirConfig(r);
 
795
    char *clientdn;
 
796
    const char *auth_line, *username, *password;
 
797
 
 
798
    /*
 
799
     * Additionally forbid access (again)
 
800
     * when strict require option is used.
 
801
     */
 
802
    if ((dc->nOptions & SSL_OPT_STRICTREQUIRE) &&
 
803
        (apr_table_get(r->notes, "ssl-access-forbidden")))
 
804
    {
 
805
        return HTTP_FORBIDDEN;
 
806
    }
 
807
 
 
808
    /*
 
809
     * We decline when we are in a subrequest.  The Authorization header
 
810
     * would already be present if it was added in the main request.
 
811
     */
 
812
    if (!ap_is_initial_req(r)) {
 
813
        return DECLINED;
 
814
    }
 
815
 
 
816
    /*
 
817
     * Make sure the user is not able to fake the client certificate
 
818
     * based authentication by just entering an X.509 Subject DN
 
819
     * ("/XX=YYY/XX=YYY/..") as the username and "password" as the
 
820
     * password.
 
821
     */
 
822
    if ((auth_line = apr_table_get(r->headers_in, "Authorization"))) {
 
823
        if (strcEQ(ap_getword(r->pool, &auth_line, ' '), "Basic")) {
 
824
            while ((*auth_line == ' ') || (*auth_line == '\t')) {
 
825
                auth_line++;
 
826
            }
 
827
 
 
828
            auth_line = ap_pbase64decode(r->pool, auth_line);
 
829
            username = ap_getword_nulls(r->pool, &auth_line, ':');
 
830
            password = auth_line;
 
831
 
 
832
            if ((username[0] == '/') && strEQ(password, "password")) {
 
833
                ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
 
834
                    "Encountered FakeBasicAuth spoof: %s", username);
 
835
                return HTTP_FORBIDDEN;
 
836
            }
 
837
        }
 
838
    }
 
839
 
 
840
    /*
 
841
     * We decline operation in various situations...
 
842
     * - SSLOptions +FakeBasicAuth not configured
 
843
     * - r->user already authenticated
 
844
     * - ssl not enabled
 
845
     * - client did not present a certificate
 
846
     */
 
847
    if (!((sc->enabled == SSL_ENABLED_TRUE || sc->enabled == SSL_ENABLED_OPTIONAL)
 
848
          && sslconn && sslconn->ssl && sslconn->client_cert) ||
 
849
        !(dc->nOptions & SSL_OPT_FAKEBASICAUTH) || r->user)
 
850
    {
 
851
        return DECLINED;
 
852
    }
 
853
 
 
854
    if (!sslconn->client_dn) {
 
855
        X509_NAME *name = X509_get_subject_name(sslconn->client_cert);
 
856
        char *cp = X509_NAME_oneline(name, NULL, 0);
 
857
        sslconn->client_dn = apr_pstrdup(r->connection->pool, cp);
 
858
        modssl_free(cp);
 
859
    }
 
860
 
 
861
    clientdn = (char *)sslconn->client_dn;
 
862
 
 
863
    /*
 
864
     * Fake a password - which one would be immaterial, as, it seems, an empty
 
865
     * password in the users file would match ALL incoming passwords, if only
 
866
     * we were using the standard crypt library routine. Unfortunately, OpenSSL
 
867
     * "fixes" a "bug" in crypt and thus prevents blank passwords from
 
868
     * working.  (IMHO what they really fix is a bug in the users of the code
 
869
     * - failing to program correctly for shadow passwords).  We need,
 
870
     * therefore, to provide a password. This password can be matched by
 
871
     * adding the string "xxj31ZMTZzkVA" as the password in the user file.
 
872
     * This is just the crypted variant of the word "password" ;-)
 
873
     */
 
874
    auth_line = apr_pstrcat(r->pool, "Basic ",
 
875
                            ap_pbase64encode(r->pool,
 
876
                                             apr_pstrcat(r->pool, clientdn,
 
877
                                                         ":password", NULL)),
 
878
                            NULL);
 
879
    apr_table_set(r->headers_in, "Authorization", auth_line);
 
880
 
 
881
    ap_log_error(APLOG_MARK, APLOG_INFO, 0, r->server,
 
882
                 "Faking HTTP Basic Auth header: \"Authorization: %s\"",
 
883
                 auth_line);
 
884
 
 
885
    return DECLINED;
 
886
}
 
887
 
 
888
/* authorization phase */
 
889
int ssl_hook_Auth(request_rec *r)
 
890
{
 
891
    SSLDirConfigRec *dc = myDirConfig(r);
 
892
 
 
893
    /*
 
894
     * Additionally forbid access (again)
 
895
     * when strict require option is used.
 
896
     */
 
897
    if ((dc->nOptions & SSL_OPT_STRICTREQUIRE) &&
 
898
        (apr_table_get(r->notes, "ssl-access-forbidden")))
 
899
    {
 
900
        return HTTP_FORBIDDEN;
 
901
    }
 
902
 
 
903
    return DECLINED;
 
904
}
 
905
 
 
906
/*
 
907
 *   Fixup Handler
 
908
 */
 
909
 
 
910
static const char *ssl_hook_Fixup_vars[] = {
 
911
    "SSL_VERSION_INTERFACE",
 
912
    "SSL_VERSION_LIBRARY",
 
913
    "SSL_PROTOCOL",
 
914
    "SSL_COMPRESS_METHOD",
 
915
    "SSL_CIPHER",
 
916
    "SSL_CIPHER_EXPORT",
 
917
    "SSL_CIPHER_USEKEYSIZE",
 
918
    "SSL_CIPHER_ALGKEYSIZE",
 
919
    "SSL_CLIENT_VERIFY",
 
920
    "SSL_CLIENT_M_VERSION",
 
921
    "SSL_CLIENT_M_SERIAL",
 
922
    "SSL_CLIENT_V_START",
 
923
    "SSL_CLIENT_V_END",
 
924
    "SSL_CLIENT_V_REMAIN",
 
925
    "SSL_CLIENT_S_DN",
 
926
    "SSL_CLIENT_S_DN_C",
 
927
    "SSL_CLIENT_S_DN_ST",
 
928
    "SSL_CLIENT_S_DN_L",
 
929
    "SSL_CLIENT_S_DN_O",
 
930
    "SSL_CLIENT_S_DN_OU",
 
931
    "SSL_CLIENT_S_DN_CN",
 
932
    "SSL_CLIENT_S_DN_T",
 
933
    "SSL_CLIENT_S_DN_I",
 
934
    "SSL_CLIENT_S_DN_G",
 
935
    "SSL_CLIENT_S_DN_S",
 
936
    "SSL_CLIENT_S_DN_D",
 
937
    "SSL_CLIENT_S_DN_UID",
 
938
    "SSL_CLIENT_S_DN_Email",
 
939
    "SSL_CLIENT_I_DN",
 
940
    "SSL_CLIENT_I_DN_C",
 
941
    "SSL_CLIENT_I_DN_ST",
 
942
    "SSL_CLIENT_I_DN_L",
 
943
    "SSL_CLIENT_I_DN_O",
 
944
    "SSL_CLIENT_I_DN_OU",
 
945
    "SSL_CLIENT_I_DN_CN",
 
946
    "SSL_CLIENT_I_DN_T",
 
947
    "SSL_CLIENT_I_DN_I",
 
948
    "SSL_CLIENT_I_DN_G",
 
949
    "SSL_CLIENT_I_DN_S",
 
950
    "SSL_CLIENT_I_DN_D",
 
951
    "SSL_CLIENT_I_DN_UID",
 
952
    "SSL_CLIENT_I_DN_Email",
 
953
    "SSL_CLIENT_A_KEY",
 
954
    "SSL_CLIENT_A_SIG",
 
955
    "SSL_SERVER_M_VERSION",
 
956
    "SSL_SERVER_M_SERIAL",
 
957
    "SSL_SERVER_V_START",
 
958
    "SSL_SERVER_V_END",
 
959
    "SSL_SERVER_S_DN",
 
960
    "SSL_SERVER_S_DN_C",
 
961
    "SSL_SERVER_S_DN_ST",
 
962
    "SSL_SERVER_S_DN_L",
 
963
    "SSL_SERVER_S_DN_O",
 
964
    "SSL_SERVER_S_DN_OU",
 
965
    "SSL_SERVER_S_DN_CN",
 
966
    "SSL_SERVER_S_DN_T",
 
967
    "SSL_SERVER_S_DN_I",
 
968
    "SSL_SERVER_S_DN_G",
 
969
    "SSL_SERVER_S_DN_S",
 
970
    "SSL_SERVER_S_DN_D",
 
971
    "SSL_SERVER_S_DN_UID",
 
972
    "SSL_SERVER_S_DN_Email",
 
973
    "SSL_SERVER_I_DN",
 
974
    "SSL_SERVER_I_DN_C",
 
975
    "SSL_SERVER_I_DN_ST",
 
976
    "SSL_SERVER_I_DN_L",
 
977
    "SSL_SERVER_I_DN_O",
 
978
    "SSL_SERVER_I_DN_OU",
 
979
    "SSL_SERVER_I_DN_CN",
 
980
    "SSL_SERVER_I_DN_T",
 
981
    "SSL_SERVER_I_DN_I",
 
982
    "SSL_SERVER_I_DN_G",
 
983
    "SSL_SERVER_I_DN_S",
 
984
    "SSL_SERVER_I_DN_D",
 
985
    "SSL_SERVER_I_DN_UID",
 
986
    "SSL_SERVER_I_DN_Email",
 
987
    "SSL_SERVER_A_KEY",
 
988
    "SSL_SERVER_A_SIG",
 
989
    "SSL_SESSION_ID",
 
990
    NULL
 
991
};
 
992
 
 
993
int ssl_hook_Fixup(request_rec *r)
 
994
{
 
995
    SSLConnRec *sslconn = myConnConfig(r->connection);
 
996
    SSLSrvConfigRec *sc = mySrvConfig(r->server);
 
997
    SSLDirConfigRec *dc = myDirConfig(r);
 
998
    apr_table_t *env = r->subprocess_env;
 
999
    char *var, *val = "";
 
1000
    STACK_OF(X509) *peer_certs;
 
1001
    SSL *ssl;
 
1002
    int i;
 
1003
 
 
1004
    if (sc->enabled == SSL_ENABLED_OPTIONAL && !(sslconn && sslconn->ssl)) {
 
1005
        apr_table_setn(r->headers_out, "Upgrade", "TLS/1.0, HTTP/1.1");
 
1006
    }
 
1007
 
 
1008
    /*
 
1009
     * Check to see if SSL is on
 
1010
     */
 
1011
    if (!(((sc->enabled == SSL_ENABLED_TRUE) || (sc->enabled == SSL_ENABLED_OPTIONAL)) && sslconn && (ssl = sslconn->ssl))) {
 
1012
        return DECLINED;
 
1013
    }
 
1014
 
 
1015
    /*
 
1016
     * Annotate the SSI/CGI environment with standard SSL information
 
1017
     */
 
1018
    /* the always present HTTPS (=HTTP over SSL) flag! */
 
1019
    apr_table_setn(env, "HTTPS", "on");
 
1020
 
 
1021
    /* standard SSL environment variables */
 
1022
    if (dc->nOptions & SSL_OPT_STDENVVARS) {
 
1023
        for (i = 0; ssl_hook_Fixup_vars[i]; i++) {
 
1024
            var = (char *)ssl_hook_Fixup_vars[i];
 
1025
            val = ssl_var_lookup(r->pool, r->server, r->connection, r, var);
 
1026
            if (!strIsEmpty(val)) {
 
1027
                apr_table_setn(env, var, val);
 
1028
            }
 
1029
        }
 
1030
    }
 
1031
 
 
1032
    /*
 
1033
     * On-demand bloat up the SSI/CGI environment with certificate data
 
1034
     */
 
1035
    if (dc->nOptions & SSL_OPT_EXPORTCERTDATA) {
 
1036
        val = ssl_var_lookup(r->pool, r->server, r->connection,
 
1037
                             r, "SSL_SERVER_CERT");
 
1038
 
 
1039
        apr_table_setn(env, "SSL_SERVER_CERT", val);
 
1040
 
 
1041
        val = ssl_var_lookup(r->pool, r->server, r->connection,
 
1042
                             r, "SSL_CLIENT_CERT");
 
1043
 
 
1044
        apr_table_setn(env, "SSL_CLIENT_CERT", val);
 
1045
 
 
1046
        if ((peer_certs = (STACK_OF(X509) *)SSL_get_peer_cert_chain(ssl))) {
 
1047
            for (i = 0; i < sk_X509_num(peer_certs); i++) {
 
1048
                var = apr_psprintf(r->pool, "SSL_CLIENT_CERT_CHAIN_%d", i);
 
1049
                val = ssl_var_lookup(r->pool, r->server, r->connection,
 
1050
                                     r, var);
 
1051
                if (val) {
 
1052
                    apr_table_setn(env, var, val);
 
1053
                }
 
1054
            }
 
1055
        }
 
1056
    }
 
1057
 
 
1058
    return DECLINED;
 
1059
}
 
1060
 
 
1061
/*  _________________________________________________________________
 
1062
**
 
1063
**  OpenSSL Callback Functions
 
1064
**  _________________________________________________________________
 
1065
*/
 
1066
 
 
1067
/*
 
1068
 * Handle out temporary RSA private keys on demand
 
1069
 *
 
1070
 * The background of this as the TLSv1 standard explains it:
 
1071
 *
 
1072
 * | D.1. Temporary RSA keys
 
1073
 * |
 
1074
 * |    US Export restrictions limit RSA keys used for encryption to 512
 
1075
 * |    bits, but do not place any limit on lengths of RSA keys used for
 
1076
 * |    signing operations. Certificates often need to be larger than 512
 
1077
 * |    bits, since 512-bit RSA keys are not secure enough for high-value
 
1078
 * |    transactions or for applications requiring long-term security. Some
 
1079
 * |    certificates are also designated signing-only, in which case they
 
1080
 * |    cannot be used for key exchange.
 
1081
 * |
 
1082
 * |    When the public key in the certificate cannot be used for encryption,
 
1083
 * |    the server signs a temporary RSA key, which is then exchanged. In
 
1084
 * |    exportable applications, the temporary RSA key should be the maximum
 
1085
 * |    allowable length (i.e., 512 bits). Because 512-bit RSA keys are
 
1086
 * |    relatively insecure, they should be changed often. For typical
 
1087
 * |    electronic commerce applications, it is suggested that keys be
 
1088
 * |    changed daily or every 500 transactions, and more often if possible.
 
1089
 * |    Note that while it is acceptable to use the same temporary key for
 
1090
 * |    multiple transactions, it must be signed each time it is used.
 
1091
 * |
 
1092
 * |    RSA key generation is a time-consuming process. In many cases, a
 
1093
 * |    low-priority process can be assigned the task of key generation.
 
1094
 * |    Whenever a new key is completed, the existing temporary key can be
 
1095
 * |    replaced with the new one.
 
1096
 *
 
1097
 * XXX: base on comment above, if thread support is enabled,
 
1098
 * we should spawn a low-priority thread to generate new keys
 
1099
 * on the fly.
 
1100
 *
 
1101
 * So we generated 512 and 1024 bit temporary keys on startup
 
1102
 * which we now just hand out on demand....
 
1103
 */
 
1104
 
 
1105
RSA *ssl_callback_TmpRSA(SSL *ssl, int export, int keylen)
 
1106
{
 
1107
    conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
 
1108
    SSLModConfigRec *mc = myModConfig(c->base_server);
 
1109
    int idx;
 
1110
 
 
1111
    ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c,
 
1112
                  "handing out temporary %d bit RSA key", keylen);
 
1113
 
 
1114
    /* doesn't matter if export flag is on,
 
1115
     * we won't be asked for keylen > 512 in that case.
 
1116
     * if we are asked for a keylen > 1024, it is too expensive
 
1117
     * to generate on the fly.
 
1118
     * XXX: any reason not to generate 2048 bit keys at startup?
 
1119
     */
 
1120
 
 
1121
    switch (keylen) {
 
1122
      case 512:
 
1123
        idx = SSL_TMP_KEY_RSA_512;
 
1124
        break;
 
1125
 
 
1126
      case 1024:
 
1127
      default:
 
1128
        idx = SSL_TMP_KEY_RSA_1024;
 
1129
    }
 
1130
 
 
1131
    return (RSA *)mc->pTmpKeys[idx];
 
1132
}
 
1133
 
 
1134
/*
 
1135
 * Hand out the already generated DH parameters...
 
1136
 */
 
1137
DH *ssl_callback_TmpDH(SSL *ssl, int export, int keylen)
 
1138
{
 
1139
    conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
 
1140
    SSLModConfigRec *mc = myModConfig(c->base_server);
 
1141
    int idx;
 
1142
 
 
1143
    ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, c,
 
1144
                  "handing out temporary %d bit DH key", keylen);
 
1145
 
 
1146
    switch (keylen) {
 
1147
      case 512:
 
1148
        idx = SSL_TMP_KEY_DH_512;
 
1149
        break;
 
1150
 
 
1151
      case 1024:
 
1152
      default:
 
1153
        idx = SSL_TMP_KEY_DH_1024;
 
1154
    }
 
1155
 
 
1156
    return (DH *)mc->pTmpKeys[idx];
 
1157
}
 
1158
 
 
1159
/*
 
1160
 * This OpenSSL callback function is called when OpenSSL
 
1161
 * does client authentication and verifies the certificate chain.
 
1162
 */
 
1163
int ssl_callback_SSLVerify(int ok, X509_STORE_CTX *ctx)
 
1164
{
 
1165
    /* Get Apache context back through OpenSSL context */
 
1166
    SSL *ssl = X509_STORE_CTX_get_ex_data(ctx,
 
1167
                                          SSL_get_ex_data_X509_STORE_CTX_idx());
 
1168
    conn_rec *conn      = (conn_rec *)SSL_get_app_data(ssl);
 
1169
    server_rec *s       = conn->base_server;
 
1170
    request_rec *r      = (request_rec *)SSL_get_app_data2(ssl);
 
1171
 
 
1172
    SSLSrvConfigRec *sc = mySrvConfig(s);
 
1173
    SSLDirConfigRec *dc = r ? myDirConfig(r) : NULL;
 
1174
    SSLConnRec *sslconn = myConnConfig(conn);
 
1175
    modssl_ctx_t *mctx  = myCtxConfig(sslconn, sc);
 
1176
 
 
1177
    /* Get verify ingredients */
 
1178
    int errnum   = X509_STORE_CTX_get_error(ctx);
 
1179
    int errdepth = X509_STORE_CTX_get_error_depth(ctx);
 
1180
    int depth, verify;
 
1181
 
 
1182
    /*
 
1183
     * Log verification information
 
1184
     */
 
1185
    if (s->loglevel >= APLOG_DEBUG) {
 
1186
        X509 *cert  = X509_STORE_CTX_get_current_cert(ctx);
 
1187
        char *sname = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
 
1188
        char *iname = X509_NAME_oneline(X509_get_issuer_name(cert),  NULL, 0);
 
1189
 
 
1190
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1191
                     "Certificate Verification: "
 
1192
                     "depth: %d, subject: %s, issuer: %s",
 
1193
                     errdepth,
 
1194
                     sname ? sname : "-unknown-",
 
1195
                     iname ? iname : "-unknown-");
 
1196
 
 
1197
        if (sname) {
 
1198
            modssl_free(sname);
 
1199
        }
 
1200
 
 
1201
        if (iname) {
 
1202
            modssl_free(iname);
 
1203
        }
 
1204
    }
 
1205
 
 
1206
    /*
 
1207
     * Check for optionally acceptable non-verifiable issuer situation
 
1208
     */
 
1209
    if (dc && (dc->nVerifyClient != SSL_CVERIFY_UNSET)) {
 
1210
        verify = dc->nVerifyClient;
 
1211
    }
 
1212
    else {
 
1213
        verify = mctx->auth.verify_mode;
 
1214
    }
 
1215
 
 
1216
    if (verify == SSL_CVERIFY_NONE) {
 
1217
        /*
 
1218
         * SSLProxyVerify is either not configured or set to "none".
 
1219
         * (this callback doesn't happen in the server context if SSLVerify
 
1220
         *  is not configured or set to "none")
 
1221
         */
 
1222
        return TRUE;
 
1223
    }
 
1224
 
 
1225
    if (ssl_verify_error_is_optional(errnum) &&
 
1226
        (verify == SSL_CVERIFY_OPTIONAL_NO_CA))
 
1227
    {
 
1228
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1229
                     "Certificate Verification: Verifiable Issuer is "
 
1230
                     "configured as optional, therefore we're accepting "
 
1231
                     "the certificate");
 
1232
 
 
1233
        sslconn->verify_info = "GENEROUS";
 
1234
        ok = TRUE;
 
1235
    }
 
1236
 
 
1237
    /*
 
1238
     * Additionally perform CRL-based revocation checks
 
1239
     */
 
1240
    if (ok) {
 
1241
        if (!(ok = ssl_callback_SSLVerify_CRL(ok, ctx, conn))) {
 
1242
            errnum = X509_STORE_CTX_get_error(ctx);
 
1243
        }
 
1244
    }
 
1245
 
 
1246
    /*
 
1247
     * If we already know it's not ok, log the real reason
 
1248
     */
 
1249
    if (!ok) {
 
1250
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
 
1251
                     "Certificate Verification: Error (%d): %s",
 
1252
                     errnum, X509_verify_cert_error_string(errnum));
 
1253
 
 
1254
        if (sslconn->client_cert) {
 
1255
            X509_free(sslconn->client_cert);
 
1256
            sslconn->client_cert = NULL;
 
1257
        }
 
1258
        sslconn->client_dn = NULL;
 
1259
        sslconn->verify_error = X509_verify_cert_error_string(errnum);
 
1260
    }
 
1261
 
 
1262
    /*
 
1263
     * Finally check the depth of the certificate verification
 
1264
     */
 
1265
    if (dc && (dc->nVerifyDepth != UNSET)) {
 
1266
        depth = dc->nVerifyDepth;
 
1267
    }
 
1268
    else {
 
1269
        depth = mctx->auth.verify_depth;
 
1270
    }
 
1271
 
 
1272
    if (errdepth > depth) {
 
1273
        ap_log_error(APLOG_MARK, APLOG_ERR, 0, s,
 
1274
                     "Certificate Verification: Certificate Chain too long "
 
1275
                     "(chain has %d certificates, but maximum allowed are "
 
1276
                     "only %d)",
 
1277
                     errdepth, depth);
 
1278
 
 
1279
        errnum = X509_V_ERR_CERT_CHAIN_TOO_LONG;
 
1280
        sslconn->verify_error = X509_verify_cert_error_string(errnum);
 
1281
 
 
1282
        ok = FALSE;
 
1283
    }
 
1284
 
 
1285
    /*
 
1286
     * And finally signal OpenSSL the (perhaps changed) state
 
1287
     */
 
1288
    return ok;
 
1289
}
 
1290
 
 
1291
int ssl_callback_SSLVerify_CRL(int ok, X509_STORE_CTX *ctx, conn_rec *c)
 
1292
{
 
1293
    server_rec *s       = c->base_server;
 
1294
    SSLSrvConfigRec *sc = mySrvConfig(s);
 
1295
    SSLConnRec *sslconn = myConnConfig(c);
 
1296
    modssl_ctx_t *mctx  = myCtxConfig(sslconn, sc);
 
1297
    X509_OBJECT obj;
 
1298
    X509_NAME *subject, *issuer;
 
1299
    X509 *cert;
 
1300
    X509_CRL *crl;
 
1301
    EVP_PKEY *pubkey;
 
1302
    int i, n, rc;
 
1303
 
 
1304
    /*
 
1305
     * Unless a revocation store for CRLs was created we
 
1306
     * cannot do any CRL-based verification, of course.
 
1307
     */
 
1308
    if (!mctx->crl) {
 
1309
        return ok;
 
1310
    }
 
1311
 
 
1312
    /*
 
1313
     * Determine certificate ingredients in advance
 
1314
     */
 
1315
    cert    = X509_STORE_CTX_get_current_cert(ctx);
 
1316
    subject = X509_get_subject_name(cert);
 
1317
    issuer  = X509_get_issuer_name(cert);
 
1318
 
 
1319
    /*
 
1320
     * OpenSSL provides the general mechanism to deal with CRLs but does not
 
1321
     * use them automatically when verifying certificates, so we do it
 
1322
     * explicitly here. We will check the CRL for the currently checked
 
1323
     * certificate, if there is such a CRL in the store.
 
1324
     *
 
1325
     * We come through this procedure for each certificate in the certificate
 
1326
     * chain, starting with the root-CA's certificate. At each step we've to
 
1327
     * both verify the signature on the CRL (to make sure it's a valid CRL)
 
1328
     * and it's revocation list (to make sure the current certificate isn't
 
1329
     * revoked).  But because to check the signature on the CRL we need the
 
1330
     * public key of the issuing CA certificate (which was already processed
 
1331
     * one round before), we've a little problem. But we can both solve it and
 
1332
     * at the same time optimize the processing by using the following
 
1333
     * verification scheme (idea and code snippets borrowed from the GLOBUS
 
1334
     * project):
 
1335
     *
 
1336
     * 1. We'll check the signature of a CRL in each step when we find a CRL
 
1337
     *    through the _subject_ name of the current certificate. This CRL
 
1338
     *    itself will be needed the first time in the next round, of course.
 
1339
     *    But we do the signature processing one round before this where the
 
1340
     *    public key of the CA is available.
 
1341
     *
 
1342
     * 2. We'll check the revocation list of a CRL in each step when
 
1343
     *    we find a CRL through the _issuer_ name of the current certificate.
 
1344
     *    This CRLs signature was then already verified one round before.
 
1345
     *
 
1346
     * This verification scheme allows a CA to revoke its own certificate as
 
1347
     * well, of course.
 
1348
     */
 
1349
 
 
1350
    /*
 
1351
     * Try to retrieve a CRL corresponding to the _subject_ of
 
1352
     * the current certificate in order to verify it's integrity.
 
1353
     */
 
1354
    memset((char *)&obj, 0, sizeof(obj));
 
1355
    rc = SSL_X509_STORE_lookup(mctx->crl,
 
1356
                               X509_LU_CRL, subject, &obj);
 
1357
    crl = obj.data.crl;
 
1358
 
 
1359
    if ((rc > 0) && crl) {
 
1360
        /*
 
1361
         * Log information about CRL
 
1362
         * (A little bit complicated because of ASN.1 and BIOs...)
 
1363
         */
 
1364
        if (s->loglevel >= APLOG_DEBUG) {
 
1365
            char buff[512]; /* should be plenty */
 
1366
            BIO *bio = BIO_new(BIO_s_mem());
 
1367
 
 
1368
            BIO_printf(bio, "CA CRL: Issuer: ");
 
1369
            X509_NAME_print(bio, issuer, 0);
 
1370
 
 
1371
            BIO_printf(bio, ", lastUpdate: ");
 
1372
            ASN1_UTCTIME_print(bio, X509_CRL_get_lastUpdate(crl));
 
1373
 
 
1374
            BIO_printf(bio, ", nextUpdate: ");
 
1375
            ASN1_UTCTIME_print(bio, X509_CRL_get_nextUpdate(crl));
 
1376
 
 
1377
            n = BIO_read(bio, buff, sizeof(buff) - 1);
 
1378
            buff[n] = '\0';
 
1379
 
 
1380
            BIO_free(bio);
 
1381
 
 
1382
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s, "%s", buff);
 
1383
        }
 
1384
 
 
1385
        /*
 
1386
         * Verify the signature on this CRL
 
1387
         */
 
1388
        pubkey = X509_get_pubkey(cert);
 
1389
        rc = X509_CRL_verify(crl, pubkey);
 
1390
#ifdef OPENSSL_VERSION_NUMBER
 
1391
        /* Only refcounted in OpenSSL */
 
1392
        if (pubkey)
 
1393
            EVP_PKEY_free(pubkey);
 
1394
#endif
 
1395
        if (rc <= 0) {
 
1396
            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
 
1397
                         "Invalid signature on CRL");
 
1398
 
 
1399
            X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
 
1400
            X509_OBJECT_free_contents(&obj);
 
1401
            return FALSE;
 
1402
        }
 
1403
 
 
1404
        /*
 
1405
         * Check date of CRL to make sure it's not expired
 
1406
         */
 
1407
        i = X509_cmp_current_time(X509_CRL_get_nextUpdate(crl));
 
1408
 
 
1409
        if (i == 0) {
 
1410
            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
 
1411
                         "Found CRL has invalid nextUpdate field");
 
1412
 
 
1413
            X509_STORE_CTX_set_error(ctx,
 
1414
                                     X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
 
1415
            X509_OBJECT_free_contents(&obj);
 
1416
 
 
1417
            return FALSE;
 
1418
        }
 
1419
 
 
1420
        if (i < 0) {
 
1421
            ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
 
1422
                         "Found CRL is expired - "
 
1423
                         "revoking all certificates until you get updated CRL");
 
1424
 
 
1425
            X509_STORE_CTX_set_error(ctx, X509_V_ERR_CRL_HAS_EXPIRED);
 
1426
            X509_OBJECT_free_contents(&obj);
 
1427
 
 
1428
            return FALSE;
 
1429
        }
 
1430
 
 
1431
        X509_OBJECT_free_contents(&obj);
 
1432
    }
 
1433
 
 
1434
    /*
 
1435
     * Try to retrieve a CRL corresponding to the _issuer_ of
 
1436
     * the current certificate in order to check for revocation.
 
1437
     */
 
1438
    memset((char *)&obj, 0, sizeof(obj));
 
1439
    rc = SSL_X509_STORE_lookup(mctx->crl,
 
1440
                               X509_LU_CRL, issuer, &obj);
 
1441
 
 
1442
    crl = obj.data.crl;
 
1443
    if ((rc > 0) && crl) {
 
1444
        /*
 
1445
         * Check if the current certificate is revoked by this CRL
 
1446
         */
 
1447
        n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
 
1448
 
 
1449
        for (i = 0; i < n; i++) {
 
1450
            X509_REVOKED *revoked =
 
1451
                sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
 
1452
 
 
1453
            ASN1_INTEGER *sn = X509_REVOKED_get_serialNumber(revoked);
 
1454
 
 
1455
            if (!ASN1_INTEGER_cmp(sn, X509_get_serialNumber(cert))) {
 
1456
                if (s->loglevel >= APLOG_DEBUG) {
 
1457
                    char *cp = X509_NAME_oneline(issuer, NULL, 0);
 
1458
                    long serial = ASN1_INTEGER_get(sn);
 
1459
 
 
1460
                    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
 
1461
                                 "Certificate with serial %ld (0x%lX) "
 
1462
                                 "revoked per CRL from issuer %s",
 
1463
                                 serial, serial, cp);
 
1464
                    modssl_free(cp);
 
1465
                }
 
1466
 
 
1467
                X509_STORE_CTX_set_error(ctx, X509_V_ERR_CERT_REVOKED);
 
1468
                X509_OBJECT_free_contents(&obj);
 
1469
 
 
1470
                return FALSE;
 
1471
            }
 
1472
        }
 
1473
 
 
1474
        X509_OBJECT_free_contents(&obj);
 
1475
    }
 
1476
 
 
1477
    return ok;
 
1478
}
 
1479
 
 
1480
#define SSLPROXY_CERT_CB_LOG_FMT \
 
1481
   "Proxy client certificate callback: (%s) "
 
1482
 
 
1483
static void modssl_proxy_info_log(server_rec *s,
 
1484
                                  X509_INFO *info,
 
1485
                                  const char *msg)
 
1486
{
 
1487
    SSLSrvConfigRec *sc = mySrvConfig(s);
 
1488
    char name_buf[256];
 
1489
    X509_NAME *name;
 
1490
    char *dn;
 
1491
 
 
1492
    if (s->loglevel < APLOG_DEBUG) {
 
1493
        return;
 
1494
    }
 
1495
 
 
1496
    name = X509_get_subject_name(info->x509);
 
1497
    dn = X509_NAME_oneline(name, name_buf, sizeof(name_buf));
 
1498
 
 
1499
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1500
                 SSLPROXY_CERT_CB_LOG_FMT "%s, sending %s",
 
1501
                 sc->vhost_id, msg, dn ? dn : "-uknown-");
 
1502
}
 
1503
 
 
1504
/*
 
1505
 * caller will decrement the cert and key reference
 
1506
 * so we need to increment here to prevent them from
 
1507
 * being freed.
 
1508
 */
 
1509
#define modssl_set_cert_info(info, cert, pkey) \
 
1510
    *cert = info->x509; \
 
1511
    X509_reference_inc(*cert); \
 
1512
    *pkey = info->x_pkey->dec_pkey; \
 
1513
    EVP_PKEY_reference_inc(*pkey)
 
1514
 
 
1515
int ssl_callback_proxy_cert(SSL *ssl, MODSSL_CLIENT_CERT_CB_ARG_TYPE **x509, EVP_PKEY **pkey)
 
1516
{
 
1517
    conn_rec *c = (conn_rec *)SSL_get_app_data(ssl);
 
1518
    server_rec *s = c->base_server;
 
1519
    SSLSrvConfigRec *sc = mySrvConfig(s);
 
1520
    X509_NAME *ca_name, *issuer;
 
1521
    X509_INFO *info;
 
1522
    STACK_OF(X509_NAME) *ca_list;
 
1523
    STACK_OF(X509_INFO) *certs = sc->proxy->pkp->certs;
 
1524
    int i, j;
 
1525
 
 
1526
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1527
                 SSLPROXY_CERT_CB_LOG_FMT "entered",
 
1528
                 sc->vhost_id);
 
1529
 
 
1530
    if (!certs || (sk_X509_INFO_num(certs) <= 0)) {
 
1531
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
 
1532
                     SSLPROXY_CERT_CB_LOG_FMT
 
1533
                     "downstream server wanted client certificate "
 
1534
                     "but none are configured", sc->vhost_id);
 
1535
        return FALSE;
 
1536
    }
 
1537
 
 
1538
    ca_list = SSL_get_client_CA_list(ssl);
 
1539
 
 
1540
    if (!ca_list || (sk_X509_NAME_num(ca_list) <= 0)) {
 
1541
        /*
 
1542
         * downstream server didn't send us a list of acceptable CA certs,
 
1543
         * so we send the first client cert in the list.
 
1544
         */
 
1545
        info = sk_X509_INFO_value(certs, 0);
 
1546
 
 
1547
        modssl_proxy_info_log(s, info, "no acceptable CA list");
 
1548
 
 
1549
        modssl_set_cert_info(info, x509, pkey);
 
1550
 
 
1551
        return TRUE;
 
1552
    }
 
1553
 
 
1554
    for (i = 0; i < sk_X509_NAME_num(ca_list); i++) {
 
1555
        ca_name = sk_X509_NAME_value(ca_list, i);
 
1556
 
 
1557
        for (j = 0; j < sk_X509_INFO_num(certs); j++) {
 
1558
            info = sk_X509_INFO_value(certs, j);
 
1559
            issuer = X509_get_issuer_name(info->x509);
 
1560
 
 
1561
            if (X509_NAME_cmp(issuer, ca_name) == 0) {
 
1562
                modssl_proxy_info_log(s, info, "found acceptable cert");
 
1563
 
 
1564
                modssl_set_cert_info(info, x509, pkey);
 
1565
 
 
1566
                return TRUE;
 
1567
            }
 
1568
        }
 
1569
    }
 
1570
 
 
1571
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1572
                 SSLPROXY_CERT_CB_LOG_FMT
 
1573
                 "no client certificate found!?", sc->vhost_id);
 
1574
 
 
1575
    return FALSE;
 
1576
}
 
1577
 
 
1578
static void ssl_session_log(server_rec *s,
 
1579
                            const char *request,
 
1580
                            unsigned char *id,
 
1581
                            unsigned int idlen,
 
1582
                            const char *status,
 
1583
                            const char *result,
 
1584
                            long timeout)
 
1585
{
 
1586
    char buf[SSL_SESSION_ID_STRING_LEN];
 
1587
    char timeout_str[56] = {'\0'};
 
1588
 
 
1589
    if (s->loglevel < APLOG_DEBUG) {
 
1590
        return;
 
1591
    }
 
1592
 
 
1593
    if (timeout) {
 
1594
        apr_snprintf(timeout_str, sizeof(timeout_str),
 
1595
                     "timeout=%lds ", (timeout - time(NULL)));
 
1596
    }
 
1597
 
 
1598
    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1599
                 "Inter-Process Session Cache: "
 
1600
                 "request=%s status=%s id=%s %s(session %s)",
 
1601
                 request, status,
 
1602
                 SSL_SESSION_id2sz(id, idlen, buf, sizeof(buf)),
 
1603
                 timeout_str, result);
 
1604
}
 
1605
 
 
1606
/*
 
1607
 *  This callback function is executed by OpenSSL whenever a new SSL_SESSION is
 
1608
 *  added to the internal OpenSSL session cache. We use this hook to spread the
 
1609
 *  SSL_SESSION also to the inter-process disk-cache to make share it with our
 
1610
 *  other Apache pre-forked server processes.
 
1611
 */
 
1612
int ssl_callback_NewSessionCacheEntry(SSL *ssl, SSL_SESSION *session)
 
1613
{
 
1614
    /* Get Apache context back through OpenSSL context */
 
1615
    conn_rec *conn      = (conn_rec *)SSL_get_app_data(ssl);
 
1616
    server_rec *s       = conn->base_server;
 
1617
    SSLSrvConfigRec *sc = mySrvConfig(s);
 
1618
    long timeout        = sc->session_cache_timeout;
 
1619
    BOOL rc;
 
1620
    unsigned char *id;
 
1621
    unsigned int idlen;
 
1622
 
 
1623
    /*
 
1624
     * Set the timeout also for the internal OpenSSL cache, because this way
 
1625
     * our inter-process cache is consulted only when it's really necessary.
 
1626
     */
 
1627
    SSL_set_timeout(session, timeout);
 
1628
 
 
1629
    /*
 
1630
     * Store the SSL_SESSION in the inter-process cache with the
 
1631
     * same expire time, so it expires automatically there, too.
 
1632
     */
 
1633
    id = SSL_SESSION_get_session_id(session);
 
1634
    idlen = SSL_SESSION_get_session_id_length(session);
 
1635
 
 
1636
    timeout += modssl_session_get_time(session);
 
1637
 
 
1638
    rc = ssl_scache_store(s, id, idlen, timeout, session);
 
1639
 
 
1640
    ssl_session_log(s, "SET", id, idlen,
 
1641
                    rc == TRUE ? "OK" : "BAD",
 
1642
                    "caching", timeout);
 
1643
 
 
1644
    /*
 
1645
     * return 0 which means to OpenSSL that the session is still
 
1646
     * valid and was not freed by us with SSL_SESSION_free().
 
1647
     */
 
1648
    return 0;
 
1649
}
 
1650
 
 
1651
/*
 
1652
 *  This callback function is executed by OpenSSL whenever a
 
1653
 *  SSL_SESSION is looked up in the internal OpenSSL cache and it
 
1654
 *  was not found. We use this to lookup the SSL_SESSION in the
 
1655
 *  inter-process disk-cache where it was perhaps stored by one
 
1656
 *  of our other Apache pre-forked server processes.
 
1657
 */
 
1658
SSL_SESSION *ssl_callback_GetSessionCacheEntry(SSL *ssl,
 
1659
                                               unsigned char *id,
 
1660
                                               int idlen, int *do_copy)
 
1661
{
 
1662
    /* Get Apache context back through OpenSSL context */
 
1663
    conn_rec *conn = (conn_rec *)SSL_get_app_data(ssl);
 
1664
    server_rec *s  = conn->base_server;
 
1665
    SSL_SESSION *session;
 
1666
 
 
1667
    /*
 
1668
     * Try to retrieve the SSL_SESSION from the inter-process cache
 
1669
     */
 
1670
    session = ssl_scache_retrieve(s, id, idlen);
 
1671
 
 
1672
    ssl_session_log(s, "GET", id, idlen,
 
1673
                    session ? "FOUND" : "MISSED",
 
1674
                    session ? "reuse" : "renewal", 0);
 
1675
 
 
1676
    /*
 
1677
     * Return NULL or the retrieved SSL_SESSION. But indicate (by
 
1678
     * setting do_copy to 0) that the reference count on the
 
1679
     * SSL_SESSION should not be incremented by the SSL library,
 
1680
     * because we will no longer hold a reference to it ourself.
 
1681
     */
 
1682
    *do_copy = 0;
 
1683
 
 
1684
    return session;
 
1685
}
 
1686
 
 
1687
/*
 
1688
 *  This callback function is executed by OpenSSL whenever a
 
1689
 *  SSL_SESSION is removed from the the internal OpenSSL cache.
 
1690
 *  We use this to remove the SSL_SESSION in the inter-process
 
1691
 *  disk-cache, too.
 
1692
 */
 
1693
void ssl_callback_DelSessionCacheEntry(SSL_CTX *ctx,
 
1694
                                       SSL_SESSION *session)
 
1695
{
 
1696
    server_rec *s;
 
1697
    SSLSrvConfigRec *sc;
 
1698
    unsigned char *id;
 
1699
    unsigned int idlen;
 
1700
 
 
1701
    /*
 
1702
     * Get Apache context back through OpenSSL context
 
1703
     */
 
1704
    if (!(s = (server_rec *)SSL_CTX_get_app_data(ctx))) {
 
1705
        return; /* on server shutdown Apache is already gone */
 
1706
    }
 
1707
 
 
1708
    sc = mySrvConfig(s);
 
1709
 
 
1710
    /*
 
1711
     * Remove the SSL_SESSION from the inter-process cache
 
1712
     */
 
1713
    id = SSL_SESSION_get_session_id(session);
 
1714
    idlen = SSL_SESSION_get_session_id_length(session);
 
1715
 
 
1716
    ssl_scache_remove(s, id, idlen);
 
1717
 
 
1718
    ssl_session_log(s, "REM", id, idlen,
 
1719
                    "OK", "dead", 0);
 
1720
 
 
1721
    return;
 
1722
}
 
1723
 
 
1724
/*
 
1725
 * This callback function is executed while OpenSSL processes the
 
1726
 * SSL handshake and does SSL record layer stuff. We use it to
 
1727
 * trace OpenSSL's processing in out SSL logfile.
 
1728
 */
 
1729
void ssl_callback_LogTracingState(MODSSL_INFO_CB_ARG_TYPE ssl, int where, int rc)
 
1730
{
 
1731
    conn_rec *c;
 
1732
    server_rec *s;
 
1733
    SSLSrvConfigRec *sc;
 
1734
 
 
1735
    /*
 
1736
     * find corresponding server
 
1737
     */
 
1738
    if (!(c = (conn_rec *)SSL_get_app_data((SSL *)ssl))) {
 
1739
        return;
 
1740
    }
 
1741
 
 
1742
    s = c->base_server;
 
1743
    if (!(sc = mySrvConfig(s))) {
 
1744
        return;
 
1745
    }
 
1746
 
 
1747
    /*
 
1748
     * create the various trace messages
 
1749
     */
 
1750
    if (s->loglevel >= APLOG_DEBUG) {
 
1751
        if (where & SSL_CB_HANDSHAKE_START) {
 
1752
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1753
                         "%s: Handshake: start", SSL_LIBRARY_NAME);
 
1754
        }
 
1755
        else if (where & SSL_CB_HANDSHAKE_DONE) {
 
1756
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1757
                         "%s: Handshake: done", SSL_LIBRARY_NAME);
 
1758
        }
 
1759
        else if (where & SSL_CB_LOOP) {
 
1760
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1761
                         "%s: Loop: %s",
 
1762
                         SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
 
1763
        }
 
1764
        else if (where & SSL_CB_READ) {
 
1765
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1766
                         "%s: Read: %s",
 
1767
                         SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
 
1768
        }
 
1769
        else if (where & SSL_CB_WRITE) {
 
1770
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1771
                         "%s: Write: %s",
 
1772
                         SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
 
1773
        }
 
1774
        else if (where & SSL_CB_ALERT) {
 
1775
            char *str = (where & SSL_CB_READ) ? "read" : "write";
 
1776
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1777
                         "%s: Alert: %s:%s:%s",
 
1778
                         SSL_LIBRARY_NAME, str,
 
1779
                         SSL_alert_type_string_long(rc),
 
1780
                         SSL_alert_desc_string_long(rc));
 
1781
        }
 
1782
        else if (where & SSL_CB_EXIT) {
 
1783
            if (rc == 0) {
 
1784
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1785
                             "%s: Exit: failed in %s",
 
1786
                             SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
 
1787
            }
 
1788
            else if (rc < 0) {
 
1789
                ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
 
1790
                             "%s: Exit: error in %s",
 
1791
                             SSL_LIBRARY_NAME, SSL_state_string_long(ssl));
 
1792
            }
 
1793
        }
 
1794
    }
 
1795
 
 
1796
    /*
 
1797
     * Because SSL renegotations can happen at any time (not only after
 
1798
     * SSL_accept()), the best way to log the current connection details is
 
1799
     * right after a finished handshake.
 
1800
     */
 
1801
    if (where & SSL_CB_HANDSHAKE_DONE) {
 
1802
        ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
 
1803
                     "Connection: Client IP: %s, Protocol: %s, "
 
1804
                     "Cipher: %s (%s/%s bits)",
 
1805
                     ssl_var_lookup(NULL, s, c, NULL, "REMOTE_ADDR"),
 
1806
                     ssl_var_lookup(NULL, s, c, NULL, "SSL_PROTOCOL"),
 
1807
                     ssl_var_lookup(NULL, s, c, NULL, "SSL_CIPHER"),
 
1808
                     ssl_var_lookup(NULL, s, c, NULL, "SSL_CIPHER_USEKEYSIZE"),
 
1809
                     ssl_var_lookup(NULL, s, c, NULL, "SSL_CIPHER_ALGKEYSIZE"));
 
1810
    }
 
1811
}
 
1812