ssl_engine_rand.c revision 577c9a5040351bb9c7c68d7b90e1a786529a4e36
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** _ __ ___ ___ __| | ___ ___| | mod_ssl
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** | | | | | | (_) | (_| | \__ \__ \ | www.modssl.org
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** |_| |_| |_|\___/ \__,_|___|___/___/_| ftp.modssl.org
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** Random Number Generator Seeding
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe/* ====================================================================
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * The Apache Software License, Version 1.1
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * reserved.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Redistribution and use in source and binary forms, with or without
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * modification, are permitted provided that the following conditions
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * 1. Redistributions of source code must retain the above copyright
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * notice, this list of conditions and the following disclaimer.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * 2. Redistributions in binary form must reproduce the above copyright
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * notice, this list of conditions and the following disclaimer in
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * the documentation and/or other materials provided with the
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * distribution.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * 3. The end-user documentation included with the redistribution,
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * if any, must include the following acknowledgment:
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * "This product includes software developed by the
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Apache Software Foundation (http://www.apache.org/)."
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Alternately, this acknowledgment may appear in the software itself,
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * if and wherever such third-party acknowledgments normally appear.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * 4. The names "Apache" and "Apache Software Foundation" must
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * not be used to endorse or promote products derived from this
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * software without prior written permission. For written
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * permission, please contact apache@apache.org.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * 5. Products derived from this software may not be called "Apache",
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * nor may "Apache" appear in their name, without prior written
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * permission of the Apache Software Foundation.
66b7d26b8c3164ddd1cd1ee1d0bec6cd5780301btrawick * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * SUCH DAMAGE.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * ====================================================================
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf /* ``The generation of random
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf numbers is too important
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe to be left to chance.'' */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe/* _________________________________________________________________
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** Support for better seeding of SSL library's RNG
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** _________________________________________________________________
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_choosenum(int, int);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_feedfp(apr_pool_t *, apr_file_t *, int);
a1a4e55fb7678118a8334019c18509c248b0c0b3wroweint ssl_rand_seed(server_rec *s, apr_pool_t *p, ssl_rsctx_t nCtx, char *prefix)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe int i, n, l;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in contents of an external file
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in contents generated by an external program
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if ((fp = ssl_util_ppopen(s, p, apr_psprintf(p, "%s %d",
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in contents provided by the external
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Entropy Gathering Daemon (EGD)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in the current time (usually just 4 bytes)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in the current process id (usually just 4 bytes)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe l = sizeof(my_seed);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in some current state of the run-time stack (128 bytes)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * XXX: This is entirely borked, sizeof(scoreboard) < 1024
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in an 1KB extract of the current scoreboard
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe RAND_seed(((unsigned char *)ap_scoreboard_image)+n, 1024);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_log(s, SSL_LOG_INFO, "%sSeeding PRNG with %d bytes of entropy", prefix, nDone);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_log(s, SSL_LOG_WARN, "%sPRNG still contains not sufficient entropy!", prefix);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_feedfp(apr_pool_t *p, apr_file_t *fp, int nReq)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe while (1) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_choosenum(int l, int h)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (i < l) i = l;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (i > h) i = h;