Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/bio.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_BIO_H
5 #define OPENSSL_HEADER_BIO_H
6
7 #include <openssl/base.h>
8
9 #include <stdio.h> // For FILE
10
11 #include <openssl/buffer.h>
12 #include <openssl/err.h> // for ERR_print_errors_fp
13 #include <openssl/ex_data.h>
14 #include <openssl/stack.h>
15 #include <openssl/thread.h>
16
17 #if defined(__cplusplus)
18 extern "C" {
19 #endif
20
21
22 // BIO abstracts over a file-descriptor like interface.
23
24
25 // Allocation and freeing.
26
27 DEFINE_STACK_OF(BIO)
28
29 // BIO_new creates a new BIO with the given method and a reference count of one.
30 // It returns the fresh |BIO|, or NULL on error.
31 OPENSSL_EXPORT BIO *BIO_new(const BIO_METHOD *method);
32
33 // BIO_free decrements the reference count of |bio|. If the reference count
34 // drops to zero, it (optionally) calls the BIO's callback with |BIO_CB_FREE|,
35 // calls the destroy callback, if present, on the method and frees |bio| itself.
36 // It then repeats that for the next BIO in the chain, if any.
37 //
38 // It returns one on success or zero otherwise.
39 OPENSSL_EXPORT int BIO_free(BIO *bio);
40
41 // BIO_vfree performs the same actions as |BIO_free|, but has a void return
42 // value. This is provided for API-compat.
43 //
44 // TODO(fork): remove.
45 OPENSSL_EXPORT void BIO_vfree(BIO *bio);
46
47 // BIO_up_ref increments the reference count of |bio| and returns one.
48 OPENSSL_EXPORT int BIO_up_ref(BIO *bio);
49
50
51 // Basic I/O.
52
53 // BIO_read calls the |bio| |callback_ex| if set with |BIO_CB_READ|, attempts to
54 // read |len| bytes into |data|, then calls |callback_ex| with
55 // |BIO_CB_READ|+|BIO_CB_RETURN|. If |len| is less than or equal to zero, the
56 // function does nothing and return zero. If |callback_ex| is set BIO_read
57 // returns the value from calling the |callback_ex|, otherwise |BIO_read|
58 // returns the number of bytes read, zero on EOF, or a negative number on error.
59 OPENSSL_EXPORT int BIO_read(BIO *bio, void *data, int len);
60
61 // BIO_read_ex calls |BIO_read| and stores the number of bytes read in
62 // |read_bytes|. It returns one on success and zero otherwise.
63 //
64 // WARNING: Don't use this, use |BIO_read| instead. |BIO_read_ex| returns zero
65 // on EOF, which disallows any mechanism to notify the user that an EOF has
66 // occurred and renders this API unusable. See openssl/openssl#8208.
67 OPENSSL_EXPORT int BIO_read_ex(BIO *bio, void *data, size_t data_len,
68 size_t *read_bytes);
69
70 // BIO_gets calls |callback_ex| from |bio| if set with |BIO_CB_GETS|, attempts
71 // to read a line from |bio| and writes at most |size| bytes into |buf|. Then it
72 // calls |callback_ex| with |BIO_CB_GETS|+|BIO_CB_RETURN|. If |size| is less
73 // than or equal to zero, the function does nothing and returns zero.
74 // If |callback_ex| is set, |BIO_gets| returns the value from calling
75 // |callback_ex|, otherwise |BIO_gets| returns the number of bytes read, or a
76 // negative number on error.
77 //
78 // This function's output always includes a trailing NUL byte, so it will read
79 // at most |size - 1| bytes.
80 // If the function read a complete line, the output will include the newline
81 // character, '\n'. If no newline was found before |size - 1| bytes or EOF, it
82 // outputs the bytes which were available.
83 OPENSSL_EXPORT int BIO_gets(BIO *bio, char *buf, int size);
84
85 // BIO_write call the |bio| |callback_ex| if set with |BIO_CB_WRITE|, writes
86 // |len| bytes from |data| to |bio|, then calls |callback_ex| with
87 // |BIO_CB_WRITE|+|BIO_CB_RETURN|. If |len| is less than or equal to zero, the
88 // function does nothing and return zero. If |callback_ex| is set BIO_write
89 // returns the value from calling the |callback_ex|, otherwise |BIO_write|
90 // returns the number of bytes written, or a negative number on error.
91 OPENSSL_EXPORT int BIO_write(BIO *bio, const void *data, int len);
92
93 // BIO_write_ex calls |BIO_write| and stores the number of bytes written in
94 // |written_bytes|, unless |written_bytes| is NULL. It returns one on success
95 // and zero otherwise.
96 OPENSSL_EXPORT int BIO_write_ex(BIO *bio, const void *data, size_t data_len,
97 size_t *written_bytes);
98
99 // BIO_write_all writes |len| bytes from |data| to |bio|, looping as necessary.
100 // It returns one if all bytes were successfully written and zero on error.
101 OPENSSL_EXPORT int BIO_write_all(BIO *bio, const void *data, size_t len);
102
103 // BIO_puts calls the |bio| |callback_ex| if set with |BIO_CB_PUTS|, attempts
104 // to write a NUL terminated string from |buf| to |bio|, then calls
105 // |callback_ex| with |BIO_CB_PUTS|+|BIO_CB_RETURN|. If |callback_ex| is set
106 // BIO_puts returns the value from calling the |callback_ex|, otherwise
107 // |BIO_puts| returns the number of bytes written, or a negative number on
108 // error. Unless the application defines a custom bputs method, this will
109 // delegate to using bwrite.
110 OPENSSL_EXPORT int BIO_puts(BIO *bio, const char *buf);
111
112 // BIO_flush flushes any buffered output. It returns one on success and zero
113 // otherwise.
114 OPENSSL_EXPORT int BIO_flush(BIO *bio);
115
116
117 // Low-level control functions.
118 //
119 // These are generic functions for sending control requests to a BIO. In
120 // general one should use the wrapper functions like |BIO_get_close|.
121
122 // BIO_ctrl call the |bio| |callback_ex| if set with |BIO_CB_CTRL|, sends the
123 // control request |cmd| to |bio|, then calls |callback_ex| with |BIO_CB_CTRL|
124 // + |BIO_CB_RETURN|. The |cmd| argument should be one of the |BIO_C_*| values.
125 OPENSSL_EXPORT long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) OPENSSL_WARN_UNUSED_RESULT;
126
127 // BIO_ptr_ctrl acts like |BIO_ctrl| but passes the address of a |void*|
128 // pointer as |parg| and returns the value that is written to it, or NULL if
129 // the control request returns <= 0.
130 OPENSSL_EXPORT char *BIO_ptr_ctrl(BIO *bp, int cmd, long larg);
131
132 // BIO_int_ctrl acts like |BIO_ctrl| but passes the address of a copy of |iarg|
133 // as |parg|.
134 OPENSSL_EXPORT long BIO_int_ctrl(BIO *bp, int cmd, long larg, int iarg);
135
136 // BIO_reset resets |bio| to its initial state, the precise meaning of which
137 // depends on the concrete type of |bio|. It returns one on success and zero
138 // otherwise.
139 OPENSSL_EXPORT int BIO_reset(BIO *bio) OPENSSL_WARN_UNUSED_RESULT;
140
141 // BIO_eof returns non-zero when |bio| has reached end-of-file. The precise
142 // meaning of which depends on the concrete type of |bio|. Note that in the
143 // case of BIO_pair this always returns non-zero.
144 OPENSSL_EXPORT int BIO_eof(BIO *bio);
145
146 // BIO_set_flags ORs |flags| with |bio->flags|.
147 OPENSSL_EXPORT void BIO_set_flags(BIO *bio, int flags);
148
149 // BIO_test_flags returns |bio->flags| AND |flags|.
150 OPENSSL_EXPORT int BIO_test_flags(const BIO *bio, int flags);
151
152 // BIO_should_read returns non-zero if |bio| encountered a temporary error
153 // while reading (i.e. EAGAIN), indicating that the caller should retry the
154 // read.
155 OPENSSL_EXPORT int BIO_should_read(const BIO *bio);
156
157 // BIO_should_write returns non-zero if |bio| encountered a temporary error
158 // while writing (i.e. EAGAIN), indicating that the caller should retry the
159 // write.
160 OPENSSL_EXPORT int BIO_should_write(const BIO *bio);
161
162 // BIO_should_retry returns non-zero if the reason that caused a failed I/O
163 // operation is temporary and thus the operation should be retried. Otherwise,
164 // it was a permanent error and it returns zero.
165 OPENSSL_EXPORT int BIO_should_retry(const BIO *bio);
166
167 // BIO_should_io_special returns non-zero if |bio| encountered a temporary
168 // error while performing a special I/O operation, indicating that the caller
169 // should retry. The operation that caused the error is returned by
170 // |BIO_get_retry_reason|.
171 OPENSSL_EXPORT int BIO_should_io_special(const BIO *bio);
172
173 // BIO_RR_CONNECT indicates that a connect would have blocked
174 #define BIO_RR_CONNECT 0x02
175
176 // BIO_RR_ACCEPT indicates that an accept would have blocked
177 #define BIO_RR_ACCEPT 0x03
178
179 // BIO_get_retry_reason returns the special I/O operation that needs to be
180 // retried. The return value is one of the |BIO_RR_*| values.
181 OPENSSL_EXPORT int BIO_get_retry_reason(const BIO *bio);
182
183 // BIO_set_retry_reason sets the special I/O operation that needs to be retried
184 // to |reason|, which should be one of the |BIO_RR_*| values.
185 OPENSSL_EXPORT void BIO_set_retry_reason(BIO *bio, int reason);
186
187 // BIO_clear_flags ANDs |bio->flags| with the bitwise-complement of |flags|.
188 OPENSSL_EXPORT void BIO_clear_flags(BIO *bio, int flags);
189
190 // BIO_set_retry_read sets the |BIO_FLAGS_READ| and |BIO_FLAGS_SHOULD_RETRY|
191 // flags on |bio|.
192 OPENSSL_EXPORT void BIO_set_retry_read(BIO *bio);
193
194 // BIO_set_retry_write sets the |BIO_FLAGS_WRITE| and |BIO_FLAGS_SHOULD_RETRY|
195 // flags on |bio|.
196 OPENSSL_EXPORT void BIO_set_retry_write(BIO *bio);
197
198 // BIO_get_retry_flags gets the |BIO_FLAGS_READ|, |BIO_FLAGS_WRITE|,
199 // |BIO_FLAGS_IO_SPECIAL| and |BIO_FLAGS_SHOULD_RETRY| flags from |bio|.
200 OPENSSL_EXPORT int BIO_get_retry_flags(BIO *bio);
201
202 // BIO_clear_retry_flags clears the |BIO_FLAGS_READ|, |BIO_FLAGS_WRITE|,
203 // |BIO_FLAGS_IO_SPECIAL| and |BIO_FLAGS_SHOULD_RETRY| flags from |bio|.
204 OPENSSL_EXPORT void BIO_clear_retry_flags(BIO *bio);
205
206 // BIO_method_type returns the type of |bio|, which is one of the |BIO_TYPE_*|
207 // values.
208 OPENSSL_EXPORT int BIO_method_type(const BIO *bio);
209
210 // BIO_method_name returns the name of |bio|.
211 OPENSSL_EXPORT const char *BIO_method_name(const BIO *b);
212
213 // These are passed to the BIO callback
214 #define BIO_CB_FREE 0x01
215 #define BIO_CB_READ 0x02
216 #define BIO_CB_WRITE 0x03
217 #define BIO_CB_PUTS 0x04
218 #define BIO_CB_GETS 0x05
219 #define BIO_CB_CTRL 0x06
220
221 // The callback is called before and after the underling operation,
222 // The BIO_CB_RETURN flag indicates if it is after the call
223 #define BIO_CB_RETURN 0x80
224
225 // |bio_info_cb| is a type of callback function providing information about a
226 // BIO operation. |state| identifies the current state of the BIO
227 // object, such as |BIO_CONN_S_BEFORE|. |res| represent the result of the
228 // operation that triggered the callback. This can be a positive value, zero,
229 // or a negative value depending on the operation and its outcome.
230 typedef long (*bio_info_cb)(BIO *b, int state, int res);
231
232 // |BIO_callback_fn_ex| parameters have the following meaning:
233 // |bio| the bio that made the call
234 // |oper| the operation being performed, may be or'd with |BIO_CB_RETURN|
235 // |argp|, |argi|, and |argl| depends on the value of oper
236 // |bio_ret| is the return value from the BIO method itself, if the callback
237 // is called before the operation the value 1 is used
238 // |len| the length of data requested to be read or written
239 // |processed| points to the location which will be updated with the amount
240 // of data actually read or written
241 typedef long (*BIO_callback_fn_ex)(BIO *bio, int oper, const char *argp,
242 size_t len, int argi, long argl, int bio_ret,
243 size_t *processed);
244
245 // |BIO_callback_fn_ex| parameters have the following meaning:
246 // |bio| the bio that made the call
247 // |oper| the operation being performed, may be or'd with |BIO_CB_RETURN|
248 // |argp| and |argl| depends on the value of oper
249 // |argi| is used to hold len value for |BIO_CB_READ|, |BIO_CB_WRITE|, and
250 // |BIO_CB_GETS|
251 // |bio_ret| is the return value from the BIO method itself,
252 // or value of processed where applicable
253 typedef long (*BIO_callback_fn)(BIO *bio, int oper, const char *argp,
254 int argi, long argl, long bio_ret);
255
256
257 // BIO_callback_ctrl allows the callback function to be manipulated. The |cmd|
258 // arg will generally be |BIO_CTRL_SET_CALLBACK| but arbitrary command values
259 // can be interpreted by the |BIO|.
260 OPENSSL_EXPORT long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp);
261
262 // BIO_pending returns the number of bytes pending to be read.
263 OPENSSL_EXPORT size_t BIO_pending(const BIO *bio);
264
265 // BIO_ctrl_pending calls |BIO_pending| and exists only for compatibility with
266 // OpenSSL.
267 OPENSSL_EXPORT size_t BIO_ctrl_pending(const BIO *bio);
268
269 // BIO_wpending returns the number of bytes pending to be written.
270 OPENSSL_EXPORT size_t BIO_wpending(const BIO *bio);
271
272 // BIO_set_close sets the close flag for |bio|. The meaning of which depends on
273 // the type of |bio| but, for example, a memory BIO interprets the close flag
274 // as meaning that it owns its buffer. It returns one on success and zero
275 // otherwise.
276 OPENSSL_EXPORT int BIO_set_close(BIO *bio, int close_flag);
277
278 // BIO_get_close returns the close flag for |bio|.
279 OPENSSL_EXPORT int BIO_get_close(BIO *bio);
280
281 // BIO_number_read returns the number of bytes that have been read from
282 // |bio|.
283 OPENSSL_EXPORT uint64_t BIO_number_read(const BIO *bio);
284
285 // BIO_number_written returns the number of bytes that have been written to
286 // |bio|.
287 OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio);
288
289 // BIO_set_callback_ex sets the |callback_ex| for |bio|.
290 OPENSSL_EXPORT void BIO_set_callback_ex(BIO *bio, BIO_callback_fn_ex callback_ex);
291
292 // BIO_set_callback sets the legacy |callback| for |bio|. When both |callback| and
293 // |callback_ex| are set, |callback_ex| will be used. Added for compatibility with
294 // existing applications.
295 OPENSSL_EXPORT OPENSSL_DEPRECATED void BIO_set_callback(BIO *bio, BIO_callback_fn callback);
296
297 // BIO_set_callback_arg sets the callback |arg| for |bio|.
298 OPENSSL_EXPORT void BIO_set_callback_arg(BIO *bio, char *arg);
299
300 // BIO_get_callback_arg returns the callback argument for BIO |bio|.
301 OPENSSL_EXPORT char *BIO_get_callback_arg(const BIO *bio);
302
303 // Managing chains of BIOs.
304 //
305 // BIOs can be put into chains where the output of one is used as the input of
306 // the next etc. The most common case is a buffering BIO, which accepts and
307 // buffers writes until flushed into the next BIO in the chain.
308
309 // BIO_push adds |appended_bio| to the end of the chain with |bio| at the head.
310 // It returns |bio|. Note that |appended_bio| may be the head of a chain itself
311 // and thus this function can be used to join two chains.
312 //
313 // BIO_push takes ownership of the caller's reference to |appended_bio|.
314 OPENSSL_EXPORT BIO *BIO_push(BIO *bio, BIO *appended_bio);
315
316 // BIO_pop removes |bio| from the head of a chain and returns the next BIO in
317 // the chain, or NULL if there is no next BIO.
318 //
319 // The caller takes ownership of the chain's reference to |bio|.
320 OPENSSL_EXPORT BIO *BIO_pop(BIO *bio);
321
322 // BIO_next returns the next BIO in the chain after |bio|, or NULL if there is
323 // no such BIO.
324 OPENSSL_EXPORT BIO *BIO_next(BIO *bio);
325
326 // BIO_free_all calls |BIO_free|.
327 //
328 // TODO(fork): update callers and remove.
329 OPENSSL_EXPORT void BIO_free_all(BIO *bio);
330
331 // BIO_find_type walks a chain of BIOs and returns the first that matches
332 // |type|, which is one of the |BIO_TYPE_*| values.
333 OPENSSL_EXPORT BIO *BIO_find_type(BIO *bio, int type);
334
335 // BIO_copy_next_retry sets the retry flags and |retry_reason| of |bio| from
336 // the next BIO in the chain.
337 OPENSSL_EXPORT void BIO_copy_next_retry(BIO *bio);
338
339
340 // Printf functions.
341
342 // BIO_printf behaves like |printf| but outputs to |bio| rather than a |FILE|.
343 // It returns the number of bytes written or a negative number on error.
344 OPENSSL_EXPORT int BIO_printf(BIO *bio, const char *format, ...)
345 OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
346
347
348 // Utility functions.
349
350 // BIO_indent prints min(|indent|, |max_indent|) spaces. It returns one on
351 // success and zero otherwise.
352 OPENSSL_EXPORT int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent);
353
354 // BIO_hexdump writes a hex dump of |data| to |bio|. Each line will be indented
355 // by |indent| spaces. It returns one on success and zero otherwise.
356 OPENSSL_EXPORT int BIO_hexdump(BIO *bio, const uint8_t *data, size_t len,
357 unsigned indent);
358
359 // BIO_dump writes a hex dump of |data| for |len| bytes to |bio|.
360 // It returns the exact number of bytes written to |bio| on success, or a negative value on error.
361 // If |len| is zero or |data| is NULL with |len| being zero, it returns 0.
362 OPENSSL_EXPORT int BIO_dump(BIO *bio, const void *data, int len);
363
364 // ERR_print_errors prints the current contents of the error stack to |bio|
365 // using human readable strings where possible.
366 OPENSSL_EXPORT void ERR_print_errors(BIO *bio);
367
368 // BIO_read_asn1 reads a single ASN.1 object from |bio|. If successful it sets
369 // |*out| to be an allocated buffer (that should be freed with |OPENSSL_free|),
370 // |*out_size| to the length, in bytes, of that buffer and returns one.
371 // Otherwise it returns zero.
372 //
373 // If the length of the object is greater than |max_len| or 2^32 then the
374 // function will fail. Long-form tags are not supported. If the length of the
375 // object is indefinite the full contents of |bio| are read, unless it would be
376 // greater than |max_len|, in which case the function fails.
377 //
378 // If the function fails then some unknown amount of data may have been read
379 // from |bio|.
380 OPENSSL_EXPORT int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len,
381 size_t max_len);
382
383
384 // Memory BIOs.
385 //
386 // Memory BIOs can be used as a read-only source (with |BIO_new_mem_buf|) or a
387 // writable sink (with |BIO_new|, |BIO_s_mem| and |BIO_mem_contents|). Data
388 // written to a writable, memory BIO can be recalled by reading from it.
389 //
390 // Calling |BIO_reset| on a read-only BIO resets it to the original contents.
391 // On a writable BIO, it clears any data.
392 //
393 // If the close flag is set to |BIO_NOCLOSE| (not the default) then the
394 // underlying |BUF_MEM| will not be freed when the |BIO| is freed.
395 //
396 // Memory BIOs support |BIO_gets| and |BIO_puts|.
397 //
398 // |BIO_ctrl_pending| returns the number of bytes currently stored.
399
400 // BIO_s_mem returns a |BIO_METHOD| that uses a in-memory buffer.
401 OPENSSL_EXPORT const BIO_METHOD *BIO_s_mem(void);
402
403 // BIO_new_mem_buf creates read-only BIO that reads from |len| bytes at |buf|.
404 // It returns the BIO or NULL on error. This function does not copy or take
405 // ownership of |buf|. The caller must ensure the memory pointed to by |buf|
406 // outlives the |BIO|.
407 //
408 // If |len| is negative, then |buf| is treated as a NUL-terminated string, but
409 // don't depend on this in new code.
410 OPENSSL_EXPORT BIO *BIO_new_mem_buf(const void *buf, ossl_ssize_t len);
411
412 // BIO_mem_contents sets |*out_contents|, if not null, to point to the current
413 // contents of|bio| and |*out_len|, if not null, to contain the length of
414 // that data.
415 //
416 // It returns one on success and zero otherwise.
417 OPENSSL_EXPORT int BIO_mem_contents(const BIO *bio,
418 const uint8_t **out_contents,
419 size_t *out_len);
420
421 // BIO_get_mem_data sets |*contents| to point to the current contents of |bio|
422 // and returns the length of the data. Despite being a macro, this function
423 // should always take |char *| as a value and nothing else.
424 //
425 // WARNING: don't use this, use |BIO_mem_contents|. A negative return value
426 // or zero from this function can mean either that it failed or that the
427 // memory buffer is empty.
428 #define BIO_get_mem_data(bio, contents) BIO_ctrl(bio, BIO_CTRL_INFO, 0, \
429 (char *)(contents))
430 // BIO_get_mem_ptr sets |*out| to a BUF_MEM containing the current contents of
431 // |bio|. It returns one on success or zero on error.
432 OPENSSL_EXPORT int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out);
433
434 // BIO_set_mem_buf sets |b| as the contents of |bio|. If |take_ownership| is
435 // non-zero, then |b| will be freed when |bio| is closed. Returns one on
436 // success or zero otherwise.
437 OPENSSL_EXPORT int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership);
438
439 // BIO_set_mem_eof_return sets the value that will be returned from reading
440 // |bio| when empty. If |eof_value| is zero then an empty memory BIO will
441 // return EOF (that is it will return zero and |BIO_should_retry| will be
442 // false). If |eof_value| is non zero then it will return |eof_value| when it
443 // is empty and it will set the read retry flag (that is |BIO_read_retry| is
444 // true). To avoid ambiguity with a normal positive return value, |eof_value|
445 // should be set to a negative value, typically -1.
446 //
447 // For a read-only BIO, the default is zero (EOF). For a writable BIO, the
448 // default is -1 so that additional data can be written once exhausted.
449 OPENSSL_EXPORT int BIO_set_mem_eof_return(BIO *bio, int eof_value);
450
451
452 // BIO close flags.
453 //
454 // These can be used as symbolic arguments when a "close flag" is passed to a
455 // BIO function.
456
457 // BIO_NOCLOSE will not close the underlying file on BIO free
458 #define BIO_NOCLOSE 0
459
460 // BIO_CLOSE will close the underlying file on BIO free
461 #define BIO_CLOSE 1
462
463 // BIO_FP_TEXT will cause the file to be treated as a text file instead of the
464 // default behavior of treating it as a raw binary file. This is only relevant
465 // on Windows due to CRLF endings.
466 #define BIO_FP_TEXT 0x10
467
468
469 // File descriptor BIOs.
470 //
471 // File descriptor BIOs are wrappers around the system's |read| and |write|
472 // functions. If the close flag is set then then |close| is called on the
473 // underlying file descriptor when the BIO is freed.
474 //
475 // |BIO_reset| attempts to seek the file pointer to the start of file using
476 // |lseek|.
477
478 #if !defined(OPENSSL_NO_POSIX_IO)
479 // BIO_s_fd returns a |BIO_METHOD| for file descriptor fds.
480 OPENSSL_EXPORT const BIO_METHOD *BIO_s_fd(void);
481
482 // BIO_new_fd creates a new file descriptor BIO wrapping |fd|. If |close_flag|
483 // is non-zero, then |fd| will be closed when the BIO is.
484 OPENSSL_EXPORT BIO *BIO_new_fd(int fd, int close_flag);
485 #endif
486
487 // BIO_set_fd sets the file descriptor of |bio| to |fd|. If |close_flag| is
488 // non-zero then |fd| will be closed when |bio| is. It returns one on success
489 // or zero on error.
490 //
491 // This function may also be used with socket BIOs (see |BIO_s_socket| and
492 // |BIO_new_socket|).
493 OPENSSL_EXPORT int BIO_set_fd(BIO *bio, int fd, int close_flag);
494
495 // BIO_get_fd returns the file descriptor currently in use by |bio| or -1 if
496 // |bio| does not wrap a file descriptor. If there is a file descriptor and
497 // |out_fd| is not NULL, it also sets |*out_fd| to the file descriptor.
498 //
499 // This function may also be used with socket BIOs (see |BIO_s_socket| and
500 // |BIO_new_socket|).
501 OPENSSL_EXPORT int BIO_get_fd(BIO *bio, int *out_fd);
502
503
504 // File BIOs.
505 //
506 // File BIOs are wrappers around a C |FILE| object.
507 //
508 // |BIO_flush| on a file BIO calls |fflush| on the wrapped stream.
509 //
510 // |BIO_reset| attempts to seek the file pointer to the start of file using
511 // |fseek|.
512 //
513 // Setting the close flag causes |fclose| to be called on the stream when the
514 // BIO is freed.
515
516 // BIO_s_file returns a BIO_METHOD that wraps a |FILE|.
517 OPENSSL_EXPORT const BIO_METHOD *BIO_s_file(void);
518
519 // BIO_new_file creates a file BIO by opening |filename| with the given mode.
520 // See the |fopen| manual page for details of the mode argument.
521 OPENSSL_EXPORT BIO *BIO_new_file(const char *filename, const char *mode);
522
523 // BIO_new_fp creates a new file BIO that wraps the given |FILE|. If
524 // |close_flag| is |BIO_CLOSE|, then |fclose| will be called on |stream| when
525 // the BIO is closed. If |close_flag| is |BIO_FP_TEXT|, the file will be set as
526 // a text file after opening (only on Windows).
527 OPENSSL_EXPORT BIO *BIO_new_fp(FILE *stream, int close_flag);
528
529 // BIO_get_fp sets |*out_file| to the current |FILE| for |bio|. It returns one
530 // on success and zero otherwise.
531 OPENSSL_EXPORT int BIO_get_fp(BIO *bio, FILE **out_file);
532
533 // BIO_set_fp sets the |FILE| for |bio|. If |close_flag| is |BIO_CLOSE| then
534 // |fclose| will be called on |file| when |bio| is closed. If |close_flag| is
535 // |BIO_FP_TEXT|, the file will be set as a text file (only on Windows). It
536 // returns one on success and zero otherwise.
537 OPENSSL_EXPORT int BIO_set_fp(BIO *bio, FILE *file, int close_flag);
538
539 // BIO_read_filename opens |filename| for reading and sets the result as the
540 // |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE|
541 // will be closed when |bio| is freed. On Windows, the file is opened in binary
542 // mode.
543 OPENSSL_EXPORT int BIO_read_filename(BIO *bio, const char *filename);
544
545 // BIO_write_filename opens |filename| for writing and sets the result as the
546 // |FILE| for |bio|. It returns one on success and zero otherwise. The |FILE|
547 // will be closed when |bio| is freed. On Windows, the file is opened in binary
548 // mode.
549 OPENSSL_EXPORT int BIO_write_filename(BIO *bio, const char *filename);
550
551 // BIO_append_filename opens |filename| for appending and sets the result as
552 // the |FILE| for |bio|. It returns one on success and zero otherwise. The
553 // |FILE| will be closed when |bio| is freed. On Windows, the file is opened in
554 // binary mode.
555 OPENSSL_EXPORT int BIO_append_filename(BIO *bio, const char *filename);
556
557 // BIO_rw_filename opens |filename| for reading and writing and sets the result
558 // as the |FILE| for |bio|. It returns one on success and zero otherwise. The
559 // |FILE| will be closed when |bio| is freed. On Windows, the file is opened in
560 // binary mode.
561 OPENSSL_EXPORT int BIO_rw_filename(BIO *bio, const char *filename);
562
563 // BIO_tell returns the file offset of |bio|, or a negative number on error or
564 // if |bio| does not support the operation.
565 //
566 // TODO(https://crbug.com/boringssl/465): On platforms where |long| is 32-bit,
567 // this function cannot report 64-bit offsets.
568 OPENSSL_EXPORT long BIO_tell(BIO *bio);
569
570 // BIO_seek sets the file offset of |bio| to |offset|. It returns a non-negative
571 // number on success and a negative number on error. If |bio| is a file
572 // descriptor |BIO|, it returns the resulting file offset on success. If |bio|
573 // is a file |BIO|, it returns zero on success.
574 //
575 // WARNING: This function's return value conventions differs from most functions
576 // in this library.
577 //
578 // TODO(https://crbug.com/boringssl/465): On platforms where |long| is 32-bit,
579 // this function cannot handle 64-bit offsets.
580 OPENSSL_EXPORT long BIO_seek(BIO *bio, long offset);
581
582
583 // Socket BIOs.
584 //
585 // Socket BIOs behave like file descriptor BIOs but, on Windows systems, wrap
586 // the system's |recv| and |send| functions instead of |read| and |write|. On
587 // Windows, file descriptors are provided by C runtime and are not
588 // interchangeable with sockets.
589 //
590 // Socket BIOs may be used with |BIO_set_fd| and |BIO_get_fd|.
591 //
592 // TODO(davidben): Add separate APIs and fix the internals to use |SOCKET|s
593 // around rather than rely on int casts.
594
595 #if !defined(OPENSSL_NO_SOCK)
596 OPENSSL_EXPORT const BIO_METHOD *BIO_s_socket(void);
597
598 // BIO_new_socket allocates and initialises a fresh BIO which will read and
599 // write to the socket |fd|. If |close_flag| is |BIO_CLOSE| then closing the
600 // BIO will close |fd|. It returns the fresh |BIO| or NULL on error.
601 OPENSSL_EXPORT BIO *BIO_new_socket(int fd, int close_flag);
602 #endif // !OPENSSL_NO_SOCK
603
604
605 // Connect BIOs.
606 //
607 // A connection BIO creates a network connection and transfers data over the
608 // resulting socket.
609
610 #if !defined(OPENSSL_NO_SOCK)
611 OPENSSL_EXPORT const BIO_METHOD *BIO_s_connect(void);
612
613 // BIO_new_connect returns a BIO that connects to the given hostname and port.
614 // The |host_and_optional_port| argument should be of the form
615 // "www.example.com" or "www.example.com:443". If the port is omitted, it must
616 // be provided with |BIO_set_conn_port|.
617 //
618 // It returns the new BIO on success, or NULL on error.
619 OPENSSL_EXPORT BIO *BIO_new_connect(const char *host_and_optional_port);
620
621 // BIO_set_conn_hostname sets |host_and_optional_port| as the hostname and
622 // optional port that |bio| will connect to. If the port is omitted, it must be
623 // provided with |BIO_set_conn_port|.
624 //
625 // It returns one on success and zero otherwise.
626 OPENSSL_EXPORT int BIO_set_conn_hostname(BIO *bio,
627 const char *host_and_optional_port);
628
629 // BIO_set_conn_port sets |port_str| as the port or service name that |bio|
630 // will connect to. It returns one on success and zero otherwise.
631 OPENSSL_EXPORT int BIO_set_conn_port(BIO *bio, const char *port_str);
632
633 // BIO_set_conn_int_port sets |*port| as the port that |bio| will connect to.
634 // It returns one on success and zero otherwise.
635 OPENSSL_EXPORT int BIO_set_conn_int_port(BIO *bio, const int *port);
636
637 // BIO_set_nbio sets whether |bio| will use non-blocking I/O operations. It
638 // returns one on success and zero otherwise. This only works for connect BIOs
639 // and must be called before |bio| is connected to take effect.
640 //
641 // For socket and fd BIOs, callers must configure blocking vs. non-blocking I/O
642 // using the underlying platform APIs.
643 OPENSSL_EXPORT int BIO_set_nbio(BIO *bio, int on);
644
645 // BIO_do_connect connects |bio| if it has not been connected yet. It returns
646 // one on success and <= 0 otherwise.
647 OPENSSL_EXPORT int BIO_do_connect(BIO *bio);
648 #endif // !OPENSSL_NO_SOCK
649
650
651 // Message digest BIOs
652
653 // BIO_f_md provides a filter |BIO| that digests any data passed through it.
654 // The BIO must be initialized with |BIO_set_md| or |BIO_get_md_ctx| before it
655 // can be used.
656 OPENSSL_EXPORT const BIO_METHOD *BIO_f_md(void);
657
658 // BIO_get_md_ctx writes a reference of |b|'s EVP_MD_CTX* to |*ctx|. It returns
659 // one on success and zero on error.
660 OPENSSL_EXPORT int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **ctx);
661
662 // BIO_set_md set's |b|'s EVP_MD* to |md|. It returns one on success and zero on
663 // error.
664 OPENSSL_EXPORT int BIO_set_md(BIO *b, const EVP_MD *md);
665
666 // BIO_get_md set's |*md| to |b|'s |EVP_MD*|. It returns one on success and zero
667 // on error.
668 OPENSSL_EXPORT int BIO_get_md(BIO *b, EVP_MD **md);
669
670
671 // Datagram BIOs.
672
673 #define BIO_CTRL_DGRAM_CONNECT 31 // BIO dgram special
674 #define BIO_CTRL_DGRAM_SET_CONNECTED 32 /* allow for an externally connected
675 * socket to be passed in */
676 #define BIO_CTRL_DGRAM_SET_RECV_TIMEOUT 33 // setsockopt, essentially
677 #define BIO_CTRL_DGRAM_GET_RECV_TIMEOUT 34 // getsockopt, essentially
678 #define BIO_CTRL_DGRAM_SET_SEND_TIMEOUT 35 // setsockopt, essentially
679 #define BIO_CTRL_DGRAM_GET_SEND_TIMEOUT 36 // getsockopt, essentially
680
681 # define BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP 37 // flag whether the last
682 # define BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP 38 // I/O operation tiemd out
683
684 #define BIO_CTRL_DGRAM_QUERY_MTU 40 // as kernel for current MTU
685 #define BIO_CTRL_DGRAM_GET_MTU 41 // get cached value for MTU
686 #define BIO_CTRL_DGRAM_SET_MTU 42 /* set cached value for MTU. want to use
687 * this if asking the kernel fails */
688
689 #define BIO_CTRL_DGRAM_MTU_EXCEEDED 43 /* check whether the MTU was exceeded in
690 the previous write operation. */
691
692 #define BIO_CTRL_DGRAM_SET_PEER 44 // Destination for the data
693
694 // BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT is unsupported as it is unused by consumers
695 // and depends on |timeval|, which is not 2038-clean on all platforms.
696
697 #define BIO_CTRL_DGRAM_GET_PEER 46
698
699 #define BIO_CTRL_DGRAM_GET_FALLBACK_MTU 47
700
701
702 #if !defined(OPENSSL_NO_SOCK)
703
704 typedef union bio_addr_st BIO_ADDR;
705
706 // BIO_s_datagram returns the datagram |BIO_METHOD|. A datagram BIO provides a wrapper
707 // around datagram sockets.
708 OPENSSL_EXPORT const BIO_METHOD *BIO_s_datagram(void) OPENSSL_WARN_UNUSED_RESULT;
709
710 // BIO_new_dgram creates a new datagram BIO wrapping |fd|. If |close_flag| is
711 // non-zero, then |fd| will be closed when the BIO is freed.
712 OPENSSL_EXPORT BIO *BIO_new_dgram(int fd, int close_flag) OPENSSL_WARN_UNUSED_RESULT;
713
714 // BIO_ctrl_dgram_connect attempts to connect the datagram BIO to the specified
715 // |peer| address. It returns 1 on success and a non-positive value on error.
716 OPENSSL_EXPORT int BIO_ctrl_dgram_connect(BIO *bp, const BIO_ADDR *peer) OPENSSL_WARN_UNUSED_RESULT;
717
718 // BIO_ctrl_set_connected marks the datagram BIO as connected to the specified
719 // |peer| address. This is used for handling DTLS connection-oriented BIOs.
720 // It returns 1 on success and a non-positive value on error.
721 OPENSSL_EXPORT int BIO_ctrl_set_connected(BIO* bp, const BIO_ADDR *peer) OPENSSL_WARN_UNUSED_RESULT;
722
723 // BIO_dgram_recv_timedout returns 1 if the most recent datagram receive
724 // operation on |bp| timed out, and a non-positive value otherwise. Any error
725 // for this socket gets reset by this call.
726 OPENSSL_EXPORT int BIO_dgram_recv_timedout(BIO* bp) OPENSSL_WARN_UNUSED_RESULT;
727
728 // BIO_dgram_send_timedout returns 1 if the most recent datagram send
729 // operation on |bp| timed out, and a non-positive value otherwise. Any error
730 // for this socket gets reset by this call.
731 OPENSSL_EXPORT int BIO_dgram_send_timedout(BIO *bp) OPENSSL_WARN_UNUSED_RESULT;
732
733 // BIO_dgram_get_peer stores the address of the peer the datagram BIO is
734 // connected to in |peer|. It returns 1 on success and a non-positive value on error.
735 OPENSSL_EXPORT int BIO_dgram_get_peer(BIO* bp, BIO_ADDR *peer) OPENSSL_WARN_UNUSED_RESULT;
736
737 // BIO_dgram_set_peer sets the peer address for the datagram BIO to |peer|.
738 // It returns 1 on success and a non-positive value on error.
739 OPENSSL_EXPORT int BIO_dgram_set_peer(BIO* bp, const BIO_ADDR *peer) OPENSSL_WARN_UNUSED_RESULT;
740
741 // BIO_ADDR_new allocates and initializes a new BIO_ADDR structure.
742 // Returns the new BIO_ADDR structure on success, NULL on error.
743 OPENSSL_EXPORT BIO_ADDR *BIO_ADDR_new(void);
744
745 // BIO_ADDR_copy copies the contents of the BIO_ADDR structure from src to dst.
746 // Returns 1 on success, 0 on error.
747 OPENSSL_EXPORT int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src);
748
749 // BIO_ADDR_dup creates a copy of the BIO_ADDR.
750 // Returns the new BIO_ADDR structure on success, NULL on error.
751 OPENSSL_EXPORT BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap);
752
753 // BIO_ADDR_free releases all resources associated with the BIO_ADDR structure.
754 // If 'ap' is NULL, no action occurs.
755 OPENSSL_EXPORT void BIO_ADDR_free(BIO_ADDR *ap);
756
757 // BIO_ADDR_clear zeros the contents of the BIO_ADDR structure but does not free it.
758 // If 'ap' is NULL, no action occurs.
759 OPENSSL_EXPORT void BIO_ADDR_clear(BIO_ADDR *ap);
760
761 // BIO_ADDR_rawmake sets up a BIO_ADDR with the provided values. |family| is
762 // the Address family (e.g., AF_INET, AF_INET6). |where| is a pointer to the
763 // address data. |wherelen| is the length of the address data. For AF_UNIX,
764 // |wherelen| is expected to be the length of the path string not including the
765 // terminating NUL. |port| is the port number in host byte order. Returns 1 on
766 // success, 0 on error.
767 OPENSSL_EXPORT int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, const void *where,
768 size_t wherelen, unsigned short port);
769
770 // BIO_ADDR_family returns the address family of the BIO_ADDR.
771 // Returns the address family (e.g., AF_INET, AF_INET6) on success,
772 // -1 on error.
773 OPENSSL_EXPORT int BIO_ADDR_family(const BIO_ADDR *ap);
774
775 // BIO_ADDR_rawaddress retrieves the raw address data from a BIO_ADDR structure.
776 // The address data from |ap| is copied into the buffer |p| if |p| is not NULL.
777 // If |l| is not NULL, |*l| will be updated with the size of the address data.
778 // For AF_INET, this is the 4-byte IPv4 address; for AF_INET6, the 16-byte IPv6
779 // address; for AF_UNIX, the socket path. With AF_UNIX addresses, the buffer |p|
780 // must be large enough for both the path and a NUL terminator. The function will
781 // write the terminator to the buffer, but the length stored in |*l| excludes it.
782 // Returns 1 on success, 0 if the address family is unsupported or |ap| is NULL.
783 OPENSSL_EXPORT int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l);
784
785 // BIO_ADDR_rawport returns the port number stored in the BIO_ADDR.
786 // Returns the port number in host byte order.
787 OPENSSL_EXPORT unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap);
788
789 #endif // !defined(OPENSSL_NO_SOCK)
790
791 // BIO Pairs.
792 //
793 // BIO pairs provide a "loopback" like system: a pair of BIOs where data
794 // written to one can be read from the other and vice versa.
795
796 // BIO_new_bio_pair sets |*out1| and |*out2| to two freshly created BIOs where
797 // data written to one can be read from the other and vice versa. The
798 // |writebuf1| argument gives the size of the buffer used in |*out1| and
799 // |writebuf2| for |*out2|. It returns one on success and zero on error.
800 OPENSSL_EXPORT int BIO_new_bio_pair(BIO **out1, size_t writebuf1, BIO **out2,
801 size_t writebuf2);
802
803 // BIO_destroy_bio_pair destroys the connection between |b| and |b->ptr->peer|.
804 // It disconnects both BIOs, resets their state, but does not free their memory.
805 // It always returns one to indicate success.
806 OPENSSL_EXPORT int BIO_destroy_bio_pair(BIO *b);
807
808 // BIO_ctrl_get_read_request returns the number of bytes that the other side of
809 // |bio| tried (unsuccessfully) to read.
810 OPENSSL_EXPORT size_t BIO_ctrl_get_read_request(BIO *bio);
811
812 // BIO_ctrl_get_write_guarantee returns the number of bytes that |bio| (which
813 // must have been returned by |BIO_new_bio_pair|) will accept on the next
814 // |BIO_write| call.
815 OPENSSL_EXPORT size_t BIO_ctrl_get_write_guarantee(BIO *bio);
816
817 // BIO_shutdown_wr marks |bio| as closed, from the point of view of the other
818 // side of the pair. Future |BIO_write| calls on |bio| will fail. It returns
819 // one on success and zero otherwise.
820 OPENSSL_EXPORT int BIO_shutdown_wr(BIO *bio);
821
822
823 // Custom BIOs.
824 //
825 // Consumers can create custom |BIO|s by filling in a |BIO_METHOD| and using
826 // low-level control functions to set state.
827
828 // BIO_get_new_index returns a new "type" value for a custom |BIO|.
829 OPENSSL_EXPORT int BIO_get_new_index(void);
830
831 // BIO_meth_new returns a newly-allocated |BIO_METHOD| or NULL on allocation
832 // error. The |type| specifies the type that will be returned by
833 // |BIO_method_type|. If this is unnecessary, this value may be zero. The |name|
834 // parameter is vestigial and may be NULL.
835 //
836 // Use the |BIO_meth_set_*| functions below to initialize the |BIO_METHOD|. The
837 // function implementations may use |BIO_set_data| and |BIO_get_data| to add
838 // method-specific state to associated |BIO|s. Additionally, |BIO_set_init| must
839 // be called after an associated |BIO| is fully initialized. State set via
840 // |BIO_set_data| may be released by configuring a destructor with
841 // |BIO_meth_set_destroy|.
842 OPENSSL_EXPORT BIO_METHOD *BIO_meth_new(int type, const char *name);
843
844 // BIO_meth_free releases memory associated with |method|.
845 OPENSSL_EXPORT void BIO_meth_free(BIO_METHOD *method);
846
847 // BIO_meth_set_create sets a function to be called on |BIO_new| for |method|
848 // and returns one. The function should return one on success and zero on
849 // error.
850 OPENSSL_EXPORT int BIO_meth_set_create(BIO_METHOD *method,
851 int (*create)(BIO *));
852
853 // BIO_meth_get_create returns |create| function of |method|.
854 OPENSSL_EXPORT int (*BIO_meth_get_create(const BIO_METHOD *method)) (BIO *);
855
856 // BIO_meth_set_destroy sets a function to release data associated with a |BIO|
857 // and returns one. The function's return value is ignored.
858 OPENSSL_EXPORT int BIO_meth_set_destroy(BIO_METHOD *method,
859 int (*destroy)(BIO *));
860
861 // BIO_meth_get_destroy returns |destroy| function of |method|.
862 OPENSSL_EXPORT int (*BIO_meth_get_destroy(const BIO_METHOD *method)) (BIO *);
863
864 // BIO_meth_set_write sets the implementation of |BIO_write| for |method| and
865 // returns one. |BIO_METHOD|s which implement |BIO_write| should also implement
866 // |BIO_CTRL_FLUSH|. (See |BIO_meth_set_ctrl|.)
867 OPENSSL_EXPORT int BIO_meth_set_write(BIO_METHOD *method,
868 int (*write)(BIO *, const char *, int));
869
870 // BIO_meth_set_read sets the implementation of |BIO_read| for |method| and
871 // returns one.
872 OPENSSL_EXPORT int BIO_meth_set_read(BIO_METHOD *method,
873 int (*read)(BIO *, char *, int));
874
875 // BIO_meth_set_gets sets the implementation of |BIO_gets| for |method| and
876 // returns one.
877 OPENSSL_EXPORT int BIO_meth_set_gets(BIO_METHOD *method,
878 int (*gets)(BIO *, char *, int));
879
880 // BIO_meth_get_gets returns |gets| function of |method|.
881 OPENSSL_EXPORT int (*BIO_meth_get_gets(const BIO_METHOD *method)) (BIO *, char *, int);
882
883 // BIO_meth_set_ctrl sets the implementation of |BIO_ctrl| for |method| and
884 // returns one.
885 OPENSSL_EXPORT int BIO_meth_set_ctrl(BIO_METHOD *method,
886 long (*ctrl)(BIO *, int, long, void *));
887
888 // BIO_meth_get_ctrl returns |ctrl| function of |method|.
889 OPENSSL_EXPORT long (*BIO_meth_get_ctrl(const BIO_METHOD *method)) (BIO *, int, long, void *);
890
891 // BIO_meth_set_callback_ctrl sets the implementation of |callback_ctrl| for
892 // |method| and returns one.
893 OPENSSL_EXPORT int BIO_meth_set_callback_ctrl(BIO_METHOD *method,
894 long (*callback_ctrl)(BIO *, int, bio_info_cb));
895
896 // BIO_meth_get_callback_ctrl returns |callback_ctrl| function of |method|.
897 OPENSSL_EXPORT long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *method)) (BIO *, int, bio_info_cb);
898
899 // BIO_set_data sets custom data on |bio|. It may be retried with
900 // |BIO_get_data|.
901 //
902 // This function should only be called by the implementation of a custom |BIO|.
903 // In particular, the data pointer of a built-in |BIO| is private to the
904 // library. For other uses, see |BIO_set_ex_data| and |BIO_set_app_data|.
905 OPENSSL_EXPORT void BIO_set_data(BIO *bio, void *ptr);
906
907 // BIO_get_data returns custom data on |bio| set by |BIO_get_data|.
908 //
909 // This function should only be called by the implementation of a custom |BIO|.
910 // In particular, the data pointer of a built-in |BIO| is private to the
911 // library. For other uses, see |BIO_get_ex_data| and |BIO_get_app_data|.
912 OPENSSL_EXPORT void *BIO_get_data(BIO *bio);
913
914 // BIO_set_init sets whether |bio| has been fully initialized. Until fully
915 // initialized, |BIO_read| and |BIO_write| will fail.
916 OPENSSL_EXPORT void BIO_set_init(BIO *bio, int init);
917
918 // BIO_get_init returns whether |bio| has been fully initialized.
919 OPENSSL_EXPORT int BIO_get_init(BIO *bio);
920
921 // These are values of the |cmd| argument to |BIO_ctrl|.
922
923 // BIO_CTRL_RESET implements |BIO_reset|. The arguments are unused.
924 #define BIO_CTRL_RESET 1
925
926 // BIO_CTRL_EOF implements |BIO_eof|. The arguments are unused.
927 #define BIO_CTRL_EOF 2
928
929 // BIO_CTRL_INFO is a legacy command that returns information specific to the
930 // type of |BIO|. It is not safe to call generically and should not be
931 // implemented in new |BIO| types.
932 #define BIO_CTRL_INFO 3
933
934 // BIO_CTRL_GET_CLOSE returns the close flag set by |BIO_CTRL_SET_CLOSE|. The
935 // arguments are unused.
936 #define BIO_CTRL_GET_CLOSE 8
937
938 // BIO_CTRL_SET_CLOSE implements |BIO_set_close|. The |larg| argument is the
939 // close flag.
940 #define BIO_CTRL_SET_CLOSE 9
941
942 // BIO_CTRL_PENDING implements |BIO_pending|. The arguments are unused.
943 #define BIO_CTRL_PENDING 10
944
945 // BIO_CTRL_FLUSH implements |BIO_flush|. The arguments are unused.
946 #define BIO_CTRL_FLUSH 11
947
948 // BIO_CTRL_WPENDING implements |BIO_wpending|. The arguments are unused.
949 #define BIO_CTRL_WPENDING 13
950
951 // BIO_CTRL_SET_CALLBACK sets an informational callback of type
952 // int cb(BIO *bio, int state, int ret)
953 #define BIO_CTRL_SET_CALLBACK 14
954
955 // BIO_CTRL_GET_CALLBACK returns the callback set by |BIO_CTRL_SET_CALLBACK|.
956 #define BIO_CTRL_GET_CALLBACK 15
957
958 // The following are never used, but are defined to aid porting existing code.
959 #define BIO_CTRL_SET 4
960 #define BIO_CTRL_GET 5
961 #define BIO_CTRL_PUSH 6
962 #define BIO_CTRL_POP 7
963 #define BIO_CTRL_DUP 12
964 #define BIO_CTRL_SET_FILENAME 30
965
966
967 // ex_data functions.
968 //
969 // See |ex_data.h| for details.
970
971 OPENSSL_EXPORT int BIO_get_ex_new_index(long argl, void *argp,
972 CRYPTO_EX_unused *unused,
973 CRYPTO_EX_dup *dup_unused,
974 CRYPTO_EX_free *free_func);
975 OPENSSL_EXPORT int BIO_set_ex_data(BIO *bio, int idx, void *arg);
976 OPENSSL_EXPORT void *BIO_get_ex_data(const BIO *bio, int idx);
977
978 #define BIO_set_app_data(bio, arg) (BIO_set_ex_data(bio, 0, (char *)(arg)))
979 #define BIO_get_app_data(bio) (BIO_get_ex_data(bio, 0))
980
981
982 // Deprecated functions.
983
984 // BIO_f_base64 returns a filter |BIO| that base64-encodes data written into
985 // it, and decodes data read from it. |BIO_gets| is not supported. Call
986 // |BIO_flush| when done writing, to signal that no more data are to be
987 // encoded. The flag |BIO_FLAGS_BASE64_NO_NL| may be set to encode all the data
988 // on one line.
989 //
990 // Use |EVP_EncodeBlock| and |EVP_DecodeBase64| instead.
991 OPENSSL_EXPORT const BIO_METHOD *BIO_f_base64(void);
992
993 OPENSSL_EXPORT void BIO_set_retry_special(BIO *bio);
994
995 // BIO_set_shutdown sets a method-specific "shutdown" bit on |bio|.
996 OPENSSL_EXPORT void BIO_set_shutdown(BIO *bio, int shutdown);
997
998 // BIO_get_shutdown returns the method-specific "shutdown" bit.
999 OPENSSL_EXPORT int BIO_get_shutdown(BIO *bio);
1000
1001 // BIO_meth_set_puts sets the implementation of |BIO_puts| for |method| and
1002 // returns 1.
1003 OPENSSL_EXPORT int BIO_meth_set_puts(BIO_METHOD *method,
1004 int (*puts)(BIO *, const char *));
1005
1006 // BIO_meth_get_puts returns |puts| function of |method|.
1007 OPENSSL_EXPORT int (*BIO_meth_get_puts(const BIO_METHOD *method)) (BIO *, const char *);
1008
1009 // BIO_s_secmem returns the normal BIO_METHOD |BIO_s_mem|. Deprecated since AWS-LC
1010 // does not support secure heaps.
1011 OPENSSL_EXPORT OPENSSL_DEPRECATED const BIO_METHOD *BIO_s_secmem(void);
1012
1013
1014 // General No-op Functions [Deprecated].
1015
1016 // BIO_set_write_buffer_size returns zero.
1017 //
1018 // TODO (CryptoAlg-2398): Add |OPENSSL_DEPRECATED|. nginx defines -Werror and
1019 // depends on this.
1020 OPENSSL_EXPORT int BIO_set_write_buffer_size(BIO *bio, int buffer_size);
1021
1022
1023 // Private functions
1024
1025 #define BIO_FLAGS_READ 0x01
1026 #define BIO_FLAGS_WRITE 0x02
1027 #define BIO_FLAGS_IO_SPECIAL 0x04
1028 #define BIO_FLAGS_RWS (BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL)
1029 #define BIO_FLAGS_SHOULD_RETRY 0x08
1030 #define BIO_FLAGS_BASE64_NO_NL 0x100
1031 // BIO_FLAGS_MEM_RDONLY is used with memory BIOs. It means we shouldn't free up
1032 // or change the data in any way.
1033 #define BIO_FLAGS_MEM_RDONLY 0x200
1034
1035 // BIO_TYPE_DESCRIPTOR denotes that the |BIO| responds to the |BIO_C_SET_FD|
1036 // (|BIO_set_fd|) and |BIO_C_GET_FD| (|BIO_get_fd|) control hooks.
1037 #define BIO_TYPE_DESCRIPTOR 0x0100 // socket, fd, connect or accept
1038 #define BIO_TYPE_FILTER 0x0200
1039 #define BIO_TYPE_SOURCE_SINK 0x0400
1040
1041 // These are the 'types' of BIOs
1042 #define BIO_TYPE_NONE 0
1043 #define BIO_TYPE_MEM (1 | BIO_TYPE_SOURCE_SINK)
1044 #define BIO_TYPE_FILE (2 | BIO_TYPE_SOURCE_SINK)
1045 #define BIO_TYPE_FD (4 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR)
1046 #define BIO_TYPE_SOCKET (5 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR)
1047 #define BIO_TYPE_NULL (6 | BIO_TYPE_SOURCE_SINK)
1048 #define BIO_TYPE_SSL (7 | BIO_TYPE_FILTER)
1049 #define BIO_TYPE_MD (8 | BIO_TYPE_FILTER)
1050 #define BIO_TYPE_BUFFER (9 | BIO_TYPE_FILTER)
1051 #define BIO_TYPE_CIPHER (10 | BIO_TYPE_FILTER)
1052 #define BIO_TYPE_BASE64 (11 | BIO_TYPE_FILTER)
1053 #define BIO_TYPE_CONNECT (12 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR)
1054 #define BIO_TYPE_ACCEPT (13 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR)
1055 #define BIO_TYPE_PROXY_CLIENT (14 | BIO_TYPE_FILTER)
1056 #define BIO_TYPE_PROXY_SERVER (15 | BIO_TYPE_FILTER)
1057 #define BIO_TYPE_NBIO_TEST (16 | BIO_TYPE_FILTER)
1058 #define BIO_TYPE_NULL_FILTER (17 | BIO_TYPE_FILTER)
1059 #define BIO_TYPE_BER (18 | BIO_TYPE_FILTER) // BER -> bin filter
1060 #define BIO_TYPE_BIO (19 | BIO_TYPE_SOURCE_SINK) // (half a) BIO pair
1061 #define BIO_TYPE_LINEBUFFER (20 | BIO_TYPE_FILTER)
1062 #define BIO_TYPE_DGRAM (21 | BIO_TYPE_SOURCE_SINK | BIO_TYPE_DESCRIPTOR)
1063 #define BIO_TYPE_ASN1 (22 | BIO_TYPE_FILTER)
1064 #define BIO_TYPE_COMP (23 | BIO_TYPE_FILTER)
1065
1066 // BIO_TYPE_START is the first user-allocated |BIO| type. No pre-defined type,
1067 // flag bits aside, may exceed this value.
1068 #define BIO_TYPE_START 128
1069
1070 struct bio_method_st {
1071 int type;
1072 const char *name;
1073 int (*bwrite)(BIO *, const char *, int);
1074 int (*bread)(BIO *, char *, int);
1075 int (*bputs)(BIO *, const char *);
1076 int (*bgets)(BIO *, char *, int);
1077 long (*ctrl)(BIO *, int, long, void *);
1078 int (*create)(BIO *);
1079 int (*destroy)(BIO *);
1080 long (*callback_ctrl)(BIO *, int, bio_info_cb);
1081 };
1082
1083 struct bio_st {
1084 const BIO_METHOD *method;
1085 CRYPTO_EX_DATA ex_data;
1086
1087 // If set, |BIO_read|, |BIO_write|, |BIO_free|, |BIO_gets|, |BIO_puts|,
1088 // and |BIO_ctrl| execute |callback_ex|.
1089 // Callbacks are only called with for the following events: |BIO_CB_READ|,
1090 // |BIO_CB_READ|+|BIO_CB_RETURN|, |BIO_CB_WRITE|,
1091 // |BIO_CB_WRITE|+|BIO_CB_RETURN|, |BIO_CB_PUTS|,
1092 // |BIO_CB_PUTS|+|BIO_CB_RETURN|, |BIO_CB_GETS|,
1093 // |BIO_CB_GETS|+|BIO_CB_RETURN|, |BIO_CB_CTRL|,
1094 // |BIO_CB_CTRL|+|BIO_CB_RETURN|, and |BIO_CB_FREE|.
1095 BIO_callback_fn_ex callback_ex;
1096
1097 // Legacy callback function that handles the same events as |callback_ex| but without
1098 // length and processed parameters.
1099 // When both callbacks are set, |callback_ex| will be used.
1100 // Handles |BIO_read|, |BIO_write|, |BIO_free|, |BIO_gets|, |BIO_puts|,
1101 // and |BIO_ctrl| operations.
1102 // Callbacks are only called with for the following events: |BIO_CB_READ|,
1103 // |BIO_CB_READ|+|BIO_CB_RETURN|, |BIO_CB_WRITE|,
1104 // |BIO_CB_WRITE|+|BIO_CB_RETURN|, |BIO_CB_PUTS|,
1105 // |BIO_CB_PUTS|+|BIO_CB_RETURN|, |BIO_CB_GETS|,
1106 // |BIO_CB_GETS|+|BIO_CB_RETURN|, |BIO_CB_CTRL|,
1107 // |BIO_CB_CTRL|+|BIO_CB_RETURN|, and |BIO_CB_FREE|.
1108 BIO_callback_fn callback;
1109
1110 // Optional callback argument, only intended for applications use.
1111 char *cb_arg;
1112
1113 // init is non-zero if this |BIO| has been initialised.
1114 int init;
1115 // shutdown is often used by specific |BIO_METHOD|s to determine whether
1116 // they own some underlying resource. This flag can often by controlled by
1117 // |BIO_set_close|. For example, whether an fd BIO closes the underlying fd
1118 // when it, itself, is closed.
1119 int shutdown;
1120 int flags;
1121 int retry_reason;
1122 // num is a BIO-specific value. For example, in fd BIOs it's used to store a
1123 // file descriptor.
1124 int num;
1125 CRYPTO_refcount_t references;
1126 void *ptr;
1127 // next_bio points to the next |BIO| in a chain. This |BIO| owns a reference
1128 // to |next_bio|.
1129 BIO *next_bio; // used by filter BIOs
1130 uint64_t num_read, num_write;
1131 };
1132
1133 #define BIO_C_SET_CONNECT 100
1134 #define BIO_C_DO_STATE_MACHINE 101
1135 #define BIO_C_SET_NBIO 102
1136 #define BIO_C_SET_PROXY_PARAM 103
1137 #define BIO_C_SET_FD 104
1138 #define BIO_C_GET_FD 105
1139 #define BIO_C_SET_FILE_PTR 106
1140 #define BIO_C_GET_FILE_PTR 107
1141 #define BIO_C_SET_FILENAME 108
1142 #define BIO_C_SET_SSL 109
1143 #define BIO_C_GET_SSL 110
1144 #define BIO_C_SET_MD 111
1145 #define BIO_C_GET_MD 112
1146 #define BIO_C_GET_CIPHER_STATUS 113
1147 #define BIO_C_SET_BUF_MEM 114
1148 #define BIO_C_GET_BUF_MEM_PTR 115
1149 #define BIO_C_GET_BUFF_NUM_LINES 116
1150 #define BIO_C_SET_BUFF_SIZE 117
1151 #define BIO_C_SET_ACCEPT 118
1152 #define BIO_C_SSL_MODE 119
1153 #define BIO_C_GET_MD_CTX 120
1154 #define BIO_C_GET_PROXY_PARAM 121
1155 #define BIO_C_SET_BUFF_READ_DATA 122 // data to read first
1156 #define BIO_C_GET_ACCEPT 124
1157 #define BIO_C_FILE_SEEK 128
1158 #define BIO_C_GET_CIPHER_CTX 129
1159 #define BIO_C_SET_BUF_MEM_EOF_RETURN 130 // return end of input value
1160 #define BIO_C_SET_BIND_MODE 131
1161 #define BIO_C_GET_BIND_MODE 132
1162 #define BIO_C_FILE_TELL 133
1163 #define BIO_C_GET_SOCKS 134
1164 #define BIO_C_SET_SOCKS 135
1165
1166 #define BIO_C_SET_WRITE_BUF_SIZE 136 // for BIO_s_bio
1167 #define BIO_C_GET_WRITE_BUF_SIZE 137
1168 #define BIO_C_GET_WRITE_GUARANTEE 140
1169 #define BIO_C_GET_READ_REQUEST 141
1170 #define BIO_C_SHUTDOWN_WR 142
1171 #define BIO_C_NREAD0 143
1172 #define BIO_C_NREAD 144
1173 #define BIO_C_NWRITE0 145
1174 #define BIO_C_NWRITE 146
1175 #define BIO_C_RESET_READ_REQUEST 147
1176 #define BIO_C_SET_MD_CTX 148
1177
1178 #define BIO_C_SET_PREFIX 149
1179 #define BIO_C_GET_PREFIX 150
1180 #define BIO_C_SET_SUFFIX 151
1181 #define BIO_C_GET_SUFFIX 152
1182
1183 #define BIO_C_SET_EX_ARG 153
1184 #define BIO_C_GET_EX_ARG 154
1185
1186
1187 #if defined(__cplusplus)
1188 } // extern C
1189
1190 extern "C++" {
1191
1192 BSSL_NAMESPACE_BEGIN
1193
1194 BORINGSSL_MAKE_DELETER(BIO, BIO_free)
1195 BORINGSSL_MAKE_UP_REF(BIO, BIO_up_ref)
1196 BORINGSSL_MAKE_DELETER(BIO_METHOD, BIO_meth_free)
1197
1198 #if !defined(OPENSSL_NO_SOCK)
1199 BORINGSSL_MAKE_DELETER(BIO_ADDR, BIO_ADDR_free)
1200 #endif
1201
1202 BSSL_NAMESPACE_END
1203
1204 } // extern C++
1205
1206 #endif
1207
1208 #define BIO_R_BAD_FOPEN_MODE 100
1209 #define BIO_R_BROKEN_PIPE 101
1210 #define BIO_R_CONNECT_ERROR 102
1211 #define BIO_R_ERROR_SETTING_NBIO 103
1212 #define BIO_R_INVALID_ARGUMENT 104
1213 #define BIO_R_IN_USE 105
1214 #define BIO_R_KEEPALIVE 106
1215 #define BIO_R_NBIO_CONNECT_ERROR 107
1216 #define BIO_R_NO_HOSTNAME_SPECIFIED 108
1217 #define BIO_R_NO_PORT_SPECIFIED 109
1218 #define BIO_R_NO_SUCH_FILE 110
1219 #define BIO_R_NULL_PARAMETER 111
1220 #define BIO_R_SYS_LIB 112
1221 #define BIO_R_UNABLE_TO_CREATE_SOCKET 113
1222 #define BIO_R_UNINITIALIZED 114
1223 #define BIO_R_UNSUPPORTED_METHOD 115
1224 #define BIO_R_WRITE_TO_READ_ONLY_BIO 116
1225
1226 #endif // OPENSSL_HEADER_BIO_H