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
27295367Sdes#ifdef WITH_OPENSSL
28295367Sdes
29162852Sdes#include <sys/types.h>
30226046Sdes#include <sys/socket.h>
31226046Sdes#ifdef HAVE_SYS_UN_H
32226046Sdes# include <sys/un.h>
33162852Sdes#endif
34162852Sdes
35226046Sdes#include <netinet/in.h>
36226046Sdes#include <arpa/inet.h>
37226046Sdes
38226046Sdes#include <errno.h>
39226046Sdes#include <signal.h>
40181111Sdes#include <string.h>
41162852Sdes#include <unistd.h>
42226046Sdes#include <stddef.h> /* for offsetof */
43162852Sdes
4498937Sdes#include <openssl/rand.h>
4598937Sdes#include <openssl/crypto.h>
46157016Sdes#include <openssl/err.h>
4798937Sdes
48295367Sdes#include "openbsd-compat/openssl-compat.h"
49295367Sdes
5098937Sdes#include "ssh.h"
5198937Sdes#include "misc.h"
5298937Sdes#include "xmalloc.h"
5398937Sdes#include "atomicio.h"
5498937Sdes#include "pathnames.h"
5598937Sdes#include "log.h"
56157016Sdes#include "buffer.h"
5798937Sdes
5898937Sdes/*
5998937Sdes * Portable OpenSSH PRNG seeding:
60126274Sdes * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
61226046Sdes * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
62226046Sdes * PRNGd.
6398937Sdes */
64226046Sdes#ifndef OPENSSL_PRNG_ONLY
6598937Sdes
6698937Sdes#define RANDOM_SEED_SIZE 48
6798937Sdes
68226046Sdes/*
69226046Sdes * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
70226046Sdes * listening either on 'tcp_port', or via Unix domain socket at *
71226046Sdes * 'socket_path'.
72226046Sdes * Either a non-zero tcp_port or a non-null socket_path must be
73226046Sdes * supplied.
74226046Sdes * Returns 0 on success, -1 on error
75226046Sdes */
76226046Sdesint
77226046Sdesget_random_bytes_prngd(unsigned char *buf, int len,
78226046Sdes    unsigned short tcp_port, char *socket_path)
7998937Sdes{
80226046Sdes	int fd, addr_len, rval, errors;
81226046Sdes	u_char msg[2];
82226046Sdes	struct sockaddr_storage addr;
83226046Sdes	struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
84226046Sdes	struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
85226046Sdes	mysig_t old_sigpipe;
8698937Sdes
87226046Sdes	/* Sanity checks */
88226046Sdes	if (socket_path == NULL && tcp_port == 0)
89226046Sdes		fatal("You must specify a port or a socket");
90226046Sdes	if (socket_path != NULL &&
91226046Sdes	    strlen(socket_path) >= sizeof(addr_un->sun_path))
92226046Sdes		fatal("Random pool path is too long");
93226046Sdes	if (len <= 0 || len > 255)
94226046Sdes		fatal("Too many bytes (%d) to read from PRNGD", len);
95226046Sdes
96226046Sdes	memset(&addr, '\0', sizeof(addr));
97226046Sdes
98226046Sdes	if (tcp_port != 0) {
99226046Sdes		addr_in->sin_family = AF_INET;
100226046Sdes		addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
101226046Sdes		addr_in->sin_port = htons(tcp_port);
102226046Sdes		addr_len = sizeof(*addr_in);
103226046Sdes	} else {
104226046Sdes		addr_un->sun_family = AF_UNIX;
105226046Sdes		strlcpy(addr_un->sun_path, socket_path,
106226046Sdes		    sizeof(addr_un->sun_path));
107226046Sdes		addr_len = offsetof(struct sockaddr_un, sun_path) +
108226046Sdes		    strlen(socket_path) + 1;
10998937Sdes	}
11098937Sdes
111226046Sdes	old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
11298937Sdes
113226046Sdes	errors = 0;
114226046Sdes	rval = -1;
115226046Sdesreopen:
116226046Sdes	fd = socket(addr.ss_family, SOCK_STREAM, 0);
117226046Sdes	if (fd == -1) {
118226046Sdes		error("Couldn't create socket: %s", strerror(errno));
119226046Sdes		goto done;
120226046Sdes	}
12198937Sdes
122226046Sdes	if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
123226046Sdes		if (tcp_port != 0) {
124226046Sdes			error("Couldn't connect to PRNGD port %d: %s",
125226046Sdes			    tcp_port, strerror(errno));
126226046Sdes		} else {
127226046Sdes			error("Couldn't connect to PRNGD socket \"%s\": %s",
128226046Sdes			    addr_un->sun_path, strerror(errno));
12998937Sdes		}
130226046Sdes		goto done;
13198937Sdes	}
13298937Sdes
133226046Sdes	/* Send blocking read request to PRNGD */
134226046Sdes	msg[0] = 0x02;
135226046Sdes	msg[1] = len;
13698937Sdes
137226046Sdes	if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
138226046Sdes		if (errno == EPIPE && errors < 10) {
139226046Sdes			close(fd);
140226046Sdes			errors++;
141226046Sdes			goto reopen;
142226046Sdes		}
143226046Sdes		error("Couldn't write to PRNGD socket: %s",
14498937Sdes		    strerror(errno));
145226046Sdes		goto done;
146226046Sdes	}
14798937Sdes
148226046Sdes	if (atomicio(read, fd, buf, len) != (size_t)len) {
149226046Sdes		if (errno == EPIPE && errors < 10) {
150226046Sdes			close(fd);
151226046Sdes			errors++;
152226046Sdes			goto reopen;
153226046Sdes		}
154226046Sdes		error("Couldn't read from PRNGD socket: %s",
155149749Sdes		    strerror(errno));
156226046Sdes		goto done;
157226046Sdes	}
15898937Sdes
159226046Sdes	rval = 0;
160226046Sdesdone:
161226046Sdes	mysignal(SIGPIPE, old_sigpipe);
162226046Sdes	if (fd != -1)
163226046Sdes		close(fd);
164226046Sdes	return rval;
16598937Sdes}
16698937Sdes
167226046Sdesstatic int
168226046Sdesseed_from_prngd(unsigned char *buf, size_t bytes)
16998937Sdes{
170226046Sdes#ifdef PRNGD_PORT
171226046Sdes	debug("trying egd/prngd port %d", PRNGD_PORT);
172226046Sdes	if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
173226046Sdes		return 0;
17498937Sdes#endif
175226046Sdes#ifdef PRNGD_SOCKET
176226046Sdes	debug("trying egd/prngd socket %s", PRNGD_SOCKET);
177226046Sdes	if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
178226046Sdes		return 0;
179226046Sdes#endif
180226046Sdes	return -1;
18198937Sdes}
18298937Sdes
183157016Sdesvoid
184157016Sdesrexec_send_rng_seed(Buffer *m)
185157016Sdes{
186157016Sdes	u_char buf[RANDOM_SEED_SIZE];
187157016Sdes
188157016Sdes	if (RAND_bytes(buf, sizeof(buf)) <= 0) {
189157016Sdes		error("Couldn't obtain random bytes (error %ld)",
190157016Sdes		    ERR_get_error());
191157016Sdes		buffer_put_string(m, "", 0);
192157016Sdes	} else
193157016Sdes		buffer_put_string(m, buf, sizeof(buf));
194157016Sdes}
195157016Sdes
196157016Sdesvoid
197157016Sdesrexec_recv_rng_seed(Buffer *m)
198157016Sdes{
199157016Sdes	u_char *buf;
200157016Sdes	u_int len;
201157016Sdes
202157016Sdes	buf = buffer_get_string_ret(m, &len);
203157016Sdes	if (buf != NULL) {
204157016Sdes		debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
205157016Sdes		RAND_add(buf, len, len);
206157016Sdes	}
207157016Sdes}
208226046Sdes#endif /* OPENSSL_PRNG_ONLY */
209226046Sdes
210226046Sdesvoid
211226046Sdesseed_rng(void)
212226046Sdes{
213226046Sdes#ifndef OPENSSL_PRNG_ONLY
214226046Sdes	unsigned char buf[RANDOM_SEED_SIZE];
215157016Sdes#endif
216295367Sdes	if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, SSLeay()))
217226046Sdes		fatal("OpenSSL version mismatch. Built against %lx, you "
218226046Sdes		    "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
219226046Sdes
220226046Sdes#ifndef OPENSSL_PRNG_ONLY
221226046Sdes	if (RAND_status() == 1) {
222226046Sdes		debug3("RNG is ready, skipping seeding");
223226046Sdes		return;
224226046Sdes	}
225226046Sdes
226226046Sdes	if (seed_from_prngd(buf, sizeof(buf)) == -1)
227226046Sdes		fatal("Could not obtain seed from PRNGd");
228226046Sdes	RAND_add(buf, sizeof(buf), sizeof(buf));
229226046Sdes	memset(buf, '\0', sizeof(buf));
230226046Sdes
231226046Sdes#endif /* OPENSSL_PRNG_ONLY */
232226046Sdes	if (RAND_status() != 1)
233226046Sdes		fatal("PRNG is not seeded");
234226046Sdes}
235295367Sdes
236295367Sdes#else /* WITH_OPENSSL */
237295367Sdes
238295367Sdes/* Handled in arc4random() */
239295367Sdesvoid
240295367Sdesseed_rng(void)
241295367Sdes{
242295367Sdes}
243295367Sdes
244295367Sdes#endif /* WITH_OPENSSL */
245