198937Sdes/*
298937Sdes * Copyright (c) 2001 Damien Miller.  All rights reserved.
398937Sdes *
498937Sdes * Redistribution and use in source and binary forms, with or without
598937Sdes * modification, are permitted provided that the following conditions
698937Sdes * are met:
798937Sdes * 1. Redistributions of source code must retain the above copyright
898937Sdes *    notice, this list of conditions and the following disclaimer.
998937Sdes * 2. Redistributions in binary form must reproduce the above copyright
1098937Sdes *    notice, this list of conditions and the following disclaimer in the
1198937Sdes *    documentation and/or other materials provided with the distribution.
1298937Sdes *
1398937Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1498937Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1598937Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1698937Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1798937Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1898937Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1998937Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2098937Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2198937Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2298937Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2398937Sdes */
2498937Sdes
2598937Sdes#include "includes.h"
2698937Sdes
27162852Sdes#include <sys/types.h>
28226046Sdes#include <sys/socket.h>
29226046Sdes#ifdef HAVE_SYS_UN_H
30226046Sdes# include <sys/un.h>
31162852Sdes#endif
32162852Sdes
33226046Sdes#include <netinet/in.h>
34226046Sdes#include <arpa/inet.h>
35226046Sdes
36226046Sdes#include <errno.h>
37226046Sdes#include <signal.h>
38181111Sdes#include <string.h>
39162852Sdes#include <unistd.h>
40226046Sdes#include <stddef.h> /* for offsetof */
41162852Sdes
4298937Sdes#include <openssl/rand.h>
4398937Sdes#include <openssl/crypto.h>
44157016Sdes#include <openssl/err.h>
4598937Sdes
4698937Sdes#include "ssh.h"
4798937Sdes#include "misc.h"
4898937Sdes#include "xmalloc.h"
4998937Sdes#include "atomicio.h"
5098937Sdes#include "pathnames.h"
5198937Sdes#include "log.h"
52157016Sdes#include "buffer.h"
5398937Sdes
5498937Sdes/*
5598937Sdes * Portable OpenSSH PRNG seeding:
56126274Sdes * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
57226046Sdes * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
58226046Sdes * PRNGd.
5998937Sdes */
60226046Sdes#ifndef OPENSSL_PRNG_ONLY
6198937Sdes
6298937Sdes#define RANDOM_SEED_SIZE 48
6398937Sdes
64226046Sdes/*
65226046Sdes * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
66226046Sdes * listening either on 'tcp_port', or via Unix domain socket at *
67226046Sdes * 'socket_path'.
68226046Sdes * Either a non-zero tcp_port or a non-null socket_path must be
69226046Sdes * supplied.
70226046Sdes * Returns 0 on success, -1 on error
71226046Sdes */
72226046Sdesint
73226046Sdesget_random_bytes_prngd(unsigned char *buf, int len,
74226046Sdes    unsigned short tcp_port, char *socket_path)
7598937Sdes{
76226046Sdes	int fd, addr_len, rval, errors;
77226046Sdes	u_char msg[2];
78226046Sdes	struct sockaddr_storage addr;
79226046Sdes	struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
80226046Sdes	struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
81226046Sdes	mysig_t old_sigpipe;
8298937Sdes
83226046Sdes	/* Sanity checks */
84226046Sdes	if (socket_path == NULL && tcp_port == 0)
85226046Sdes		fatal("You must specify a port or a socket");
86226046Sdes	if (socket_path != NULL &&
87226046Sdes	    strlen(socket_path) >= sizeof(addr_un->sun_path))
88226046Sdes		fatal("Random pool path is too long");
89226046Sdes	if (len <= 0 || len > 255)
90226046Sdes		fatal("Too many bytes (%d) to read from PRNGD", len);
91226046Sdes
92226046Sdes	memset(&addr, '\0', sizeof(addr));
93226046Sdes
94226046Sdes	if (tcp_port != 0) {
95226046Sdes		addr_in->sin_family = AF_INET;
96226046Sdes		addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
97226046Sdes		addr_in->sin_port = htons(tcp_port);
98226046Sdes		addr_len = sizeof(*addr_in);
99226046Sdes	} else {
100226046Sdes		addr_un->sun_family = AF_UNIX;
101226046Sdes		strlcpy(addr_un->sun_path, socket_path,
102226046Sdes		    sizeof(addr_un->sun_path));
103226046Sdes		addr_len = offsetof(struct sockaddr_un, sun_path) +
104226046Sdes		    strlen(socket_path) + 1;
10598937Sdes	}
10698937Sdes
107226046Sdes	old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
10898937Sdes
109226046Sdes	errors = 0;
110226046Sdes	rval = -1;
111226046Sdesreopen:
112226046Sdes	fd = socket(addr.ss_family, SOCK_STREAM, 0);
113226046Sdes	if (fd == -1) {
114226046Sdes		error("Couldn't create socket: %s", strerror(errno));
115226046Sdes		goto done;
116226046Sdes	}
11798937Sdes
118226046Sdes	if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
119226046Sdes		if (tcp_port != 0) {
120226046Sdes			error("Couldn't connect to PRNGD port %d: %s",
121226046Sdes			    tcp_port, strerror(errno));
122226046Sdes		} else {
123226046Sdes			error("Couldn't connect to PRNGD socket \"%s\": %s",
124226046Sdes			    addr_un->sun_path, strerror(errno));
12598937Sdes		}
126226046Sdes		goto done;
12798937Sdes	}
12898937Sdes
129226046Sdes	/* Send blocking read request to PRNGD */
130226046Sdes	msg[0] = 0x02;
131226046Sdes	msg[1] = len;
13298937Sdes
133226046Sdes	if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
134226046Sdes		if (errno == EPIPE && errors < 10) {
135226046Sdes			close(fd);
136226046Sdes			errors++;
137226046Sdes			goto reopen;
138226046Sdes		}
139226046Sdes		error("Couldn't write to PRNGD socket: %s",
14098937Sdes		    strerror(errno));
141226046Sdes		goto done;
142226046Sdes	}
14398937Sdes
144226046Sdes	if (atomicio(read, fd, buf, len) != (size_t)len) {
145226046Sdes		if (errno == EPIPE && errors < 10) {
146226046Sdes			close(fd);
147226046Sdes			errors++;
148226046Sdes			goto reopen;
149226046Sdes		}
150226046Sdes		error("Couldn't read from PRNGD socket: %s",
151149749Sdes		    strerror(errno));
152226046Sdes		goto done;
153226046Sdes	}
15498937Sdes
155226046Sdes	rval = 0;
156226046Sdesdone:
157226046Sdes	mysignal(SIGPIPE, old_sigpipe);
158226046Sdes	if (fd != -1)
159226046Sdes		close(fd);
160226046Sdes	return rval;
16198937Sdes}
16298937Sdes
163226046Sdesstatic int
164226046Sdesseed_from_prngd(unsigned char *buf, size_t bytes)
16598937Sdes{
166226046Sdes#ifdef PRNGD_PORT
167226046Sdes	debug("trying egd/prngd port %d", PRNGD_PORT);
168226046Sdes	if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
169226046Sdes		return 0;
17098937Sdes#endif
171226046Sdes#ifdef PRNGD_SOCKET
172226046Sdes	debug("trying egd/prngd socket %s", PRNGD_SOCKET);
173226046Sdes	if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
174226046Sdes		return 0;
175226046Sdes#endif
176226046Sdes	return -1;
17798937Sdes}
17898937Sdes
179157016Sdesvoid
180157016Sdesrexec_send_rng_seed(Buffer *m)
181157016Sdes{
182157016Sdes	u_char buf[RANDOM_SEED_SIZE];
183157016Sdes
184157016Sdes	if (RAND_bytes(buf, sizeof(buf)) <= 0) {
185157016Sdes		error("Couldn't obtain random bytes (error %ld)",
186157016Sdes		    ERR_get_error());
187157016Sdes		buffer_put_string(m, "", 0);
188157016Sdes	} else
189157016Sdes		buffer_put_string(m, buf, sizeof(buf));
190157016Sdes}
191157016Sdes
192157016Sdesvoid
193157016Sdesrexec_recv_rng_seed(Buffer *m)
194157016Sdes{
195157016Sdes	u_char *buf;
196157016Sdes	u_int len;
197157016Sdes
198157016Sdes	buf = buffer_get_string_ret(m, &len);
199157016Sdes	if (buf != NULL) {
200157016Sdes		debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
201157016Sdes		RAND_add(buf, len, len);
202157016Sdes	}
203157016Sdes}
204226046Sdes#endif /* OPENSSL_PRNG_ONLY */
205226046Sdes
206226046Sdesvoid
207226046Sdesseed_rng(void)
208226046Sdes{
209226046Sdes#ifndef OPENSSL_PRNG_ONLY
210226046Sdes	unsigned char buf[RANDOM_SEED_SIZE];
211157016Sdes#endif
212226046Sdes	/*
213226046Sdes	 * OpenSSL version numbers: MNNFFPPS: major minor fix patch status
214240075Sdes	 * We match major, minor, fix and status (not patch) for <1.0.0.
215240075Sdes	 * After that, we acceptable compatible fix versions (so we
216240075Sdes	 * allow 1.0.1 to work with 1.0.0). Going backwards is only allowed
217240075Sdes	 * within a patch series.
218226046Sdes	 */
219240075Sdes	u_long version_mask = SSLeay() >= 0x1000000f ?  ~0xffff0L : ~0xff0L;
220240075Sdes	if (((SSLeay() ^ OPENSSL_VERSION_NUMBER) & version_mask) ||
221240075Sdes	    (SSLeay() >> 12) < (OPENSSL_VERSION_NUMBER >> 12))
222226046Sdes		fatal("OpenSSL version mismatch. Built against %lx, you "
223226046Sdes		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
224226046Sdes
225226046Sdes#ifndef OPENSSL_PRNG_ONLY
226226046Sdes	if (RAND_status() == 1) {
227226046Sdes		debug3("RNG is ready, skipping seeding");
228226046Sdes		return;
229226046Sdes	}
230226046Sdes
231226046Sdes	if (seed_from_prngd(buf, sizeof(buf)) == -1)
232226046Sdes		fatal("Could not obtain seed from PRNGd");
233226046Sdes	RAND_add(buf, sizeof(buf), sizeof(buf));
234226046Sdes	memset(buf, '\0', sizeof(buf));
235226046Sdes
236226046Sdes#endif /* OPENSSL_PRNG_ONLY */
237226046Sdes	if (RAND_status() != 1)
238226046Sdes		fatal("PRNG is not seeded");
239226046Sdes}
240