Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/evp/bio_md.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: bio_md.c,v 1.18 2022/01/14 08:40:57 tb Exp $ */
2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3
 * All rights reserved.
4
 *
5
 * This package is an SSL implementation written
6
 * by Eric Young (eay@cryptsoft.com).
7
 * The implementation was written so as to conform with Netscapes SSL.
8
 *
9
 * This library is free for commercial and non-commercial use as long as
10
 * the following conditions are aheared to.  The following conditions
11
 * apply to all code found in this distribution, be it the RC4, RSA,
12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13
 * included with this distribution is covered by the same copyright terms
14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15
 *
16
 * Copyright remains Eric Young's, and as such any Copyright notices in
17
 * the code are not to be removed.
18
 * If this package is used in a product, Eric Young should be given attribution
19
 * as the author of the parts of the library used.
20
 * This can be in the form of a textual message at program startup or
21
 * in documentation (online or textual) provided with the package.
22
 *
23
 * Redistribution and use in source and binary forms, with or without
24
 * modification, are permitted provided that the following conditions
25
 * are met:
26
 * 1. Redistributions of source code must retain the copyright
27
 *    notice, this list of conditions and the following disclaimer.
28
 * 2. Redistributions in binary form must reproduce the above copyright
29
 *    notice, this list of conditions and the following disclaimer in the
30
 *    documentation and/or other materials provided with the distribution.
31
 * 3. All advertising materials mentioning features or use of this software
32
 *    must display the following acknowledgement:
33
 *    "This product includes cryptographic software written by
34
 *     Eric Young (eay@cryptsoft.com)"
35
 *    The word 'cryptographic' can be left out if the rouines from the library
36
 *    being used are not cryptographic related :-).
37
 * 4. If you include any Windows specific code (or a derivative thereof) from
38
 *    the apps directory (application code) you must include an acknowledgement:
39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 * The licence and distribution terms for any publically available version or
54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55
 * copied and put under another distribution licence
56
 * [including the GNU Public Licence.]
57
 */
58
59
#include <stdio.h>
60
#include <errno.h>
61
62
#include <openssl/buffer.h>
63
#include <openssl/evp.h>
64
65
#include "bio_local.h"
66
#include "evp_locl.h"
67
68
/* BIO_put and BIO_get both add to the digest,
69
 * BIO_gets returns the digest */
70
71
static int md_write(BIO *h, char const *buf, int num);
72
static int md_read(BIO *h, char *buf, int size);
73
/*static int md_puts(BIO *h, const char *str); */
74
static int md_gets(BIO *h, char *str, int size);
75
static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
76
static int md_new(BIO *h);
77
static int md_free(BIO *data);
78
static long md_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
79
80
static const BIO_METHOD methods_md = {
81
  .type = BIO_TYPE_MD,
82
  .name = "message digest",
83
  .bwrite = md_write,
84
  .bread = md_read,
85
  .bgets = md_gets,
86
  .ctrl = md_ctrl,
87
  .create = md_new,
88
  .destroy = md_free,
89
  .callback_ctrl = md_callback_ctrl
90
};
91
92
const BIO_METHOD *
93
BIO_f_md(void)
94
0
{
95
0
  return (&methods_md);
96
0
}
97
98
static int
99
md_new(BIO *bi)
100
0
{
101
0
  EVP_MD_CTX *ctx;
102
103
0
  ctx = EVP_MD_CTX_create();
104
0
  if (ctx == NULL)
105
0
    return (0);
106
107
0
  bi->init = 0;
108
0
  bi->ptr = (char *)ctx;
109
0
  bi->flags = 0;
110
0
  return (1);
111
0
}
112
113
static int
114
md_free(BIO *a)
115
0
{
116
0
  if (a == NULL)
117
0
    return (0);
118
0
  EVP_MD_CTX_destroy(a->ptr);
119
0
  a->ptr = NULL;
120
0
  a->init = 0;
121
0
  a->flags = 0;
122
0
  return (1);
123
0
}
124
125
static int
126
md_read(BIO *b, char *out, int outl)
127
0
{
128
0
  int ret = 0;
129
0
  EVP_MD_CTX *ctx;
130
131
0
  if (out == NULL)
132
0
    return (0);
133
0
  ctx = b->ptr;
134
135
0
  if ((ctx == NULL) || (b->next_bio == NULL))
136
0
    return (0);
137
138
0
  ret = BIO_read(b->next_bio, out, outl);
139
0
  if (b->init) {
140
0
    if (ret > 0) {
141
0
      if (EVP_DigestUpdate(ctx, (unsigned char *)out,
142
0
          (unsigned int)ret) <= 0)
143
0
        return (-1);
144
0
    }
145
0
  }
146
0
  BIO_clear_retry_flags(b);
147
0
  BIO_copy_next_retry(b);
148
0
  return (ret);
149
0
}
150
151
static int
152
md_write(BIO *b, const char *in, int inl)
153
0
{
154
0
  int ret = 0;
155
0
  EVP_MD_CTX *ctx;
156
157
0
  if ((in == NULL) || (inl <= 0))
158
0
    return (0);
159
0
  ctx = b->ptr;
160
161
0
  if ((ctx != NULL) && (b->next_bio != NULL))
162
0
    ret = BIO_write(b->next_bio, in, inl);
163
0
  if (b->init) {
164
0
    if (ret > 0) {
165
0
      if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
166
0
          (unsigned int)ret)) {
167
0
        BIO_clear_retry_flags(b);
168
0
        return 0;
169
0
      }
170
0
    }
171
0
  }
172
0
  if (b->next_bio != NULL) {
173
0
    BIO_clear_retry_flags(b);
174
0
    BIO_copy_next_retry(b);
175
0
  }
176
0
  return (ret);
177
0
}
178
179
static long
180
md_ctrl(BIO *b, int cmd, long num, void *ptr)
181
0
{
182
0
  EVP_MD_CTX *ctx, *dctx, **pctx;
183
0
  const EVP_MD **ppmd;
184
0
  EVP_MD *md;
185
0
  long ret = 1;
186
0
  BIO *dbio;
187
188
0
  ctx = b->ptr;
189
190
0
  switch (cmd) {
191
0
  case BIO_CTRL_RESET:
192
0
    if (b->init)
193
0
      ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
194
0
    else
195
0
      ret = 0;
196
0
    if (ret > 0)
197
0
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
198
0
    break;
199
0
  case BIO_C_GET_MD:
200
0
    if (b->init) {
201
0
      ppmd = ptr;
202
0
      *ppmd = ctx->digest;
203
0
    } else
204
0
      ret = 0;
205
0
    break;
206
0
  case BIO_C_GET_MD_CTX:
207
0
    pctx = ptr;
208
0
    *pctx = ctx;
209
0
    b->init = 1;
210
0
    break;
211
0
  case BIO_C_SET_MD_CTX:
212
0
    if (b->init)
213
0
      b->ptr = ptr;
214
0
    else
215
0
      ret = 0;
216
0
    break;
217
0
  case BIO_C_DO_STATE_MACHINE:
218
0
    BIO_clear_retry_flags(b);
219
0
    ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
220
0
    BIO_copy_next_retry(b);
221
0
    break;
222
223
0
  case BIO_C_SET_MD:
224
0
    md = ptr;
225
0
    ret = EVP_DigestInit_ex(ctx, md, NULL);
226
0
    if (ret > 0)
227
0
      b->init = 1;
228
0
    break;
229
0
  case BIO_CTRL_DUP:
230
0
    dbio = ptr;
231
0
    dctx = dbio->ptr;
232
0
    if (!EVP_MD_CTX_copy_ex(dctx, ctx))
233
0
      return 0;
234
0
    b->init = 1;
235
0
    break;
236
0
  default:
237
0
    ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
238
0
    break;
239
0
  }
240
0
  return (ret);
241
0
}
242
243
static long
244
md_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
245
0
{
246
0
  long ret = 1;
247
248
0
  if (b->next_bio == NULL)
249
0
    return (0);
250
0
  switch (cmd) {
251
0
  default:
252
0
    ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
253
0
    break;
254
0
  }
255
0
  return (ret);
256
0
}
257
258
static int
259
md_gets(BIO *bp, char *buf, int size)
260
0
{
261
0
  EVP_MD_CTX *ctx;
262
0
  unsigned int ret;
263
264
0
  ctx = bp->ptr;
265
0
  if (size < ctx->digest->md_size)
266
0
    return (0);
267
0
  if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
268
0
    return -1;
269
270
0
  return ((int)ret);
271
0
}
272
273
/*
274
static int md_puts(bp,str)
275
BIO *bp;
276
char *str;
277
  {
278
  return(-1);
279
  }
280
*/