Coverage Report

Created: 2025-06-11 06:40

/src/boringssl/crypto/bio/bio.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/bio.h>
16
17
#include <assert.h>
18
#include <errno.h>
19
#include <limits.h>
20
#include <string.h>
21
22
#include <openssl/asn1.h>
23
#include <openssl/err.h>
24
#include <openssl/mem.h>
25
26
#include "../internal.h"
27
#include "internal.h"
28
29
30
static CRYPTO_EX_DATA_CLASS g_ex_data_class =
31
    CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
32
33
0
BIO *BIO_new(const BIO_METHOD *method) {
34
0
  BIO *ret = reinterpret_cast<BIO *>(OPENSSL_zalloc(sizeof(BIO)));
35
0
  if (ret == NULL) {
36
0
    return NULL;
37
0
  }
38
39
0
  ret->method = method;
40
0
  ret->shutdown = 1;
41
0
  ret->references = 1;
42
0
  CRYPTO_new_ex_data(&ret->ex_data);
43
44
0
  if (method->create != NULL && !method->create(ret)) {
45
0
    OPENSSL_free(ret);
46
0
    return NULL;
47
0
  }
48
49
0
  return ret;
50
0
}
51
52
0
int BIO_free(BIO *bio) {
53
0
  BIO *next_bio;
54
55
0
  for (; bio != NULL; bio = next_bio) {
56
0
    if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) {
57
0
      return 0;
58
0
    }
59
60
0
    next_bio = BIO_pop(bio);
61
62
0
    if (bio->method != NULL && bio->method->destroy != NULL) {
63
0
      bio->method->destroy(bio);
64
0
    }
65
66
0
    CRYPTO_free_ex_data(&g_ex_data_class, &bio->ex_data);
67
0
    OPENSSL_free(bio);
68
0
  }
69
0
  return 1;
70
0
}
71
72
0
int BIO_up_ref(BIO *bio) {
73
0
  CRYPTO_refcount_inc(&bio->references);
74
0
  return 1;
75
0
}
76
77
0
void BIO_vfree(BIO *bio) { BIO_free(bio); }
78
79
0
void BIO_free_all(BIO *bio) { BIO_free(bio); }
80
81
0
int BIO_read(BIO *bio, void *buf, int len) {
82
0
  if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) {
83
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
84
0
    return -2;
85
0
  }
86
0
  if (!bio->init) {
87
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
88
0
    return -2;
89
0
  }
90
0
  if (len <= 0) {
91
0
    return 0;
92
0
  }
93
0
  int ret = bio->method->bread(bio, reinterpret_cast<char *>(buf), len);
94
0
  if (ret > 0) {
95
0
    bio->num_read += ret;
96
0
  }
97
0
  return ret;
98
0
}
99
100
0
int BIO_gets(BIO *bio, char *buf, int len) {
101
0
  if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) {
102
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
103
0
    return -2;
104
0
  }
105
0
  if (!bio->init) {
106
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
107
0
    return -2;
108
0
  }
109
0
  if (len <= 0) {
110
0
    return 0;
111
0
  }
112
0
  int ret = bio->method->bgets(bio, buf, len);
113
0
  if (ret > 0) {
114
0
    bio->num_read += ret;
115
0
  }
116
0
  return ret;
117
0
}
118
119
0
int BIO_write(BIO *bio, const void *in, int inl) {
120
0
  if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) {
121
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
122
0
    return -2;
123
0
  }
124
0
  if (!bio->init) {
125
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
126
0
    return -2;
127
0
  }
128
0
  if (inl <= 0) {
129
0
    return 0;
130
0
  }
131
0
  int ret = bio->method->bwrite(bio, reinterpret_cast<const char *>(in), inl);
132
0
  if (ret > 0) {
133
0
    bio->num_write += ret;
134
0
  }
135
0
  return ret;
136
0
}
137
138
0
int BIO_write_all(BIO *bio, const void *data, size_t len) {
139
0
  const uint8_t *data_u8 = reinterpret_cast<const uint8_t *>(data);
140
0
  while (len > 0) {
141
0
    int ret = BIO_write(bio, data_u8, len > INT_MAX ? INT_MAX : (int)len);
142
0
    if (ret <= 0) {
143
0
      return 0;
144
0
    }
145
0
    data_u8 += ret;
146
0
    len -= ret;
147
0
  }
148
0
  return 1;
149
0
}
150
151
0
int BIO_puts(BIO *bio, const char *in) {
152
0
  size_t len = strlen(in);
153
0
  if (len > INT_MAX) {
154
    // |BIO_write| and the return value both assume the string fits in |int|.
155
0
    OPENSSL_PUT_ERROR(BIO, ERR_R_OVERFLOW);
156
0
    return -1;
157
0
  }
158
0
  return BIO_write(bio, in, (int)len);
159
0
}
160
161
0
int BIO_flush(BIO *bio) { return (int)BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL); }
162
163
0
long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
164
0
  if (bio == NULL) {
165
0
    return 0;
166
0
  }
167
168
0
  if (bio->method == NULL || bio->method->ctrl == NULL) {
169
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
170
0
    return -2;
171
0
  }
172
173
0
  return bio->method->ctrl(bio, cmd, larg, parg);
174
0
}
175
176
0
char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
177
0
  char *p = NULL;
178
179
0
  if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
180
0
    return NULL;
181
0
  }
182
183
0
  return p;
184
0
}
185
186
0
long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
187
0
  int i = iarg;
188
189
0
  return BIO_ctrl(b, cmd, larg, (void *)&i);
190
0
}
191
192
0
int BIO_reset(BIO *bio) { return (int)BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL); }
193
194
0
int BIO_eof(BIO *bio) { return (int)BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL); }
195
196
0
void BIO_set_flags(BIO *bio, int flags) { bio->flags |= flags; }
197
198
0
int BIO_test_flags(const BIO *bio, int flags) { return bio->flags & flags; }
199
200
0
int BIO_should_read(const BIO *bio) {
201
0
  return BIO_test_flags(bio, BIO_FLAGS_READ);
202
0
}
203
204
0
int BIO_should_write(const BIO *bio) {
205
0
  return BIO_test_flags(bio, BIO_FLAGS_WRITE);
206
0
}
207
208
0
int BIO_should_retry(const BIO *bio) {
209
0
  return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
210
0
}
211
212
0
int BIO_should_io_special(const BIO *bio) {
213
0
  return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
214
0
}
215
216
0
int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
217
218
0
void BIO_set_retry_reason(BIO *bio, int reason) { bio->retry_reason = reason; }
219
220
0
void BIO_clear_flags(BIO *bio, int flags) { bio->flags &= ~flags; }
221
222
0
void BIO_set_retry_read(BIO *bio) {
223
0
  bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
224
0
}
225
226
0
void BIO_set_retry_write(BIO *bio) {
227
0
  bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
228
0
}
229
230
static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
231
232
0
int BIO_get_retry_flags(BIO *bio) { return bio->flags & kRetryFlags; }
233
234
0
void BIO_clear_retry_flags(BIO *bio) {
235
0
  bio->flags &= ~kRetryFlags;
236
0
  bio->retry_reason = 0;
237
0
}
238
239
0
int BIO_method_type(const BIO *bio) { return bio->method->type; }
240
241
0
void BIO_copy_next_retry(BIO *bio) {
242
0
  BIO_clear_retry_flags(bio);
243
0
  BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
244
0
  bio->retry_reason = bio->next_bio->retry_reason;
245
0
}
246
247
0
long BIO_callback_ctrl(BIO *bio, int cmd, BIO_info_cb *fp) {
248
0
  if (bio == NULL) {
249
0
    return 0;
250
0
  }
251
252
0
  if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
253
0
    OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
254
0
    return 0;
255
0
  }
256
257
0
  return bio->method->callback_ctrl(bio, cmd, fp);
258
0
}
259
260
0
size_t BIO_pending(const BIO *bio) {
261
0
  const long r = BIO_ctrl((BIO *)bio, BIO_CTRL_PENDING, 0, NULL);
262
0
  assert(r >= 0);
263
264
0
  if (r < 0) {
265
0
    return 0;
266
0
  }
267
0
  return r;
268
0
}
269
270
0
size_t BIO_ctrl_pending(const BIO *bio) { return BIO_pending(bio); }
271
272
0
size_t BIO_wpending(const BIO *bio) {
273
0
  const long r = BIO_ctrl((BIO *)bio, BIO_CTRL_WPENDING, 0, NULL);
274
0
  assert(r >= 0);
275
276
0
  if (r < 0) {
277
0
    return 0;
278
0
  }
279
0
  return r;
280
0
}
281
282
0
int BIO_set_close(BIO *bio, int close_flag) {
283
0
  return (int)BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
284
0
}
285
286
0
OPENSSL_EXPORT uint64_t BIO_number_read(const BIO *bio) {
287
0
  return bio->num_read;
288
0
}
289
290
0
OPENSSL_EXPORT uint64_t BIO_number_written(const BIO *bio) {
291
0
  return bio->num_write;
292
0
}
293
294
0
BIO *BIO_push(BIO *bio, BIO *appended_bio) {
295
0
  BIO *last_bio;
296
297
0
  if (bio == NULL) {
298
0
    return bio;
299
0
  }
300
301
0
  last_bio = bio;
302
0
  while (last_bio->next_bio != NULL) {
303
0
    last_bio = last_bio->next_bio;
304
0
  }
305
306
0
  last_bio->next_bio = appended_bio;
307
0
  return bio;
308
0
}
309
310
0
BIO *BIO_pop(BIO *bio) {
311
0
  BIO *ret;
312
313
0
  if (bio == NULL) {
314
0
    return NULL;
315
0
  }
316
0
  ret = bio->next_bio;
317
0
  bio->next_bio = NULL;
318
0
  return ret;
319
0
}
320
321
0
BIO *BIO_next(BIO *bio) {
322
0
  if (!bio) {
323
0
    return NULL;
324
0
  }
325
0
  return bio->next_bio;
326
0
}
327
328
0
BIO *BIO_find_type(BIO *bio, int type) {
329
0
  int method_type, mask;
330
331
0
  if (!bio) {
332
0
    return NULL;
333
0
  }
334
0
  mask = type & 0xff;
335
336
0
  do {
337
0
    if (bio->method != NULL) {
338
0
      method_type = bio->method->type;
339
340
0
      if (!mask) {
341
0
        if (method_type & type) {
342
0
          return bio;
343
0
        }
344
0
      } else if (method_type == type) {
345
0
        return bio;
346
0
      }
347
0
    }
348
0
    bio = bio->next_bio;
349
0
  } while (bio != NULL);
350
351
0
  return NULL;
352
0
}
353
354
0
int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
355
0
  if (indent > max_indent) {
356
0
    indent = max_indent;
357
0
  }
358
359
0
  while (indent--) {
360
0
    if (BIO_puts(bio, " ") != 1) {
361
0
      return 0;
362
0
    }
363
0
  }
364
0
  return 1;
365
0
}
366
367
0
static int print_bio(const char *str, size_t len, void *bio) {
368
0
  return BIO_write_all((BIO *)bio, str, len);
369
0
}
370
371
0
void ERR_print_errors(BIO *bio) { ERR_print_errors_cb(print_bio, bio); }
372
373
// bio_read_all reads everything from |bio| and prepends |prefix| to it. On
374
// success, |*out| is set to an allocated buffer (which should be freed with
375
// |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
376
// buffer will contain |prefix| followed by the contents of |bio|. On failure,
377
// zero is returned.
378
//
379
// The function will fail if the size of the output would equal or exceed
380
// |max_len|.
381
static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
382
                        const uint8_t *prefix, size_t prefix_len,
383
0
                        size_t max_len) {
384
0
  static const size_t kChunkSize = 4096;
385
386
0
  size_t len = prefix_len + kChunkSize;
387
0
  if (len > max_len) {
388
0
    len = max_len;
389
0
  }
390
0
  if (len < prefix_len) {
391
0
    return 0;
392
0
  }
393
0
  *out = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len));
394
0
  if (*out == NULL) {
395
0
    return 0;
396
0
  }
397
0
  OPENSSL_memcpy(*out, prefix, prefix_len);
398
0
  size_t done = prefix_len;
399
400
0
  for (;;) {
401
0
    if (done == len) {
402
0
      OPENSSL_free(*out);
403
0
      return 0;
404
0
    }
405
0
    size_t todo = len - done;
406
0
    if (todo > INT_MAX) {
407
0
      todo = INT_MAX;
408
0
    }
409
0
    const int n = BIO_read(bio, *out + done, (int)todo);
410
0
    if (n == 0) {
411
0
      *out_len = done;
412
0
      return 1;
413
0
    } else if (n == -1) {
414
0
      OPENSSL_free(*out);
415
0
      return 0;
416
0
    }
417
418
0
    done += n;
419
0
    if (len < max_len && len - done < kChunkSize / 2) {
420
0
      len += kChunkSize;
421
0
      if (len < kChunkSize || len > max_len) {
422
0
        len = max_len;
423
0
      }
424
0
      uint8_t *new_buf =
425
0
          reinterpret_cast<uint8_t *>(OPENSSL_realloc(*out, len));
426
0
      if (new_buf == NULL) {
427
0
        OPENSSL_free(*out);
428
0
        return 0;
429
0
      }
430
0
      *out = new_buf;
431
0
    }
432
0
  }
433
0
}
434
435
// bio_read_full reads |len| bytes |bio| and writes them into |out|. It
436
// tolerates partial reads from |bio| and returns one on success or zero if a
437
// read fails before |len| bytes are read. On failure, it additionally sets
438
// |*out_eof_on_first_read| to whether the error was due to |bio| returning zero
439
// on the first read. |out_eof_on_first_read| may be NULL to discard the value.
440
static int bio_read_full(BIO *bio, uint8_t *out, int *out_eof_on_first_read,
441
0
                         size_t len) {
442
0
  int first_read = 1;
443
0
  while (len > 0) {
444
0
    int todo = len <= INT_MAX ? (int)len : INT_MAX;
445
0
    int ret = BIO_read(bio, out, todo);
446
0
    if (ret <= 0) {
447
0
      if (out_eof_on_first_read != NULL) {
448
0
        *out_eof_on_first_read = first_read && ret == 0;
449
0
      }
450
0
      return 0;
451
0
    }
452
0
    out += ret;
453
0
    len -= (size_t)ret;
454
0
    first_read = 0;
455
0
  }
456
457
0
  return 1;
458
0
}
459
460
// For compatibility with existing |d2i_*_bio| callers, |BIO_read_asn1| uses
461
// |ERR_LIB_ASN1| errors.
462
OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_DECODE_ERROR)
463
OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_HEADER_TOO_LONG)
464
OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_NOT_ENOUGH_DATA)
465
OPENSSL_DECLARE_ERROR_REASON(ASN1, ASN1_R_TOO_LONG)
466
467
0
int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
468
0
  uint8_t header[6];
469
470
0
  static const size_t kInitialHeaderLen = 2;
471
0
  int eof_on_first_read;
472
0
  if (!bio_read_full(bio, header, &eof_on_first_read, kInitialHeaderLen)) {
473
0
    if (eof_on_first_read) {
474
      // Historically, OpenSSL returned |ASN1_R_HEADER_TOO_LONG| when
475
      // |d2i_*_bio| could not read anything. CPython conditions on this to
476
      // determine if |bio| was empty.
477
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_HEADER_TOO_LONG);
478
0
    } else {
479
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
480
0
    }
481
0
    return 0;
482
0
  }
483
484
0
  const uint8_t tag = header[0];
485
0
  const uint8_t length_byte = header[1];
486
487
0
  if ((tag & 0x1f) == 0x1f) {
488
    // Long form tags are not supported.
489
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
490
0
    return 0;
491
0
  }
492
493
0
  size_t len, header_len;
494
0
  if ((length_byte & 0x80) == 0) {
495
    // Short form length.
496
0
    len = length_byte;
497
0
    header_len = kInitialHeaderLen;
498
0
  } else {
499
0
    const size_t num_bytes = length_byte & 0x7f;
500
501
0
    if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
502
      // indefinite length.
503
0
      if (!bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
504
0
                        max_len)) {
505
0
        OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
506
0
        return 0;
507
0
      }
508
0
      return 1;
509
0
    }
510
511
0
    if (num_bytes == 0 || num_bytes > 4) {
512
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
513
0
      return 0;
514
0
    }
515
516
0
    if (!bio_read_full(bio, header + kInitialHeaderLen, NULL, num_bytes)) {
517
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
518
0
      return 0;
519
0
    }
520
0
    header_len = kInitialHeaderLen + num_bytes;
521
522
0
    uint32_t len32 = 0;
523
0
    for (unsigned i = 0; i < num_bytes; i++) {
524
0
      len32 <<= 8;
525
0
      len32 |= header[kInitialHeaderLen + i];
526
0
    }
527
528
0
    if (len32 < 128) {
529
      // Length should have used short-form encoding.
530
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
531
0
      return 0;
532
0
    }
533
534
0
    if ((len32 >> ((num_bytes - 1) * 8)) == 0) {
535
      // Length should have been at least one byte shorter.
536
0
      OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
537
0
      return 0;
538
0
    }
539
540
0
    len = len32;
541
0
  }
542
543
0
  if (len + header_len < len || len + header_len > max_len || len > INT_MAX) {
544
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_TOO_LONG);
545
0
    return 0;
546
0
  }
547
0
  len += header_len;
548
0
  *out_len = len;
549
550
0
  *out = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len));
551
0
  if (*out == NULL) {
552
0
    return 0;
553
0
  }
554
0
  OPENSSL_memcpy(*out, header, header_len);
555
0
  if (!bio_read_full(bio, (*out) + header_len, NULL, len - header_len)) {
556
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_NOT_ENOUGH_DATA);
557
0
    OPENSSL_free(*out);
558
0
    return 0;
559
0
  }
560
561
0
  return 1;
562
0
}
563
564
0
void BIO_set_retry_special(BIO *bio) {
565
0
  bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL;
566
0
}
567
568
0
int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }
569
570
static CRYPTO_MUTEX g_index_lock = CRYPTO_MUTEX_INIT;
571
static int g_index = BIO_TYPE_START;
572
573
0
int BIO_get_new_index(void) {
574
0
  CRYPTO_MUTEX_lock_write(&g_index_lock);
575
  // If |g_index| exceeds 255, it will collide with the flags bits.
576
0
  int ret = g_index > 255 ? -1 : g_index++;
577
0
  CRYPTO_MUTEX_unlock_write(&g_index_lock);
578
0
  return ret;
579
0
}
580
581
0
BIO_METHOD *BIO_meth_new(int type, const char *name) {
582
0
  BIO_METHOD *method =
583
0
      reinterpret_cast<BIO_METHOD *>(OPENSSL_zalloc(sizeof(BIO_METHOD)));
584
0
  if (method == NULL) {
585
0
    return NULL;
586
0
  }
587
0
  method->type = type;
588
0
  method->name = name;
589
0
  return method;
590
0
}
591
592
0
void BIO_meth_free(BIO_METHOD *method) { OPENSSL_free(method); }
593
594
0
int BIO_meth_set_create(BIO_METHOD *method, int (*create_func)(BIO *)) {
595
0
  method->create = create_func;
596
0
  return 1;
597
0
}
598
599
0
int BIO_meth_set_destroy(BIO_METHOD *method, int (*destroy_func)(BIO *)) {
600
0
  method->destroy = destroy_func;
601
0
  return 1;
602
0
}
603
604
int BIO_meth_set_write(BIO_METHOD *method,
605
0
                       int (*write_func)(BIO *, const char *, int)) {
606
0
  method->bwrite = write_func;
607
0
  return 1;
608
0
}
609
610
int BIO_meth_set_read(BIO_METHOD *method,
611
0
                      int (*read_func)(BIO *, char *, int)) {
612
0
  method->bread = read_func;
613
0
  return 1;
614
0
}
615
616
int BIO_meth_set_gets(BIO_METHOD *method,
617
0
                      int (*gets_func)(BIO *, char *, int)) {
618
0
  method->bgets = gets_func;
619
0
  return 1;
620
0
}
621
622
int BIO_meth_set_ctrl(BIO_METHOD *method,
623
0
                      long (*ctrl_func)(BIO *, int, long, void *)) {
624
0
  method->ctrl = ctrl_func;
625
0
  return 1;
626
0
}
627
628
int BIO_meth_set_callback_ctrl(BIO_METHOD *method,
629
                               long (*callback_ctrl_func)(BIO *, int,
630
0
                                                          BIO_info_cb *)) {
631
0
  method->callback_ctrl = callback_ctrl_func;
632
0
  return 1;
633
0
}
634
635
0
void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
636
637
0
void *BIO_get_data(BIO *bio) { return bio->ptr; }
638
639
0
void BIO_set_init(BIO *bio, int init) { bio->init = init; }
640
641
0
int BIO_get_init(BIO *bio) { return bio->init; }
642
643
0
void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; }
644
645
0
int BIO_get_shutdown(BIO *bio) { return bio->shutdown; }
646
647
0
int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) {
648
  // Ignore the parameter. We implement |BIO_puts| using |BIO_write|.
649
0
  return 1;
650
0
}
651
652
int BIO_get_ex_new_index(long argl, void *argp,      //
653
                         CRYPTO_EX_unused *unused,   //
654
                         CRYPTO_EX_dup *dup_unused,  //
655
0
                         CRYPTO_EX_free *free_func) {
656
0
  return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func);
657
0
}
658
659
0
int BIO_set_ex_data(BIO *bio, int idx, void *data) {
660
0
  return CRYPTO_set_ex_data(&bio->ex_data, idx, data);
661
0
}
662
663
0
void *BIO_get_ex_data(const BIO *bio, int idx) {
664
0
  return CRYPTO_get_ex_data(&bio->ex_data, idx);
665
0
}