Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/evp/bio_md.c
Line
Count
Source
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
#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
2.36k
{
45
2.36k
    return &methods_md;
46
2.36k
}
47
48
static int md_new(BIO *bi)
49
2.36k
{
50
2.36k
    EVP_MD_CTX *ctx;
51
52
2.36k
    ctx = EVP_MD_CTX_new();
53
2.36k
    if (ctx == NULL)
54
0
        return 0;
55
56
2.36k
    BIO_set_init(bi, 1);
57
2.36k
    BIO_set_data(bi, ctx);
58
59
2.36k
    return 1;
60
2.36k
}
61
62
static int md_free(BIO *a)
63
2.36k
{
64
2.36k
    if (a == NULL)
65
0
        return 0;
66
2.36k
    EVP_MD_CTX_free(BIO_get_data(a));
67
2.36k
    BIO_set_data(a, NULL);
68
2.36k
    BIO_set_init(a, 0);
69
70
2.36k
    return 1;
71
2.36k
}
72
73
static int md_read(BIO *b, char *out, int outl)
74
14.2k
{
75
14.2k
    int ret = 0;
76
14.2k
    EVP_MD_CTX *ctx;
77
14.2k
    BIO *next;
78
79
14.2k
    if (out == NULL)
80
0
        return 0;
81
82
14.2k
    ctx = BIO_get_data(b);
83
14.2k
    next = BIO_next(b);
84
85
14.2k
    if ((ctx == NULL) || (next == NULL))
86
0
        return 0;
87
88
14.2k
    ret = BIO_read(next, out, outl);
89
14.2k
    if (BIO_get_init(b)) {
90
14.2k
        if (ret > 0) {
91
11.9k
            if (EVP_DigestUpdate(ctx, (unsigned char *)out,
92
11.9k
                    (unsigned int)ret)
93
11.9k
                <= 0)
94
0
                return -1;
95
11.9k
        }
96
14.2k
    }
97
14.2k
    BIO_clear_retry_flags(b);
98
14.2k
    BIO_copy_next_retry(b);
99
14.2k
    return ret;
100
14.2k
}
101
102
static int md_write(BIO *b, const char *in, int inl)
103
0
{
104
0
    int ret = 0;
105
0
    EVP_MD_CTX *ctx;
106
0
    BIO *next;
107
108
0
    if ((in == NULL) || (inl <= 0))
109
0
        return 0;
110
111
0
    ctx = BIO_get_data(b);
112
0
    next = BIO_next(b);
113
0
    if ((ctx != NULL) && (next != NULL))
114
0
        ret = BIO_write(next, in, inl);
115
116
0
    if (BIO_get_init(b)) {
117
0
        if (ret > 0) {
118
0
            if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
119
0
                    (unsigned int)ret)) {
120
0
                BIO_clear_retry_flags(b);
121
0
                return 0;
122
0
            }
123
0
        }
124
0
    }
125
0
    if (next != NULL) {
126
0
        BIO_clear_retry_flags(b);
127
0
        BIO_copy_next_retry(b);
128
0
    }
129
0
    return ret;
130
0
}
131
132
static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
133
{
134
    EVP_MD_CTX *ctx, *dctx, **pctx;
135
    const EVP_MD **ppmd;
136
    EVP_MD *md;
137
    long ret = 1;
138
    BIO *dbio, *next;
139
140
    ctx = BIO_get_data(b);
141
    next = BIO_next(b);
142
143
    switch (cmd) {
144
    case BIO_CTRL_RESET:
145
        if (BIO_get_init(b))
146
            ret = EVP_DigestInit_ex(ctx, EVP_MD_CTX_get0_md(ctx), NULL);
147
        else
148
            ret = 0;
149
        if (ret > 0)
150
            ret = BIO_ctrl(next, cmd, num, ptr);
151
        break;
152
    case BIO_C_GET_MD:
153
        if (BIO_get_init(b)) {
154
            ppmd = ptr;
155
            *ppmd = EVP_MD_CTX_get0_md(ctx);
156
        } else
157
            ret = 0;
158
        break;
159
    case BIO_C_GET_MD_CTX:
160
        pctx = ptr;
161
        *pctx = ctx;
162
        BIO_set_init(b, 1);
163
        break;
164
    case BIO_C_SET_MD_CTX:
165
        if (BIO_get_init(b))
166
            BIO_set_data(b, ptr);
167
        else
168
            ret = 0;
169
        break;
170
    case BIO_C_DO_STATE_MACHINE:
171
        BIO_clear_retry_flags(b);
172
        ret = BIO_ctrl(next, cmd, num, ptr);
173
        BIO_copy_next_retry(b);
174
        break;
175
176
    case BIO_C_SET_MD:
177
        md = ptr;
178
        ret = EVP_DigestInit_ex(ctx, md, NULL);
179
        if (ret > 0)
180
            BIO_set_init(b, 1);
181
        break;
182
    case BIO_CTRL_DUP:
183
        dbio = ptr;
184
        dctx = BIO_get_data(dbio);
185
        if (!EVP_MD_CTX_copy_ex(dctx, ctx))
186
            return 0;
187
        BIO_set_init(b, 1);
188
        break;
189
    default:
190
        ret = BIO_ctrl(next, cmd, num, ptr);
191
        break;
192
    }
193
    return ret;
194
}
195
196
static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
197
0
{
198
0
    BIO *next;
199
200
0
    next = BIO_next(b);
201
202
0
    if (next == NULL)
203
0
        return 0;
204
205
0
    return BIO_callback_ctrl(next, cmd, fp);
206
0
}
207
208
static int md_gets(BIO *bp, char *buf, int size)
209
0
{
210
0
    EVP_MD_CTX *ctx;
211
0
    unsigned int ret;
212
213
0
    ctx = BIO_get_data(bp);
214
215
0
    if (size < EVP_MD_CTX_get_size(ctx))
216
0
        return 0;
217
218
0
    if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
219
0
        return -1;
220
221
0
    return (int)ret;
222
0
}