2000-04-03 04:50:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000 Damien Miller. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2000-04-16 02:31:48 +00:00
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
2001-02-26 22:20:57 +00:00
|
|
|
#include <openssl/crypto.h>
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-07-15 04:59:14 +00:00
|
|
|
/* SunOS 4.4.4 needs this */
|
|
|
|
#ifdef HAVE_FLOATINGPOINT_H
|
|
|
|
# include <floatingpoint.h>
|
|
|
|
#endif /* HAVE_FLOATINGPOINT_H */
|
|
|
|
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "ssh.h"
|
2001-02-18 04:28:11 +00:00
|
|
|
#include "misc.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "atomicio.h"
|
2001-01-22 21:06:19 +00:00
|
|
|
#include "pathnames.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "log.h"
|
|
|
|
|
2001-06-27 13:36:08 +00:00
|
|
|
RCSID("$Id: entropy.c,v 1.37 2001/06/27 13:36:08 djm Exp $");
|
2000-04-03 04:50:43 +00:00
|
|
|
|
|
|
|
#ifndef offsetof
|
|
|
|
# define offsetof(type, member) ((size_t) &((type *)0)->member)
|
|
|
|
#endif
|
2000-06-07 12:20:23 +00:00
|
|
|
|
|
|
|
/* Number of times to pass through command list gathering entropy */
|
|
|
|
#define NUM_ENTROPY_RUNS 1
|
|
|
|
|
|
|
|
/* Scale entropy estimates back by this amount on subsequent runs */
|
|
|
|
#define SCALE_PER_RUN 10.0
|
|
|
|
|
|
|
|
/* Minimum number of commands to be considered valid */
|
|
|
|
#define MIN_ENTROPY_SOURCES 16
|
|
|
|
|
|
|
|
#define WHITESPACE " \t\n"
|
|
|
|
|
2000-06-18 04:07:04 +00:00
|
|
|
#ifndef RUSAGE_SELF
|
|
|
|
# define RUSAGE_SELF 0
|
|
|
|
#endif
|
|
|
|
#ifndef RUSAGE_CHILDREN
|
|
|
|
# define RUSAGE_CHILDREN 0
|
|
|
|
#endif
|
|
|
|
|
2001-02-26 21:39:07 +00:00
|
|
|
#if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
|
|
|
|
# define SAVED_IDS_WORK_WITH_SETEUID
|
|
|
|
#endif
|
|
|
|
|
2001-06-27 13:36:08 +00:00
|
|
|
static void
|
2001-03-18 22:38:15 +00:00
|
|
|
check_openssl_version(void)
|
2001-02-26 22:20:57 +00:00
|
|
|
{
|
|
|
|
if (SSLeay() != OPENSSL_VERSION_NUMBER)
|
2001-02-27 00:00:52 +00:00
|
|
|
fatal("OpenSSL version mismatch. Built against %lx, you "
|
|
|
|
"have %lx", OPENSSL_VERSION_NUMBER, SSLeay());
|
2001-02-26 22:20:57 +00:00
|
|
|
}
|
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
#if defined(PRNGD_SOCKET) || defined(PRNGD_PORT)
|
|
|
|
# define USE_PRNGD
|
|
|
|
#endif
|
2001-02-26 22:20:57 +00:00
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
#if defined(USE_PRNGD) || defined(RANDOM_POOL)
|
2000-06-07 12:20:23 +00:00
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
#ifdef USE_PRNGD
|
|
|
|
/* Collect entropy from PRNGD/EGD */
|
2001-03-18 22:38:15 +00:00
|
|
|
int
|
|
|
|
get_random_bytes(unsigned char *buf, int len)
|
2000-04-03 04:50:43 +00:00
|
|
|
{
|
2000-06-26 03:01:33 +00:00
|
|
|
int fd;
|
|
|
|
char msg[2];
|
2001-03-03 13:29:20 +00:00
|
|
|
#ifdef PRNGD_PORT
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
#else
|
2000-04-03 04:50:43 +00:00
|
|
|
struct sockaddr_un addr;
|
2001-03-03 13:29:20 +00:00
|
|
|
#endif
|
2001-02-18 01:44:29 +00:00
|
|
|
int addr_len, rval, errors;
|
2001-02-18 04:28:11 +00:00
|
|
|
mysig_t old_sigpipe;
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
memset(&addr, '\0', sizeof(addr));
|
|
|
|
|
|
|
|
#ifdef PRNGD_PORT
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
addr.sin_port = htons(PRNGD_PORT);
|
|
|
|
addr_len = sizeof(struct sockaddr_in);
|
|
|
|
#else /* use IP socket PRNGD_SOCKET instead */
|
2000-06-26 03:01:33 +00:00
|
|
|
/* Sanity checks */
|
2001-03-03 13:29:20 +00:00
|
|
|
if (sizeof(PRNGD_SOCKET) > sizeof(addr.sun_path))
|
2000-04-03 04:50:43 +00:00
|
|
|
fatal("Random pool path is too long");
|
2000-06-26 03:01:33 +00:00
|
|
|
if (len > 255)
|
2001-03-03 13:29:20 +00:00
|
|
|
fatal("Too many bytes to read from PRNGD");
|
2000-06-26 03:01:33 +00:00
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
2001-03-03 13:29:20 +00:00
|
|
|
strlcpy(addr.sun_path, PRNGD_SOCKET, sizeof(addr.sun_path));
|
|
|
|
addr_len = offsetof(struct sockaddr_un, sun_path) +
|
|
|
|
sizeof(PRNGD_SOCKET);
|
|
|
|
#endif
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2001-02-18 04:28:11 +00:00
|
|
|
old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
|
2001-02-18 01:44:29 +00:00
|
|
|
|
|
|
|
errors = rval = 0;
|
|
|
|
reopen:
|
2001-03-03 13:29:20 +00:00
|
|
|
#ifdef PRNGD_PORT
|
|
|
|
fd = socket(addr.sin_family, SOCK_STREAM, 0);
|
|
|
|
if (fd == -1) {
|
|
|
|
error("Couldn't create AF_INET socket: %s", strerror(errno));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
fd = socket(addr.sun_family, SOCK_STREAM, 0);
|
2000-06-26 03:01:33 +00:00
|
|
|
if (fd == -1) {
|
|
|
|
error("Couldn't create AF_UNIX socket: %s", strerror(errno));
|
2001-02-18 01:44:29 +00:00
|
|
|
goto done;
|
2000-06-26 03:01:33 +00:00
|
|
|
}
|
2001-03-03 13:29:20 +00:00
|
|
|
#endif
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-06-26 03:01:33 +00:00
|
|
|
if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
|
2001-03-03 13:29:20 +00:00
|
|
|
#ifdef PRNGD_PORT
|
|
|
|
error("Couldn't connect to PRNGD port %d: %s",
|
|
|
|
PRNGD_PORT, strerror(errno));
|
|
|
|
#else
|
|
|
|
error("Couldn't connect to PRNGD socket \"%s\": %s",
|
|
|
|
addr.sun_path, strerror(errno));
|
|
|
|
#endif
|
2001-02-18 01:44:29 +00:00
|
|
|
goto done;
|
2000-06-26 03:01:33 +00:00
|
|
|
}
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
/* Send blocking read request to PRNGD */
|
2000-06-26 03:01:33 +00:00
|
|
|
msg[0] = 0x02;
|
|
|
|
msg[1] = len;
|
|
|
|
|
|
|
|
if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
|
2001-02-18 01:44:29 +00:00
|
|
|
if (errno == EPIPE && errors < 10) {
|
|
|
|
close(fd);
|
|
|
|
errors++;
|
|
|
|
goto reopen;
|
|
|
|
}
|
2001-03-03 13:29:20 +00:00
|
|
|
error("Couldn't write to PRNGD socket: %s",
|
|
|
|
strerror(errno));
|
2001-02-18 01:44:29 +00:00
|
|
|
goto done;
|
2000-06-26 03:01:33 +00:00
|
|
|
}
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-06-26 03:01:33 +00:00
|
|
|
if (atomicio(read, fd, buf, len) != len) {
|
2001-02-18 01:44:29 +00:00
|
|
|
if (errno == EPIPE && errors < 10) {
|
|
|
|
close(fd);
|
|
|
|
errors++;
|
|
|
|
goto reopen;
|
|
|
|
}
|
2001-03-03 13:29:20 +00:00
|
|
|
error("Couldn't read from PRNGD socket: %s",
|
|
|
|
strerror(errno));
|
2001-02-18 01:44:29 +00:00
|
|
|
goto done;
|
2000-06-26 03:01:33 +00:00
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2001-02-18 01:44:29 +00:00
|
|
|
rval = 1;
|
|
|
|
done:
|
2001-02-18 11:34:32 +00:00
|
|
|
mysignal(SIGPIPE, old_sigpipe);
|
2001-02-18 01:44:29 +00:00
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return(rval);
|
2000-04-03 04:50:43 +00:00
|
|
|
}
|
2001-03-03 13:29:20 +00:00
|
|
|
#else /* !USE_PRNGD */
|
2000-04-03 04:50:43 +00:00
|
|
|
#ifdef RANDOM_POOL
|
|
|
|
/* Collect entropy from /dev/urandom or pipe */
|
2001-06-27 13:36:08 +00:00
|
|
|
static int
|
2001-03-18 22:38:15 +00:00
|
|
|
get_random_bytes(unsigned char *buf, int len)
|
2000-04-03 04:50:43 +00:00
|
|
|
{
|
2000-06-26 03:01:33 +00:00
|
|
|
int random_pool;
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-06-26 03:01:33 +00:00
|
|
|
random_pool = open(RANDOM_POOL, O_RDONLY);
|
2000-04-03 04:50:43 +00:00
|
|
|
if (random_pool == -1) {
|
2001-02-05 12:42:17 +00:00
|
|
|
error("Couldn't open random pool \"%s\": %s",
|
2000-06-26 03:01:33 +00:00
|
|
|
RANDOM_POOL, strerror(errno));
|
|
|
|
return(0);
|
2000-04-03 04:50:43 +00:00
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-26 03:01:33 +00:00
|
|
|
if (atomicio(read, random_pool, buf, len) != len) {
|
2001-02-05 12:42:17 +00:00
|
|
|
error("Couldn't read from random pool \"%s\": %s",
|
2000-06-26 03:01:33 +00:00
|
|
|
RANDOM_POOL, strerror(errno));
|
|
|
|
close(random_pool);
|
|
|
|
return(0);
|
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-26 03:01:33 +00:00
|
|
|
close(random_pool);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-26 03:01:33 +00:00
|
|
|
return(1);
|
2000-04-03 04:50:43 +00:00
|
|
|
}
|
|
|
|
#endif /* RANDOM_POOL */
|
2001-03-03 13:29:20 +00:00
|
|
|
#endif /* USE_PRNGD */
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/*
|
|
|
|
* Seed OpenSSL's random number pool from Kernel random number generator
|
2001-03-03 13:29:20 +00:00
|
|
|
* or PRNGD/EGD
|
2000-06-07 12:20:23 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
seed_rng(void)
|
|
|
|
{
|
2001-03-03 13:29:20 +00:00
|
|
|
unsigned char buf[32];
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
debug("Seeding random number generator");
|
2000-06-26 03:01:33 +00:00
|
|
|
|
2000-06-26 03:55:31 +00:00
|
|
|
if (!get_random_bytes(buf, sizeof(buf))) {
|
|
|
|
if (!RAND_status())
|
|
|
|
fatal("Entropy collection failed and entropy exhausted");
|
|
|
|
} else {
|
|
|
|
RAND_add(buf, sizeof(buf), sizeof(buf));
|
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
memset(buf, '\0', sizeof(buf));
|
|
|
|
}
|
|
|
|
|
2001-03-18 22:38:15 +00:00
|
|
|
void
|
|
|
|
init_rng(void)
|
2001-02-26 22:20:57 +00:00
|
|
|
{
|
|
|
|
check_openssl_version();
|
|
|
|
}
|
2000-07-09 12:42:32 +00:00
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
#else /* defined(USE_PRNGD) || defined(RANDOM_POOL) */
|
2000-06-07 12:20:23 +00:00
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
/*
|
2000-04-03 04:50:43 +00:00
|
|
|
* FIXME: proper entropy estimations. All current values are guesses
|
2000-04-29 23:30:44 +00:00
|
|
|
* FIXME: (ATL) do estimates at compile time?
|
2000-04-03 04:50:43 +00:00
|
|
|
* FIXME: More entropy sources
|
|
|
|
*/
|
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
/* slow command timeouts (all in milliseconds) */
|
|
|
|
/* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
|
|
|
|
static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
|
|
|
|
|
2000-05-01 23:56:41 +00:00
|
|
|
static int prng_seed_saved = 0;
|
2000-07-09 12:42:32 +00:00
|
|
|
static int prng_initialised = 0;
|
|
|
|
uid_t original_uid;
|
2000-04-03 04:50:43 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/* Proportion of data that is entropy */
|
|
|
|
double rate;
|
2000-04-29 23:30:44 +00:00
|
|
|
/* Counter goes positive if this command times out */
|
|
|
|
unsigned int badness;
|
|
|
|
/* Increases by factor of two each timeout */
|
|
|
|
unsigned int sticky_badness;
|
2000-04-03 04:50:43 +00:00
|
|
|
/* Path to executable */
|
2000-05-01 23:56:41 +00:00
|
|
|
char *path;
|
2000-04-03 04:50:43 +00:00
|
|
|
/* argv to pass to executable */
|
2000-05-01 23:56:41 +00:00
|
|
|
char *args[5];
|
2000-05-17 11:34:07 +00:00
|
|
|
/* full command string (debug) */
|
|
|
|
char *cmdstring;
|
2000-04-03 04:50:43 +00:00
|
|
|
} entropy_source_t;
|
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
double stir_from_system(void);
|
|
|
|
double stir_from_programs(void);
|
|
|
|
double stir_gettimeofday(double entropy_estimate);
|
|
|
|
double stir_clock(double entropy_estimate);
|
|
|
|
double stir_rusage(int who, double entropy_estimate);
|
|
|
|
double hash_output_from_command(entropy_source_t *src, char *hash);
|
|
|
|
|
2000-05-01 23:56:41 +00:00
|
|
|
/* this is initialised from a file, by prng_read_commands() */
|
|
|
|
entropy_source_t *entropy_sources = NULL;
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
double
|
2000-04-03 04:50:43 +00:00
|
|
|
stir_from_system(void)
|
|
|
|
{
|
|
|
|
double total_entropy_estimate;
|
|
|
|
long int i;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
total_entropy_estimate = 0;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
i = getpid();
|
2000-06-18 04:07:04 +00:00
|
|
|
RAND_add(&i, sizeof(i), 0.5);
|
2000-04-03 04:50:43 +00:00
|
|
|
total_entropy_estimate += 0.1;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
i = getppid();
|
2000-06-18 04:07:04 +00:00
|
|
|
RAND_add(&i, sizeof(i), 0.5);
|
2000-04-03 04:50:43 +00:00
|
|
|
total_entropy_estimate += 0.1;
|
|
|
|
|
|
|
|
i = getuid();
|
|
|
|
RAND_add(&i, sizeof(i), 0.0);
|
|
|
|
i = getgid();
|
|
|
|
RAND_add(&i, sizeof(i), 0.0);
|
|
|
|
|
|
|
|
total_entropy_estimate += stir_gettimeofday(1.0);
|
2000-06-18 04:07:04 +00:00
|
|
|
total_entropy_estimate += stir_clock(0.5);
|
2000-04-03 04:50:43 +00:00
|
|
|
total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
|
|
|
|
|
|
|
|
return(total_entropy_estimate);
|
|
|
|
}
|
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
double
|
2000-04-03 04:50:43 +00:00
|
|
|
stir_from_programs(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int c;
|
|
|
|
double entropy_estimate;
|
|
|
|
double total_entropy_estimate;
|
|
|
|
char hash[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
total_entropy_estimate = 0;
|
2000-06-07 12:20:23 +00:00
|
|
|
for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
|
2000-04-03 04:50:43 +00:00
|
|
|
c = 0;
|
|
|
|
while (entropy_sources[c].path != NULL) {
|
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
if (!entropy_sources[c].badness) {
|
|
|
|
/* Hash output from command */
|
|
|
|
entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
|
|
|
|
|
|
|
|
/* Scale back entropy estimate according to command's rate */
|
|
|
|
entropy_estimate *= entropy_sources[c].rate;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
/* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
|
|
|
|
if (entropy_estimate > SHA_DIGEST_LENGTH)
|
|
|
|
entropy_estimate = SHA_DIGEST_LENGTH;
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
/* Scale back estimates for subsequent passes through list */
|
2000-06-07 12:20:23 +00:00
|
|
|
entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
/* Stir it in */
|
|
|
|
RAND_add(hash, sizeof(hash), entropy_estimate);
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate,
|
2000-05-17 11:34:07 +00:00
|
|
|
entropy_sources[c].cmdstring);
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
total_entropy_estimate += entropy_estimate;
|
2000-04-03 04:50:43 +00:00
|
|
|
|
|
|
|
/* Execution times should be a little unpredictable */
|
2000-04-29 23:30:44 +00:00
|
|
|
total_entropy_estimate += stir_gettimeofday(0.05);
|
|
|
|
total_entropy_estimate += stir_clock(0.05);
|
|
|
|
total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
|
|
|
|
total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
|
|
|
|
} else {
|
2000-09-29 01:12:36 +00:00
|
|
|
debug2("Command '%s' disabled (badness %d)",
|
2000-05-17 11:34:07 +00:00
|
|
|
entropy_sources[c].cmdstring, entropy_sources[c].badness);
|
2000-04-29 23:30:44 +00:00
|
|
|
|
|
|
|
if (entropy_sources[c].badness > 0)
|
|
|
|
entropy_sources[c].badness--;
|
|
|
|
}
|
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
c++;
|
|
|
|
}
|
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
return(total_entropy_estimate);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_gettimeofday(double entropy_estimate)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
if (gettimeofday(&tv, NULL) == -1)
|
|
|
|
fatal("Couldn't gettimeofday: %s", strerror(errno));
|
|
|
|
|
|
|
|
RAND_add(&tv, sizeof(tv), entropy_estimate);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
return(entropy_estimate);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_clock(double entropy_estimate)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CLOCK
|
|
|
|
clock_t c;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
c = clock();
|
|
|
|
RAND_add(&c, sizeof(c), entropy_estimate);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
return(entropy_estimate);
|
|
|
|
#else /* _HAVE_CLOCK */
|
|
|
|
return(0);
|
|
|
|
#endif /* _HAVE_CLOCK */
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_rusage(int who, double entropy_estimate)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_GETRUSAGE
|
|
|
|
struct rusage ru;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
if (getrusage(who, &ru) == -1)
|
2000-06-18 04:07:04 +00:00
|
|
|
return(0);
|
2000-04-03 04:50:43 +00:00
|
|
|
|
2000-06-18 04:07:04 +00:00
|
|
|
RAND_add(&ru, sizeof(ru), entropy_estimate);
|
2000-04-03 04:50:43 +00:00
|
|
|
|
|
|
|
return(entropy_estimate);
|
|
|
|
#else /* _HAVE_GETRUSAGE */
|
|
|
|
return(0);
|
|
|
|
#endif /* _HAVE_GETRUSAGE */
|
|
|
|
}
|
|
|
|
|
2000-05-17 11:34:07 +00:00
|
|
|
|
2001-03-18 22:38:15 +00:00
|
|
|
static int
|
2000-05-17 11:34:07 +00:00
|
|
|
_get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
|
|
|
|
int secdiff, usecdiff;
|
|
|
|
|
|
|
|
secdiff = t2->tv_sec - t1->tv_sec;
|
|
|
|
usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
|
|
|
|
return (int)(usecdiff / 1000);
|
|
|
|
}
|
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
double
|
2000-04-29 23:30:44 +00:00
|
|
|
hash_output_from_command(entropy_source_t *src, char *hash)
|
2000-04-03 04:50:43 +00:00
|
|
|
{
|
|
|
|
static int devnull = -1;
|
|
|
|
int p[2];
|
2000-04-29 23:30:44 +00:00
|
|
|
fd_set rdset;
|
|
|
|
int cmd_eof = 0, error_abort = 0;
|
2000-05-17 11:34:07 +00:00
|
|
|
struct timeval tv_start, tv_current;
|
|
|
|
int msec_elapsed = 0;
|
2000-04-03 04:50:43 +00:00
|
|
|
pid_t pid;
|
|
|
|
int status;
|
2000-05-17 11:34:07 +00:00
|
|
|
char buf[16384];
|
2000-04-03 04:50:43 +00:00
|
|
|
int bytes_read;
|
|
|
|
int total_bytes_read;
|
|
|
|
SHA_CTX sha;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-09-29 01:12:36 +00:00
|
|
|
debug3("Reading output from \'%s\'", src->cmdstring);
|
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
if (devnull == -1) {
|
|
|
|
devnull = open("/dev/null", O_RDWR);
|
|
|
|
if (devnull == -1)
|
|
|
|
fatal("Couldn't open /dev/null: %s", strerror(errno));
|
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
if (pipe(p) == -1)
|
|
|
|
fatal("Couldn't open pipe: %s", strerror(errno));
|
|
|
|
|
2000-05-17 11:34:07 +00:00
|
|
|
(void)gettimeofday(&tv_start, NULL); /* record start time */
|
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1: /* Error */
|
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
fatal("Couldn't fork: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 0: /* Child */
|
2000-04-29 23:30:44 +00:00
|
|
|
dup2(devnull, STDIN_FILENO);
|
|
|
|
dup2(p[1], STDOUT_FILENO);
|
|
|
|
dup2(p[1], STDERR_FILENO);
|
2000-04-03 04:50:43 +00:00
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
close(devnull);
|
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
setuid(original_uid);
|
2000-04-29 23:30:44 +00:00
|
|
|
execv(src->path, (char**)(src->args));
|
2000-05-17 11:34:07 +00:00
|
|
|
debug("(child) Couldn't exec '%s': %s", src->cmdstring,
|
|
|
|
strerror(errno));
|
2000-04-03 04:50:43 +00:00
|
|
|
_exit(-1);
|
|
|
|
default: /* Parent */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RAND_add(&pid, sizeof(&pid), 0.0);
|
|
|
|
|
|
|
|
close(p[1]);
|
|
|
|
|
|
|
|
/* Hash output from child */
|
|
|
|
SHA1_Init(&sha);
|
|
|
|
total_bytes_read = 0;
|
2000-04-29 23:30:44 +00:00
|
|
|
|
|
|
|
while (!error_abort && !cmd_eof) {
|
|
|
|
int ret;
|
|
|
|
struct timeval tv;
|
2000-05-17 11:34:07 +00:00
|
|
|
int msec_remaining;
|
|
|
|
|
|
|
|
(void) gettimeofday(&tv_current, 0);
|
2000-06-07 12:20:23 +00:00
|
|
|
msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
|
2000-05-17 11:34:07 +00:00
|
|
|
if (msec_elapsed >= entropy_timeout_current) {
|
|
|
|
error_abort=1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
msec_remaining = entropy_timeout_current - msec_elapsed;
|
2000-04-29 23:30:44 +00:00
|
|
|
|
|
|
|
FD_ZERO(&rdset);
|
|
|
|
FD_SET(p[0], &rdset);
|
2000-05-17 11:34:07 +00:00
|
|
|
tv.tv_sec = msec_remaining / 1000;
|
|
|
|
tv.tv_usec = (msec_remaining % 1000) * 1000;
|
2000-04-29 23:30:44 +00:00
|
|
|
|
|
|
|
ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
|
2000-05-17 11:34:07 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
RAND_add(&tv, sizeof(tv), 0.0);
|
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
/* timer expired */
|
|
|
|
error_abort = 1;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* command input */
|
|
|
|
bytes_read = read(p[0], buf, sizeof(buf));
|
2000-07-09 12:42:32 +00:00
|
|
|
RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
|
2000-04-29 23:30:44 +00:00
|
|
|
if (bytes_read == -1) {
|
|
|
|
error_abort = 1;
|
|
|
|
break;
|
2000-07-09 12:42:32 +00:00
|
|
|
} else if (bytes_read) {
|
2000-05-17 11:34:07 +00:00
|
|
|
SHA1_Update(&sha, buf, bytes_read);
|
|
|
|
total_bytes_read += bytes_read;
|
2000-07-09 12:42:32 +00:00
|
|
|
} else {
|
2000-05-17 11:34:07 +00:00
|
|
|
cmd_eof = 1;
|
2000-07-09 12:42:32 +00:00
|
|
|
}
|
2000-04-29 23:30:44 +00:00
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
default:
|
2000-07-09 12:42:32 +00:00
|
|
|
/* error */
|
2000-05-17 11:34:07 +00:00
|
|
|
debug("Command '%s': select() failed: %s", src->cmdstring,
|
|
|
|
strerror(errno));
|
2000-04-29 23:30:44 +00:00
|
|
|
error_abort = 1;
|
|
|
|
break;
|
2000-07-09 12:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-29 23:30:44 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
SHA1_Final(hash, &sha);
|
|
|
|
|
|
|
|
close(p[0]);
|
2000-05-17 11:34:07 +00:00
|
|
|
|
2000-09-29 01:12:36 +00:00
|
|
|
debug3("Time elapsed: %d msec", msec_elapsed);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
if (waitpid(pid, &status, 0) == -1) {
|
2000-09-29 01:12:36 +00:00
|
|
|
error("Couldn't wait for child '%s' completion: %s", src->cmdstring,
|
2000-05-17 11:34:07 +00:00
|
|
|
strerror(errno));
|
2000-04-29 23:30:44 +00:00
|
|
|
return(0.0);
|
2000-04-03 04:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RAND_add(&status, sizeof(&status), 0.0);
|
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
if (error_abort) {
|
|
|
|
/* closing p[0] on timeout causes the entropy command to
|
|
|
|
* SIGPIPE. Take whatever output we got, and mark this command
|
|
|
|
* as slow */
|
2000-09-29 01:12:36 +00:00
|
|
|
debug2("Command '%s' timed out", src->cmdstring);
|
2000-04-29 23:30:44 +00:00
|
|
|
src->sticky_badness *= 2;
|
|
|
|
src->badness = src->sticky_badness;
|
2000-04-03 04:50:43 +00:00
|
|
|
return(total_bytes_read);
|
2000-04-29 23:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
if (WEXITSTATUS(status)==0) {
|
|
|
|
return(total_bytes_read);
|
|
|
|
} else {
|
2001-02-05 12:42:17 +00:00
|
|
|
debug2("Command '%s' exit status was %d", src->cmdstring,
|
2000-06-07 12:20:23 +00:00
|
|
|
WEXITSTATUS(status));
|
2000-04-29 23:30:44 +00:00
|
|
|
src->badness = src->sticky_badness = 128;
|
|
|
|
return (0.0);
|
|
|
|
}
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2001-02-05 12:42:17 +00:00
|
|
|
debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring,
|
2000-06-07 12:20:23 +00:00
|
|
|
status);
|
2000-04-29 23:30:44 +00:00
|
|
|
src->badness = src->sticky_badness = 128;
|
|
|
|
return(0.0);
|
|
|
|
} else
|
|
|
|
return(0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prng seedfile functions
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
prng_check_seedfile(char *filename) {
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/* FIXME raceable: eg replace seed between this stat and subsequent open */
|
|
|
|
/* Not such a problem because we don't trust the seed file anyway */
|
|
|
|
if (lstat(filename, &st) == -1) {
|
2000-10-16 09:13:43 +00:00
|
|
|
/* Give up on hard errors */
|
2000-04-29 23:30:44 +00:00
|
|
|
if (errno != ENOENT)
|
2001-02-05 12:42:17 +00:00
|
|
|
debug("WARNING: Couldn't stat random seed file \"%s\": %s",
|
2000-10-16 09:13:43 +00:00
|
|
|
filename, strerror(errno));
|
2000-04-29 23:30:44 +00:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* regular file? */
|
|
|
|
if (!S_ISREG(st.st_mode))
|
|
|
|
fatal("PRNG seedfile %.100s is not a regular file", filename);
|
|
|
|
|
|
|
|
/* mode 0600, owned by root or the current user? */
|
2000-10-16 09:13:43 +00:00
|
|
|
if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) {
|
|
|
|
debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d",
|
2000-04-29 23:30:44 +00:00
|
|
|
filename, getuid());
|
2000-10-16 09:13:43 +00:00
|
|
|
return(0);
|
|
|
|
}
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prng_write_seedfile(void) {
|
|
|
|
int fd;
|
|
|
|
char seed[1024];
|
|
|
|
char filename[1024];
|
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
/* Don't bother if we have already saved a seed */
|
|
|
|
if (prng_seed_saved)
|
|
|
|
return;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
setuid(original_uid);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-05-01 14:03:55 +00:00
|
|
|
prng_seed_saved = 1;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
pw = getpwuid(original_uid);
|
2000-04-29 23:30:44 +00:00
|
|
|
if (pw == NULL)
|
2001-02-05 12:42:17 +00:00
|
|
|
fatal("Couldn't get password entry for current user (%i): %s",
|
2000-07-09 12:42:32 +00:00
|
|
|
original_uid, strerror(errno));
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
/* Try to ensure that the parent directory is there */
|
2001-02-05 12:42:17 +00:00
|
|
|
snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
|
2001-01-22 21:06:19 +00:00
|
|
|
_PATH_SSH_USER_DIR);
|
2000-04-29 23:30:44 +00:00
|
|
|
mkdir(filename, 0700);
|
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
|
2000-04-29 23:30:44 +00:00
|
|
|
SSH_PRNG_SEED_FILE);
|
|
|
|
|
|
|
|
debug("writing PRNG seed to file %.100s", filename);
|
|
|
|
|
|
|
|
RAND_bytes(seed, sizeof(seed));
|
|
|
|
|
|
|
|
/* Don't care if the seed doesn't exist */
|
|
|
|
prng_check_seedfile(filename);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-10-16 09:13:43 +00:00
|
|
|
if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
|
2001-02-05 12:42:17 +00:00
|
|
|
debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)",
|
2000-10-16 09:13:43 +00:00
|
|
|
filename, strerror(errno));
|
2001-02-05 12:42:17 +00:00
|
|
|
} else {
|
2000-10-16 09:13:43 +00:00
|
|
|
if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
|
2001-02-05 12:42:17 +00:00
|
|
|
fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
|
2000-10-16 09:13:43 +00:00
|
|
|
strerror(errno));
|
2000-04-29 23:30:44 +00:00
|
|
|
|
2000-10-16 09:13:43 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2000-04-29 23:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
prng_read_seedfile(void) {
|
|
|
|
int fd;
|
|
|
|
char seed[1024];
|
|
|
|
char filename[1024];
|
|
|
|
struct passwd *pw;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
pw = getpwuid(original_uid);
|
2000-04-29 23:30:44 +00:00
|
|
|
if (pw == NULL)
|
2001-02-05 12:42:17 +00:00
|
|
|
fatal("Couldn't get password entry for current user (%i): %s",
|
2000-07-09 12:42:32 +00:00
|
|
|
original_uid, strerror(errno));
|
2001-02-05 12:42:17 +00:00
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
|
2000-04-29 23:30:44 +00:00
|
|
|
SSH_PRNG_SEED_FILE);
|
|
|
|
|
|
|
|
debug("loading PRNG seed from file %.100s", filename);
|
|
|
|
|
|
|
|
if (!prng_check_seedfile(filename)) {
|
2001-01-16 22:37:15 +00:00
|
|
|
verbose("Random seed file not found or not valid, ignoring.");
|
2000-04-29 23:30:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file and read in the seed */
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd == -1)
|
2001-02-05 12:42:17 +00:00
|
|
|
fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
|
2000-04-29 23:30:44 +00:00
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
|
|
|
|
verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
|
|
|
|
filename);
|
|
|
|
memset(seed, '\0', sizeof(seed));
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* stir in the seed, with estimated entropy zero */
|
|
|
|
RAND_add(&seed, sizeof(seed), 0.0);
|
2000-04-03 04:50:43 +00:00
|
|
|
}
|
2000-04-29 23:30:44 +00:00
|
|
|
|
2000-05-01 23:56:41 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* entropy command initialisation functions
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
prng_read_commands(char *cmdfilename)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char *cp;
|
2000-06-07 12:20:23 +00:00
|
|
|
char line[1024];
|
|
|
|
char cmd[1024];
|
|
|
|
char path[256];
|
2000-05-01 23:56:41 +00:00
|
|
|
int linenum;
|
|
|
|
int num_cmds = 64;
|
|
|
|
int cur_cmd = 0;
|
2000-06-07 12:20:23 +00:00
|
|
|
double est;
|
|
|
|
entropy_source_t *entcmd;
|
2000-05-01 23:56:41 +00:00
|
|
|
|
|
|
|
f = fopen(cmdfilename, "r");
|
|
|
|
if (!f) {
|
|
|
|
fatal("couldn't read entropy commands file %.100s: %.100s",
|
|
|
|
cmdfilename, strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
|
|
|
|
memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
|
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/* Read in file */
|
|
|
|
linenum = 0;
|
2000-05-01 23:56:41 +00:00
|
|
|
while (fgets(line, sizeof(line), f)) {
|
2000-06-07 12:20:23 +00:00
|
|
|
int arg;
|
|
|
|
char *argv;
|
|
|
|
|
2000-05-01 23:56:41 +00:00
|
|
|
linenum++;
|
|
|
|
|
|
|
|
/* skip leading whitespace, test for blank line or comment */
|
|
|
|
cp = line + strspn(line, WHITESPACE);
|
|
|
|
if ((*cp == 0) || (*cp == '#'))
|
|
|
|
continue; /* done with this line */
|
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/* First non-whitespace char should be double quote delimiting */
|
|
|
|
/* commandline */
|
|
|
|
if (*cp != '"') {
|
|
|
|
error("bad entropy command, %.100s line %d", cmdfilename,
|
|
|
|
linenum);
|
|
|
|
continue;
|
2001-02-05 12:42:17 +00:00
|
|
|
}
|
2000-05-01 23:56:41 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/* first token, command args (incl. argv[0]) in double quotes */
|
|
|
|
cp = strtok(cp, "\"");
|
|
|
|
if (cp == NULL) {
|
|
|
|
error("missing or bad command string, %.100s line %d -- ignored",
|
|
|
|
cmdfilename, linenum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strlcpy(cmd, cp, sizeof(cmd));
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/* second token, full command path */
|
|
|
|
if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
|
|
|
|
error("missing command path, %.100s line %d -- ignored",
|
|
|
|
cmdfilename, linenum);
|
|
|
|
continue;
|
|
|
|
}
|
2000-05-01 23:56:41 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/* did configure mark this as dead? */
|
|
|
|
if (strncmp("undef", cp, 5) == 0)
|
|
|
|
continue;
|
2000-05-01 23:56:41 +00:00
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
strlcpy(path, cp, sizeof(path));
|
2000-06-07 12:20:23 +00:00
|
|
|
|
|
|
|
/* third token, entropy rate estimate for this command */
|
|
|
|
if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
|
|
|
|
error("missing entropy estimate, %.100s line %d -- ignored",
|
|
|
|
cmdfilename, linenum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
est = strtod(cp, &argv);
|
|
|
|
|
|
|
|
/* end of line */
|
|
|
|
if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
|
2001-02-05 12:42:17 +00:00
|
|
|
error("garbage at end of line %d in %.100s -- ignored", linenum,
|
2000-06-07 12:20:23 +00:00
|
|
|
cmdfilename);
|
2000-05-01 23:56:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-06-07 12:20:23 +00:00
|
|
|
|
|
|
|
/* save the command for debug messages */
|
|
|
|
entcmd[cur_cmd].cmdstring = xstrdup(cmd);
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
/* split the command args */
|
|
|
|
cp = strtok(cmd, WHITESPACE);
|
|
|
|
arg = 0;
|
|
|
|
argv = NULL;
|
|
|
|
do {
|
|
|
|
char *s = (char*)xmalloc(strlen(cp) + 1);
|
|
|
|
strncpy(s, cp, strlen(cp) + 1);
|
|
|
|
entcmd[cur_cmd].args[arg] = s;
|
|
|
|
arg++;
|
|
|
|
} while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-06-07 12:20:23 +00:00
|
|
|
if (strtok(NULL, WHITESPACE))
|
|
|
|
error("ignored extra command elements (max 5), %.100s line %d",
|
|
|
|
cmdfilename, linenum);
|
|
|
|
|
|
|
|
/* Copy the command path and rate estimate */
|
|
|
|
entcmd[cur_cmd].path = xstrdup(path);
|
|
|
|
entcmd[cur_cmd].rate = est;
|
|
|
|
|
|
|
|
/* Initialise other values */
|
|
|
|
entcmd[cur_cmd].sticky_badness = 1;
|
|
|
|
|
|
|
|
cur_cmd++;
|
|
|
|
|
|
|
|
/* If we've filled the array, reallocate it twice the size */
|
|
|
|
/* Do this now because even if this we're on the last command,
|
|
|
|
we need another slot to mark the last entry */
|
|
|
|
if (cur_cmd == num_cmds) {
|
|
|
|
num_cmds *= 2;
|
|
|
|
entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
|
|
|
|
}
|
2000-05-01 23:56:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* zero the last entry */
|
|
|
|
memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
|
2000-06-07 12:20:23 +00:00
|
|
|
|
2000-05-01 23:56:41 +00:00
|
|
|
/* trim to size */
|
|
|
|
entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
|
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
|
2000-05-01 23:56:41 +00:00
|
|
|
|
|
|
|
return (cur_cmd >= MIN_ENTROPY_SOURCES);
|
|
|
|
}
|
|
|
|
|
2000-04-29 23:30:44 +00:00
|
|
|
/*
|
|
|
|
* Write a keyfile at exit
|
2001-02-05 12:42:17 +00:00
|
|
|
*/
|
2000-04-29 23:30:44 +00:00
|
|
|
void
|
|
|
|
prng_seed_cleanup(void *junk)
|
|
|
|
{
|
|
|
|
prng_write_seedfile();
|
|
|
|
}
|
|
|
|
|
2000-04-03 04:50:43 +00:00
|
|
|
/*
|
2000-04-29 23:30:44 +00:00
|
|
|
* Conditionally Seed OpenSSL's random number pool from
|
|
|
|
* syscalls and program output
|
2000-04-03 04:50:43 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
seed_rng(void)
|
|
|
|
{
|
2001-02-18 04:28:11 +00:00
|
|
|
mysig_t old_sigchld_handler;
|
2000-05-01 23:56:41 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
if (!prng_initialised)
|
|
|
|
fatal("RNG not initialised");
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-05-17 12:08:29 +00:00
|
|
|
/* Make sure some other sigchld handler doesn't reap our entropy */
|
|
|
|
/* commands */
|
2001-02-18 04:28:11 +00:00
|
|
|
old_sigchld_handler = mysignal(SIGCHLD, SIG_DFL);
|
2000-05-17 12:08:29 +00:00
|
|
|
|
2001-03-18 22:38:15 +00:00
|
|
|
debug("Seeded RNG with %i bytes from programs",
|
|
|
|
(int)stir_from_programs());
|
|
|
|
debug("Seeded RNG with %i bytes from system calls",
|
|
|
|
(int)stir_from_system());
|
2000-07-09 12:42:32 +00:00
|
|
|
|
|
|
|
if (!RAND_status())
|
|
|
|
fatal("Not enough entropy in RNG");
|
2000-04-29 23:30:44 +00:00
|
|
|
|
2001-02-18 04:28:11 +00:00
|
|
|
mysignal(SIGCHLD, old_sigchld_handler);
|
2000-05-17 12:08:29 +00:00
|
|
|
|
2000-05-17 11:34:07 +00:00
|
|
|
if (!RAND_status())
|
|
|
|
fatal("Couldn't initialise builtin random number generator -- exiting.");
|
2000-07-09 12:42:32 +00:00
|
|
|
}
|
2000-05-17 11:34:07 +00:00
|
|
|
|
2001-03-18 22:38:15 +00:00
|
|
|
void
|
|
|
|
init_rng(void)
|
2000-07-09 12:42:32 +00:00
|
|
|
{
|
2000-11-24 23:09:32 +00:00
|
|
|
int original_euid;
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2001-02-26 22:20:57 +00:00
|
|
|
check_openssl_version();
|
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
original_uid = getuid();
|
2000-11-24 23:09:32 +00:00
|
|
|
original_euid = geteuid();
|
2000-07-09 12:42:32 +00:00
|
|
|
|
|
|
|
/* Read in collection commands */
|
|
|
|
if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
|
|
|
|
fatal("PRNG initialisation failed -- exiting.");
|
|
|
|
|
|
|
|
/* Set ourselves up to save a seed upon exit */
|
2001-02-05 12:42:17 +00:00
|
|
|
prng_seed_saved = 0;
|
2000-11-24 23:09:32 +00:00
|
|
|
|
|
|
|
/* Give up privs while reading seed file */
|
2001-02-26 09:49:58 +00:00
|
|
|
#ifdef SAVED_IDS_WORK_WITH_SETEUID
|
2000-11-24 23:09:32 +00:00
|
|
|
if ((original_uid != original_euid) && (seteuid(original_uid) == -1))
|
|
|
|
fatal("Couldn't give up privileges");
|
2001-02-26 09:49:58 +00:00
|
|
|
#else /* SAVED_IDS_WORK_WITH_SETEUID */
|
|
|
|
/*
|
|
|
|
* Propagate the privileged uid to all of our uids.
|
|
|
|
* Set the effective uid to the given (unprivileged) uid.
|
|
|
|
*/
|
2001-02-26 22:47:16 +00:00
|
|
|
if (original_uid != original_euid && (setuid(original_euid) == -1 ||
|
|
|
|
seteuid(original_uid) == -1))
|
2001-02-26 09:49:58 +00:00
|
|
|
fatal("Couldn't give up privileges");
|
|
|
|
#endif /* SAVED_IDS_WORK_WITH_SETEUID */
|
2001-02-05 12:42:17 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
prng_read_seedfile();
|
2000-11-24 23:09:32 +00:00
|
|
|
|
2001-02-26 09:49:58 +00:00
|
|
|
#ifdef SAVED_IDS_WORK_WITH_SETEUID
|
2000-11-24 23:09:32 +00:00
|
|
|
if ((original_uid != original_euid) && (seteuid(original_euid) == -1))
|
|
|
|
fatal("Couldn't restore privileges");
|
2001-02-26 09:49:58 +00:00
|
|
|
#else /* SAVED_IDS_WORK_WITH_SETEUID */
|
|
|
|
/*
|
|
|
|
* We are unable to restore the real uid to its unprivileged value.
|
|
|
|
* Propagate the real uid (usually more privileged) to effective uid
|
|
|
|
* as well.
|
|
|
|
*/
|
2001-02-26 22:47:16 +00:00
|
|
|
if (original_uid != original_euid && (seteuid(original_euid) == -1 ||
|
|
|
|
setuid(original_uid) == -1))
|
2001-02-26 09:49:58 +00:00
|
|
|
fatal("Couldn't restore privileges");
|
|
|
|
#endif /* SAVED_IDS_WORK_WITH_SETEUID */
|
2000-11-24 23:09:32 +00:00
|
|
|
|
2000-07-09 12:42:32 +00:00
|
|
|
fatal_add_cleanup(prng_seed_cleanup, NULL);
|
|
|
|
atexit(prng_write_seedfile);
|
|
|
|
|
|
|
|
prng_initialised = 1;
|
2000-04-03 04:50:43 +00:00
|
|
|
}
|
2000-07-09 12:42:32 +00:00
|
|
|
|
2001-03-03 13:29:20 +00:00
|
|
|
#endif /* defined(USE_PRNGD) || defined(RANDOM_POOL) */
|