2016-05-17 18:52:22 +00:00
|
|
|
/*
|
2018-05-29 12:07:08 +00:00
|
|
|
* Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2000-02-24 02:51:47 +00:00
|
|
|
*
|
2018-12-06 12:52:15 +00:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 18:52:22 +00:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2000-02-24 02:51:47 +00:00
|
|
|
*/
|
|
|
|
|
2016-01-31 18:08:23 +00:00
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
#ifdef OPENSSL_NO_EGD
|
|
|
|
NON_EMPTY_TRANSLATION_UNIT
|
|
|
|
#else
|
|
|
|
|
|
|
|
# include <openssl/crypto.h>
|
|
|
|
# include <openssl/e_os2.h>
|
|
|
|
# include <openssl/rand.h>
|
2000-02-24 02:51:47 +00:00
|
|
|
|
2017-07-08 18:45:15 +00:00
|
|
|
/*
|
|
|
|
* Query an EGD
|
2000-02-24 02:51:47 +00:00
|
|
|
*/
|
|
|
|
|
2016-03-17 16:53:11 +00:00
|
|
|
# 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)
|
2008-11-12 07:27:36 +00:00
|
|
|
int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-07-06 04:56:20 +00:00
|
|
|
return -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2000-02-24 02:51:47 +00:00
|
|
|
int RAND_egd(const char *path)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-08-03 13:23:28 +00:00
|
|
|
return -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-06-03 14:13:58 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
int RAND_egd_bytes(const char *path, int bytes)
|
|
|
|
{
|
2017-08-03 13:23:28 +00:00
|
|
|
return -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2017-07-08 18:45:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
# else
|
2017-07-08 18:45:15 +00:00
|
|
|
|
2019-06-20 21:07:25 +00:00
|
|
|
# include <unistd.h>
|
2016-01-13 00:02:16 +00:00
|
|
|
# include <stddef.h>
|
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/socket.h>
|
|
|
|
# ifndef NO_SYS_UN_H
|
|
|
|
# ifdef OPENSSL_SYS_VXWORKS
|
|
|
|
# include <streams/un.h>
|
|
|
|
# else
|
|
|
|
# include <sys/un.h>
|
|
|
|
# endif
|
|
|
|
# else
|
2015-01-22 03:40:55 +00:00
|
|
|
struct sockaddr_un {
|
|
|
|
short sun_family; /* AF_UNIX */
|
|
|
|
char sun_path[108]; /* path name (gag) */
|
2001-03-18 14:25:01 +00:00
|
|
|
};
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif /* NO_SYS_UN_H */
|
|
|
|
# include <string.h>
|
|
|
|
# include <errno.h>
|
2000-02-24 02:51:47 +00:00
|
|
|
|
2008-11-12 03:58:08 +00:00
|
|
|
int RAND_query_egd_bytes(const char *path, unsigned char *buf, int bytes)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-07-08 18:45:15 +00:00
|
|
|
FILE *fp = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
struct sockaddr_un addr;
|
2017-07-08 18:45:15 +00:00
|
|
|
int mybuffer, ret = -1, i, numbytes, fd;
|
|
|
|
unsigned char tempbuf[255];
|
2000-02-24 02:51:47 +00:00
|
|
|
|
2017-07-08 18:45:15 +00:00
|
|
|
if (bytes > (int)sizeof(tempbuf))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Make socket. */
|
2015-01-22 03:40:55 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
if (strlen(path) >= sizeof(addr.sun_path))
|
2017-08-03 13:23:28 +00:00
|
|
|
return -1;
|
2017-06-15 00:34:37 +00:00
|
|
|
strcpy(addr.sun_path, path);
|
2017-07-08 18:45:15 +00:00
|
|
|
i = offsetof(struct sockaddr_un, sun_path) + strlen(path);
|
2015-01-22 03:40:55 +00:00
|
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
2017-07-08 18:45:15 +00:00
|
|
|
if (fd == -1 || (fp = fdopen(fd, "r+")) == NULL)
|
2017-08-03 13:23:28 +00:00
|
|
|
return -1;
|
|
|
|
setbuf(fp, NULL);
|
2017-07-08 18:45:15 +00:00
|
|
|
|
|
|
|
/* Try to connect */
|
|
|
|
for ( ; ; ) {
|
|
|
|
if (connect(fd, (struct sockaddr *)&addr, i) == 0)
|
|
|
|
break;
|
2016-01-13 00:02:16 +00:00
|
|
|
# ifdef EISCONN
|
2017-07-08 18:45:15 +00:00
|
|
|
if (errno == EISCONN)
|
|
|
|
break;
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif
|
2017-07-08 18:45:15 +00:00
|
|
|
switch (errno) {
|
2016-01-13 00:02:16 +00:00
|
|
|
# ifdef EINTR
|
2017-07-08 18:45:15 +00:00
|
|
|
case EINTR:
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif
|
|
|
|
# ifdef EAGAIN
|
2017-07-08 18:45:15 +00:00
|
|
|
case EAGAIN:
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif
|
2017-07-08 18:45:15 +00:00
|
|
|
# ifdef EINPROGRESS
|
|
|
|
case EINPROGRESS:
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif
|
2017-07-08 18:45:15 +00:00
|
|
|
# ifdef EALREADY
|
|
|
|
case EALREADY:
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif
|
2017-07-08 18:45:15 +00:00
|
|
|
/* No error, try again */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -1;
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 18:45:15 +00:00
|
|
|
|
|
|
|
/* Make request, see how many bytes we can get back. */
|
|
|
|
tempbuf[0] = 1;
|
|
|
|
tempbuf[1] = bytes;
|
|
|
|
if (fwrite(tempbuf, sizeof(char), 2, fp) != 2 || fflush(fp) == EOF)
|
|
|
|
goto err;
|
|
|
|
if (fread(tempbuf, sizeof(char), 1, fp) != 1 || tempbuf[0] == 0)
|
|
|
|
goto err;
|
|
|
|
numbytes = tempbuf[0];
|
|
|
|
|
|
|
|
/* Which buffer are we using? */
|
|
|
|
mybuffer = buf == NULL;
|
|
|
|
if (mybuffer)
|
|
|
|
buf = tempbuf;
|
|
|
|
|
|
|
|
/* Read bytes. */
|
|
|
|
i = fread(buf, sizeof(char), numbytes, fp);
|
|
|
|
if (i < numbytes)
|
|
|
|
goto err;
|
|
|
|
ret = numbytes;
|
|
|
|
if (mybuffer)
|
2017-08-03 13:23:28 +00:00
|
|
|
RAND_add(tempbuf, i, i);
|
2017-07-08 18:45:15 +00:00
|
|
|
|
2001-01-09 16:44:59 +00:00
|
|
|
err:
|
2017-07-08 18:45:15 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
fclose(fp);
|
2017-07-06 04:56:20 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2001-02-07 22:13:38 +00:00
|
|
|
|
2008-11-12 03:58:08 +00:00
|
|
|
int RAND_egd_bytes(const char *path, int bytes)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-07-08 18:45:15 +00:00
|
|
|
int num;
|
2001-01-09 16:44:59 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
num = RAND_query_egd_bytes(path, NULL, bytes);
|
2016-11-11 03:13:13 +00:00
|
|
|
if (num < 0)
|
2017-07-08 18:45:15 +00:00
|
|
|
return -1;
|
|
|
|
if (RAND_status() != 1)
|
|
|
|
return -1;
|
|
|
|
return num;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-06-03 14:13:58 +00:00
|
|
|
|
2001-02-07 22:13:38 +00:00
|
|
|
int RAND_egd(const char *path)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
2017-08-03 13:23:28 +00:00
|
|
|
return RAND_egd_bytes(path, 255);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-06-03 14:13:58 +00:00
|
|
|
|
2016-01-13 00:02:16 +00:00
|
|
|
# endif
|
|
|
|
|
2000-02-24 02:51:47 +00:00
|
|
|
#endif
|