Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 1 | /* |
| 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 Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 12 | * |
| 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 Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 27 | #include <openssl/rand.h> |
| 28 | #include <openssl/sha.h> |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 29 | |
Damien Miller | ecbb26d | 2000-07-15 14:59:14 +1000 | [diff] [blame] | 30 | /* SunOS 4.4.4 needs this */ |
| 31 | #ifdef HAVE_FLOATINGPOINT_H |
| 32 | # include <floatingpoint.h> |
| 33 | #endif /* HAVE_FLOATINGPOINT_H */ |
| 34 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 35 | #include "ssh.h" |
| 36 | #include "xmalloc.h" |
| 37 | #include "atomicio.h" |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 38 | #include "pathnames.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 39 | #include "log.h" |
| 40 | |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 41 | RCSID("$Id: entropy.c,v 1.25 2001/01/22 21:06:20 mouring Exp $"); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 42 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 43 | #ifndef offsetof |
| 44 | # define offsetof(type, member) ((size_t) &((type *)0)->member) |
| 45 | #endif |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 46 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 47 | /* 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 Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 58 | #ifndef RUSAGE_SELF |
| 59 | # define RUSAGE_SELF 0 |
| 60 | #endif |
| 61 | #ifndef RUSAGE_CHILDREN |
| 62 | # define RUSAGE_CHILDREN 0 |
| 63 | #endif |
| 64 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 65 | #if defined(EGD_SOCKET) || defined(RANDOM_POOL) |
| 66 | |
| 67 | #ifdef EGD_SOCKET |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 68 | /* Collect entropy from EGD */ |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 69 | int get_random_bytes(unsigned char *buf, int len) |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 70 | { |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 71 | int fd; |
| 72 | char msg[2]; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 73 | struct sockaddr_un addr; |
| 74 | int addr_len; |
| 75 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 76 | /* Sanity checks */ |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 77 | if (sizeof(EGD_SOCKET) > sizeof(addr.sun_path)) |
| 78 | fatal("Random pool path is too long"); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 79 | if (len > 255) |
| 80 | fatal("Too many bytes to read from EGD"); |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 81 | |
| 82 | memset(&addr, '\0', sizeof(addr)); |
| 83 | addr.sun_family = AF_UNIX; |
| 84 | strlcpy(addr.sun_path, EGD_SOCKET, sizeof(addr.sun_path)); |
| 85 | addr_len = offsetof(struct sockaddr_un, sun_path) + sizeof(EGD_SOCKET); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 86 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 87 | fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 88 | if (fd == -1) { |
| 89 | error("Couldn't create AF_UNIX socket: %s", strerror(errno)); |
| 90 | return(0); |
| 91 | } |
| 92 | |
| 93 | if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) { |
| 94 | error("Couldn't connect to EGD socket \"%s\": %s", |
| 95 | addr.sun_path, strerror(errno)); |
| 96 | close(fd); |
| 97 | return(0); |
| 98 | } |
| 99 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 100 | /* Send blocking read request to EGD */ |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 101 | msg[0] = 0x02; |
| 102 | msg[1] = len; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 103 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 104 | if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) { |
| 105 | error("Couldn't write to EGD socket \"%s\": %s", |
| 106 | EGD_SOCKET, strerror(errno)); |
| 107 | close(fd); |
| 108 | return(0); |
| 109 | } |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 110 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 111 | if (atomicio(read, fd, buf, len) != len) { |
| 112 | error("Couldn't read from EGD socket \"%s\": %s", |
| 113 | EGD_SOCKET, strerror(errno)); |
| 114 | close(fd); |
| 115 | return(0); |
| 116 | } |
| 117 | |
| 118 | close(fd); |
| 119 | |
| 120 | return(1); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 121 | } |
| 122 | #else /* !EGD_SOCKET */ |
| 123 | #ifdef RANDOM_POOL |
| 124 | /* Collect entropy from /dev/urandom or pipe */ |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 125 | int get_random_bytes(unsigned char *buf, int len) |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 126 | { |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 127 | int random_pool; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 128 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 129 | random_pool = open(RANDOM_POOL, O_RDONLY); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 130 | if (random_pool == -1) { |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 131 | error("Couldn't open random pool \"%s\": %s", |
| 132 | RANDOM_POOL, strerror(errno)); |
| 133 | return(0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 134 | } |
| 135 | |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 136 | if (atomicio(read, random_pool, buf, len) != len) { |
| 137 | error("Couldn't read from random pool \"%s\": %s", |
| 138 | RANDOM_POOL, strerror(errno)); |
| 139 | close(random_pool); |
| 140 | return(0); |
| 141 | } |
| 142 | |
| 143 | close(random_pool); |
| 144 | |
| 145 | return(1); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 146 | } |
| 147 | #endif /* RANDOM_POOL */ |
| 148 | #endif /* EGD_SOCKET */ |
| 149 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 150 | /* |
| 151 | * Seed OpenSSL's random number pool from Kernel random number generator |
| 152 | * or EGD |
| 153 | */ |
| 154 | void |
| 155 | seed_rng(void) |
| 156 | { |
| 157 | char buf[32]; |
| 158 | |
| 159 | debug("Seeding random number generator"); |
Damien Miller | 6468125 | 2000-06-26 13:01:33 +1000 | [diff] [blame] | 160 | |
Damien Miller | 0800647 | 2000-06-26 13:55:31 +1000 | [diff] [blame] | 161 | if (!get_random_bytes(buf, sizeof(buf))) { |
| 162 | if (!RAND_status()) |
| 163 | fatal("Entropy collection failed and entropy exhausted"); |
| 164 | } else { |
| 165 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
| 166 | } |
| 167 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 168 | memset(buf, '\0', sizeof(buf)); |
| 169 | } |
| 170 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 171 | /* No-op */ |
| 172 | void init_rng(void) {} |
| 173 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 174 | #else /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */ |
| 175 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 176 | /* |
| 177 | * FIXME: proper entropy estimations. All current values are guesses |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 178 | * FIXME: (ATL) do estimates at compile time? |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 179 | * FIXME: More entropy sources |
| 180 | */ |
| 181 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 182 | /* slow command timeouts (all in milliseconds) */ |
| 183 | /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */ |
| 184 | static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC; |
| 185 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 186 | static int prng_seed_saved = 0; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 187 | static int prng_initialised = 0; |
| 188 | uid_t original_uid; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 189 | |
| 190 | typedef struct |
| 191 | { |
| 192 | /* Proportion of data that is entropy */ |
| 193 | double rate; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 194 | /* Counter goes positive if this command times out */ |
| 195 | unsigned int badness; |
| 196 | /* Increases by factor of two each timeout */ |
| 197 | unsigned int sticky_badness; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 198 | /* Path to executable */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 199 | char *path; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 200 | /* argv to pass to executable */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 201 | char *args[5]; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 202 | /* full command string (debug) */ |
| 203 | char *cmdstring; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 204 | } entropy_source_t; |
| 205 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 206 | double stir_from_system(void); |
| 207 | double stir_from_programs(void); |
| 208 | double stir_gettimeofday(double entropy_estimate); |
| 209 | double stir_clock(double entropy_estimate); |
| 210 | double stir_rusage(int who, double entropy_estimate); |
| 211 | double hash_output_from_command(entropy_source_t *src, char *hash); |
| 212 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 213 | /* this is initialised from a file, by prng_read_commands() */ |
| 214 | entropy_source_t *entropy_sources = NULL; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 215 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 216 | double |
| 217 | stir_from_system(void) |
| 218 | { |
| 219 | double total_entropy_estimate; |
| 220 | long int i; |
| 221 | |
| 222 | total_entropy_estimate = 0; |
| 223 | |
| 224 | i = getpid(); |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 225 | RAND_add(&i, sizeof(i), 0.5); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 226 | total_entropy_estimate += 0.1; |
| 227 | |
| 228 | i = getppid(); |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 229 | RAND_add(&i, sizeof(i), 0.5); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 230 | total_entropy_estimate += 0.1; |
| 231 | |
| 232 | i = getuid(); |
| 233 | RAND_add(&i, sizeof(i), 0.0); |
| 234 | i = getgid(); |
| 235 | RAND_add(&i, sizeof(i), 0.0); |
| 236 | |
| 237 | total_entropy_estimate += stir_gettimeofday(1.0); |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 238 | total_entropy_estimate += stir_clock(0.5); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 239 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0); |
| 240 | |
| 241 | return(total_entropy_estimate); |
| 242 | } |
| 243 | |
| 244 | double |
| 245 | stir_from_programs(void) |
| 246 | { |
| 247 | int i; |
| 248 | int c; |
| 249 | double entropy_estimate; |
| 250 | double total_entropy_estimate; |
| 251 | char hash[SHA_DIGEST_LENGTH]; |
| 252 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 253 | total_entropy_estimate = 0; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 254 | for(i = 0; i < NUM_ENTROPY_RUNS; i++) { |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 255 | c = 0; |
| 256 | while (entropy_sources[c].path != NULL) { |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 257 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 258 | if (!entropy_sources[c].badness) { |
| 259 | /* Hash output from command */ |
| 260 | entropy_estimate = hash_output_from_command(&entropy_sources[c], hash); |
| 261 | |
| 262 | /* Scale back entropy estimate according to command's rate */ |
| 263 | entropy_estimate *= entropy_sources[c].rate; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 264 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 265 | /* Upper bound of entropy estimate is SHA_DIGEST_LENGTH */ |
| 266 | if (entropy_estimate > SHA_DIGEST_LENGTH) |
| 267 | entropy_estimate = SHA_DIGEST_LENGTH; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 268 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 269 | /* Scale back estimates for subsequent passes through list */ |
| 270 | entropy_estimate /= SCALE_PER_RUN * (i + 1.0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 271 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 272 | /* Stir it in */ |
| 273 | RAND_add(hash, sizeof(hash), entropy_estimate); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 274 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 275 | debug3("Got %0.2f bytes of entropy from '%s'", entropy_estimate, |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 276 | entropy_sources[c].cmdstring); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 277 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 278 | total_entropy_estimate += entropy_estimate; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 279 | |
| 280 | /* Execution times should be a little unpredictable */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 281 | total_entropy_estimate += stir_gettimeofday(0.05); |
| 282 | total_entropy_estimate += stir_clock(0.05); |
| 283 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 0.1); |
| 284 | total_entropy_estimate += stir_rusage(RUSAGE_CHILDREN, 0.1); |
| 285 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 286 | debug2("Command '%s' disabled (badness %d)", |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 287 | entropy_sources[c].cmdstring, entropy_sources[c].badness); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 288 | |
| 289 | if (entropy_sources[c].badness > 0) |
| 290 | entropy_sources[c].badness--; |
| 291 | } |
| 292 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 293 | c++; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return(total_entropy_estimate); |
| 298 | } |
| 299 | |
| 300 | double |
| 301 | stir_gettimeofday(double entropy_estimate) |
| 302 | { |
| 303 | struct timeval tv; |
| 304 | |
| 305 | if (gettimeofday(&tv, NULL) == -1) |
| 306 | fatal("Couldn't gettimeofday: %s", strerror(errno)); |
| 307 | |
| 308 | RAND_add(&tv, sizeof(tv), entropy_estimate); |
| 309 | |
| 310 | return(entropy_estimate); |
| 311 | } |
| 312 | |
| 313 | double |
| 314 | stir_clock(double entropy_estimate) |
| 315 | { |
| 316 | #ifdef HAVE_CLOCK |
| 317 | clock_t c; |
| 318 | |
| 319 | c = clock(); |
| 320 | RAND_add(&c, sizeof(c), entropy_estimate); |
| 321 | |
| 322 | return(entropy_estimate); |
| 323 | #else /* _HAVE_CLOCK */ |
| 324 | return(0); |
| 325 | #endif /* _HAVE_CLOCK */ |
| 326 | } |
| 327 | |
| 328 | double |
| 329 | stir_rusage(int who, double entropy_estimate) |
| 330 | { |
| 331 | #ifdef HAVE_GETRUSAGE |
| 332 | struct rusage ru; |
| 333 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 334 | if (getrusage(who, &ru) == -1) |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 335 | return(0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 336 | |
Damien Miller | 7b22d65 | 2000-06-18 14:07:04 +1000 | [diff] [blame] | 337 | RAND_add(&ru, sizeof(ru), entropy_estimate); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 338 | |
| 339 | return(entropy_estimate); |
| 340 | #else /* _HAVE_GETRUSAGE */ |
| 341 | return(0); |
| 342 | #endif /* _HAVE_GETRUSAGE */ |
| 343 | } |
| 344 | |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 345 | |
| 346 | static |
| 347 | int |
| 348 | _get_timeval_msec_difference(struct timeval *t1, struct timeval *t2) { |
| 349 | int secdiff, usecdiff; |
| 350 | |
| 351 | secdiff = t2->tv_sec - t1->tv_sec; |
| 352 | usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec); |
| 353 | return (int)(usecdiff / 1000); |
| 354 | } |
| 355 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 356 | double |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 357 | hash_output_from_command(entropy_source_t *src, char *hash) |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 358 | { |
| 359 | static int devnull = -1; |
| 360 | int p[2]; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 361 | fd_set rdset; |
| 362 | int cmd_eof = 0, error_abort = 0; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 363 | struct timeval tv_start, tv_current; |
| 364 | int msec_elapsed = 0; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 365 | pid_t pid; |
| 366 | int status; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 367 | char buf[16384]; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 368 | int bytes_read; |
| 369 | int total_bytes_read; |
| 370 | SHA_CTX sha; |
| 371 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 372 | debug3("Reading output from \'%s\'", src->cmdstring); |
| 373 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 374 | if (devnull == -1) { |
| 375 | devnull = open("/dev/null", O_RDWR); |
| 376 | if (devnull == -1) |
| 377 | fatal("Couldn't open /dev/null: %s", strerror(errno)); |
| 378 | } |
| 379 | |
| 380 | if (pipe(p) == -1) |
| 381 | fatal("Couldn't open pipe: %s", strerror(errno)); |
| 382 | |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 383 | (void)gettimeofday(&tv_start, NULL); /* record start time */ |
| 384 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 385 | switch (pid = fork()) { |
| 386 | case -1: /* Error */ |
| 387 | close(p[0]); |
| 388 | close(p[1]); |
| 389 | fatal("Couldn't fork: %s", strerror(errno)); |
| 390 | /* NOTREACHED */ |
| 391 | case 0: /* Child */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 392 | dup2(devnull, STDIN_FILENO); |
| 393 | dup2(p[1], STDOUT_FILENO); |
| 394 | dup2(p[1], STDERR_FILENO); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 395 | close(p[0]); |
| 396 | close(p[1]); |
| 397 | close(devnull); |
| 398 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 399 | setuid(original_uid); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 400 | execv(src->path, (char**)(src->args)); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 401 | debug("(child) Couldn't exec '%s': %s", src->cmdstring, |
| 402 | strerror(errno)); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 403 | _exit(-1); |
| 404 | default: /* Parent */ |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | RAND_add(&pid, sizeof(&pid), 0.0); |
| 409 | |
| 410 | close(p[1]); |
| 411 | |
| 412 | /* Hash output from child */ |
| 413 | SHA1_Init(&sha); |
| 414 | total_bytes_read = 0; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 415 | |
| 416 | while (!error_abort && !cmd_eof) { |
| 417 | int ret; |
| 418 | struct timeval tv; |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 419 | int msec_remaining; |
| 420 | |
| 421 | (void) gettimeofday(&tv_current, 0); |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 422 | msec_elapsed = _get_timeval_msec_difference(&tv_start, &tv_current); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 423 | if (msec_elapsed >= entropy_timeout_current) { |
| 424 | error_abort=1; |
| 425 | continue; |
| 426 | } |
| 427 | msec_remaining = entropy_timeout_current - msec_elapsed; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 428 | |
| 429 | FD_ZERO(&rdset); |
| 430 | FD_SET(p[0], &rdset); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 431 | tv.tv_sec = msec_remaining / 1000; |
| 432 | tv.tv_usec = (msec_remaining % 1000) * 1000; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 433 | |
| 434 | ret = select(p[0]+1, &rdset, NULL, NULL, &tv); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 435 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 436 | RAND_add(&tv, sizeof(tv), 0.0); |
| 437 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 438 | switch (ret) { |
| 439 | case 0: |
| 440 | /* timer expired */ |
| 441 | error_abort = 1; |
| 442 | break; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 443 | case 1: |
| 444 | /* command input */ |
| 445 | bytes_read = read(p[0], buf, sizeof(buf)); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 446 | RAND_add(&bytes_read, sizeof(&bytes_read), 0.0); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 447 | if (bytes_read == -1) { |
| 448 | error_abort = 1; |
| 449 | break; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 450 | } else if (bytes_read) { |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 451 | SHA1_Update(&sha, buf, bytes_read); |
| 452 | total_bytes_read += bytes_read; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 453 | } else { |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 454 | cmd_eof = 1; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 455 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 456 | break; |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 457 | case -1: |
| 458 | default: |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 459 | /* error */ |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 460 | debug("Command '%s': select() failed: %s", src->cmdstring, |
| 461 | strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 462 | error_abort = 1; |
| 463 | break; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 464 | } |
| 465 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 466 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 467 | SHA1_Final(hash, &sha); |
| 468 | |
| 469 | close(p[0]); |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 470 | |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 471 | debug3("Time elapsed: %d msec", msec_elapsed); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 472 | |
| 473 | if (waitpid(pid, &status, 0) == -1) { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 474 | error("Couldn't wait for child '%s' completion: %s", src->cmdstring, |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 475 | strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 476 | return(0.0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | RAND_add(&status, sizeof(&status), 0.0); |
| 480 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 481 | if (error_abort) { |
| 482 | /* closing p[0] on timeout causes the entropy command to |
| 483 | * SIGPIPE. Take whatever output we got, and mark this command |
| 484 | * as slow */ |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 485 | debug2("Command '%s' timed out", src->cmdstring); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 486 | src->sticky_badness *= 2; |
| 487 | src->badness = src->sticky_badness; |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 488 | return(total_bytes_read); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | if (WIFEXITED(status)) { |
| 492 | if (WEXITSTATUS(status)==0) { |
| 493 | return(total_bytes_read); |
| 494 | } else { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 495 | debug2("Command '%s' exit status was %d", src->cmdstring, |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 496 | WEXITSTATUS(status)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 497 | src->badness = src->sticky_badness = 128; |
| 498 | return (0.0); |
| 499 | } |
| 500 | } else if (WIFSIGNALED(status)) { |
Damien Miller | cb5e44a | 2000-09-29 12:12:36 +1100 | [diff] [blame] | 501 | debug2("Command '%s' returned on uncaught signal %d !", src->cmdstring, |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 502 | status); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 503 | src->badness = src->sticky_badness = 128; |
| 504 | return(0.0); |
| 505 | } else |
| 506 | return(0.0); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 507 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * prng seedfile functions |
| 511 | */ |
| 512 | int |
| 513 | prng_check_seedfile(char *filename) { |
| 514 | |
| 515 | struct stat st; |
| 516 | |
| 517 | /* FIXME raceable: eg replace seed between this stat and subsequent open */ |
| 518 | /* Not such a problem because we don't trust the seed file anyway */ |
| 519 | if (lstat(filename, &st) == -1) { |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 520 | /* Give up on hard errors */ |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 521 | if (errno != ENOENT) |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 522 | debug("WARNING: Couldn't stat random seed file \"%s\": %s", |
| 523 | filename, strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 524 | |
| 525 | return(0); |
| 526 | } |
| 527 | |
| 528 | /* regular file? */ |
| 529 | if (!S_ISREG(st.st_mode)) |
| 530 | fatal("PRNG seedfile %.100s is not a regular file", filename); |
| 531 | |
| 532 | /* mode 0600, owned by root or the current user? */ |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 533 | if (((st.st_mode & 0177) != 0) || !(st.st_uid == original_uid)) { |
| 534 | debug("WARNING: PRNG seedfile %.100s must be mode 0600, owned by uid %d", |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 535 | filename, getuid()); |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 536 | return(0); |
| 537 | } |
| 538 | |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 539 | return(1); |
| 540 | } |
| 541 | |
| 542 | void |
| 543 | prng_write_seedfile(void) { |
| 544 | int fd; |
| 545 | char seed[1024]; |
| 546 | char filename[1024]; |
| 547 | struct passwd *pw; |
| 548 | |
| 549 | /* Don't bother if we have already saved a seed */ |
| 550 | if (prng_seed_saved) |
| 551 | return; |
| 552 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 553 | setuid(original_uid); |
| 554 | |
Damien Miller | fc0b11b | 2000-05-02 00:03:55 +1000 | [diff] [blame] | 555 | prng_seed_saved = 1; |
| 556 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 557 | pw = getpwuid(original_uid); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 558 | if (pw == NULL) |
| 559 | fatal("Couldn't get password entry for current user (%i): %s", |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 560 | original_uid, strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 561 | |
| 562 | /* Try to ensure that the parent directory is there */ |
| 563 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 564 | _PATH_SSH_USER_DIR); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 565 | mkdir(filename, 0700); |
| 566 | |
| 567 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
| 568 | SSH_PRNG_SEED_FILE); |
| 569 | |
| 570 | debug("writing PRNG seed to file %.100s", filename); |
| 571 | |
| 572 | RAND_bytes(seed, sizeof(seed)); |
| 573 | |
| 574 | /* Don't care if the seed doesn't exist */ |
| 575 | prng_check_seedfile(filename); |
| 576 | |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 577 | if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) { |
| 578 | debug("WARNING: couldn't access PRNG seedfile %.100s (%.100s)", |
| 579 | filename, strerror(errno)); |
| 580 | } else { |
| 581 | if (atomicio(write, fd, &seed, sizeof(seed)) != sizeof(seed)) |
| 582 | fatal("problem writing PRNG seedfile %.100s (%.100s)", filename, |
| 583 | strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 584 | |
Damien Miller | 52dc96b | 2000-10-16 20:13:43 +1100 | [diff] [blame] | 585 | close(fd); |
| 586 | } |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | void |
| 590 | prng_read_seedfile(void) { |
| 591 | int fd; |
| 592 | char seed[1024]; |
| 593 | char filename[1024]; |
| 594 | struct passwd *pw; |
| 595 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 596 | pw = getpwuid(original_uid); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 597 | if (pw == NULL) |
| 598 | fatal("Couldn't get password entry for current user (%i): %s", |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 599 | original_uid, strerror(errno)); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 600 | |
| 601 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, |
| 602 | SSH_PRNG_SEED_FILE); |
| 603 | |
| 604 | debug("loading PRNG seed from file %.100s", filename); |
| 605 | |
| 606 | if (!prng_check_seedfile(filename)) { |
Damien Miller | 21de450 | 2001-01-17 09:37:15 +1100 | [diff] [blame] | 607 | verbose("Random seed file not found or not valid, ignoring."); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 608 | return; |
| 609 | } |
| 610 | |
| 611 | /* open the file and read in the seed */ |
| 612 | fd = open(filename, O_RDONLY); |
| 613 | if (fd == -1) |
| 614 | fatal("could not open PRNG seedfile %.100s (%.100s)", filename, |
| 615 | strerror(errno)); |
| 616 | |
| 617 | if (atomicio(read, fd, &seed, sizeof(seed)) != sizeof(seed)) { |
| 618 | verbose("invalid or short read from PRNG seedfile %.100s - ignoring", |
| 619 | filename); |
| 620 | memset(seed, '\0', sizeof(seed)); |
| 621 | } |
| 622 | close(fd); |
| 623 | |
| 624 | /* stir in the seed, with estimated entropy zero */ |
| 625 | RAND_add(&seed, sizeof(seed), 0.0); |
| 626 | } |
| 627 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * entropy command initialisation functions |
| 631 | */ |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 632 | int |
| 633 | prng_read_commands(char *cmdfilename) |
| 634 | { |
| 635 | FILE *f; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 636 | char *cp; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 637 | char line[1024]; |
| 638 | char cmd[1024]; |
| 639 | char path[256]; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 640 | int linenum; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 641 | int num_cmds = 64; |
| 642 | int cur_cmd = 0; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 643 | double est; |
| 644 | entropy_source_t *entcmd; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 645 | |
| 646 | f = fopen(cmdfilename, "r"); |
| 647 | if (!f) { |
| 648 | fatal("couldn't read entropy commands file %.100s: %.100s", |
| 649 | cmdfilename, strerror(errno)); |
| 650 | } |
| 651 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 652 | entcmd = (entropy_source_t *)xmalloc(num_cmds * sizeof(entropy_source_t)); |
| 653 | memset(entcmd, '\0', num_cmds * sizeof(entropy_source_t)); |
| 654 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 655 | /* Read in file */ |
| 656 | linenum = 0; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 657 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 658 | int arg; |
| 659 | char *argv; |
| 660 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 661 | linenum++; |
| 662 | |
| 663 | /* skip leading whitespace, test for blank line or comment */ |
| 664 | cp = line + strspn(line, WHITESPACE); |
| 665 | if ((*cp == 0) || (*cp == '#')) |
| 666 | continue; /* done with this line */ |
| 667 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 668 | /* First non-whitespace char should be double quote delimiting */ |
| 669 | /* commandline */ |
| 670 | if (*cp != '"') { |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 671 | error("bad entropy command, %.100s line %d", cmdfilename, |
| 672 | linenum); |
| 673 | continue; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* first token, command args (incl. argv[0]) in double quotes */ |
| 677 | cp = strtok(cp, "\""); |
| 678 | if (cp == NULL) { |
| 679 | error("missing or bad command string, %.100s line %d -- ignored", |
| 680 | cmdfilename, linenum); |
| 681 | continue; |
| 682 | } |
| 683 | strlcpy(cmd, cp, sizeof(cmd)); |
| 684 | |
| 685 | /* second token, full command path */ |
| 686 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { |
| 687 | error("missing command path, %.100s line %d -- ignored", |
| 688 | cmdfilename, linenum); |
| 689 | continue; |
| 690 | } |
| 691 | |
| 692 | /* did configure mark this as dead? */ |
| 693 | if (strncmp("undef", cp, 5) == 0) |
| 694 | continue; |
| 695 | |
| 696 | strlcpy(path, cp, sizeof(path)); |
| 697 | |
| 698 | /* third token, entropy rate estimate for this command */ |
| 699 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { |
| 700 | error("missing entropy estimate, %.100s line %d -- ignored", |
| 701 | cmdfilename, linenum); |
| 702 | continue; |
| 703 | } |
| 704 | est = strtod(cp, &argv); |
| 705 | |
| 706 | /* end of line */ |
| 707 | if ((cp = strtok(NULL, WHITESPACE)) != NULL) { |
| 708 | error("garbage at end of line %d in %.100s -- ignored", linenum, |
| 709 | cmdfilename); |
| 710 | continue; |
| 711 | } |
| 712 | |
| 713 | /* save the command for debug messages */ |
| 714 | entcmd[cur_cmd].cmdstring = xstrdup(cmd); |
| 715 | |
| 716 | /* split the command args */ |
| 717 | cp = strtok(cmd, WHITESPACE); |
| 718 | arg = 0; |
| 719 | argv = NULL; |
| 720 | do { |
| 721 | char *s = (char*)xmalloc(strlen(cp) + 1); |
| 722 | strncpy(s, cp, strlen(cp) + 1); |
| 723 | entcmd[cur_cmd].args[arg] = s; |
| 724 | arg++; |
| 725 | } while ((arg < 5) && (cp = strtok(NULL, WHITESPACE))); |
| 726 | |
| 727 | if (strtok(NULL, WHITESPACE)) |
| 728 | error("ignored extra command elements (max 5), %.100s line %d", |
| 729 | cmdfilename, linenum); |
| 730 | |
| 731 | /* Copy the command path and rate estimate */ |
| 732 | entcmd[cur_cmd].path = xstrdup(path); |
| 733 | entcmd[cur_cmd].rate = est; |
| 734 | |
| 735 | /* Initialise other values */ |
| 736 | entcmd[cur_cmd].sticky_badness = 1; |
| 737 | |
| 738 | cur_cmd++; |
| 739 | |
| 740 | /* If we've filled the array, reallocate it twice the size */ |
| 741 | /* Do this now because even if this we're on the last command, |
| 742 | we need another slot to mark the last entry */ |
| 743 | if (cur_cmd == num_cmds) { |
| 744 | num_cmds *= 2; |
| 745 | entcmd = xrealloc(entcmd, num_cmds * sizeof(entropy_source_t)); |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | |
| 749 | /* zero the last entry */ |
| 750 | memset(&entcmd[cur_cmd], '\0', sizeof(entropy_source_t)); |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 751 | |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 752 | /* trim to size */ |
| 753 | entropy_sources = xrealloc(entcmd, (cur_cmd+1) * sizeof(entropy_source_t)); |
| 754 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 755 | debug("Loaded %d entropy commands from %.100s", cur_cmd, cmdfilename); |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 756 | |
| 757 | return (cur_cmd >= MIN_ENTROPY_SOURCES); |
| 758 | } |
| 759 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 760 | /* |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 761 | * Write a keyfile at exit |
| 762 | */ |
| 763 | void |
| 764 | prng_seed_cleanup(void *junk) |
| 765 | { |
| 766 | prng_write_seedfile(); |
| 767 | } |
| 768 | |
| 769 | /* |
| 770 | * Conditionally Seed OpenSSL's random number pool from |
| 771 | * syscalls and program output |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 772 | */ |
| 773 | void |
| 774 | seed_rng(void) |
| 775 | { |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 776 | void *old_sigchld_handler; |
Damien Miller | 0437b33 | 2000-05-02 09:56:41 +1000 | [diff] [blame] | 777 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 778 | if (!prng_initialised) |
| 779 | fatal("RNG not initialised"); |
| 780 | |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 781 | /* Make sure some other sigchld handler doesn't reap our entropy */ |
| 782 | /* commands */ |
| 783 | old_sigchld_handler = signal(SIGCHLD, SIG_DFL); |
| 784 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 785 | debug("Seeded RNG with %i bytes from programs", (int)stir_from_programs()); |
| 786 | debug("Seeded RNG with %i bytes from system calls", (int)stir_from_system()); |
| 787 | |
| 788 | if (!RAND_status()) |
| 789 | fatal("Not enough entropy in RNG"); |
Damien Miller | 4018c19 | 2000-04-30 09:30:44 +1000 | [diff] [blame] | 790 | |
Damien Miller | f3c6cf1 | 2000-05-17 22:08:29 +1000 | [diff] [blame] | 791 | signal(SIGCHLD, old_sigchld_handler); |
| 792 | |
Damien Miller | 8d1fd57 | 2000-05-17 21:34:07 +1000 | [diff] [blame] | 793 | if (!RAND_status()) |
| 794 | fatal("Couldn't initialise builtin random number generator -- exiting."); |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 795 | } |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 796 | |
| 797 | void init_rng(void) |
| 798 | { |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 799 | int original_euid; |
| 800 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 801 | original_uid = getuid(); |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 802 | original_euid = geteuid(); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 803 | |
| 804 | /* Read in collection commands */ |
| 805 | if (!prng_read_commands(SSH_PRNG_COMMAND_FILE)) |
| 806 | fatal("PRNG initialisation failed -- exiting."); |
| 807 | |
| 808 | /* Set ourselves up to save a seed upon exit */ |
| 809 | prng_seed_saved = 0; |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 810 | |
| 811 | /* Give up privs while reading seed file */ |
| 812 | if ((original_uid != original_euid) && (seteuid(original_uid) == -1)) |
| 813 | fatal("Couldn't give up privileges"); |
| 814 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 815 | prng_read_seedfile(); |
Damien Miller | d592b63 | 2000-11-25 10:09:32 +1100 | [diff] [blame] | 816 | |
| 817 | if ((original_uid != original_euid) && (seteuid(original_euid) == -1)) |
| 818 | fatal("Couldn't restore privileges"); |
| 819 | |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 820 | fatal_add_cleanup(prng_seed_cleanup, NULL); |
| 821 | atexit(prng_write_seedfile); |
| 822 | |
| 823 | prng_initialised = 1; |
| 824 | } |
| 825 | |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 826 | #endif /* defined(EGD_SOCKET) || defined(RANDOM_POOL) */ |