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 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 46 | #include "ssh.h" |
Damien Miller | a1072a8 | 2001-02-18 15:28:11 +1100 | [diff] [blame] | 47 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 48 | #include "xmalloc.h" |
| 49 | #include "atomicio.h" |
Ben Lindstrom | cb57733 | 2001-01-22 21:06:19 +0000 | [diff] [blame] | 50 | #include "pathnames.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 51 | #include "log.h" |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 52 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 53 | |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 54 | /* |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 55 | * Portable OpenSSH PRNG seeding: |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 56 | * If OpenSSL has not "internally seeded" itself (e.g. pulled data from |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 57 | * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from |
| 58 | * PRNGd. |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 59 | */ |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 60 | #ifndef OPENSSL_PRNG_ONLY |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 61 | |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 62 | #define RANDOM_SEED_SIZE 48 |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 63 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 64 | /* |
| 65 | * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon |
| 66 | * listening either on 'tcp_port', or via Unix domain socket at * |
| 67 | * 'socket_path'. |
| 68 | * Either a non-zero tcp_port or a non-null socket_path must be |
| 69 | * supplied. |
| 70 | * Returns 0 on success, -1 on error |
| 71 | */ |
| 72 | int |
| 73 | get_random_bytes_prngd(unsigned char *buf, int len, |
| 74 | unsigned short tcp_port, char *socket_path) |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 75 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 76 | int fd, addr_len, rval, errors; |
| 77 | u_char msg[2]; |
| 78 | struct sockaddr_storage addr; |
| 79 | struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr; |
| 80 | struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr; |
| 81 | mysig_t old_sigpipe; |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 82 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 83 | /* Sanity checks */ |
| 84 | if (socket_path == NULL && tcp_port == 0) |
| 85 | fatal("You must specify a port or a socket"); |
| 86 | if (socket_path != NULL && |
| 87 | strlen(socket_path) >= sizeof(addr_un->sun_path)) |
| 88 | fatal("Random pool path is too long"); |
| 89 | if (len <= 0 || len > 255) |
| 90 | fatal("Too many bytes (%d) to read from PRNGD", len); |
| 91 | |
| 92 | memset(&addr, '\0', sizeof(addr)); |
| 93 | |
| 94 | if (tcp_port != 0) { |
| 95 | addr_in->sin_family = AF_INET; |
| 96 | addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 97 | addr_in->sin_port = htons(tcp_port); |
| 98 | addr_len = sizeof(*addr_in); |
| 99 | } else { |
| 100 | addr_un->sun_family = AF_UNIX; |
| 101 | strlcpy(addr_un->sun_path, socket_path, |
| 102 | sizeof(addr_un->sun_path)); |
| 103 | addr_len = offsetof(struct sockaddr_un, sun_path) + |
| 104 | strlen(socket_path) + 1; |
Damien Miller | 0800647 | 2000-06-26 13:55:31 +1000 | [diff] [blame] | 105 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 106 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 107 | old_sigpipe = mysignal(SIGPIPE, SIG_IGN); |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 108 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 109 | errors = 0; |
| 110 | rval = -1; |
| 111 | reopen: |
| 112 | fd = socket(addr.ss_family, SOCK_STREAM, 0); |
| 113 | if (fd == -1) { |
| 114 | error("Couldn't create socket: %s", strerror(errno)); |
| 115 | goto done; |
| 116 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 117 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 118 | if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) { |
| 119 | if (tcp_port != 0) { |
| 120 | error("Couldn't connect to PRNGD port %d: %s", |
| 121 | tcp_port, strerror(errno)); |
| 122 | } else { |
| 123 | error("Couldn't connect to PRNGD socket \"%s\": %s", |
| 124 | addr_un->sun_path, strerror(errno)); |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 125 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 126 | goto done; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 127 | } |
| 128 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 129 | /* Send blocking read request to PRNGD */ |
| 130 | msg[0] = 0x02; |
| 131 | msg[1] = len; |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 132 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 133 | if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) { |
| 134 | if (errno == EPIPE && errors < 10) { |
| 135 | close(fd); |
| 136 | errors++; |
| 137 | goto reopen; |
| 138 | } |
| 139 | error("Couldn't write to PRNGD socket: %s", |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 140 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 141 | goto done; |
| 142 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 143 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 144 | if (atomicio(read, fd, buf, len) != (size_t)len) { |
| 145 | if (errno == EPIPE && errors < 10) { |
| 146 | close(fd); |
| 147 | errors++; |
| 148 | goto reopen; |
| 149 | } |
| 150 | error("Couldn't read from PRNGD socket: %s", |
Damien Miller | b6f72f5 | 2005-07-17 17:26:43 +1000 | [diff] [blame] | 151 | strerror(errno)); |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 152 | goto done; |
| 153 | } |
Damien Miller | 62116dc | 2001-12-24 01:41:47 +1100 | [diff] [blame] | 154 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 155 | rval = 0; |
| 156 | done: |
| 157 | mysignal(SIGPIPE, old_sigpipe); |
| 158 | if (fd != -1) |
| 159 | close(fd); |
| 160 | return rval; |
Damien Miller | 14c12cb | 2000-06-07 22:20:23 +1000 | [diff] [blame] | 161 | } |
| 162 | |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 163 | static int |
| 164 | seed_from_prngd(unsigned char *buf, size_t bytes) |
Damien Miller | 767c7fc | 2001-02-27 09:20:57 +1100 | [diff] [blame] | 165 | { |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 166 | #ifdef PRNGD_PORT |
| 167 | debug("trying egd/prngd port %d", PRNGD_PORT); |
| 168 | if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0) |
| 169 | return 0; |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 170 | #endif |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 171 | #ifdef PRNGD_SOCKET |
| 172 | debug("trying egd/prngd socket %s", PRNGD_SOCKET); |
| 173 | if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0) |
| 174 | return 0; |
| 175 | #endif |
| 176 | return -1; |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 177 | } |
Damien Miller | 6c21c51 | 2002-01-22 21:57:53 +1100 | [diff] [blame] | 178 | |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 179 | void |
| 180 | rexec_send_rng_seed(Buffer *m) |
| 181 | { |
| 182 | u_char buf[RANDOM_SEED_SIZE]; |
| 183 | |
| 184 | if (RAND_bytes(buf, sizeof(buf)) <= 0) { |
| 185 | error("Couldn't obtain random bytes (error %ld)", |
| 186 | ERR_get_error()); |
| 187 | buffer_put_string(m, "", 0); |
| 188 | } else |
| 189 | buffer_put_string(m, buf, sizeof(buf)); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | rexec_recv_rng_seed(Buffer *m) |
| 194 | { |
Darren Tucker | 46e7ba5 | 2005-09-28 08:26:30 +1000 | [diff] [blame] | 195 | u_char *buf; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 196 | u_int len; |
| 197 | |
| 198 | buf = buffer_get_string_ret(m, &len); |
| 199 | if (buf != NULL) { |
| 200 | debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len); |
| 201 | RAND_add(buf, len, len); |
| 202 | } |
| 203 | } |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 204 | #endif /* OPENSSL_PRNG_ONLY */ |
| 205 | |
| 206 | void |
| 207 | seed_rng(void) |
| 208 | { |
| 209 | #ifndef OPENSSL_PRNG_ONLY |
| 210 | unsigned char buf[RANDOM_SEED_SIZE]; |
Darren Tucker | c6f8219 | 2005-09-27 22:46:32 +1000 | [diff] [blame] | 211 | #endif |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 212 | /* |
| 213 | * OpenSSL version numbers: MNNFFPPS: major minor fix patch status |
Damien Miller | 4d55734 | 2012-03-30 11:34:27 +1100 | [diff] [blame] | 214 | * We match major, minor, fix and status (not patch) for <1.0.0. |
| 215 | * After that, we acceptable compatible fix versions (so we |
| 216 | * allow 1.0.1 to work with 1.0.0). Going backwards is only allowed |
| 217 | * within a patch series. |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 218 | */ |
Damien Miller | 4d55734 | 2012-03-30 11:34:27 +1100 | [diff] [blame] | 219 | u_long version_mask = SSLeay() >= 0x1000000f ? ~0xffff0L : ~0xff0L; |
| 220 | if (((SSLeay() ^ OPENSSL_VERSION_NUMBER) & version_mask) || |
| 221 | (SSLeay() >> 12) < (OPENSSL_VERSION_NUMBER >> 12)) |
Damien Miller | f22019b | 2011-05-05 13:48:37 +1000 | [diff] [blame] | 222 | fatal("OpenSSL version mismatch. Built against %lx, you " |
| 223 | "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay()); |
| 224 | |
| 225 | #ifndef OPENSSL_PRNG_ONLY |
| 226 | if (RAND_status() == 1) { |
| 227 | debug3("RNG is ready, skipping seeding"); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | if (seed_from_prngd(buf, sizeof(buf)) == -1) |
| 232 | fatal("Could not obtain seed from PRNGd"); |
| 233 | RAND_add(buf, sizeof(buf), sizeof(buf)); |
| 234 | memset(buf, '\0', sizeof(buf)); |
| 235 | |
| 236 | #endif /* OPENSSL_PRNG_ONLY */ |
| 237 | if (RAND_status() != 1) |
| 238 | fatal("PRNG is not seeded"); |
| 239 | } |