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> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 42 | #include <string.h> |
Darren Tucker | a8d51ee | 2007-03-13 07:35:38 +1100 | [diff] [blame] | 43 | #include <unistd.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 44 | #include <stddef.h> /* for offsetof */ |
Darren Tucker | e0e4aad | 2006-07-11 19:01:51 +1000 | [diff] [blame] | 45 | |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 46 | #include <openssl/rand.h> |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 47 | #include <openssl/crypto.h> |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 48 | #include <openssl/err.h> |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 49 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 50 | #include "openbsd-compat/openssl-compat.h" |
| 51 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 52 | #include "ssh.h" |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 53 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 54 | #include "xmalloc.h" |
| 55 | #include "atomicio.h" |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 56 | #include "pathnames.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 57 | #include "log.h" |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 58 | #include "sshbuf.h" |
| 59 | #include "ssherr.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 60 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 61 | /* |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 62 | * Portable OpenSSH PRNG seeding: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 63 | * If OpenSSL has not "internally seeded" itself (e.g. pulled data from |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 64 | * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from |
| 65 | * PRNGd. |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 66 | */ |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 67 | #ifndef OPENSSL_PRNG_ONLY |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 68 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 69 | /* |
| 70 | * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon |
| 71 | * listening either on 'tcp_port', or via Unix domain socket at * |
| 72 | * 'socket_path'. |
| 73 | * Either a non-zero tcp_port or a non-null socket_path must be |
| 74 | * supplied. |
| 75 | * Returns 0 on success, -1 on error |
| 76 | */ |
| 77 | int |
| 78 | get_random_bytes_prngd(unsigned char *buf, int len, |
| 79 | unsigned short tcp_port, char *socket_path) |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 80 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 81 | int fd, addr_len, rval, errors; |
| 82 | u_char msg[2]; |
| 83 | struct sockaddr_storage addr; |
| 84 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; |
| 85 | struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr; |
| 86 | mysig_t old_sigpipe; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 87 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 88 | /* Sanity checks */ |
| 89 | if (socket_path == NULL && tcp_port == 0) |
| 90 | fatal("You must specify a port or a socket"); |
| 91 | if (socket_path != NULL && |
| 92 | strlen(socket_path) >= sizeof(addr_un->sun_path)) |
| 93 | fatal("Random pool path is too long"); |
| 94 | if (len <= 0 || len > 255) |
| 95 | fatal("Too many bytes (%d) to read from PRNGD", len); |
| 96 | |
| 97 | memset(&addr, '\0', sizeof(addr)); |
| 98 | |
| 99 | if (tcp_port != 0) { |
| 100 | addr_in->sin_family = AF_INET; |
| 101 | addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 102 | addr_in->sin_port = htons(tcp_port); |
| 103 | addr_len = sizeof(*addr_in); |
| 104 | } else { |
| 105 | addr_un->sun_family = AF_UNIX; |
| 106 | strlcpy(addr_un->sun_path, socket_path, |
| 107 | sizeof(addr_un->sun_path)); |
| 108 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
| 109 | strlen(socket_path) + 1; |
Damien Miller | 0800647 | 2000-06-26 13:55:31 +1000 | [diff] [blame] | 110 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 111 | |
Darren Tucker | 389125b | 2018-02-15 21:43:01 +1100 | [diff] [blame] | 112 | old_sigpipe = signal(SIGPIPE, SIG_IGN); |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 113 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 114 | errors = 0; |
| 115 | rval = -1; |
| 116 | reopen: |
| 117 | fd = socket(addr.ss_family, SOCK_STREAM, 0); |
| 118 | if (fd == -1) { |
| 119 | error("Couldn't create socket: %s", strerror(errno)); |
| 120 | goto done; |
| 121 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 122 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 123 | if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) { |
| 124 | if (tcp_port != 0) { |
| 125 | error("Couldn't connect to PRNGD port %d: %s", |
| 126 | tcp_port, strerror(errno)); |
| 127 | } else { |
| 128 | error("Couldn't connect to PRNGD socket \"%s\": %s", |
| 129 | addr_un->sun_path, strerror(errno)); |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 130 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 131 | goto done; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 132 | } |
| 133 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 134 | /* Send blocking read request to PRNGD */ |
| 135 | msg[0] = 0x02; |
| 136 | msg[1] = len; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 137 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 138 | if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) { |
| 139 | if (errno == EPIPE && errors < 10) { |
| 140 | close(fd); |
| 141 | errors++; |
| 142 | goto reopen; |
| 143 | } |
| 144 | error("Couldn't write to PRNGD socket: %s", |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 145 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 146 | goto done; |
| 147 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 148 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 149 | if (atomicio(read, fd, buf, len) != (size_t)len) { |
| 150 | if (errno == EPIPE && errors < 10) { |
| 151 | close(fd); |
| 152 | errors++; |
| 153 | goto reopen; |
| 154 | } |
| 155 | error("Couldn't read from PRNGD socket: %s", |
Damien Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 156 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 157 | goto done; |
| 158 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 159 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 160 | rval = 0; |
| 161 | done: |
Darren Tucker | 389125b | 2018-02-15 21:43:01 +1100 | [diff] [blame] | 162 | signal(SIGPIPE, old_sigpipe); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 163 | if (fd != -1) |
| 164 | close(fd); |
| 165 | return rval; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 166 | } |
| 167 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 168 | static int |
| 169 | seed_from_prngd(unsigned char *buf, size_t bytes) |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 170 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 171 | #ifdef PRNGD_PORT |
| 172 | debug("trying egd/prngd port %d", PRNGD_PORT); |
| 173 | if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0) |
| 174 | return 0; |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 175 | #endif |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 176 | #ifdef PRNGD_SOCKET |
| 177 | debug("trying egd/prngd socket %s", PRNGD_SOCKET); |
| 178 | if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0) |
| 179 | return 0; |
| 180 | #endif |
| 181 | return -1; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 182 | } |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 183 | |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 184 | void |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 185 | rexec_send_rng_seed(struct sshbuf *m) |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 186 | { |
| 187 | u_char buf[RANDOM_SEED_SIZE]; |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 188 | size_t len = sizeof(buf); |
| 189 | int r; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 190 | |
| 191 | if (RAND_bytes(buf, sizeof(buf)) <= 0) { |
| 192 | error("Couldn't obtain random bytes (error %ld)", |
| 193 | ERR_get_error()); |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 194 | len = 0; |
| 195 | } |
| 196 | if ((r = sshbuf_put_string(m, buf, len)) != 0) |
| 197 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 198 | explicit_bzero(buf, sizeof(buf)); |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 202 | rexec_recv_rng_seed(struct sshbuf *m) |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 203 | { |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 204 | u_char *buf = NULL; |
| 205 | size_t len = 0; |
| 206 | int r; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 207 | |
Damien Miller | 120a1ec | 2018-07-10 19:39:52 +1000 | [diff] [blame] | 208 | if ((r = sshbuf_get_string_direct(m, &buf, &len)) != 0 |
| 209 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 210 | |
| 211 | debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len); |
| 212 | RAND_add(buf, len, len); |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 213 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 214 | #endif /* OPENSSL_PRNG_ONLY */ |
| 215 | |
| 216 | void |
| 217 | seed_rng(void) |
| 218 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 219 | unsigned char buf[RANDOM_SEED_SIZE]; |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 220 | |
| 221 | /* Initialise libcrypto */ |
| 222 | ssh_libcrypto_init(); |
| 223 | |
Damien Miller | 859754b | 2018-10-23 17:10:41 +1100 | [diff] [blame] | 224 | if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, |
| 225 | OpenSSL_version_num())) |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 226 | fatal("OpenSSL version mismatch. Built against %lx, you " |
Damien Miller | 859754b | 2018-10-23 17:10:41 +1100 | [diff] [blame] | 227 | "have %lx", (u_long)OPENSSL_VERSION_NUMBER, |
| 228 | OpenSSL_version_num()); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 229 | |
| 230 | #ifndef OPENSSL_PRNG_ONLY |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 231 | if (RAND_status() == 1) |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 232 | debug3("RNG is ready, skipping seeding"); |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 233 | else { |
| 234 | if (seed_from_prngd(buf, sizeof(buf)) == -1) |
| 235 | fatal("Could not obtain seed from PRNGd"); |
| 236 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 237 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 238 | #endif /* OPENSSL_PRNG_ONLY */ |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 239 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 240 | if (RAND_status() != 1) |
| 241 | fatal("PRNG is not seeded"); |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 242 | |
| 243 | /* Ensure arc4random() is primed */ |
| 244 | arc4random_buf(buf, sizeof(buf)); |
| 245 | explicit_bzero(buf, sizeof(buf)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 246 | } |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 247 | |
| 248 | #else /* WITH_OPENSSL */ |
| 249 | |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 250 | /* Acutal initialisation is handled in arc4random() */ |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 251 | void |
| 252 | seed_rng(void) |
| 253 | { |
Damien Miller | 42c5ec4 | 2018-11-23 10:40:06 +1100 | [diff] [blame] | 254 | unsigned char buf[RANDOM_SEED_SIZE]; |
| 255 | |
| 256 | /* Ensure arc4random() is primed */ |
| 257 | arc4random_buf(buf, sizeof(buf)); |
| 258 | explicit_bzero(buf, sizeof(buf)); |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | #endif /* WITH_OPENSSL */ |