ustream-ssl: openssl: fix bio memory leak
[project/ustream-ssl.git] / ustream-io-openssl.c
1 /*
2 * ustream-ssl - library for SSL over ustream
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <string.h>
20
21 #include <libubox/ustream.h>
22
23 #include "ustream-ssl.h"
24 #include "openssl_bio_compat.h"
25 #include "ustream-internal.h"
26
27 static int
28 s_ustream_new(BIO *b)
29 {
30 BIO_set_init(b, 1);
31 BIO_set_data(b, NULL);
32 BIO_clear_flags(b, ~0);
33 return 1;
34 }
35
36 static int
37 s_ustream_free(BIO *b)
38 {
39 if (!b)
40 return 0;
41
42 BIO_set_data(b, NULL);
43 BIO_set_init(b, 0);
44 BIO_clear_flags(b, ~0);
45 BIO_meth_free(b->method);
46 return 1;
47 }
48
49 static int
50 s_ustream_read(BIO *b, char *buf, int len)
51 {
52 struct ustream *s;
53 char *sbuf;
54 int slen;
55
56 if (!buf || len <= 0)
57 return 0;
58
59 s = (struct ustream *)BIO_get_data(b);
60 if (!s)
61 return 0;
62
63 sbuf = ustream_get_read_buf(s, &slen);
64
65 BIO_clear_retry_flags(b);
66 if (!slen) {
67 BIO_set_retry_read(b);
68 return -1;
69 }
70
71 if (slen > len)
72 slen = len;
73
74 memcpy(buf, sbuf, slen);
75 ustream_consume(s, slen);
76
77 return slen;
78 }
79
80 static int
81 s_ustream_write(BIO *b, const char *buf, int len)
82 {
83 struct ustream *s;
84
85 if (!buf || len <= 0)
86 return 0;
87
88 s = (struct ustream *)BIO_get_data(b);
89 if (!s)
90 return 0;
91
92 if (s->write_error)
93 return len;
94
95 return ustream_write(s, buf, len, false);
96 }
97
98 static int
99 s_ustream_gets(BIO *b, char *buf, int len)
100 {
101 return -1;
102 }
103
104 static int
105 s_ustream_puts(BIO *b, const char *str)
106 {
107 return s_ustream_write(b, str, strlen(str));
108 }
109
110 static long s_ustream_ctrl(BIO *b, int cmd, long num, void *ptr)
111 {
112 switch (cmd) {
113 case BIO_CTRL_FLUSH:
114 return 1;
115 default:
116 return 0;
117 };
118 }
119
120 static BIO *ustream_bio_new(struct ustream *s)
121 {
122 BIO *bio;
123
124 BIO_METHOD *methods_ustream;
125
126 methods_ustream = BIO_meth_new(100 | BIO_TYPE_SOURCE_SINK, "ustream");
127 BIO_meth_set_write(methods_ustream, s_ustream_write);
128 BIO_meth_set_read(methods_ustream, s_ustream_read);
129 BIO_meth_set_puts(methods_ustream, s_ustream_puts);
130 BIO_meth_set_gets(methods_ustream, s_ustream_gets);
131 BIO_meth_set_ctrl(methods_ustream, s_ustream_ctrl);
132 BIO_meth_set_create(methods_ustream, s_ustream_new);
133 BIO_meth_set_destroy(methods_ustream, s_ustream_free);
134 bio = BIO_new(methods_ustream);
135 BIO_set_data(bio, s);
136
137 return bio;
138 }
139
140 __hidden void ustream_set_io(struct ustream_ssl_ctx *ctx, void *ssl, struct ustream *conn)
141 {
142 BIO *bio = ustream_bio_new(conn);
143 SSL_set_bio(ssl, bio, bio);
144 }