/src/openssl33/crypto/bio/bss_file.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #if defined(__linux) || defined(__sun) || defined(__hpux) |
11 | | /* |
12 | | * Following definition aliases fopen to fopen64 on above mentioned |
13 | | * platforms. This makes it possible to open and sequentially access files |
14 | | * larger than 2GB from 32-bit application. It does not allow one to traverse |
15 | | * them beyond 2GB with fseek/ftell, but on the other hand *no* 32-bit |
16 | | * platform permits that, not with fseek/ftell. Not to mention that breaking |
17 | | * 2GB limit for seeking would require surgery to *our* API. But sequential |
18 | | * access suffices for practical cases when you can run into large files, |
19 | | * such as fingerprinting, so we can let API alone. For reference, the list |
20 | | * of 32-bit platforms which allow for sequential access of large files |
21 | | * without extra "magic" comprise *BSD, Darwin, IRIX... |
22 | | */ |
23 | | #ifndef _FILE_OFFSET_BITS |
24 | | #define _FILE_OFFSET_BITS 64 |
25 | | #endif |
26 | | #endif |
27 | | |
28 | | #include <stdio.h> |
29 | | #include <errno.h> |
30 | | #include "bio_local.h" |
31 | | #include <openssl/err.h> |
32 | | |
33 | | #if !defined(OPENSSL_NO_STDIO) |
34 | | |
35 | | static int file_write(BIO *h, const char *buf, int num); |
36 | | static int file_read(BIO *h, char *buf, int size); |
37 | | static int file_puts(BIO *h, const char *str); |
38 | | static int file_gets(BIO *h, char *str, int size); |
39 | | static long file_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
40 | | static int file_new(BIO *h); |
41 | | static int file_free(BIO *data); |
42 | | static const BIO_METHOD methods_filep = { |
43 | | BIO_TYPE_FILE, |
44 | | "FILE pointer", |
45 | | bwrite_conv, |
46 | | file_write, |
47 | | bread_conv, |
48 | | file_read, |
49 | | file_puts, |
50 | | file_gets, |
51 | | file_ctrl, |
52 | | file_new, |
53 | | file_free, |
54 | | NULL, /* file_callback_ctrl */ |
55 | | }; |
56 | | |
57 | | BIO *BIO_new_file(const char *filename, const char *mode) |
58 | 11.5k | { |
59 | 11.5k | BIO *ret; |
60 | 11.5k | FILE *file = openssl_fopen(filename, mode); |
61 | 11.5k | int fp_flags = BIO_CLOSE; |
62 | | |
63 | 11.5k | if (strchr(mode, 'b') == NULL) |
64 | 11.3k | fp_flags |= BIO_FP_TEXT; |
65 | | |
66 | 11.5k | if (file == NULL) { |
67 | 8.88k | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
68 | 8.88k | "calling fopen(%s, %s)", |
69 | 8.88k | filename, mode); |
70 | 8.88k | if (errno == ENOENT |
71 | 8.69k | #ifdef ENXIO |
72 | 8.88k | || errno == ENXIO |
73 | 8.88k | #endif |
74 | 8.88k | ) |
75 | 8.88k | ERR_raise(ERR_LIB_BIO, BIO_R_NO_SUCH_FILE); |
76 | 8.64k | else |
77 | 8.88k | ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
78 | 8.88k | return NULL; |
79 | 8.88k | } |
80 | 2.65k | if ((ret = BIO_new(BIO_s_file())) == NULL) { |
81 | 0 | fclose(file); |
82 | 0 | return NULL; |
83 | 0 | } |
84 | | |
85 | | /* we did fopen -> we disengage UPLINK */ |
86 | 2.65k | BIO_clear_flags(ret, BIO_FLAGS_UPLINK_INTERNAL); |
87 | 2.65k | BIO_set_fp(ret, file, fp_flags); |
88 | 2.65k | return ret; |
89 | 2.65k | } |
90 | | |
91 | | BIO *BIO_new_fp(FILE *stream, int close_flag) |
92 | 124k | { |
93 | 124k | BIO *ret; |
94 | | |
95 | 124k | if ((ret = BIO_new(BIO_s_file())) == NULL) |
96 | 0 | return NULL; |
97 | | |
98 | | /* redundant flag, left for documentation purposes */ |
99 | 124k | BIO_set_flags(ret, BIO_FLAGS_UPLINK_INTERNAL); |
100 | 124k | BIO_set_fp(ret, stream, close_flag); |
101 | 124k | return ret; |
102 | 124k | } |
103 | | |
104 | | const BIO_METHOD *BIO_s_file(void) |
105 | 127k | { |
106 | 127k | return &methods_filep; |
107 | 127k | } |
108 | | |
109 | | static int file_new(BIO *bi) |
110 | 127k | { |
111 | 127k | bi->init = 0; |
112 | 127k | bi->num = 0; |
113 | 127k | bi->ptr = NULL; |
114 | 127k | bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* default to UPLINK */ |
115 | 127k | return 1; |
116 | 127k | } |
117 | | |
118 | | static int file_free(BIO *a) |
119 | 254k | { |
120 | 254k | if (a == NULL) |
121 | 0 | return 0; |
122 | 254k | if (a->shutdown) { |
123 | 129k | if ((a->init) && (a->ptr != NULL)) { |
124 | 2.65k | if (a->flags & BIO_FLAGS_UPLINK_INTERNAL) |
125 | 0 | UP_fclose(a->ptr); |
126 | 2.65k | else |
127 | 2.65k | fclose(a->ptr); |
128 | 2.65k | a->ptr = NULL; |
129 | 2.65k | a->flags = BIO_FLAGS_UPLINK_INTERNAL; |
130 | 2.65k | } |
131 | 129k | a->init = 0; |
132 | 129k | } |
133 | 254k | return 1; |
134 | 254k | } |
135 | | |
136 | | static int file_read(BIO *b, char *out, int outl) |
137 | 0 | { |
138 | 0 | int ret = 0; |
139 | |
|
140 | 0 | if (b->init && (out != NULL)) { |
141 | 0 | if (b->flags & BIO_FLAGS_UPLINK_INTERNAL) |
142 | 0 | ret = UP_fread(out, 1, (int)outl, b->ptr); |
143 | 0 | else |
144 | 0 | ret = fread(out, 1, (int)outl, (FILE *)b->ptr); |
145 | 0 | if (ret == 0 |
146 | 0 | && (b->flags & BIO_FLAGS_UPLINK_INTERNAL |
147 | 0 | ? UP_ferror((FILE *)b->ptr) |
148 | 0 | : ferror((FILE *)b->ptr))) { |
149 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
150 | 0 | "calling fread()"); |
151 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
152 | 0 | ret = -1; |
153 | 0 | } |
154 | 0 | } |
155 | 0 | return ret; |
156 | 0 | } |
157 | | |
158 | | static int file_write(BIO *b, const char *in, int inl) |
159 | 0 | { |
160 | 0 | int ret = 0; |
161 | |
|
162 | 0 | if (b->init && (in != NULL)) { |
163 | 0 | if (b->flags & BIO_FLAGS_UPLINK_INTERNAL) |
164 | 0 | ret = UP_fwrite(in, (int)inl, 1, b->ptr); |
165 | 0 | else |
166 | 0 | ret = fwrite(in, (int)inl, 1, (FILE *)b->ptr); |
167 | 0 | if (ret) |
168 | 0 | ret = inl; |
169 | | /* ret=fwrite(in,1,(int)inl,(FILE *)b->ptr); */ |
170 | | /* |
171 | | * according to Tim Hudson <tjh@openssl.org>, the commented out |
172 | | * version above can cause 'inl' write calls under some stupid stdio |
173 | | * implementations (VMS) |
174 | | */ |
175 | 0 | } |
176 | 0 | return ret; |
177 | 0 | } |
178 | | |
179 | | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) |
180 | 127k | { |
181 | 127k | long ret = 1; |
182 | 127k | FILE *fp = (FILE *)b->ptr; |
183 | 127k | FILE **fpp; |
184 | 127k | char p[4]; |
185 | 127k | int st; |
186 | | |
187 | 127k | switch (cmd) { |
188 | 0 | case BIO_C_FILE_SEEK: |
189 | 0 | case BIO_CTRL_RESET: |
190 | 0 | if (b->flags & BIO_FLAGS_UPLINK_INTERNAL) |
191 | 0 | ret = (long)UP_fseek(b->ptr, num, 0); |
192 | 0 | else |
193 | 0 | ret = (long)fseek(fp, num, 0); |
194 | 0 | break; |
195 | 0 | case BIO_CTRL_EOF: |
196 | 0 | if (b->flags & BIO_FLAGS_UPLINK_INTERNAL) |
197 | 0 | ret = (long)UP_feof(fp); |
198 | 0 | else |
199 | 0 | ret = (long)feof(fp); |
200 | 0 | break; |
201 | 0 | case BIO_C_FILE_TELL: |
202 | 0 | case BIO_CTRL_INFO: |
203 | 0 | if (b->flags & BIO_FLAGS_UPLINK_INTERNAL) |
204 | 0 | ret = UP_ftell(b->ptr); |
205 | 0 | else |
206 | 0 | ret = ftell(fp); |
207 | 0 | break; |
208 | 127k | case BIO_C_SET_FILE_PTR: |
209 | 127k | file_free(b); |
210 | 127k | b->shutdown = (int)num & BIO_CLOSE; |
211 | 127k | b->ptr = ptr; |
212 | 127k | b->init = 1; |
213 | | #if BIO_FLAGS_UPLINK_INTERNAL != 0 |
214 | | #if defined(__MINGW32__) && defined(__MSVCRT__) && !defined(_IOB_ENTRIES) |
215 | | #define _IOB_ENTRIES 20 |
216 | | #endif |
217 | | /* Safety net to catch purely internal BIO_set_fp calls */ |
218 | | #if (defined(_MSC_VER) && _MSC_VER >= 1900) || defined(__BORLANDC__) |
219 | | if (ptr == stdin || ptr == stdout || ptr == stderr) |
220 | | BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL); |
221 | | #elif defined(_IOB_ENTRIES) |
222 | | if ((size_t)ptr >= (size_t)stdin && (size_t)ptr < (size_t)(stdin + _IOB_ENTRIES)) |
223 | | BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL); |
224 | | #endif |
225 | | #endif |
226 | | #ifdef UP_fsetmod |
227 | | if (b->flags & BIO_FLAGS_UPLINK_INTERNAL) |
228 | | UP_fsetmod(b->ptr, (char)((num & BIO_FP_TEXT) ? 't' : 'b')); |
229 | | else |
230 | | #endif |
231 | 127k | { |
232 | | #if defined(OPENSSL_SYS_WINDOWS) |
233 | | int fd = _fileno((FILE *)ptr); |
234 | | if (num & BIO_FP_TEXT) |
235 | | _setmode(fd, _O_TEXT); |
236 | | else |
237 | | _setmode(fd, _O_BINARY); |
238 | | /* |
239 | | * Reports show that ftell() isn't trustable in text mode. |
240 | | * This has been confirmed as a bug in the Universal C RTL, see |
241 | | * https://developercommunity.visualstudio.com/content/problem/425878/fseek-ftell-fail-in-text-mode-for-unix-style-text.html |
242 | | * The suggested work-around from Microsoft engineering is to |
243 | | * turn off buffering until the bug is resolved. |
244 | | */ |
245 | | if ((num & BIO_FP_TEXT) != 0) |
246 | | setvbuf((FILE *)ptr, NULL, _IONBF, 0); |
247 | | #elif defined(OPENSSL_SYS_MSDOS) |
248 | | int fd = fileno((FILE *)ptr); |
249 | | /* Set correct text/binary mode */ |
250 | | if (num & BIO_FP_TEXT) |
251 | | _setmode(fd, _O_TEXT); |
252 | | /* Dangerous to set stdin/stdout to raw (unless redirected) */ |
253 | | else { |
254 | | if (fd == STDIN_FILENO || fd == STDOUT_FILENO) { |
255 | | if (isatty(fd) <= 0) |
256 | | _setmode(fd, _O_BINARY); |
257 | | } else |
258 | | _setmode(fd, _O_BINARY); |
259 | | } |
260 | | #elif defined(OPENSSL_SYS_WIN32_CYGWIN) |
261 | | int fd = fileno((FILE *)ptr); |
262 | | if (!(num & BIO_FP_TEXT)) |
263 | | setmode(fd, O_BINARY); |
264 | | #endif |
265 | 127k | } |
266 | 127k | break; |
267 | 0 | case BIO_C_SET_FILENAME: |
268 | 0 | file_free(b); |
269 | 0 | b->shutdown = (int)num & BIO_CLOSE; |
270 | 0 | if (num & BIO_FP_APPEND) { |
271 | 0 | if (num & BIO_FP_READ) |
272 | 0 | OPENSSL_strlcpy(p, "a+", sizeof(p)); |
273 | 0 | else |
274 | 0 | OPENSSL_strlcpy(p, "a", sizeof(p)); |
275 | 0 | } else if ((num & BIO_FP_READ) && (num & BIO_FP_WRITE)) |
276 | 0 | OPENSSL_strlcpy(p, "r+", sizeof(p)); |
277 | 0 | else if (num & BIO_FP_WRITE) |
278 | 0 | OPENSSL_strlcpy(p, "w", sizeof(p)); |
279 | 0 | else if (num & BIO_FP_READ) |
280 | 0 | OPENSSL_strlcpy(p, "r", sizeof(p)); |
281 | 0 | else { |
282 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_BAD_FOPEN_MODE); |
283 | 0 | ret = 0; |
284 | 0 | break; |
285 | 0 | } |
286 | | #if defined(OPENSSL_SYS_MSDOS) || defined(OPENSSL_SYS_WINDOWS) |
287 | | if (!(num & BIO_FP_TEXT)) |
288 | | OPENSSL_strlcat(p, "b", sizeof(p)); |
289 | | else |
290 | | OPENSSL_strlcat(p, "t", sizeof(p)); |
291 | | #elif defined(OPENSSL_SYS_WIN32_CYGWIN) |
292 | | if (!(num & BIO_FP_TEXT)) |
293 | | OPENSSL_strlcat(p, "b", sizeof(p)); |
294 | | #endif |
295 | 0 | fp = openssl_fopen(ptr, p); |
296 | 0 | if (fp == NULL) { |
297 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
298 | 0 | "calling fopen(%s, %s)", |
299 | 0 | (const char *)ptr, p); |
300 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
301 | 0 | ret = 0; |
302 | 0 | break; |
303 | 0 | } |
304 | 0 | b->ptr = fp; |
305 | 0 | b->init = 1; |
306 | | /* we did fopen -> we disengage UPLINK */ |
307 | 0 | BIO_clear_flags(b, BIO_FLAGS_UPLINK_INTERNAL); |
308 | 0 | break; |
309 | 0 | case BIO_C_GET_FILE_PTR: |
310 | | /* the ptr parameter is actually a FILE ** in this case. */ |
311 | 0 | if (ptr != NULL) { |
312 | 0 | fpp = (FILE **)ptr; |
313 | 0 | *fpp = (FILE *)b->ptr; |
314 | 0 | } |
315 | 0 | break; |
316 | 0 | case BIO_CTRL_GET_CLOSE: |
317 | 0 | ret = (long)b->shutdown; |
318 | 0 | break; |
319 | 0 | case BIO_CTRL_SET_CLOSE: |
320 | 0 | b->shutdown = (int)num; |
321 | 0 | break; |
322 | 0 | case BIO_CTRL_FLUSH: |
323 | 0 | st = b->flags & BIO_FLAGS_UPLINK_INTERNAL |
324 | 0 | ? UP_fflush(b->ptr) |
325 | 0 | : fflush((FILE *)b->ptr); |
326 | 0 | if (st == EOF) { |
327 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
328 | 0 | "calling fflush()"); |
329 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
330 | 0 | ret = 0; |
331 | 0 | } |
332 | 0 | break; |
333 | 0 | case BIO_CTRL_DUP: |
334 | 0 | ret = 1; |
335 | 0 | break; |
336 | | |
337 | 0 | case BIO_CTRL_WPENDING: |
338 | 0 | case BIO_CTRL_PENDING: |
339 | 0 | case BIO_CTRL_PUSH: |
340 | 0 | case BIO_CTRL_POP: |
341 | 0 | default: |
342 | 0 | ret = 0; |
343 | 0 | break; |
344 | 127k | } |
345 | 127k | return ret; |
346 | 127k | } |
347 | | |
348 | | static int file_gets(BIO *bp, char *buf, int size) |
349 | 534k | { |
350 | 534k | int ret = 0; |
351 | | |
352 | 534k | buf[0] = '\0'; |
353 | 534k | if (bp->flags & BIO_FLAGS_UPLINK_INTERNAL) { |
354 | 0 | if (!UP_fgets(buf, size, bp->ptr)) |
355 | 0 | goto err; |
356 | 534k | } else { |
357 | 534k | if (!fgets(buf, size, (FILE *)bp->ptr)) |
358 | 2.43k | goto err; |
359 | 534k | } |
360 | 531k | if (buf[0] != '\0') |
361 | 531k | ret = strlen(buf); |
362 | 534k | err: |
363 | 534k | return ret; |
364 | 531k | } |
365 | | |
366 | | static int file_puts(BIO *bp, const char *str) |
367 | 0 | { |
368 | 0 | int n, ret; |
369 | |
|
370 | 0 | n = strlen(str); |
371 | 0 | ret = file_write(bp, str, n); |
372 | 0 | return ret; |
373 | 0 | } |
374 | | |
375 | | #else |
376 | | |
377 | | static int file_write(BIO *b, const char *in, int inl) |
378 | | { |
379 | | return -1; |
380 | | } |
381 | | static int file_read(BIO *b, char *out, int outl) |
382 | | { |
383 | | return -1; |
384 | | } |
385 | | static int file_puts(BIO *bp, const char *str) |
386 | | { |
387 | | return -1; |
388 | | } |
389 | | static int file_gets(BIO *bp, char *buf, int size) |
390 | | { |
391 | | return 0; |
392 | | } |
393 | | static long file_ctrl(BIO *b, int cmd, long num, void *ptr) |
394 | | { |
395 | | return 0; |
396 | | } |
397 | | static int file_new(BIO *bi) |
398 | | { |
399 | | return 0; |
400 | | } |
401 | | static int file_free(BIO *a) |
402 | | { |
403 | | return 0; |
404 | | } |
405 | | |
406 | | static const BIO_METHOD methods_filep = { |
407 | | BIO_TYPE_FILE, |
408 | | "FILE pointer", |
409 | | bwrite_conv, |
410 | | file_write, |
411 | | bread_conv, |
412 | | file_read, |
413 | | file_puts, |
414 | | file_gets, |
415 | | file_ctrl, |
416 | | file_new, |
417 | | file_free, |
418 | | NULL, /* file_callback_ctrl */ |
419 | | }; |
420 | | |
421 | | const BIO_METHOD *BIO_s_file(void) |
422 | | { |
423 | | return &methods_filep; |
424 | | } |
425 | | |
426 | | BIO *BIO_new_file(const char *filename, const char *mode) |
427 | | { |
428 | | return NULL; |
429 | | } |
430 | | |
431 | | #endif /* OPENSSL_NO_STDIO */ |