1 /*
2  * Copyright 2019-2021 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->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->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->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->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->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     BIO_set_init(bio, 0);
105     bcgbl->c_bio_free(BIO_get_data(bio));
106 
107     return 1;
108 }
109 
110 static const BIO_METHOD corebiometh = {
111     BIO_TYPE_CORE_TO_PROV,
112     "BIO to Core filter",
113     bio_core_write_ex,
114     NULL,
115     bio_core_read_ex,
116     NULL,
117     bio_core_puts,
118     bio_core_gets,
119     bio_core_ctrl,
120     bio_core_new,
121     bio_core_free,
122     NULL,
123 };
124 
BIO_s_core(void)125 const BIO_METHOD *BIO_s_core(void)
126 {
127     return &corebiometh;
128 }
129 
BIO_new_from_core_bio(OSSL_LIB_CTX * libctx,OSSL_CORE_BIO * corebio)130 BIO *BIO_new_from_core_bio(OSSL_LIB_CTX *libctx, OSSL_CORE_BIO *corebio)
131 {
132     BIO *outbio;
133     BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
134 
135     /* Check the library context has been initialised with the callbacks */
136     if (bcgbl->c_bio_write_ex == NULL && bcgbl->c_bio_read_ex == NULL)
137         return NULL;
138 
139     if ((outbio = BIO_new_ex(libctx, BIO_s_core())) == NULL)
140         return NULL;
141 
142     if (!bcgbl->c_bio_up_ref(corebio)) {
143         BIO_free(outbio);
144         return NULL;
145     }
146     BIO_set_data(outbio, corebio);
147     return outbio;
148 }
149 
ossl_bio_init_core(OSSL_LIB_CTX * libctx,const OSSL_DISPATCH * fns)150 int ossl_bio_init_core(OSSL_LIB_CTX *libctx, const OSSL_DISPATCH *fns)
151 {
152     BIO_CORE_GLOBALS *bcgbl = get_globals(libctx);
153 
154     for (; fns->function_id != 0; fns++) {
155         switch (fns->function_id) {
156         case OSSL_FUNC_BIO_READ_EX:
157             if (bcgbl->c_bio_read_ex == NULL)
158                 bcgbl->c_bio_read_ex = OSSL_FUNC_BIO_read_ex(fns);
159             break;
160         case OSSL_FUNC_BIO_WRITE_EX:
161             if (bcgbl->c_bio_write_ex == NULL)
162                 bcgbl->c_bio_write_ex = OSSL_FUNC_BIO_write_ex(fns);
163             break;
164         case OSSL_FUNC_BIO_GETS:
165             if (bcgbl->c_bio_gets == NULL)
166                 bcgbl->c_bio_gets = OSSL_FUNC_BIO_gets(fns);
167             break;
168         case OSSL_FUNC_BIO_PUTS:
169             if (bcgbl->c_bio_puts == NULL)
170                 bcgbl->c_bio_puts = OSSL_FUNC_BIO_puts(fns);
171             break;
172         case OSSL_FUNC_BIO_CTRL:
173             if (bcgbl->c_bio_ctrl == NULL)
174                 bcgbl->c_bio_ctrl = OSSL_FUNC_BIO_ctrl(fns);
175             break;
176         case OSSL_FUNC_BIO_UP_REF:
177             if (bcgbl->c_bio_up_ref == NULL)
178                 bcgbl->c_bio_up_ref = OSSL_FUNC_BIO_up_ref(fns);
179             break;
180         case OSSL_FUNC_BIO_FREE:
181             if (bcgbl->c_bio_free == NULL)
182                 bcgbl->c_bio_free = OSSL_FUNC_BIO_free(fns);
183             break;
184         }
185     }
186 
187     return 1;
188 }
189