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