1.\" $OpenBSD: BIO_set_callback.3,v 1.10 2022/12/16 23:56:57 schwarze Exp $ 2.\" full merge up to: OpenSSL 24a535ea Sep 22 13:14:20 2020 +0100 3.\" 4.\" This file is a derived work. 5.\" The changes are covered by the following Copyright and license: 6.\" 7.\" Copyright (c) 2018, 2022 Ingo Schwarze <schwarze@openbsd.org> 8.\" 9.\" Permission to use, copy, modify, and distribute this software for any 10.\" purpose with or without fee is hereby granted, provided that the above 11.\" copyright notice and this permission notice appear in all copies. 12.\" 13.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 16.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 18.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 19.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20.\" 21.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>. 22.\" Copyright (c) 2000, 2016, 2017 The OpenSSL Project. All rights reserved. 23.\" 24.\" Redistribution and use in source and binary forms, with or without 25.\" modification, are permitted provided that the following conditions 26.\" are met: 27.\" 28.\" 1. Redistributions of source code must retain the above copyright 29.\" notice, this list of conditions and the following disclaimer. 30.\" 31.\" 2. Redistributions in binary form must reproduce the above copyright 32.\" notice, this list of conditions and the following disclaimer in 33.\" the documentation and/or other materials provided with the 34.\" distribution. 35.\" 36.\" 3. All advertising materials mentioning features or use of this 37.\" software must display the following acknowledgment: 38.\" "This product includes software developed by the OpenSSL Project 39.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 40.\" 41.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 42.\" endorse or promote products derived from this software without 43.\" prior written permission. For written permission, please contact 44.\" openssl-core@openssl.org. 45.\" 46.\" 5. Products derived from this software may not be called "OpenSSL" 47.\" nor may "OpenSSL" appear in their names without prior written 48.\" permission of the OpenSSL Project. 49.\" 50.\" 6. Redistributions of any form whatsoever must retain the following 51.\" acknowledgment: 52.\" "This product includes software developed by the OpenSSL Project 53.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 54.\" 55.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 56.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 57.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 58.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 59.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 60.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 61.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 62.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 63.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 64.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 66.\" OF THE POSSIBILITY OF SUCH DAMAGE. 67.\" 68.Dd $Mdocdate: December 16 2022 $ 69.Dt BIO_SET_CALLBACK 3 70.Os 71.Sh NAME 72.Nm BIO_callback_fn_ex , 73.Nm BIO_set_callback_ex , 74.Nm BIO_get_callback_ex , 75.Nm BIO_callback_fn , 76.Nm BIO_set_callback , 77.Nm BIO_get_callback , 78.Nm BIO_set_callback_arg , 79.Nm BIO_get_callback_arg , 80.Nm BIO_debug_callback 81.Nd BIO callback functions 82.Sh SYNOPSIS 83.In openssl/bio.h 84.Ft typedef long 85.Fo (*BIO_callback_fn_ex) 86.Fa "BIO *b" 87.Fa "int oper" 88.Fa "const char *argp" 89.Fa "size_t len" 90.Fa "int argi" 91.Fa "long argl" 92.Fa "int ret" 93.Fa "size_t *processed" 94.Fc 95.Ft void 96.Fo BIO_set_callback_ex 97.Fa "BIO *b" 98.Fa "BIO_callback_fn_ex cb_ex" 99.Fc 100.Ft BIO_callback_fn_ex 101.Fo BIO_get_callback_ex 102.Fa "const BIO *b" 103.Fc 104.Ft typedef long 105.Fo (*BIO_callback_fn) 106.Fa "BIO *b" 107.Fa "int oper" 108.Fa "const char *argp" 109.Fa "int argi" 110.Fa "long argl" 111.Fa "long ret" 112.Fc 113.Ft void 114.Fo BIO_set_callback 115.Fa "BIO *b" 116.Fa "BIO_callback_fn cb" 117.Fc 118.Ft BIO_callback_fn 119.Fo BIO_get_callback 120.Fa "BIO *b" 121.Fc 122.Ft void 123.Fo BIO_set_callback_arg 124.Fa "BIO *b" 125.Fa "char *pointer" 126.Fc 127.Ft char * 128.Fo BIO_get_callback_arg 129.Fa "const BIO *b" 130.Fc 131.Ft long 132.Fo BIO_debug_callback 133.Fa "BIO *bio" 134.Fa "int oper" 135.Fa "const char *argp" 136.Fa "int argi" 137.Fa "long argl" 138.Fa "long ret" 139.Fc 140.Sh DESCRIPTION 141.Fn BIO_set_callback_ex 142and 143.Fn BIO_get_callback_ex 144set and retrieve the BIO callback. 145The callback is called during most high-level BIO operations. 146It can be used for debugging purposes to trace operations on a BIO 147or to modify its operation. 148.Pp 149.Fn BIO_set_callback 150and 151.Fn BIO_get_callback 152are deprecated functions that set and retrieve the old-style BIO callback, 153which is only used if no new-style callback is set with 154.Fn BIO_set_callback_ex . 155.Pp 156.Fn BIO_set_callback_arg 157stores the 158.Fa pointer 159internally in 160.Fa b 161and 162.Fn BIO_get_callback_arg 163retrieves it from 164.Fa b . 165The name of these two functions is badly misleading: the 166.Fa pointer 167is never passed as an argument to any callback function. 168But of course, callback functions can call 169.Fn BIO_get_callback_arg 170and access the pointer, just like any other code can. 171.Pp 172.Fn BIO_debug_callback 173is a standard debugging callback which prints 174out information related to each BIO operation. 175If 176.Fn BIO_set_callback_arg 177was called with a 178.Pf non- Dv NULL 179argument, information is sent to the BIO pointed to by the 180.Fa pointer ; 181otherwise, standard error output is used. 182.Pp 183The arguments of the callback functions are as follows: 184.Bl -tag -width Ds 185.It Fa b 186The BIO the callback is attached to. 187.It Fa oper 188The operation being performed. 189For some operations, the callback is called twice, 190once before and once after the actual operation. 191The latter case has 192.Fa oper 193OR'ed with 194.Dv BIO_CB_RETURN . 195.It Fa argp , argi , argl 196The meaning of these three arguments depends on the value of 197.Fa oper , 198that is on the operation being performed. 199.It Fa len 200The length of the data requested to be read or written. 201This is only useful if 202.Fa oper 203is 204.Dv BIO_CB_READ , 205.Dv BIO_CB_WRITE , 206or 207.Dv BIO_CB_GETS . 208.It Fa ret 209When 210.Fa oper 211does not include 212.Dv BIO_CB_RETURN , 213i.e. when the callback is invoked before an operation, 214the value passed into the callback via 215.Fa ret 216is always 1. 217In this case, if the callback returns a negative value, the library 218aborts the requested operation and instead returns the negative 219return value from the callback to the application. 220If the callback returns a non-negative value, that return value is 221ignored by the library, and the operation is performed normally. 222.Pp 223When 224.Fa oper 225includes 226.Dv BIO_CB_RETURN , 227i.e. when the callback is invoked after an operation, 228the value passed into the callback via 229.Fa ret 230is the return value that the operation would return to the application 231if no callback were present. 232When a callback is present, the operation only passes this value 233to the callback and instead of it returns the return value of the 234callback to the application. 235.It Fa processed 236The location pointed to is updated with the number of bytes 237actually read or written. 238Only used for 239.Dv BIO_CB_READ , 240.Dv BIO_CB_WRITE , 241.Dv BIO_CB_GETS , 242and 243.Dv BIO_CB_PUTS . 244.El 245.Pp 246The callback should normally simply return 247.Fa ret 248when it has finished processing, unless it specifically wishes to 249abort the operation or to modify the value returned to the application. 250.Pp 251The callbacks are called as follows: 252.Bl -tag -width 1n 253.It \&In Fn BIO_free "BIO *b" : 254.Bd -literal 255before the free operation: 256cb_ex(b, BIO_CB_FREE, NULL, 0, 0, 0, 1, NULL) 257or cb(b, BIO_CB_FREE, NULL, 0, 0, 1) 258.Ed 259.It \&In Fn BIO_read "BIO *b" "void *out" "int outl" : 260.Bd -literal 261before the read operation: 262cb_ex(b, BIO_CB_READ, out, outl, 0, 0, 1, NULL) 263or cb(b, BIO_CB_READ, out, outl, 0, 1) 264 265after the read operation: 266cb_ex(b, BIO_CB_READ|BIO_CB_RETURN, out, outl, 0, 0, ret, &bytes) 267or cb(b, BIO_CB_READ|BIO_CB_RETURN, out, outl, 0, ret) 268.Ed 269.It \&In Fn BIO_write "BIO *b" "const void *in" "int inl" : 270.Bd -literal 271before the write operation: 272cb_ex(b, BIO_CB_WRITE, in, inl, 0, 0, 1, NULL) 273or cb(b, BIO_CB_WRITE, in, inl, 0, 1) 274 275after the write operation: 276cb_ex(b, BIO_CB_WRITE|BIO_CB_RETURN, in, inl, 0, 0, ret, &bytes) 277or cb(b, BIO_CB_WRITE|BIO_CB_RETURN, in, inl, 0, ret) 278.Ed 279.It \&In Fn BIO_gets "BIO *b" "char *out" "int outl" : 280.Bd -literal 281before the read operation: 282cb_ex(b, BIO_CB_GETS, out, outl, 0, 0, 1, NULL) 283or cb(b, BIO_CB_GETS, out, outl, 0, 1) 284 285after the read operation: 286cb_ex(b, BIO_CB_GETS|BIO_CB_RETURN, out, outl, 0, 0, ret, &bytes) 287or cb(b, BIO_CB_GETS|BIO_CB_RETURN, out, outl, 0, ret) 288.Ed 289.It \&In Fn BIO_puts "BIO *b" "const char *in" : 290.Bd -literal 291before the write operation: 292cb_ex(b, BIO_CB_PUTS, in, 0, 0, 0, 1, NULL) 293or cb(b, BIO_CB_PUTS, in, 0, 0, 1) 294 295after the write operation: 296cb_ex(b, BIO_CB_PUTS|BIO_CB_RETURN, in, 0, 0, 0, ret, &bytes) 297or cb(b, BIO_CB_PUTS|BIO_CB_RETURN, in, 0, 0, ret) 298.Ed 299.It \&In Fn BIO_ctrl "BIO *b" "int cmd" "long larg" "void *parg" : 300.Bd -literal 301before the control operation: 302cb_ex(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1, NULL) 303or cb(b, BIO_CB_CTRL, parg, cmd, larg, 1) 304 305after the control operation: 306cb_ex(b, BIO_CB_CTRL|BIO_CB_RETURN, parg, 0, cmd, larg, ret, NULL) 307or cb(b, BIO_CB_CTRL|BIO_CB_RETURN, parg, cmd, larg, ret) 308.Ed 309.It \&In Fn BIO_callback_ctrl "BIO *b" "int cmd" "BIO_info_cb *fp" : 310.Bd -literal 311before the control operation: 312cb_ex(b, BIO_CB_CTRL, fp, 0, cmd, 0, 1, NULL) 313or cb(b, BIO_CB_CTRL, fp, cmd, 0, 1) 314 315after the control operation: 316cb_ex(b, BIO_CB_CTRL|BIO_CB_RETURN, fp, 0, cmd, 0, ret, NULL) 317or cb(b, BIO_CB_CTRL|BIO_CB_RETURN, fp, cmd, 0, ret) 318.Ed 319.El 320.Sh RETURN VALUES 321.Fn BIO_get_callback_ex 322returns a pointer to the function 323.Fa cb_ex 324previously installed with 325.Fn BIO_set_callback_cb , 326or 327.Dv NULL 328if no such callback was installed. 329.Pp 330.Fn BIO_get_callback 331returns a pointer to the function 332.Fa cb 333previously installed with 334.Fn BIO_set_callback , 335or 336.Dv NULL 337if no such callback was installed. 338.Pp 339.Fn BIO_get_callback_arg 340returns the 341.Fa pointer 342previously set with 343.Fn BIO_set_callback_arg , 344or 345.Dv NULL 346if no such pointer was set. 347.Pp 348.Fn BIO_debug_callback 349returns 350.Fa ret 351if the bit 352.Dv BIO_CB_RETURN 353is set in 354.Fa cmd , 355or 1 otherwise. 356.Sh EXAMPLES 357The 358.Fn BIO_debug_callback 359function is a good example. 360Its source is in the file 361.Pa crypto/bio/bio_cb.c . 362.Sh SEE ALSO 363.Xr BIO_new 3 364.Sh HISTORY 365.Fn BIO_set_callback , 366.Fn BIO_get_callback , 367.Fn BIO_set_callback_arg , 368and 369.Fn BIO_debug_callback 370first appeared in SSLeay 0.6.0. 371.Fn BIO_get_callback_arg 372first appeared in SSLeay 0.8.0. 373These functions have been available since 374.Ox 2.4 . 375.Pp 376.Fn BIO_callback_fn 377first appeared in OpenSSL 1.1.0. 378.Fn BIO_callback_fn_ex , 379.Fn BIO_set_callback_ex , 380and 381.Fn BIO_get_callback_ex 382first appeared in OpenSSL 1.1.1. 383These functions have been available since 384.Ox 7.1 . 385