]>
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; | |
34fee935 | 124 | u_char msg[2]; |
e9a17296 | 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"); | |
34fee935 | 136 | if (len <= 0 || len > 255) |
137 | fatal("Too many bytes (%d) to read from PRNGD", len); | |
e9a17296 | 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 | ||
34fee935 | 191 | if (atomicio(read, fd, buf, len) != (size_t)len) { |
e9a17296 | 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 | ||
34fee935 | 210 | static int |
211 | seed_from_prngd(unsigned char *buf, size_t bytes) | |
212 | { | |
213 | #ifdef PRNGD_PORT | |
214 | debug("trying egd/prngd port %d", PRNGD_PORT); | |
215 | if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0) | |
216 | return 0; | |
217 | #endif | |
218 | #ifdef PRNGD_SOCKET | |
219 | debug("trying egd/prngd socket %s", PRNGD_SOCKET); | |
220 | if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0) | |
221 | return 0; | |
222 | #endif | |
223 | return -1; | |
224 | } | |
225 | ||
e9a17296 | 226 | double |
227 | stir_gettimeofday(double entropy_estimate) | |
228 | { | |
229 | struct timeval tv; | |
230 | ||
231 | if (gettimeofday(&tv, NULL) == -1) | |
232 | fatal("Couldn't gettimeofday: %s", strerror(errno)); | |
233 | ||
234 | RAND_add(&tv, sizeof(tv), entropy_estimate); | |
235 | ||
236 | return entropy_estimate; | |
237 | } | |
238 | ||
239 | double | |
240 | stir_clock(double entropy_estimate) | |
241 | { | |
242 | #ifdef HAVE_CLOCK | |
243 | clock_t c; | |
244 | ||
245 | c = clock(); | |
246 | RAND_add(&c, sizeof(c), entropy_estimate); | |
247 | ||
248 | return entropy_estimate; | |
249 | #else /* _HAVE_CLOCK */ | |
250 | return 0; | |
251 | #endif /* _HAVE_CLOCK */ | |
252 | } | |
253 | ||
254 | double | |
255 | stir_rusage(int who, double entropy_estimate) | |
256 | { | |
257 | #ifdef HAVE_GETRUSAGE | |
258 | struct rusage ru; | |
259 | ||
260 | if (getrusage(who, &ru) == -1) | |
261 | return 0; | |
262 | ||
263 | RAND_add(&ru, sizeof(ru), entropy_estimate); | |
264 | ||
265 | return entropy_estimate; | |
266 | #else /* _HAVE_GETRUSAGE */ | |
267 | return 0; | |
268 | #endif /* _HAVE_GETRUSAGE */ | |
269 | } | |
270 | ||
271 | static int | |
272 | timeval_diff(struct timeval *t1, struct timeval *t2) | |
273 | { | |
274 | int secdiff, usecdiff; | |
275 | ||
276 | secdiff = t2->tv_sec - t1->tv_sec; | |
277 | usecdiff = (secdiff*1000000) + (t2->tv_usec - t1->tv_usec); | |
278 | return (int)(usecdiff / 1000); | |
279 | } | |
280 | ||
281 | double | |
e54b3d7c | 282 | hash_command_output(entropy_cmd_t *src, unsigned char *hash) |
e9a17296 | 283 | { |
284 | char buf[8192]; | |
285 | fd_set rdset; | |
286 | int bytes_read, cmd_eof, error_abort, msec_elapsed, p[2]; | |
287 | int status, total_bytes_read; | |
288 | static int devnull = -1; | |
289 | pid_t pid; | |
290 | SHA_CTX sha; | |
291 | struct timeval tv_start, tv_current; | |
292 | ||
293 | debug3("Reading output from \'%s\'", src->cmdstring); | |
294 | ||
295 | if (devnull == -1) { | |
296 | devnull = open("/dev/null", O_RDWR); | |
297 | if (devnull == -1) | |
416fd2a8 | 298 | fatal("Couldn't open /dev/null: %s", |
e9a17296 | 299 | strerror(errno)); |
300 | } | |
301 | ||
302 | if (pipe(p) == -1) | |
303 | fatal("Couldn't open pipe: %s", strerror(errno)); | |
304 | ||
305 | (void)gettimeofday(&tv_start, NULL); /* record start time */ | |
306 | ||
307 | switch (pid = fork()) { | |
308 | case -1: /* Error */ | |
309 | close(p[0]); | |
310 | close(p[1]); | |
311 | fatal("Couldn't fork: %s", strerror(errno)); | |
312 | /* NOTREACHED */ | |
313 | case 0: /* Child */ | |
314 | dup2(devnull, STDIN_FILENO); | |
315 | dup2(p[1], STDOUT_FILENO); | |
316 | dup2(p[1], STDERR_FILENO); | |
317 | close(p[0]); | |
318 | close(p[1]); | |
319 | close(devnull); | |
320 | ||
321 | execv(src->path, (char**)(src->args)); | |
322 | ||
416fd2a8 | 323 | debug("(child) Couldn't exec '%s': %s", |
e9a17296 | 324 | src->cmdstring, strerror(errno)); |
325 | _exit(-1); | |
326 | default: /* Parent */ | |
327 | break; | |
328 | } | |
329 | ||
330 | RAND_add(&pid, sizeof(&pid), 0.0); | |
331 | ||
332 | close(p[1]); | |
333 | ||
334 | /* Hash output from child */ | |
335 | SHA1_Init(&sha); | |
336 | ||
337 | cmd_eof = error_abort = msec_elapsed = total_bytes_read = 0; | |
338 | while (!error_abort && !cmd_eof) { | |
339 | int ret; | |
340 | struct timeval tv; | |
341 | int msec_remaining; | |
342 | ||
343 | (void) gettimeofday(&tv_current, 0); | |
344 | msec_elapsed = timeval_diff(&tv_start, &tv_current); | |
345 | if (msec_elapsed >= entropy_timeout_current) { | |
346 | error_abort=1; | |
347 | continue; | |
348 | } | |
349 | msec_remaining = entropy_timeout_current - msec_elapsed; | |
350 | ||
351 | FD_ZERO(&rdset); | |
352 | FD_SET(p[0], &rdset); | |
353 | tv.tv_sec = msec_remaining / 1000; | |
354 | tv.tv_usec = (msec_remaining % 1000) * 1000; | |
355 | ||
356 | ret = select(p[0] + 1, &rdset, NULL, NULL, &tv); | |
357 | ||
358 | RAND_add(&tv, sizeof(tv), 0.0); | |
359 | ||
360 | switch (ret) { | |
361 | case 0: | |
362 | /* timer expired */ | |
363 | error_abort = 1; | |
1c14df9e | 364 | kill(pid, SIGINT); |
e9a17296 | 365 | break; |
366 | case 1: | |
367 | /* command input */ | |
368 | do { | |
369 | bytes_read = read(p[0], buf, sizeof(buf)); | |
370 | } while (bytes_read == -1 && errno == EINTR); | |
371 | RAND_add(&bytes_read, sizeof(&bytes_read), 0.0); | |
372 | if (bytes_read == -1) { | |
373 | error_abort = 1; | |
374 | break; | |
375 | } else if (bytes_read) { | |
376 | SHA1_Update(&sha, buf, bytes_read); | |
377 | total_bytes_read += bytes_read; | |
378 | } else { | |
379 | cmd_eof = 1; | |
380 | } | |
381 | break; | |
382 | case -1: | |
383 | default: | |
384 | /* error */ | |
416fd2a8 | 385 | debug("Command '%s': select() failed: %s", |
e9a17296 | 386 | src->cmdstring, strerror(errno)); |
387 | error_abort = 1; | |
388 | break; | |
389 | } | |
390 | } | |
391 | ||
392 | SHA1_Final(hash, &sha); | |
393 | ||
394 | close(p[0]); | |
395 | ||
396 | debug3("Time elapsed: %d msec", msec_elapsed); | |
397 | ||
398 | if (waitpid(pid, &status, 0) == -1) { | |
34fee935 | 399 | error("Couldn't wait for child '%s' completion: %s", |
400 | src->cmdstring, strerror(errno)); | |
e9a17296 | 401 | return 0.0; |
402 | } | |
403 | ||
404 | RAND_add(&status, sizeof(&status), 0.0); | |
405 | ||
406 | if (error_abort) { | |
407 | /* | |
408 | * Closing p[0] on timeout causes the entropy command to | |
416fd2a8 | 409 | * SIGPIPE. Take whatever output we got, and mark this |
410 | * command as slow | |
e9a17296 | 411 | */ |
412 | debug2("Command '%s' timed out", src->cmdstring); | |
413 | src->sticky_badness *= 2; | |
414 | src->badness = src->sticky_badness; | |
415 | return total_bytes_read; | |
416 | } | |
417 | ||
418 | if (WIFEXITED(status)) { | |
419 | if (WEXITSTATUS(status) == 0) { | |
420 | return total_bytes_read; | |
421 | } else { | |
422 | debug2("Command '%s' exit status was %d", | |
423 | src->cmdstring, WEXITSTATUS(status)); | |
424 | src->badness = src->sticky_badness = 128; | |
425 | return 0.0; | |
426 | } | |
427 | } else if (WIFSIGNALED(status)) { | |
428 | debug2("Command '%s' returned on uncaught signal %d !", | |
429 | src->cmdstring, status); | |
430 | src->badness = src->sticky_badness = 128; | |
431 | return 0.0; | |
432 | } else | |
433 | return 0.0; | |
434 | } | |
435 | ||
436 | double | |
437 | stir_from_system(void) | |
438 | { | |
439 | double total_entropy_estimate; | |
440 | long int i; | |
441 | ||
442 | total_entropy_estimate = 0; | |
443 | ||
444 | i = getpid(); | |
445 | RAND_add(&i, sizeof(i), 0.5); | |
446 | total_entropy_estimate += 0.1; | |
447 | ||
448 | i = getppid(); | |
449 | RAND_add(&i, sizeof(i), 0.5); | |
450 | total_entropy_estimate += 0.1; | |
451 | ||
452 | i = getuid(); | |
453 | RAND_add(&i, sizeof(i), 0.0); | |
454 | i = getgid(); | |
455 | RAND_add(&i, sizeof(i), 0.0); | |
456 | ||
457 | total_entropy_estimate += stir_gettimeofday(1.0); | |
458 | total_entropy_estimate += stir_clock(0.5); | |
459 | total_entropy_estimate += stir_rusage(RUSAGE_SELF, 2.0); | |
460 | ||
461 | return total_entropy_estimate; | |
462 | } | |
463 | ||
464 | double | |
465 | stir_from_programs(void) | |
466 | { | |
467 | int c; | |
468 | double entropy, total_entropy; | |
e54b3d7c | 469 | unsigned char hash[SHA_DIGEST_LENGTH]; |
e9a17296 | 470 | |
471 | total_entropy = 0; | |
472 | for(c = 0; entropy_cmds[c].path != NULL; c++) { | |
473 | if (!entropy_cmds[c].badness) { | |
474 | /* Hash output from command */ | |
475 | entropy = hash_command_output(&entropy_cmds[c], | |
476 | hash); | |
477 | ||
478 | /* Scale back estimate by command's rate */ | |
479 | entropy *= entropy_cmds[c].rate; | |
480 | ||
481 | /* Upper bound of entropy is SHA_DIGEST_LENGTH */ | |
482 | if (entropy > SHA_DIGEST_LENGTH) | |
483 | entropy = SHA_DIGEST_LENGTH; | |
484 | ||
485 | /* Stir it in */ | |
486 | RAND_add(hash, sizeof(hash), entropy); | |
487 | ||
416fd2a8 | 488 | debug3("Got %0.2f bytes of entropy from '%s'", |
e9a17296 | 489 | entropy, entropy_cmds[c].cmdstring); |
490 | ||
491 | total_entropy += entropy; | |
492 | ||
493 | /* Execution time should be a bit unpredictable */ | |
494 | total_entropy += stir_gettimeofday(0.05); | |
495 | total_entropy += stir_clock(0.05); | |
496 | total_entropy += stir_rusage(RUSAGE_SELF, 0.1); | |
497 | total_entropy += stir_rusage(RUSAGE_CHILDREN, 0.1); | |
498 | } else { | |
499 | debug2("Command '%s' disabled (badness %d)", | |
416fd2a8 | 500 | entropy_cmds[c].cmdstring, |
e9a17296 | 501 | entropy_cmds[c].badness); |
502 | ||
503 | if (entropy_cmds[c].badness > 0) | |
504 | entropy_cmds[c].badness--; | |
505 | } | |
506 | } | |
507 | ||
508 | return total_entropy; | |
509 | } | |
510 | ||
511 | /* | |
512 | * prng seedfile functions | |
513 | */ | |
514 | int | |
515 | prng_check_seedfile(char *filename) | |
516 | { | |
517 | struct stat st; | |
518 | ||
519 | /* | |
416fd2a8 | 520 | * XXX raceable: eg replace seed between this stat and subsequent |
521 | * open. Not such a problem because we don't really trust the | |
e9a17296 | 522 | * seed file anyway. |
523 | * XXX: use secure path checking as elsewhere in OpenSSH | |
524 | */ | |
525 | if (lstat(filename, &st) == -1) { | |
526 | /* Give up on hard errors */ | |
527 | if (errno != ENOENT) | |
528 | debug("WARNING: Couldn't stat random seed file " | |
529 | "\"%.100s\": %s", filename, strerror(errno)); | |
530 | return 0; | |
531 | } | |
532 | ||
533 | /* regular file? */ | |
534 | if (!S_ISREG(st.st_mode)) | |
535 | fatal("PRNG seedfile %.100s is not a regular file", | |
536 | filename); | |
537 | ||
538 | /* mode 0600, owned by root or the current user? */ | |
539 | if (((st.st_mode & 0177) != 0) || !(st.st_uid == getuid())) { | |
540 | debug("WARNING: PRNG seedfile %.100s must be mode 0600, " | |
70791e56 | 541 | "owned by uid %li", filename, (long int)getuid()); |
e9a17296 | 542 | return 0; |
543 | } | |
544 | ||
545 | return 1; | |
546 | } | |
547 | ||
548 | void | |
549 | prng_write_seedfile(void) | |
550 | { | |
34fee935 | 551 | int fd, save_errno; |
e54b3d7c | 552 | unsigned char seed[SEED_FILE_SIZE]; |
34fee935 | 553 | char filename[MAXPATHLEN], tmpseed[MAXPATHLEN]; |
e9a17296 | 554 | struct passwd *pw; |
34fee935 | 555 | mode_t old_umask; |
e9a17296 | 556 | |
557 | pw = getpwuid(getuid()); | |
558 | if (pw == NULL) | |
559 | fatal("Couldn't get password entry for current user " | |
70791e56 | 560 | "(%li): %s", (long int)getuid(), strerror(errno)); |
e9a17296 | 561 | |
562 | /* Try to ensure that the parent directory is there */ | |
563 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
564 | _PATH_SSH_USER_DIR); | |
565 | mkdir(filename, 0700); | |
566 | ||
567 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
568 | SSH_PRNG_SEED_FILE); | |
569 | ||
34fee935 | 570 | strlcpy(tmpseed, filename, sizeof(tmpseed)); |
571 | if (strlcat(tmpseed, ".XXXXXXXXXX", sizeof(tmpseed)) >= | |
572 | sizeof(tmpseed)) | |
573 | fatal("PRNG seed filename too long"); | |
e9a17296 | 574 | |
1c14df9e | 575 | if (RAND_bytes(seed, sizeof(seed)) <= 0) |
416fd2a8 | 576 | fatal("PRNG seed extraction failed"); |
e9a17296 | 577 | |
578 | /* Don't care if the seed doesn't exist */ | |
579 | prng_check_seedfile(filename); | |
580 | ||
34fee935 | 581 | old_umask = umask(0177); |
582 | ||
583 | if ((fd = mkstemp(tmpseed)) == -1) { | |
584 | debug("WARNING: couldn't make temporary PRNG seedfile %.100s " | |
585 | "(%.100s)", tmpseed, strerror(errno)); | |
e9a17296 | 586 | } else { |
34fee935 | 587 | debug("writing PRNG seed to file %.100s", tmpseed); |
588 | if (atomicio(vwrite, fd, &seed, sizeof(seed)) < sizeof(seed)) { | |
589 | save_errno = errno; | |
590 | close(fd); | |
591 | unlink(tmpseed); | |
e9a17296 | 592 | fatal("problem writing PRNG seedfile %.100s " |
34fee935 | 593 | "(%.100s)", filename, strerror(save_errno)); |
594 | } | |
e9a17296 | 595 | close(fd); |
34fee935 | 596 | debug("moving temporary PRNG seed to file %.100s", filename); |
597 | if (rename(tmpseed, filename) == -1) { | |
598 | save_errno = errno; | |
599 | unlink(tmpseed); | |
600 | fatal("problem renaming PRNG seedfile from %.100s " | |
601 | "to %.100s (%.100s)", tmpseed, filename, | |
602 | strerror(save_errno)); | |
603 | } | |
e9a17296 | 604 | } |
34fee935 | 605 | umask(old_umask); |
e9a17296 | 606 | } |
607 | ||
608 | void | |
609 | prng_read_seedfile(void) | |
610 | { | |
611 | int fd; | |
612 | char seed[SEED_FILE_SIZE], filename[MAXPATHLEN]; | |
613 | struct passwd *pw; | |
614 | ||
615 | pw = getpwuid(getuid()); | |
616 | if (pw == NULL) | |
617 | fatal("Couldn't get password entry for current user " | |
70791e56 | 618 | "(%li): %s", (long int)getuid(), strerror(errno)); |
e9a17296 | 619 | |
620 | snprintf(filename, sizeof(filename), "%.512s/%s", pw->pw_dir, | |
621 | SSH_PRNG_SEED_FILE); | |
622 | ||
623 | debug("loading PRNG seed from file %.100s", filename); | |
624 | ||
625 | if (!prng_check_seedfile(filename)) { | |
626 | verbose("Random seed file not found or invalid, ignoring."); | |
627 | return; | |
628 | } | |
629 | ||
630 | /* open the file and read in the seed */ | |
631 | fd = open(filename, O_RDONLY); | |
632 | if (fd == -1) | |
633 | fatal("could not open PRNG seedfile %.100s (%.100s)", | |
634 | filename, strerror(errno)); | |
635 | ||
636 | if (atomicio(read, fd, &seed, sizeof(seed)) < sizeof(seed)) { | |
637 | verbose("invalid or short read from PRNG seedfile " | |
638 | "%.100s - ignoring", filename); | |
639 | memset(seed, '\0', sizeof(seed)); | |
640 | } | |
641 | close(fd); | |
642 | ||
643 | /* stir in the seed, with estimated entropy zero */ | |
644 | RAND_add(&seed, sizeof(seed), 0.0); | |
645 | } | |
646 | ||
647 | ||
648 | /* | |
649 | * entropy command initialisation functions | |
650 | */ | |
651 | int | |
652 | prng_read_commands(char *cmdfilename) | |
653 | { | |
654 | char cmd[SEED_FILE_SIZE], *cp, line[1024], path[SEED_FILE_SIZE]; | |
655 | double est; | |
656 | entropy_cmd_t *entcmd; | |
657 | FILE *f; | |
658 | int cur_cmd, linenum, num_cmds, arg; | |
659 | ||
660 | if ((f = fopen(cmdfilename, "r")) == NULL) { | |
661 | fatal("couldn't read entropy commands file %.100s: %.100s", | |
662 | cmdfilename, strerror(errno)); | |
663 | } | |
664 | ||
665 | num_cmds = 64; | |
666 | entcmd = xmalloc(num_cmds * sizeof(entropy_cmd_t)); | |
667 | memset(entcmd, '\0', num_cmds * sizeof(entropy_cmd_t)); | |
668 | ||
669 | /* Read in file */ | |
670 | cur_cmd = linenum = 0; | |
671 | while (fgets(line, sizeof(line), f)) { | |
672 | linenum++; | |
673 | ||
674 | /* Skip leading whitespace, blank lines and comments */ | |
675 | cp = line + strspn(line, WHITESPACE); | |
676 | if ((*cp == 0) || (*cp == '#')) | |
677 | continue; /* done with this line */ | |
678 | ||
679 | /* | |
416fd2a8 | 680 | * The first non-whitespace char should be a double quote |
e9a17296 | 681 | * delimiting the commandline |
682 | */ | |
683 | if (*cp != '"') { | |
684 | error("bad entropy command, %.100s line %d", | |
685 | cmdfilename, linenum); | |
686 | continue; | |
687 | } | |
688 | ||
689 | /* | |
690 | * First token, command args (incl. argv[0]) in double | |
691 | * quotes | |
692 | */ | |
693 | cp = strtok(cp, "\""); | |
694 | if (cp == NULL) { | |
695 | error("missing or bad command string, %.100s " | |
696 | "line %d -- ignored", cmdfilename, linenum); | |
697 | continue; | |
698 | } | |
699 | strlcpy(cmd, cp, sizeof(cmd)); | |
700 | ||
701 | /* Second token, full command path */ | |
702 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { | |
703 | error("missing command path, %.100s " | |
704 | "line %d -- ignored", cmdfilename, linenum); | |
705 | continue; | |
706 | } | |
707 | ||
708 | /* Did configure mark this as dead? */ | |
709 | if (strncmp("undef", cp, 5) == 0) | |
710 | continue; | |
711 | ||
712 | strlcpy(path, cp, sizeof(path)); | |
713 | ||
714 | /* Third token, entropy rate estimate for this command */ | |
715 | if ((cp = strtok(NULL, WHITESPACE)) == NULL) { | |
716 | error("missing entropy estimate, %.100s " | |
717 | "line %d -- ignored", cmdfilename, linenum); | |
718 | continue; | |
719 | } | |
720 | est = strtod(cp, NULL); | |
721 | ||
722 | /* end of line */ | |
723 | if ((cp = strtok(NULL, WHITESPACE)) != NULL) { | |
724 | error("garbage at end of line %d in %.100s " | |
725 | "-- ignored", linenum, cmdfilename); | |
726 | continue; | |
727 | } | |
728 | ||
729 | /* save the command for debug messages */ | |
730 | entcmd[cur_cmd].cmdstring = xstrdup(cmd); | |
731 | ||
732 | /* split the command args */ | |
733 | cp = strtok(cmd, WHITESPACE); | |
734 | arg = 0; | |
735 | do { | |
736 | entcmd[cur_cmd].args[arg] = xstrdup(cp); | |
737 | arg++; | |
738 | } while(arg < NUM_ARGS && (cp = strtok(NULL, WHITESPACE))); | |
739 | ||
740 | if (strtok(NULL, WHITESPACE)) | |
741 | error("ignored extra commands (max %d), %.100s " | |
742 | "line %d", NUM_ARGS, cmdfilename, linenum); | |
743 | ||
744 | /* Copy the command path and rate estimate */ | |
745 | entcmd[cur_cmd].path = xstrdup(path); | |
746 | entcmd[cur_cmd].rate = est; | |
747 | ||
748 | /* Initialise other values */ | |
749 | entcmd[cur_cmd].sticky_badness = 1; | |
750 | ||
751 | cur_cmd++; | |
752 | ||
753 | /* | |
754 | * If we've filled the array, reallocate it twice the size | |
416fd2a8 | 755 | * Do this now because even if this we're on the last |
e9a17296 | 756 | * command we need another slot to mark the last entry |
757 | */ | |
758 | if (cur_cmd == num_cmds) { | |
759 | num_cmds *= 2; | |
760 | entcmd = xrealloc(entcmd, num_cmds * | |
761 | sizeof(entropy_cmd_t)); | |
762 | } | |
763 | } | |
764 | ||
765 | /* zero the last entry */ | |
766 | memset(&entcmd[cur_cmd], '\0', sizeof(entropy_cmd_t)); | |
767 | ||
768 | /* trim to size */ | |
769 | entropy_cmds = xrealloc(entcmd, (cur_cmd + 1) * | |
770 | sizeof(entropy_cmd_t)); | |
771 | ||
772 | debug("Loaded %d entropy commands from %.100s", cur_cmd, | |
773 | cmdfilename); | |
774 | ||
775 | return cur_cmd < MIN_ENTROPY_SOURCES ? -1 : 0; | |
776 | } | |
777 | ||
2980ea68 | 778 | void |
779 | usage(void) | |
780 | { | |
781 | fprintf(stderr, "Usage: %s [options]\n", __progname); | |
782 | fprintf(stderr, " -v Verbose; display verbose debugging messages.\n"); | |
783 | fprintf(stderr, " Multiple -v increases verbosity.\n"); | |
34fee935 | 784 | fprintf(stderr, " -x Force output in hexadecimal (for debugging)\n"); |
2980ea68 | 785 | fprintf(stderr, " -X Force output in binary\n"); |
786 | fprintf(stderr, " -b bytes Number of bytes to output (default %d)\n", | |
787 | OUTPUT_SEED_SIZE); | |
788 | } | |
789 | ||
416fd2a8 | 790 | int |
e9a17296 | 791 | main(int argc, char **argv) |
792 | { | |
2980ea68 | 793 | unsigned char *buf; |
794 | int ret, ch, debug_level, output_hex, bytes; | |
795 | extern char *optarg; | |
796 | LogLevel ll; | |
e9a17296 | 797 | |
70791e56 | 798 | __progname = ssh_get_progname(argv[0]); |
ae43c103 | 799 | init_pathnames(); |
e9a17296 | 800 | log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); |
801 | ||
2980ea68 | 802 | ll = SYSLOG_LEVEL_INFO; |
803 | debug_level = output_hex = 0; | |
804 | bytes = OUTPUT_SEED_SIZE; | |
805 | ||
806 | /* Don't write binary data to a tty, unless we are forced to */ | |
807 | if (isatty(STDOUT_FILENO)) | |
808 | output_hex = 1; | |
416fd2a8 | 809 | |
2980ea68 | 810 | while ((ch = getopt(argc, argv, "vxXhb:")) != -1) { |
811 | switch (ch) { | |
812 | case 'v': | |
813 | if (debug_level < 3) | |
814 | ll = SYSLOG_LEVEL_DEBUG1 + debug_level++; | |
815 | break; | |
816 | case 'x': | |
817 | output_hex = 1; | |
818 | break; | |
819 | case 'X': | |
820 | output_hex = 0; | |
821 | break; | |
822 | case 'b': | |
823 | if ((bytes = atoi(optarg)) <= 0) | |
824 | fatal("Invalid number of output bytes"); | |
825 | break; | |
826 | case 'h': | |
827 | usage(); | |
828 | exit(0); | |
829 | default: | |
830 | error("Invalid commandline option"); | |
831 | usage(); | |
832 | } | |
833 | } | |
834 | ||
835 | log_init(argv[0], ll, SYSLOG_FACILITY_USER, 1); | |
416fd2a8 | 836 | |
e9a17296 | 837 | #ifdef USE_SEED_FILES |
838 | prng_read_seedfile(); | |
839 | #endif | |
840 | ||
2980ea68 | 841 | buf = xmalloc(bytes); |
842 | ||
e9a17296 | 843 | /* |
844 | * Seed the RNG from wherever we can | |
845 | */ | |
416fd2a8 | 846 | |
e9a17296 | 847 | /* Take whatever is on the stack, but don't credit it */ |
2980ea68 | 848 | RAND_add(buf, bytes, 0); |
e9a17296 | 849 | |
416fd2a8 | 850 | debug("Seeded RNG with %i bytes from system calls", |
e9a17296 | 851 | (int)stir_from_system()); |
852 | ||
34fee935 | 853 | /* try prngd, fall back to commands if prngd fails or not configured */ |
854 | if (seed_from_prngd(buf, bytes) == 0) { | |
855 | RAND_add(buf, bytes, bytes); | |
856 | } else { | |
857 | /* Read in collection commands */ | |
858 | if (prng_read_commands(SSH_PRNG_COMMAND_FILE) == -1) | |
859 | fatal("PRNG initialisation failed -- exiting."); | |
860 | debug("Seeded RNG with %i bytes from programs", | |
861 | (int)stir_from_programs()); | |
862 | } | |
e9a17296 | 863 | |
864 | #ifdef USE_SEED_FILES | |
865 | prng_write_seedfile(); | |
866 | #endif | |
867 | ||
868 | /* | |
869 | * Write the seed to stdout | |
870 | */ | |
871 | ||
872 | if (!RAND_status()) | |
873 | fatal("Not enough entropy in RNG"); | |
874 | ||
1c14df9e | 875 | if (RAND_bytes(buf, bytes) <= 0) |
876 | fatal("Couldn't extract entropy from PRNG"); | |
e9a17296 | 877 | |
2980ea68 | 878 | if (output_hex) { |
879 | for(ret = 0; ret < bytes; ret++) | |
880 | printf("%02x", (unsigned char)(buf[ret])); | |
881 | printf("\n"); | |
882 | } else | |
70791e56 | 883 | ret = atomicio(vwrite, STDOUT_FILENO, buf, bytes); |
416fd2a8 | 884 | |
2980ea68 | 885 | memset(buf, '\0', bytes); |
886 | xfree(buf); | |
416fd2a8 | 887 | |
2980ea68 | 888 | return ret == bytes ? 0 : 1; |
e9a17296 | 889 | } |
34fee935 | 890 | |
891 | /* | |
892 | * We may attempt to re-seed during mkstemp if we are using the one in the | |
893 | * compat library (via mkstemp -> _gettemp -> arc4random -> seed_rng) so we | |
894 | * need our own seed_rng(). We must also check that we have enough entropy. | |
895 | */ | |
896 | void | |
897 | seed_rng(void) | |
898 | { | |
899 | if (!RAND_status()) | |
900 | fatal("Not enough entropy in RNG"); | |
901 | } |