1*acf64401Sbeck /* $OpenBSD: bss_file.c,v 1.35 2023/07/05 21:23:37 beck Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker * All rights reserved.
45b37fcf3Sryker *
55b37fcf3Sryker * This package is an SSL implementation written
65b37fcf3Sryker * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker *
95b37fcf3Sryker * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker * the following conditions are aheared to. The following conditions
115b37fcf3Sryker * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker * lhash, DES, etc., code; not just the SSL code. The SSL documentation
135b37fcf3Sryker * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker *
165b37fcf3Sryker * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker * the code are not to be removed.
185b37fcf3Sryker * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker * as the author of the parts of the library used.
205b37fcf3Sryker * This can be in the form of a textual message at program startup or
215b37fcf3Sryker * in documentation (online or textual) provided with the package.
225b37fcf3Sryker *
235b37fcf3Sryker * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker * modification, are permitted provided that the following conditions
255b37fcf3Sryker * are met:
265b37fcf3Sryker * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker * notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker * notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker * documentation and/or other materials provided with the distribution.
315b37fcf3Sryker * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker * must display the following acknowledgement:
335b37fcf3Sryker * "This product includes cryptographic software written by
345b37fcf3Sryker * Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker * The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker * being used are not cryptographic related :-).
375b37fcf3Sryker * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker * the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker *
415b37fcf3Sryker * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker * SUCH DAMAGE.
525b37fcf3Sryker *
535b37fcf3Sryker * The licence and distribution terms for any publically available version or
545b37fcf3Sryker * derivative of this code cannot be changed. i.e. this code cannot simply be
555b37fcf3Sryker * copied and put under another distribution licence
565b37fcf3Sryker * [including the GNU Public Licence.]
575b37fcf3Sryker */
585b37fcf3Sryker
595b37fcf3Sryker /*
605b37fcf3Sryker * 03-Dec-1997 rdenny@dc3.com Fix bug preventing use of stdin/stdout
615b37fcf3Sryker * with binary data (e.g. asn1parse -inform DER < xxx) under
625b37fcf3Sryker * Windows
635b37fcf3Sryker */
645b37fcf3Sryker
655b37fcf3Sryker #ifndef HEADER_BSS_FILE_C
665b37fcf3Sryker #define HEADER_BSS_FILE_C
675b37fcf3Sryker
684fcf65c5Sdjm #if defined(__linux) || defined(__sun) || defined(__hpux)
694fcf65c5Sdjm /* Following definition aliases fopen to fopen64 on above mentioned
704fcf65c5Sdjm * platforms. This makes it possible to open and sequentially access
714fcf65c5Sdjm * files larger than 2GB from 32-bit application. It does not allow to
724fcf65c5Sdjm * traverse them beyond 2GB with fseek/ftell, but on the other hand *no*
734fcf65c5Sdjm * 32-bit platform permits that, not with fseek/ftell. Not to mention
744fcf65c5Sdjm * that breaking 2GB limit for seeking would require surgery to *our*
754fcf65c5Sdjm * API. But sequential access suffices for practical cases when you
764fcf65c5Sdjm * can run into large files, such as fingerprinting, so we can let API
774fcf65c5Sdjm * alone. For reference, the list of 32-bit platforms which allow for
784fcf65c5Sdjm * sequential access of large files without extra "magic" comprise *BSD,
794fcf65c5Sdjm * Darwin, IRIX...
804fcf65c5Sdjm */
814fcf65c5Sdjm #ifndef _FILE_OFFSET_BITS
824fcf65c5Sdjm #define _FILE_OFFSET_BITS 64
834fcf65c5Sdjm #endif
844fcf65c5Sdjm #endif
854fcf65c5Sdjm
865b37fcf3Sryker #include <errno.h>
87a8913c44Sjsing #include <stdio.h>
88a8913c44Sjsing #include <string.h>
89a8913c44Sjsing
9087d6f0d1Smiod #include <openssl/bio.h>
91913ec974Sbeck #include <openssl/err.h>
925b37fcf3Sryker
9394b1984eStb #include "bio_local.h"
9494b1984eStb
958b5c64d9Sbeck static int file_write(BIO *h, const char *buf, int num);
968b5c64d9Sbeck static int file_read(BIO *h, char *buf, int size);
978b5c64d9Sbeck static int file_puts(BIO *h, const char *str);
988b5c64d9Sbeck static int file_gets(BIO *h, char *str, int size);
998b5c64d9Sbeck static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2);
1008b5c64d9Sbeck static int file_new(BIO *h);
1018b5c64d9Sbeck static int file_free(BIO *data);
102c3d505beSjsing
103cc83181bStb static const BIO_METHOD methods_filep = {
104e402ce74Smiod .type = BIO_TYPE_FILE,
105e402ce74Smiod .name = "FILE pointer",
106e402ce74Smiod .bwrite = file_write,
107e402ce74Smiod .bread = file_read,
108e402ce74Smiod .bputs = file_puts,
109e402ce74Smiod .bgets = file_gets,
110e402ce74Smiod .ctrl = file_ctrl,
111e402ce74Smiod .create = file_new,
112e402ce74Smiod .destroy = file_free
1135b37fcf3Sryker };
1145b37fcf3Sryker
115ae7f143bSderaadt BIO *
BIO_new_file(const char * filename,const char * mode)116ae7f143bSderaadt BIO_new_file(const char *filename, const char *mode)
1175b37fcf3Sryker {
1185b37fcf3Sryker BIO *ret;
1190a5d6edeSdjm FILE *file = NULL;
1205b37fcf3Sryker
1210a5d6edeSdjm file = fopen(filename, mode);
122c3d505beSjsing
123c3d505beSjsing if (file == NULL) {
1245067ae9fSbeck SYSerror(errno);
1250f637b92Sbeck ERR_asprintf_error_data("fopen('%s', '%s')", filename, mode);
126da347917Sbeck if (errno == ENOENT)
1275067ae9fSbeck BIOerror(BIO_R_NO_SUCH_FILE);
128da347917Sbeck else
1295067ae9fSbeck BIOerror(ERR_R_SYS_LIB);
1305b37fcf3Sryker return (NULL);
1315b37fcf3Sryker }
132c3d505beSjsing if ((ret = BIO_new(BIO_s_file())) == NULL) {
1334fcf65c5Sdjm fclose(file);
1345b37fcf3Sryker return (NULL);
1354fcf65c5Sdjm }
1365b37fcf3Sryker
1375b37fcf3Sryker BIO_set_fp(ret, file, BIO_CLOSE);
1385b37fcf3Sryker return (ret);
1395b37fcf3Sryker }
140*acf64401Sbeck LCRYPTO_ALIAS(BIO_new_file);
1415b37fcf3Sryker
142ae7f143bSderaadt BIO *
BIO_new_fp(FILE * stream,int close_flag)143ae7f143bSderaadt BIO_new_fp(FILE *stream, int close_flag)
1445b37fcf3Sryker {
1455b37fcf3Sryker BIO *ret;
1465b37fcf3Sryker
1475b37fcf3Sryker if ((ret = BIO_new(BIO_s_file())) == NULL)
1485b37fcf3Sryker return (NULL);
1495b37fcf3Sryker
1505b37fcf3Sryker BIO_set_fp(ret, stream, close_flag);
1515b37fcf3Sryker return (ret);
1525b37fcf3Sryker }
153*acf64401Sbeck LCRYPTO_ALIAS(BIO_new_fp);
1545b37fcf3Sryker
155cc83181bStb const BIO_METHOD *
BIO_s_file(void)156ae7f143bSderaadt BIO_s_file(void)
1575b37fcf3Sryker {
1585b37fcf3Sryker return (&methods_filep);
1595b37fcf3Sryker }
160*acf64401Sbeck LCRYPTO_ALIAS(BIO_s_file);
1615b37fcf3Sryker
162c3d505beSjsing static int
file_new(BIO * bi)163c3d505beSjsing file_new(BIO *bi)
1645b37fcf3Sryker {
1655b37fcf3Sryker bi->init = 0;
1665b37fcf3Sryker bi->num = 0;
1675b37fcf3Sryker bi->ptr = NULL;
16887d6f0d1Smiod bi->flags=0;
1695b37fcf3Sryker return (1);
1705b37fcf3Sryker }
1715b37fcf3Sryker
172c3d505beSjsing static int
file_free(BIO * a)173c3d505beSjsing file_free(BIO *a)
1745b37fcf3Sryker {
175c3d505beSjsing if (a == NULL)
176c3d505beSjsing return (0);
177c3d505beSjsing if (a->shutdown) {
178c3d505beSjsing if ((a->init) && (a->ptr != NULL)) {
1794fcf65c5Sdjm fclose (a->ptr);
1805b37fcf3Sryker a->ptr = NULL;
18187d6f0d1Smiod a->flags = 0;
1825b37fcf3Sryker }
1835b37fcf3Sryker a->init = 0;
1845b37fcf3Sryker }
1855b37fcf3Sryker return (1);
1865b37fcf3Sryker }
1875b37fcf3Sryker
188c3d505beSjsing static int
file_read(BIO * b,char * out,int outl)189c3d505beSjsing file_read(BIO *b, char *out, int outl)
1905b37fcf3Sryker {
1915b37fcf3Sryker int ret = 0;
1925b37fcf3Sryker
193ed7d0629Smiod if (b->init && out != NULL) {
194ed7d0629Smiod ret = fread(out, 1, outl, (FILE *)b->ptr);
19587d6f0d1Smiod if (ret == 0 && ferror((FILE *)b->ptr)) {
1965067ae9fSbeck SYSerror(errno);
1975067ae9fSbeck BIOerror(ERR_R_SYS_LIB);
1981f9308f9Smarkus ret = -1;
1991f9308f9Smarkus }
2005b37fcf3Sryker }
2015b37fcf3Sryker return (ret);
2025b37fcf3Sryker }
2035b37fcf3Sryker
204c3d505beSjsing static int
file_write(BIO * b,const char * in,int inl)205c3d505beSjsing file_write(BIO *b, const char *in, int inl)
2065b37fcf3Sryker {
2075b37fcf3Sryker int ret = 0;
2085b37fcf3Sryker
209ed7d0629Smiod if (b->init && in != NULL)
210ed7d0629Smiod ret = fwrite(in, 1, inl, (FILE *)b->ptr);
2115b37fcf3Sryker return (ret);
2125b37fcf3Sryker }
2135b37fcf3Sryker
214c3d505beSjsing static long
file_ctrl(BIO * b,int cmd,long num,void * ptr)215c3d505beSjsing file_ctrl(BIO *b, int cmd, long num, void *ptr)
2165b37fcf3Sryker {
2175b37fcf3Sryker long ret = 1;
2185b37fcf3Sryker FILE *fp = (FILE *)b->ptr;
2195b37fcf3Sryker FILE **fpp;
2205b37fcf3Sryker char p[4];
2215b37fcf3Sryker
222c3d505beSjsing switch (cmd) {
223913ec974Sbeck case BIO_C_FILE_SEEK:
2245b37fcf3Sryker case BIO_CTRL_RESET:
2250a5d6edeSdjm ret = (long)fseek(fp, num, 0);
2265b37fcf3Sryker break;
2275b37fcf3Sryker case BIO_CTRL_EOF:
2285b37fcf3Sryker ret = (long)feof(fp);
2295b37fcf3Sryker break;
230913ec974Sbeck case BIO_C_FILE_TELL:
2315b37fcf3Sryker case BIO_CTRL_INFO:
2325b37fcf3Sryker ret = ftell(fp);
2335b37fcf3Sryker break;
2345b37fcf3Sryker case BIO_C_SET_FILE_PTR:
2355b37fcf3Sryker file_free(b);
236913ec974Sbeck b->shutdown = (int)num&BIO_CLOSE;
2374fcf65c5Sdjm b->ptr = ptr;
2385b37fcf3Sryker b->init = 1;
2395b37fcf3Sryker break;
2405b37fcf3Sryker case BIO_C_SET_FILENAME:
2415b37fcf3Sryker file_free(b);
2425b37fcf3Sryker b->shutdown = (int)num&BIO_CLOSE;
243c3d505beSjsing if (num & BIO_FP_APPEND) {
2445b37fcf3Sryker if (num & BIO_FP_READ)
245143cedbbSbeck strlcpy(p, "a+", sizeof p);
246143cedbbSbeck else strlcpy(p, "a", sizeof p);
247c3d505beSjsing } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE))
248143cedbbSbeck strlcpy(p, "r+", sizeof p);
2495b37fcf3Sryker else if (num & BIO_FP_WRITE)
250143cedbbSbeck strlcpy(p, "w", sizeof p);
2515b37fcf3Sryker else if (num & BIO_FP_READ)
252143cedbbSbeck strlcpy(p, "r", sizeof p);
253c3d505beSjsing else {
2545067ae9fSbeck BIOerror(BIO_R_BAD_FOPEN_MODE);
2555b37fcf3Sryker ret = 0;
2565b37fcf3Sryker break;
2575b37fcf3Sryker }
2585b37fcf3Sryker fp = fopen(ptr, p);
259c3d505beSjsing if (fp == NULL) {
2605067ae9fSbeck SYSerror(errno);
2610f637b92Sbeck ERR_asprintf_error_data("fopen('%s', '%s')", ptr, p);
2625067ae9fSbeck BIOerror(ERR_R_SYS_LIB);
2635b37fcf3Sryker ret = 0;
2645b37fcf3Sryker break;
2655b37fcf3Sryker }
2664fcf65c5Sdjm b->ptr = fp;
2675b37fcf3Sryker b->init = 1;
2685b37fcf3Sryker break;
2695b37fcf3Sryker case BIO_C_GET_FILE_PTR:
2705b37fcf3Sryker /* the ptr parameter is actually a FILE ** in this case. */
271c3d505beSjsing if (ptr != NULL) {
2725b37fcf3Sryker fpp = (FILE **)ptr;
2735b37fcf3Sryker *fpp = (FILE *)b->ptr;
2745b37fcf3Sryker }
2755b37fcf3Sryker break;
2765b37fcf3Sryker case BIO_CTRL_GET_CLOSE:
2775b37fcf3Sryker ret = (long)b->shutdown;
2785b37fcf3Sryker break;
2795b37fcf3Sryker case BIO_CTRL_SET_CLOSE:
2805b37fcf3Sryker b->shutdown = (int)num;
2815b37fcf3Sryker break;
2825b37fcf3Sryker case BIO_CTRL_FLUSH:
2835b37fcf3Sryker fflush((FILE *)b->ptr);
2845b37fcf3Sryker break;
2855b37fcf3Sryker case BIO_CTRL_DUP:
2865b37fcf3Sryker ret = 1;
2875b37fcf3Sryker break;
2885b37fcf3Sryker
2895b37fcf3Sryker case BIO_CTRL_WPENDING:
2905b37fcf3Sryker case BIO_CTRL_PENDING:
2915b37fcf3Sryker case BIO_CTRL_PUSH:
2925b37fcf3Sryker case BIO_CTRL_POP:
2935b37fcf3Sryker default:
2945b37fcf3Sryker ret = 0;
2955b37fcf3Sryker break;
2965b37fcf3Sryker }
2975b37fcf3Sryker return (ret);
2985b37fcf3Sryker }
2995b37fcf3Sryker
300c3d505beSjsing static int
file_gets(BIO * bp,char * buf,int size)301c3d505beSjsing file_gets(BIO *bp, char *buf, int size)
3025b37fcf3Sryker {
3035b37fcf3Sryker int ret = 0;
3045b37fcf3Sryker
3055b37fcf3Sryker buf[0] = '\0';
3060a5d6edeSdjm if (!fgets(buf, size,(FILE *)bp->ptr))
3070a5d6edeSdjm goto err;
3085b37fcf3Sryker if (buf[0] != '\0')
3095b37fcf3Sryker ret = strlen(buf);
3100a5d6edeSdjm err:
3115b37fcf3Sryker return (ret);
3125b37fcf3Sryker }
3135b37fcf3Sryker
314c3d505beSjsing static int
file_puts(BIO * bp,const char * str)315c3d505beSjsing file_puts(BIO *bp, const char *str)
3165b37fcf3Sryker {
3175b37fcf3Sryker int n, ret;
3185b37fcf3Sryker
3195b37fcf3Sryker n = strlen(str);
3205b37fcf3Sryker ret = file_write(bp, str, n);
3215b37fcf3Sryker return (ret);
3225b37fcf3Sryker }
3235b37fcf3Sryker
3245b37fcf3Sryker
3255b37fcf3Sryker #endif /* HEADER_BSS_FILE_C */
326