1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
Obtained from: https://chromium-review.googlesource.com/c/chromiumos/third_party/tlsdate/+/549533
--- src/proxy-bio.c.orig 2015-05-28 18:49:40 UTC
+++ src/proxy-bio.c
@@ -35,6 +35,7 @@
#include "src/common/strnlen.h"
#endif
+#include "src/openssl_compat.h"
#include "src/proxy-bio.h"
int socks4a_connect (BIO *b);
@@ -50,29 +51,29 @@ int proxy_new (BIO *b)
ctx->connect = NULL;
ctx->host = NULL;
ctx->port = 0;
- b->init = 1;
- b->flags = 0;
- b->ptr = ctx;
+ BIO_set_init(b, 1);
+ BIO_clear_flags(b, ~0);
+ BIO_set_data(b, ctx);
return 1;
}
int proxy_free (BIO *b)
{
struct proxy_ctx *c;
- if (!b || !b->ptr)
+ if (!b || !BIO_get_data(b))
return 1;
- c = (struct proxy_ctx *) b->ptr;
+ c = (struct proxy_ctx *) BIO_get_data(b);
if (c->host)
free (c->host);
c->host = NULL;
- b->ptr = NULL;
+ BIO_set_data(b, NULL);
free (c);
return 1;
}
int socks4a_connect (BIO *b)
{
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
int r;
unsigned char buf[NI_MAXHOST + 16];
uint16_t port_n = htons (ctx->port);
@@ -102,13 +103,13 @@ int socks4a_connect (BIO *b)
memcpy (buf + sz, ctx->host, strlen (ctx->host) + 1);
sz += strlen (ctx->host) + 1;
- r = BIO_write (b->next_bio, buf, sz);
+ r = BIO_write (BIO_next(b), buf, sz);
if ( -1 == r )
return -1;
if ( (size_t) r != sz)
return 0;
/* server reply: 1 + 1 + 2 + 4 */
- r = BIO_read (b->next_bio, buf, 8);
+ r = BIO_read (BIO_next(b), buf, 8);
if ( -1 == r )
return -1;
if ( (size_t) r != 8)
@@ -126,7 +127,7 @@ int socks5_connect (BIO *b)
{
unsigned char buf[NI_MAXHOST + 16];
int r;
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
uint16_t port_n = htons (ctx->port);
size_t sz = 0;
/* the length for SOCKS addresses is only one byte. */
@@ -145,10 +146,10 @@ int socks5_connect (BIO *b)
buf[0] = 0x05;
buf[1] = 0x01;
buf[2] = 0x00;
- r = BIO_write (b->next_bio, buf, 3);
+ r = BIO_write (BIO_next(b), buf, 3);
if (r != 3)
return 0;
- r = BIO_read (b->next_bio, buf, 2);
+ r = BIO_read (BIO_next(b), buf, 2);
if (r != 2)
return 0;
if (buf[0] != 0x05 || buf[1] != 0x00)
@@ -175,7 +176,7 @@ int socks5_connect (BIO *b)
sz += strlen (ctx->host);
memcpy (buf + sz, &port_n, sizeof (port_n));
sz += sizeof (port_n);
- r = BIO_write (b->next_bio, buf, sz);
+ r = BIO_write (BIO_next(b), buf, sz);
if ( -1 == r )
return -1;
if ( (size_t) r != sz)
@@ -190,7 +191,7 @@ int socks5_connect (BIO *b)
* 2b: port, network byte order
*/
/* grab up through the addr type */
- r = BIO_read (b->next_bio, buf, 4);
+ r = BIO_read (BIO_next(b), buf, 4);
if ( -1 == r )
return -1;
if (r != 4)
@@ -203,14 +204,14 @@ int socks5_connect (BIO *b)
if (buf[3] == 0x03)
{
unsigned int len;
- r = BIO_read (b->next_bio, buf + 4, 1);
+ r = BIO_read (BIO_next(b), buf + 4, 1);
if (r != 1)
return 0;
/* host (buf[4] bytes) + port (2 bytes) */
len = buf[4] + 2;
while (len)
{
- r = BIO_read (b->next_bio, buf + 5, min (len, sizeof (buf)));
+ r = BIO_read (BIO_next(b), buf + 5, min (len, sizeof (buf)));
if (r <= 0)
return 0;
len -= min (len, r);
@@ -219,7 +220,7 @@ int socks5_connect (BIO *b)
else if (buf[3] == 0x01)
{
/* 4 bytes ipv4 addr, 2 bytes port */
- r = BIO_read (b->next_bio, buf + 4, 6);
+ r = BIO_read (BIO_next(b), buf + 4, 6);
if (r != 6)
return 0;
}
@@ -248,30 +249,30 @@ int sock_gets (BIO *b, char *buf, size_t
int http_connect (BIO *b)
{
int r;
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
char buf[4096];
int retcode;
snprintf (buf, sizeof (buf), "CONNECT %s:%d HTTP/1.1\r\n",
ctx->host, ctx->port);
- r = BIO_write (b->next_bio, buf, strlen (buf));
+ r = BIO_write (BIO_next(b), buf, strlen (buf));
if ( -1 == r )
return -1;
if ( (size_t) r != strlen(buf))
return 0;
/* required by RFC 2616 14.23 */
snprintf (buf, sizeof (buf), "Host: %s:%d\r\n", ctx->host, ctx->port);
- r = BIO_write (b->next_bio, buf, strlen (buf));
+ r = BIO_write (BIO_next(b), buf, strlen (buf));
if ( -1 == r )
return -1;
if ( (size_t) r != strlen(buf))
return 0;
strcpy (buf, "\r\n");
- r = BIO_write (b->next_bio, buf, strlen (buf));
+ r = BIO_write (BIO_next(b), buf, strlen (buf));
if ( -1 == r )
return -1;
if ( (size_t) r != strlen(buf))
return 0;
- r = sock_gets (b->next_bio, buf, sizeof (buf));
+ r = sock_gets (BIO_next(b), buf, sizeof (buf));
if (r)
return 0;
/* use %*s to ignore the version */
@@ -279,7 +280,7 @@ int http_connect (BIO *b)
return 0;
if (retcode < 200 || retcode > 299)
return 0;
- while (! (r = sock_gets (b->next_bio, buf, sizeof (buf))))
+ while (! (r = sock_gets (BIO_next(b), buf, sizeof (buf))))
{
if (!strcmp (buf, "\r\n"))
{
@@ -294,12 +295,12 @@ int http_connect (BIO *b)
int proxy_write (BIO *b, const char *buf, int sz)
{
int r;
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
assert (buf);
if (sz <= 0)
return 0;
- if (!b->next_bio)
+ if (!BIO_next(b))
return 0;
if (!ctx->connected)
{
@@ -307,7 +308,7 @@ int proxy_write (BIO *b, const char *buf
if (!ctx->connect (b))
return 0;
}
- r = BIO_write (b->next_bio, buf, sz);
+ r = BIO_write (BIO_next(b), buf, sz);
BIO_clear_retry_flags (b);
BIO_copy_next_retry (b);
return r;
@@ -316,10 +317,10 @@ int proxy_write (BIO *b, const char *buf
int proxy_read (BIO *b, char *buf, int sz)
{
int r;
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
assert (buf);
- if (!b->next_bio)
+ if (!BIO_next(b))
return 0;
if (!ctx->connected)
{
@@ -327,7 +328,7 @@ int proxy_read (BIO *b, char *buf, int s
if (!ctx->connect (b))
return 0;
}
- r = BIO_read (b->next_bio, buf, sz);
+ r = BIO_read (BIO_next(b), buf, sz);
BIO_clear_retry_flags (b);
BIO_copy_next_retry (b);
return r;
@@ -337,43 +338,45 @@ long proxy_ctrl (BIO *b, int cmd, long n
{
long ret;
struct proxy_ctx *ctx;
- if (!b->next_bio)
+ if (!BIO_next(b))
return 0;
- ctx = (struct proxy_ctx *) b->ptr;
+ ctx = (struct proxy_ctx *) BIO_get_data(b);
assert (ctx);
switch (cmd)
{
case BIO_C_DO_STATE_MACHINE:
BIO_clear_retry_flags (b);
- ret = BIO_ctrl (b->next_bio, cmd, num, ptr);
+ ret = BIO_ctrl (BIO_next(b), cmd, num, ptr);
BIO_copy_next_retry (b);
break;
case BIO_CTRL_DUP:
ret = 0;
break;
default:
- ret = BIO_ctrl (b->next_bio, cmd, num, ptr);
+ ret = BIO_ctrl (BIO_next(b), cmd, num, ptr);
}
return ret;
}
int proxy_gets (BIO *b, char *buf, int size)
{
- return BIO_gets (b->next_bio, buf, size);
+ return BIO_gets (BIO_next(b), buf, size);
}
int proxy_puts (BIO *b, const char *str)
{
- return BIO_puts (b->next_bio, str);
+ return BIO_puts (BIO_next(b), str);
}
long proxy_callback_ctrl (BIO *b, int cmd, bio_info_cb *fp)
{
- if (!b->next_bio)
+ if (!BIO_next(b))
return 0;
- return BIO_callback_ctrl (b->next_bio, cmd, fp);
+ return BIO_callback_ctrl (BIO_next(b), cmd, fp);
}
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+
BIO_METHOD proxy_methods =
{
BIO_TYPE_MEM,
@@ -393,6 +396,29 @@ BIO_METHOD *BIO_f_proxy()
return &proxy_methods;
}
+#else
+
+static BIO_METHOD *proxy_methods;
+
+BIO_METHOD *BIO_f_proxy()
+{
+ if (!proxy_methods) {
+ proxy_methods = BIO_meth_new(BIO_TYPE_MEM, "proxy");
+ BIO_meth_set_write(proxy_methods, proxy_write);
+ BIO_meth_set_read(proxy_methods, proxy_read);
+ BIO_meth_set_puts(proxy_methods, proxy_puts);
+ BIO_meth_set_gets(proxy_methods, proxy_gets);
+ BIO_meth_set_ctrl(proxy_methods, proxy_ctrl);
+ BIO_meth_set_create(proxy_methods, proxy_new);
+ BIO_meth_set_destroy(proxy_methods, proxy_free);
+ BIO_meth_set_callback_ctrl(proxy_methods, proxy_callback_ctrl);
+ }
+
+ return proxy_methods;
+}
+
+#endif
+
/* API starts here */
BIO API *BIO_new_proxy()
@@ -402,7 +428,7 @@ BIO API *BIO_new_proxy()
int API BIO_proxy_set_type (BIO *b, const char *type)
{
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
if (!strcmp (type, "socks5"))
ctx->connect = socks5_connect;
else if (!strcmp (type, "socks4a"))
@@ -416,7 +442,7 @@ int API BIO_proxy_set_type (BIO *b, cons
int API BIO_proxy_set_host (BIO *b, const char *host)
{
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
if (strnlen (host, NI_MAXHOST) == NI_MAXHOST)
return 1;
ctx->host = strdup (host);
@@ -425,6 +451,6 @@ int API BIO_proxy_set_host (BIO *b, cons
void API BIO_proxy_set_port (BIO *b, uint16_t port)
{
- struct proxy_ctx *ctx = (struct proxy_ctx *) b->ptr;
+ struct proxy_ctx *ctx = (struct proxy_ctx *) BIO_get_data(b);
ctx->port = port;
}
|