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