blob: 5de68016b02dae8a68bfa53c289144aa16772453 [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 Tuckerc721d582018-11-23 14:11:20 +110027#define RANDOM_SEED_SIZE 48
28
Damien Miller72ef7c12015-01-15 02:21:31 +110029#ifdef WITH_OPENSSL
30
Darren Tuckerd82cbcb2006-03-16 07:21:35 +110031#include <sys/types.h>
Damien Millerf22019b2011-05-05 13:48:37 +100032#include <sys/socket.h>
33#ifdef HAVE_SYS_UN_H
34# include <sys/un.h>
Darren Tuckere0e4aad2006-07-11 19:01:51 +100035#endif
36
Damien Millerf22019b2011-05-05 13:48:37 +100037#include <netinet/in.h>
38#include <arpa/inet.h>
39
40#include <errno.h>
Darren Tucker23dd6582006-09-28 19:40:20 +100041#include <signal.h>
Darren Tucker9634ffb2019-07-23 22:25:44 +100042#include <stdlib.h>
Damien Millerf22019b2011-05-05 13:48:37 +100043#include <string.h>
Darren Tuckera8d51ee2007-03-13 07:35:38 +110044#include <unistd.h>
Damien Millerf22019b2011-05-05 13:48:37 +100045#include <stddef.h> /* for offsetof */
Darren Tuckere0e4aad2006-07-11 19:01:51 +100046
Damien Miller5f056372000-04-16 12:31:48 +100047#include <openssl/rand.h>
Damien Miller767c7fc2001-02-27 09:20:57 +110048#include <openssl/crypto.h>
Darren Tuckerc6f82192005-09-27 22:46:32 +100049#include <openssl/err.h>
Damien Miller040f3832000-04-03 14:50:43 +100050
Damien Miller86687062014-07-02 15:28:02 +100051#include "openbsd-compat/openssl-compat.h"
52
Ben Lindstrom226cfa02001-01-22 05:34:40 +000053#include "ssh.h"
Damien Millera1072a82001-02-18 15:28:11 +110054#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "xmalloc.h"
56#include "atomicio.h"
Ben Lindstromcb577332001-01-22 21:06:19 +000057#include "pathnames.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
Damien Miller120a1ec2018-07-10 19:39:52 +100059#include "sshbuf.h"
60#include "ssherr.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000061
Damien Miller14c12cb2000-06-07 22:20:23 +100062/*
Damien Miller62116dc2001-12-24 01:41:47 +110063 * Portable OpenSSH PRNG seeding:
Damien Millera8e06ce2003-11-21 23:48:55 +110064 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
Damien Millerf22019b2011-05-05 13:48:37 +100065 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
66 * PRNGd.
Damien Miller14c12cb2000-06-07 22:20:23 +100067 */
Damien Miller6c21c512002-01-22 21:57:53 +110068#ifndef OPENSSL_PRNG_ONLY
Damien Millerf22019b2011-05-05 13:48:37 +100069
Damien Millerf22019b2011-05-05 13:48:37 +100070/*
71 * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
72 * listening either on 'tcp_port', or via Unix domain socket at *
73 * 'socket_path'.
74 * Either a non-zero tcp_port or a non-null socket_path must be
75 * supplied.
76 * Returns 0 on success, -1 on error
77 */
78int
79get_random_bytes_prngd(unsigned char *buf, int len,
80 unsigned short tcp_port, char *socket_path)
Damien Miller14c12cb2000-06-07 22:20:23 +100081{
Damien Millerf22019b2011-05-05 13:48:37 +100082 int fd, addr_len, rval, errors;
83 u_char msg[2];
84 struct sockaddr_storage addr;
85 struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
86 struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
87 mysig_t old_sigpipe;
Kevin Stevesef4eea92001-02-05 12:42:17 +000088
Damien Millerf22019b2011-05-05 13:48:37 +100089 /* Sanity checks */
90 if (socket_path == NULL && tcp_port == 0)
91 fatal("You must specify a port or a socket");
92 if (socket_path != NULL &&
93 strlen(socket_path) >= sizeof(addr_un->sun_path))
94 fatal("Random pool path is too long");
95 if (len <= 0 || len > 255)
96 fatal("Too many bytes (%d) to read from PRNGD", len);
97
98 memset(&addr, '\0', sizeof(addr));
99
100 if (tcp_port != 0) {
101 addr_in->sin_family = AF_INET;
102 addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
103 addr_in->sin_port = htons(tcp_port);
104 addr_len = sizeof(*addr_in);
105 } else {
106 addr_un->sun_family = AF_UNIX;
107 strlcpy(addr_un->sun_path, socket_path,
108 sizeof(addr_un->sun_path));
109 addr_len = offsetof(struct sockaddr_un, sun_path) +
110 strlen(socket_path) + 1;
Damien Miller08006472000-06-26 13:55:31 +1000111 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000112
Darren Tucker389125b2018-02-15 21:43:01 +1100113 old_sigpipe = signal(SIGPIPE, SIG_IGN);
Damien Miller62116dc2001-12-24 01:41:47 +1100114
Damien Millerf22019b2011-05-05 13:48:37 +1000115 errors = 0;
116 rval = -1;
117reopen:
118 fd = socket(addr.ss_family, SOCK_STREAM, 0);
119 if (fd == -1) {
120 error("Couldn't create socket: %s", strerror(errno));
121 goto done;
122 }
Damien Miller62116dc2001-12-24 01:41:47 +1100123
Damien Millerf22019b2011-05-05 13:48:37 +1000124 if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
125 if (tcp_port != 0) {
126 error("Couldn't connect to PRNGD port %d: %s",
127 tcp_port, strerror(errno));
128 } else {
129 error("Couldn't connect to PRNGD socket \"%s\": %s",
130 addr_un->sun_path, strerror(errno));
Damien Miller62116dc2001-12-24 01:41:47 +1100131 }
Damien Millerf22019b2011-05-05 13:48:37 +1000132 goto done;
Damien Miller62116dc2001-12-24 01:41:47 +1100133 }
134
Damien Millerf22019b2011-05-05 13:48:37 +1000135 /* Send blocking read request to PRNGD */
136 msg[0] = 0x02;
137 msg[1] = len;
Damien Miller62116dc2001-12-24 01:41:47 +1100138
Damien Millerf22019b2011-05-05 13:48:37 +1000139 if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
140 if (errno == EPIPE && errors < 10) {
141 close(fd);
142 errors++;
143 goto reopen;
144 }
145 error("Couldn't write to PRNGD socket: %s",
Damien Miller62116dc2001-12-24 01:41:47 +1100146 strerror(errno));
Damien Millerf22019b2011-05-05 13:48:37 +1000147 goto done;
148 }
Damien Miller62116dc2001-12-24 01:41:47 +1100149
Damien Millerf22019b2011-05-05 13:48:37 +1000150 if (atomicio(read, fd, buf, len) != (size_t)len) {
151 if (errno == EPIPE && errors < 10) {
152 close(fd);
153 errors++;
154 goto reopen;
155 }
156 error("Couldn't read from PRNGD socket: %s",
Damien Millerb6f72f52005-07-17 17:26:43 +1000157 strerror(errno));
Damien Millerf22019b2011-05-05 13:48:37 +1000158 goto done;
159 }
Damien Miller62116dc2001-12-24 01:41:47 +1100160
Damien Millerf22019b2011-05-05 13:48:37 +1000161 rval = 0;
162done:
Darren Tucker389125b2018-02-15 21:43:01 +1100163 signal(SIGPIPE, old_sigpipe);
Damien Millerf22019b2011-05-05 13:48:37 +1000164 if (fd != -1)
165 close(fd);
166 return rval;
Damien Miller14c12cb2000-06-07 22:20:23 +1000167}
168
Damien Millerf22019b2011-05-05 13:48:37 +1000169static int
170seed_from_prngd(unsigned char *buf, size_t bytes)
Damien Miller767c7fc2001-02-27 09:20:57 +1100171{
Damien Millerf22019b2011-05-05 13:48:37 +1000172#ifdef PRNGD_PORT
173 debug("trying egd/prngd port %d", PRNGD_PORT);
174 if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
175 return 0;
Damien Miller6c21c512002-01-22 21:57:53 +1100176#endif
Damien Millerf22019b2011-05-05 13:48:37 +1000177#ifdef PRNGD_SOCKET
178 debug("trying egd/prngd socket %s", PRNGD_SOCKET);
179 if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
180 return 0;
181#endif
182 return -1;
Damien Millerf9b625c2000-07-09 22:42:32 +1000183}
Damien Miller6c21c512002-01-22 21:57:53 +1100184
Darren Tuckerc6f82192005-09-27 22:46:32 +1000185void
Damien Miller120a1ec2018-07-10 19:39:52 +1000186rexec_send_rng_seed(struct sshbuf *m)
Darren Tuckerc6f82192005-09-27 22:46:32 +1000187{
188 u_char buf[RANDOM_SEED_SIZE];
Damien Miller120a1ec2018-07-10 19:39:52 +1000189 size_t len = sizeof(buf);
190 int r;
Darren Tuckerc6f82192005-09-27 22:46:32 +1000191
192 if (RAND_bytes(buf, sizeof(buf)) <= 0) {
193 error("Couldn't obtain random bytes (error %ld)",
194 ERR_get_error());
Damien Miller120a1ec2018-07-10 19:39:52 +1000195 len = 0;
196 }
197 if ((r = sshbuf_put_string(m, buf, len)) != 0)
198 fatal("%s: buffer error: %s", __func__, ssh_err(r));
199 explicit_bzero(buf, sizeof(buf));
Darren Tuckerc6f82192005-09-27 22:46:32 +1000200}
201
202void
Damien Miller120a1ec2018-07-10 19:39:52 +1000203rexec_recv_rng_seed(struct sshbuf *m)
Darren Tuckerc6f82192005-09-27 22:46:32 +1000204{
Darren Tucker01a1e212019-07-06 12:00:41 +1000205 const u_char *buf = NULL;
Damien Miller120a1ec2018-07-10 19:39:52 +1000206 size_t len = 0;
207 int r;
Darren Tuckerc6f82192005-09-27 22:46:32 +1000208
Darren Tucker01a1e212019-07-06 12:00:41 +1000209 if ((r = sshbuf_get_string_direct(m, &buf, &len)) != 0)
Damien Miller120a1ec2018-07-10 19:39:52 +1000210 fatal("%s: buffer error: %s", __func__, ssh_err(r));
211
Darren Tucker01a1e212019-07-06 12:00:41 +1000212 debug3("rexec_recv_rng_seed: seeding rng with %lu bytes",
213 (unsigned long)len);
Damien Miller120a1ec2018-07-10 19:39:52 +1000214 RAND_add(buf, len, len);
Darren Tuckerc6f82192005-09-27 22:46:32 +1000215}
Damien Millerf22019b2011-05-05 13:48:37 +1000216#endif /* OPENSSL_PRNG_ONLY */
217
218void
219seed_rng(void)
220{
Damien Millerf22019b2011-05-05 13:48:37 +1000221 unsigned char buf[RANDOM_SEED_SIZE];
Damien Miller42c5ec42018-11-23 10:40:06 +1100222
223 /* Initialise libcrypto */
224 ssh_libcrypto_init();
225
Damien Miller859754b2018-10-23 17:10:41 +1100226 if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER,
227 OpenSSL_version_num()))
Damien Millerf22019b2011-05-05 13:48:37 +1000228 fatal("OpenSSL version mismatch. Built against %lx, you "
Damien Miller859754b2018-10-23 17:10:41 +1100229 "have %lx", (u_long)OPENSSL_VERSION_NUMBER,
230 OpenSSL_version_num());
Damien Millerf22019b2011-05-05 13:48:37 +1000231
232#ifndef OPENSSL_PRNG_ONLY
Damien Miller42c5ec42018-11-23 10:40:06 +1100233 if (RAND_status() == 1)
Damien Millerf22019b2011-05-05 13:48:37 +1000234 debug3("RNG is ready, skipping seeding");
Damien Miller42c5ec42018-11-23 10:40:06 +1100235 else {
236 if (seed_from_prngd(buf, sizeof(buf)) == -1)
237 fatal("Could not obtain seed from PRNGd");
238 RAND_add(buf, sizeof(buf), sizeof(buf));
Damien Millerf22019b2011-05-05 13:48:37 +1000239 }
Damien Millerf22019b2011-05-05 13:48:37 +1000240#endif /* OPENSSL_PRNG_ONLY */
Damien Miller42c5ec42018-11-23 10:40:06 +1100241
Damien Millerf22019b2011-05-05 13:48:37 +1000242 if (RAND_status() != 1)
243 fatal("PRNG is not seeded");
Damien Miller42c5ec42018-11-23 10:40:06 +1100244
245 /* Ensure arc4random() is primed */
246 arc4random_buf(buf, sizeof(buf));
247 explicit_bzero(buf, sizeof(buf));
Damien Millerf22019b2011-05-05 13:48:37 +1000248}
Damien Miller72ef7c12015-01-15 02:21:31 +1100249
250#else /* WITH_OPENSSL */
251
Darren Tucker9634ffb2019-07-23 22:25:44 +1000252#include <stdlib.h>
253#include <string.h>
254
Damien Miller13c508d2019-10-02 10:51:15 +1000255/* Actual initialisation is handled in arc4random() */
Damien Miller72ef7c12015-01-15 02:21:31 +1100256void
257seed_rng(void)
258{
Damien Miller42c5ec42018-11-23 10:40:06 +1100259 unsigned char buf[RANDOM_SEED_SIZE];
260
261 /* Ensure arc4random() is primed */
262 arc4random_buf(buf, sizeof(buf));
263 explicit_bzero(buf, sizeof(buf));
Damien Miller72ef7c12015-01-15 02:21:31 +1100264}
265
266#endif /* WITH_OPENSSL */