2 * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include <openssl/opensslconf.h>
12 #include <openssl/crypto.h>
13 #include <openssl/e_os2.h>
14 #include <openssl/rand.h>
20 #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)
21 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
26 int RAND_egd(const char *path)
31 int RAND_egd_bytes(const char *path, int bytes)
40 # include <sys/types.h>
41 # include <sys/socket.h>
46 short sun_family; /* AF_UNIX */
47 char sun_path[108]; /* path name (gag) */
49 # endif /* NO_SYS_UN_H */
53 # if defined(OPENSSL_SYS_TANDEM)
57 * This code forces the use of compatibility mode if required on HPE NonStop
58 * when coreutils PRNGD is used and then restores the previous mode
59 * after establishing the socket. This is not required on x86 where hardware
60 * randomization should be used instead of EGD available as of OpenSSL 3.0.
61 * Use --with-rand-seed=rdcpu when configuring x86 with 3.0 and above.
65 * The better long-term solution is to either run two EGD's each in one of
66 * the two modes or revise the EGD code to listen on two different sockets
67 * (each in one of the two modes) or use the hardware randomizer.
70 int hpns_socket(int family,
76 char current_transport[20];
78 # define AF_UNIX_PORTABILITY "$ZAFN2"
79 # define AF_UNIX_COMPATIBILITY "$ZPLS"
81 if (!_arg_present(transport) || transport == NULL || transport[0] == '\0')
82 return socket(family, type, protocol);
84 socket_transport_name_get(AF_UNIX, current_transport, 20);
86 if (strcmp(current_transport, transport) == 0)
87 return socket(family, type, protocol);
89 /* set the requested socket transport */
90 if (socket_transport_name_set(AF_UNIX, transport))
93 socket_rc = socket(family, type, protocol);
95 /* set mode back to what it was */
96 if (socket_transport_name_set(AF_UNIX, current_transport))
102 /*#define socket(a,b,c,...) hpns_socket(a,b,c,__VA_ARGS__) */
104 static int hpns_connect_attempt = 0;
106 # endif /* defined(OPENSSL_SYS_HPNS) */
109 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
112 struct sockaddr_un addr;
113 int mybuffer, ret = -1, i, numbytes, fd;
114 unsigned char tempbuf[255];
116 if (bytes > (int)sizeof(tempbuf))
120 memset(&addr, 0, sizeof(addr));
121 addr.sun_family = AF_UNIX;
122 if (strlen(path) >= sizeof(addr.sun_path))
124 strcpy(addr.sun_path, path);
125 i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
126 #if defined(OPENSSL_SYS_TANDEM)
127 fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_COMPATIBILITY);
129 fd = socket(AF_UNIX, SOCK_STREAM, 0);
131 if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
137 if (connect(fd, (struct sockaddr *)&addr, i) == 0)
140 if (errno == EISCONN)
156 /* No error, try again */
159 # if defined(OPENSSL_SYS_TANDEM)
160 if (hpns_connect_attempt == 0) {
161 /* try the other kind of AF_UNIX socket */
163 fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_PORTABILITY);
166 ++hpns_connect_attempt;
167 break; /* try the connect again */
176 /* Make request, see how many bytes we can get back. */
179 if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
181 if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
183 numbytes = tempbuf[0];
185 /* Which buffer are we using? */
186 mybuffer = buf == NULL;
191 i = fread(buf, sizeof(char), numbytes, fp);
196 RAND_add(tempbuf, i, i);
204 int RAND_egd_bytes(const char *path, int bytes)
208 num = RAND_query_egd_bytes(path, NULL, bytes);
211 if (RAND_status() != 1)
216 int RAND_egd(const char *path)
218 return RAND_egd_bytes(path, 255);