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