Upgrade openssh. 1/2
[dragonfly.git] / crypto / openssh / entropy.c
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 #define RANDOM_SEED_SIZE 48
28
29 #ifdef WITH_OPENSSL
30
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #ifdef HAVE_SYS_UN_H
34 # include <sys/un.h>
35 #endif
36
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39
40 #include <errno.h>
41 #include <signal.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <stddef.h> /* for offsetof */
46
47 #include <openssl/rand.h>
48 #include <openssl/crypto.h>
49 #include <openssl/err.h>
50
51 #include "openbsd-compat/openssl-compat.h"
52
53 #include "ssh.h"
54 #include "misc.h"
55 #include "xmalloc.h"
56 #include "atomicio.h"
57 #include "pathnames.h"
58 #include "log.h"
59 #include "sshbuf.h"
60 #include "ssherr.h"
61
62 /*
63  * Portable OpenSSH PRNG seeding:
64  * If OpenSSL has not "internally seeded" itself (e.g. pulled data from
65  * /dev/random), then collect RANDOM_SEED_SIZE bytes of randomness from
66  * PRNGd.
67  */
68 #ifndef OPENSSL_PRNG_ONLY
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  */
78 int
79 get_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         sshsig_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 = ssh_signal(SIGPIPE, SIG_IGN);
114
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         }
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;
162 done:
163         ssh_signal(SIGPIPE, old_sigpipe);
164         if (fd != -1)
165                 close(fd);
166         return rval;
167 }
168
169 static int
170 seed_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
185 void
186 rexec_send_rng_seed(struct sshbuf *m)
187 {
188         u_char buf[RANDOM_SEED_SIZE];
189         size_t len = sizeof(buf);
190         int r;
191
192         if (RAND_bytes(buf, sizeof(buf)) <= 0) {
193                 error("Couldn't obtain random bytes (error %ld)",
194                     ERR_get_error());
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));
200 }
201
202 void
203 rexec_recv_rng_seed(struct sshbuf *m)
204 {
205         const u_char *buf = NULL;
206         size_t len = 0;
207         int r;
208
209         if ((r = sshbuf_get_string_direct(m, &buf, &len)) != 0)
210                 fatal("%s: buffer error: %s", __func__, ssh_err(r));
211
212         debug3("rexec_recv_rng_seed: seeding rng with %lu bytes",
213             (unsigned long)len);
214         RAND_add(buf, len, len);
215 }
216 #endif /* OPENSSL_PRNG_ONLY */
217
218 void
219 seed_rng(void)
220 {
221         unsigned char buf[RANDOM_SEED_SIZE];
222
223         /* Initialise libcrypto */
224         ssh_libcrypto_init();
225
226         if (!ssh_compatible_openssl(OPENSSL_VERSION_NUMBER,
227             OpenSSL_version_num()))
228                 fatal("OpenSSL version mismatch. Built against %lx, you "
229                     "have %lx", (u_long)OPENSSL_VERSION_NUMBER,
230                     OpenSSL_version_num());
231
232 #ifndef OPENSSL_PRNG_ONLY
233         if (RAND_status() == 1)
234                 debug3("RNG is ready, skipping seeding");
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));
239         }
240 #endif /* OPENSSL_PRNG_ONLY */
241
242         if (RAND_status() != 1)
243                 fatal("PRNG is not seeded");
244
245         /* Ensure arc4random() is primed */
246         arc4random_buf(buf, sizeof(buf));
247         explicit_bzero(buf, sizeof(buf));
248 }
249
250 #else /* WITH_OPENSSL */
251
252 #include <stdlib.h>
253 #include <string.h>
254
255 /* Actual initialisation is handled in arc4random() */
256 void
257 seed_rng(void)
258 {
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));
264 }
265
266 #endif /* WITH_OPENSSL */