blob: 665f77324b1785868451df1c17c79dbcb1e3d0c7 [file] [log] [blame]
Damien Miller040f3832000-04-03 14:50:43 +10001/*
2 * Copyright (c) 2000 Damien Miller. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
Damien Miller040f3832000-04-03 14:50:43 +100012 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "includes.h"
26
Damien Miller5f056372000-04-16 12:31:48 +100027#include <openssl/rand.h>
28#include <openssl/sha.h>
Damien Miller767c7fc2001-02-27 09:20:57 +110029#include <openssl/crypto.h>
Damien Miller040f3832000-04-03 14:50:43 +100030
Damien Millerecbb26d2000-07-15 14:59:14 +100031/* SunOS 4.4.4 needs this */
32#ifdef HAVE_FLOATINGPOINT_H
33# include <floatingpoint.h>
34#endif /* HAVE_FLOATINGPOINT_H */
35
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036#include "ssh.h"
Damien Millera1072a82001-02-18 15:28:11 +110037#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038#include "xmalloc.h"
39#include "atomicio.h"
Ben Lindstromcb577332001-01-22 21:06:19 +000040#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041#include "log.h"
42
Damien Millerd0ccb982001-03-04 00:29:20 +110043RCSID("$Id: entropy.c,v 1.35 2001/03/03 13:29:21 djm Exp $");
Damien Miller040f3832000-04-03 14:50:43 +100044
Damien Miller040f3832000-04-03 14:50:43 +100045#ifndef offsetof
46# define offsetof(type, member) ((size_t) &((type *)0)->member)
47#endif
Damien Miller14c12cb2000-06-07 22:20:23 +100048
Damien Miller14c12cb2000-06-07 22:20:23 +100049/* Number of times to pass through command list gathering entropy */
50#define NUM_ENTROPY_RUNS 1
51
52/* Scale entropy estimates back by this amount on subsequent runs */
53#define SCALE_PER_RUN 10.0
54
55/* Minimum number of commands to be considered valid */
56#define MIN_ENTROPY_SOURCES 16
57
58#define WHITESPACE " \t\n"
59
Damien Miller7b22d652000-06-18 14:07:04 +100060#ifndef RUSAGE_SELF
61# define RUSAGE_SELF 0
62#endif
63#ifndef RUSAGE_CHILDREN
64# define RUSAGE_CHILDREN 0
65#endif
66
Damien Millerfbd884a2001-02-27 08:39:07 +110067#if defined(_POSIX_SAVED_IDS) && !defined(BROKEN_SAVED_UIDS)
68# define SAVED_IDS_WORK_WITH_SETEUID
69#endif
70
Damien Miller767c7fc2001-02-27 09:20:57 +110071void check_openssl_version(void)
72{
73 if (SSLeay() != OPENSSL_VERSION_NUMBER)
Damien Miller3456d322001-02-27 11:00:52 +110074 fatal("OpenSSL version mismatch. Built against %lx, you "
75 "have %lx", OPENSSL_VERSION_NUMBER, SSLeay());
Damien Miller767c7fc2001-02-27 09:20:57 +110076}
77
Damien Millerd0ccb982001-03-04 00:29:20 +110078#if defined(PRNGD_SOCKET) || defined(PRNGD_PORT)
79# define USE_PRNGD
80#endif
Damien Miller767c7fc2001-02-27 09:20:57 +110081
Damien Millerd0ccb982001-03-04 00:29:20 +110082#if defined(USE_PRNGD) || defined(RANDOM_POOL)
Damien Miller14c12cb2000-06-07 22:20:23 +100083
Damien Millerd0ccb982001-03-04 00:29:20 +110084#ifdef USE_PRNGD
85/* Collect entropy from PRNGD/EGD */
Damien Miller64681252000-06-26 13:01:33 +100086int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +100087{
Damien Miller64681252000-06-26 13:01:33 +100088 int fd;
89 char msg[2];
Damien Millerd0ccb982001-03-04 00:29:20 +110090#ifdef PRNGD_PORT
91 struct sockaddr_in addr;
92#else
Damien Miller040f3832000-04-03 14:50:43 +100093 struct sockaddr_un addr;
Damien Millerd0ccb982001-03-04 00:29:20 +110094#endif
Damien Millerb3ffc5f2001-02-18 12:44:29 +110095 int addr_len, rval, errors;
Damien Millera1072a82001-02-18 15:28:11 +110096 mysig_t old_sigpipe;
Damien Miller040f3832000-04-03 14:50:43 +100097
Damien Millerd0ccb982001-03-04 00:29:20 +110098 memset(&addr, '\0', sizeof(addr));
99
100#ifdef PRNGD_PORT
101 addr.sin_family = AF_INET;
102 addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
103 addr.sin_port = htons(PRNGD_PORT);
104 addr_len = sizeof(struct sockaddr_in);
105#else /* use IP socket PRNGD_SOCKET instead */
Damien Miller64681252000-06-26 13:01:33 +1000106 /* Sanity checks */
Damien Millerd0ccb982001-03-04 00:29:20 +1100107 if (sizeof(PRNGD_SOCKET) > sizeof(addr.sun_path))
Damien Miller040f3832000-04-03 14:50:43 +1000108 fatal("Random pool path is too long");
Damien Miller040f3832000-04-03 14:50:43 +1000109 if (len > 255)
Damien Millerd0ccb982001-03-04 00:29:20 +1100110 fatal("Too many bytes to read from PRNGD");
Damien Miller64681252000-06-26 13:01:33 +1000111
Damien Miller64681252000-06-26 13:01:33 +1000112 addr.sun_family = AF_UNIX;
Damien Millerd0ccb982001-03-04 00:29:20 +1100113 strlcpy(addr.sun_path, PRNGD_SOCKET, sizeof(addr.sun_path));
114 addr_len = offsetof(struct sockaddr_un, sun_path) +
115 sizeof(PRNGD_SOCKET);
116#endif
Kevin Stevesef4eea92001-02-05 12:42:17 +0000117
Damien Millera1072a82001-02-18 15:28:11 +1100118 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100119
120 errors = rval = 0;
121reopen:
Damien Millerd0ccb982001-03-04 00:29:20 +1100122#ifdef PRNGD_PORT
123 fd = socket(addr.sin_family, SOCK_STREAM, 0);
124 if (fd == -1) {
125 error("Couldn't create AF_INET socket: %s", strerror(errno));
126 goto done;
127 }
128#else
129 fd = socket(addr.sun_family, SOCK_STREAM, 0);
Damien Miller64681252000-06-26 13:01:33 +1000130 if (fd == -1) {
131 error("Couldn't create AF_UNIX socket: %s", strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100132 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000133 }
Damien Millerd0ccb982001-03-04 00:29:20 +1100134#endif
Damien Miller64681252000-06-26 13:01:33 +1000135
136 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
Damien Millerd0ccb982001-03-04 00:29:20 +1100137#ifdef PRNGD_PORT
138 error("Couldn't connect to PRNGD port %d: %s",
139 PRNGD_PORT, strerror(errno));
140#else
141 error("Couldn't connect to PRNGD socket \"%s\": %s",
142 addr.sun_path, strerror(errno));
143#endif
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100144 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000145 }
146
Damien Millerd0ccb982001-03-04 00:29:20 +1100147 /* Send blocking read request to PRNGD */
Damien Miller64681252000-06-26 13:01:33 +1000148 msg[0] = 0x02;
149 msg[1] = len;
Damien Miller040f3832000-04-03 14:50:43 +1000150
Damien Miller64681252000-06-26 13:01:33 +1000151 if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100152 if (errno == EPIPE && errors < 10) {
153 close(fd);
154 errors++;
155 goto reopen;
156 }
Damien Millerd0ccb982001-03-04 00:29:20 +1100157 error("Couldn't write to PRNGD socket: %s",
158 strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100159 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000160 }
Damien Miller040f3832000-04-03 14:50:43 +1000161
Damien Miller64681252000-06-26 13:01:33 +1000162 if (atomicio(read, fd, buf, len) != len) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100163 if (errno == EPIPE && errors < 10) {
164 close(fd);
165 errors++;
166 goto reopen;
167 }
Damien Millerd0ccb982001-03-04 00:29:20 +1100168 error("Couldn't read from PRNGD socket: %s",
169 strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100170 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000171 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000172
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100173 rval = 1;
174done:
Kevin Steves4679f5b2001-02-18 11:34:32 +0000175 mysignal(SIGPIPE, old_sigpipe);
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100176 if (fd != -1)
177 close(fd);
178 return(rval);
Damien Miller040f3832000-04-03 14:50:43 +1000179}
Damien Millerd0ccb982001-03-04 00:29:20 +1100180#else /* !USE_PRNGD */
Damien Miller040f3832000-04-03 14:50:43 +1000181#ifdef RANDOM_POOL
182/* Collect entropy from /dev/urandom or pipe */
Damien Miller64681252000-06-26 13:01:33 +1000183int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +1000184{
Damien Miller64681252000-06-26 13:01:33 +1000185 int random_pool;
Damien Miller040f3832000-04-03 14:50:43 +1000186
Damien Miller64681252000-06-26 13:01:33 +1000187 random_pool = open(RANDOM_POOL, O_RDONLY);
Damien Miller040f3832000-04-03 14:50:43 +1000188 if (random_pool == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000189 error("Couldn't open random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000190 RANDOM_POOL, strerror(errno));
191 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000192 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000193
Damien Miller64681252000-06-26 13:01:33 +1000194 if (atomicio(read, random_pool, buf, len) != len) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000195 error("Couldn't read from random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000196 RANDOM_POOL, strerror(errno));
197 close(random_pool);
198 return(0);
199 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000200
Damien Miller64681252000-06-26 13:01:33 +1000201 close(random_pool);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000202
Damien Miller64681252000-06-26 13:01:33 +1000203 return(1);
Damien Miller040f3832000-04-03 14:50:43 +1000204}
205#endif /* RANDOM_POOL */
Damien Millerd0ccb982001-03-04 00:29:20 +1100206#endif /* USE_PRNGD */
Damien Miller040f3832000-04-03 14:50:43 +1000207
Damien Miller14c12cb2000-06-07 22:20:23 +1000208/*
209 * Seed OpenSSL's random number pool from Kernel random number generator
Damien Millerd0ccb982001-03-04 00:29:20 +1100210 * or PRNGD/EGD
Damien Miller14c12cb2000-06-07 22:20:23 +1000211 */
212void
213seed_rng(void)
214{
Damien Millerd0ccb982001-03-04 00:29:20 +1100215 unsigned char buf[32];
Kevin Stevesef4eea92001-02-05 12:42:17 +0000216
Damien Miller14c12cb2000-06-07 22:20:23 +1000217 debug("Seeding random number generator");
Damien Miller64681252000-06-26 13:01:33 +1000218
Damien Miller08006472000-06-26 13:55:31 +1000219 if (!get_random_bytes(buf, sizeof(buf))) {
220 if (!RAND_status())
221 fatal("Entropy collection failed and entropy exhausted");
222 } else {
223 RAND_add(buf, sizeof(buf), sizeof(buf));
224 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000225
Damien Miller14c12cb2000-06-07 22:20:23 +1000226 memset(buf, '\0', sizeof(buf));
227}
228
Damien Miller767c7fc2001-02-27 09:20:57 +1100229void init_rng(void)
230{
231 check_openssl_version();
232}
Damien Millerf9b625c2000-07-09 22:42:32 +1000233
Damien Millerd0ccb982001-03-04 00:29:20 +1100234#else /* defined(USE_PRNGD) || defined(RANDOM_POOL) */
Damien Miller14c12cb2000-06-07 22:20:23 +1000235
Kevin Stevesef4eea92001-02-05 12:42:17 +0000236/*
Damien Miller040f3832000-04-03 14:50:43 +1000237 * FIXME: proper entropy estimations. All current values are guesses
Damien Miller4018c192000-04-30 09:30:44 +1000238 * FIXME: (ATL) do estimates at compile time?
Damien Miller040f3832000-04-03 14:50:43 +1000239 * FIXME: More entropy sources
240 */
241
Damien Miller4018c192000-04-30 09:30:44 +1000242/* slow command timeouts (all in milliseconds) */
243/* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
244static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
245
Damien Miller0437b332000-05-02 09:56:41 +1000246static int prng_seed_saved = 0;
Damien Millerf9b625c2000-07-09 22:42:32 +1000247static int prng_initialised = 0;
248uid_t original_uid;
Damien Miller040f3832000-04-03 14:50:43 +1000249
250typedef struct
251{
252 /* Proportion of data that is entropy */
253 double rate;
Damien Miller4018c192000-04-30 09:30:44 +1000254 /* Counter goes positive if this command times out */
255 unsigned int badness;
256 /* Increases by factor of two each timeout */
257 unsigned int sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000258 /* Path to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000259 char *path;
Damien Miller040f3832000-04-03 14:50:43 +1000260 /* argv to pass to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000261 char *args[5];
Damien Miller8d1fd572000-05-17 21:34:07 +1000262 /* full command string (debug) */
263 char *cmdstring;
Damien Miller040f3832000-04-03 14:50:43 +1000264} entropy_source_t;
265
Damien Miller4018c192000-04-30 09:30:44 +1000266double stir_from_system(void);
267double stir_from_programs(void);
268double stir_gettimeofday(double entropy_estimate);
269double stir_clock(double entropy_estimate);
270double stir_rusage(int who, double entropy_estimate);
271double hash_output_from_command(entropy_source_t *src, char *hash);
272
Damien Miller0437b332000-05-02 09:56:41 +1000273/* this is initialised from a file, by prng_read_commands() */
274entropy_source_t *entropy_sources = NULL;
Damien Miller040f3832000-04-03 14:50:43 +1000275
Kevin Stevesef4eea92001-02-05 12:42:17 +0000276double
Damien Miller040f3832000-04-03 14:50:43 +1000277stir_from_system(void)
278{
279 double total_entropy_estimate;
280 long int i;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000281
Damien Miller040f3832000-04-03 14:50:43 +1000282 total_entropy_estimate = 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000283
Damien Miller040f3832000-04-03 14:50:43 +1000284 i = getpid();
Damien Miller7b22d652000-06-18 14:07:04 +1000285 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000286 total_entropy_estimate += 0.1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000287
Damien Miller040f3832000-04-03 14:50:43 +1000288 i = getppid();
Damien Miller7b22d652000-06-18 14:07:04 +1000289 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000290 total_entropy_estimate += 0.1;
291
292 i = getuid();
293 RAND_add(&i, sizeof(i), 0.0);
294 i = getgid();
295 RAND_add(&i, sizeof(i), 0.0);
296
297 total_entropy_estimate += stir_gettimeofday(1.0);
Damien Miller7b22d652000-06-18 14:07:04 +1000298 total_entropy_estimate += stir_clock(0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000299 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
300
301 return(total_entropy_estimate);
302}
303
Kevin Stevesef4eea92001-02-05 12:42:17 +0000304double
Damien Miller040f3832000-04-03 14:50:43 +1000305stir_from_programs(void)
306{
307 int i;
308 int c;
309 double entropy_estimate;
310 double total_entropy_estimate;
311 char hash[SHA_DIGEST_LENGTH];
312
Damien Miller040f3832000-04-03 14:50:43 +1000313 total_entropy_estimate = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000314 for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
Damien Miller040f3832000-04-03 14:50:43 +1000315 c = 0;
316 while (entropy_sources[c].path != NULL) {
Damien Miller040f3832000-04-03 14:50:43 +1000317
Damien Miller4018c192000-04-30 09:30:44 +1000318 if (!entropy_sources[c].badness) {
319 /* Hash output from command */
320 entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
321
322 /* Scale back entropy estimate according to command's rate */
323 entropy_estimate *= entropy_sources[c].rate;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000324
Damien Miller4018c192000-04-30 09:30:44 +1000325 /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
326 if (entropy_estimate > SHA_DIGEST_LENGTH)
327 entropy_estimate = SHA_DIGEST_LENGTH;
Damien Miller040f3832000-04-03 14:50:43 +1000328
Kevin Stevesef4eea92001-02-05 12:42:17 +0000329 /* Scale back estimates for subsequent passes through list */
Damien Miller14c12cb2000-06-07 22:20:23 +1000330 entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000331
Damien Miller4018c192000-04-30 09:30:44 +1000332 /* Stir it in */
333 RAND_add(hash, sizeof(hash), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000334
Kevin Stevesef4eea92001-02-05 12:42:17 +0000335 debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate,
Damien Miller8d1fd572000-05-17 21:34:07 +1000336 entropy_sources[c].cmdstring);
Damien Miller040f3832000-04-03 14:50:43 +1000337
Damien Miller4018c192000-04-30 09:30:44 +1000338 total_entropy_estimate += entropy_estimate;
Damien Miller040f3832000-04-03 14:50:43 +1000339
340 /* Execution times should be a little unpredictable */
Damien Miller4018c192000-04-30 09:30:44 +1000341 total_entropy_estimate += stir_gettimeofday(0.05);
342 total_entropy_estimate += stir_clock(0.05);
343 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
344 total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
345 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +1100346 debug2("Command '%s' disabled (badness %d)",
Damien Miller8d1fd572000-05-17 21:34:07 +1000347 entropy_sources[c].cmdstring, entropy_sources[c].badness);
Damien Miller4018c192000-04-30 09:30:44 +1000348
349 if (entropy_sources[c].badness > 0)
350 entropy_sources[c].badness--;
351 }
352
Damien Miller040f3832000-04-03 14:50:43 +1000353 c++;
354 }
355 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000356
Damien Miller040f3832000-04-03 14:50:43 +1000357 return(total_entropy_estimate);
358}
359
360double
361stir_gettimeofday(double entropy_estimate)
362{
363 struct timeval tv;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000364
Damien Miller040f3832000-04-03 14:50:43 +1000365 if (gettimeofday(&tv, NULL) == -1)
366 fatal("Couldn't gettimeofday: %s", strerror(errno));
367
368 RAND_add(&tv, sizeof(tv), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000369
Damien Miller040f3832000-04-03 14:50:43 +1000370 return(entropy_estimate);
371}
372
373double
374stir_clock(double entropy_estimate)
375{
376#ifdef HAVE_CLOCK
377 clock_t c;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000378
Damien Miller040f3832000-04-03 14:50:43 +1000379 c = clock();
380 RAND_add(&c, sizeof(c), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000381
Damien Miller040f3832000-04-03 14:50:43 +1000382 return(entropy_estimate);
383#else /* _HAVE_CLOCK */
384 return(0);
385#endif /* _HAVE_CLOCK */
386}
387
388double
389stir_rusage(int who, double entropy_estimate)
390{
391#ifdef HAVE_GETRUSAGE
392 struct rusage ru;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000393
Damien Miller4018c192000-04-30 09:30:44 +1000394 if (getrusage(who, &ru) == -1)
Damien Miller7b22d652000-06-18 14:07:04 +1000395 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000396
Damien Miller7b22d652000-06-18 14:07:04 +1000397 RAND_add(&ru, sizeof(ru), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000398
399 return(entropy_estimate);
400#else /* _HAVE_GETRUSAGE */
401 return(0);
402#endif /* _HAVE_GETRUSAGE */
403}
404
Damien Miller8d1fd572000-05-17 21:34:07 +1000405
406static
407int
408_get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
409 int secdiff, usecdiff;
410
411 secdiff = t2->tv_sec - t1->tv_sec;
412 usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
413 return (int)(usecdiff / 1000);
414}
415
Damien Miller040f3832000-04-03 14:50:43 +1000416double
Damien Miller4018c192000-04-30 09:30:44 +1000417hash_output_from_command(entropy_source_t *src, char *hash)
Damien Miller040f3832000-04-03 14:50:43 +1000418{
419 static int devnull = -1;
420 int p[2];
Damien Miller4018c192000-04-30 09:30:44 +1000421 fd_set rdset;
422 int cmd_eof = 0, error_abort = 0;
Damien Miller8d1fd572000-05-17 21:34:07 +1000423 struct timeval tv_start, tv_current;
424 int msec_elapsed = 0;
Damien Miller040f3832000-04-03 14:50:43 +1000425 pid_t pid;
426 int status;
Damien Miller8d1fd572000-05-17 21:34:07 +1000427 char buf[16384];
Damien Miller040f3832000-04-03 14:50:43 +1000428 int bytes_read;
429 int total_bytes_read;
430 SHA_CTX sha;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000431
Damien Millercb5e44a2000-09-29 12:12:36 +1100432 debug3("Reading output from \'%s\'", src->cmdstring);
433
Damien Miller040f3832000-04-03 14:50:43 +1000434 if (devnull == -1) {
435 devnull = open("/dev/null", O_RDWR);
436 if (devnull == -1)
437 fatal("Couldn't open /dev/null: %s", strerror(errno));
438 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000439
Damien Miller040f3832000-04-03 14:50:43 +1000440 if (pipe(p) == -1)
441 fatal("Couldn't open pipe: %s", strerror(errno));
442
Damien Miller8d1fd572000-05-17 21:34:07 +1000443 (void)gettimeofday(&tv_start, NULL); /* record start time */
444
Damien Miller040f3832000-04-03 14:50:43 +1000445 switch (pid = fork()) {
446 case -1: /* Error */
447 close(p[0]);
448 close(p[1]);
449 fatal("Couldn't fork: %s", strerror(errno));
450 /* NOTREACHED */
451 case 0: /* Child */
Damien Miller4018c192000-04-30 09:30:44 +1000452 dup2(devnull, STDIN_FILENO);
453 dup2(p[1], STDOUT_FILENO);
454 dup2(p[1], STDERR_FILENO);
Damien Miller040f3832000-04-03 14:50:43 +1000455 close(p[0]);
456 close(p[1]);
457 close(devnull);
458
Damien Millerf9b625c2000-07-09 22:42:32 +1000459 setuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000460 execv(src->path, (char**)(src->args));
Damien Miller8d1fd572000-05-17 21:34:07 +1000461 debug("(child) Couldn't exec '%s': %s", src->cmdstring,
462 strerror(errno));
Damien Miller040f3832000-04-03 14:50:43 +1000463 _exit(-1);
464 default: /* Parent */
465 break;
466 }
467
468 RAND_add(&pid, sizeof(&pid), 0.0);
469
470 close(p[1]);
471
472 /* Hash output from child */
473 SHA1_Init(&sha);
474 total_bytes_read = 0;
Damien Miller4018c192000-04-30 09:30:44 +1000475
476 while (!error_abort && !cmd_eof) {
477 int ret;
478 struct timeval tv;
Damien Miller8d1fd572000-05-17 21:34:07 +1000479 int msec_remaining;
480
481 (void) gettimeofday(&tv_current, 0);
Damien Miller14c12cb2000-06-07 22:20:23 +1000482 msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
Damien Miller8d1fd572000-05-17 21:34:07 +1000483 if (msec_elapsed >= entropy_timeout_current) {
484 error_abort=1;
485 continue;
486 }
487 msec_remaining = entropy_timeout_current - msec_elapsed;
Damien Miller4018c192000-04-30 09:30:44 +1000488
489 FD_ZERO(&rdset);
490 FD_SET(p[0], &rdset);
Damien Miller8d1fd572000-05-17 21:34:07 +1000491 tv.tv_sec = msec_remaining / 1000;
492 tv.tv_usec = (msec_remaining % 1000) * 1000;
Damien Miller4018c192000-04-30 09:30:44 +1000493
494 ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
Damien Miller8d1fd572000-05-17 21:34:07 +1000495
Damien Millerf9b625c2000-07-09 22:42:32 +1000496 RAND_add(&tv, sizeof(tv), 0.0);
497
Damien Miller4018c192000-04-30 09:30:44 +1000498 switch (ret) {
499 case 0:
500 /* timer expired */
501 error_abort = 1;
502 break;
Damien Miller4018c192000-04-30 09:30:44 +1000503 case 1:
504 /* command input */
505 bytes_read = read(p[0], buf, sizeof(buf));
Damien Millerf9b625c2000-07-09 22:42:32 +1000506 RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
Damien Miller4018c192000-04-30 09:30:44 +1000507 if (bytes_read == -1) {
508 error_abort = 1;
509 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000510 } else if (bytes_read) {
Damien Miller8d1fd572000-05-17 21:34:07 +1000511 SHA1_Update(&sha, buf, bytes_read);
512 total_bytes_read += bytes_read;
Damien Millerf9b625c2000-07-09 22:42:32 +1000513 } else {
Damien Miller8d1fd572000-05-17 21:34:07 +1000514 cmd_eof = 1;
Damien Millerf9b625c2000-07-09 22:42:32 +1000515 }
Damien Miller4018c192000-04-30 09:30:44 +1000516 break;
Damien Miller4018c192000-04-30 09:30:44 +1000517 case -1:
518 default:
Damien Millerf9b625c2000-07-09 22:42:32 +1000519 /* error */
Damien Miller8d1fd572000-05-17 21:34:07 +1000520 debug("Command '%s': select() failed: %s", src->cmdstring,
521 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000522 error_abort = 1;
523 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000524 }
525 }
Damien Miller4018c192000-04-30 09:30:44 +1000526
Damien Miller040f3832000-04-03 14:50:43 +1000527 SHA1_Final(hash, &sha);
528
529 close(p[0]);
Damien Miller8d1fd572000-05-17 21:34:07 +1000530
Damien Millercb5e44a2000-09-29 12:12:36 +1100531 debug3("Time elapsed: %d msec", msec_elapsed);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000532
Damien Miller040f3832000-04-03 14:50:43 +1000533 if (waitpid(pid, &status, 0) == -1) {
Damien Millercb5e44a2000-09-29 12:12:36 +1100534 error("Couldn't wait for child '%s' completion: %s", src->cmdstring,
Damien Miller8d1fd572000-05-17 21:34:07 +1000535 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000536 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000537 }
538
539 RAND_add(&status, sizeof(&status), 0.0);
540
Damien Miller4018c192000-04-30 09:30:44 +1000541 if (error_abort) {
542 /* closing p[0] on timeout causes the entropy command to
543 * SIGPIPE. Take whatever output we got, and mark this command
544 * as slow */
Damien Millercb5e44a2000-09-29 12:12:36 +1100545 debug2("Command '%s' timed out", src->cmdstring);
Damien Miller4018c192000-04-30 09:30:44 +1000546 src->sticky_badness *= 2;
547 src->badness = src->sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000548 return(total_bytes_read);
Damien Miller4018c192000-04-30 09:30:44 +1000549 }
550
551 if (WIFEXITED(status)) {
552 if (WEXITSTATUS(status)==0) {
553 return(total_bytes_read);
554 } else {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000555 debug2("Command '%s' exit status was %d", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000556 WEXITSTATUS(status));
Damien Miller4018c192000-04-30 09:30:44 +1000557 src->badness = src->sticky_badness = 128;
558 return (0.0);
559 }
560 } else if (WIFSIGNALED(status)) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000561 debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000562 status);
Damien Miller4018c192000-04-30 09:30:44 +1000563 src->badness = src->sticky_badness = 128;
564 return(0.0);
565 } else
566 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000567}
Damien Miller4018c192000-04-30 09:30:44 +1000568
569/*
570 * prng seedfile functions
571 */
572int
573prng_check_seedfile(char *filename) {
574
575 struct stat st;
576
577 /* FIXME raceable: eg replace seed between this stat and subsequent open */
578 /* Not such a problem because we don't trust the seed file anyway */
579 if (lstat(filename, &st) == -1) {
Damien Miller52dc96b2000-10-16 20:13:43 +1100580 /* Give up on hard errors */
Damien Miller4018c192000-04-30 09:30:44 +1000581 if (errno != ENOENT)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000582 debug("WARNING: Couldn't stat random seed file \"%s\": %s",
Damien Miller52dc96b2000-10-16 20:13:43 +1100583 filename, strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000584
585 return(0);
586 }
587
588 /* regular file? */
589 if (!S_ISREG(st.st_mode))
590 fatal("PRNG seedfile %.100s is not a regular file", filename);
591
592 /* mode 0600, owned by root or the current user? */
Damien Miller52dc96b2000-10-16 20:13:43 +1100593 if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) {
594 debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d",
Damien Miller4018c192000-04-30 09:30:44 +1000595 filename, getuid());
Damien Miller52dc96b2000-10-16 20:13:43 +1100596 return(0);
597 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598
Damien Miller4018c192000-04-30 09:30:44 +1000599 return(1);
600}
601
602void
603prng_write_seedfile(void) {
604 int fd;
605 char seed[1024];
606 char filename[1024];
607 struct passwd *pw;
608
609 /* Don't bother if we have already saved a seed */
610 if (prng_seed_saved)
611 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000612
Damien Millerf9b625c2000-07-09 22:42:32 +1000613 setuid(original_uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000614
Damien Millerfc0b11b2000-05-02 00:03:55 +1000615 prng_seed_saved = 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000616
Damien Millerf9b625c2000-07-09 22:42:32 +1000617 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000618 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000619 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000620 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000621
Damien Miller4018c192000-04-30 09:30:44 +1000622 /* Try to ensure that the parent directory is there */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000623 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Ben Lindstromcb577332001-01-22 21:06:19 +0000624 _PATH_SSH_USER_DIR);
Damien Miller4018c192000-04-30 09:30:44 +1000625 mkdir(filename, 0700);
626
Kevin Stevesef4eea92001-02-05 12:42:17 +0000627 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000628 SSH_PRNG_SEED_FILE);
629
630 debug("writing PRNG seed to file %.100s", filename);
631
632 RAND_bytes(seed, sizeof(seed));
633
634 /* Don't care if the seed doesn't exist */
635 prng_check_seedfile(filename);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000636
Damien Miller52dc96b2000-10-16 20:13:43 +1100637 if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000638 debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)",
Damien Miller52dc96b2000-10-16 20:13:43 +1100639 filename, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000640 } else {
Damien Miller52dc96b2000-10-16 20:13:43 +1100641 if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000642 fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
Damien Miller52dc96b2000-10-16 20:13:43 +1100643 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000644
Damien Miller52dc96b2000-10-16 20:13:43 +1100645 close(fd);
646 }
Damien Miller4018c192000-04-30 09:30:44 +1000647}
648
649void
650prng_read_seedfile(void) {
651 int fd;
652 char seed[1024];
653 char filename[1024];
654 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000655
Damien Millerf9b625c2000-07-09 22:42:32 +1000656 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000657 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000658 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000659 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000660
661 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000662 SSH_PRNG_SEED_FILE);
663
664 debug("loading PRNG seed from file %.100s", filename);
665
666 if (!prng_check_seedfile(filename)) {
Damien Miller21de4502001-01-17 09:37:15 +1100667 verbose("Random seed file not found or not valid, ignoring.");
Damien Miller4018c192000-04-30 09:30:44 +1000668 return;
669 }
670
671 /* open the file and read in the seed */
672 fd = open(filename, O_RDONLY);
673 if (fd == -1)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000674 fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
Damien Miller4018c192000-04-30 09:30:44 +1000675 strerror(errno));
676
677 if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
678 verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
679 filename);
680 memset(seed, '\0', sizeof(seed));
681 }
682 close(fd);
683
684 /* stir in the seed, with estimated entropy zero */
685 RAND_add(&seed, sizeof(seed), 0.0);
686}
687
Damien Miller0437b332000-05-02 09:56:41 +1000688
689/*
690 * entropy command initialisation functions
691 */
Damien Miller0437b332000-05-02 09:56:41 +1000692int
693prng_read_commands(char *cmdfilename)
694{
695 FILE *f;
Damien Miller0437b332000-05-02 09:56:41 +1000696 char *cp;
Damien Miller14c12cb2000-06-07 22:20:23 +1000697 char line[1024];
698 char cmd[1024];
699 char path[256];
Damien Miller0437b332000-05-02 09:56:41 +1000700 int linenum;
Damien Miller0437b332000-05-02 09:56:41 +1000701 int num_cmds = 64;
702 int cur_cmd = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000703 double est;
704 entropy_source_t *entcmd;
Damien Miller0437b332000-05-02 09:56:41 +1000705
706 f = fopen(cmdfilename, "r");
707 if (!f) {
708 fatal("couldn't read entropy commands file %.100s: %.100s",
709 cmdfilename, strerror(errno));
710 }
711
Damien Miller0437b332000-05-02 09:56:41 +1000712 entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
713 memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
714
Damien Miller14c12cb2000-06-07 22:20:23 +1000715 /* Read in file */
716 linenum = 0;
Damien Miller0437b332000-05-02 09:56:41 +1000717 while (fgets(line, sizeof(line), f)) {
Damien Miller14c12cb2000-06-07 22:20:23 +1000718 int arg;
719 char *argv;
720
Damien Miller0437b332000-05-02 09:56:41 +1000721 linenum++;
722
723 /* skip leading whitespace, test for blank line or comment */
724 cp = line + strspn(line, WHITESPACE);
725 if ((*cp == 0) || (*cp == '#'))
726 continue; /* done with this line */
727
Damien Miller14c12cb2000-06-07 22:20:23 +1000728 /* First non-whitespace char should be double quote delimiting */
729 /* commandline */
730 if (*cp != '"') {
Damien Miller0437b332000-05-02 09:56:41 +1000731 error("bad entropy command, %.100s line %d", cmdfilename,
732 linenum);
733 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000734 }
Damien Miller14c12cb2000-06-07 22:20:23 +1000735
736 /* first token, command args (incl. argv[0]) in double quotes */
737 cp = strtok(cp, "\"");
738 if (cp == NULL) {
739 error("missing or bad command string, %.100s line %d -- ignored",
740 cmdfilename, linenum);
741 continue;
742 }
743 strlcpy(cmd, cp, sizeof(cmd));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000744
Damien Miller14c12cb2000-06-07 22:20:23 +1000745 /* second token, full command path */
746 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
747 error("missing command path, %.100s line %d -- ignored",
748 cmdfilename, linenum);
749 continue;
750 }
751
752 /* did configure mark this as dead? */
753 if (strncmp("undef", cp, 5) == 0)
754 continue;
755
Kevin Stevesef4eea92001-02-05 12:42:17 +0000756 strlcpy(path, cp, sizeof(path));
Damien Miller14c12cb2000-06-07 22:20:23 +1000757
758 /* third token, entropy rate estimate for this command */
759 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
760 error("missing entropy estimate, %.100s line %d -- ignored",
761 cmdfilename, linenum);
762 continue;
763 }
764 est = strtod(cp, &argv);
765
766 /* end of line */
767 if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000768 error("garbage at end of line %d in %.100s -- ignored", linenum,
Damien Miller14c12cb2000-06-07 22:20:23 +1000769 cmdfilename);
770 continue;
771 }
772
773 /* save the command for debug messages */
774 entcmd[cur_cmd].cmdstring = xstrdup(cmd);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000775
Damien Miller14c12cb2000-06-07 22:20:23 +1000776 /* split the command args */
777 cp = strtok(cmd, WHITESPACE);
778 arg = 0;
779 argv = NULL;
780 do {
781 char *s = (char*)xmalloc(strlen(cp) + 1);
782 strncpy(s, cp, strlen(cp) + 1);
783 entcmd[cur_cmd].args[arg] = s;
784 arg++;
785 } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000786
Damien Miller14c12cb2000-06-07 22:20:23 +1000787 if (strtok(NULL, WHITESPACE))
788 error("ignored extra command elements (max 5), %.100s line %d",
789 cmdfilename, linenum);
790
791 /* Copy the command path and rate estimate */
792 entcmd[cur_cmd].path = xstrdup(path);
793 entcmd[cur_cmd].rate = est;
794
795 /* Initialise other values */
796 entcmd[cur_cmd].sticky_badness = 1;
797
798 cur_cmd++;
799
800 /* If we've filled the array, reallocate it twice the size */
801 /* Do this now because even if this we're on the last command,
802 we need another slot to mark the last entry */
803 if (cur_cmd == num_cmds) {
804 num_cmds *= 2;
805 entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
Damien Miller0437b332000-05-02 09:56:41 +1000806 }
807 }
808
809 /* zero the last entry */
810 memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
Damien Miller14c12cb2000-06-07 22:20:23 +1000811
Damien Miller0437b332000-05-02 09:56:41 +1000812 /* trim to size */
813 entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
814
Damien Millerf9b625c2000-07-09 22:42:32 +1000815 debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
Damien Miller0437b332000-05-02 09:56:41 +1000816
817 return (cur_cmd >= MIN_ENTROPY_SOURCES);
818}
819
Damien Miller040f3832000-04-03 14:50:43 +1000820/*
Damien Miller4018c192000-04-30 09:30:44 +1000821 * Write a keyfile at exit
Kevin Stevesef4eea92001-02-05 12:42:17 +0000822 */
Damien Miller4018c192000-04-30 09:30:44 +1000823void
824prng_seed_cleanup(void *junk)
825{
826 prng_write_seedfile();
827}
828
829/*
830 * Conditionally Seed OpenSSL's random number pool from
831 * syscalls and program output
Damien Miller040f3832000-04-03 14:50:43 +1000832 */
833void
834seed_rng(void)
835{
Damien Millera1072a82001-02-18 15:28:11 +1100836 mysig_t old_sigchld_handler;
Damien Miller0437b332000-05-02 09:56:41 +1000837
Damien Millerf9b625c2000-07-09 22:42:32 +1000838 if (!prng_initialised)
839 fatal("RNG not initialised");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000840
Damien Millerf3c6cf12000-05-17 22:08:29 +1000841 /* Make sure some other sigchld handler doesn't reap our entropy */
842 /* commands */
Damien Millera1072a82001-02-18 15:28:11 +1100843 old_sigchld_handler = mysignal(SIGCHLD, SIG_DFL);
Damien Millerf3c6cf12000-05-17 22:08:29 +1000844
Damien Millerf9b625c2000-07-09 22:42:32 +1000845 debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs());
846 debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system());
847
848 if (!RAND_status())
849 fatal("Not enough entropy in RNG");
Damien Miller4018c192000-04-30 09:30:44 +1000850
Damien Millera1072a82001-02-18 15:28:11 +1100851 mysignal(SIGCHLD, old_sigchld_handler);
Damien Millerf3c6cf12000-05-17 22:08:29 +1000852
Damien Miller8d1fd572000-05-17 21:34:07 +1000853 if (!RAND_status())
854 fatal("Couldn't initialise builtin random number generator -- exiting.");
Damien Miller040f3832000-04-03 14:50:43 +1000855}
Damien Millerf9b625c2000-07-09 22:42:32 +1000856
Kevin Stevesef4eea92001-02-05 12:42:17 +0000857void init_rng(void)
Damien Millerf9b625c2000-07-09 22:42:32 +1000858{
Damien Millerd592b632000-11-25 10:09:32 +1100859 int original_euid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000860
Damien Miller767c7fc2001-02-27 09:20:57 +1100861 check_openssl_version();
862
Damien Millerf9b625c2000-07-09 22:42:32 +1000863 original_uid = getuid();
Damien Millerd592b632000-11-25 10:09:32 +1100864 original_euid = geteuid();
Damien Millerf9b625c2000-07-09 22:42:32 +1000865
866 /* Read in collection commands */
867 if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
868 fatal("PRNG initialisation failed -- exiting.");
869
870 /* Set ourselves up to save a seed upon exit */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000871 prng_seed_saved = 0;
Damien Millerd592b632000-11-25 10:09:32 +1100872
873 /* Give up privs while reading seed file */
Damien Millerbb7c9762001-02-26 20:49:58 +1100874#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Millerd592b632000-11-25 10:09:32 +1100875 if ((original_uid != original_euid) && (seteuid(original_uid) == -1))
876 fatal("Couldn't give up privileges");
Damien Millerbb7c9762001-02-26 20:49:58 +1100877#else /* SAVED_IDS_WORK_WITH_SETEUID */
878 /*
879 * Propagate the privileged uid to all of our uids.
880 * Set the effective uid to the given (unprivileged) uid.
881 */
Damien Miller248131a2001-02-27 09:47:16 +1100882 if (original_uid != original_euid && (setuid(original_euid) == -1 ||
883 seteuid(original_uid) == -1))
Damien Millerbb7c9762001-02-26 20:49:58 +1100884 fatal("Couldn't give up privileges");
885#endif /* SAVED_IDS_WORK_WITH_SETEUID */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000886
Damien Millerf9b625c2000-07-09 22:42:32 +1000887 prng_read_seedfile();
Damien Millerd592b632000-11-25 10:09:32 +1100888
Damien Millerbb7c9762001-02-26 20:49:58 +1100889#ifdef SAVED_IDS_WORK_WITH_SETEUID
Damien Millerd592b632000-11-25 10:09:32 +1100890 if ((original_uid != original_euid) && (seteuid(original_euid) == -1))
891 fatal("Couldn't restore privileges");
Damien Millerbb7c9762001-02-26 20:49:58 +1100892#else /* SAVED_IDS_WORK_WITH_SETEUID */
893 /*
894 * We are unable to restore the real uid to its unprivileged value.
895 * Propagate the real uid (usually more privileged) to effective uid
896 * as well.
897 */
Damien Miller248131a2001-02-27 09:47:16 +1100898 if (original_uid != original_euid && (seteuid(original_euid) == -1 ||
899 setuid(original_uid) == -1))
Damien Millerbb7c9762001-02-26 20:49:58 +1100900 fatal("Couldn't restore privileges");
901#endif /* SAVED_IDS_WORK_WITH_SETEUID */
Damien Millerd592b632000-11-25 10:09:32 +1100902
Damien Millerf9b625c2000-07-09 22:42:32 +1000903 fatal_add_cleanup(prng_seed_cleanup, NULL);
904 atexit(prng_write_seedfile);
905
906 prng_initialised = 1;
907}
908
Damien Millerd0ccb982001-03-04 00:29:20 +1100909#endif /* defined(USE_PRNGD) || defined(RANDOM_POOL) */