| 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 | #ifndef OPENSSL_HEADER_BIO_H |
| 16 | #define OPENSSL_HEADER_BIO_H |
| 17 |
| 18 | #include <openssl/base.h> // IWYU pragma: export |
| 19 |
| 20 | #include <stdio.h> // For FILE |
| 21 |
| 22 | #include <openssl/buffer.h> |
| 23 | #include <openssl/err.h> // for ERR_print_errors_fp |
| 24 | #include <openssl/ex_data.h> |
| 25 | #include <openssl/stack.h> |
| 26 | #include <openssl/thread.h> |
| 27 |
| 28 | #if defined(__cplusplus) |
| 29 | extern "C" { |
| 30 | #endif |
| 31 |
| 32 |
| 33 | // BIO abstracts over a file-descriptor like interface. |
| 34 |
| 35 |
| 36 | // Allocation and freeing. |
| 37 |
| 38 | DEFINE_STACK_OF(BIO) |
| 39 |
| 40 | // BIO_new creates a new BIO with the given method and a reference count of one. |
| 41 | // It returns the fresh |BIO|, or NULL on error. |
| 42 | OPENSSL_EXPORT BIO *BIO_new(const BIO_METHOD *method); |
| 43 |
| 44 | // BIO_free decrements the reference count of |bio|. If the reference count |
| 45 | // drops to zero, it calls the destroy callback, if present, on the method and |
| 46 | // frees |bio| itself. It then repeats that for the next BIO in the chain, if |
| 47 | // any. |
| 48 | // |
| 49 | // It returns one on success or zero otherwise. |
| 50 | OPENSSL_EXPORT int BIO_free(BIO *bio); |
| 51 |
| 52 | // BIO_vfree performs the same actions as |BIO_free|, but has a void return |
| 53 | // value. This is provided for API-compat. |
| 54 | // |
| 55 | // TODO(fork): remove. |
| 56 | OPENSSL_EXPORT void BIO_vfree(BIO *bio); |
| 57 |
| 58 | // BIO_up_ref increments the reference count of |bio| and returns one. |
| 59 | OPENSSL_EXPORT int BIO_up_ref(BIO *bio); |
| 60 |
| 61 |
| 62 | // Basic I/O. |
| 63 |
| 64 | // BIO_read attempts to read |len| bytes into |data|. It returns the number of |
| 65 | // bytes read, zero on EOF, or a negative number on error. |
| 66 | OPENSSL_EXPORT int BIO_read(BIO *bio, void *data, int len); |
| 67 |
| 68 | // BIO_gets reads a line from |bio| and writes at most |size| bytes into |buf|. |
| 69 | // It returns the number of bytes read or a negative number on error. This |
| 70 | // function's output always includes a trailing NUL byte, so it will read at |
| 71 | // most |size - 1| bytes. |
| 72 | // |
| 73 | // If the function read a complete line, the output will include the newline |
| 74 | // character, '\n'. If no newline was found before |size - 1| bytes or EOF, it |
| 75 | // outputs the bytes which were available. |
| 76 | OPENSSL_EXPORT int BIO_gets(BIO *bio, char *buf, int size); |
| 77 |
| 78 | // BIO_write writes |len| bytes from |data| to |bio|. It returns the number of |
| 79 | // bytes written or a negative number on error. |
| 80 | OPENSSL_EXPORT int BIO_write(BIO *bio, const void *data, int len); |
| 81 |
| 82 | // BIO_write_all writes |len| bytes from |data| to |bio|, looping as necessary. |
| 83 | // It returns one if all bytes were successfully written and zero on error. |
| 84 | OPENSSL_EXPORT int BIO_write_all(BIO *bio, const void *data, size_t len); |
| 85 |
| 86 | // BIO_puts writes a NUL terminated string from |buf| to |bio|. It returns the |
| 87 | // number of bytes written or a negative number on error. |
| 88 | OPENSSL_EXPORT int BIO_puts(BIO *bio, const char *buf); |
| 89 |
| 90 | // BIO_flush flushes any buffered output. It returns one on success and zero |
| 91 | // otherwise. |
| 92 | OPENSSL_EXPORT int BIO_flush(BIO *bio); |
| 93 |
| 94 |
| 95 | // Low-level control functions. |
| 96 | // |
| 97 | // These are generic functions for sending control requests to a BIO. In |
| 98 | // general one should use the wrapper functions like |BIO_get_close|. |
| 99 |
| 100 | // BIO_ctrl sends the control request |cmd| to |bio|. The |cmd| argument should |
| 101 | // be one of the |BIO_C_*| values. |
| 102 | OPENSSL_EXPORT long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg); |
| 103 |
| 104 | // BIO_ptr_ctrl acts like |BIO_ctrl| but passes the address of a |void*| |
| 105 | // pointer as |parg| and returns the value that is written to it, or NULL if |
| 106 | // the control request returns <= 0. |
| 107 | OPENSSL_EXPORT char *BIO_ptr_ctrl(BIO *bp, int cmd, long larg); |
| 108 |
| 109 | // BIO_int_ctrl acts like |BIO_ctrl| but passes the address of a copy of |iarg| |
| 110 | // as |parg|. |
| 111 | OPENSSL_EXPORT long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg); |
| 112 |
| 113 | // BIO_reset resets |bio| to its initial state, the precise meaning of which |
| 114 | // depends on the concrete type of |bio|. It returns one on success and zero |
| 115 | // otherwise. |
| 116 | OPENSSL_EXPORT int BIO_reset(BIO *bio); |
| 117 |
| 118 | // BIO_eof returns non-zero when |bio| has reached end-of-file. The precise |
| 119 | // meaning of which depends on the concrete type of |bio|. Note that in the |
| 120 | // case of BIO_pair this always returns non-zero. |
| 121 | OPENSSL_EXPORT int BIO_eof(BIO *bio); |
| 122 |
| 123 | // BIO_set_flags ORs |flags| with |bio->flags|. |
| 124 | OPENSSL_EXPORT void BIO_set_flags(BIO *bio, int flags); |
| 125 |
| 126 | // BIO_test_flags returns |bio->flags| AND |flags|. |
| 127 | OPENSSL_EXPORT int BIO_test_flags(const BIO *bio, int flags); |
| 128 |
| 129 | // BIO_should_read returns non-zero if |bio| encountered a temporary error |
| 130 | // while reading (i.e. EAGAIN), indicating that the caller should retry the |
| 131 | // read. |
| 132 | OPENSSL_EXPORT int BIO_should_read(const BIO *bio); |
| 133 |
| 134 | // BIO_should_write returns non-zero if |bio| encountered a temporary error |
| 135 | // while writing (i.e. EAGAIN), indicating that the caller should retry the |
| 136 | // write. |
| 137 | OPENSSL_EXPORT int BIO_should_write(const BIO *bio); |
| 138 |
| 139 | // BIO_should_retry returns non-zero if the reason that caused a failed I/O |
| 140 | // operation is temporary and thus the operation should be retried. Otherwise, |
| 141 | // it was a permanent error and it returns zero. |
| 142 | OPENSSL_EXPORT int BIO_should_retry(const BIO *bio); |
| 143 |
| 144 | // BIO_should_io_special returns non-zero if |bio| encountered a temporary |
| 145 | // error while performing a special I/O operation, indicating that the caller |
| 146 | // should retry. The operation that caused the error is returned by |
| 147 | // |BIO_get_retry_reason|. |
| 148 | OPENSSL_EXPORT int BIO_should_io_special(const BIO *bio); |
| 149 |
| 150 | // BIO_RR_CONNECT indicates that a connect would have blocked |
| 151 | #define BIO_RR_CONNECT 0x02 |
| 152 |
| 153 | // BIO_RR_ACCEPT indicates that an accept would have blocked |
| 154 | #define BIO_RR_ACCEPT 0x03 |
| 155 |
| 156 | // BIO_get_retry_reason returns the special I/O operation that needs to be |
| 157 | // retried. The return value is one of the |BIO_RR_*| values. |
| 158 | OPENSSL_EXPORT int BIO_get_retry_reason(const BIO *bio); |
| 159 |
| 160 | // BIO_set_retry_reason sets the special I/O operation that needs to be retried |
| 161 | // to |reason|, which should be one of the |BIO_RR_*| values. |
| 162 | OPENSSL_EXPORT void BIO_set_retry_reason(BIO *bio, int reason); |
| 163 |
| 164 | // BIO_clear_flags ANDs |bio->flags| with the bitwise-complement of |flags|. |
| 165 | OPENSSL_EXPORT void BIO_clear_flags(BIO *bio, int flags); |
| 166 |
| 167 | // BIO_set_retry_read sets the |BIO_FLAGS_READ| and |BIO_FLAGS_SHOULD_RETRY| |
| 168 | // flags on |bio|. |
| 169 | OPENSSL_EXPORT void BIO_set_retry_read(BIO *bio); |
| 170 |
| 171 | // BIO_set_retry_write sets the |BIO_FLAGS_WRITE| and |BIO_FLAGS_SHOULD_RETRY| |
| 172 | // flags on |bio|. |
| 173 | OPENSSL_EXPORT void BIO_set_retry_write(BIO *bio); |
| 174 |
| 175 | // BIO_get_retry_flags gets the |BIO_FLAGS_READ|, |BIO_FLAGS_WRITE|, |
| 176 | // |BIO_FLAGS_IO_SPECIAL| and |BIO_FLAGS_SHOULD_RETRY| flags from |bio|. |
| 177 | OPENSSL_EXPORT int BIO_get_retry_flags(BIO *bio); |
| 178 |
| 179 | // BIO_clear_retry_flags clears the |BIO_FLAGS_READ|, |BIO_FLAGS_WRITE|, |
| 180 | // |BIO_FLAGS_IO_SPECIAL| and |BIO_FLAGS_SHOULD_RETRY| flags from |bio|. |
| 181 | OPENSSL_EXPORT void BIO_clear_retry_flags(BIO *bio); |
| 182 |
| 183 | // BIO_method_type returns the type of |bio|, which is one of the |BIO_TYPE_*| |
| 184 | // values. |
| 185 | OPENSSL_EXPORT int BIO_method_type(const BIO *bio); |
| 186 |
| 187 | // These are passed to the BIO callback |
| 188 | #define BIO_CB_FREE 0x01 |
| 189 | #define BIO_CB_READ 0x02 |
| 190 | #define BIO_CB_WRITE 0x03 |
| 191 | #define BIO_CB_PUTS 0x04 |
| 192 | #define BIO_CB_GETS 0x05 |
| 193 | #define BIO_CB_CTRL 0x06 |
| 194 |
| 195 | // The callback is called before and after the underling operation, |
| 196 | // The BIO_CB_RETURN flag indicates if it is after the call |
| 197 | #define BIO_CB_RETURN 0x80 |
| 198 |
| 199 | // bio_info_cb is the type of a callback function that can be called for most |
| 200 | // BIO operations. The |event| argument is one of |BIO_CB_*| and can be ORed |
| 201 | // with |BIO_CB_RETURN| if the callback is being made after the operation in |
| 202 | // question. In that case, |return_value| will contain the return value from |
| 203 | // the operation. |
| 204 | typedef long (*bio_info_cb)(BIO *bio, int event, const char *parg, int cmd, |
| 205 | long larg, long return_value); |
| 206 |
| 207 | // BIO_callback_ctrl allows the callback function to be manipulated. The |cmd| |
| 208 | // arg will generally be |BIO_CTRL_SET_CALLBACK| but arbitrary command values |
| 209 | // can be interpreted by the |BIO|. |
| 210 | OPENSSL_EXPORT long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp); |
| 211 |
| 212 | // BIO_pending returns the number of bytes pending to be read. |
| 213 | OPENSSL_EXPORT size_t BIO_pending(const BIO *bio); |
| 214 |
| 215 | // BIO_ctrl_pending calls |BIO_pending| and exists only for compatibility with |
| 216 | // OpenSSL. |
| 217 | OPENSSL_EXPORT size_t BIO_ctrl_pending(const BIO *bio); |
| 218 |
| 219 | // BIO_wpending returns the number of bytes pending to be written. |
| 220 | OPENSSL_EXPORT size_t BIO_wpending(const BIO *bio); |
| 221 |
| 222 | // BIO_set_close sets the close flag for |bio|. The meaning of which depends on |
| 223 | // the type of |bio| but, for example, a memory BIO interprets the close flag |
| 224 | // as meaning that it owns its buffer. It returns one on success and zero |
| 225 | // otherwise. |
| 226 | OPENSSL_EXPORT int BIO_set_close(BIO *bio, int close_flag); |
| 227 |
| 228 | // BIO_number_read returns the number of bytes that have been read from |
| 229 | // |bio|. |
| 230 | OPENSSL_EXPORT uint64_t BIO_number_read(const BIO *bio); |
| 231 |
| 232 | // BIO_number_written returns the number of bytes that have been written to |
| 233 | // |bio|. |
| 234 | OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio); |
| 235 |
| 236 |
| 237 | // Managing chains of BIOs. |
| 238 | // |
| 239 | // BIOs can be put into chains where the output of one is used as the input of |
| 240 | // the next etc. The most common case is a buffering BIO, which accepts and |
| 241 | // buffers writes until flushed into the next BIO in the chain. |
| 242 |
| 243 | // BIO_push adds |appended_bio| to the end of the chain with |bio| at the head. |
| 244 | // It returns |bio|. Note that |appended_bio| may be the head of a chain itself |
| 245 | // and thus this function can be used to join two chains. |
| 246 | // |
| 247 | // BIO_push takes ownership of the caller's reference to |appended_bio|. |
| 248 | OPENSSL_EXPORT BIO *BIO_push(BIO *bio, BIO *appended_bio); |
| 249 |
| 250 | // BIO_pop removes |bio| from the head of a chain and returns the next BIO in |
| 251 | // the chain, or NULL if there is no next BIO. |
| 252 | // |
| 253 | // The caller takes ownership of the chain's reference to |bio|. |
| 254 | OPENSSL_EXPORT BIO *BIO_pop(BIO *bio); |
| 255 |
| 256 | // BIO_next returns the next BIO in the chain after |bio|, or NULL if there is |
| 257 | // no such BIO. |
| 258 | OPENSSL_EXPORT BIO *BIO_next(BIO *bio); |
| 259 |
| 260 | // BIO_free_all calls |BIO_free|. |
| 261 | // |
| 262 | // TODO(fork): update callers and remove. |
| 263 | OPENSSL_EXPORT void BIO_free_all(BIO *bio); |
| 264 |
| 265 | // BIO_find_type walks a chain of BIOs and returns the first that matches |
| 266 | // |type|, which is one of the |BIO_TYPE_*| values. |
| 267 | OPENSSL_EXPORT BIO *BIO_find_type(BIO *bio, int type); |
| 268 |
| 269 | // BIO_copy_next_retry sets the retry flags and |retry_reason| of |bio| from |
| 270 | // the next BIO in the chain. |
| 271 | OPENSSL_EXPORT void BIO_copy_next_retry(BIO *bio); |
| 272 |
| 273 |
| 274 | // Printf functions. |
| 275 |
| 276 | // BIO_printf behaves like |printf| but outputs to |bio| rather than a |FILE|. |
| 277 | // It returns the number of bytes written or a negative number on error. |
| 278 | OPENSSL_EXPORT int BIO_printf(BIO *bio, const char *format, ...) |
| 279 | OPENSSL_PRINTF_FORMAT_FUNC(2, 3); |
| 280 |
| 281 |
| 282 | // Utility functions. |
| 283 |
| 284 | // BIO_indent prints min(|indent|, |max_indent|) spaces. It returns one on |
| 285 | // success and zero otherwise. |
| 286 | OPENSSL_EXPORT int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent); |
| 287 |
| 288 | // BIO_hexdump writes a hex dump of |data| to |bio|. Each line will be indented |
| 289 | // by |indent| spaces. It returns one on success and zero otherwise. |
| 290 | OPENSSL_EXPORT int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len, |
| 291 | unsigned indent); |
| 292 |
| 293 | // ERR_print_errors prints the current contents of the error stack to |bio| |
| 294 | // using human readable strings where possible. |
| 295 | OPENSSL_EXPORT void ERR_print_errors(BIO *bio); |
| 296 |
| 297 | // BIO_read_asn1 reads a single ASN.1 object from |bio|. If successful it sets |
| 298 | // |*out| to be an allocated buffer (that should be freed with |OPENSSL_free|), |
| 299 | // |*out_size| to the length, in bytes, of that buffer and returns one. |
| 300 | // Otherwise it returns zero. |
| 301 | // |
| 302 | // If the length of the object is greater than |max_len| or 2^32 then the |
| 303 | // function will fail. Long-form tags are not supported. If the length of the |
| 304 | // object is indefinite the full contents of |bio| are read, unless it would be |
| 305 | // greater than |max_len|, in which case the function fails. |
| 306 | // |
| 307 | // If the function fails then some unknown amount of data may have been read |
| 308 | // from |bio|. |
| 309 | OPENSSL_EXPORT int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, |
| 310 | size_t max_len); |
| 311 |
| 312 |
| 313 | // Memory BIOs. |
| 314 | // |
| 315 | // Memory BIOs can be used as a read-only source (with |BIO_new_mem_buf|) or a |
| 316 | // writable sink (with |BIO_new|, |BIO_s_mem| and |BIO_mem_contents|). Data |
| 317 | // written to a writable, memory BIO can be recalled by reading from it. |
| 318 | // |
| 319 | // Calling |BIO_reset| on a read-only BIO resets it to the original contents. |
| 320 | // On a writable BIO, it clears any data. |
| 321 | // |
| 322 | // If the close flag is set to |BIO_NOCLOSE| (not the default) then the |
| 323 | // underlying |BUF_MEM| will not be freed when the |BIO| is freed. |
| 324 | // |
| 325 | // Memory BIOs support |BIO_gets| and |BIO_puts|. |
| 326 | // |
| 327 | // |BIO_ctrl_pending| returns the number of bytes currently stored. |
| 328 |
| 329 | // BIO_NOCLOSE and |BIO_CLOSE| can be used as symbolic arguments when a "close |
| 330 | // flag" is passed to a BIO function. |
| 331 | #define BIO_NOCLOSE 0 |
| 332 | #define BIO_CLOSE 1 |
| 333 |
| 334 | // BIO_s_mem returns a |BIO_METHOD| that uses a in-memory buffer. |
| 335 | OPENSSL_EXPORT const BIO_METHOD *BIO_s_mem(void); |
| 336 |
| 337 | // BIO_new_mem_buf creates read-only BIO that reads from |len| bytes at |buf|. |
| 338 | // It returns the BIO or NULL on error. This function does not copy or take |
| 339 | // ownership of |buf|. The caller must ensure the memory pointed to by |buf| |
| 340 | // outlives the |BIO|. |
| 341 | // |
| 342 | // If |len| is negative, then |buf| is treated as a NUL-terminated string, but |
| 343 | // don't depend on this in new code. |
| 344 | OPENSSL_EXPORT BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len); |
| 345 |
| 346 | // BIO_mem_contents sets |*out_contents| to point to the current contents of |
| 347 | // |bio| and |*out_len| to contain the length of that data. It returns one on |
| 348 | // success and zero otherwise. |
| 349 | OPENSSL_EXPORT int BIO_mem_contents(const BIO *bio, |
| 350 | const uint8_t **out_contents, |
| 351 | size_t *out_len); |
| 352 |
| 353 | // BIO_get_mem_data sets |*contents| to point to the current contents of |bio| |
| 354 | // and returns the length of the data. |
| 355 | // |
| 356 | // WARNING: don't use this, use |BIO_mem_contents|. A return value of zero from |
| 357 | // this function can mean either that it failed or that the memory buffer is |
| 358 | // empty. |
| 359 | OPENSSL_EXPORT long BIO_get_mem_data(BIO *bio, char **contents); |
| 360 |
| 361 | // BIO_get_mem_ptr sets |*out| to a BUF_MEM containing the current contents of |
| 362 | // |bio|. It returns one on success or zero on error. |
| 363 | OPENSSL_EXPORT int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out); |
| 364 |
| 365 | // BIO_set_mem_buf sets |b| as the contents of |bio|. If |take_ownership| is |
| 366 | // non-zero, then |b| will be freed when |bio| is closed. Returns one on |
| 367 | // success or zero otherwise. |
| 368 | OPENSSL_EXPORT int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership); |
| 369 |
| 370 | // BIO_set_mem_eof_return sets the value that will be returned from reading |
| 371 | // |bio| when empty. If |eof_value| is zero then an empty memory BIO will |
| 372 | // return EOF (that is it will return zero and |BIO_should_retry| will be |
| 373 | // false). If |eof_value| is non zero then it will return |eof_value| when it |
| 374 | // is empty and it will set the read retry flag (that is |BIO_read_retry| is |
| 375 | // true). To avoid ambiguity with a normal positive return value, |eof_value| |
| 376 | // should be set to a negative value, typically -1. |
| 377 | // |
| 378 | // For a read-only BIO, the default is zero (EOF). For a writable BIO, the |
| 379 | // default is -1 so that additional data can be written once exhausted. |
| 380 | OPENSSL_EXPORT int BIO_set_mem_eof_return(BIO *bio, int eof_value); |
| 381 |
| 382 |
| 383 | // File descriptor BIOs. |
| 384 | // |
| 385 | // File descriptor BIOs are wrappers around the system's |read| and |write| |
| 386 | // functions. If the close flag is set then then |close| is called on the |
| 387 | // underlying file descriptor when the BIO is freed. |
| 388 | // |
| 389 | // |BIO_reset| attempts to seek the file pointer to the start of file using |
| 390 | // |lseek|. |
| 391 |
| 392 | #if !defined(OPENSSL_NO_POSIX_IO) |
| 393 | // BIO_s_fd returns a |BIO_METHOD| for file descriptor fds. |
| 394 | OPENSSL_EXPORT const BIO_METHOD *BIO_s_fd(void); |
| 395 |
| 396 | // BIO_new_fd creates a new file descriptor BIO wrapping |fd|. If |close_flag| |
| 397 | // is non-zero, then |fd| will be closed when the BIO is. |
| 398 | OPENSSL_EXPORT BIO *BIO_new_fd(int fd, int close_flag); |
| 399 | #endif |
| 400 |
| 401 | // BIO_set_fd sets the file descriptor of |bio| to |fd|. If |close_flag| is |
| 402 | // non-zero then |fd| will be closed when |bio| is. It returns one on success |
| 403 | // or zero on error. |
| 404 | // |
| 405 | // This function may also be used with socket BIOs (see |BIO_s_socket| and |
| 406 | // |BIO_new_socket|). |
| 407 | OPENSSL_EXPORT int BIO_set_fd(BIO *bio, int fd, int close_flag); |
| 408 |
| 409 | // BIO_get_fd returns the file descriptor currently in use by |bio| or -1 if |
| 410 | // |bio| does not wrap a file descriptor. If there is a file descriptor and |
| 411 | // |out_fd| is not NULL, it also sets |*out_fd| to the file descriptor. |
| 412 | // |
| 413 | // This function may also be used with socket BIOs (see |BIO_s_socket| and |
| 414 | // |BIO_new_socket|). |
| 415 | OPENSSL_EXPORT int BIO_get_fd(BIO *bio, int *out_fd); |
| 416 |
| 417 |
| 418 | // File BIOs. |
| 419 | // |
| 420 | // File BIOs are wrappers around a C |FILE| object. |
| 421 | // |
| 422 | // |BIO_flush| on a file BIO calls |fflush| on the wrapped stream. |
| 423 | // |
| 424 | // |BIO_reset| attempts to seek the file pointer to the start of file using |
| 425 | // |fseek|. |
| 426 | // |
| 427 | // Setting the close flag causes |fclose| to be called on the stream when the |
| 428 | // BIO is freed. |
| 429 |
| 430 | // BIO_s_file returns a BIO_METHOD that wraps a |FILE|. |
| 431 | OPENSSL_EXPORT const BIO_METHOD *BIO_s_file(void); |
| 432 |
| 433 | // BIO_new_file creates a file BIO by opening |filename| with the given mode. |
| 434 | // See the |fopen| manual page for details of the mode argument. On Windows, |
| 435 | // files may be opened in either binary or text mode so, as in |fopen|, callers |
| 436 | // must specify the desired option in |mode|. |
| 437 | OPENSSL_EXPORT BIO *BIO_new_file(const char *filename, const char *mode); |
| 438 |
| 439 | // BIO_FP_TEXT indicates the |FILE| should be switched to text mode on Windows. |
| 440 | // It has no effect on non-Windows platforms. |
| 441 | #define BIO_FP_TEXT 0x10 |
| 442 |
| 443 | // BIO_new_fp creates a new file BIO that wraps |file|. If |flags| contains |
| 444 | // |BIO_CLOSE|, then |fclose| will be called on |file| when the BIO is closed. |
| 445 | // |
| 446 | // On Windows, if |flags| contains |BIO_FP_TEXT|, this function will |
| 447 | // additionally switch |file| to text mode. This is not recommended, but may be |
| 448 | // required for OpenSSL compatibility. If |file| was not already in text mode, |
| 449 | // mode changes can cause unflushed data in |file| to be written in unexpected |
| 450 | // ways. See |_setmode| in Windows documentation for details. |
| 451 | // |
| 452 | // Unlike OpenSSL, if |flags| does not contain |BIO_FP_TEXT|, the translation |
| 453 | // mode of |file| is left as-is. In OpenSSL, |file| will be set to binary, with |
| 454 | // the same pitfalls as above. BoringSSL does not do this so that wrapping a |
| 455 | // |FILE| in a |BIO| will not inadvertently change its state. |
| 456 | // |
| 457 | // To avoid these pitfalls, callers should set the desired translation mode when |
| 458 | // opening the file. If targeting just BoringSSL, this is sufficient. If |
| 459 | // targeting both OpenSSL and BoringSSL, callers should set |BIO_FP_TEXT| to |
| 460 | // match the desired state of the file. |
| 461 | OPENSSL_EXPORT BIO *BIO_new_fp(FILE *file, int flags); |
| 462 |
| 463 | // BIO_get_fp sets |*out_file| to the current |FILE| for |bio|. It returns one |
| 464 | // on success and zero otherwise. |
| 465 | OPENSSL_EXPORT int BIO_get_fp(BIO *bio, FILE **out_file); |
| 466 |
| 467 | // BIO_set_fp sets the |FILE| for |bio|. If |flags| contains |BIO_CLOSE| then |
| 468 | // |fclose| will be called on |file| when |bio| is closed. It returns one on |
| 469 | // success and zero otherwise. |
| 470 | // |
| 471 | // On Windows, if |flags| contains |BIO_FP_TEXT|, this function will |
| 472 | // additionally switch |file| to text mode. This is not recommended, but may be |
| 473 | // required for OpenSSL compatibility. If |file| was not already in text mode, |
| 474 | // mode changes can cause unflushed data in |file| to be written in unexpected |
| 475 | // ways. See |_setmode| in Windows documentation for details. |
| 476 | // |
| 477 | // Unlike OpenSSL, if |flags| does not contain |BIO_FP_TEXT|, the translation |
| 478 | // mode of |file| is left as-is. In OpenSSL, |file| will be set to binary, with |
| 479 | // the same pitfalls as above. BoringSSL does not do this so that wrapping a |
| 480 | // |FILE| in a |BIO| will not inadvertently change its state. |
| 481 | // |
| 482 | // To avoid these pitfalls, callers should set the desired translation mode when |
| 483 | // opening the file. If targeting just BoringSSL, this is sufficient. If |
| 484 | // targeting both OpenSSL and BoringSSL, callers should set |BIO_FP_TEXT| to |
| 485 | // match the desired state of the file. |
| 486 | OPENSSL_EXPORT int BIO_set_fp(BIO *bio, FILE *file, int flags); |
| 487 |
| 488 | // BIO_read_filename opens |filename| for reading and sets the result as the |
| 489 | // |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE| |
| 490 | // will be closed when |bio| is freed. On Windows, the file is opened in binary |
| 491 | // mode. |
| 492 | OPENSSL_EXPORT int BIO_read_filename(BIO *bio, const char *filename); |
| 493 |
| 494 | // BIO_write_filename opens |filename| for writing and sets the result as the |
| 495 | // |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE| |
| 496 | // will be closed when |bio| is freed. On Windows, the file is opened in binary |
| 497 | // mode. |
| 498 | OPENSSL_EXPORT int BIO_write_filename(BIO *bio, const char *filename); |
| 499 |
| 500 | // BIO_append_filename opens |filename| for appending and sets the result as |
| 501 | // the |FILE| for |bio|. It returns one on success and zero otherwise. The |
| 502 | // |FILE| will be closed when |bio| is freed. On Windows, the file is opened in |
| 503 | // binary mode. |
| 504 | OPENSSL_EXPORT int BIO_append_filename(BIO *bio, const char *filename); |
| 505 |
| 506 | // BIO_rw_filename opens |filename| for reading and writing and sets the result |
| 507 | // as the |FILE| for |bio|. It returns one on success and zero otherwise. The |
| 508 | // |FILE| will be closed when |bio| is freed. On Windows, the file is opened in |
| 509 | // binary mode. |
| 510 | OPENSSL_EXPORT int BIO_rw_filename(BIO *bio, const char *filename); |
| 511 |
| 512 | // BIO_tell returns the file offset of |bio|, or a negative number on error or |
| 513 | // if |bio| does not support the operation. |
| 514 | // |
| 515 | // TODO(https://crbug.com/boringssl/465): On platforms where |long| is 32-bit, |
| 516 | // this function cannot report 64-bit offsets. |
| 517 | OPENSSL_EXPORT long BIO_tell(BIO *bio); |
| 518 |
| 519 | // BIO_seek sets the file offset of |bio| to |offset|. It returns a non-negative |
| 520 | // number on success and a negative number on error. If |bio| is a file |
| 521 | // descriptor |BIO|, it returns the resulting file offset on success. If |bio| |
| 522 | // is a file |BIO|, it returns zero on success. |
| 523 | // |
| 524 | // WARNING: This function's return value conventions differs from most functions |
| 525 | // in this library. |
| 526 | // |
| 527 | // TODO(https://crbug.com/boringssl/465): On platforms where |long| is 32-bit, |
| 528 | // this function cannot handle 64-bit offsets. |
| 529 | OPENSSL_EXPORT long BIO_seek(BIO *bio, long offset); |
| 530 |
| 531 |
| 532 | // Socket BIOs. |
| 533 | // |
| 534 | // Socket BIOs behave like file descriptor BIOs but, on Windows systems, wrap |
| 535 | // the system's |recv| and |send| functions instead of |read| and |write|. On |
| 536 | // Windows, file descriptors are provided by C runtime and are not |
| 537 | // interchangeable with sockets. |
| 538 | // |
| 539 | // Socket BIOs may be used with |BIO_set_fd| and |BIO_get_fd|. |
| 540 | // |
| 541 | // TODO(davidben): Add separate APIs and fix the internals to use |SOCKET|s |
| 542 | // around rather than rely on int casts. |
| 543 |
| 544 | #if !defined(OPENSSL_NO_SOCK) |
| 545 | OPENSSL_EXPORT const BIO_METHOD *BIO_s_socket(void); |
| 546 |
| 547 | // BIO_new_socket allocates and initialises a fresh BIO which will read and |
| 548 | // write to the socket |fd|. If |close_flag| is |BIO_CLOSE| then closing the |
| 549 | // BIO will close |fd|. It returns the fresh |BIO| or NULL on error. |
| 550 | OPENSSL_EXPORT BIO *BIO_new_socket(int fd, int close_flag); |
| 551 | #endif // !OPENSSL_NO_SOCK |
| 552 |
| 553 |
| 554 | // Connect BIOs. |
| 555 | // |
| 556 | // A connection BIO creates a network connection and transfers data over the |
| 557 | // resulting socket. |
| 558 |
| 559 | #if !defined(OPENSSL_NO_SOCK) |
| 560 | OPENSSL_EXPORT const BIO_METHOD *BIO_s_connect(void); |
| 561 |
| 562 | // BIO_new_connect returns a BIO that connects to the given hostname and port. |
| 563 | // The |host_and_optional_port| argument should be of the form |
| 564 | // "www.example.com" or "www.example.com:443". If the port is omitted, it must |
| 565 | // be provided with |BIO_set_conn_port|. |
| 566 | // |
| 567 | // It returns the new BIO on success, or NULL on error. |
| 568 | OPENSSL_EXPORT BIO *BIO_new_connect(const char *host_and_optional_port); |
| 569 |
| 570 | // BIO_set_conn_hostname sets |host_and_optional_port| as the hostname and |
| 571 | // optional port that |bio| will connect to. If the port is omitted, it must be |
| 572 | // provided with |BIO_set_conn_port|. |
| 573 | // |
| 574 | // It returns one on success and zero otherwise. |
| 575 | OPENSSL_EXPORT int BIO_set_conn_hostname(BIO *bio, |
| 576 | const char *host_and_optional_port); |
| 577 |
| 578 | // BIO_set_conn_port sets |port_str| as the port or service name that |bio| |
| 579 | // will connect to. It returns one on success and zero otherwise. |
| 580 | OPENSSL_EXPORT int BIO_set_conn_port(BIO *bio, const char *port_str); |
| 581 |
| 582 | // BIO_set_conn_int_port sets |*port| as the port that |bio| will connect to. |
| 583 | // It returns one on success and zero otherwise. |
| 584 | OPENSSL_EXPORT int BIO_set_conn_int_port(BIO *bio, const int *port); |
| 585 |
| 586 | // BIO_set_nbio sets whether |bio| will use non-blocking I/O operations. It |
| 587 | // returns one on success and zero otherwise. This only works for connect BIOs |
| 588 | // and must be called before |bio| is connected to take effect. |
| 589 | // |
| 590 | // For socket and fd BIOs, callers must configure blocking vs. non-blocking I/O |
| 591 | // using the underlying platform APIs. |
| 592 | OPENSSL_EXPORT int BIO_set_nbio(BIO *bio, int on); |
| 593 |
| 594 | // BIO_do_connect connects |bio| if it has not been connected yet. It returns |
| 595 | // one on success and <= 0 otherwise. |
| 596 | OPENSSL_EXPORT int BIO_do_connect(BIO *bio); |
| 597 | #endif // !OPENSSL_NO_SOCK |
| 598 |
| 599 |
| 600 | // Datagram BIOs. |
| 601 | // |
| 602 | // TODO(fork): not implemented. |
| 603 |
| 604 | #define BIO_CTRL_DGRAM_QUERY_MTU 40 // as kernel for current MTU |
| 605 |
| 606 | #define BIO_CTRL_DGRAM_SET_MTU 42 /* set cached value for MTU. want to use |
| 607 | this if asking the kernel fails */ |
| 608 |
| 609 | #define BIO_CTRL_DGRAM_MTU_EXCEEDED 43 /* check whether the MTU was exceed in |
| 610 | the previous write operation. */ |
| 611 |
| 612 | // BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT is unsupported as it is unused by consumers |
| 613 | // and depends on |timeval|, which is not 2038-clean on all platforms. |
| 614 |
| 615 | #define BIO_CTRL_DGRAM_GET_PEER 46 |
| 616 |
| 617 | #define BIO_CTRL_DGRAM_GET_FALLBACK_MTU 47 |
| 618 |
| 619 |
| 620 | // BIO Pairs. |
| 621 | // |
| 622 | // BIO pairs provide a "loopback" like system: a pair of BIOs where data |
| 623 | // written to one can be read from the other and vice versa. |
| 624 |
| 625 | // BIO_new_bio_pair sets |*out1| and |*out2| to two freshly created BIOs where |
| 626 | // data written to one can be read from the other and vice versa. The |
| 627 | // |writebuf1| argument gives the size of the buffer used in |*out1| and |
| 628 | // |writebuf2| for |*out2|. It returns one on success and zero on error. |
| 629 | OPENSSL_EXPORT int BIO_new_bio_pair(BIO **out1, size_t writebuf1, BIO **out2, |
| 630 | size_t writebuf2); |
| 631 |
| 632 | // BIO_ctrl_get_read_request returns the number of bytes that the other side of |
| 633 | // |bio| tried (unsuccessfully) to read. |
| 634 | OPENSSL_EXPORT size_t BIO_ctrl_get_read_request(BIO *bio); |
| 635 |
| 636 | // BIO_ctrl_get_write_guarantee returns the number of bytes that |bio| (which |
| 637 | // must have been returned by |BIO_new_bio_pair|) will accept on the next |
| 638 | // |BIO_write| call. |
| 639 | OPENSSL_EXPORT size_t BIO_ctrl_get_write_guarantee(BIO *bio); |
| 640 |
| 641 | // BIO_shutdown_wr marks |bio| as closed, from the point of view of the other |
| 642 | // side of the pair. Future |BIO_write| calls on |bio| will fail. It returns |
| 643 | // one on success and zero otherwise. |
| 644 | OPENSSL_EXPORT int BIO_shutdown_wr(BIO *bio); |
| 645 |
| 646 |
| 647 | // Custom BIOs. |
| 648 | // |
| 649 | // Consumers can create custom |BIO|s by filling in a |BIO_METHOD| and using |
| 650 | // low-level control functions to set state. |
| 651 |
| 652 | // BIO_get_new_index returns a new "type" value for a custom |BIO|. |
| 653 | OPENSSL_EXPORT int BIO_get_new_index(void); |
| 654 |
| 655 | // BIO_meth_new returns a newly-allocated |BIO_METHOD| or NULL on allocation |
| 656 | // error. The |type| specifies the type that will be returned by |
| 657 | // |BIO_method_type|. If this is unnecessary, this value may be zero. The |name| |
| 658 | // parameter is vestigial and may be NULL. |
| 659 | // |
| 660 | // Use the |BIO_meth_set_*| functions below to initialize the |BIO_METHOD|. The |
| 661 | // function implementations may use |BIO_set_data| and |BIO_get_data| to add |
| 662 | // method-specific state to associated |BIO|s. Additionally, |BIO_set_init| must |
| 663 | // be called after an associated |BIO| is fully initialized. State set via |
| 664 | // |BIO_set_data| may be released by configuring a destructor with |
| 665 | // |BIO_meth_set_destroy|. |
| 666 | OPENSSL_EXPORT BIO_METHOD *BIO_meth_new(int type, const char *name); |
| 667 |
| 668 | // BIO_meth_free releases memory associated with |method|. |
| 669 | OPENSSL_EXPORT void BIO_meth_free(BIO_METHOD *method); |
| 670 |
| 671 | // BIO_meth_set_create sets a function to be called on |BIO_new| for |method| |
| 672 | // and returns one. The function should return one on success and zero on |
| 673 | // error. |
| 674 | OPENSSL_EXPORT int BIO_meth_set_create(BIO_METHOD *method, |
| 675 | int (*create_func)(BIO *)); |
| 676 |
| 677 | // BIO_meth_set_destroy sets a function to release data associated with a |BIO| |
| 678 | // and returns one. The function's return value is ignored. |
| 679 | OPENSSL_EXPORT int BIO_meth_set_destroy(BIO_METHOD *method, |
| 680 | int (*destroy_func)(BIO *)); |
| 681 |
| 682 | // BIO_meth_set_write sets the implementation of |BIO_write| for |method| and |
| 683 | // returns one. |BIO_METHOD|s which implement |BIO_write| should also implement |
| 684 | // |BIO_CTRL_FLUSH|. (See |BIO_meth_set_ctrl|.) |
| 685 | OPENSSL_EXPORT int BIO_meth_set_write(BIO_METHOD *method, |
| 686 | int (*write_func)(BIO *, const char *, |
| 687 | int)); |
| 688 |
| 689 | // BIO_meth_set_read sets the implementation of |BIO_read| for |method| and |
| 690 | // returns one. |
| 691 | OPENSSL_EXPORT int BIO_meth_set_read(BIO_METHOD *method, |
| 692 | int (*read_func)(BIO *, char *, int)); |
| 693 |
| 694 | // BIO_meth_set_gets sets the implementation of |BIO_gets| for |method| and |
| 695 | // returns one. |
| 696 | OPENSSL_EXPORT int BIO_meth_set_gets(BIO_METHOD *method, |
| 697 | int (*gets_func)(BIO *, char *, int)); |
| 698 |
| 699 | // BIO_meth_set_ctrl sets the implementation of |BIO_ctrl| for |method| and |
| 700 | // returns one. |
| 701 | OPENSSL_EXPORT int BIO_meth_set_ctrl(BIO_METHOD *method, |
| 702 | long (*ctrl_func)(BIO *, int, long, |
| 703 | void *)); |
| 704 |
| 705 | // BIO_set_data sets custom data on |bio|. It may be retried with |
| 706 | // |BIO_get_data|. |
| 707 | // |
| 708 | // This function should only be called by the implementation of a custom |BIO|. |
| 709 | // In particular, the data pointer of a built-in |BIO| is private to the |
| 710 | // library. For other uses, see |BIO_set_ex_data| and |BIO_set_app_data|. |
| 711 | OPENSSL_EXPORT void BIO_set_data(BIO *bio, void *ptr); |
| 712 |
| 713 | // BIO_get_data returns custom data on |bio| set by |BIO_get_data|. |
| 714 | // |
| 715 | // This function should only be called by the implementation of a custom |BIO|. |
| 716 | // In particular, the data pointer of a built-in |BIO| is private to the |
| 717 | // library. For other uses, see |BIO_get_ex_data| and |BIO_get_app_data|. |
| 718 | OPENSSL_EXPORT void *BIO_get_data(BIO *bio); |
| 719 |
| 720 | // BIO_set_init sets whether |bio| has been fully initialized. Until fully |
| 721 | // initialized, |BIO_read| and |BIO_write| will fail. |
| 722 | OPENSSL_EXPORT void BIO_set_init(BIO *bio, int init); |
| 723 |
| 724 | // BIO_get_init returns whether |bio| has been fully initialized. |
| 725 | OPENSSL_EXPORT int BIO_get_init(BIO *bio); |
| 726 |
| 727 | // These are values of the |cmd| argument to |BIO_ctrl|. |
| 728 |
| 729 | // BIO_CTRL_RESET implements |BIO_reset|. The arguments are unused. |
| 730 | #define BIO_CTRL_RESET 1 |
| 731 |
| 732 | // BIO_CTRL_EOF implements |BIO_eof|. The arguments are unused. |
| 733 | #define BIO_CTRL_EOF 2 |
| 734 |
| 735 | // BIO_CTRL_INFO is a legacy command that returns information specific to the |
| 736 | // type of |BIO|. It is not safe to call generically and should not be |
| 737 | // implemented in new |BIO| types. |
| 738 | #define BIO_CTRL_INFO 3 |
| 739 |
| 740 | // BIO_CTRL_GET_CLOSE returns the close flag set by |BIO_CTRL_SET_CLOSE|. The |
| 741 | // arguments are unused. |
| 742 | #define BIO_CTRL_GET_CLOSE 8 |
| 743 |
| 744 | // BIO_CTRL_SET_CLOSE implements |BIO_set_close|. The |larg| argument is the |
| 745 | // close flag. |
| 746 | #define BIO_CTRL_SET_CLOSE 9 |
| 747 |
| 748 | // BIO_CTRL_PENDING implements |BIO_pending|. The arguments are unused. |
| 749 | #define BIO_CTRL_PENDING 10 |
| 750 |
| 751 | // BIO_CTRL_FLUSH implements |BIO_flush|. The arguments are unused. |
| 752 | #define BIO_CTRL_FLUSH 11 |
| 753 |
| 754 | // BIO_CTRL_WPENDING implements |BIO_wpending|. The arguments are unused. |
| 755 | #define BIO_CTRL_WPENDING 13 |
| 756 |
| 757 | // BIO_CTRL_SET_CALLBACK sets an informational callback of type |
| 758 | // int cb(BIO *bio, int state, int ret) |
| 759 | #define BIO_CTRL_SET_CALLBACK 14 |
| 760 |
| 761 | // BIO_CTRL_GET_CALLBACK returns the callback set by |BIO_CTRL_SET_CALLBACK|. |
| 762 | #define BIO_CTRL_GET_CALLBACK 15 |
| 763 |
| 764 | // The following are never used, but are defined to aid porting existing code. |
| 765 | #define BIO_CTRL_SET 4 |
| 766 | #define BIO_CTRL_GET 5 |
| 767 | #define BIO_CTRL_PUSH 6 |
| 768 | #define BIO_CTRL_POP 7 |
| 769 | #define BIO_CTRL_DUP 12 |
| 770 | #define BIO_CTRL_SET_FILENAME 30 |
| 771 |
| 772 |
| 773 | // ex_data functions. |
| 774 | // |
| 775 | // See |ex_data.h| for details. |
| 776 |
| 777 | OPENSSL_EXPORT int BIO_get_ex_new_index(long argl, void *argp, |
| 778 | CRYPTO_EX_unused *unused, |
| 779 | CRYPTO_EX_dup *dup_unused, |
| 780 | CRYPTO_EX_free *free_func); |
| 781 | OPENSSL_EXPORT int BIO_set_ex_data(BIO *bio, int idx, void *arg); |
| 782 | OPENSSL_EXPORT void *BIO_get_ex_data(const BIO *bio, int idx); |
| 783 |
| 784 | #define BIO_set_app_data(bio, arg) (BIO_set_ex_data(bio, 0, (char *)(arg))) |
| 785 | #define BIO_get_app_data(bio) (BIO_get_ex_data(bio, 0)) |
| 786 |
| 787 |
| 788 | // Deprecated functions. |
| 789 |
| 790 | // BIO_f_base64 returns a filter |BIO| that base64-encodes data written into |
| 791 | // it, and decodes data read from it. |BIO_gets| is not supported. Call |
| 792 | // |BIO_flush| when done writing, to signal that no more data are to be |
| 793 | // encoded. The flag |BIO_FLAGS_BASE64_NO_NL| may be set to encode all the data |
| 794 | // on one line. |
| 795 | // |
| 796 | // Use |EVP_EncodeBlock| and |EVP_DecodeBase64| instead. |
| 797 | OPENSSL_EXPORT const BIO_METHOD *BIO_f_base64(void); |
| 798 |
| 799 | OPENSSL_EXPORT void BIO_set_retry_special(BIO *bio); |
| 800 |
| 801 | // BIO_set_write_buffer_size returns zero. |
| 802 | OPENSSL_EXPORT int BIO_set_write_buffer_size(BIO *bio, int buffer_size); |
| 803 |
| 804 | // BIO_set_shutdown sets a method-specific "shutdown" bit on |bio|. |
| 805 | OPENSSL_EXPORT void BIO_set_shutdown(BIO *bio, int shutdown); |
| 806 |
| 807 | // BIO_get_shutdown returns the method-specific "shutdown" bit. |
| 808 | OPENSSL_EXPORT int BIO_get_shutdown(BIO *bio); |
| 809 |
| 810 | // BIO_meth_set_puts returns one. |BIO_puts| is implemented with |BIO_write| in |
| 811 | // BoringSSL. |
| 812 | OPENSSL_EXPORT int BIO_meth_set_puts(BIO_METHOD *method, |
| 813 | int (*puts)(BIO *, const char *)); |
| 814 |
| 815 |
| 816 | // Private functions |
| 817 |
| 818 | #define BIO_FLAGS_READ 0x01 |
| 819 | #define BIO_FLAGS_WRITE 0x02 |
| 820 | #define BIO_FLAGS_IO_SPECIAL 0x04 |
| 821 | #define BIO_FLAGS_RWS (BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL) |
| 822 | #define BIO_FLAGS_SHOULD_RETRY 0x08 |
| 823 | #define BIO_FLAGS_BASE64_NO_NL 0x100 |
| 824 | // BIO_FLAGS_MEM_RDONLY is used with memory BIOs. It means we shouldn't free up |
| 825 | // or change the data in any way. |
| 826 | #define BIO_FLAGS_MEM_RDONLY 0x200 |
| 827 |
| 828 | // BIO_TYPE_DESCRIPTOR denotes that the |BIO| responds to the |BIO_C_SET_FD| |
| 829 | // (|BIO_set_fd|) and |BIO_C_GET_FD| (|BIO_get_fd|) control hooks. |
| 830 | #define BIO_TYPE_DESCRIPTOR 0x0100 // socket, fd, connect or accept |
| 831 | #define BIO_TYPE_FILTER 0x0200 |
| 832 | #define BIO_TYPE_SOURCE_SINK 0x0400 |
| 833 |
| 834 | // These are the 'types' of BIOs |
| 835 | #define BIO_TYPE_NONE 0 |
| 836 | #define BIO_TYPE_MEM (1 | BIO_TYPE_SOURCE_SINK) |
| 837 | #define BIO_TYPE_FILE (2 | BIO_TYPE_SOURCE_SINK) |
| 838 | #define BIO_TYPE_FD (4 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR) |
| 839 | #define BIO_TYPE_SOCKET (5 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR) |
| 840 | #define BIO_TYPE_NULL (6 | BIO_TYPE_SOURCE_SINK) |
| 841 | #define BIO_TYPE_SSL (7 | BIO_TYPE_FILTER) |
| 842 | #define BIO_TYPE_MD (8 | BIO_TYPE_FILTER) |
| 843 | #define BIO_TYPE_BUFFER (9 | BIO_TYPE_FILTER) |
| 844 | #define BIO_TYPE_CIPHER (10 | BIO_TYPE_FILTER) |
| 845 | #define BIO_TYPE_BASE64 (11 | BIO_TYPE_FILTER) |
| 846 | #define BIO_TYPE_CONNECT (12 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR) |
| 847 | #define BIO_TYPE_ACCEPT (13 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR) |
| 848 | #define BIO_TYPE_PROXY_CLIENT (14 | BIO_TYPE_FILTER) |
| 849 | #define BIO_TYPE_PROXY_SERVER (15 | BIO_TYPE_FILTER) |
| 850 | #define BIO_TYPE_NBIO_TEST (16 | BIO_TYPE_FILTER) |
| 851 | #define BIO_TYPE_NULL_FILTER (17 | BIO_TYPE_FILTER) |
| 852 | #define BIO_TYPE_BER (18 | BIO_TYPE_FILTER) // BER -> bin filter |
| 853 | #define BIO_TYPE_BIO (19 | BIO_TYPE_SOURCE_SINK) // (half a) BIO pair |
| 854 | #define BIO_TYPE_LINEBUFFER (20 | BIO_TYPE_FILTER) |
| 855 | #define BIO_TYPE_DGRAM (21 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR) |
| 856 | #define BIO_TYPE_ASN1 (22 | BIO_TYPE_FILTER) |
| 857 | #define BIO_TYPE_COMP (23 | BIO_TYPE_FILTER) |
| 858 |
| 859 | // BIO_TYPE_START is the first user-allocated |BIO| type. No pre-defined type, |
| 860 | // flag bits aside, may exceed this value. |
| 861 | #define BIO_TYPE_START 128 |
| 862 |
| 863 | struct bio_method_st { |
| 864 | int type; |
| 865 | const char *name; |
| 866 | int (*bwrite)(BIO *, const char *, int); |
| 867 | int (*bread)(BIO *, char *, int); |
| 868 | // TODO(fork): remove bputs. |
| 869 | int (*bputs)(BIO *, const char *); |
| 870 | int (*bgets)(BIO *, char *, int); |
| 871 | long (*ctrl)(BIO *, int, long, void *); |
| 872 | int (*create)(BIO *); |
| 873 | int (*destroy)(BIO *); |
| 874 | long (*callback_ctrl)(BIO *, int, bio_info_cb); |
| 875 | }; |
| 876 |
| 877 | struct bio_st { |
| 878 | const BIO_METHOD *method; |
| 879 | CRYPTO_EX_DATA ex_data; |
| 880 |
| 881 | // init is non-zero if this |BIO| has been initialised. |
| 882 | int init; |
| 883 | // shutdown is often used by specific |BIO_METHOD|s to determine whether |
| 884 | // they own some underlying resource. This flag can often by controlled by |
| 885 | // |BIO_set_close|. For example, whether an fd BIO closes the underlying fd |
| 886 | // when it, itself, is closed. |
| 887 | int shutdown; |
| 888 | int flags; |
| 889 | int retry_reason; |
| 890 | // num is a BIO-specific value. For example, in fd BIOs it's used to store a |
| 891 | // file descriptor. |
| 892 | int num; |
| 893 | CRYPTO_refcount_t references; |
| 894 | void *ptr; |
| 895 | // next_bio points to the next |BIO| in a chain. This |BIO| owns a reference |
| 896 | // to |next_bio|. |
| 897 | BIO *next_bio; // used by filter BIOs |
| 898 | uint64_t num_read, num_write; |
| 899 | }; |
| 900 |
| 901 | #define BIO_C_SET_CONNECT 100 |
| 902 | #define BIO_C_DO_STATE_MACHINE 101 |
| 903 | #define BIO_C_SET_NBIO 102 |
| 904 | #define BIO_C_SET_PROXY_PARAM 103 |
| 905 | #define BIO_C_SET_FD 104 |
| 906 | #define BIO_C_GET_FD 105 |
| 907 | #define BIO_C_SET_FILE_PTR 106 |
| 908 | #define BIO_C_GET_FILE_PTR 107 |
| 909 | #define BIO_C_SET_FILENAME 108 |
| 910 | #define BIO_C_SET_SSL 109 |
| 911 | #define BIO_C_SET_MD 111 |
| 912 | #define BIO_C_GET_MD 112 |
| 913 | #define BIO_C_GET_CIPHER_STATUS 113 |
| 914 | #define BIO_C_SET_BUF_MEM 114 |
| 915 | #define BIO_C_GET_BUF_MEM_PTR 115 |
| 916 | #define BIO_C_GET_BUFF_NUM_LINES 116 |
| 917 | #define BIO_C_SET_BUFF_SIZE 117 |
| 918 | #define BIO_C_SET_ACCEPT 118 |
| 919 | #define BIO_C_SSL_MODE 119 |
| 920 | #define BIO_C_GET_MD_CTX 120 |
| 921 | #define BIO_C_GET_PROXY_PARAM 121 |
| 922 | #define BIO_C_SET_BUFF_READ_DATA 122 // data to read first |
| 923 | #define BIO_C_GET_ACCEPT 124 |
| 924 | #define BIO_C_FILE_SEEK 128 |
| 925 | #define BIO_C_GET_CIPHER_CTX 129 |
| 926 | #define BIO_C_SET_BUF_MEM_EOF_RETURN 130 // return end of input value |
| 927 | #define BIO_C_SET_BIND_MODE 131 |
| 928 | #define BIO_C_GET_BIND_MODE 132 |
| 929 | #define BIO_C_FILE_TELL 133 |
| 930 | #define BIO_C_GET_SOCKS 134 |
| 931 | #define BIO_C_SET_SOCKS 135 |
| 932 |
| 933 | #define BIO_C_SET_WRITE_BUF_SIZE 136 // for BIO_s_bio |
| 934 | #define BIO_C_GET_WRITE_BUF_SIZE 137 |
| 935 | #define BIO_C_GET_WRITE_GUARANTEE 140 |
| 936 | #define BIO_C_GET_READ_REQUEST 141 |
| 937 | #define BIO_C_SHUTDOWN_WR 142 |
| 938 | #define BIO_C_NREAD0 143 |
| 939 | #define BIO_C_NREAD 144 |
| 940 | #define BIO_C_NWRITE0 145 |
| 941 | #define BIO_C_NWRITE 146 |
| 942 | #define BIO_C_RESET_READ_REQUEST 147 |
| 943 | #define BIO_C_SET_MD_CTX 148 |
| 944 |
| 945 | #define BIO_C_SET_PREFIX 149 |
| 946 | #define BIO_C_GET_PREFIX 150 |
| 947 | #define BIO_C_SET_SUFFIX 151 |
| 948 | #define BIO_C_GET_SUFFIX 152 |
| 949 |
| 950 | #define BIO_C_SET_EX_ARG 153 |
| 951 | #define BIO_C_GET_EX_ARG 154 |
| 952 |
| 953 |
| 954 | #if defined(__cplusplus) |
| 955 | } // extern C |
| 956 |
| 957 | extern "C++" { |
| 958 |
| 959 | BSSL_NAMESPACE_BEGIN |
| 960 |
| 961 | BORINGSSL_MAKE_DELETER(BIO, BIO_free) |
| 962 | BORINGSSL_MAKE_UP_REF(BIO, BIO_up_ref) |
| 963 | BORINGSSL_MAKE_DELETER(BIO_METHOD, BIO_meth_free) |
| 964 |
| 965 | BSSL_NAMESPACE_END |
| 966 |
| 967 | } // extern C++ |
| 968 |
| 969 | #endif |
| 970 |
| 971 | #define BIO_R_BAD_FOPEN_MODE 100 |
| 972 | #define BIO_R_BROKEN_PIPE 101 |
| 973 | #define BIO_R_CONNECT_ERROR 102 |
| 974 | #define BIO_R_ERROR_SETTING_NBIO 103 |
| 975 | #define BIO_R_INVALID_ARGUMENT 104 |
| 976 | #define BIO_R_IN_USE 105 |
| 977 | #define BIO_R_KEEPALIVE 106 |
| 978 | #define BIO_R_NBIO_CONNECT_ERROR 107 |
| 979 | #define BIO_R_NO_HOSTNAME_SPECIFIED 108 |
| 980 | #define BIO_R_NO_PORT_SPECIFIED 109 |
| 981 | #define BIO_R_NO_SUCH_FILE 110 |
| 982 | #define BIO_R_NULL_PARAMETER 111 |
| 983 | #define BIO_R_SYS_LIB 112 |
| 984 | #define BIO_R_UNABLE_TO_CREATE_SOCKET 113 |
| 985 | #define BIO_R_UNINITIALIZED 114 |
| 986 | #define BIO_R_UNSUPPORTED_METHOD 115 |
| 987 | #define BIO_R_WRITE_TO_READ_ONLY_BIO 116 |
| 988 |
| 989 | #endif // OPENSSL_HEADER_BIO_H |
| 990 |