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