1 /* Copyright (C) 1995-1998 Eric Young (eay (at) cryptsoft.com) 2 * All rights reserved. 3 * 4 * This package is an SSL implementation written 5 * by Eric Young (eay (at) cryptsoft.com). 6 * The implementation was written so as to conform with Netscapes SSL. 7 * 8 * This library is free for commercial and non-commercial use as long as 9 * the following conditions are aheared to. The following conditions 10 * apply to all code found in this distribution, be it the RC4, RSA, 11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * included with this distribution is covered by the same copyright terms 13 * except that the holder is Tim Hudson (tjh (at) cryptsoft.com). 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. 17 * If this package is used in a product, Eric Young should be given attribution 18 * as the author of the parts of the library used. 19 * This can be in the form of a textual message at program startup or 20 * in documentation (online or textual) provided with the package. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. All advertising materials mentioning features or use of this software 31 * must display the following acknowledgement: 32 * "This product includes cryptographic software written by 33 * Eric Young (eay (at) cryptsoft.com)" 34 * The word 'cryptographic' can be left out if the rouines from the library 35 * being used are not cryptographic related :-). 36 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * the apps directory (application code) you must include an acknowledgement: 38 * "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)" 39 * 40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * The licence and distribution terms for any publically available version or 53 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * copied and put under another distribution licence 55 * [including the GNU Public Licence.] */ 56 57 #if defined(__linux) || defined(__sun) || defined(__hpux) 58 /* Following definition aliases fopen to fopen64 on above mentioned 59 * platforms. This makes it possible to open and sequentially access 60 * files larger than 2GB from 32-bit application. It does not allow to 61 * traverse them beyond 2GB with fseek/ftell, but on the other hand *no* 62 * 32-bit platform permits that, not with fseek/ftell. Not to mention 63 * that breaking 2GB limit for seeking would require surgery to *our* 64 * API. But sequential access suffices for practical cases when you 65 * can run into large files, such as fingerprinting, so we can let API 66 * alone. For reference, the list of 32-bit platforms which allow for 67 * sequential access of large files without extra "magic" comprise *BSD, 68 * Darwin, IRIX... 69 */ 70 #ifndef _FILE_OFFSET_BITS 71 #define _FILE_OFFSET_BITS 64 72 #endif 73 #endif 74 75 #include <openssl/bio.h> 76 77 #include <errno.h> 78 #include <stdio.h> 79 #include <string.h> 80 81 #include <openssl/buf.h> 82 #include <openssl/err.h> 83 #include <openssl/mem.h> 84 85 86 #define BIO_FP_READ 0x02 87 #define BIO_FP_WRITE 0x04 88 #define BIO_FP_APPEND 0x08 89 90 BIO *BIO_new_file(const char *filename, const char *mode) { 91 BIO *ret; 92 FILE *file; 93 94 file = fopen(filename, mode); 95 if (file == NULL) { 96 OPENSSL_PUT_SYSTEM_ERROR(); 97 98 ERR_add_error_data(5, "fopen('", filename, "','", mode, "')"); 99 if (errno == ENOENT) { 100 OPENSSL_PUT_ERROR(BIO, BIO_R_NO_SUCH_FILE); 101 } else { 102 OPENSSL_PUT_ERROR(BIO, BIO_R_SYS_LIB); 103 } 104 return NULL; 105 } 106 107 ret = BIO_new(BIO_s_file()); 108 if (ret == NULL) { 109 fclose(file); 110 return NULL; 111 } 112 113 BIO_set_fp(ret, file, BIO_CLOSE); 114 return ret; 115 } 116 117 BIO *BIO_new_fp(FILE *stream, int close_flag) { 118 BIO *ret = BIO_new(BIO_s_file()); 119 120 if (ret == NULL) { 121 return NULL; 122 } 123 124 BIO_set_fp(ret, stream, close_flag); 125 return ret; 126 } 127 128 static int file_new(BIO *bio) { return 1; } 129 130 static int file_free(BIO *bio) { 131 if (bio == NULL) { 132 return 0; 133 } 134 135 if (!bio->shutdown) { 136 return 1; 137 } 138 139 if (bio->init && bio->ptr != NULL) { 140 fclose(bio->ptr); 141 bio->ptr = NULL; 142 } 143 bio->init = 0; 144 145 return 1; 146 } 147 148 static int file_read(BIO *b, char *out, int outl) { 149 if (!b->init) { 150 return 0; 151 } 152 153 size_t ret = fread(out, 1, outl, (FILE *)b->ptr); 154 if (ret == 0 && ferror((FILE *)b->ptr)) { 155 OPENSSL_PUT_SYSTEM_ERROR(); 156 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); 157 return -1; 158 } 159 160 /* fread reads at most |outl| bytes, so |ret| fits in an int. */ 161 return (int)ret; 162 } 163 164 static int file_write(BIO *b, const char *in, int inl) { 165 int ret = 0; 166 167 if (!b->init) { 168 return 0; 169 } 170 171 ret = fwrite(in, inl, 1, (FILE *)b->ptr); 172 if (ret > 0) { 173 ret = inl; 174 } 175 return ret; 176 } 177 178 static long file_ctrl(BIO *b, int cmd, long num, void *ptr) { 179 long ret = 1; 180 FILE *fp = (FILE *)b->ptr; 181 FILE **fpp; 182 char p[4]; 183 184 switch (cmd) { 185 case BIO_CTRL_RESET: 186 num = 0; 187 case BIO_C_FILE_SEEK: 188 ret = (long)fseek(fp, num, 0); 189 break; 190 case BIO_CTRL_EOF: 191 ret = (long)feof(fp); 192 break; 193 case BIO_C_FILE_TELL: 194 case BIO_CTRL_INFO: 195 ret = ftell(fp); 196 break; 197 case BIO_C_SET_FILE_PTR: 198 file_free(b); 199 b->shutdown = (int)num & BIO_CLOSE; 200 b->ptr = ptr; 201 b->init = 1; 202 break; 203 case BIO_C_SET_FILENAME: 204 file_free(b); 205 b->shutdown = (int)num & BIO_CLOSE; 206 if (num & BIO_FP_APPEND) { 207 if (num & BIO_FP_READ) { 208 BUF_strlcpy(p, "a+", sizeof(p)); 209 } else { 210 BUF_strlcpy(p, "a", sizeof(p)); 211 } 212 } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) { 213 BUF_strlcpy(p, "r+", sizeof(p)); 214 } else if (num & BIO_FP_WRITE) { 215 BUF_strlcpy(p, "w", sizeof(p)); 216 } else if (num & BIO_FP_READ) { 217 BUF_strlcpy(p, "r", sizeof(p)); 218 } else { 219 OPENSSL_PUT_ERROR(BIO, BIO_R_BAD_FOPEN_MODE); 220 ret = 0; 221 break; 222 } 223 fp = fopen(ptr, p); 224 if (fp == NULL) { 225 OPENSSL_PUT_SYSTEM_ERROR(); 226 ERR_add_error_data(5, "fopen('", ptr, "','", p, "')"); 227 OPENSSL_PUT_ERROR(BIO, ERR_R_SYS_LIB); 228 ret = 0; 229 break; 230 } 231 b->ptr = fp; 232 b->init = 1; 233 break; 234 case BIO_C_GET_FILE_PTR: 235 /* the ptr parameter is actually a FILE ** in this case. */ 236 if (ptr != NULL) { 237 fpp = (FILE **)ptr; 238 *fpp = (FILE *)b->ptr; 239 } 240 break; 241 case BIO_CTRL_GET_CLOSE: 242 ret = (long)b->shutdown; 243 break; 244 case BIO_CTRL_SET_CLOSE: 245 b->shutdown = (int)num; 246 break; 247 case BIO_CTRL_FLUSH: 248 ret = 0 == fflush((FILE *)b->ptr); 249 break; 250 case BIO_CTRL_WPENDING: 251 case BIO_CTRL_PENDING: 252 default: 253 ret = 0; 254 break; 255 } 256 return ret; 257 } 258 259 static int file_gets(BIO *bp, char *buf, int size) { 260 int ret = 0; 261 262 if (size == 0) { 263 return 0; 264 } 265 266 if (!fgets(buf, size, (FILE *)bp->ptr)) { 267 buf[0] = 0; 268 goto err; 269 } 270 ret = strlen(buf); 271 272 err: 273 return ret; 274 } 275 276 static const BIO_METHOD methods_filep = { 277 BIO_TYPE_FILE, "FILE pointer", 278 file_write, file_read, 279 NULL /* puts */, file_gets, 280 file_ctrl, file_new, 281 file_free, NULL /* callback_ctrl */, 282 }; 283 284 const BIO_METHOD *BIO_s_file(void) { return &methods_filep; } 285 286 287 int BIO_get_fp(BIO *bio, FILE **out_file) { 288 return BIO_ctrl(bio, BIO_C_GET_FILE_PTR, 0, (char*) out_file); 289 } 290 291 int BIO_set_fp(BIO *bio, FILE *file, int close_flag) { 292 return BIO_ctrl(bio, BIO_C_SET_FILE_PTR, close_flag, (char *) file); 293 } 294 295 int BIO_read_filename(BIO *bio, const char *filename) { 296 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_READ, 297 (char *)filename); 298 } 299 300 int BIO_write_filename(BIO *bio, const char *filename) { 301 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_WRITE, 302 (char *)filename); 303 } 304 305 int BIO_append_filename(BIO *bio, const char *filename) { 306 return BIO_ctrl(bio, BIO_C_SET_FILENAME, BIO_CLOSE | BIO_FP_APPEND, 307 (char *)filename); 308 } 309 310 int BIO_rw_filename(BIO *bio, const char *filename) { 311 return BIO_ctrl(bio, BIO_C_SET_FILENAME, 312 BIO_CLOSE | BIO_FP_READ | BIO_FP_WRITE, (char *)filename); 313 } 314