1 /*
2 * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <openssl/opensslconf.h>
11
12 #include <openssl/crypto.h>
13 #include <openssl/e_os2.h>
14 #include <openssl/rand.h>
15
16 /*
17 * Query an EGD
18 */
19
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)
RAND_query_egd_bytes(const char * path,unsigned char * buf,int bytes)21 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
22 {
23 return -1;
24 }
25
RAND_egd(const char * path)26 int RAND_egd(const char *path)
27 {
28 return -1;
29 }
30
RAND_egd_bytes(const char * path,int bytes)31 int RAND_egd_bytes(const char *path, int bytes)
32 {
33 return -1;
34 }
35
36 #else
37
38 #include <unistd.h>
39 #include <stddef.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #ifndef NO_SYS_UN_H
43 #include <sys/un.h>
44 #else
45 struct sockaddr_un {
46 short sun_family; /* AF_UNIX */
47 char sun_path[108]; /* path name (gag) */
48 };
49 #endif /* NO_SYS_UN_H */
50 #include <string.h>
51 #include <errno.h>
52
53 #if defined(OPENSSL_SYS_TANDEM)
54 /*
55 * HPNS:
56 *
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.
62 *
63 * Needs review:
64 *
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.
68 */
hpns_socket(int family,int type,int protocol,char * transport)69 _variable int hpns_socket(int family,
70 int type,
71 int protocol,
72 char *transport)
73 {
74 int socket_rc;
75 char current_transport[20];
76
77 #define AF_UNIX_PORTABILITY "$ZAFN2"
78 #define AF_UNIX_COMPATIBILITY "$ZPLS"
79
80 if (!_arg_present(transport) || transport == NULL || transport[0] == '\0')
81 return socket(family, type, protocol);
82
83 socket_transport_name_get(AF_UNIX, current_transport, 20);
84
85 if (strcmp(current_transport, transport) == 0)
86 return socket(family, type, protocol);
87
88 /* set the requested socket transport */
89 if (socket_transport_name_set(AF_UNIX, transport))
90 return -1;
91
92 socket_rc = socket(family, type, protocol);
93
94 /* set mode back to what it was */
95 if (socket_transport_name_set(AF_UNIX, current_transport))
96 return -1;
97
98 return socket_rc;
99 }
100
101 /*#define socket(a,b,c,...) hpns_socket(a,b,c,__VA_ARGS__) */
102
103 static int hpns_connect_attempt = 0;
104
105 #endif /* defined(OPENSSL_SYS_HPNS) */
106
RAND_query_egd_bytes(const char * path,unsigned char * buf,int bytes)107 int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
108 {
109 FILE *fp = NULL;
110 struct sockaddr_un addr;
111 int mybuffer, ret = -1, i, numbytes, fd;
112 unsigned char tempbuf[255];
113
114 if (bytes > (int)sizeof(tempbuf))
115 return -1;
116
117 /* Make socket. */
118 memset(&addr, 0, sizeof(addr));
119 addr.sun_family = AF_UNIX;
120 if (strlen(path) >= sizeof(addr.sun_path))
121 return -1;
122 strcpy(addr.sun_path, path);
123 i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
124 #if defined(OPENSSL_SYS_TANDEM)
125 fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_COMPATIBILITY);
126 #else
127 fd = socket(AF_UNIX, SOCK_STREAM, 0);
128 #endif
129 if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
130 return -1;
131 setbuf(fp, NULL);
132
133 /* Try to connect */
134 for (;;) {
135 if (connect(fd, (struct sockaddr *)&addr, i) == 0)
136 break;
137 #ifdef EISCONN
138 if (errno == EISCONN)
139 break;
140 #endif
141 switch (errno) {
142 #ifdef EINTR
143 case EINTR:
144 #endif
145 #ifdef EAGAIN
146 case EAGAIN:
147 #endif
148 #ifdef EINPROGRESS
149 case EINPROGRESS:
150 #endif
151 #ifdef EALREADY
152 case EALREADY:
153 #endif
154 /* No error, try again */
155 break;
156 default:
157 #if defined(OPENSSL_SYS_TANDEM)
158 if (hpns_connect_attempt == 0) {
159 /* try the other kind of AF_UNIX socket */
160 close(fd);
161 fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_PORTABILITY);
162 if (fd == -1)
163 return -1;
164 ++hpns_connect_attempt;
165 break; /* try the connect again */
166 }
167 #endif
168
169 ret = -1;
170 goto err;
171 }
172 }
173
174 /* Make request, see how many bytes we can get back. */
175 tempbuf[0] = 1;
176 tempbuf[1] = bytes;
177 if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
178 goto err;
179 if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
180 goto err;
181 numbytes = tempbuf[0];
182
183 /* Which buffer are we using? */
184 mybuffer = buf == NULL;
185 if (mybuffer)
186 buf = tempbuf;
187
188 /* Read bytes. */
189 i = fread(buf, sizeof(char), numbytes, fp);
190 if (i < numbytes)
191 goto err;
192 ret = numbytes;
193 if (mybuffer)
194 RAND_add(tempbuf, i, i);
195
196 err:
197 if (fp != NULL)
198 fclose(fp);
199 return ret;
200 }
201
RAND_egd_bytes(const char * path,int bytes)202 int RAND_egd_bytes(const char *path, int bytes)
203 {
204 int num;
205
206 num = RAND_query_egd_bytes(path, NULL, bytes);
207 if (num < 0)
208 return -1;
209 if (RAND_status() != 1)
210 return -1;
211 return num;
212 }
213
RAND_egd(const char * path)214 int RAND_egd(const char *path)
215 {
216 return RAND_egd_bytes(path, 255);
217 }
218
219 #endif
220