blob: fb81d78220986bd28ace7ac895742fbcf1fd5f5c [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 Miller040f3832000-04-03 14:50:43 +100029
Damien Millerecbb26d2000-07-15 14:59:14 +100030/* SunOS 4.4.4 needs this */
31#ifdef HAVE_FLOATINGPOINT_H
32# include <floatingpoint.h>
33#endif /* HAVE_FLOATINGPOINT_H */
34
Ben Lindstrom226cfa02001-01-22 05:34:40 +000035#include "ssh.h"
36#include "xmalloc.h"
37#include "atomicio.h"
Ben Lindstromcb577332001-01-22 21:06:19 +000038#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000039#include "log.h"
40
Damien Millerb3ffc5f2001-02-18 12:44:29 +110041RCSID("$Id: entropy.c,v 1.27 2001/02/18 01:44:29 djm Exp $");
Damien Miller040f3832000-04-03 14:50:43 +100042
Damien Miller040f3832000-04-03 14:50:43 +100043#ifndef offsetof
44# define offsetof(type, member) ((size_t) &((type *)0)->member)
45#endif
Damien Miller14c12cb2000-06-07 22:20:23 +100046
Damien Miller14c12cb2000-06-07 22:20:23 +100047/* Number of times to pass through command list gathering entropy */
48#define NUM_ENTROPY_RUNS 1
49
50/* Scale entropy estimates back by this amount on subsequent runs */
51#define SCALE_PER_RUN 10.0
52
53/* Minimum number of commands to be considered valid */
54#define MIN_ENTROPY_SOURCES 16
55
56#define WHITESPACE " \t\n"
57
Damien Miller7b22d652000-06-18 14:07:04 +100058#ifndef RUSAGE_SELF
59# define RUSAGE_SELF 0
60#endif
61#ifndef RUSAGE_CHILDREN
62# define RUSAGE_CHILDREN 0
63#endif
64
Damien Miller14c12cb2000-06-07 22:20:23 +100065#if defined(EGD_SOCKET) || defined(RANDOM_POOL)
66
67#ifdef EGD_SOCKET
Damien Miller040f3832000-04-03 14:50:43 +100068/* Collect entropy from EGD */
Damien Miller64681252000-06-26 13:01:33 +100069int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +100070{
Damien Miller64681252000-06-26 13:01:33 +100071 int fd;
72 char msg[2];
Damien Miller040f3832000-04-03 14:50:43 +100073 struct sockaddr_un addr;
Damien Millerb3ffc5f2001-02-18 12:44:29 +110074 int addr_len, rval, errors;
75 struct sigaction nsa, osa;
Damien Miller040f3832000-04-03 14:50:43 +100076
Damien Miller64681252000-06-26 13:01:33 +100077 /* Sanity checks */
Damien Miller040f3832000-04-03 14:50:43 +100078 if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path))
79 fatal("Random pool path is too long");
Damien Miller040f3832000-04-03 14:50:43 +100080 if (len > 255)
81 fatal("Too many bytes to read from EGD");
Damien Miller64681252000-06-26 13:01:33 +100082
83 memset(&addr, '\0', sizeof(addr));
84 addr.sun_family = AF_UNIX;
85 strlcpy(addr.sun_path, EGD_SOCKET, sizeof(addr.sun_path));
86 addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET);
Kevin Stevesef4eea92001-02-05 12:42:17 +000087
Damien Millerb3ffc5f2001-02-18 12:44:29 +110088 memset(&nsa, 0, sizeof(nsa));
89 nsa.sa_handler = SIG_IGN;
90 (void) sigaction(SIGPIPE, &nsa, &osa);
91
92 errors = rval = 0;
93reopen:
Damien Miller64681252000-06-26 13:01:33 +100094 fd = socket(AF_UNIX, SOCK_STREAM, 0);
95 if (fd == -1) {
96 error("Couldn't create AF_UNIX socket: %s", strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +110097 goto done;
Damien Miller64681252000-06-26 13:01:33 +100098 }
99
100 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000101 error("Couldn't connect to EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000102 addr.sun_path, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100103 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000104 }
105
Damien Miller040f3832000-04-03 14:50:43 +1000106 /* Send blocking read request to EGD */
Damien Miller64681252000-06-26 13:01:33 +1000107 msg[0] = 0x02;
108 msg[1] = len;
Damien Miller040f3832000-04-03 14:50:43 +1000109
Damien Miller64681252000-06-26 13:01:33 +1000110 if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100111 if (errno == EPIPE && errors < 10) {
112 close(fd);
113 errors++;
114 goto reopen;
115 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000116 error("Couldn't write to EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000117 EGD_SOCKET, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100118 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000119 }
Damien Miller040f3832000-04-03 14:50:43 +1000120
Damien Miller64681252000-06-26 13:01:33 +1000121 if (atomicio(read, fd, buf, len) != len) {
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100122 if (errno == EPIPE && errors < 10) {
123 close(fd);
124 errors++;
125 goto reopen;
126 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000127 error("Couldn't read from EGD socket \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000128 EGD_SOCKET, strerror(errno));
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100129 goto done;
Damien Miller64681252000-06-26 13:01:33 +1000130 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000131
Damien Millerb3ffc5f2001-02-18 12:44:29 +1100132 rval = 1;
133done:
134 (void) sigaction(SIGPIPE, &osa, NULL);
135 if (fd != -1)
136 close(fd);
137 return(rval);
Damien Miller040f3832000-04-03 14:50:43 +1000138}
139#else /* !EGD_SOCKET */
140#ifdef RANDOM_POOL
141/* Collect entropy from /dev/urandom or pipe */
Damien Miller64681252000-06-26 13:01:33 +1000142int get_random_bytes(unsigned char *buf, int len)
Damien Miller040f3832000-04-03 14:50:43 +1000143{
Damien Miller64681252000-06-26 13:01:33 +1000144 int random_pool;
Damien Miller040f3832000-04-03 14:50:43 +1000145
Damien Miller64681252000-06-26 13:01:33 +1000146 random_pool = open(RANDOM_POOL, O_RDONLY);
Damien Miller040f3832000-04-03 14:50:43 +1000147 if (random_pool == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000148 error("Couldn't open random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000149 RANDOM_POOL, strerror(errno));
150 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000151 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000152
Damien Miller64681252000-06-26 13:01:33 +1000153 if (atomicio(read, random_pool, buf, len) != len) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000154 error("Couldn't read from random pool \"%s\": %s",
Damien Miller64681252000-06-26 13:01:33 +1000155 RANDOM_POOL, strerror(errno));
156 close(random_pool);
157 return(0);
158 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000159
Damien Miller64681252000-06-26 13:01:33 +1000160 close(random_pool);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000161
Damien Miller64681252000-06-26 13:01:33 +1000162 return(1);
Damien Miller040f3832000-04-03 14:50:43 +1000163}
164#endif /* RANDOM_POOL */
165#endif /* EGD_SOCKET */
166
Damien Miller14c12cb2000-06-07 22:20:23 +1000167/*
168 * Seed OpenSSL's random number pool from Kernel random number generator
169 * or EGD
170 */
171void
172seed_rng(void)
173{
174 char buf[32];
Kevin Stevesef4eea92001-02-05 12:42:17 +0000175
Damien Miller14c12cb2000-06-07 22:20:23 +1000176 debug("Seeding random number generator");
Damien Miller64681252000-06-26 13:01:33 +1000177
Damien Miller08006472000-06-26 13:55:31 +1000178 if (!get_random_bytes(buf, sizeof(buf))) {
179 if (!RAND_status())
180 fatal("Entropy collection failed and entropy exhausted");
181 } else {
182 RAND_add(buf, sizeof(buf), sizeof(buf));
183 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000184
Damien Miller14c12cb2000-06-07 22:20:23 +1000185 memset(buf, '\0', sizeof(buf));
186}
187
Damien Millerf9b625c2000-07-09 22:42:32 +1000188/* No-op */
189void init_rng(void) {}
190
Damien Miller14c12cb2000-06-07 22:20:23 +1000191#else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */
192
Kevin Stevesef4eea92001-02-05 12:42:17 +0000193/*
Damien Miller040f3832000-04-03 14:50:43 +1000194 * FIXME: proper entropy estimations. All current values are guesses
Damien Miller4018c192000-04-30 09:30:44 +1000195 * FIXME: (ATL) do estimates at compile time?
Damien Miller040f3832000-04-03 14:50:43 +1000196 * FIXME: More entropy sources
197 */
198
Damien Miller4018c192000-04-30 09:30:44 +1000199/* slow command timeouts (all in milliseconds) */
200/* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */
201static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC;
202
Damien Miller0437b332000-05-02 09:56:41 +1000203static int prng_seed_saved = 0;
Damien Millerf9b625c2000-07-09 22:42:32 +1000204static int prng_initialised = 0;
205uid_t original_uid;
Damien Miller040f3832000-04-03 14:50:43 +1000206
207typedef struct
208{
209 /* Proportion of data that is entropy */
210 double rate;
Damien Miller4018c192000-04-30 09:30:44 +1000211 /* Counter goes positive if this command times out */
212 unsigned int badness;
213 /* Increases by factor of two each timeout */
214 unsigned int sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000215 /* Path to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000216 char *path;
Damien Miller040f3832000-04-03 14:50:43 +1000217 /* argv to pass to executable */
Damien Miller0437b332000-05-02 09:56:41 +1000218 char *args[5];
Damien Miller8d1fd572000-05-17 21:34:07 +1000219 /* full command string (debug) */
220 char *cmdstring;
Damien Miller040f3832000-04-03 14:50:43 +1000221} entropy_source_t;
222
Damien Miller4018c192000-04-30 09:30:44 +1000223double stir_from_system(void);
224double stir_from_programs(void);
225double stir_gettimeofday(double entropy_estimate);
226double stir_clock(double entropy_estimate);
227double stir_rusage(int who, double entropy_estimate);
228double hash_output_from_command(entropy_source_t *src, char *hash);
229
Damien Miller0437b332000-05-02 09:56:41 +1000230/* this is initialised from a file, by prng_read_commands() */
231entropy_source_t *entropy_sources = NULL;
Damien Miller040f3832000-04-03 14:50:43 +1000232
Kevin Stevesef4eea92001-02-05 12:42:17 +0000233double
Damien Miller040f3832000-04-03 14:50:43 +1000234stir_from_system(void)
235{
236 double total_entropy_estimate;
237 long int i;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000238
Damien Miller040f3832000-04-03 14:50:43 +1000239 total_entropy_estimate = 0;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000240
Damien Miller040f3832000-04-03 14:50:43 +1000241 i = getpid();
Damien Miller7b22d652000-06-18 14:07:04 +1000242 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000243 total_entropy_estimate += 0.1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000244
Damien Miller040f3832000-04-03 14:50:43 +1000245 i = getppid();
Damien Miller7b22d652000-06-18 14:07:04 +1000246 RAND_add(&i, sizeof(i), 0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000247 total_entropy_estimate += 0.1;
248
249 i = getuid();
250 RAND_add(&i, sizeof(i), 0.0);
251 i = getgid();
252 RAND_add(&i, sizeof(i), 0.0);
253
254 total_entropy_estimate += stir_gettimeofday(1.0);
Damien Miller7b22d652000-06-18 14:07:04 +1000255 total_entropy_estimate += stir_clock(0.5);
Damien Miller040f3832000-04-03 14:50:43 +1000256 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0);
257
258 return(total_entropy_estimate);
259}
260
Kevin Stevesef4eea92001-02-05 12:42:17 +0000261double
Damien Miller040f3832000-04-03 14:50:43 +1000262stir_from_programs(void)
263{
264 int i;
265 int c;
266 double entropy_estimate;
267 double total_entropy_estimate;
268 char hash[SHA_DIGEST_LENGTH];
269
Damien Miller040f3832000-04-03 14:50:43 +1000270 total_entropy_estimate = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000271 for(i = 0; i < NUM_ENTROPY_RUNS; i++) {
Damien Miller040f3832000-04-03 14:50:43 +1000272 c = 0;
273 while (entropy_sources[c].path != NULL) {
Damien Miller040f3832000-04-03 14:50:43 +1000274
Damien Miller4018c192000-04-30 09:30:44 +1000275 if (!entropy_sources[c].badness) {
276 /* Hash output from command */
277 entropy_estimate = hash_output_from_command(&entropy_sources[c], hash);
278
279 /* Scale back entropy estimate according to command's rate */
280 entropy_estimate *= entropy_sources[c].rate;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000281
Damien Miller4018c192000-04-30 09:30:44 +1000282 /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */
283 if (entropy_estimate > SHA_DIGEST_LENGTH)
284 entropy_estimate = SHA_DIGEST_LENGTH;
Damien Miller040f3832000-04-03 14:50:43 +1000285
Kevin Stevesef4eea92001-02-05 12:42:17 +0000286 /* Scale back estimates for subsequent passes through list */
Damien Miller14c12cb2000-06-07 22:20:23 +1000287 entropy_estimate /= SCALE_PER_RUN * (i + 1.0);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000288
Damien Miller4018c192000-04-30 09:30:44 +1000289 /* Stir it in */
290 RAND_add(hash, sizeof(hash), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000291
Kevin Stevesef4eea92001-02-05 12:42:17 +0000292 debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate,
Damien Miller8d1fd572000-05-17 21:34:07 +1000293 entropy_sources[c].cmdstring);
Damien Miller040f3832000-04-03 14:50:43 +1000294
Damien Miller4018c192000-04-30 09:30:44 +1000295 total_entropy_estimate += entropy_estimate;
Damien Miller040f3832000-04-03 14:50:43 +1000296
297 /* Execution times should be a little unpredictable */
Damien Miller4018c192000-04-30 09:30:44 +1000298 total_entropy_estimate += stir_gettimeofday(0.05);
299 total_entropy_estimate += stir_clock(0.05);
300 total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1);
301 total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1);
302 } else {
Damien Millercb5e44a2000-09-29 12:12:36 +1100303 debug2("Command '%s' disabled (badness %d)",
Damien Miller8d1fd572000-05-17 21:34:07 +1000304 entropy_sources[c].cmdstring, entropy_sources[c].badness);
Damien Miller4018c192000-04-30 09:30:44 +1000305
306 if (entropy_sources[c].badness > 0)
307 entropy_sources[c].badness--;
308 }
309
Damien Miller040f3832000-04-03 14:50:43 +1000310 c++;
311 }
312 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000313
Damien Miller040f3832000-04-03 14:50:43 +1000314 return(total_entropy_estimate);
315}
316
317double
318stir_gettimeofday(double entropy_estimate)
319{
320 struct timeval tv;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000321
Damien Miller040f3832000-04-03 14:50:43 +1000322 if (gettimeofday(&tv, NULL) == -1)
323 fatal("Couldn't gettimeofday: %s", strerror(errno));
324
325 RAND_add(&tv, sizeof(tv), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000326
Damien Miller040f3832000-04-03 14:50:43 +1000327 return(entropy_estimate);
328}
329
330double
331stir_clock(double entropy_estimate)
332{
333#ifdef HAVE_CLOCK
334 clock_t c;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000335
Damien Miller040f3832000-04-03 14:50:43 +1000336 c = clock();
337 RAND_add(&c, sizeof(c), entropy_estimate);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000338
Damien Miller040f3832000-04-03 14:50:43 +1000339 return(entropy_estimate);
340#else /* _HAVE_CLOCK */
341 return(0);
342#endif /* _HAVE_CLOCK */
343}
344
345double
346stir_rusage(int who, double entropy_estimate)
347{
348#ifdef HAVE_GETRUSAGE
349 struct rusage ru;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000350
Damien Miller4018c192000-04-30 09:30:44 +1000351 if (getrusage(who, &ru) == -1)
Damien Miller7b22d652000-06-18 14:07:04 +1000352 return(0);
Damien Miller040f3832000-04-03 14:50:43 +1000353
Damien Miller7b22d652000-06-18 14:07:04 +1000354 RAND_add(&ru, sizeof(ru), entropy_estimate);
Damien Miller040f3832000-04-03 14:50:43 +1000355
356 return(entropy_estimate);
357#else /* _HAVE_GETRUSAGE */
358 return(0);
359#endif /* _HAVE_GETRUSAGE */
360}
361
Damien Miller8d1fd572000-05-17 21:34:07 +1000362
363static
364int
365_get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) {
366 int secdiff, usecdiff;
367
368 secdiff = t2->tv_sec - t1->tv_sec;
369 usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec);
370 return (int)(usecdiff / 1000);
371}
372
Damien Miller040f3832000-04-03 14:50:43 +1000373double
Damien Miller4018c192000-04-30 09:30:44 +1000374hash_output_from_command(entropy_source_t *src, char *hash)
Damien Miller040f3832000-04-03 14:50:43 +1000375{
376 static int devnull = -1;
377 int p[2];
Damien Miller4018c192000-04-30 09:30:44 +1000378 fd_set rdset;
379 int cmd_eof = 0, error_abort = 0;
Damien Miller8d1fd572000-05-17 21:34:07 +1000380 struct timeval tv_start, tv_current;
381 int msec_elapsed = 0;
Damien Miller040f3832000-04-03 14:50:43 +1000382 pid_t pid;
383 int status;
Damien Miller8d1fd572000-05-17 21:34:07 +1000384 char buf[16384];
Damien Miller040f3832000-04-03 14:50:43 +1000385 int bytes_read;
386 int total_bytes_read;
387 SHA_CTX sha;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000388
Damien Millercb5e44a2000-09-29 12:12:36 +1100389 debug3("Reading output from \'%s\'", src->cmdstring);
390
Damien Miller040f3832000-04-03 14:50:43 +1000391 if (devnull == -1) {
392 devnull = open("/dev/null", O_RDWR);
393 if (devnull == -1)
394 fatal("Couldn't open /dev/null: %s", strerror(errno));
395 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000396
Damien Miller040f3832000-04-03 14:50:43 +1000397 if (pipe(p) == -1)
398 fatal("Couldn't open pipe: %s", strerror(errno));
399
Damien Miller8d1fd572000-05-17 21:34:07 +1000400 (void)gettimeofday(&tv_start, NULL); /* record start time */
401
Damien Miller040f3832000-04-03 14:50:43 +1000402 switch (pid = fork()) {
403 case -1: /* Error */
404 close(p[0]);
405 close(p[1]);
406 fatal("Couldn't fork: %s", strerror(errno));
407 /* NOTREACHED */
408 case 0: /* Child */
Damien Miller4018c192000-04-30 09:30:44 +1000409 dup2(devnull, STDIN_FILENO);
410 dup2(p[1], STDOUT_FILENO);
411 dup2(p[1], STDERR_FILENO);
Damien Miller040f3832000-04-03 14:50:43 +1000412 close(p[0]);
413 close(p[1]);
414 close(devnull);
415
Damien Millerf9b625c2000-07-09 22:42:32 +1000416 setuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000417 execv(src->path, (char**)(src->args));
Damien Miller8d1fd572000-05-17 21:34:07 +1000418 debug("(child) Couldn't exec '%s': %s", src->cmdstring,
419 strerror(errno));
Damien Miller040f3832000-04-03 14:50:43 +1000420 _exit(-1);
421 default: /* Parent */
422 break;
423 }
424
425 RAND_add(&pid, sizeof(&pid), 0.0);
426
427 close(p[1]);
428
429 /* Hash output from child */
430 SHA1_Init(&sha);
431 total_bytes_read = 0;
Damien Miller4018c192000-04-30 09:30:44 +1000432
433 while (!error_abort && !cmd_eof) {
434 int ret;
435 struct timeval tv;
Damien Miller8d1fd572000-05-17 21:34:07 +1000436 int msec_remaining;
437
438 (void) gettimeofday(&tv_current, 0);
Damien Miller14c12cb2000-06-07 22:20:23 +1000439 msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current);
Damien Miller8d1fd572000-05-17 21:34:07 +1000440 if (msec_elapsed >= entropy_timeout_current) {
441 error_abort=1;
442 continue;
443 }
444 msec_remaining = entropy_timeout_current - msec_elapsed;
Damien Miller4018c192000-04-30 09:30:44 +1000445
446 FD_ZERO(&rdset);
447 FD_SET(p[0], &rdset);
Damien Miller8d1fd572000-05-17 21:34:07 +1000448 tv.tv_sec = msec_remaining / 1000;
449 tv.tv_usec = (msec_remaining % 1000) * 1000;
Damien Miller4018c192000-04-30 09:30:44 +1000450
451 ret = select(p[0]+1, &rdset, NULL, NULL, &tv);
Damien Miller8d1fd572000-05-17 21:34:07 +1000452
Damien Millerf9b625c2000-07-09 22:42:32 +1000453 RAND_add(&tv, sizeof(tv), 0.0);
454
Damien Miller4018c192000-04-30 09:30:44 +1000455 switch (ret) {
456 case 0:
457 /* timer expired */
458 error_abort = 1;
459 break;
Damien Miller4018c192000-04-30 09:30:44 +1000460 case 1:
461 /* command input */
462 bytes_read = read(p[0], buf, sizeof(buf));
Damien Millerf9b625c2000-07-09 22:42:32 +1000463 RAND_add(&bytes_read, sizeof(&bytes_read), 0.0);
Damien Miller4018c192000-04-30 09:30:44 +1000464 if (bytes_read == -1) {
465 error_abort = 1;
466 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000467 } else if (bytes_read) {
Damien Miller8d1fd572000-05-17 21:34:07 +1000468 SHA1_Update(&sha, buf, bytes_read);
469 total_bytes_read += bytes_read;
Damien Millerf9b625c2000-07-09 22:42:32 +1000470 } else {
Damien Miller8d1fd572000-05-17 21:34:07 +1000471 cmd_eof = 1;
Damien Millerf9b625c2000-07-09 22:42:32 +1000472 }
Damien Miller4018c192000-04-30 09:30:44 +1000473 break;
Damien Miller4018c192000-04-30 09:30:44 +1000474 case -1:
475 default:
Damien Millerf9b625c2000-07-09 22:42:32 +1000476 /* error */
Damien Miller8d1fd572000-05-17 21:34:07 +1000477 debug("Command '%s': select() failed: %s", src->cmdstring,
478 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000479 error_abort = 1;
480 break;
Damien Millerf9b625c2000-07-09 22:42:32 +1000481 }
482 }
Damien Miller4018c192000-04-30 09:30:44 +1000483
Damien Miller040f3832000-04-03 14:50:43 +1000484 SHA1_Final(hash, &sha);
485
486 close(p[0]);
Damien Miller8d1fd572000-05-17 21:34:07 +1000487
Damien Millercb5e44a2000-09-29 12:12:36 +1100488 debug3("Time elapsed: %d msec", msec_elapsed);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000489
Damien Miller040f3832000-04-03 14:50:43 +1000490 if (waitpid(pid, &status, 0) == -1) {
Damien Millercb5e44a2000-09-29 12:12:36 +1100491 error("Couldn't wait for child '%s' completion: %s", src->cmdstring,
Damien Miller8d1fd572000-05-17 21:34:07 +1000492 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000493 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000494 }
495
496 RAND_add(&status, sizeof(&status), 0.0);
497
Damien Miller4018c192000-04-30 09:30:44 +1000498 if (error_abort) {
499 /* closing p[0] on timeout causes the entropy command to
500 * SIGPIPE. Take whatever output we got, and mark this command
501 * as slow */
Damien Millercb5e44a2000-09-29 12:12:36 +1100502 debug2("Command '%s' timed out", src->cmdstring);
Damien Miller4018c192000-04-30 09:30:44 +1000503 src->sticky_badness *= 2;
504 src->badness = src->sticky_badness;
Damien Miller040f3832000-04-03 14:50:43 +1000505 return(total_bytes_read);
Damien Miller4018c192000-04-30 09:30:44 +1000506 }
507
508 if (WIFEXITED(status)) {
509 if (WEXITSTATUS(status)==0) {
510 return(total_bytes_read);
511 } else {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000512 debug2("Command '%s' exit status was %d", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000513 WEXITSTATUS(status));
Damien Miller4018c192000-04-30 09:30:44 +1000514 src->badness = src->sticky_badness = 128;
515 return (0.0);
516 }
517 } else if (WIFSIGNALED(status)) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000518 debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring,
Damien Miller14c12cb2000-06-07 22:20:23 +1000519 status);
Damien Miller4018c192000-04-30 09:30:44 +1000520 src->badness = src->sticky_badness = 128;
521 return(0.0);
522 } else
523 return(0.0);
Damien Miller040f3832000-04-03 14:50:43 +1000524}
Damien Miller4018c192000-04-30 09:30:44 +1000525
526/*
527 * prng seedfile functions
528 */
529int
530prng_check_seedfile(char *filename) {
531
532 struct stat st;
533
534 /* FIXME raceable: eg replace seed between this stat and subsequent open */
535 /* Not such a problem because we don't trust the seed file anyway */
536 if (lstat(filename, &st) == -1) {
Damien Miller52dc96b2000-10-16 20:13:43 +1100537 /* Give up on hard errors */
Damien Miller4018c192000-04-30 09:30:44 +1000538 if (errno != ENOENT)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000539 debug("WARNING: Couldn't stat random seed file \"%s\": %s",
Damien Miller52dc96b2000-10-16 20:13:43 +1100540 filename, strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000541
542 return(0);
543 }
544
545 /* regular file? */
546 if (!S_ISREG(st.st_mode))
547 fatal("PRNG seedfile %.100s is not a regular file", filename);
548
549 /* mode 0600, owned by root or the current user? */
Damien Miller52dc96b2000-10-16 20:13:43 +1100550 if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) {
551 debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d",
Damien Miller4018c192000-04-30 09:30:44 +1000552 filename, getuid());
Damien Miller52dc96b2000-10-16 20:13:43 +1100553 return(0);
554 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000555
Damien Miller4018c192000-04-30 09:30:44 +1000556 return(1);
557}
558
559void
560prng_write_seedfile(void) {
561 int fd;
562 char seed[1024];
563 char filename[1024];
564 struct passwd *pw;
565
566 /* Don't bother if we have already saved a seed */
567 if (prng_seed_saved)
568 return;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000569
Damien Millerf9b625c2000-07-09 22:42:32 +1000570 setuid(original_uid);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000571
Damien Millerfc0b11b2000-05-02 00:03:55 +1000572 prng_seed_saved = 1;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000573
Damien Millerf9b625c2000-07-09 22:42:32 +1000574 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000575 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000576 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000577 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000578
Damien Miller4018c192000-04-30 09:30:44 +1000579 /* Try to ensure that the parent directory is there */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000580 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Ben Lindstromcb577332001-01-22 21:06:19 +0000581 _PATH_SSH_USER_DIR);
Damien Miller4018c192000-04-30 09:30:44 +1000582 mkdir(filename, 0700);
583
Kevin Stevesef4eea92001-02-05 12:42:17 +0000584 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000585 SSH_PRNG_SEED_FILE);
586
587 debug("writing PRNG seed to file %.100s", filename);
588
589 RAND_bytes(seed, sizeof(seed));
590
591 /* Don't care if the seed doesn't exist */
592 prng_check_seedfile(filename);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000593
Damien Miller52dc96b2000-10-16 20:13:43 +1100594 if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000595 debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)",
Damien Miller52dc96b2000-10-16 20:13:43 +1100596 filename, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000597 } else {
Damien Miller52dc96b2000-10-16 20:13:43 +1100598 if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000599 fatal("problem writing PRNG seedfile %.100s (%.100s)", filename,
Damien Miller52dc96b2000-10-16 20:13:43 +1100600 strerror(errno));
Damien Miller4018c192000-04-30 09:30:44 +1000601
Damien Miller52dc96b2000-10-16 20:13:43 +1100602 close(fd);
603 }
Damien Miller4018c192000-04-30 09:30:44 +1000604}
605
606void
607prng_read_seedfile(void) {
608 int fd;
609 char seed[1024];
610 char filename[1024];
611 struct passwd *pw;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000612
Damien Millerf9b625c2000-07-09 22:42:32 +1000613 pw = getpwuid(original_uid);
Damien Miller4018c192000-04-30 09:30:44 +1000614 if (pw == NULL)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000615 fatal("Couldn't get password entry for current user (%i): %s",
Damien Millerf9b625c2000-07-09 22:42:32 +1000616 original_uid, strerror(errno));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000617
618 snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir,
Damien Miller4018c192000-04-30 09:30:44 +1000619 SSH_PRNG_SEED_FILE);
620
621 debug("loading PRNG seed from file %.100s", filename);
622
623 if (!prng_check_seedfile(filename)) {
Damien Miller21de4502001-01-17 09:37:15 +1100624 verbose("Random seed file not found or not valid, ignoring.");
Damien Miller4018c192000-04-30 09:30:44 +1000625 return;
626 }
627
628 /* open the file and read in the seed */
629 fd = open(filename, O_RDONLY);
630 if (fd == -1)
Kevin Stevesef4eea92001-02-05 12:42:17 +0000631 fatal("could not open PRNG seedfile %.100s (%.100s)", filename,
Damien Miller4018c192000-04-30 09:30:44 +1000632 strerror(errno));
633
634 if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) {
635 verbose("invalid or short read from PRNG seedfile %.100s - ignoring",
636 filename);
637 memset(seed, '\0', sizeof(seed));
638 }
639 close(fd);
640
641 /* stir in the seed, with estimated entropy zero */
642 RAND_add(&seed, sizeof(seed), 0.0);
643}
644
Damien Miller0437b332000-05-02 09:56:41 +1000645
646/*
647 * entropy command initialisation functions
648 */
Damien Miller0437b332000-05-02 09:56:41 +1000649int
650prng_read_commands(char *cmdfilename)
651{
652 FILE *f;
Damien Miller0437b332000-05-02 09:56:41 +1000653 char *cp;
Damien Miller14c12cb2000-06-07 22:20:23 +1000654 char line[1024];
655 char cmd[1024];
656 char path[256];
Damien Miller0437b332000-05-02 09:56:41 +1000657 int linenum;
Damien Miller0437b332000-05-02 09:56:41 +1000658 int num_cmds = 64;
659 int cur_cmd = 0;
Damien Miller14c12cb2000-06-07 22:20:23 +1000660 double est;
661 entropy_source_t *entcmd;
Damien Miller0437b332000-05-02 09:56:41 +1000662
663 f = fopen(cmdfilename, "r");
664 if (!f) {
665 fatal("couldn't read entropy commands file %.100s: %.100s",
666 cmdfilename, strerror(errno));
667 }
668
Damien Miller0437b332000-05-02 09:56:41 +1000669 entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t));
670 memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t));
671
Damien Miller14c12cb2000-06-07 22:20:23 +1000672 /* Read in file */
673 linenum = 0;
Damien Miller0437b332000-05-02 09:56:41 +1000674 while (fgets(line, sizeof(line), f)) {
Damien Miller14c12cb2000-06-07 22:20:23 +1000675 int arg;
676 char *argv;
677
Damien Miller0437b332000-05-02 09:56:41 +1000678 linenum++;
679
680 /* skip leading whitespace, test for blank line or comment */
681 cp = line + strspn(line, WHITESPACE);
682 if ((*cp == 0) || (*cp == '#'))
683 continue; /* done with this line */
684
Damien Miller14c12cb2000-06-07 22:20:23 +1000685 /* First non-whitespace char should be double quote delimiting */
686 /* commandline */
687 if (*cp != '"') {
Damien Miller0437b332000-05-02 09:56:41 +1000688 error("bad entropy command, %.100s line %d", cmdfilename,
689 linenum);
690 continue;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000691 }
Damien Miller14c12cb2000-06-07 22:20:23 +1000692
693 /* first token, command args (incl. argv[0]) in double quotes */
694 cp = strtok(cp, "\"");
695 if (cp == NULL) {
696 error("missing or bad command string, %.100s line %d -- ignored",
697 cmdfilename, linenum);
698 continue;
699 }
700 strlcpy(cmd, cp, sizeof(cmd));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000701
Damien Miller14c12cb2000-06-07 22:20:23 +1000702 /* second token, full command path */
703 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
704 error("missing command path, %.100s line %d -- ignored",
705 cmdfilename, linenum);
706 continue;
707 }
708
709 /* did configure mark this as dead? */
710 if (strncmp("undef", cp, 5) == 0)
711 continue;
712
Kevin Stevesef4eea92001-02-05 12:42:17 +0000713 strlcpy(path, cp, sizeof(path));
Damien Miller14c12cb2000-06-07 22:20:23 +1000714
715 /* third token, entropy rate estimate for this command */
716 if ((cp = strtok(NULL, WHITESPACE)) == NULL) {
717 error("missing entropy estimate, %.100s line %d -- ignored",
718 cmdfilename, linenum);
719 continue;
720 }
721 est = strtod(cp, &argv);
722
723 /* end of line */
724 if ((cp = strtok(NULL, WHITESPACE)) != NULL) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000725 error("garbage at end of line %d in %.100s -- ignored", linenum,
Damien Miller14c12cb2000-06-07 22:20:23 +1000726 cmdfilename);
727 continue;
728 }
729
730 /* save the command for debug messages */
731 entcmd[cur_cmd].cmdstring = xstrdup(cmd);
Kevin Stevesef4eea92001-02-05 12:42:17 +0000732
Damien Miller14c12cb2000-06-07 22:20:23 +1000733 /* split the command args */
734 cp = strtok(cmd, WHITESPACE);
735 arg = 0;
736 argv = NULL;
737 do {
738 char *s = (char*)xmalloc(strlen(cp) + 1);
739 strncpy(s, cp, strlen(cp) + 1);
740 entcmd[cur_cmd].args[arg] = s;
741 arg++;
742 } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE)));
Kevin Stevesef4eea92001-02-05 12:42:17 +0000743
Damien Miller14c12cb2000-06-07 22:20:23 +1000744 if (strtok(NULL, WHITESPACE))
745 error("ignored extra command elements (max 5), %.100s line %d",
746 cmdfilename, linenum);
747
748 /* Copy the command path and rate estimate */
749 entcmd[cur_cmd].path = xstrdup(path);
750 entcmd[cur_cmd].rate = est;
751
752 /* Initialise other values */
753 entcmd[cur_cmd].sticky_badness = 1;
754
755 cur_cmd++;
756
757 /* If we've filled the array, reallocate it twice the size */
758 /* Do this now because even if this we're on the last command,
759 we need another slot to mark the last entry */
760 if (cur_cmd == num_cmds) {
761 num_cmds *= 2;
762 entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t));
Damien Miller0437b332000-05-02 09:56:41 +1000763 }
764 }
765
766 /* zero the last entry */
767 memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t));
Damien Miller14c12cb2000-06-07 22:20:23 +1000768
Damien Miller0437b332000-05-02 09:56:41 +1000769 /* trim to size */
770 entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t));
771
Damien Millerf9b625c2000-07-09 22:42:32 +1000772 debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename);
Damien Miller0437b332000-05-02 09:56:41 +1000773
774 return (cur_cmd >= MIN_ENTROPY_SOURCES);
775}
776
Damien Miller040f3832000-04-03 14:50:43 +1000777/*
Damien Miller4018c192000-04-30 09:30:44 +1000778 * Write a keyfile at exit
Kevin Stevesef4eea92001-02-05 12:42:17 +0000779 */
Damien Miller4018c192000-04-30 09:30:44 +1000780void
781prng_seed_cleanup(void *junk)
782{
783 prng_write_seedfile();
784}
785
786/*
787 * Conditionally Seed OpenSSL's random number pool from
788 * syscalls and program output
Damien Miller040f3832000-04-03 14:50:43 +1000789 */
790void
791seed_rng(void)
792{
Damien Millerf3c6cf12000-05-17 22:08:29 +1000793 void *old_sigchld_handler;
Damien Miller0437b332000-05-02 09:56:41 +1000794
Damien Millerf9b625c2000-07-09 22:42:32 +1000795 if (!prng_initialised)
796 fatal("RNG not initialised");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000797
Damien Millerf3c6cf12000-05-17 22:08:29 +1000798 /* Make sure some other sigchld handler doesn't reap our entropy */
799 /* commands */
800 old_sigchld_handler = signal(SIGCHLD, SIG_DFL);
801
Damien Millerf9b625c2000-07-09 22:42:32 +1000802 debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs());
803 debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system());
804
805 if (!RAND_status())
806 fatal("Not enough entropy in RNG");
Damien Miller4018c192000-04-30 09:30:44 +1000807
Damien Millerf3c6cf12000-05-17 22:08:29 +1000808 signal(SIGCHLD, old_sigchld_handler);
809
Damien Miller8d1fd572000-05-17 21:34:07 +1000810 if (!RAND_status())
811 fatal("Couldn't initialise builtin random number generator -- exiting.");
Damien Miller040f3832000-04-03 14:50:43 +1000812}
Damien Millerf9b625c2000-07-09 22:42:32 +1000813
Kevin Stevesef4eea92001-02-05 12:42:17 +0000814void init_rng(void)
Damien Millerf9b625c2000-07-09 22:42:32 +1000815{
Damien Millerd592b632000-11-25 10:09:32 +1100816 int original_euid;
Kevin Stevesef4eea92001-02-05 12:42:17 +0000817
Damien Millerf9b625c2000-07-09 22:42:32 +1000818 original_uid = getuid();
Damien Millerd592b632000-11-25 10:09:32 +1100819 original_euid = geteuid();
Damien Millerf9b625c2000-07-09 22:42:32 +1000820
821 /* Read in collection commands */
822 if (!prng_read_commands(SSH_PRNG_COMMAND_FILE))
823 fatal("PRNG initialisation failed -- exiting.");
824
825 /* Set ourselves up to save a seed upon exit */
Kevin Stevesef4eea92001-02-05 12:42:17 +0000826 prng_seed_saved = 0;
Damien Millerd592b632000-11-25 10:09:32 +1100827
828 /* Give up privs while reading seed file */
829 if ((original_uid != original_euid) && (seteuid(original_uid) == -1))
830 fatal("Couldn't give up privileges");
Kevin Stevesef4eea92001-02-05 12:42:17 +0000831
Damien Millerf9b625c2000-07-09 22:42:32 +1000832 prng_read_seedfile();
Damien Millerd592b632000-11-25 10:09:32 +1100833
834 if ((original_uid != original_euid) && (seteuid(original_euid) == -1))
835 fatal("Couldn't restore privileges");
836
Damien Millerf9b625c2000-07-09 22:42:32 +1000837 fatal_add_cleanup(prng_seed_cleanup, NULL);
838 atexit(prng_write_seedfile);
839
840 prng_initialised = 1;
841}
842
Damien Miller040f3832000-04-03 14:50:43 +1000843#endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */