Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/evp/bio_md.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
#include <stdio.h>
11
#include <errno.h>
12
#include <openssl/buffer.h>
13
#include <openssl/evp.h>
14
#include "internal/bio.h"
15
16
/*
17
 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18
 */
19
20
static int md_write(BIO *h, char const *buf, int num);
21
static int md_read(BIO *h, char *buf, int size);
22
static int md_gets(BIO *h, char *str, int size);
23
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
24
static int md_new(BIO *h);
25
static int md_free(BIO *data);
26
static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
27
28
static const BIO_METHOD methods_md = {
29
    BIO_TYPE_MD,
30
    "message digest",
31
    bwrite_conv,
32
    md_write,
33
    bread_conv,
34
    md_read,
35
    NULL, /* md_puts, */
36
    md_gets,
37
    md_ctrl,
38
    md_new,
39
    md_free,
40
    md_callback_ctrl,
41
};
42
43
const BIO_METHOD *BIO_f_md(void)
44
6.90k
{
45
6.90k
    return &methods_md;
46
6.90k
}
47
48
static int md_new(BIO *bi)
49
6.90k
{
50
6.90k
    EVP_MD_CTX *ctx;
51
52
6.90k
    ctx = EVP_MD_CTX_new();
53
6.90k
    if (ctx == NULL)
54
0
        return 0;
55
56
6.90k
    BIO_set_init(bi, 1);
57
6.90k
    BIO_set_data(bi, ctx);
58
59
6.90k
    return 1;
60
6.90k
}
61
62
static int md_free(BIO *a)
63
6.90k
{
64
6.90k
    if (a == NULL)
65
0
        return 0;
66
6.90k
    EVP_MD_CTX_free(BIO_get_data(a));
67
6.90k
    BIO_set_data(a, NULL);
68
6.90k
    BIO_set_init(a, 0);
69
70
6.90k
    return 1;
71
6.90k
}
72
73
static int md_read(BIO *b, char *out, int outl)
74
30.8k
{
75
30.8k
    int ret = 0;
76
30.8k
    EVP_MD_CTX *ctx;
77
30.8k
    BIO *next;
78
79
30.8k
    if (out == NULL)
80
0
        return 0;
81
82
30.8k
    ctx = BIO_get_data(b);
83
30.8k
    next = BIO_next(b);
84
85
30.8k
    if ((ctx == NULL) || (next == NULL))
86
0
        return 0;
87
88
30.8k
    ret = BIO_read(next, out, outl);
89
30.8k
    if (BIO_get_init(b)) {
90
30.8k
        if (ret > 0) {
91
25.9k
            if (EVP_DigestUpdate(ctx, (unsigned char *)out,
92
25.9k
                    (unsigned int)ret)
93
25.9k
                <= 0)
94
0
                return -1;
95
25.9k
        }
96
30.8k
    }
97
30.8k
    BIO_clear_retry_flags(b);
98
30.8k
    BIO_copy_next_retry(b);
99
30.8k
    return ret;
100
30.8k
}
101
102
static int md_write(BIO *b, const char *in, int inl)
103
13.1k
{
104
13.1k
    int ret = 0;
105
13.1k
    EVP_MD_CTX *ctx;
106
13.1k
    BIO *next;
107
108
13.1k
    if ((in == NULL) || (inl <= 0))
109
0
        return 0;
110
111
13.1k
    ctx = BIO_get_data(b);
112
13.1k
    next = BIO_next(b);
113
13.1k
    if ((ctx != NULL) && (next != NULL))
114
13.1k
        ret = BIO_write(next, in, inl);
115
116
13.1k
    if (BIO_get_init(b)) {
117
13.1k
        if (ret > 0) {
118
13.1k
            if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
119
13.1k
                    (unsigned int)ret)) {
120
0
                BIO_clear_retry_flags(b);
121
0
                return 0;
122
0
            }
123
13.1k
        }
124
13.1k
    }
125
13.1k
    if (next != NULL) {
126
13.1k
        BIO_clear_retry_flags(b);
127
13.1k
        BIO_copy_next_retry(b);
128
13.1k
    }
129
13.1k
    return ret;
130
13.1k
}
131
132
static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
133
43.0k
{
134
43.0k
    EVP_MD_CTX *ctx, *dctx, **pctx;
135
43.0k
    const EVP_MD **ppmd;
136
43.0k
    EVP_MD *md;
137
43.0k
    long ret = 1;
138
43.0k
    BIO *dbio, *next;
139
140
43.0k
    ctx = BIO_get_data(b);
141
43.0k
    next = BIO_next(b);
142
143
43.0k
    switch (cmd) {
144
0
    case BIO_CTRL_RESET:
145
0
        if (BIO_get_init(b))
146
0
            ret = EVP_DigestInit_ex(ctx, EVP_MD_CTX_get0_md(ctx), NULL);
147
0
        else
148
0
            ret = 0;
149
0
        if (ret > 0)
150
0
            ret = BIO_ctrl(next, cmd, num, ptr);
151
0
        break;
152
0
    case BIO_C_GET_MD:
153
0
        if (BIO_get_init(b)) {
154
0
            ppmd = ptr;
155
0
            *ppmd = EVP_MD_CTX_get0_md(ctx);
156
0
        } else
157
0
            ret = 0;
158
0
        break;
159
7.19k
    case BIO_C_GET_MD_CTX:
160
7.19k
        pctx = ptr;
161
7.19k
        *pctx = ctx;
162
7.19k
        BIO_set_init(b, 1);
163
7.19k
        break;
164
0
    case BIO_C_SET_MD_CTX:
165
0
        if (BIO_get_init(b))
166
0
            BIO_set_data(b, ptr);
167
0
        else
168
0
            ret = 0;
169
0
        break;
170
9.74k
    case BIO_CTRL_EOF:
171
        /*
172
         * If there is no ctx or no next BIO, BIO_read() returns 0, which means
173
         * EOF, BIO_eof() should return 1 in this case.
174
         */
175
9.74k
        if (ctx == NULL || next == NULL)
176
0
            ret = 1;
177
9.74k
        else
178
9.74k
            ret = BIO_ctrl(next, cmd, num, ptr);
179
9.74k
        break;
180
1.94k
    case BIO_CTRL_FLUSH:
181
1.94k
    case BIO_C_DO_STATE_MACHINE:
182
1.94k
        BIO_clear_retry_flags(b);
183
1.94k
        ret = BIO_ctrl(next, cmd, num, ptr);
184
1.94k
        BIO_copy_next_retry(b);
185
1.94k
        break;
186
6.81k
    case BIO_C_SET_MD:
187
6.81k
        md = ptr;
188
6.81k
        ret = EVP_DigestInit_ex(ctx, md, NULL);
189
6.81k
        if (ret > 0)
190
6.81k
            BIO_set_init(b, 1);
191
6.81k
        break;
192
0
    case BIO_CTRL_DUP:
193
0
        dbio = ptr;
194
0
        dctx = BIO_get_data(dbio);
195
0
        if (!EVP_MD_CTX_copy_ex(dctx, ctx))
196
0
            return 0;
197
0
        BIO_set_init(b, 1);
198
0
        break;
199
17.4k
    default:
200
17.4k
        ret = BIO_ctrl(next, cmd, num, ptr);
201
17.4k
        break;
202
43.0k
    }
203
43.0k
    return ret;
204
43.0k
}
205
206
static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
207
0
{
208
0
    BIO *next;
209
210
0
    next = BIO_next(b);
211
212
0
    if (next == NULL)
213
0
        return 0;
214
215
0
    return BIO_callback_ctrl(next, cmd, fp);
216
0
}
217
218
static int md_gets(BIO *bp, char *buf, int size)
219
0
{
220
0
    EVP_MD_CTX *ctx;
221
0
    unsigned int ret;
222
223
0
    ctx = BIO_get_data(bp);
224
225
0
    if (size < EVP_MD_CTX_get_size(ctx))
226
0
        return 0;
227
228
0
    if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
229
0
        return -1;
230
231
0
    return (int)ret;
232
0
}