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 #include <sys/types.h>
28 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
40 #include <stddef.h> /* for offsetof */
42 #include <openssl/rand.h>
43 #include <openssl/crypto.h>
44 #include <openssl/err.h>
46 #include "openbsd-compat/openssl-compat.h"
52 #include "pathnames.h"
57 * Portable OpenSSH PRNG seeding:
58 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
59 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
62 #ifndef OPENSSL_PRNG_ONLY
64 #define RANDOM_SEED_SIZE 48
67 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
68 * listening either on 'tcp_port', or via Unix domain socket at *
70 * Either a non-zero tcp_port or a non-null socket_path must be
72 * Returns 0 on success, -1 on error
75 get_random_bytes_prngd(unsigned char *buf, int len,
76 unsigned short tcp_port, char *socket_path)
78 int fd, addr_len, rval, errors;
80 struct sockaddr_storage addr;
81 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
82 struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
86 if (socket_path == NULL && tcp_port == 0)
87 fatal("You must specify a port or a socket");
88 if (socket_path != NULL &&
89 strlen(socket_path) >= sizeof(addr_un->sun_path))
90 fatal("Random pool path is too long");
91 if (len <= 0 || len > 255)
92 fatal("Too many bytes (%d) to read from PRNGD", len);
94 memset(&addr, '\0', sizeof(addr));
97 addr_in->sin_family = AF_INET;
98 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
99 addr_in->sin_port = htons(tcp_port);
100 addr_len = sizeof(*addr_in);
102 addr_un->sun_family = AF_UNIX;
103 strlcpy(addr_un->sun_path, socket_path,
104 sizeof(addr_un->sun_path));
105 addr_len = offsetof(struct sockaddr_un, sun_path) +
106 strlen(socket_path) + 1;
109 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
114 fd = socket(addr.ss_family, SOCK_STREAM, 0);
116 error("Couldn't create socket: %s", strerror(errno));
120 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
122 error("Couldn't connect to PRNGD port %d: %s",
123 tcp_port, strerror(errno));
125 error("Couldn't connect to PRNGD socket \"%s\": %s",
126 addr_un->sun_path, strerror(errno));
131 /* Send blocking read request to PRNGD */
135 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
136 if (errno == EPIPE && errors < 10) {
141 error("Couldn't write to PRNGD socket: %s",
146 if (atomicio(read, fd, buf, len) != (size_t)len) {
147 if (errno == EPIPE && errors < 10) {
152 error("Couldn't read from PRNGD socket: %s",
159 mysignal(SIGPIPE, old_sigpipe);
166 seed_from_prngd(unsigned char *buf, size_t bytes)
169 debug("trying egd/prngd port %d", PRNGD_PORT);
170 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
174 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
175 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
182 rexec_send_rng_seed(Buffer *m)
184 u_char buf[RANDOM_SEED_SIZE];
186 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
187 error("Couldn't obtain random bytes (error %ld)",
189 buffer_put_string(m, "", 0);
191 buffer_put_string(m, buf, sizeof(buf));
195 rexec_recv_rng_seed(Buffer *m)
200 buf = buffer_get_string_ret(m, &len);
202 debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
203 RAND_add(buf, len, len);
206 #endif /* OPENSSL_PRNG_ONLY */
211 #ifndef OPENSSL_PRNG_ONLY
212 unsigned char buf[RANDOM_SEED_SIZE];
214 if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, SSLeay()))
215 fatal("OpenSSL version mismatch. Built against %lx, you "
216 "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
218 #ifndef OPENSSL_PRNG_ONLY
219 if (RAND_status() == 1) {
220 debug3("RNG is ready, skipping seeding");
224 if (seed_from_prngd(buf, sizeof(buf)) == -1)
225 fatal("Could not obtain seed from PRNGd");
226 RAND_add(buf, sizeof(buf), sizeof(buf));
227 memset(buf, '\0', sizeof(buf));
229 #endif /* OPENSSL_PRNG_ONLY */
230 if (RAND_status() != 1)
231 fatal("PRNG is not seeded");