| 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 | #endif |
| 27 |
| 28 | #include <openssl/err.h> |
| 29 | #include <openssl/mem.h> |
| 30 |
| 31 | #include "internal.h" |
| 32 | #include "../internal.h" |
| 33 |
| 34 |
| 35 | #if defined(OPENSSL_WINDOWS) |
| 36 | #define BORINGSSL_CLOSE _close |
| 37 | #define BORINGSSL_LSEEK _lseek |
| 38 | #define BORINGSSL_READ _read |
| 39 | #define BORINGSSL_WRITE _write |
| 40 | #else |
| 41 | #define BORINGSSL_CLOSE close |
| 42 | #define BORINGSSL_LSEEK lseek |
| 43 | #define BORINGSSL_READ read |
| 44 | #define BORINGSSL_WRITE write |
| 45 | #endif |
| 46 |
| 47 | BIO *BIO_new_fd(int fd, int close_flag) { |
| 48 | BIO *ret = BIO_new(BIO_s_fd()); |
| 49 | if (ret == NULL) { |
| 50 | return NULL; |
| 51 | } |
| 52 | BIO_set_fd(ret, fd, close_flag); |
| 53 | return ret; |
| 54 | } |
| 55 |
| 56 | static int fd_new(BIO *bio) { |
| 57 | // num is used to store the file descriptor. |
| 58 | bio->num = -1; |
| 59 | return 1; |
| 60 | } |
| 61 |
| 62 | static int fd_free(BIO *bio) { |
| 63 | if (bio->shutdown) { |
| 64 | if (bio->init) { |
| 65 | BORINGSSL_CLOSE(bio->num); |
| 66 | } |
| 67 | bio->init = 0; |
| 68 | } |
| 69 | return 1; |
| 70 | } |
| 71 |
| 72 | static int fd_read(BIO *b, char *out, int outl) { |
| 73 | int ret = 0; |
| 74 |
| 75 | ret = (int)BORINGSSL_READ(b->num, out, outl); |
| 76 | BIO_clear_retry_flags(b); |
| 77 | if (ret <= 0) { |
| 78 | if (bio_errno_should_retry(ret)) { |
| 79 | BIO_set_retry_read(b); |
| 80 | } |
| 81 | } |
| 82 |
| 83 | return ret; |
| 84 | } |
| 85 |
| 86 | static int fd_write(BIO *b, const char *in, int inl) { |
| 87 | int ret = (int)BORINGSSL_WRITE(b->num, in, inl); |
| 88 | BIO_clear_retry_flags(b); |
| 89 | if (ret <= 0) { |
| 90 | if (bio_errno_should_retry(ret)) { |
| 91 | BIO_set_retry_write(b); |
| 92 | } |
| 93 | } |
| 94 |
| 95 | return ret; |
| 96 | } |
| 97 |
| 98 | static long fd_ctrl(BIO *b, int cmd, long num, void *ptr) { |
| 99 | long ret = 1; |
| 100 | int *ip; |
| 101 |
| 102 | switch (cmd) { |
| 103 | case BIO_CTRL_RESET: |
| 104 | num = 0; |
| 105 | [[fallthrough]]; |
| 106 | case BIO_C_FILE_SEEK: |
| 107 | ret = 0; |
| 108 | if (b->init) { |
| 109 | ret = (long)BORINGSSL_LSEEK(b->num, num, SEEK_SET); |
| 110 | } |
| 111 | break; |
| 112 | case BIO_C_FILE_TELL: |
| 113 | case BIO_CTRL_INFO: |
| 114 | ret = 0; |
| 115 | if (b->init) { |
| 116 | ret = (long)BORINGSSL_LSEEK(b->num, 0, SEEK_CUR); |
| 117 | } |
| 118 | break; |
| 119 | case BIO_C_SET_FD: |
| 120 | fd_free(b); |
| 121 | b->num = *((int *)ptr); |
| 122 | b->shutdown = (int)num; |
| 123 | b->init = 1; |
| 124 | break; |
| 125 | case BIO_C_GET_FD: |
| 126 | if (b->init) { |
| 127 | ip = (int *)ptr; |
| 128 | if (ip != NULL) { |
| 129 | *ip = b->num; |
| 130 | } |
| 131 | return b->num; |
| 132 | } else { |
| 133 | ret = -1; |
| 134 | } |
| 135 | break; |
| 136 | case BIO_CTRL_GET_CLOSE: |
| 137 | ret = b->shutdown; |
| 138 | break; |
| 139 | case BIO_CTRL_SET_CLOSE: |
| 140 | b->shutdown = (int)num; |
| 141 | break; |
| 142 | case BIO_CTRL_PENDING: |
| 143 | case BIO_CTRL_WPENDING: |
| 144 | ret = 0; |
| 145 | break; |
| 146 | case BIO_CTRL_FLUSH: |
| 147 | ret = 1; |
| 148 | break; |
| 149 | default: |
| 150 | ret = 0; |
| 151 | break; |
| 152 | } |
| 153 |
| 154 | return ret; |
| 155 | } |
| 156 |
| 157 | static int fd_gets(BIO *bp, char *buf, int size) { |
| 158 | if (size <= 0) { |
| 159 | return 0; |
| 160 | } |
| 161 |
| 162 | char *ptr = buf; |
| 163 | char *end = buf + size - 1; |
| 164 | while (ptr < end && fd_read(bp, ptr, 1) > 0) { |
| 165 | char c = ptr[0]; |
| 166 | ptr++; |
| 167 | if (c == '\n') { |
| 168 | break; |
| 169 | } |
| 170 | } |
| 171 |
| 172 | ptr[0] = '\0'; |
| 173 |
| 174 | // The output length is bounded by |size|. |
| 175 | return (int)(ptr - buf); |
| 176 | } |
| 177 |
| 178 | static const BIO_METHOD methods_fdp = { |
| 179 | BIO_TYPE_FD, "file descriptor", fd_write, fd_read, NULL /* puts */, |
| 180 | fd_gets, fd_ctrl, fd_new, fd_free, NULL /* callback_ctrl */, |
| 181 | }; |
| 182 |
| 183 | const BIO_METHOD *BIO_s_fd(void) { return &methods_fdp; } |
| 184 |
| 185 | #endif // OPENSSL_NO_POSIX_IO |
| 186 |
| 187 | int BIO_set_fd(BIO *bio, int fd, int close_flag) { |
| 188 | return (int)BIO_int_ctrl(bio, BIO_C_SET_FD, close_flag, fd); |
| 189 | } |
| 190 |
| 191 | int BIO_get_fd(BIO *bio, int *out_fd) { |
| 192 | return (int)BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd); |
| 193 | } |
| 194 |