2001-12-23 14:41:47 +00:00
|
|
|
/*
|
2002-01-21 12:44:12 +00:00
|
|
|
* Copyright (c) 2001-2002 Damien Miller. All rights reserved.
|
2001-12-23 14:41:47 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
|
|
|
/* SunOS 4.4.4 needs this */
|
|
|
|
#ifdef HAVE_FLOATINGPOINT_H
|
|
|
|
# include <floatingpoint.h>
|
|
|
|
#endif /* HAVE_FLOATINGPOINT_H */
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "atomicio.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2005-07-17 07:26:43 +00:00
|
|
|
RCSID("$Id: ssh-rand-helper.c,v 1.26 2005/07/17 07:26:44 djm Exp $");
|
2001-12-25 04:32:58 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Number of bytes we write out */
|
|
|
|
#define OUTPUT_SEED_SIZE 48
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Length of on-disk seedfiles */
|
|
|
|
#define SEED_FILE_SIZE 1024
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Maximum number of command-line arguments to read from file */
|
|
|
|
#define NUM_ARGS 10
|
|
|
|
|
|
|
|
/* Minimum number of usable commands to be considered sufficient */
|
|
|
|
#define MIN_ENTROPY_SOURCES 16
|
|
|
|
|
|
|
|
/* Path to on-disk seed file (relative to user's home directory */
|
2001-12-23 14:41:47 +00:00
|
|
|
#ifndef SSH_PRNG_SEED_FILE
|
|
|
|
# define SSH_PRNG_SEED_FILE _PATH_SSH_USER_DIR"/prng_seed"
|
2002-01-21 12:44:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Path to PRNG commands list */
|
2001-12-23 14:41:47 +00:00
|
|
|
#ifndef SSH_PRNG_COMMAND_FILE
|
2002-02-10 07:32:28 +00:00
|
|
|
# define SSH_PRNG_COMMAND_FILE SSHDIR "/ssh_prng_cmds"
|
2002-01-21 12:44:12 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
extern char *__progname;
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
#define WHITESPACE " \t\n"
|
|
|
|
|
|
|
|
#ifndef RUSAGE_SELF
|
|
|
|
# define RUSAGE_SELF 0
|
|
|
|
#endif
|
|
|
|
#ifndef RUSAGE_CHILDREN
|
|
|
|
# define RUSAGE_CHILDREN 0
|
|
|
|
#endif
|
|
|
|
|
2002-01-22 10:58:27 +00:00
|
|
|
#if !defined(PRNGD_SOCKET) && !defined(PRNGD_PORT)
|
2002-01-21 12:44:12 +00:00
|
|
|
# define USE_SEED_FILES
|
2001-12-23 14:41:47 +00:00
|
|
|
#endif
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
typedef struct {
|
|
|
|
/* Proportion of data that is entropy */
|
|
|
|
double rate;
|
|
|
|
/* Counter goes positive if this command times out */
|
|
|
|
unsigned int badness;
|
|
|
|
/* Increases by factor of two each timeout */
|
|
|
|
unsigned int sticky_badness;
|
|
|
|
/* Path to executable */
|
|
|
|
char *path;
|
|
|
|
/* argv to pass to executable */
|
|
|
|
char *args[NUM_ARGS]; /* XXX: arbitrary limit */
|
|
|
|
/* full command string (debug) */
|
|
|
|
char *cmdstring;
|
|
|
|
} entropy_cmd_t;
|
|
|
|
|
|
|
|
/* slow command timeouts (all in milliseconds) */
|
|
|
|
/* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
|
|
|
|
static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
|
|
|
|
|
|
|
|
/* this is initialised from a file, by prng_read_commands() */
|
|
|
|
static entropy_cmd_t *entropy_cmds = NULL;
|
|
|
|
|
|
|
|
/* Prototypes */
|
|
|
|
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);
|
2002-07-28 20:42:23 +00:00
|
|
|
double hash_command_output(entropy_cmd_t *src, unsigned char *hash);
|
2003-11-21 12:48:55 +00:00
|
|
|
int get_random_bytes_prngd(unsigned char *buf, int len,
|
2002-01-21 12:44:12 +00:00
|
|
|
unsigned short tcp_port, char *socket_path);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
|
|
|
|
* listening either on 'tcp_port', or via Unix domain socket at *
|
|
|
|
* 'socket_path'.
|
2003-11-21 12:48:55 +00:00
|
|
|
* Either a non-zero tcp_port or a non-null socket_path must be
|
2002-01-21 12:44:12 +00:00
|
|
|
* supplied.
|
|
|
|
* Returns 0 on success, -1 on error
|
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
int
|
2003-11-21 12:48:55 +00:00
|
|
|
get_random_bytes_prngd(unsigned char *buf, int len,
|
2002-01-21 12:44:12 +00:00
|
|
|
unsigned short tcp_port, char *socket_path)
|
2001-12-23 14:41:47 +00:00
|
|
|
{
|
2002-01-21 12:44:12 +00:00
|
|
|
int fd, addr_len, rval, errors;
|
2005-06-19 00:19:43 +00:00
|
|
|
u_char msg[2];
|
2002-01-21 12:44:12 +00:00
|
|
|
struct sockaddr_storage addr;
|
|
|
|
struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
|
|
|
|
struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
|
2001-12-23 14:41:47 +00:00
|
|
|
mysig_t old_sigpipe;
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Sanity checks */
|
|
|
|
if (socket_path == NULL && tcp_port == 0)
|
|
|
|
fatal("You must specify a port or a socket");
|
|
|
|
if (socket_path != NULL &&
|
|
|
|
strlen(socket_path) >= sizeof(addr_un->sun_path))
|
|
|
|
fatal("Random pool path is too long");
|
2005-06-19 00:19:43 +00:00
|
|
|
if (len <= 0 || len > 255)
|
|
|
|
fatal("Too many bytes (%d) to read from PRNGD", len);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
memset(&addr, '\0', sizeof(addr));
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
if (tcp_port != 0) {
|
|
|
|
addr_in->sin_family = AF_INET;
|
|
|
|
addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
|
|
addr_in->sin_port = htons(tcp_port);
|
|
|
|
addr_len = sizeof(*addr_in);
|
|
|
|
} else {
|
|
|
|
addr_un->sun_family = AF_UNIX;
|
|
|
|
strlcpy(addr_un->sun_path, socket_path,
|
|
|
|
sizeof(addr_un->sun_path));
|
|
|
|
addr_len = offsetof(struct sockaddr_un, sun_path) +
|
|
|
|
strlen(socket_path) + 1;
|
|
|
|
}
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
errors = 0;
|
|
|
|
rval = -1;
|
2001-12-23 14:41:47 +00:00
|
|
|
reopen:
|
2002-01-21 12:44:12 +00:00
|
|
|
fd = socket(addr.ss_family, SOCK_STREAM, 0);
|
2001-12-23 14:41:47 +00:00
|
|
|
if (fd == -1) {
|
2002-01-21 12:44:12 +00:00
|
|
|
error("Couldn't create socket: %s", strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
|
2002-01-21 12:44:12 +00:00
|
|
|
if (tcp_port != 0) {
|
|
|
|
error("Couldn't connect to PRNGD port %d: %s",
|
|
|
|
tcp_port, strerror(errno));
|
|
|
|
} else {
|
|
|
|
error("Couldn't connect to PRNGD socket \"%s\": %s",
|
|
|
|
addr_un->sun_path, strerror(errno));
|
|
|
|
}
|
2001-12-23 14:41:47 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send blocking read request to PRNGD */
|
|
|
|
msg[0] = 0x02;
|
|
|
|
msg[1] = len;
|
|
|
|
|
2003-07-06 05:20:46 +00:00
|
|
|
if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
|
2001-12-23 14:41:47 +00:00
|
|
|
if (errno == EPIPE && errors < 10) {
|
|
|
|
close(fd);
|
|
|
|
errors++;
|
|
|
|
goto reopen;
|
|
|
|
}
|
|
|
|
error("Couldn't write to PRNGD socket: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2005-06-19 00:19:43 +00:00
|
|
|
if (atomicio(read, fd, buf, len) != (size_t)len) {
|
2001-12-23 14:41:47 +00:00
|
|
|
if (errno == EPIPE && errors < 10) {
|
|
|
|
close(fd);
|
|
|
|
errors++;
|
|
|
|
goto reopen;
|
|
|
|
}
|
|
|
|
error("Couldn't read from PRNGD socket: %s",
|
|
|
|
strerror(errno));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
rval = 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
done:
|
|
|
|
mysignal(SIGPIPE, old_sigpipe);
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
2002-01-21 12:44:12 +00:00
|
|
|
return rval;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
2004-12-20 01:05:08 +00:00
|
|
|
static int
|
|
|
|
seed_from_prngd(unsigned char *buf, size_t bytes)
|
|
|
|
{
|
|
|
|
#ifdef PRNGD_PORT
|
|
|
|
debug("trying egd/prngd port %d", PRNGD_PORT);
|
|
|
|
if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
#ifdef PRNGD_SOCKET
|
|
|
|
debug("trying egd/prngd socket %s", PRNGD_SOCKET);
|
|
|
|
if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-12-23 14:41:47 +00:00
|
|
|
double
|
|
|
|
stir_gettimeofday(double entropy_estimate)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (gettimeofday(&tv, NULL) == -1)
|
|
|
|
fatal("Couldn't gettimeofday: %s", strerror(errno));
|
|
|
|
|
|
|
|
RAND_add(&tv, sizeof(tv), entropy_estimate);
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
return entropy_estimate;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_clock(double entropy_estimate)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_CLOCK
|
|
|
|
clock_t c;
|
|
|
|
|
|
|
|
c = clock();
|
|
|
|
RAND_add(&c, sizeof(c), entropy_estimate);
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
return entropy_estimate;
|
2001-12-23 14:41:47 +00:00
|
|
|
#else /* _HAVE_CLOCK */
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
#endif /* _HAVE_CLOCK */
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_rusage(int who, double entropy_estimate)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_GETRUSAGE
|
|
|
|
struct rusage ru;
|
|
|
|
|
|
|
|
if (getrusage(who, &ru) == -1)
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
RAND_add(&ru, sizeof(ru), entropy_estimate);
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
return entropy_estimate;
|
2001-12-23 14:41:47 +00:00
|
|
|
#else /* _HAVE_GETRUSAGE */
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
#endif /* _HAVE_GETRUSAGE */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-01-21 12:44:12 +00:00
|
|
|
timeval_diff(struct timeval *t1, struct timeval *t2)
|
|
|
|
{
|
2001-12-23 14:41:47 +00:00
|
|
|
int secdiff, usecdiff;
|
|
|
|
|
|
|
|
secdiff = t2->tv_sec - t1->tv_sec;
|
|
|
|
usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
|
|
|
|
return (int)(usecdiff / 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2002-07-28 20:42:23 +00:00
|
|
|
hash_command_output(entropy_cmd_t *src, unsigned char *hash)
|
2001-12-23 14:41:47 +00:00
|
|
|
{
|
2002-01-21 12:44:12 +00:00
|
|
|
char buf[8192];
|
2001-12-23 14:41:47 +00:00
|
|
|
fd_set rdset;
|
2002-01-21 12:44:12 +00:00
|
|
|
int bytes_read, cmd_eof, error_abort, msec_elapsed, p[2];
|
|
|
|
int status, total_bytes_read;
|
|
|
|
static int devnull = -1;
|
2001-12-23 14:41:47 +00:00
|
|
|
pid_t pid;
|
|
|
|
SHA_CTX sha;
|
2002-01-21 12:44:12 +00:00
|
|
|
struct timeval tv_start, tv_current;
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
debug3("Reading output from \'%s\'", src->cmdstring);
|
|
|
|
|
|
|
|
if (devnull == -1) {
|
|
|
|
devnull = open("/dev/null", O_RDWR);
|
|
|
|
if (devnull == -1)
|
2003-11-21 12:48:55 +00:00
|
|
|
fatal("Couldn't open /dev/null: %s",
|
2002-01-21 12:44:12 +00:00
|
|
|
strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pipe(p) == -1)
|
|
|
|
fatal("Couldn't open pipe: %s", strerror(errno));
|
|
|
|
|
|
|
|
(void)gettimeofday(&tv_start, NULL); /* record start time */
|
|
|
|
|
|
|
|
switch (pid = fork()) {
|
|
|
|
case -1: /* Error */
|
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
fatal("Couldn't fork: %s", strerror(errno));
|
|
|
|
/* NOTREACHED */
|
|
|
|
case 0: /* Child */
|
|
|
|
dup2(devnull, STDIN_FILENO);
|
|
|
|
dup2(p[1], STDOUT_FILENO);
|
|
|
|
dup2(p[1], STDERR_FILENO);
|
|
|
|
close(p[0]);
|
|
|
|
close(p[1]);
|
|
|
|
close(devnull);
|
|
|
|
|
|
|
|
execv(src->path, (char**)(src->args));
|
2002-01-21 12:44:12 +00:00
|
|
|
|
2003-11-21 12:48:55 +00:00
|
|
|
debug("(child) Couldn't exec '%s': %s",
|
2002-01-21 12:44:12 +00:00
|
|
|
src->cmdstring, strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
_exit(-1);
|
|
|
|
default: /* Parent */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
RAND_add(&pid, sizeof(&pid), 0.0);
|
|
|
|
|
|
|
|
close(p[1]);
|
|
|
|
|
|
|
|
/* Hash output from child */
|
|
|
|
SHA1_Init(&sha);
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
cmd_eof = error_abort = msec_elapsed = total_bytes_read = 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
while (!error_abort && !cmd_eof) {
|
|
|
|
int ret;
|
|
|
|
struct timeval tv;
|
|
|
|
int msec_remaining;
|
|
|
|
|
|
|
|
(void) gettimeofday(&tv_current, 0);
|
2002-01-21 12:44:12 +00:00
|
|
|
msec_elapsed = timeval_diff(&tv_start, &tv_current);
|
2001-12-23 14:41:47 +00:00
|
|
|
if (msec_elapsed >= entropy_timeout_current) {
|
|
|
|
error_abort=1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
msec_remaining = entropy_timeout_current - msec_elapsed;
|
|
|
|
|
|
|
|
FD_ZERO(&rdset);
|
|
|
|
FD_SET(p[0], &rdset);
|
2002-01-21 12:44:12 +00:00
|
|
|
tv.tv_sec = msec_remaining / 1000;
|
2001-12-23 14:41:47 +00:00
|
|
|
tv.tv_usec = (msec_remaining % 1000) * 1000;
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
ret = select(p[0] + 1, &rdset, NULL, NULL, &tv);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
RAND_add(&tv, sizeof(tv), 0.0);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
/* timer expired */
|
|
|
|
error_abort = 1;
|
2002-10-21 00:13:35 +00:00
|
|
|
kill(pid, SIGINT);
|
2001-12-23 14:41:47 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* command input */
|
|
|
|
do {
|
|
|
|
bytes_read = read(p[0], buf, sizeof(buf));
|
|
|
|
} while (bytes_read == -1 && errno == EINTR);
|
|
|
|
RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
|
|
|
|
if (bytes_read == -1) {
|
|
|
|
error_abort = 1;
|
|
|
|
break;
|
|
|
|
} else if (bytes_read) {
|
|
|
|
SHA1_Update(&sha, buf, bytes_read);
|
|
|
|
total_bytes_read += bytes_read;
|
|
|
|
} else {
|
|
|
|
cmd_eof = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
default:
|
|
|
|
/* error */
|
2003-11-21 12:48:55 +00:00
|
|
|
debug("Command '%s': select() failed: %s",
|
2002-01-21 12:44:12 +00:00
|
|
|
src->cmdstring, strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
error_abort = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SHA1_Final(hash, &sha);
|
|
|
|
|
|
|
|
close(p[0]);
|
|
|
|
|
|
|
|
debug3("Time elapsed: %d msec", msec_elapsed);
|
|
|
|
|
|
|
|
if (waitpid(pid, &status, 0) == -1) {
|
2005-07-17 07:26:43 +00:00
|
|
|
error("Couldn't wait for child '%s' completion: %s",
|
|
|
|
src->cmdstring, strerror(errno));
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0.0;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RAND_add(&status, sizeof(&status), 0.0);
|
|
|
|
|
|
|
|
if (error_abort) {
|
2002-01-21 12:44:12 +00:00
|
|
|
/*
|
|
|
|
* Closing p[0] on timeout causes the entropy command to
|
2003-11-21 12:48:55 +00:00
|
|
|
* SIGPIPE. Take whatever output we got, and mark this
|
|
|
|
* command as slow
|
2002-01-21 12:44:12 +00:00
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
debug2("Command '%s' timed out", src->cmdstring);
|
|
|
|
src->sticky_badness *= 2;
|
|
|
|
src->badness = src->sticky_badness;
|
2002-01-21 12:44:12 +00:00
|
|
|
return total_bytes_read;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
2002-01-21 12:44:12 +00:00
|
|
|
if (WEXITSTATUS(status) == 0) {
|
|
|
|
return total_bytes_read;
|
2001-12-23 14:41:47 +00:00
|
|
|
} else {
|
2002-01-21 12:44:12 +00:00
|
|
|
debug2("Command '%s' exit status was %d",
|
|
|
|
src->cmdstring, WEXITSTATUS(status));
|
2001-12-23 14:41:47 +00:00
|
|
|
src->badness = src->sticky_badness = 128;
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0.0;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
} else if (WIFSIGNALED(status)) {
|
2002-01-21 12:44:12 +00:00
|
|
|
debug2("Command '%s' returned on uncaught signal %d !",
|
|
|
|
src->cmdstring, status);
|
2001-12-23 14:41:47 +00:00
|
|
|
src->badness = src->sticky_badness = 128;
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0.0;
|
2001-12-23 14:41:47 +00:00
|
|
|
} else
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_from_system(void)
|
|
|
|
{
|
|
|
|
double total_entropy_estimate;
|
|
|
|
long int i;
|
|
|
|
|
|
|
|
total_entropy_estimate = 0;
|
|
|
|
|
|
|
|
i = getpid();
|
|
|
|
RAND_add(&i, sizeof(i), 0.5);
|
|
|
|
total_entropy_estimate += 0.1;
|
|
|
|
|
|
|
|
i = getppid();
|
|
|
|
RAND_add(&i, sizeof(i), 0.5);
|
|
|
|
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);
|
|
|
|
total_entropy_estimate += stir_clock(0.5);
|
|
|
|
total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
|
|
|
|
|
|
|
|
return total_entropy_estimate;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
stir_from_programs(void)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
double entropy, total_entropy;
|
2002-07-28 20:42:23 +00:00
|
|
|
unsigned char hash[SHA_DIGEST_LENGTH];
|
2002-01-21 12:44:12 +00:00
|
|
|
|
|
|
|
total_entropy = 0;
|
|
|
|
for(c = 0; entropy_cmds[c].path != NULL; c++) {
|
|
|
|
if (!entropy_cmds[c].badness) {
|
|
|
|
/* Hash output from command */
|
|
|
|
entropy = hash_command_output(&entropy_cmds[c],
|
|
|
|
hash);
|
|
|
|
|
|
|
|
/* Scale back estimate by command's rate */
|
|
|
|
entropy *= entropy_cmds[c].rate;
|
|
|
|
|
|
|
|
/* Upper bound of entropy is SHA_DIGEST_LENGTH */
|
|
|
|
if (entropy > SHA_DIGEST_LENGTH)
|
|
|
|
entropy = SHA_DIGEST_LENGTH;
|
|
|
|
|
|
|
|
/* Stir it in */
|
|
|
|
RAND_add(hash, sizeof(hash), entropy);
|
|
|
|
|
2003-11-21 12:48:55 +00:00
|
|
|
debug3("Got %0.2f bytes of entropy from '%s'",
|
2002-01-21 12:44:12 +00:00
|
|
|
entropy, entropy_cmds[c].cmdstring);
|
|
|
|
|
|
|
|
total_entropy += entropy;
|
|
|
|
|
|
|
|
/* Execution time should be a bit unpredictable */
|
|
|
|
total_entropy += stir_gettimeofday(0.05);
|
|
|
|
total_entropy += stir_clock(0.05);
|
|
|
|
total_entropy += stir_rusage(RUSAGE_SELF, 0.1);
|
|
|
|
total_entropy += stir_rusage(RUSAGE_CHILDREN, 0.1);
|
|
|
|
} else {
|
|
|
|
debug2("Command '%s' disabled (badness %d)",
|
2003-11-21 12:48:55 +00:00
|
|
|
entropy_cmds[c].cmdstring,
|
2002-01-21 12:44:12 +00:00
|
|
|
entropy_cmds[c].badness);
|
|
|
|
|
|
|
|
if (entropy_cmds[c].badness > 0)
|
|
|
|
entropy_cmds[c].badness--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_entropy;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* prng seedfile functions
|
|
|
|
*/
|
|
|
|
int
|
2002-01-21 12:44:12 +00:00
|
|
|
prng_check_seedfile(char *filename)
|
|
|
|
{
|
2001-12-23 14:41:47 +00:00
|
|
|
struct stat st;
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/*
|
2003-11-21 12:48:55 +00:00
|
|
|
* XXX raceable: eg replace seed between this stat and subsequent
|
|
|
|
* open. Not such a problem because we don't really trust the
|
2002-01-21 12:44:12 +00:00
|
|
|
* seed file anyway.
|
|
|
|
* XXX: use secure path checking as elsewhere in OpenSSH
|
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
if (lstat(filename, &st) == -1) {
|
|
|
|
/* Give up on hard errors */
|
|
|
|
if (errno != ENOENT)
|
2002-01-21 12:44:12 +00:00
|
|
|
debug("WARNING: Couldn't stat random seed file "
|
|
|
|
"\"%.100s\": %s", filename, strerror(errno));
|
|
|
|
return 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* regular file? */
|
|
|
|
if (!S_ISREG(st.st_mode))
|
2002-01-21 12:44:12 +00:00
|
|
|
fatal("PRNG seedfile %.100s is not a regular file",
|
|
|
|
filename);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* mode 0600, owned by root or the current user? */
|
|
|
|
if (((st.st_mode & 0177) != 0) || !(st.st_uid == getuid())) {
|
2002-01-21 12:44:12 +00:00
|
|
|
debug("WARNING: PRNG seedfile %.100s must be mode 0600, "
|
2003-05-16 05:51:44 +00:00
|
|
|
"owned by uid %li", filename, (long int)getuid());
|
2002-01-21 12:44:12 +00:00
|
|
|
return 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
return 1;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-21 12:44:12 +00:00
|
|
|
prng_write_seedfile(void)
|
|
|
|
{
|
2005-02-16 02:02:45 +00:00
|
|
|
int fd, save_errno;
|
2002-07-28 20:42:23 +00:00
|
|
|
unsigned char seed[SEED_FILE_SIZE];
|
2005-02-16 02:02:45 +00:00
|
|
|
char filename[MAXPATHLEN], tmpseed[MAXPATHLEN];
|
2001-12-23 14:41:47 +00:00
|
|
|
struct passwd *pw;
|
2005-02-16 02:02:45 +00:00
|
|
|
mode_t old_umask;
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if (pw == NULL)
|
2002-01-21 12:44:12 +00:00
|
|
|
fatal("Couldn't get password entry for current user "
|
2003-05-16 05:51:44 +00:00
|
|
|
"(%li): %s", (long int)getuid(), strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* Try to ensure that the parent directory is there */
|
|
|
|
snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
|
2002-01-21 12:44:12 +00:00
|
|
|
_PATH_SSH_USER_DIR);
|
2001-12-23 14:41:47 +00:00
|
|
|
mkdir(filename, 0700);
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
|
2002-01-21 12:44:12 +00:00
|
|
|
SSH_PRNG_SEED_FILE);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2005-02-16 02:02:45 +00:00
|
|
|
strlcpy(tmpseed, filename, sizeof(tmpseed));
|
|
|
|
if (strlcat(tmpseed, ".XXXXXXXXXX", sizeof(tmpseed)) >=
|
|
|
|
sizeof(tmpseed))
|
|
|
|
fatal("PRNG seed filename too long");
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2003-03-17 05:13:53 +00:00
|
|
|
if (RAND_bytes(seed, sizeof(seed)) <= 0)
|
2003-09-22 15:36:15 +00:00
|
|
|
fatal("PRNG seed extraction failed");
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* Don't care if the seed doesn't exist */
|
|
|
|
prng_check_seedfile(filename);
|
|
|
|
|
2005-02-16 02:02:45 +00:00
|
|
|
old_umask = umask(0177);
|
|
|
|
|
|
|
|
if ((fd = mkstemp(tmpseed)) == -1) {
|
|
|
|
debug("WARNING: couldn't make temporary PRNG seedfile %.100s "
|
|
|
|
"(%.100s)", tmpseed, strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
} else {
|
2005-02-16 02:02:45 +00:00
|
|
|
debug("writing PRNG seed to file %.100s", tmpseed);
|
|
|
|
if (atomicio(vwrite, fd, &seed, sizeof(seed)) < sizeof(seed)) {
|
|
|
|
save_errno = errno;
|
|
|
|
close(fd);
|
|
|
|
unlink(tmpseed);
|
2002-01-21 12:44:12 +00:00
|
|
|
fatal("problem writing PRNG seedfile %.100s "
|
2005-02-16 02:02:45 +00:00
|
|
|
"(%.100s)", filename, strerror(save_errno));
|
|
|
|
}
|
2001-12-23 14:41:47 +00:00
|
|
|
close(fd);
|
2005-02-16 02:02:45 +00:00
|
|
|
debug("moving temporary PRNG seed to file %.100s", filename);
|
|
|
|
if (rename(tmpseed, filename) == -1) {
|
|
|
|
save_errno = errno;
|
|
|
|
unlink(tmpseed);
|
|
|
|
fatal("problem renaming PRNG seedfile from %.100s "
|
2005-07-17 07:04:47 +00:00
|
|
|
"to %.100s (%.100s)", tmpseed, filename,
|
2005-02-16 02:02:45 +00:00
|
|
|
strerror(save_errno));
|
|
|
|
}
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
2005-02-16 02:02:45 +00:00
|
|
|
umask(old_umask);
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-01-21 12:44:12 +00:00
|
|
|
prng_read_seedfile(void)
|
|
|
|
{
|
2001-12-23 14:41:47 +00:00
|
|
|
int fd;
|
2002-01-21 12:44:12 +00:00
|
|
|
char seed[SEED_FILE_SIZE], filename[MAXPATHLEN];
|
2001-12-23 14:41:47 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
|
|
|
|
pw = getpwuid(getuid());
|
|
|
|
if (pw == NULL)
|
2002-01-21 12:44:12 +00:00
|
|
|
fatal("Couldn't get password entry for current user "
|
2003-05-16 05:51:44 +00:00
|
|
|
"(%li): %s", (long int)getuid(), strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
|
|
|
|
SSH_PRNG_SEED_FILE);
|
|
|
|
|
|
|
|
debug("loading PRNG seed from file %.100s", filename);
|
|
|
|
|
|
|
|
if (!prng_check_seedfile(filename)) {
|
2002-01-21 12:44:12 +00:00
|
|
|
verbose("Random seed file not found or invalid, ignoring.");
|
2001-12-23 14:41:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* open the file and read in the seed */
|
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd == -1)
|
2002-01-21 12:44:12 +00:00
|
|
|
fatal("could not open PRNG seedfile %.100s (%.100s)",
|
|
|
|
filename, strerror(errno));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
if (atomicio(read, fd, &seed, sizeof(seed)) < sizeof(seed)) {
|
|
|
|
verbose("invalid or short read from PRNG seedfile "
|
|
|
|
"%.100s - ignoring", filename);
|
2001-12-23 14:41:47 +00:00
|
|
|
memset(seed, '\0', sizeof(seed));
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
/* stir in the seed, with estimated entropy zero */
|
|
|
|
RAND_add(&seed, sizeof(seed), 0.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* entropy command initialisation functions
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
prng_read_commands(char *cmdfilename)
|
|
|
|
{
|
2002-01-21 12:44:12 +00:00
|
|
|
char cmd[SEED_FILE_SIZE], *cp, line[1024], path[SEED_FILE_SIZE];
|
2001-12-23 14:41:47 +00:00
|
|
|
double est;
|
2002-01-21 12:44:12 +00:00
|
|
|
entropy_cmd_t *entcmd;
|
|
|
|
FILE *f;
|
|
|
|
int cur_cmd, linenum, num_cmds, arg;
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
if ((f = fopen(cmdfilename, "r")) == NULL) {
|
2001-12-23 14:41:47 +00:00
|
|
|
fatal("couldn't read entropy commands file %.100s: %.100s",
|
|
|
|
cmdfilename, strerror(errno));
|
|
|
|
}
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
num_cmds = 64;
|
|
|
|
entcmd = xmalloc(num_cmds * sizeof(entropy_cmd_t));
|
|
|
|
memset(entcmd, '\0', num_cmds * sizeof(entropy_cmd_t));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* Read in file */
|
2002-01-21 12:44:12 +00:00
|
|
|
cur_cmd = linenum = 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
while (fgets(line, sizeof(line), f)) {
|
|
|
|
linenum++;
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Skip leading whitespace, blank lines and comments */
|
2001-12-23 14:41:47 +00:00
|
|
|
cp = line + strspn(line, WHITESPACE);
|
|
|
|
if ((*cp == 0) || (*cp == '#'))
|
|
|
|
continue; /* done with this line */
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/*
|
2003-11-21 12:48:55 +00:00
|
|
|
* The first non-whitespace char should be a double quote
|
2002-01-21 12:44:12 +00:00
|
|
|
* delimiting the commandline
|
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
if (*cp != '"') {
|
2002-01-21 12:44:12 +00:00
|
|
|
error("bad entropy command, %.100s line %d",
|
|
|
|
cmdfilename, linenum);
|
2001-12-23 14:41:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/*
|
|
|
|
* First token, command args (incl. argv[0]) in double
|
|
|
|
* quotes
|
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
cp = strtok(cp, "\"");
|
|
|
|
if (cp == NULL) {
|
2002-01-21 12:44:12 +00:00
|
|
|
error("missing or bad command string, %.100s "
|
|
|
|
"line %d -- ignored", cmdfilename, linenum);
|
2001-12-23 14:41:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
strlcpy(cmd, cp, sizeof(cmd));
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Second token, full command path */
|
2001-12-23 14:41:47 +00:00
|
|
|
if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
|
2002-01-21 12:44:12 +00:00
|
|
|
error("missing command path, %.100s "
|
|
|
|
"line %d -- ignored", cmdfilename, linenum);
|
2001-12-23 14:41:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Did configure mark this as dead? */
|
2001-12-23 14:41:47 +00:00
|
|
|
if (strncmp("undef", cp, 5) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
strlcpy(path, cp, sizeof(path));
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Third token, entropy rate estimate for this command */
|
2001-12-23 14:41:47 +00:00
|
|
|
if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
|
2002-01-21 12:44:12 +00:00
|
|
|
error("missing entropy estimate, %.100s "
|
|
|
|
"line %d -- ignored", cmdfilename, linenum);
|
2001-12-23 14:41:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-01-21 12:44:12 +00:00
|
|
|
est = strtod(cp, NULL);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* end of line */
|
|
|
|
if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
|
2002-01-21 12:44:12 +00:00
|
|
|
error("garbage at end of line %d in %.100s "
|
|
|
|
"-- ignored", linenum, cmdfilename);
|
2001-12-23 14:41:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save the command for debug messages */
|
|
|
|
entcmd[cur_cmd].cmdstring = xstrdup(cmd);
|
|
|
|
|
|
|
|
/* split the command args */
|
|
|
|
cp = strtok(cmd, WHITESPACE);
|
|
|
|
arg = 0;
|
|
|
|
do {
|
2002-01-21 12:44:12 +00:00
|
|
|
entcmd[cur_cmd].args[arg] = xstrdup(cp);
|
2001-12-23 14:41:47 +00:00
|
|
|
arg++;
|
2002-01-21 12:44:12 +00:00
|
|
|
} while(arg < NUM_ARGS && (cp = strtok(NULL, WHITESPACE)));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
if (strtok(NULL, WHITESPACE))
|
2002-01-21 12:44:12 +00:00
|
|
|
error("ignored extra commands (max %d), %.100s "
|
|
|
|
"line %d", NUM_ARGS, cmdfilename, linenum);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* 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++;
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/*
|
|
|
|
* If we've filled the array, reallocate it twice the size
|
2003-11-21 12:48:55 +00:00
|
|
|
* Do this now because even if this we're on the last
|
2002-01-21 12:44:12 +00:00
|
|
|
* command we need another slot to mark the last entry
|
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
if (cur_cmd == num_cmds) {
|
|
|
|
num_cmds *= 2;
|
2002-01-21 12:44:12 +00:00
|
|
|
entcmd = xrealloc(entcmd, num_cmds *
|
|
|
|
sizeof(entropy_cmd_t));
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* zero the last entry */
|
2002-01-21 12:44:12 +00:00
|
|
|
memset(&entcmd[cur_cmd], '\0', sizeof(entropy_cmd_t));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
/* trim to size */
|
2002-01-21 12:44:12 +00:00
|
|
|
entropy_cmds = xrealloc(entcmd, (cur_cmd + 1) *
|
|
|
|
sizeof(entropy_cmd_t));
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
debug("Loaded %d entropy commands from %.100s", cur_cmd,
|
|
|
|
cmdfilename);
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
return cur_cmd < MIN_ENTROPY_SOURCES ? -1 : 0;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s [options]\n", __progname);
|
|
|
|
fprintf(stderr, " -v Verbose; display verbose debugging messages.\n");
|
|
|
|
fprintf(stderr, " Multiple -v increases verbosity.\n");
|
2004-08-23 11:52:08 +00:00
|
|
|
fprintf(stderr, " -x Force output in hexadecimal (for debugging)\n");
|
2002-04-14 09:27:12 +00:00
|
|
|
fprintf(stderr, " -X Force output in binary\n");
|
|
|
|
fprintf(stderr, " -b bytes Number of bytes to output (default %d)\n",
|
|
|
|
OUTPUT_SEED_SIZE);
|
|
|
|
}
|
|
|
|
|
2003-11-21 12:48:55 +00:00
|
|
|
int
|
2001-12-23 14:41:47 +00:00
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2002-04-14 09:27:12 +00:00
|
|
|
unsigned char *buf;
|
|
|
|
int ret, ch, debug_level, output_hex, bytes;
|
|
|
|
extern char *optarg;
|
|
|
|
LogLevel ll;
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2003-08-21 23:34:41 +00:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
2001-12-23 14:41:47 +00:00
|
|
|
log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
|
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
ll = SYSLOG_LEVEL_INFO;
|
|
|
|
debug_level = output_hex = 0;
|
|
|
|
bytes = OUTPUT_SEED_SIZE;
|
|
|
|
|
|
|
|
/* Don't write binary data to a tty, unless we are forced to */
|
|
|
|
if (isatty(STDOUT_FILENO))
|
|
|
|
output_hex = 1;
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
while ((ch = getopt(argc, argv, "vxXhb:")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'v':
|
|
|
|
if (debug_level < 3)
|
|
|
|
ll = SYSLOG_LEVEL_DEBUG1 + debug_level++;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
output_hex = 1;
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
output_hex = 0;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
if ((bytes = atoi(optarg)) <= 0)
|
|
|
|
fatal("Invalid number of output bytes");
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
default:
|
|
|
|
error("Invalid commandline option");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1);
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
#ifdef USE_SEED_FILES
|
|
|
|
prng_read_seedfile();
|
|
|
|
#endif
|
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
buf = xmalloc(bytes);
|
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/*
|
|
|
|
* Seed the RNG from wherever we can
|
|
|
|
*/
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2002-01-21 12:44:12 +00:00
|
|
|
/* Take whatever is on the stack, but don't credit it */
|
2002-04-14 09:27:12 +00:00
|
|
|
RAND_add(buf, bytes, 0);
|
2002-01-21 12:44:12 +00:00
|
|
|
|
2003-11-21 12:48:55 +00:00
|
|
|
debug("Seeded RNG with %i bytes from system calls",
|
2002-01-21 12:44:12 +00:00
|
|
|
(int)stir_from_system());
|
|
|
|
|
2004-12-20 01:05:08 +00:00
|
|
|
/* try prngd, fall back to commands if prngd fails or not configured */
|
|
|
|
if (seed_from_prngd(buf, bytes) == 0) {
|
|
|
|
RAND_add(buf, bytes, bytes);
|
|
|
|
} else {
|
|
|
|
/* Read in collection commands */
|
|
|
|
if (prng_read_commands(SSH_PRNG_COMMAND_FILE) == -1)
|
|
|
|
fatal("PRNG initialisation failed -- exiting.");
|
|
|
|
debug("Seeded RNG with %i bytes from programs",
|
|
|
|
(int)stir_from_programs());
|
|
|
|
}
|
2002-01-21 12:44:12 +00:00
|
|
|
|
|
|
|
#ifdef USE_SEED_FILES
|
|
|
|
prng_write_seedfile();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the seed to stdout
|
|
|
|
*/
|
2001-12-23 14:41:47 +00:00
|
|
|
|
|
|
|
if (!RAND_status())
|
|
|
|
fatal("Not enough entropy in RNG");
|
|
|
|
|
2003-03-17 05:13:53 +00:00
|
|
|
if (RAND_bytes(buf, bytes) <= 0)
|
|
|
|
fatal("Couldn't extract entropy from PRNG");
|
2001-12-23 14:41:47 +00:00
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
if (output_hex) {
|
|
|
|
for(ret = 0; ret < bytes; ret++)
|
|
|
|
printf("%02x", (unsigned char)(buf[ret]));
|
|
|
|
printf("\n");
|
|
|
|
} else
|
2003-07-06 05:20:46 +00:00
|
|
|
ret = atomicio(vwrite, STDOUT_FILENO, buf, bytes);
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
memset(buf, '\0', bytes);
|
|
|
|
xfree(buf);
|
2003-11-21 12:56:47 +00:00
|
|
|
|
2002-04-14 09:27:12 +00:00
|
|
|
return ret == bytes ? 0 : 1;
|
2001-12-23 14:41:47 +00:00
|
|
|
}
|
2005-02-16 02:20:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We may attempt to re-seed during mkstemp if we are using the one in the
|
2005-02-16 02:32:30 +00:00
|
|
|
* compat library (via mkstemp -> _gettemp -> arc4random -> seed_rng) so we
|
|
|
|
* need our own seed_rng(). We must also check that we have enough entropy.
|
2005-02-16 02:20:07 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
seed_rng(void)
|
|
|
|
{
|
|
|
|
if (!RAND_status())
|
|
|
|
fatal("Not enough entropy in RNG");
|
|
|
|
}
|