blob: 3cbac5bf85dfae8e5d4da3184eb59f61bc29d5da [file] [log] [blame]
Rich Salzb1322252016-05-17 14:52:22 -04001/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
Matt Caswellf3344612016-03-22 11:34:32 +00003 *
Rich Salzb1322252016-05-17 14:52:22 -04004 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
Matt Caswellf3344612016-03-22 11:34:32 +00008 */
9
10#include "bio_lcl.h"
Rich Salz5a7ad1f2016-08-20 19:06:43 -040011#include <internal/thread_once.h>
Matt Caswellf3344612016-03-22 11:34:32 +000012
Rich Salz5a7ad1f2016-08-20 19:06:43 -040013CRYPTO_RWLOCK *bio_type_lock = NULL;
14static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
15
16DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
17{
18 bio_type_lock = CRYPTO_THREAD_lock_new();
19 return bio_type_lock != NULL;
20}
Rich Salz8b8d9632016-08-19 21:04:41 -040021
22int BIO_get_new_index()
23{
Kurt Roeckx2f545ae2016-08-27 16:01:08 +020024 static CRYPTO_REF_COUNT bio_count = BIO_TYPE_START;
Rich Salz8b8d9632016-08-19 21:04:41 -040025 int newval;
26
Rich Salz5a7ad1f2016-08-20 19:06:43 -040027 if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
28 BIOerr(BIO_F_BIO_GET_NEW_INDEX, ERR_R_MALLOC_FAILURE);
29 return -1;
30 }
Kurt Roeckx2f545ae2016-08-27 16:01:08 +020031 if (!CRYPTO_UP_REF(&bio_count, &newval, bio_type_lock))
Rich Salz8b8d9632016-08-19 21:04:41 -040032 return -1;
33 return newval;
34}
35
Matt Caswellf3344612016-03-22 11:34:32 +000036BIO_METHOD *BIO_meth_new(int type, const char *name)
37{
38 BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
39
40 if (biom != NULL) {
41 biom->type = type;
42 biom->name = name;
43 }
44 return biom;
45}
46
47void BIO_meth_free(BIO_METHOD *biom)
48{
49 OPENSSL_free(biom);
50}
51
52int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int)
53{
Matt Caswell3befffa2016-10-20 15:18:39 +010054 return biom->bwrite_old;
55}
56
57int (*BIO_meth_get_write_ex(BIO_METHOD *biom)) (BIO *, const char *, size_t,
58 size_t *)
59{
Matt Caswellf3344612016-03-22 11:34:32 +000060 return biom->bwrite;
61}
62
Matt Caswell3befffa2016-10-20 15:18:39 +010063/* Conversion for old style bwrite to new style */
Matt Caswell42c60462016-10-21 15:15:51 +010064int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
Matt Caswell3befffa2016-10-20 15:18:39 +010065{
66 int ret;
67
Matt Caswell42c60462016-10-21 15:15:51 +010068 if (datal > INT_MAX)
69 datal = INT_MAX;
Matt Caswell3befffa2016-10-20 15:18:39 +010070
Matt Caswell42c60462016-10-21 15:15:51 +010071 ret = bio->method->bwrite_old(bio, data, (int)datal);
Matt Caswell3befffa2016-10-20 15:18:39 +010072
73 if (ret <= 0) {
74 *written = 0;
75 return ret;
76 }
77
78 *written = (size_t)ret;
79
80 return 1;
81}
82
Matt Caswellf3344612016-03-22 11:34:32 +000083int BIO_meth_set_write(BIO_METHOD *biom,
Richard Levitteadb40762016-04-04 00:11:20 +020084 int (*bwrite) (BIO *, const char *, int))
Matt Caswellf3344612016-03-22 11:34:32 +000085{
Matt Caswell3befffa2016-10-20 15:18:39 +010086 biom->bwrite_old = bwrite;
87 biom->bwrite = bwrite_conv;
88 return 1;
89}
90
91int BIO_meth_set_write_ex(BIO_METHOD *biom,
92 int (*bwrite) (BIO *, const char *, size_t, size_t *))
93{
94 biom->bwrite_old = NULL;
Richard Levitteadb40762016-04-04 00:11:20 +020095 biom->bwrite = bwrite;
Matt Caswellf3344612016-03-22 11:34:32 +000096 return 1;
97}
98
Matt Caswella146ae52016-03-22 09:21:29 +000099int (*BIO_meth_get_read(BIO_METHOD *biom)) (BIO *, char *, int)
Matt Caswellf3344612016-03-22 11:34:32 +0000100{
Matt Caswelld07aee22016-09-05 17:26:58 +0100101 return biom->bread_old;
102}
103
104int (*BIO_meth_get_read_ex(BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
105{
Matt Caswellf3344612016-03-22 11:34:32 +0000106 return biom->bread;
107}
108
Matt Caswelld07aee22016-09-05 17:26:58 +0100109/* Conversion for old style bread to new style */
Matt Caswelld62bf892016-10-26 00:05:25 +0100110int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
Matt Caswelld07aee22016-09-05 17:26:58 +0100111{
112 int ret;
113
Matt Caswell42c60462016-10-21 15:15:51 +0100114 if (datal > INT_MAX)
115 datal = INT_MAX;
Matt Caswelld07aee22016-09-05 17:26:58 +0100116
Matt Caswell42c60462016-10-21 15:15:51 +0100117 ret = bio->method->bread_old(bio, data, (int)datal);
Matt Caswelld07aee22016-09-05 17:26:58 +0100118
119 if (ret <= 0) {
Matt Caswelld62bf892016-10-26 00:05:25 +0100120 *readbytes = 0;
Matt Caswelld07aee22016-09-05 17:26:58 +0100121 return ret;
122 }
123
Matt Caswelld62bf892016-10-26 00:05:25 +0100124 *readbytes = (size_t)ret;
Matt Caswelld07aee22016-09-05 17:26:58 +0100125
126 return 1;
127}
128
Matt Caswellf3344612016-03-22 11:34:32 +0000129int BIO_meth_set_read(BIO_METHOD *biom,
Richard Levitteadb40762016-04-04 00:11:20 +0200130 int (*bread) (BIO *, char *, int))
Matt Caswellf3344612016-03-22 11:34:32 +0000131{
Matt Caswelld07aee22016-09-05 17:26:58 +0100132 biom->bread_old = bread;
133 biom->bread = bread_conv;
134 return 1;
135}
136
137int BIO_meth_set_read_ex(BIO_METHOD *biom,
138 int (*bread) (BIO *, char *, size_t, size_t *))
139{
Matt Caswell3befffa2016-10-20 15:18:39 +0100140 biom->bread_old = NULL;
Richard Levitteadb40762016-04-04 00:11:20 +0200141 biom->bread = bread;
Matt Caswellf3344612016-03-22 11:34:32 +0000142 return 1;
143}
144
145int (*BIO_meth_get_puts(BIO_METHOD *biom)) (BIO *, const char *)
146{
147 return biom->bputs;
148}
149
150int BIO_meth_set_puts(BIO_METHOD *biom,
Richard Levitteadb40762016-04-04 00:11:20 +0200151 int (*bputs) (BIO *, const char *))
Matt Caswellf3344612016-03-22 11:34:32 +0000152{
Richard Levitteadb40762016-04-04 00:11:20 +0200153 biom->bputs = bputs;
Matt Caswellf3344612016-03-22 11:34:32 +0000154 return 1;
155}
156
157int (*BIO_meth_get_gets(BIO_METHOD *biom)) (BIO *, char *, int)
158{
Matt Caswella146ae52016-03-22 09:21:29 +0000159 return biom->bgets;
Matt Caswellf3344612016-03-22 11:34:32 +0000160}
161
162int BIO_meth_set_gets(BIO_METHOD *biom,
Richard Levitteadb40762016-04-04 00:11:20 +0200163 int (*bgets) (BIO *, char *, int))
Matt Caswellf3344612016-03-22 11:34:32 +0000164{
Richard Levitteadb40762016-04-04 00:11:20 +0200165 biom->bgets = bgets;
Matt Caswellf3344612016-03-22 11:34:32 +0000166 return 1;
167}
168
169long (*BIO_meth_get_ctrl(BIO_METHOD *biom)) (BIO *, int, long, void *)
170{
171 return biom->ctrl;
172}
173
174int BIO_meth_set_ctrl(BIO_METHOD *biom,
175 long (*ctrl) (BIO *, int, long, void *))
176{
177 biom->ctrl = ctrl;
178 return 1;
179}
180
Matt Caswella146ae52016-03-22 09:21:29 +0000181int (*BIO_meth_get_create(BIO_METHOD *biom)) (BIO *)
Matt Caswellf3344612016-03-22 11:34:32 +0000182{
183 return biom->create;
184}
185
186int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
187{
188 biom->create = create;
189 return 1;
190}
191
192int (*BIO_meth_get_destroy(BIO_METHOD *biom)) (BIO *)
193{
194 return biom->destroy;
195}
196
197int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
198{
199 biom->destroy = destroy;
200 return 1;
201}
202
203long (*BIO_meth_get_callback_ctrl(BIO_METHOD *biom)) (BIO *, int, bio_info_cb *)
204{
205 return biom->callback_ctrl;
206}
207
208int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
209 long (*callback_ctrl) (BIO *, int,
210 bio_info_cb *))
211{
212 biom->callback_ctrl = callback_ctrl;
213 return 1;
214}