1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
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 (tjh@cryptsoft.com).
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 (eay@cryptsoft.com)"
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 (tjh@cryptsoft.com)"
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 #include <errno.h>
60 #include <string.h>
61 
62 #if !defined(OPENSSL_WINDOWS)
63 #include <unistd.h>
64 #else
65 #include <io.h>
66 OPENSSL_MSVC_PRAGMA(warning(push, 3))
67 #include <windows.h>
OPENSSL_MSVC_PRAGMA(warning (pop))68 OPENSSL_MSVC_PRAGMA(warning(pop))
69 #endif
70 
71 #include <openssl/buf.h>
72 #include <openssl/err.h>
73 #include <openssl/mem.h>
74 
75 #include "internal.h"
76 #include "../internal.h"
77 
78 
79 static int bio_fd_non_fatal_error(int err) {
80   if (
81 #ifdef EWOULDBLOCK
82     err == EWOULDBLOCK ||
83 #endif
84 #ifdef WSAEWOULDBLOCK
85     err == WSAEWOULDBLOCK ||
86 #endif
87 #ifdef ENOTCONN
88     err == ENOTCONN ||
89 #endif
90 #ifdef EINTR
91     err == EINTR ||
92 #endif
93 #ifdef EAGAIN
94     err == EAGAIN ||
95 #endif
96 #ifdef EPROTO
97     err == EPROTO ||
98 #endif
99 #ifdef EINPROGRESS
100     err == EINPROGRESS ||
101 #endif
102 #ifdef EALREADY
103     err == EALREADY ||
104 #endif
105     0) {
106     return 1;
107   }
108   return 0;
109 }
110 
111 #if defined(OPENSSL_WINDOWS)
112   #define BORINGSSL_ERRNO (int)GetLastError()
113   #define BORINGSSL_CLOSE _close
114   #define BORINGSSL_LSEEK _lseek
115   #define BORINGSSL_READ _read
116   #define BORINGSSL_WRITE _write
117 #else
118   #define BORINGSSL_ERRNO errno
119   #define BORINGSSL_CLOSE close
120   #define BORINGSSL_LSEEK lseek
121   #define BORINGSSL_READ read
122   #define BORINGSSL_WRITE write
123 #endif
124 
bio_fd_should_retry(int i)125 int bio_fd_should_retry(int i) {
126   if (i == -1) {
127     return bio_fd_non_fatal_error(BORINGSSL_ERRNO);
128   }
129   return 0;
130 }
131 
BIO_new_fd(int fd,int close_flag)132 BIO *BIO_new_fd(int fd, int close_flag) {
133   BIO *ret = BIO_new(BIO_s_fd());
134   if (ret == NULL) {
135     return NULL;
136   }
137   BIO_set_fd(ret, fd, close_flag);
138   return ret;
139 }
140 
fd_new(BIO * bio)141 static int fd_new(BIO *bio) {
142   // num is used to store the file descriptor.
143   bio->num = -1;
144   return 1;
145 }
146 
fd_free(BIO * bio)147 static int fd_free(BIO *bio) {
148   if (bio == NULL) {
149     return 0;
150   }
151 
152   if (bio->shutdown) {
153     if (bio->init) {
154       BORINGSSL_CLOSE(bio->num);
155     }
156     bio->init = 0;
157   }
158   return 1;
159 }
160 
fd_read(BIO * b,char * out,int outl)161 static int fd_read(BIO *b, char *out, int outl) {
162   int ret = 0;
163 
164   ret = BORINGSSL_READ(b->num, out, outl);
165   BIO_clear_retry_flags(b);
166   if (ret <= 0) {
167     if (bio_fd_should_retry(ret)) {
168       BIO_set_retry_read(b);
169     }
170   }
171 
172   return ret;
173 }
174 
fd_write(BIO * b,const char * in,int inl)175 static int fd_write(BIO *b, const char *in, int inl) {
176   int ret = BORINGSSL_WRITE(b->num, in, inl);
177   BIO_clear_retry_flags(b);
178   if (ret <= 0) {
179     if (bio_fd_should_retry(ret)) {
180       BIO_set_retry_write(b);
181     }
182   }
183 
184   return ret;
185 }
186 
fd_ctrl(BIO * b,int cmd,long num,void * ptr)187 static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
188   long ret = 1;
189   int *ip;
190 
191   switch (cmd) {
192     case BIO_CTRL_RESET:
193       num = 0;
194       OPENSSL_FALLTHROUGH;
195     case BIO_C_FILE_SEEK:
196       ret = 0;
197       if (b->init) {
198         ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET);
199       }
200       break;
201     case BIO_C_FILE_TELL:
202     case BIO_CTRL_INFO:
203       ret = 0;
204       if (b->init) {
205         ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR);
206       }
207       break;
208     case BIO_C_SET_FD:
209       fd_free(b);
210       b->num = *((int *)ptr);
211       b->shutdown = (int)num;
212       b->init = 1;
213       break;
214     case BIO_C_GET_FD:
215       if (b->init) {
216         ip = (int *)ptr;
217         if (ip != NULL) {
218           *ip = b->num;
219         }
220         return b->num;
221       } else {
222         ret = -1;
223       }
224       break;
225     case BIO_CTRL_GET_CLOSE:
226       ret = b->shutdown;
227       break;
228     case BIO_CTRL_SET_CLOSE:
229       b->shutdown = (int)num;
230       break;
231     case BIO_CTRL_PENDING:
232     case BIO_CTRL_WPENDING:
233       ret = 0;
234       break;
235     case BIO_CTRL_FLUSH:
236       ret = 1;
237       break;
238     default:
239       ret = 0;
240       break;
241   }
242 
243   return ret;
244 }
245 
fd_gets(BIO * bp,char * buf,int size)246 static int fd_gets(BIO *bp, char *buf, int size) {
247   char *ptr = buf;
248   char *end = buf + size - 1;
249 
250   if (size <= 0) {
251     return 0;
252   }
253 
254   while (ptr < end && fd_read(bp, ptr, 1) > 0 && ptr[0] != '\n') {
255     ptr++;
256   }
257 
258   ptr[0] = '\0';
259 
260   return ptr - buf;
261 }
262 
263 static const BIO_METHOD methods_fdp = {
264     BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */,
265     fd_gets,     fd_ctrl,           fd_new,   fd_free, NULL /* callback_ctrl */,
266 };
267 
BIO_s_fd(void)268 const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; }
269 
BIO_set_fd(BIO * bio,int fd,int close_flag)270 int BIO_set_fd(BIO *bio, int fd, int close_flag) {
271   return BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
272 }
273 
BIO_get_fd(BIO * bio,int * out_fd)274 int BIO_get_fd(BIO *bio, int *out_fd) {
275   return BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
276 }
277