1 /*
2 * Copyright 2019-2022 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 <openssl/core_dispatch.h>
11 #include "bio_local.h"
12 #include "internal/cryptlib.h"
13
14 typedef struct {
15 OSSL_FUNC_BIO_read_ex_fn *c_bio_read_ex;
16 OSSL_FUNC_BIO_write_ex_fn *c_bio_write_ex;
17 OSSL_FUNC_BIO_gets_fn *c_bio_gets;
18 OSSL_FUNC_BIO_puts_fn *c_bio_puts;
19 OSSL_FUNC_BIO_ctrl_fn *c_bio_ctrl;
20 OSSL_FUNC_BIO_up_ref_fn *c_bio_up_ref;
21 OSSL_FUNC_BIO_free_fn *c_bio_free;
22 } BIO_CORE_GLOBALS;
23
bio_core_globals_free(void * vbcg)24 static void bio_core_globals_free(void *vbcg)
25 {
26 OPENSSL_free(vbcg);
27 }
28
bio_core_globals_new(OSSL_LIB_CTX * ctx)29 static void *bio_core_globals_new(OSSL_LIB_CTX *ctx)
30 {
31 return OPENSSL_zalloc(sizeof(BIO_CORE_GLOBALS));
32 }
33
34 static const OSSL_LIB_CTX_METHOD bio_core_globals_method = {
35 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
36 bio_core_globals_new,
37 bio_core_globals_free,
38 };
39
get_globals(OSSL_LIB_CTX * libctx)40 static ossl_inline BIO_CORE_GLOBALS *get_globals(OSSL_LIB_CTX *libctx)
41 {
42 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_BIO_CORE_INDEX,
43 &bio_core_globals_method);
44 }
45
bio_core_read_ex(BIO * bio,char * data,size_t data_len,size_t * bytes_read)46 static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
47 size_t *bytes_read)
48 {
49 BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
50
51 if (bcgbl == NULL || bcgbl->c_bio_read_ex == NULL)
52 return 0;
53 return bcgbl->c_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
54 }
55
bio_core_write_ex(BIO * bio,const char * data,size_t data_len,size_t * written)56 static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
57 size_t *written)
58 {
59 BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
60
61 if (bcgbl == NULL || bcgbl->c_bio_write_ex == NULL)
62 return 0;
63 return bcgbl->c_bio_write_ex(BIO_get_data(bio), data, data_len, written);
64 }
65
bio_core_ctrl(BIO * bio,int cmd,long num,void * ptr)66 static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
67 {
68 BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
69
70 if (bcgbl == NULL || bcgbl->c_bio_ctrl == NULL)
71 return -1;
72 return bcgbl->c_bio_ctrl(BIO_get_data(bio), cmd, num, ptr);
73 }
74
bio_core_gets(BIO * bio,char * buf,int size)75 static int bio_core_gets(BIO *bio, char *buf, int size)
76 {
77 BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
78
79 if (bcgbl == NULL || bcgbl->c_bio_gets == NULL)
80 return -1;
81 return bcgbl->c_bio_gets(BIO_get_data(bio), buf, size);
82 }
83
bio_core_puts(BIO * bio,const char * str)84 static int bio_core_puts(BIO *bio, const char *str)
85 {
86 BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
87
88 if (bcgbl == NULL || bcgbl->c_bio_puts == NULL)
89 return -1;
90 return bcgbl->c_bio_puts(BIO_get_data(bio), str);
91 }
92
bio_core_new(BIO * bio)93 static int bio_core_new(BIO *bio)
94 {
95 BIO_set_init(bio, 1);
96
97 return 1;
98 }
99
bio_core_free(BIO * bio)100 static int bio_core_free(BIO *bio)
101 {
102 BIO_CORE_GLOBALS *bcgbl = get_globals(bio->libctx);
103
104 if (bcgbl == NULL)
105 return 0;
106
107 BIO_set_init(bio, 0);
108 bcgbl->c_bio_free(BIO_get_data(bio));
109
110 return 1;
111 }
112
113 static const BIO_METHOD corebiometh = {
114 BIO_TYPE_CORE_TO_PROV,
115 "BIO to Core filter",
116 bio_core_write_ex,
117 NULL,
118 bio_core_read_ex,
119 NULL,
120 bio_core_puts,
121 bio_core_gets,
122 bio_core_ctrl,
123 bio_core_new,
124 bio_core_free,
125 NULL,
126 };
127
BIO_s_core(void)128 const BIO_METHOD *BIO_s_core(void)
129 {
130 return &corebiometh;
131 }
132
BIO_new_from_core_bio(OSSL_LIB_CTX * libctx,OSSL_CORE_BIO * corebio)133 BIO *BIO_new_from_core_bio(OSSL_LIB_CTX *libctx, OSSL_CORE_BIO *corebio)
134 {
135 BIO *outbio;
136 BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
137
138 /* Check the library context has been initialised with the callbacks */
139 if (bcgbl == NULL || (bcgbl->c_bio_write_ex == NULL && bcgbl->c_bio_read_ex == NULL))
140 return NULL;
141
142 if ((outbio = BIO_new_ex(libctx, BIO_s_core())) == NULL)
143 return NULL;
144
145 if (!bcgbl->c_bio_up_ref(corebio)) {
146 BIO_free(outbio);
147 return NULL;
148 }
149 BIO_set_data(outbio, corebio);
150 return outbio;
151 }
152
ossl_bio_init_core(OSSL_LIB_CTX * libctx,const OSSL_DISPATCH * fns)153 int ossl_bio_init_core(OSSL_LIB_CTX *libctx, const OSSL_DISPATCH *fns)
154 {
155 BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
156
157 if (bcgbl == NULL)
158 return 0;
159
160 for (; fns->function_id != 0; fns++) {
161 switch (fns->function_id) {
162 case OSSL_FUNC_BIO_READ_EX:
163 if (bcgbl->c_bio_read_ex == NULL)
164 bcgbl->c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
165 break;
166 case OSSL_FUNC_BIO_WRITE_EX:
167 if (bcgbl->c_bio_write_ex == NULL)
168 bcgbl->c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
169 break;
170 case OSSL_FUNC_BIO_GETS:
171 if (bcgbl->c_bio_gets == NULL)
172 bcgbl->c_bio_gets = OSSL_FUNC_BIO_gets(fns);
173 break;
174 case OSSL_FUNC_BIO_PUTS:
175 if (bcgbl->c_bio_puts == NULL)
176 bcgbl->c_bio_puts = OSSL_FUNC_BIO_puts(fns);
177 break;
178 case OSSL_FUNC_BIO_CTRL:
179 if (bcgbl->c_bio_ctrl == NULL)
180 bcgbl->c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
181 break;
182 case OSSL_FUNC_BIO_UP_REF:
183 if (bcgbl->c_bio_up_ref == NULL)
184 bcgbl->c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);
185 break;
186 case OSSL_FUNC_BIO_FREE:
187 if (bcgbl->c_bio_free == NULL)
188 bcgbl->c_bio_free = OSSL_FUNC_BIO_free(fns);
189 break;
190 }
191 }
192
193 return 1;
194 }
195