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