/src/openssl/crypto/bio/bss_fd.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  |  | #include <stdio.h>  | 
11  |  | #include <errno.h>  | 
12  |  |  | 
13  |  | #include "bio_local.h"  | 
14  |  |  | 
15  |  | #if defined(OPENSSL_NO_POSIX_IO)  | 
16  |  | /*  | 
17  |  |  * Dummy placeholder for BIO_s_fd...  | 
18  |  |  */  | 
19  |  | BIO *BIO_new_fd(int fd, int close_flag)  | 
20  |  | { | 
21  |  |     return NULL;  | 
22  |  | }  | 
23  |  |  | 
24  |  | int BIO_fd_non_fatal_error(int err)  | 
25  |  | { | 
26  |  |     return 0;  | 
27  |  | }  | 
28  |  |  | 
29  |  | int BIO_fd_should_retry(int i)  | 
30  |  | { | 
31  |  |     return 0;  | 
32  |  | }  | 
33  |  |  | 
34  |  | const BIO_METHOD *BIO_s_fd(void)  | 
35  |  | { | 
36  |  |     return NULL;  | 
37  |  | }  | 
38  |  | #else  | 
39  |  | /*  | 
40  |  |  * As for unconditional usage of "UPLINK" interface in this module.  | 
41  |  |  * Trouble is that unlike Unix file descriptors [which are indexes  | 
42  |  |  * in kernel-side per-process table], corresponding descriptors on  | 
43  |  |  * platforms which require "UPLINK" interface seem to be indexes  | 
44  |  |  * in a user-land, non-global table. Well, in fact they are indexes  | 
45  |  |  * in stdio _iob[], and recall that _iob[] was the very reason why  | 
46  |  |  * "UPLINK" interface was introduced in first place. But one way on  | 
47  |  |  * another. Neither libcrypto or libssl use this BIO meaning that  | 
48  |  |  * file descriptors can only be provided by application. Therefore  | 
49  |  |  * "UPLINK" calls are due...  | 
50  |  |  */  | 
51  |  | static int fd_write(BIO *h, const char *buf, int num);  | 
52  |  | static int fd_read(BIO *h, char *buf, int size);  | 
53  |  | static int fd_puts(BIO *h, const char *str);  | 
54  |  | static int fd_gets(BIO *h, char *buf, int size);  | 
55  |  | static long fd_ctrl(BIO *h, int cmd, long arg1, void *arg2);  | 
56  |  | static int fd_new(BIO *h);  | 
57  |  | static int fd_free(BIO *data);  | 
58  |  | int BIO_fd_should_retry(int s);  | 
59  |  |  | 
60  |  | static const BIO_METHOD methods_fdp = { | 
61  |  |     BIO_TYPE_FD,  | 
62  |  |     "file descriptor",  | 
63  |  |     bwrite_conv,  | 
64  |  |     fd_write,  | 
65  |  |     bread_conv,  | 
66  |  |     fd_read,  | 
67  |  |     fd_puts,  | 
68  |  |     fd_gets,  | 
69  |  |     fd_ctrl,  | 
70  |  |     fd_new,  | 
71  |  |     fd_free,  | 
72  |  |     NULL,                       /* fd_callback_ctrl */  | 
73  |  | };  | 
74  |  |  | 
75  |  | const BIO_METHOD *BIO_s_fd(void)  | 
76  | 0  | { | 
77  | 0  |     return &methods_fdp;  | 
78  | 0  | }  | 
79  |  |  | 
80  |  | BIO *BIO_new_fd(int fd, int close_flag)  | 
81  | 0  | { | 
82  | 0  |     BIO *ret;  | 
83  | 0  |     ret = BIO_new(BIO_s_fd());  | 
84  | 0  |     if (ret == NULL)  | 
85  | 0  |         return NULL;  | 
86  | 0  |     BIO_set_fd(ret, fd, close_flag);  | 
87  | 0  |     return ret;  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | static int fd_new(BIO *bi)  | 
91  | 0  | { | 
92  | 0  |     bi->init = 0;  | 
93  | 0  |     bi->num = -1;  | 
94  | 0  |     bi->ptr = NULL;  | 
95  | 0  |     bi->flags = BIO_FLAGS_UPLINK_INTERNAL; /* essentially redundant */  | 
96  | 0  |     return 1;  | 
97  | 0  | }  | 
98  |  |  | 
99  |  | static int fd_free(BIO *a)  | 
100  | 0  | { | 
101  | 0  |     if (a == NULL)  | 
102  | 0  |         return 0;  | 
103  | 0  |     if (a->shutdown) { | 
104  | 0  |         if (a->init) { | 
105  | 0  |             UP_close(a->num);  | 
106  | 0  |         }  | 
107  | 0  |         a->init = 0;  | 
108  | 0  |         a->flags = BIO_FLAGS_UPLINK_INTERNAL;  | 
109  | 0  |     }  | 
110  | 0  |     return 1;  | 
111  | 0  | }  | 
112  |  |  | 
113  |  | static int fd_read(BIO *b, char *out, int outl)  | 
114  | 0  | { | 
115  | 0  |     int ret = 0;  | 
116  |  | 
  | 
117  | 0  |     if (out != NULL) { | 
118  | 0  |         clear_sys_error();  | 
119  | 0  |         ret = (int)UP_read(b->num, out, outl);  | 
120  | 0  |         BIO_clear_retry_flags(b);  | 
121  | 0  |         if (ret <= 0) { | 
122  | 0  |             if (BIO_fd_should_retry(ret))  | 
123  | 0  |                 BIO_set_retry_read(b);  | 
124  | 0  |             else if (ret == 0)  | 
125  | 0  |                 b->flags |= BIO_FLAGS_IN_EOF;  | 
126  | 0  |         }  | 
127  | 0  |     }  | 
128  | 0  |     return ret;  | 
129  | 0  | }  | 
130  |  |  | 
131  |  | static int fd_write(BIO *b, const char *in, int inl)  | 
132  | 0  | { | 
133  | 0  |     int ret;  | 
134  | 0  |     clear_sys_error();  | 
135  | 0  |     ret = (int)UP_write(b->num, in, inl);  | 
136  | 0  |     BIO_clear_retry_flags(b);  | 
137  | 0  |     if (ret <= 0) { | 
138  | 0  |         if (BIO_fd_should_retry(ret))  | 
139  | 0  |             BIO_set_retry_write(b);  | 
140  | 0  |     }  | 
141  | 0  |     return ret;  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | static long fd_ctrl(BIO *b, int cmd, long num, void *ptr)  | 
145  | 0  | { | 
146  | 0  |     long ret = 1;  | 
147  | 0  |     int *ip;  | 
148  |  | 
  | 
149  | 0  |     switch (cmd) { | 
150  | 0  |     case BIO_CTRL_RESET:  | 
151  | 0  |         num = 0;  | 
152  |  |         /* fall through */  | 
153  | 0  |     case BIO_C_FILE_SEEK:  | 
154  | 0  |         ret = (long)UP_lseek(b->num, num, 0);  | 
155  | 0  |         break;  | 
156  | 0  |     case BIO_C_FILE_TELL:  | 
157  | 0  |     case BIO_CTRL_INFO:  | 
158  | 0  |         ret = (long)UP_lseek(b->num, 0, 1);  | 
159  | 0  |         break;  | 
160  | 0  |     case BIO_C_SET_FD:  | 
161  | 0  |         fd_free(b);  | 
162  | 0  |         b->num = *((int *)ptr);  | 
163  | 0  |         b->shutdown = (int)num;  | 
164  | 0  |         b->init = 1;  | 
165  | 0  |         break;  | 
166  | 0  |     case BIO_C_GET_FD:  | 
167  | 0  |         if (b->init) { | 
168  | 0  |             ip = (int *)ptr;  | 
169  | 0  |             if (ip != NULL)  | 
170  | 0  |                 *ip = b->num;  | 
171  | 0  |             ret = b->num;  | 
172  | 0  |         } else  | 
173  | 0  |             ret = -1;  | 
174  | 0  |         break;  | 
175  | 0  |     case BIO_CTRL_GET_CLOSE:  | 
176  | 0  |         ret = b->shutdown;  | 
177  | 0  |         break;  | 
178  | 0  |     case BIO_CTRL_SET_CLOSE:  | 
179  | 0  |         b->shutdown = (int)num;  | 
180  | 0  |         break;  | 
181  | 0  |     case BIO_CTRL_PENDING:  | 
182  | 0  |     case BIO_CTRL_WPENDING:  | 
183  | 0  |         ret = 0;  | 
184  | 0  |         break;  | 
185  | 0  |     case BIO_CTRL_DUP:  | 
186  | 0  |     case BIO_CTRL_FLUSH:  | 
187  | 0  |         ret = 1;  | 
188  | 0  |         break;  | 
189  | 0  |     case BIO_CTRL_EOF:  | 
190  | 0  |         ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;  | 
191  | 0  |         break;  | 
192  | 0  |     default:  | 
193  | 0  |         ret = 0;  | 
194  | 0  |         break;  | 
195  | 0  |     }  | 
196  | 0  |     return ret;  | 
197  | 0  | }  | 
198  |  |  | 
199  |  | static int fd_puts(BIO *bp, const char *str)  | 
200  | 0  | { | 
201  | 0  |     int ret;  | 
202  | 0  |     size_t n = strlen(str);  | 
203  |  | 
  | 
204  | 0  |     if (n > INT_MAX)  | 
205  | 0  |         return -1;  | 
206  | 0  |     ret = fd_write(bp, str, (int)n);  | 
207  | 0  |     return ret;  | 
208  | 0  | }  | 
209  |  |  | 
210  |  | static int fd_gets(BIO *bp, char *buf, int size)  | 
211  | 0  | { | 
212  | 0  |     int ret = 0;  | 
213  | 0  |     char *ptr = buf;  | 
214  | 0  |     char *end = buf + size - 1;  | 
215  |  | 
  | 
216  | 0  |     while (ptr < end && fd_read(bp, ptr, 1) > 0) { | 
217  | 0  |         if (*ptr++ == '\n')  | 
218  | 0  |            break;  | 
219  | 0  |     }  | 
220  |  | 
  | 
221  | 0  |     ptr[0] = '\0';  | 
222  |  | 
  | 
223  | 0  |     if (buf[0] != '\0')  | 
224  | 0  |         ret = (int)strlen(buf);  | 
225  | 0  |     return ret;  | 
226  | 0  | }  | 
227  |  |  | 
228  |  | int BIO_fd_should_retry(int i)  | 
229  | 0  | { | 
230  | 0  |     int err;  | 
231  |  | 
  | 
232  | 0  |     if ((i == 0) || (i == -1)) { | 
233  | 0  |         err = get_last_sys_error();  | 
234  |  | 
  | 
235  | 0  |         return BIO_fd_non_fatal_error(err);  | 
236  | 0  |     }  | 
237  | 0  |     return 0;  | 
238  | 0  | }  | 
239  |  |  | 
240  |  | int BIO_fd_non_fatal_error(int err)  | 
241  | 0  | { | 
242  | 0  |     switch (err) { | 
243  |  |  | 
244  | 0  | # ifdef EWOULDBLOCK  | 
245  |  | #  ifdef WSAEWOULDBLOCK  | 
246  |  | #   if WSAEWOULDBLOCK != EWOULDBLOCK  | 
247  |  |     case EWOULDBLOCK:  | 
248  |  | #   endif  | 
249  |  | #  else  | 
250  | 0  |     case EWOULDBLOCK:  | 
251  | 0  | #  endif  | 
252  | 0  | # endif  | 
253  |  | 
  | 
254  | 0  | # if defined(ENOTCONN)  | 
255  | 0  |     case ENOTCONN:  | 
256  | 0  | # endif  | 
257  |  | 
  | 
258  | 0  | # ifdef EINTR  | 
259  | 0  |     case EINTR:  | 
260  | 0  | # endif  | 
261  |  | 
  | 
262  | 0  | # ifdef EAGAIN  | 
263  |  | #  if EWOULDBLOCK != EAGAIN  | 
264  |  |     case EAGAIN:  | 
265  |  | #  endif  | 
266  | 0  | # endif  | 
267  |  | 
  | 
268  | 0  | # ifdef EPROTO  | 
269  | 0  |     case EPROTO:  | 
270  | 0  | # endif  | 
271  |  | 
  | 
272  | 0  | # ifdef EINPROGRESS  | 
273  | 0  |     case EINPROGRESS:  | 
274  | 0  | # endif  | 
275  |  | 
  | 
276  | 0  | # ifdef EALREADY  | 
277  | 0  |     case EALREADY:  | 
278  | 0  | # endif  | 
279  | 0  |         return 1;  | 
280  | 0  |     default:  | 
281  | 0  |         break;  | 
282  | 0  |     }  | 
283  | 0  |     return 0;  | 
284  | 0  | }  | 
285  |  | #endif  |