Coverage Report

Created: 2026-10-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/bio/fd.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/bio.h>
16
17
#if !defined(OPENSSL_NO_POSIX_IO)
18
19
#include <errno.h>
20
#include <string.h>
21
22
#if !defined(OPENSSL_WINDOWS)
23
#include <unistd.h>
24
#else
25
#include <io.h>
26
#include <limits.h>
27
#include <algorithm>
28
#endif
29
30
#include <openssl/err.h>
31
#include <openssl/mem.h>
32
33
#include "internal.h"
34
#include "../internal.h"
35
36
37
#if defined(OPENSSL_WINDOWS)
38
  #define BORINGSSL_CLOSE _close
39
  #define BORINGSSL_LSEEK _lseek
40
  #define BORINGSSL_READ _read
41
#else
42
0
  #define BORINGSSL_CLOSE close
43
0
  #define BORINGSSL_LSEEK lseek
44
0
  #define BORINGSSL_READ read
45
#endif
46
47
using namespace bssl;
48
49
0
BIO *BIO_new_fd(int fd, int close_flag) {
50
0
  BIO *ret = BIO_new(BIO_s_fd());
51
0
  if (ret == nullptr) {
52
0
    return nullptr;
53
0
  }
54
0
  BIO_set_fd(ret, fd, close_flag);
55
0
  return ret;
56
0
}
57
58
0
static int fd_new(BIO *bio) {
59
  // num is used to store the file descriptor.
60
0
  FromOpaque(bio)->num = -1;
61
0
  return 1;
62
0
}
63
64
0
static int fd_free(BIO *bio) {
65
0
  if (BIO_get_shutdown(bio)) {
66
0
    if (BIO_get_init(bio)) {
67
0
      BORINGSSL_CLOSE(FromOpaque(bio)->num);
68
0
    }
69
0
    BIO_set_init(bio, 0);
70
0
  }
71
0
  return 1;
72
0
}
73
74
0
static int fd_read(BIO *b, char *out, int outl) {
75
0
  int ret = (int)BORINGSSL_READ(FromOpaque(b)->num, out, outl);
76
0
  BIO_clear_retry_flags(b);
77
0
  if (ret <= 0) {
78
0
    if (bio_errno_should_retry(ret)) {
79
0
      BIO_set_retry_read(b);
80
0
    }
81
0
  }
82
83
0
  return ret;
84
0
}
85
86
static int fd_write_ex(BIO *b, const char *in, size_t inl,
87
0
                       size_t *out_written) {
88
#if defined(OPENSSL_WINDOWS)
89
  inl = std::min(inl, size_t{UINT_MAX});
90
  int ret = _write(FromOpaque(b)->num, in, static_cast<unsigned>(inl));
91
#else
92
0
  ssize_t ret = write(FromOpaque(b)->num, in, inl);
93
0
#endif
94
0
  BIO_clear_retry_flags(b);
95
0
  if (ret <= 0) {
96
0
    if (bio_errno_should_retry(ret)) {
97
0
      BIO_set_retry_write(b);
98
0
    }
99
0
    return 0;
100
0
  }
101
102
0
  *out_written = ret;
103
0
  return 1;
104
0
}
105
106
0
static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) {
107
0
  switch (cmd) {
108
0
    case BIO_CTRL_RESET:
109
0
      num = 0;
110
0
      [[fallthrough]];
111
0
    case BIO_C_FILE_SEEK:
112
0
      if (BIO_get_init(b)) {
113
0
        return (long)BORINGSSL_LSEEK(FromOpaque(b)->num, num, SEEK_SET);
114
0
      }
115
0
      return 0;
116
0
    case BIO_C_FILE_TELL:
117
0
    case BIO_CTRL_INFO:
118
0
      if (BIO_get_init(b)) {
119
0
        return (long)BORINGSSL_LSEEK(FromOpaque(b)->num, 0, SEEK_CUR);
120
0
      }
121
0
      return 0;
122
0
    case BIO_C_SET_FD:
123
0
      fd_free(b);
124
0
      FromOpaque(b)->num = *static_cast<int *>(ptr);
125
0
      BIO_set_shutdown(b, static_cast<int>(num));
126
0
      BIO_set_init(b, 1);
127
0
      return 1;
128
0
    case BIO_C_GET_FD:
129
0
      if (BIO_get_init(b)) {
130
0
        int *out = static_cast<int *>(ptr);
131
0
        if (out != nullptr) {
132
0
          *out = FromOpaque(b)->num;
133
0
        }
134
0
        return FromOpaque(b)->num;
135
0
      } else {
136
0
        return -1;
137
0
      }
138
0
    case BIO_CTRL_GET_CLOSE:
139
0
      return BIO_get_shutdown(b);
140
0
    case BIO_CTRL_SET_CLOSE:
141
0
      BIO_set_shutdown(b, static_cast<int>(num));
142
0
      return 1;
143
0
    case BIO_CTRL_FLUSH:
144
0
      return 1;
145
0
    default:
146
0
      return 0;
147
0
  }
148
0
}
149
150
0
static int fd_gets(BIO *bp, char *buf, int size) {
151
0
  if (size <= 0) {
152
0
    return 0;
153
0
  }
154
155
0
  char *ptr = buf;
156
0
  char *end = buf + size - 1;
157
0
  while (ptr < end && fd_read(bp, ptr, 1) > 0) {
158
0
    char c = ptr[0];
159
0
    ptr++;
160
0
    if (c == '\n') {
161
0
      break;
162
0
    }
163
0
  }
164
165
0
  ptr[0] = '\0';
166
167
  // The output length is bounded by `size`.
168
0
  return (int)(ptr - buf);
169
0
}
170
171
static const BIO_METHOD methods_fdp = {
172
    BIO_TYPE_FD,
173
    /*bwrite=*/nullptr,
174
    fd_write_ex,
175
    fd_read,
176
    fd_gets,
177
    fd_ctrl,
178
    fd_new,
179
    fd_free,
180
    /*callback_ctrl=*/nullptr,
181
};
182
183
0
const BIO_METHOD *BIO_s_fd() { return &methods_fdp; }
184
185
#endif  // OPENSSL_NO_POSIX_IO
186
187
0
int BIO_set_fd(BIO *bio, int fd, int close_flag) {
188
0
  return (int)BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd);
189
0
}
190
191
0
int BIO_get_fd(BIO *bio, int *out_fd) {
192
0
  return (int)BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
193
0
}