xref: /openssl/crypto/bio/bio_meth.c (revision b6461792)
1 /*
2  * Copyright 2016-2024 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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
8  */
9 
10 #include "bio_local.h"
11 #include "internal/thread_once.h"
12 
13 CRYPTO_REF_COUNT bio_type_count;
14 static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT;
15 
DEFINE_RUN_ONCE_STATIC(do_bio_type_init)16 DEFINE_RUN_ONCE_STATIC(do_bio_type_init)
17 {
18     return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START);
19 }
20 
BIO_get_new_index(void)21 int BIO_get_new_index(void)
22 {
23     int newval;
24 
25     if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) {
26         /* Perhaps the error should be raised in do_bio_type_init()? */
27         ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB);
28         return -1;
29     }
30     if (!CRYPTO_UP_REF(&bio_type_count, &newval))
31         return -1;
32     if (newval > BIO_TYPE_MASK)
33         return -1;
34     return newval;
35 }
36 
BIO_meth_new(int type,const char * name)37 BIO_METHOD *BIO_meth_new(int type, const char *name)
38 {
39     BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD));
40 
41     if (biom == NULL
42             || (biom->name = OPENSSL_strdup(name)) == NULL) {
43         OPENSSL_free(biom);
44         return NULL;
45     }
46     biom->type = type;
47     return biom;
48 }
49 
BIO_meth_free(BIO_METHOD * biom)50 void BIO_meth_free(BIO_METHOD *biom)
51 {
52     if (biom != NULL) {
53         OPENSSL_free(biom->name);
54         OPENSSL_free(biom);
55     }
56 }
57 
BIO_meth_get_write(const BIO_METHOD * biom)58 int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int)
59 {
60     return biom->bwrite_old;
61 }
62 
BIO_meth_get_write_ex(const BIO_METHOD * biom)63 int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t,
64                                                 size_t *)
65 {
66     return biom->bwrite;
67 }
68 
69 /* Conversion for old style bwrite to new style */
bwrite_conv(BIO * bio,const char * data,size_t datal,size_t * written)70 int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written)
71 {
72     int ret;
73 
74     if (datal > INT_MAX)
75         datal = INT_MAX;
76 
77     ret = bio->method->bwrite_old(bio, data, (int)datal);
78 
79     if (ret <= 0) {
80         *written = 0;
81         return ret;
82     }
83 
84     *written = (size_t)ret;
85 
86     return 1;
87 }
88 
BIO_meth_set_write(BIO_METHOD * biom,int (* bwrite)(BIO *,const char *,int))89 int BIO_meth_set_write(BIO_METHOD *biom,
90                        int (*bwrite) (BIO *, const char *, int))
91 {
92     biom->bwrite_old = bwrite;
93     biom->bwrite = bwrite_conv;
94     return 1;
95 }
96 
BIO_meth_set_write_ex(BIO_METHOD * biom,int (* bwrite)(BIO *,const char *,size_t,size_t *))97 int BIO_meth_set_write_ex(BIO_METHOD *biom,
98                        int (*bwrite) (BIO *, const char *, size_t, size_t *))
99 {
100     biom->bwrite_old = NULL;
101     biom->bwrite = bwrite;
102     return 1;
103 }
104 
BIO_meth_get_read(const BIO_METHOD * biom)105 int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int)
106 {
107     return biom->bread_old;
108 }
109 
BIO_meth_get_read_ex(const BIO_METHOD * biom)110 int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *)
111 {
112     return biom->bread;
113 }
114 
115 /* Conversion for old style bread to new style */
bread_conv(BIO * bio,char * data,size_t datal,size_t * readbytes)116 int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes)
117 {
118     int ret;
119 
120     if (datal > INT_MAX)
121         datal = INT_MAX;
122 
123     ret = bio->method->bread_old(bio, data, (int)datal);
124 
125     if (ret <= 0) {
126         *readbytes = 0;
127         return ret;
128     }
129 
130     *readbytes = (size_t)ret;
131 
132     return 1;
133 }
134 
BIO_meth_set_read(BIO_METHOD * biom,int (* bread)(BIO *,char *,int))135 int BIO_meth_set_read(BIO_METHOD *biom,
136                       int (*bread) (BIO *, char *, int))
137 {
138     biom->bread_old = bread;
139     biom->bread = bread_conv;
140     return 1;
141 }
142 
BIO_meth_set_read_ex(BIO_METHOD * biom,int (* bread)(BIO *,char *,size_t,size_t *))143 int BIO_meth_set_read_ex(BIO_METHOD *biom,
144                          int (*bread) (BIO *, char *, size_t, size_t *))
145 {
146     biom->bread_old = NULL;
147     biom->bread = bread;
148     return 1;
149 }
150 
BIO_meth_get_puts(const BIO_METHOD * biom)151 int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *)
152 {
153     return biom->bputs;
154 }
155 
BIO_meth_set_puts(BIO_METHOD * biom,int (* bputs)(BIO *,const char *))156 int BIO_meth_set_puts(BIO_METHOD *biom,
157                       int (*bputs) (BIO *, const char *))
158 {
159     biom->bputs = bputs;
160     return 1;
161 }
162 
BIO_meth_get_gets(const BIO_METHOD * biom)163 int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int)
164 {
165     return biom->bgets;
166 }
167 
BIO_meth_set_gets(BIO_METHOD * biom,int (* bgets)(BIO *,char *,int))168 int BIO_meth_set_gets(BIO_METHOD *biom,
169                       int (*bgets) (BIO *, char *, int))
170 {
171     biom->bgets = bgets;
172     return 1;
173 }
174 
BIO_meth_get_ctrl(const BIO_METHOD * biom)175 long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *)
176 {
177     return biom->ctrl;
178 }
179 
BIO_meth_set_ctrl(BIO_METHOD * biom,long (* ctrl)(BIO *,int,long,void *))180 int BIO_meth_set_ctrl(BIO_METHOD *biom,
181                       long (*ctrl) (BIO *, int, long, void *))
182 {
183     biom->ctrl = ctrl;
184     return 1;
185 }
186 
BIO_meth_get_create(const BIO_METHOD * biom)187 int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *)
188 {
189     return biom->create;
190 }
191 
BIO_meth_set_create(BIO_METHOD * biom,int (* create)(BIO *))192 int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *))
193 {
194     biom->create = create;
195     return 1;
196 }
197 
BIO_meth_get_destroy(const BIO_METHOD * biom)198 int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *)
199 {
200     return biom->destroy;
201 }
202 
BIO_meth_set_destroy(BIO_METHOD * biom,int (* destroy)(BIO *))203 int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *))
204 {
205     biom->destroy = destroy;
206     return 1;
207 }
208 
BIO_meth_get_callback_ctrl(const BIO_METHOD * biom)209 long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *)
210 {
211     return biom->callback_ctrl;
212 }
213 
BIO_meth_set_callback_ctrl(BIO_METHOD * biom,long (* callback_ctrl)(BIO *,int,BIO_info_cb *))214 int BIO_meth_set_callback_ctrl(BIO_METHOD *biom,
215                                long (*callback_ctrl) (BIO *, int,
216                                                       BIO_info_cb *))
217 {
218     biom->callback_ctrl = callback_ctrl;
219     return 1;
220 }
221 
BIO_meth_set_sendmmsg(BIO_METHOD * biom,int (* bsendmmsg)(BIO *,BIO_MSG *,size_t,size_t,uint64_t,size_t *))222 int BIO_meth_set_sendmmsg(BIO_METHOD *biom,
223                           int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
224 {
225     biom->bsendmmsg = bsendmmsg;
226     return 1;
227 }
228 
BIO_meth_get_sendmmsg(const BIO_METHOD * biom)229 int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
230     return biom->bsendmmsg;
231 }
232 
BIO_meth_set_recvmmsg(BIO_METHOD * biom,int (* brecvmmsg)(BIO *,BIO_MSG *,size_t,size_t,uint64_t,size_t *))233 int BIO_meth_set_recvmmsg(BIO_METHOD *biom,
234                           int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *))
235 {
236     biom->brecvmmsg = brecvmmsg;
237     return 1;
238 }
239 
BIO_meth_get_recvmmsg(const BIO_METHOD * biom)240 int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) {
241     return biom->brecvmmsg;
242 }
243