summaryrefslogtreecommitdiff
path: root/crypto/rand/randfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/rand/randfile.c')
-rw-r--r--crypto/rand/randfile.c107
1 files changed, 74 insertions, 33 deletions
diff --git a/crypto/rand/randfile.c b/crypto/rand/randfile.c
index dda020fb10..4ed40b7b70 100644
--- a/crypto/rand/randfile.c
+++ b/crypto/rand/randfile.c
@@ -57,7 +57,7 @@
*/
/* We need to define this to get macros like S_IFBLK and S_IFCHR */
-#define _XOPEN_SOURCE 1
+#define _XOPEN_SOURCE 500
#include <errno.h>
#include <stdio.h>
@@ -75,16 +75,29 @@
#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
+#ifdef _WIN32
+#define stat _stat
+#define chmod _chmod
+#define open _open
+#define fdopen _fdopen
+#endif
+
#undef BUFSIZE
#define BUFSIZE 1024
#define RAND_DATA 1024
+#ifdef OPENSSL_SYS_VMS
+/* This declaration is a nasty hack to get around vms' extension to fopen
+ * for passing in sharing options being disabled by our /STANDARD=ANSI89 */
+static FILE *(*const vms_fopen)(const char *, const char *, ...) =
+ (FILE *(*)(const char *, const char *, ...))fopen;
+#define VMS_OPEN_ATTRS "shr=get,put,upd,del","ctx=bin,stm","rfm=stm","rat=none","mrs=0"
+#endif
+
/* #define RFILE ".rnd" - defined in ../../e_os.h */
/* Note that these functions are intended for seed files only.
@@ -96,21 +109,35 @@ int RAND_load_file(const char *file, long bytes)
* 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);
- i=stat(file,&sb);
- /* If the state fails, put some crap in anyway */
+#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
+ * structure before calling RAND_add() to avoid complaints from
+ * applications such as Valgrind.
+ */
+ memset(&sb, 0, sizeof(sb));
+#endif
+ if (stat(file,&sb) < 0) return(0);
RAND_add(&sb,sizeof(sb),0.0);
- if (i < 0) return(0);
+#endif
if (bytes == 0) return(ret);
+#ifdef OPENSSL_SYS_VMS
+ in=vms_fopen(file,"rb",VMS_OPEN_ATTRS);
+#else
in=fopen(file,"rb");
+#endif
if (in == NULL) goto err;
-#if defined(S_IFBLK) && defined(S_IFCHR)
+#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
@@ -128,8 +155,12 @@ int RAND_load_file(const char *file, long bytes)
n = BUFSIZE;
i=fread(buf,1,n,in);
if (i <= 0) break;
+#ifdef PURIFY
+ RAND_add(buf,i,(double)i);
+#else
/* even if n != i, use the full array */
RAND_add(buf,n,(double)i);
+#endif
ret+=i;
if (bytes > 0)
{
@@ -149,12 +180,13 @@ int RAND_write_file(const char *file)
int i,ret=0,rand_err=0;
FILE *out = NULL;
int n;
+#ifndef OPENSSL_NO_POSIX_IO
struct stat sb;
i=stat(file,&sb);
if (i != -1) {
-#if defined(S_IFBLK) && defined(S_IFCHR)
- if (sb.st_mode & (S_IFBLK | S_IFCHR)) {
+#if defined(S_ISBLK) && defined(S_ISCHR)
+ if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
/* this file is a device. we don't write back to it.
* we "succeed" on the assumption this is some sort
* of random device. Otherwise attempting to write to
@@ -164,20 +196,48 @@ int RAND_write_file(const char *file)
}
#endif
}
+#endif
-#if defined(O_CREAT) && !defined(OPENSSL_SYS_WIN32)
+#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");
}
#endif
+
+#ifdef OPENSSL_SYS_VMS
+ /* VMS NOTE: Prior versions of this routine created a _new_
+ * version of the rand file for each call into this routine, then
+ * deleted all existing versions named ;-1, and finally renamed
+ * the current version as ';1'. Under concurrent usage, this
+ * resulted in an RMS race condition in rename() which could
+ * orphan files (see vms message help for RMS$_REENT). With the
+ * fopen() calls below, openssl/VMS now shares the top-level
+ * version of the rand file. Note that there may still be
+ * conditions where the top-level rand file is locked. If so, this
+ * code will then create a new version of the rand file. Without
+ * the delete and rename code, this can result in ascending file
+ * versions that stop at version 32767, and this routine will then
+ * return an error. The remedy for this is to recode the calling
+ * application to avoid concurrent use of the rand file, or
+ * synchronize usage at the application level. Also consider
+ * whether or not you NEED a persistent rand file in a concurrent
+ * use situation.
+ */
+
+ out = vms_fopen(file,"rb+",VMS_OPEN_ATTRS);
+ if (out == NULL)
+ out = vms_fopen(file,"wb",VMS_OPEN_ATTRS);
+#else
if (out == NULL)
out = fopen(file,"wb");
+#endif
if (out == NULL) goto err;
#ifndef NO_CHMOD
@@ -199,25 +259,6 @@ int RAND_write_file(const char *file)
ret+=i;
if (n <= 0) break;
}
-#ifdef OPENSSL_SYS_VMS
- /* Try to delete older versions of the file, until there aren't
- any */
- {
- char *tmpf;
-
- tmpf = OPENSSL_malloc(strlen(file) + 4); /* to add ";-1" and a nul */
- if (tmpf)
- {
- strcpy(tmpf, file);
- strcat(tmpf, ";-1");
- while(delete(tmpf) == 0)
- ;
- rename(file,";1"); /* Make sure it's version 1, or we
- will reach the limit (32767) at
- some point... */
- }
- }
-#endif /* OPENSSL_SYS_VMS */
fclose(out);
OPENSSL_cleanse(buf,BUFSIZE);