]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/bio/bio_lib.c
Fix a double free in ca command line
[thirdparty/openssl.git] / crypto / bio / bio_lib.c
CommitLineData
b1322259
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
b1322259
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
11#include <errno.h>
ec577822 12#include <openssl/crypto.h>
a146ae55 13#include "bio_lcl.h"
b39fc560 14#include "internal/cryptlib.h"
58964a49 15
04f6b0fd 16BIO *BIO_new(const BIO_METHOD *method)
0f113f3e 17{
9d7bfb14 18 BIO *bio = OPENSSL_zalloc(sizeof(*bio));
0f113f3e 19
9d7bfb14 20 if (bio == NULL) {
0f113f3e
MC
21 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
22 return (NULL);
23 }
d02b48c6 24
0f113f3e 25 bio->method = method;
0f113f3e 26 bio->shutdown = 1;
0f113f3e 27 bio->references = 1;
9d7bfb14 28
25a807bc 29 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
9d7bfb14 30 goto err;
fb46be03
AG
31
32 bio->lock = CRYPTO_THREAD_lock_new();
33 if (bio->lock == NULL) {
9d7bfb14 34 BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
fb46be03 35 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
9d7bfb14 36 goto err;
fb46be03
AG
37 }
38
9d7bfb14
F
39 if (method->create != NULL && !method->create(bio)) {
40 BIOerr(BIO_F_BIO_NEW, ERR_R_INIT_FAIL);
41 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
42 CRYPTO_THREAD_lock_free(bio->lock);
43 goto err;
fb46be03
AG
44 }
45
9d7bfb14
F
46 return bio;
47
48err:
49 OPENSSL_free(bio);
50 return NULL;
0f113f3e 51}
d02b48c6 52
6b691a5c 53int BIO_free(BIO *a)
0f113f3e
MC
54{
55 int i;
d02b48c6 56
0f113f3e 57 if (a == NULL)
fb46be03
AG
58 return 0;
59
60 if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0)
61 return 0;
d02b48c6 62
f3f1cf84 63 REF_PRINT_COUNT("BIO", a);
0f113f3e 64 if (i > 0)
fb46be03 65 return 1;
f3f1cf84 66 REF_ASSERT_ISNT(i < 0);
0f113f3e
MC
67 if ((a->callback != NULL) &&
68 ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
fb46be03 69 return i;
d02b48c6 70
a14a740d
F
71 if ((a->method != NULL) && (a->method->destroy != NULL))
72 a->method->destroy(a);
73
0f113f3e 74 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
58964a49 75
fb46be03
AG
76 CRYPTO_THREAD_lock_free(a->lock);
77
0f113f3e 78 OPENSSL_free(a);
fb46be03
AG
79
80 return 1;
0f113f3e 81}
d02b48c6 82
a146ae55
MC
83void BIO_set_data(BIO *a, void *ptr)
84{
85 a->ptr = ptr;
86}
87
88void *BIO_get_data(BIO *a)
89{
90 return a->ptr;
91}
92
93void BIO_set_init(BIO *a, int init)
94{
95 a->init = init;
96}
97
98int BIO_get_init(BIO *a)
99{
100 return a->init;
101}
102
103void BIO_set_shutdown(BIO *a, int shut)
104{
105 a->shutdown = shut;
106}
107
108int BIO_get_shutdown(BIO *a)
109{
110 return a->shutdown;
111}
112
371acb22 113void BIO_vfree(BIO *a)
0f113f3e
MC
114{
115 BIO_free(a);
116}
371acb22 117
fb46be03
AG
118int BIO_up_ref(BIO *a)
119{
120 int i;
121
122 if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
123 return 0;
124
125 REF_PRINT_COUNT("BIO", a);
126 REF_ASSERT_ISNT(i < 2);
127 return ((i > 1) ? 1 : 0);
128}
129
7806f3dd 130void BIO_clear_flags(BIO *b, int flags)
0f113f3e
MC
131{
132 b->flags &= ~flags;
133}
134
135int BIO_test_flags(const BIO *b, int flags)
136{
137 return (b->flags & flags);
138}
139
140void BIO_set_flags(BIO *b, int flags)
141{
142 b->flags |= flags;
143}
144
145long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
146 int, long, long) {
147 return b->callback;
148}
149
150void BIO_set_callback(BIO *b,
151 long (*cb) (struct bio_st *, int, const char *, int,
152 long, long))
153{
154 b->callback = cb;
155}
7806f3dd
NL
156
157void BIO_set_callback_arg(BIO *b, char *arg)
0f113f3e
MC
158{
159 b->cb_arg = arg;
160}
7806f3dd 161
0f113f3e
MC
162char *BIO_get_callback_arg(const BIO *b)
163{
164 return b->cb_arg;
165}
7806f3dd 166
0f113f3e
MC
167const char *BIO_method_name(const BIO *b)
168{
169 return b->method->name;
170}
7806f3dd
NL
171
172int BIO_method_type(const BIO *b)
0f113f3e
MC
173{
174 return b->method->type;
175}
7806f3dd 176
61f5b6f3 177int BIO_read(BIO *b, void *out, int outl)
0f113f3e
MC
178{
179 int i;
180 long (*cb) (BIO *, int, const char *, int, long, long);
d02b48c6 181
0f113f3e
MC
182 if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
183 BIOerr(BIO_F_BIO_READ, BIO_R_UNSUPPORTED_METHOD);
184 return (-2);
185 }
d02b48c6 186
0f113f3e
MC
187 cb = b->callback;
188 if ((cb != NULL) &&
189 ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
190 return (i);
d02b48c6 191
0f113f3e
MC
192 if (!b->init) {
193 BIOerr(BIO_F_BIO_READ, BIO_R_UNINITIALIZED);
194 return (-2);
195 }
d02b48c6 196
0f113f3e 197 i = b->method->bread(b, out, outl);
dfeab068 198
0f113f3e 199 if (i > 0)
b8b12aad 200 b->num_read += (uint64_t)i;
d02b48c6 201
0f113f3e
MC
202 if (cb != NULL)
203 i = (int)cb(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0L, (long)i);
204 return (i);
205}
d02b48c6 206
6343829a 207int BIO_write(BIO *b, const void *in, int inl)
0f113f3e
MC
208{
209 int i;
210 long (*cb) (BIO *, int, const char *, int, long, long);
58964a49 211
0f113f3e
MC
212 if (b == NULL)
213 return (0);
d02b48c6 214
0f113f3e
MC
215 cb = b->callback;
216 if ((b->method == NULL) || (b->method->bwrite == NULL)) {
217 BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
218 return (-2);
219 }
d02b48c6 220
0f113f3e
MC
221 if ((cb != NULL) &&
222 ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
223 return (i);
d02b48c6 224
0f113f3e
MC
225 if (!b->init) {
226 BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
227 return (-2);
228 }
d02b48c6 229
0f113f3e 230 i = b->method->bwrite(b, in, inl);
dfeab068 231
0f113f3e 232 if (i > 0)
b8b12aad 233 b->num_write += (uint64_t)i;
d02b48c6 234
0f113f3e
MC
235 if (cb != NULL)
236 i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
237 return (i);
238}
d02b48c6 239
6b691a5c 240int BIO_puts(BIO *b, const char *in)
0f113f3e
MC
241{
242 int i;
243 long (*cb) (BIO *, int, const char *, int, long, long);
d02b48c6 244
0f113f3e
MC
245 if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
246 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
247 return (-2);
248 }
d02b48c6 249
0f113f3e 250 cb = b->callback;
58964a49 251
0f113f3e
MC
252 if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
253 return (i);
d02b48c6 254
0f113f3e
MC
255 if (!b->init) {
256 BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
257 return (-2);
258 }
d02b48c6 259
0f113f3e 260 i = b->method->bputs(b, in);
d02b48c6 261
0f113f3e 262 if (i > 0)
b8b12aad 263 b->num_write += (uint64_t)i;
7d95ff76 264
0f113f3e
MC
265 if (cb != NULL)
266 i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
267 return (i);
268}
d02b48c6 269
6b691a5c 270int BIO_gets(BIO *b, char *in, int inl)
0f113f3e
MC
271{
272 int i;
273 long (*cb) (BIO *, int, const char *, int, long, long);
274
275 if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
276 BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
277 return (-2);
278 }
279
280 cb = b->callback;
281
282 if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
283 return (i);
284
285 if (!b->init) {
286 BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
287 return (-2);
288 }
289
290 i = b->method->bgets(b, in, inl);
291
292 if (cb != NULL)
293 i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
294 return (i);
295}
296
297int BIO_indent(BIO *b, int indent, int max)
298{
299 if (indent < 0)
300 indent = 0;
301 if (indent > max)
302 indent = max;
303 while (indent--)
304 if (BIO_puts(b, " ") != 1)
305 return 0;
306 return 1;
307}
54a656ef 308
6b691a5c 309long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
0f113f3e
MC
310{
311 int i;
d02b48c6 312
0f113f3e
MC
313 i = iarg;
314 return (BIO_ctrl(b, cmd, larg, (char *)&i));
315}
d02b48c6 316
417be660 317void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
0f113f3e 318{
417be660 319 void *p = NULL;
58964a49 320
0f113f3e
MC
321 if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
322 return (NULL);
323 else
324 return (p);
325}
58964a49 326
95d29597 327long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
0f113f3e
MC
328{
329 long ret;
330 long (*cb) (BIO *, int, const char *, int, long, long);
d02b48c6 331
0f113f3e
MC
332 if (b == NULL)
333 return (0);
d02b48c6 334
0f113f3e
MC
335 if ((b->method == NULL) || (b->method->ctrl == NULL)) {
336 BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
337 return (-2);
338 }
d02b48c6 339
0f113f3e 340 cb = b->callback;
58964a49 341
0f113f3e
MC
342 if ((cb != NULL) &&
343 ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
344 return (ret);
d02b48c6 345
0f113f3e 346 ret = b->method->ctrl(b, cmd, larg, parg);
d02b48c6 347
0f113f3e
MC
348 if (cb != NULL)
349 ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
350 return (ret);
351}
d02b48c6 352
0f113f3e
MC
353long BIO_callback_ctrl(BIO *b, int cmd,
354 void (*fp) (struct bio_st *, int, const char *, int,
355 long, long))
356{
357 long ret;
358 long (*cb) (BIO *, int, const char *, int, long, long);
d3442bc7 359
0f113f3e
MC
360 if (b == NULL)
361 return (0);
d3442bc7 362
0f113f3e
MC
363 if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
364 BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
365 return (-2);
366 }
d3442bc7 367
0f113f3e 368 cb = b->callback;
d3442bc7 369
0f113f3e
MC
370 if ((cb != NULL) &&
371 ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
372 return (ret);
d3442bc7 373
0f113f3e 374 ret = b->method->callback_ctrl(b, cmd, fp);
d3442bc7 375
0f113f3e
MC
376 if (cb != NULL)
377 ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
378 return (ret);
379}
d3442bc7 380
0f113f3e
MC
381/*
382 * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
95d29597 383 * do; but those macros have inappropriate return type, and for interfacing
0f113f3e
MC
384 * from other programming languages, C macros aren't much of a help anyway.
385 */
95d29597 386size_t BIO_ctrl_pending(BIO *bio)
0f113f3e
MC
387{
388 return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
389}
95d29597
BM
390
391size_t BIO_ctrl_wpending(BIO *bio)
0f113f3e
MC
392{
393 return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
394}
95d29597 395
d02b48c6 396/* put the 'bio' on the end of b's list of operators */
6b691a5c 397BIO *BIO_push(BIO *b, BIO *bio)
0f113f3e
MC
398{
399 BIO *lb;
400
401 if (b == NULL)
402 return (bio);
403 lb = b;
404 while (lb->next_bio != NULL)
405 lb = lb->next_bio;
406 lb->next_bio = bio;
407 if (bio != NULL)
408 bio->prev_bio = lb;
409 /* called to do internal processing */
410 BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
411 return (b);
412}
d02b48c6
RE
413
414/* Remove the first and return the rest */
6b691a5c 415BIO *BIO_pop(BIO *b)
0f113f3e
MC
416{
417 BIO *ret;
d02b48c6 418
0f113f3e
MC
419 if (b == NULL)
420 return (NULL);
421 ret = b->next_bio;
d02b48c6 422
0f113f3e 423 BIO_ctrl(b, BIO_CTRL_POP, 0, b);
5d780bab 424
0f113f3e
MC
425 if (b->prev_bio != NULL)
426 b->prev_bio->next_bio = b->next_bio;
427 if (b->next_bio != NULL)
428 b->next_bio->prev_bio = b->prev_bio;
d02b48c6 429
0f113f3e
MC
430 b->next_bio = NULL;
431 b->prev_bio = NULL;
432 return (ret);
433}
d02b48c6 434
6b691a5c 435BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
0f113f3e
MC
436{
437 BIO *b, *last;
438
439 b = last = bio;
440 for (;;) {
441 if (!BIO_should_retry(b))
442 break;
443 last = b;
444 b = b->next_bio;
445 if (b == NULL)
446 break;
447 }
448 if (reason != NULL)
449 *reason = last->retry_reason;
450 return (last);
451}
d02b48c6 452
6b691a5c 453int BIO_get_retry_reason(BIO *bio)
0f113f3e
MC
454{
455 return (bio->retry_reason);
456}
d02b48c6 457
a146ae55
MC
458void BIO_set_retry_reason(BIO *bio, int reason)
459{
460 bio->retry_reason = reason;
461}
462
6b691a5c 463BIO *BIO_find_type(BIO *bio, int type)
0f113f3e
MC
464{
465 int mt, mask;
466
cc99bfa7 467 if (bio == NULL)
0f113f3e
MC
468 return NULL;
469 mask = type & 0xff;
470 do {
471 if (bio->method != NULL) {
472 mt = bio->method->type;
473
474 if (!mask) {
475 if (mt & type)
476 return (bio);
477 } else if (mt == type)
478 return (bio);
479 }
480 bio = bio->next_bio;
481 } while (bio != NULL);
482 return (NULL);
483}
d02b48c6 484
cfd3bb17 485BIO *BIO_next(BIO *b)
0f113f3e 486{
cc99bfa7 487 if (b == NULL)
0f113f3e
MC
488 return NULL;
489 return b->next_bio;
490}
cfd3bb17 491
a146ae55
MC
492void BIO_set_next(BIO *b, BIO *next)
493{
494 b->next_bio = next;
495}
496
6b691a5c 497void BIO_free_all(BIO *bio)
0f113f3e
MC
498{
499 BIO *b;
500 int ref;
501
502 while (bio != NULL) {
503 b = bio;
504 ref = b->references;
505 bio = bio->next_bio;
506 BIO_free(b);
507 /* Since ref count > 1, don't free anyone else. */
508 if (ref > 1)
509 break;
510 }
511}
d02b48c6 512
6b691a5c 513BIO *BIO_dup_chain(BIO *in)
0f113f3e
MC
514{
515 BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
516
517 for (bio = in; bio != NULL; bio = bio->next_bio) {
518 if ((new_bio = BIO_new(bio->method)) == NULL)
519 goto err;
520 new_bio->callback = bio->callback;
521 new_bio->cb_arg = bio->cb_arg;
522 new_bio->init = bio->init;
523 new_bio->shutdown = bio->shutdown;
524 new_bio->flags = bio->flags;
525
526 /* This will let SSL_s_sock() work with stdin/stdout */
527 new_bio->num = bio->num;
528
529 if (!BIO_dup_state(bio, (char *)new_bio)) {
530 BIO_free(new_bio);
531 goto err;
532 }
533
534 /* copy app data */
535 if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
aec54108
MC
536 &bio->ex_data)) {
537 BIO_free(new_bio);
0f113f3e 538 goto err;
aec54108 539 }
0f113f3e
MC
540
541 if (ret == NULL) {
542 eoc = new_bio;
543 ret = eoc;
544 } else {
545 BIO_push(eoc, new_bio);
546 eoc = new_bio;
547 }
548 }
549 return (ret);
550 err:
aec54108
MC
551 BIO_free_all(ret);
552
0f113f3e
MC
553 return (NULL);
554}
d02b48c6 555
6b691a5c 556void BIO_copy_next_retry(BIO *b)
0f113f3e
MC
557{
558 BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
559 b->retry_reason = b->next_bio->retry_reason;
560}
d02b48c6 561
dd9d233e 562int BIO_set_ex_data(BIO *bio, int idx, void *data)
0f113f3e
MC
563{
564 return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
565}
58964a49 566
dd9d233e 567void *BIO_get_ex_data(BIO *bio, int idx)
0f113f3e
MC
568{
569 return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
570}
58964a49 571
b8b12aad 572uint64_t BIO_number_read(BIO *bio)
c3ed3b6e 573{
0f113f3e
MC
574 if (bio)
575 return bio->num_read;
576 return 0;
c3ed3b6e
DSH
577}
578
b8b12aad 579uint64_t BIO_number_written(BIO *bio)
c3ed3b6e 580{
0f113f3e
MC
581 if (bio)
582 return bio->num_write;
583 return 0;
c3ed3b6e 584}
ff234405 585
1ee7b8b9
MC
586void bio_free_ex_data(BIO *bio)
587{
588 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
589}
ff234405
MC
590
591void bio_cleanup(void)
592{
593#ifndef OPENSSL_NO_SOCK
594 bio_sock_cleanup_int();
595 CRYPTO_THREAD_lock_free(bio_lookup_lock);
596 bio_lookup_lock = NULL;
5a7ad1f0 597#endif
8b8d963d
RS
598 CRYPTO_THREAD_lock_free(bio_type_lock);
599 bio_type_lock = NULL;
ff234405 600}