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