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.c70
1 files changed, 22 insertions, 48 deletions
diff --git a/src/lib/libcrypto/rand/randfile.c b/src/lib/libcrypto/rand/randfile.c
index d108353bbc..005cb38cb0 100644
--- a/src/lib/libcrypto/rand/randfile.c
+++ b/src/lib/libcrypto/rand/randfile.c
@@ -81,25 +81,10 @@
81# include <sys/stat.h> 81# include <sys/stat.h>
82#endif 82#endif
83 83
84#ifdef _WIN32
85#define stat _stat
86#define chmod _chmod
87#define open _open
88#define fdopen _fdopen
89#endif
90
91#undef BUFSIZE 84#undef BUFSIZE
92#define BUFSIZE 1024 85#define BUFSIZE 1024
93#define RAND_DATA 1024 86#define RAND_DATA 1024
94 87
95#ifdef OPENSSL_SYS_VMS
96/* This declaration is a nasty hack to get around vms' extension to fopen
97 * for passing in sharing options being disabled by our /STANDARD=ANSI89 */
98static FILE *(*const vms_fopen)(const char *, const char *, ...) =
99 (FILE *(*)(const char *, const char *, ...))fopen;
100#define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
101#endif
102
103/* #define RFILE ".rnd" - defined in ../../e_os.h */ 88/* #define RFILE ".rnd" - defined in ../../e_os.h */
104 89
105/* Note that these functions are intended for seed files only. 90/* Note that these functions are intended for seed files only.
@@ -121,11 +106,7 @@ int RAND_load_file(const char *file, long bytes)
121 RAND_add(&sb,sizeof(sb),0.0); 106 RAND_add(&sb,sizeof(sb),0.0);
122 if (bytes == 0) return(ret); 107 if (bytes == 0) return(ret);
123 108
124#ifdef OPENSSL_SYS_VMS
125 in=vms_fopen(file,"rb",VMS_OPEN_ATTRS);
126#else
127 in=fopen(file,"rb"); 109 in=fopen(file,"rb");
128#endif
129 if (in == NULL) goto err; 110 if (in == NULL) goto err;
130#if defined(S_IFBLK) && defined(S_IFCHR) 111#if defined(S_IFBLK) && defined(S_IFCHR)
131 if (sb.st_mode & (S_IFBLK | S_IFCHR)) { 112 if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
@@ -186,7 +167,7 @@ int RAND_write_file(const char *file)
186#endif 167#endif
187 } 168 }
188 169
189#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32) && !defined(OPENSSL_SYS_VMS) 170#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
190 { 171 {
191 /* For some reason Win32 can't write to files created this way */ 172 /* For some reason Win32 can't write to files created this way */
192 173
@@ -197,34 +178,8 @@ int RAND_write_file(const char *file)
197 out = fdopen(fd, "wb"); 178 out = fdopen(fd, "wb");
198 } 179 }
199#endif 180#endif
200
201#ifdef OPENSSL_SYS_VMS
202 /* VMS NOTE: Prior versions of this routine created a _new_
203 * version of the rand file for each call into this routine, then
204 * deleted all existing versions named ;-1, and finally renamed
205 * the current version as ';1'. Under concurrent usage, this
206 * resulted in an RMS race condition in rename() which could
207 * orphan files (see vms message help for RMS$_REENT). With the
208 * fopen() calls below, openssl/VMS now shares the top-level
209 * version of the rand file. Note that there may still be
210 * conditions where the top-level rand file is locked. If so, this
211 * code will then create a new version of the rand file. Without
212 * the delete and rename code, this can result in ascending file
213 * versions that stop at version 32767, and this routine will then
214 * return an error. The remedy for this is to recode the calling
215 * application to avoid concurrent use of the rand file, or
216 * synchronize usage at the application level. Also consider
217 * whether or not you NEED a persistent rand file in a concurrent
218 * use situation.
219 */
220
221 out = vms_fopen(file,"rb+",VMS_OPEN_ATTRS);
222 if (out == NULL)
223 out = vms_fopen(file,"wb",VMS_OPEN_ATTRS);
224#else
225 if (out == NULL) 181 if (out == NULL)
226 out = fopen(file,"wb"); 182 out = fopen(file,"wb");
227#endif
228 if (out == NULL) goto err; 183 if (out == NULL) goto err;
229 184
230#ifndef NO_CHMOD 185#ifndef NO_CHMOD
@@ -246,6 +201,25 @@ int RAND_write_file(const char *file)
246 ret+=i; 201 ret+=i;
247 if (n <= 0) break; 202 if (n <= 0) break;
248 } 203 }
204#ifdef OPENSSL_SYS_VMS
205 /* Try to delete older versions of the file, until there aren't
206 any */
207 {
208 char *tmpf;
209
210 tmpf = OPENSSL_malloc(strlen(file) + 4); /* to add ";-1" and a nul */
211 if (tmpf)
212 {
213 strcpy(tmpf, file);
214 strcat(tmpf, ";-1");
215 while(delete(tmpf) == 0)
216 ;
217 rename(file,";1"); /* Make sure it's version 1, or we
218 will reach the limit (32767) at
219 some point... */
220 }
221 }
222#endif /* OPENSSL_SYS_VMS */
249 223
250 fclose(out); 224 fclose(out);
251 OPENSSL_cleanse(buf,BUFSIZE); 225 OPENSSL_cleanse(buf,BUFSIZE);
@@ -261,7 +235,7 @@ const char *RAND_file_name(char *buf, size_t size)
261 struct stat sb; 235 struct stat sb;
262#endif 236#endif
263 237
264 if (OPENSSL_issetugid() == 0) 238 if (issetugid() == 0)
265 s=getenv("RANDFILE"); 239 s=getenv("RANDFILE");
266 if (s != NULL && *s && strlen(s) + 1 < size) 240 if (s != NULL && *s && strlen(s) + 1 < size)
267 { 241 {
@@ -270,7 +244,7 @@ const char *RAND_file_name(char *buf, size_t size)
270 } 244 }
271 else 245 else
272 { 246 {
273 if (OPENSSL_issetugid() == 0) 247 if (issetugid() == 0)
274 s=getenv("HOME"); 248 s=getenv("HOME");
275#ifdef DEFAULT_HOME 249#ifdef DEFAULT_HOME
276 if (s == NULL) 250 if (s == NULL)