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