2 * Copyright (c) 2001 Damien Miller. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #define RANDOM_SEED_SIZE 48
31 #include <sys/types.h>
32 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
45 #include <stddef.h> /* for offsetof */
47 #include <openssl/rand.h>
48 #include <openssl/crypto.h>
49 #include <openssl/err.h>
51 #include "openbsd-compat/openssl-compat.h"
57 #include "pathnames.h"
63 * Portable OpenSSH PRNG seeding:
64 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
65 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
68 #ifndef OPENSSL_PRNG_ONLY
71 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
72 * listening either on 'tcp_port', or via Unix domain socket at *
74 * Either a non-zero tcp_port or a non-null socket_path must be
76 * Returns 0 on success, -1 on error
79 get_random_bytes_prngd(unsigned char *buf, int len,
80 unsigned short tcp_port, char *socket_path)
82 int fd, addr_len, rval, errors;
84 struct sockaddr_storage addr;
85 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
86 struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
90 if (socket_path == NULL && tcp_port == 0)
91 fatal("You must specify a port or a socket");
92 if (socket_path != NULL &&
93 strlen(socket_path) >= sizeof(addr_un->sun_path))
94 fatal("Random pool path is too long");
95 if (len <= 0 || len > 255)
96 fatal("Too many bytes (%d) to read from PRNGD", len);
98 memset(&addr, '\0', sizeof(addr));
101 addr_in->sin_family = AF_INET;
102 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
103 addr_in->sin_port = htons(tcp_port);
104 addr_len = sizeof(*addr_in);
106 addr_un->sun_family = AF_UNIX;
107 strlcpy(addr_un->sun_path, socket_path,
108 sizeof(addr_un->sun_path));
109 addr_len = offsetof(struct sockaddr_un, sun_path) +
110 strlen(socket_path) + 1;
113 old_sigpipe = ssh_signal(SIGPIPE, SIG_IGN);
118 fd = socket(addr.ss_family, SOCK_STREAM, 0);
120 error("Couldn't create socket: %s", strerror(errno));
124 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
126 error("Couldn't connect to PRNGD port %d: %s",
127 tcp_port, strerror(errno));
129 error("Couldn't connect to PRNGD socket \"%s\": %s",
130 addr_un->sun_path, strerror(errno));
135 /* Send blocking read request to PRNGD */
139 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
140 if (errno == EPIPE && errors < 10) {
145 error("Couldn't write to PRNGD socket: %s",
150 if (atomicio(read, fd, buf, len) != (size_t)len) {
151 if (errno == EPIPE && errors < 10) {
156 error("Couldn't read from PRNGD socket: %s",
163 ssh_signal(SIGPIPE, old_sigpipe);
170 seed_from_prngd(unsigned char *buf, size_t bytes)
173 debug("trying egd/prngd port %d", PRNGD_PORT);
174 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
178 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
179 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
186 rexec_send_rng_seed(struct sshbuf *m)
188 u_char buf[RANDOM_SEED_SIZE];
189 size_t len = sizeof(buf);
192 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
193 error("Couldn't obtain random bytes (error %ld)",
197 if ((r = sshbuf_put_string(m, buf, len)) != 0)
198 fatal("%s: buffer error: %s", __func__, ssh_err(r));
199 explicit_bzero(buf, sizeof(buf));
203 rexec_recv_rng_seed(struct sshbuf *m)
205 const u_char *buf = NULL;
209 if ((r = sshbuf_get_string_direct(m, &buf, &len)) != 0)
210 fatal("%s: buffer error: %s", __func__, ssh_err(r));
212 debug3("rexec_recv_rng_seed: seeding rng with %lu bytes",
214 RAND_add(buf, len, len);
216 #endif /* OPENSSL_PRNG_ONLY */
221 unsigned char buf[RANDOM_SEED_SIZE];
223 /* Initialise libcrypto */
224 ssh_libcrypto_init();
226 if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER,
227 OpenSSL_version_num()))
228 fatal("OpenSSL version mismatch. Built against %lx, you "
229 "have %lx", (u_long)OPENSSL_VERSION_NUMBER,
230 OpenSSL_version_num());
232 #ifndef OPENSSL_PRNG_ONLY
233 if (RAND_status() == 1)
234 debug3("RNG is ready, skipping seeding");
236 if (seed_from_prngd(buf, sizeof(buf)) == -1)
237 fatal("Could not obtain seed from PRNGd");
238 RAND_add(buf, sizeof(buf), sizeof(buf));
240 #endif /* OPENSSL_PRNG_ONLY */
242 if (RAND_status() != 1)
243 fatal("PRNG is not seeded");
245 /* Ensure arc4random() is primed */
246 arc4random_buf(buf, sizeof(buf));
247 explicit_bzero(buf, sizeof(buf));
250 #else /* WITH_OPENSSL */
255 /* Actual initialisation is handled in arc4random() */
259 unsigned char buf[RANDOM_SEED_SIZE];
261 /* Ensure arc4random() is primed */
262 arc4random_buf(buf, sizeof(buf));
263 explicit_bzero(buf, sizeof(buf));
266 #endif /* WITH_OPENSSL */