1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define APR_WANT_MEMFUNC
19 #include "apr_general.h"
21 #include "apr_arch_misc.h"
23 #if APR_HAVE_SYS_TYPES_H
24 #include <sys/types.h>
26 #if APR_HAVE_SYS_SOCKET_H
27 #include <sys/socket.h>
45 APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf,
46 #ifdef APR_ENABLE_FOR_1_0
56 /* On BSD/OS 4.1, /dev/random gives out 8 bytes at a time, then
57 * gives EOF, so reading 'length' bytes may require opening the
58 * device several times. */
63 if ((fd = open(DEV_RANDOM, O_RDONLY)) == -1)
66 rc = read(fd, buf, length);
74 fd = -1; /* force open() again */
84 static UCHAR randbyte();
87 for (idx=0; idx<length; idx++)
88 buf[idx] = randbyte();
90 #elif defined(HAVE_EGD)
91 /* use EGD-compatible socket daemon (such as EGD or PRNGd).
93 * 0x00 (get entropy level)
94 * 0xMM (msb) 0xmm 0xll 0xLL (lsb)
95 * 0x01 (read entropy nonblocking) 0xNN (bytes requested)
96 * 0xMM (bytes granted) MM bytes
97 * 0x02 (read entropy blocking) 0xNN (bytes desired)
99 * 0x03 (write entropy) 0xMM 0xLL (bits of entropy) 0xNN (bytes of data)
101 * (no response - write only)
103 * 0xMM (length of PID string, not null-terminated) MM chars
105 static const char *egd_sockets[] = { EGD_DEFAULT_SOCKET, NULL };
106 const char **egdsockname = NULL;
108 int egd_socket, egd_path_len, rv, bad_errno;
109 struct sockaddr_un addr;
110 apr_socklen_t egd_addr_len;
111 apr_size_t resp_expected;
112 unsigned char req[2], resp[255];
113 unsigned char *curbuf = buf;
115 for (egdsockname = egd_sockets; *egdsockname && length > 0; egdsockname++) {
116 egd_path_len = strlen(*egdsockname);
118 if (egd_path_len > sizeof(addr.sun_path)) {
122 memset(&addr, 0, sizeof(struct sockaddr_un));
123 addr.sun_family = AF_UNIX;
124 memcpy(addr.sun_path, *egdsockname, egd_path_len);
125 egd_addr_len = APR_OFFSETOF(struct sockaddr_un, sun_path) +
128 egd_socket = socket(PF_UNIX, SOCK_STREAM, 0);
130 if (egd_socket == -1) {
134 rv = connect(egd_socket, (struct sockaddr*)&addr, egd_addr_len);
141 /* EGD can only return 255 bytes of data at a time. Silly. */
144 req[0] = 2; /* We'll block for now. */
145 req[1] = length > 255 ? 255: length;
147 srv = write(egd_socket, req, 2);
150 shutdown(egd_socket, SHUT_RDWR);
156 shutdown(egd_socket, SHUT_RDWR);
161 resp_expected = req[1];
162 srv = read(egd_socket, resp, resp_expected);
165 shutdown(egd_socket, SHUT_RDWR);
170 memcpy(curbuf, resp, srv);
175 shutdown(egd_socket, SHUT_RDWR);
180 /* We must have iterated through the list of sockets,
181 * and no go. Return the errno.
186 #elif defined(HAVE_TRUERAND) /* use truerand */
188 extern int randbyte(void); /* from the truerand library */
191 /* this will increase the startup time of the server, unfortunately...
192 * (generating 20 bytes takes about 8 seconds)
194 for (idx=0; idx<length; idx++)
195 buf[idx] = (unsigned char) randbyte();
197 #endif /* DEV_RANDOM */
206 #include "../os2/randbyte.c"
209 #endif /* APR_HAS_RANDOM */