xref: /openbsd-src/lib/libcrypto/bio/bio_cb.c (revision acf644016ec1190723fc541ba590471e90a9ef53)
1*acf64401Sbeck /* $OpenBSD: bio_cb.c,v 1.19 2023/07/05 21:23:37 beck Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
595b37fcf3Sryker #include <stdio.h>
605b37fcf3Sryker #include <stdlib.h>
61b6ab114eSjsing #include <string.h>
62b6ab114eSjsing 
63913ec974Sbeck #include <openssl/err.h>
64b6ab114eSjsing #include <openssl/bio.h>
655b37fcf3Sryker 
6694b1984eStb #include "bio_local.h"
6794b1984eStb 
68c3d505beSjsing long
BIO_debug_callback(BIO * bio,int cmd,const char * argp,int argi,long argl,long ret)69c3d505beSjsing BIO_debug_callback(BIO *bio, int cmd, const char *argp, int argi, long argl,
70c3d505beSjsing     long ret)
715b37fcf3Sryker {
725b37fcf3Sryker 	BIO *b;
738b5c64d9Sbeck 	char buf[256];
745b37fcf3Sryker 	char *p;
75386d3c6dStb 	int nbuf;
765b37fcf3Sryker 	long r = 1;
775a303869Sho 	size_t p_maxlen;
785b37fcf3Sryker 
795b37fcf3Sryker 	if (BIO_CB_RETURN & cmd)
805b37fcf3Sryker 		r = ret;
815b37fcf3Sryker 
82386d3c6dStb 	nbuf = snprintf(buf, sizeof(buf), "BIO[%p]: ", bio);
83386d3c6dStb 	if (nbuf < 0)
84386d3c6dStb 		nbuf = 0;	/* Ignore error; continue printing. */
85386d3c6dStb 	if (nbuf >= sizeof(buf))
86386d3c6dStb 		goto out;
87386d3c6dStb 
88386d3c6dStb 	p = buf + nbuf;
89386d3c6dStb 	p_maxlen = sizeof(buf) - nbuf;
90386d3c6dStb 
91c3d505beSjsing 	switch (cmd) {
925b37fcf3Sryker 	case BIO_CB_FREE:
935359f6d5Sguenther 		snprintf(p, p_maxlen, "Free - %s\n", bio->method->name);
945b37fcf3Sryker 		break;
955b37fcf3Sryker 	case BIO_CB_READ:
965b37fcf3Sryker 		if (bio->method->type & BIO_TYPE_DESCRIPTOR)
975359f6d5Sguenther 			snprintf(p, p_maxlen,
98c3d505beSjsing 			    "read(%d,%lu) - %s fd=%d\n",
990a5d6edeSdjm 			    bio->num, (unsigned long)argi,
1000a5d6edeSdjm 			    bio->method->name, bio->num);
1015b37fcf3Sryker 		else
1025359f6d5Sguenther 			snprintf(p, p_maxlen, "read(%d,%lu) - %s\n",
103c3d505beSjsing 			    bio->num, (unsigned long)argi, bio->method->name);
1045b37fcf3Sryker 		break;
1055b37fcf3Sryker 	case BIO_CB_WRITE:
1065b37fcf3Sryker 		if (bio->method->type & BIO_TYPE_DESCRIPTOR)
1075359f6d5Sguenther 			snprintf(p, p_maxlen,
108c3d505beSjsing 			    "write(%d,%lu) - %s fd=%d\n",
1090a5d6edeSdjm 			    bio->num, (unsigned long)argi,
1100a5d6edeSdjm 			    bio->method->name, bio->num);
1115b37fcf3Sryker 		else
1125359f6d5Sguenther 			snprintf(p, p_maxlen, "write(%d,%lu) - %s\n",
113c3d505beSjsing 			    bio->num, (unsigned long)argi, bio->method->name);
1145b37fcf3Sryker 		break;
1155b37fcf3Sryker 	case BIO_CB_PUTS:
1165359f6d5Sguenther 		snprintf(p, p_maxlen,
117c3d505beSjsing 		    "puts() - %s\n", bio->method->name);
1185b37fcf3Sryker 		break;
1195b37fcf3Sryker 	case BIO_CB_GETS:
1205359f6d5Sguenther 		snprintf(p, p_maxlen, "gets(%lu) - %s\n",
121c3d505beSjsing 		    (unsigned long)argi, bio->method->name);
1225b37fcf3Sryker 		break;
1235b37fcf3Sryker 	case BIO_CB_CTRL:
1245359f6d5Sguenther 		snprintf(p, p_maxlen, "ctrl(%lu) - %s\n",
125c3d505beSjsing 		    (unsigned long)argi, bio->method->name);
1265b37fcf3Sryker 		break;
1275b37fcf3Sryker 	case BIO_CB_RETURN|BIO_CB_READ:
1285359f6d5Sguenther 		snprintf(p, p_maxlen, "read return %ld\n", ret);
1295b37fcf3Sryker 		break;
1305b37fcf3Sryker 	case BIO_CB_RETURN|BIO_CB_WRITE:
1315359f6d5Sguenther 		snprintf(p, p_maxlen, "write return %ld\n", ret);
1325b37fcf3Sryker 		break;
1335b37fcf3Sryker 	case BIO_CB_RETURN|BIO_CB_GETS:
1345359f6d5Sguenther 		snprintf(p, p_maxlen, "gets return %ld\n", ret);
1355b37fcf3Sryker 		break;
1365b37fcf3Sryker 	case BIO_CB_RETURN|BIO_CB_PUTS:
1375359f6d5Sguenther 		snprintf(p, p_maxlen, "puts return %ld\n", ret);
1385b37fcf3Sryker 		break;
1395b37fcf3Sryker 	case BIO_CB_RETURN|BIO_CB_CTRL:
1405359f6d5Sguenther 		snprintf(p, p_maxlen, "ctrl return %ld\n", ret);
1415b37fcf3Sryker 		break;
1425b37fcf3Sryker 	default:
1435359f6d5Sguenther 		snprintf(p, p_maxlen,
144c3d505beSjsing 		    "bio callback - unknown type (%d)\n", cmd);
1455b37fcf3Sryker 		break;
1465b37fcf3Sryker 	}
1475b37fcf3Sryker 
148386d3c6dStb  out:
1495b37fcf3Sryker 	b = (BIO *)bio->cb_arg;
1505b37fcf3Sryker 	if (b != NULL)
1515b37fcf3Sryker 		BIO_write(b, buf, strlen(buf));
1525b37fcf3Sryker 	else
1535b37fcf3Sryker 		fputs(buf, stderr);
1545b37fcf3Sryker 	return (r);
1555b37fcf3Sryker }
156*acf64401Sbeck LCRYPTO_ALIAS(BIO_debug_callback);
157