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