ssl_engine_rand.c revision 577c9a5040351bb9c7c68d7b90e1a786529a4e36
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe/* _ _
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** _ __ ___ ___ __| | ___ ___| | mod_ssl
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** | '_ ` _ \ / _ \ / _` | / __/ __| | Apache Interface to OpenSSL
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** | | | | | | (_) | (_| | \__ \__ \ | www.modssl.org
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** |_| |_| |_|\___/ \__,_|___|___/___/_| ftp.modssl.org
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** |_____|
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** ssl_engine_rand.c
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** Random Number Generator Seeding
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe*/
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe/* ====================================================================
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * The Apache Software License, Version 1.1
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe *
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Copyright (c) 2000-2001 The Apache Software Foundation. All rights
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * reserved.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe *
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Redistribution and use in source and binary forms, with or without
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * modification, are permitted provided that the following conditions
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * are met:
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe *
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * 1. Redistributions of source code must retain the above copyright
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * notice, this list of conditions and the following disclaimer.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe *
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 *
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 *
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 *
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.
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe *
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 * ====================================================================
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf /* ``The generation of random
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asf numbers is too important
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe to be left to chance.'' */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#include "mod_ssl.h"
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe/* _________________________________________________________________
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe**
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** Support for better seeding of SSL library's RNG
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe** _________________________________________________________________
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe*/
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_choosenum(int, int);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_feedfp(apr_pool_t *, apr_file_t *, int);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wroweint ssl_rand_seed(server_rec *s, apr_pool_t *p, ssl_rsctx_t nCtx, char *prefix)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe{
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe SSLModConfigRec *mc;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_array_header_t *apRandSeed;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_randseed_t *pRandSeeds;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_randseed_t *pRandSeed;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe unsigned char stackdata[256];
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe int nReq, nDone;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_file_t *fp;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe int i, n, l;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe mc = myModConfig(s);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nReq = 0;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone = 0;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apRandSeed = mc->aRandSeed;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe pRandSeeds = (ssl_randseed_t *)apRandSeed->elts;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe for (i = 0; i < apRandSeed->nelts; i++) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe pRandSeed = &pRandSeeds[i];
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (pRandSeed->nCtx == nCtx) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nReq += pRandSeed->nBytes;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (pRandSeed->nSrc == SSL_RSSRC_FILE) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in contents of an external file
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (apr_file_open(&fp, pRandSeed->cpPath,
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe APR_READ, APR_OS_DEFAULT, p) != APR_SUCCESS)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe continue;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += ssl_rand_feedfp(p, fp, pRandSeed->nBytes);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_file_close(fp);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe else if (pRandSeed->nSrc == SSL_RSSRC_EXEC) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in contents generated by an external program
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if ((fp = ssl_util_ppopen(s, p, apr_psprintf(p, "%s %d",
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe pRandSeed->cpPath, pRandSeed->nBytes))) == NULL)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe continue;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += ssl_rand_feedfp(p, fp, pRandSeed->nBytes);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_util_ppclose(s, p, fp);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#if SSL_LIBRARY_VERSION >= 0x00905100
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe else if (pRandSeed->nSrc == SSL_RSSRC_EGD) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in contents provided by the external
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * Entropy Gathering Daemon (EGD)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if ((n = RAND_egd(pRandSeed->cpPath)) == -1)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe continue;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += n;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#endif
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe else if (pRandSeed->nSrc == SSL_RSSRC_BUILTIN) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe struct {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe time_t t;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe pid_t pid;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe } my_seed;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in the current time (usually just 4 bytes)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe my_seed.t = time(NULL);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in the current process id (usually just 4 bytes)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe my_seed.pid = mc->pid;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe l = sizeof(my_seed);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe RAND_seed((unsigned char *)&my_seed, l);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += l;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in some current state of the run-time stack (128 bytes)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe n = ssl_rand_choosenum(0, sizeof(stackdata)-128-1);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe RAND_seed(stackdata+n, 128);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += 128;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#if XXX_SBENTROPY_SOLVED
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe /*
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * XXX: This is entirely borked, sizeof(scoreboard) < 1024
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe *
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe * seed in an 1KB extract of the current scoreboard
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe */
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (ap_scoreboard_image != NULL) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe n = ssl_rand_choosenum(0, sizeof(scoreboard)-1024-1);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe RAND_seed(((unsigned char *)ap_scoreboard_image)+n, 1024);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += 1024;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#endif
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_log(s, SSL_LOG_INFO, "%sSeeding PRNG with %d bytes of entropy", prefix, nDone);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#if SSL_LIBRARY_VERSION >= 0x00905100
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (RAND_status() == 0)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe ssl_log(s, SSL_LOG_WARN, "%sPRNG still contains not sufficient entropy!", prefix);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#endif
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe return nDone;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe}
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe#define BUFSIZE 8192
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_feedfp(apr_pool_t *p, apr_file_t *fp, int nReq)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe{
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_size_t nDone;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe unsigned char caBuf[BUFSIZE];
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_size_t nBuf;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_size_t nRead;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_size_t nTodo;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone = 0;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nRead = BUFSIZE;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nTodo = nReq;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe while (1) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (nReq > 0)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nRead = (nTodo < BUFSIZE ? nTodo : BUFSIZE);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nBuf = nRead;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (apr_file_read(fp, caBuf, &nBuf) != APR_SUCCESS)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe break;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe RAND_seed(caBuf, nBuf);
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nDone += nBuf;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (nReq > 0) {
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe nTodo -= nBuf;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (nTodo <= 0)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe break;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe }
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe return nDone;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe}
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowestatic int ssl_rand_choosenum(int l, int h)
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe{
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe int i;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe char buf[50];
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe apr_snprintf(buf, sizeof(buf), "%.0f",
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe (((double)(rand()%RAND_MAX)/RAND_MAX)*(h-l)));
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe i = atoi(buf)+1;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (i < l) i = l;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe if (i > h) i = h;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe return i;
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe}
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe
a1a4e55fb7678118a8334019c18509c248b0c0b3wrowe