/src/openssl/ssl/bio_ssl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* ssl/bio_ssl.c */ |
2 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | | * All rights reserved. |
4 | | * |
5 | | * This package is an SSL implementation written |
6 | | * by Eric Young (eay@cryptsoft.com). |
7 | | * The implementation was written so as to conform with Netscapes SSL. |
8 | | * |
9 | | * This library is free for commercial and non-commercial use as long as |
10 | | * the following conditions are aheared to. The following conditions |
11 | | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | | * included with this distribution is covered by the same copyright terms |
14 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | | * |
16 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | | * the code are not to be removed. |
18 | | * If this package is used in a product, Eric Young should be given attribution |
19 | | * as the author of the parts of the library used. |
20 | | * This can be in the form of a textual message at program startup or |
21 | | * in documentation (online or textual) provided with the package. |
22 | | * |
23 | | * Redistribution and use in source and binary forms, with or without |
24 | | * modification, are permitted provided that the following conditions |
25 | | * are met: |
26 | | * 1. Redistributions of source code must retain the copyright |
27 | | * notice, this list of conditions and the following disclaimer. |
28 | | * 2. Redistributions in binary form must reproduce the above copyright |
29 | | * notice, this list of conditions and the following disclaimer in the |
30 | | * documentation and/or other materials provided with the distribution. |
31 | | * 3. All advertising materials mentioning features or use of this software |
32 | | * must display the following acknowledgement: |
33 | | * "This product includes cryptographic software written by |
34 | | * Eric Young (eay@cryptsoft.com)" |
35 | | * The word 'cryptographic' can be left out if the rouines from the library |
36 | | * being used are not cryptographic related :-). |
37 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | | * the apps directory (application code) you must include an acknowledgement: |
39 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | | * |
41 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
44 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
45 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
46 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
47 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
48 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
49 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | | * SUCH DAMAGE. |
52 | | * |
53 | | * The licence and distribution terms for any publically available version or |
54 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | | * copied and put under another distribution licence |
56 | | * [including the GNU Public Licence.] |
57 | | */ |
58 | | |
59 | | #include <stdio.h> |
60 | | #include <stdlib.h> |
61 | | #include <string.h> |
62 | | #include <errno.h> |
63 | | #include <openssl/crypto.h> |
64 | | #include <openssl/bio.h> |
65 | | #include <openssl/err.h> |
66 | | #include <openssl/ssl.h> |
67 | | |
68 | | static int ssl_write(BIO *h, const char *buf, int num); |
69 | | static int ssl_read(BIO *h, char *buf, int size); |
70 | | static int ssl_puts(BIO *h, const char *str); |
71 | | static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2); |
72 | | static int ssl_new(BIO *h); |
73 | | static int ssl_free(BIO *data); |
74 | | static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); |
75 | | typedef struct bio_ssl_st { |
76 | | SSL *ssl; /* The ssl handle :-) */ |
77 | | /* re-negotiate every time the total number of bytes is this size */ |
78 | | int num_renegotiates; |
79 | | unsigned long renegotiate_count; |
80 | | unsigned long byte_count; |
81 | | unsigned long renegotiate_timeout; |
82 | | unsigned long last_time; |
83 | | } BIO_SSL; |
84 | | |
85 | | static BIO_METHOD methods_sslp = { |
86 | | BIO_TYPE_SSL, "ssl", |
87 | | ssl_write, |
88 | | ssl_read, |
89 | | ssl_puts, |
90 | | NULL, /* ssl_gets, */ |
91 | | ssl_ctrl, |
92 | | ssl_new, |
93 | | ssl_free, |
94 | | ssl_callback_ctrl, |
95 | | }; |
96 | | |
97 | | BIO_METHOD *BIO_f_ssl(void) |
98 | 0 | { |
99 | 0 | return (&methods_sslp); |
100 | 0 | } |
101 | | |
102 | | static int ssl_new(BIO *bi) |
103 | 0 | { |
104 | 0 | BIO_SSL *bs; |
105 | |
|
106 | 0 | bs = (BIO_SSL *)OPENSSL_malloc(sizeof(BIO_SSL)); |
107 | 0 | if (bs == NULL) { |
108 | 0 | BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE); |
109 | 0 | return (0); |
110 | 0 | } |
111 | 0 | memset(bs, 0, sizeof(BIO_SSL)); |
112 | 0 | bi->init = 0; |
113 | 0 | bi->ptr = (char *)bs; |
114 | 0 | bi->flags = 0; |
115 | 0 | return (1); |
116 | 0 | } |
117 | | |
118 | | static int ssl_free(BIO *a) |
119 | 0 | { |
120 | 0 | BIO_SSL *bs; |
121 | |
|
122 | 0 | if (a == NULL) |
123 | 0 | return (0); |
124 | 0 | bs = (BIO_SSL *)a->ptr; |
125 | 0 | if (bs->ssl != NULL) |
126 | 0 | SSL_shutdown(bs->ssl); |
127 | 0 | if (a->shutdown) { |
128 | 0 | if (a->init && (bs->ssl != NULL)) |
129 | 0 | SSL_free(bs->ssl); |
130 | 0 | a->init = 0; |
131 | 0 | a->flags = 0; |
132 | 0 | } |
133 | 0 | if (a->ptr != NULL) |
134 | 0 | OPENSSL_free(a->ptr); |
135 | 0 | return (1); |
136 | 0 | } |
137 | | |
138 | | static int ssl_read(BIO *b, char *out, int outl) |
139 | 0 | { |
140 | 0 | int ret = 1; |
141 | 0 | BIO_SSL *sb; |
142 | 0 | SSL *ssl; |
143 | 0 | int retry_reason = 0; |
144 | 0 | int r = 0; |
145 | |
|
146 | 0 | if (out == NULL) |
147 | 0 | return (0); |
148 | 0 | sb = (BIO_SSL *)b->ptr; |
149 | 0 | ssl = sb->ssl; |
150 | |
|
151 | 0 | BIO_clear_retry_flags(b); |
152 | |
|
153 | | #if 0 |
154 | | if (!SSL_is_init_finished(ssl)) { |
155 | | /* ret=SSL_do_handshake(ssl); */ |
156 | | if (ret > 0) { |
157 | | |
158 | | outflags = (BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
159 | | ret = -1; |
160 | | goto end; |
161 | | } |
162 | | } |
163 | | #endif |
164 | | /* if (ret > 0) */ |
165 | 0 | ret = SSL_read(ssl, out, outl); |
166 | |
|
167 | 0 | switch (SSL_get_error(ssl, ret)) { |
168 | 0 | case SSL_ERROR_NONE: |
169 | 0 | if (ret <= 0) |
170 | 0 | break; |
171 | 0 | if (sb->renegotiate_count > 0) { |
172 | 0 | sb->byte_count += ret; |
173 | 0 | if (sb->byte_count > sb->renegotiate_count) { |
174 | 0 | sb->byte_count = 0; |
175 | 0 | sb->num_renegotiates++; |
176 | 0 | SSL_renegotiate(ssl); |
177 | 0 | r = 1; |
178 | 0 | } |
179 | 0 | } |
180 | 0 | if ((sb->renegotiate_timeout > 0) && (!r)) { |
181 | 0 | unsigned long tm; |
182 | |
|
183 | 0 | tm = (unsigned long)time(NULL); |
184 | 0 | if (tm > sb->last_time + sb->renegotiate_timeout) { |
185 | 0 | sb->last_time = tm; |
186 | 0 | sb->num_renegotiates++; |
187 | 0 | SSL_renegotiate(ssl); |
188 | 0 | } |
189 | 0 | } |
190 | |
|
191 | 0 | break; |
192 | 0 | case SSL_ERROR_WANT_READ: |
193 | 0 | BIO_set_retry_read(b); |
194 | 0 | break; |
195 | 0 | case SSL_ERROR_WANT_WRITE: |
196 | 0 | BIO_set_retry_write(b); |
197 | 0 | break; |
198 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
199 | 0 | BIO_set_retry_special(b); |
200 | 0 | retry_reason = BIO_RR_SSL_X509_LOOKUP; |
201 | 0 | break; |
202 | 0 | case SSL_ERROR_WANT_ACCEPT: |
203 | 0 | BIO_set_retry_special(b); |
204 | 0 | retry_reason = BIO_RR_ACCEPT; |
205 | 0 | break; |
206 | 0 | case SSL_ERROR_WANT_CONNECT: |
207 | 0 | BIO_set_retry_special(b); |
208 | 0 | retry_reason = BIO_RR_CONNECT; |
209 | 0 | break; |
210 | 0 | case SSL_ERROR_SYSCALL: |
211 | 0 | case SSL_ERROR_SSL: |
212 | 0 | case SSL_ERROR_ZERO_RETURN: |
213 | 0 | default: |
214 | 0 | break; |
215 | 0 | } |
216 | | |
217 | 0 | b->retry_reason = retry_reason; |
218 | 0 | return (ret); |
219 | 0 | } |
220 | | |
221 | | static int ssl_write(BIO *b, const char *out, int outl) |
222 | 0 | { |
223 | 0 | int ret, r = 0; |
224 | 0 | int retry_reason = 0; |
225 | 0 | SSL *ssl; |
226 | 0 | BIO_SSL *bs; |
227 | |
|
228 | 0 | if (out == NULL) |
229 | 0 | return (0); |
230 | 0 | bs = (BIO_SSL *)b->ptr; |
231 | 0 | ssl = bs->ssl; |
232 | |
|
233 | 0 | BIO_clear_retry_flags(b); |
234 | | |
235 | | /* |
236 | | * ret=SSL_do_handshake(ssl); if (ret > 0) |
237 | | */ |
238 | 0 | ret = SSL_write(ssl, out, outl); |
239 | |
|
240 | 0 | switch (SSL_get_error(ssl, ret)) { |
241 | 0 | case SSL_ERROR_NONE: |
242 | 0 | if (ret <= 0) |
243 | 0 | break; |
244 | 0 | if (bs->renegotiate_count > 0) { |
245 | 0 | bs->byte_count += ret; |
246 | 0 | if (bs->byte_count > bs->renegotiate_count) { |
247 | 0 | bs->byte_count = 0; |
248 | 0 | bs->num_renegotiates++; |
249 | 0 | SSL_renegotiate(ssl); |
250 | 0 | r = 1; |
251 | 0 | } |
252 | 0 | } |
253 | 0 | if ((bs->renegotiate_timeout > 0) && (!r)) { |
254 | 0 | unsigned long tm; |
255 | |
|
256 | 0 | tm = (unsigned long)time(NULL); |
257 | 0 | if (tm > bs->last_time + bs->renegotiate_timeout) { |
258 | 0 | bs->last_time = tm; |
259 | 0 | bs->num_renegotiates++; |
260 | 0 | SSL_renegotiate(ssl); |
261 | 0 | } |
262 | 0 | } |
263 | 0 | break; |
264 | 0 | case SSL_ERROR_WANT_WRITE: |
265 | 0 | BIO_set_retry_write(b); |
266 | 0 | break; |
267 | 0 | case SSL_ERROR_WANT_READ: |
268 | 0 | BIO_set_retry_read(b); |
269 | 0 | break; |
270 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
271 | 0 | BIO_set_retry_special(b); |
272 | 0 | retry_reason = BIO_RR_SSL_X509_LOOKUP; |
273 | 0 | break; |
274 | 0 | case SSL_ERROR_WANT_CONNECT: |
275 | 0 | BIO_set_retry_special(b); |
276 | 0 | retry_reason = BIO_RR_CONNECT; |
277 | 0 | case SSL_ERROR_SYSCALL: |
278 | 0 | case SSL_ERROR_SSL: |
279 | 0 | default: |
280 | 0 | break; |
281 | 0 | } |
282 | | |
283 | 0 | b->retry_reason = retry_reason; |
284 | 0 | return (ret); |
285 | 0 | } |
286 | | |
287 | | static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) |
288 | 0 | { |
289 | 0 | SSL **sslp, *ssl; |
290 | 0 | BIO_SSL *bs; |
291 | 0 | BIO *dbio, *bio; |
292 | 0 | long ret = 1; |
293 | |
|
294 | 0 | bs = (BIO_SSL *)b->ptr; |
295 | 0 | ssl = bs->ssl; |
296 | 0 | if ((ssl == NULL) && (cmd != BIO_C_SET_SSL)) |
297 | 0 | return (0); |
298 | 0 | switch (cmd) { |
299 | 0 | case BIO_CTRL_RESET: |
300 | 0 | SSL_shutdown(ssl); |
301 | |
|
302 | 0 | if (ssl->handshake_func == ssl->method->ssl_connect) |
303 | 0 | SSL_set_connect_state(ssl); |
304 | 0 | else if (ssl->handshake_func == ssl->method->ssl_accept) |
305 | 0 | SSL_set_accept_state(ssl); |
306 | |
|
307 | 0 | SSL_clear(ssl); |
308 | |
|
309 | 0 | if (b->next_bio != NULL) |
310 | 0 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); |
311 | 0 | else if (ssl->rbio != NULL) |
312 | 0 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); |
313 | 0 | else |
314 | 0 | ret = 1; |
315 | 0 | break; |
316 | 0 | case BIO_CTRL_INFO: |
317 | 0 | ret = 0; |
318 | 0 | break; |
319 | 0 | case BIO_C_SSL_MODE: |
320 | 0 | if (num) /* client mode */ |
321 | 0 | SSL_set_connect_state(ssl); |
322 | 0 | else |
323 | 0 | SSL_set_accept_state(ssl); |
324 | 0 | break; |
325 | 0 | case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT: |
326 | 0 | ret = bs->renegotiate_timeout; |
327 | 0 | if (num < 60) |
328 | 0 | num = 5; |
329 | 0 | bs->renegotiate_timeout = (unsigned long)num; |
330 | 0 | bs->last_time = (unsigned long)time(NULL); |
331 | 0 | break; |
332 | 0 | case BIO_C_SET_SSL_RENEGOTIATE_BYTES: |
333 | 0 | ret = bs->renegotiate_count; |
334 | 0 | if ((long)num >= 512) |
335 | 0 | bs->renegotiate_count = (unsigned long)num; |
336 | 0 | break; |
337 | 0 | case BIO_C_GET_SSL_NUM_RENEGOTIATES: |
338 | 0 | ret = bs->num_renegotiates; |
339 | 0 | break; |
340 | 0 | case BIO_C_SET_SSL: |
341 | 0 | if (ssl != NULL) { |
342 | 0 | ssl_free(b); |
343 | 0 | if (!ssl_new(b)) |
344 | 0 | return 0; |
345 | 0 | } |
346 | 0 | b->shutdown = (int)num; |
347 | 0 | ssl = (SSL *)ptr; |
348 | 0 | ((BIO_SSL *)b->ptr)->ssl = ssl; |
349 | 0 | bio = SSL_get_rbio(ssl); |
350 | 0 | if (bio != NULL) { |
351 | 0 | if (b->next_bio != NULL) |
352 | 0 | BIO_push(bio, b->next_bio); |
353 | 0 | b->next_bio = bio; |
354 | 0 | CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO); |
355 | 0 | } |
356 | 0 | b->init = 1; |
357 | 0 | break; |
358 | 0 | case BIO_C_GET_SSL: |
359 | 0 | if (ptr != NULL) { |
360 | 0 | sslp = (SSL **)ptr; |
361 | 0 | *sslp = ssl; |
362 | 0 | } else |
363 | 0 | ret = 0; |
364 | 0 | break; |
365 | 0 | case BIO_CTRL_GET_CLOSE: |
366 | 0 | ret = b->shutdown; |
367 | 0 | break; |
368 | 0 | case BIO_CTRL_SET_CLOSE: |
369 | 0 | b->shutdown = (int)num; |
370 | 0 | break; |
371 | 0 | case BIO_CTRL_WPENDING: |
372 | 0 | ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); |
373 | 0 | break; |
374 | 0 | case BIO_CTRL_PENDING: |
375 | 0 | ret = SSL_pending(ssl); |
376 | 0 | if (ret == 0) |
377 | 0 | ret = BIO_pending(ssl->rbio); |
378 | 0 | break; |
379 | 0 | case BIO_CTRL_FLUSH: |
380 | 0 | BIO_clear_retry_flags(b); |
381 | 0 | ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); |
382 | 0 | BIO_copy_next_retry(b); |
383 | 0 | break; |
384 | 0 | case BIO_CTRL_PUSH: |
385 | 0 | if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { |
386 | 0 | SSL_set_bio(ssl, b->next_bio, b->next_bio); |
387 | 0 | CRYPTO_add(&b->next_bio->references, 1, CRYPTO_LOCK_BIO); |
388 | 0 | } |
389 | 0 | break; |
390 | 0 | case BIO_CTRL_POP: |
391 | | /* Only detach if we are the BIO explicitly being popped */ |
392 | 0 | if (b == ptr) { |
393 | | /* |
394 | | * Shouldn't happen in practice because the rbio and wbio are the |
395 | | * same when pushed. |
396 | | */ |
397 | 0 | if (ssl->rbio != ssl->wbio) |
398 | 0 | BIO_free_all(ssl->wbio); |
399 | 0 | if (b->next_bio != NULL) |
400 | 0 | CRYPTO_add(&b->next_bio->references, -1, CRYPTO_LOCK_BIO); |
401 | 0 | ssl->wbio = NULL; |
402 | 0 | ssl->rbio = NULL; |
403 | 0 | } |
404 | 0 | break; |
405 | 0 | case BIO_C_DO_STATE_MACHINE: |
406 | 0 | BIO_clear_retry_flags(b); |
407 | |
|
408 | 0 | b->retry_reason = 0; |
409 | 0 | ret = (int)SSL_do_handshake(ssl); |
410 | |
|
411 | 0 | switch (SSL_get_error(ssl, (int)ret)) { |
412 | 0 | case SSL_ERROR_WANT_READ: |
413 | 0 | BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); |
414 | 0 | break; |
415 | 0 | case SSL_ERROR_WANT_WRITE: |
416 | 0 | BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); |
417 | 0 | break; |
418 | 0 | case SSL_ERROR_WANT_CONNECT: |
419 | 0 | BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); |
420 | 0 | b->retry_reason = b->next_bio->retry_reason; |
421 | 0 | break; |
422 | 0 | case SSL_ERROR_WANT_X509_LOOKUP: |
423 | 0 | BIO_set_retry_special(b); |
424 | 0 | b->retry_reason = BIO_RR_SSL_X509_LOOKUP; |
425 | 0 | break; |
426 | 0 | default: |
427 | 0 | break; |
428 | 0 | } |
429 | 0 | break; |
430 | 0 | case BIO_CTRL_DUP: |
431 | 0 | dbio = (BIO *)ptr; |
432 | 0 | if (((BIO_SSL *)dbio->ptr)->ssl != NULL) |
433 | 0 | SSL_free(((BIO_SSL *)dbio->ptr)->ssl); |
434 | 0 | ((BIO_SSL *)dbio->ptr)->ssl = SSL_dup(ssl); |
435 | 0 | ((BIO_SSL *)dbio->ptr)->renegotiate_count = |
436 | 0 | ((BIO_SSL *)b->ptr)->renegotiate_count; |
437 | 0 | ((BIO_SSL *)dbio->ptr)->byte_count = ((BIO_SSL *)b->ptr)->byte_count; |
438 | 0 | ((BIO_SSL *)dbio->ptr)->renegotiate_timeout = |
439 | 0 | ((BIO_SSL *)b->ptr)->renegotiate_timeout; |
440 | 0 | ((BIO_SSL *)dbio->ptr)->last_time = ((BIO_SSL *)b->ptr)->last_time; |
441 | 0 | ret = (((BIO_SSL *)dbio->ptr)->ssl != NULL); |
442 | 0 | break; |
443 | 0 | case BIO_C_GET_FD: |
444 | 0 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); |
445 | 0 | break; |
446 | 0 | case BIO_CTRL_SET_CALLBACK: |
447 | 0 | { |
448 | | #if 0 /* FIXME: Should this be used? -- Richard |
449 | | * Levitte */ |
450 | | SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
451 | | ret = -1; |
452 | | #else |
453 | 0 | ret = 0; |
454 | 0 | #endif |
455 | 0 | } |
456 | 0 | break; |
457 | 0 | case BIO_CTRL_GET_CALLBACK: |
458 | 0 | { |
459 | 0 | void (**fptr) (const SSL *xssl, int type, int val); |
460 | |
|
461 | 0 | fptr = (void (**)(const SSL *xssl, int type, int val))ptr; |
462 | 0 | *fptr = SSL_get_info_callback(ssl); |
463 | 0 | } |
464 | 0 | break; |
465 | 0 | default: |
466 | 0 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); |
467 | 0 | break; |
468 | 0 | } |
469 | 0 | return (ret); |
470 | 0 | } |
471 | | |
472 | | static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
473 | 0 | { |
474 | 0 | SSL *ssl; |
475 | 0 | BIO_SSL *bs; |
476 | 0 | long ret = 1; |
477 | |
|
478 | 0 | bs = (BIO_SSL *)b->ptr; |
479 | 0 | ssl = bs->ssl; |
480 | 0 | switch (cmd) { |
481 | 0 | case BIO_CTRL_SET_CALLBACK: |
482 | 0 | { |
483 | | /* |
484 | | * FIXME: setting this via a completely different prototype seems |
485 | | * like a crap idea |
486 | | */ |
487 | 0 | SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp); |
488 | 0 | } |
489 | 0 | break; |
490 | 0 | default: |
491 | 0 | ret = BIO_callback_ctrl(ssl->rbio, cmd, fp); |
492 | 0 | break; |
493 | 0 | } |
494 | 0 | return (ret); |
495 | 0 | } |
496 | | |
497 | | static int ssl_puts(BIO *bp, const char *str) |
498 | 0 | { |
499 | 0 | int n, ret; |
500 | |
|
501 | 0 | n = strlen(str); |
502 | 0 | ret = BIO_write(bp, str, n); |
503 | 0 | return (ret); |
504 | 0 | } |
505 | | |
506 | | BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx) |
507 | 0 | { |
508 | 0 | #ifndef OPENSSL_NO_SOCK |
509 | 0 | BIO *ret = NULL, *buf = NULL, *ssl = NULL; |
510 | |
|
511 | 0 | if ((buf = BIO_new(BIO_f_buffer())) == NULL) |
512 | 0 | return (NULL); |
513 | 0 | if ((ssl = BIO_new_ssl_connect(ctx)) == NULL) |
514 | 0 | goto err; |
515 | 0 | if ((ret = BIO_push(buf, ssl)) == NULL) |
516 | 0 | goto err; |
517 | 0 | return (ret); |
518 | 0 | err: |
519 | 0 | if (buf != NULL) |
520 | 0 | BIO_free(buf); |
521 | 0 | if (ssl != NULL) |
522 | 0 | BIO_free(ssl); |
523 | 0 | #endif |
524 | 0 | return (NULL); |
525 | 0 | } |
526 | | |
527 | | BIO *BIO_new_ssl_connect(SSL_CTX *ctx) |
528 | 0 | { |
529 | 0 | #ifndef OPENSSL_NO_SOCK |
530 | 0 | BIO *ret = NULL, *con = NULL, *ssl = NULL; |
531 | |
|
532 | 0 | if ((con = BIO_new(BIO_s_connect())) == NULL) |
533 | 0 | return (NULL); |
534 | 0 | if ((ssl = BIO_new_ssl(ctx, 1)) == NULL) |
535 | 0 | goto err; |
536 | 0 | if ((ret = BIO_push(ssl, con)) == NULL) |
537 | 0 | goto err; |
538 | 0 | return (ret); |
539 | 0 | err: |
540 | 0 | if (con != NULL) |
541 | 0 | BIO_free(con); |
542 | 0 | #endif |
543 | 0 | return (NULL); |
544 | 0 | } |
545 | | |
546 | | BIO *BIO_new_ssl(SSL_CTX *ctx, int client) |
547 | 0 | { |
548 | 0 | BIO *ret; |
549 | 0 | SSL *ssl; |
550 | |
|
551 | 0 | if ((ret = BIO_new(BIO_f_ssl())) == NULL) |
552 | 0 | return (NULL); |
553 | 0 | if ((ssl = SSL_new(ctx)) == NULL) { |
554 | 0 | BIO_free(ret); |
555 | 0 | return (NULL); |
556 | 0 | } |
557 | 0 | if (client) |
558 | 0 | SSL_set_connect_state(ssl); |
559 | 0 | else |
560 | 0 | SSL_set_accept_state(ssl); |
561 | |
|
562 | 0 | BIO_set_ssl(ret, ssl, BIO_CLOSE); |
563 | 0 | return (ret); |
564 | 0 | } |
565 | | |
566 | | int BIO_ssl_copy_session_id(BIO *t, BIO *f) |
567 | 0 | { |
568 | 0 | t = BIO_find_type(t, BIO_TYPE_SSL); |
569 | 0 | f = BIO_find_type(f, BIO_TYPE_SSL); |
570 | 0 | if ((t == NULL) || (f == NULL)) |
571 | 0 | return (0); |
572 | 0 | if ((((BIO_SSL *)t->ptr)->ssl == NULL) || |
573 | 0 | (((BIO_SSL *)f->ptr)->ssl == NULL)) |
574 | 0 | return (0); |
575 | 0 | SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl, ((BIO_SSL *)f->ptr)->ssl); |
576 | 0 | return (1); |
577 | 0 | } |
578 | | |
579 | | void BIO_ssl_shutdown(BIO *b) |
580 | 0 | { |
581 | 0 | SSL *s; |
582 | |
|
583 | 0 | while (b != NULL) { |
584 | 0 | if (b->method->type == BIO_TYPE_SSL) { |
585 | 0 | s = ((BIO_SSL *)b->ptr)->ssl; |
586 | 0 | SSL_shutdown(s); |
587 | 0 | break; |
588 | 0 | } |
589 | 0 | b = b->next_bio; |
590 | 0 | } |
591 | 0 | } |