xref: /freebsd/crypto/openssh/openbsd-compat/port-prngd.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1*19261079SEd Maste /*
2*19261079SEd Maste  * Copyright (c) 2001 Damien Miller.  All rights reserved.
3*19261079SEd Maste  *
4*19261079SEd Maste  * Redistribution and use in source and binary forms, with or without
5*19261079SEd Maste  * modification, are permitted provided that the following conditions
6*19261079SEd Maste  * are met:
7*19261079SEd Maste  * 1. Redistributions of source code must retain the above copyright
8*19261079SEd Maste  *    notice, this list of conditions and the following disclaimer.
9*19261079SEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
10*19261079SEd Maste  *    notice, this list of conditions and the following disclaimer in the
11*19261079SEd Maste  *    documentation and/or other materials provided with the distribution.
12*19261079SEd Maste  *
13*19261079SEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*19261079SEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*19261079SEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*19261079SEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*19261079SEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*19261079SEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*19261079SEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*19261079SEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*19261079SEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*19261079SEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*19261079SEd Maste  */
24*19261079SEd Maste 
25*19261079SEd Maste #include "includes.h"
26*19261079SEd Maste 
27*19261079SEd Maste #include <sys/types.h>
28*19261079SEd Maste #include <sys/socket.h>
29*19261079SEd Maste #ifdef HAVE_SYS_UN_H
30*19261079SEd Maste # include <sys/un.h>
31*19261079SEd Maste #endif
32*19261079SEd Maste 
33*19261079SEd Maste #include <netinet/in.h>
34*19261079SEd Maste #include <arpa/inet.h>
35*19261079SEd Maste 
36*19261079SEd Maste #include <errno.h>
37*19261079SEd Maste #include <signal.h>
38*19261079SEd Maste #include <stdlib.h>
39*19261079SEd Maste #include <string.h>
40*19261079SEd Maste #include <unistd.h>
41*19261079SEd Maste #include <stddef.h> /* for offsetof */
42*19261079SEd Maste 
43*19261079SEd Maste #include "atomicio.h"
44*19261079SEd Maste #include "misc.h"
45*19261079SEd Maste #include "log.h"
46*19261079SEd Maste 
47*19261079SEd Maste #if defined(PRNGD_PORT) || defined(PRNGD_SOCKET)
48*19261079SEd Maste /*
49*19261079SEd Maste  * EGD/PRNGD interface.
50*19261079SEd Maste  *
51*19261079SEd Maste  * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
52*19261079SEd Maste  * listening either on 'tcp_port', or via Unix domain socket at *
53*19261079SEd Maste  * 'socket_path'.
54*19261079SEd Maste  * Either a non-zero tcp_port or a non-null socket_path must be
55*19261079SEd Maste  * supplied.
56*19261079SEd Maste  * Returns 0 on success, -1 on error
57*19261079SEd Maste  */
58*19261079SEd Maste static int
get_random_bytes_prngd(unsigned char * buf,int len,unsigned short tcp_port,char * socket_path)59*19261079SEd Maste get_random_bytes_prngd(unsigned char *buf, int len,
60*19261079SEd Maste     unsigned short tcp_port, char *socket_path)
61*19261079SEd Maste {
62*19261079SEd Maste 	int fd, addr_len, rval, errors;
63*19261079SEd Maste 	u_char msg[2];
64*19261079SEd Maste 	struct sockaddr_storage addr;
65*19261079SEd Maste 	struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
66*19261079SEd Maste 	struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
67*19261079SEd Maste 	sshsig_t old_sigpipe;
68*19261079SEd Maste 
69*19261079SEd Maste 	/* Sanity checks */
70*19261079SEd Maste 	if (socket_path == NULL && tcp_port == 0)
71*19261079SEd Maste 		fatal("You must specify a port or a socket");
72*19261079SEd Maste 	if (socket_path != NULL &&
73*19261079SEd Maste 	    strlen(socket_path) >= sizeof(addr_un->sun_path))
74*19261079SEd Maste 		fatal("Random pool path is too long");
75*19261079SEd Maste 	if (len <= 0 || len > 255)
76*19261079SEd Maste 		fatal("Too many bytes (%d) to read from PRNGD", len);
77*19261079SEd Maste 
78*19261079SEd Maste 	memset(&addr, '\0', sizeof(addr));
79*19261079SEd Maste 
80*19261079SEd Maste 	if (tcp_port != 0) {
81*19261079SEd Maste 		addr_in->sin_family = AF_INET;
82*19261079SEd Maste 		addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
83*19261079SEd Maste 		addr_in->sin_port = htons(tcp_port);
84*19261079SEd Maste 		addr_len = sizeof(*addr_in);
85*19261079SEd Maste 	} else {
86*19261079SEd Maste 		addr_un->sun_family = AF_UNIX;
87*19261079SEd Maste 		strlcpy(addr_un->sun_path, socket_path,
88*19261079SEd Maste 		    sizeof(addr_un->sun_path));
89*19261079SEd Maste 		addr_len = offsetof(struct sockaddr_un, sun_path) +
90*19261079SEd Maste 		    strlen(socket_path) + 1;
91*19261079SEd Maste 	}
92*19261079SEd Maste 
93*19261079SEd Maste 	old_sigpipe = ssh_signal(SIGPIPE, SIG_IGN);
94*19261079SEd Maste 
95*19261079SEd Maste 	errors = 0;
96*19261079SEd Maste 	rval = -1;
97*19261079SEd Maste reopen:
98*19261079SEd Maste 	fd = socket(addr.ss_family, SOCK_STREAM, 0);
99*19261079SEd Maste 	if (fd == -1) {
100*19261079SEd Maste 		error("Couldn't create socket: %s", strerror(errno));
101*19261079SEd Maste 		goto done;
102*19261079SEd Maste 	}
103*19261079SEd Maste 
104*19261079SEd Maste 	if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
105*19261079SEd Maste 		if (tcp_port != 0) {
106*19261079SEd Maste 			error("Couldn't connect to PRNGD port %d: %s",
107*19261079SEd Maste 			    tcp_port, strerror(errno));
108*19261079SEd Maste 		} else {
109*19261079SEd Maste 			error("Couldn't connect to PRNGD socket \"%s\": %s",
110*19261079SEd Maste 			    addr_un->sun_path, strerror(errno));
111*19261079SEd Maste 		}
112*19261079SEd Maste 		goto done;
113*19261079SEd Maste 	}
114*19261079SEd Maste 
115*19261079SEd Maste 	/* Send blocking read request to PRNGD */
116*19261079SEd Maste 	msg[0] = 0x02;
117*19261079SEd Maste 	msg[1] = len;
118*19261079SEd Maste 
119*19261079SEd Maste 	if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
120*19261079SEd Maste 		if (errno == EPIPE && errors < 10) {
121*19261079SEd Maste 			close(fd);
122*19261079SEd Maste 			errors++;
123*19261079SEd Maste 			goto reopen;
124*19261079SEd Maste 		}
125*19261079SEd Maste 		error("Couldn't write to PRNGD socket: %s",
126*19261079SEd Maste 		    strerror(errno));
127*19261079SEd Maste 		goto done;
128*19261079SEd Maste 	}
129*19261079SEd Maste 
130*19261079SEd Maste 	if (atomicio(read, fd, buf, len) != (size_t)len) {
131*19261079SEd Maste 		if (errno == EPIPE && errors < 10) {
132*19261079SEd Maste 			close(fd);
133*19261079SEd Maste 			errors++;
134*19261079SEd Maste 			goto reopen;
135*19261079SEd Maste 		}
136*19261079SEd Maste 		error("Couldn't read from PRNGD socket: %s",
137*19261079SEd Maste 		    strerror(errno));
138*19261079SEd Maste 		goto done;
139*19261079SEd Maste 	}
140*19261079SEd Maste 
141*19261079SEd Maste 	rval = 0;
142*19261079SEd Maste done:
143*19261079SEd Maste 	ssh_signal(SIGPIPE, old_sigpipe);
144*19261079SEd Maste 	if (fd != -1)
145*19261079SEd Maste 		close(fd);
146*19261079SEd Maste 	return rval;
147*19261079SEd Maste }
148*19261079SEd Maste #endif /* PRNGD_PORT || PRNGD_SOCKET */
149*19261079SEd Maste 
150*19261079SEd Maste int
seed_from_prngd(unsigned char * buf,size_t bytes)151*19261079SEd Maste seed_from_prngd(unsigned char *buf, size_t bytes)
152*19261079SEd Maste {
153*19261079SEd Maste #ifdef PRNGD_PORT
154*19261079SEd Maste 	debug("trying egd/prngd port %d", PRNGD_PORT);
155*19261079SEd Maste 	if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
156*19261079SEd Maste 		return 0;
157*19261079SEd Maste #endif
158*19261079SEd Maste #ifdef PRNGD_SOCKET
159*19261079SEd Maste 	debug("trying egd/prngd socket %s", PRNGD_SOCKET);
160*19261079SEd Maste 	if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
161*19261079SEd Maste 		return 0;
162*19261079SEd Maste #endif
163*19261079SEd Maste 	return -1;
164*19261079SEd Maste }
165