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