1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery #include <assert.h>
11*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
12*b077aed3SPierre Pronchery #include "internal/cryptlib.h"
13*b077aed3SPierre Pronchery #include "prov/bio.h"
14*b077aed3SPierre Pronchery
15*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_new_file_fn *c_bio_new_file = NULL;
16*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_new_membuf_fn *c_bio_new_membuf = NULL;
17*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex = NULL;
18*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_write_ex_fn *c_bio_write_ex = NULL;
19*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_gets_fn *c_bio_gets = NULL;
20*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_puts_fn *c_bio_puts = NULL;
21*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl = NULL;
22*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_up_ref_fn *c_bio_up_ref = NULL;
23*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_free_fn *c_bio_free = NULL;
24*b077aed3SPierre Pronchery static OSSL_FUNC_BIO_vprintf_fn *c_bio_vprintf = NULL;
25*b077aed3SPierre Pronchery
ossl_prov_bio_from_dispatch(const OSSL_DISPATCH * fns)26*b077aed3SPierre Pronchery int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
27*b077aed3SPierre Pronchery {
28*b077aed3SPierre Pronchery for (; fns->function_id != 0; fns++) {
29*b077aed3SPierre Pronchery switch (fns->function_id) {
30*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_NEW_FILE:
31*b077aed3SPierre Pronchery if (c_bio_new_file == NULL)
32*b077aed3SPierre Pronchery c_bio_new_file = OSSL_FUNC_BIO_new_file(fns);
33*b077aed3SPierre Pronchery break;
34*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_NEW_MEMBUF:
35*b077aed3SPierre Pronchery if (c_bio_new_membuf == NULL)
36*b077aed3SPierre Pronchery c_bio_new_membuf = OSSL_FUNC_BIO_new_membuf(fns);
37*b077aed3SPierre Pronchery break;
38*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_READ_EX:
39*b077aed3SPierre Pronchery if (c_bio_read_ex == NULL)
40*b077aed3SPierre Pronchery c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
41*b077aed3SPierre Pronchery break;
42*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_WRITE_EX:
43*b077aed3SPierre Pronchery if (c_bio_write_ex == NULL)
44*b077aed3SPierre Pronchery c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
45*b077aed3SPierre Pronchery break;
46*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_GETS:
47*b077aed3SPierre Pronchery if (c_bio_gets == NULL)
48*b077aed3SPierre Pronchery c_bio_gets = OSSL_FUNC_BIO_gets(fns);
49*b077aed3SPierre Pronchery break;
50*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_PUTS:
51*b077aed3SPierre Pronchery if (c_bio_puts == NULL)
52*b077aed3SPierre Pronchery c_bio_puts = OSSL_FUNC_BIO_puts(fns);
53*b077aed3SPierre Pronchery break;
54*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_CTRL:
55*b077aed3SPierre Pronchery if (c_bio_ctrl == NULL)
56*b077aed3SPierre Pronchery c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
57*b077aed3SPierre Pronchery break;
58*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_UP_REF:
59*b077aed3SPierre Pronchery if (c_bio_up_ref == NULL)
60*b077aed3SPierre Pronchery c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);
61*b077aed3SPierre Pronchery break;
62*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_FREE:
63*b077aed3SPierre Pronchery if (c_bio_free == NULL)
64*b077aed3SPierre Pronchery c_bio_free = OSSL_FUNC_BIO_free(fns);
65*b077aed3SPierre Pronchery break;
66*b077aed3SPierre Pronchery case OSSL_FUNC_BIO_VPRINTF:
67*b077aed3SPierre Pronchery if (c_bio_vprintf == NULL)
68*b077aed3SPierre Pronchery c_bio_vprintf = OSSL_FUNC_BIO_vprintf(fns);
69*b077aed3SPierre Pronchery break;
70*b077aed3SPierre Pronchery }
71*b077aed3SPierre Pronchery }
72*b077aed3SPierre Pronchery
73*b077aed3SPierre Pronchery return 1;
74*b077aed3SPierre Pronchery }
75*b077aed3SPierre Pronchery
ossl_prov_bio_new_file(const char * filename,const char * mode)76*b077aed3SPierre Pronchery OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
77*b077aed3SPierre Pronchery {
78*b077aed3SPierre Pronchery if (c_bio_new_file == NULL)
79*b077aed3SPierre Pronchery return NULL;
80*b077aed3SPierre Pronchery return c_bio_new_file(filename, mode);
81*b077aed3SPierre Pronchery }
82*b077aed3SPierre Pronchery
ossl_prov_bio_new_membuf(const char * filename,int len)83*b077aed3SPierre Pronchery OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
84*b077aed3SPierre Pronchery {
85*b077aed3SPierre Pronchery if (c_bio_new_membuf == NULL)
86*b077aed3SPierre Pronchery return NULL;
87*b077aed3SPierre Pronchery return c_bio_new_membuf(filename, len);
88*b077aed3SPierre Pronchery }
89*b077aed3SPierre Pronchery
ossl_prov_bio_read_ex(OSSL_CORE_BIO * bio,void * data,size_t data_len,size_t * bytes_read)90*b077aed3SPierre Pronchery int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
91*b077aed3SPierre Pronchery size_t *bytes_read)
92*b077aed3SPierre Pronchery {
93*b077aed3SPierre Pronchery if (c_bio_read_ex == NULL)
94*b077aed3SPierre Pronchery return 0;
95*b077aed3SPierre Pronchery return c_bio_read_ex(bio, data, data_len, bytes_read);
96*b077aed3SPierre Pronchery }
97*b077aed3SPierre Pronchery
ossl_prov_bio_write_ex(OSSL_CORE_BIO * bio,const void * data,size_t data_len,size_t * written)98*b077aed3SPierre Pronchery int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
99*b077aed3SPierre Pronchery size_t *written)
100*b077aed3SPierre Pronchery {
101*b077aed3SPierre Pronchery if (c_bio_write_ex == NULL)
102*b077aed3SPierre Pronchery return 0;
103*b077aed3SPierre Pronchery return c_bio_write_ex(bio, data, data_len, written);
104*b077aed3SPierre Pronchery }
105*b077aed3SPierre Pronchery
ossl_prov_bio_gets(OSSL_CORE_BIO * bio,char * buf,int size)106*b077aed3SPierre Pronchery int ossl_prov_bio_gets(OSSL_CORE_BIO *bio, char *buf, int size)
107*b077aed3SPierre Pronchery {
108*b077aed3SPierre Pronchery if (c_bio_gets == NULL)
109*b077aed3SPierre Pronchery return -1;
110*b077aed3SPierre Pronchery return c_bio_gets(bio, buf, size);
111*b077aed3SPierre Pronchery }
112*b077aed3SPierre Pronchery
ossl_prov_bio_puts(OSSL_CORE_BIO * bio,const char * str)113*b077aed3SPierre Pronchery int ossl_prov_bio_puts(OSSL_CORE_BIO *bio, const char *str)
114*b077aed3SPierre Pronchery {
115*b077aed3SPierre Pronchery if (c_bio_puts == NULL)
116*b077aed3SPierre Pronchery return -1;
117*b077aed3SPierre Pronchery return c_bio_puts(bio, str);
118*b077aed3SPierre Pronchery }
119*b077aed3SPierre Pronchery
ossl_prov_bio_ctrl(OSSL_CORE_BIO * bio,int cmd,long num,void * ptr)120*b077aed3SPierre Pronchery int ossl_prov_bio_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
121*b077aed3SPierre Pronchery {
122*b077aed3SPierre Pronchery if (c_bio_ctrl == NULL)
123*b077aed3SPierre Pronchery return -1;
124*b077aed3SPierre Pronchery return c_bio_ctrl(bio, cmd, num, ptr);
125*b077aed3SPierre Pronchery }
126*b077aed3SPierre Pronchery
ossl_prov_bio_up_ref(OSSL_CORE_BIO * bio)127*b077aed3SPierre Pronchery int ossl_prov_bio_up_ref(OSSL_CORE_BIO *bio)
128*b077aed3SPierre Pronchery {
129*b077aed3SPierre Pronchery if (c_bio_up_ref == NULL)
130*b077aed3SPierre Pronchery return 0;
131*b077aed3SPierre Pronchery return c_bio_up_ref(bio);
132*b077aed3SPierre Pronchery }
133*b077aed3SPierre Pronchery
ossl_prov_bio_free(OSSL_CORE_BIO * bio)134*b077aed3SPierre Pronchery int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
135*b077aed3SPierre Pronchery {
136*b077aed3SPierre Pronchery if (c_bio_free == NULL)
137*b077aed3SPierre Pronchery return 0;
138*b077aed3SPierre Pronchery return c_bio_free(bio);
139*b077aed3SPierre Pronchery }
140*b077aed3SPierre Pronchery
ossl_prov_bio_vprintf(OSSL_CORE_BIO * bio,const char * format,va_list ap)141*b077aed3SPierre Pronchery int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
142*b077aed3SPierre Pronchery {
143*b077aed3SPierre Pronchery if (c_bio_vprintf == NULL)
144*b077aed3SPierre Pronchery return -1;
145*b077aed3SPierre Pronchery return c_bio_vprintf(bio, format, ap);
146*b077aed3SPierre Pronchery }
147*b077aed3SPierre Pronchery
ossl_prov_bio_printf(OSSL_CORE_BIO * bio,const char * format,...)148*b077aed3SPierre Pronchery int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
149*b077aed3SPierre Pronchery {
150*b077aed3SPierre Pronchery va_list ap;
151*b077aed3SPierre Pronchery int ret;
152*b077aed3SPierre Pronchery
153*b077aed3SPierre Pronchery va_start(ap, format);
154*b077aed3SPierre Pronchery ret = ossl_prov_bio_vprintf(bio, format, ap);
155*b077aed3SPierre Pronchery va_end(ap);
156*b077aed3SPierre Pronchery
157*b077aed3SPierre Pronchery return ret;
158*b077aed3SPierre Pronchery }
159*b077aed3SPierre Pronchery
160*b077aed3SPierre Pronchery #ifndef FIPS_MODULE
161*b077aed3SPierre Pronchery
162*b077aed3SPierre Pronchery /* No direct BIO support in the FIPS module */
163*b077aed3SPierre Pronchery
bio_core_read_ex(BIO * bio,char * data,size_t data_len,size_t * bytes_read)164*b077aed3SPierre Pronchery static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
165*b077aed3SPierre Pronchery size_t *bytes_read)
166*b077aed3SPierre Pronchery {
167*b077aed3SPierre Pronchery return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
168*b077aed3SPierre Pronchery }
169*b077aed3SPierre Pronchery
bio_core_write_ex(BIO * bio,const char * data,size_t data_len,size_t * written)170*b077aed3SPierre Pronchery static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
171*b077aed3SPierre Pronchery size_t *written)
172*b077aed3SPierre Pronchery {
173*b077aed3SPierre Pronchery return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
174*b077aed3SPierre Pronchery }
175*b077aed3SPierre Pronchery
bio_core_ctrl(BIO * bio,int cmd,long num,void * ptr)176*b077aed3SPierre Pronchery static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
177*b077aed3SPierre Pronchery {
178*b077aed3SPierre Pronchery return ossl_prov_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
179*b077aed3SPierre Pronchery }
180*b077aed3SPierre Pronchery
bio_core_gets(BIO * bio,char * buf,int size)181*b077aed3SPierre Pronchery static int bio_core_gets(BIO *bio, char *buf, int size)
182*b077aed3SPierre Pronchery {
183*b077aed3SPierre Pronchery return ossl_prov_bio_gets(BIO_get_data(bio), buf, size);
184*b077aed3SPierre Pronchery }
185*b077aed3SPierre Pronchery
bio_core_puts(BIO * bio,const char * str)186*b077aed3SPierre Pronchery static int bio_core_puts(BIO *bio, const char *str)
187*b077aed3SPierre Pronchery {
188*b077aed3SPierre Pronchery return ossl_prov_bio_puts(BIO_get_data(bio), str);
189*b077aed3SPierre Pronchery }
190*b077aed3SPierre Pronchery
bio_core_new(BIO * bio)191*b077aed3SPierre Pronchery static int bio_core_new(BIO *bio)
192*b077aed3SPierre Pronchery {
193*b077aed3SPierre Pronchery BIO_set_init(bio, 1);
194*b077aed3SPierre Pronchery
195*b077aed3SPierre Pronchery return 1;
196*b077aed3SPierre Pronchery }
197*b077aed3SPierre Pronchery
bio_core_free(BIO * bio)198*b077aed3SPierre Pronchery static int bio_core_free(BIO *bio)
199*b077aed3SPierre Pronchery {
200*b077aed3SPierre Pronchery BIO_set_init(bio, 0);
201*b077aed3SPierre Pronchery ossl_prov_bio_free(BIO_get_data(bio));
202*b077aed3SPierre Pronchery
203*b077aed3SPierre Pronchery return 1;
204*b077aed3SPierre Pronchery }
205*b077aed3SPierre Pronchery
ossl_bio_prov_init_bio_method(void)206*b077aed3SPierre Pronchery BIO_METHOD *ossl_bio_prov_init_bio_method(void)
207*b077aed3SPierre Pronchery {
208*b077aed3SPierre Pronchery BIO_METHOD *corebiometh = NULL;
209*b077aed3SPierre Pronchery
210*b077aed3SPierre Pronchery corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
211*b077aed3SPierre Pronchery if (corebiometh == NULL
212*b077aed3SPierre Pronchery || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
213*b077aed3SPierre Pronchery || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
214*b077aed3SPierre Pronchery || !BIO_meth_set_puts(corebiometh, bio_core_puts)
215*b077aed3SPierre Pronchery || !BIO_meth_set_gets(corebiometh, bio_core_gets)
216*b077aed3SPierre Pronchery || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
217*b077aed3SPierre Pronchery || !BIO_meth_set_create(corebiometh, bio_core_new)
218*b077aed3SPierre Pronchery || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
219*b077aed3SPierre Pronchery BIO_meth_free(corebiometh);
220*b077aed3SPierre Pronchery return NULL;
221*b077aed3SPierre Pronchery }
222*b077aed3SPierre Pronchery
223*b077aed3SPierre Pronchery return corebiometh;
224*b077aed3SPierre Pronchery }
225*b077aed3SPierre Pronchery
ossl_bio_new_from_core_bio(PROV_CTX * provctx,OSSL_CORE_BIO * corebio)226*b077aed3SPierre Pronchery BIO *ossl_bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
227*b077aed3SPierre Pronchery {
228*b077aed3SPierre Pronchery BIO *outbio;
229*b077aed3SPierre Pronchery BIO_METHOD *corebiometh = ossl_prov_ctx_get0_core_bio_method(provctx);
230*b077aed3SPierre Pronchery
231*b077aed3SPierre Pronchery if (corebiometh == NULL)
232*b077aed3SPierre Pronchery return NULL;
233*b077aed3SPierre Pronchery
234*b077aed3SPierre Pronchery if ((outbio = BIO_new(corebiometh)) == NULL)
235*b077aed3SPierre Pronchery return NULL;
236*b077aed3SPierre Pronchery if (!ossl_prov_bio_up_ref(corebio)) {
237*b077aed3SPierre Pronchery BIO_free(outbio);
238*b077aed3SPierre Pronchery return NULL;
239*b077aed3SPierre Pronchery }
240*b077aed3SPierre Pronchery BIO_set_data(outbio, corebio);
241*b077aed3SPierre Pronchery return outbio;
242*b077aed3SPierre Pronchery }
243*b077aed3SPierre Pronchery
244*b077aed3SPierre Pronchery #endif
245