1 | /*
|
---|
2 | * Copyright 2000-2020 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)
|
---|
21 | int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
|
---|
22 | {
|
---|
23 | return -1;
|
---|
24 | }
|
---|
25 |
|
---|
26 | int RAND_egd(const char *path)
|
---|
27 | {
|
---|
28 | return -1;
|
---|
29 | }
|
---|
30 |
|
---|
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 | # ifdef OPENSSL_SYS_VXWORKS
|
---|
44 | # include <streams/un.h>
|
---|
45 | # else
|
---|
46 | # include <sys/un.h>
|
---|
47 | # endif
|
---|
48 | # else
|
---|
49 | struct sockaddr_un {
|
---|
50 | short sun_family; /* AF_UNIX */
|
---|
51 | char sun_path[108]; /* path name (gag) */
|
---|
52 | };
|
---|
53 | # endif /* NO_SYS_UN_H */
|
---|
54 | # include <string.h>
|
---|
55 | # include <errno.h>
|
---|
56 |
|
---|
57 | # if defined(OPENSSL_SYS_TANDEM)
|
---|
58 | /*
|
---|
59 | * HPNS:
|
---|
60 | *
|
---|
61 | * Our current MQ 5.3 EGD requies compatability-mode sockets
|
---|
62 | * This code forces the mode to compatibility if required
|
---|
63 | * and then restores the mode.
|
---|
64 | *
|
---|
65 | * Needs review:
|
---|
66 | *
|
---|
67 | * The better long-term solution is to either run two EGD's each in one of
|
---|
68 | * the two modes or revise the EGD code to listen on two different sockets
|
---|
69 | * (each in one of the two modes).
|
---|
70 | */
|
---|
71 | _variable
|
---|
72 | int hpns_socket(int family,
|
---|
73 | int type,
|
---|
74 | int protocol,
|
---|
75 | char* transport)
|
---|
76 | {
|
---|
77 | int socket_rc;
|
---|
78 | char current_transport[20];
|
---|
79 |
|
---|
80 | # define AF_UNIX_PORTABILITY "$ZAFN2"
|
---|
81 | # define AF_UNIX_COMPATIBILITY "$ZPLS"
|
---|
82 |
|
---|
83 | if (!_arg_present(transport) || transport != NULL || transport[0] == '\0')
|
---|
84 | return socket(family, type, protocol);
|
---|
85 |
|
---|
86 | socket_transport_name_get(AF_UNIX, current_transport, 20);
|
---|
87 |
|
---|
88 | if (strcmp(current_transport,transport) == 0)
|
---|
89 | return socket(family, type, protocol);
|
---|
90 |
|
---|
91 | /* set the requested socket transport */
|
---|
92 | if (socket_transport_name_set(AF_UNIX, transport))
|
---|
93 | return -1;
|
---|
94 |
|
---|
95 | socket_rc = socket(family,type,protocol);
|
---|
96 |
|
---|
97 | /* set mode back to what it was */
|
---|
98 | if (socket_transport_name_set(AF_UNIX, current_transport))
|
---|
99 | return -1;
|
---|
100 |
|
---|
101 | return socket_rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*#define socket(a,b,c,...) hpns_socket(a,b,c,__VA_ARGS__) */
|
---|
105 |
|
---|
106 | static int hpns_connect_attempt = 0;
|
---|
107 |
|
---|
108 | # endif /* defined(OPENSSL_SYS_HPNS) */
|
---|
109 |
|
---|
110 |
|
---|
111 | int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
|
---|
112 | {
|
---|
113 | FILE *fp = NULL;
|
---|
114 | struct sockaddr_un addr;
|
---|
115 | int mybuffer, ret = -1, i, numbytes, fd;
|
---|
116 | unsigned char tempbuf[255];
|
---|
117 |
|
---|
118 | if (bytes > (int)sizeof(tempbuf))
|
---|
119 | return -1;
|
---|
120 |
|
---|
121 | /* Make socket. */
|
---|
122 | memset(&addr, 0, sizeof(addr));
|
---|
123 | addr.sun_family = AF_UNIX;
|
---|
124 | if (strlen(path) >= sizeof(addr.sun_path))
|
---|
125 | return -1;
|
---|
126 | strcpy(addr.sun_path, path);
|
---|
127 | i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
|
---|
128 | #if defined(OPENSSL_SYS_TANDEM)
|
---|
129 | fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_COMPATIBILITY);
|
---|
130 | #else
|
---|
131 | fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
---|
132 | #endif
|
---|
133 | if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
|
---|
134 | return -1;
|
---|
135 | setbuf(fp, NULL);
|
---|
136 |
|
---|
137 | /* Try to connect */
|
---|
138 | for ( ; ; ) {
|
---|
139 | if (connect(fd, (struct sockaddr *)&addr, i) == 0)
|
---|
140 | break;
|
---|
141 | # ifdef EISCONN
|
---|
142 | if (errno == EISCONN)
|
---|
143 | break;
|
---|
144 | # endif
|
---|
145 | switch (errno) {
|
---|
146 | # ifdef EINTR
|
---|
147 | case EINTR:
|
---|
148 | # endif
|
---|
149 | # ifdef EAGAIN
|
---|
150 | case EAGAIN:
|
---|
151 | # endif
|
---|
152 | # ifdef EINPROGRESS
|
---|
153 | case EINPROGRESS:
|
---|
154 | # endif
|
---|
155 | # ifdef EALREADY
|
---|
156 | case EALREADY:
|
---|
157 | # endif
|
---|
158 | /* No error, try again */
|
---|
159 | break;
|
---|
160 | default:
|
---|
161 | # if defined(OPENSSL_SYS_TANDEM)
|
---|
162 | if (hpns_connect_attempt == 0) {
|
---|
163 | /* try the other kind of AF_UNIX socket */
|
---|
164 | close(fd);
|
---|
165 | fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_PORTABILITY);
|
---|
166 | if (fd == -1)
|
---|
167 | return -1;
|
---|
168 | ++hpns_connect_attempt;
|
---|
169 | break; /* try the connect again */
|
---|
170 | }
|
---|
171 | # endif
|
---|
172 |
|
---|
173 | ret = -1;
|
---|
174 | goto err;
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Make request, see how many bytes we can get back. */
|
---|
179 | tempbuf[0] = 1;
|
---|
180 | tempbuf[1] = bytes;
|
---|
181 | if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
|
---|
182 | goto err;
|
---|
183 | if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
|
---|
184 | goto err;
|
---|
185 | numbytes = tempbuf[0];
|
---|
186 |
|
---|
187 | /* Which buffer are we using? */
|
---|
188 | mybuffer = buf == NULL;
|
---|
189 | if (mybuffer)
|
---|
190 | buf = tempbuf;
|
---|
191 |
|
---|
192 | /* Read bytes. */
|
---|
193 | i = fread(buf, sizeof(char), numbytes, fp);
|
---|
194 | if (i < numbytes)
|
---|
195 | goto err;
|
---|
196 | ret = numbytes;
|
---|
197 | if (mybuffer)
|
---|
198 | RAND_add(tempbuf, i, i);
|
---|
199 |
|
---|
200 | err:
|
---|
201 | if (fp != NULL)
|
---|
202 | fclose(fp);
|
---|
203 | return ret;
|
---|
204 | }
|
---|
205 |
|
---|
206 | int RAND_egd_bytes(const char *path, int bytes)
|
---|
207 | {
|
---|
208 | int num;
|
---|
209 |
|
---|
210 | num = RAND_query_egd_bytes(path, NULL, bytes);
|
---|
211 | if (num < 0)
|
---|
212 | return -1;
|
---|
213 | if (RAND_status() != 1)
|
---|
214 | return -1;
|
---|
215 | return num;
|
---|
216 | }
|
---|
217 |
|
---|
218 | int RAND_egd(const char *path)
|
---|
219 | {
|
---|
220 | return RAND_egd_bytes(path, 255);
|
---|
221 | }
|
---|
222 |
|
---|
223 | #endif
|
---|