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 | d82cbcb | 2006-03-16 07:21:35 +1100 | [diff] [blame] | 27 | #include <sys/types.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 28 | #include <sys/socket.h> |
| 29 | #ifdef HAVE_SYS_UN_H |
| 30 | # include <sys/un.h> |
Darren Tucker | e0e4aad | 2006-07-11 19:01:51 +1000 | [diff] [blame] | 31 | #endif |
| 32 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
| 36 | #include <errno.h> |
Darren Tucker | 23dd658 | 2006-09-28 19:40:20 +1000 | [diff] [blame] | 37 | #include <signal.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 38 | #include <string.h> |
Darren Tucker | a8d51ee | 2007-03-13 07:35:38 +1100 | [diff] [blame] | 39 | #include <unistd.h> |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 40 | #include <stddef.h> /* for offsetof */ |
Darren Tucker | e0e4aad | 2006-07-11 19:01:51 +1000 | [diff] [blame] | 41 | |
Damien Miller | 5f05637 | 2000-04-16 12:31:48 +1000 | [diff] [blame] | 42 | #include <openssl/rand.h> |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 43 | #include <openssl/crypto.h> |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 44 | #include <openssl/err.h> |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 45 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 46 | #include "openbsd-compat/openssl-compat.h" |
| 47 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include "ssh.h" |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 49 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 50 | #include "xmalloc.h" |
| 51 | #include "atomicio.h" |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 52 | #include "pathnames.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 53 | #include "log.h" |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 54 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 55 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 56 | /* |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 57 | * Portable OpenSSH PRNG seeding: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 58 | * If OpenSSL has not "internally seeded" itself (e.g. pulled data from |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 59 | * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from |
| 60 | * PRNGd. |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 61 | */ |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 62 | #ifndef OPENSSL_PRNG_ONLY |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 63 | |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 64 | #define RANDOM_SEED_SIZE 48 |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 65 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 66 | /* |
| 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 | */ |
| 74 | int |
| 75 | get_random_bytes_prngd(unsigned char *buf, int len, |
| 76 | unsigned short tcp_port, char *socket_path) |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 77 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 78 | 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 Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 84 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 85 | /* 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 Miller | 0800647 | 2000-06-26 13:55:31 +1000 | [diff] [blame] | 107 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 108 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 109 | old_sigpipe = mysignal(SIGPIPE, SIG_IGN); |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 110 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 111 | errors = 0; |
| 112 | rval = -1; |
| 113 | reopen: |
| 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 Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 119 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 120 | 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 Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 127 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 128 | goto done; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 129 | } |
| 130 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 131 | /* Send blocking read request to PRNGD */ |
| 132 | msg[0] = 0x02; |
| 133 | msg[1] = len; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 134 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 135 | 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 Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 142 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 143 | goto done; |
| 144 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 145 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 146 | 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 Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 153 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 154 | goto done; |
| 155 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 156 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 157 | rval = 0; |
| 158 | done: |
| 159 | mysignal(SIGPIPE, old_sigpipe); |
| 160 | if (fd != -1) |
| 161 | close(fd); |
| 162 | return rval; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 163 | } |
| 164 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 165 | static int |
| 166 | seed_from_prngd(unsigned char *buf, size_t bytes) |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 167 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 168 | #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 Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 172 | #endif |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 173 | #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 Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 179 | } |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 180 | |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 181 | void |
| 182 | rexec_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 | |
| 194 | void |
| 195 | rexec_recv_rng_seed(Buffer *m) |
| 196 | { |
Darren Tucker | 46e7ba5 | 2005-09-28 08:26:30 +1000 | [diff] [blame] | 197 | u_char *buf; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 198 | 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 Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 206 | #endif /* OPENSSL_PRNG_ONLY */ |
| 207 | |
| 208 | void |
| 209 | seed_rng(void) |
| 210 | { |
| 211 | #ifndef OPENSSL_PRNG_ONLY |
| 212 | unsigned char buf[RANDOM_SEED_SIZE]; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 213 | #endif |
Darren Tucker | 316fac6 | 2014-06-17 23:06:07 +1000 | [diff] [blame] | 214 | if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER, SSLeay())) |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 215 | 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 | } |