1/* 2 * Copyright (c) 2001 Damien Miller. All rights reserved. 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. 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 27#include <sys/types.h> 28#include <sys/socket.h> 29#ifdef HAVE_SYS_UN_H 30# include <sys/un.h> 31#endif 32 33#include <netinet/in.h> 34#include <arpa/inet.h> 35 36#include <errno.h> 37#include <signal.h> 38#include <string.h> 39#include <unistd.h> 40#include <stddef.h> /* for offsetof */ 41 42#ifdef __APPLE_CRYPTO__ 43#include "ossl-rand.h" 44#include "ossl-crypto.h" 45#include "ossl-err.h" 46#else 47#include <openssl/rand.h> 48#include <openssl/crypto.h> 49#include <openssl/err.h> 50#endif 51 52#include "ssh.h" 53#include "misc.h" 54#include "xmalloc.h" 55#include "atomicio.h" 56#include "pathnames.h" 57#include "log.h" 58#include "buffer.h" 59 60/* 61 * Portable OpenSSH PRNG seeding: 62 * If OpenSSL has not "internally seeded" itself (e.g. pulled data from 63 * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from 64 * PRNGd. 65 */ 66#ifndef OPENSSL_PRNG_ONLY 67 68#define RANDOM_SEED_SIZE 48 69 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 */ 78int 79get_random_bytes_prngd(unsigned char *buf, int len, 80 unsigned short tcp_port, char *socket_path) 81{ 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; 87 mysig_t old_sigpipe; 88 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; 111 } 112 113 old_sigpipe = mysignal(SIGPIPE, SIG_IGN); 114 115 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 } 123 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)); 131 } 132 goto done; 133 } 134 135 /* Send blocking read request to PRNGD */ 136 msg[0] = 0x02; 137 msg[1] = len; 138 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", 146 strerror(errno)); 147 goto done; 148 } 149 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", 157 strerror(errno)); 158 goto done; 159 } 160 161 rval = 0; 162done: 163 mysignal(SIGPIPE, old_sigpipe); 164 if (fd != -1) 165 close(fd); 166 return rval; 167} 168 169static int 170seed_from_prngd(unsigned char *buf, size_t bytes) 171{ 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; 176#endif 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; 183} 184 185void 186rexec_send_rng_seed(Buffer *m) 187{ 188 u_char buf[RANDOM_SEED_SIZE]; 189 190 if (RAND_bytes(buf, sizeof(buf)) <= 0) { 191 error("Couldn't obtain random bytes (error %ld)", 192 ERR_get_error()); 193 buffer_put_string(m, "", 0); 194 } else 195 buffer_put_string(m, buf, sizeof(buf)); 196} 197 198void 199rexec_recv_rng_seed(Buffer *m) 200{ 201 u_char *buf; 202 u_int len; 203 204 buf = buffer_get_string_ret(m, &len); 205 if (buf != NULL) { 206 debug3("rexec_recv_rng_seed: seeding rng with %u bytes", len); 207 RAND_add(buf, len, len); 208 } 209} 210#endif /* OPENSSL_PRNG_ONLY */ 211 212void 213seed_rng(void) 214{ 215#ifndef OPENSSL_PRNG_ONLY 216 unsigned char buf[RANDOM_SEED_SIZE]; 217#endif 218 /* 219 * OpenSSL version numbers: MNNFFPPS: major minor fix patch status 220 * We match major, minor, fix and status (not patch) for <1.0.0. 221 * After that, we acceptable compatible fix versions (so we 222 * allow 1.0.1 to work with 1.0.0). Going backwards is only allowed 223 * within a patch series. 224 */ 225 u_long version_mask = SSLeay() >= 0x1000000f ? ~0xffff0L : ~0xff0L; 226 if (((SSLeay() ^ OPENSSL_VERSION_NUMBER) & version_mask) || 227 (SSLeay() >> 12) < (OPENSSL_VERSION_NUMBER >> 12)) 228 fatal("OpenSSL version mismatch. Built against %lx, you " 229 "have %lx", (u_long)OPENSSL_VERSION_NUMBER, SSLeay()); 230 231#ifndef OPENSSL_PRNG_ONLY 232 if (RAND_status() == 1) { 233 debug3("RNG is ready, skipping seeding"); 234 return; 235 } 236 237 if (seed_from_prngd(buf, sizeof(buf)) == -1) 238 fatal("Could not obtain seed from PRNGd"); 239 RAND_add(buf, sizeof(buf), sizeof(buf)); 240 memset(buf, '\0', sizeof(buf)); 241 242#endif /* OPENSSL_PRNG_ONLY */ 243 if (RAND_status() != 1) 244 fatal("PRNG is not seeded"); 245} 246