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>
50 #include "pathnames.h"
55 * Portable OpenSSH PRNG seeding:
56 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
57 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
60 #ifndef OPENSSL_PRNG_ONLY
62 #define RANDOM_SEED_SIZE 48
65 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
66 * listening either on 'tcp_port', or via Unix domain socket at *
68 * Either a non-zero tcp_port or a non-null socket_path must be
70 * Returns 0 on success, -1 on error
73 get_random_bytes_prngd(unsigned char *buf, int len,
74 unsigned short tcp_port, char *socket_path)
76 int fd, addr_len, rval, errors;
78 struct sockaddr_storage addr;
79 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
80 struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
84 if (socket_path == NULL && tcp_port == 0)
85 fatal("You must specify a port or a socket");
86 if (socket_path != NULL &&
87 strlen(socket_path) >= sizeof(addr_un->sun_path))
88 fatal("Random pool path is too long");
89 if (len <= 0 || len > 255)
90 fatal("Too many bytes (%d) to read from PRNGD", len);
92 memset(&addr, '\0', sizeof(addr));
95 addr_in->sin_family = AF_INET;
96 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
97 addr_in->sin_port = htons(tcp_port);
98 addr_len = sizeof(*addr_in);
100 addr_un->sun_family = AF_UNIX;
101 strlcpy(addr_un->sun_path, socket_path,
102 sizeof(addr_un->sun_path));
103 addr_len = offsetof(struct sockaddr_un, sun_path) +
104 strlen(socket_path) + 1;
107 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
112 fd = socket(addr.ss_family, SOCK_STREAM, 0);
114 error("Couldn't create socket: %s", strerror(errno));
118 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
120 error("Couldn't connect to PRNGD port %d: %s",
121 tcp_port, strerror(errno));
123 error("Couldn't connect to PRNGD socket \"%s\": %s",
124 addr_un->sun_path, strerror(errno));
129 /* Send blocking read request to PRNGD */
133 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
134 if (errno == EPIPE && errors < 10) {
139 error("Couldn't write to PRNGD socket: %s",
144 if (atomicio(read, fd, buf, len) != (size_t)len) {
145 if (errno == EPIPE && errors < 10) {
150 error("Couldn't read from PRNGD socket: %s",
157 mysignal(SIGPIPE, old_sigpipe);
164 seed_from_prngd(unsigned char *buf, size_t bytes)
167 debug("trying egd/prngd port %d", PRNGD_PORT);
168 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
172 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
173 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
180 rexec_send_rng_seed(Buffer *m)
182 u_char buf[RANDOM_SEED_SIZE];
184 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
185 error("Couldn't obtain random bytes (error %ld)",
187 buffer_put_string(m, "", 0);
189 buffer_put_string(m, buf, sizeof(buf));
193 rexec_recv_rng_seed(Buffer *m)
198 buf = buffer_get_string_ret(m, &len);
200 debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
201 RAND_add(buf, len, len);
204 #endif /* OPENSSL_PRNG_ONLY */
209 #ifndef OPENSSL_PRNG_ONLY
210 unsigned char buf[RANDOM_SEED_SIZE];
213 * OpenSSL version numbers: MNNFFPPS: major minor fix patch status
214 * We match major, minor, fix and status (not patch)
216 if ((SSLeay() ^ OPENSSL_VERSION_NUMBER) & ~0xff0L)
217 fatal("OpenSSL version mismatch. Built against %lx, you "
218 "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
220 #ifndef OPENSSL_PRNG_ONLY
221 if (RAND_status() == 1) {
222 debug3("RNG is ready, skipping seeding");
226 if (seed_from_prngd(buf, sizeof(buf)) == -1)
227 fatal("Could not obtain seed from PRNGd");
228 RAND_add(buf, sizeof(buf), sizeof(buf));
229 memset(buf, '\0', sizeof(buf));
231 #endif /* OPENSSL_PRNG_ONLY */
232 if (RAND_status() != 1)
233 fatal("PRNG is not seeded");