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.
29 #include <sys/types.h>
30 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
42 #include <stddef.h> /* for offsetof */
44 #include <openssl/rand.h>
45 #include <openssl/crypto.h>
46 #include <openssl/err.h>
48 #include "openbsd-compat/openssl-compat.h"
54 #include "pathnames.h"
60 * Portable OpenSSH PRNG seeding:
61 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
62 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
65 #ifndef OPENSSL_PRNG_ONLY
67 #define RANDOM_SEED_SIZE 48
70 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
71 * listening either on 'tcp_port', or via Unix domain socket at *
73 * Either a non-zero tcp_port or a non-null socket_path must be
75 * Returns 0 on success, -1 on error
78 get_random_bytes_prngd(unsigned char *buf, int len,
79 unsigned short tcp_port, char *socket_path)
81 int fd, addr_len, rval, errors;
83 struct sockaddr_storage addr;
84 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
85 struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
89 if (socket_path == NULL && tcp_port == 0)
90 fatal("You must specify a port or a socket");
91 if (socket_path != NULL &&
92 strlen(socket_path) >= sizeof(addr_un->sun_path))
93 fatal("Random pool path is too long");
94 if (len <= 0 || len > 255)
95 fatal("Too many bytes (%d) to read from PRNGD", len);
97 memset(&addr, '\0', sizeof(addr));
100 addr_in->sin_family = AF_INET;
101 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
102 addr_in->sin_port = htons(tcp_port);
103 addr_len = sizeof(*addr_in);
105 addr_un->sun_family = AF_UNIX;
106 strlcpy(addr_un->sun_path, socket_path,
107 sizeof(addr_un->sun_path));
108 addr_len = offsetof(struct sockaddr_un, sun_path) +
109 strlen(socket_path) + 1;
112 old_sigpipe = signal(SIGPIPE, SIG_IGN);
117 fd = socket(addr.ss_family, SOCK_STREAM, 0);
119 error("Couldn't create socket: %s", strerror(errno));
123 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
125 error("Couldn't connect to PRNGD port %d: %s",
126 tcp_port, strerror(errno));
128 error("Couldn't connect to PRNGD socket \"%s\": %s",
129 addr_un->sun_path, strerror(errno));
134 /* Send blocking read request to PRNGD */
138 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
139 if (errno == EPIPE && errors < 10) {
144 error("Couldn't write to PRNGD socket: %s",
149 if (atomicio(read, fd, buf, len) != (size_t)len) {
150 if (errno == EPIPE && errors < 10) {
155 error("Couldn't read from PRNGD socket: %s",
162 signal(SIGPIPE, old_sigpipe);
169 seed_from_prngd(unsigned char *buf, size_t bytes)
172 debug("trying egd/prngd port %d", PRNGD_PORT);
173 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
177 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
178 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
185 rexec_send_rng_seed(struct sshbuf *m)
187 u_char buf[RANDOM_SEED_SIZE];
188 size_t len = sizeof(buf);
191 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
192 error("Couldn't obtain random bytes (error %ld)",
196 if ((r = sshbuf_put_string(m, buf, len)) != 0)
197 fatal("%s: buffer error: %s", __func__, ssh_err(r));
198 explicit_bzero(buf, sizeof(buf));
202 rexec_recv_rng_seed(struct sshbuf *m)
208 if ((r = sshbuf_get_string_direct(m, &buf, &len)) != 0
209 fatal("%s: buffer error: %s", __func__, ssh_err(r));
211 debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
212 RAND_add(buf, len, len);
214 #endif /* OPENSSL_PRNG_ONLY */
219 #ifndef OPENSSL_PRNG_ONLY
220 unsigned char buf[RANDOM_SEED_SIZE];
222 if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, SSLeay()))
223 fatal("OpenSSL version mismatch. Built against %lx, you "
224 "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
226 #ifndef OPENSSL_PRNG_ONLY
227 if (RAND_status() == 1) {
228 debug3("RNG is ready, skipping seeding");
232 if (seed_from_prngd(buf, sizeof(buf)) == -1)
233 fatal("Could not obtain seed from PRNGd");
234 RAND_add(buf, sizeof(buf), sizeof(buf));
235 memset(buf, '\0', sizeof(buf));
237 #endif /* OPENSSL_PRNG_ONLY */
238 if (RAND_status() != 1)
239 fatal("PRNG is not seeded");
242 #else /* WITH_OPENSSL */
244 /* Handled in arc4random() */
250 #endif /* WITH_OPENSSL */