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