Coverage Report

Created: 2022-08-24 06:30

/src/libressl/crypto/bio/bf_buff.c
Line
Count
Source (jump to first uncovered line)
1
/* $OpenBSD: bf_buff.c,v 1.27 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 <errno.h>
60
#include <stdio.h>
61
#include <string.h>
62
63
#include <openssl/bio.h>
64
#include <openssl/err.h>
65
66
#include "bio_local.h"
67
68
static int buffer_write(BIO *h, const char *buf, int num);
69
static int buffer_read(BIO *h, char *buf, int size);
70
static int buffer_puts(BIO *h, const char *str);
71
static int buffer_gets(BIO *h, char *str, int size);
72
static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
73
static int buffer_new(BIO *h);
74
static int buffer_free(BIO *data);
75
static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
76
0
#define DEFAULT_BUFFER_SIZE 4096
77
78
static const BIO_METHOD methods_buffer = {
79
  .type = BIO_TYPE_BUFFER,
80
  .name = "buffer",
81
  .bwrite = buffer_write,
82
  .bread = buffer_read,
83
  .bputs = buffer_puts,
84
  .bgets = buffer_gets,
85
  .ctrl = buffer_ctrl,
86
  .create = buffer_new,
87
  .destroy = buffer_free,
88
  .callback_ctrl = buffer_callback_ctrl
89
};
90
91
const BIO_METHOD *
92
BIO_f_buffer(void)
93
0
{
94
0
  return (&methods_buffer);
95
0
}
96
97
static int
98
buffer_new(BIO *bi)
99
0
{
100
0
  BIO_F_BUFFER_CTX *ctx;
101
102
0
  ctx = malloc(sizeof(BIO_F_BUFFER_CTX));
103
0
  if (ctx == NULL)
104
0
    return (0);
105
0
  ctx->ibuf = malloc(DEFAULT_BUFFER_SIZE);
106
0
  if (ctx->ibuf == NULL) {
107
0
    free(ctx);
108
0
    return (0);
109
0
  }
110
0
  ctx->obuf = malloc(DEFAULT_BUFFER_SIZE);
111
0
  if (ctx->obuf == NULL) {
112
0
    free(ctx->ibuf);
113
0
    free(ctx);
114
0
    return (0);
115
0
  }
116
0
  ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
117
0
  ctx->obuf_size = DEFAULT_BUFFER_SIZE;
118
0
  ctx->ibuf_len = 0;
119
0
  ctx->ibuf_off = 0;
120
0
  ctx->obuf_len = 0;
121
0
  ctx->obuf_off = 0;
122
123
0
  bi->init = 1;
124
0
  bi->ptr = (char *)ctx;
125
0
  bi->flags = 0;
126
0
  return (1);
127
0
}
128
129
static int
130
buffer_free(BIO *a)
131
0
{
132
0
  BIO_F_BUFFER_CTX *b;
133
134
0
  if (a == NULL)
135
0
    return (0);
136
0
  b = (BIO_F_BUFFER_CTX *)a->ptr;
137
0
  free(b->ibuf);
138
0
  free(b->obuf);
139
0
  free(a->ptr);
140
0
  a->ptr = NULL;
141
0
  a->init = 0;
142
0
  a->flags = 0;
143
0
  return (1);
144
0
}
145
146
static int
147
buffer_read(BIO *b, char *out, int outl)
148
0
{
149
0
  int i, num = 0;
150
0
  BIO_F_BUFFER_CTX *ctx;
151
152
0
  if (out == NULL)
153
0
    return (0);
154
0
  ctx = (BIO_F_BUFFER_CTX *)b->ptr;
155
156
0
  if ((ctx == NULL) || (b->next_bio == NULL))
157
0
    return (0);
158
0
  num = 0;
159
0
  BIO_clear_retry_flags(b);
160
161
0
start:
162
0
  i = ctx->ibuf_len;
163
  /* If there is stuff left over, grab it */
164
0
  if (i != 0) {
165
0
    if (i > outl)
166
0
      i = outl;
167
0
    memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
168
0
    ctx->ibuf_off += i;
169
0
    ctx->ibuf_len -= i;
170
0
    num += i;
171
0
    if (outl == i)
172
0
      return (num);
173
0
    outl -= i;
174
0
    out += i;
175
0
  }
176
177
  /* We may have done a partial read. try to do more.
178
   * We have nothing in the buffer.
179
   * If we get an error and have read some data, just return it
180
   * and let them retry to get the error again.
181
   * copy direct to parent address space */
182
0
  if (outl > ctx->ibuf_size) {
183
0
    for (;;) {
184
0
      i = BIO_read(b->next_bio, out, outl);
185
0
      if (i <= 0) {
186
0
        BIO_copy_next_retry(b);
187
0
        if (i < 0)
188
0
          return ((num > 0) ? num : i);
189
0
        if (i == 0)
190
0
          return (num);
191
0
      }
192
0
      num += i;
193
0
      if (outl == i)
194
0
        return (num);
195
0
      out += i;
196
0
      outl -= i;
197
0
    }
198
0
  }
199
  /* else */
200
201
  /* we are going to be doing some buffering */
202
0
  i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
203
0
  if (i <= 0) {
204
0
    BIO_copy_next_retry(b);
205
0
    if (i < 0)
206
0
      return ((num > 0) ? num : i);
207
0
    if (i == 0)
208
0
      return (num);
209
0
  }
210
0
  ctx->ibuf_off = 0;
211
0
  ctx->ibuf_len = i;
212
213
  /* Lets re-read using ourselves :-) */
214
0
  goto start;
215
0
}
216
217
static int
218
buffer_write(BIO *b, const char *in, int inl)
219
0
{
220
0
  int i, num = 0;
221
0
  BIO_F_BUFFER_CTX *ctx;
222
223
0
  if ((in == NULL) || (inl <= 0))
224
0
    return (0);
225
0
  ctx = (BIO_F_BUFFER_CTX *)b->ptr;
226
0
  if ((ctx == NULL) || (b->next_bio == NULL))
227
0
    return (0);
228
229
0
  BIO_clear_retry_flags(b);
230
0
start:
231
0
  i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
232
  /* add to buffer and return */
233
0
  if (i >= inl) {
234
0
    memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
235
0
    ctx->obuf_len += inl;
236
0
    return (num + inl);
237
0
  }
238
  /* else */
239
  /* stuff already in buffer, so add to it first, then flush */
240
0
  if (ctx->obuf_len != 0) {
241
0
    if (i > 0) /* lets fill it up if we can */
242
0
    {
243
0
      memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
244
0
      in += i;
245
0
      inl -= i;
246
0
      num += i;
247
0
      ctx->obuf_len += i;
248
0
    }
249
    /* we now have a full buffer needing flushing */
250
0
    for (;;) {
251
0
      i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
252
0
          ctx->obuf_len);
253
0
      if (i <= 0) {
254
0
        BIO_copy_next_retry(b);
255
256
0
        if (i < 0)
257
0
          return ((num > 0) ? num : i);
258
0
        if (i == 0)
259
0
          return (num);
260
0
      }
261
0
      ctx->obuf_off += i;
262
0
      ctx->obuf_len -= i;
263
0
      if (ctx->obuf_len == 0)
264
0
        break;
265
0
    }
266
0
  }
267
  /* we only get here if the buffer has been flushed and we
268
   * still have stuff to write */
269
0
  ctx->obuf_off = 0;
270
271
  /* we now have inl bytes to write */
272
0
  while (inl >= ctx->obuf_size) {
273
0
    i = BIO_write(b->next_bio, in, inl);
274
0
    if (i <= 0) {
275
0
      BIO_copy_next_retry(b);
276
0
      if (i < 0)
277
0
        return ((num > 0) ? num : i);
278
0
      if (i == 0)
279
0
        return (num);
280
0
    }
281
0
    num += i;
282
0
    in += i;
283
0
    inl -= i;
284
0
    if (inl == 0)
285
0
      return (num);
286
0
  }
287
288
  /* copy the rest into the buffer since we have only a small
289
   * amount left */
290
0
  goto start;
291
0
}
292
293
static long
294
buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
295
0
{
296
0
  BIO *dbio;
297
0
  BIO_F_BUFFER_CTX *ctx;
298
0
  long ret = 1;
299
0
  char *p1, *p2;
300
0
  int r, i, *ip;
301
0
  int ibs, obs;
302
303
0
  ctx = (BIO_F_BUFFER_CTX *)b->ptr;
304
305
0
  switch (cmd) {
306
0
  case BIO_CTRL_RESET:
307
0
    ctx->ibuf_off = 0;
308
0
    ctx->ibuf_len = 0;
309
0
    ctx->obuf_off = 0;
310
0
    ctx->obuf_len = 0;
311
0
    if (b->next_bio == NULL)
312
0
      return (0);
313
0
    ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
314
0
    break;
315
0
  case BIO_CTRL_INFO:
316
0
    ret = (long)ctx->obuf_len;
317
0
    break;
318
0
  case BIO_C_GET_BUFF_NUM_LINES:
319
0
    ret = 0;
320
0
    p1 = ctx->ibuf;
321
0
    for (i = 0; i < ctx->ibuf_len; i++) {
322
0
      if (p1[ctx->ibuf_off + i] == '\n')
323
0
        ret++;
324
0
    }
325
0
    break;
326
0
  case BIO_CTRL_WPENDING:
327
0
    ret = (long)ctx->obuf_len;
328
0
    if (ret == 0) {
329
0
      if (b->next_bio == NULL)
330
0
        return (0);
331
0
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
332
0
    }
333
0
    break;
334
0
  case BIO_CTRL_PENDING:
335
0
    ret = (long)ctx->ibuf_len;
336
0
    if (ret == 0) {
337
0
      if (b->next_bio == NULL)
338
0
        return (0);
339
0
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
340
0
    }
341
0
    break;
342
0
  case BIO_C_SET_BUFF_READ_DATA:
343
0
    if (num > ctx->ibuf_size) {
344
0
      p1 = malloc(num);
345
0
      if (p1 == NULL)
346
0
        goto malloc_error;
347
0
      free(ctx->ibuf);
348
0
      ctx->ibuf = p1;
349
0
    }
350
0
    ctx->ibuf_off = 0;
351
0
    ctx->ibuf_len = (int)num;
352
0
    memcpy(ctx->ibuf, ptr, num);
353
0
    ret = 1;
354
0
    break;
355
0
  case BIO_C_SET_BUFF_SIZE:
356
0
    if (ptr != NULL) {
357
0
      ip = (int *)ptr;
358
0
      if (*ip == 0) {
359
0
        ibs = (int)num;
360
0
        obs = ctx->obuf_size;
361
0
      }
362
0
      else /* if (*ip == 1) */
363
0
      {
364
0
        ibs = ctx->ibuf_size;
365
0
        obs = (int)num;
366
0
      }
367
0
    } else {
368
0
      ibs = (int)num;
369
0
      obs = (int)num;
370
0
    }
371
0
    p1 = ctx->ibuf;
372
0
    p2 = ctx->obuf;
373
0
    if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
374
0
      p1 = malloc(num);
375
0
      if (p1 == NULL)
376
0
        goto malloc_error;
377
0
    }
378
0
    if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
379
0
      p2 = malloc(num);
380
0
      if (p2 == NULL) {
381
0
        if (p1 != ctx->ibuf)
382
0
          free(p1);
383
0
        goto malloc_error;
384
0
      }
385
0
    }
386
0
    if (ctx->ibuf != p1) {
387
0
      free(ctx->ibuf);
388
0
      ctx->ibuf = p1;
389
0
      ctx->ibuf_off = 0;
390
0
      ctx->ibuf_len = 0;
391
0
      ctx->ibuf_size = ibs;
392
0
    }
393
0
    if (ctx->obuf != p2) {
394
0
      free(ctx->obuf);
395
0
      ctx->obuf = p2;
396
0
      ctx->obuf_off = 0;
397
0
      ctx->obuf_len = 0;
398
0
      ctx->obuf_size = obs;
399
0
    }
400
0
    break;
401
0
  case BIO_C_DO_STATE_MACHINE:
402
0
    if (b->next_bio == NULL)
403
0
      return (0);
404
0
    BIO_clear_retry_flags(b);
405
0
    ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
406
0
    BIO_copy_next_retry(b);
407
0
    break;
408
409
0
  case BIO_CTRL_FLUSH:
410
0
    if (b->next_bio == NULL)
411
0
      return (0);
412
0
    if (ctx->obuf_len <= 0) {
413
0
      ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
414
0
      break;
415
0
    }
416
417
0
    for (;;) {
418
0
      BIO_clear_retry_flags(b);
419
0
      if (ctx->obuf_len > 0) {
420
0
        r = BIO_write(b->next_bio,
421
0
            &(ctx->obuf[ctx->obuf_off]),
422
0
            ctx->obuf_len);
423
0
        BIO_copy_next_retry(b);
424
0
        if (r <= 0)
425
0
          return ((long)r);
426
0
        ctx->obuf_off += r;
427
0
        ctx->obuf_len -= r;
428
0
      } else {
429
0
        ctx->obuf_len = 0;
430
0
        ctx->obuf_off = 0;
431
0
        break;
432
0
      }
433
0
    }
434
0
    ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
435
0
    break;
436
0
  case BIO_CTRL_DUP:
437
0
    dbio = (BIO *)ptr;
438
0
    if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
439
0
        !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
440
0
      ret = 0;
441
0
    break;
442
0
  default:
443
0
    if (b->next_bio == NULL)
444
0
      return (0);
445
0
    ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
446
0
    break;
447
0
  }
448
0
  return (ret);
449
0
malloc_error:
450
0
  BIOerror(ERR_R_MALLOC_FAILURE);
451
0
  return (0);
452
0
}
453
454
static long
455
buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
456
0
{
457
0
  long ret = 1;
458
459
0
  if (b->next_bio == NULL)
460
0
    return (0);
461
0
  switch (cmd) {
462
0
  default:
463
0
    ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
464
0
    break;
465
0
  }
466
0
  return (ret);
467
0
}
468
469
static int
470
buffer_gets(BIO *b, char *buf, int size)
471
0
{
472
0
  BIO_F_BUFFER_CTX *ctx;
473
0
  int num = 0, i, flag;
474
0
  char *p;
475
476
0
  ctx = (BIO_F_BUFFER_CTX *)b->ptr;
477
0
  size--; /* reserve space for a '\0' */
478
0
  BIO_clear_retry_flags(b);
479
480
0
  for (;;) {
481
0
    if (ctx->ibuf_len > 0) {
482
0
      p = &(ctx->ibuf[ctx->ibuf_off]);
483
0
      flag = 0;
484
0
      for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
485
0
        *(buf++) = p[i];
486
0
        if (p[i] == '\n') {
487
0
          flag = 1;
488
0
          i++;
489
0
          break;
490
0
        }
491
0
      }
492
0
      num += i;
493
0
      size -= i;
494
0
      ctx->ibuf_len -= i;
495
0
      ctx->ibuf_off += i;
496
0
      if (flag || size == 0) {
497
0
        *buf = '\0';
498
0
        return (num);
499
0
      }
500
0
    }
501
0
    else  /* read another chunk */
502
0
    {
503
0
      i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
504
0
      if (i <= 0) {
505
0
        BIO_copy_next_retry(b);
506
0
        *buf = '\0';
507
0
        if (i < 0)
508
0
          return ((num > 0) ? num : i);
509
0
        if (i == 0)
510
0
          return (num);
511
0
      }
512
0
      ctx->ibuf_len = i;
513
0
      ctx->ibuf_off = 0;
514
0
    }
515
0
  }
516
0
}
517
518
static int
519
buffer_puts(BIO *b, const char *str)
520
0
{
521
0
  return (buffer_write(b, str, strlen(str)));
522
0
}