Coverage Report

Created: 2018-08-29 13:53

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