1.\" $OpenBSD: BIO_set_callback.3,v 1.9 2018/03/29 20:42:17 schwarze Exp $ 2.\" full merge up to: OpenSSL 99d63d46 Oct 26 13:56:48 2016 -0400 3.\" selective merge up to: OpenSSL 61f805c1 Jan 16 01:01:46 2018 +0800 4.\" 5.\" This file is a derived work. 6.\" The changes are covered by the following Copyright and license: 7.\" 8.\" Copyright (c) 2018 Ingo Schwarze <schwarze@openbsd.org> 9.\" 10.\" Permission to use, copy, modify, and distribute this software for any 11.\" purpose with or without fee is hereby granted, provided that the above 12.\" copyright notice and this permission notice appear in all copies. 13.\" 14.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 15.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 17.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 20.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21.\" 22.\" The original file was written by Dr. Stephen Henson <steve@openssl.org>. 23.\" Copyright (c) 2000, 2016, 2017 The OpenSSL Project. All rights reserved. 24.\" 25.\" Redistribution and use in source and binary forms, with or without 26.\" modification, are permitted provided that the following conditions 27.\" are met: 28.\" 29.\" 1. Redistributions of source code must retain the above copyright 30.\" notice, this list of conditions and the following disclaimer. 31.\" 32.\" 2. Redistributions in binary form must reproduce the above copyright 33.\" notice, this list of conditions and the following disclaimer in 34.\" the documentation and/or other materials provided with the 35.\" distribution. 36.\" 37.\" 3. All advertising materials mentioning features or use of this 38.\" software must display the following acknowledgment: 39.\" "This product includes software developed by the OpenSSL Project 40.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 41.\" 42.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 43.\" endorse or promote products derived from this software without 44.\" prior written permission. For written permission, please contact 45.\" openssl-core@openssl.org. 46.\" 47.\" 5. Products derived from this software may not be called "OpenSSL" 48.\" nor may "OpenSSL" appear in their names without prior written 49.\" permission of the OpenSSL Project. 50.\" 51.\" 6. Redistributions of any form whatsoever must retain the following 52.\" acknowledgment: 53.\" "This product includes software developed by the OpenSSL Project 54.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 55.\" 56.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 57.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 58.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 60.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 61.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 62.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 63.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 64.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 65.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 66.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 67.\" OF THE POSSIBILITY OF SUCH DAMAGE. 68.\" 69.Dd $Mdocdate: March 29 2018 $ 70.Dt BIO_SET_CALLBACK 3 71.Os 72.Sh NAME 73.Nm BIO_set_callback , 74.Nm BIO_get_callback , 75.Nm BIO_set_callback_arg , 76.Nm BIO_get_callback_arg , 77.Nm BIO_debug_callback 78.Nd BIO callback functions 79.Sh SYNOPSIS 80.In openssl/bio.h 81.Ft void 82.Fo BIO_set_callback 83.Fa "BIO *b" 84.Fa "BIO_callback_fn cb" 85.Fc 86.Ft BIO_callback_fn 87.Fo BIO_get_callback 88.Fa "BIO *b" 89.Fc 90.Ft void 91.Fo BIO_set_callback_arg 92.Fa "BIO *b" 93.Fa "char *arg" 94.Fc 95.Ft char * 96.Fo BIO_get_callback_arg 97.Fa "const BIO *b" 98.Fc 99.Ft long 100.Fo BIO_debug_callback 101.Fa "BIO *bio" 102.Fa "int oper" 103.Fa "const char *argp" 104.Fa "int argi" 105.Fa "long argl" 106.Fa "long ret" 107.Fc 108.Ft typedef long 109.Fo "(*BIO_callback_fn)" 110.Fa "BIO *b" 111.Fa "int oper" 112.Fa "const char *argp" 113.Fa "int argi" 114.Fa "long argl" 115.Fa "long ret" 116.Fc 117.Sh DESCRIPTION 118.Fn BIO_set_callback 119and 120.Fn BIO_get_callback 121set and retrieve the BIO callback. 122The callback is called during most high level BIO operations. 123It can be used for debugging purposes to trace operations on a BIO 124or to modify its operation. 125.Pp 126.Fn BIO_set_callback_arg 127and 128.Fn BIO_get_callback_arg 129set and retrieve an argument for use in the callback. 130.Pp 131.Fn BIO_debug_callback 132is a standard debugging callback which prints 133out information relating to each BIO operation. 134If the callback argument is set, it is interpreted as a BIO 135to send the information to, otherwise stderr is used. 136.Pp 137.Fn BIO_callback_fn 138is the type of the callback function. 139The meaning of each argument is described below. 140.Pp 141The BIO the callback is attached to is passed in 142.Fa b . 143.Pp 144.Fa oper 145is set to the operation being performed. 146For some operations the callback is called twice, 147once before and once after the actual operation. 148The latter case has 149.Fa oper 150or'ed with 151.Dv BIO_CB_RETURN . 152.Pp 153The meaning of the arguments 154.Fa argp , 155.Fa argi 156and 157.Fa argl 158depends on the value of 159.Fa oper 160(i.e. the operation being performed). 161.Pp 162When 163.Fa oper 164does not include 165.Dv BIO_CB_RETURN , 166i.e. when the callback is invoked before an operation, 167the value passed into the callback via 168.Fa ret 169is always 1. 170In this case, if the callback returns a negative value, the library 171aborts the requested operation and instead returns the negative 172return value from the callback to the application. 173If the callback returns a non-negative value, that return value is 174ignored by the library, and the operation is performed normally. 175.Pp 176When 177.Fa oper 178includes 179.Dv BIO_CB_RETURN , 180i.e. when the callback is invoked after an operation, 181the value passed into the callback via 182.Fa ret 183is the return value that the operation would return to the application 184if no callback were present. 185When a callback is present, the operation only passes this value 186to the callback and instead of it returns the return value of the 187callback to the application. 188.Pp 189The callback should normally simply return 190.Fa ret 191when it has finished processing, unless it specifically wishes to 192abort the operation or to modify the value returned to the application. 193.Ss Callback operations 194.Bl -tag -width Ds 195.It Fn BIO_free b 196.Fn callback b BIO_CB_FREE NULL 0L 0L 1L 197is called before the free operation. 198.It Fn BIO_read b out outl 199.Fn callback b BIO_CB_READ out outl 0L 1L 200is called before the read and 201.Fn callback b BIO_CB_READ|BIO_CB_RETURN out outl 0L ret 202after. 203.It Fn BIO_write b in inl 204.Fn callback b BIO_CB_WRITE in inl 0L 1L 205is called before the write and 206.Fn callback b BIO_CB_WRITE|BIO_CB_RETURN in inl 0L ret 207after. 208.It Fn BIO_gets b out outl 209.Fn callback b BIO_CB_GETS out outl 0L 1L 210is called before the operation and 211.Fn callback b BIO_CB_GETS|BIO_CB_RETURN out outl 0L ret 212after. 213.It Fn BIO_puts b in 214.Fn callback b BIO_CB_PUTS in 0 0L 1L 215is called before the operation and 216.Fn callback b BIO_CB_PUTS|BIO_CB_RETURN in 0 0L ret 217after. 218.It Fn BIO_ctrl b oper larg parg 219.Fn callback b BIO_CB_CTRL parg oper larg 1L 220is called before the call and 221.Fn callback b BIO_CB_CTRL|BIO_CB_RETURN parg oper larg ret 222after. 223.El 224.Sh RETURN VALUES 225.Fn BIO_get_callback 226returns a pointer to the function 227.Fa cb 228previously installed with 229.Fn BIO_set_callback , 230or 231.Dv NULL 232if no callback was installed. 233.Pp 234.Fn BIO_get_callback_arg 235returns a pointer to the 236.Fa arg 237previously set with 238.Fn BIO_set_callback_arg , 239or 240.Dv NULL 241if no such argument was set. 242.Pp 243.Fn BIO_debug_callback 244returns 245.Fa ret 246if the bit 247.Dv BIO_CB_RETURN 248is set in 249.Fa cmd , 250or 1 otherwise. 251.Sh EXAMPLES 252The 253.Fn BIO_debug_callback 254function is a good example. 255Its source is in the file 256.Pa crypto/bio/bio_cb.c . 257.Sh SEE ALSO 258.Xr BIO_new 3 259.Sh HISTORY 260.Fn BIO_set_callback , 261.Fn BIO_get_callback , 262.Fn BIO_set_callback_arg , 263and 264.Fn BIO_debug_callback 265first appeared in SSLeay 0.6.0. 266.Fn BIO_get_callback_arg 267first appeared in SSLeay 0.8.0. 268All these functions have been available since 269.Ox 2.4 . 270