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