/src/openssl/crypto/bio/bio_meth.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2016-2023 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 "bio_local.h" | 
| 11 |  | #include "internal/thread_once.h" | 
| 12 |  |  | 
| 13 |  | CRYPTO_REF_COUNT bio_type_count; | 
| 14 |  | static CRYPTO_ONCE bio_type_init = CRYPTO_ONCE_STATIC_INIT; | 
| 15 |  |  | 
| 16 |  | DEFINE_RUN_ONCE_STATIC(do_bio_type_init) | 
| 17 | 0 | { | 
| 18 | 0 |     return CRYPTO_NEW_REF(&bio_type_count, BIO_TYPE_START); | 
| 19 | 0 | } | 
| 20 |  |  | 
| 21 |  | int BIO_get_new_index(void) | 
| 22 | 0 | { | 
| 23 | 0 |     int newval; | 
| 24 |  | 
 | 
| 25 | 0 |     if (!RUN_ONCE(&bio_type_init, do_bio_type_init)) { | 
| 26 |  |         /* Perhaps the error should be raised in do_bio_type_init()? */ | 
| 27 | 0 |         ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB); | 
| 28 | 0 |         return -1; | 
| 29 | 0 |     } | 
| 30 | 0 |     if (!CRYPTO_UP_REF(&bio_type_count, &newval)) | 
| 31 | 0 |         return -1; | 
| 32 | 0 |     return newval; | 
| 33 | 0 | } | 
| 34 |  |  | 
| 35 |  | BIO_METHOD *BIO_meth_new(int type, const char *name) | 
| 36 | 1 | { | 
| 37 | 1 |     BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD)); | 
| 38 |  |  | 
| 39 | 1 |     if (biom == NULL | 
| 40 | 1 |             || (biom->name = OPENSSL_strdup(name)) == NULL) { | 
| 41 | 0 |         OPENSSL_free(biom); | 
| 42 | 0 |         return NULL; | 
| 43 | 0 |     } | 
| 44 | 1 |     biom->type = type; | 
| 45 | 1 |     return biom; | 
| 46 | 1 | } | 
| 47 |  |  | 
| 48 |  | void BIO_meth_free(BIO_METHOD *biom) | 
| 49 | 1 | { | 
| 50 | 1 |     if (biom != NULL) { | 
| 51 | 1 |         OPENSSL_free(biom->name); | 
| 52 | 1 |         OPENSSL_free(biom); | 
| 53 | 1 |     } | 
| 54 | 1 | } | 
| 55 |  |  | 
| 56 |  | int (*BIO_meth_get_write(const BIO_METHOD *biom)) (BIO *, const char *, int) | 
| 57 | 0 | { | 
| 58 | 0 |     return biom->bwrite_old; | 
| 59 | 0 | } | 
| 60 |  |  | 
| 61 |  | int (*BIO_meth_get_write_ex(const BIO_METHOD *biom)) (BIO *, const char *, size_t, | 
| 62 |  |                                                 size_t *) | 
| 63 | 0 | { | 
| 64 | 0 |     return biom->bwrite; | 
| 65 | 0 | } | 
| 66 |  |  | 
| 67 |  | /* Conversion for old style bwrite to new style */ | 
| 68 |  | int bwrite_conv(BIO *bio, const char *data, size_t datal, size_t *written) | 
| 69 | 14.4M | { | 
| 70 | 14.4M |     int ret; | 
| 71 |  |  | 
| 72 | 14.4M |     if (datal > INT_MAX) | 
| 73 | 0 |         datal = INT_MAX; | 
| 74 |  |  | 
| 75 | 14.4M |     ret = bio->method->bwrite_old(bio, data, (int)datal); | 
| 76 |  |  | 
| 77 | 14.4M |     if (ret <= 0) { | 
| 78 | 0 |         *written = 0; | 
| 79 | 0 |         return ret; | 
| 80 | 0 |     } | 
| 81 |  |  | 
| 82 | 14.4M |     *written = (size_t)ret; | 
| 83 |  |  | 
| 84 | 14.4M |     return 1; | 
| 85 | 14.4M | } | 
| 86 |  |  | 
| 87 |  | int BIO_meth_set_write(BIO_METHOD *biom, | 
| 88 |  |                        int (*bwrite) (BIO *, const char *, int)) | 
| 89 | 0 | { | 
| 90 | 0 |     biom->bwrite_old = bwrite; | 
| 91 | 0 |     biom->bwrite = bwrite_conv; | 
| 92 | 0 |     return 1; | 
| 93 | 0 | } | 
| 94 |  |  | 
| 95 |  | int BIO_meth_set_write_ex(BIO_METHOD *biom, | 
| 96 |  |                        int (*bwrite) (BIO *, const char *, size_t, size_t *)) | 
| 97 | 1 | { | 
| 98 | 1 |     biom->bwrite_old = NULL; | 
| 99 | 1 |     biom->bwrite = bwrite; | 
| 100 | 1 |     return 1; | 
| 101 | 1 | } | 
| 102 |  |  | 
| 103 |  | int (*BIO_meth_get_read(const BIO_METHOD *biom)) (BIO *, char *, int) | 
| 104 | 0 | { | 
| 105 | 0 |     return biom->bread_old; | 
| 106 | 0 | } | 
| 107 |  |  | 
| 108 |  | int (*BIO_meth_get_read_ex(const BIO_METHOD *biom)) (BIO *, char *, size_t, size_t *) | 
| 109 | 0 | { | 
| 110 | 0 |     return biom->bread; | 
| 111 | 0 | } | 
| 112 |  |  | 
| 113 |  | /* Conversion for old style bread to new style */ | 
| 114 |  | int bread_conv(BIO *bio, char *data, size_t datal, size_t *readbytes) | 
| 115 | 20.7M | { | 
| 116 | 20.7M |     int ret; | 
| 117 |  |  | 
| 118 | 20.7M |     if (datal > INT_MAX) | 
| 119 | 0 |         datal = INT_MAX; | 
| 120 |  |  | 
| 121 | 20.7M |     ret = bio->method->bread_old(bio, data, (int)datal); | 
| 122 |  |  | 
| 123 | 20.7M |     if (ret <= 0) { | 
| 124 | 4.28k |         *readbytes = 0; | 
| 125 | 4.28k |         return ret; | 
| 126 | 4.28k |     } | 
| 127 |  |  | 
| 128 | 20.6M |     *readbytes = (size_t)ret; | 
| 129 |  |  | 
| 130 | 20.6M |     return 1; | 
| 131 | 20.7M | } | 
| 132 |  |  | 
| 133 |  | int BIO_meth_set_read(BIO_METHOD *biom, | 
| 134 |  |                       int (*bread) (BIO *, char *, int)) | 
| 135 | 0 | { | 
| 136 | 0 |     biom->bread_old = bread; | 
| 137 | 0 |     biom->bread = bread_conv; | 
| 138 | 0 |     return 1; | 
| 139 | 0 | } | 
| 140 |  |  | 
| 141 |  | int BIO_meth_set_read_ex(BIO_METHOD *biom, | 
| 142 |  |                          int (*bread) (BIO *, char *, size_t, size_t *)) | 
| 143 | 1 | { | 
| 144 | 1 |     biom->bread_old = NULL; | 
| 145 | 1 |     biom->bread = bread; | 
| 146 | 1 |     return 1; | 
| 147 | 1 | } | 
| 148 |  |  | 
| 149 |  | int (*BIO_meth_get_puts(const BIO_METHOD *biom)) (BIO *, const char *) | 
| 150 | 0 | { | 
| 151 | 0 |     return biom->bputs; | 
| 152 | 0 | } | 
| 153 |  |  | 
| 154 |  | int BIO_meth_set_puts(BIO_METHOD *biom, | 
| 155 |  |                       int (*bputs) (BIO *, const char *)) | 
| 156 | 1 | { | 
| 157 | 1 |     biom->bputs = bputs; | 
| 158 | 1 |     return 1; | 
| 159 | 1 | } | 
| 160 |  |  | 
| 161 |  | int (*BIO_meth_get_gets(const BIO_METHOD *biom)) (BIO *, char *, int) | 
| 162 | 0 | { | 
| 163 | 0 |     return biom->bgets; | 
| 164 | 0 | } | 
| 165 |  |  | 
| 166 |  | int BIO_meth_set_gets(BIO_METHOD *biom, | 
| 167 |  |                       int (*bgets) (BIO *, char *, int)) | 
| 168 | 1 | { | 
| 169 | 1 |     biom->bgets = bgets; | 
| 170 | 1 |     return 1; | 
| 171 | 1 | } | 
| 172 |  |  | 
| 173 |  | long (*BIO_meth_get_ctrl(const BIO_METHOD *biom)) (BIO *, int, long, void *) | 
| 174 | 0 | { | 
| 175 | 0 |     return biom->ctrl; | 
| 176 | 0 | } | 
| 177 |  |  | 
| 178 |  | int BIO_meth_set_ctrl(BIO_METHOD *biom, | 
| 179 |  |                       long (*ctrl) (BIO *, int, long, void *)) | 
| 180 | 1 | { | 
| 181 | 1 |     biom->ctrl = ctrl; | 
| 182 | 1 |     return 1; | 
| 183 | 1 | } | 
| 184 |  |  | 
| 185 |  | int (*BIO_meth_get_create(const BIO_METHOD *biom)) (BIO *) | 
| 186 | 0 | { | 
| 187 | 0 |     return biom->create; | 
| 188 | 0 | } | 
| 189 |  |  | 
| 190 |  | int BIO_meth_set_create(BIO_METHOD *biom, int (*create) (BIO *)) | 
| 191 | 1 | { | 
| 192 | 1 |     biom->create = create; | 
| 193 | 1 |     return 1; | 
| 194 | 1 | } | 
| 195 |  |  | 
| 196 |  | int (*BIO_meth_get_destroy(const BIO_METHOD *biom)) (BIO *) | 
| 197 | 0 | { | 
| 198 | 0 |     return biom->destroy; | 
| 199 | 0 | } | 
| 200 |  |  | 
| 201 |  | int BIO_meth_set_destroy(BIO_METHOD *biom, int (*destroy) (BIO *)) | 
| 202 | 1 | { | 
| 203 | 1 |     biom->destroy = destroy; | 
| 204 | 1 |     return 1; | 
| 205 | 1 | } | 
| 206 |  |  | 
| 207 |  | long (*BIO_meth_get_callback_ctrl(const BIO_METHOD *biom)) (BIO *, int, BIO_info_cb *) | 
| 208 | 0 | { | 
| 209 | 0 |     return biom->callback_ctrl; | 
| 210 | 0 | } | 
| 211 |  |  | 
| 212 |  | int BIO_meth_set_callback_ctrl(BIO_METHOD *biom, | 
| 213 |  |                                long (*callback_ctrl) (BIO *, int, | 
| 214 |  |                                                       BIO_info_cb *)) | 
| 215 | 0 | { | 
| 216 | 0 |     biom->callback_ctrl = callback_ctrl; | 
| 217 | 0 |     return 1; | 
| 218 | 0 | } | 
| 219 |  |  | 
| 220 |  | int BIO_meth_set_sendmmsg(BIO_METHOD *biom, | 
| 221 |  |                           int (*bsendmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)) | 
| 222 | 0 | { | 
| 223 | 0 |     biom->bsendmmsg = bsendmmsg; | 
| 224 | 0 |     return 1; | 
| 225 | 0 | } | 
| 226 |  |  | 
| 227 | 0 | int (*BIO_meth_get_sendmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) { | 
| 228 | 0 |     return biom->bsendmmsg; | 
| 229 | 0 | } | 
| 230 |  |  | 
| 231 |  | int BIO_meth_set_recvmmsg(BIO_METHOD *biom, | 
| 232 |  |                           int (*brecvmmsg) (BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *)) | 
| 233 | 0 | { | 
| 234 | 0 |     biom->brecvmmsg = brecvmmsg; | 
| 235 | 0 |     return 1; | 
| 236 | 0 | } | 
| 237 |  |  | 
| 238 | 0 | int (*BIO_meth_get_recvmmsg(const BIO_METHOD *biom))(BIO *, BIO_MSG *, size_t, size_t, uint64_t, size_t *) { | 
| 239 | 0 |     return biom->brecvmmsg; | 
| 240 | 0 | } |