Coverage Report

Created: 2025-04-22 06:18

/src/openssl/crypto/bio/bio_cb.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#define OPENSSL_SUPPRESS_DEPRECATED
11
12
#include <stdio.h>
13
#include <string.h>
14
#include <stdlib.h>
15
#include "bio_local.h"
16
#include "internal/cryptlib.h"
17
#include <openssl/err.h>
18
19
long BIO_debug_callback_ex(BIO *bio, int cmd, const char *argp, size_t len,
20
                           int argi, long argl, int ret, size_t *processed)
21
0
{
22
0
    BIO *b;
23
0
    char buf[256];
24
0
    char *p;
25
0
    int left;
26
0
    size_t l = 0;
27
28
0
    if (processed != NULL)
29
0
        l = *processed;
30
31
0
    left = BIO_snprintf(buf, sizeof(buf), "BIO[%p]: ", (void *)bio);
32
33
    /* Ignore errors and continue printing the other information. */
34
0
    if (left < 0)
35
0
        left = 0;
36
0
    p = buf + left;
37
0
    left = sizeof(buf) - left;
38
39
0
    switch (cmd) {
40
0
    case BIO_CB_FREE:
41
0
        BIO_snprintf(p, left, "Free - %s\n", bio->method->name);
42
0
        break;
43
0
    case BIO_CB_READ:
44
0
        if (bio->method->type & BIO_TYPE_DESCRIPTOR)
45
0
            BIO_snprintf(p, left, "read(%d,%zu) - %s fd=%d\n",
46
0
                         bio->num, len,
47
0
                         bio->method->name, bio->num);
48
0
        else
49
0
            BIO_snprintf(p, left, "read(%d,%zu) - %s\n",
50
0
                    bio->num, len, bio->method->name);
51
0
        break;
52
0
    case BIO_CB_WRITE:
53
0
        if (bio->method->type & BIO_TYPE_DESCRIPTOR)
54
0
            BIO_snprintf(p, left, "write(%d,%zu) - %s fd=%d\n",
55
0
                         bio->num, len,
56
0
                         bio->method->name, bio->num);
57
0
        else
58
0
            BIO_snprintf(p, left, "write(%d,%zu) - %s\n",
59
0
                         bio->num, len, bio->method->name);
60
0
        break;
61
0
    case BIO_CB_PUTS:
62
0
        BIO_snprintf(p, left, "puts() - %s\n", bio->method->name);
63
0
        break;
64
0
    case BIO_CB_GETS:
65
0
        BIO_snprintf(p, left, "gets(%zu) - %s\n", len,
66
0
                     bio->method->name);
67
0
        break;
68
0
    case BIO_CB_CTRL:
69
0
        BIO_snprintf(p, left, "ctrl(%d) - %s\n", argi,
70
0
                     bio->method->name);
71
0
        break;
72
0
    case BIO_CB_RETURN | BIO_CB_READ:
73
0
        BIO_snprintf(p, left, "read return %d processed: %zu\n", ret, l);
74
0
        break;
75
0
    case BIO_CB_RETURN | BIO_CB_WRITE:
76
0
        BIO_snprintf(p, left, "write return %d processed: %zu\n", ret, l);
77
0
        break;
78
0
    case BIO_CB_RETURN | BIO_CB_GETS:
79
0
        BIO_snprintf(p, left, "gets return %d processed: %zu\n", ret, l);
80
0
        break;
81
0
    case BIO_CB_RETURN | BIO_CB_PUTS:
82
0
        BIO_snprintf(p, left, "puts return %d processed: %zu\n", ret, l);
83
0
        break;
84
0
    case BIO_CB_RETURN | BIO_CB_CTRL:
85
0
        BIO_snprintf(p, left, "ctrl return %d\n", ret);
86
0
        break;
87
0
    default:
88
0
        BIO_snprintf(p, left, "bio callback - unknown type (%d)\n", cmd);
89
0
        break;
90
0
    }
91
92
0
    b = (BIO *)bio->cb_arg;
93
0
    if (b != NULL)
94
0
        BIO_write(b, buf, strlen(buf));
95
0
#if !defined(OPENSSL_NO_STDIO)
96
0
    else
97
0
        fputs(buf, stderr);
98
0
#endif
99
0
    return ret;
100
0
}
101
102
#ifndef OPENSSL_NO_DEPRECATED_3_0
103
long BIO_debug_callback(BIO *bio, int cmd, const char *argp,
104
                        int argi, long argl, long ret)
105
0
{
106
0
    size_t processed = 0;
107
108
0
    if (ret > 0)
109
0
        processed = (size_t)ret;
110
0
    BIO_debug_callback_ex(bio, cmd, argp, (size_t)argi,
111
0
                          argi, argl, ret > 0 ? 1 : (int)ret, &processed);
112
0
    return ret;
113
0
}
114
#endif