155714Skris/* crypto/bio/bss_file.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris/* 6055714Skris * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout 6155714Skris * with binary data (e.g. asn1parse -inform DER < xxx) under 6255714Skris * Windows 6355714Skris */ 6455714Skris 6555714Skris#ifndef HEADER_BSS_FILE_C 6655714Skris#define HEADER_BSS_FILE_C 6755714Skris 68160814Ssimon#if defined(__linux) || defined(__sun) || defined(__hpux) 69160814Ssimon/* Following definition aliases fopen to fopen64 on above mentioned 70160814Ssimon * platforms. This makes it possible to open and sequentially access 71160814Ssimon * files larger than 2GB from 32-bit application. It does not allow to 72160814Ssimon * traverse them beyond 2GB with fseek/ftell, but on the other hand *no* 73160814Ssimon * 32-bit platform permits that, not with fseek/ftell. Not to mention 74160814Ssimon * that breaking 2GB limit for seeking would require surgery to *our* 75160814Ssimon * API. But sequential access suffices for practical cases when you 76160814Ssimon * can run into large files, such as fingerprinting, so we can let API 77160814Ssimon * alone. For reference, the list of 32-bit platforms which allow for 78160814Ssimon * sequential access of large files without extra "magic" comprise *BSD, 79160814Ssimon * Darwin, IRIX... 80160814Ssimon */ 81160814Ssimon#ifndef _FILE_OFFSET_BITS 82160814Ssimon#define _FILE_OFFSET_BITS 64 83160814Ssimon#endif 84160814Ssimon#endif 85160814Ssimon 8655714Skris#include <stdio.h> 8755714Skris#include <errno.h> 8855714Skris#include "cryptlib.h" 89160814Ssimon#include "bio_lcl.h" 9055714Skris#include <openssl/err.h> 9155714Skris 92194206Ssimon#if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) 93194206Ssimon#include <nwfileio.h> 94194206Ssimon#endif 95194206Ssimon 96109998Smarkm#if !defined(OPENSSL_NO_STDIO) 9755714Skris 9868651Skrisstatic int MS_CALLBACK file_write(BIO *h, const char *buf, int num); 9968651Skrisstatic int MS_CALLBACK file_read(BIO *h, char *buf, int size); 10068651Skrisstatic int MS_CALLBACK file_puts(BIO *h, const char *str); 10168651Skrisstatic int MS_CALLBACK file_gets(BIO *h, char *str, int size); 10268651Skrisstatic long MS_CALLBACK file_ctrl(BIO *h, int cmd, long arg1, void *arg2); 10355714Skrisstatic int MS_CALLBACK file_new(BIO *h); 10455714Skrisstatic int MS_CALLBACK file_free(BIO *data); 10555714Skrisstatic BIO_METHOD methods_filep= 10655714Skris { 10755714Skris BIO_TYPE_FILE, 10855714Skris "FILE pointer", 10955714Skris file_write, 11055714Skris file_read, 11155714Skris file_puts, 11255714Skris file_gets, 11355714Skris file_ctrl, 11455714Skris file_new, 11555714Skris file_free, 11659191Skris NULL, 11755714Skris }; 11855714Skris 11955714SkrisBIO *BIO_new_file(const char *filename, const char *mode) 12055714Skris { 121238405Sjkim BIO *ret; 122238405Sjkim FILE *file=NULL; 12355714Skris 124238405Sjkim#if defined(_WIN32) && defined(CP_UTF8) 125238405Sjkim int sz, len_0 = (int)strlen(filename)+1; 126238405Sjkim DWORD flags; 127238405Sjkim 128238405Sjkim /* 129238405Sjkim * Basically there are three cases to cover: a) filename is 130238405Sjkim * pure ASCII string; b) actual UTF-8 encoded string and 131238405Sjkim * c) locale-ized string, i.e. one containing 8-bit 132238405Sjkim * characters that are meaningful in current system locale. 133238405Sjkim * If filename is pure ASCII or real UTF-8 encoded string, 134238405Sjkim * MultiByteToWideChar succeeds and _wfopen works. If 135238405Sjkim * filename is locale-ized string, chances are that 136238405Sjkim * MultiByteToWideChar fails reporting 137238405Sjkim * ERROR_NO_UNICODE_TRANSLATION, in which case we fall 138238405Sjkim * back to fopen... 139238405Sjkim */ 140238405Sjkim if ((sz=MultiByteToWideChar(CP_UTF8,(flags=MB_ERR_INVALID_CHARS), 141238405Sjkim filename,len_0,NULL,0))>0 || 142238405Sjkim (GetLastError()==ERROR_INVALID_FLAGS && 143238405Sjkim (sz=MultiByteToWideChar(CP_UTF8,(flags=0), 144238405Sjkim filename,len_0,NULL,0))>0) 145238405Sjkim ) 14655714Skris { 147238405Sjkim WCHAR wmode[8]; 148238405Sjkim WCHAR *wfilename = _alloca(sz*sizeof(WCHAR)); 149238405Sjkim 150238405Sjkim if (MultiByteToWideChar(CP_UTF8,flags, 151238405Sjkim filename,len_0,wfilename,sz) && 152238405Sjkim MultiByteToWideChar(CP_UTF8,0,mode,strlen(mode)+1, 153238405Sjkim wmode,sizeof(wmode)/sizeof(wmode[0])) && 154238405Sjkim (file=_wfopen(wfilename,wmode))==NULL && 155238405Sjkim (errno==ENOENT || errno==EBADF) 156238405Sjkim ) /* UTF-8 decode succeeded, but no file, filename 157238405Sjkim * could still have been locale-ized... */ 158238405Sjkim file = fopen(filename,mode); 159238405Sjkim } 160238405Sjkim else if (GetLastError()==ERROR_NO_UNICODE_TRANSLATION) 161238405Sjkim { 162238405Sjkim file = fopen(filename,mode); 163238405Sjkim } 164238405Sjkim#else 165238405Sjkim file=fopen(filename,mode); 166238405Sjkim#endif 167238405Sjkim if (file == NULL) 168238405Sjkim { 16955714Skris SYSerr(SYS_F_FOPEN,get_last_sys_error()); 17055714Skris ERR_add_error_data(5,"fopen('",filename,"','",mode,"')"); 171109998Smarkm if (errno == ENOENT) 172109998Smarkm BIOerr(BIO_F_BIO_NEW_FILE,BIO_R_NO_SUCH_FILE); 173109998Smarkm else 174109998Smarkm BIOerr(BIO_F_BIO_NEW_FILE,ERR_R_SYS_LIB); 17555714Skris return(NULL); 17655714Skris } 177238405Sjkim if ((ret=BIO_new(BIO_s_file())) == NULL) 178160814Ssimon { 179160814Ssimon fclose(file); 18055714Skris return(NULL); 181160814Ssimon } 18255714Skris 183160814Ssimon BIO_clear_flags(ret,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */ 18455714Skris BIO_set_fp(ret,file,BIO_CLOSE); 18555714Skris return(ret); 18655714Skris } 18755714Skris 18855714SkrisBIO *BIO_new_fp(FILE *stream, int close_flag) 18955714Skris { 19055714Skris BIO *ret; 19155714Skris 19255714Skris if ((ret=BIO_new(BIO_s_file())) == NULL) 19355714Skris return(NULL); 19455714Skris 195160814Ssimon BIO_set_flags(ret,BIO_FLAGS_UPLINK); /* redundant, left for documentation puposes */ 19655714Skris BIO_set_fp(ret,stream,close_flag); 19755714Skris return(ret); 19855714Skris } 19955714Skris 20055714SkrisBIO_METHOD *BIO_s_file(void) 20155714Skris { 20255714Skris return(&methods_filep); 20355714Skris } 20455714Skris 20555714Skrisstatic int MS_CALLBACK file_new(BIO *bi) 20655714Skris { 20755714Skris bi->init=0; 20855714Skris bi->num=0; 20955714Skris bi->ptr=NULL; 210160814Ssimon bi->flags=BIO_FLAGS_UPLINK; /* default to UPLINK */ 21155714Skris return(1); 21255714Skris } 21355714Skris 21455714Skrisstatic int MS_CALLBACK file_free(BIO *a) 21555714Skris { 21655714Skris if (a == NULL) return(0); 21755714Skris if (a->shutdown) 21855714Skris { 21955714Skris if ((a->init) && (a->ptr != NULL)) 22055714Skris { 221160814Ssimon if (a->flags&BIO_FLAGS_UPLINK) 222160814Ssimon UP_fclose (a->ptr); 223160814Ssimon else 224160814Ssimon fclose (a->ptr); 22555714Skris a->ptr=NULL; 226160814Ssimon a->flags=BIO_FLAGS_UPLINK; 22755714Skris } 22855714Skris a->init=0; 22955714Skris } 23055714Skris return(1); 23155714Skris } 23255714Skris 23355714Skrisstatic int MS_CALLBACK file_read(BIO *b, char *out, int outl) 23455714Skris { 23555714Skris int ret=0; 23655714Skris 23755714Skris if (b->init && (out != NULL)) 23855714Skris { 239160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 240160814Ssimon ret=UP_fread(out,1,(int)outl,b->ptr); 241160814Ssimon else 242160814Ssimon ret=fread(out,1,(int)outl,(FILE *)b->ptr); 243160814Ssimon if(ret == 0 && (b->flags&BIO_FLAGS_UPLINK)?UP_ferror((FILE *)b->ptr):ferror((FILE *)b->ptr)) 244109998Smarkm { 245109998Smarkm SYSerr(SYS_F_FREAD,get_last_sys_error()); 246109998Smarkm BIOerr(BIO_F_FILE_READ,ERR_R_SYS_LIB); 247109998Smarkm ret=-1; 248109998Smarkm } 24955714Skris } 25055714Skris return(ret); 25155714Skris } 25255714Skris 25368651Skrisstatic int MS_CALLBACK file_write(BIO *b, const char *in, int inl) 25455714Skris { 25555714Skris int ret=0; 25655714Skris 25755714Skris if (b->init && (in != NULL)) 25855714Skris { 259160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 260160814Ssimon ret=UP_fwrite(in,(int)inl,1,b->ptr); 261160814Ssimon else 262160814Ssimon ret=fwrite(in,(int)inl,1,(FILE *)b->ptr); 263160814Ssimon if (ret) 26455714Skris ret=inl; 26555714Skris /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ 26659191Skris /* according to Tim Hudson <tjh@cryptsoft.com>, the commented 26755714Skris * out version above can cause 'inl' write calls under 26855714Skris * some stupid stdio implementations (VMS) */ 26955714Skris } 27055714Skris return(ret); 27155714Skris } 27255714Skris 27368651Skrisstatic long MS_CALLBACK file_ctrl(BIO *b, int cmd, long num, void *ptr) 27455714Skris { 27555714Skris long ret=1; 27655714Skris FILE *fp=(FILE *)b->ptr; 27755714Skris FILE **fpp; 27855714Skris char p[4]; 27955714Skris 28055714Skris switch (cmd) 28155714Skris { 28255714Skris case BIO_C_FILE_SEEK: 28355714Skris case BIO_CTRL_RESET: 284160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 285160814Ssimon ret=(long)UP_fseek(b->ptr,num,0); 286160814Ssimon else 287160814Ssimon ret=(long)fseek(fp,num,0); 28855714Skris break; 28955714Skris case BIO_CTRL_EOF: 290160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 291160814Ssimon ret=(long)UP_feof(fp); 292160814Ssimon else 293160814Ssimon ret=(long)feof(fp); 29455714Skris break; 29555714Skris case BIO_C_FILE_TELL: 29655714Skris case BIO_CTRL_INFO: 297160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 298160814Ssimon ret=UP_ftell(b->ptr); 299160814Ssimon else 300160814Ssimon ret=ftell(fp); 30155714Skris break; 30255714Skris case BIO_C_SET_FILE_PTR: 30355714Skris file_free(b); 30455714Skris b->shutdown=(int)num&BIO_CLOSE; 305160814Ssimon b->ptr=ptr; 30655714Skris b->init=1; 307160814Ssimon#if BIO_FLAGS_UPLINK!=0 308160814Ssimon#if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES) 309160814Ssimon#define _IOB_ENTRIES 20 310160814Ssimon#endif 311160814Ssimon#if defined(_IOB_ENTRIES) 312160814Ssimon /* Safety net to catch purely internal BIO_set_fp calls */ 313160814Ssimon if ((size_t)ptr >= (size_t)stdin && 314160814Ssimon (size_t)ptr < (size_t)(stdin+_IOB_ENTRIES)) 315160814Ssimon BIO_clear_flags(b,BIO_FLAGS_UPLINK); 316160814Ssimon#endif 317160814Ssimon#endif 318206046Ssimon#ifdef UP_fsetmod 319160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 320206046Ssimon UP_fsetmod(b->ptr,(char)((num&BIO_FP_TEXT)?'t':'b')); 321160814Ssimon else 322160814Ssimon#endif 323142425Snectar { 324120631Snectar#if defined(OPENSSL_SYS_WINDOWS) 325194206Ssimon int fd = _fileno((FILE*)ptr); 32655714Skris if (num & BIO_FP_TEXT) 327142425Snectar _setmode(fd,_O_TEXT); 32855714Skris else 329142425Snectar _setmode(fd,_O_BINARY); 330160814Ssimon#elif defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_CLIB) 331160814Ssimon int fd = fileno((FILE*)ptr); 332238405Sjkim /* Under CLib there are differences in file modes */ 333160814Ssimon if (num & BIO_FP_TEXT) 334194206Ssimon setmode(fd,O_TEXT); 335160814Ssimon else 336194206Ssimon setmode(fd,O_BINARY); 337120631Snectar#elif defined(OPENSSL_SYS_MSDOS) 338120631Snectar int fd = fileno((FILE*)ptr); 339120631Snectar /* Set correct text/binary mode */ 340120631Snectar if (num & BIO_FP_TEXT) 341120631Snectar _setmode(fd,_O_TEXT); 342120631Snectar /* Dangerous to set stdin/stdout to raw (unless redirected) */ 343120631Snectar else 344120631Snectar { 345120631Snectar if (fd == STDIN_FILENO || fd == STDOUT_FILENO) 346120631Snectar { 347120631Snectar if (isatty(fd) <= 0) 348120631Snectar _setmode(fd,_O_BINARY); 349120631Snectar } 350120631Snectar else 351120631Snectar _setmode(fd,_O_BINARY); 352120631Snectar } 353238405Sjkim#elif defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN) 354142425Snectar int fd = fileno((FILE*)ptr); 355109998Smarkm if (num & BIO_FP_TEXT) 356142425Snectar setmode(fd, O_TEXT); 357109998Smarkm else 358142425Snectar setmode(fd, O_BINARY); 35955714Skris#endif 360142425Snectar } 36155714Skris break; 36255714Skris case BIO_C_SET_FILENAME: 36355714Skris file_free(b); 36455714Skris b->shutdown=(int)num&BIO_CLOSE; 36555714Skris if (num & BIO_FP_APPEND) 36655714Skris { 36755714Skris if (num & BIO_FP_READ) 368127128Snectar BUF_strlcpy(p,"a+",sizeof p); 369127128Snectar else BUF_strlcpy(p,"a",sizeof p); 37055714Skris } 37155714Skris else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) 372127128Snectar BUF_strlcpy(p,"r+",sizeof p); 37355714Skris else if (num & BIO_FP_WRITE) 374127128Snectar BUF_strlcpy(p,"w",sizeof p); 37555714Skris else if (num & BIO_FP_READ) 376127128Snectar BUF_strlcpy(p,"r",sizeof p); 37755714Skris else 37855714Skris { 37955714Skris BIOerr(BIO_F_FILE_CTRL,BIO_R_BAD_FOPEN_MODE); 38055714Skris ret=0; 38155714Skris break; 38255714Skris } 383160814Ssimon#if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_OS2) || defined(OPENSSL_SYS_WIN32_CYGWIN) 38455714Skris if (!(num & BIO_FP_TEXT)) 38555714Skris strcat(p,"b"); 38655714Skris else 38755714Skris strcat(p,"t"); 38855714Skris#endif 389160814Ssimon#if defined(OPENSSL_SYS_NETWARE) 390160814Ssimon if (!(num & BIO_FP_TEXT)) 391160814Ssimon strcat(p,"b"); 392160814Ssimon else 393160814Ssimon strcat(p,"t"); 394160814Ssimon#endif 39555714Skris fp=fopen(ptr,p); 39655714Skris if (fp == NULL) 39755714Skris { 39855714Skris SYSerr(SYS_F_FOPEN,get_last_sys_error()); 39955714Skris ERR_add_error_data(5,"fopen('",ptr,"','",p,"')"); 40055714Skris BIOerr(BIO_F_FILE_CTRL,ERR_R_SYS_LIB); 40155714Skris ret=0; 40255714Skris break; 40355714Skris } 404160814Ssimon b->ptr=fp; 40555714Skris b->init=1; 406160814Ssimon BIO_clear_flags(b,BIO_FLAGS_UPLINK); /* we did fopen -> we disengage UPLINK */ 40755714Skris break; 40855714Skris case BIO_C_GET_FILE_PTR: 40955714Skris /* the ptr parameter is actually a FILE ** in this case. */ 41055714Skris if (ptr != NULL) 41155714Skris { 41255714Skris fpp=(FILE **)ptr; 41355714Skris *fpp=(FILE *)b->ptr; 41455714Skris } 41555714Skris break; 41655714Skris case BIO_CTRL_GET_CLOSE: 41755714Skris ret=(long)b->shutdown; 41855714Skris break; 41955714Skris case BIO_CTRL_SET_CLOSE: 42055714Skris b->shutdown=(int)num; 42155714Skris break; 42255714Skris case BIO_CTRL_FLUSH: 423160814Ssimon if (b->flags&BIO_FLAGS_UPLINK) 424160814Ssimon UP_fflush(b->ptr); 425160814Ssimon else 426160814Ssimon fflush((FILE *)b->ptr); 42755714Skris break; 42855714Skris case BIO_CTRL_DUP: 42955714Skris ret=1; 43055714Skris break; 43155714Skris 43255714Skris case BIO_CTRL_WPENDING: 43355714Skris case BIO_CTRL_PENDING: 43455714Skris case BIO_CTRL_PUSH: 43555714Skris case BIO_CTRL_POP: 43655714Skris default: 43755714Skris ret=0; 43855714Skris break; 43955714Skris } 44055714Skris return(ret); 44155714Skris } 44255714Skris 44355714Skrisstatic int MS_CALLBACK file_gets(BIO *bp, char *buf, int size) 44455714Skris { 44555714Skris int ret=0; 44655714Skris 44755714Skris buf[0]='\0'; 448160814Ssimon if (bp->flags&BIO_FLAGS_UPLINK) 449205128Ssimon { 450205128Ssimon if (!UP_fgets(buf,size,bp->ptr)) 451205128Ssimon goto err; 452205128Ssimon } 453160814Ssimon else 454205128Ssimon { 455205128Ssimon if (!fgets(buf,size,(FILE *)bp->ptr)) 456205128Ssimon goto err; 457205128Ssimon } 45855714Skris if (buf[0] != '\0') 45955714Skris ret=strlen(buf); 460205128Ssimon err: 46155714Skris return(ret); 46255714Skris } 46355714Skris 46468651Skrisstatic int MS_CALLBACK file_puts(BIO *bp, const char *str) 46555714Skris { 46655714Skris int n,ret; 46755714Skris 46855714Skris n=strlen(str); 46955714Skris ret=file_write(bp,str,n); 47055714Skris return(ret); 47155714Skris } 47255714Skris 473109998Smarkm#endif /* OPENSSL_NO_STDIO */ 47455714Skris 47555714Skris#endif /* HEADER_BSS_FILE_C */ 47655714Skris 47755714Skris 478