Coverage Report

Created: 2025-03-09 06:52

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