xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/bio/fd.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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 #include <openssl/bio.h>
58 
59 #if !defined(OPENSSL_NO_POSIX_IO)
60 
61 #include <errno.h>
62 #include <string.h>
63 
64 #if !defined(OPENSSL_WINDOWS)
65 #include <unistd.h>
66 #else
67 #include <io.h>
68 #endif
69 
70 #include <openssl/err.h>
71 #include <openssl/mem.h>
72 
73 #include "internal.h"
74 #include "../internal.h"
75 
76 
77 #if defined(OPENSSL_WINDOWS)
78   #define BORINGSSL_CLOSE _close
79   #define BORINGSSL_LSEEK _lseek
80   #define BORINGSSL_READ _read
81   #define BORINGSSL_WRITE _write
82 #else
83   #define BORINGSSL_CLOSE close
84   #define BORINGSSL_LSEEK lseek
85   #define BORINGSSL_READ read
86   #define BORINGSSL_WRITE write
87 #endif
88 
BIO_new_fd(int fd,int close_flag)89 BIO *BIO_new_fd(int fd, int close_flag) {
90   BIO *ret = BIO_new(BIO_s_fd());
91   if (ret == NULL) {
92     return NULL;
93   }
94   BIO_set_fd(ret, fd, close_flag);
95   return ret;
96 }
97 
fd_new(BIO * bio)98 static int fd_new(BIO *bio) {
99   // num is used to store the file descriptor.
100   bio->num = -1;
101   return 1;
102 }
103 
fd_free(BIO * bio)104 static int fd_free(BIO *bio) {
105   if (bio->shutdown) {
106     if (bio->init) {
107       BORINGSSL_CLOSE(bio->num);
108     }
109     bio->init = 0;
110   }
111   return 1;
112 }
113 
fd_read(BIO * b,char * out,int outl)114 static int fd_read(BIO *b, char *out, int outl) {
115   int ret = 0;
116 
117   ret = (int)BORINGSSL_READ(b->num, out, outl);
118   BIO_clear_retry_flags(b);
119   if (ret <= 0) {
120     if (bio_errno_should_retry(ret)) {
121       BIO_set_retry_read(b);
122     }
123   }
124 
125   return ret;
126 }
127 
fd_write(BIO * b,const char * in,int inl)128 static int fd_write(BIO *b, const char *in, int inl) {
129   int ret = (int)BORINGSSL_WRITE(b->num, in, inl);
130   BIO_clear_retry_flags(b);
131   if (ret <= 0) {
132     if (bio_errno_should_retry(ret)) {
133       BIO_set_retry_write(b);
134     }
135   }
136 
137   return ret;
138 }
139 
fd_ctrl(BIO * b,int cmd,long num,void * ptr)140 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
141   long ret = 1;
142   int *ip;
143 
144   switch (cmd) {
145     case BIO_CTRL_RESET:
146       num = 0;
147       OPENSSL_FALLTHROUGH;
148     case BIO_C_FILE_SEEK:
149       ret = 0;
150       if (b->init) {
151         ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
152       }
153       break;
154     case BIO_C_FILE_TELL:
155     case BIO_CTRL_INFO:
156       ret = 0;
157       if (b->init) {
158         ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
159       }
160       break;
161     case BIO_C_SET_FD:
162       fd_free(b);
163       b->num = *((int *)ptr);
164       b->shutdown = (int)num;
165       b->init = 1;
166       break;
167     case BIO_C_GET_FD:
168       if (b->init) {
169         ip = (int *)ptr;
170         if (ip != NULL) {
171           *ip = b->num;
172         }
173         return b->num;
174       } else {
175         ret = -1;
176       }
177       break;
178     case BIO_CTRL_GET_CLOSE:
179       ret = b->shutdown;
180       break;
181     case BIO_CTRL_SET_CLOSE:
182       b->shutdown = (int)num;
183       break;
184     case BIO_CTRL_PENDING:
185     case BIO_CTRL_WPENDING:
186       ret = 0;
187       break;
188     case BIO_CTRL_FLUSH:
189       ret = 1;
190       break;
191     default:
192       ret = 0;
193       break;
194   }
195 
196   return ret;
197 }
198 
fd_gets(BIO * bp,char * buf,int size)199 static int fd_gets(BIO *bp, char *buf, int size) {
200   if (size <= 0) {
201     return 0;
202   }
203 
204   char *ptr = buf;
205   char *end = buf + size - 1;
206   while (ptr < end && fd_read(bp, ptr, 1) > 0) {
207     char c = ptr[0];
208     ptr++;
209     if (c == '\n') {
210       break;
211     }
212   }
213 
214   ptr[0] = '\0';
215 
216   // The output length is bounded by |size|.
217   return (int)(ptr - buf);
218 }
219 
220 static const BIO_METHOD methods_fdp = {
221     BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
222     fd_gets,     fd_ctrl,           fd_new,   fd_free, NULL /* callback_ctrl */,
223 };
224 
BIO_s_fd(void)225 const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
226 
227 #endif  // OPENSSL_NO_POSIX_IO
228 
BIO_set_fd(BIO * bio,int fd,int close_flag)229 int BIO_set_fd(BIO *bio, int fd, int close_flag) {
230   return (int)BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
231 }
232 
BIO_get_fd(BIO * bio,int * out_fd)233 int BIO_get_fd(BIO *bio, int *out_fd) {
234   return (int)BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
235 }
236