xref: /freebsd/crypto/openssh/entropy.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
1 /*
2  * Copyright (c) 2001 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 <sys/types.h>
28 #include <sys/wait.h>
29 
30 #ifdef HAVE_SYS_STAT_H
31 # include <sys/stat.h>
32 #endif
33 
34 #ifdef HAVE_FCNTL_H
35 # include <fcntl.h>
36 #endif
37 #include <stdarg.h>
38 #include <unistd.h>
39 
40 #include <openssl/rand.h>
41 #include <openssl/crypto.h>
42 #include <openssl/err.h>
43 
44 #include "ssh.h"
45 #include "misc.h"
46 #include "xmalloc.h"
47 #include "atomicio.h"
48 #include "pathnames.h"
49 #include "log.h"
50 #include "buffer.h"
51 
52 /*
53  * Portable OpenSSH PRNG seeding:
54  * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
55  * /dev/random), then we execute a "ssh-rand-helper" program which
56  * collects entropy and writes it to stdout. The child program must
57  * write at least RANDOM_SEED_SIZE bytes. The child is run with stderr
58  * attached, so error/debugging output should be visible.
59  *
60  * XXX: we should tell the child how many bytes we need.
61  */
62 
63 #ifndef OPENSSL_PRNG_ONLY
64 #define RANDOM_SEED_SIZE 48
65 static uid_t original_uid, original_euid;
66 #endif
67 
68 void
69 seed_rng(void)
70 {
71 #ifndef OPENSSL_PRNG_ONLY
72 	int devnull;
73 	int p[2];
74 	pid_t pid;
75 	int ret;
76 	unsigned char buf[RANDOM_SEED_SIZE];
77 	mysig_t old_sigchld;
78 
79 	if (RAND_status() == 1) {
80 		debug3("RNG is ready, skipping seeding");
81 		return;
82 	}
83 
84 	debug3("Seeding PRNG from %s", SSH_RAND_HELPER);
85 
86 	if ((devnull = open("/dev/null", O_RDWR)) == -1)
87 		fatal("Couldn't open /dev/null: %s", strerror(errno));
88 	if (pipe(p) == -1)
89 		fatal("pipe: %s", strerror(errno));
90 
91 	old_sigchld = signal(SIGCHLD, SIG_DFL);
92 	if ((pid = fork()) == -1)
93 		fatal("Couldn't fork: %s", strerror(errno));
94 	if (pid == 0) {
95 		dup2(devnull, STDIN_FILENO);
96 		dup2(p[1], STDOUT_FILENO);
97 		/* Keep stderr open for errors */
98 		close(p[0]);
99 		close(p[1]);
100 		close(devnull);
101 
102 		if (original_uid != original_euid &&
103 		    ( seteuid(getuid()) == -1 ||
104 		      setuid(original_uid) == -1) ) {
105 			fprintf(stderr, "(rand child) setuid(%li): %s\n",
106 			    (long int)original_uid, strerror(errno));
107 			_exit(1);
108 		}
109 
110 		execl(SSH_RAND_HELPER, "ssh-rand-helper", NULL);
111 		fprintf(stderr, "(rand child) Couldn't exec '%s': %s\n",
112 		    SSH_RAND_HELPER, strerror(errno));
113 		_exit(1);
114 	}
115 
116 	close(devnull);
117 	close(p[1]);
118 
119 	memset(buf, '\0', sizeof(buf));
120 	ret = atomicio(read, p[0], buf, sizeof(buf));
121 	if (ret == -1)
122 		fatal("Couldn't read from ssh-rand-helper: %s",
123 		    strerror(errno));
124 	if (ret != sizeof(buf))
125 		fatal("ssh-rand-helper child produced insufficient data");
126 
127 	close(p[0]);
128 
129 	if (waitpid(pid, &ret, 0) == -1)
130 		fatal("Couldn't wait for ssh-rand-helper completion: %s",
131 		    strerror(errno));
132 	signal(SIGCHLD, old_sigchld);
133 
134 	/* We don't mind if the child exits upon a SIGPIPE */
135 	if (!WIFEXITED(ret) &&
136 	    (!WIFSIGNALED(ret) || WTERMSIG(ret) != SIGPIPE))
137 		fatal("ssh-rand-helper terminated abnormally");
138 	if (WEXITSTATUS(ret) != 0)
139 		fatal("ssh-rand-helper exit with exit status %d", ret);
140 
141 	RAND_add(buf, sizeof(buf), sizeof(buf));
142 	memset(buf, '\0', sizeof(buf));
143 
144 #endif /* OPENSSL_PRNG_ONLY */
145 	if (RAND_status() != 1)
146 		fatal("PRNG is not seeded");
147 }
148 
149 void
150 init_rng(void)
151 {
152 	/*
153 	 * OpenSSL version numbers: MNNFFPPS: major minor fix patch status
154 	 * We match major, minor, fix and status (not patch)
155 	 */
156 	if ((SSLeay() ^ OPENSSL_VERSION_NUMBER) & ~0xff0L)
157 		fatal("OpenSSL version mismatch. Built against %lx, you "
158 		    "have %lx", OPENSSL_VERSION_NUMBER, SSLeay());
159 
160 #ifndef OPENSSL_PRNG_ONLY
161 	original_uid = getuid();
162 	original_euid = geteuid();
163 #endif
164 }
165 
166 #ifndef OPENSSL_PRNG_ONLY
167 void
168 rexec_send_rng_seed(Buffer *m)
169 {
170 	u_char buf[RANDOM_SEED_SIZE];
171 
172 	if (RAND_bytes(buf, sizeof(buf)) <= 0) {
173 		error("Couldn't obtain random bytes (error %ld)",
174 		    ERR_get_error());
175 		buffer_put_string(m, "", 0);
176 	} else
177 		buffer_put_string(m, buf, sizeof(buf));
178 }
179 
180 void
181 rexec_recv_rng_seed(Buffer *m)
182 {
183 	u_char *buf;
184 	u_int len;
185 
186 	buf = buffer_get_string_ret(m, &len);
187 	if (buf != NULL) {
188 		debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
189 		RAND_add(buf, len, len);
190 	}
191 }
192 #endif
193