| 1 |
/* |
| 2 |
* Copyright 2000-2026 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 |
#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 |
*/ |
| 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 |
|
| 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 = -1; |
| 112 |
unsigned char tempbuf[255]; |
| 113 |
#if defined(OPENSSL_SYS_TANDEM) |
| 114 |
int hpns_connect_attempt = 0; |
| 115 |
#endif |
| 116 |
|
| 117 |
if (bytes <= 0 || bytes > (int)sizeof(tempbuf)) |
| 118 |
return -1; |
| 119 |
|
| 120 |
/* Make socket. */ |
| 121 |
memset(&addr, 0, sizeof(addr)); |
| 122 |
addr.sun_family = AF_UNIX; |
| 123 |
if (strlen(path) >= sizeof(addr.sun_path)) |
| 124 |
return -1; |
| 125 |
strcpy(addr.sun_path, path); |
| 126 |
i = offsetof(struct sockaddr_un, sun_path) + strlen(path); |
| 127 |
#if defined(OPENSSL_SYS_TANDEM) |
| 128 |
fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_COMPATIBILITY); |
| 129 |
#else |
| 130 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 131 |
#endif |
| 132 |
if (fd == -1) |
| 133 |
return -1; |
| 134 |
|
| 135 |
/* Try to connect */ |
| 136 |
for (;;) { |
| 137 |
if (connect(fd, (struct sockaddr *)&addr, i) == 0) |
| 138 |
break; |
| 139 |
#ifdef EISCONN |
| 140 |
if (errno == EISCONN) |
| 141 |
break; |
| 142 |
#endif |
| 143 |
switch (errno) { |
| 144 |
#ifdef EINTR |
| 145 |
case EINTR: |
| 146 |
#endif |
| 147 |
#ifdef EAGAIN |
| 148 |
case EAGAIN: |
| 149 |
#endif |
| 150 |
#ifdef EINPROGRESS |
| 151 |
case EINPROGRESS: |
| 152 |
#endif |
| 153 |
#ifdef EALREADY |
| 154 |
case EALREADY: |
| 155 |
#endif |
| 156 |
/* No error, try again */ |
| 157 |
break; |
| 158 |
default: |
| 159 |
#if defined(OPENSSL_SYS_TANDEM) |
| 160 |
if (hpns_connect_attempt == 0) { |
| 161 |
/* try the other kind of AF_UNIX socket */ |
| 162 |
close(fd); |
| 163 |
fd = hpns_socket(AF_UNIX, SOCK_STREAM, 0, AF_UNIX_PORTABILITY); |
| 164 |
if (fd == -1) |
| 165 |
return -1; |
| 166 |
++hpns_connect_attempt; |
| 167 |
break; /* try the connect again */ |
| 168 |
} |
| 169 |
#endif |
| 170 |
|
| 171 |
ret = -1; |
| 172 |
goto err; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/* Create stream only after a successful connect to avoid stale FILE* on fd swap. */ |
| 177 |
fp = fdopen(fd, "r+"); |
| 178 |
if (fp == NULL) { |
| 179 |
close(fd); |
| 180 |
return -1; |
| 181 |
} |
| 182 |
setbuf(fp, NULL); |
| 183 |
|
| 184 |
/* Make request, see how many bytes we can get back. */ |
| 185 |
tempbuf[0] = 1; |
| 186 |
tempbuf[1] = bytes; |
| 187 |
if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF) |
| 188 |
goto err; |
| 189 |
if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0) |
| 190 |
goto err; |
| 191 |
numbytes = tempbuf[0]; |
| 192 |
|
| 193 |
if (numbytes <= 0 || numbytes > bytes || numbytes > (int)sizeof(tempbuf)) |
| 194 |
goto err; |
| 195 |
|
| 196 |
/* Which buffer are we using? */ |
| 197 |
mybuffer = buf == NULL; |
| 198 |
if (mybuffer) |
| 199 |
buf = tempbuf; |
| 200 |
|
| 201 |
/* Read bytes. */ |
| 202 |
i = fread(buf, sizeof(char), numbytes, fp); |
| 203 |
if (i < numbytes) |
| 204 |
goto err; |
| 205 |
ret = numbytes; |
| 206 |
if (mybuffer) |
| 207 |
RAND_add(tempbuf, i, i); |
| 208 |
|
| 209 |
err: |
| 210 |
if (fp != NULL) |
| 211 |
fclose(fp); |
| 212 |
else if (fd != -1) |
| 213 |
close(fd); |
| 214 |
return ret; |
| 215 |
} |
| 216 |
|
| 217 |
int RAND_egd_bytes(const char *path, int bytes) |
| 218 |
{ |
| 219 |
int num; |
| 220 |
|
| 221 |
num = RAND_query_egd_bytes(path, NULL, bytes); |
| 222 |
if (num < 0) |
| 223 |
return -1; |
| 224 |
if (RAND_status() != 1) |
| 225 |
return -1; |
| 226 |
return num; |
| 227 |
} |
| 228 |
|
| 229 |
int RAND_egd(const char *path) |
| 230 |
{ |
| 231 |
return RAND_egd_bytes(path, 255); |
| 232 |
} |
| 233 |
|
| 234 |
#endif |