diff options
author | jkim <jkim@FreeBSD.org> | 2012-07-11 23:31:36 +0000 |
---|---|---|
committer | jkim <jkim@FreeBSD.org> | 2012-07-11 23:31:36 +0000 |
commit | 532b4084cb8cac5e6d91d42aa6a497dd4ba4a4f5 (patch) | |
tree | 0c30591ac90cb5e07a0763793709fd1056b67f57 /crypto/rand/randfile.c | |
parent | 1554498e64df093a519f9074c3412047f398aa17 (diff) | |
download | FreeBSD-src-532b4084cb8cac5e6d91d42aa6a497dd4ba4a4f5.zip FreeBSD-src-532b4084cb8cac5e6d91d42aa6a497dd4ba4a4f5.tar.gz |
Import OpenSSL 1.0.1c.
Approved by: benl (maintainer)
Diffstat (limited to 'crypto/rand/randfile.c')
-rw-r--r-- | crypto/rand/randfile.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/crypto/rand/randfile.c b/crypto/rand/randfile.c index 1810568..030e07f 100644 --- a/crypto/rand/randfile.c +++ b/crypto/rand/randfile.c @@ -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 @@ 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); +#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 @@ int RAND_load_file(const char *file, long bytes) */ 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 @@ int RAND_load_file(const char *file, long bytes) 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(OPENSSL_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 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); @@ -194,14 +198,16 @@ int RAND_write_file(const char *file) } #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"); } @@ -266,7 +272,6 @@ const char *RAND_file_name(char *buf, size_t size) { char *s=NULL; #ifdef __OpenBSD__ - int ok = 0; struct stat sb; #endif @@ -294,9 +299,6 @@ const char *RAND_file_name(char *buf, size_t size) BUF_strlcat(buf,"/",size); #endif BUF_strlcat(buf,RFILE,size); -#ifdef __OpenBSD__ - ok = 1; -#endif } else buf[0] = '\0'; /* no file name */ @@ -310,7 +312,7 @@ const char *RAND_file_name(char *buf, size_t size) * to something hopefully decent if that isn't available. */ - if (!ok) + if (!buf[0]) if (BUF_strlcpy(buf,"/dev/arandom",size) >= size) { return(NULL); } |