]>
Commit | Line | Data |
---|---|---|
e9a17296 | 1 | /* |
2 | * Copyright (c) 2001-2002 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 | * | |
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 | ||
27 | #include <openssl/rand.h> | |
28 | #include <openssl/sha.h> | |
29 | #include <openssl/crypto.h> | |
30 | ||
31 | /* SunOS 4.4.4 needs this */ | |
32 | #ifdef HAVE_FLOATINGPOINT_H | |
33 | # include <floatingpoint.h> | |
34 | #endif /* HAVE_FLOATINGPOINT_H */ | |
35 | ||
36 | #include "misc.h" | |
37 | #include "xmalloc.h" | |
38 | #include "atomicio.h" | |
39 | #include "pathnames.h" | |
40 | #include "log.h" | |
41 | ||
42 | RCSID("$Id$"); | |
43 | ||
44 | /* Number of bytes we write out */ | |
45 | #define OUTPUT_SEED_SIZE 48 | |
46 | ||
47 | /* Length of on-disk seedfiles */ | |
48 | #define SEED_FILE_SIZE 1024 | |
49 | ||
50 | /* Maximum number of command-line arguments to read from file */ | |
51 | #define NUM_ARGS 10 | |
52 | ||
53 | /* Minimum number of usable commands to be considered sufficient */ | |
54 | #define MIN_ENTROPY_SOURCES 16 | |
55 | ||
56 | /* Path to on-disk seed file (relative to user's home directory */ | |
57 | #ifndef SSH_PRNG_SEED_FILE | |
58 | # define SSH_PRNG_SEED_FILE _PATH_SSH_USER_DIR"/prng_seed" | |
59 | #endif | |
60 | ||
ae43c103 | 61 | /* Path to PRNG commands list (from pathnames.c) */ |
62 | extern char *SSH_PRNG_COMMAND_FILE; | |
e9a17296 | 63 | |
e9a17296 | 64 | extern char *__progname; |
e9a17296 | 65 | |
66 | #define WHITESPACE " \t\n" | |
67 | ||
68 | #ifndef RUSAGE_SELF | |
69 | # define RUSAGE_SELF 0 | |
70 | #endif | |
71 | #ifndef RUSAGE_CHILDREN | |
72 | # define RUSAGE_CHILDREN 0 | |
73 | #endif | |
74 | ||
75 | #if !defined(PRNGD_SOCKET) && !defined(PRNGD_PORT) | |
76 | # define USE_SEED_FILES | |
77 | #endif | |
78 | ||
79 | typedef struct { | |
80 | /* Proportion of data that is entropy */ | |
81 | double rate; | |
82 | /* Counter goes positive if this command times out */ | |
83 | unsigned int badness; | |
84 | /* Increases by factor of two each timeout */ | |
85 | unsigned int sticky_badness; | |
86 | /* Path to executable */ | |
87 | char *path; | |
88 | /* argv to pass to executable */ | |
89 | char *args[NUM_ARGS]; /* XXX: arbitrary limit */ | |
90 | /* full command string (debug) */ | |
91 | char *cmdstring; | |
92 | } entropy_cmd_t; | |
93 | ||
94 | /* slow command timeouts (all in milliseconds) */ | |
95 | /* static int entropy_timeout_default = ENTROPY_TIMEOUT_MSEC; */ | |
96 | static int entropy_timeout_current = ENTROPY_TIMEOUT_MSEC; | |
97 | ||
98 | /* this is initialised from a file, by prng_read_commands() */ | |
99 | static entropy_cmd_t *entropy_cmds = NULL; | |
100 | ||
101 | /* Prototypes */ | |
102 | double stir_from_system(void); | |
103 | double stir_from_programs(void); | |
104 | double stir_gettimeofday(double entropy_estimate); | |
105 | double stir_clock(double entropy_estimate); | |
106 | double stir_rusage(int who, double entropy_estimate); | |
e54b3d7c | 107 | double hash_command_output(entropy_cmd_t *src, unsigned char *hash); |
416fd2a8 | 108 | int get_random_bytes_prngd(unsigned char *buf, int len, |
e9a17296 | 109 | unsigned short tcp_port, char *socket_path); |
110 | ||
111 | /* | |
112 | * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon | |
113 | * listening either on 'tcp_port', or via Unix domain socket at * | |
114 | * 'socket_path'. | |
416fd2a8 | 115 | * Either a non-zero tcp_port or a non-null socket_path must be |
e9a17296 | 116 | * supplied. |
117 | * Returns 0 on success, -1 on error | |
118 | */ | |
119 | int | |
416fd2a8 | 120 | get_random_bytes_prngd(unsigned char *buf, int len, |
e9a17296 | 121 | unsigned short tcp_port, char *socket_path) |
122 | { | |
123 | int fd, addr_len, rval, errors; | |
124 | char msg[2]; | |
125 | struct sockaddr_storage addr; | |
126 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; | |
127 | struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr; | |
128 | mysig_t old_sigpipe; | |
129 | ||
130 | /* Sanity checks */ | |
131 | if (socket_path == NULL && tcp_port == 0) | |
132 | fatal("You must specify a port or a socket"); | |
133 | if (socket_path != NULL && | |
134 | strlen(socket_path) >= sizeof(addr_un->sun_path)) | |
135 | fatal("Random pool path is too long"); | |
136 | if (len > 255) | |
137 | fatal("Too many bytes to read from PRNGD"); | |
138 | ||
139 | memset(&addr, '\0', sizeof(addr)); | |
140 | ||
141 | if (tcp_port != 0) { | |
142 | addr_in->sin_family = AF_INET; | |
143 | addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
144 | addr_in->sin_port = htons(tcp_port); | |
145 | addr_len = sizeof(*addr_in); | |
146 | } else { | |
147 | addr_un->sun_family = AF_UNIX; | |
148 | strlcpy(addr_un->sun_path, socket_path, | |
149 | sizeof(addr_un->sun_path)); | |
150 | addr_len = offsetof(struct sockaddr_un, sun_path) + | |
151 | strlen(socket_path) + 1; | |
152 | } | |
153 | ||
154 | old_sigpipe = mysignal(SIGPIPE, SIG_IGN); | |
155 | ||
156 | errors = 0; | |
157 | rval = -1; | |
158 | reopen: | |
159 | fd = socket(addr.ss_family, SOCK_STREAM, 0); | |
160 | if (fd == -1) { | |
161 | error("Couldn't create socket: %s", strerror(errno)); | |
162 | goto done; | |
163 | } | |
164 | ||
165 | if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) { | |
166 | if (tcp_port != 0) { | |
167 | error("Couldn't connect to PRNGD port %d: %s", | |
168 | tcp_port, strerror(errno)); | |
169 | } else { | |
170 | error("Couldn't connect to PRNGD socket \"%s\": %s", | |
171 | addr_un->sun_path, strerror(errno)); | |
172 | } | |
173 | goto done; | |
174 | } | |
175 | ||
176 | /* Send blocking read request to PRNGD */ | |
177 | msg[0] = 0x02; | |
178 | msg[1] = len; | |
179 | ||
70791e56 | 180 | if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) { |
e9a17296 | 181 | if (errno == EPIPE && errors < 10) { |
182 | close(fd); | |
183 | errors++; | |
184 | goto reopen; | |
185 | } | |
186 | error("Couldn't write to PRNGD socket: %s", | |
187 | strerror(errno)); | |
188 | goto done; | |
189 | } | |
190 | ||
191 | if (atomicio(read, fd, buf, len) != len) { | |
192 | if (errno == EPIPE && errors < 10) { | |
193 | close(fd); | |
194 | errors++; | |
195 | goto reopen; | |
196 | } | |
197 | error("Couldn't read from PRNGD socket: %s", | |
198 | strerror(errno)); | |
199 | goto done; | |
200 | } | |
201 | ||
202 | rval = 0; | |
203 | done: | |
204 | mysignal(SIGPIPE, old_sigpipe); | |
205 | if (fd != -1) | |
206 | close(fd); | |
207 | return rval; | |
208 | } | |
209 | ||
210 | double | |
211 | stir_gettimeofday(double entropy_estimate) | |
212 | { | |
213 | struct timeval tv; | |
214 | ||
215 | if (gettimeofday(&tv, NULL) == -1) | |
216 | fatal("Couldn't gettimeofday: %s", strerror(errno)); | |
217 | ||
218 | RAND_add(&tv, sizeof(tv), entropy_estimate); | |
219 | ||
220 | return entropy_estimate; | |
221 | } | |
222 | ||
223 | double | |
224 | stir_clock(double entropy_estimate) | |
225 | { | |
226 | #ifdef HAVE_CLOCK | |
227 | clock_t c; | |
228 | ||
229 | c = clock(); | |
230 | RAND_add(&c, sizeof(c), entropy_estimate); | |
231 | ||
232 | return entropy_estimate; | |
233 | #else /* _HAVE_CLOCK */ | |
234 | return 0; | |
235 | #endif /* _HAVE_CLOCK */ | |
236 | } | |
237 | ||
238 | double | |
239 | stir_rusage(int who, double entropy_estimate) | |
240 | { | |
241 | #ifdef HAVE_GETRUSAGE | |
242 | struct rusage ru; | |
243 | ||
244 | if (getrusage(who, &ru) == -1) | |
245 | return 0; | |
246 | ||
247 | RAND_add(&ru, sizeof(ru), entropy_estimate); | |
248 | ||
249 | return entropy_estimate; | |
250 | #else /* _HAVE_GETRUSAGE */ | |
251 | return 0; | |
252 | #endif /* _HAVE_GETRUSAGE */ | |
253 | } | |
254 | ||
255 | static int | |
256 | timeval_diff(struct timeval *t1, struct timeval *t2) | |
257 | { | |
258 | int secdiff, usecdiff; | |
259 | ||
260 | secdiff = t2->tv_sec - t1->tv_sec; | |
261 | usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec); | |
262 | return (int)(usecdiff / 1000); | |
263 | } | |
264 | ||
265 | double | |
e54b3d7c | 266 | hash_command_output(entropy_cmd_t *src, unsigned char *hash) |
e9a17296 | 267 | { |
268 | char buf[8192]; | |
269 | fd_set rdset; | |
270 | int bytes_read, cmd_eof, error_abort, msec_elapsed, p[2]; | |
271 | int status, total_bytes_read; | |
272 | static int devnull = -1; | |
273 | pid_t pid; | |
274 | SHA_CTX sha; | |
275 | struct timeval tv_start, tv_current; | |
276 | ||
277 | debug3("Reading output from \'%s\'", src->cmdstring); | |
278 | ||
279 | if (devnull == -1) { | |
280 | devnull = open("/dev/null", O_RDWR); | |
281 | if (devnull == -1) | |
416fd2a8 | 282 | fatal("Couldn't open /dev/null: %s", |
e9a17296 | 283 | strerror(errno)); |
284 | } | |
285 | ||
286 | if (pipe(p) == -1) | |
287 | fatal("Couldn't open pipe: %s", strerror(errno)); | |
288 | ||
289 | (void)gettimeofday(&tv_start, NULL); /* record start time */ | |
290 | ||
291 | switch (pid = fork()) { | |
292 | case -1: /* Error */ | |
293 | close(p[0]); | |
294 | close(p[1]); | |
295 | fatal("Couldn't fork: %s", strerror(errno)); | |
296 | /* NOTREACHED */ | |
297 | case 0: /* Child */ | |
298 | dup2(devnull, STDIN_FILENO); | |
299 | dup2(p[1], STDOUT_FILENO); | |
300 | dup2(p[1], STDERR_FILENO); | |
301 | close(p[0]); | |
302 | close(p[1]); | |
303 | close(devnull); | |
304 | ||
305 | execv(src->path, (char**)(src->args)); | |
306 | ||
416fd2a8 | 307 | debug("(child) Couldn't exec '%s': %s", |
e9a17296 | 308 | src->cmdstring, strerror(errno)); |
309 | _exit(-1); | |
310 | default: /* Parent */ | |
311 | break; | |
312 | } | |
313 | ||
314 | RAND_add(&pid, sizeof(&pid), 0.0); | |
315 | ||
316 | close(p[1]); | |
317 | ||
318 | /* Hash output from child */ | |
319 | SHA1_Init(&sha); | |
320 | ||
321 | cmd_eof = error_abort = msec_elapsed = total_bytes_read = 0; | |
322 | while (!error_abort && !cmd_eof) { | |
323 | int ret; | |
324 | struct timeval tv; | |
325 | int msec_remaining; | |
326 | ||
327 | (void) gettimeofday(&tv_current, 0); | |
328 | msec_elapsed = timeval_diff(&tv_start, &tv_current); | |
329 | if (msec_elapsed >= entropy_timeout_current) { | |
330 | error_abort=1; | |
331 | continue; | |
332 | } | |
333 | msec_remaining = entropy_timeout_current - msec_elapsed; | |
334 | ||
335 | FD_ZERO(&rdset); | |
336 | FD_SET(p[0], &rdset); | |
337 | tv.tv_sec = msec_remaining / 1000; | |
338 | tv.tv_usec = (msec_remaining % 1000) * 1000; | |
339 | ||
340 | ret = select(p[0] + 1, &rdset, NULL, NULL, &tv); | |
341 | ||
342 | RAND_add(&tv, sizeof(tv), 0.0); | |
343 | ||
344 | switch (ret) { | |
345 | case 0: | |
346 | /* timer expired */ | |
347 | error_abort = 1; | |
1c14df9e | 348 | kill(pid, SIGINT); |
e9a17296 | 349 | break; |
350 | case 1: | |
351 | /* command input */ | |
352 | do { | |
353 | bytes_read = read(p[0], buf, sizeof(buf)); | |
354 | } while (bytes_read == -1 && errno == EINTR); | |
355 | RAND_add(&bytes_read, sizeof(&bytes_read), 0.0); | |
356 | if (bytes_read == -1) { | |
357 | error_abort = 1; | |
358 | break; | |
359 | } else if (bytes_read) { | |
360 | SHA1_Update(&sha, buf, bytes_read); | |
361 | total_bytes_read += bytes_read; | |
362 | } else { | |
363 | cmd_eof = 1; | |
364 | } | |
365 | break; | |
366 | case -1: | |
367 | default: | |
368 | /* error */ | |
416fd2a8 | 369 | debug("Command '%s': select() failed: %s", |
e9a17296 | 370 | src->cmdstring, strerror(errno)); |
371 | error_abort = 1; | |
372 | break; | |
373 | } | |
374 | } | |
375 | ||
376 | SHA1_Final(hash, &sha); | |
377 | ||
378 | close(p[0]); | |
379 | ||
380 | debug3("Time elapsed: %d msec", msec_elapsed); | |
381 | ||
382 | if (waitpid(pid, &status, 0) == -1) { | |
383 | error("Couldn't wait for child '%s' completion: %s", | |
ff2d7a98 | 384 | src->cmdstring, strerror(errno)); |
e9a17296 | 385 | return 0.0; |
386 | } | |
387 | ||
388 | RAND_add(&status, sizeof(&status), 0.0); | |
389 | ||
390 | if (error_abort) { | |
391 | /* | |
392 | * Closing p[0] on timeout causes the entropy command to | |
416fd2a8 | 393 | * SIGPIPE. Take whatever output we got, and mark this |
394 | * command as slow | |
e9a17296 | 395 | */ |
396 | debug2("Command '%s' timed out", src->cmdstring); | |
397 | src->sticky_badness *= 2; | |
398 | src->badness = src->sticky_badness; | |
399 | return total_bytes_read; | |
400 | } | |
401 | ||
402 | if (WIFEXITED(status)) { | |
403 | if (WEXITSTATUS(status) == 0) { | |
404 | return total_bytes_read; | |
405 | } else { | |
406 | debug2("Command '%s' exit status was %d", | |
407 | src->cmdstring, WEXITSTATUS(status)); | |
408 | src->badness = src->sticky_badness = 128; | |
409 | return 0.0; | |
410 | } | |
411 | } else if (WIFSIGNALED(status)) { | |
412 | debug2("Command '%s' returned on uncaught signal %d !", | |
413 | src->cmdstring, status); | |
414 | src->badness = src->sticky_badness = 128; | |
415 | return 0.0; | |
416 | } else | |
417 | return 0.0; | |
418 | } | |
419 | ||
420 | double | |
421 | stir_from_system(void) | |
422 | { | |
423 | double total_entropy_estimate; | |
424 | long int i; | |
425 | ||
426 | total_entropy_estimate = 0; | |
427 | ||
428 | i = getpid(); | |
429 | RAND_add(&i, sizeof(i), 0.5); | |
430 | total_entropy_estimate += 0.1; | |
431 | ||
432 | i = getppid(); | |
433 | RAND_add(&i, sizeof(i), 0.5); | |
434 | total_entropy_estimate += 0.1; | |
435 | ||
436 | i = getuid(); | |
437 | RAND_add(&i, sizeof(i), 0.0); | |
438 | i = getgid(); | |
439 | RAND_add(&i, sizeof(i), 0.0); | |
440 | ||
441 | total_entropy_estimate += stir_gettimeofday(1.0); | |
442 | total_entropy_estimate += stir_clock(0.5); | |
443 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0); | |
444 | ||
445 | return total_entropy_estimate; | |
446 | } | |
447 | ||
448 | double | |
449 | stir_from_programs(void) | |
450 | { | |
451 | int c; | |
452 | double entropy, total_entropy; | |
e54b3d7c | 453 | unsigned char hash[SHA_DIGEST_LENGTH]; |
e9a17296 | 454 | |
455 | total_entropy = 0; | |
456 | for(c = 0; entropy_cmds[c].path != NULL; c++) { | |
457 | if (!entropy_cmds[c].badness) { | |
458 | /* Hash output from command */ | |
459 | entropy = hash_command_output(&entropy_cmds[c], | |
460 | hash); | |
461 | ||
462 | /* Scale back estimate by command's rate */ | |
463 | entropy *= entropy_cmds[c].rate; | |
464 | ||
465 | /* Upper bound of entropy is SHA_DIGEST_LENGTH */ | |
466 | if (entropy > SHA_DIGEST_LENGTH) | |
467 | entropy = SHA_DIGEST_LENGTH; | |
468 | ||
469 | /* Stir it in */ | |
470 | RAND_add(hash, sizeof(hash), entropy); | |
471 | ||
416fd2a8 | 472 | debug3("Got %0.2f bytes of entropy from '%s'", |
e9a17296 | 473 | entropy, entropy_cmds[c].cmdstring); |
474 | ||
475 | total_entropy += entropy; | |
476 | ||
477 | /* Execution time should be a bit unpredictable */ | |
478 | total_entropy += stir_gettimeofday(0.05); | |
479 | total_entropy += stir_clock(0.05); | |
480 | total_entropy += stir_rusage(RUSAGE_SELF, 0.1); | |
481 | total_entropy += stir_rusage(RUSAGE_CHILDREN, 0.1); | |
482 | } else { | |
483 | debug2("Command '%s' disabled (badness %d)", | |
416fd2a8 | 484 | entropy_cmds[c].cmdstring, |
e9a17296 | 485 | entropy_cmds[c].badness); |
486 | ||
487 | if (entropy_cmds[c].badness > 0) | |
488 | entropy_cmds[c].badness--; | |
489 | } | |
490 | } | |
491 | ||
492 | return total_entropy; | |
493 | } | |
494 | ||
495 | /* | |
496 | * prng seedfile functions | |
497 | */ | |
498 | int | |
499 | prng_check_seedfile(char *filename) | |
500 | { | |
501 | struct stat st; | |
502 | ||
503 | /* | |
416fd2a8 | 504 | * XXX raceable: eg replace seed between this stat and subsequent |
505 | * open. Not such a problem because we don't really trust the | |
e9a17296 | 506 | * seed file anyway. |
507 | * XXX: use secure path checking as elsewhere in OpenSSH | |
508 | */ | |
509 | if (lstat(filename, &st) == -1) { | |
510 | /* Give up on hard errors */ | |
511 | if (errno != ENOENT) | |
512 | debug("WARNING: Couldn't stat random seed file " | |
513 | "\"%.100s\": %s", filename, strerror(errno)); | |
514 | return 0; | |
515 | } | |
516 | ||
517 | /* regular file? */ | |
518 | if (!S_ISREG(st.st_mode)) | |
519 | fatal("PRNG seedfile %.100s is not a regular file", | |
520 | filename); | |
521 | ||
522 | /* mode 0600, owned by root or the current user? */ | |
523 | if (((st.st_mode & 0177) != 0) || !(st.st_uid == getuid())) { | |
524 | debug("WARNING: PRNG seedfile %.100s must be mode 0600, " | |
70791e56 | 525 | "owned by uid %li", filename, (long int)getuid()); |
e9a17296 | 526 | return 0; |
527 | } | |
528 | ||
529 | return 1; | |
530 | } | |
531 | ||
532 | void | |
533 | prng_write_seedfile(void) | |
534 | { | |
535 | int fd; | |
e54b3d7c | 536 | unsigned char seed[SEED_FILE_SIZE]; |
537 | char filename[MAXPATHLEN]; | |
e9a17296 | 538 | struct passwd *pw; |
539 | ||
540 | pw = getpwuid(getuid()); | |
541 | if (pw == NULL) | |
542 | fatal("Couldn't get password entry for current user " | |
70791e56 | 543 | "(%li): %s", (long int)getuid(), strerror(errno)); |
e9a17296 | 544 | |
545 | /* Try to ensure that the parent directory is there */ | |
546 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
547 | _PATH_SSH_USER_DIR); | |
548 | mkdir(filename, 0700); | |
549 | ||
550 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
551 | SSH_PRNG_SEED_FILE); | |
552 | ||
553 | debug("writing PRNG seed to file %.100s", filename); | |
554 | ||
1c14df9e | 555 | if (RAND_bytes(seed, sizeof(seed)) <= 0) |
416fd2a8 | 556 | fatal("PRNG seed extraction failed"); |
e9a17296 | 557 | |
558 | /* Don't care if the seed doesn't exist */ | |
559 | prng_check_seedfile(filename); | |
560 | ||
561 | if ((fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0600)) == -1) { | |
562 | debug("WARNING: couldn't access PRNG seedfile %.100s " | |
563 | "(%.100s)", filename, strerror(errno)); | |
564 | } else { | |
70791e56 | 565 | if (atomicio(vwrite, fd, &seed, sizeof(seed)) < sizeof(seed)) |
e9a17296 | 566 | fatal("problem writing PRNG seedfile %.100s " |
567 | "(%.100s)", filename, strerror(errno)); | |
568 | close(fd); | |
569 | } | |
570 | } | |
571 | ||
572 | void | |
573 | prng_read_seedfile(void) | |
574 | { | |
575 | int fd; | |
576 | char seed[SEED_FILE_SIZE], filename[MAXPATHLEN]; | |
577 | struct passwd *pw; | |
578 | ||
579 | pw = getpwuid(getuid()); | |
580 | if (pw == NULL) | |
581 | fatal("Couldn't get password entry for current user " | |
70791e56 | 582 | "(%li): %s", (long int)getuid(), strerror(errno)); |
e9a17296 | 583 | |
584 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
585 | SSH_PRNG_SEED_FILE); | |
586 | ||
587 | debug("loading PRNG seed from file %.100s", filename); | |
588 | ||
589 | if (!prng_check_seedfile(filename)) { | |
590 | verbose("Random seed file not found or invalid, ignoring."); | |
591 | return; | |
592 | } | |
593 | ||
594 | /* open the file and read in the seed */ | |
595 | fd = open(filename, O_RDONLY); | |
596 | if (fd == -1) | |
597 | fatal("could not open PRNG seedfile %.100s (%.100s)", | |
598 | filename, strerror(errno)); | |
599 | ||
600 | if (atomicio(read, fd, &seed, sizeof(seed)) < sizeof(seed)) { | |
601 | verbose("invalid or short read from PRNG seedfile " | |
602 | "%.100s - ignoring", filename); | |
603 | memset(seed, '\0', sizeof(seed)); | |
604 | } | |
605 | close(fd); | |
606 | ||
607 | /* stir in the seed, with estimated entropy zero */ | |
608 | RAND_add(&seed, sizeof(seed), 0.0); | |
609 | } | |
610 | ||
611 | ||
612 | /* | |
613 | * entropy command initialisation functions | |
614 | */ | |
615 | int | |
616 | prng_read_commands(char *cmdfilename) | |
617 | { | |
618 | char cmd[SEED_FILE_SIZE], *cp, line[1024], path[SEED_FILE_SIZE]; | |
619 | double est; | |
620 | entropy_cmd_t *entcmd; | |
621 | FILE *f; | |
622 | int cur_cmd, linenum, num_cmds, arg; | |
623 | ||
624 | if ((f = fopen(cmdfilename, "r")) == NULL) { | |
625 | fatal("couldn't read entropy commands file %.100s: %.100s", | |
626 | cmdfilename, strerror(errno)); | |
627 | } | |
628 | ||
629 | num_cmds = 64; | |
630 | entcmd = xmalloc(num_cmds * sizeof(entropy_cmd_t)); | |
631 | memset(entcmd, '\0', num_cmds * sizeof(entropy_cmd_t)); | |
632 | ||
633 | /* Read in file */ | |
634 | cur_cmd = linenum = 0; | |
635 | while (fgets(line, sizeof(line), f)) { | |
636 | linenum++; | |
637 | ||
638 | /* Skip leading whitespace, blank lines and comments */ | |
639 | cp = line + strspn(line, WHITESPACE); | |
640 | if ((*cp == 0) || (*cp == '#')) | |
641 | continue; /* done with this line */ | |
642 | ||
643 | /* | |
416fd2a8 | 644 | * The first non-whitespace char should be a double quote |
e9a17296 | 645 | * delimiting the commandline |
646 | */ | |
647 | if (*cp != '"') { | |
648 | error("bad entropy command, %.100s line %d", | |
649 | cmdfilename, linenum); | |
650 | continue; | |
651 | } | |
652 | ||
653 | /* | |
654 | * First token, command args (incl. argv[0]) in double | |
655 | * quotes | |
656 | */ | |
657 | cp = strtok(cp, "\""); | |
658 | if (cp == NULL) { | |
659 | error("missing or bad command string, %.100s " | |
660 | "line %d -- ignored", cmdfilename, linenum); | |
661 | continue; | |
662 | } | |
663 | strlcpy(cmd, cp, sizeof(cmd)); | |
664 | ||
665 | /* Second token, full command path */ | |
666 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { | |
667 | error("missing command path, %.100s " | |
668 | "line %d -- ignored", cmdfilename, linenum); | |
669 | continue; | |
670 | } | |
671 | ||
672 | /* Did configure mark this as dead? */ | |
673 | if (strncmp("undef", cp, 5) == 0) | |
674 | continue; | |
675 | ||
676 | strlcpy(path, cp, sizeof(path)); | |
677 | ||
678 | /* Third token, entropy rate estimate for this command */ | |
679 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { | |
680 | error("missing entropy estimate, %.100s " | |
681 | "line %d -- ignored", cmdfilename, linenum); | |
682 | continue; | |
683 | } | |
684 | est = strtod(cp, NULL); | |
685 | ||
686 | /* end of line */ | |
687 | if ((cp = strtok(NULL, WHITESPACE)) != NULL) { | |
688 | error("garbage at end of line %d in %.100s " | |
689 | "-- ignored", linenum, cmdfilename); | |
690 | continue; | |
691 | } | |
692 | ||
693 | /* save the command for debug messages */ | |
694 | entcmd[cur_cmd].cmdstring = xstrdup(cmd); | |
695 | ||
696 | /* split the command args */ | |
697 | cp = strtok(cmd, WHITESPACE); | |
698 | arg = 0; | |
699 | do { | |
700 | entcmd[cur_cmd].args[arg] = xstrdup(cp); | |
701 | arg++; | |
702 | } while(arg < NUM_ARGS && (cp = strtok(NULL, WHITESPACE))); | |
703 | ||
704 | if (strtok(NULL, WHITESPACE)) | |
705 | error("ignored extra commands (max %d), %.100s " | |
706 | "line %d", NUM_ARGS, cmdfilename, linenum); | |
707 | ||
708 | /* Copy the command path and rate estimate */ | |
709 | entcmd[cur_cmd].path = xstrdup(path); | |
710 | entcmd[cur_cmd].rate = est; | |
711 | ||
712 | /* Initialise other values */ | |
713 | entcmd[cur_cmd].sticky_badness = 1; | |
714 | ||
715 | cur_cmd++; | |
716 | ||
717 | /* | |
718 | * If we've filled the array, reallocate it twice the size | |
416fd2a8 | 719 | * Do this now because even if this we're on the last |
e9a17296 | 720 | * command we need another slot to mark the last entry |
721 | */ | |
722 | if (cur_cmd == num_cmds) { | |
723 | num_cmds *= 2; | |
724 | entcmd = xrealloc(entcmd, num_cmds * | |
725 | sizeof(entropy_cmd_t)); | |
726 | } | |
727 | } | |
728 | ||
729 | /* zero the last entry */ | |
730 | memset(&entcmd[cur_cmd], '\0', sizeof(entropy_cmd_t)); | |
731 | ||
732 | /* trim to size */ | |
733 | entropy_cmds = xrealloc(entcmd, (cur_cmd + 1) * | |
734 | sizeof(entropy_cmd_t)); | |
735 | ||
736 | debug("Loaded %d entropy commands from %.100s", cur_cmd, | |
737 | cmdfilename); | |
738 | ||
739 | return cur_cmd < MIN_ENTROPY_SOURCES ? -1 : 0; | |
740 | } | |
741 | ||
2980ea68 | 742 | void |
743 | usage(void) | |
744 | { | |
745 | fprintf(stderr, "Usage: %s [options]\n", __progname); | |
746 | fprintf(stderr, " -v Verbose; display verbose debugging messages.\n"); | |
747 | fprintf(stderr, " Multiple -v increases verbosity.\n"); | |
748 | fprintf(stderr, " -x Force output in hexidecimal (for debugging)\n"); | |
749 | fprintf(stderr, " -X Force output in binary\n"); | |
750 | fprintf(stderr, " -b bytes Number of bytes to output (default %d)\n", | |
751 | OUTPUT_SEED_SIZE); | |
752 | } | |
753 | ||
416fd2a8 | 754 | int |
e9a17296 | 755 | main(int argc, char **argv) |
756 | { | |
2980ea68 | 757 | unsigned char *buf; |
758 | int ret, ch, debug_level, output_hex, bytes; | |
759 | extern char *optarg; | |
760 | LogLevel ll; | |
e9a17296 | 761 | |
70791e56 | 762 | __progname = ssh_get_progname(argv[0]); |
ae43c103 | 763 | init_pathnames(); |
e9a17296 | 764 | log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); |
765 | ||
2980ea68 | 766 | ll = SYSLOG_LEVEL_INFO; |
767 | debug_level = output_hex = 0; | |
768 | bytes = OUTPUT_SEED_SIZE; | |
769 | ||
770 | /* Don't write binary data to a tty, unless we are forced to */ | |
771 | if (isatty(STDOUT_FILENO)) | |
772 | output_hex = 1; | |
416fd2a8 | 773 | |
2980ea68 | 774 | while ((ch = getopt(argc, argv, "vxXhb:")) != -1) { |
775 | switch (ch) { | |
776 | case 'v': | |
777 | if (debug_level < 3) | |
778 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level++; | |
779 | break; | |
780 | case 'x': | |
781 | output_hex = 1; | |
782 | break; | |
783 | case 'X': | |
784 | output_hex = 0; | |
785 | break; | |
786 | case 'b': | |
787 | if ((bytes = atoi(optarg)) <= 0) | |
788 | fatal("Invalid number of output bytes"); | |
789 | break; | |
790 | case 'h': | |
791 | usage(); | |
792 | exit(0); | |
793 | default: | |
794 | error("Invalid commandline option"); | |
795 | usage(); | |
796 | } | |
797 | } | |
798 | ||
799 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); | |
416fd2a8 | 800 | |
e9a17296 | 801 | #ifdef USE_SEED_FILES |
802 | prng_read_seedfile(); | |
803 | #endif | |
804 | ||
2980ea68 | 805 | buf = xmalloc(bytes); |
806 | ||
e9a17296 | 807 | /* |
808 | * Seed the RNG from wherever we can | |
809 | */ | |
416fd2a8 | 810 | |
e9a17296 | 811 | /* Take whatever is on the stack, but don't credit it */ |
2980ea68 | 812 | RAND_add(buf, bytes, 0); |
e9a17296 | 813 | |
416fd2a8 | 814 | debug("Seeded RNG with %i bytes from system calls", |
e9a17296 | 815 | (int)stir_from_system()); |
816 | ||
817 | #ifdef PRNGD_PORT | |
2980ea68 | 818 | if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == -1) |
e9a17296 | 819 | fatal("Entropy collection failed"); |
2980ea68 | 820 | RAND_add(buf, bytes, bytes); |
e9a17296 | 821 | #elif defined(PRNGD_SOCKET) |
2980ea68 | 822 | if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == -1) |
e9a17296 | 823 | fatal("Entropy collection failed"); |
2980ea68 | 824 | RAND_add(buf, bytes, bytes); |
e9a17296 | 825 | #else |
826 | /* Read in collection commands */ | |
827 | if (prng_read_commands(SSH_PRNG_COMMAND_FILE) == -1) | |
828 | fatal("PRNG initialisation failed -- exiting."); | |
416fd2a8 | 829 | debug("Seeded RNG with %i bytes from programs", |
e9a17296 | 830 | (int)stir_from_programs()); |
831 | #endif | |
832 | ||
833 | #ifdef USE_SEED_FILES | |
834 | prng_write_seedfile(); | |
835 | #endif | |
836 | ||
837 | /* | |
838 | * Write the seed to stdout | |
839 | */ | |
840 | ||
841 | if (!RAND_status()) | |
842 | fatal("Not enough entropy in RNG"); | |
843 | ||
1c14df9e | 844 | if (RAND_bytes(buf, bytes) <= 0) |
845 | fatal("Couldn't extract entropy from PRNG"); | |
e9a17296 | 846 | |
2980ea68 | 847 | if (output_hex) { |
848 | for(ret = 0; ret < bytes; ret++) | |
849 | printf("%02x", (unsigned char)(buf[ret])); | |
850 | printf("\n"); | |
851 | } else | |
70791e56 | 852 | ret = atomicio(vwrite, STDOUT_FILENO, buf, bytes); |
416fd2a8 | 853 | |
2980ea68 | 854 | memset(buf, '\0', bytes); |
855 | xfree(buf); | |
416fd2a8 | 856 | |
2980ea68 | 857 | return ret == bytes ? 0 : 1; |
e9a17296 | 858 | } |