Coverage Report

Created: 2023-06-07 07:11

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