xref: /freebsd/crypto/openssh/openbsd-compat/port-prngd.c (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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 <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <stddef.h> /* for offsetof */
42 
43 #include "atomicio.h"
44 #include "misc.h"
45 #include "log.h"
46 
47 #if defined(PRNGD_PORT) || defined(PRNGD_SOCKET)
48 /*
49  * EGD/PRNGD interface.
50  *
51  * Collect 'len' bytes of entropy into 'buf' from PRNGD/EGD daemon
52  * listening either on 'tcp_port', or via Unix domain socket at *
53  * 'socket_path'.
54  * Either a non-zero tcp_port or a non-null socket_path must be
55  * supplied.
56  * Returns 0 on success, -1 on error
57  */
58 static int
59 get_random_bytes_prngd(unsigned char *buf, int len,
60     unsigned short tcp_port, char *socket_path)
61 {
62 	int fd, addr_len, rval, errors;
63 	u_char msg[2];
64 	struct sockaddr_storage addr;
65 	struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
66 	struct sockaddr_un *addr_un = (struct sockaddr_un *)&addr;
67 	sshsig_t old_sigpipe;
68 
69 	/* Sanity checks */
70 	if (socket_path == NULL && tcp_port == 0)
71 		fatal("You must specify a port or a socket");
72 	if (socket_path != NULL &&
73 	    strlen(socket_path) >= sizeof(addr_un->sun_path))
74 		fatal("Random pool path is too long");
75 	if (len <= 0 || len > 255)
76 		fatal("Too many bytes (%d) to read from PRNGD", len);
77 
78 	memset(&addr, '\0', sizeof(addr));
79 
80 	if (tcp_port != 0) {
81 		addr_in->sin_family = AF_INET;
82 		addr_in->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
83 		addr_in->sin_port = htons(tcp_port);
84 		addr_len = sizeof(*addr_in);
85 	} else {
86 		addr_un->sun_family = AF_UNIX;
87 		strlcpy(addr_un->sun_path, socket_path,
88 		    sizeof(addr_un->sun_path));
89 		addr_len = offsetof(struct sockaddr_un, sun_path) +
90 		    strlen(socket_path) + 1;
91 	}
92 
93 	old_sigpipe = ssh_signal(SIGPIPE, SIG_IGN);
94 
95 	errors = 0;
96 	rval = -1;
97 reopen:
98 	fd = socket(addr.ss_family, SOCK_STREAM, 0);
99 	if (fd == -1) {
100 		error("Couldn't create socket: %s", strerror(errno));
101 		goto done;
102 	}
103 
104 	if (connect(fd, (struct sockaddr*)&addr, addr_len) == -1) {
105 		if (tcp_port != 0) {
106 			error("Couldn't connect to PRNGD port %d: %s",
107 			    tcp_port, strerror(errno));
108 		} else {
109 			error("Couldn't connect to PRNGD socket \"%s\": %s",
110 			    addr_un->sun_path, strerror(errno));
111 		}
112 		goto done;
113 	}
114 
115 	/* Send blocking read request to PRNGD */
116 	msg[0] = 0x02;
117 	msg[1] = len;
118 
119 	if (atomicio(vwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
120 		if (errno == EPIPE && errors < 10) {
121 			close(fd);
122 			errors++;
123 			goto reopen;
124 		}
125 		error("Couldn't write to PRNGD socket: %s",
126 		    strerror(errno));
127 		goto done;
128 	}
129 
130 	if (atomicio(read, fd, buf, len) != (size_t)len) {
131 		if (errno == EPIPE && errors < 10) {
132 			close(fd);
133 			errors++;
134 			goto reopen;
135 		}
136 		error("Couldn't read from PRNGD socket: %s",
137 		    strerror(errno));
138 		goto done;
139 	}
140 
141 	rval = 0;
142 done:
143 	ssh_signal(SIGPIPE, old_sigpipe);
144 	if (fd != -1)
145 		close(fd);
146 	return rval;
147 }
148 #endif /* PRNGD_PORT || PRNGD_SOCKET */
149 
150 int
151 seed_from_prngd(unsigned char *buf, size_t bytes)
152 {
153 #ifdef PRNGD_PORT
154 	debug("trying egd/prngd port %d", PRNGD_PORT);
155 	if (get_random_bytes_prngd(buf, bytes, PRNGD_PORT, NULL) == 0)
156 		return 0;
157 #endif
158 #ifdef PRNGD_SOCKET
159 	debug("trying egd/prngd socket %s", PRNGD_SOCKET);
160 	if (get_random_bytes_prngd(buf, bytes, 0, PRNGD_SOCKET) == 0)
161 		return 0;
162 #endif
163 	return -1;
164 }
165