blob: 1e9d52ac4605cdec061ff61cd3b4e19aa4c77bcc [file] [log] [blame]
Damien Miller040f3832000-04-03 14:50:43 +10001/*
Damien Miller62116dc2001-12-24 01:41:47 +11002 * Copyright (c) 2001 Damien Miller. All rights reserved.
Damien Miller040f3832000-04-03 14:50:43 +10003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
Damien Miller040f3832000-04-03 14:50:43 +100012 *
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.
23 */
24
25#include "includes.h"
26
Darren Tuckerd82cbcb2006-03-16 07:21:35 +110027#include <sys/types.h>
Damien Millerf22019b2011-05-05 13:48:37 +100028#include <sys/socket.h>
29#ifdef HAVE_SYS_UN_H
30# include <sys/un.h>
Darren Tuckere0e4aad2006-07-11 19:01:51 +100031#endif
32
Damien Millerf22019b2011-05-05 13:48:37 +100033#include <netinet/in.h>
34#include <arpa/inet.h>
35
36#include <errno.h>
Darren Tucker23dd6582006-09-28 19:40:20 +100037#include <signal.h>
Damien Millerf22019b2011-05-05 13:48:37 +100038#include <string.h>
Darren Tuckera8d51ee2007-03-13 07:35:38 +110039#include <unistd.h>
Damien Millerf22019b2011-05-05 13:48:37 +100040#include <stddef.h> /* for offsetof */
Darren Tuckere0e4aad2006-07-11 19:01:51 +100041
Damien Miller5f056372000-04-16 12:31:48 +100042#include <openssl/rand.h>
Damien Miller767c7fc2001-02-27 09:20:57 +110043#include <openssl/crypto.h>
Darren Tuckerc6f82192005-09-27 22:46:32 +100044#include <openssl/err.h>
Damien Miller040f3832000-04-03 14:50:43 +100045
Damien Miller86687062014-07-02 15:28:02 +100046#include "openbsd-compat/openssl-compat.h"
47
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "ssh.h"
Damien Millera1072a82001-02-18 15:28:11 +110049#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "xmalloc.h"
51#include "atomicio.h"
Ben Lindstromcb577332001-01-22 21:06:19 +000052#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "log.h"
Darren Tuckerc6f82192005-09-27 22:46:32 +100054#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055
Damien Miller14c12cb2000-06-07 22:20:23 +100056/*
Damien Miller62116dc2001-12-24 01:41:47 +110057 * Portable OpenSSH PRNG seeding:
Damien Millera8e06ce2003-11-21 23:48:55 +110058 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
Damien Millerf22019b2011-05-05 13:48:37 +100059 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
60 * PRNGd.
Damien Miller14c12cb2000-06-07 22:20:23 +100061 */
Damien Miller6c21c512002-01-22 21:57:53 +110062#ifndef OPENSSL_PRNG_ONLY
Damien Millerf22019b2011-05-05 13:48:37 +100063
Damien Miller62116dc2001-12-24 01:41:47 +110064#define RANDOM_SEED_SIZE 48
Damien Miller62116dc2001-12-24 01:41:47 +110065
Damien Millerf22019b2011-05-05 13:48:37 +100066/*
67 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
68 * listening either on 'tcp_port', or via Unix domain socket at *
69 * 'socket_path'.
70 * Either a non-zero tcp_port or a non-null socket_path must be
71 * supplied.
72 * Returns 0 on success, -1 on error
73 */
74int
75get_random_bytes_prngd(unsigned char *buf, int len,
76 unsigned short tcp_port, char *socket_path)
Damien Miller14c12cb2000-06-07 22:20:23 +100077{
Damien Millerf22019b2011-05-05 13:48:37 +100078 int fd, addr_len, rval, errors;
79 u_char msg[2];
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;
83 mysig_t old_sigpipe;
Kevin Stevesef4eea92001-02-05 12:42:17 +000084
Damien Millerf22019b2011-05-05 13:48:37 +100085 /* Sanity checks */
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);
93
94 memset(&addr, '\0', sizeof(addr));
95
96 if (tcp_port != 0) {
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);
101 } else {
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;
Damien Miller08006472000-06-26 13:55:31 +1000107 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000108
Damien Millerf22019b2011-05-05 13:48:37 +1000109 old_sigpipe = mysignal(SIGPIPE, SIG_IGN);
Damien Miller62116dc2001-12-24 01:41:47 +1100110
Damien Millerf22019b2011-05-05 13:48:37 +1000111 errors = 0;
112 rval = -1;
113reopen:
114 fd = socket(addr.ss_family, SOCK_STREAM, 0);
115 if (fd == -1) {
116 error("Couldn't create socket: %s", strerror(errno));
117 goto done;
118 }
Damien Miller62116dc2001-12-24 01:41:47 +1100119
Damien Millerf22019b2011-05-05 13:48:37 +1000120 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
121 if (tcp_port != 0) {
122 error("Couldn't connect to PRNGD port %d: %s",
123 tcp_port, strerror(errno));
124 } else {
125 error("Couldn't connect to PRNGD socket \"%s\": %s",
126 addr_un->sun_path, strerror(errno));
Damien Miller62116dc2001-12-24 01:41:47 +1100127 }
Damien Millerf22019b2011-05-05 13:48:37 +1000128 goto done;
Damien Miller62116dc2001-12-24 01:41:47 +1100129 }
130
Damien Millerf22019b2011-05-05 13:48:37 +1000131 /* Send blocking read request to PRNGD */
132 msg[0] = 0x02;
133 msg[1] = len;
Damien Miller62116dc2001-12-24 01:41:47 +1100134
Damien Millerf22019b2011-05-05 13:48:37 +1000135 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
136 if (errno == EPIPE && errors < 10) {
137 close(fd);
138 errors++;
139 goto reopen;
140 }
141 error("Couldn't write to PRNGD socket: %s",
Damien Miller62116dc2001-12-24 01:41:47 +1100142 strerror(errno));
Damien Millerf22019b2011-05-05 13:48:37 +1000143 goto done;
144 }
Damien Miller62116dc2001-12-24 01:41:47 +1100145
Damien Millerf22019b2011-05-05 13:48:37 +1000146 if (atomicio(read, fd, buf, len) != (size_t)len) {
147 if (errno == EPIPE && errors < 10) {
148 close(fd);
149 errors++;
150 goto reopen;
151 }
152 error("Couldn't read from PRNGD socket: %s",
Damien Millerb6f72f52005-07-17 17:26:43 +1000153 strerror(errno));
Damien Millerf22019b2011-05-05 13:48:37 +1000154 goto done;
155 }
Damien Miller62116dc2001-12-24 01:41:47 +1100156
Damien Millerf22019b2011-05-05 13:48:37 +1000157 rval = 0;
158done:
159 mysignal(SIGPIPE, old_sigpipe);
160 if (fd != -1)
161 close(fd);
162 return rval;
Damien Miller14c12cb2000-06-07 22:20:23 +1000163}
164
Damien Millerf22019b2011-05-05 13:48:37 +1000165static int
166seed_from_prngd(unsigned char *buf, size_t bytes)
Damien Miller767c7fc2001-02-27 09:20:57 +1100167{
Damien Millerf22019b2011-05-05 13:48:37 +1000168#ifdef PRNGD_PORT
169 debug("trying egd/prngd port %d", PRNGD_PORT);
170 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
171 return 0;
Damien Miller6c21c512002-01-22 21:57:53 +1100172#endif
Damien Millerf22019b2011-05-05 13:48:37 +1000173#ifdef PRNGD_SOCKET
174 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
175 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
176 return 0;
177#endif
178 return -1;
Damien Millerf9b625c2000-07-09 22:42:32 +1000179}
Damien Miller6c21c512002-01-22 21:57:53 +1100180
Darren Tuckerc6f82192005-09-27 22:46:32 +1000181void
182rexec_send_rng_seed(Buffer *m)
183{
184 u_char buf[RANDOM_SEED_SIZE];
185
186 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
187 error("Couldn't obtain random bytes (error %ld)",
188 ERR_get_error());
189 buffer_put_string(m, "", 0);
190 } else
191 buffer_put_string(m, buf, sizeof(buf));
192}
193
194void
195rexec_recv_rng_seed(Buffer *m)
196{
Darren Tucker46e7ba52005-09-28 08:26:30 +1000197 u_char *buf;
Darren Tuckerc6f82192005-09-27 22:46:32 +1000198 u_int len;
199
200 buf = buffer_get_string_ret(m, &len);
201 if (buf != NULL) {
202 debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len);
203 RAND_add(buf, len, len);
204 }
205}
Damien Millerf22019b2011-05-05 13:48:37 +1000206#endif /* OPENSSL_PRNG_ONLY */
207
208void
209seed_rng(void)
210{
211#ifndef OPENSSL_PRNG_ONLY
212 unsigned char buf[RANDOM_SEED_SIZE];
Darren Tuckerc6f82192005-09-27 22:46:32 +1000213#endif
Darren Tucker316fac62014-06-17 23:06:07 +1000214 if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, SSLeay()))
Damien Millerf22019b2011-05-05 13:48:37 +1000215 fatal("OpenSSL version mismatch. Built against %lx, you "
216 "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay());
217
218#ifndef OPENSSL_PRNG_ONLY
219 if (RAND_status() == 1) {
220 debug3("RNG is ready, skipping seeding");
221 return;
222 }
223
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));
228
229#endif /* OPENSSL_PRNG_ONLY */
230 if (RAND_status() != 1)
231 fatal("PRNG is not seeded");
232}