Index: openssl/crypto/rand/randfile.c |
=================================================================== |
--- openssl/crypto/rand/randfile.c (revision 105093) |
+++ openssl/crypto/rand/randfile.c (working copy) |
@@ -75,9 +75,7 @@ |
#ifndef NO_SYS_TYPES_H |
# include <sys/types.h> |
#endif |
-#ifdef MAC_OS_pre_X |
-# include <stat.h> |
-#else |
+#ifndef OPENSSL_NO_POSIX_IO |
# include <sys/stat.h> |
#endif |
@@ -111,12 +109,15 @@ |
* if bytes == -1, read complete file. */ |
MS_STATIC unsigned char buf[BUFSIZE]; |
+#ifndef OPENSSL_NO_POSIX_IO |
struct stat sb; |
+#endif |
int i,ret=0,n; |
FILE *in; |
if (file == NULL) return(0); |
+#ifndef OPENSSL_NO_POSIX_IO |
#ifdef PURIFY |
/* struct stat can have padding and unused fields that may not be |
* initialized in the call to stat(). We need to clear the entire |
@@ -125,9 +126,9 @@ |
*/ |
memset(&sb, 0, sizeof(sb)); |
#endif |
- |
if (stat(file,&sb) < 0) return(0); |
RAND_add(&sb,sizeof(sb),0.0); |
+#endif |
if (bytes == 0) return(ret); |
#ifdef OPENSSL_SYS_VMS |
@@ -136,14 +137,16 @@ |
in=fopen(file,"rb"); |
#endif |
if (in == NULL) goto err; |
-#if defined(S_ISBLK) && defined(S_ISCHR) |
- if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { |
+#if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPNESSL_NO_POSIX_IO) |
+ if (sb.st_mode & (S_IFBLK | S_IFCHR)) { |
/* this file is a device. we don't want read an infinite number |
* of bytes from a random device, nor do we want to use buffered |
* I/O because we will waste system entropy. |
*/ |
bytes = (bytes == -1) ? 2048 : bytes; /* ok, is 2048 enough? */ |
+#ifndef OPENSSL_NO_SETVBUF_IONBF |
setvbuf(in, NULL, _IONBF, 0); /* don't do buffered reads */ |
+#endif /* ndef OPENSSL_NO_SETVBUF_IONBF */ |
} |
#endif |
for (;;) |
@@ -179,6 +182,7 @@ |
int i,ret=0,rand_err=0; |
FILE *out = NULL; |
int n; |
+#ifndef OPENSSL_NO_POSIX_IO |
struct stat sb; |
i=stat(file,&sb); |
@@ -194,14 +198,16 @@ |
} |
#endif |
} |
+#endif |
-#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_VMS) |
+#if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && !defined(OPENSSL_SYS_VMS) |
{ |
- /* For some reason Win32 can't write to files created this way */ |
- |
+#ifndef O_BINARY |
+#define O_BINARY 0 |
+#endif |
/* chmod(..., 0600) is too late to protect the file, |
* permissions should be restrictive from the start */ |
- int fd = open(file, O_CREAT, 0600); |
+ int fd = open(file, O_WRONLY|O_CREAT|O_BINARY, 0600); |
if (fd != -1) |
out = fdopen(fd, "wb"); |
} |
@@ -265,7 +271,6 @@ |
const char *RAND_file_name(char *buf, size_t size) |
{ |
char *s=NULL; |
- int ok = 0; |
#ifdef __OpenBSD__ |
struct stat sb; |
#endif |
@@ -294,7 +299,6 @@ |
BUF_strlcat(buf,"/",size); |
#endif |
BUF_strlcat(buf,RFILE,size); |
- ok = 1; |
} |
else |
buf[0] = '\0'; /* no file name */ |
@@ -308,7 +312,7 @@ |
* to something hopefully decent if that isn't available. |
*/ |
- if (!ok) |
+ if (!buf[0]) |
if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) { |
return(NULL); |
} |