BIO *BIO_new_file(const char *filename, const char *mode) { BIO *ret; FILE *file = openssl_fopen(filename, mode); int fp_flags = BIO_CLOSE; if (strchr(mode, 'b') == NULL) fp_flags |= BIO_FP_TEXT; if (file == NULL) { SYSerr(SYS_F_FOPEN, get_last_sys_error()); ERR_add_error_data(5, "fopen('", filename, "','", mode, "')"); if (errno == ENOENT # ifdef ENXIO || errno == ENXIO # endif ) BIOerr(BIO_F_BIO_NEW_FILE, BIO_R_NO_SUCH_FILE); else BIOerr(BIO_F_BIO_NEW_FILE, ERR_R_SYS_LIB); return NULL; } if ((ret = BIO_new(BIO_s_file())) == NULL) { fclose(file); return NULL; } BIO_clear_flags(ret, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage * UPLINK */ BIO_set_fp(ret, file, fp_flags); return ret; }
int RAND_write_file(const char *file) { unsigned char buf[BUFSIZE]; int i, ret = 0, rand_err = 0; FILE *out = NULL; int n; #ifndef OPENSSL_NO_POSIX_IO struct stat sb; # if defined(S_ISBLK) && defined(S_ISCHR) # ifdef _WIN32 /* * Check for |file| being a driver as "ASCII-safe" on Windows, * because driver paths are always ASCII. */ # endif i = stat(file, &sb); if (i != -1) { 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 and chmod the device * causes problems. */ return 1; } # endif } #endif #if defined(O_CREAT) && !defined(OPENSSL_NO_POSIX_IO) && \ !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WINDOWS) { # 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_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 = openssl_fopen(file, "rb+"); #endif if (out == NULL) out = openssl_fopen(file, "wb"); if (out == NULL) goto err; #if !defined(NO_CHMOD) && !defined(OPENSSL_NO_POSIX_IO) chmod(file, 0600); #endif n = RAND_DATA; for (;;) { i = (n > BUFSIZE) ? BUFSIZE : n; n -= BUFSIZE; if (RAND_bytes(buf, i) <= 0) rand_err = 1; i = fwrite(buf, 1, i, out); if (i <= 0) { ret = 0; break; } ret += i; if (n <= 0) break; } fclose(out); OPENSSL_cleanse(buf, BUFSIZE); err: return (rand_err ? -1 : ret); }
int RAND_load_file(const char *file, long bytes) { /*- * If bytes >= 0, read up to 'bytes' bytes. * if bytes == -1, read complete file. */ unsigned char buf[BUFSIZE]; #ifndef OPENSSL_NO_POSIX_IO struct stat sb; #endif int i, ret = 0, n; FILE *in = NULL; if (file == NULL) return 0; if (bytes == 0) return ret; in = openssl_fopen(file, "rb"); if (in == NULL) goto err; #ifndef OPENSSL_NO_POSIX_IO /* * 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)); if (fstat(fileno(in), &sb) < 0) goto err; RAND_add(&sb, sizeof(sb), 0.0); # 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 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? */ setbuf(in, NULL); /* don't do buffered reads */ } # endif #endif for (;;) { if (bytes > 0) n = (bytes < BUFSIZE) ? (int)bytes : BUFSIZE; else n = BUFSIZE; i = fread(buf, 1, n, in); if (i <= 0) break; RAND_add(buf, i, (double)i); ret += i; if (bytes > 0) { bytes -= n; if (bytes <= 0) break; } } OPENSSL_cleanse(buf, BUFSIZE); err: if (in != NULL) fclose(in); return ret; }
static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { long ret = 1; FILE *fp = (FILE *)b->ptr; FILE **fpp; char p[4]; int st; switch (cmd) { case BIO_C_FILE_SEEK: case BIO_CTRL_RESET: if (b->flags & BIO_FLAGS_UPLINK) ret = (long)UP_fseek(b->ptr, num, 0); else ret = (long)fseek(fp, num, 0); break; case BIO_CTRL_EOF: if (b->flags & BIO_FLAGS_UPLINK) ret = (long)UP_feof(fp); else ret = (long)feof(fp); break; case BIO_C_FILE_TELL: case BIO_CTRL_INFO: if (b->flags & BIO_FLAGS_UPLINK) ret = UP_ftell(b->ptr); else ret = ftell(fp); break; case BIO_C_SET_FILE_PTR: file_free(b); b->shutdown = (int)num & BIO_CLOSE; b->ptr = ptr; b->init = 1; # if BIO_FLAGS_UPLINK!=0 # if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES) # define _IOB_ENTRIES 20 # endif /* Safety net to catch purely internal BIO_set_fp calls */ # if defined(_MSC_VER) && _MSC_VER>=1900 if (ptr == stdin || ptr == stdout || ptr == stderr) BIO_clear_flags(b, BIO_FLAGS_UPLINK); # elif defined(_IOB_ENTRIES) if ((size_t)ptr >= (size_t)stdin && (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES)) BIO_clear_flags(b, BIO_FLAGS_UPLINK); # endif # endif # ifdef UP_fsetmod if (b->flags & BIO_FLAGS_UPLINK) UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b')); else # endif { # if defined(OPENSSL_SYS_WINDOWS) int fd = _fileno((FILE *)ptr); if (num & BIO_FP_TEXT) _setmode(fd, _O_TEXT); else _setmode(fd, _O_BINARY); # elif defined(OPENSSL_SYS_MSDOS) int fd = fileno((FILE *)ptr); /* Set correct text/binary mode */ if (num & BIO_FP_TEXT) _setmode(fd, _O_TEXT); /* Dangerous to set stdin/stdout to raw (unless redirected) */ else { if (fd == STDIN_FILENO || fd == STDOUT_FILENO) { if (isatty(fd) <= 0) _setmode(fd, _O_BINARY); } else _setmode(fd, _O_BINARY); } # elif defined(OPENSSL_SYS_WIN32_CYGWIN) int fd = fileno((FILE *)ptr); if (!(num & BIO_FP_TEXT)) setmode(fd, O_BINARY); # endif } break; case BIO_C_SET_FILENAME: file_free(b); b->shutdown = (int)num & BIO_CLOSE; if (num & BIO_FP_APPEND) { if (num & BIO_FP_READ) OPENSSL_strlcpy(p, "a+", sizeof(p)); else OPENSSL_strlcpy(p, "a", sizeof(p)); } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) OPENSSL_strlcpy(p, "r+", sizeof(p)); else if (num & BIO_FP_WRITE) OPENSSL_strlcpy(p, "w", sizeof(p)); else if (num & BIO_FP_READ) OPENSSL_strlcpy(p, "r", sizeof(p)); else { BIOerr(BIO_F_FILE_CTRL, BIO_R_BAD_FOPEN_MODE); ret = 0; break; } # if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) if (!(num & BIO_FP_TEXT)) OPENSSL_strlcat(p, "b", sizeof(p)); else OPENSSL_strlcat(p, "t", sizeof(p)); # elif defined(OPENSSL_SYS_WIN32_CYGWIN) if (!(num & BIO_FP_TEXT)) OPENSSL_strlcat(p, "b", sizeof(p)); # endif fp = openssl_fopen(ptr, p); if (fp == NULL) { SYSerr(SYS_F_FOPEN, get_last_sys_error()); ERR_add_error_data(5, "fopen('", ptr, "','", p, "')"); BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB); ret = 0; break; } b->ptr = fp; b->init = 1; BIO_clear_flags(b, BIO_FLAGS_UPLINK); /* we did fopen -> we disengage * UPLINK */ break; case BIO_C_GET_FILE_PTR: /* the ptr parameter is actually a FILE ** in this case. */ if (ptr != NULL) { fpp = (FILE **)ptr; *fpp = (FILE *)b->ptr; } break; case BIO_CTRL_GET_CLOSE: ret = (long)b->shutdown; break; case BIO_CTRL_SET_CLOSE: b->shutdown = (int)num; break; case BIO_CTRL_FLUSH: st = b->flags & BIO_FLAGS_UPLINK ? UP_fflush(b->ptr) : fflush((FILE *)b->ptr); if (st == EOF) { SYSerr(SYS_F_FFLUSH, get_last_sys_error()); ERR_add_error_data(1, "fflush()"); BIOerr(BIO_F_FILE_CTRL, ERR_R_SYS_LIB); ret = 0; } break; case BIO_CTRL_DUP: ret = 1; break; case BIO_CTRL_WPENDING: case BIO_CTRL_PENDING: case BIO_CTRL_PUSH: case BIO_CTRL_POP: default: ret = 0; break; } return ret; }