Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 2 | * Copyright (c) 2001 Damien Miller. All rights reserved. |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 3 | * |
| 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 Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 12 | * |
| 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 Tucker | c721d58 | 2018-11-23 14:11:20 +1100 | [diff] [blame] | 27 | #define RANDOM_SEED_SIZE 48 |
| 28 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 29 | #ifdef WITH_OPENSSL |
| 30 | |
Darren Tucker | d82cbcb | 2006-03-16 07:21:35 +1100 | [diff] [blame] | 31 | #include <sys/types.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #ifdef HAVE_SYS_UN_H |
| 34 | # include <sys/un.h> |
Darren Tucker | e0e4aad | 2006-07-11 19:01:51 +1000 | [diff] [blame] | 35 | #endif |
| 36 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 37 | #include <netinet/in.h> |
| 38 | #include <arpa/inet.h> |
| 39 | |
| 40 | #include <errno.h> |
Darren Tucker | 23dd658 | 2006-09-28 19:40:20 +1000 | [diff] [blame] | 41 | #include <signal.h> |
Darren Tucker | 9634ffb | 2019-07-23 22:25:44 +1000 | [diff] [blame] | 42 | #include <stdlib.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 43 | #include <string.h> |
Darren Tucker | a8d51ee | 2007-03-13 07:35:38 +1100 | [diff] [blame] | 44 | #include <unistd.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 45 | #include <stddef.h> /* for offsetof */ |
Darren Tucker | e0e4aad | 2006-07-11 19:01:51 +1000 | [diff] [blame] | 46 | |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 47 | #include <openssl/rand.h> |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 48 | #include <openssl/crypto.h> |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 49 | #include <openssl/err.h> |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 50 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 51 | #include "openbsd-compat/openssl-compat.h" |
| 52 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 53 | #include "ssh.h" |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 54 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 55 | #include "xmalloc.h" |
| 56 | #include "atomicio.h" |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 57 | #include "pathnames.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 58 | #include "log.h" |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 59 | #include "sshbuf.h" |
| 60 | #include "ssherr.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 61 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 62 | /* |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 63 | * Portable OpenSSH PRNG seeding: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 64 | * If OpenSSL has not "internally seeded" itself (e.g. pulled data from |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 65 | * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from |
| 66 | * PRNGd. |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 67 | */ |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 68 | #ifndef OPENSSL_PRNG_ONLY |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 69 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 70 | /* |
| 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 | */ |
| 78 | int |
| 79 | get_random_bytes_prngd(unsigned char *buf, int len, |
| 80 | unsigned short tcp_port, char *socket_path) |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 81 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 82 | 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; |
Darren Tucker | 945bf52 | 2020-01-23 21:06:45 +1100 | [diff] [blame] | 87 | sshsig_t old_sigpipe; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 88 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 89 | /* 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 Miller | 0800647 | 2000-06-26 13:55:31 +1000 | [diff] [blame] | 111 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 112 | |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 113 | old_sigpipe = ssh_signal(SIGPIPE, SIG_IGN); |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 114 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 115 | errors = 0; |
| 116 | rval = -1; |
| 117 | reopen: |
| 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 Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 123 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 124 | 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 Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 131 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 132 | goto done; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 133 | } |
| 134 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 135 | /* Send blocking read request to PRNGD */ |
| 136 | msg[0] = 0x02; |
| 137 | msg[1] = len; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 138 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 139 | 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 Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 146 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 147 | goto done; |
| 148 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 149 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 150 | 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 Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 157 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 158 | goto done; |
| 159 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 160 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 161 | rval = 0; |
| 162 | done: |
dtucker@openbsd.org | 3bf2a6a | 2020-01-23 07:10:22 +0000 | [diff] [blame] | 163 | ssh_signal(SIGPIPE, old_sigpipe); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 164 | if (fd != -1) |
| 165 | close(fd); |
| 166 | return rval; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 167 | } |
| 168 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 169 | static int |
| 170 | seed_from_prngd(unsigned char *buf, size_t bytes) |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 171 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 172 | #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 Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 176 | #endif |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 177 | #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 Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 183 | } |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 184 | |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 185 | void |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 186 | rexec_send_rng_seed(struct sshbuf *m) |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 187 | { |
| 188 | u_char buf[RANDOM_SEED_SIZE]; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 189 | size_t len = sizeof(buf); |
| 190 | int r; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 191 | |
| 192 | if (RAND_bytes(buf, sizeof(buf)) <= 0) { |
| 193 | error("Couldn't obtain random bytes (error %ld)", |
| 194 | ERR_get_error()); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 195 | 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 Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 203 | rexec_recv_rng_seed(struct sshbuf *m) |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 204 | { |
Darren Tucker | 01a1e21 | 2019-07-06 12:00:41 +1000 | [diff] [blame] | 205 | const u_char *buf = NULL; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 206 | size_t len = 0; |
| 207 | int r; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 208 | |
Darren Tucker | 01a1e21 | 2019-07-06 12:00:41 +1000 | [diff] [blame] | 209 | if ((r = sshbuf_get_string_direct(m, &buf, &len)) != 0) |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 210 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 211 | |
Darren Tucker | 01a1e21 | 2019-07-06 12:00:41 +1000 | [diff] [blame] | 212 | debug3("rexec_recv_rng_seed: seeding rng with %lu bytes", |
| 213 | (unsigned long)len); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 214 | RAND_add(buf, len, len); |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 215 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 216 | #endif /* OPENSSL_PRNG_ONLY */ |
| 217 | |
| 218 | void |
| 219 | seed_rng(void) |
| 220 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 221 | unsigned char buf[RANDOM_SEED_SIZE]; |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 222 | |
| 223 | /* Initialise libcrypto */ |
| 224 | ssh_libcrypto_init(); |
| 225 | |
Damien Miller | 859754b | 2018-10-23 17:10:41 +1100 | [diff] [blame] | 226 | if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, |
| 227 | OpenSSL_version_num())) |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 228 | fatal("OpenSSL version mismatch. Built against %lx, you " |
Damien Miller | 859754b | 2018-10-23 17:10:41 +1100 | [diff] [blame] | 229 | "have %lx", (u_long)OPENSSL_VERSION_NUMBER, |
| 230 | OpenSSL_version_num()); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 231 | |
| 232 | #ifndef OPENSSL_PRNG_ONLY |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 233 | if (RAND_status() == 1) |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 234 | debug3("RNG is ready, skipping seeding"); |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 235 | 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 Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 239 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 240 | #endif /* OPENSSL_PRNG_ONLY */ |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 241 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 242 | if (RAND_status() != 1) |
| 243 | fatal("PRNG is not seeded"); |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 244 | |
| 245 | /* Ensure arc4random() is primed */ |
| 246 | arc4random_buf(buf, sizeof(buf)); |
| 247 | explicit_bzero(buf, sizeof(buf)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 248 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 249 | |
| 250 | #else /* WITH_OPENSSL */ |
| 251 | |
Darren Tucker | 9634ffb | 2019-07-23 22:25:44 +1000 | [diff] [blame] | 252 | #include <stdlib.h> |
| 253 | #include <string.h> |
| 254 | |
Damien Miller | 13c508d | 2019-10-02 10:51:15 +1000 | [diff] [blame] | 255 | /* Actual initialisation is handled in arc4random() */ |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 256 | void |
| 257 | seed_rng(void) |
| 258 | { |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 259 | 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 Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | #endif /* WITH_OPENSSL */ |