bio_md.c 5.0 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
8 9 10 11
 */

#include <stdio.h>
#include <errno.h>
12
#include "internal/cryptlib.h"
13 14
#include <openssl/buffer.h>
#include <openssl/evp.h>
15
#include "crypto/evp.h"
16
#include "evp_local.h"
M
Matt Caswell 已提交
17
#include "internal/bio.h"
18

19 20 21
/*
 * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
 */
22

23 24 25 26
static int md_write(BIO *h, char const *buf, int num);
static int md_read(BIO *h, char *buf, int size);
static int md_gets(BIO *h, char *str, int size);
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
27 28
static int md_new(BIO *h);
static int md_free(BIO *data);
B
Bernd Edlinger 已提交
29
static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
30

31
static const BIO_METHOD methods_md = {
32 33
    BIO_TYPE_MD,
    "message digest",
34 35
    /* TODO: Convert to new style write function */
    bwrite_conv,
36
    md_write,
37 38
    /* TODO: Convert to new style read function */
    bread_conv,
39 40 41 42 43 44 45 46
    md_read,
    NULL,                       /* md_puts, */
    md_gets,
    md_ctrl,
    md_new,
    md_free,
    md_callback_ctrl,
};
47

48
const BIO_METHOD *BIO_f_md(void)
49
{
K
KaoruToda 已提交
50
    return &methods_md;
51
}
52

U
Ulf Möller 已提交
53
static int md_new(BIO *bi)
54 55
{
    EVP_MD_CTX *ctx;
56

57
    ctx = EVP_MD_CTX_new();
58
    if (ctx == NULL)
K
KaoruToda 已提交
59
        return 0;
60

M
Matt Caswell 已提交
61 62 63 64
    BIO_set_init(bi, 1);
    BIO_set_data(bi, ctx);

    return 1;
65
}
66

U
Ulf Möller 已提交
67
static int md_free(BIO *a)
68 69
{
    if (a == NULL)
K
KaoruToda 已提交
70
        return 0;
M
Matt Caswell 已提交
71 72 73 74 75
    EVP_MD_CTX_free(BIO_get_data(a));
    BIO_set_data(a, NULL);
    BIO_set_init(a, 0);

    return 1;
76 77
}

U
Ulf Möller 已提交
78
static int md_read(BIO *b, char *out, int outl)
79 80 81
{
    int ret = 0;
    EVP_MD_CTX *ctx;
M
Matt Caswell 已提交
82
    BIO *next;
83

84
    if (out == NULL)
K
KaoruToda 已提交
85
        return 0;
86

M
Matt Caswell 已提交
87 88 89 90
    ctx = BIO_get_data(b);
    next = BIO_next(b);

    if ((ctx == NULL) || (next == NULL))
K
KaoruToda 已提交
91
        return 0;
92

M
Matt Caswell 已提交
93 94
    ret = BIO_read(next, out, outl);
    if (BIO_get_init(b)) {
95 96 97
        if (ret > 0) {
            if (EVP_DigestUpdate(ctx, (unsigned char *)out,
                                 (unsigned int)ret) <= 0)
K
KaoruToda 已提交
98
                return -1;
99 100 101 102
        }
    }
    BIO_clear_retry_flags(b);
    BIO_copy_next_retry(b);
K
KaoruToda 已提交
103
    return ret;
104
}
105

106
static int md_write(BIO *b, const char *in, int inl)
107 108 109
{
    int ret = 0;
    EVP_MD_CTX *ctx;
M
Matt Caswell 已提交
110
    BIO *next;
111

112
    if ((in == NULL) || (inl <= 0))
M
Matt Caswell 已提交
113
        return 0;
114

M
Matt Caswell 已提交
115 116 117 118 119 120
    ctx = BIO_get_data(b);
    next = BIO_next(b);
    if ((ctx != NULL) && (next != NULL))
        ret = BIO_write(next, in, inl);

    if (BIO_get_init(b)) {
121 122 123 124 125 126 127 128
        if (ret > 0) {
            if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
                                  (unsigned int)ret)) {
                BIO_clear_retry_flags(b);
                return 0;
            }
        }
    }
M
Matt Caswell 已提交
129
    if (next != NULL) {
130 131 132
        BIO_clear_retry_flags(b);
        BIO_copy_next_retry(b);
    }
M
Matt Caswell 已提交
133
    return ret;
134
}
135

136
static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
137 138 139 140 141
{
    EVP_MD_CTX *ctx, *dctx, **pctx;
    const EVP_MD **ppmd;
    EVP_MD *md;
    long ret = 1;
M
Matt Caswell 已提交
142
    BIO *dbio, *next;
143

M
Matt Caswell 已提交
144 145 146

    ctx = BIO_get_data(b);
    next = BIO_next(b);
147

148 149
    switch (cmd) {
    case BIO_CTRL_RESET:
M
Matt Caswell 已提交
150
        if (BIO_get_init(b))
151 152 153 154
            ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
        else
            ret = 0;
        if (ret > 0)
M
Matt Caswell 已提交
155
            ret = BIO_ctrl(next, cmd, num, ptr);
156 157
        break;
    case BIO_C_GET_MD:
M
Matt Caswell 已提交
158
        if (BIO_get_init(b)) {
159 160 161 162 163 164 165 166
            ppmd = ptr;
            *ppmd = ctx->digest;
        } else
            ret = 0;
        break;
    case BIO_C_GET_MD_CTX:
        pctx = ptr;
        *pctx = ctx;
M
Matt Caswell 已提交
167
        BIO_set_init(b, 1);
168 169
        break;
    case BIO_C_SET_MD_CTX:
M
Matt Caswell 已提交
170 171
        if (BIO_get_init(b))
            BIO_set_data(b, ptr);
172 173 174 175 176
        else
            ret = 0;
        break;
    case BIO_C_DO_STATE_MACHINE:
        BIO_clear_retry_flags(b);
M
Matt Caswell 已提交
177
        ret = BIO_ctrl(next, cmd, num, ptr);
178 179
        BIO_copy_next_retry(b);
        break;
180

181 182 183 184
    case BIO_C_SET_MD:
        md = ptr;
        ret = EVP_DigestInit_ex(ctx, md, NULL);
        if (ret > 0)
M
Matt Caswell 已提交
185
            BIO_set_init(b, 1);
186 187 188
        break;
    case BIO_CTRL_DUP:
        dbio = ptr;
M
Matt Caswell 已提交
189
        dctx = BIO_get_data(dbio);
190 191
        if (!EVP_MD_CTX_copy_ex(dctx, ctx))
            return 0;
M
Matt Caswell 已提交
192
        BIO_set_init(b, 1);
193 194
        break;
    default:
M
Matt Caswell 已提交
195
        ret = BIO_ctrl(next, cmd, num, ptr);
196 197
        break;
    }
K
KaoruToda 已提交
198
    return ret;
199
}
200

B
Bernd Edlinger 已提交
201
static long md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
202 203
{
    long ret = 1;
M
Matt Caswell 已提交
204 205 206 207 208 209
    BIO *next;

    next = BIO_next(b);

    if (next == NULL)
        return 0;
210

211 212
    switch (cmd) {
    default:
M
Matt Caswell 已提交
213
        ret = BIO_callback_ctrl(next, cmd, fp);
214 215
        break;
    }
K
KaoruToda 已提交
216
    return ret;
217
}
218

U
Ulf Möller 已提交
219
static int md_gets(BIO *bp, char *buf, int size)
220 221 222
{
    EVP_MD_CTX *ctx;
    unsigned int ret;
223

M
Matt Caswell 已提交
224 225
    ctx = BIO_get_data(bp);

226
    if (size < ctx->digest->md_size)
M
Matt Caswell 已提交
227 228
        return 0;

229 230
    if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
        return -1;
231

K
KaoruToda 已提交
232
    return (int)ret;
233
}