]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/rand/rand_egd.c
Copyright consolidation 09/10
[thirdparty/openssl.git] / crypto / rand / rand_egd.c
1 /*
2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/opensslconf.h>
11 #ifdef OPENSSL_NO_EGD
12 NON_EMPTY_TRANSLATION_UNIT
13 #else
14
15 # include <openssl/crypto.h>
16 # include <openssl/e_os2.h>
17 # include <openssl/rand.h>
18
19 /*-
20 * Query the EGD <URL: http://www.lothar.com/tech/crypto/>.
21 *
22 * This module supplies three routines:
23 *
24 * RAND_query_egd_bytes(path, buf, bytes)
25 * will actually query "bytes" bytes of entropy form the egd-socket located
26 * at path and will write them to buf (if supplied) or will directly feed
27 * it to RAND_seed() if buf==NULL.
28 * The number of bytes is not limited by the maximum chunk size of EGD,
29 * which is 255 bytes. If more than 255 bytes are wanted, several chunks
30 * of entropy bytes are requested. The connection is left open until the
31 * query is competed.
32 * RAND_query_egd_bytes() returns with
33 * -1 if an error occurred during connection or communication.
34 * num the number of bytes read from the EGD socket. This number is either
35 * the number of bytes requested or smaller, if the EGD pool is
36 * drained and the daemon signals that the pool is empty.
37 * This routine does not touch any RAND_status(). This is necessary, since
38 * PRNG functions may call it during initialization.
39 *
40 * RAND_egd_bytes(path, bytes) will query "bytes" bytes and have them
41 * used to seed the PRNG.
42 * RAND_egd_bytes() is a wrapper for RAND_query_egd_bytes() with buf=NULL.
43 * Unlike RAND_query_egd_bytes(), RAND_status() is used to test the
44 * seed status so that the return value can reflect the seed state:
45 * -1 if an error occurred during connection or communication _or_
46 * if the PRNG has still not received the required seeding.
47 * num the number of bytes read from the EGD socket. This number is either
48 * the number of bytes requested or smaller, if the EGD pool is
49 * drained and the daemon signals that the pool is empty.
50 *
51 * RAND_egd(path) will query 255 bytes and use the bytes retreived to seed
52 * the PRNG.
53 * RAND_egd() is a wrapper for RAND_egd_bytes() with numbytes=255.
54 */
55
56 # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS) || defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_VOS) || defined(OPENSSL_SYS_UEFI)
57 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
58 {
59 return (-1);
60 }
61
62 int RAND_egd(const char *path)
63 {
64 return (-1);
65 }
66
67 int RAND_egd_bytes(const char *path, int bytes)
68 {
69 return (-1);
70 }
71 # else
72 # include <openssl/opensslconf.h>
73 # include OPENSSL_UNISTD
74 # include <stddef.h>
75 # include <sys/types.h>
76 # include <sys/socket.h>
77 # ifndef NO_SYS_UN_H
78 # ifdef OPENSSL_SYS_VXWORKS
79 # include <streams/un.h>
80 # else
81 # include <sys/un.h>
82 # endif
83 # else
84 struct sockaddr_un {
85 short sun_family; /* AF_UNIX */
86 char sun_path[108]; /* path name (gag) */
87 };
88 # endif /* NO_SYS_UN_H */
89 # include <string.h>
90 # include <errno.h>
91
92 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
93 {
94 int ret = 0;
95 struct sockaddr_un addr;
96 int len, num, numbytes;
97 int fd = -1;
98 int success;
99 unsigned char egdbuf[2], tempbuf[255], *retrievebuf;
100
101 memset(&addr, 0, sizeof(addr));
102 addr.sun_family = AF_UNIX;
103 if (strlen(path) >= sizeof(addr.sun_path))
104 return (-1);
105 OPENSSL_strlcpy(addr.sun_path, path, sizeof addr.sun_path);
106 len = offsetof(struct sockaddr_un, sun_path) + strlen(path);
107 fd = socket(AF_UNIX, SOCK_STREAM, 0);
108 if (fd == -1)
109 return (-1);
110 success = 0;
111 while (!success) {
112 if (connect(fd, (struct sockaddr *)&addr, len) == 0)
113 success = 1;
114 else {
115 switch (errno) {
116 # ifdef EINTR
117 case EINTR:
118 # endif
119 # ifdef EAGAIN
120 case EAGAIN:
121 # endif
122 # ifdef EINPROGRESS
123 case EINPROGRESS:
124 # endif
125 # ifdef EALREADY
126 case EALREADY:
127 # endif
128 /* No error, try again */
129 break;
130 # ifdef EISCONN
131 case EISCONN:
132 success = 1;
133 break;
134 # endif
135 default:
136 goto err; /* failure */
137 }
138 }
139 }
140
141 while (bytes > 0) {
142 egdbuf[0] = 1;
143 egdbuf[1] = bytes < 255 ? bytes : 255;
144 numbytes = 0;
145 while (numbytes != 2) {
146 num = write(fd, egdbuf + numbytes, 2 - numbytes);
147 if (num >= 0)
148 numbytes += num;
149 else {
150 switch (errno) {
151 # ifdef EINTR
152 case EINTR:
153 # endif
154 # ifdef EAGAIN
155 case EAGAIN:
156 # endif
157 /* No error, try again */
158 break;
159 default:
160 ret = -1;
161 goto err; /* failure */
162 }
163 }
164 }
165 numbytes = 0;
166 while (numbytes != 1) {
167 num = read(fd, egdbuf, 1);
168 if (num == 0)
169 goto err; /* descriptor closed */
170 else if (num > 0)
171 numbytes += num;
172 else {
173 switch (errno) {
174 # ifdef EINTR
175 case EINTR:
176 # endif
177 # ifdef EAGAIN
178 case EAGAIN:
179 # endif
180 /* No error, try again */
181 break;
182 default:
183 ret = -1;
184 goto err; /* failure */
185 }
186 }
187 }
188 if (egdbuf[0] == 0)
189 goto err;
190 if (buf)
191 retrievebuf = buf + ret;
192 else
193 retrievebuf = tempbuf;
194 numbytes = 0;
195 while (numbytes != egdbuf[0]) {
196 num = read(fd, retrievebuf + numbytes, egdbuf[0] - numbytes);
197 if (num == 0)
198 goto err; /* descriptor closed */
199 else if (num > 0)
200 numbytes += num;
201 else {
202 switch (errno) {
203 # ifdef EINTR
204 case EINTR:
205 # endif
206 # ifdef EAGAIN
207 case EAGAIN:
208 # endif
209 /* No error, try again */
210 break;
211 default:
212 ret = -1;
213 goto err; /* failure */
214 }
215 }
216 }
217 ret += egdbuf[0];
218 bytes -= egdbuf[0];
219 if (!buf)
220 RAND_seed(tempbuf, egdbuf[0]);
221 }
222 err:
223 if (fd != -1)
224 close(fd);
225 return (ret);
226 }
227
228 int RAND_egd_bytes(const char *path, int bytes)
229 {
230 int num, ret = 0;
231
232 num = RAND_query_egd_bytes(path, NULL, bytes);
233 if (num < 1)
234 goto err;
235 if (RAND_status() == 1)
236 ret = num;
237 err:
238 return (ret);
239 }
240
241 int RAND_egd(const char *path)
242 {
243 return (RAND_egd_bytes(path, 255));
244 }
245
246 # endif
247
248 #endif