Update copyright year
[oweals/openssl.git] / crypto / bio / bio_lib.c
1 /*
2  * Copyright 1995-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 <stdio.h>
11 #include <errno.h>
12 #include <openssl/crypto.h>
13 #include "bio_lcl.h"
14 #include "internal/cryptlib.h"
15
16 BIO *BIO_new(const BIO_METHOD *method)
17 {
18     BIO *bio = OPENSSL_zalloc(sizeof(*bio));
19
20     if (bio == NULL) {
21         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
22         return (NULL);
23     }
24
25     bio->method = method;
26     bio->shutdown = 1;
27     bio->references = 1;
28
29     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
30         goto err;
31
32     bio->lock = CRYPTO_THREAD_lock_new();
33     if (bio->lock == NULL) {
34         BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
35         CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
36         goto err;
37     }
38
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;
44     }
45     if (method->create == NULL)
46         bio->init = 1;
47
48     return bio;
49
50 err:
51     OPENSSL_free(bio);
52     return NULL;
53 }
54
55 int BIO_free(BIO *a)
56 {
57     int i;
58
59     if (a == NULL)
60         return 0;
61
62     if (CRYPTO_atomic_add(&a->references, -1, &i, a->lock) <= 0)
63         return 0;
64
65     REF_PRINT_COUNT("BIO", a);
66     if (i > 0)
67         return 1;
68     REF_ASSERT_ISNT(i < 0);
69     if ((a->callback != NULL) &&
70         ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
71         return i;
72
73     if ((a->method != NULL) && (a->method->destroy != NULL))
74         a->method->destroy(a);
75
76     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
77
78     CRYPTO_THREAD_lock_free(a->lock);
79
80     OPENSSL_free(a);
81
82     return 1;
83 }
84
85 void BIO_set_data(BIO *a, void *ptr)
86 {
87     a->ptr = ptr;
88 }
89
90 void *BIO_get_data(BIO *a)
91 {
92     return a->ptr;
93 }
94
95 void BIO_set_init(BIO *a, int init)
96 {
97     a->init = init;
98 }
99
100 int BIO_get_init(BIO *a)
101 {
102     return a->init;
103 }
104
105 void BIO_set_shutdown(BIO *a, int shut)
106 {
107     a->shutdown = shut;
108 }
109
110 int BIO_get_shutdown(BIO *a)
111 {
112     return a->shutdown;
113 }
114
115 void BIO_vfree(BIO *a)
116 {
117     BIO_free(a);
118 }
119
120 int BIO_up_ref(BIO *a)
121 {
122     int i;
123
124     if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
125         return 0;
126
127     REF_PRINT_COUNT("BIO", a);
128     REF_ASSERT_ISNT(i < 2);
129     return ((i > 1) ? 1 : 0);
130 }
131
132 void BIO_clear_flags(BIO *b, int flags)
133 {
134     b->flags &= ~flags;
135 }
136
137 int BIO_test_flags(const BIO *b, int flags)
138 {
139     return (b->flags & flags);
140 }
141
142 void BIO_set_flags(BIO *b, int flags)
143 {
144     b->flags |= flags;
145 }
146
147 long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
148                                         int, long, long) {
149     return b->callback;
150 }
151
152 void BIO_set_callback(BIO *b,
153                       long (*cb) (struct bio_st *, int, const char *, int,
154                                   long, long))
155 {
156     b->callback = cb;
157 }
158
159 void BIO_set_callback_arg(BIO *b, char *arg)
160 {
161     b->cb_arg = arg;
162 }
163
164 char *BIO_get_callback_arg(const BIO *b)
165 {
166     return b->cb_arg;
167 }
168
169 const char *BIO_method_name(const BIO *b)
170 {
171     return b->method->name;
172 }
173
174 int BIO_method_type(const BIO *b)
175 {
176     return b->method->type;
177 }
178
179 int BIO_read(BIO *b, void *out, int outl)
180 {
181     int i;
182     long (*cb) (BIO *, int, const char *, int, long, long);
183
184     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
185         BIOerr(BIO_F_BIO_READ, BIO_R_UNSUPPORTED_METHOD);
186         return (-2);
187     }
188
189     cb = b->callback;
190     if ((cb != NULL) &&
191         ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
192         return (i);
193
194     if (!b->init) {
195         BIOerr(BIO_F_BIO_READ, BIO_R_UNINITIALIZED);
196         return (-2);
197     }
198
199     i = b->method->bread(b, out, outl);
200
201     if (i > 0)
202         b->num_read += (uint64_t)i;
203
204     if (cb != NULL)
205         i = (int)cb(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0L, (long)i);
206     return (i);
207 }
208
209 int BIO_write(BIO *b, const void *in, int inl)
210 {
211     int i;
212     long (*cb) (BIO *, int, const char *, int, long, long);
213
214     if (b == NULL)
215         return (0);
216
217     cb = b->callback;
218     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
219         BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
220         return (-2);
221     }
222
223     if ((cb != NULL) &&
224         ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
225         return (i);
226
227     if (!b->init) {
228         BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
229         return (-2);
230     }
231
232     i = b->method->bwrite(b, in, inl);
233
234     if (i > 0)
235         b->num_write += (uint64_t)i;
236
237     if (cb != NULL)
238         i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
239     return (i);
240 }
241
242 int BIO_puts(BIO *b, const char *in)
243 {
244     int i;
245     long (*cb) (BIO *, int, const char *, int, long, long);
246
247     if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
248         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
249         return (-2);
250     }
251
252     cb = b->callback;
253
254     if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
255         return (i);
256
257     if (!b->init) {
258         BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
259         return (-2);
260     }
261
262     i = b->method->bputs(b, in);
263
264     if (i > 0)
265         b->num_write += (uint64_t)i;
266
267     if (cb != NULL)
268         i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
269     return (i);
270 }
271
272 int BIO_gets(BIO *b, char *in, int inl)
273 {
274     int i;
275     long (*cb) (BIO *, int, const char *, int, long, long);
276
277     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
278         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
279         return (-2);
280     }
281
282     cb = b->callback;
283
284     if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
285         return (i);
286
287     if (!b->init) {
288         BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
289         return (-2);
290     }
291
292     i = b->method->bgets(b, in, inl);
293
294     if (cb != NULL)
295         i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
296     return (i);
297 }
298
299 int BIO_indent(BIO *b, int indent, int max)
300 {
301     if (indent < 0)
302         indent = 0;
303     if (indent > max)
304         indent = max;
305     while (indent--)
306         if (BIO_puts(b, " ") != 1)
307             return 0;
308     return 1;
309 }
310
311 long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
312 {
313     int i;
314
315     i = iarg;
316     return (BIO_ctrl(b, cmd, larg, (char *)&i));
317 }
318
319 void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
320 {
321     void *p = NULL;
322
323     if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
324         return (NULL);
325     else
326         return (p);
327 }
328
329 long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
330 {
331     long ret;
332     long (*cb) (BIO *, int, const char *, int, long, long);
333
334     if (b == NULL)
335         return (0);
336
337     if ((b->method == NULL) || (b->method->ctrl == NULL)) {
338         BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
339         return (-2);
340     }
341
342     cb = b->callback;
343
344     if ((cb != NULL) &&
345         ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
346         return (ret);
347
348     ret = b->method->ctrl(b, cmd, larg, parg);
349
350     if (cb != NULL)
351         ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
352     return (ret);
353 }
354
355 long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
356 {
357     long ret;
358     long (*cb) (BIO *, int, const char *, int, long, long);
359
360     if (b == NULL)
361         return (0);
362
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     }
367
368     cb = b->callback;
369
370     if ((cb != NULL) &&
371         ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
372         return (ret);
373
374     ret = b->method->callback_ctrl(b, cmd, fp);
375
376     if (cb != NULL)
377         ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
378     return (ret);
379 }
380
381 /*
382  * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
383  * do; but those macros have inappropriate return type, and for interfacing
384  * from other programming languages, C macros aren't much of a help anyway.
385  */
386 size_t BIO_ctrl_pending(BIO *bio)
387 {
388     return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
389 }
390
391 size_t BIO_ctrl_wpending(BIO *bio)
392 {
393     return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
394 }
395
396 /* put the 'bio' on the end of b's list of operators */
397 BIO *BIO_push(BIO *b, BIO *bio)
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 }
413
414 /* Remove the first and return the rest */
415 BIO *BIO_pop(BIO *b)
416 {
417     BIO *ret;
418
419     if (b == NULL)
420         return (NULL);
421     ret = b->next_bio;
422
423     BIO_ctrl(b, BIO_CTRL_POP, 0, b);
424
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;
429
430     b->next_bio = NULL;
431     b->prev_bio = NULL;
432     return (ret);
433 }
434
435 BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
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 }
452
453 int BIO_get_retry_reason(BIO *bio)
454 {
455     return (bio->retry_reason);
456 }
457
458 void BIO_set_retry_reason(BIO *bio, int reason)
459 {
460     bio->retry_reason = reason;
461 }
462
463 BIO *BIO_find_type(BIO *bio, int type)
464 {
465     int mt, mask;
466
467     if (bio == NULL)
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 }
484
485 BIO *BIO_next(BIO *b)
486 {
487     if (b == NULL)
488         return NULL;
489     return b->next_bio;
490 }
491
492 void BIO_set_next(BIO *b, BIO *next)
493 {
494     b->next_bio = next;
495 }
496
497 void BIO_free_all(BIO *bio)
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 }
512
513 BIO *BIO_dup_chain(BIO *in)
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,
536                                 &bio->ex_data)) {
537             BIO_free(new_bio);
538             goto err;
539         }
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:
551     BIO_free_all(ret);
552
553     return (NULL);
554 }
555
556 void BIO_copy_next_retry(BIO *b)
557 {
558     BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
559     b->retry_reason = b->next_bio->retry_reason;
560 }
561
562 int BIO_set_ex_data(BIO *bio, int idx, void *data)
563 {
564     return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
565 }
566
567 void *BIO_get_ex_data(BIO *bio, int idx)
568 {
569     return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
570 }
571
572 uint64_t BIO_number_read(BIO *bio)
573 {
574     if (bio)
575         return bio->num_read;
576     return 0;
577 }
578
579 uint64_t BIO_number_written(BIO *bio)
580 {
581     if (bio)
582         return bio->num_write;
583     return 0;
584 }
585
586 void bio_free_ex_data(BIO *bio)
587 {
588     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
589 }
590
591 void 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;
597 #endif
598     CRYPTO_THREAD_lock_free(bio_type_lock);
599     bio_type_lock = NULL;
600 }