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

« back to all changes in this revision

Viewing changes to modules/ssl/ssl_engine_rand.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_rand.c
 
24
 *  Random Number Generator Seeding
 
25
 */
 
26
                             /* ``The generation of random
 
27
                                  numbers is too important
 
28
                                  to be left to chance.'' */
 
29
 
 
30
#include "ssl_private.h"
 
31
 
 
32
/*  _________________________________________________________________
 
33
**
 
34
**  Support for better seeding of SSL library's RNG
 
35
**  _________________________________________________________________
 
36
*/
 
37
 
 
38
static int ssl_rand_choosenum(int, int);
 
39
static int ssl_rand_feedfp(apr_pool_t *, apr_file_t *, int);
 
40
 
 
41
int ssl_rand_seed(server_rec *s, apr_pool_t *p, ssl_rsctx_t nCtx, char *prefix)
 
42
{
 
43
    SSLModConfigRec *mc;
 
44
    apr_array_header_t *apRandSeed;
 
45
    ssl_randseed_t *pRandSeeds;
 
46
    ssl_randseed_t *pRandSeed;
 
47
    unsigned char stackdata[256];
 
48
    int nReq, nDone;
 
49
    apr_file_t *fp;
 
50
    int i, n, l;
 
51
 
 
52
    mc = myModConfig(s);
 
53
    nReq  = 0;
 
54
    nDone = 0;
 
55
    apRandSeed = mc->aRandSeed;
 
56
    pRandSeeds = (ssl_randseed_t *)apRandSeed->elts;
 
57
    for (i = 0; i < apRandSeed->nelts; i++) {
 
58
        pRandSeed = &pRandSeeds[i];
 
59
        if (pRandSeed->nCtx == nCtx) {
 
60
            nReq += pRandSeed->nBytes;
 
61
            if (pRandSeed->nSrc == SSL_RSSRC_FILE) {
 
62
                /*
 
63
                 * seed in contents of an external file
 
64
                 */
 
65
                if (apr_file_open(&fp, pRandSeed->cpPath,
 
66
                                  APR_READ, APR_OS_DEFAULT, p) != APR_SUCCESS)
 
67
                    continue;
 
68
                nDone += ssl_rand_feedfp(p, fp, pRandSeed->nBytes);
 
69
                apr_file_close(fp);
 
70
            }
 
71
            else if (pRandSeed->nSrc == SSL_RSSRC_EXEC) {
 
72
                const char *cmd = pRandSeed->cpPath;
 
73
                const char **argv = apr_palloc(p, sizeof(char *) * 3);
 
74
                /*
 
75
                 * seed in contents generated by an external program
 
76
                 */
 
77
                argv[0] = cmd;
 
78
                argv[1] = apr_itoa(p, pRandSeed->nBytes);
 
79
                argv[2] = NULL;
 
80
 
 
81
                if ((fp = ssl_util_ppopen(s, p, cmd, argv)) == NULL)
 
82
                    continue;
 
83
                nDone += ssl_rand_feedfp(p, fp, pRandSeed->nBytes);
 
84
                ssl_util_ppclose(s, p, fp);
 
85
            }
 
86
#ifdef HAVE_SSL_RAND_EGD
 
87
            else if (pRandSeed->nSrc == SSL_RSSRC_EGD) {
 
88
                /*
 
89
                 * seed in contents provided by the external
 
90
                 * Entropy Gathering Daemon (EGD)
 
91
                 */
 
92
                if ((n = RAND_egd(pRandSeed->cpPath)) == -1)
 
93
                    continue;
 
94
                nDone += n;
 
95
            }
 
96
#endif
 
97
            else if (pRandSeed->nSrc == SSL_RSSRC_BUILTIN) {
 
98
                struct {
 
99
                    time_t t;
 
100
                    pid_t pid;
 
101
                } my_seed;
 
102
 
 
103
                /*
 
104
                 * seed in the current time (usually just 4 bytes)
 
105
                 */
 
106
                my_seed.t = time(NULL);
 
107
 
 
108
                /*
 
109
                 * seed in the current process id (usually just 4 bytes)
 
110
                 */
 
111
                my_seed.pid = mc->pid;
 
112
 
 
113
                l = sizeof(my_seed);
 
114
                RAND_seed((unsigned char *)&my_seed, l);
 
115
                nDone += l;
 
116
 
 
117
                /*
 
118
                 * seed in some current state of the run-time stack (128 bytes)
 
119
                 */
 
120
                n = ssl_rand_choosenum(0, sizeof(stackdata)-128-1);
 
121
                RAND_seed(stackdata+n, 128);
 
122
                nDone += 128;
 
123
 
 
124
            }
 
125
        }
 
126
    }
 
127
    ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
 
128
                 "%sSeeding PRNG with %d bytes of entropy", prefix, nDone);
 
129
 
 
130
    if (RAND_status() == 0)
 
131
        ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s,
 
132
                     "%sPRNG still contains insufficient entropy!", prefix);
 
133
 
 
134
    return nDone;
 
135
}
 
136
 
 
137
#define BUFSIZE 8192
 
138
 
 
139
static int ssl_rand_feedfp(apr_pool_t *p, apr_file_t *fp, int nReq)
 
140
{
 
141
    apr_size_t nDone;
 
142
    unsigned char caBuf[BUFSIZE];
 
143
    apr_size_t nBuf;
 
144
    apr_size_t nRead;
 
145
    apr_size_t nTodo;
 
146
 
 
147
    nDone = 0;
 
148
    nRead = BUFSIZE;
 
149
    nTodo = nReq;
 
150
    while (1) {
 
151
        if (nReq > 0)
 
152
            nRead = (nTodo < BUFSIZE ? nTodo : BUFSIZE);
 
153
        nBuf = nRead;
 
154
        if (apr_file_read(fp, caBuf, &nBuf) != APR_SUCCESS)
 
155
            break;
 
156
        RAND_seed(caBuf, nBuf);
 
157
        nDone += nBuf;
 
158
        if (nReq > 0) {
 
159
            nTodo -= nBuf;
 
160
            if (nTodo <= 0)
 
161
                break;
 
162
        }
 
163
    }
 
164
    return nDone;
 
165
}
 
166
 
 
167
static int ssl_rand_choosenum(int l, int h)
 
168
{
 
169
    int i;
 
170
    char buf[50];
 
171
 
 
172
    apr_snprintf(buf, sizeof(buf), "%.0f",
 
173
                 (((double)(rand()%RAND_MAX)/RAND_MAX)*(h-l)));
 
174
    i = atoi(buf)+1;
 
175
    if (i < l) i = l;
 
176
    if (i > h) i = h;
 
177
    return i;
 
178
}
 
179