diff options
Diffstat (limited to 'src/lib/libcrypto/rand/randfile.c')
-rw-r--r-- | src/lib/libcrypto/rand/randfile.c | 33 |
1 files changed, 10 insertions, 23 deletions
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c index 4ed40b7b70..d108353bbc 100644 --- a/src/lib/libcrypto/rand/randfile.c +++ b/src/lib/libcrypto/rand/randfile.c | |||
@@ -75,7 +75,9 @@ | |||
75 | #ifndef NO_SYS_TYPES_H | 75 | #ifndef NO_SYS_TYPES_H |
76 | # include <sys/types.h> | 76 | # include <sys/types.h> |
77 | #endif | 77 | #endif |
78 | #ifndef OPENSSL_NO_POSIX_IO | 78 | #ifdef MAC_OS_pre_X |
79 | # include <stat.h> | ||
80 | #else | ||
79 | # include <sys/stat.h> | 81 | # include <sys/stat.h> |
80 | #endif | 82 | #endif |
81 | 83 | ||
@@ -109,26 +111,14 @@ int RAND_load_file(const char *file, long bytes) | |||
109 | * if bytes == -1, read complete file. */ | 111 | * if bytes == -1, read complete file. */ |
110 | 112 | ||
111 | MS_STATIC unsigned char buf[BUFSIZE]; | 113 | MS_STATIC unsigned char buf[BUFSIZE]; |
112 | #ifndef OPENSSL_NO_POSIX_IO | ||
113 | struct stat sb; | 114 | struct stat sb; |
114 | #endif | ||
115 | int i,ret=0,n; | 115 | int i,ret=0,n; |
116 | FILE *in; | 116 | FILE *in; |
117 | 117 | ||
118 | if (file == NULL) return(0); | 118 | if (file == NULL) return(0); |
119 | 119 | ||
120 | #ifndef OPENSSL_NO_POSIX_IO | ||
121 | #ifdef PURIFY | ||
122 | /* struct stat can have padding and unused fields that may not be | ||
123 | * initialized in the call to stat(). We need to clear the entire | ||
124 | * structure before calling RAND_add() to avoid complaints from | ||
125 | * applications such as Valgrind. | ||
126 | */ | ||
127 | memset(&sb, 0, sizeof(sb)); | ||
128 | #endif | ||
129 | if (stat(file,&sb) < 0) return(0); | 120 | if (stat(file,&sb) < 0) return(0); |
130 | RAND_add(&sb,sizeof(sb),0.0); | 121 | RAND_add(&sb,sizeof(sb),0.0); |
131 | #endif | ||
132 | if (bytes == 0) return(ret); | 122 | if (bytes == 0) return(ret); |
133 | 123 | ||
134 | #ifdef OPENSSL_SYS_VMS | 124 | #ifdef OPENSSL_SYS_VMS |
@@ -137,7 +127,7 @@ int RAND_load_file(const char *file, long bytes) | |||
137 | in=fopen(file,"rb"); | 127 | in=fopen(file,"rb"); |
138 | #endif | 128 | #endif |
139 | if (in == NULL) goto err; | 129 | if (in == NULL) goto err; |
140 | #if defined(S_IFBLK) && defined(S_IFCHR) && !defined(OPNESSL_NO_POSIX_IO) | 130 | #if defined(S_IFBLK) && defined(S_IFCHR) |
141 | if (sb.st_mode & (S_IFBLK | S_IFCHR)) { | 131 | if (sb.st_mode & (S_IFBLK | S_IFCHR)) { |
142 | /* this file is a device. we don't want read an infinite number | 132 | /* this file is a device. we don't want read an infinite number |
143 | * of bytes from a random device, nor do we want to use buffered | 133 | * of bytes from a random device, nor do we want to use buffered |
@@ -180,13 +170,12 @@ int RAND_write_file(const char *file) | |||
180 | int i,ret=0,rand_err=0; | 170 | int i,ret=0,rand_err=0; |
181 | FILE *out = NULL; | 171 | FILE *out = NULL; |
182 | int n; | 172 | int n; |
183 | #ifndef OPENSSL_NO_POSIX_IO | ||
184 | struct stat sb; | 173 | struct stat sb; |
185 | 174 | ||
186 | i=stat(file,&sb); | 175 | i=stat(file,&sb); |
187 | if (i != -1) { | 176 | if (i != -1) { |
188 | #if defined(S_ISBLK) && defined(S_ISCHR) | 177 | #if defined(S_IFBLK) && defined(S_IFCHR) |
189 | if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) { | 178 | if (sb.st_mode & (S_IFBLK | S_IFCHR)) { |
190 | /* this file is a device. we don't write back to it. | 179 | /* this file is a device. we don't write back to it. |
191 | * we "succeed" on the assumption this is some sort | 180 | * we "succeed" on the assumption this is some sort |
192 | * of random device. Otherwise attempting to write to | 181 | * of random device. Otherwise attempting to write to |
@@ -196,16 +185,14 @@ int RAND_write_file(const char *file) | |||
196 | } | 185 | } |
197 | #endif | 186 | #endif |
198 | } | 187 | } |
199 | #endif | ||
200 | 188 | ||
201 | #if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && !defined(OPENSSL_SYS_VMS) | 189 | #if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_VMS) |
202 | { | 190 | { |
203 | #ifndef O_BINARY | 191 | /* For some reason Win32 can't write to files created this way */ |
204 | #define O_BINARY 0 | 192 | |
205 | #endif | ||
206 | /* chmod(..., 0600) is too late to protect the file, | 193 | /* chmod(..., 0600) is too late to protect the file, |
207 | * permissions should be restrictive from the start */ | 194 | * permissions should be restrictive from the start */ |
208 | int fd = open(file, O_WRONLY|O_CREAT|O_BINARY, 0600); | 195 | int fd = open(file, O_CREAT, 0600); |
209 | if (fd != -1) | 196 | if (fd != -1) |
210 | out = fdopen(fd, "wb"); | 197 | out = fdopen(fd, "wb"); |
211 | } | 198 | } |