]>
Commit | Line | Data |
---|---|---|
d02b48c6 | 1 | /* ssl/bio_ssl.c */ |
58964a49 | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
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. | |
0f113f3e | 8 | * |
d02b48c6 RE |
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). | |
0f113f3e | 15 | * |
d02b48c6 RE |
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. | |
0f113f3e | 22 | * |
d02b48c6 RE |
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 :-). | |
0f113f3e | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 40 | * |
d02b48c6 RE |
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. | |
0f113f3e | 52 | * |
d02b48c6 RE |
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> | |
58964a49 | 60 | #include <stdlib.h> |
d02b48c6 RE |
61 | #include <string.h> |
62 | #include <errno.h> | |
ec577822 BM |
63 | #include <openssl/crypto.h> |
64 | #include <openssl/bio.h> | |
65 | #include <openssl/err.h> | |
b6ba4014 | 66 | #include "ssl_locl.h" |
d02b48c6 | 67 | |
0e1c0612 UM |
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); | |
d02b48c6 RE |
72 | static int ssl_new(BIO *h); |
73 | static int ssl_free(BIO *data); | |
13083215 | 74 | static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); |
0f113f3e MC |
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 | }; | |
a9188d4e | 96 | |
6b691a5c | 97 | BIO_METHOD *BIO_f_ssl(void) |
0f113f3e MC |
98 | { |
99 | return (&methods_sslp); | |
100 | } | |
d02b48c6 | 101 | |
6b691a5c | 102 | static int ssl_new(BIO *bi) |
0f113f3e | 103 | { |
b51bce94 | 104 | BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs)); |
0f113f3e | 105 | |
0f113f3e MC |
106 | if (bs == NULL) { |
107 | BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE); | |
108 | return (0); | |
109 | } | |
0f113f3e MC |
110 | bi->init = 0; |
111 | bi->ptr = (char *)bs; | |
112 | bi->flags = 0; | |
113 | return (1); | |
114 | } | |
d02b48c6 | 115 | |
6b691a5c | 116 | static int ssl_free(BIO *a) |
0f113f3e MC |
117 | { |
118 | BIO_SSL *bs; | |
119 | ||
120 | if (a == NULL) | |
121 | return (0); | |
122 | bs = (BIO_SSL *)a->ptr; | |
123 | if (bs->ssl != NULL) | |
124 | SSL_shutdown(bs->ssl); | |
125 | if (a->shutdown) { | |
62adbcee | 126 | if (a->init) |
0f113f3e MC |
127 | SSL_free(bs->ssl); |
128 | a->init = 0; | |
129 | a->flags = 0; | |
130 | } | |
b548a1f1 | 131 | OPENSSL_free(a->ptr); |
0f113f3e MC |
132 | return (1); |
133 | } | |
134 | ||
6b691a5c | 135 | static int ssl_read(BIO *b, char *out, int outl) |
0f113f3e MC |
136 | { |
137 | int ret = 1; | |
138 | BIO_SSL *sb; | |
139 | SSL *ssl; | |
140 | int retry_reason = 0; | |
141 | int r = 0; | |
d02b48c6 | 142 | |
0f113f3e MC |
143 | if (out == NULL) |
144 | return (0); | |
145 | sb = (BIO_SSL *)b->ptr; | |
146 | ssl = sb->ssl; | |
d02b48c6 | 147 | |
0f113f3e | 148 | BIO_clear_retry_flags(b); |
d02b48c6 | 149 | |
0f113f3e MC |
150 | ret = SSL_read(ssl, out, outl); |
151 | ||
152 | switch (SSL_get_error(ssl, ret)) { | |
153 | case SSL_ERROR_NONE: | |
154 | if (ret <= 0) | |
155 | break; | |
156 | if (sb->renegotiate_count > 0) { | |
157 | sb->byte_count += ret; | |
158 | if (sb->byte_count > sb->renegotiate_count) { | |
159 | sb->byte_count = 0; | |
160 | sb->num_renegotiates++; | |
161 | SSL_renegotiate(ssl); | |
162 | r = 1; | |
163 | } | |
164 | } | |
165 | if ((sb->renegotiate_timeout > 0) && (!r)) { | |
166 | unsigned long tm; | |
167 | ||
168 | tm = (unsigned long)time(NULL); | |
169 | if (tm > sb->last_time + sb->renegotiate_timeout) { | |
170 | sb->last_time = tm; | |
171 | sb->num_renegotiates++; | |
172 | SSL_renegotiate(ssl); | |
173 | } | |
174 | } | |
175 | ||
176 | break; | |
177 | case SSL_ERROR_WANT_READ: | |
178 | BIO_set_retry_read(b); | |
179 | break; | |
180 | case SSL_ERROR_WANT_WRITE: | |
181 | BIO_set_retry_write(b); | |
182 | break; | |
183 | case SSL_ERROR_WANT_X509_LOOKUP: | |
184 | BIO_set_retry_special(b); | |
185 | retry_reason = BIO_RR_SSL_X509_LOOKUP; | |
186 | break; | |
187 | case SSL_ERROR_WANT_ACCEPT: | |
188 | BIO_set_retry_special(b); | |
189 | retry_reason = BIO_RR_ACCEPT; | |
190 | break; | |
191 | case SSL_ERROR_WANT_CONNECT: | |
192 | BIO_set_retry_special(b); | |
193 | retry_reason = BIO_RR_CONNECT; | |
194 | break; | |
195 | case SSL_ERROR_SYSCALL: | |
196 | case SSL_ERROR_SSL: | |
197 | case SSL_ERROR_ZERO_RETURN: | |
198 | default: | |
199 | break; | |
200 | } | |
201 | ||
202 | b->retry_reason = retry_reason; | |
203 | return (ret); | |
204 | } | |
d02b48c6 | 205 | |
0e1c0612 | 206 | static int ssl_write(BIO *b, const char *out, int outl) |
0f113f3e MC |
207 | { |
208 | int ret, r = 0; | |
209 | int retry_reason = 0; | |
210 | SSL *ssl; | |
211 | BIO_SSL *bs; | |
212 | ||
213 | if (out == NULL) | |
214 | return (0); | |
215 | bs = (BIO_SSL *)b->ptr; | |
216 | ssl = bs->ssl; | |
217 | ||
218 | BIO_clear_retry_flags(b); | |
219 | ||
220 | /* | |
221 | * ret=SSL_do_handshake(ssl); if (ret > 0) | |
222 | */ | |
223 | ret = SSL_write(ssl, out, outl); | |
224 | ||
225 | switch (SSL_get_error(ssl, ret)) { | |
226 | case SSL_ERROR_NONE: | |
227 | if (ret <= 0) | |
228 | break; | |
229 | if (bs->renegotiate_count > 0) { | |
230 | bs->byte_count += ret; | |
231 | if (bs->byte_count > bs->renegotiate_count) { | |
232 | bs->byte_count = 0; | |
233 | bs->num_renegotiates++; | |
234 | SSL_renegotiate(ssl); | |
235 | r = 1; | |
236 | } | |
237 | } | |
238 | if ((bs->renegotiate_timeout > 0) && (!r)) { | |
239 | unsigned long tm; | |
240 | ||
241 | tm = (unsigned long)time(NULL); | |
242 | if (tm > bs->last_time + bs->renegotiate_timeout) { | |
243 | bs->last_time = tm; | |
244 | bs->num_renegotiates++; | |
245 | SSL_renegotiate(ssl); | |
246 | } | |
247 | } | |
248 | break; | |
249 | case SSL_ERROR_WANT_WRITE: | |
250 | BIO_set_retry_write(b); | |
251 | break; | |
252 | case SSL_ERROR_WANT_READ: | |
253 | BIO_set_retry_read(b); | |
254 | break; | |
255 | case SSL_ERROR_WANT_X509_LOOKUP: | |
256 | BIO_set_retry_special(b); | |
257 | retry_reason = BIO_RR_SSL_X509_LOOKUP; | |
258 | break; | |
259 | case SSL_ERROR_WANT_CONNECT: | |
260 | BIO_set_retry_special(b); | |
261 | retry_reason = BIO_RR_CONNECT; | |
262 | case SSL_ERROR_SYSCALL: | |
263 | case SSL_ERROR_SSL: | |
264 | default: | |
265 | break; | |
266 | } | |
267 | ||
268 | b->retry_reason = retry_reason; | |
269 | return (ret); | |
270 | } | |
d02b48c6 | 271 | |
0e1c0612 | 272 | static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) |
0f113f3e MC |
273 | { |
274 | SSL **sslp, *ssl; | |
275 | BIO_SSL *bs; | |
276 | BIO *dbio, *bio; | |
277 | long ret = 1; | |
278 | ||
279 | bs = (BIO_SSL *)b->ptr; | |
280 | ssl = bs->ssl; | |
281 | if ((ssl == NULL) && (cmd != BIO_C_SET_SSL)) | |
282 | return (0); | |
283 | switch (cmd) { | |
284 | case BIO_CTRL_RESET: | |
285 | SSL_shutdown(ssl); | |
286 | ||
287 | if (ssl->handshake_func == ssl->method->ssl_connect) | |
288 | SSL_set_connect_state(ssl); | |
289 | else if (ssl->handshake_func == ssl->method->ssl_accept) | |
290 | SSL_set_accept_state(ssl); | |
291 | ||
61986d32 | 292 | if (!SSL_clear(ssl)) { |
69f68237 MC |
293 | ret = 0; |
294 | break; | |
295 | } | |
0f113f3e MC |
296 | |
297 | if (b->next_bio != NULL) | |
298 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | |
299 | else if (ssl->rbio != NULL) | |
300 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); | |
301 | else | |
302 | ret = 1; | |
303 | break; | |
304 | case BIO_CTRL_INFO: | |
305 | ret = 0; | |
306 | break; | |
307 | case BIO_C_SSL_MODE: | |
308 | if (num) /* client mode */ | |
309 | SSL_set_connect_state(ssl); | |
310 | else | |
311 | SSL_set_accept_state(ssl); | |
312 | break; | |
313 | case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT: | |
314 | ret = bs->renegotiate_timeout; | |
315 | if (num < 60) | |
316 | num = 5; | |
317 | bs->renegotiate_timeout = (unsigned long)num; | |
318 | bs->last_time = (unsigned long)time(NULL); | |
319 | break; | |
320 | case BIO_C_SET_SSL_RENEGOTIATE_BYTES: | |
321 | ret = bs->renegotiate_count; | |
322 | if ((long)num >= 512) | |
323 | bs->renegotiate_count = (unsigned long)num; | |
324 | break; | |
325 | case BIO_C_GET_SSL_NUM_RENEGOTIATES: | |
326 | ret = bs->num_renegotiates; | |
327 | break; | |
328 | case BIO_C_SET_SSL: | |
329 | if (ssl != NULL) { | |
330 | ssl_free(b); | |
331 | if (!ssl_new(b)) | |
332 | return 0; | |
333 | } | |
334 | b->shutdown = (int)num; | |
335 | ssl = (SSL *)ptr; | |
336 | ((BIO_SSL *)b->ptr)->ssl = ssl; | |
337 | bio = SSL_get_rbio(ssl); | |
338 | if (bio != NULL) { | |
339 | if (b->next_bio != NULL) | |
340 | BIO_push(bio, b->next_bio); | |
341 | b->next_bio = bio; | |
342 | CRYPTO_add(&bio->references, 1, CRYPTO_LOCK_BIO); | |
343 | } | |
344 | b->init = 1; | |
345 | break; | |
346 | case BIO_C_GET_SSL: | |
347 | if (ptr != NULL) { | |
348 | sslp = (SSL **)ptr; | |
349 | *sslp = ssl; | |
350 | } else | |
351 | ret = 0; | |
352 | break; | |
353 | case BIO_CTRL_GET_CLOSE: | |
354 | ret = b->shutdown; | |
355 | break; | |
356 | case BIO_CTRL_SET_CLOSE: | |
357 | b->shutdown = (int)num; | |
358 | break; | |
359 | case BIO_CTRL_WPENDING: | |
360 | ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); | |
361 | break; | |
362 | case BIO_CTRL_PENDING: | |
363 | ret = SSL_pending(ssl); | |
364 | if (ret == 0) | |
365 | ret = BIO_pending(ssl->rbio); | |
366 | break; | |
367 | case BIO_CTRL_FLUSH: | |
368 | BIO_clear_retry_flags(b); | |
369 | ret = BIO_ctrl(ssl->wbio, cmd, num, ptr); | |
370 | BIO_copy_next_retry(b); | |
371 | break; | |
372 | case BIO_CTRL_PUSH: | |
373 | if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { | |
374 | SSL_set_bio(ssl, b->next_bio, b->next_bio); | |
375 | CRYPTO_add(&b->next_bio->references, 1, CRYPTO_LOCK_BIO); | |
376 | } | |
377 | break; | |
378 | case BIO_CTRL_POP: | |
379 | /* Only detach if we are the BIO explicitly being popped */ | |
380 | if (b == ptr) { | |
381 | /* | |
382 | * Shouldn't happen in practice because the rbio and wbio are the | |
383 | * same when pushed. | |
384 | */ | |
385 | if (ssl->rbio != ssl->wbio) | |
386 | BIO_free_all(ssl->wbio); | |
387 | if (b->next_bio != NULL) | |
388 | CRYPTO_add(&b->next_bio->references, -1, CRYPTO_LOCK_BIO); | |
389 | ssl->wbio = NULL; | |
390 | ssl->rbio = NULL; | |
391 | } | |
392 | break; | |
393 | case BIO_C_DO_STATE_MACHINE: | |
394 | BIO_clear_retry_flags(b); | |
395 | ||
396 | b->retry_reason = 0; | |
397 | ret = (int)SSL_do_handshake(ssl); | |
398 | ||
399 | switch (SSL_get_error(ssl, (int)ret)) { | |
400 | case SSL_ERROR_WANT_READ: | |
401 | BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); | |
402 | break; | |
403 | case SSL_ERROR_WANT_WRITE: | |
404 | BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); | |
405 | break; | |
406 | case SSL_ERROR_WANT_CONNECT: | |
407 | BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); | |
408 | b->retry_reason = b->next_bio->retry_reason; | |
409 | break; | |
410 | default: | |
411 | break; | |
412 | } | |
413 | break; | |
414 | case BIO_CTRL_DUP: | |
415 | dbio = (BIO *)ptr; | |
62adbcee | 416 | SSL_free(((BIO_SSL *)dbio->ptr)->ssl); |
0f113f3e MC |
417 | ((BIO_SSL *)dbio->ptr)->ssl = SSL_dup(ssl); |
418 | ((BIO_SSL *)dbio->ptr)->renegotiate_count = | |
419 | ((BIO_SSL *)b->ptr)->renegotiate_count; | |
420 | ((BIO_SSL *)dbio->ptr)->byte_count = ((BIO_SSL *)b->ptr)->byte_count; | |
421 | ((BIO_SSL *)dbio->ptr)->renegotiate_timeout = | |
422 | ((BIO_SSL *)b->ptr)->renegotiate_timeout; | |
423 | ((BIO_SSL *)dbio->ptr)->last_time = ((BIO_SSL *)b->ptr)->last_time; | |
424 | ret = (((BIO_SSL *)dbio->ptr)->ssl != NULL); | |
425 | break; | |
426 | case BIO_C_GET_FD: | |
427 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); | |
428 | break; | |
429 | case BIO_CTRL_SET_CALLBACK: | |
430 | { | |
431 | #if 0 /* FIXME: Should this be used? -- Richard | |
432 | * Levitte */ | |
433 | SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | |
434 | ret = -1; | |
d3442bc7 | 435 | #else |
0f113f3e | 436 | ret = 0; |
d3442bc7 | 437 | #endif |
0f113f3e MC |
438 | } |
439 | break; | |
440 | case BIO_CTRL_GET_CALLBACK: | |
441 | { | |
442 | void (**fptr) (const SSL *xssl, int type, int val); | |
443 | ||
444 | fptr = (void (**)(const SSL *xssl, int type, int val))ptr; | |
445 | *fptr = SSL_get_info_callback(ssl); | |
446 | } | |
447 | break; | |
448 | default: | |
449 | ret = BIO_ctrl(ssl->rbio, cmd, num, ptr); | |
450 | break; | |
451 | } | |
452 | return (ret); | |
453 | } | |
d02b48c6 | 454 | |
13083215 | 455 | static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) |
0f113f3e MC |
456 | { |
457 | SSL *ssl; | |
458 | BIO_SSL *bs; | |
459 | long ret = 1; | |
460 | ||
461 | bs = (BIO_SSL *)b->ptr; | |
462 | ssl = bs->ssl; | |
463 | switch (cmd) { | |
464 | case BIO_CTRL_SET_CALLBACK: | |
465 | { | |
466 | /* | |
467 | * FIXME: setting this via a completely different prototype seems | |
468 | * like a crap idea | |
469 | */ | |
470 | SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp); | |
471 | } | |
472 | break; | |
473 | default: | |
474 | ret = BIO_callback_ctrl(ssl->rbio, cmd, fp); | |
475 | break; | |
476 | } | |
477 | return (ret); | |
478 | } | |
d3442bc7 | 479 | |
0e1c0612 | 480 | static int ssl_puts(BIO *bp, const char *str) |
0f113f3e MC |
481 | { |
482 | int n, ret; | |
d02b48c6 | 483 | |
0f113f3e MC |
484 | n = strlen(str); |
485 | ret = BIO_write(bp, str, n); | |
486 | return (ret); | |
487 | } | |
d02b48c6 | 488 | |
6b691a5c | 489 | BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx) |
0f113f3e | 490 | { |
85d686e7 | 491 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
492 | BIO *ret = NULL, *buf = NULL, *ssl = NULL; |
493 | ||
494 | if ((buf = BIO_new(BIO_f_buffer())) == NULL) | |
495 | return (NULL); | |
496 | if ((ssl = BIO_new_ssl_connect(ctx)) == NULL) | |
497 | goto err; | |
498 | if ((ret = BIO_push(buf, ssl)) == NULL) | |
499 | goto err; | |
500 | return (ret); | |
501 | err: | |
ca3a82c3 RS |
502 | BIO_free(buf); |
503 | BIO_free(ssl); | |
85d686e7 | 504 | #endif |
0f113f3e MC |
505 | return (NULL); |
506 | } | |
58964a49 | 507 | |
6b691a5c | 508 | BIO *BIO_new_ssl_connect(SSL_CTX *ctx) |
0f113f3e | 509 | { |
4a1fbd13 | 510 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
511 | BIO *ret = NULL, *con = NULL, *ssl = NULL; |
512 | ||
513 | if ((con = BIO_new(BIO_s_connect())) == NULL) | |
514 | return (NULL); | |
515 | if ((ssl = BIO_new_ssl(ctx, 1)) == NULL) | |
516 | goto err; | |
517 | if ((ret = BIO_push(ssl, con)) == NULL) | |
518 | goto err; | |
519 | return (ret); | |
520 | err: | |
ca3a82c3 | 521 | BIO_free(con); |
4a1fbd13 | 522 | #endif |
0f113f3e MC |
523 | return (NULL); |
524 | } | |
58964a49 | 525 | |
6b691a5c | 526 | BIO *BIO_new_ssl(SSL_CTX *ctx, int client) |
0f113f3e MC |
527 | { |
528 | BIO *ret; | |
529 | SSL *ssl; | |
530 | ||
531 | if ((ret = BIO_new(BIO_f_ssl())) == NULL) | |
532 | return (NULL); | |
533 | if ((ssl = SSL_new(ctx)) == NULL) { | |
534 | BIO_free(ret); | |
535 | return (NULL); | |
536 | } | |
537 | if (client) | |
538 | SSL_set_connect_state(ssl); | |
539 | else | |
540 | SSL_set_accept_state(ssl); | |
541 | ||
542 | BIO_set_ssl(ret, ssl, BIO_CLOSE); | |
543 | return (ret); | |
544 | } | |
d02b48c6 | 545 | |
6b691a5c | 546 | int BIO_ssl_copy_session_id(BIO *t, BIO *f) |
0f113f3e MC |
547 | { |
548 | t = BIO_find_type(t, BIO_TYPE_SSL); | |
549 | f = BIO_find_type(f, BIO_TYPE_SSL); | |
550 | if ((t == NULL) || (f == NULL)) | |
551 | return (0); | |
552 | if ((((BIO_SSL *)t->ptr)->ssl == NULL) || | |
553 | (((BIO_SSL *)f->ptr)->ssl == NULL)) | |
554 | return (0); | |
61986d32 | 555 | if (!SSL_copy_session_id(((BIO_SSL *)t->ptr)->ssl, ((BIO_SSL *)f->ptr)->ssl)) |
17dd65e6 | 556 | return 0; |
0f113f3e MC |
557 | return (1); |
558 | } | |
d02b48c6 | 559 | |
6b691a5c | 560 | void BIO_ssl_shutdown(BIO *b) |
0f113f3e MC |
561 | { |
562 | SSL *s; | |
563 | ||
564 | while (b != NULL) { | |
565 | if (b->method->type == BIO_TYPE_SSL) { | |
566 | s = ((BIO_SSL *)b->ptr)->ssl; | |
567 | SSL_shutdown(s); | |
568 | break; | |
569 | } | |
570 | b = b->next_bio; | |
571 | } | |
572 | } |