Update copyright year
[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_numbers.h>
12 #include "internal/cryptlib.h"
13 #include "prov/bio.h"
14
15 static OSSL_BIO_new_file_fn *c_bio_new_file = NULL;
16 static OSSL_BIO_new_membuf_fn *c_bio_new_membuf = NULL;
17 static OSSL_BIO_read_ex_fn *c_bio_read_ex = NULL;
18 static OSSL_BIO_write_ex_fn *c_bio_write_ex = NULL;
19 static OSSL_BIO_free_fn *c_bio_free = NULL;
20 static OSSL_BIO_vprintf_fn *c_bio_vprintf = NULL;
21
22 int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
23 {
24     for (; fns->function_id != 0; fns++) {
25         switch (fns->function_id) {
26         case OSSL_FUNC_BIO_NEW_FILE:
27             if (c_bio_new_file == NULL)
28                 c_bio_new_file = OSSL_get_BIO_new_file(fns);
29             break;
30         case OSSL_FUNC_BIO_NEW_MEMBUF:
31             if (c_bio_new_membuf == NULL)
32                 c_bio_new_membuf = OSSL_get_BIO_new_membuf(fns);
33             break;
34         case OSSL_FUNC_BIO_READ_EX:
35             if (c_bio_read_ex == NULL)
36                 c_bio_read_ex = OSSL_get_BIO_read_ex(fns);
37             break;
38         case OSSL_FUNC_BIO_WRITE_EX:
39             if (c_bio_write_ex == NULL)
40                 c_bio_write_ex = OSSL_get_BIO_write_ex(fns);
41             break;
42         case OSSL_FUNC_BIO_FREE:
43             if (c_bio_free == NULL)
44                 c_bio_free = OSSL_get_BIO_free(fns);
45             break;
46         case OSSL_FUNC_BIO_VPRINTF:
47             if (c_bio_vprintf == NULL)
48                 c_bio_vprintf = OSSL_get_BIO_vprintf(fns);
49             break;
50         }
51     }
52
53     return 1;
54 }
55
56 OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
57 {
58     if (c_bio_new_file == NULL)
59         return NULL;
60     return c_bio_new_file(filename, mode);
61 }
62
63 OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
64 {
65     if (c_bio_new_membuf == NULL)
66         return NULL;
67     return c_bio_new_membuf(filename, len);
68 }
69
70 int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
71                           size_t *bytes_read)
72 {
73     if (c_bio_read_ex == NULL)
74         return 0;
75     return c_bio_read_ex(bio, data, data_len, bytes_read);
76 }
77
78 int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
79                            size_t *written)
80 {
81     if (c_bio_write_ex == NULL)
82         return 0;
83     return c_bio_write_ex(bio, data, data_len, written);
84 }
85
86 int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
87 {
88     if (c_bio_free == NULL)
89         return 0;
90     return c_bio_free(bio);
91 }
92
93 int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
94 {
95     if (c_bio_vprintf == NULL)
96         return -1;
97     return c_bio_vprintf(bio, format, ap);
98 }
99
100 int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
101 {
102     va_list ap;
103     int ret;
104
105     va_start(ap, format);
106     ret = ossl_prov_bio_vprintf(bio, format, ap);
107     va_end(ap);
108
109     return ret;
110 }
111
112 #ifndef FIPS_MODULE
113
114 /* No direct BIO support in the FIPS module */
115
116 static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
117                             size_t *bytes_read)
118 {
119     return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
120 }
121
122 static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
123                              size_t *written)
124 {
125     return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
126 }
127
128 static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
129 {
130     /* We don't support this */
131     assert(0);
132     return 0;
133 }
134
135 static int bio_core_gets(BIO *bio, char *buf, int size)
136 {
137     /* We don't support this */
138     assert(0);
139     return -1;
140 }
141
142 static int bio_core_puts(BIO *bio, const char *str)
143 {
144     /* We don't support this */
145     assert(0);
146     return -1;
147 }
148
149 static int bio_core_new(BIO *bio)
150 {
151     BIO_set_init(bio, 1);
152
153     return 1;
154 }
155
156 static int bio_core_free(BIO *bio)
157 {
158     BIO_set_init(bio, 0);
159
160     return 1;
161 }
162
163 BIO_METHOD *bio_prov_init_bio_method(void)
164 {
165     BIO_METHOD *corebiometh = NULL;
166
167     corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
168     if (corebiometh == NULL
169             || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
170             || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
171             || !BIO_meth_set_puts(corebiometh, bio_core_puts)
172             || !BIO_meth_set_gets(corebiometh, bio_core_gets)
173             || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
174             || !BIO_meth_set_create(corebiometh, bio_core_new)
175             || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
176         BIO_meth_free(corebiometh);
177         return NULL;
178     }
179
180     return corebiometh;
181 }
182
183 BIO *bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
184 {
185     BIO *outbio;
186     BIO_METHOD *corebiometh = PROV_CTX_get0_core_bio_method(provctx);
187
188     if (corebiometh == NULL)
189         return NULL;
190
191     outbio = BIO_new(corebiometh);
192     if (outbio != NULL)
193         BIO_set_data(outbio, corebio);
194
195     return outbio;
196 }
197
198 #endif